DeclPrinterTest.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. //===- unittests/AST/DeclPrinterTest.cpp --- Declaration 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 Decl::print() and related methods.
  10. //
  11. // Search this file for WRONG to see test cases that are producing something
  12. // completely wrong, invalid C++ or just misleading.
  13. //
  14. // These tests have a coding convention:
  15. // * declaration to be printed is named 'A' unless it should have some special
  16. // name (e.g., 'operator+');
  17. // * additional helper declarations are 'Z', 'Y', 'X' and so on.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #include "clang/AST/ASTContext.h"
  21. #include "clang/ASTMatchers/ASTMatchFinder.h"
  22. #include "clang/Tooling/Tooling.h"
  23. #include "llvm/ADT/SmallString.h"
  24. #include "gtest/gtest.h"
  25. using namespace clang;
  26. using namespace ast_matchers;
  27. using namespace tooling;
  28. namespace {
  29. using PrintingPolicyModifier = void (*)(PrintingPolicy &policy);
  30. void PrintDecl(raw_ostream &Out, const ASTContext *Context, const Decl *D,
  31. PrintingPolicyModifier PolicyModifier) {
  32. PrintingPolicy Policy = Context->getPrintingPolicy();
  33. Policy.TerseOutput = true;
  34. if (PolicyModifier)
  35. PolicyModifier(Policy);
  36. D->print(Out, Policy, /*Indentation*/ 0, /*PrintInstantiation*/ false);
  37. }
  38. class PrintMatch : public MatchFinder::MatchCallback {
  39. SmallString<1024> Printed;
  40. unsigned NumFoundDecls;
  41. PrintingPolicyModifier PolicyModifier;
  42. public:
  43. PrintMatch(PrintingPolicyModifier PolicyModifier)
  44. : NumFoundDecls(0), PolicyModifier(PolicyModifier) {}
  45. void run(const MatchFinder::MatchResult &Result) override {
  46. const Decl *D = Result.Nodes.getNodeAs<Decl>("id");
  47. if (!D || D->isImplicit())
  48. return;
  49. NumFoundDecls++;
  50. if (NumFoundDecls > 1)
  51. return;
  52. llvm::raw_svector_ostream Out(Printed);
  53. PrintDecl(Out, Result.Context, D, PolicyModifier);
  54. }
  55. StringRef getPrinted() const {
  56. return Printed;
  57. }
  58. unsigned getNumFoundDecls() const {
  59. return NumFoundDecls;
  60. }
  61. };
  62. ::testing::AssertionResult
  63. PrintedDeclMatches(StringRef Code, const std::vector<std::string> &Args,
  64. const DeclarationMatcher &NodeMatch,
  65. StringRef ExpectedPrinted, StringRef FileName,
  66. PrintingPolicyModifier PolicyModifier = nullptr) {
  67. PrintMatch Printer(PolicyModifier);
  68. MatchFinder Finder;
  69. Finder.addMatcher(NodeMatch, &Printer);
  70. std::unique_ptr<FrontendActionFactory> Factory(
  71. newFrontendActionFactory(&Finder));
  72. if (!runToolOnCodeWithArgs(Factory->create(), Code, Args, FileName))
  73. return testing::AssertionFailure()
  74. << "Parsing error in \"" << Code.str() << "\"";
  75. if (Printer.getNumFoundDecls() == 0)
  76. return testing::AssertionFailure()
  77. << "Matcher didn't find any declarations";
  78. if (Printer.getNumFoundDecls() > 1)
  79. return testing::AssertionFailure()
  80. << "Matcher should match only one declaration "
  81. "(found " << Printer.getNumFoundDecls() << ")";
  82. if (Printer.getPrinted() != ExpectedPrinted)
  83. return ::testing::AssertionFailure()
  84. << "Expected \"" << ExpectedPrinted.str() << "\", "
  85. "got \"" << Printer.getPrinted().str() << "\"";
  86. return ::testing::AssertionSuccess();
  87. }
  88. ::testing::AssertionResult
  89. PrintedDeclCXX98Matches(StringRef Code, StringRef DeclName,
  90. StringRef ExpectedPrinted,
  91. PrintingPolicyModifier PolicyModifier = nullptr) {
  92. std::vector<std::string> Args(1, "-std=c++98");
  93. return PrintedDeclMatches(Code,
  94. Args,
  95. namedDecl(hasName(DeclName)).bind("id"),
  96. ExpectedPrinted,
  97. "input.cc",
  98. PolicyModifier);
  99. }
  100. ::testing::AssertionResult
  101. PrintedDeclCXX98Matches(StringRef Code, const DeclarationMatcher &NodeMatch,
  102. StringRef ExpectedPrinted,
  103. PrintingPolicyModifier PolicyModifier = nullptr) {
  104. std::vector<std::string> Args(1, "-std=c++98");
  105. return PrintedDeclMatches(Code,
  106. Args,
  107. NodeMatch,
  108. ExpectedPrinted,
  109. "input.cc",
  110. PolicyModifier);
  111. }
  112. ::testing::AssertionResult PrintedDeclCXX11Matches(StringRef Code,
  113. StringRef DeclName,
  114. StringRef ExpectedPrinted) {
  115. std::vector<std::string> Args(1, "-std=c++11");
  116. return PrintedDeclMatches(Code,
  117. Args,
  118. namedDecl(hasName(DeclName)).bind("id"),
  119. ExpectedPrinted,
  120. "input.cc");
  121. }
  122. ::testing::AssertionResult PrintedDeclCXX11Matches(
  123. StringRef Code,
  124. const DeclarationMatcher &NodeMatch,
  125. StringRef ExpectedPrinted) {
  126. std::vector<std::string> Args(1, "-std=c++11");
  127. return PrintedDeclMatches(Code,
  128. Args,
  129. NodeMatch,
  130. ExpectedPrinted,
  131. "input.cc");
  132. }
  133. ::testing::AssertionResult PrintedDeclCXX11nonMSCMatches(
  134. StringRef Code,
  135. const DeclarationMatcher &NodeMatch,
  136. StringRef ExpectedPrinted) {
  137. std::vector<std::string> Args(1, "-std=c++11");
  138. Args.push_back("-fno-delayed-template-parsing");
  139. return PrintedDeclMatches(Code,
  140. Args,
  141. NodeMatch,
  142. ExpectedPrinted,
  143. "input.cc");
  144. }
  145. ::testing::AssertionResult
  146. PrintedDeclCXX1ZMatches(StringRef Code, const DeclarationMatcher &NodeMatch,
  147. StringRef ExpectedPrinted) {
  148. std::vector<std::string> Args(1, "-std=c++1z");
  149. return PrintedDeclMatches(Code,
  150. Args,
  151. NodeMatch,
  152. ExpectedPrinted,
  153. "input.cc");
  154. }
  155. ::testing::AssertionResult PrintedDeclObjCMatches(
  156. StringRef Code,
  157. const DeclarationMatcher &NodeMatch,
  158. StringRef ExpectedPrinted) {
  159. std::vector<std::string> Args(1, "");
  160. return PrintedDeclMatches(Code,
  161. Args,
  162. NodeMatch,
  163. ExpectedPrinted,
  164. "input.m");
  165. }
  166. } // unnamed namespace
  167. TEST(DeclPrinter, TestTypedef1) {
  168. ASSERT_TRUE(PrintedDeclCXX98Matches(
  169. "typedef int A;",
  170. "A",
  171. "typedef int A"));
  172. // Should be: with semicolon
  173. }
  174. TEST(DeclPrinter, TestTypedef2) {
  175. ASSERT_TRUE(PrintedDeclCXX98Matches(
  176. "typedef const char *A;",
  177. "A",
  178. "typedef const char *A"));
  179. // Should be: with semicolon
  180. }
  181. TEST(DeclPrinter, TestTypedef3) {
  182. ASSERT_TRUE(PrintedDeclCXX98Matches(
  183. "template <typename Y> class X {};"
  184. "typedef X<int> A;",
  185. "A",
  186. "typedef X<int> A"));
  187. // Should be: with semicolon
  188. }
  189. TEST(DeclPrinter, TestTypedef4) {
  190. ASSERT_TRUE(PrintedDeclCXX98Matches(
  191. "namespace X { class Y {}; }"
  192. "typedef X::Y A;",
  193. "A",
  194. "typedef X::Y A"));
  195. // Should be: with semicolon
  196. }
  197. TEST(DeclPrinter, TestNamespace1) {
  198. ASSERT_TRUE(PrintedDeclCXX98Matches(
  199. "namespace A { int B; }",
  200. "A",
  201. "namespace A {\n}"));
  202. // Should be: with { ... }
  203. }
  204. TEST(DeclPrinter, TestNamespace2) {
  205. ASSERT_TRUE(PrintedDeclCXX11Matches(
  206. "inline namespace A { int B; }",
  207. "A",
  208. "inline namespace A {\n}"));
  209. // Should be: with { ... }
  210. }
  211. TEST(DeclPrinter, TestNamespaceAlias1) {
  212. ASSERT_TRUE(PrintedDeclCXX98Matches(
  213. "namespace Z { }"
  214. "namespace A = Z;",
  215. "A",
  216. "namespace A = Z"));
  217. // Should be: with semicolon
  218. }
  219. TEST(DeclPrinter, TestNamespaceAlias2) {
  220. ASSERT_TRUE(PrintedDeclCXX98Matches(
  221. "namespace X { namespace Y {} }"
  222. "namespace A = X::Y;",
  223. "A",
  224. "namespace A = X::Y"));
  225. // Should be: with semicolon
  226. }
  227. TEST(DeclPrinter, TestCXXRecordDecl1) {
  228. ASSERT_TRUE(PrintedDeclCXX98Matches(
  229. "class A { int a; };",
  230. "A",
  231. "class A {}"));
  232. }
  233. TEST(DeclPrinter, TestCXXRecordDecl2) {
  234. ASSERT_TRUE(PrintedDeclCXX98Matches(
  235. "struct A { int a; };",
  236. "A",
  237. "struct A {}"));
  238. }
  239. TEST(DeclPrinter, TestCXXRecordDecl3) {
  240. ASSERT_TRUE(PrintedDeclCXX98Matches(
  241. "union A { int a; };",
  242. "A",
  243. "union A {}"));
  244. }
  245. TEST(DeclPrinter, TestCXXRecordDecl4) {
  246. ASSERT_TRUE(PrintedDeclCXX98Matches(
  247. "class Z { int a; };"
  248. "class A : Z { int b; };",
  249. "A",
  250. "class A : Z {}"));
  251. }
  252. TEST(DeclPrinter, TestCXXRecordDecl5) {
  253. ASSERT_TRUE(PrintedDeclCXX98Matches(
  254. "struct Z { int a; };"
  255. "struct A : Z { int b; };",
  256. "A",
  257. "struct A : Z {}"));
  258. }
  259. TEST(DeclPrinter, TestCXXRecordDecl6) {
  260. ASSERT_TRUE(PrintedDeclCXX98Matches(
  261. "class Z { int a; };"
  262. "class A : public Z { int b; };",
  263. "A",
  264. "class A : public Z {}"));
  265. }
  266. TEST(DeclPrinter, TestCXXRecordDecl7) {
  267. ASSERT_TRUE(PrintedDeclCXX98Matches(
  268. "class Z { int a; };"
  269. "class A : protected Z { int b; };",
  270. "A",
  271. "class A : protected Z {}"));
  272. }
  273. TEST(DeclPrinter, TestCXXRecordDecl8) {
  274. ASSERT_TRUE(PrintedDeclCXX98Matches(
  275. "class Z { int a; };"
  276. "class A : private Z { int b; };",
  277. "A",
  278. "class A : private Z {}"));
  279. }
  280. TEST(DeclPrinter, TestCXXRecordDecl9) {
  281. ASSERT_TRUE(PrintedDeclCXX98Matches(
  282. "class Z { int a; };"
  283. "class A : virtual Z { int b; };",
  284. "A",
  285. "class A : virtual Z {}"));
  286. }
  287. TEST(DeclPrinter, TestCXXRecordDecl10) {
  288. ASSERT_TRUE(PrintedDeclCXX98Matches(
  289. "class Z { int a; };"
  290. "class A : virtual public Z { int b; };",
  291. "A",
  292. "class A : virtual public Z {}"));
  293. }
  294. TEST(DeclPrinter, TestCXXRecordDecl11) {
  295. ASSERT_TRUE(PrintedDeclCXX98Matches(
  296. "class Z { int a; };"
  297. "class Y : virtual public Z { int b; };"
  298. "class A : virtual public Z, private Y { int c; };",
  299. "A",
  300. "class A : virtual public Z, private Y {}"));
  301. }
  302. TEST(DeclPrinter, TestFunctionDecl1) {
  303. ASSERT_TRUE(PrintedDeclCXX98Matches(
  304. "void A();",
  305. "A",
  306. "void A()"));
  307. }
  308. TEST(DeclPrinter, TestFreeFunctionDecl_FullyQualifiedName) {
  309. ASSERT_TRUE(PrintedDeclCXX98Matches(
  310. "void A();",
  311. "A",
  312. "void A()",
  313. [](PrintingPolicy &Policy){ Policy.FullyQualifiedName = true; }));
  314. }
  315. TEST(DeclPrinter, TestFreeFunctionDeclInNamespace_FullyQualifiedName) {
  316. ASSERT_TRUE(PrintedDeclCXX98Matches(
  317. "namespace X { void A(); };",
  318. "A",
  319. "void X::A()",
  320. [](PrintingPolicy &Policy){ Policy.FullyQualifiedName = true; }));
  321. }
  322. TEST(DeclPrinter, TestMemberFunction_FullyQualifiedName) {
  323. ASSERT_TRUE(PrintedDeclCXX98Matches(
  324. "struct X { void A(); };",
  325. "A",
  326. "void X::A()",
  327. [](PrintingPolicy &Policy){ Policy.FullyQualifiedName = true; }));
  328. }
  329. TEST(DeclPrinter, TestMemberFunctionInNamespace_FullyQualifiedName) {
  330. ASSERT_TRUE(PrintedDeclCXX98Matches(
  331. "namespace Z { struct X { void A(); }; }",
  332. "A",
  333. "void Z::X::A()",
  334. [](PrintingPolicy &Policy){ Policy.FullyQualifiedName = true; }));
  335. }
  336. TEST(DeclPrinter, TestMemberFunctionOutside_FullyQualifiedName) {
  337. ASSERT_TRUE(PrintedDeclCXX98Matches(
  338. "struct X { void A(); };"
  339. "void X::A() {}",
  340. functionDecl(hasName("A"), isDefinition()).bind("id"),
  341. "void X::A()",
  342. [](PrintingPolicy &Policy){ Policy.FullyQualifiedName = true; }));
  343. }
  344. TEST(DeclPrinter, TestFunctionDecl2) {
  345. ASSERT_TRUE(PrintedDeclCXX98Matches(
  346. "void A() {}",
  347. "A",
  348. "void A()"));
  349. }
  350. TEST(DeclPrinter, TestFunctionDecl3) {
  351. ASSERT_TRUE(PrintedDeclCXX98Matches(
  352. "void Z();"
  353. "void A() { Z(); }",
  354. "A",
  355. "void A()"));
  356. }
  357. TEST(DeclPrinter, TestFunctionDecl4) {
  358. ASSERT_TRUE(PrintedDeclCXX98Matches(
  359. "extern void A();",
  360. "A",
  361. "extern void A()"));
  362. }
  363. TEST(DeclPrinter, TestFunctionDecl5) {
  364. ASSERT_TRUE(PrintedDeclCXX98Matches(
  365. "static void A();",
  366. "A",
  367. "static void A()"));
  368. }
  369. TEST(DeclPrinter, TestFunctionDecl6) {
  370. ASSERT_TRUE(PrintedDeclCXX98Matches(
  371. "inline void A();",
  372. "A",
  373. "inline void A()"));
  374. }
  375. TEST(DeclPrinter, TestFunctionDecl7) {
  376. ASSERT_TRUE(PrintedDeclCXX11Matches(
  377. "constexpr int A(int a);",
  378. "A",
  379. "constexpr int A(int a)"));
  380. }
  381. TEST(DeclPrinter, TestFunctionDecl8) {
  382. ASSERT_TRUE(PrintedDeclCXX98Matches(
  383. "void A(int a);",
  384. "A",
  385. "void A(int a)"));
  386. }
  387. TEST(DeclPrinter, TestFunctionDecl9) {
  388. ASSERT_TRUE(PrintedDeclCXX98Matches(
  389. "void A(...);",
  390. "A",
  391. "void A(...)"));
  392. }
  393. TEST(DeclPrinter, TestFunctionDecl10) {
  394. ASSERT_TRUE(PrintedDeclCXX98Matches(
  395. "void A(int a, ...);",
  396. "A",
  397. "void A(int a, ...)"));
  398. }
  399. TEST(DeclPrinter, TestFunctionDecl11) {
  400. ASSERT_TRUE(PrintedDeclCXX98Matches(
  401. "typedef long ssize_t;"
  402. "typedef int *pInt;"
  403. "void A(int a, pInt b, ssize_t c);",
  404. "A",
  405. "void A(int a, pInt b, ssize_t c)"));
  406. }
  407. TEST(DeclPrinter, TestFunctionDecl12) {
  408. ASSERT_TRUE(PrintedDeclCXX98Matches(
  409. "void A(int a, int b = 0);",
  410. "A",
  411. "void A(int a, int b = 0)"));
  412. }
  413. TEST(DeclPrinter, TestFunctionDecl13) {
  414. ASSERT_TRUE(PrintedDeclCXX98Matches(
  415. "void (*A(int a))(int b);",
  416. "A",
  417. "void (*A(int a))(int)"));
  418. // Should be: with parameter name (?)
  419. }
  420. TEST(DeclPrinter, TestFunctionDecl14) {
  421. ASSERT_TRUE(PrintedDeclCXX98Matches(
  422. "template<typename T>"
  423. "void A(T t) { }"
  424. "template<>"
  425. "void A(int N) { }",
  426. functionDecl(hasName("A"), isExplicitTemplateSpecialization()).bind("id"),
  427. "template<> void A<int>(int N)"));
  428. }
  429. TEST(DeclPrinter, TestCXXConstructorDecl1) {
  430. ASSERT_TRUE(PrintedDeclCXX98Matches(
  431. "struct A {"
  432. " A();"
  433. "};",
  434. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  435. "A()"));
  436. }
  437. TEST(DeclPrinter, TestCXXConstructorDecl2) {
  438. ASSERT_TRUE(PrintedDeclCXX98Matches(
  439. "struct A {"
  440. " A(int a);"
  441. "};",
  442. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  443. "A(int a)"));
  444. }
  445. TEST(DeclPrinter, TestCXXConstructorDecl3) {
  446. ASSERT_TRUE(PrintedDeclCXX98Matches(
  447. "struct A {"
  448. " A(const A &a);"
  449. "};",
  450. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  451. "A(const A &a)"));
  452. }
  453. TEST(DeclPrinter, TestCXXConstructorDecl4) {
  454. ASSERT_TRUE(PrintedDeclCXX98Matches(
  455. "struct A {"
  456. " A(const A &a, int = 0);"
  457. "};",
  458. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  459. "A(const A &a, int = 0)"));
  460. }
  461. TEST(DeclPrinter, TestCXXConstructorDeclWithMemberInitializer) {
  462. ASSERT_TRUE(PrintedDeclCXX98Matches(
  463. "struct A {"
  464. " int m;"
  465. " A() : m(2) {}"
  466. "};",
  467. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  468. "A()"));
  469. }
  470. TEST(DeclPrinter, TestCXXConstructorDeclWithMemberInitializer_NoTerseOutput) {
  471. ASSERT_TRUE(PrintedDeclCXX98Matches(
  472. "struct A {"
  473. " int m;"
  474. " A() : m(2) {}"
  475. "};",
  476. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  477. "A() : m(2) {\n}\n",
  478. [](PrintingPolicy &Policy){ Policy.TerseOutput = false; }));
  479. }
  480. TEST(DeclPrinter, TestCXXConstructorDecl5) {
  481. ASSERT_TRUE(PrintedDeclCXX11Matches(
  482. "struct A {"
  483. " A(const A &&a);"
  484. "};",
  485. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  486. "A(const A &&a)"));
  487. }
  488. TEST(DeclPrinter, TestCXXConstructorDecl6) {
  489. ASSERT_TRUE(PrintedDeclCXX98Matches(
  490. "struct A {"
  491. " explicit A(int a);"
  492. "};",
  493. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  494. "explicit A(int a)"));
  495. }
  496. TEST(DeclPrinter, TestCXXConstructorDecl7) {
  497. ASSERT_TRUE(PrintedDeclCXX11Matches(
  498. "struct A {"
  499. " constexpr A();"
  500. "};",
  501. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  502. "constexpr A()"));
  503. }
  504. TEST(DeclPrinter, TestCXXConstructorDecl8) {
  505. ASSERT_TRUE(PrintedDeclCXX11Matches(
  506. "struct A {"
  507. " A() = default;"
  508. "};",
  509. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  510. "A() = default"));
  511. }
  512. TEST(DeclPrinter, TestCXXConstructorDecl9) {
  513. ASSERT_TRUE(PrintedDeclCXX11Matches(
  514. "struct A {"
  515. " A() = delete;"
  516. "};",
  517. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  518. "A() = delete"));
  519. }
  520. TEST(DeclPrinter, TestCXXConstructorDecl10) {
  521. ASSERT_TRUE(PrintedDeclCXX11Matches(
  522. "template<typename... T>"
  523. "struct A {"
  524. " A(const A &a);"
  525. "};",
  526. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  527. "A<T...>(const A<T...> &a)"));
  528. }
  529. TEST(DeclPrinter, TestCXXConstructorDecl11) {
  530. ASSERT_TRUE(PrintedDeclCXX11nonMSCMatches(
  531. "template<typename... T>"
  532. "struct A : public T... {"
  533. " A(T&&... ts) : T(ts)... {}"
  534. "};",
  535. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  536. "A<T...>(T &&...ts)"));
  537. }
  538. TEST(DeclPrinter, TestCXXDestructorDecl1) {
  539. ASSERT_TRUE(PrintedDeclCXX98Matches(
  540. "struct A {"
  541. " ~A();"
  542. "};",
  543. cxxDestructorDecl(ofClass(hasName("A"))).bind("id"),
  544. "~A()"));
  545. }
  546. TEST(DeclPrinter, TestCXXDestructorDecl2) {
  547. ASSERT_TRUE(PrintedDeclCXX98Matches(
  548. "struct A {"
  549. " virtual ~A();"
  550. "};",
  551. cxxDestructorDecl(ofClass(hasName("A"))).bind("id"),
  552. "virtual ~A()"));
  553. }
  554. TEST(DeclPrinter, TestCXXConversionDecl1) {
  555. ASSERT_TRUE(PrintedDeclCXX98Matches(
  556. "struct A {"
  557. " operator int();"
  558. "};",
  559. cxxMethodDecl(ofClass(hasName("A"))).bind("id"),
  560. "operator int()"));
  561. }
  562. TEST(DeclPrinter, TestCXXConversionDecl2) {
  563. ASSERT_TRUE(PrintedDeclCXX98Matches(
  564. "struct A {"
  565. " operator bool();"
  566. "};",
  567. cxxMethodDecl(ofClass(hasName("A"))).bind("id"),
  568. "operator bool()"));
  569. }
  570. TEST(DeclPrinter, TestCXXConversionDecl3) {
  571. ASSERT_TRUE(PrintedDeclCXX98Matches(
  572. "struct Z {};"
  573. "struct A {"
  574. " operator Z();"
  575. "};",
  576. cxxMethodDecl(ofClass(hasName("A"))).bind("id"),
  577. "operator Z()"));
  578. }
  579. TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction1) {
  580. ASSERT_TRUE(PrintedDeclCXX11Matches(
  581. "namespace std { typedef decltype(sizeof(int)) size_t; }"
  582. "struct Z {"
  583. " void *operator new(std::size_t);"
  584. "};",
  585. cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
  586. "void *operator new(std::size_t)"));
  587. }
  588. TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction2) {
  589. ASSERT_TRUE(PrintedDeclCXX11Matches(
  590. "namespace std { typedef decltype(sizeof(int)) size_t; }"
  591. "struct Z {"
  592. " void *operator new[](std::size_t);"
  593. "};",
  594. cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
  595. "void *operator new[](std::size_t)"));
  596. }
  597. TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction3) {
  598. ASSERT_TRUE(PrintedDeclCXX11Matches(
  599. "struct Z {"
  600. " void operator delete(void *);"
  601. "};",
  602. cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
  603. "void operator delete(void *) noexcept"));
  604. // Should be: without noexcept?
  605. }
  606. TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction4) {
  607. ASSERT_TRUE(PrintedDeclCXX98Matches(
  608. "struct Z {"
  609. " void operator delete(void *);"
  610. "};",
  611. cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
  612. "void operator delete(void *)"));
  613. }
  614. TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction5) {
  615. ASSERT_TRUE(PrintedDeclCXX11Matches(
  616. "struct Z {"
  617. " void operator delete[](void *);"
  618. "};",
  619. cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
  620. "void operator delete[](void *) noexcept"));
  621. // Should be: without noexcept?
  622. }
  623. TEST(DeclPrinter, TestCXXMethodDecl_Operator1) {
  624. const char *OperatorNames[] = {
  625. "+", "-", "*", "/", "%", "^", "&", "|",
  626. "=", "<", ">", "+=", "-=", "*=", "/=", "%=",
  627. "^=", "&=", "|=", "<<", ">>", ">>=", "<<=", "==", "!=",
  628. "<=", ">=", "&&", "||", ",", "->*",
  629. "()", "[]"
  630. };
  631. for (unsigned i = 0, e = llvm::array_lengthof(OperatorNames); i != e; ++i) {
  632. SmallString<128> Code;
  633. Code.append("struct Z { void operator");
  634. Code.append(OperatorNames[i]);
  635. Code.append("(Z z); };");
  636. SmallString<128> Expected;
  637. Expected.append("void operator");
  638. Expected.append(OperatorNames[i]);
  639. Expected.append("(Z z)");
  640. ASSERT_TRUE(PrintedDeclCXX98Matches(
  641. Code,
  642. cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
  643. Expected));
  644. }
  645. }
  646. TEST(DeclPrinter, TestCXXMethodDecl_Operator2) {
  647. const char *OperatorNames[] = {
  648. "~", "!", "++", "--", "->"
  649. };
  650. for (unsigned i = 0, e = llvm::array_lengthof(OperatorNames); i != e; ++i) {
  651. SmallString<128> Code;
  652. Code.append("struct Z { void operator");
  653. Code.append(OperatorNames[i]);
  654. Code.append("(); };");
  655. SmallString<128> Expected;
  656. Expected.append("void operator");
  657. Expected.append(OperatorNames[i]);
  658. Expected.append("()");
  659. ASSERT_TRUE(PrintedDeclCXX98Matches(
  660. Code,
  661. cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
  662. Expected));
  663. }
  664. }
  665. TEST(DeclPrinter, TestCXXMethodDecl1) {
  666. ASSERT_TRUE(PrintedDeclCXX98Matches(
  667. "struct Z {"
  668. " void A(int a);"
  669. "};",
  670. "A",
  671. "void A(int a)"));
  672. }
  673. TEST(DeclPrinter, TestCXXMethodDecl2) {
  674. ASSERT_TRUE(PrintedDeclCXX98Matches(
  675. "struct Z {"
  676. " virtual void A(int a);"
  677. "};",
  678. "A",
  679. "virtual void A(int a)"));
  680. }
  681. TEST(DeclPrinter, TestCXXMethodDecl3) {
  682. ASSERT_TRUE(PrintedDeclCXX98Matches(
  683. "struct Z {"
  684. " virtual void A(int a);"
  685. "};"
  686. "struct ZZ : Z {"
  687. " void A(int a);"
  688. "};",
  689. "ZZ::A",
  690. "void A(int a)"));
  691. // TODO: should we print "virtual"?
  692. }
  693. TEST(DeclPrinter, TestCXXMethodDecl4) {
  694. ASSERT_TRUE(PrintedDeclCXX98Matches(
  695. "struct Z {"
  696. " inline void A(int a);"
  697. "};",
  698. "A",
  699. "inline void A(int a)"));
  700. }
  701. TEST(DeclPrinter, TestCXXMethodDecl5) {
  702. ASSERT_TRUE(PrintedDeclCXX98Matches(
  703. "struct Z {"
  704. " virtual void A(int a) = 0;"
  705. "};",
  706. "A",
  707. "virtual void A(int a) = 0"));
  708. }
  709. TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier1) {
  710. ASSERT_TRUE(PrintedDeclCXX98Matches(
  711. "struct Z {"
  712. " void A(int a) const;"
  713. "};",
  714. "A",
  715. "void A(int a) const"));
  716. }
  717. TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier2) {
  718. ASSERT_TRUE(PrintedDeclCXX98Matches(
  719. "struct Z {"
  720. " void A(int a) volatile;"
  721. "};",
  722. "A",
  723. "void A(int a) volatile"));
  724. }
  725. TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier3) {
  726. ASSERT_TRUE(PrintedDeclCXX98Matches(
  727. "struct Z {"
  728. " void A(int a) const volatile;"
  729. "};",
  730. "A",
  731. "void A(int a) const volatile"));
  732. }
  733. TEST(DeclPrinter, TestCXXMethodDecl_RefQualifier1) {
  734. ASSERT_TRUE(PrintedDeclCXX11Matches(
  735. "struct Z {"
  736. " void A(int a) &;"
  737. "};",
  738. "A",
  739. "void A(int a) &"));
  740. }
  741. TEST(DeclPrinter, TestCXXMethodDecl_RefQualifier2) {
  742. ASSERT_TRUE(PrintedDeclCXX11Matches(
  743. "struct Z {"
  744. " void A(int a) &&;"
  745. "};",
  746. "A",
  747. "void A(int a) &&"));
  748. }
  749. TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification1) {
  750. ASSERT_TRUE(PrintedDeclCXX98Matches(
  751. "struct Z {"
  752. " void A(int a) throw();"
  753. "};",
  754. "A",
  755. "void A(int a) throw()"));
  756. }
  757. TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification2) {
  758. ASSERT_TRUE(PrintedDeclCXX98Matches(
  759. "struct Z {"
  760. " void A(int a) throw(int);"
  761. "};",
  762. "A",
  763. "void A(int a) throw(int)"));
  764. }
  765. TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification3) {
  766. ASSERT_TRUE(PrintedDeclCXX98Matches(
  767. "class ZZ {};"
  768. "struct Z {"
  769. " void A(int a) throw(ZZ, int);"
  770. "};",
  771. "A",
  772. "void A(int a) throw(ZZ, int)"));
  773. }
  774. TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification4) {
  775. ASSERT_TRUE(PrintedDeclCXX11Matches(
  776. "struct Z {"
  777. " void A(int a) noexcept;"
  778. "};",
  779. "A",
  780. "void A(int a) noexcept"));
  781. }
  782. TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification5) {
  783. ASSERT_TRUE(PrintedDeclCXX11Matches(
  784. "struct Z {"
  785. " void A(int a) noexcept(true);"
  786. "};",
  787. "A",
  788. "void A(int a) noexcept(trueA(int a) noexcept(true)"));
  789. // WRONG; Should be: "void A(int a) noexcept(true);"
  790. }
  791. TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification6) {
  792. ASSERT_TRUE(PrintedDeclCXX11Matches(
  793. "struct Z {"
  794. " void A(int a) noexcept(1 < 2);"
  795. "};",
  796. "A",
  797. "void A(int a) noexcept(1 < 2A(int a) noexcept(1 < 2)"));
  798. // WRONG; Should be: "void A(int a) noexcept(1 < 2);"
  799. }
  800. TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification7) {
  801. ASSERT_TRUE(PrintedDeclCXX11Matches(
  802. "template<int N>"
  803. "struct Z {"
  804. " void A(int a) noexcept(N < 2);"
  805. "};",
  806. "A",
  807. "void A(int a) noexcept(N < 2A(int a) noexcept(N < 2)"));
  808. // WRONG; Should be: "void A(int a) noexcept(N < 2);"
  809. }
  810. TEST(DeclPrinter, TestVarDecl1) {
  811. ASSERT_TRUE(PrintedDeclCXX98Matches(
  812. "char *const (*(*A)[5])(int);",
  813. "A",
  814. "char *const (*(*A)[5])(int)"));
  815. // Should be: with semicolon
  816. }
  817. TEST(DeclPrinter, TestVarDecl2) {
  818. ASSERT_TRUE(PrintedDeclCXX98Matches(
  819. "void (*A)() throw(int);",
  820. "A",
  821. "void (*A)() throw(int)"));
  822. // Should be: with semicolon
  823. }
  824. TEST(DeclPrinter, TestVarDecl3) {
  825. ASSERT_TRUE(PrintedDeclCXX11Matches(
  826. "void (*A)() noexcept;",
  827. "A",
  828. "void (*A)() noexcept"));
  829. // Should be: with semicolon
  830. }
  831. TEST(DeclPrinter, TestFieldDecl1) {
  832. ASSERT_TRUE(PrintedDeclCXX98Matches(
  833. "template<typename T>"
  834. "struct Z { T A; };",
  835. "A",
  836. "T A"));
  837. // Should be: with semicolon
  838. }
  839. TEST(DeclPrinter, TestFieldDecl2) {
  840. ASSERT_TRUE(PrintedDeclCXX98Matches(
  841. "template<int N>"
  842. "struct Z { int A[N]; };",
  843. "A",
  844. "int A[N]"));
  845. // Should be: with semicolon
  846. }
  847. TEST(DeclPrinter, TestClassTemplateDecl1) {
  848. ASSERT_TRUE(PrintedDeclCXX98Matches(
  849. "template<typename T>"
  850. "struct A { T a; };",
  851. classTemplateDecl(hasName("A")).bind("id"),
  852. "template <typename T> struct A {}"));
  853. }
  854. TEST(DeclPrinter, TestClassTemplateDecl2) {
  855. ASSERT_TRUE(PrintedDeclCXX98Matches(
  856. "template<typename T = int>"
  857. "struct A { T a; };",
  858. classTemplateDecl(hasName("A")).bind("id"),
  859. "template <typename T = int> struct A {}"));
  860. }
  861. TEST(DeclPrinter, TestClassTemplateDecl3) {
  862. ASSERT_TRUE(PrintedDeclCXX98Matches(
  863. "template<class T>"
  864. "struct A { T a; };",
  865. classTemplateDecl(hasName("A")).bind("id"),
  866. "template <class T> struct A {}"));
  867. }
  868. TEST(DeclPrinter, TestClassTemplateDecl4) {
  869. ASSERT_TRUE(PrintedDeclCXX98Matches(
  870. "template<typename T, typename U>"
  871. "struct A { T a; U b; };",
  872. classTemplateDecl(hasName("A")).bind("id"),
  873. "template <typename T, typename U> struct A {}"));
  874. }
  875. TEST(DeclPrinter, TestClassTemplateDecl5) {
  876. ASSERT_TRUE(PrintedDeclCXX98Matches(
  877. "template<int N>"
  878. "struct A { int a[N]; };",
  879. classTemplateDecl(hasName("A")).bind("id"),
  880. "template <int N> struct A {}"));
  881. }
  882. TEST(DeclPrinter, TestClassTemplateDecl6) {
  883. ASSERT_TRUE(PrintedDeclCXX98Matches(
  884. "template<int N = 42>"
  885. "struct A { int a[N]; };",
  886. classTemplateDecl(hasName("A")).bind("id"),
  887. "template <int N = 42> struct A {}"));
  888. }
  889. TEST(DeclPrinter, TestClassTemplateDecl7) {
  890. ASSERT_TRUE(PrintedDeclCXX98Matches(
  891. "typedef int MyInt;"
  892. "template<MyInt N>"
  893. "struct A { int a[N]; };",
  894. classTemplateDecl(hasName("A")).bind("id"),
  895. "template <MyInt N> struct A {}"));
  896. }
  897. TEST(DeclPrinter, TestClassTemplateDecl8) {
  898. ASSERT_TRUE(PrintedDeclCXX98Matches(
  899. "template<template<typename U> class T> struct A { };",
  900. classTemplateDecl(hasName("A")).bind("id"),
  901. "template <template <typename U> class T> struct A {}"));
  902. }
  903. TEST(DeclPrinter, TestClassTemplateDecl9) {
  904. ASSERT_TRUE(PrintedDeclCXX98Matches(
  905. "template<typename T> struct Z { };"
  906. "template<template<typename U> class T = Z> struct A { };",
  907. classTemplateDecl(hasName("A")).bind("id"),
  908. "template <template <typename U> class T> struct A {}"));
  909. }
  910. TEST(DeclPrinter, TestClassTemplateDecl10) {
  911. ASSERT_TRUE(PrintedDeclCXX11Matches(
  912. "template<typename... T>"
  913. "struct A { int a; };",
  914. classTemplateDecl(hasName("A")).bind("id"),
  915. "template <typename ...T> struct A {}"));
  916. }
  917. TEST(DeclPrinter, TestClassTemplateDecl11) {
  918. ASSERT_TRUE(PrintedDeclCXX11Matches(
  919. "template<typename... T>"
  920. "struct A : public T... { int a; };",
  921. classTemplateDecl(hasName("A")).bind("id"),
  922. "template <typename ...T> struct A : public T... {}"));
  923. }
  924. TEST(DeclPrinter, TestClassTemplatePartialSpecializationDecl1) {
  925. ASSERT_TRUE(PrintedDeclCXX98Matches(
  926. "template<typename T, typename U>"
  927. "struct A { T a; U b; };"
  928. "template<typename T>"
  929. "struct A<T, int> { T a; };",
  930. classTemplateSpecializationDecl().bind("id"),
  931. "template <typename T> struct A<T, int> {}"));
  932. }
  933. TEST(DeclPrinter, TestClassTemplatePartialSpecializationDecl2) {
  934. ASSERT_TRUE(PrintedDeclCXX98Matches(
  935. "template<typename T>"
  936. "struct A { T a; };"
  937. "template<typename T>"
  938. "struct A<T *> { T a; };",
  939. classTemplateSpecializationDecl().bind("id"),
  940. "template <typename T> struct A<type-parameter-0-0 *> {}"));
  941. // WRONG; Should be: "template<typename T> struct A<T *> { ... }"
  942. }
  943. TEST(DeclPrinter, TestClassTemplateSpecializationDecl1) {
  944. ASSERT_TRUE(PrintedDeclCXX98Matches(
  945. "template<typename T>"
  946. "struct A { T a; };"
  947. "template<>"
  948. "struct A<int> { int a; };",
  949. classTemplateSpecializationDecl().bind("id"),
  950. "template<> struct A<int> {}"));
  951. }
  952. TEST(DeclPrinter, TestFunctionTemplateDecl1) {
  953. ASSERT_TRUE(PrintedDeclCXX98Matches(
  954. "template<typename T>"
  955. "void A(T &t);",
  956. functionTemplateDecl(hasName("A")).bind("id"),
  957. "template <typename T> void A(T &t)"));
  958. }
  959. TEST(DeclPrinter, TestFunctionTemplateDecl2) {
  960. ASSERT_TRUE(PrintedDeclCXX98Matches(
  961. "template<typename T>"
  962. "void A(T &t) { }",
  963. functionTemplateDecl(hasName("A")).bind("id"),
  964. "template <typename T> void A(T &t)"));
  965. }
  966. TEST(DeclPrinter, TestFunctionTemplateDecl3) {
  967. ASSERT_TRUE(PrintedDeclCXX11Matches(
  968. "template<typename... T>"
  969. "void A(T... a);",
  970. functionTemplateDecl(hasName("A")).bind("id"),
  971. "template <typename ...T> void A(T ...a)"));
  972. }
  973. TEST(DeclPrinter, TestFunctionTemplateDecl4) {
  974. ASSERT_TRUE(PrintedDeclCXX98Matches(
  975. "struct Z { template<typename T> void A(T t); };",
  976. functionTemplateDecl(hasName("A")).bind("id"),
  977. "template <typename T> void A(T t)"));
  978. }
  979. TEST(DeclPrinter, TestFunctionTemplateDecl5) {
  980. ASSERT_TRUE(PrintedDeclCXX98Matches(
  981. "struct Z { template<typename T> void A(T t) {} };",
  982. functionTemplateDecl(hasName("A")).bind("id"),
  983. "template <typename T> void A(T t)"));
  984. }
  985. TEST(DeclPrinter, TestFunctionTemplateDecl6) {
  986. ASSERT_TRUE(PrintedDeclCXX98Matches(
  987. "template<typename T >struct Z {"
  988. " template<typename U> void A(U t) {}"
  989. "};",
  990. functionTemplateDecl(hasName("A")).bind("id"),
  991. "template <typename U> void A(U t)"));
  992. }
  993. TEST(DeclPrinter, TestTemplateArgumentList1) {
  994. ASSERT_TRUE(PrintedDeclCXX98Matches(
  995. "template<typename T> struct Z {};"
  996. "struct X {};"
  997. "Z<X> A;",
  998. "A",
  999. "Z<X> A"));
  1000. // Should be: with semicolon
  1001. }
  1002. TEST(DeclPrinter, TestTemplateArgumentList2) {
  1003. ASSERT_TRUE(PrintedDeclCXX98Matches(
  1004. "template<typename T, typename U> struct Z {};"
  1005. "struct X {};"
  1006. "typedef int Y;"
  1007. "Z<X, Y> A;",
  1008. "A",
  1009. "Z<X, Y> A"));
  1010. // Should be: with semicolon
  1011. }
  1012. TEST(DeclPrinter, TestTemplateArgumentList3) {
  1013. ASSERT_TRUE(PrintedDeclCXX98Matches(
  1014. "template<typename T> struct Z {};"
  1015. "template<typename T> struct X {};"
  1016. "Z<X<int> > A;",
  1017. "A",
  1018. "Z<X<int> > A"));
  1019. // Should be: with semicolon
  1020. }
  1021. TEST(DeclPrinter, TestTemplateArgumentList4) {
  1022. ASSERT_TRUE(PrintedDeclCXX11Matches(
  1023. "template<typename T> struct Z {};"
  1024. "template<typename T> struct X {};"
  1025. "Z<X<int>> A;",
  1026. "A",
  1027. "Z<X<int> > A"));
  1028. // Should be: with semicolon, without extra space in "> >"
  1029. }
  1030. TEST(DeclPrinter, TestTemplateArgumentList5) {
  1031. ASSERT_TRUE(PrintedDeclCXX98Matches(
  1032. "template<typename T> struct Z {};"
  1033. "template<typename T> struct X { Z<T> A; };",
  1034. "A",
  1035. "Z<T> A"));
  1036. // Should be: with semicolon
  1037. }
  1038. TEST(DeclPrinter, TestTemplateArgumentList6) {
  1039. ASSERT_TRUE(PrintedDeclCXX98Matches(
  1040. "template<template<typename T> class U> struct Z {};"
  1041. "template<typename T> struct X {};"
  1042. "Z<X> A;",
  1043. "A",
  1044. "Z<X> A"));
  1045. // Should be: with semicolon
  1046. }
  1047. TEST(DeclPrinter, TestTemplateArgumentList7) {
  1048. ASSERT_TRUE(PrintedDeclCXX98Matches(
  1049. "template<template<typename T> class U> struct Z {};"
  1050. "template<template<typename T> class U> struct Y {"
  1051. " Z<U> A;"
  1052. "};",
  1053. "A",
  1054. "Z<U> A"));
  1055. // Should be: with semicolon
  1056. }
  1057. TEST(DeclPrinter, TestTemplateArgumentList8) {
  1058. ASSERT_TRUE(PrintedDeclCXX98Matches(
  1059. "template<typename T> struct Z {};"
  1060. "template<template<typename T> class U> struct Y {"
  1061. " Z<U<int> > A;"
  1062. "};",
  1063. "A",
  1064. "Z<U<int> > A"));
  1065. // Should be: with semicolon
  1066. }
  1067. TEST(DeclPrinter, TestTemplateArgumentList9) {
  1068. ASSERT_TRUE(PrintedDeclCXX98Matches(
  1069. "template<unsigned I> struct Z {};"
  1070. "Z<0> A;",
  1071. "A",
  1072. "Z<0> A"));
  1073. // Should be: with semicolon
  1074. }
  1075. TEST(DeclPrinter, TestTemplateArgumentList10) {
  1076. ASSERT_TRUE(PrintedDeclCXX98Matches(
  1077. "template<unsigned I> struct Z {};"
  1078. "template<unsigned I> struct X { Z<I> A; };",
  1079. "A",
  1080. "Z<I> A"));
  1081. // Should be: with semicolon
  1082. }
  1083. TEST(DeclPrinter, TestTemplateArgumentList11) {
  1084. ASSERT_TRUE(PrintedDeclCXX98Matches(
  1085. "template<int I> struct Z {};"
  1086. "Z<42 * 10 - 420 / 1> A;",
  1087. "A",
  1088. "Z<42 * 10 - 420 / 1> A"));
  1089. // Should be: with semicolon
  1090. }
  1091. TEST(DeclPrinter, TestTemplateArgumentList12) {
  1092. ASSERT_TRUE(PrintedDeclCXX98Matches(
  1093. "template<const char *p> struct Z {};"
  1094. "extern const char X[] = \"aaa\";"
  1095. "Z<X> A;",
  1096. "A",
  1097. "Z<X> A"));
  1098. // Should be: with semicolon
  1099. }
  1100. TEST(DeclPrinter, TestTemplateArgumentList13) {
  1101. ASSERT_TRUE(PrintedDeclCXX11Matches(
  1102. "template<typename... T> struct Z {};"
  1103. "template<typename... T> struct X {"
  1104. " Z<T...> A;"
  1105. "};",
  1106. "A",
  1107. "Z<T...> A"));
  1108. // Should be: with semicolon
  1109. }
  1110. TEST(DeclPrinter, TestTemplateArgumentList14) {
  1111. ASSERT_TRUE(PrintedDeclCXX11Matches(
  1112. "template<typename... T> struct Z {};"
  1113. "template<typename T> struct Y {};"
  1114. "template<typename... T> struct X {"
  1115. " Z<Y<T>...> A;"
  1116. "};",
  1117. "A",
  1118. "Z<Y<T>...> A"));
  1119. // Should be: with semicolon
  1120. }
  1121. TEST(DeclPrinter, TestTemplateArgumentList15) {
  1122. ASSERT_TRUE(PrintedDeclCXX11Matches(
  1123. "template<unsigned I> struct Z {};"
  1124. "template<typename... T> struct X {"
  1125. " Z<sizeof...(T)> A;"
  1126. "};",
  1127. "A",
  1128. "Z<sizeof...(T)> A"));
  1129. // Should be: with semicolon
  1130. }
  1131. TEST(DeclPrinter, TestStaticAssert1) {
  1132. ASSERT_TRUE(PrintedDeclCXX1ZMatches(
  1133. "static_assert(true);",
  1134. staticAssertDecl().bind("id"),
  1135. "static_assert(true)"));
  1136. }
  1137. TEST(DeclPrinter, TestObjCMethod1) {
  1138. ASSERT_TRUE(PrintedDeclObjCMatches(
  1139. "__attribute__((objc_root_class)) @interface X\n"
  1140. "- (int)A:(id)anObject inRange:(long)range;\n"
  1141. "@end\n"
  1142. "@implementation X\n"
  1143. "- (int)A:(id)anObject inRange:(long)range { int printThis; return 0; }\n"
  1144. "@end\n",
  1145. namedDecl(hasName("A:inRange:"),
  1146. hasDescendant(namedDecl(hasName("printThis")))).bind("id"),
  1147. "- (int)A:(id)anObject inRange:(long)range"));
  1148. }
  1149. TEST(DeclPrinter, TestObjCProtocol1) {
  1150. ASSERT_TRUE(PrintedDeclObjCMatches(
  1151. "@protocol P1, P2;",
  1152. namedDecl(hasName("P1")).bind("id"),
  1153. "@protocol P1;\n"));
  1154. ASSERT_TRUE(PrintedDeclObjCMatches(
  1155. "@protocol P1, P2;",
  1156. namedDecl(hasName("P2")).bind("id"),
  1157. "@protocol P2;\n"));
  1158. }
  1159. TEST(DeclPrinter, TestObjCProtocol2) {
  1160. ASSERT_TRUE(PrintedDeclObjCMatches(
  1161. "@protocol P2 @end"
  1162. "@protocol P1<P2> @end",
  1163. namedDecl(hasName("P1")).bind("id"),
  1164. "@protocol P1<P2>\n@end"));
  1165. }