[clang-tools-extra] 961e32c - [include-cleaner] Check macros against stdlib database
Kadir Cetinkaya via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 23 00:37:05 PST 2023
Author: Kadir Cetinkaya
Date: 2023-02-23T09:34:37+01:00
New Revision: 961e32c587cb2289e8c2873848b660a153ed5618
URL: https://github.com/llvm/llvm-project/commit/961e32c587cb2289e8c2873848b660a153ed5618
DIFF: https://github.com/llvm/llvm-project/commit/961e32c587cb2289e8c2873848b660a153ed5618.diff
LOG: [include-cleaner] Check macros against stdlib database
Differential Revision: https://reviews.llvm.org/D144579
Added:
Modified:
clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp
clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp b/clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp
index 60b18c2fe94fb..78e783a62eb27 100644
--- a/clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp
+++ b/clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "AnalysisInternal.h"
+#include "clang-include-cleaner/Types.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclBase.h"
#include "clang/AST/DeclCXX.h"
@@ -53,6 +54,12 @@ std::vector<Hinted<SymbolLocation>> locateDecl(const Decl &D) {
return Result;
}
+std::vector<Hinted<SymbolLocation>> locateMacro(const Macro &M) {
+ // FIXME: Should we also provide physical locations?
+ if (auto SS = tooling::stdlib::Symbol::named("", M.Name->getName()))
+ return {{*SS, Hints::CompleteSymbol}};
+ return {{M.Definition, Hints::CompleteSymbol}};
+}
} // namespace
std::vector<Hinted<SymbolLocation>> locateSymbol(const Symbol &S) {
@@ -60,7 +67,7 @@ std::vector<Hinted<SymbolLocation>> locateSymbol(const Symbol &S) {
case Symbol::Declaration:
return locateDecl(S.declaration());
case Symbol::Macro:
- return {{S.macro().Definition, Hints::CompleteSymbol}};
+ return locateMacro(S.macro());
}
llvm_unreachable("Unknown Symbol::Kind enum");
}
diff --git a/clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp b/clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp
index 16a185269b5d9..9961b013b24d8 100644
--- a/clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp
@@ -122,9 +122,17 @@ TEST(LocateSymbol, Decl) {
}
TEST(LocateSymbol, Stdlib) {
- LocateExample Test("namespace std { struct vector; }");
- EXPECT_THAT(locateSymbol(Test.findDecl("vector")),
- ElementsAre(*tooling::stdlib::Symbol::named("std::", "vector")));
+ {
+ LocateExample Test("namespace std { struct vector; }");
+ EXPECT_THAT(
+ locateSymbol(Test.findDecl("vector")),
+ ElementsAre(*tooling::stdlib::Symbol::named("std::", "vector")));
+ }
+ {
+ LocateExample Test("#define assert(x)\nvoid foo() { assert(true); }");
+ EXPECT_THAT(locateSymbol(Test.findMacro("assert")),
+ ElementsAre(*tooling::stdlib::Symbol::named("", "assert")));
+ }
}
TEST(LocateSymbol, Macros) {
More information about the cfe-commits
mailing list