| 1 |
//===--- IndexTests.cpp - Test indexing actions -----------------*- 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 |
|
| 9 |
#include "clang/AST/ASTConsumer.h" |
| 10 |
#include "clang/AST/ASTContext.h" |
| 11 |
#include "clang/AST/Decl.h" |
| 12 |
#include "clang/Basic/SourceLocation.h" |
| 13 |
#include "clang/Basic/SourceManager.h" |
| 14 |
#include "clang/Frontend/CompilerInstance.h" |
| 15 |
#include "clang/Frontend/FrontendAction.h" |
| 16 |
#include "clang/Index/IndexDataConsumer.h" |
| 17 |
#include "clang/Index/IndexSymbol.h" |
| 18 |
#include "clang/Index/IndexingAction.h" |
| 19 |
#include "clang/Lex/Preprocessor.h" |
| 20 |
#include "clang/Tooling/Tooling.h" |
| 21 |
#include "llvm/ADT/StringRef.h" |
| 22 |
#include "llvm/Support/VirtualFileSystem.h" |
| 23 |
#include "gmock/gmock.h" |
| 24 |
#include "gtest/gtest.h" |
| 25 |
#include <memory> |
| 26 |
|
| 27 |
namespace clang { |
| 28 |
namespace index { |
| 29 |
namespace { |
| 30 |
struct Position { |
| 31 |
size_t Line = 0; |
| 32 |
size_t Column = 0; |
| 33 |
|
| 34 |
Position(size_t Line = 0, size_t Column = 0) : Line(Line), Column(Column) {} |
| 35 |
|
| 36 |
static Position fromSourceLocation(SourceLocation Loc, |
| 37 |
const SourceManager &SM) { |
| 38 |
FileID FID; |
| 39 |
unsigned Offset; |
| 40 |
std::tie(FID, Offset) = SM.getDecomposedSpellingLoc(Loc); |
| 41 |
Position P; |
| 42 |
P.Line = SM.getLineNumber(FID, Offset); |
| 43 |
P.Column = SM.getColumnNumber(FID, Offset); |
| 44 |
return P; |
| 45 |
} |
| 46 |
}; |
| 47 |
|
| 48 |
bool operator==(const Position &LHS, const Position &RHS) { |
| 49 |
return std::tie(LHS.Line, LHS.Column) == std::tie(RHS.Line, RHS.Column); |
| 50 |
} |
| 51 |
|
| 52 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Position &Pos) { |
| 53 |
return OS << Pos.Line << ':' << Pos.Column; |
| 54 |
} |
| 55 |
|
| 56 |
struct TestSymbol { |
| 57 |
std::string QName; |
| 58 |
Position WrittenPos; |
| 59 |
Position DeclPos; |
| 60 |
SymbolInfo SymInfo; |
| 61 |
SymbolRoleSet Roles; |
| 62 |
// FIXME: add more information. |
| 63 |
}; |
| 64 |
|
| 65 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const TestSymbol &S) { |
| 66 |
return OS << S.QName << '[' << S.WrittenPos << ']' << '@' << S.DeclPos << '(' |
| 67 |
<< static_cast<unsigned>(S.SymInfo.Kind) << ')'; |
| 68 |
} |
| 69 |
|
| 70 |
class Indexer : public IndexDataConsumer { |
| 71 |
public: |
| 72 |
void initialize(ASTContext &Ctx) override { |
| 73 |
AST = &Ctx; |
| 74 |
IndexDataConsumer::initialize(Ctx); |
| 75 |
} |
| 76 |
|
| 77 |
bool handleDeclOccurrence(const Decl *D, SymbolRoleSet Roles, |
| 78 |
ArrayRef<SymbolRelation>, SourceLocation Loc, |
| 79 |
ASTNodeInfo) override { |
| 80 |
const auto *ND = llvm::dyn_cast<NamedDecl>(D); |
| 81 |
if (!ND) |
| 82 |
return true; |
| 83 |
TestSymbol S; |
| 84 |
S.SymInfo = getSymbolInfo(D); |
| 85 |
S.QName = ND->getQualifiedNameAsString(); |
| 86 |
S.WrittenPos = Position::fromSourceLocation(Loc, AST->getSourceManager()); |
| 87 |
S.DeclPos = |
| 88 |
Position::fromSourceLocation(D->getLocation(), AST->getSourceManager()); |
| 89 |
S.Roles = Roles; |
| 90 |
Symbols.push_back(std::move(S)); |
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
bool handleMacroOccurrence(const IdentifierInfo *Name, const MacroInfo *MI, |
| 95 |
SymbolRoleSet Roles, SourceLocation Loc) override { |
| 96 |
TestSymbol S; |
| 97 |
S.SymInfo = getSymbolInfoForMacro(*MI); |
| 98 |
S.QName = std::string(Name->getName()); |
| 99 |
S.WrittenPos = Position::fromSourceLocation(Loc, AST->getSourceManager()); |
| 100 |
S.DeclPos = Position::fromSourceLocation(MI->getDefinitionLoc(), |
| 101 |
AST->getSourceManager()); |
| 102 |
S.Roles = Roles; |
| 103 |
Symbols.push_back(std::move(S)); |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
std::vector<TestSymbol> Symbols; |
| 108 |
const ASTContext *AST = nullptr; |
| 109 |
}; |
| 110 |
|
| 111 |
class IndexAction : public ASTFrontendAction { |
| 112 |
public: |
| 113 |
IndexAction(std::shared_ptr<Indexer> Index, |
| 114 |
IndexingOptions Opts = IndexingOptions()) |
| 115 |
: Index(std::move(Index)), Opts(Opts) {} |
| 116 |
|
| 117 |
protected: |
| 118 |
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
| 119 |
StringRef InFile) override { |
| 120 |
class Consumer : public ASTConsumer { |
| 121 |
std::shared_ptr<Indexer> Index; |
| 122 |
std::shared_ptr<Preprocessor> PP; |
| 123 |
IndexingOptions Opts; |
| 124 |
|
| 125 |
public: |
| 126 |
Consumer(std::shared_ptr<Indexer> Index, std::shared_ptr<Preprocessor> PP, |
| 127 |
IndexingOptions Opts) |
| 128 |
: Index(std::move(Index)), PP(std::move(PP)), Opts(Opts) {} |
| 129 |
|
| 130 |
void HandleTranslationUnit(ASTContext &Ctx) override { |
| 131 |
std::vector<Decl *> DeclsToIndex( |
| 132 |
Ctx.getTranslationUnitDecl()->decls().begin(), |
| 133 |
Ctx.getTranslationUnitDecl()->decls().end()); |
| 134 |
indexTopLevelDecls(Ctx, *PP, DeclsToIndex, *Index, Opts); |
| 135 |
} |
| 136 |
}; |
| 137 |
return std::make_unique<Consumer>(Index, CI.getPreprocessorPtr(), Opts); |
| 138 |
} |
| 139 |
|
| 140 |
private: |
| 141 |
std::shared_ptr<Indexer> Index; |
| 142 |
IndexingOptions Opts; |
| 143 |
}; |
| 144 |
|
| 145 |
using testing::AllOf; |
| 146 |
using testing::Contains; |
| 147 |
using testing::Not; |
| 148 |
using testing::UnorderedElementsAre; |
| 149 |
|
| 150 |
MATCHER_P(QName, Name, "") { return arg.QName == Name; } |
| 151 |
MATCHER_P(WrittenAt, Pos, "") { return arg.WrittenPos == Pos; } |
| 152 |
MATCHER_P(DeclAt, Pos, "") { return arg.DeclPos == Pos; } |
| 153 |
MATCHER_P(Kind, SymKind, "") { return arg.SymInfo.Kind == SymKind; } |
| 154 |
MATCHER_P(HasRole, Role, "") { return arg.Roles & static_cast<unsigned>(Role); } |
| 155 |
|
| 156 |
TEST(IndexTest, Simple) { |
| 157 |
auto Index = std::make_shared<Indexer>(); |
| 158 |
tooling::runToolOnCode(std::make_unique<IndexAction>(Index), |
| 159 |
"class X {}; void f() {}"); |
| 160 |
EXPECT_THAT(Index->Symbols, UnorderedElementsAre(QName("X"), QName("f"))); |
| 161 |
} |
| 162 |
|
| 163 |
TEST(IndexTest, IndexPreprocessorMacros) { |
| 164 |
std::string Code = R"cpp( |
| 165 |
#define INDEX_MAC 1 |
| 166 |
#define INDEX_MAC_UNDEF 1 |
| 167 |
#undef INDEX_MAC_UNDEF |
| 168 |
#define INDEX_MAC_REDEF 1 |
| 169 |
#undef INDEX_MAC_REDEF |
| 170 |
#define INDEX_MAC_REDEF 2 |
| 171 |
)cpp"; |
| 172 |
auto Index = std::make_shared<Indexer>(); |
| 173 |
IndexingOptions Opts; |
| 174 |
Opts.IndexMacrosInPreprocessor = true; |
| 175 |
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code); |
| 176 |
EXPECT_THAT(Index->Symbols, |
| 177 |
Contains(AllOf(QName("INDEX_MAC"), WrittenAt(Position(2, 13)), |
| 178 |
DeclAt(Position(2, 13)), |
| 179 |
HasRole(SymbolRole::Definition)))); |
| 180 |
EXPECT_THAT( |
| 181 |
Index->Symbols, |
| 182 |
AllOf(Contains(AllOf(QName("INDEX_MAC_UNDEF"), WrittenAt(Position(3, 13)), |
| 183 |
DeclAt(Position(3, 13)), |
| 184 |
HasRole(SymbolRole::Definition))), |
| 185 |
Contains(AllOf(QName("INDEX_MAC_UNDEF"), WrittenAt(Position(4, 12)), |
| 186 |
DeclAt(Position(3, 13)), |
| 187 |
HasRole(SymbolRole::Undefinition))))); |
| 188 |
EXPECT_THAT( |
| 189 |
Index->Symbols, |
| 190 |
AllOf(Contains(AllOf(QName("INDEX_MAC_REDEF"), WrittenAt(Position(5, 13)), |
| 191 |
DeclAt(Position(5, 13)), |
| 192 |
HasRole(SymbolRole::Definition))), |
| 193 |
Contains(AllOf(QName("INDEX_MAC_REDEF"), WrittenAt(Position(6, 12)), |
| 194 |
DeclAt(Position(5, 13)), |
| 195 |
HasRole(SymbolRole::Undefinition))), |
| 196 |
Contains(AllOf(QName("INDEX_MAC_REDEF"), WrittenAt(Position(7, 13)), |
| 197 |
DeclAt(Position(7, 13)), |
| 198 |
HasRole(SymbolRole::Definition))))); |
| 199 |
|
| 200 |
Opts.IndexMacrosInPreprocessor = false; |
| 201 |
Index->Symbols.clear(); |
| 202 |
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code); |
| 203 |
EXPECT_THAT(Index->Symbols, UnorderedElementsAre()); |
| 204 |
} |
| 205 |
|
| 206 |
TEST(IndexTest, IndexParametersInDecls) { |
| 207 |
std::string Code = "void foo(int bar);"; |
| 208 |
auto Index = std::make_shared<Indexer>(); |
| 209 |
IndexingOptions Opts; |
| 210 |
Opts.IndexFunctionLocals = true; |
| 211 |
Opts.IndexParametersInDeclarations = true; |
| 212 |
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code); |
| 213 |
EXPECT_THAT(Index->Symbols, Contains(QName("bar"))); |
| 214 |
|
| 215 |
Opts.IndexParametersInDeclarations = false; |
| 216 |
Index->Symbols.clear(); |
| 217 |
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code); |
| 218 |
EXPECT_THAT(Index->Symbols, Not(Contains(QName("bar")))); |
| 219 |
} |
| 220 |
|
| 221 |
TEST(IndexTest, IndexExplicitTemplateInstantiation) { |
| 222 |
std::string Code = R"cpp( |
| 223 |
template <typename T> |
| 224 |
struct Foo { void bar() {} }; |
| 225 |
template <> |
| 226 |
struct Foo<int> { void bar() {} }; |
| 227 |
void foo() { |
| 228 |
Foo<char> abc; |
| 229 |
Foo<int> b; |
| 230 |
} |
| 231 |
)cpp"; |
| 232 |
auto Index = std::make_shared<Indexer>(); |
| 233 |
IndexingOptions Opts; |
| 234 |
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code); |
| 235 |
EXPECT_THAT(Index->Symbols, |
| 236 |
AllOf(Contains(AllOf(QName("Foo"), WrittenAt(Position(8, 7)), |
| 237 |
DeclAt(Position(5, 12)))), |
| 238 |
Contains(AllOf(QName("Foo"), WrittenAt(Position(7, 7)), |
| 239 |
DeclAt(Position(3, 12)))))); |
| 240 |
} |
| 241 |
|
| 242 |
TEST(IndexTest, IndexTemplateInstantiationPartial) { |
| 243 |
std::string Code = R"cpp( |
| 244 |
template <typename T1, typename T2> |
| 245 |
struct Foo { void bar() {} }; |
| 246 |
template <typename T> |
| 247 |
struct Foo<T, int> { void bar() {} }; |
| 248 |
void foo() { |
| 249 |
Foo<char, char> abc; |
| 250 |
Foo<int, int> b; |
| 251 |
} |
| 252 |
)cpp"; |
| 253 |
auto Index = std::make_shared<Indexer>(); |
| 254 |
IndexingOptions Opts; |
| 255 |
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code); |
| 256 |
EXPECT_THAT(Index->Symbols, |
| 257 |
Contains(AllOf(QName("Foo"), WrittenAt(Position(8, 7)), |
| 258 |
DeclAt(Position(5, 12))))); |
| 259 |
} |
| 260 |
|
| 261 |
TEST(IndexTest, IndexTypeParmDecls) { |
| 262 |
std::string Code = R"cpp( |
| 263 |
template <typename T, int I, template<typename> class C, typename NoRef> |
| 264 |
struct Foo { |
| 265 |
T t = I; |
| 266 |
C<int> x; |
| 267 |
}; |
| 268 |
)cpp"; |
| 269 |
auto Index = std::make_shared<Indexer>(); |
| 270 |
IndexingOptions Opts; |
| 271 |
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code); |
| 272 |
EXPECT_THAT(Index->Symbols, AllOf(Not(Contains(QName("Foo::T"))), |
| 273 |
Not(Contains(QName("Foo::I"))), |
| 274 |
Not(Contains(QName("Foo::C"))), |
| 275 |
Not(Contains(QName("Foo::NoRef"))))); |
| 276 |
|
| 277 |
Opts.IndexTemplateParameters = true; |
| 278 |
Index->Symbols.clear(); |
| 279 |
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code); |
| 280 |
EXPECT_THAT(Index->Symbols, |
| 281 |
AllOf(Contains(AllOf(QName("Foo::T"), |
| 282 |
Kind(SymbolKind::TemplateTypeParm))), |
| 283 |
Contains(AllOf(QName("Foo::I"), |
| 284 |
Kind(SymbolKind::NonTypeTemplateParm))), |
| 285 |
Contains(AllOf(QName("Foo::C"), |
| 286 |
Kind(SymbolKind::TemplateTemplateParm))), |
| 287 |
Contains(QName("Foo::NoRef")))); |
| 288 |
} |
| 289 |
|
| 290 |
TEST(IndexTest, UsingDecls) { |
| 291 |
std::string Code = R"cpp( |
| 292 |
void foo(int bar); |
| 293 |
namespace std { |
| 294 |
using ::foo; |
| 295 |
} |
| 296 |
)cpp"; |
| 297 |
auto Index = std::make_shared<Indexer>(); |
| 298 |
IndexingOptions Opts; |
| 299 |
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code); |
| 300 |
EXPECT_THAT(Index->Symbols, |
| 301 |
Contains(AllOf(QName("std::foo"), Kind(SymbolKind::Using)))); |
| 302 |
} |
| 303 |
|
| 304 |
TEST(IndexTest, Constructors) { |
| 305 |
std::string Code = R"cpp( |
| 306 |
struct Foo { |
| 307 |
Foo(int); |
| 308 |
~Foo(); |
| 309 |
}; |
| 310 |
)cpp"; |
| 311 |
auto Index = std::make_shared<Indexer>(); |
| 312 |
IndexingOptions Opts; |
| 313 |
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code); |
| 314 |
EXPECT_THAT( |
| 315 |
Index->Symbols, |
| 316 |
UnorderedElementsAre( |
| 317 |
AllOf(QName("Foo"), Kind(SymbolKind::Struct), |
| 318 |
WrittenAt(Position(2, 12))), |
| 319 |
AllOf(QName("Foo::Foo"), Kind(SymbolKind::Constructor), |
| 320 |
WrittenAt(Position(3, 7))), |
| 321 |
AllOf(QName("Foo"), Kind(SymbolKind::Struct), |
| 322 |
HasRole(SymbolRole::NameReference), WrittenAt(Position(3, 7))), |
| 323 |
AllOf(QName("Foo::~Foo"), Kind(SymbolKind::Destructor), |
| 324 |
WrittenAt(Position(4, 7))), |
| 325 |
AllOf(QName("Foo"), Kind(SymbolKind::Struct), |
| 326 |
HasRole(SymbolRole::NameReference), |
| 327 |
WrittenAt(Position(4, 8))))); |
| 328 |
} |
| 329 |
|
| 330 |
TEST(IndexTest, InjecatedNameClass) { |
| 331 |
std::string Code = R"cpp( |
| 332 |
template <typename T> |
| 333 |
class Foo { |
| 334 |
void f(Foo x); |
| 335 |
}; |
| 336 |
)cpp"; |
| 337 |
auto Index = std::make_shared<Indexer>(); |
| 338 |
IndexingOptions Opts; |
| 339 |
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code); |
| 340 |
EXPECT_THAT(Index->Symbols, |
| 341 |
UnorderedElementsAre(AllOf(QName("Foo"), Kind(SymbolKind::Class), |
| 342 |
WrittenAt(Position(3, 11))), |
| 343 |
AllOf(QName("Foo::f"), |
| 344 |
Kind(SymbolKind::InstanceMethod), |
| 345 |
WrittenAt(Position(4, 12))), |
| 346 |
AllOf(QName("Foo"), Kind(SymbolKind::Class), |
| 347 |
HasRole(SymbolRole::Reference), |
| 348 |
WrittenAt(Position(4, 14))))); |
| 349 |
} |
| 350 |
|
| 351 |
TEST(IndexTest, VisitDefaultArgs) { |
| 352 |
std::string Code = R"cpp( |
| 353 |
int var = 0; |
| 354 |
void f(int s = var) {} |
| 355 |
)cpp"; |
| 356 |
auto Index = std::make_shared<Indexer>(); |
| 357 |
IndexingOptions Opts; |
| 358 |
Opts.IndexFunctionLocals = true; |
| 359 |
Opts.IndexParametersInDeclarations = true; |
| 360 |
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code); |
| 361 |
EXPECT_THAT(Index->Symbols, |
| 362 |
Contains(AllOf(QName("var"), HasRole(SymbolRole::Reference), |
| 363 |
WrittenAt(Position(3, 20))))); |
| 364 |
} |
| 365 |
|
| 366 |
TEST(IndexTest, RelationBaseOf) { |
| 367 |
std::string Code = R"cpp( |
| 368 |
class A {}; |
| 369 |
template <typename> class B {}; |
| 370 |
class C : B<A> {}; |
| 371 |
)cpp"; |
| 372 |
auto Index = std::make_shared<Indexer>(); |
| 373 |
tooling::runToolOnCode(std::make_unique<IndexAction>(Index), Code); |
| 374 |
// A should not be the base of anything. |
| 375 |
EXPECT_THAT(Index->Symbols, |
| 376 |
Contains(AllOf(QName("A"), HasRole(SymbolRole::Reference), |
| 377 |
Not(HasRole(SymbolRole::RelationBaseOf))))); |
| 378 |
} |
| 379 |
|
| 380 |
TEST(IndexTest, EnumBase) { |
| 381 |
std::string Code = R"cpp( |
| 382 |
typedef int MyTypedef; |
| 383 |
enum Foo : MyTypedef; |
| 384 |
enum Foo : MyTypedef {}; |
| 385 |
)cpp"; |
| 386 |
auto Index = std::make_shared<Indexer>(); |
| 387 |
tooling::runToolOnCode(std::make_unique<IndexAction>(Index), Code); |
| 388 |
EXPECT_THAT( |
| 389 |
Index->Symbols, |
| 390 |
AllOf(Contains(AllOf(QName("MyTypedef"), HasRole(SymbolRole::Reference), |
| 391 |
WrittenAt(Position(3, 16)))), |
| 392 |
Contains(AllOf(QName("MyTypedef"), HasRole(SymbolRole::Reference), |
| 393 |
WrittenAt(Position(4, 16)))))); |
| 394 |
} |
| 395 |
} // namespace |
| 396 |
} // namespace index |
| 397 |
} // namespace clang |