Debian Codegraph @

llvm-project/clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp

raw
1 //===--- LocateSymbolTest.cpp -------------------------------------- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #include "AnalysisInternal.h"
9 #include "TypesInternal.h"
10 #include "clang-include-cleaner/Types.h"
11 #include "clang/AST/Decl.h"
12 #include "clang/AST/DeclBase.h"
13 #include "clang/AST/RecursiveASTVisitor.h"
14 #include "clang/Basic/SourceLocation.h"
15 #include "clang/Lex/Preprocessor.h"
16 #include "clang/Testing/TestAST.h"
17 #include "clang/Tooling/Inclusions/StandardLibrary.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Testing/Annotations/Annotations.h"
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 #include <cstddef>
23 #include <memory>
24 #include <tuple>
25 #include <unordered_map>
26 #include <utility>
27 #include <variant>
28 #include <vector>
29
30 namespace clang::include_cleaner {
31 namespace {
32 using testing::Each;
33 using testing::ElementsAre;
34 using testing::ElementsAreArray;
35 using testing::Eq;
36 using testing::Field;
37 using testing::Pair;
38 using testing::UnorderedElementsAre;
39
40 // A helper for building ASTs and getting decls out of it by name. Example usage
41 // looks like:
42 // LocateExample X("void ^foo();");
43 // Decl &Foo = X.findDecl("foo");
44 // X.points(); // returns all the points in annotated test input.
45 struct LocateExample {
46 private:
47 llvm::Annotations Target;
48 TestAST AST;
49
50 public:
51 LocateExample(llvm::StringRef AnnotatedCode)
52 : Target(AnnotatedCode), AST([this] {
53 TestInputs Inputs(Target.code());
54 Inputs.ExtraArgs.push_back("-std=c++17");
55 return Inputs;
56 }()) {}
57
58 const Decl &findDecl(llvm::StringRef SymbolName) {
59 struct Visitor : RecursiveASTVisitor<Visitor> {
60 llvm::StringRef NameToFind;
61 const NamedDecl *Out = nullptr;
62 bool VisitNamedDecl(const NamedDecl *ND) {
63 // Skip the templated decls, as they have the same name and matches in
64 // this file care about the outer template name.
65 if (auto *TD = ND->getDescribedTemplate())
66 ND = TD;
67 if (ND->getName() == NameToFind) {
68 EXPECT_TRUE(Out == nullptr || Out == ND->getCanonicalDecl())
69 << "Found multiple matches for " << NameToFind;
70 Out = cast<NamedDecl>(ND->getCanonicalDecl());
71 }
72 return true;
73 }
74 };
75 Visitor V;
76 V.NameToFind = SymbolName;
77 V.TraverseDecl(AST.context().getTranslationUnitDecl());
78 if (!V.Out)
79 ADD_FAILURE() << "Couldn't find any decls with name: " << SymbolName;
80 assert(V.Out);
81 return *V.Out;
82 }
83
84 Macro findMacro(llvm::StringRef Name) {
85 auto &PP = AST.preprocessor();
86 auto *II = PP.getIdentifierInfo(Name);
87 if (!II || !II->hasMacroDefinition()) {
88 ADD_FAILURE() << "Couldn't find any macros with name: " << Name;
89 return {};
90 }
91 auto MD = PP.getMacroDefinition(II);
92 assert(MD.getMacroInfo());
93 return {II, MD.getMacroInfo()->getDefinitionLoc()};
94 }
95
96 std::vector<SymbolLocation> points() {
97 auto &SM = AST.sourceManager();
98 auto FID = SM.getMainFileID();
99 auto Offsets = Target.points();
100 std::vector<SymbolLocation> Results;
101 for (auto &Offset : Offsets)
102 Results.emplace_back(SM.getComposedLoc(FID, Offset));
103 return Results;
104 }
105 };
106
107 TEST(LocateSymbol, Decl) {
108 // Looks for decl with name 'foo' and performs locateSymbol on it.
109 // Expects all the locations in the case to be returned as a location.
110 const llvm::StringLiteral Cases[] = {
111 "struct ^foo; struct ^foo {};",
112 "namespace ns { void ^foo(); void ^foo() {} }",
113 "enum class ^foo; enum class ^foo {};",
114 };
115
116 for (auto &Case : Cases) {
117 SCOPED_TRACE(Case);
118 LocateExample Test(Case);
119 EXPECT_THAT(locateSymbol(Test.findDecl("foo")),
120 ElementsAreArray(Test.points()));
121 }
122 }
123
124 TEST(LocateSymbol, Stdlib) {
125 LocateExample Test("namespace std { struct vector; }");
126 EXPECT_THAT(locateSymbol(Test.findDecl("vector")),
127 ElementsAre(*tooling::stdlib::Symbol::named("std::", "vector")));
128 }
129
130 TEST(LocateSymbol, Macros) {
131 // Make sure we preserve the last one.
132 LocateExample Test("#define FOO\n#undef FOO\n#define ^FOO");
133 EXPECT_THAT(locateSymbol(Test.findMacro("FOO")),
134 ElementsAreArray(Test.points()));
135 }
136
137 MATCHER_P2(HintedSymbol, Symbol, Hint, "") {
138 return std::tie(arg.Hint, arg) == std::tie(Hint, Symbol);
139 }
140 TEST(LocateSymbol, CompleteSymbolHint) {
141 {
142 // stdlib symbols are always complete.
143 LocateExample Test("namespace std { struct vector; }");
144 EXPECT_THAT(locateSymbol(Test.findDecl("vector")),
145 ElementsAre(HintedSymbol(
146 *tooling::stdlib::Symbol::named("std::", "vector"),
147 Hints::CompleteSymbol)));
148 }
149 {
150 // macros are always complete.
151 LocateExample Test("#define ^FOO");
152 EXPECT_THAT(locateSymbol(Test.findMacro("FOO")),
153 ElementsAre(HintedSymbol(Test.points().front(),
154 Hints::CompleteSymbol)));
155 }
156 {
157 // Completeness is only absent in cases that matters.
158 const llvm::StringLiteral Cases[] = {
159 "struct ^foo; struct ^foo {};",
160 "template <typename> struct ^foo; template <typename> struct ^foo {};",
161 "template <typename> void ^foo(); template <typename> void ^foo() {};",
162 };
163 for (auto &Case : Cases) {
164 SCOPED_TRACE(Case);
165 LocateExample Test(Case);
166 EXPECT_THAT(locateSymbol(Test.findDecl("foo")),
167 ElementsAre(HintedSymbol(Test.points().front(), Hints::None),
168 HintedSymbol(Test.points().back(),
169 Hints::CompleteSymbol)));
170 }
171 }
172 {
173 // All declarations should be marked as complete in cases that a definition
174 // is not usually needed.
175 const llvm::StringLiteral Cases[] = {
176 "void foo(); void foo() {}",
177 "extern int foo; int foo;",
178 };
179 for (auto &Case : Cases) {
180 SCOPED_TRACE(Case);
181 LocateExample Test(Case);
182 EXPECT_THAT(locateSymbol(Test.findDecl("foo")),
183 Each(Field(&Hinted<SymbolLocation>::Hint,
184 Eq(Hints::CompleteSymbol))));
185 }
186 }
187 }
188
189 } // namespace
190 } // namespace clang::include_cleaner