NestedNameSpecifiers.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #include "TestVisitor.h"
  9. using namespace clang;
  10. namespace {
  11. // Check to ensure that nested name specifiers are visited.
  12. class NestedNameSpecifiersVisitor
  13. : public ExpectedLocationVisitor<NestedNameSpecifiersVisitor> {
  14. public:
  15. bool VisitRecordTypeLoc(RecordTypeLoc RTL) {
  16. if (!RTL)
  17. return true;
  18. Match(RTL.getDecl()->getName(), RTL.getNameLoc());
  19. return true;
  20. }
  21. bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
  22. if (!NNS)
  23. return true;
  24. if (const NamespaceDecl *ND =
  25. NNS.getNestedNameSpecifier()->getAsNamespace())
  26. Match(ND->getName(), NNS.getLocalBeginLoc());
  27. return ExpectedLocationVisitor::TraverseNestedNameSpecifierLoc(NNS);
  28. }
  29. };
  30. TEST(RecursiveASTVisitor,
  31. NestedNameSpecifiersForTemplateSpecializationsAreVisited) {
  32. StringRef Source = R"(
  33. namespace ns {
  34. struct Outer {
  35. template<typename T, typename U>
  36. struct Nested { };
  37. template<typename T>
  38. static T x;
  39. };
  40. }
  41. template<>
  42. struct ns::Outer::Nested<int, int>;
  43. template<>
  44. struct ns::Outer::Nested<int, int> { };
  45. template<typename T>
  46. struct ns::Outer::Nested<int, T> { };
  47. template<>
  48. int ns::Outer::x<int> = 0;
  49. )";
  50. NestedNameSpecifiersVisitor Visitor;
  51. Visitor.ExpectMatch("ns", 13, 8);
  52. Visitor.ExpectMatch("ns", 16, 8);
  53. Visitor.ExpectMatch("ns", 19, 8);
  54. Visitor.ExpectMatch("ns", 22, 5);
  55. Visitor.ExpectMatch("Outer", 13, 12);
  56. Visitor.ExpectMatch("Outer", 16, 12);
  57. Visitor.ExpectMatch("Outer", 19, 12);
  58. Visitor.ExpectMatch("Outer", 22, 9);
  59. EXPECT_TRUE(Visitor.runOver(Source, NestedNameSpecifiersVisitor::Lang_CXX14));
  60. }
  61. } // end anonymous namespace