76 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "TestVisitor.h"10 11using namespace clang;12 13namespace {14 15// Check to ensure that nested name specifiers are visited.16class NestedNameSpecifiersVisitor : public ExpectedLocationVisitor {17public: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 bool26 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 38TEST(RecursiveASTVisitor,39 NestedNameSpecifiersForTemplateSpecializationsAreVisited) {40 StringRef Source = R"(41namespace ns {42struct Outer {43 template<typename T, typename U>44 struct Nested { };45 46 template<typename T>47 static T x;48};49}50 51template<>52struct ns::Outer::Nested<int, int>;53 54template<>55struct ns::Outer::Nested<int, int> { };56 57template<typename T>58struct ns::Outer::Nested<int, T> { };59 60template<>61int 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 namespace76