blob: 381536d9cd6b7c32e9c7874ce8d9a77fb8fae4cf [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
173TEST(DeclPrinter, TestNamespace1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000174 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000175 "namespace A { int B; }",
176 "A",
177 "namespace A {\n}"));
178 // Should be: with { ... }
179}
180
181TEST(DeclPrinter, TestNamespace2) {
182 ASSERT_TRUE(PrintedDeclCXX11Matches(
183 "inline namespace A { int B; }",
184 "A",
185 "inline namespace A {\n}"));
186 // Should be: with { ... }
187}
188
189TEST(DeclPrinter, TestNamespaceAlias1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000190 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000191 "namespace Z { }"
192 "namespace A = Z;",
193 "A",
194 "namespace A = Z"));
195 // Should be: with semicolon
196}
197
198TEST(DeclPrinter, TestNamespaceAlias2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000199 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000200 "namespace X { namespace Y {} }"
201 "namespace A = X::Y;",
202 "A",
203 "namespace A = X::Y"));
204 // Should be: with semicolon
205}
206
207TEST(DeclPrinter, TestCXXRecordDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000208 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000209 "class A { int a; };",
210 "A",
211 "class A {\n}"));
212 // Should be: with semicolon, with { ... }
213}
214
215TEST(DeclPrinter, TestCXXRecordDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000216 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000217 "struct A { int a; };",
218 "A",
219 "struct A {\n}"));
220 // Should be: with semicolon, with { ... }
221}
222
223TEST(DeclPrinter, TestCXXRecordDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000224 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000225 "union A { int a; };",
226 "A",
227 "union A {\n}"));
228 // Should be: with semicolon, with { ... }
229}
230
231TEST(DeclPrinter, TestCXXRecordDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000232 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000233 "class Z { int a; };"
234 "class A : Z { int b; };",
235 "A",
236 "class A : Z {\n}"));
237 // Should be: with semicolon, with { ... }, without two spaces
238}
239
240TEST(DeclPrinter, TestCXXRecordDecl5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000241 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000242 "struct Z { int a; };"
243 "struct A : Z { int b; };",
244 "A",
245 "struct A : Z {\n}"));
246 // Should be: with semicolon, with { ... }, without two spaces
247}
248
249TEST(DeclPrinter, TestCXXRecordDecl6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000250 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000251 "class Z { int a; };"
252 "class A : public Z { int b; };",
253 "A",
254 "class A : public Z {\n}"));
255 // Should be: with semicolon, with { ... }
256}
257
258TEST(DeclPrinter, TestCXXRecordDecl7) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000259 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000260 "class Z { int a; };"
261 "class A : protected Z { int b; };",
262 "A",
263 "class A : protected Z {\n}"));
264 // Should be: with semicolon, with { ... }
265}
266
267TEST(DeclPrinter, TestCXXRecordDecl8) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000268 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000269 "class Z { int a; };"
270 "class A : private Z { int b; };",
271 "A",
272 "class A : private Z {\n}"));
273 // Should be: with semicolon, with { ... }
274}
275
276TEST(DeclPrinter, TestCXXRecordDecl9) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000277 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000278 "class Z { int a; };"
279 "class A : virtual Z { int b; };",
280 "A",
281 "class A : virtual Z {\n}"));
282 // Should be: with semicolon, with { ... }, without two spaces
283}
284
285TEST(DeclPrinter, TestCXXRecordDecl10) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000286 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000287 "class Z { int a; };"
288 "class A : virtual public Z { int b; };",
289 "A",
290 "class A : virtual public Z {\n}"));
291 // Should be: with semicolon, with { ... }
292}
293
294TEST(DeclPrinter, TestCXXRecordDecl11) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000295 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000296 "class Z { int a; };"
297 "class Y : virtual public Z { int b; };"
298 "class A : virtual public Z, private Y { int c; };",
299 "A",
300 "class A : virtual public Z, private Y {\n}"));
301 // Should be: with semicolon, with { ... }
302}
303
304TEST(DeclPrinter, TestFunctionDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000305 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000306 "void A();",
307 "A",
308 "void A()"));
309 // Should be: with semicolon
310}
311
312TEST(DeclPrinter, TestFunctionDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000313 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000314 "void A() {}",
315 "A",
316 "void A()"));
317 // Should be: with semicolon
318}
319
320TEST(DeclPrinter, TestFunctionDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000321 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000322 "void Z();"
323 "void A() { Z(); }",
324 "A",
325 "void A()"));
326 // Should be: with semicolon
327}
328
329TEST(DeclPrinter, TestFunctionDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000330 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000331 "extern void A();",
332 "A",
333 "extern void A()"));
334 // Should be: with semicolon
335}
336
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000337TEST(DeclPrinter, TestFunctionDecl5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000338 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000339 "static void A();",
340 "A",
341 "static void A()"));
342 // Should be: with semicolon
343}
344
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000345TEST(DeclPrinter, TestFunctionDecl6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000346 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000347 "inline void A();",
348 "A",
349 "inline void A()"));
350 // Should be: with semicolon
351}
352
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000353TEST(DeclPrinter, TestFunctionDecl7) {
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000354 ASSERT_TRUE(PrintedDeclCXX11Matches(
355 "constexpr int A(int a);",
356 "A",
Benjamin Kramer2907b082014-02-25 18:49:49 +0000357 "constexpr int A(int a)"));
358 // Should be: with semicolon
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000359}
360
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000361TEST(DeclPrinter, TestFunctionDecl8) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000362 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000363 "void A(int a);",
364 "A",
365 "void A(int a)"));
366 // Should be: with semicolon
367}
368
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000369TEST(DeclPrinter, TestFunctionDecl9) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000370 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000371 "void A(...);",
372 "A",
373 "void A(...)"));
374 // Should be: with semicolon
375}
376
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000377TEST(DeclPrinter, TestFunctionDecl10) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000378 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000379 "void A(int a, ...);",
380 "A",
381 "void A(int a, ...)"));
382 // Should be: with semicolon
383}
384
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000385TEST(DeclPrinter, TestFunctionDecl11) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000386 ASSERT_TRUE(PrintedDeclCXX98Matches(
David Majnemerd4328de2014-01-14 08:18:49 +0000387 "typedef long ssize_t;"
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000388 "typedef int *pInt;"
David Majnemerd4328de2014-01-14 08:18:49 +0000389 "void A(int a, pInt b, ssize_t c);",
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000390 "A",
David Majnemerd4328de2014-01-14 08:18:49 +0000391 "void A(int a, pInt b, ssize_t c)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000392 // Should be: with semicolon
393}
394
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000395TEST(DeclPrinter, TestFunctionDecl12) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000396 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000397 "void A(int a, int b = 0);",
398 "A",
399 "void A(int a, int b = 0)"));
400 // Should be: with semicolon
401}
402
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000403TEST(DeclPrinter, TestFunctionDecl13) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000404 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000405 "void (*A(int a))(int b);",
406 "A",
407 "void (*A(int a))(int)"));
408 // Should be: with semicolon, with parameter name (?)
409}
410
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000411TEST(DeclPrinter, TestFunctionDecl14) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000412 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000413 "template<typename T>"
414 "void A(T t) { }"
415 "template<>"
416 "void A(int N) { }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000417 functionDecl(hasName("A"), isExplicitTemplateSpecialization()).bind("id"),
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000418 "void A(int N)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000419 // WRONG; Should be: "template <> void A(int N);"));
420}
421
422
423TEST(DeclPrinter, TestCXXConstructorDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000424 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000425 "struct A {"
426 " A();"
427 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000428 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000429 "A()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000430}
431
432TEST(DeclPrinter, TestCXXConstructorDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000433 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000434 "struct A {"
435 " A(int a);"
436 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000437 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000438 "A(int a)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000439}
440
441TEST(DeclPrinter, TestCXXConstructorDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000442 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000443 "struct A {"
444 " A(const A &a);"
445 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000446 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000447 "A(const A &a)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000448}
449
450TEST(DeclPrinter, TestCXXConstructorDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000451 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000452 "struct A {"
453 " A(const A &a, int = 0);"
454 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000455 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000456 "A(const A &a, int = 0)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000457}
458
459TEST(DeclPrinter, TestCXXConstructorDecl5) {
460 ASSERT_TRUE(PrintedDeclCXX11Matches(
461 "struct A {"
462 " A(const A &&a);"
463 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000464 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000465 "A(const A &&a)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000466}
467
468TEST(DeclPrinter, TestCXXConstructorDecl6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000469 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000470 "struct A {"
471 " explicit A(int a);"
472 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000473 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000474 "explicit A(int a)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000475}
476
477TEST(DeclPrinter, TestCXXConstructorDecl7) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000478 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000479 "struct A {"
480 " constexpr A();"
481 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000482 constructorDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer2907b082014-02-25 18:49:49 +0000483 "constexpr A()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000484}
485
486TEST(DeclPrinter, TestCXXConstructorDecl8) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000487 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000488 "struct A {"
489 " A() = default;"
490 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000491 constructorDecl(ofClass(hasName("A"))).bind("id"),
Richard Smithbd305122012-12-11 01:14:52 +0000492 "A() = default"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000493}
494
495TEST(DeclPrinter, TestCXXConstructorDecl9) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000496 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000497 "struct A {"
498 " A() = delete;"
499 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000500 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000501 "A() = delete"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000502}
503
Dmitri Gribenko340c0f62012-08-24 00:26:25 +0000504TEST(DeclPrinter, TestCXXConstructorDecl10) {
505 ASSERT_TRUE(PrintedDeclCXX11Matches(
506 "template<typename... T>"
507 "struct A {"
508 " A(const A &a);"
509 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000510 constructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000511 "A<T...>(const A<T...> &a)"));
Dmitri Gribenko340c0f62012-08-24 00:26:25 +0000512}
513
514TEST(DeclPrinter, TestCXXConstructorDecl11) {
NAKAMURA Takumi7d2da0b2014-02-16 10:16:09 +0000515 ASSERT_TRUE(PrintedDeclCXX11nonMSCMatches(
Dmitri Gribenko340c0f62012-08-24 00:26:25 +0000516 "template<typename... T>"
517 "struct A : public T... {"
518 " A(T&&... ts) : T(ts)... {}"
519 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000520 constructorDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer2907b082014-02-25 18:49:49 +0000521 "A<T...>(T &&ts...) : T(ts)..."));
Dmitri Gribenko340c0f62012-08-24 00:26:25 +0000522}
Dmitri Gribenko340c0f62012-08-24 00:26:25 +0000523
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000524TEST(DeclPrinter, TestCXXDestructorDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000525 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000526 "struct A {"
527 " ~A();"
528 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000529 destructorDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer2907b082014-02-25 18:49:49 +0000530 "~A()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000531}
532
533TEST(DeclPrinter, TestCXXDestructorDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000534 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000535 "struct A {"
536 " virtual ~A();"
537 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000538 destructorDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer2907b082014-02-25 18:49:49 +0000539 "virtual ~A()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000540}
541
542TEST(DeclPrinter, TestCXXConversionDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000543 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000544 "struct A {"
545 " operator int();"
546 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000547 methodDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000548 "operator int()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000549}
550
551TEST(DeclPrinter, TestCXXConversionDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000552 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000553 "struct A {"
554 " operator bool();"
555 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000556 methodDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000557 "operator bool()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000558}
559
560TEST(DeclPrinter, TestCXXConversionDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000561 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000562 "struct Z {};"
563 "struct A {"
564 " operator Z();"
565 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000566 methodDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000567 "operator Z()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000568}
569
570TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000571 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000572 "namespace std { typedef decltype(sizeof(int)) size_t; }"
573 "struct Z {"
574 " void *operator new(std::size_t);"
575 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000576 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000577 "void *operator new(std::size_t)"));
578 // Should be: with semicolon
579}
580
581TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000582 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000583 "namespace std { typedef decltype(sizeof(int)) size_t; }"
584 "struct Z {"
585 " void *operator new[](std::size_t);"
586 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000587 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000588 "void *operator new[](std::size_t)"));
589 // Should be: with semicolon
590}
591
592TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000593 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000594 "struct Z {"
595 " void operator delete(void *);"
596 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000597 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000598 "void operator delete(void *) noexcept"));
599 // Should be: with semicolon, without noexcept?
600}
601
602TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000603 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000604 "struct Z {"
605 " void operator delete(void *);"
606 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000607 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000608 "void operator delete(void *)"));
609 // Should be: with semicolon
610}
611
612TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000613 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000614 "struct Z {"
615 " void operator delete[](void *);"
616 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000617 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000618 "void operator delete[](void *) noexcept"));
619 // Should be: with semicolon, without noexcept?
620}
621
622TEST(DeclPrinter, TestCXXMethodDecl_Operator1) {
623 const char *OperatorNames[] = {
624 "+", "-", "*", "/", "%", "^", "&", "|",
625 "=", "<", ">", "+=", "-=", "*=", "/=", "%=",
626 "^=", "&=", "|=", "<<", ">>", ">>=", "<<=", "==", "!=",
627 "<=", ">=", "&&", "||", ",", "->*",
628 "()", "[]"
629 };
630
631 for (unsigned i = 0, e = llvm::array_lengthof(OperatorNames); i != e; ++i) {
632 SmallString<128> Code;
633 Code.append("struct Z { void operator");
634 Code.append(OperatorNames[i]);
635 Code.append("(Z z); };");
636
637 SmallString<128> Expected;
638 Expected.append("void operator");
639 Expected.append(OperatorNames[i]);
640 Expected.append("(Z z)");
641 // Should be: with semicolon
642
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000643 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000644 Code,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000645 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000646 Expected));
647 }
648}
649
650TEST(DeclPrinter, TestCXXMethodDecl_Operator2) {
651 const char *OperatorNames[] = {
652 "~", "!", "++", "--", "->"
653 };
654
655 for (unsigned i = 0, e = llvm::array_lengthof(OperatorNames); i != e; ++i) {
656 SmallString<128> Code;
657 Code.append("struct Z { void operator");
658 Code.append(OperatorNames[i]);
659 Code.append("(); };");
660
661 SmallString<128> Expected;
662 Expected.append("void operator");
663 Expected.append(OperatorNames[i]);
664 Expected.append("()");
665 // Should be: with semicolon
666
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000667 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000668 Code,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000669 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000670 Expected));
671 }
672}
673
674TEST(DeclPrinter, TestCXXMethodDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000675 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000676 "struct Z {"
677 " void A(int a);"
678 "};",
679 "A",
680 "void A(int a)"));
681 // Should be: with semicolon
682}
683
684TEST(DeclPrinter, TestCXXMethodDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000685 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000686 "struct Z {"
687 " virtual void A(int a);"
688 "};",
689 "A",
690 "virtual void A(int a)"));
691 // Should be: with semicolon
692}
693
694TEST(DeclPrinter, TestCXXMethodDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000695 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000696 "struct Z {"
697 " virtual void A(int a);"
698 "};"
699 "struct ZZ : Z {"
700 " void A(int a);"
701 "};",
702 "ZZ::A",
703 "void A(int a)"));
704 // Should be: with semicolon
705 // TODO: should we print "virtual"?
706}
707
708TEST(DeclPrinter, TestCXXMethodDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000709 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000710 "struct Z {"
711 " inline void A(int a);"
712 "};",
713 "A",
714 "inline void A(int a)"));
715 // Should be: with semicolon
716}
717
718TEST(DeclPrinter, TestCXXMethodDecl5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000719 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000720 "struct Z {"
721 " virtual void A(int a) = 0;"
722 "};",
723 "A",
724 "virtual void A(int a) = 0"));
725 // Should be: with semicolon
726}
727
728TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000729 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000730 "struct Z {"
731 " void A(int a) const;"
732 "};",
733 "A",
734 "void A(int a) const"));
735 // Should be: with semicolon
736}
737
738TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000739 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000740 "struct Z {"
741 " void A(int a) volatile;"
742 "};",
743 "A",
744 "void A(int a) volatile"));
745 // Should be: with semicolon
746}
747
748TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000749 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000750 "struct Z {"
751 " void A(int a) const volatile;"
752 "};",
753 "A",
754 "void A(int a) const volatile"));
755 // Should be: with semicolon
756}
757
758TEST(DeclPrinter, TestCXXMethodDecl_RefQualifier1) {
759 ASSERT_TRUE(PrintedDeclCXX11Matches(
760 "struct Z {"
761 " void A(int a) &;"
762 "};",
763 "A",
Benjamin Kramer2907b082014-02-25 18:49:49 +0000764 "void A(int a) &"));
765 // Should be: with semicolon
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000766}
767
768TEST(DeclPrinter, TestCXXMethodDecl_RefQualifier2) {
769 ASSERT_TRUE(PrintedDeclCXX11Matches(
770 "struct Z {"
771 " void A(int a) &&;"
772 "};",
773 "A",
Benjamin Kramer2907b082014-02-25 18:49:49 +0000774 "void A(int a) &&"));
775 // Should be: with semicolon
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000776}
777
778TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000779 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000780 "struct Z {"
781 " void A(int a) throw();"
782 "};",
783 "A",
784 "void A(int a) throw()"));
785 // Should be: with semicolon
786}
787
788TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000789 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000790 "struct Z {"
791 " void A(int a) throw(int);"
792 "};",
793 "A",
794 "void A(int a) throw(int)"));
795 // Should be: with semicolon
796}
797
798TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000799 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000800 "class ZZ {};"
801 "struct Z {"
802 " void A(int a) throw(ZZ, int);"
803 "};",
804 "A",
805 "void A(int a) throw(ZZ, int)"));
806 // Should be: with semicolon
807}
808
809TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification4) {
810 ASSERT_TRUE(PrintedDeclCXX11Matches(
811 "struct Z {"
812 " void A(int a) noexcept;"
813 "};",
814 "A",
815 "void A(int a) noexcept"));
816 // Should be: with semicolon
817}
818
819TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification5) {
820 ASSERT_TRUE(PrintedDeclCXX11Matches(
821 "struct Z {"
822 " void A(int a) noexcept(true);"
823 "};",
824 "A",
825 "void A(int a) noexcept(trueA(int a) noexcept(true)"));
826 // WRONG; Should be: "void A(int a) noexcept(true);"
827}
828
829TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification6) {
830 ASSERT_TRUE(PrintedDeclCXX11Matches(
831 "struct Z {"
832 " void A(int a) noexcept(1 < 2);"
833 "};",
834 "A",
835 "void A(int a) noexcept(1 < 2A(int a) noexcept(1 < 2)"));
836 // WRONG; Should be: "void A(int a) noexcept(1 < 2);"
837}
838
839TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification7) {
840 ASSERT_TRUE(PrintedDeclCXX11Matches(
841 "template<int N>"
842 "struct Z {"
843 " void A(int a) noexcept(N < 2);"
844 "};",
845 "A",
846 "void A(int a) noexcept(N < 2A(int a) noexcept(N < 2)"));
847 // WRONG; Should be: "void A(int a) noexcept(N < 2);"
848}
849
850TEST(DeclPrinter, TestVarDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000851 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000852 "char *const (*(*A)[5])(int);",
853 "A",
854 "char *const (*(*A)[5])(int)"));
855 // Should be: with semicolon
856}
857
858TEST(DeclPrinter, TestVarDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000859 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000860 "void (*A)() throw(int);",
861 "A",
862 "void (*A)() throw(int)"));
863 // Should be: with semicolon
864}
865
866TEST(DeclPrinter, TestVarDecl3) {
867 ASSERT_TRUE(PrintedDeclCXX11Matches(
868 "void (*A)() noexcept;",
869 "A",
870 "void (*A)() noexcept"));
871 // Should be: with semicolon
872}
873
874TEST(DeclPrinter, TestFieldDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000875 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000876 "template<typename T>"
877 "struct Z { T A; };",
878 "A",
879 "T A"));
880 // Should be: with semicolon
881}
882
883TEST(DeclPrinter, TestFieldDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000884 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000885 "template<int N>"
886 "struct Z { int A[N]; };",
887 "A",
888 "int A[N]"));
889 // Should be: with semicolon
890}
891
892TEST(DeclPrinter, TestClassTemplateDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000893 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000894 "template<typename T>"
895 "struct A { T a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000896 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000897 "template <typename T> struct A {\n}"));
898 // Should be: with semicolon, with { ... }
899}
900
901TEST(DeclPrinter, TestClassTemplateDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000902 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000903 "template<typename T = int>"
904 "struct A { T a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000905 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000906 "template <typename T = int> struct A {\n}"));
907 // Should be: with semicolon, with { ... }
908}
909
910TEST(DeclPrinter, TestClassTemplateDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000911 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000912 "template<class T>"
913 "struct A { T a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000914 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000915 "template <class T> struct A {\n}"));
916 // Should be: with semicolon, with { ... }
917}
918
919TEST(DeclPrinter, TestClassTemplateDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000920 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000921 "template<typename T, typename U>"
922 "struct A { T a; U b; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000923 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000924 "template <typename T, typename U> struct A {\n}"));
925 // Should be: with semicolon, with { ... }
926}
927
928TEST(DeclPrinter, TestClassTemplateDecl5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000929 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000930 "template<int N>"
931 "struct A { int a[N]; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000932 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000933 "template <int N> struct A {\n}"));
934 // Should be: with semicolon, with { ... }
935}
936
937TEST(DeclPrinter, TestClassTemplateDecl6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000938 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000939 "template<int N = 42>"
940 "struct A { int a[N]; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000941 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000942 "template <int N = 42> struct A {\n}"));
943 // Should be: with semicolon, with { ... }
944}
945
946TEST(DeclPrinter, TestClassTemplateDecl7) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000947 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000948 "typedef int MyInt;"
949 "template<MyInt N>"
950 "struct A { int a[N]; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000951 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000952 "template <MyInt N> struct A {\n}"));
953 // Should be: with semicolon, with { ... }
954}
955
956TEST(DeclPrinter, TestClassTemplateDecl8) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000957 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000958 "template<template<typename U> class T> struct A { };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000959 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000960 "template <template <typename U> class T> struct A {\n}"));
961 // Should be: with semicolon, with { ... }
962}
963
964TEST(DeclPrinter, TestClassTemplateDecl9) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000965 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000966 "template<typename T> struct Z { };"
967 "template<template<typename U> class T = Z> struct A { };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000968 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000969 "template <template <typename U> class T> struct A {\n}"));
970 // Should be: with semicolon, with { ... }
971}
972
973TEST(DeclPrinter, TestClassTemplateDecl10) {
974 ASSERT_TRUE(PrintedDeclCXX11Matches(
975 "template<typename... T>"
976 "struct A { int a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000977 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000978 "template <typename ... T> struct A {\n}"));
979 // Should be: with semicolon, with { ... }, without spaces before '...'
980}
981
982TEST(DeclPrinter, TestClassTemplateDecl11) {
983 ASSERT_TRUE(PrintedDeclCXX11Matches(
984 "template<typename... T>"
985 "struct A : public T... { int a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000986 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000987 "template <typename ... T> struct A : public T... {\n}"));
988 // Should be: with semicolon, with { ... }, without spaces before '...'
989}
990
991TEST(DeclPrinter, TestClassTemplatePartialSpecializationDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000992 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000993 "template<typename T, typename U>"
994 "struct A { T a; U b; };"
995 "template<typename T>"
996 "struct A<T, int> { T a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000997 classTemplateSpecializationDecl().bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000998 "struct A {\n}"));
999 // WRONG; Should be: "template<typename T> struct A<T, int> { ... }"
1000}
1001
1002TEST(DeclPrinter, TestClassTemplatePartialSpecializationDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001003 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001004 "template<typename T>"
1005 "struct A { T a; };"
1006 "template<typename T>"
1007 "struct A<T *> { T a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001008 classTemplateSpecializationDecl().bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001009 "struct A {\n}"));
1010 // WRONG; Should be: "template<typename T> struct A<T *> { ... }"
1011}
1012
1013TEST(DeclPrinter, TestClassTemplateSpecializationDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001014 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001015 "template<typename T>"
1016 "struct A { T a; };"
1017 "template<>"
1018 "struct A<int> { int a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001019 classTemplateSpecializationDecl().bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001020 "struct A {\n}"));
1021 // WRONG; Should be: "template<> struct A<int> { ... }"
1022}
1023
1024TEST(DeclPrinter, TestFunctionTemplateDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001025 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001026 "template<typename T>"
1027 "void A(T &t);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001028 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001029 "template <typename T> void A(T &t)"));
1030 // Should be: with semicolon
1031}
1032
1033TEST(DeclPrinter, TestFunctionTemplateDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001034 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001035 "template<typename T>"
1036 "void A(T &t) { }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001037 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +00001038 "template <typename T> void A(T &t)"));
1039 // Should be: with semicolon
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001040}
1041
1042TEST(DeclPrinter, TestFunctionTemplateDecl3) {
1043 ASSERT_TRUE(PrintedDeclCXX11Matches(
1044 "template<typename... T>"
1045 "void A(T... a);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001046 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001047 "template <typename ... T> void A(T a...)"));
1048 // WRONG; Should be: "template <typename ... T> void A(T... a)"
1049 // (not "T a...")
1050 // Should be: with semicolon.
1051}
1052
1053TEST(DeclPrinter, TestFunctionTemplateDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001054 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001055 "struct Z { template<typename T> void A(T t); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001056 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001057 "template <typename T> void A(T t)"));
1058 // Should be: with semicolon
1059}
1060
1061TEST(DeclPrinter, TestFunctionTemplateDecl5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001062 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001063 "struct Z { template<typename T> void A(T t) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001064 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +00001065 "template <typename T> void A(T t)"));
1066 // Should be: with semicolon
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001067}
1068
1069TEST(DeclPrinter, TestFunctionTemplateDecl6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001070 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001071 "template<typename T >struct Z {"
1072 " template<typename U> void A(U t) {}"
1073 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001074 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +00001075 "template <typename U> void A(U t)"));
1076 // Should be: with semicolon
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001077}
1078
1079TEST(DeclPrinter, TestTemplateArgumentList1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001080 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001081 "template<typename T> struct Z {};"
1082 "struct X {};"
1083 "Z<X> A;",
1084 "A",
1085 "Z<X> A"));
1086 // Should be: with semicolon
1087}
1088
1089TEST(DeclPrinter, TestTemplateArgumentList2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001090 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001091 "template<typename T, typename U> struct Z {};"
1092 "struct X {};"
1093 "typedef int Y;"
1094 "Z<X, Y> A;",
1095 "A",
1096 "Z<X, Y> A"));
1097 // Should be: with semicolon
1098}
1099
1100TEST(DeclPrinter, TestTemplateArgumentList3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001101 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001102 "template<typename T> struct Z {};"
1103 "template<typename T> struct X {};"
1104 "Z<X<int> > A;",
1105 "A",
1106 "Z<X<int> > A"));
1107 // Should be: with semicolon
1108}
1109
1110TEST(DeclPrinter, TestTemplateArgumentList4) {
1111 ASSERT_TRUE(PrintedDeclCXX11Matches(
1112 "template<typename T> struct Z {};"
1113 "template<typename T> struct X {};"
1114 "Z<X<int>> A;",
1115 "A",
1116 "Z<X<int> > A"));
1117 // Should be: with semicolon, without extra space in "> >"
1118}
1119
1120TEST(DeclPrinter, TestTemplateArgumentList5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001121 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001122 "template<typename T> struct Z {};"
1123 "template<typename T> struct X { Z<T> A; };",
1124 "A",
1125 "Z<T> A"));
1126 // Should be: with semicolon
1127}
1128
1129TEST(DeclPrinter, TestTemplateArgumentList6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001130 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001131 "template<template<typename T> class U> struct Z {};"
1132 "template<typename T> struct X {};"
1133 "Z<X> A;",
1134 "A",
1135 "Z<X> A"));
1136 // Should be: with semicolon
1137}
1138
1139TEST(DeclPrinter, TestTemplateArgumentList7) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001140 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001141 "template<template<typename T> class U> struct Z {};"
1142 "template<template<typename T> class U> struct Y {"
1143 " Z<U> A;"
1144 "};",
1145 "A",
1146 "Z<U> A"));
1147 // Should be: with semicolon
1148}
1149
1150TEST(DeclPrinter, TestTemplateArgumentList8) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001151 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001152 "template<typename T> struct Z {};"
1153 "template<template<typename T> class U> struct Y {"
1154 " Z<U<int> > A;"
1155 "};",
1156 "A",
1157 "Z<U<int> > A"));
1158 // Should be: with semicolon
1159}
1160
1161TEST(DeclPrinter, TestTemplateArgumentList9) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001162 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001163 "template<unsigned I> struct Z {};"
1164 "Z<0> A;",
1165 "A",
1166 "Z<0> A"));
1167 // Should be: with semicolon
1168}
1169
1170TEST(DeclPrinter, TestTemplateArgumentList10) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001171 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001172 "template<unsigned I> struct Z {};"
1173 "template<unsigned I> struct X { Z<I> A; };",
1174 "A",
1175 "Z<I> A"));
1176 // Should be: with semicolon
1177}
1178
1179TEST(DeclPrinter, TestTemplateArgumentList11) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001180 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001181 "template<int I> struct Z {};"
1182 "Z<42 * 10 - 420 / 1> A;",
1183 "A",
1184 "Z<42 * 10 - 420 / 1> A"));
1185 // Should be: with semicolon
1186}
1187
1188TEST(DeclPrinter, TestTemplateArgumentList12) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001189 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001190 "template<const char *p> struct Z {};"
1191 "extern const char X[] = \"aaa\";"
1192 "Z<X> A;",
1193 "A",
1194 "Z<X> A"));
1195 // Should be: with semicolon
1196}
1197
1198TEST(DeclPrinter, TestTemplateArgumentList13) {
1199 ASSERT_TRUE(PrintedDeclCXX11Matches(
1200 "template<typename... T> struct Z {};"
1201 "template<typename... T> struct X {"
1202 " Z<T...> A;"
1203 "};",
1204 "A",
1205 "Z<T...> A"));
1206 // Should be: with semicolon, without extra space in "> >"
1207}
1208
1209TEST(DeclPrinter, TestTemplateArgumentList14) {
1210 ASSERT_TRUE(PrintedDeclCXX11Matches(
1211 "template<typename... T> struct Z {};"
1212 "template<typename T> struct Y {};"
1213 "template<typename... T> struct X {"
1214 " Z<Y<T>...> A;"
1215 "};",
1216 "A",
1217 "Z<Y<T>...> A"));
1218 // Should be: with semicolon, without extra space in "> >"
1219}
1220
1221TEST(DeclPrinter, TestTemplateArgumentList15) {
1222 ASSERT_TRUE(PrintedDeclCXX11Matches(
1223 "template<unsigned I> struct Z {};"
1224 "template<typename... T> struct X {"
1225 " Z<sizeof...(T)> A;"
1226 "};",
1227 "A",
1228 "Z<sizeof...(T)> A"));
1229 // Should be: with semicolon, without extra space in "> >"
1230}
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001231
Fariborz Jahaniane0586a52012-10-18 19:12:17 +00001232TEST(DeclPrinter, TestObjCMethod1) {
1233 ASSERT_TRUE(PrintedDeclObjCMatches(
1234 "__attribute__((objc_root_class)) @interface X\n"
1235 "- (int)A:(id)anObject inRange:(long)range;\n"
1236 "@end\n"
1237 "@implementation X\n"
1238 "- (int)A:(id)anObject inRange:(long)range { int printThis; return 0; }\n"
1239 "@end\n",
1240 namedDecl(hasName("A:inRange:"),
1241 hasDescendant(namedDecl(hasName("printThis")))).bind("id"),
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001242 "- (int) A:(id)anObject inRange:(long)range"));
Fariborz Jahaniane0586a52012-10-18 19:12:17 +00001243}
1244
Fariborz Jahanian44194492012-12-20 02:20:09 +00001245TEST(DeclPrinter, TestObjCProtocol1) {
1246 ASSERT_TRUE(PrintedDeclObjCMatches(
1247 "@protocol P1, P2;",
1248 namedDecl(hasName("P1")).bind("id"),
1249 "@protocol P1;\n"));
1250 ASSERT_TRUE(PrintedDeclObjCMatches(
1251 "@protocol P1, P2;",
1252 namedDecl(hasName("P2")).bind("id"),
1253 "@protocol P2;\n"));
1254}
1255
1256TEST(DeclPrinter, TestObjCProtocol2) {
1257 ASSERT_TRUE(PrintedDeclObjCMatches(
1258 "@protocol P2 @end"
1259 "@protocol P1<P2> @end",
1260 namedDecl(hasName("P1")).bind("id"),
1261 "@protocol P1<P2>\n@end"));
1262}