Skip to content

Commit

Permalink
[include-cleaner] Record whether includes are spelled with <angle> qu…
Browse files Browse the repository at this point in the history
…otes

This is needed to accurately remove headers with tooling::IncludeHeaders in the
rare cases where <foo> and "foo" resolve to something different.

This is also nice to have in HTML report and command-line -print=changes output.

Differential Revision: https://reviews.llvm.org/D139018
  • Loading branch information
sam-mccall committed Nov 30, 2022
1 parent d8a27ac commit 9961fa1
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 3 deletions.
Expand Up @@ -133,6 +133,8 @@ struct Include {
// nullptr if the header was not found
SourceLocation HashLocation; // of hash in #include <vector>
unsigned Line = 0; // 1-based line number for #include
bool Angled = false; // True if spelled with <angle> quotes.
std::string quote() const; // e.g. <vector>
};
llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Include &);

Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/include-cleaner/lib/HTMLReport.cpp
Expand Up @@ -416,7 +416,7 @@ class Reporter {

for (const auto *I : R.Includes) {
OS << "<tr><th>Included</th><td>";
escapeString(I->Spelled);
escapeString(I->quote());
OS << ", <a href='#line" << I->Line << "'>line " << I->Line << "</a>";
OS << "</td></tr>";
}
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/include-cleaner/lib/Record.cpp
Expand Up @@ -46,6 +46,7 @@ class PPRecorder : public PPCallbacks {
I.Resolved = File ? &File->getFileEntry() : nullptr;
I.Line = SM.getSpellingLineNumber(Hash);
I.Spelled = SpelledFilename;
I.Angled = IsAngled;
Recorded.Includes.add(I);
}

Expand Down
7 changes: 6 additions & 1 deletion clang-tools-extra/include-cleaner/lib/Types.cpp
Expand Up @@ -39,7 +39,7 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Header &H) {
}

llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Include &I) {
return OS << I.Line << ": " << I.Spelled << " => "
return OS << I.Line << ": " << I.quote() << " => "
<< (I.Resolved ? I.Resolved->getName() : "<missing>");
}

Expand All @@ -64,4 +64,9 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, RefType T) {
llvm_unreachable("Unexpected RefType");
}

std::string Include::quote() const {
return (llvm::StringRef(Angled ? "<" : "\"") + Spelled +
(Angled ? ">" : "\""))
.str();
}
} // namespace clang::include_cleaner
4 changes: 3 additions & 1 deletion clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
Expand Up @@ -134,7 +134,7 @@ MATCHER_P(spelled, S, "") { return arg.Spelled == S; }
TEST_F(RecordPPTest, CapturesIncludes) {
llvm::Annotations MainFile(R"cpp(
$H^#include "./header.h"
$M^#include "missing.h"
$M^#include <missing.h>
)cpp");
Inputs.Code = MainFile.code();
Inputs.ExtraFiles["header.h"] = "";
Expand All @@ -151,13 +151,15 @@ TEST_F(RecordPPTest, CapturesIncludes) {
AST.sourceManager().getComposedLoc(
AST.sourceManager().getMainFileID(), MainFile.point("H")));
EXPECT_EQ(H.Resolved, AST.fileManager().getFile("header.h").get());
EXPECT_FALSE(H.Angled);

auto &M = Recorded.Includes.all().back();
EXPECT_EQ(M.Line, 3u);
EXPECT_EQ(M.HashLocation,
AST.sourceManager().getComposedLoc(
AST.sourceManager().getMainFileID(), MainFile.point("M")));
EXPECT_EQ(M.Resolved, nullptr);
EXPECT_TRUE(M.Angled);
}

TEST_F(RecordPPTest, CapturesMacroRefs) {
Expand Down

0 comments on commit 9961fa1

Please sign in to comment.