NamedDeclPrinterTest.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //===- unittests/AST/NamedDeclPrinterTest.cpp --- NamedDecl printer tests -===//
  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. // This file contains tests for NamedDecl::printQualifiedName().
  10. //
  11. // These tests have a coding convention:
  12. // * declaration to be printed is named 'A' unless it should have some special
  13. // name (e.g., 'operator+');
  14. // * additional helper declarations are 'Z', 'Y', 'X' and so on.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #include "clang/AST/ASTContext.h"
  18. #include "clang/AST/Decl.h"
  19. #include "clang/AST/PrettyPrinter.h"
  20. #include "clang/ASTMatchers/ASTMatchFinder.h"
  21. #include "clang/Tooling/Tooling.h"
  22. #include "llvm/ADT/SmallString.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. #include "gtest/gtest.h"
  25. using namespace clang;
  26. using namespace ast_matchers;
  27. using namespace tooling;
  28. namespace {
  29. class PrintMatch : public MatchFinder::MatchCallback {
  30. SmallString<1024> Printed;
  31. unsigned NumFoundDecls;
  32. std::function<void(llvm::raw_ostream &OS, const NamedDecl *)> Printer;
  33. public:
  34. explicit PrintMatch(
  35. std::function<void(llvm::raw_ostream &OS, const NamedDecl *)> Printer)
  36. : NumFoundDecls(0), Printer(std::move(Printer)) {}
  37. void run(const MatchFinder::MatchResult &Result) override {
  38. const NamedDecl *ND = Result.Nodes.getNodeAs<NamedDecl>("id");
  39. if (!ND)
  40. return;
  41. NumFoundDecls++;
  42. if (NumFoundDecls > 1)
  43. return;
  44. llvm::raw_svector_ostream Out(Printed);
  45. Printer(Out, ND);
  46. }
  47. StringRef getPrinted() const {
  48. return Printed;
  49. }
  50. unsigned getNumFoundDecls() const {
  51. return NumFoundDecls;
  52. }
  53. };
  54. ::testing::AssertionResult PrintedDeclMatches(
  55. StringRef Code, const std::vector<std::string> &Args,
  56. const DeclarationMatcher &NodeMatch, StringRef ExpectedPrinted,
  57. StringRef FileName,
  58. std::function<void(llvm::raw_ostream &, const NamedDecl *)> Print) {
  59. PrintMatch Printer(std::move(Print));
  60. MatchFinder Finder;
  61. Finder.addMatcher(NodeMatch, &Printer);
  62. std::unique_ptr<FrontendActionFactory> Factory =
  63. newFrontendActionFactory(&Finder);
  64. if (!runToolOnCodeWithArgs(Factory->create(), Code, Args, FileName))
  65. return testing::AssertionFailure()
  66. << "Parsing error in \"" << Code.str() << "\"";
  67. if (Printer.getNumFoundDecls() == 0)
  68. return testing::AssertionFailure()
  69. << "Matcher didn't find any named declarations";
  70. if (Printer.getNumFoundDecls() > 1)
  71. return testing::AssertionFailure()
  72. << "Matcher should match only one named declaration "
  73. "(found " << Printer.getNumFoundDecls() << ")";
  74. if (Printer.getPrinted() != ExpectedPrinted)
  75. return ::testing::AssertionFailure()
  76. << "Expected \"" << ExpectedPrinted.str() << "\", "
  77. "got \"" << Printer.getPrinted().str() << "\"";
  78. return ::testing::AssertionSuccess();
  79. }
  80. ::testing::AssertionResult
  81. PrintedNamedDeclMatches(StringRef Code, const std::vector<std::string> &Args,
  82. bool SuppressUnwrittenScope,
  83. const DeclarationMatcher &NodeMatch,
  84. StringRef ExpectedPrinted, StringRef FileName) {
  85. return PrintedDeclMatches(Code, Args, NodeMatch, ExpectedPrinted, FileName,
  86. [=](llvm::raw_ostream &Out, const NamedDecl *ND) {
  87. auto Policy =
  88. ND->getASTContext().getPrintingPolicy();
  89. Policy.SuppressUnwrittenScope =
  90. SuppressUnwrittenScope;
  91. ND->printQualifiedName(Out, Policy);
  92. });
  93. }
  94. ::testing::AssertionResult
  95. PrintedNamedDeclCXX98Matches(StringRef Code, StringRef DeclName,
  96. StringRef ExpectedPrinted) {
  97. std::vector<std::string> Args(1, "-std=c++98");
  98. return PrintedNamedDeclMatches(Code,
  99. Args,
  100. /*SuppressUnwrittenScope*/ false,
  101. namedDecl(hasName(DeclName)).bind("id"),
  102. ExpectedPrinted,
  103. "input.cc");
  104. }
  105. ::testing::AssertionResult
  106. PrintedWrittenNamedDeclCXX11Matches(StringRef Code, StringRef DeclName,
  107. StringRef ExpectedPrinted) {
  108. std::vector<std::string> Args(1, "-std=c++11");
  109. return PrintedNamedDeclMatches(Code,
  110. Args,
  111. /*SuppressUnwrittenScope*/ true,
  112. namedDecl(hasName(DeclName)).bind("id"),
  113. ExpectedPrinted,
  114. "input.cc");
  115. }
  116. ::testing::AssertionResult
  117. PrintedWrittenPropertyDeclObjCMatches(StringRef Code, StringRef DeclName,
  118. StringRef ExpectedPrinted) {
  119. std::vector<std::string> Args{"-std=c++11", "-xobjective-c++"};
  120. return PrintedNamedDeclMatches(Code,
  121. Args,
  122. /*SuppressUnwrittenScope*/ true,
  123. objcPropertyDecl(hasName(DeclName)).bind("id"),
  124. ExpectedPrinted,
  125. "input.m");
  126. }
  127. ::testing::AssertionResult
  128. PrintedNestedNameSpecifierMatches(StringRef Code, StringRef DeclName,
  129. StringRef ExpectedPrinted) {
  130. std::vector<std::string> Args{"-std=c++11"};
  131. return PrintedDeclMatches(Code, Args, namedDecl(hasName(DeclName)).bind("id"),
  132. ExpectedPrinted, "input.cc",
  133. [](llvm::raw_ostream &Out, const NamedDecl *D) {
  134. D->printNestedNameSpecifier(Out);
  135. });
  136. }
  137. } // unnamed namespace
  138. TEST(NamedDeclPrinter, TestNamespace1) {
  139. ASSERT_TRUE(PrintedNamedDeclCXX98Matches(
  140. "namespace { int A; }",
  141. "A",
  142. "(anonymous namespace)::A"));
  143. }
  144. TEST(NamedDeclPrinter, TestNamespace2) {
  145. ASSERT_TRUE(PrintedWrittenNamedDeclCXX11Matches(
  146. "inline namespace Z { namespace { int A; } }",
  147. "A",
  148. "A"));
  149. }
  150. TEST(NamedDeclPrinter, TestUnscopedUnnamedEnum) {
  151. ASSERT_TRUE(PrintedWrittenNamedDeclCXX11Matches(
  152. "enum { A };",
  153. "A",
  154. "A"));
  155. }
  156. TEST(NamedDeclPrinter, TestNamedEnum) {
  157. ASSERT_TRUE(PrintedWrittenNamedDeclCXX11Matches(
  158. "enum X { A };",
  159. "A",
  160. "A"));
  161. }
  162. TEST(NamedDeclPrinter, TestScopedNamedEnum) {
  163. ASSERT_TRUE(PrintedWrittenNamedDeclCXX11Matches(
  164. "enum class X { A };",
  165. "A",
  166. "X::A"));
  167. }
  168. TEST(NamedDeclPrinter, TestClassWithUnscopedUnnamedEnum) {
  169. ASSERT_TRUE(PrintedWrittenNamedDeclCXX11Matches(
  170. "class X { enum { A }; };",
  171. "A",
  172. "X::A"));
  173. }
  174. TEST(NamedDeclPrinter, TestClassWithUnscopedNamedEnum) {
  175. ASSERT_TRUE(PrintedWrittenNamedDeclCXX11Matches(
  176. "class X { enum Y { A }; };",
  177. "A",
  178. "X::A"));
  179. }
  180. TEST(NamedDeclPrinter, TestClassWithScopedNamedEnum) {
  181. ASSERT_TRUE(PrintedWrittenNamedDeclCXX11Matches(
  182. "class X { enum class Y { A }; };",
  183. "A",
  184. "X::Y::A"));
  185. }
  186. TEST(NamedDeclPrinter, TestLinkageInNamespace) {
  187. ASSERT_TRUE(PrintedWrittenNamedDeclCXX11Matches(
  188. "namespace X { extern \"C\" { int A; } }",
  189. "A",
  190. "X::A"));
  191. }
  192. TEST(NamedDeclPrinter, TestObjCClassExtension) {
  193. const char *Code =
  194. R"(
  195. @interface Obj
  196. @end
  197. @interface Obj ()
  198. @property(nonatomic) int property;
  199. @end
  200. )";
  201. ASSERT_TRUE(PrintedWrittenPropertyDeclObjCMatches(
  202. Code,
  203. "property",
  204. "Obj::property"));
  205. }
  206. TEST(NamedDeclPrinter, TestObjCClassExtensionWithGetter) {
  207. const char *Code =
  208. R"(
  209. @interface Obj
  210. @end
  211. @interface Obj ()
  212. @property(nonatomic, getter=myPropertyGetter) int property;
  213. @end
  214. )";
  215. ASSERT_TRUE(PrintedWrittenPropertyDeclObjCMatches(
  216. Code,
  217. "property",
  218. "Obj::property"));
  219. }
  220. TEST(NamedDeclPrinter, NestedNameSpecifierSimple) {
  221. const char *Code =
  222. R"(
  223. namespace foo { namespace bar { void func(); } }
  224. )";
  225. ASSERT_TRUE(PrintedNestedNameSpecifierMatches(Code, "func", "foo::bar::"));
  226. }
  227. TEST(NamedDeclPrinter, NestedNameSpecifierTemplateArgs) {
  228. const char *Code =
  229. R"(
  230. template <class T> struct vector;
  231. template <> struct vector<int> { int method(); };
  232. )";
  233. ASSERT_TRUE(
  234. PrintedNestedNameSpecifierMatches(Code, "method", "vector<int>::"));
  235. }