IndexTests.cpp 10.0 KB

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