| 1 | //===--- WalkASTTest.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 "clang/AST/ASTContext.h" |
| 10 | #include "clang/Basic/FileManager.h" |
| 11 | #include "clang/Basic/SourceLocation.h" |
| 12 | #include "clang/Frontend/TextDiagnostic.h" |
| 13 | #include "clang/Testing/TestAST.h" |
| 14 | #include "llvm/ADT/ArrayRef.h" |
| 15 | #include "llvm/ADT/StringRef.h" |
| 16 | #include "llvm/Support/Error.h" |
| 17 | #include "llvm/Support/ScopedPrinter.h" |
| 18 | #include "llvm/Testing/Annotations/Annotations.h" |
| 19 | #include "gtest/gtest.h" |
| 20 | #include <cstddef> |
| 21 | #include <unordered_map> |
| 22 | #include <utility> |
| 23 | #include <vector> |
| 24 | |
| 25 | namespace clang::include_cleaner { |
| 26 | namespace { |
| 27 | |
| 28 | // Specifies a test of which symbols are referenced by a piece of code. |
| 29 | // Target should contain points annotated with the reference kind. |
| 30 | // Example: |
| 31 | // Target: int $explicit^foo(); |
| 32 | // Referencing: int x = ^foo(); |
| 33 | // There must be exactly one referencing location marked. |
| 34 | void testWalk(llvm::StringRef TargetCode, llvm::StringRef ReferencingCode) { |
| 35 | llvm::Annotations Target(TargetCode); |
| 36 | llvm::Annotations Referencing(ReferencingCode); |
| 37 | |
| 38 | TestInputs Inputs(Referencing.code()); |
| 39 | Inputs.ExtraFiles["target.h"] = Target.code().str(); |
| 40 | Inputs.ExtraArgs.push_back("-include"); |
| 41 | Inputs.ExtraArgs.push_back("target.h"); |
| 42 | Inputs.ExtraArgs.push_back("-std=c++17"); |
| 43 | TestAST AST(Inputs); |
| 44 | const auto &SM = AST.sourceManager(); |
| 45 | |
| 46 | // We're only going to record references from the nominated point, |
| 47 | // to the target file. |
| 48 | FileID ReferencingFile = SM.getMainFileID(); |
| 49 | SourceLocation ReferencingLoc = |
| 50 | SM.getComposedLoc(ReferencingFile, Referencing.point()); |
| 51 | FileID TargetFile = SM.translateFile( |
| 52 | llvm::cantFail(AST.fileManager().getFileRef("target.h"))); |
| 53 | |
| 54 | // Perform the walk, and capture the offsets of the referenced targets. |
| 55 | std::unordered_map<RefType, std::vector<size_t>> ReferencedOffsets; |
| 56 | for (Decl *D : AST.context().getTranslationUnitDecl()->decls()) { |
| 57 | if (ReferencingFile != SM.getDecomposedExpansionLoc(D->getLocation()).first) |
| 58 | continue; |
| 59 | walkAST(*D, [&](SourceLocation Loc, NamedDecl &ND, RefType RT) { |
| 60 | if (SM.getFileLoc(Loc) != ReferencingLoc) |
| 61 | return; |
| 62 | auto NDLoc = SM.getDecomposedLoc(SM.getFileLoc(ND.getLocation())); |
| 63 | if (NDLoc.first != TargetFile) |
| 64 | return; |
| 65 | ReferencedOffsets[RT].push_back(NDLoc.second); |
| 66 | }); |
| 67 | } |
| 68 | for (auto &Entry : ReferencedOffsets) |
| 69 | llvm::sort(Entry.second); |
| 70 | |
| 71 | // Compare results to the expected points. |
| 72 | // For each difference, show the target point in context, like a diagnostic. |
| 73 | std::string DiagBuf; |
| 74 | llvm::raw_string_ostream DiagOS(DiagBuf); |
| 75 | auto *DiagOpts = new DiagnosticOptions(); |
| 76 | DiagOpts->ShowLevel = 0; |
| 77 | DiagOpts->ShowNoteIncludeStack = 0; |
| 78 | TextDiagnostic Diag(DiagOS, AST.context().getLangOpts(), DiagOpts); |
| 79 | auto DiagnosePoint = [&](llvm::StringRef Message, unsigned Offset) { |
| 80 | Diag.emitDiagnostic( |
| 81 | FullSourceLoc(SM.getComposedLoc(TargetFile, Offset), SM), |
| 82 | DiagnosticsEngine::Note, Message, {}, {}); |
| 83 | }; |
| 84 | for (auto RT : {RefType::Explicit, RefType::Implicit, RefType::Ambiguous}) { |
| 85 | auto RTStr = llvm::to_string(RT); |
| 86 | for (auto Expected : Target.points(RTStr)) |
| 87 | if (!llvm::is_contained(ReferencedOffsets[RT], Expected)) |
| 88 | DiagnosePoint("location not marked used with type " + RTStr, Expected); |
| 89 | for (auto Actual : ReferencedOffsets[RT]) |
| 90 | if (!llvm::is_contained(Target.points(RTStr), Actual)) |
| 91 | DiagnosePoint("location unexpectedly used with type " + RTStr, Actual); |
| 92 | } |
| 93 | |
| 94 | // If there were any differences, we print the entire referencing code once. |
| 95 | if (!DiagBuf.empty()) |
| 96 | ADD_FAILURE() << DiagBuf << "\nfrom code:\n" << ReferencingCode; |
| 97 | } |
| 98 | |
| 99 | TEST(WalkAST, DeclRef) { |
| 100 | testWalk("int $explicit^x;", "int y = ^x;"); |
| 101 | testWalk("int $explicit^foo();", "int y = ^foo();"); |
| 102 | testWalk("namespace ns { int $explicit^x; }", "int y = ns::^x;"); |
| 103 | testWalk("struct S { static int $explicit^x; };", "int y = S::^x;"); |
| 104 | // Canonical declaration only. |
| 105 | testWalk("extern int $explicit^x; int x;", "int y = ^x;"); |
| 106 | // Return type of `foo` isn't used. |
| 107 | testWalk("struct S{}; S $explicit^foo();", "auto bar() { return ^foo(); }"); |
| 108 | } |
| 109 | |
| 110 | TEST(WalkAST, TagType) { |
| 111 | testWalk("struct $explicit^S {};", "^S *y;"); |
| 112 | testWalk("enum $explicit^E {};", "^E *y;"); |
| 113 | testWalk("struct $explicit^S { static int x; };", "int y = ^S::x;"); |
| 114 | } |
| 115 | |
| 116 | TEST(WalkAST, Alias) { |
| 117 | testWalk(R"cpp( |
| 118 | namespace ns { int x; } |
| 119 | using ns::$explicit^x; |
| 120 | )cpp", |
| 121 | "int y = ^x;"); |
| 122 | testWalk("using $explicit^foo = int;", "^foo x;"); |
| 123 | testWalk("struct S {}; using $explicit^foo = S;", "^foo x;"); |
| 124 | } |
| 125 | |
| 126 | TEST(WalkAST, Using) { |
| 127 | // We should report unused overloads as ambiguous. |
| 128 | testWalk(R"cpp( |
| 129 | namespace ns { |
| 130 | void $explicit^x(); void $ambiguous^x(int); void $ambiguous^x(char); |
| 131 | })cpp", |
| 132 | "using ns::^x; void foo() { x(); }"); |
| 133 | testWalk(R"cpp( |
| 134 | namespace ns { |
| 135 | void $ambiguous^x(); void $ambiguous^x(int); void $ambiguous^x(char); |
| 136 | })cpp", |
| 137 | "using ns::^x;"); |
| 138 | testWalk("namespace ns { struct S; } using ns::$explicit^S;", "^S *s;"); |
| 139 | |
| 140 | testWalk(R"cpp( |
| 141 | namespace ns { |
| 142 | template<class T> |
| 143 | class $ambiguous^Y {}; |
| 144 | })cpp", |
| 145 | "using ns::^Y;"); |
| 146 | testWalk(R"cpp( |
| 147 | namespace ns { |
| 148 | template<class T> |
| 149 | class Y {}; |
| 150 | } |
| 151 | using ns::$explicit^Y;)cpp", |
| 152 | "^Y<int> x;"); |
| 153 | } |
| 154 | |
| 155 | TEST(WalkAST, Namespaces) { |
| 156 | testWalk("namespace ns { void x(); }", "using namespace ^ns;"); |
| 157 | } |
| 158 | |
| 159 | TEST(WalkAST, TemplateNames) { |
| 160 | testWalk("template<typename> struct $explicit^S {};", "^S<int> s;"); |
| 161 | // FIXME: Template decl has the wrong primary location for type-alias template |
| 162 | // decls. |
| 163 | testWalk(R"cpp( |
| 164 | template <typename> struct S {}; |
| 165 | template <typename T> $explicit^using foo = S<T>;)cpp", |
| 166 | "^foo<int> x;"); |
| 167 | testWalk(R"cpp( |
| 168 | namespace ns {template <typename> struct S {}; } |
| 169 | using ns::$explicit^S;)cpp", |
| 170 | "^S<int> x;"); |
| 171 | testWalk("template<typename> struct $explicit^S {};", |
| 172 | R"cpp( |
| 173 | template <template <typename> typename> struct X {}; |
| 174 | X<^S> x;)cpp"); |
| 175 | testWalk("template<typename T> struct $explicit^S { S(T); };", "^S s(42);"); |
| 176 | // Should we mark the specialization instead? |
| 177 | testWalk( |
| 178 | "template<typename> struct $explicit^S {}; template <> struct S<int> {};", |
| 179 | "^S<int> s;"); |
| 180 | } |
| 181 | |
| 182 | TEST(WalkAST, MemberExprs) { |
| 183 | testWalk("struct $implicit^S { void foo(); };", "void foo() { S{}.^foo(); }"); |
| 184 | testWalk( |
| 185 | "struct S { void foo(); }; struct $implicit^X : S { using S::foo; };", |
| 186 | "void foo() { X{}.^foo(); }"); |
| 187 | testWalk("struct Base { int a; }; struct $implicit^Derived : public Base {};", |
| 188 | "void fun(Derived d) { d.^a; }"); |
| 189 | testWalk("struct Base { int a; }; struct $implicit^Derived : public Base {};", |
| 190 | "void fun(Derived* d) { d->^a; }"); |
| 191 | testWalk("struct Base { int a; }; struct $implicit^Derived : public Base {};", |
| 192 | "void fun(Derived& d) { d.^a; }"); |
| 193 | testWalk("struct Base { int a; }; struct $implicit^Derived : public Base {};", |
| 194 | "void fun() { Derived().^a; }"); |
| 195 | testWalk("struct Base { int a; }; struct $implicit^Derived : public Base {};", |
| 196 | "Derived foo(); void fun() { foo().^a; }"); |
| 197 | testWalk("struct Base { int a; }; struct $implicit^Derived : public Base {};", |
| 198 | "Derived& foo(); void fun() { foo().^a; }"); |
| 199 | testWalk(R"cpp( |
| 200 | template <typename T> |
| 201 | struct unique_ptr { |
| 202 | T *operator->(); |
| 203 | }; |
| 204 | struct $implicit^Foo { int a; };)cpp", |
| 205 | "void test(unique_ptr<Foo> &V) { V->^a; }"); |
| 206 | testWalk(R"cpp( |
| 207 | template <typename T> |
| 208 | struct $implicit^unique_ptr { |
| 209 | void release(); |
| 210 | }; |
| 211 | struct Foo {};)cpp", |
| 212 | "void test(unique_ptr<Foo> &V) { V.^release(); }"); |
| 213 | // Respect the sugar type (typedef, using-type). |
| 214 | testWalk(R"cpp( |
| 215 | namespace ns { struct Foo { int a; }; } |
| 216 | using $implicit^Bar = ns::Foo;)cpp", |
| 217 | "void test(Bar b) { b.^a; }"); |
| 218 | testWalk(R"cpp( |
| 219 | namespace ns { struct Foo { int a; }; } |
| 220 | using ns::$implicit^Foo;)cpp", |
| 221 | "void test(Foo b) { b.^a; }"); |
| 222 | testWalk(R"cpp( |
| 223 | namespace ns { struct Foo { int a; }; } |
| 224 | namespace ns2 { using Bar = ns::Foo; } |
| 225 | using ns2::$implicit^Bar; |
| 226 | )cpp", |
| 227 | "void test(Bar b) { b.^a; }"); |
| 228 | testWalk(R"cpp( |
| 229 | namespace ns { template<typename> struct Foo { int a; }; } |
| 230 | using ns::$implicit^Foo;)cpp", |
| 231 | "void k(Foo<int> b) { b.^a; }"); |
| 232 | // Test the dependent-type case (CXXDependentScopeMemberExpr) |
| 233 | testWalk("template<typename T> struct $implicit^Base { void method(); };", |
| 234 | "template<typename T> void k(Base<T> t) { t.^method(); }"); |
| 235 | testWalk("template<typename T> struct $implicit^Base { void method(); };", |
| 236 | "template<typename T> void k(Base<T>& t) { t.^method(); }"); |
| 237 | testWalk("template<typename T> struct $implicit^Base { void method(); };", |
| 238 | "template<typename T> void k(Base<T>* t) { t->^method(); }"); |
| 239 | } |
| 240 | |
| 241 | TEST(WalkAST, ConstructExprs) { |
| 242 | testWalk("struct $implicit^S {};", "S ^t;"); |
| 243 | testWalk("struct $implicit^S { S(); };", "S ^t;"); |
| 244 | testWalk("struct $explicit^S { S(int); };", "S ^t(42);"); |
| 245 | testWalk("struct $implicit^S { S(int); };", "S t = ^42;"); |
| 246 | testWalk("namespace ns { struct S{}; } using ns::$implicit^S;", "S ^t;"); |
| 247 | } |
| 248 | |
| 249 | TEST(WalkAST, Operator) { |
| 250 | // References to operators are not counted as uses. |
| 251 | testWalk("struct string {}; int operator+(string, string);", |
| 252 | "int k = string() ^+ string();"); |
| 253 | testWalk("struct string {int operator+(string); }; ", |
| 254 | "int k = string() ^+ string();"); |
| 255 | testWalk("struct string { friend int operator+(string, string); }; ", |
| 256 | "int k = string() ^+ string();"); |
| 257 | } |
| 258 | |
| 259 | TEST(WalkAST, Functions) { |
| 260 | // Definition uses declaration, not the other way around. |
| 261 | testWalk("void $explicit^foo();", "void ^foo() {}"); |
| 262 | testWalk("void foo() {}", "void ^foo();"); |
| 263 | |
| 264 | // Unresolved calls marks all the overloads. |
| 265 | testWalk("void $ambiguous^foo(int); void $ambiguous^foo(char);", |
| 266 | "template <typename T> void bar() { ^foo(T{}); }"); |
| 267 | } |
| 268 | |
| 269 | TEST(WalkAST, Enums) { |
| 270 | testWalk("enum E { $explicit^A = 42, B = 43 };", "int e = ^A;"); |
| 271 | testWalk("enum class $explicit^E : int;", "enum class ^E : int {};"); |
| 272 | testWalk("enum class E : int {};", "enum class ^E : int ;"); |
| 273 | } |
| 274 | |
| 275 | } // namespace |
| 276 | } // namespace clang::include_cleaner |