Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 1 | //===--- IndexTests.cpp - Test indexing actions -----------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "clang/AST/ASTConsumer.h" |
Kadir Cetinkaya | f53621b | 2019-02-18 11:30:43 +0000 | [diff] [blame] | 10 | #include "clang/AST/ASTContext.h" |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 11 | #include "clang/AST/Decl.h" |
Kadir Cetinkaya | f53621b | 2019-02-18 11:30:43 +0000 | [diff] [blame] | 12 | #include "clang/Basic/SourceLocation.h" |
| 13 | #include "clang/Basic/SourceManager.h" |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 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" |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 22 | #include "llvm/Support/VirtualFileSystem.h" |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 23 | #include "gmock/gmock.h" |
| 24 | #include "gtest/gtest.h" |
| 25 | #include <memory> |
| 26 | |
| 27 | namespace clang { |
| 28 | namespace index { |
Kadir Cetinkaya | f53621b | 2019-02-18 11:30:43 +0000 | [diff] [blame] | 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 | } |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 55 | |
| 56 | struct TestSymbol { |
| 57 | std::string QName; |
Kadir Cetinkaya | f53621b | 2019-02-18 11:30:43 +0000 | [diff] [blame] | 58 | Position WrittenPos; |
| 59 | Position DeclPos; |
Kadir Cetinkaya | a87ada0 | 2019-02-26 14:23:12 +0000 | [diff] [blame] | 60 | SymbolInfo SymInfo; |
Kadir Cetinkaya | e7eb27a | 2019-03-08 08:30:20 +0000 | [diff] [blame] | 61 | SymbolRoleSet Roles; |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 62 | // FIXME: add more information. |
| 63 | }; |
| 64 | |
| 65 | llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const TestSymbol &S) { |
Kadir Cetinkaya | e7eb27a | 2019-03-08 08:30:20 +0000 | [diff] [blame] | 66 | return OS << S.QName << '[' << S.WrittenPos << ']' << '@' << S.DeclPos << '(' |
| 67 | << static_cast<unsigned>(S.SymInfo.Kind) << ')'; |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 70 | class Indexer : public IndexDataConsumer { |
| 71 | public: |
Kadir Cetinkaya | f53621b | 2019-02-18 11:30:43 +0000 | [diff] [blame] | 72 | void initialize(ASTContext &Ctx) override { |
| 73 | AST = &Ctx; |
| 74 | IndexDataConsumer::initialize(Ctx); |
| 75 | } |
| 76 | |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 77 | bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles, |
Kadir Cetinkaya | f53621b | 2019-02-18 11:30:43 +0000 | [diff] [blame] | 78 | ArrayRef<SymbolRelation>, SourceLocation Loc, |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 79 | ASTNodeInfo) override { |
| 80 | const auto *ND = llvm::dyn_cast<NamedDecl>(D); |
| 81 | if (!ND) |
| 82 | return true; |
| 83 | TestSymbol S; |
Kadir Cetinkaya | a87ada0 | 2019-02-26 14:23:12 +0000 | [diff] [blame] | 84 | S.SymInfo = getSymbolInfo(D); |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 85 | S.QName = ND->getQualifiedNameAsString(); |
Kadir Cetinkaya | f53621b | 2019-02-18 11:30:43 +0000 | [diff] [blame] | 86 | S.WrittenPos = Position::fromSourceLocation(Loc, AST->getSourceManager()); |
| 87 | S.DeclPos = |
| 88 | Position::fromSourceLocation(D->getLocation(), AST->getSourceManager()); |
Kadir Cetinkaya | e7eb27a | 2019-03-08 08:30:20 +0000 | [diff] [blame] | 89 | S.Roles = Roles; |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 90 | Symbols.push_back(std::move(S)); |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | bool handleMacroOccurence(const IdentifierInfo *Name, const MacroInfo *, |
| 95 | SymbolRoleSet, SourceLocation) override { |
| 96 | TestSymbol S; |
| 97 | S.QName = Name->getName(); |
| 98 | Symbols.push_back(std::move(S)); |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | std::vector<TestSymbol> Symbols; |
Kadir Cetinkaya | f53621b | 2019-02-18 11:30:43 +0000 | [diff] [blame] | 103 | const ASTContext *AST = nullptr; |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | class IndexAction : public ASTFrontendAction { |
| 107 | public: |
| 108 | IndexAction(std::shared_ptr<Indexer> Index, |
| 109 | IndexingOptions Opts = IndexingOptions()) |
| 110 | : Index(std::move(Index)), Opts(Opts) {} |
| 111 | |
| 112 | protected: |
| 113 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
| 114 | StringRef InFile) override { |
| 115 | class Consumer : public ASTConsumer { |
| 116 | std::shared_ptr<Indexer> Index; |
| 117 | std::shared_ptr<Preprocessor> PP; |
| 118 | IndexingOptions Opts; |
| 119 | |
| 120 | public: |
| 121 | Consumer(std::shared_ptr<Indexer> Index, std::shared_ptr<Preprocessor> PP, |
| 122 | IndexingOptions Opts) |
| 123 | : Index(std::move(Index)), PP(std::move(PP)), Opts(Opts) {} |
| 124 | |
| 125 | void HandleTranslationUnit(ASTContext &Ctx) override { |
| 126 | std::vector<Decl *> DeclsToIndex( |
| 127 | Ctx.getTranslationUnitDecl()->decls().begin(), |
| 128 | Ctx.getTranslationUnitDecl()->decls().end()); |
| 129 | indexTopLevelDecls(Ctx, *PP, DeclsToIndex, *Index, Opts); |
| 130 | } |
| 131 | }; |
| 132 | return llvm::make_unique<Consumer>(Index, CI.getPreprocessorPtr(), Opts); |
| 133 | } |
| 134 | |
| 135 | private: |
| 136 | std::shared_ptr<Indexer> Index; |
| 137 | IndexingOptions Opts; |
| 138 | }; |
| 139 | |
Kadir Cetinkaya | f53621b | 2019-02-18 11:30:43 +0000 | [diff] [blame] | 140 | using testing::AllOf; |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 141 | using testing::Contains; |
| 142 | using testing::Not; |
| 143 | using testing::UnorderedElementsAre; |
| 144 | |
| 145 | MATCHER_P(QName, Name, "") { return arg.QName == Name; } |
Kadir Cetinkaya | f53621b | 2019-02-18 11:30:43 +0000 | [diff] [blame] | 146 | MATCHER_P(WrittenAt, Pos, "") { return arg.WrittenPos == Pos; } |
| 147 | MATCHER_P(DeclAt, Pos, "") { return arg.DeclPos == Pos; } |
Kadir Cetinkaya | a87ada0 | 2019-02-26 14:23:12 +0000 | [diff] [blame] | 148 | MATCHER_P(Kind, SymKind, "") { return arg.SymInfo.Kind == SymKind; } |
Kadir Cetinkaya | e7eb27a | 2019-03-08 08:30:20 +0000 | [diff] [blame] | 149 | MATCHER_P(HasRole, Role, "") { return arg.Roles & static_cast<unsigned>(Role); } |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 150 | |
| 151 | TEST(IndexTest, Simple) { |
| 152 | auto Index = std::make_shared<Indexer>(); |
| 153 | tooling::runToolOnCode(new IndexAction(Index), "class X {}; void f() {}"); |
| 154 | EXPECT_THAT(Index->Symbols, UnorderedElementsAre(QName("X"), QName("f"))); |
| 155 | } |
| 156 | |
| 157 | TEST(IndexTest, IndexPreprocessorMacros) { |
| 158 | std::string Code = "#define INDEX_MAC 1"; |
| 159 | auto Index = std::make_shared<Indexer>(); |
| 160 | IndexingOptions Opts; |
| 161 | Opts.IndexMacrosInPreprocessor = true; |
| 162 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 163 | EXPECT_THAT(Index->Symbols, Contains(QName("INDEX_MAC"))); |
| 164 | |
| 165 | Opts.IndexMacrosInPreprocessor = false; |
| 166 | Index->Symbols.clear(); |
| 167 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 168 | EXPECT_THAT(Index->Symbols, UnorderedElementsAre()); |
| 169 | } |
| 170 | |
Kadir Cetinkaya | 0468fc0 | 2019-02-11 13:02:21 +0000 | [diff] [blame] | 171 | TEST(IndexTest, IndexParametersInDecls) { |
| 172 | std::string Code = "void foo(int bar);"; |
| 173 | auto Index = std::make_shared<Indexer>(); |
| 174 | IndexingOptions Opts; |
| 175 | Opts.IndexFunctionLocals = true; |
| 176 | Opts.IndexParametersInDeclarations = true; |
| 177 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 178 | EXPECT_THAT(Index->Symbols, Contains(QName("bar"))); |
| 179 | |
| 180 | Opts.IndexParametersInDeclarations = false; |
| 181 | Index->Symbols.clear(); |
| 182 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 183 | EXPECT_THAT(Index->Symbols, Not(Contains(QName("bar")))); |
| 184 | } |
| 185 | |
Kadir Cetinkaya | f53621b | 2019-02-18 11:30:43 +0000 | [diff] [blame] | 186 | TEST(IndexTest, IndexExplicitTemplateInstantiation) { |
| 187 | std::string Code = R"cpp( |
| 188 | template <typename T> |
| 189 | struct Foo { void bar() {} }; |
| 190 | template <> |
| 191 | struct Foo<int> { void bar() {} }; |
| 192 | void foo() { |
| 193 | Foo<char> abc; |
| 194 | Foo<int> b; |
| 195 | } |
| 196 | )cpp"; |
| 197 | auto Index = std::make_shared<Indexer>(); |
| 198 | IndexingOptions Opts; |
| 199 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 200 | EXPECT_THAT(Index->Symbols, |
| 201 | AllOf(Contains(AllOf(QName("Foo"), WrittenAt(Position(8, 7)), |
| 202 | DeclAt(Position(5, 12)))), |
| 203 | Contains(AllOf(QName("Foo"), WrittenAt(Position(7, 7)), |
| 204 | DeclAt(Position(3, 12)))))); |
| 205 | } |
| 206 | |
| 207 | TEST(IndexTest, IndexTemplateInstantiationPartial) { |
| 208 | std::string Code = R"cpp( |
| 209 | template <typename T1, typename T2> |
| 210 | struct Foo { void bar() {} }; |
| 211 | template <typename T> |
| 212 | struct Foo<T, int> { void bar() {} }; |
| 213 | void foo() { |
| 214 | Foo<char, char> abc; |
| 215 | Foo<int, int> b; |
| 216 | } |
| 217 | )cpp"; |
| 218 | auto Index = std::make_shared<Indexer>(); |
| 219 | IndexingOptions Opts; |
| 220 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 221 | EXPECT_THAT(Index->Symbols, |
| 222 | Contains(AllOf(QName("Foo"), WrittenAt(Position(8, 7)), |
| 223 | DeclAt(Position(5, 12))))); |
| 224 | } |
| 225 | |
Kadir Cetinkaya | b780517 | 2019-02-21 09:52:33 +0000 | [diff] [blame] | 226 | TEST(IndexTest, IndexTypeParmDecls) { |
| 227 | std::string Code = R"cpp( |
| 228 | template <typename T, int I, template<typename> class C, typename NoRef> |
| 229 | struct Foo { |
| 230 | T t = I; |
| 231 | C<int> x; |
| 232 | }; |
| 233 | )cpp"; |
| 234 | auto Index = std::make_shared<Indexer>(); |
| 235 | IndexingOptions Opts; |
| 236 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 237 | EXPECT_THAT(Index->Symbols, AllOf(Not(Contains(QName("Foo::T"))), |
| 238 | Not(Contains(QName("Foo::I"))), |
| 239 | Not(Contains(QName("Foo::C"))), |
| 240 | Not(Contains(QName("Foo::NoRef"))))); |
| 241 | |
| 242 | Opts.IndexTemplateParameters = true; |
| 243 | Index->Symbols.clear(); |
| 244 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 245 | EXPECT_THAT(Index->Symbols, |
| 246 | AllOf(Contains(QName("Foo::T")), Contains(QName("Foo::I")), |
| 247 | Contains(QName("Foo::C")), Contains(QName("Foo::NoRef")))); |
| 248 | } |
| 249 | |
Kadir Cetinkaya | a87ada0 | 2019-02-26 14:23:12 +0000 | [diff] [blame] | 250 | TEST(IndexTest, UsingDecls) { |
| 251 | std::string Code = R"cpp( |
| 252 | void foo(int bar); |
| 253 | namespace std { |
| 254 | using ::foo; |
| 255 | } |
| 256 | )cpp"; |
| 257 | auto Index = std::make_shared<Indexer>(); |
| 258 | IndexingOptions Opts; |
| 259 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 260 | EXPECT_THAT(Index->Symbols, |
| 261 | Contains(AllOf(QName("std::foo"), Kind(SymbolKind::Using)))); |
| 262 | } |
| 263 | |
Kadir Cetinkaya | e7eb27a | 2019-03-08 08:30:20 +0000 | [diff] [blame] | 264 | TEST(IndexTest, Constructors) { |
| 265 | std::string Code = R"cpp( |
| 266 | struct Foo { |
| 267 | Foo(int); |
| 268 | ~Foo(); |
| 269 | }; |
| 270 | )cpp"; |
| 271 | auto Index = std::make_shared<Indexer>(); |
| 272 | IndexingOptions Opts; |
| 273 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 274 | EXPECT_THAT( |
| 275 | Index->Symbols, |
| 276 | UnorderedElementsAre( |
| 277 | AllOf(QName("Foo"), Kind(SymbolKind::Struct), |
| 278 | WrittenAt(Position(2, 12))), |
| 279 | AllOf(QName("Foo::Foo"), Kind(SymbolKind::Constructor), |
| 280 | WrittenAt(Position(3, 7))), |
| 281 | AllOf(QName("Foo"), Kind(SymbolKind::Struct), |
| 282 | HasRole(SymbolRole::NameReference), WrittenAt(Position(3, 7))), |
| 283 | AllOf(QName("Foo::~Foo"), Kind(SymbolKind::Destructor), |
| 284 | WrittenAt(Position(4, 7))), |
| 285 | AllOf(QName("Foo"), Kind(SymbolKind::Struct), |
| 286 | HasRole(SymbolRole::NameReference), |
| 287 | WrittenAt(Position(4, 8))))); |
| 288 | } |
| 289 | |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 290 | } // namespace |
| 291 | } // namespace index |
| 292 | } // namespace clang |