2018-11-28 00:40:46 +08:00
|
|
|
//===-- SymbolInfoTests.cpp -----------------------*- 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-11-28 00:40:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "Annotations.h"
|
|
|
|
#include "ClangdUnit.h"
|
|
|
|
#include "Compiler.h"
|
|
|
|
#include "Matchers.h"
|
|
|
|
#include "SyncAPI.h"
|
|
|
|
#include "TestFS.h"
|
|
|
|
#include "TestTU.h"
|
|
|
|
#include "XRefs.h"
|
|
|
|
#include "index/FileIndex.h"
|
|
|
|
#include "index/SymbolCollector.h"
|
|
|
|
#include "clang/Index/IndexingAction.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace {
|
|
|
|
|
2019-05-06 18:08:47 +08:00
|
|
|
using ::testing::ElementsAreArray;
|
2018-11-28 00:40:46 +08:00
|
|
|
|
|
|
|
auto CreateExpectedSymbolDetails = [](const std::string &name,
|
|
|
|
const std::string &container,
|
|
|
|
const std::string &USR) {
|
|
|
|
return SymbolDetails{name, container, USR, SymbolID(USR)};
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(SymbolInfoTests, All) {
|
|
|
|
std::pair<const char *, std::vector<SymbolDetails>>
|
|
|
|
TestInputExpectedOutput[] = {
|
2019-01-03 21:28:05 +08:00
|
|
|
{
|
|
|
|
R"cpp( // Simple function reference - declaration
|
2018-11-28 00:40:46 +08:00
|
|
|
void foo();
|
|
|
|
int bar() {
|
|
|
|
fo^o();
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("foo", "", "c:@F@foo#")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Simple function reference - definition
|
2018-11-28 00:40:46 +08:00
|
|
|
void foo() {}
|
|
|
|
int bar() {
|
|
|
|
fo^o();
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("foo", "", "c:@F@foo#")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Function in namespace reference
|
2018-11-28 00:40:46 +08:00
|
|
|
namespace bar {
|
|
|
|
void foo();
|
|
|
|
int baz() {
|
|
|
|
fo^o();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("foo", "bar::", "c:@N@bar@F@foo#")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Function in different namespace reference
|
2018-11-28 00:40:46 +08:00
|
|
|
namespace bar {
|
|
|
|
void foo();
|
|
|
|
}
|
|
|
|
namespace barbar {
|
|
|
|
int baz() {
|
|
|
|
bar::fo^o();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("foo", "bar::", "c:@N@bar@F@foo#")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Function in global namespace reference
|
2018-11-28 00:40:46 +08:00
|
|
|
void foo();
|
|
|
|
namespace Nbar {
|
|
|
|
namespace Nbaz {
|
|
|
|
int baz() {
|
|
|
|
::fo^o();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("foo", "", "c:@F@foo#")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Function in anonymous namespace reference
|
2018-11-28 00:40:46 +08:00
|
|
|
namespace {
|
|
|
|
void foo();
|
|
|
|
}
|
|
|
|
namespace barbar {
|
|
|
|
int baz() {
|
|
|
|
fo^o();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("foo", "(anonymous)",
|
|
|
|
"c:TestTU.cpp@aN@F@foo#")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Function reference - ADL
|
2018-11-28 00:40:46 +08:00
|
|
|
namespace bar {
|
|
|
|
struct BarType {};
|
|
|
|
void foo(const BarType&);
|
|
|
|
}
|
|
|
|
namespace barbar {
|
|
|
|
int baz() {
|
|
|
|
bar::BarType b;
|
|
|
|
fo^o(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails(
|
|
|
|
"foo", "bar::", "c:@N@bar@F@foo#&1$@N@bar@S@BarType#")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Global value reference
|
2018-11-28 00:40:46 +08:00
|
|
|
int value;
|
|
|
|
void foo(int) { }
|
|
|
|
void bar() {
|
|
|
|
foo(val^ue);
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("value", "", "c:@value")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Local value reference
|
2018-11-28 00:40:46 +08:00
|
|
|
void foo() { int aaa; int bbb = aa^a; }
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("aaa", "foo",
|
|
|
|
"c:TestTU.cpp@49@F@foo#@aaa")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Function param
|
2018-11-28 00:40:46 +08:00
|
|
|
void bar(int aaa) {
|
|
|
|
int bbb = a^aa;
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("aaa", "bar",
|
|
|
|
"c:TestTU.cpp@38@F@bar#I#@aaa")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Lambda capture
|
2018-11-28 00:40:46 +08:00
|
|
|
int ii;
|
|
|
|
auto lam = [ii]() {
|
|
|
|
return i^i;
|
|
|
|
};
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("ii", "", "c:@ii")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Macro reference
|
2018-11-28 00:40:46 +08:00
|
|
|
#define MACRO 5\nint i = MAC^RO;
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("MACRO", "",
|
2019-01-29 19:19:15 +08:00
|
|
|
"c:TestTU.cpp@38@macro@MACRO")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Macro reference
|
|
|
|
#define MACRO 5\nint i = MACRO^;
|
|
|
|
)cpp",
|
|
|
|
{CreateExpectedSymbolDetails("MACRO", "",
|
|
|
|
"c:TestTU.cpp@38@macro@MACRO")}},
|
2019-01-03 21:28:05 +08:00
|
|
|
{
|
|
|
|
R"cpp( // Multiple symbols returned - using overloaded function name
|
2018-11-28 00:40:46 +08:00
|
|
|
void foo() {}
|
|
|
|
void foo(bool) {}
|
|
|
|
void foo(int) {}
|
|
|
|
namespace bar {
|
|
|
|
using ::fo^o;
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("foo", "", "c:@F@foo#"),
|
|
|
|
CreateExpectedSymbolDetails("foo", "", "c:@F@foo#b#"),
|
2019-02-26 22:23:47 +08:00
|
|
|
CreateExpectedSymbolDetails("foo", "", "c:@F@foo#I#"),
|
|
|
|
CreateExpectedSymbolDetails("foo", "bar::", "c:@N@bar@UD@foo")}},
|
2019-01-03 21:28:05 +08:00
|
|
|
{
|
|
|
|
R"cpp( // Multiple symbols returned - implicit conversion
|
2018-11-28 00:40:46 +08:00
|
|
|
struct foo {};
|
|
|
|
struct bar {
|
|
|
|
bar(const foo&) {}
|
|
|
|
};
|
|
|
|
void func_baz1(bar) {}
|
|
|
|
void func_baz2() {
|
|
|
|
foo ff;
|
|
|
|
func_baz1(f^f);
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-02-21 22:48:33 +08:00
|
|
|
{CreateExpectedSymbolDetails(
|
|
|
|
"ff", "func_baz2", "c:TestTU.cpp@218@F@func_baz2#@ff")}},
|
2019-01-03 21:28:05 +08:00
|
|
|
{
|
|
|
|
R"cpp( // Type reference - declaration
|
2018-11-28 00:40:46 +08:00
|
|
|
struct foo;
|
|
|
|
void bar(fo^o*);
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("foo", "", "c:@S@foo")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Type reference - definition
|
2018-11-28 00:40:46 +08:00
|
|
|
struct foo {};
|
|
|
|
void bar(fo^o*);
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("foo", "", "c:@S@foo")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Type Reference - template argumen
|
2018-11-28 00:40:46 +08:00
|
|
|
struct foo {};
|
|
|
|
template<class T> struct bar {};
|
|
|
|
void baz() {
|
|
|
|
bar<fo^o> b;
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("foo", "", "c:@S@foo")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Template parameter reference - type param
|
2018-11-28 00:40:46 +08:00
|
|
|
template<class TT> struct bar {
|
|
|
|
T^T t;
|
|
|
|
};
|
|
|
|
)cpp",
|
2019-02-21 17:55:00 +08:00
|
|
|
{CreateExpectedSymbolDetails("TT", "bar::", "c:TestTU.cpp@65")}},
|
2019-01-03 21:28:05 +08:00
|
|
|
{
|
|
|
|
R"cpp( // Template parameter reference - type param
|
2018-11-28 00:40:46 +08:00
|
|
|
template<int NN> struct bar {
|
|
|
|
int a = N^N;
|
|
|
|
};
|
|
|
|
)cpp",
|
2019-02-21 17:55:00 +08:00
|
|
|
{CreateExpectedSymbolDetails("NN", "bar::", "c:TestTU.cpp@65")}},
|
2019-01-03 21:28:05 +08:00
|
|
|
{
|
|
|
|
R"cpp( // Class member reference - objec
|
2018-11-28 00:40:46 +08:00
|
|
|
struct foo {
|
|
|
|
int aa;
|
|
|
|
};
|
|
|
|
void bar() {
|
|
|
|
foo f;
|
|
|
|
f.a^a;
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("aa", "foo::", "c:@S@foo@FI@aa")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Class member reference - pointer
|
2018-11-28 00:40:46 +08:00
|
|
|
struct foo {
|
|
|
|
int aa;
|
|
|
|
};
|
|
|
|
void bar() {
|
|
|
|
&foo::a^a;
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("aa", "foo::", "c:@S@foo@FI@aa")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Class method reference - objec
|
2018-11-28 00:40:46 +08:00
|
|
|
struct foo {
|
|
|
|
void aa() {}
|
|
|
|
};
|
|
|
|
void bar() {
|
|
|
|
foo f;
|
|
|
|
f.a^a();
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("aa", "foo::", "c:@S@foo@F@aa#")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Class method reference - pointer
|
2018-11-28 00:40:46 +08:00
|
|
|
struct foo {
|
|
|
|
void aa() {}
|
|
|
|
};
|
|
|
|
void bar() {
|
|
|
|
&foo::a^a;
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("aa", "foo::", "c:@S@foo@F@aa#")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Typedef
|
2018-11-28 00:40:46 +08:00
|
|
|
typedef int foo;
|
|
|
|
void bar() {
|
|
|
|
fo^o a;
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("foo", "", "c:TestTU.cpp@T@foo")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Type alias
|
2018-11-28 00:40:46 +08:00
|
|
|
using foo = int;
|
|
|
|
void bar() {
|
|
|
|
fo^o a;
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("foo", "", "c:@foo")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Namespace reference
|
2018-11-28 00:40:46 +08:00
|
|
|
namespace foo {}
|
|
|
|
using namespace fo^o;
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("foo", "", "c:@N@foo")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Enum value reference
|
2018-11-28 00:40:46 +08:00
|
|
|
enum foo { bar, baz };
|
|
|
|
void f() {
|
|
|
|
foo fff = ba^r;
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("bar", "foo", "c:@E@foo@bar")}},
|
|
|
|
{
|
|
|
|
R"cpp( // Enum class value reference
|
2018-11-28 00:40:46 +08:00
|
|
|
enum class foo { bar, baz };
|
|
|
|
void f() {
|
|
|
|
foo fff = foo::ba^r;
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{CreateExpectedSymbolDetails("bar", "foo::", "c:@E@foo@bar")}},
|
2019-02-11 21:03:08 +08:00
|
|
|
{
|
|
|
|
R"cpp( // Parameters in declarations
|
|
|
|
void foo(int ba^r);
|
|
|
|
)cpp",
|
|
|
|
{CreateExpectedSymbolDetails("bar", "foo",
|
|
|
|
"c:TestTU.cpp@50@F@foo#I#@bar")}},
|
2019-01-03 21:28:05 +08:00
|
|
|
{
|
|
|
|
R"cpp( // Type inferrence with auto keyword
|
2018-11-28 00:40:46 +08:00
|
|
|
struct foo {};
|
|
|
|
foo getfoo() { return foo{}; }
|
|
|
|
void f() {
|
|
|
|
au^to a = getfoo();
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{/* not implemented */}},
|
|
|
|
{
|
|
|
|
R"cpp( // decltype
|
2018-11-28 00:40:46 +08:00
|
|
|
struct foo {};
|
|
|
|
void f() {
|
|
|
|
foo f;
|
|
|
|
declt^ype(f);
|
|
|
|
}
|
|
|
|
)cpp",
|
2019-01-03 21:28:05 +08:00
|
|
|
{/* not implemented */}},
|
|
|
|
};
|
2018-11-28 00:40:46 +08:00
|
|
|
|
|
|
|
for (const auto &T : TestInputExpectedOutput) {
|
|
|
|
Annotations TestInput(T.first);
|
|
|
|
auto AST = TestTU::withCode(TestInput.code()).build();
|
|
|
|
|
|
|
|
EXPECT_THAT(getSymbolInfo(AST, TestInput.point()),
|
|
|
|
ElementsAreArray(T.second))
|
|
|
|
<< T.first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|