blob: 5420d968ce27d3cfbd8e3420f247de8df04456e6 [file] [log] [blame]
Dmitri Gribenko309856a2012-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 Jasper6ed1f852012-08-24 05:50:27 +000025#include "llvm/ADT/SmallString.h"
Dmitri Gribenko309856a2012-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 Gribenkoa93a7e82012-08-21 17:36:32 +000036 Policy.TerseOutput = true;
Dmitri Gribenko309856a2012-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 Gribenko309856a2012-08-20 23:39:06 +000068::testing::AssertionResult PrintedDeclMatches(
69 StringRef Code,
Dmitri Gribenko454a43c2012-08-31 03:23:26 +000070 const std::vector<std::string> &Args,
Dmitri Gribenko309856a2012-08-20 23:39:06 +000071 const DeclarationMatcher &NodeMatch,
Fariborz Jahaniane0586a52012-10-18 19:12:17 +000072 StringRef ExpectedPrinted,
73 StringRef FileName) {
Dmitri Gribenko309856a2012-08-20 23:39:06 +000074 PrintMatch Printer;
75 MatchFinder Finder;
76 Finder.addMatcher(NodeMatch, &Printer);
77 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
78
Fariborz Jahaniane0586a52012-10-18 19:12:17 +000079 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args, FileName))
Saleem Abdulrasool8a8454b2014-01-25 20:04:44 +000080 return testing::AssertionFailure()
81 << "Parsing error in \"" << Code.str() << "\"";
Dmitri Gribenko309856a2012-08-20 23:39:06 +000082
83 if (Printer.getNumFoundDecls() == 0)
84 return testing::AssertionFailure()
85 << "Matcher didn't find any declarations";
86
87 if (Printer.getNumFoundDecls() > 1)
88 return testing::AssertionFailure()
89 << "Matcher should match only one declaration "
90 "(found " << Printer.getNumFoundDecls() << ")";
91
92 if (Printer.getPrinted() != ExpectedPrinted)
93 return ::testing::AssertionFailure()
Saleem Abdulrasool8a8454b2014-01-25 20:04:44 +000094 << "Expected \"" << ExpectedPrinted.str() << "\", "
95 "got \"" << Printer.getPrinted().str() << "\"";
Dmitri Gribenko309856a2012-08-20 23:39:06 +000096
97 return ::testing::AssertionSuccess();
98}
99
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000100::testing::AssertionResult PrintedDeclCXX98Matches(StringRef Code,
101 StringRef DeclName,
102 StringRef ExpectedPrinted) {
Dmitri Gribenko454a43c2012-08-31 03:23:26 +0000103 std::vector<std::string> Args(1, "-std=c++98");
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000104 return PrintedDeclMatches(Code,
Dmitri Gribenko454a43c2012-08-31 03:23:26 +0000105 Args,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000106 namedDecl(hasName(DeclName)).bind("id"),
Fariborz Jahaniane0586a52012-10-18 19:12:17 +0000107 ExpectedPrinted,
108 "input.cc");
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000109}
110
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000111::testing::AssertionResult PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000112 StringRef Code,
113 const DeclarationMatcher &NodeMatch,
114 StringRef ExpectedPrinted) {
Dmitri Gribenko454a43c2012-08-31 03:23:26 +0000115 std::vector<std::string> Args(1, "-std=c++98");
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000116 return PrintedDeclMatches(Code,
Dmitri Gribenko454a43c2012-08-31 03:23:26 +0000117 Args,
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000118 NodeMatch,
Fariborz Jahaniane0586a52012-10-18 19:12:17 +0000119 ExpectedPrinted,
120 "input.cc");
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000121}
122
123::testing::AssertionResult PrintedDeclCXX11Matches(StringRef Code,
124 StringRef DeclName,
125 StringRef ExpectedPrinted) {
Dmitri Gribenko454a43c2012-08-31 03:23:26 +0000126 std::vector<std::string> Args(1, "-std=c++11");
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000127 return PrintedDeclMatches(Code,
Dmitri Gribenko454a43c2012-08-31 03:23:26 +0000128 Args,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000129 namedDecl(hasName(DeclName)).bind("id"),
Fariborz Jahaniane0586a52012-10-18 19:12:17 +0000130 ExpectedPrinted,
131 "input.cc");
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000132}
133
134::testing::AssertionResult PrintedDeclCXX11Matches(
135 StringRef Code,
136 const DeclarationMatcher &NodeMatch,
137 StringRef ExpectedPrinted) {
Dmitri Gribenko454a43c2012-08-31 03:23:26 +0000138 std::vector<std::string> Args(1, "-std=c++11");
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000139 return PrintedDeclMatches(Code,
Dmitri Gribenko454a43c2012-08-31 03:23:26 +0000140 Args,
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000141 NodeMatch,
Fariborz Jahaniane0586a52012-10-18 19:12:17 +0000142 ExpectedPrinted,
143 "input.cc");
144}
145
NAKAMURA Takumi7d2da0b2014-02-16 10:16:09 +0000146::testing::AssertionResult PrintedDeclCXX11nonMSCMatches(
147 StringRef Code,
148 const DeclarationMatcher &NodeMatch,
149 StringRef ExpectedPrinted) {
150 std::vector<std::string> Args(1, "-std=c++11");
151 Args.push_back("-fno-delayed-template-parsing");
152 return PrintedDeclMatches(Code,
153 Args,
154 NodeMatch,
155 ExpectedPrinted,
156 "input.cc");
157}
158
Fariborz Jahaniane0586a52012-10-18 19:12:17 +0000159::testing::AssertionResult PrintedDeclObjCMatches(
160 StringRef Code,
161 const DeclarationMatcher &NodeMatch,
162 StringRef ExpectedPrinted) {
163 std::vector<std::string> Args(1, "");
164 return PrintedDeclMatches(Code,
165 Args,
166 NodeMatch,
167 ExpectedPrinted,
168 "input.m");
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000169}
170
171} // unnamed namespace
172
Dmitri Gribenko5ea34fc2014-03-03 13:21:00 +0000173TEST(DeclPrinter, TestTypedef1) {
174 ASSERT_TRUE(PrintedDeclCXX98Matches(
175 "typedef int A;",
176 "A",
177 "typedef int A"));
178 // Should be: with semicolon
179}
180
181TEST(DeclPrinter, TestTypedef2) {
182 ASSERT_TRUE(PrintedDeclCXX98Matches(
183 "typedef const char *A;",
184 "A",
185 "typedef const char *A"));
186 // Should be: with semicolon
187}
188
189TEST(DeclPrinter, TestTypedef3) {
190 ASSERT_TRUE(PrintedDeclCXX98Matches(
191 "template <typename Y> class X {};"
192 "typedef X<int> A;",
193 "A",
194 "typedef X<int> A"));
195 // Should be: with semicolon
196}
197
198TEST(DeclPrinter, TestTypedef4) {
199 ASSERT_TRUE(PrintedDeclCXX98Matches(
200 "namespace X { class Y {}; }"
201 "typedef X::Y A;",
202 "A",
203 "typedef X::Y A"));
204 // Should be: with semicolon
205}
206
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000207TEST(DeclPrinter, TestNamespace1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000208 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000209 "namespace A { int B; }",
210 "A",
211 "namespace A {\n}"));
212 // Should be: with { ... }
213}
214
215TEST(DeclPrinter, TestNamespace2) {
216 ASSERT_TRUE(PrintedDeclCXX11Matches(
217 "inline namespace A { int B; }",
218 "A",
219 "inline namespace A {\n}"));
220 // Should be: with { ... }
221}
222
223TEST(DeclPrinter, TestNamespaceAlias1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000224 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000225 "namespace Z { }"
226 "namespace A = Z;",
227 "A",
228 "namespace A = Z"));
229 // Should be: with semicolon
230}
231
232TEST(DeclPrinter, TestNamespaceAlias2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000233 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000234 "namespace X { namespace Y {} }"
235 "namespace A = X::Y;",
236 "A",
237 "namespace A = X::Y"));
238 // Should be: with semicolon
239}
240
241TEST(DeclPrinter, TestCXXRecordDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000242 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000243 "class A { int a; };",
244 "A",
245 "class A {\n}"));
246 // Should be: with semicolon, with { ... }
247}
248
249TEST(DeclPrinter, TestCXXRecordDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000250 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000251 "struct A { int a; };",
252 "A",
253 "struct A {\n}"));
254 // Should be: with semicolon, with { ... }
255}
256
257TEST(DeclPrinter, TestCXXRecordDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000258 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000259 "union A { int a; };",
260 "A",
261 "union A {\n}"));
262 // Should be: with semicolon, with { ... }
263}
264
265TEST(DeclPrinter, TestCXXRecordDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000266 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000267 "class Z { int a; };"
268 "class A : Z { int b; };",
269 "A",
270 "class A : Z {\n}"));
271 // Should be: with semicolon, with { ... }, without two spaces
272}
273
274TEST(DeclPrinter, TestCXXRecordDecl5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000275 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000276 "struct Z { int a; };"
277 "struct A : Z { int b; };",
278 "A",
279 "struct A : Z {\n}"));
280 // Should be: with semicolon, with { ... }, without two spaces
281}
282
283TEST(DeclPrinter, TestCXXRecordDecl6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000284 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000285 "class Z { int a; };"
286 "class A : public Z { int b; };",
287 "A",
288 "class A : public Z {\n}"));
289 // Should be: with semicolon, with { ... }
290}
291
292TEST(DeclPrinter, TestCXXRecordDecl7) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000293 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000294 "class Z { int a; };"
295 "class A : protected Z { int b; };",
296 "A",
297 "class A : protected Z {\n}"));
298 // Should be: with semicolon, with { ... }
299}
300
301TEST(DeclPrinter, TestCXXRecordDecl8) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000302 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000303 "class Z { int a; };"
304 "class A : private Z { int b; };",
305 "A",
306 "class A : private Z {\n}"));
307 // Should be: with semicolon, with { ... }
308}
309
310TEST(DeclPrinter, TestCXXRecordDecl9) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000311 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000312 "class Z { int a; };"
313 "class A : virtual Z { int b; };",
314 "A",
315 "class A : virtual Z {\n}"));
316 // Should be: with semicolon, with { ... }, without two spaces
317}
318
319TEST(DeclPrinter, TestCXXRecordDecl10) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000320 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000321 "class Z { int a; };"
322 "class A : virtual public Z { int b; };",
323 "A",
324 "class A : virtual public Z {\n}"));
325 // Should be: with semicolon, with { ... }
326}
327
328TEST(DeclPrinter, TestCXXRecordDecl11) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000329 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000330 "class Z { int a; };"
331 "class Y : virtual public Z { int b; };"
332 "class A : virtual public Z, private Y { int c; };",
333 "A",
334 "class A : virtual public Z, private Y {\n}"));
335 // Should be: with semicolon, with { ... }
336}
337
338TEST(DeclPrinter, TestFunctionDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000339 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000340 "void A();",
341 "A",
342 "void A()"));
343 // Should be: with semicolon
344}
345
346TEST(DeclPrinter, TestFunctionDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000347 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000348 "void A() {}",
349 "A",
350 "void A()"));
351 // Should be: with semicolon
352}
353
354TEST(DeclPrinter, TestFunctionDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000355 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000356 "void Z();"
357 "void A() { Z(); }",
358 "A",
359 "void A()"));
360 // Should be: with semicolon
361}
362
363TEST(DeclPrinter, TestFunctionDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000364 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000365 "extern void A();",
366 "A",
367 "extern void A()"));
368 // Should be: with semicolon
369}
370
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000371TEST(DeclPrinter, TestFunctionDecl5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000372 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000373 "static void A();",
374 "A",
375 "static void A()"));
376 // Should be: with semicolon
377}
378
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000379TEST(DeclPrinter, TestFunctionDecl6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000380 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000381 "inline void A();",
382 "A",
383 "inline void A()"));
384 // Should be: with semicolon
385}
386
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000387TEST(DeclPrinter, TestFunctionDecl7) {
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000388 ASSERT_TRUE(PrintedDeclCXX11Matches(
389 "constexpr int A(int a);",
390 "A",
Benjamin Kramer2907b082014-02-25 18:49:49 +0000391 "constexpr int A(int a)"));
392 // Should be: with semicolon
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000393}
394
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000395TEST(DeclPrinter, TestFunctionDecl8) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000396 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000397 "void A(int a);",
398 "A",
399 "void A(int a)"));
400 // Should be: with semicolon
401}
402
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000403TEST(DeclPrinter, TestFunctionDecl9) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000404 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000405 "void A(...);",
406 "A",
407 "void A(...)"));
408 // Should be: with semicolon
409}
410
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000411TEST(DeclPrinter, TestFunctionDecl10) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000412 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000413 "void A(int a, ...);",
414 "A",
415 "void A(int a, ...)"));
416 // Should be: with semicolon
417}
418
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000419TEST(DeclPrinter, TestFunctionDecl11) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000420 ASSERT_TRUE(PrintedDeclCXX98Matches(
David Majnemerd4328de2014-01-14 08:18:49 +0000421 "typedef long ssize_t;"
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000422 "typedef int *pInt;"
David Majnemerd4328de2014-01-14 08:18:49 +0000423 "void A(int a, pInt b, ssize_t c);",
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000424 "A",
David Majnemerd4328de2014-01-14 08:18:49 +0000425 "void A(int a, pInt b, ssize_t c)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000426 // Should be: with semicolon
427}
428
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000429TEST(DeclPrinter, TestFunctionDecl12) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000430 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000431 "void A(int a, int b = 0);",
432 "A",
433 "void A(int a, int b = 0)"));
434 // Should be: with semicolon
435}
436
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000437TEST(DeclPrinter, TestFunctionDecl13) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000438 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000439 "void (*A(int a))(int b);",
440 "A",
441 "void (*A(int a))(int)"));
442 // Should be: with semicolon, with parameter name (?)
443}
444
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000445TEST(DeclPrinter, TestFunctionDecl14) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000446 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000447 "template<typename T>"
448 "void A(T t) { }"
449 "template<>"
450 "void A(int N) { }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000451 functionDecl(hasName("A"), isExplicitTemplateSpecialization()).bind("id"),
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000452 "void A(int N)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000453 // WRONG; Should be: "template <> void A(int N);"));
454}
455
456
457TEST(DeclPrinter, TestCXXConstructorDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000458 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000459 "struct A {"
460 " A();"
461 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000462 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000463 "A()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000464}
465
466TEST(DeclPrinter, TestCXXConstructorDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000467 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000468 "struct A {"
469 " A(int a);"
470 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000471 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000472 "A(int a)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000473}
474
475TEST(DeclPrinter, TestCXXConstructorDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000476 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000477 "struct A {"
478 " A(const A &a);"
479 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000480 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000481 "A(const A &a)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000482}
483
484TEST(DeclPrinter, TestCXXConstructorDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000485 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000486 "struct A {"
487 " A(const A &a, int = 0);"
488 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000489 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000490 "A(const A &a, int = 0)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000491}
492
493TEST(DeclPrinter, TestCXXConstructorDecl5) {
494 ASSERT_TRUE(PrintedDeclCXX11Matches(
495 "struct A {"
496 " A(const A &&a);"
497 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000498 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000499 "A(const A &&a)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000500}
501
502TEST(DeclPrinter, TestCXXConstructorDecl6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000503 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000504 "struct A {"
505 " explicit A(int a);"
506 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000507 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000508 "explicit A(int a)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000509}
510
511TEST(DeclPrinter, TestCXXConstructorDecl7) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000512 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000513 "struct A {"
514 " constexpr A();"
515 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000516 constructorDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer2907b082014-02-25 18:49:49 +0000517 "constexpr A()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000518}
519
520TEST(DeclPrinter, TestCXXConstructorDecl8) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000521 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000522 "struct A {"
523 " A() = default;"
524 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000525 constructorDecl(ofClass(hasName("A"))).bind("id"),
Richard Smithbd305122012-12-11 01:14:52 +0000526 "A() = default"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000527}
528
529TEST(DeclPrinter, TestCXXConstructorDecl9) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000530 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000531 "struct A {"
532 " A() = delete;"
533 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000534 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000535 "A() = delete"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000536}
537
Dmitri Gribenko340c0f62012-08-24 00:26:25 +0000538TEST(DeclPrinter, TestCXXConstructorDecl10) {
539 ASSERT_TRUE(PrintedDeclCXX11Matches(
540 "template<typename... T>"
541 "struct A {"
542 " A(const A &a);"
543 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000544 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000545 "A<T...>(const A<T...> &a)"));
Dmitri Gribenko340c0f62012-08-24 00:26:25 +0000546}
547
548TEST(DeclPrinter, TestCXXConstructorDecl11) {
NAKAMURA Takumi7d2da0b2014-02-16 10:16:09 +0000549 ASSERT_TRUE(PrintedDeclCXX11nonMSCMatches(
Dmitri Gribenko340c0f62012-08-24 00:26:25 +0000550 "template<typename... T>"
551 "struct A : public T... {"
552 " A(T&&... ts) : T(ts)... {}"
553 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000554 constructorDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer2907b082014-02-25 18:49:49 +0000555 "A<T...>(T &&ts...) : T(ts)..."));
Benjamin Kramer594802f2014-02-26 10:23:43 +0000556 // WRONG; Should be: "A(T&&... ts) : T(ts)..."
Dmitri Gribenko340c0f62012-08-24 00:26:25 +0000557}
Dmitri Gribenko340c0f62012-08-24 00:26:25 +0000558
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000559TEST(DeclPrinter, TestCXXDestructorDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000560 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000561 "struct A {"
562 " ~A();"
563 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000564 destructorDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer2907b082014-02-25 18:49:49 +0000565 "~A()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000566}
567
568TEST(DeclPrinter, TestCXXDestructorDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000569 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000570 "struct A {"
571 " virtual ~A();"
572 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000573 destructorDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer2907b082014-02-25 18:49:49 +0000574 "virtual ~A()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000575}
576
577TEST(DeclPrinter, TestCXXConversionDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000578 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000579 "struct A {"
580 " operator int();"
581 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000582 methodDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000583 "operator int()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000584}
585
586TEST(DeclPrinter, TestCXXConversionDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000587 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000588 "struct A {"
589 " operator bool();"
590 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000591 methodDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000592 "operator bool()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000593}
594
595TEST(DeclPrinter, TestCXXConversionDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000596 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000597 "struct Z {};"
598 "struct A {"
599 " operator Z();"
600 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000601 methodDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000602 "operator Z()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000603}
604
605TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000606 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000607 "namespace std { typedef decltype(sizeof(int)) size_t; }"
608 "struct Z {"
609 " void *operator new(std::size_t);"
610 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000611 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000612 "void *operator new(std::size_t)"));
613 // Should be: with semicolon
614}
615
616TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000617 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000618 "namespace std { typedef decltype(sizeof(int)) size_t; }"
619 "struct Z {"
620 " void *operator new[](std::size_t);"
621 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000622 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000623 "void *operator new[](std::size_t)"));
624 // Should be: with semicolon
625}
626
627TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000628 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000629 "struct Z {"
630 " void operator delete(void *);"
631 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000632 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000633 "void operator delete(void *) noexcept"));
634 // Should be: with semicolon, without noexcept?
635}
636
637TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000638 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000639 "struct Z {"
640 " void operator delete(void *);"
641 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000642 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000643 "void operator delete(void *)"));
644 // Should be: with semicolon
645}
646
647TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000648 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000649 "struct Z {"
650 " void operator delete[](void *);"
651 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000652 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000653 "void operator delete[](void *) noexcept"));
654 // Should be: with semicolon, without noexcept?
655}
656
657TEST(DeclPrinter, TestCXXMethodDecl_Operator1) {
658 const char *OperatorNames[] = {
659 "+", "-", "*", "/", "%", "^", "&", "|",
660 "=", "<", ">", "+=", "-=", "*=", "/=", "%=",
661 "^=", "&=", "|=", "<<", ">>", ">>=", "<<=", "==", "!=",
662 "<=", ">=", "&&", "||", ",", "->*",
663 "()", "[]"
664 };
665
666 for (unsigned i = 0, e = llvm::array_lengthof(OperatorNames); i != e; ++i) {
667 SmallString<128> Code;
668 Code.append("struct Z { void operator");
669 Code.append(OperatorNames[i]);
670 Code.append("(Z z); };");
671
672 SmallString<128> Expected;
673 Expected.append("void operator");
674 Expected.append(OperatorNames[i]);
675 Expected.append("(Z z)");
676 // Should be: with semicolon
677
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000678 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000679 Code,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000680 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000681 Expected));
682 }
683}
684
685TEST(DeclPrinter, TestCXXMethodDecl_Operator2) {
686 const char *OperatorNames[] = {
687 "~", "!", "++", "--", "->"
688 };
689
690 for (unsigned i = 0, e = llvm::array_lengthof(OperatorNames); i != e; ++i) {
691 SmallString<128> Code;
692 Code.append("struct Z { void operator");
693 Code.append(OperatorNames[i]);
694 Code.append("(); };");
695
696 SmallString<128> Expected;
697 Expected.append("void operator");
698 Expected.append(OperatorNames[i]);
699 Expected.append("()");
700 // Should be: with semicolon
701
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000702 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000703 Code,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000704 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000705 Expected));
706 }
707}
708
709TEST(DeclPrinter, TestCXXMethodDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000710 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000711 "struct Z {"
712 " void A(int a);"
713 "};",
714 "A",
715 "void A(int a)"));
716 // Should be: with semicolon
717}
718
719TEST(DeclPrinter, TestCXXMethodDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000720 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000721 "struct Z {"
722 " virtual void A(int a);"
723 "};",
724 "A",
725 "virtual void A(int a)"));
726 // Should be: with semicolon
727}
728
729TEST(DeclPrinter, TestCXXMethodDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000730 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000731 "struct Z {"
732 " virtual void A(int a);"
733 "};"
734 "struct ZZ : Z {"
735 " void A(int a);"
736 "};",
737 "ZZ::A",
738 "void A(int a)"));
739 // Should be: with semicolon
740 // TODO: should we print "virtual"?
741}
742
743TEST(DeclPrinter, TestCXXMethodDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000744 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000745 "struct Z {"
746 " inline void A(int a);"
747 "};",
748 "A",
749 "inline void A(int a)"));
750 // Should be: with semicolon
751}
752
753TEST(DeclPrinter, TestCXXMethodDecl5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000754 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000755 "struct Z {"
756 " virtual void A(int a) = 0;"
757 "};",
758 "A",
759 "virtual void A(int a) = 0"));
760 // Should be: with semicolon
761}
762
763TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000764 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000765 "struct Z {"
766 " void A(int a) const;"
767 "};",
768 "A",
769 "void A(int a) const"));
770 // Should be: with semicolon
771}
772
773TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000774 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000775 "struct Z {"
776 " void A(int a) volatile;"
777 "};",
778 "A",
779 "void A(int a) volatile"));
780 // Should be: with semicolon
781}
782
783TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000784 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000785 "struct Z {"
786 " void A(int a) const volatile;"
787 "};",
788 "A",
789 "void A(int a) const volatile"));
790 // Should be: with semicolon
791}
792
793TEST(DeclPrinter, TestCXXMethodDecl_RefQualifier1) {
794 ASSERT_TRUE(PrintedDeclCXX11Matches(
795 "struct Z {"
796 " void A(int a) &;"
797 "};",
798 "A",
Benjamin Kramer2907b082014-02-25 18:49:49 +0000799 "void A(int a) &"));
800 // Should be: with semicolon
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000801}
802
803TEST(DeclPrinter, TestCXXMethodDecl_RefQualifier2) {
804 ASSERT_TRUE(PrintedDeclCXX11Matches(
805 "struct Z {"
806 " void A(int a) &&;"
807 "};",
808 "A",
Benjamin Kramer2907b082014-02-25 18:49:49 +0000809 "void A(int a) &&"));
810 // Should be: with semicolon
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000811}
812
813TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000814 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000815 "struct Z {"
816 " void A(int a) throw();"
817 "};",
818 "A",
819 "void A(int a) throw()"));
820 // Should be: with semicolon
821}
822
823TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000824 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000825 "struct Z {"
826 " void A(int a) throw(int);"
827 "};",
828 "A",
829 "void A(int a) throw(int)"));
830 // Should be: with semicolon
831}
832
833TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000834 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000835 "class ZZ {};"
836 "struct Z {"
837 " void A(int a) throw(ZZ, int);"
838 "};",
839 "A",
840 "void A(int a) throw(ZZ, int)"));
841 // Should be: with semicolon
842}
843
844TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification4) {
845 ASSERT_TRUE(PrintedDeclCXX11Matches(
846 "struct Z {"
847 " void A(int a) noexcept;"
848 "};",
849 "A",
850 "void A(int a) noexcept"));
851 // Should be: with semicolon
852}
853
854TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification5) {
855 ASSERT_TRUE(PrintedDeclCXX11Matches(
856 "struct Z {"
857 " void A(int a) noexcept(true);"
858 "};",
859 "A",
860 "void A(int a) noexcept(trueA(int a) noexcept(true)"));
861 // WRONG; Should be: "void A(int a) noexcept(true);"
862}
863
864TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification6) {
865 ASSERT_TRUE(PrintedDeclCXX11Matches(
866 "struct Z {"
867 " void A(int a) noexcept(1 < 2);"
868 "};",
869 "A",
870 "void A(int a) noexcept(1 < 2A(int a) noexcept(1 < 2)"));
871 // WRONG; Should be: "void A(int a) noexcept(1 < 2);"
872}
873
874TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification7) {
875 ASSERT_TRUE(PrintedDeclCXX11Matches(
876 "template<int N>"
877 "struct Z {"
878 " void A(int a) noexcept(N < 2);"
879 "};",
880 "A",
881 "void A(int a) noexcept(N < 2A(int a) noexcept(N < 2)"));
882 // WRONG; Should be: "void A(int a) noexcept(N < 2);"
883}
884
885TEST(DeclPrinter, TestVarDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000886 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000887 "char *const (*(*A)[5])(int);",
888 "A",
889 "char *const (*(*A)[5])(int)"));
890 // Should be: with semicolon
891}
892
893TEST(DeclPrinter, TestVarDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000894 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000895 "void (*A)() throw(int);",
896 "A",
897 "void (*A)() throw(int)"));
898 // Should be: with semicolon
899}
900
901TEST(DeclPrinter, TestVarDecl3) {
902 ASSERT_TRUE(PrintedDeclCXX11Matches(
903 "void (*A)() noexcept;",
904 "A",
905 "void (*A)() noexcept"));
906 // Should be: with semicolon
907}
908
909TEST(DeclPrinter, TestFieldDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000910 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000911 "template<typename T>"
912 "struct Z { T A; };",
913 "A",
914 "T A"));
915 // Should be: with semicolon
916}
917
918TEST(DeclPrinter, TestFieldDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000919 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000920 "template<int N>"
921 "struct Z { int A[N]; };",
922 "A",
923 "int A[N]"));
924 // Should be: with semicolon
925}
926
927TEST(DeclPrinter, TestClassTemplateDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000928 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000929 "template<typename T>"
930 "struct A { T a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000931 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000932 "template <typename T> struct A {\n}"));
933 // Should be: with semicolon, with { ... }
934}
935
936TEST(DeclPrinter, TestClassTemplateDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000937 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000938 "template<typename T = int>"
939 "struct A { T a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000940 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000941 "template <typename T = int> struct A {\n}"));
942 // Should be: with semicolon, with { ... }
943}
944
945TEST(DeclPrinter, TestClassTemplateDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000946 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000947 "template<class T>"
948 "struct A { T a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000949 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000950 "template <class T> struct A {\n}"));
951 // Should be: with semicolon, with { ... }
952}
953
954TEST(DeclPrinter, TestClassTemplateDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000955 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000956 "template<typename T, typename U>"
957 "struct A { T a; U b; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000958 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000959 "template <typename T, typename U> struct A {\n}"));
960 // Should be: with semicolon, with { ... }
961}
962
963TEST(DeclPrinter, TestClassTemplateDecl5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000964 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000965 "template<int N>"
966 "struct A { int a[N]; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000967 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000968 "template <int N> struct A {\n}"));
969 // Should be: with semicolon, with { ... }
970}
971
972TEST(DeclPrinter, TestClassTemplateDecl6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000973 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000974 "template<int N = 42>"
975 "struct A { int a[N]; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000976 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000977 "template <int N = 42> struct A {\n}"));
978 // Should be: with semicolon, with { ... }
979}
980
981TEST(DeclPrinter, TestClassTemplateDecl7) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000982 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000983 "typedef int MyInt;"
984 "template<MyInt N>"
985 "struct A { int a[N]; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000986 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000987 "template <MyInt N> struct A {\n}"));
988 // Should be: with semicolon, with { ... }
989}
990
991TEST(DeclPrinter, TestClassTemplateDecl8) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000992 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000993 "template<template<typename U> class T> struct A { };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000994 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000995 "template <template <typename U> class T> struct A {\n}"));
996 // Should be: with semicolon, with { ... }
997}
998
999TEST(DeclPrinter, TestClassTemplateDecl9) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001000 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001001 "template<typename T> struct Z { };"
1002 "template<template<typename U> class T = Z> struct A { };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001003 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001004 "template <template <typename U> class T> struct A {\n}"));
1005 // Should be: with semicolon, with { ... }
1006}
1007
1008TEST(DeclPrinter, TestClassTemplateDecl10) {
1009 ASSERT_TRUE(PrintedDeclCXX11Matches(
1010 "template<typename... T>"
1011 "struct A { int a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001012 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001013 "template <typename ... T> struct A {\n}"));
1014 // Should be: with semicolon, with { ... }, without spaces before '...'
1015}
1016
1017TEST(DeclPrinter, TestClassTemplateDecl11) {
1018 ASSERT_TRUE(PrintedDeclCXX11Matches(
1019 "template<typename... T>"
1020 "struct A : public T... { int a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001021 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001022 "template <typename ... T> struct A : public T... {\n}"));
1023 // Should be: with semicolon, with { ... }, without spaces before '...'
1024}
1025
1026TEST(DeclPrinter, TestClassTemplatePartialSpecializationDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001027 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001028 "template<typename T, typename U>"
1029 "struct A { T a; U b; };"
1030 "template<typename T>"
1031 "struct A<T, int> { T a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001032 classTemplateSpecializationDecl().bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001033 "struct A {\n}"));
1034 // WRONG; Should be: "template<typename T> struct A<T, int> { ... }"
1035}
1036
1037TEST(DeclPrinter, TestClassTemplatePartialSpecializationDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001038 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001039 "template<typename T>"
1040 "struct A { T a; };"
1041 "template<typename T>"
1042 "struct A<T *> { T a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001043 classTemplateSpecializationDecl().bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001044 "struct A {\n}"));
1045 // WRONG; Should be: "template<typename T> struct A<T *> { ... }"
1046}
1047
1048TEST(DeclPrinter, TestClassTemplateSpecializationDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001049 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001050 "template<typename T>"
1051 "struct A { T a; };"
1052 "template<>"
1053 "struct A<int> { int a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001054 classTemplateSpecializationDecl().bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001055 "struct A {\n}"));
1056 // WRONG; Should be: "template<> struct A<int> { ... }"
1057}
1058
1059TEST(DeclPrinter, TestFunctionTemplateDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001060 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001061 "template<typename T>"
1062 "void A(T &t);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001063 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001064 "template <typename T> void A(T &t)"));
1065 // Should be: with semicolon
1066}
1067
1068TEST(DeclPrinter, TestFunctionTemplateDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001069 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001070 "template<typename T>"
1071 "void A(T &t) { }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001072 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +00001073 "template <typename T> void A(T &t)"));
1074 // Should be: with semicolon
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001075}
1076
1077TEST(DeclPrinter, TestFunctionTemplateDecl3) {
1078 ASSERT_TRUE(PrintedDeclCXX11Matches(
1079 "template<typename... T>"
1080 "void A(T... a);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001081 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001082 "template <typename ... T> void A(T a...)"));
1083 // WRONG; Should be: "template <typename ... T> void A(T... a)"
1084 // (not "T a...")
1085 // Should be: with semicolon.
1086}
1087
1088TEST(DeclPrinter, TestFunctionTemplateDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001089 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001090 "struct Z { template<typename T> void A(T t); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001091 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001092 "template <typename T> void A(T t)"));
1093 // Should be: with semicolon
1094}
1095
1096TEST(DeclPrinter, TestFunctionTemplateDecl5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001097 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001098 "struct Z { template<typename T> void A(T t) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001099 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +00001100 "template <typename T> void A(T t)"));
1101 // Should be: with semicolon
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001102}
1103
1104TEST(DeclPrinter, TestFunctionTemplateDecl6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001105 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001106 "template<typename T >struct Z {"
1107 " template<typename U> void A(U t) {}"
1108 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001109 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +00001110 "template <typename U> void A(U t)"));
1111 // Should be: with semicolon
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001112}
1113
1114TEST(DeclPrinter, TestTemplateArgumentList1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001115 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001116 "template<typename T> struct Z {};"
1117 "struct X {};"
1118 "Z<X> A;",
1119 "A",
1120 "Z<X> A"));
1121 // Should be: with semicolon
1122}
1123
1124TEST(DeclPrinter, TestTemplateArgumentList2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001125 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001126 "template<typename T, typename U> struct Z {};"
1127 "struct X {};"
1128 "typedef int Y;"
1129 "Z<X, Y> A;",
1130 "A",
1131 "Z<X, Y> A"));
1132 // Should be: with semicolon
1133}
1134
1135TEST(DeclPrinter, TestTemplateArgumentList3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001136 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001137 "template<typename T> struct Z {};"
1138 "template<typename T> struct X {};"
1139 "Z<X<int> > A;",
1140 "A",
1141 "Z<X<int> > A"));
1142 // Should be: with semicolon
1143}
1144
1145TEST(DeclPrinter, TestTemplateArgumentList4) {
1146 ASSERT_TRUE(PrintedDeclCXX11Matches(
1147 "template<typename T> struct Z {};"
1148 "template<typename T> struct X {};"
1149 "Z<X<int>> A;",
1150 "A",
1151 "Z<X<int> > A"));
1152 // Should be: with semicolon, without extra space in "> >"
1153}
1154
1155TEST(DeclPrinter, TestTemplateArgumentList5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001156 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001157 "template<typename T> struct Z {};"
1158 "template<typename T> struct X { Z<T> A; };",
1159 "A",
1160 "Z<T> A"));
1161 // Should be: with semicolon
1162}
1163
1164TEST(DeclPrinter, TestTemplateArgumentList6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001165 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001166 "template<template<typename T> class U> struct Z {};"
1167 "template<typename T> struct X {};"
1168 "Z<X> A;",
1169 "A",
1170 "Z<X> A"));
1171 // Should be: with semicolon
1172}
1173
1174TEST(DeclPrinter, TestTemplateArgumentList7) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001175 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001176 "template<template<typename T> class U> struct Z {};"
1177 "template<template<typename T> class U> struct Y {"
1178 " Z<U> A;"
1179 "};",
1180 "A",
1181 "Z<U> A"));
1182 // Should be: with semicolon
1183}
1184
1185TEST(DeclPrinter, TestTemplateArgumentList8) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001186 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001187 "template<typename T> struct Z {};"
1188 "template<template<typename T> class U> struct Y {"
1189 " Z<U<int> > A;"
1190 "};",
1191 "A",
1192 "Z<U<int> > A"));
1193 // Should be: with semicolon
1194}
1195
1196TEST(DeclPrinter, TestTemplateArgumentList9) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001197 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001198 "template<unsigned I> struct Z {};"
1199 "Z<0> A;",
1200 "A",
1201 "Z<0> A"));
1202 // Should be: with semicolon
1203}
1204
1205TEST(DeclPrinter, TestTemplateArgumentList10) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001206 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001207 "template<unsigned I> struct Z {};"
1208 "template<unsigned I> struct X { Z<I> A; };",
1209 "A",
1210 "Z<I> A"));
1211 // Should be: with semicolon
1212}
1213
1214TEST(DeclPrinter, TestTemplateArgumentList11) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001215 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001216 "template<int I> struct Z {};"
1217 "Z<42 * 10 - 420 / 1> A;",
1218 "A",
1219 "Z<42 * 10 - 420 / 1> A"));
1220 // Should be: with semicolon
1221}
1222
1223TEST(DeclPrinter, TestTemplateArgumentList12) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001224 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001225 "template<const char *p> struct Z {};"
1226 "extern const char X[] = \"aaa\";"
1227 "Z<X> A;",
1228 "A",
1229 "Z<X> A"));
1230 // Should be: with semicolon
1231}
1232
1233TEST(DeclPrinter, TestTemplateArgumentList13) {
1234 ASSERT_TRUE(PrintedDeclCXX11Matches(
1235 "template<typename... T> struct Z {};"
1236 "template<typename... T> struct X {"
1237 " Z<T...> A;"
1238 "};",
1239 "A",
1240 "Z<T...> A"));
1241 // Should be: with semicolon, without extra space in "> >"
1242}
1243
1244TEST(DeclPrinter, TestTemplateArgumentList14) {
1245 ASSERT_TRUE(PrintedDeclCXX11Matches(
1246 "template<typename... T> struct Z {};"
1247 "template<typename T> struct Y {};"
1248 "template<typename... T> struct X {"
1249 " Z<Y<T>...> A;"
1250 "};",
1251 "A",
1252 "Z<Y<T>...> A"));
1253 // Should be: with semicolon, without extra space in "> >"
1254}
1255
1256TEST(DeclPrinter, TestTemplateArgumentList15) {
1257 ASSERT_TRUE(PrintedDeclCXX11Matches(
1258 "template<unsigned I> struct Z {};"
1259 "template<typename... T> struct X {"
1260 " Z<sizeof...(T)> A;"
1261 "};",
1262 "A",
1263 "Z<sizeof...(T)> A"));
1264 // Should be: with semicolon, without extra space in "> >"
1265}
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001266
Fariborz Jahaniane0586a52012-10-18 19:12:17 +00001267TEST(DeclPrinter, TestObjCMethod1) {
1268 ASSERT_TRUE(PrintedDeclObjCMatches(
1269 "__attribute__((objc_root_class)) @interface X\n"
1270 "- (int)A:(id)anObject inRange:(long)range;\n"
1271 "@end\n"
1272 "@implementation X\n"
1273 "- (int)A:(id)anObject inRange:(long)range { int printThis; return 0; }\n"
1274 "@end\n",
1275 namedDecl(hasName("A:inRange:"),
1276 hasDescendant(namedDecl(hasName("printThis")))).bind("id"),
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001277 "- (int) A:(id)anObject inRange:(long)range"));
Fariborz Jahaniane0586a52012-10-18 19:12:17 +00001278}
1279
Fariborz Jahanian44194492012-12-20 02:20:09 +00001280TEST(DeclPrinter, TestObjCProtocol1) {
1281 ASSERT_TRUE(PrintedDeclObjCMatches(
1282 "@protocol P1, P2;",
1283 namedDecl(hasName("P1")).bind("id"),
1284 "@protocol P1;\n"));
1285 ASSERT_TRUE(PrintedDeclObjCMatches(
1286 "@protocol P1, P2;",
1287 namedDecl(hasName("P2")).bind("id"),
1288 "@protocol P2;\n"));
1289}
1290
1291TEST(DeclPrinter, TestObjCProtocol2) {
1292 ASSERT_TRUE(PrintedDeclObjCMatches(
1293 "@protocol P2 @end"
1294 "@protocol P1<P2> @end",
1295 namedDecl(hasName("P1")).bind("id"),
1296 "@protocol P1<P2>\n@end"));
1297}