DeclPrinterTest.cpp 35 KB

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