clang-tools  9.0.0
PrintASTTests.cpp
Go to the documentation of this file.
1 //===--- PrintASTTests.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 "AST.h"
10 #include "Annotations.h"
11 #include "Protocol.h"
12 #include "SourceCode.h"
13 #include "TestTU.h"
14 #include "clang/AST/RecursiveASTVisitor.h"
15 #include "gmock/gmock.h"
16 #include "gtest/gtest-param-test.h"
17 #include "gtest/gtest.h"
18 #include "gtest/internal/gtest-param-util-generated.h"
19 
20 namespace clang {
21 namespace clangd {
22 namespace {
23 
24 using ::testing::ElementsAreArray;
25 
26 struct Case {
27  const char *AnnotatedCode;
28  std::vector<const char *> Expected;
29 };
30 class ASTUtils : public ::testing::Test,
31  public ::testing::WithParamInterface<Case> {};
32 
33 TEST_P(ASTUtils, PrintTemplateArgs) {
34  auto Pair = GetParam();
35  Annotations Test(Pair.AnnotatedCode);
36  auto AST = TestTU::withCode(Test.code()).build();
37  struct Visitor : RecursiveASTVisitor<Visitor> {
38  Visitor(std::vector<Position> Points) : Points(std::move(Points)) {}
39  bool VisitNamedDecl(const NamedDecl *ND) {
40  if (TemplateArgsAtPoints.size() == Points.size())
41  return true;
42  auto Pos = sourceLocToPosition(ND->getASTContext().getSourceManager(),
43  ND->getLocation());
44  if (Pos != Points[TemplateArgsAtPoints.size()])
45  return true;
46  TemplateArgsAtPoints.push_back(printTemplateSpecializationArgs(*ND));
47  return true;
48  }
49  std::vector<std::string> TemplateArgsAtPoints;
50  const std::vector<Position> Points;
51  };
52  Visitor V(Test.points());
53  V.TraverseDecl(AST.getASTContext().getTranslationUnitDecl());
54  EXPECT_THAT(V.TemplateArgsAtPoints, ElementsAreArray(Pair.Expected));
55 }
56 
57 INSTANTIATE_TEST_CASE_P(ASTUtilsTests, ASTUtils,
58  ::testing::ValuesIn(std::vector<Case>({
59  {
60  R"cpp(
61  template <class X> class Bar {};
62  template <> class ^Bar<double> {};)cpp",
63  {"<double>"}},
64  {
65  R"cpp(
66  template <class X> class Bar {};
67  template <class T, class U,
68  template<typename> class Z, int Q>
69  struct Foo {};
70  template struct ^Foo<int, bool, Bar, 8>;
71  template <typename T>
72  struct ^Foo<T *, T, Bar, 3> {};)cpp",
73  {"<int, bool, Bar, 8>", "<T *, T, Bar, 3>"}},
74  {
75  R"cpp(
76  template <int ...> void Foz() {};
77  template <> void ^Foz<3, 5, 8>() {};)cpp",
78  {"<3, 5, 8>"}},
79  {
80  R"cpp(
81  template <class X> class Bar {};
82  template <template <class> class ...>
83  class Aux {};
84  template <> class ^Aux<Bar, Bar> {};
85  template <template <class> T>
86  class ^Aux<T, T> {};)cpp",
87  {"<Bar, Bar>", "<T, T>"}},
88  {
89  R"cpp(
90  template <typename T> T var = 1234;
91  template <> int ^var<int> = 1;)cpp",
92  {"<int>"}},
93  {
94  R"cpp(
95  template <typename T> struct Foo;
96  struct Bar { friend class Foo<int>; };
97  template <> struct ^Foo<int> {};)cpp",
98  {"<int>"}},
99  })),);
100 } // namespace
101 } // namespace clangd
102 } // namespace clang
const char * AnnotatedCode
std::string printTemplateSpecializationArgs(const NamedDecl &ND)
Prints template arguments of a decl as written in the source code, including enclosing &#39;<&#39; and &#39;>&#39;...
Definition: AST.cpp:117
Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc)
Turn a SourceLocation into a [line, column] pair.
Definition: SourceCode.cpp:186
static TestTU withCode(llvm::StringRef Code)
Definition: TestTU.h:33
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::vector< const char * > Expected