[include-cleaner] Don't apply the PreferredHeader hint for standard headers.
authorHaojian Wu <hokein.wu@gmail.com>
Tue, 13 Jun 2023 11:42:36 +0000 (13:42 +0200)
committerHaojian Wu <hokein.wu@gmail.com>
Tue, 13 Jun 2023 16:57:45 +0000 (18:57 +0200)
Fixes https://github.com/llvm/llvm-project/issues/62635

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

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

index 1b705e1..1eff19c 100644 (file)
@@ -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;
   }
index 2efdc7e..964f4c6 100644 (file)
@@ -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