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); }
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); }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits