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

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 30 08:00:43 PST 2022


sammccall created this revision.
sammccall added a reviewer: kadircet.
Herald added a project: All.
sammccall requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139018

Files:
  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


Index: clang-tools-extra/include-cleaner/lib/Types.cpp
===================================================================
--- clang-tools-extra/include-cleaner/lib/Types.cpp
+++ clang-tools-extra/include-cleaner/lib/Types.cpp
@@ -39,7 +39,7 @@
 }
 
 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_unreachable("Unexpected RefType");
 }
 
+std::string Include::quote() const {
+  return (llvm::StringRef(Angled ? "<" : "\"") + Spelled +
+          (Angled ? ">" : "\""))
+      .str();
+}
 } // namespace clang::include_cleaner
Index: clang-tools-extra/include-cleaner/lib/Record.cpp
===================================================================
--- clang-tools-extra/include-cleaner/lib/Record.cpp
+++ clang-tools-extra/include-cleaner/lib/Record.cpp
@@ -46,6 +46,7 @@
     I.Resolved = File ? &File->getFileEntry() : nullptr;
     I.Line = SM.getSpellingLineNumber(Hash);
     I.Spelled = SpelledFilename;
+    I.Angled = IsAngled;
     Recorded.Includes.add(I);
   }
 
Index: clang-tools-extra/include-cleaner/lib/HTMLReport.cpp
===================================================================
--- clang-tools-extra/include-cleaner/lib/HTMLReport.cpp
+++ clang-tools-extra/include-cleaner/lib/HTMLReport.cpp
@@ -416,7 +416,7 @@
 
     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>";
     }
Index: clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
===================================================================
--- clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
+++ clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
@@ -133,6 +133,8 @@
                                        // 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 &);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139018.478959.patch
Type: text/x-patch
Size: 2493 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221130/b517effd/attachment.bin>


More information about the cfe-commits mailing list