NamedDeclPrinterTest.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //===- unittests/AST/NamedDeclPrinterTest.cpp --- NamedDecl printer tests -===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains tests for NamedDecl::printQualifiedName().
  11. //
  12. // These tests have a coding convention:
  13. // * declaration to be printed is named 'A' unless it should have some special
  14. // name (e.g., 'operator+');
  15. // * additional helper declarations are 'Z', 'Y', 'X' and so on.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #include "clang/AST/ASTContext.h"
  19. #include "clang/ASTMatchers/ASTMatchFinder.h"
  20. #include "clang/Tooling/Tooling.h"
  21. #include "llvm/ADT/SmallString.h"
  22. #include "gtest/gtest.h"
  23. using namespace clang;
  24. using namespace ast_matchers;
  25. using namespace tooling;
  26. namespace {
  27. class PrintMatch : public MatchFinder::MatchCallback {
  28. SmallString<1024> Printed;
  29. unsigned NumFoundDecls;
  30. bool SuppressUnwrittenScope;
  31. public:
  32. explicit PrintMatch(bool suppressUnwrittenScope)
  33. : NumFoundDecls(0), SuppressUnwrittenScope(suppressUnwrittenScope) {}
  34. void run(const MatchFinder::MatchResult &Result) override {
  35. const NamedDecl *ND = Result.Nodes.getNodeAs<NamedDecl>("id");
  36. if (!ND)
  37. return;
  38. NumFoundDecls++;
  39. if (NumFoundDecls > 1)
  40. return;
  41. llvm::raw_svector_ostream Out(Printed);
  42. PrintingPolicy Policy = Result.Context->getPrintingPolicy();
  43. Policy.SuppressUnwrittenScope = SuppressUnwrittenScope;
  44. ND->printQualifiedName(Out, Policy);
  45. }
  46. StringRef getPrinted() const {
  47. return Printed;
  48. }
  49. unsigned getNumFoundDecls() const {
  50. return NumFoundDecls;
  51. }
  52. };
  53. ::testing::AssertionResult
  54. PrintedNamedDeclMatches(StringRef Code, const std::vector<std::string> &Args,
  55. bool SuppressUnwrittenScope,
  56. const DeclarationMatcher &NodeMatch,
  57. StringRef ExpectedPrinted, StringRef FileName) {
  58. PrintMatch Printer(SuppressUnwrittenScope);
  59. MatchFinder Finder;
  60. Finder.addMatcher(NodeMatch, &Printer);
  61. std::unique_ptr<FrontendActionFactory> Factory =
  62. newFrontendActionFactory(&Finder);
  63. if (!runToolOnCodeWithArgs(Factory->create(), Code, Args, FileName))
  64. return testing::AssertionFailure()
  65. << "Parsing error in \"" << Code.str() << "\"";
  66. if (Printer.getNumFoundDecls() == 0)
  67. return testing::AssertionFailure()
  68. << "Matcher didn't find any named declarations";
  69. if (Printer.getNumFoundDecls() > 1)
  70. return testing::AssertionFailure()
  71. << "Matcher should match only one named declaration "
  72. "(found " << Printer.getNumFoundDecls() << ")";
  73. if (Printer.getPrinted() != ExpectedPrinted)
  74. return ::testing::AssertionFailure()
  75. << "Expected \"" << ExpectedPrinted.str() << "\", "
  76. "got \"" << Printer.getPrinted().str() << "\"";
  77. return ::testing::AssertionSuccess();
  78. }
  79. ::testing::AssertionResult
  80. PrintedNamedDeclCXX98Matches(StringRef Code, StringRef DeclName,
  81. StringRef ExpectedPrinted) {
  82. std::vector<std::string> Args(1, "-std=c++98");
  83. return PrintedNamedDeclMatches(Code,
  84. Args,
  85. /*SuppressUnwrittenScope*/ false,
  86. namedDecl(hasName(DeclName)).bind("id"),
  87. ExpectedPrinted,
  88. "input.cc");
  89. }
  90. ::testing::AssertionResult
  91. PrintedWrittenNamedDeclCXX11Matches(StringRef Code, StringRef DeclName,
  92. StringRef ExpectedPrinted) {
  93. std::vector<std::string> Args(1, "-std=c++11");
  94. return PrintedNamedDeclMatches(Code,
  95. Args,
  96. /*SuppressUnwrittenScope*/ true,
  97. namedDecl(hasName(DeclName)).bind("id"),
  98. ExpectedPrinted,
  99. "input.cc");
  100. }
  101. } // unnamed namespace
  102. TEST(NamedDeclPrinter, TestNamespace1) {
  103. ASSERT_TRUE(PrintedNamedDeclCXX98Matches(
  104. "namespace { int A; }",
  105. "A",
  106. "(anonymous namespace)::A"));
  107. }
  108. TEST(NamedDeclPrinter, TestNamespace2) {
  109. ASSERT_TRUE(PrintedWrittenNamedDeclCXX11Matches(
  110. "inline namespace Z { namespace { int A; } }",
  111. "A",
  112. "A"));
  113. }