[clang-tools-extra] 48967c6 - [include-cleaner] Don't apply the PreferredHeader hint for standard headers.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 13 10:00:11 PDT 2023


Author: Haojian Wu
Date: 2023-06-13T18:57:45+02:00
New Revision: 48967c6e5504c7bcb3f7a55fc3894760c62cef53

URL: https://github.com/llvm/llvm-project/commit/48967c6e5504c7bcb3f7a55fc3894760c62cef53
DIFF: https://github.com/llvm/llvm-project/commit/48967c6e5504c7bcb3f7a55fc3894760c62cef53.diff

LOG: [include-cleaner] Don't apply the PreferredHeader hint for standard headers.

Fixes https://github.com/llvm/llvm-project/issues/62635

Differential Revision: https://reviews.llvm.org/D152801

Added: 
    

Modified: 
    clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
    clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/include-cleaner/lib/FindHeaders.cpp b/clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
index 1b705e1d52c2d..1eff19cb44445 100644
--- a/clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
+++ b/clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
@@ -248,6 +248,10 @@ llvm::SmallVector<Header> headersForSymbol(const Symbol &S,
   // Add name match hints to deduplicated providers.
   llvm::StringRef SymbolName = symbolName(S);
   for (auto &H : Headers) {
+    // Don't apply name match hints to standard headers as the standard headers
+    // are already ranked in the stdlib mapping.
+    if (H.kind() == Header::Standard)
+      continue;
     if (nameMatch(SymbolName, H))
       H.Hint |= Hints::PreferredHeader;
   }

diff  --git a/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp b/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
index 2efdc7ebbb784..964f4c6c9190d 100644
--- a/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
@@ -486,5 +486,16 @@ TEST_F(HeadersForSymbolTest, AmbiguousStdSymbols) {
   }
 }
 
+TEST_F(HeadersForSymbolTest, StandardHeaders) {
+  Inputs.Code = "void assert();";
+  buildAST();
+  EXPECT_THAT(
+      headersFor("assert"),
+      // Respect the ordering from the stdlib mapping.
+      UnorderedElementsAre(tooling::stdlib::Header::named("<cassert>"),
+                           tooling::stdlib::Header::named("<assert.h>")));
+}
+
+
 } // namespace
 } // namespace clang::include_cleaner


        


More information about the cfe-commits mailing list