DeclPrinterTest.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  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.getNodeAs<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 {}"));
  225. }
  226. TEST(DeclPrinter, TestCXXRecordDecl2) {
  227. ASSERT_TRUE(PrintedDeclCXX98Matches(
  228. "struct A { int a; };",
  229. "A",
  230. "struct A {}"));
  231. }
  232. TEST(DeclPrinter, TestCXXRecordDecl3) {
  233. ASSERT_TRUE(PrintedDeclCXX98Matches(
  234. "union A { int a; };",
  235. "A",
  236. "union A {}"));
  237. }
  238. TEST(DeclPrinter, TestCXXRecordDecl4) {
  239. ASSERT_TRUE(PrintedDeclCXX98Matches(
  240. "class Z { int a; };"
  241. "class A : Z { int b; };",
  242. "A",
  243. "class A : Z {}"));
  244. }
  245. TEST(DeclPrinter, TestCXXRecordDecl5) {
  246. ASSERT_TRUE(PrintedDeclCXX98Matches(
  247. "struct Z { int a; };"
  248. "struct A : Z { int b; };",
  249. "A",
  250. "struct A : Z {}"));
  251. }
  252. TEST(DeclPrinter, TestCXXRecordDecl6) {
  253. ASSERT_TRUE(PrintedDeclCXX98Matches(
  254. "class Z { int a; };"
  255. "class A : public Z { int b; };",
  256. "A",
  257. "class A : public Z {}"));
  258. }
  259. TEST(DeclPrinter, TestCXXRecordDecl7) {
  260. ASSERT_TRUE(PrintedDeclCXX98Matches(
  261. "class Z { int a; };"
  262. "class A : protected Z { int b; };",
  263. "A",
  264. "class A : protected Z {}"));
  265. }
  266. TEST(DeclPrinter, TestCXXRecordDecl8) {
  267. ASSERT_TRUE(PrintedDeclCXX98Matches(
  268. "class Z { int a; };"
  269. "class A : private Z { int b; };",
  270. "A",
  271. "class A : private Z {}"));
  272. }
  273. TEST(DeclPrinter, TestCXXRecordDecl9) {
  274. ASSERT_TRUE(PrintedDeclCXX98Matches(
  275. "class Z { int a; };"
  276. "class A : virtual Z { int b; };",
  277. "A",
  278. "class A : virtual Z {}"));
  279. }
  280. TEST(DeclPrinter, TestCXXRecordDecl10) {
  281. ASSERT_TRUE(PrintedDeclCXX98Matches(
  282. "class Z { int a; };"
  283. "class A : virtual public Z { int b; };",
  284. "A",
  285. "class A : virtual public Z {}"));
  286. }
  287. TEST(DeclPrinter, TestCXXRecordDecl11) {
  288. ASSERT_TRUE(PrintedDeclCXX98Matches(
  289. "class Z { int a; };"
  290. "class Y : virtual public Z { int b; };"
  291. "class A : virtual public Z, private Y { int c; };",
  292. "A",
  293. "class A : virtual public Z, private Y {}"));
  294. }
  295. TEST(DeclPrinter, TestFunctionDecl1) {
  296. ASSERT_TRUE(PrintedDeclCXX98Matches(
  297. "void A();",
  298. "A",
  299. "void A()"));
  300. }
  301. TEST(DeclPrinter, TestFunctionDecl2) {
  302. ASSERT_TRUE(PrintedDeclCXX98Matches(
  303. "void A() {}",
  304. "A",
  305. "void A()"));
  306. }
  307. TEST(DeclPrinter, TestFunctionDecl3) {
  308. ASSERT_TRUE(PrintedDeclCXX98Matches(
  309. "void Z();"
  310. "void A() { Z(); }",
  311. "A",
  312. "void A()"));
  313. }
  314. TEST(DeclPrinter, TestFunctionDecl4) {
  315. ASSERT_TRUE(PrintedDeclCXX98Matches(
  316. "extern void A();",
  317. "A",
  318. "extern void A()"));
  319. }
  320. TEST(DeclPrinter, TestFunctionDecl5) {
  321. ASSERT_TRUE(PrintedDeclCXX98Matches(
  322. "static void A();",
  323. "A",
  324. "static void A()"));
  325. }
  326. TEST(DeclPrinter, TestFunctionDecl6) {
  327. ASSERT_TRUE(PrintedDeclCXX98Matches(
  328. "inline void A();",
  329. "A",
  330. "inline void A()"));
  331. }
  332. TEST(DeclPrinter, TestFunctionDecl7) {
  333. ASSERT_TRUE(PrintedDeclCXX11Matches(
  334. "constexpr int A(int a);",
  335. "A",
  336. "constexpr int A(int a)"));
  337. }
  338. TEST(DeclPrinter, TestFunctionDecl8) {
  339. ASSERT_TRUE(PrintedDeclCXX98Matches(
  340. "void A(int a);",
  341. "A",
  342. "void A(int a)"));
  343. }
  344. TEST(DeclPrinter, TestFunctionDecl9) {
  345. ASSERT_TRUE(PrintedDeclCXX98Matches(
  346. "void A(...);",
  347. "A",
  348. "void A(...)"));
  349. }
  350. TEST(DeclPrinter, TestFunctionDecl10) {
  351. ASSERT_TRUE(PrintedDeclCXX98Matches(
  352. "void A(int a, ...);",
  353. "A",
  354. "void A(int a, ...)"));
  355. }
  356. TEST(DeclPrinter, TestFunctionDecl11) {
  357. ASSERT_TRUE(PrintedDeclCXX98Matches(
  358. "typedef long ssize_t;"
  359. "typedef int *pInt;"
  360. "void A(int a, pInt b, ssize_t c);",
  361. "A",
  362. "void A(int a, pInt b, ssize_t c)"));
  363. }
  364. TEST(DeclPrinter, TestFunctionDecl12) {
  365. ASSERT_TRUE(PrintedDeclCXX98Matches(
  366. "void A(int a, int b = 0);",
  367. "A",
  368. "void A(int a, int b = 0)"));
  369. }
  370. TEST(DeclPrinter, TestFunctionDecl13) {
  371. ASSERT_TRUE(PrintedDeclCXX98Matches(
  372. "void (*A(int a))(int b);",
  373. "A",
  374. "void (*A(int a))(int)"));
  375. // Should be: with parameter name (?)
  376. }
  377. TEST(DeclPrinter, TestFunctionDecl14) {
  378. ASSERT_TRUE(PrintedDeclCXX98Matches(
  379. "template<typename T>"
  380. "void A(T t) { }"
  381. "template<>"
  382. "void A(int N) { }",
  383. functionDecl(hasName("A"), isExplicitTemplateSpecialization()).bind("id"),
  384. "template<> void A<int>(int N)"));
  385. }
  386. TEST(DeclPrinter, TestCXXConstructorDecl1) {
  387. ASSERT_TRUE(PrintedDeclCXX98Matches(
  388. "struct A {"
  389. " A();"
  390. "};",
  391. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  392. "A()"));
  393. }
  394. TEST(DeclPrinter, TestCXXConstructorDecl2) {
  395. ASSERT_TRUE(PrintedDeclCXX98Matches(
  396. "struct A {"
  397. " A(int a);"
  398. "};",
  399. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  400. "A(int a)"));
  401. }
  402. TEST(DeclPrinter, TestCXXConstructorDecl3) {
  403. ASSERT_TRUE(PrintedDeclCXX98Matches(
  404. "struct A {"
  405. " A(const A &a);"
  406. "};",
  407. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  408. "A(const A &a)"));
  409. }
  410. TEST(DeclPrinter, TestCXXConstructorDecl4) {
  411. ASSERT_TRUE(PrintedDeclCXX98Matches(
  412. "struct A {"
  413. " A(const A &a, int = 0);"
  414. "};",
  415. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  416. "A(const A &a, int = 0)"));
  417. }
  418. TEST(DeclPrinter, TestCXXConstructorDecl5) {
  419. ASSERT_TRUE(PrintedDeclCXX11Matches(
  420. "struct A {"
  421. " A(const A &&a);"
  422. "};",
  423. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  424. "A(const A &&a)"));
  425. }
  426. TEST(DeclPrinter, TestCXXConstructorDecl6) {
  427. ASSERT_TRUE(PrintedDeclCXX98Matches(
  428. "struct A {"
  429. " explicit A(int a);"
  430. "};",
  431. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  432. "explicit A(int a)"));
  433. }
  434. TEST(DeclPrinter, TestCXXConstructorDecl7) {
  435. ASSERT_TRUE(PrintedDeclCXX11Matches(
  436. "struct A {"
  437. " constexpr A();"
  438. "};",
  439. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  440. "constexpr A()"));
  441. }
  442. TEST(DeclPrinter, TestCXXConstructorDecl8) {
  443. ASSERT_TRUE(PrintedDeclCXX11Matches(
  444. "struct A {"
  445. " A() = default;"
  446. "};",
  447. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  448. "A() = default"));
  449. }
  450. TEST(DeclPrinter, TestCXXConstructorDecl9) {
  451. ASSERT_TRUE(PrintedDeclCXX11Matches(
  452. "struct A {"
  453. " A() = delete;"
  454. "};",
  455. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  456. "A() = delete"));
  457. }
  458. TEST(DeclPrinter, TestCXXConstructorDecl10) {
  459. ASSERT_TRUE(PrintedDeclCXX11Matches(
  460. "template<typename... T>"
  461. "struct A {"
  462. " A(const A &a);"
  463. "};",
  464. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  465. "A<T...>(const A<T...> &a)"));
  466. }
  467. TEST(DeclPrinter, TestCXXConstructorDecl11) {
  468. ASSERT_TRUE(PrintedDeclCXX11nonMSCMatches(
  469. "template<typename... T>"
  470. "struct A : public T... {"
  471. " A(T&&... ts) : T(ts)... {}"
  472. "};",
  473. cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
  474. "A<T...>(T &&...ts) : T(ts)... {}"));
  475. }
  476. TEST(DeclPrinter, TestCXXDestructorDecl1) {
  477. ASSERT_TRUE(PrintedDeclCXX98Matches(
  478. "struct A {"
  479. " ~A();"
  480. "};",
  481. cxxDestructorDecl(ofClass(hasName("A"))).bind("id"),
  482. "~A()"));
  483. }
  484. TEST(DeclPrinter, TestCXXDestructorDecl2) {
  485. ASSERT_TRUE(PrintedDeclCXX98Matches(
  486. "struct A {"
  487. " virtual ~A();"
  488. "};",
  489. cxxDestructorDecl(ofClass(hasName("A"))).bind("id"),
  490. "virtual ~A()"));
  491. }
  492. TEST(DeclPrinter, TestCXXConversionDecl1) {
  493. ASSERT_TRUE(PrintedDeclCXX98Matches(
  494. "struct A {"
  495. " operator int();"
  496. "};",
  497. cxxMethodDecl(ofClass(hasName("A"))).bind("id"),
  498. "operator int()"));
  499. }
  500. TEST(DeclPrinter, TestCXXConversionDecl2) {
  501. ASSERT_TRUE(PrintedDeclCXX98Matches(
  502. "struct A {"
  503. " operator bool();"
  504. "};",
  505. cxxMethodDecl(ofClass(hasName("A"))).bind("id"),
  506. "operator bool()"));
  507. }
  508. TEST(DeclPrinter, TestCXXConversionDecl3) {
  509. ASSERT_TRUE(PrintedDeclCXX98Matches(
  510. "struct Z {};"
  511. "struct A {"
  512. " operator Z();"
  513. "};",
  514. cxxMethodDecl(ofClass(hasName("A"))).bind("id"),
  515. "operator Z()"));
  516. }
  517. TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction1) {
  518. ASSERT_TRUE(PrintedDeclCXX11Matches(
  519. "namespace std { typedef decltype(sizeof(int)) size_t; }"
  520. "struct Z {"
  521. " void *operator new(std::size_t);"
  522. "};",
  523. cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
  524. "void *operator new(std::size_t)"));
  525. }
  526. TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction2) {
  527. ASSERT_TRUE(PrintedDeclCXX11Matches(
  528. "namespace std { typedef decltype(sizeof(int)) size_t; }"
  529. "struct Z {"
  530. " void *operator new[](std::size_t);"
  531. "};",
  532. cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
  533. "void *operator new[](std::size_t)"));
  534. }
  535. TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction3) {
  536. ASSERT_TRUE(PrintedDeclCXX11Matches(
  537. "struct Z {"
  538. " void operator delete(void *);"
  539. "};",
  540. cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
  541. "void operator delete(void *) noexcept"));
  542. // Should be: without noexcept?
  543. }
  544. TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction4) {
  545. ASSERT_TRUE(PrintedDeclCXX98Matches(
  546. "struct Z {"
  547. " void operator delete(void *);"
  548. "};",
  549. cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
  550. "void operator delete(void *)"));
  551. }
  552. TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction5) {
  553. ASSERT_TRUE(PrintedDeclCXX11Matches(
  554. "struct Z {"
  555. " void operator delete[](void *);"
  556. "};",
  557. cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
  558. "void operator delete[](void *) noexcept"));
  559. // Should be: without noexcept?
  560. }
  561. TEST(DeclPrinter, TestCXXMethodDecl_Operator1) {
  562. const char *OperatorNames[] = {
  563. "+", "-", "*", "/", "%", "^", "&", "|",
  564. "=", "<", ">", "+=", "-=", "*=", "/=", "%=",
  565. "^=", "&=", "|=", "<<", ">>", ">>=", "<<=", "==", "!=",
  566. "<=", ">=", "&&", "||", ",", "->*",
  567. "()", "[]"
  568. };
  569. for (unsigned i = 0, e = llvm::array_lengthof(OperatorNames); i != e; ++i) {
  570. SmallString<128> Code;
  571. Code.append("struct Z { void operator");
  572. Code.append(OperatorNames[i]);
  573. Code.append("(Z z); };");
  574. SmallString<128> Expected;
  575. Expected.append("void operator");
  576. Expected.append(OperatorNames[i]);
  577. Expected.append("(Z z)");
  578. ASSERT_TRUE(PrintedDeclCXX98Matches(
  579. Code,
  580. cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
  581. Expected));
  582. }
  583. }
  584. TEST(DeclPrinter, TestCXXMethodDecl_Operator2) {
  585. const char *OperatorNames[] = {
  586. "~", "!", "++", "--", "->"
  587. };
  588. for (unsigned i = 0, e = llvm::array_lengthof(OperatorNames); i != e; ++i) {
  589. SmallString<128> Code;
  590. Code.append("struct Z { void operator");
  591. Code.append(OperatorNames[i]);
  592. Code.append("(); };");
  593. SmallString<128> Expected;
  594. Expected.append("void operator");
  595. Expected.append(OperatorNames[i]);
  596. Expected.append("()");
  597. ASSERT_TRUE(PrintedDeclCXX98Matches(
  598. Code,
  599. cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
  600. Expected));
  601. }
  602. }
  603. TEST(DeclPrinter, TestCXXMethodDecl1) {
  604. ASSERT_TRUE(PrintedDeclCXX98Matches(
  605. "struct Z {"
  606. " void A(int a);"
  607. "};",
  608. "A",
  609. "void A(int a)"));
  610. }
  611. TEST(DeclPrinter, TestCXXMethodDecl2) {
  612. ASSERT_TRUE(PrintedDeclCXX98Matches(
  613. "struct Z {"
  614. " virtual void A(int a);"
  615. "};",
  616. "A",
  617. "virtual void A(int a)"));
  618. }
  619. TEST(DeclPrinter, TestCXXMethodDecl3) {
  620. ASSERT_TRUE(PrintedDeclCXX98Matches(
  621. "struct Z {"
  622. " virtual void A(int a);"
  623. "};"
  624. "struct ZZ : Z {"
  625. " void A(int a);"
  626. "};",
  627. "ZZ::A",
  628. "void A(int a)"));
  629. // TODO: should we print "virtual"?
  630. }
  631. TEST(DeclPrinter, TestCXXMethodDecl4) {
  632. ASSERT_TRUE(PrintedDeclCXX98Matches(
  633. "struct Z {"
  634. " inline void A(int a);"
  635. "};",
  636. "A",
  637. "inline void A(int a)"));
  638. }
  639. TEST(DeclPrinter, TestCXXMethodDecl5) {
  640. ASSERT_TRUE(PrintedDeclCXX98Matches(
  641. "struct Z {"
  642. " virtual void A(int a) = 0;"
  643. "};",
  644. "A",
  645. "virtual void A(int a) = 0"));
  646. }
  647. TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier1) {
  648. ASSERT_TRUE(PrintedDeclCXX98Matches(
  649. "struct Z {"
  650. " void A(int a) const;"
  651. "};",
  652. "A",
  653. "void A(int a) const"));
  654. }
  655. TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier2) {
  656. ASSERT_TRUE(PrintedDeclCXX98Matches(
  657. "struct Z {"
  658. " void A(int a) volatile;"
  659. "};",
  660. "A",
  661. "void A(int a) volatile"));
  662. }
  663. TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier3) {
  664. ASSERT_TRUE(PrintedDeclCXX98Matches(
  665. "struct Z {"
  666. " void A(int a) const volatile;"
  667. "};",
  668. "A",
  669. "void A(int a) const volatile"));
  670. }
  671. TEST(DeclPrinter, TestCXXMethodDecl_RefQualifier1) {
  672. ASSERT_TRUE(PrintedDeclCXX11Matches(
  673. "struct Z {"
  674. " void A(int a) &;"
  675. "};",
  676. "A",
  677. "void A(int a) &"));
  678. }
  679. TEST(DeclPrinter, TestCXXMethodDecl_RefQualifier2) {
  680. ASSERT_TRUE(PrintedDeclCXX11Matches(
  681. "struct Z {"
  682. " void A(int a) &&;"
  683. "};",
  684. "A",
  685. "void A(int a) &&"));
  686. }
  687. TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification1) {
  688. ASSERT_TRUE(PrintedDeclCXX98Matches(
  689. "struct Z {"
  690. " void A(int a) throw();"
  691. "};",
  692. "A",
  693. "void A(int a) throw()"));
  694. }
  695. TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification2) {
  696. ASSERT_TRUE(PrintedDeclCXX98Matches(
  697. "struct Z {"
  698. " void A(int a) throw(int);"
  699. "};",
  700. "A",
  701. "void A(int a) throw(int)"));
  702. }
  703. TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification3) {
  704. ASSERT_TRUE(PrintedDeclCXX98Matches(
  705. "class ZZ {};"
  706. "struct Z {"
  707. " void A(int a) throw(ZZ, int);"
  708. "};",
  709. "A",
  710. "void A(int a) throw(ZZ, int)"));
  711. }
  712. TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification4) {
  713. ASSERT_TRUE(PrintedDeclCXX11Matches(
  714. "struct Z {"
  715. " void A(int a) noexcept;"
  716. "};",
  717. "A",
  718. "void A(int a) noexcept"));
  719. }
  720. TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification5) {
  721. ASSERT_TRUE(PrintedDeclCXX11Matches(
  722. "struct Z {"
  723. " void A(int a) noexcept(true);"
  724. "};",
  725. "A",
  726. "void A(int a) noexcept(trueA(int a) noexcept(true)"));
  727. // WRONG; Should be: "void A(int a) noexcept(true);"
  728. }
  729. TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification6) {
  730. ASSERT_TRUE(PrintedDeclCXX11Matches(
  731. "struct Z {"
  732. " void A(int a) noexcept(1 < 2);"
  733. "};",
  734. "A",
  735. "void A(int a) noexcept(1 < 2A(int a) noexcept(1 < 2)"));
  736. // WRONG; Should be: "void A(int a) noexcept(1 < 2);"
  737. }
  738. TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification7) {
  739. ASSERT_TRUE(PrintedDeclCXX11Matches(
  740. "template<int N>"
  741. "struct Z {"
  742. " void A(int a) noexcept(N < 2);"
  743. "};",
  744. "A",
  745. "void A(int a) noexcept(N < 2A(int a) noexcept(N < 2)"));
  746. // WRONG; Should be: "void A(int a) noexcept(N < 2);"
  747. }
  748. TEST(DeclPrinter, TestVarDecl1) {
  749. ASSERT_TRUE(PrintedDeclCXX98Matches(
  750. "char *const (*(*A)[5])(int);",
  751. "A",
  752. "char *const (*(*A)[5])(int)"));
  753. // Should be: with semicolon
  754. }
  755. TEST(DeclPrinter, TestVarDecl2) {
  756. ASSERT_TRUE(PrintedDeclCXX98Matches(
  757. "void (*A)() throw(int);",
  758. "A",
  759. "void (*A)() throw(int)"));
  760. // Should be: with semicolon
  761. }
  762. TEST(DeclPrinter, TestVarDecl3) {
  763. ASSERT_TRUE(PrintedDeclCXX11Matches(
  764. "void (*A)() noexcept;",
  765. "A",
  766. "void (*A)() noexcept"));
  767. // Should be: with semicolon
  768. }
  769. TEST(DeclPrinter, TestFieldDecl1) {
  770. ASSERT_TRUE(PrintedDeclCXX98Matches(
  771. "template<typename T>"
  772. "struct Z { T A; };",
  773. "A",
  774. "T A"));
  775. // Should be: with semicolon
  776. }
  777. TEST(DeclPrinter, TestFieldDecl2) {
  778. ASSERT_TRUE(PrintedDeclCXX98Matches(
  779. "template<int N>"
  780. "struct Z { int A[N]; };",
  781. "A",
  782. "int A[N]"));
  783. // Should be: with semicolon
  784. }
  785. TEST(DeclPrinter, TestClassTemplateDecl1) {
  786. ASSERT_TRUE(PrintedDeclCXX98Matches(
  787. "template<typename T>"
  788. "struct A { T a; };",
  789. classTemplateDecl(hasName("A")).bind("id"),
  790. "template <typename T> struct A {}"));
  791. }
  792. TEST(DeclPrinter, TestClassTemplateDecl2) {
  793. ASSERT_TRUE(PrintedDeclCXX98Matches(
  794. "template<typename T = int>"
  795. "struct A { T a; };",
  796. classTemplateDecl(hasName("A")).bind("id"),
  797. "template <typename T = int> struct A {}"));
  798. }
  799. TEST(DeclPrinter, TestClassTemplateDecl3) {
  800. ASSERT_TRUE(PrintedDeclCXX98Matches(
  801. "template<class T>"
  802. "struct A { T a; };",
  803. classTemplateDecl(hasName("A")).bind("id"),
  804. "template <class T> struct A {}"));
  805. }
  806. TEST(DeclPrinter, TestClassTemplateDecl4) {
  807. ASSERT_TRUE(PrintedDeclCXX98Matches(
  808. "template<typename T, typename U>"
  809. "struct A { T a; U b; };",
  810. classTemplateDecl(hasName("A")).bind("id"),
  811. "template <typename T, typename U> struct A {}"));
  812. }
  813. TEST(DeclPrinter, TestClassTemplateDecl5) {
  814. ASSERT_TRUE(PrintedDeclCXX98Matches(
  815. "template<int N>"
  816. "struct A { int a[N]; };",
  817. classTemplateDecl(hasName("A")).bind("id"),
  818. "template <int N> struct A {}"));
  819. }
  820. TEST(DeclPrinter, TestClassTemplateDecl6) {
  821. ASSERT_TRUE(PrintedDeclCXX98Matches(
  822. "template<int N = 42>"
  823. "struct A { int a[N]; };",
  824. classTemplateDecl(hasName("A")).bind("id"),
  825. "template <int N = 42> struct A {}"));
  826. }
  827. TEST(DeclPrinter, TestClassTemplateDecl7) {
  828. ASSERT_TRUE(PrintedDeclCXX98Matches(
  829. "typedef int MyInt;"
  830. "template<MyInt N>"
  831. "struct A { int a[N]; };",
  832. classTemplateDecl(hasName("A")).bind("id"),
  833. "template <MyInt N> struct A {}"));
  834. }
  835. TEST(DeclPrinter, TestClassTemplateDecl8) {
  836. ASSERT_TRUE(PrintedDeclCXX98Matches(
  837. "template<template<typename U> class T> struct A { };",
  838. classTemplateDecl(hasName("A")).bind("id"),
  839. "template <template <typename U> class T> struct A {}"));
  840. }
  841. TEST(DeclPrinter, TestClassTemplateDecl9) {
  842. ASSERT_TRUE(PrintedDeclCXX98Matches(
  843. "template<typename T> struct Z { };"
  844. "template<template<typename U> class T = Z> struct A { };",
  845. classTemplateDecl(hasName("A")).bind("id"),
  846. "template <template <typename U> class T> struct A {}"));
  847. }
  848. TEST(DeclPrinter, TestClassTemplateDecl10) {
  849. ASSERT_TRUE(PrintedDeclCXX11Matches(
  850. "template<typename... T>"
  851. "struct A { int a; };",
  852. classTemplateDecl(hasName("A")).bind("id"),
  853. "template <typename ...T> struct A {}"));
  854. }
  855. TEST(DeclPrinter, TestClassTemplateDecl11) {
  856. ASSERT_TRUE(PrintedDeclCXX11Matches(
  857. "template<typename... T>"
  858. "struct A : public T... { int a; };",
  859. classTemplateDecl(hasName("A")).bind("id"),
  860. "template <typename ...T> struct A : public T... {}"));
  861. }
  862. TEST(DeclPrinter, TestClassTemplatePartialSpecializationDecl1) {
  863. ASSERT_TRUE(PrintedDeclCXX98Matches(
  864. "template<typename T, typename U>"
  865. "struct A { T a; U b; };"
  866. "template<typename T>"
  867. "struct A<T, int> { T a; };",
  868. classTemplateSpecializationDecl().bind("id"),
  869. "template <typename T> struct A<T, int> {}"));
  870. }
  871. TEST(DeclPrinter, TestClassTemplatePartialSpecializationDecl2) {
  872. ASSERT_TRUE(PrintedDeclCXX98Matches(
  873. "template<typename T>"
  874. "struct A { T a; };"
  875. "template<typename T>"
  876. "struct A<T *> { T a; };",
  877. classTemplateSpecializationDecl().bind("id"),
  878. "template <typename T> struct A<type-parameter-0-0 *> {}"));
  879. // WRONG; Should be: "template<typename T> struct A<T *> { ... }"
  880. }
  881. TEST(DeclPrinter, TestClassTemplateSpecializationDecl1) {
  882. ASSERT_TRUE(PrintedDeclCXX98Matches(
  883. "template<typename T>"
  884. "struct A { T a; };"
  885. "template<>"
  886. "struct A<int> { int a; };",
  887. classTemplateSpecializationDecl().bind("id"),
  888. "template<> struct A<int> {}"));
  889. }
  890. TEST(DeclPrinter, TestFunctionTemplateDecl1) {
  891. ASSERT_TRUE(PrintedDeclCXX98Matches(
  892. "template<typename T>"
  893. "void A(T &t);",
  894. functionTemplateDecl(hasName("A")).bind("id"),
  895. "template <typename T> void A(T &t)"));
  896. }
  897. TEST(DeclPrinter, TestFunctionTemplateDecl2) {
  898. ASSERT_TRUE(PrintedDeclCXX98Matches(
  899. "template<typename T>"
  900. "void A(T &t) { }",
  901. functionTemplateDecl(hasName("A")).bind("id"),
  902. "template <typename T> void A(T &t)"));
  903. }
  904. TEST(DeclPrinter, TestFunctionTemplateDecl3) {
  905. ASSERT_TRUE(PrintedDeclCXX11Matches(
  906. "template<typename... T>"
  907. "void A(T... a);",
  908. functionTemplateDecl(hasName("A")).bind("id"),
  909. "template <typename ...T> void A(T ...a)"));
  910. }
  911. TEST(DeclPrinter, TestFunctionTemplateDecl4) {
  912. ASSERT_TRUE(PrintedDeclCXX98Matches(
  913. "struct Z { template<typename T> void A(T t); };",
  914. functionTemplateDecl(hasName("A")).bind("id"),
  915. "template <typename T> void A(T t)"));
  916. }
  917. TEST(DeclPrinter, TestFunctionTemplateDecl5) {
  918. ASSERT_TRUE(PrintedDeclCXX98Matches(
  919. "struct Z { template<typename T> void A(T t) {} };",
  920. functionTemplateDecl(hasName("A")).bind("id"),
  921. "template <typename T> void A(T t)"));
  922. }
  923. TEST(DeclPrinter, TestFunctionTemplateDecl6) {
  924. ASSERT_TRUE(PrintedDeclCXX98Matches(
  925. "template<typename T >struct Z {"
  926. " template<typename U> void A(U t) {}"
  927. "};",
  928. functionTemplateDecl(hasName("A")).bind("id"),
  929. "template <typename U> void A(U t)"));
  930. }
  931. TEST(DeclPrinter, TestTemplateArgumentList1) {
  932. ASSERT_TRUE(PrintedDeclCXX98Matches(
  933. "template<typename T> struct Z {};"
  934. "struct X {};"
  935. "Z<X> A;",
  936. "A",
  937. "Z<X> A"));
  938. // Should be: with semicolon
  939. }
  940. TEST(DeclPrinter, TestTemplateArgumentList2) {
  941. ASSERT_TRUE(PrintedDeclCXX98Matches(
  942. "template<typename T, typename U> struct Z {};"
  943. "struct X {};"
  944. "typedef int Y;"
  945. "Z<X, Y> A;",
  946. "A",
  947. "Z<X, Y> A"));
  948. // Should be: with semicolon
  949. }
  950. TEST(DeclPrinter, TestTemplateArgumentList3) {
  951. ASSERT_TRUE(PrintedDeclCXX98Matches(
  952. "template<typename T> struct Z {};"
  953. "template<typename T> struct X {};"
  954. "Z<X<int> > A;",
  955. "A",
  956. "Z<X<int> > A"));
  957. // Should be: with semicolon
  958. }
  959. TEST(DeclPrinter, TestTemplateArgumentList4) {
  960. ASSERT_TRUE(PrintedDeclCXX11Matches(
  961. "template<typename T> struct Z {};"
  962. "template<typename T> struct X {};"
  963. "Z<X<int>> A;",
  964. "A",
  965. "Z<X<int> > A"));
  966. // Should be: with semicolon, without extra space in "> >"
  967. }
  968. TEST(DeclPrinter, TestTemplateArgumentList5) {
  969. ASSERT_TRUE(PrintedDeclCXX98Matches(
  970. "template<typename T> struct Z {};"
  971. "template<typename T> struct X { Z<T> A; };",
  972. "A",
  973. "Z<T> A"));
  974. // Should be: with semicolon
  975. }
  976. TEST(DeclPrinter, TestTemplateArgumentList6) {
  977. ASSERT_TRUE(PrintedDeclCXX98Matches(
  978. "template<template<typename T> class U> struct Z {};"
  979. "template<typename T> struct X {};"
  980. "Z<X> A;",
  981. "A",
  982. "Z<X> A"));
  983. // Should be: with semicolon
  984. }
  985. TEST(DeclPrinter, TestTemplateArgumentList7) {
  986. ASSERT_TRUE(PrintedDeclCXX98Matches(
  987. "template<template<typename T> class U> struct Z {};"
  988. "template<template<typename T> class U> struct Y {"
  989. " Z<U> A;"
  990. "};",
  991. "A",
  992. "Z<U> A"));
  993. // Should be: with semicolon
  994. }
  995. TEST(DeclPrinter, TestTemplateArgumentList8) {
  996. ASSERT_TRUE(PrintedDeclCXX98Matches(
  997. "template<typename T> struct Z {};"
  998. "template<template<typename T> class U> struct Y {"
  999. " Z<U<int> > A;"
  1000. "};",
  1001. "A",
  1002. "Z<U<int> > A"));
  1003. // Should be: with semicolon
  1004. }
  1005. TEST(DeclPrinter, TestTemplateArgumentList9) {
  1006. ASSERT_TRUE(PrintedDeclCXX98Matches(
  1007. "template<unsigned I> struct Z {};"
  1008. "Z<0> A;",
  1009. "A",
  1010. "Z<0> A"));
  1011. // Should be: with semicolon
  1012. }
  1013. TEST(DeclPrinter, TestTemplateArgumentList10) {
  1014. ASSERT_TRUE(PrintedDeclCXX98Matches(
  1015. "template<unsigned I> struct Z {};"
  1016. "template<unsigned I> struct X { Z<I> A; };",
  1017. "A",
  1018. "Z<I> A"));
  1019. // Should be: with semicolon
  1020. }
  1021. TEST(DeclPrinter, TestTemplateArgumentList11) {
  1022. ASSERT_TRUE(PrintedDeclCXX98Matches(
  1023. "template<int I> struct Z {};"
  1024. "Z<42 * 10 - 420 / 1> A;",
  1025. "A",
  1026. "Z<42 * 10 - 420 / 1> A"));
  1027. // Should be: with semicolon
  1028. }
  1029. TEST(DeclPrinter, TestTemplateArgumentList12) {
  1030. ASSERT_TRUE(PrintedDeclCXX98Matches(
  1031. "template<const char *p> struct Z {};"
  1032. "extern const char X[] = \"aaa\";"
  1033. "Z<X> A;",
  1034. "A",
  1035. "Z<X> A"));
  1036. // Should be: with semicolon
  1037. }
  1038. TEST(DeclPrinter, TestTemplateArgumentList13) {
  1039. ASSERT_TRUE(PrintedDeclCXX11Matches(
  1040. "template<typename... T> struct Z {};"
  1041. "template<typename... T> struct X {"
  1042. " Z<T...> A;"
  1043. "};",
  1044. "A",
  1045. "Z<T...> A"));
  1046. // Should be: with semicolon
  1047. }
  1048. TEST(DeclPrinter, TestTemplateArgumentList14) {
  1049. ASSERT_TRUE(PrintedDeclCXX11Matches(
  1050. "template<typename... T> struct Z {};"
  1051. "template<typename T> struct Y {};"
  1052. "template<typename... T> struct X {"
  1053. " Z<Y<T>...> A;"
  1054. "};",
  1055. "A",
  1056. "Z<Y<T>...> A"));
  1057. // Should be: with semicolon
  1058. }
  1059. TEST(DeclPrinter, TestTemplateArgumentList15) {
  1060. ASSERT_TRUE(PrintedDeclCXX11Matches(
  1061. "template<unsigned I> struct Z {};"
  1062. "template<typename... T> struct X {"
  1063. " Z<sizeof...(T)> A;"
  1064. "};",
  1065. "A",
  1066. "Z<sizeof...(T)> A"));
  1067. // Should be: with semicolon
  1068. }
  1069. TEST(DeclPrinter, TestStaticAssert1) {
  1070. ASSERT_TRUE(PrintedDeclCXX1ZMatches(
  1071. "static_assert(true);",
  1072. staticAssertDecl().bind("id"),
  1073. "static_assert(true)"));
  1074. }
  1075. TEST(DeclPrinter, TestObjCMethod1) {
  1076. ASSERT_TRUE(PrintedDeclObjCMatches(
  1077. "__attribute__((objc_root_class)) @interface X\n"
  1078. "- (int)A:(id)anObject inRange:(long)range;\n"
  1079. "@end\n"
  1080. "@implementation X\n"
  1081. "- (int)A:(id)anObject inRange:(long)range { int printThis; return 0; }\n"
  1082. "@end\n",
  1083. namedDecl(hasName("A:inRange:"),
  1084. hasDescendant(namedDecl(hasName("printThis")))).bind("id"),
  1085. "- (int) A:(id)anObject inRange:(long)range"));
  1086. }
  1087. TEST(DeclPrinter, TestObjCProtocol1) {
  1088. ASSERT_TRUE(PrintedDeclObjCMatches(
  1089. "@protocol P1, P2;",
  1090. namedDecl(hasName("P1")).bind("id"),
  1091. "@protocol P1;\n"));
  1092. ASSERT_TRUE(PrintedDeclObjCMatches(
  1093. "@protocol P1, P2;",
  1094. namedDecl(hasName("P2")).bind("id"),
  1095. "@protocol P2;\n"));
  1096. }
  1097. TEST(DeclPrinter, TestObjCProtocol2) {
  1098. ASSERT_TRUE(PrintedDeclObjCMatches(
  1099. "@protocol P2 @end"
  1100. "@protocol P1<P2> @end",
  1101. namedDecl(hasName("P1")).bind("id"),
  1102. "@protocol P1<P2>\n@end"));
  1103. }