Debian Codegraph @

clang/unittests/Tooling/RecursiveASTVisitorTests/NestedNameSpecifiers.cpp

raw
1 //===- unittest/Tooling/RecursiveASTVisitorTests/NestedNameSpecifiers.cpp -===//
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 "TestVisitor.h"
10
11 using namespace clang;
12
13 namespace {
14
15 // Check to ensure that nested name specifiers are visited.
16 class NestedNameSpecifiersVisitor : public ExpectedLocationVisitor {
17 public:
18 bool VisitRecordTypeLoc(RecordTypeLoc RTL) override {
19 if (!RTL)
20 return true;
21 Match(RTL.getDecl()->getName(), RTL.getNameLoc());
22 return true;
23 }
24
25 bool
26 TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc QualifierLoc) override {
27 NestedNameSpecifier Qualifier = QualifierLoc.getNestedNameSpecifier();
28 if (Qualifier.getKind() == NestedNameSpecifier::Kind::Namespace) {
29 if (const auto *ND = dyn_cast<NamespaceDecl>(
30 Qualifier.getAsNamespaceAndPrefix().Namespace))
31 Match(ND->getName(), QualifierLoc.getLocalBeginLoc());
32 }
33 return ExpectedLocationVisitor::TraverseNestedNameSpecifierLoc(
34 QualifierLoc);
35 }
36 };
37
38 TEST(RecursiveASTVisitor,
39 NestedNameSpecifiersForTemplateSpecializationsAreVisited) {
40 StringRef Source = R"(
41 namespace ns {
42 struct Outer {
43 template<typename T, typename U>
44 struct Nested { };
45
46 template<typename T>
47 static T x;
48 };
49 }
50
51 template<>
52 struct ns::Outer::Nested<int, int>;
53
54 template<>
55 struct ns::Outer::Nested<int, int> { };
56
57 template<typename T>
58 struct ns::Outer::Nested<int, T> { };
59
60 template<>
61 int ns::Outer::x<int> = 0;
62 )";
63 NestedNameSpecifiersVisitor Visitor;
64 Visitor.ExpectMatch("ns", 13, 8);
65 Visitor.ExpectMatch("ns", 16, 8);
66 Visitor.ExpectMatch("ns", 19, 8);
67 Visitor.ExpectMatch("ns", 22, 5);
68 Visitor.ExpectMatch("Outer", 13, 12);
69 Visitor.ExpectMatch("Outer", 16, 12);
70 Visitor.ExpectMatch("Outer", 19, 12);
71 Visitor.ExpectMatch("Outer", 22, 9);
72 EXPECT_TRUE(Visitor.runOver(Source, NestedNameSpecifiersVisitor::Lang_CXX14));
73 }
74
75 } // end anonymous namespace