blob: 5340756c03dbc79dc4af8b2e155bf7f3188da79f [file] [log] [blame]
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001//===- 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
22#include "clang/AST/ASTContext.h"
23#include "clang/ASTMatchers/ASTMatchFinder.h"
24#include "clang/Tooling/Tooling.h"
Daniel Jasper7fd90b02012-08-24 05:50:27 +000025#include "llvm/ADT/SmallString.h"
Dmitri Gribenko49795ae2012-08-20 23:39:06 +000026#include "gtest/gtest.h"
27
28using namespace clang;
29using namespace ast_matchers;
30using namespace tooling;
31
32namespace {
33
34void PrintDecl(raw_ostream &Out, const ASTContext *Context, const Decl *D) {
35 PrintingPolicy Policy = Context->getPrintingPolicy();
Dmitri Gribenkod1fc82e2012-08-21 17:36:32 +000036 Policy.TerseOutput = true;
Dmitri Gribenko49795ae2012-08-20 23:39:06 +000037 D->print(Out, Policy, /*Indentation*/ 0, /*PrintInstantiation*/ false);
38}
39
40class PrintMatch : public MatchFinder::MatchCallback {
41 SmallString<1024> Printed;
42 unsigned NumFoundDecls;
43
44public:
45 PrintMatch() : NumFoundDecls(0) {}
46
47 virtual void run(const MatchFinder::MatchResult &Result) {
48 const Decl *D = Result.Nodes.getDeclAs<Decl>("id");
49 if (!D || D->isImplicit())
50 return;
51 NumFoundDecls++;
52 if (NumFoundDecls > 1)
53 return;
54
55 llvm::raw_svector_ostream Out(Printed);
56 PrintDecl(Out, Result.Context, D);
57 }
58
59 StringRef getPrinted() const {
60 return Printed;
61 }
62
63 unsigned getNumFoundDecls() const {
64 return NumFoundDecls;
65 }
66};
67
Dmitri Gribenko49795ae2012-08-20 23:39:06 +000068::testing::AssertionResult PrintedDeclMatches(
69 StringRef Code,
Dmitri Gribenko976f1182012-08-31 03:23:26 +000070 const std::vector<std::string> &Args,
Dmitri Gribenko49795ae2012-08-20 23:39:06 +000071 const DeclarationMatcher &NodeMatch,
Fariborz Jahaniana7a68b62012-10-18 19:12:17 +000072 StringRef ExpectedPrinted,
73 StringRef FileName) {
Dmitri Gribenko49795ae2012-08-20 23:39:06 +000074 PrintMatch Printer;
75 MatchFinder Finder;
76 Finder.addMatcher(NodeMatch, &Printer);
Stephen Hines651f13c2014-04-23 16:59:28 -070077 std::unique_ptr<FrontendActionFactory> Factory(
78 newFrontendActionFactory(&Finder));
Dmitri Gribenko49795ae2012-08-20 23:39:06 +000079
Fariborz Jahaniana7a68b62012-10-18 19:12:17 +000080 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args, FileName))
Stephen Hines651f13c2014-04-23 16:59:28 -070081 return testing::AssertionFailure()
82 << "Parsing error in \"" << Code.str() << "\"";
Dmitri Gribenko49795ae2012-08-20 23:39:06 +000083
84 if (Printer.getNumFoundDecls() == 0)
85 return testing::AssertionFailure()
86 << "Matcher didn't find any declarations";
87
88 if (Printer.getNumFoundDecls() > 1)
89 return testing::AssertionFailure()
90 << "Matcher should match only one declaration "
91 "(found " << Printer.getNumFoundDecls() << ")";
92
93 if (Printer.getPrinted() != ExpectedPrinted)
94 return ::testing::AssertionFailure()
Stephen Hines651f13c2014-04-23 16:59:28 -070095 << "Expected \"" << ExpectedPrinted.str() << "\", "
96 "got \"" << Printer.getPrinted().str() << "\"";
Dmitri Gribenko49795ae2012-08-20 23:39:06 +000097
98 return ::testing::AssertionSuccess();
99}
100
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000101::testing::AssertionResult PrintedDeclCXX98Matches(StringRef Code,
102 StringRef DeclName,
103 StringRef ExpectedPrinted) {
Dmitri Gribenko976f1182012-08-31 03:23:26 +0000104 std::vector<std::string> Args(1, "-std=c++98");
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000105 return PrintedDeclMatches(Code,
Dmitri Gribenko976f1182012-08-31 03:23:26 +0000106 Args,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000107 namedDecl(hasName(DeclName)).bind("id"),
Fariborz Jahaniana7a68b62012-10-18 19:12:17 +0000108 ExpectedPrinted,
109 "input.cc");
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000110}
111
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000112::testing::AssertionResult PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000113 StringRef Code,
114 const DeclarationMatcher &NodeMatch,
115 StringRef ExpectedPrinted) {
Dmitri Gribenko976f1182012-08-31 03:23:26 +0000116 std::vector<std::string> Args(1, "-std=c++98");
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000117 return PrintedDeclMatches(Code,
Dmitri Gribenko976f1182012-08-31 03:23:26 +0000118 Args,
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000119 NodeMatch,
Fariborz Jahaniana7a68b62012-10-18 19:12:17 +0000120 ExpectedPrinted,
121 "input.cc");
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000122}
123
124::testing::AssertionResult PrintedDeclCXX11Matches(StringRef Code,
125 StringRef DeclName,
126 StringRef ExpectedPrinted) {
Dmitri Gribenko976f1182012-08-31 03:23:26 +0000127 std::vector<std::string> Args(1, "-std=c++11");
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000128 return PrintedDeclMatches(Code,
Dmitri Gribenko976f1182012-08-31 03:23:26 +0000129 Args,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000130 namedDecl(hasName(DeclName)).bind("id"),
Fariborz Jahaniana7a68b62012-10-18 19:12:17 +0000131 ExpectedPrinted,
132 "input.cc");
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000133}
134
135::testing::AssertionResult PrintedDeclCXX11Matches(
136 StringRef Code,
137 const DeclarationMatcher &NodeMatch,
138 StringRef ExpectedPrinted) {
Dmitri Gribenko976f1182012-08-31 03:23:26 +0000139 std::vector<std::string> Args(1, "-std=c++11");
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000140 return PrintedDeclMatches(Code,
Dmitri Gribenko976f1182012-08-31 03:23:26 +0000141 Args,
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000142 NodeMatch,
Fariborz Jahaniana7a68b62012-10-18 19:12:17 +0000143 ExpectedPrinted,
144 "input.cc");
145}
146
Stephen Hines651f13c2014-04-23 16:59:28 -0700147::testing::AssertionResult PrintedDeclCXX11nonMSCMatches(
148 StringRef Code,
149 const DeclarationMatcher &NodeMatch,
150 StringRef ExpectedPrinted) {
151 std::vector<std::string> Args(1, "-std=c++11");
152 Args.push_back("-fno-delayed-template-parsing");
153 return PrintedDeclMatches(Code,
154 Args,
155 NodeMatch,
156 ExpectedPrinted,
157 "input.cc");
158}
159
Fariborz Jahaniana7a68b62012-10-18 19:12:17 +0000160::testing::AssertionResult PrintedDeclObjCMatches(
161 StringRef Code,
162 const DeclarationMatcher &NodeMatch,
163 StringRef ExpectedPrinted) {
164 std::vector<std::string> Args(1, "");
165 return PrintedDeclMatches(Code,
166 Args,
167 NodeMatch,
168 ExpectedPrinted,
169 "input.m");
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000170}
171
172} // unnamed namespace
173
Stephen Hines651f13c2014-04-23 16:59:28 -0700174TEST(DeclPrinter, TestTypedef1) {
175 ASSERT_TRUE(PrintedDeclCXX98Matches(
176 "typedef int A;",
177 "A",
178 "typedef int A"));
179 // Should be: with semicolon
180}
181
182TEST(DeclPrinter, TestTypedef2) {
183 ASSERT_TRUE(PrintedDeclCXX98Matches(
184 "typedef const char *A;",
185 "A",
186 "typedef const char *A"));
187 // Should be: with semicolon
188}
189
190TEST(DeclPrinter, TestTypedef3) {
191 ASSERT_TRUE(PrintedDeclCXX98Matches(
192 "template <typename Y> class X {};"
193 "typedef X<int> A;",
194 "A",
195 "typedef X<int> A"));
196 // Should be: with semicolon
197}
198
199TEST(DeclPrinter, TestTypedef4) {
200 ASSERT_TRUE(PrintedDeclCXX98Matches(
201 "namespace X { class Y {}; }"
202 "typedef X::Y A;",
203 "A",
204 "typedef X::Y A"));
205 // Should be: with semicolon
206}
207
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000208TEST(DeclPrinter, TestNamespace1) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000209 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000210 "namespace A { int B; }",
211 "A",
212 "namespace A {\n}"));
213 // Should be: with { ... }
214}
215
216TEST(DeclPrinter, TestNamespace2) {
217 ASSERT_TRUE(PrintedDeclCXX11Matches(
218 "inline namespace A { int B; }",
219 "A",
220 "inline namespace A {\n}"));
221 // Should be: with { ... }
222}
223
224TEST(DeclPrinter, TestNamespaceAlias1) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000225 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000226 "namespace Z { }"
227 "namespace A = Z;",
228 "A",
229 "namespace A = Z"));
230 // Should be: with semicolon
231}
232
233TEST(DeclPrinter, TestNamespaceAlias2) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000234 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000235 "namespace X { namespace Y {} }"
236 "namespace A = X::Y;",
237 "A",
238 "namespace A = X::Y"));
239 // Should be: with semicolon
240}
241
242TEST(DeclPrinter, TestCXXRecordDecl1) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000243 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000244 "class A { int a; };",
245 "A",
246 "class A {\n}"));
247 // Should be: with semicolon, with { ... }
248}
249
250TEST(DeclPrinter, TestCXXRecordDecl2) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000251 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000252 "struct A { int a; };",
253 "A",
254 "struct A {\n}"));
255 // Should be: with semicolon, with { ... }
256}
257
258TEST(DeclPrinter, TestCXXRecordDecl3) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000259 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000260 "union A { int a; };",
261 "A",
262 "union A {\n}"));
263 // Should be: with semicolon, with { ... }
264}
265
266TEST(DeclPrinter, TestCXXRecordDecl4) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000267 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000268 "class Z { int a; };"
269 "class A : Z { int b; };",
270 "A",
271 "class A : Z {\n}"));
272 // Should be: with semicolon, with { ... }, without two spaces
273}
274
275TEST(DeclPrinter, TestCXXRecordDecl5) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000276 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000277 "struct Z { int a; };"
278 "struct A : Z { int b; };",
279 "A",
280 "struct A : Z {\n}"));
281 // Should be: with semicolon, with { ... }, without two spaces
282}
283
284TEST(DeclPrinter, TestCXXRecordDecl6) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000285 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000286 "class Z { int a; };"
287 "class A : public Z { int b; };",
288 "A",
289 "class A : public Z {\n}"));
290 // Should be: with semicolon, with { ... }
291}
292
293TEST(DeclPrinter, TestCXXRecordDecl7) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000294 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000295 "class Z { int a; };"
296 "class A : protected Z { int b; };",
297 "A",
298 "class A : protected Z {\n}"));
299 // Should be: with semicolon, with { ... }
300}
301
302TEST(DeclPrinter, TestCXXRecordDecl8) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000303 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000304 "class Z { int a; };"
305 "class A : private Z { int b; };",
306 "A",
307 "class A : private Z {\n}"));
308 // Should be: with semicolon, with { ... }
309}
310
311TEST(DeclPrinter, TestCXXRecordDecl9) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000312 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000313 "class Z { int a; };"
314 "class A : virtual Z { int b; };",
315 "A",
316 "class A : virtual Z {\n}"));
317 // Should be: with semicolon, with { ... }, without two spaces
318}
319
320TEST(DeclPrinter, TestCXXRecordDecl10) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000321 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000322 "class Z { int a; };"
323 "class A : virtual public Z { int b; };",
324 "A",
325 "class A : virtual public Z {\n}"));
326 // Should be: with semicolon, with { ... }
327}
328
329TEST(DeclPrinter, TestCXXRecordDecl11) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000330 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000331 "class Z { int a; };"
332 "class Y : virtual public Z { int b; };"
333 "class A : virtual public Z, private Y { int c; };",
334 "A",
335 "class A : virtual public Z, private Y {\n}"));
336 // Should be: with semicolon, with { ... }
337}
338
339TEST(DeclPrinter, TestFunctionDecl1) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000340 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000341 "void A();",
342 "A",
343 "void A()"));
344 // Should be: with semicolon
345}
346
347TEST(DeclPrinter, TestFunctionDecl2) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000348 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko2e0b8d92012-08-21 17:47:24 +0000349 "void A() {}",
350 "A",
351 "void A()"));
352 // Should be: with semicolon
353}
354
355TEST(DeclPrinter, TestFunctionDecl3) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000356 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko2e0b8d92012-08-21 17:47:24 +0000357 "void Z();"
358 "void A() { Z(); }",
359 "A",
360 "void A()"));
361 // Should be: with semicolon
362}
363
364TEST(DeclPrinter, TestFunctionDecl4) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000365 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000366 "extern void A();",
367 "A",
368 "extern void A()"));
369 // Should be: with semicolon
370}
371
Dmitri Gribenko2e0b8d92012-08-21 17:47:24 +0000372TEST(DeclPrinter, TestFunctionDecl5) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000373 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000374 "static void A();",
375 "A",
376 "static void A()"));
377 // Should be: with semicolon
378}
379
Dmitri Gribenko2e0b8d92012-08-21 17:47:24 +0000380TEST(DeclPrinter, TestFunctionDecl6) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000381 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000382 "inline void A();",
383 "A",
384 "inline void A()"));
385 // Should be: with semicolon
386}
387
Dmitri Gribenko2e0b8d92012-08-21 17:47:24 +0000388TEST(DeclPrinter, TestFunctionDecl7) {
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000389 ASSERT_TRUE(PrintedDeclCXX11Matches(
390 "constexpr int A(int a);",
391 "A",
Stephen Hines651f13c2014-04-23 16:59:28 -0700392 "constexpr int A(int a)"));
393 // Should be: with semicolon
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000394}
395
Dmitri Gribenko2e0b8d92012-08-21 17:47:24 +0000396TEST(DeclPrinter, TestFunctionDecl8) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000397 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000398 "void A(int a);",
399 "A",
400 "void A(int a)"));
401 // Should be: with semicolon
402}
403
Dmitri Gribenko2e0b8d92012-08-21 17:47:24 +0000404TEST(DeclPrinter, TestFunctionDecl9) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000405 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000406 "void A(...);",
407 "A",
408 "void A(...)"));
409 // Should be: with semicolon
410}
411
Dmitri Gribenko2e0b8d92012-08-21 17:47:24 +0000412TEST(DeclPrinter, TestFunctionDecl10) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000413 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000414 "void A(int a, ...);",
415 "A",
416 "void A(int a, ...)"));
417 // Should be: with semicolon
418}
419
Dmitri Gribenko2e0b8d92012-08-21 17:47:24 +0000420TEST(DeclPrinter, TestFunctionDecl11) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000421 ASSERT_TRUE(PrintedDeclCXX98Matches(
Stephen Hines651f13c2014-04-23 16:59:28 -0700422 "typedef long ssize_t;"
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000423 "typedef int *pInt;"
Stephen Hines651f13c2014-04-23 16:59:28 -0700424 "void A(int a, pInt b, ssize_t c);",
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000425 "A",
Stephen Hines651f13c2014-04-23 16:59:28 -0700426 "void A(int a, pInt b, ssize_t c)"));
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000427 // Should be: with semicolon
428}
429
Dmitri Gribenko2e0b8d92012-08-21 17:47:24 +0000430TEST(DeclPrinter, TestFunctionDecl12) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000431 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000432 "void A(int a, int b = 0);",
433 "A",
434 "void A(int a, int b = 0)"));
435 // Should be: with semicolon
436}
437
Dmitri Gribenko2e0b8d92012-08-21 17:47:24 +0000438TEST(DeclPrinter, TestFunctionDecl13) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000439 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000440 "void (*A(int a))(int b);",
441 "A",
442 "void (*A(int a))(int)"));
443 // Should be: with semicolon, with parameter name (?)
444}
445
Dmitri Gribenko2e0b8d92012-08-21 17:47:24 +0000446TEST(DeclPrinter, TestFunctionDecl14) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000447 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000448 "template<typename T>"
449 "void A(T t) { }"
450 "template<>"
451 "void A(int N) { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000452 functionDecl(hasName("A"), isExplicitTemplateSpecialization()).bind("id"),
Dmitri Gribenko2e0b8d92012-08-21 17:47:24 +0000453 "void A(int N)"));
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000454 // WRONG; Should be: "template <> void A(int N);"));
455}
456
457
458TEST(DeclPrinter, TestCXXConstructorDecl1) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000459 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000460 "struct A {"
461 " A();"
462 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000463 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian02a2e5a2012-12-05 19:54:11 +0000464 "A()"));
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000465}
466
467TEST(DeclPrinter, TestCXXConstructorDecl2) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000468 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000469 "struct A {"
470 " A(int a);"
471 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000472 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian02a2e5a2012-12-05 19:54:11 +0000473 "A(int a)"));
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000474}
475
476TEST(DeclPrinter, TestCXXConstructorDecl3) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000477 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000478 "struct A {"
479 " A(const A &a);"
480 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000481 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian02a2e5a2012-12-05 19:54:11 +0000482 "A(const A &a)"));
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000483}
484
485TEST(DeclPrinter, TestCXXConstructorDecl4) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000486 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000487 "struct A {"
488 " A(const A &a, int = 0);"
489 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000490 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian02a2e5a2012-12-05 19:54:11 +0000491 "A(const A &a, int = 0)"));
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000492}
493
494TEST(DeclPrinter, TestCXXConstructorDecl5) {
495 ASSERT_TRUE(PrintedDeclCXX11Matches(
496 "struct A {"
497 " A(const A &&a);"
498 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000499 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian02a2e5a2012-12-05 19:54:11 +0000500 "A(const A &&a)"));
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000501}
502
503TEST(DeclPrinter, TestCXXConstructorDecl6) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000504 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000505 "struct A {"
506 " explicit A(int a);"
507 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000508 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian65bcdab2012-12-05 22:19:06 +0000509 "explicit A(int a)"));
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000510}
511
512TEST(DeclPrinter, TestCXXConstructorDecl7) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000513 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000514 "struct A {"
515 " constexpr A();"
516 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000517 constructorDecl(ofClass(hasName("A"))).bind("id"),
Stephen Hines651f13c2014-04-23 16:59:28 -0700518 "constexpr A()"));
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000519}
520
521TEST(DeclPrinter, TestCXXConstructorDecl8) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000522 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000523 "struct A {"
524 " A() = default;"
525 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000526 constructorDecl(ofClass(hasName("A"))).bind("id"),
Richard Smith1d28caf2012-12-11 01:14:52 +0000527 "A() = default"));
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000528}
529
530TEST(DeclPrinter, TestCXXConstructorDecl9) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000531 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000532 "struct A {"
533 " A() = delete;"
534 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000535 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian02a2e5a2012-12-05 19:54:11 +0000536 "A() = delete"));
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000537}
538
Dmitri Gribenkoc4684242012-08-24 00:26:25 +0000539TEST(DeclPrinter, TestCXXConstructorDecl10) {
540 ASSERT_TRUE(PrintedDeclCXX11Matches(
541 "template<typename... T>"
542 "struct A {"
543 " A(const A &a);"
544 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000545 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian02a2e5a2012-12-05 19:54:11 +0000546 "A<T...>(const A<T...> &a)"));
Dmitri Gribenkoc4684242012-08-24 00:26:25 +0000547}
548
549TEST(DeclPrinter, TestCXXConstructorDecl11) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700550 ASSERT_TRUE(PrintedDeclCXX11nonMSCMatches(
Dmitri Gribenkoc4684242012-08-24 00:26:25 +0000551 "template<typename... T>"
552 "struct A : public T... {"
553 " A(T&&... ts) : T(ts)... {}"
554 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000555 constructorDecl(ofClass(hasName("A"))).bind("id"),
Stephen Hines651f13c2014-04-23 16:59:28 -0700556 "A<T...>(T &&ts...) : T(ts)..."));
Dmitri Gribenkof6ec15a2012-08-24 00:27:50 +0000557 // WRONG; Should be: "A(T&&... ts) : T(ts)..."
Dmitri Gribenkoc4684242012-08-24 00:26:25 +0000558}
Dmitri Gribenkoc4684242012-08-24 00:26:25 +0000559
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000560TEST(DeclPrinter, TestCXXDestructorDecl1) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000561 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000562 "struct A {"
563 " ~A();"
564 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000565 destructorDecl(ofClass(hasName("A"))).bind("id"),
Stephen Hines651f13c2014-04-23 16:59:28 -0700566 "~A()"));
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000567}
568
569TEST(DeclPrinter, TestCXXDestructorDecl2) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000570 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000571 "struct A {"
572 " virtual ~A();"
573 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000574 destructorDecl(ofClass(hasName("A"))).bind("id"),
Stephen Hines651f13c2014-04-23 16:59:28 -0700575 "virtual ~A()"));
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000576}
577
578TEST(DeclPrinter, TestCXXConversionDecl1) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000579 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000580 "struct A {"
581 " operator int();"
582 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000583 methodDecl(ofClass(hasName("A"))).bind("id"),
Stephen Hines651f13c2014-04-23 16:59:28 -0700584 "operator int()"));
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000585}
586
587TEST(DeclPrinter, TestCXXConversionDecl2) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000588 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000589 "struct A {"
590 " operator bool();"
591 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000592 methodDecl(ofClass(hasName("A"))).bind("id"),
Stephen Hines651f13c2014-04-23 16:59:28 -0700593 "operator bool()"));
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000594}
595
596TEST(DeclPrinter, TestCXXConversionDecl3) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000597 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000598 "struct Z {};"
599 "struct A {"
600 " operator Z();"
601 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000602 methodDecl(ofClass(hasName("A"))).bind("id"),
Stephen Hines651f13c2014-04-23 16:59:28 -0700603 "operator Z()"));
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000604}
605
606TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction1) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000607 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000608 "namespace std { typedef decltype(sizeof(int)) size_t; }"
609 "struct Z {"
610 " void *operator new(std::size_t);"
611 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000612 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000613 "void *operator new(std::size_t)"));
614 // Should be: with semicolon
615}
616
617TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction2) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000618 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000619 "namespace std { typedef decltype(sizeof(int)) size_t; }"
620 "struct Z {"
621 " void *operator new[](std::size_t);"
622 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000623 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000624 "void *operator new[](std::size_t)"));
625 // Should be: with semicolon
626}
627
628TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction3) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000629 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000630 "struct Z {"
631 " void operator delete(void *);"
632 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000633 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000634 "void operator delete(void *) noexcept"));
635 // Should be: with semicolon, without noexcept?
636}
637
638TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction4) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000639 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000640 "struct Z {"
641 " void operator delete(void *);"
642 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000643 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000644 "void operator delete(void *)"));
645 // Should be: with semicolon
646}
647
648TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction5) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000649 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000650 "struct Z {"
651 " void operator delete[](void *);"
652 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000653 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000654 "void operator delete[](void *) noexcept"));
655 // Should be: with semicolon, without noexcept?
656}
657
658TEST(DeclPrinter, TestCXXMethodDecl_Operator1) {
659 const char *OperatorNames[] = {
660 "+", "-", "*", "/", "%", "^", "&", "|",
661 "=", "<", ">", "+=", "-=", "*=", "/=", "%=",
662 "^=", "&=", "|=", "<<", ">>", ">>=", "<<=", "==", "!=",
663 "<=", ">=", "&&", "||", ",", "->*",
664 "()", "[]"
665 };
666
667 for (unsigned i = 0, e = llvm::array_lengthof(OperatorNames); i != e; ++i) {
668 SmallString<128> Code;
669 Code.append("struct Z { void operator");
670 Code.append(OperatorNames[i]);
671 Code.append("(Z z); };");
672
673 SmallString<128> Expected;
674 Expected.append("void operator");
675 Expected.append(OperatorNames[i]);
676 Expected.append("(Z z)");
677 // Should be: with semicolon
678
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000679 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000680 Code,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000681 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000682 Expected));
683 }
684}
685
686TEST(DeclPrinter, TestCXXMethodDecl_Operator2) {
687 const char *OperatorNames[] = {
688 "~", "!", "++", "--", "->"
689 };
690
691 for (unsigned i = 0, e = llvm::array_lengthof(OperatorNames); i != e; ++i) {
692 SmallString<128> Code;
693 Code.append("struct Z { void operator");
694 Code.append(OperatorNames[i]);
695 Code.append("(); };");
696
697 SmallString<128> Expected;
698 Expected.append("void operator");
699 Expected.append(OperatorNames[i]);
700 Expected.append("()");
701 // Should be: with semicolon
702
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000703 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000704 Code,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000705 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000706 Expected));
707 }
708}
709
710TEST(DeclPrinter, TestCXXMethodDecl1) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000711 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000712 "struct Z {"
713 " void A(int a);"
714 "};",
715 "A",
716 "void A(int a)"));
717 // Should be: with semicolon
718}
719
720TEST(DeclPrinter, TestCXXMethodDecl2) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000721 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000722 "struct Z {"
723 " virtual void A(int a);"
724 "};",
725 "A",
726 "virtual void A(int a)"));
727 // Should be: with semicolon
728}
729
730TEST(DeclPrinter, TestCXXMethodDecl3) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000731 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000732 "struct Z {"
733 " virtual void A(int a);"
734 "};"
735 "struct ZZ : Z {"
736 " void A(int a);"
737 "};",
738 "ZZ::A",
739 "void A(int a)"));
740 // Should be: with semicolon
741 // TODO: should we print "virtual"?
742}
743
744TEST(DeclPrinter, TestCXXMethodDecl4) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000745 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000746 "struct Z {"
747 " inline void A(int a);"
748 "};",
749 "A",
750 "inline void A(int a)"));
751 // Should be: with semicolon
752}
753
754TEST(DeclPrinter, TestCXXMethodDecl5) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000755 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000756 "struct Z {"
757 " virtual void A(int a) = 0;"
758 "};",
759 "A",
760 "virtual void A(int a) = 0"));
761 // Should be: with semicolon
762}
763
764TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier1) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000765 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000766 "struct Z {"
767 " void A(int a) const;"
768 "};",
769 "A",
770 "void A(int a) const"));
771 // Should be: with semicolon
772}
773
774TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier2) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000775 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000776 "struct Z {"
777 " void A(int a) volatile;"
778 "};",
779 "A",
780 "void A(int a) volatile"));
781 // Should be: with semicolon
782}
783
784TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier3) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000785 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000786 "struct Z {"
787 " void A(int a) const volatile;"
788 "};",
789 "A",
790 "void A(int a) const volatile"));
791 // Should be: with semicolon
792}
793
794TEST(DeclPrinter, TestCXXMethodDecl_RefQualifier1) {
795 ASSERT_TRUE(PrintedDeclCXX11Matches(
796 "struct Z {"
797 " void A(int a) &;"
798 "};",
799 "A",
Stephen Hines651f13c2014-04-23 16:59:28 -0700800 "void A(int a) &"));
801 // Should be: with semicolon
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000802}
803
804TEST(DeclPrinter, TestCXXMethodDecl_RefQualifier2) {
805 ASSERT_TRUE(PrintedDeclCXX11Matches(
806 "struct Z {"
807 " void A(int a) &&;"
808 "};",
809 "A",
Stephen Hines651f13c2014-04-23 16:59:28 -0700810 "void A(int a) &&"));
811 // Should be: with semicolon
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000812}
813
814TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification1) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000815 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000816 "struct Z {"
817 " void A(int a) throw();"
818 "};",
819 "A",
820 "void A(int a) throw()"));
821 // Should be: with semicolon
822}
823
824TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification2) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000825 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000826 "struct Z {"
827 " void A(int a) throw(int);"
828 "};",
829 "A",
830 "void A(int a) throw(int)"));
831 // Should be: with semicolon
832}
833
834TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification3) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000835 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000836 "class ZZ {};"
837 "struct Z {"
838 " void A(int a) throw(ZZ, int);"
839 "};",
840 "A",
841 "void A(int a) throw(ZZ, int)"));
842 // Should be: with semicolon
843}
844
845TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification4) {
846 ASSERT_TRUE(PrintedDeclCXX11Matches(
847 "struct Z {"
848 " void A(int a) noexcept;"
849 "};",
850 "A",
851 "void A(int a) noexcept"));
852 // Should be: with semicolon
853}
854
855TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification5) {
856 ASSERT_TRUE(PrintedDeclCXX11Matches(
857 "struct Z {"
858 " void A(int a) noexcept(true);"
859 "};",
860 "A",
861 "void A(int a) noexcept(trueA(int a) noexcept(true)"));
862 // WRONG; Should be: "void A(int a) noexcept(true);"
863}
864
865TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification6) {
866 ASSERT_TRUE(PrintedDeclCXX11Matches(
867 "struct Z {"
868 " void A(int a) noexcept(1 < 2);"
869 "};",
870 "A",
871 "void A(int a) noexcept(1 < 2A(int a) noexcept(1 < 2)"));
872 // WRONG; Should be: "void A(int a) noexcept(1 < 2);"
873}
874
875TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification7) {
876 ASSERT_TRUE(PrintedDeclCXX11Matches(
877 "template<int N>"
878 "struct Z {"
879 " void A(int a) noexcept(N < 2);"
880 "};",
881 "A",
882 "void A(int a) noexcept(N < 2A(int a) noexcept(N < 2)"));
883 // WRONG; Should be: "void A(int a) noexcept(N < 2);"
884}
885
886TEST(DeclPrinter, TestVarDecl1) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000887 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000888 "char *const (*(*A)[5])(int);",
889 "A",
890 "char *const (*(*A)[5])(int)"));
891 // Should be: with semicolon
892}
893
894TEST(DeclPrinter, TestVarDecl2) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000895 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000896 "void (*A)() throw(int);",
897 "A",
898 "void (*A)() throw(int)"));
899 // Should be: with semicolon
900}
901
902TEST(DeclPrinter, TestVarDecl3) {
903 ASSERT_TRUE(PrintedDeclCXX11Matches(
904 "void (*A)() noexcept;",
905 "A",
906 "void (*A)() noexcept"));
907 // Should be: with semicolon
908}
909
910TEST(DeclPrinter, TestFieldDecl1) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000911 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000912 "template<typename T>"
913 "struct Z { T A; };",
914 "A",
915 "T A"));
916 // Should be: with semicolon
917}
918
919TEST(DeclPrinter, TestFieldDecl2) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000920 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000921 "template<int N>"
922 "struct Z { int A[N]; };",
923 "A",
924 "int A[N]"));
925 // Should be: with semicolon
926}
927
928TEST(DeclPrinter, TestClassTemplateDecl1) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000929 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000930 "template<typename T>"
931 "struct A { T a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000932 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000933 "template <typename T> struct A {\n}"));
934 // Should be: with semicolon, with { ... }
935}
936
937TEST(DeclPrinter, TestClassTemplateDecl2) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000938 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000939 "template<typename T = int>"
940 "struct A { T a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000941 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000942 "template <typename T = int> struct A {\n}"));
943 // Should be: with semicolon, with { ... }
944}
945
946TEST(DeclPrinter, TestClassTemplateDecl3) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000947 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000948 "template<class T>"
949 "struct A { T a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000950 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000951 "template <class T> struct A {\n}"));
952 // Should be: with semicolon, with { ... }
953}
954
955TEST(DeclPrinter, TestClassTemplateDecl4) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000956 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000957 "template<typename T, typename U>"
958 "struct A { T a; U b; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000959 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000960 "template <typename T, typename U> struct A {\n}"));
961 // Should be: with semicolon, with { ... }
962}
963
964TEST(DeclPrinter, TestClassTemplateDecl5) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000965 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000966 "template<int N>"
967 "struct A { int a[N]; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000968 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000969 "template <int N> struct A {\n}"));
970 // Should be: with semicolon, with { ... }
971}
972
973TEST(DeclPrinter, TestClassTemplateDecl6) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000974 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000975 "template<int N = 42>"
976 "struct A { int a[N]; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000977 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000978 "template <int N = 42> struct A {\n}"));
979 // Should be: with semicolon, with { ... }
980}
981
982TEST(DeclPrinter, TestClassTemplateDecl7) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000983 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000984 "typedef int MyInt;"
985 "template<MyInt N>"
986 "struct A { int a[N]; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000987 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000988 "template <MyInt N> struct A {\n}"));
989 // Should be: with semicolon, with { ... }
990}
991
992TEST(DeclPrinter, TestClassTemplateDecl8) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +0000993 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000994 "template<template<typename U> class T> struct A { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000995 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000996 "template <template <typename U> class T> struct A {\n}"));
997 // Should be: with semicolon, with { ... }
998}
999
1000TEST(DeclPrinter, TestClassTemplateDecl9) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001001 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001002 "template<typename T> struct Z { };"
1003 "template<template<typename U> class T = Z> struct A { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001004 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001005 "template <template <typename U> class T> struct A {\n}"));
1006 // Should be: with semicolon, with { ... }
1007}
1008
1009TEST(DeclPrinter, TestClassTemplateDecl10) {
1010 ASSERT_TRUE(PrintedDeclCXX11Matches(
1011 "template<typename... T>"
1012 "struct A { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001013 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001014 "template <typename ... T> struct A {\n}"));
1015 // Should be: with semicolon, with { ... }, without spaces before '...'
1016}
1017
1018TEST(DeclPrinter, TestClassTemplateDecl11) {
1019 ASSERT_TRUE(PrintedDeclCXX11Matches(
1020 "template<typename... T>"
1021 "struct A : public T... { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001022 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001023 "template <typename ... T> struct A : public T... {\n}"));
1024 // Should be: with semicolon, with { ... }, without spaces before '...'
1025}
1026
1027TEST(DeclPrinter, TestClassTemplatePartialSpecializationDecl1) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001028 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001029 "template<typename T, typename U>"
1030 "struct A { T a; U b; };"
1031 "template<typename T>"
1032 "struct A<T, int> { T a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001033 classTemplateSpecializationDecl().bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001034 "struct A {\n}"));
1035 // WRONG; Should be: "template<typename T> struct A<T, int> { ... }"
1036}
1037
1038TEST(DeclPrinter, TestClassTemplatePartialSpecializationDecl2) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001039 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001040 "template<typename T>"
1041 "struct A { T a; };"
1042 "template<typename T>"
1043 "struct A<T *> { T a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001044 classTemplateSpecializationDecl().bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001045 "struct A {\n}"));
1046 // WRONG; Should be: "template<typename T> struct A<T *> { ... }"
1047}
1048
1049TEST(DeclPrinter, TestClassTemplateSpecializationDecl1) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001050 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001051 "template<typename T>"
1052 "struct A { T a; };"
1053 "template<>"
1054 "struct A<int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001055 classTemplateSpecializationDecl().bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001056 "struct A {\n}"));
1057 // WRONG; Should be: "template<> struct A<int> { ... }"
1058}
1059
1060TEST(DeclPrinter, TestFunctionTemplateDecl1) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001061 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001062 "template<typename T>"
1063 "void A(T &t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001064 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001065 "template <typename T> void A(T &t)"));
1066 // Should be: with semicolon
1067}
1068
1069TEST(DeclPrinter, TestFunctionTemplateDecl2) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001070 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001071 "template<typename T>"
1072 "void A(T &t) { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001073 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko2e0b8d92012-08-21 17:47:24 +00001074 "template <typename T> void A(T &t)"));
1075 // Should be: with semicolon
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001076}
1077
1078TEST(DeclPrinter, TestFunctionTemplateDecl3) {
1079 ASSERT_TRUE(PrintedDeclCXX11Matches(
1080 "template<typename... T>"
1081 "void A(T... a);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001082 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001083 "template <typename ... T> void A(T a...)"));
1084 // WRONG; Should be: "template <typename ... T> void A(T... a)"
1085 // (not "T a...")
1086 // Should be: with semicolon.
1087}
1088
1089TEST(DeclPrinter, TestFunctionTemplateDecl4) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001090 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001091 "struct Z { template<typename T> void A(T t); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001092 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001093 "template <typename T> void A(T t)"));
1094 // Should be: with semicolon
1095}
1096
1097TEST(DeclPrinter, TestFunctionTemplateDecl5) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001098 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001099 "struct Z { template<typename T> void A(T t) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001100 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko2e0b8d92012-08-21 17:47:24 +00001101 "template <typename T> void A(T t)"));
1102 // Should be: with semicolon
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001103}
1104
1105TEST(DeclPrinter, TestFunctionTemplateDecl6) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001106 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001107 "template<typename T >struct Z {"
1108 " template<typename U> void A(U t) {}"
1109 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001110 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko2e0b8d92012-08-21 17:47:24 +00001111 "template <typename U> void A(U t)"));
1112 // Should be: with semicolon
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001113}
1114
1115TEST(DeclPrinter, TestTemplateArgumentList1) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001116 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001117 "template<typename T> struct Z {};"
1118 "struct X {};"
1119 "Z<X> A;",
1120 "A",
1121 "Z<X> A"));
1122 // Should be: with semicolon
1123}
1124
1125TEST(DeclPrinter, TestTemplateArgumentList2) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001126 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001127 "template<typename T, typename U> struct Z {};"
1128 "struct X {};"
1129 "typedef int Y;"
1130 "Z<X, Y> A;",
1131 "A",
1132 "Z<X, Y> A"));
1133 // Should be: with semicolon
1134}
1135
1136TEST(DeclPrinter, TestTemplateArgumentList3) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001137 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001138 "template<typename T> struct Z {};"
1139 "template<typename T> struct X {};"
1140 "Z<X<int> > A;",
1141 "A",
1142 "Z<X<int> > A"));
1143 // Should be: with semicolon
1144}
1145
1146TEST(DeclPrinter, TestTemplateArgumentList4) {
1147 ASSERT_TRUE(PrintedDeclCXX11Matches(
1148 "template<typename T> struct Z {};"
1149 "template<typename T> struct X {};"
1150 "Z<X<int>> A;",
1151 "A",
1152 "Z<X<int> > A"));
1153 // Should be: with semicolon, without extra space in "> >"
1154}
1155
1156TEST(DeclPrinter, TestTemplateArgumentList5) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001157 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001158 "template<typename T> struct Z {};"
1159 "template<typename T> struct X { Z<T> A; };",
1160 "A",
1161 "Z<T> A"));
1162 // Should be: with semicolon
1163}
1164
1165TEST(DeclPrinter, TestTemplateArgumentList6) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001166 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001167 "template<template<typename T> class U> struct Z {};"
1168 "template<typename T> struct X {};"
1169 "Z<X> A;",
1170 "A",
1171 "Z<X> A"));
1172 // Should be: with semicolon
1173}
1174
1175TEST(DeclPrinter, TestTemplateArgumentList7) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001176 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001177 "template<template<typename T> class U> struct Z {};"
1178 "template<template<typename T> class U> struct Y {"
1179 " Z<U> A;"
1180 "};",
1181 "A",
1182 "Z<U> A"));
1183 // Should be: with semicolon
1184}
1185
1186TEST(DeclPrinter, TestTemplateArgumentList8) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001187 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001188 "template<typename T> struct Z {};"
1189 "template<template<typename T> class U> struct Y {"
1190 " Z<U<int> > A;"
1191 "};",
1192 "A",
1193 "Z<U<int> > A"));
1194 // Should be: with semicolon
1195}
1196
1197TEST(DeclPrinter, TestTemplateArgumentList9) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001198 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001199 "template<unsigned I> struct Z {};"
1200 "Z<0> A;",
1201 "A",
1202 "Z<0> A"));
1203 // Should be: with semicolon
1204}
1205
1206TEST(DeclPrinter, TestTemplateArgumentList10) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001207 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001208 "template<unsigned I> struct Z {};"
1209 "template<unsigned I> struct X { Z<I> A; };",
1210 "A",
1211 "Z<I> A"));
1212 // Should be: with semicolon
1213}
1214
1215TEST(DeclPrinter, TestTemplateArgumentList11) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001216 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001217 "template<int I> struct Z {};"
1218 "Z<42 * 10 - 420 / 1> A;",
1219 "A",
1220 "Z<42 * 10 - 420 / 1> A"));
1221 // Should be: with semicolon
1222}
1223
1224TEST(DeclPrinter, TestTemplateArgumentList12) {
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001225 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko49795ae2012-08-20 23:39:06 +00001226 "template<const char *p> struct Z {};"
1227 "extern const char X[] = \"aaa\";"
1228 "Z<X> A;",
1229 "A",
1230 "Z<X> A"));
1231 // Should be: with semicolon
1232}
1233
1234TEST(DeclPrinter, TestTemplateArgumentList13) {
1235 ASSERT_TRUE(PrintedDeclCXX11Matches(
1236 "template<typename... T> struct Z {};"
1237 "template<typename... T> struct X {"
1238 " Z<T...> A;"
1239 "};",
1240 "A",
1241 "Z<T...> A"));
1242 // Should be: with semicolon, without extra space in "> >"
1243}
1244
1245TEST(DeclPrinter, TestTemplateArgumentList14) {
1246 ASSERT_TRUE(PrintedDeclCXX11Matches(
1247 "template<typename... T> struct Z {};"
1248 "template<typename T> struct Y {};"
1249 "template<typename... T> struct X {"
1250 " Z<Y<T>...> A;"
1251 "};",
1252 "A",
1253 "Z<Y<T>...> A"));
1254 // Should be: with semicolon, without extra space in "> >"
1255}
1256
1257TEST(DeclPrinter, TestTemplateArgumentList15) {
1258 ASSERT_TRUE(PrintedDeclCXX11Matches(
1259 "template<unsigned I> struct Z {};"
1260 "template<typename... T> struct X {"
1261 " Z<sizeof...(T)> A;"
1262 "};",
1263 "A",
1264 "Z<sizeof...(T)> A"));
1265 // Should be: with semicolon, without extra space in "> >"
1266}
Dmitri Gribenko44470ef2012-08-31 03:05:44 +00001267
Fariborz Jahaniana7a68b62012-10-18 19:12:17 +00001268TEST(DeclPrinter, TestObjCMethod1) {
1269 ASSERT_TRUE(PrintedDeclObjCMatches(
1270 "__attribute__((objc_root_class)) @interface X\n"
1271 "- (int)A:(id)anObject inRange:(long)range;\n"
1272 "@end\n"
1273 "@implementation X\n"
1274 "- (int)A:(id)anObject inRange:(long)range { int printThis; return 0; }\n"
1275 "@end\n",
1276 namedDecl(hasName("A:inRange:"),
1277 hasDescendant(namedDecl(hasName("printThis")))).bind("id"),
Fariborz Jahanian40902d82012-12-19 23:36:00 +00001278 "- (int) A:(id)anObject inRange:(long)range"));
Fariborz Jahaniana7a68b62012-10-18 19:12:17 +00001279}
1280
Fariborz Jahanian88d158c2012-12-20 02:20:09 +00001281TEST(DeclPrinter, TestObjCProtocol1) {
1282 ASSERT_TRUE(PrintedDeclObjCMatches(
1283 "@protocol P1, P2;",
1284 namedDecl(hasName("P1")).bind("id"),
1285 "@protocol P1;\n"));
1286 ASSERT_TRUE(PrintedDeclObjCMatches(
1287 "@protocol P1, P2;",
1288 namedDecl(hasName("P2")).bind("id"),
1289 "@protocol P2;\n"));
1290}
1291
1292TEST(DeclPrinter, TestObjCProtocol2) {
1293 ASSERT_TRUE(PrintedDeclObjCMatches(
1294 "@protocol P2 @end"
1295 "@protocol P1<P2> @end",
1296 namedDecl(hasName("P1")).bind("id"),
1297 "@protocol P1<P2>\n@end"));
1298}