[clang-tools-extra] 8354622 - [include-cleaner] Ignore builtin symbols in the WalkAST.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 30 04:47:13 PDT 2023
Author: Haojian Wu
Date: 2023-03-30T13:46:41+02:00
New Revision: 83546221af43ca347f8e1b1197a9df54810244e0
URL: https://github.com/llvm/llvm-project/commit/83546221af43ca347f8e1b1197a9df54810244e0
DIFF: https://github.com/llvm/llvm-project/commit/83546221af43ca347f8e1b1197a9df54810244e0.diff
LOG: [include-cleaner] Ignore builtin symbols in the WalkAST.
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.
Differential Revision: https://reviews.llvm.org/D147213
Added:
Modified:
clang-tools-extra/include-cleaner/lib/WalkAST.cpp
clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
index 0db1f2bec8e6b..b10c722b6653a 100644
--- a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
+++ b/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 @@ class ASTWalker : public RecursiveASTVisitor<ASTWalker> {
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);
}
diff --git a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
index 8ce556f5fbf1f..fd6c6da9d5509 100644
--- a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
@@ -322,5 +322,11 @@ TEST(WalkAST, Enums) {
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
More information about the cfe-commits
mailing list