2018-09-18 16:51:08 +08:00
|
|
|
//===--- IndexTests.cpp - Test indexing actions -----------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-09-18 16:51:08 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2019-02-18 19:30:43 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2018-09-18 16:51:08 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2019-02-18 19:30:43 +08:00
|
|
|
#include "clang/Basic/SourceLocation.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2018-09-18 16:51:08 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/FrontendAction.h"
|
|
|
|
#include "clang/Index/IndexDataConsumer.h"
|
|
|
|
#include "clang/Index/IndexSymbol.h"
|
|
|
|
#include "clang/Index/IndexingAction.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Tooling/Tooling.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2018-10-10 21:27:25 +08:00
|
|
|
#include "llvm/Support/VirtualFileSystem.h"
|
2018-09-18 16:51:08 +08:00
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace index {
|
2019-02-18 19:30:43 +08:00
|
|
|
namespace {
|
|
|
|
struct Position {
|
|
|
|
size_t Line = 0;
|
|
|
|
size_t Column = 0;
|
|
|
|
|
|
|
|
Position(size_t Line = 0, size_t Column = 0) : Line(Line), Column(Column) {}
|
|
|
|
|
|
|
|
static Position fromSourceLocation(SourceLocation Loc,
|
|
|
|
const SourceManager &SM) {
|
|
|
|
FileID FID;
|
|
|
|
unsigned Offset;
|
|
|
|
std::tie(FID, Offset) = SM.getDecomposedSpellingLoc(Loc);
|
|
|
|
Position P;
|
|
|
|
P.Line = SM.getLineNumber(FID, Offset);
|
|
|
|
P.Column = SM.getColumnNumber(FID, Offset);
|
|
|
|
return P;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool operator==(const Position &LHS, const Position &RHS) {
|
|
|
|
return std::tie(LHS.Line, LHS.Column) == std::tie(RHS.Line, RHS.Column);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Position &Pos) {
|
|
|
|
return OS << Pos.Line << ':' << Pos.Column;
|
|
|
|
}
|
2018-09-18 16:51:08 +08:00
|
|
|
|
|
|
|
struct TestSymbol {
|
|
|
|
std::string QName;
|
2019-02-18 19:30:43 +08:00
|
|
|
Position WrittenPos;
|
|
|
|
Position DeclPos;
|
2019-02-26 22:23:12 +08:00
|
|
|
SymbolInfo SymInfo;
|
2019-03-08 16:30:20 +08:00
|
|
|
SymbolRoleSet Roles;
|
2018-09-18 16:51:08 +08:00
|
|
|
// FIXME: add more information.
|
|
|
|
};
|
|
|
|
|
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const TestSymbol &S) {
|
2019-03-08 16:30:20 +08:00
|
|
|
return OS << S.QName << '[' << S.WrittenPos << ']' << '@' << S.DeclPos << '('
|
|
|
|
<< static_cast<unsigned>(S.SymInfo.Kind) << ')';
|
2018-09-18 16:51:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class Indexer : public IndexDataConsumer {
|
|
|
|
public:
|
2019-02-18 19:30:43 +08:00
|
|
|
void initialize(ASTContext &Ctx) override {
|
|
|
|
AST = &Ctx;
|
|
|
|
IndexDataConsumer::initialize(Ctx);
|
|
|
|
}
|
|
|
|
|
2019-12-16 17:33:56 +08:00
|
|
|
bool handleDeclOccurrence(const Decl *D, SymbolRoleSet Roles,
|
|
|
|
ArrayRef<SymbolRelation>, SourceLocation Loc,
|
|
|
|
ASTNodeInfo) override {
|
2018-09-18 16:51:08 +08:00
|
|
|
const auto *ND = llvm::dyn_cast<NamedDecl>(D);
|
|
|
|
if (!ND)
|
|
|
|
return true;
|
|
|
|
TestSymbol S;
|
2019-02-26 22:23:12 +08:00
|
|
|
S.SymInfo = getSymbolInfo(D);
|
2018-09-18 16:51:08 +08:00
|
|
|
S.QName = ND->getQualifiedNameAsString();
|
2019-02-18 19:30:43 +08:00
|
|
|
S.WrittenPos = Position::fromSourceLocation(Loc, AST->getSourceManager());
|
|
|
|
S.DeclPos =
|
|
|
|
Position::fromSourceLocation(D->getLocation(), AST->getSourceManager());
|
2019-03-08 16:30:20 +08:00
|
|
|
S.Roles = Roles;
|
2018-09-18 16:51:08 +08:00
|
|
|
Symbols.push_back(std::move(S));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-12-16 17:33:56 +08:00
|
|
|
bool handleMacroOccurrence(const IdentifierInfo *Name, const MacroInfo *MI,
|
|
|
|
SymbolRoleSet Roles, SourceLocation Loc) override {
|
2018-09-18 16:51:08 +08:00
|
|
|
TestSymbol S;
|
2019-03-08 18:18:40 +08:00
|
|
|
S.SymInfo = getSymbolInfoForMacro(*MI);
|
2020-01-29 03:23:46 +08:00
|
|
|
S.QName = std::string(Name->getName());
|
2019-03-08 18:18:40 +08:00
|
|
|
S.WrittenPos = Position::fromSourceLocation(Loc, AST->getSourceManager());
|
|
|
|
S.DeclPos = Position::fromSourceLocation(MI->getDefinitionLoc(),
|
|
|
|
AST->getSourceManager());
|
|
|
|
S.Roles = Roles;
|
2018-09-18 16:51:08 +08:00
|
|
|
Symbols.push_back(std::move(S));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<TestSymbol> Symbols;
|
2019-02-18 19:30:43 +08:00
|
|
|
const ASTContext *AST = nullptr;
|
2018-09-18 16:51:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class IndexAction : public ASTFrontendAction {
|
|
|
|
public:
|
|
|
|
IndexAction(std::shared_ptr<Indexer> Index,
|
|
|
|
IndexingOptions Opts = IndexingOptions())
|
|
|
|
: Index(std::move(Index)), Opts(Opts) {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
|
|
|
|
StringRef InFile) override {
|
|
|
|
class Consumer : public ASTConsumer {
|
|
|
|
std::shared_ptr<Indexer> Index;
|
|
|
|
std::shared_ptr<Preprocessor> PP;
|
|
|
|
IndexingOptions Opts;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Consumer(std::shared_ptr<Indexer> Index, std::shared_ptr<Preprocessor> PP,
|
|
|
|
IndexingOptions Opts)
|
|
|
|
: Index(std::move(Index)), PP(std::move(PP)), Opts(Opts) {}
|
|
|
|
|
|
|
|
void HandleTranslationUnit(ASTContext &Ctx) override {
|
|
|
|
std::vector<Decl *> DeclsToIndex(
|
|
|
|
Ctx.getTranslationUnitDecl()->decls().begin(),
|
|
|
|
Ctx.getTranslationUnitDecl()->decls().end());
|
|
|
|
indexTopLevelDecls(Ctx, *PP, DeclsToIndex, *Index, Opts);
|
|
|
|
}
|
|
|
|
};
|
2019-08-15 07:04:18 +08:00
|
|
|
return std::make_unique<Consumer>(Index, CI.getPreprocessorPtr(), Opts);
|
2018-09-18 16:51:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<Indexer> Index;
|
|
|
|
IndexingOptions Opts;
|
|
|
|
};
|
|
|
|
|
2019-02-18 19:30:43 +08:00
|
|
|
using testing::AllOf;
|
2018-09-18 16:51:08 +08:00
|
|
|
using testing::Contains;
|
|
|
|
using testing::Not;
|
|
|
|
using testing::UnorderedElementsAre;
|
|
|
|
|
|
|
|
MATCHER_P(QName, Name, "") { return arg.QName == Name; }
|
2019-02-18 19:30:43 +08:00
|
|
|
MATCHER_P(WrittenAt, Pos, "") { return arg.WrittenPos == Pos; }
|
|
|
|
MATCHER_P(DeclAt, Pos, "") { return arg.DeclPos == Pos; }
|
2019-02-26 22:23:12 +08:00
|
|
|
MATCHER_P(Kind, SymKind, "") { return arg.SymInfo.Kind == SymKind; }
|
2019-03-08 16:30:20 +08:00
|
|
|
MATCHER_P(HasRole, Role, "") { return arg.Roles & static_cast<unsigned>(Role); }
|
2018-09-18 16:51:08 +08:00
|
|
|
|
|
|
|
TEST(IndexTest, Simple) {
|
|
|
|
auto Index = std::make_shared<Indexer>();
|
2019-08-30 17:29:34 +08:00
|
|
|
tooling::runToolOnCode(std::make_unique<IndexAction>(Index),
|
|
|
|
"class X {}; void f() {}");
|
2018-09-18 16:51:08 +08:00
|
|
|
EXPECT_THAT(Index->Symbols, UnorderedElementsAre(QName("X"), QName("f")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(IndexTest, IndexPreprocessorMacros) {
|
[index] Improve macro indexing support
The major change here is to index macro occurrences in more places than
before, specifically
* In non-expansion references such as `#if`, `#ifdef`, etc.
* When the macro is a reference to a builtin macro such as __LINE__.
* When using the preprocessor state instead of callbacks, we now include
all definition locations and undefinitions instead of just the latest
one (which may also have had the wrong location previously).
* When indexing an existing module file (.pcm), we now include module
macros, and we no longer report unrelated preprocessor macros during
indexing the module, which could have caused duplication.
Additionally, we now correctly obey the system symbol filter for macros,
so by default in system headers only definition/undefinition occurrences
are reported, but it can be configured to report references as well if
desired.
Extends FileIndexRecord to support occurrences of macros. Since the
design of this type is to keep a single list of entities organized by
source location, we incorporate macros into the existing DeclOccurrence
struct.
Differential Revision: https://reviews.llvm.org/D99758
2021-03-24 06:22:58 +08:00
|
|
|
std::string Code = R"cpp(
|
|
|
|
#define INDEX_MAC 1
|
|
|
|
#define INDEX_MAC_UNDEF 1
|
|
|
|
#undef INDEX_MAC_UNDEF
|
|
|
|
#define INDEX_MAC_REDEF 1
|
|
|
|
#undef INDEX_MAC_REDEF
|
|
|
|
#define INDEX_MAC_REDEF 2
|
|
|
|
)cpp";
|
2018-09-18 16:51:08 +08:00
|
|
|
auto Index = std::make_shared<Indexer>();
|
|
|
|
IndexingOptions Opts;
|
|
|
|
Opts.IndexMacrosInPreprocessor = true;
|
2019-08-30 17:29:34 +08:00
|
|
|
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
|
[index] Improve macro indexing support
The major change here is to index macro occurrences in more places than
before, specifically
* In non-expansion references such as `#if`, `#ifdef`, etc.
* When the macro is a reference to a builtin macro such as __LINE__.
* When using the preprocessor state instead of callbacks, we now include
all definition locations and undefinitions instead of just the latest
one (which may also have had the wrong location previously).
* When indexing an existing module file (.pcm), we now include module
macros, and we no longer report unrelated preprocessor macros during
indexing the module, which could have caused duplication.
Additionally, we now correctly obey the system symbol filter for macros,
so by default in system headers only definition/undefinition occurrences
are reported, but it can be configured to report references as well if
desired.
Extends FileIndexRecord to support occurrences of macros. Since the
design of this type is to keep a single list of entities organized by
source location, we incorporate macros into the existing DeclOccurrence
struct.
Differential Revision: https://reviews.llvm.org/D99758
2021-03-24 06:22:58 +08:00
|
|
|
EXPECT_THAT(Index->Symbols,
|
|
|
|
Contains(AllOf(QName("INDEX_MAC"), WrittenAt(Position(2, 13)),
|
|
|
|
DeclAt(Position(2, 13)),
|
|
|
|
HasRole(SymbolRole::Definition))));
|
|
|
|
EXPECT_THAT(
|
|
|
|
Index->Symbols,
|
|
|
|
AllOf(Contains(AllOf(QName("INDEX_MAC_UNDEF"), WrittenAt(Position(3, 13)),
|
|
|
|
DeclAt(Position(3, 13)),
|
|
|
|
HasRole(SymbolRole::Definition))),
|
|
|
|
Contains(AllOf(QName("INDEX_MAC_UNDEF"), WrittenAt(Position(4, 12)),
|
|
|
|
DeclAt(Position(3, 13)),
|
|
|
|
HasRole(SymbolRole::Undefinition)))));
|
|
|
|
EXPECT_THAT(
|
|
|
|
Index->Symbols,
|
|
|
|
AllOf(Contains(AllOf(QName("INDEX_MAC_REDEF"), WrittenAt(Position(5, 13)),
|
|
|
|
DeclAt(Position(5, 13)),
|
|
|
|
HasRole(SymbolRole::Definition))),
|
|
|
|
Contains(AllOf(QName("INDEX_MAC_REDEF"), WrittenAt(Position(6, 12)),
|
|
|
|
DeclAt(Position(5, 13)),
|
|
|
|
HasRole(SymbolRole::Undefinition))),
|
|
|
|
Contains(AllOf(QName("INDEX_MAC_REDEF"), WrittenAt(Position(7, 13)),
|
|
|
|
DeclAt(Position(7, 13)),
|
|
|
|
HasRole(SymbolRole::Definition)))));
|
2018-09-18 16:51:08 +08:00
|
|
|
|
|
|
|
Opts.IndexMacrosInPreprocessor = false;
|
|
|
|
Index->Symbols.clear();
|
2019-08-30 17:29:34 +08:00
|
|
|
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
|
2018-09-18 16:51:08 +08:00
|
|
|
EXPECT_THAT(Index->Symbols, UnorderedElementsAre());
|
|
|
|
}
|
|
|
|
|
2019-02-11 21:02:21 +08:00
|
|
|
TEST(IndexTest, IndexParametersInDecls) {
|
|
|
|
std::string Code = "void foo(int bar);";
|
|
|
|
auto Index = std::make_shared<Indexer>();
|
|
|
|
IndexingOptions Opts;
|
|
|
|
Opts.IndexFunctionLocals = true;
|
|
|
|
Opts.IndexParametersInDeclarations = true;
|
2019-08-30 17:29:34 +08:00
|
|
|
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
|
2019-02-11 21:02:21 +08:00
|
|
|
EXPECT_THAT(Index->Symbols, Contains(QName("bar")));
|
|
|
|
|
|
|
|
Opts.IndexParametersInDeclarations = false;
|
|
|
|
Index->Symbols.clear();
|
2019-08-30 17:29:34 +08:00
|
|
|
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
|
2019-02-11 21:02:21 +08:00
|
|
|
EXPECT_THAT(Index->Symbols, Not(Contains(QName("bar"))));
|
|
|
|
}
|
|
|
|
|
2019-02-18 19:30:43 +08:00
|
|
|
TEST(IndexTest, IndexExplicitTemplateInstantiation) {
|
|
|
|
std::string Code = R"cpp(
|
|
|
|
template <typename T>
|
|
|
|
struct Foo { void bar() {} };
|
|
|
|
template <>
|
|
|
|
struct Foo<int> { void bar() {} };
|
|
|
|
void foo() {
|
|
|
|
Foo<char> abc;
|
|
|
|
Foo<int> b;
|
|
|
|
}
|
|
|
|
)cpp";
|
|
|
|
auto Index = std::make_shared<Indexer>();
|
|
|
|
IndexingOptions Opts;
|
2019-08-30 17:29:34 +08:00
|
|
|
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
|
2019-02-18 19:30:43 +08:00
|
|
|
EXPECT_THAT(Index->Symbols,
|
|
|
|
AllOf(Contains(AllOf(QName("Foo"), WrittenAt(Position(8, 7)),
|
|
|
|
DeclAt(Position(5, 12)))),
|
|
|
|
Contains(AllOf(QName("Foo"), WrittenAt(Position(7, 7)),
|
|
|
|
DeclAt(Position(3, 12))))));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(IndexTest, IndexTemplateInstantiationPartial) {
|
|
|
|
std::string Code = R"cpp(
|
|
|
|
template <typename T1, typename T2>
|
|
|
|
struct Foo { void bar() {} };
|
|
|
|
template <typename T>
|
|
|
|
struct Foo<T, int> { void bar() {} };
|
|
|
|
void foo() {
|
|
|
|
Foo<char, char> abc;
|
|
|
|
Foo<int, int> b;
|
|
|
|
}
|
|
|
|
)cpp";
|
|
|
|
auto Index = std::make_shared<Indexer>();
|
|
|
|
IndexingOptions Opts;
|
2019-08-30 17:29:34 +08:00
|
|
|
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
|
2019-02-18 19:30:43 +08:00
|
|
|
EXPECT_THAT(Index->Symbols,
|
|
|
|
Contains(AllOf(QName("Foo"), WrittenAt(Position(8, 7)),
|
|
|
|
DeclAt(Position(5, 12)))));
|
|
|
|
}
|
|
|
|
|
2019-02-21 17:52:33 +08:00
|
|
|
TEST(IndexTest, IndexTypeParmDecls) {
|
|
|
|
std::string Code = R"cpp(
|
|
|
|
template <typename T, int I, template<typename> class C, typename NoRef>
|
|
|
|
struct Foo {
|
|
|
|
T t = I;
|
|
|
|
C<int> x;
|
|
|
|
};
|
|
|
|
)cpp";
|
|
|
|
auto Index = std::make_shared<Indexer>();
|
|
|
|
IndexingOptions Opts;
|
2019-08-30 17:29:34 +08:00
|
|
|
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
|
2019-02-21 17:52:33 +08:00
|
|
|
EXPECT_THAT(Index->Symbols, AllOf(Not(Contains(QName("Foo::T"))),
|
|
|
|
Not(Contains(QName("Foo::I"))),
|
|
|
|
Not(Contains(QName("Foo::C"))),
|
|
|
|
Not(Contains(QName("Foo::NoRef")))));
|
|
|
|
|
|
|
|
Opts.IndexTemplateParameters = true;
|
|
|
|
Index->Symbols.clear();
|
2019-08-30 17:29:34 +08:00
|
|
|
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
|
2019-02-21 17:52:33 +08:00
|
|
|
EXPECT_THAT(Index->Symbols,
|
2020-01-30 21:07:42 +08:00
|
|
|
AllOf(Contains(AllOf(QName("Foo::T"),
|
|
|
|
Kind(SymbolKind::TemplateTypeParm))),
|
|
|
|
Contains(AllOf(QName("Foo::I"),
|
|
|
|
Kind(SymbolKind::NonTypeTemplateParm))),
|
|
|
|
Contains(AllOf(QName("Foo::C"),
|
|
|
|
Kind(SymbolKind::TemplateTemplateParm))),
|
|
|
|
Contains(QName("Foo::NoRef"))));
|
2019-02-21 17:52:33 +08:00
|
|
|
}
|
|
|
|
|
2019-02-26 22:23:12 +08:00
|
|
|
TEST(IndexTest, UsingDecls) {
|
|
|
|
std::string Code = R"cpp(
|
|
|
|
void foo(int bar);
|
|
|
|
namespace std {
|
|
|
|
using ::foo;
|
|
|
|
}
|
|
|
|
)cpp";
|
|
|
|
auto Index = std::make_shared<Indexer>();
|
|
|
|
IndexingOptions Opts;
|
2019-08-30 17:29:34 +08:00
|
|
|
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
|
2019-02-26 22:23:12 +08:00
|
|
|
EXPECT_THAT(Index->Symbols,
|
|
|
|
Contains(AllOf(QName("std::foo"), Kind(SymbolKind::Using))));
|
|
|
|
}
|
|
|
|
|
2019-03-08 16:30:20 +08:00
|
|
|
TEST(IndexTest, Constructors) {
|
|
|
|
std::string Code = R"cpp(
|
|
|
|
struct Foo {
|
|
|
|
Foo(int);
|
|
|
|
~Foo();
|
|
|
|
};
|
|
|
|
)cpp";
|
|
|
|
auto Index = std::make_shared<Indexer>();
|
|
|
|
IndexingOptions Opts;
|
2019-08-30 17:29:34 +08:00
|
|
|
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
|
2019-03-08 16:30:20 +08:00
|
|
|
EXPECT_THAT(
|
|
|
|
Index->Symbols,
|
|
|
|
UnorderedElementsAre(
|
|
|
|
AllOf(QName("Foo"), Kind(SymbolKind::Struct),
|
|
|
|
WrittenAt(Position(2, 12))),
|
|
|
|
AllOf(QName("Foo::Foo"), Kind(SymbolKind::Constructor),
|
|
|
|
WrittenAt(Position(3, 7))),
|
|
|
|
AllOf(QName("Foo"), Kind(SymbolKind::Struct),
|
|
|
|
HasRole(SymbolRole::NameReference), WrittenAt(Position(3, 7))),
|
|
|
|
AllOf(QName("Foo::~Foo"), Kind(SymbolKind::Destructor),
|
|
|
|
WrittenAt(Position(4, 7))),
|
|
|
|
AllOf(QName("Foo"), Kind(SymbolKind::Struct),
|
|
|
|
HasRole(SymbolRole::NameReference),
|
|
|
|
WrittenAt(Position(4, 8)))));
|
|
|
|
}
|
|
|
|
|
2020-01-21 23:42:18 +08:00
|
|
|
TEST(IndexTest, InjecatedNameClass) {
|
|
|
|
std::string Code = R"cpp(
|
|
|
|
template <typename T>
|
|
|
|
class Foo {
|
|
|
|
void f(Foo x);
|
|
|
|
};
|
|
|
|
)cpp";
|
|
|
|
auto Index = std::make_shared<Indexer>();
|
|
|
|
IndexingOptions Opts;
|
|
|
|
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
|
|
|
|
EXPECT_THAT(Index->Symbols,
|
|
|
|
UnorderedElementsAre(AllOf(QName("Foo"), Kind(SymbolKind::Class),
|
|
|
|
WrittenAt(Position(3, 11))),
|
|
|
|
AllOf(QName("Foo::f"),
|
|
|
|
Kind(SymbolKind::InstanceMethod),
|
|
|
|
WrittenAt(Position(4, 12))),
|
|
|
|
AllOf(QName("Foo"), Kind(SymbolKind::Class),
|
|
|
|
HasRole(SymbolRole::Reference),
|
|
|
|
WrittenAt(Position(4, 14)))));
|
|
|
|
}
|
|
|
|
|
2020-02-12 21:42:18 +08:00
|
|
|
TEST(IndexTest, VisitDefaultArgs) {
|
|
|
|
std::string Code = R"cpp(
|
|
|
|
int var = 0;
|
|
|
|
void f(int s = var) {}
|
|
|
|
)cpp";
|
|
|
|
auto Index = std::make_shared<Indexer>();
|
|
|
|
IndexingOptions Opts;
|
|
|
|
Opts.IndexFunctionLocals = true;
|
|
|
|
Opts.IndexParametersInDeclarations = true;
|
|
|
|
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
|
|
|
|
EXPECT_THAT(Index->Symbols,
|
|
|
|
Contains(AllOf(QName("var"), HasRole(SymbolRole::Reference),
|
|
|
|
WrittenAt(Position(3, 20)))));
|
|
|
|
}
|
|
|
|
|
2020-08-24 10:15:12 +08:00
|
|
|
TEST(IndexTest, RelationBaseOf) {
|
|
|
|
std::string Code = R"cpp(
|
|
|
|
class A {};
|
|
|
|
template <typename> class B {};
|
|
|
|
class C : B<A> {};
|
|
|
|
)cpp";
|
|
|
|
auto Index = std::make_shared<Indexer>();
|
|
|
|
tooling::runToolOnCode(std::make_unique<IndexAction>(Index), Code);
|
|
|
|
// A should not be the base of anything.
|
|
|
|
EXPECT_THAT(Index->Symbols,
|
|
|
|
Contains(AllOf(QName("A"), HasRole(SymbolRole::Reference),
|
|
|
|
Not(HasRole(SymbolRole::RelationBaseOf)))));
|
|
|
|
}
|
|
|
|
|
2018-09-18 16:51:08 +08:00
|
|
|
} // namespace
|
|
|
|
} // namespace index
|
|
|
|
} // namespace clang
|