[clang-tools-extra] 9961fa1 - [include-cleaner] Record whether includes are spelled with <angle> quotes

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 30 09:14:21 PST 2022


Author: Sam McCall
Date: 2022-11-30T18:13:10+01:00
New Revision: 9961fa1653a2e05cb729252a6c430558446868b6

URL: https://github.com/llvm/llvm-project/commit/9961fa1653a2e05cb729252a6c430558446868b6
DIFF: https://github.com/llvm/llvm-project/commit/9961fa1653a2e05cb729252a6c430558446868b6.diff

LOG: [include-cleaner] Record whether includes are spelled with <angle> quotes

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

Added: 
    

Modified: 
    clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
    clang-tools-extra/include-cleaner/lib/HTMLReport.cpp
    clang-tools-extra/include-cleaner/lib/Record.cpp
    clang-tools-extra/include-cleaner/lib/Types.cpp
    clang-tools-extra/include-cleaner/unittests/RecordTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h b/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
index 405ded21561c..03742f5efe67 100644
--- a/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
+++ b/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
@@ -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 &);
 

diff  --git a/clang-tools-extra/include-cleaner/lib/HTMLReport.cpp b/clang-tools-extra/include-cleaner/lib/HTMLReport.cpp
index cd43a27e5fc5..ac9dfc19b3e3 100644
--- a/clang-tools-extra/include-cleaner/lib/HTMLReport.cpp
+++ b/clang-tools-extra/include-cleaner/lib/HTMLReport.cpp
@@ -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>";
     }

diff  --git a/clang-tools-extra/include-cleaner/lib/Record.cpp b/clang-tools-extra/include-cleaner/lib/Record.cpp
index 5a9ec6d0aadc..82cb2d646aa3 100644
--- a/clang-tools-extra/include-cleaner/lib/Record.cpp
+++ b/clang-tools-extra/include-cleaner/lib/Record.cpp
@@ -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);
   }
 

diff  --git a/clang-tools-extra/include-cleaner/lib/Types.cpp b/clang-tools-extra/include-cleaner/lib/Types.cpp
index 0aa527946bb0..7ea6c6942983 100644
--- a/clang-tools-extra/include-cleaner/lib/Types.cpp
+++ b/clang-tools-extra/include-cleaner/lib/Types.cpp
@@ -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>");
 }
 
@@ -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

diff  --git a/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp b/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
index 107689147343..a10b5f43f9d0 100644
--- a/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
@@ -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"] = "";
@@ -151,6 +151,7 @@ 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);
@@ -158,6 +159,7 @@ TEST_F(RecordPPTest, CapturesIncludes) {
             AST.sourceManager().getComposedLoc(
                 AST.sourceManager().getMainFileID(), MainFile.point("M")));
   EXPECT_EQ(M.Resolved, nullptr);
+  EXPECT_TRUE(M.Angled);
 }
 
 TEST_F(RecordPPTest, CapturesMacroRefs) {


        


More information about the cfe-commits mailing list