[PATCH] D147213: [include-cleaner] Ignore builtin symbols in the WalkAST.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 30 03:49:20 PDT 2023


hokein created this revision.
hokein added a reviewer: kadircet.
Herald added a project: All.
hokein requested review of this revision.
Herald added a project: clang-tools-extra.

There is no need to add headers for builtin symbols.

Additionally, there is a bonus benefit which help eliminate some bugs -- builtin
functions are modeled as implicit FunctionDecls in the clang AST, which results in
them being treated as normal FunctionDecls in the implementation of the include-cleaner
(going through the path: ast-node -> decl -> source location -> header).
And, the source location of these built-in symbols' AST nodes is not precise (e.g. points to the first call site),
which leads to subtle behavior that inserts a header of the call site.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D147213

Files:
  clang-tools-extra/include-cleaner/lib/WalkAST.cpp
  clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp


Index: clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
===================================================================
--- clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
@@ -322,5 +322,11 @@
   testWalk("enum class E : int {};", "enum class ^E : int ;");
 }
 
+TEST(WalkAST, BuiltinSymbols) {
+  testWalk(R"cpp(
+    extern "C" int __builtin_popcount(unsigned int) noexcept;
+  )cpp", "int x = ^__builtin_popcount(1);");
+}
+
 } // namespace
 } // namespace clang::include_cleaner
Index: clang-tools-extra/include-cleaner/lib/WalkAST.cpp
===================================================================
--- clang-tools-extra/include-cleaner/lib/WalkAST.cpp
+++ clang-tools-extra/include-cleaner/lib/WalkAST.cpp
@@ -17,6 +17,7 @@
 #include "clang/AST/TemplateName.h"
 #include "clang/AST/Type.h"
 #include "clang/AST/TypeLoc.h"
+#include "clang/Basic/IdentifierTable.h"
 #include "clang/Basic/SourceLocation.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/STLFunctionalExtras.h"
@@ -34,6 +35,9 @@
               RefType RT = RefType::Explicit) {
     if (!ND || Loc.isInvalid())
       return;
+    // Don't report builtin symbols.
+    if (const auto *II = ND->getIdentifier(); II && II->getBuiltinID() > 0)
+      return;
     Callback(Loc, *cast<NamedDecl>(ND->getCanonicalDecl()), RT);
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147213.509601.patch
Type: text/x-patch
Size: 1400 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230330/293ea1ab/attachment-0001.bin>


More information about the cfe-commits mailing list