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

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 19 06:46:33 PDT 2024


================
@@ -39,20 +40,24 @@ 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)
----------------
HerrCai0907 wrote:

> telling apart a "physical" stdlib header (e.g. <__utility/pair.h>) from a user header (e.g. <my_stdlib/pair.h>) isn't possible. hence when something goes wrong, we'll always "insert" the wrong header (implementation detail, rather than public header for a stdlib symbol).

I aggree it is not possible to identifier the user-defined stdlib with stdlib. But it is possible to identifier user-defined function with stdlib function which may have totally difference meanings. e.g. `log` to print something and `log` in `<cmath>` 

We may distinguish them by quota include or angle brackets? Since normally c++ developer will only use `#include <>` for system header file but `include ""` for the other file.


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


More information about the cfe-commits mailing list