[clang-tools-extra] [clang][include-cleaner]skip stdlib recogntion only when there are defintion with body in main file. (PR #95797)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 17 07:46:47 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tools-extra

Author: Congcong Cai (HerrCai0907)

<details>
<summary>Changes</summary>

When a definition is being located, standard library function recognition is skipped if the function has a definition with body in the main file


---
Full diff: https://github.com/llvm/llvm-project/pull/95797.diff


2 Files Affected:

- (modified) clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp (+12-7) 
- (modified) clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp (+3-6) 


``````````diff
diff --git a/clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp b/clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp
index 9148d36a5038f..2ddc807871af1 100644
--- a/clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp
+++ b/clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp
@@ -13,6 +13,7 @@
 #include "clang/AST/DeclCXX.h"
 #include "clang/AST/DeclTemplate.h"
 #include "clang/Tooling/Inclusions/StandardLibrary.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/raw_ostream.h"
 #include <utility>
@@ -39,13 +40,16 @@ Hints declHints(const Decl *D) {
 }
 
 std::vector<Hinted<SymbolLocation>> locateDecl(const Decl &D) {
-  std::vector<Hinted<SymbolLocation>> Result;
-  // FIXME: Should we also provide physical locations?
-  if (auto SS = tooling::stdlib::Recognizer()(&D)) {
-    Result.push_back({*SS, Hints::CompleteSymbol});
-    if (!D.hasBody())
-      return Result;
-  }
+  SourceManager &SM = D.getASTContext().getSourceManager();
+  bool HasBodyInMainFile = llvm::any_of(D.redecls(), [&](Decl *Redecl) {
+    return Redecl->hasBody() && SM.isInMainFile(Redecl->getLocation());
+  });
+  // if decl has body and in main file, we don't need to do further search.
+  if (!HasBodyInMainFile)
+    // FIXME: Should we also provide physical locations?
+    if (auto SS = tooling::stdlib::Recognizer()(&D))
+      return {{*SS, Hints::CompleteSymbol}};
+
   // FIXME: Signal foreign decls, e.g. a forward declaration not owned by a
   // library. Some useful signals could be derived by checking the DeclContext.
   // Most incidental forward decls look like:
@@ -53,6 +57,7 @@ std::vector<Hinted<SymbolLocation>> locateDecl(const Decl &D) {
   //   class SourceManager; // likely an incidental forward decl.
   //   namespace my_own_ns {}
   //   }
+  std::vector<Hinted<SymbolLocation>> Result;
   for (auto *Redecl : D.redecls())
     Result.push_back({Redecl->getLocation(), declHints(Redecl)});
   return Result;
diff --git a/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp b/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
index fdcbf25fd628c..f1eadd113dead 100644
--- a/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
@@ -631,12 +631,9 @@ TEST_F(HeadersForSymbolTest, StandardHeaders) {
 TEST_F(HeadersForSymbolTest, NonStandardHeaders) {
   Inputs.Code = "void assert() {}";
   buildAST();
-  EXPECT_THAT(
-      headersFor("assert"),
-      // Respect the ordering from the stdlib mapping.
-      UnorderedElementsAre(physicalHeader("input.mm"),
-                           tooling::stdlib::Header::named("<cassert>"),
-                           tooling::stdlib::Header::named("<assert.h>")));
+  EXPECT_THAT(headersFor("assert"),
+              // Respect the ordering from the stdlib mapping.
+              UnorderedElementsAre(physicalHeader("input.mm")));
 }
 
 TEST_F(HeadersForSymbolTest, ExporterNoNameMatch) {

``````````

</details>


https://github.com/llvm/llvm-project/pull/95797


More information about the cfe-commits mailing list