| 1 |
//===-- ASTSignalsTests.cpp -------------------------------------*- 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 |
|
| 9 |
#include "ASTSignals.h" |
| 10 |
#include "TestIndex.h" |
| 11 |
#include "TestTU.h" |
| 12 |
#include "llvm/ADT/StringRef.h" |
| 13 |
#include "gmock/gmock.h" |
| 14 |
#include "gtest/gtest.h" |
| 15 |
|
| 16 |
namespace clang { |
| 17 |
namespace clangd { |
| 18 |
namespace { |
| 19 |
|
| 20 |
using ::testing::_; |
| 21 |
using ::testing::Pair; |
| 22 |
using ::testing::UnorderedElementsAre; |
| 23 |
|
| 24 |
TEST(ASTSignals, Derive) { |
| 25 |
TestTU TU = TestTU::withCode(R"cpp( |
| 26 |
namespace ns1 { |
| 27 |
namespace ns2 { |
| 28 |
namespace { |
| 29 |
int func() { |
| 30 |
tar::X a; |
| 31 |
a.Y = 1; |
| 32 |
return ADD(tar::kConst, a.Y, tar::foo()) + fooInNS2() + tar::foo(); |
| 33 |
} |
| 34 |
} // namespace |
| 35 |
} // namespace ns2 |
| 36 |
} // namespace ns1 |
| 37 |
)cpp"); |
| 38 |
|
| 39 |
TU.HeaderCode = R"cpp( |
| 40 |
#define ADD(x, y, z) (x + y + z) |
| 41 |
namespace tar { // A related namespace. |
| 42 |
int kConst = 5; |
| 43 |
int foo(); |
| 44 |
void bar(); // Unused symbols are not recorded. |
| 45 |
class X { |
| 46 |
public: int Y; |
| 47 |
}; |
| 48 |
} // namespace tar |
| 49 |
namespace ns1::ns2 { int fooInNS2(); }} |
| 50 |
)cpp"; |
| 51 |
ASTSignals Signals = ASTSignals::derive(TU.build()); |
| 52 |
std::vector<std::pair<StringRef, int>> NS; |
| 53 |
for (const auto &P : Signals.RelatedNamespaces) |
| 54 |
NS.emplace_back(P.getKey(), P.getValue()); |
| 55 |
EXPECT_THAT(NS, UnorderedElementsAre(Pair("ns1::", 1), Pair("ns1::ns2::", 1), |
| 56 |
Pair("tar::", /*foo, kConst, X*/ 3))); |
| 57 |
|
| 58 |
std::vector<std::pair<SymbolID, int>> Sym; |
| 59 |
for (const auto &P : Signals.ReferencedSymbols) |
| 60 |
Sym.emplace_back(P.getFirst(), P.getSecond()); |
| 61 |
EXPECT_THAT( |
| 62 |
Sym, |
| 63 |
UnorderedElementsAre( |
| 64 |
Pair(ns("tar").ID, 4), Pair(ns("ns1").ID, 1), |
| 65 |
Pair(ns("ns1::ns2").ID, 1), Pair(_ /*int func();*/, 1), |
| 66 |
Pair(cls("tar::X").ID, 1), Pair(var("tar::kConst").ID, 1), |
| 67 |
Pair(func("tar::foo").ID, 2), Pair(func("ns1::ns2::fooInNS2").ID, 1), |
| 68 |
Pair(sym("Y", index::SymbolKind::Variable, "@N@tar@S@X@FI@\\0").ID, |
| 69 |
2), |
| 70 |
Pair(_ /*a*/, 3))); |
| 71 |
EXPECT_EQ(Signals.InsertionDirective, Symbol::IncludeDirective::Include); |
| 72 |
} |
| 73 |
} // namespace |
| 74 |
} // namespace clangd |
| 75 |
} // namespace clang |