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" |
| 10 | #include "clang/AST/Decl.h" |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 11 | #include "clang/Frontend/CompilerInstance.h" |
| 12 | #include "clang/Frontend/FrontendAction.h" |
| 13 | #include "clang/Index/IndexDataConsumer.h" |
| 14 | #include "clang/Index/IndexSymbol.h" |
| 15 | #include "clang/Index/IndexingAction.h" |
| 16 | #include "clang/Lex/Preprocessor.h" |
| 17 | #include "clang/Tooling/Tooling.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 19 | #include "llvm/Support/VirtualFileSystem.h" |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 20 | #include "gmock/gmock.h" |
| 21 | #include "gtest/gtest.h" |
| 22 | #include <memory> |
| 23 | |
| 24 | namespace clang { |
| 25 | namespace index { |
| 26 | |
| 27 | struct TestSymbol { |
| 28 | std::string QName; |
| 29 | // FIXME: add more information. |
| 30 | }; |
| 31 | |
| 32 | llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const TestSymbol &S) { |
| 33 | return OS << S.QName; |
| 34 | } |
| 35 | |
| 36 | namespace { |
| 37 | class Indexer : public IndexDataConsumer { |
| 38 | public: |
| 39 | bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles, |
| 40 | ArrayRef<SymbolRelation>, SourceLocation, |
| 41 | ASTNodeInfo) override { |
| 42 | const auto *ND = llvm::dyn_cast<NamedDecl>(D); |
| 43 | if (!ND) |
| 44 | return true; |
| 45 | TestSymbol S; |
| 46 | S.QName = ND->getQualifiedNameAsString(); |
| 47 | Symbols.push_back(std::move(S)); |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | bool handleMacroOccurence(const IdentifierInfo *Name, const MacroInfo *, |
| 52 | SymbolRoleSet, SourceLocation) override { |
| 53 | TestSymbol S; |
| 54 | S.QName = Name->getName(); |
| 55 | Symbols.push_back(std::move(S)); |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | std::vector<TestSymbol> Symbols; |
| 60 | }; |
| 61 | |
| 62 | class IndexAction : public ASTFrontendAction { |
| 63 | public: |
| 64 | IndexAction(std::shared_ptr<Indexer> Index, |
| 65 | IndexingOptions Opts = IndexingOptions()) |
| 66 | : Index(std::move(Index)), Opts(Opts) {} |
| 67 | |
| 68 | protected: |
| 69 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
| 70 | StringRef InFile) override { |
| 71 | class Consumer : public ASTConsumer { |
| 72 | std::shared_ptr<Indexer> Index; |
| 73 | std::shared_ptr<Preprocessor> PP; |
| 74 | IndexingOptions Opts; |
| 75 | |
| 76 | public: |
| 77 | Consumer(std::shared_ptr<Indexer> Index, std::shared_ptr<Preprocessor> PP, |
| 78 | IndexingOptions Opts) |
| 79 | : Index(std::move(Index)), PP(std::move(PP)), Opts(Opts) {} |
| 80 | |
| 81 | void HandleTranslationUnit(ASTContext &Ctx) override { |
| 82 | std::vector<Decl *> DeclsToIndex( |
| 83 | Ctx.getTranslationUnitDecl()->decls().begin(), |
| 84 | Ctx.getTranslationUnitDecl()->decls().end()); |
| 85 | indexTopLevelDecls(Ctx, *PP, DeclsToIndex, *Index, Opts); |
| 86 | } |
| 87 | }; |
| 88 | return llvm::make_unique<Consumer>(Index, CI.getPreprocessorPtr(), Opts); |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | std::shared_ptr<Indexer> Index; |
| 93 | IndexingOptions Opts; |
| 94 | }; |
| 95 | |
| 96 | using testing::Contains; |
| 97 | using testing::Not; |
| 98 | using testing::UnorderedElementsAre; |
| 99 | |
| 100 | MATCHER_P(QName, Name, "") { return arg.QName == Name; } |
| 101 | |
| 102 | TEST(IndexTest, Simple) { |
| 103 | auto Index = std::make_shared<Indexer>(); |
| 104 | tooling::runToolOnCode(new IndexAction(Index), "class X {}; void f() {}"); |
| 105 | EXPECT_THAT(Index->Symbols, UnorderedElementsAre(QName("X"), QName("f"))); |
| 106 | } |
| 107 | |
| 108 | TEST(IndexTest, IndexPreprocessorMacros) { |
| 109 | std::string Code = "#define INDEX_MAC 1"; |
| 110 | auto Index = std::make_shared<Indexer>(); |
| 111 | IndexingOptions Opts; |
| 112 | Opts.IndexMacrosInPreprocessor = true; |
| 113 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 114 | EXPECT_THAT(Index->Symbols, Contains(QName("INDEX_MAC"))); |
| 115 | |
| 116 | Opts.IndexMacrosInPreprocessor = false; |
| 117 | Index->Symbols.clear(); |
| 118 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 119 | EXPECT_THAT(Index->Symbols, UnorderedElementsAre()); |
| 120 | } |
| 121 | |
Kadir Cetinkaya | 0468fc0 | 2019-02-11 13:02:21 +0000 | [diff] [blame^] | 122 | TEST(IndexTest, IndexParametersInDecls) { |
| 123 | std::string Code = "void foo(int bar);"; |
| 124 | auto Index = std::make_shared<Indexer>(); |
| 125 | IndexingOptions Opts; |
| 126 | Opts.IndexFunctionLocals = true; |
| 127 | Opts.IndexParametersInDeclarations = true; |
| 128 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 129 | EXPECT_THAT(Index->Symbols, Contains(QName("bar"))); |
| 130 | |
| 131 | Opts.IndexParametersInDeclarations = false; |
| 132 | Index->Symbols.clear(); |
| 133 | tooling::runToolOnCode(new IndexAction(Index, Opts), Code); |
| 134 | EXPECT_THAT(Index->Symbols, Not(Contains(QName("bar")))); |
| 135 | } |
| 136 | |
Eric Liu | 4d22172 | 2018-09-18 08:51:08 +0000 | [diff] [blame] | 137 | } // namespace |
| 138 | } // namespace index |
| 139 | } // namespace clang |