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; |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 60 | // FIXME: add more information. |
| 61 | }; |
| 62 | |
| 63 | llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const TestSymbol &S) { |
Kadir Cetinkaya | f53621b | 2019-02-18 11:30:43 +0000 | [diff] [blame] | 64 | return OS << S.QName << '[' << S.WrittenPos << ']' << '@' << S.DeclPos; |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 67 | class Indexer : public IndexDataConsumer { |
| 68 | public: |
Kadir Cetinkaya | f53621b | 2019-02-18 11:30:43 +0000 | [diff] [blame] | 69 | void initialize(ASTContext &Ctx) override { |
| 70 | AST = &Ctx; |
| 71 | IndexDataConsumer::initialize(Ctx); |
| 72 | } |
| 73 | |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 74 | bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles, |
Kadir Cetinkaya | f53621b | 2019-02-18 11:30:43 +0000 | [diff] [blame] | 75 | ArrayRef<SymbolRelation>, SourceLocation Loc, |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 76 | ASTNodeInfo) override { |
| 77 | const auto *ND = llvm::dyn_cast<NamedDecl>(D); |
| 78 | if (!ND) |
| 79 | return true; |
| 80 | TestSymbol S; |
| 81 | S.QName = ND->getQualifiedNameAsString(); |
Kadir Cetinkaya | f53621b | 2019-02-18 11:30:43 +0000 | [diff] [blame] | 82 | S.WrittenPos = Position::fromSourceLocation(Loc, AST->getSourceManager()); |
| 83 | S.DeclPos = |
| 84 | Position::fromSourceLocation(D->getLocation(), AST->getSourceManager()); |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 85 | Symbols.push_back(std::move(S)); |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | bool handleMacroOccurence(const IdentifierInfo *Name, const MacroInfo *, |
| 90 | SymbolRoleSet, SourceLocation) override { |
| 91 | TestSymbol S; |
| 92 | S.QName = Name->getName(); |
| 93 | Symbols.push_back(std::move(S)); |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | std::vector<TestSymbol> Symbols; |
Kadir Cetinkaya | f53621b | 2019-02-18 11:30:43 +0000 | [diff] [blame] | 98 | const ASTContext *AST = nullptr; |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | class IndexAction : public ASTFrontendAction { |
| 102 | public: |
| 103 | IndexAction(std::shared_ptr<Indexer> Index, |
| 104 | IndexingOptions Opts = IndexingOptions()) |
| 105 | : Index(std::move(Index)), Opts(Opts) {} |
| 106 | |
| 107 | protected: |
| 108 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
| 109 | StringRef InFile) override { |
| 110 | class Consumer : public ASTConsumer { |
| 111 | std::shared_ptr<Indexer> Index; |
| 112 | std::shared_ptr<Preprocessor> PP; |
| 113 | IndexingOptions Opts; |
| 114 | |
| 115 | public: |
| 116 | Consumer(std::shared_ptr<Indexer> Index, std::shared_ptr<Preprocessor> PP, |
| 117 | IndexingOptions Opts) |
| 118 | : Index(std::move(Index)), PP(std::move(PP)), Opts(Opts) {} |
| 119 | |
| 120 | void HandleTranslationUnit(ASTContext &Ctx) override { |
| 121 | std::vector<Decl *> DeclsToIndex( |
| 122 | Ctx.getTranslationUnitDecl()->decls().begin(), |
| 123 | Ctx.getTranslationUnitDecl()->decls().end()); |
| 124 | indexTopLevelDecls(Ctx, *PP, DeclsToIndex, *Index, Opts); |
| 125 | } |
| 126 | }; |
| 127 | return llvm::make_unique<Consumer>(Index, CI.getPreprocessorPtr(), Opts); |
| 128 | } |
| 129 | |
| 130 | private: |
| 131 | std::shared_ptr<Indexer> Index; |
| 132 | IndexingOptions Opts; |
| 133 | }; |
| 134 | |
Kadir Cetinkaya | f53621b | 2019-02-18 11:30:43 +0000 | [diff] [blame] | 135 | using testing::AllOf; |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 136 | using testing::Contains; |
| 137 | using testing::Not; |
| 138 | using testing::UnorderedElementsAre; |
| 139 | |
| 140 | MATCHER_P(QName, Name, "") { return arg.QName == Name; } |
Kadir Cetinkaya | f53621b | 2019-02-18 11:30:43 +0000 | [diff] [blame] | 141 | MATCHER_P(WrittenAt, Pos, "") { return arg.WrittenPos == Pos; } |
| 142 | MATCHER_P(DeclAt, Pos, "") { return arg.DeclPos == Pos; } |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 143 | |
| 144 | TEST(IndexTest, Simple) { |
| 145 | auto Index = std::make_shared<Indexer>(); |
| 146 | tooling::runToolOnCode(new IndexAction(Index), "class X {}; void f() {}"); |
| 147 | EXPECT_THAT(Index->Symbols, UnorderedElementsAre(QName("X"), QName("f"))); |
| 148 | } |
| 149 | |
| 150 | TEST(IndexTest, IndexPreprocessorMacros) { |
| 151 | std::string Code = "#define INDEX_MAC 1"; |
| 152 | auto Index = std::make_shared<Indexer>(); |
| 153 | IndexingOptions Opts; |
| 154 | Opts.IndexMacrosInPreprocessor = true; |
| 155 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 156 | EXPECT_THAT(Index->Symbols, Contains(QName("INDEX_MAC"))); |
| 157 | |
| 158 | Opts.IndexMacrosInPreprocessor = false; |
| 159 | Index->Symbols.clear(); |
| 160 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 161 | EXPECT_THAT(Index->Symbols, UnorderedElementsAre()); |
| 162 | } |
| 163 | |
Kadir Cetinkaya | 0468fc0 | 2019-02-11 13:02:21 +0000 | [diff] [blame] | 164 | TEST(IndexTest, IndexParametersInDecls) { |
| 165 | std::string Code = "void foo(int bar);"; |
| 166 | auto Index = std::make_shared<Indexer>(); |
| 167 | IndexingOptions Opts; |
| 168 | Opts.IndexFunctionLocals = true; |
| 169 | Opts.IndexParametersInDeclarations = true; |
| 170 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 171 | EXPECT_THAT(Index->Symbols, Contains(QName("bar"))); |
| 172 | |
| 173 | Opts.IndexParametersInDeclarations = false; |
| 174 | Index->Symbols.clear(); |
| 175 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 176 | EXPECT_THAT(Index->Symbols, Not(Contains(QName("bar")))); |
| 177 | } |
| 178 | |
Kadir Cetinkaya | f53621b | 2019-02-18 11:30:43 +0000 | [diff] [blame] | 179 | TEST(IndexTest, IndexExplicitTemplateInstantiation) { |
| 180 | std::string Code = R"cpp( |
| 181 | template <typename T> |
| 182 | struct Foo { void bar() {} }; |
| 183 | template <> |
| 184 | struct Foo<int> { void bar() {} }; |
| 185 | void foo() { |
| 186 | Foo<char> abc; |
| 187 | Foo<int> b; |
| 188 | } |
| 189 | )cpp"; |
| 190 | auto Index = std::make_shared<Indexer>(); |
| 191 | IndexingOptions Opts; |
| 192 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 193 | EXPECT_THAT(Index->Symbols, |
| 194 | AllOf(Contains(AllOf(QName("Foo"), WrittenAt(Position(8, 7)), |
| 195 | DeclAt(Position(5, 12)))), |
| 196 | Contains(AllOf(QName("Foo"), WrittenAt(Position(7, 7)), |
| 197 | DeclAt(Position(3, 12)))))); |
| 198 | } |
| 199 | |
| 200 | TEST(IndexTest, IndexTemplateInstantiationPartial) { |
| 201 | std::string Code = R"cpp( |
| 202 | template <typename T1, typename T2> |
| 203 | struct Foo { void bar() {} }; |
| 204 | template <typename T> |
| 205 | struct Foo<T, int> { void bar() {} }; |
| 206 | void foo() { |
| 207 | Foo<char, char> abc; |
| 208 | Foo<int, int> b; |
| 209 | } |
| 210 | )cpp"; |
| 211 | auto Index = std::make_shared<Indexer>(); |
| 212 | IndexingOptions Opts; |
| 213 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 214 | EXPECT_THAT(Index->Symbols, |
| 215 | Contains(AllOf(QName("Foo"), WrittenAt(Position(8, 7)), |
| 216 | DeclAt(Position(5, 12))))); |
| 217 | } |
| 218 | |
Kadir Cetinkaya | b780517 | 2019-02-21 09:52:33 +0000 | [diff] [blame^] | 219 | TEST(IndexTest, IndexTypeParmDecls) { |
| 220 | std::string Code = R"cpp( |
| 221 | template <typename T, int I, template<typename> class C, typename NoRef> |
| 222 | struct Foo { |
| 223 | T t = I; |
| 224 | C<int> x; |
| 225 | }; |
| 226 | )cpp"; |
| 227 | auto Index = std::make_shared<Indexer>(); |
| 228 | IndexingOptions Opts; |
| 229 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 230 | EXPECT_THAT(Index->Symbols, AllOf(Not(Contains(QName("Foo::T"))), |
| 231 | Not(Contains(QName("Foo::I"))), |
| 232 | Not(Contains(QName("Foo::C"))), |
| 233 | Not(Contains(QName("Foo::NoRef"))))); |
| 234 | |
| 235 | Opts.IndexTemplateParameters = true; |
| 236 | Index->Symbols.clear(); |
| 237 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 238 | EXPECT_THAT(Index->Symbols, |
| 239 | AllOf(Contains(QName("Foo::T")), Contains(QName("Foo::I")), |
| 240 | Contains(QName("Foo::C")), Contains(QName("Foo::NoRef")))); |
| 241 | } |
| 242 | |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 243 | } // namespace |
| 244 | } // namespace index |
| 245 | } // namespace clang |