clang-tools  16.0.0git
ASTSignalsTests.cpp
Go to the documentation of this file.
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 }
72 } // namespace
73 } // namespace clangd
74 } // namespace clang
clang::clangd::TEST
TEST(BackgroundQueueTest, Priority)
Definition: BackgroundIndexTests.cpp:750
TestTU.h
clang::clangd::var
Symbol var(llvm::StringRef Name)
Definition: TestIndex.cpp:72
clang::clangd::cls
Symbol cls(llvm::StringRef Name)
Definition: TestIndex.cpp:64
clang::clangd::func
Symbol func(llvm::StringRef Name)
Definition: TestIndex.cpp:60
clang::clangd::ns
Symbol ns(llvm::StringRef Name)
Definition: TestIndex.cpp:76
clang::clangd::TestTU::withCode
static TestTU withCode(llvm::StringRef Code)
Definition: TestTU.h:36
clang::clangd::sym
Symbol sym(llvm::StringRef QName, index::SymbolKind Kind, llvm::StringRef USRFormat)
Definition: TestIndex.cpp:39
ASTSignals.h
ID
static char ID
Definition: Logger.cpp:74
TestIndex.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::ASTSignals::derive
static ASTSignals derive(const ParsedAST &AST)
Definition: ASTSignals.cpp:16