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