blob: c604fb1e7e3ed2be4017920e74e695b26ca3a20a [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)..."));
Benjamin Kramer594802f2014-02-26 10:23:43 +0000522 // WRONG; Should be: "A(T&&... ts) : T(ts)..."
Dmitri Gribenko340c0f62012-08-24 00:26:25 +0000523}
Dmitri Gribenko340c0f62012-08-24 00:26:25 +0000524
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000525TEST(DeclPrinter, TestCXXDestructorDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000526 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000527 "struct A {"
528 " ~A();"
529 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000530 destructorDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer2907b082014-02-25 18:49:49 +0000531 "~A()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000532}
533
534TEST(DeclPrinter, TestCXXDestructorDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000535 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000536 "struct A {"
537 " virtual ~A();"
538 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000539 destructorDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer2907b082014-02-25 18:49:49 +0000540 "virtual ~A()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000541}
542
543TEST(DeclPrinter, TestCXXConversionDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000544 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000545 "struct A {"
546 " operator int();"
547 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000548 methodDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000549 "operator int()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000550}
551
552TEST(DeclPrinter, TestCXXConversionDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000553 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000554 "struct A {"
555 " operator bool();"
556 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000557 methodDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000558 "operator bool()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000559}
560
561TEST(DeclPrinter, TestCXXConversionDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000562 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000563 "struct Z {};"
564 "struct A {"
565 " operator Z();"
566 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000567 methodDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000568 "operator Z()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000569}
570
571TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000572 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000573 "namespace std { typedef decltype(sizeof(int)) size_t; }"
574 "struct Z {"
575 " void *operator new(std::size_t);"
576 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000577 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000578 "void *operator new(std::size_t)"));
579 // Should be: with semicolon
580}
581
582TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000583 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000584 "namespace std { typedef decltype(sizeof(int)) size_t; }"
585 "struct Z {"
586 " void *operator new[](std::size_t);"
587 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000588 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000589 "void *operator new[](std::size_t)"));
590 // Should be: with semicolon
591}
592
593TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000594 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000595 "struct Z {"
596 " void operator delete(void *);"
597 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000598 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000599 "void operator delete(void *) noexcept"));
600 // Should be: with semicolon, without noexcept?
601}
602
603TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000604 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000605 "struct Z {"
606 " void operator delete(void *);"
607 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000608 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000609 "void operator delete(void *)"));
610 // Should be: with semicolon
611}
612
613TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000614 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000615 "struct Z {"
616 " void operator delete[](void *);"
617 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000618 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000619 "void operator delete[](void *) noexcept"));
620 // Should be: with semicolon, without noexcept?
621}
622
623TEST(DeclPrinter, TestCXXMethodDecl_Operator1) {
624 const char *OperatorNames[] = {
625 "+", "-", "*", "/", "%", "^", "&", "|",
626 "=", "<", ">", "+=", "-=", "*=", "/=", "%=",
627 "^=", "&=", "|=", "<<", ">>", ">>=", "<<=", "==", "!=",
628 "<=", ">=", "&&", "||", ",", "->*",
629 "()", "[]"
630 };
631
632 for (unsigned i = 0, e = llvm::array_lengthof(OperatorNames); i != e; ++i) {
633 SmallString<128> Code;
634 Code.append("struct Z { void operator");
635 Code.append(OperatorNames[i]);
636 Code.append("(Z z); };");
637
638 SmallString<128> Expected;
639 Expected.append("void operator");
640 Expected.append(OperatorNames[i]);
641 Expected.append("(Z z)");
642 // Should be: with semicolon
643
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000644 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000645 Code,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000646 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000647 Expected));
648 }
649}
650
651TEST(DeclPrinter, TestCXXMethodDecl_Operator2) {
652 const char *OperatorNames[] = {
653 "~", "!", "++", "--", "->"
654 };
655
656 for (unsigned i = 0, e = llvm::array_lengthof(OperatorNames); i != e; ++i) {
657 SmallString<128> Code;
658 Code.append("struct Z { void operator");
659 Code.append(OperatorNames[i]);
660 Code.append("(); };");
661
662 SmallString<128> Expected;
663 Expected.append("void operator");
664 Expected.append(OperatorNames[i]);
665 Expected.append("()");
666 // Should be: with semicolon
667
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000668 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000669 Code,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000670 methodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000671 Expected));
672 }
673}
674
675TEST(DeclPrinter, TestCXXMethodDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000676 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000677 "struct Z {"
678 " void A(int a);"
679 "};",
680 "A",
681 "void A(int a)"));
682 // Should be: with semicolon
683}
684
685TEST(DeclPrinter, TestCXXMethodDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000686 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000687 "struct Z {"
688 " virtual void A(int a);"
689 "};",
690 "A",
691 "virtual void A(int a)"));
692 // Should be: with semicolon
693}
694
695TEST(DeclPrinter, TestCXXMethodDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000696 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000697 "struct Z {"
698 " virtual void A(int a);"
699 "};"
700 "struct ZZ : Z {"
701 " void A(int a);"
702 "};",
703 "ZZ::A",
704 "void A(int a)"));
705 // Should be: with semicolon
706 // TODO: should we print "virtual"?
707}
708
709TEST(DeclPrinter, TestCXXMethodDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000710 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000711 "struct Z {"
712 " inline void A(int a);"
713 "};",
714 "A",
715 "inline void A(int a)"));
716 // Should be: with semicolon
717}
718
719TEST(DeclPrinter, TestCXXMethodDecl5) {
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) = 0;"
723 "};",
724 "A",
725 "virtual void A(int a) = 0"));
726 // Should be: with semicolon
727}
728
729TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000730 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000731 "struct Z {"
732 " void A(int a) const;"
733 "};",
734 "A",
735 "void A(int a) const"));
736 // Should be: with semicolon
737}
738
739TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000740 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000741 "struct Z {"
742 " void A(int a) volatile;"
743 "};",
744 "A",
745 "void A(int a) volatile"));
746 // Should be: with semicolon
747}
748
749TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000750 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000751 "struct Z {"
752 " void A(int a) const volatile;"
753 "};",
754 "A",
755 "void A(int a) const volatile"));
756 // Should be: with semicolon
757}
758
759TEST(DeclPrinter, TestCXXMethodDecl_RefQualifier1) {
760 ASSERT_TRUE(PrintedDeclCXX11Matches(
761 "struct Z {"
762 " void A(int a) &;"
763 "};",
764 "A",
Benjamin Kramer2907b082014-02-25 18:49:49 +0000765 "void A(int a) &"));
766 // Should be: with semicolon
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000767}
768
769TEST(DeclPrinter, TestCXXMethodDecl_RefQualifier2) {
770 ASSERT_TRUE(PrintedDeclCXX11Matches(
771 "struct Z {"
772 " void A(int a) &&;"
773 "};",
774 "A",
Benjamin Kramer2907b082014-02-25 18:49:49 +0000775 "void A(int a) &&"));
776 // Should be: with semicolon
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000777}
778
779TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000780 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000781 "struct Z {"
782 " void A(int a) throw();"
783 "};",
784 "A",
785 "void A(int a) throw()"));
786 // Should be: with semicolon
787}
788
789TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000790 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000791 "struct Z {"
792 " void A(int a) throw(int);"
793 "};",
794 "A",
795 "void A(int a) throw(int)"));
796 // Should be: with semicolon
797}
798
799TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000800 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000801 "class ZZ {};"
802 "struct Z {"
803 " void A(int a) throw(ZZ, int);"
804 "};",
805 "A",
806 "void A(int a) throw(ZZ, int)"));
807 // Should be: with semicolon
808}
809
810TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification4) {
811 ASSERT_TRUE(PrintedDeclCXX11Matches(
812 "struct Z {"
813 " void A(int a) noexcept;"
814 "};",
815 "A",
816 "void A(int a) noexcept"));
817 // Should be: with semicolon
818}
819
820TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification5) {
821 ASSERT_TRUE(PrintedDeclCXX11Matches(
822 "struct Z {"
823 " void A(int a) noexcept(true);"
824 "};",
825 "A",
826 "void A(int a) noexcept(trueA(int a) noexcept(true)"));
827 // WRONG; Should be: "void A(int a) noexcept(true);"
828}
829
830TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification6) {
831 ASSERT_TRUE(PrintedDeclCXX11Matches(
832 "struct Z {"
833 " void A(int a) noexcept(1 < 2);"
834 "};",
835 "A",
836 "void A(int a) noexcept(1 < 2A(int a) noexcept(1 < 2)"));
837 // WRONG; Should be: "void A(int a) noexcept(1 < 2);"
838}
839
840TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification7) {
841 ASSERT_TRUE(PrintedDeclCXX11Matches(
842 "template<int N>"
843 "struct Z {"
844 " void A(int a) noexcept(N < 2);"
845 "};",
846 "A",
847 "void A(int a) noexcept(N < 2A(int a) noexcept(N < 2)"));
848 // WRONG; Should be: "void A(int a) noexcept(N < 2);"
849}
850
851TEST(DeclPrinter, TestVarDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000852 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000853 "char *const (*(*A)[5])(int);",
854 "A",
855 "char *const (*(*A)[5])(int)"));
856 // Should be: with semicolon
857}
858
859TEST(DeclPrinter, TestVarDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000860 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000861 "void (*A)() throw(int);",
862 "A",
863 "void (*A)() throw(int)"));
864 // Should be: with semicolon
865}
866
867TEST(DeclPrinter, TestVarDecl3) {
868 ASSERT_TRUE(PrintedDeclCXX11Matches(
869 "void (*A)() noexcept;",
870 "A",
871 "void (*A)() noexcept"));
872 // Should be: with semicolon
873}
874
875TEST(DeclPrinter, TestFieldDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000876 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000877 "template<typename T>"
878 "struct Z { T A; };",
879 "A",
880 "T A"));
881 // Should be: with semicolon
882}
883
884TEST(DeclPrinter, TestFieldDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000885 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000886 "template<int N>"
887 "struct Z { int A[N]; };",
888 "A",
889 "int A[N]"));
890 // Should be: with semicolon
891}
892
893TEST(DeclPrinter, TestClassTemplateDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000894 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000895 "template<typename T>"
896 "struct A { T a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000897 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000898 "template <typename T> struct A {\n}"));
899 // Should be: with semicolon, with { ... }
900}
901
902TEST(DeclPrinter, TestClassTemplateDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000903 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000904 "template<typename T = int>"
905 "struct A { T a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000906 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000907 "template <typename T = int> struct A {\n}"));
908 // Should be: with semicolon, with { ... }
909}
910
911TEST(DeclPrinter, TestClassTemplateDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000912 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000913 "template<class T>"
914 "struct A { T a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000915 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000916 "template <class T> struct A {\n}"));
917 // Should be: with semicolon, with { ... }
918}
919
920TEST(DeclPrinter, TestClassTemplateDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000921 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000922 "template<typename T, typename U>"
923 "struct A { T a; U b; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000924 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000925 "template <typename T, typename U> struct A {\n}"));
926 // Should be: with semicolon, with { ... }
927}
928
929TEST(DeclPrinter, TestClassTemplateDecl5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000930 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000931 "template<int N>"
932 "struct A { int a[N]; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000933 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000934 "template <int N> struct A {\n}"));
935 // Should be: with semicolon, with { ... }
936}
937
938TEST(DeclPrinter, TestClassTemplateDecl6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000939 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000940 "template<int N = 42>"
941 "struct A { int a[N]; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000942 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000943 "template <int N = 42> struct A {\n}"));
944 // Should be: with semicolon, with { ... }
945}
946
947TEST(DeclPrinter, TestClassTemplateDecl7) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000948 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000949 "typedef int MyInt;"
950 "template<MyInt N>"
951 "struct A { int a[N]; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000952 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000953 "template <MyInt N> struct A {\n}"));
954 // Should be: with semicolon, with { ... }
955}
956
957TEST(DeclPrinter, TestClassTemplateDecl8) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000958 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000959 "template<template<typename U> class T> struct A { };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000960 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000961 "template <template <typename U> class T> struct A {\n}"));
962 // Should be: with semicolon, with { ... }
963}
964
965TEST(DeclPrinter, TestClassTemplateDecl9) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000966 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000967 "template<typename T> struct Z { };"
968 "template<template<typename U> class T = Z> struct A { };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000969 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000970 "template <template <typename U> class T> struct A {\n}"));
971 // Should be: with semicolon, with { ... }
972}
973
974TEST(DeclPrinter, TestClassTemplateDecl10) {
975 ASSERT_TRUE(PrintedDeclCXX11Matches(
976 "template<typename... T>"
977 "struct A { int a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000978 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000979 "template <typename ... T> struct A {\n}"));
980 // Should be: with semicolon, with { ... }, without spaces before '...'
981}
982
983TEST(DeclPrinter, TestClassTemplateDecl11) {
984 ASSERT_TRUE(PrintedDeclCXX11Matches(
985 "template<typename... T>"
986 "struct A : public T... { int a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000987 classTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000988 "template <typename ... T> struct A : public T... {\n}"));
989 // Should be: with semicolon, with { ... }, without spaces before '...'
990}
991
992TEST(DeclPrinter, TestClassTemplatePartialSpecializationDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000993 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000994 "template<typename T, typename U>"
995 "struct A { T a; U b; };"
996 "template<typename T>"
997 "struct A<T, int> { T a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000998 classTemplateSpecializationDecl().bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000999 "struct A {\n}"));
1000 // WRONG; Should be: "template<typename T> struct A<T, int> { ... }"
1001}
1002
1003TEST(DeclPrinter, TestClassTemplatePartialSpecializationDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001004 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001005 "template<typename T>"
1006 "struct A { T a; };"
1007 "template<typename T>"
1008 "struct A<T *> { T a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001009 classTemplateSpecializationDecl().bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001010 "struct A {\n}"));
1011 // WRONG; Should be: "template<typename T> struct A<T *> { ... }"
1012}
1013
1014TEST(DeclPrinter, TestClassTemplateSpecializationDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001015 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001016 "template<typename T>"
1017 "struct A { T a; };"
1018 "template<>"
1019 "struct A<int> { int a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001020 classTemplateSpecializationDecl().bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001021 "struct A {\n}"));
1022 // WRONG; Should be: "template<> struct A<int> { ... }"
1023}
1024
1025TEST(DeclPrinter, TestFunctionTemplateDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001026 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001027 "template<typename T>"
1028 "void A(T &t);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001029 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001030 "template <typename T> void A(T &t)"));
1031 // Should be: with semicolon
1032}
1033
1034TEST(DeclPrinter, TestFunctionTemplateDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001035 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001036 "template<typename T>"
1037 "void A(T &t) { }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001038 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +00001039 "template <typename T> void A(T &t)"));
1040 // Should be: with semicolon
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001041}
1042
1043TEST(DeclPrinter, TestFunctionTemplateDecl3) {
1044 ASSERT_TRUE(PrintedDeclCXX11Matches(
1045 "template<typename... T>"
1046 "void A(T... a);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001047 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001048 "template <typename ... T> void A(T a...)"));
1049 // WRONG; Should be: "template <typename ... T> void A(T... a)"
1050 // (not "T a...")
1051 // Should be: with semicolon.
1052}
1053
1054TEST(DeclPrinter, TestFunctionTemplateDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001055 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001056 "struct Z { template<typename T> void A(T t); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001057 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001058 "template <typename T> void A(T t)"));
1059 // Should be: with semicolon
1060}
1061
1062TEST(DeclPrinter, TestFunctionTemplateDecl5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001063 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001064 "struct Z { template<typename T> void A(T t) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001065 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +00001066 "template <typename T> void A(T t)"));
1067 // Should be: with semicolon
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001068}
1069
1070TEST(DeclPrinter, TestFunctionTemplateDecl6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001071 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001072 "template<typename T >struct Z {"
1073 " template<typename U> void A(U t) {}"
1074 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001075 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +00001076 "template <typename U> void A(U t)"));
1077 // Should be: with semicolon
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001078}
1079
1080TEST(DeclPrinter, TestTemplateArgumentList1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001081 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001082 "template<typename T> struct Z {};"
1083 "struct X {};"
1084 "Z<X> A;",
1085 "A",
1086 "Z<X> A"));
1087 // Should be: with semicolon
1088}
1089
1090TEST(DeclPrinter, TestTemplateArgumentList2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001091 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001092 "template<typename T, typename U> struct Z {};"
1093 "struct X {};"
1094 "typedef int Y;"
1095 "Z<X, Y> A;",
1096 "A",
1097 "Z<X, Y> A"));
1098 // Should be: with semicolon
1099}
1100
1101TEST(DeclPrinter, TestTemplateArgumentList3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001102 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001103 "template<typename T> struct Z {};"
1104 "template<typename T> struct X {};"
1105 "Z<X<int> > A;",
1106 "A",
1107 "Z<X<int> > A"));
1108 // Should be: with semicolon
1109}
1110
1111TEST(DeclPrinter, TestTemplateArgumentList4) {
1112 ASSERT_TRUE(PrintedDeclCXX11Matches(
1113 "template<typename T> struct Z {};"
1114 "template<typename T> struct X {};"
1115 "Z<X<int>> A;",
1116 "A",
1117 "Z<X<int> > A"));
1118 // Should be: with semicolon, without extra space in "> >"
1119}
1120
1121TEST(DeclPrinter, TestTemplateArgumentList5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001122 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001123 "template<typename T> struct Z {};"
1124 "template<typename T> struct X { Z<T> A; };",
1125 "A",
1126 "Z<T> A"));
1127 // Should be: with semicolon
1128}
1129
1130TEST(DeclPrinter, TestTemplateArgumentList6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001131 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001132 "template<template<typename T> class U> struct Z {};"
1133 "template<typename T> struct X {};"
1134 "Z<X> A;",
1135 "A",
1136 "Z<X> A"));
1137 // Should be: with semicolon
1138}
1139
1140TEST(DeclPrinter, TestTemplateArgumentList7) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001141 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001142 "template<template<typename T> class U> struct Z {};"
1143 "template<template<typename T> class U> struct Y {"
1144 " Z<U> A;"
1145 "};",
1146 "A",
1147 "Z<U> A"));
1148 // Should be: with semicolon
1149}
1150
1151TEST(DeclPrinter, TestTemplateArgumentList8) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001152 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001153 "template<typename T> struct Z {};"
1154 "template<template<typename T> class U> struct Y {"
1155 " Z<U<int> > A;"
1156 "};",
1157 "A",
1158 "Z<U<int> > A"));
1159 // Should be: with semicolon
1160}
1161
1162TEST(DeclPrinter, TestTemplateArgumentList9) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001163 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001164 "template<unsigned I> struct Z {};"
1165 "Z<0> A;",
1166 "A",
1167 "Z<0> A"));
1168 // Should be: with semicolon
1169}
1170
1171TEST(DeclPrinter, TestTemplateArgumentList10) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001172 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001173 "template<unsigned I> struct Z {};"
1174 "template<unsigned I> struct X { Z<I> A; };",
1175 "A",
1176 "Z<I> A"));
1177 // Should be: with semicolon
1178}
1179
1180TEST(DeclPrinter, TestTemplateArgumentList11) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001181 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001182 "template<int I> struct Z {};"
1183 "Z<42 * 10 - 420 / 1> A;",
1184 "A",
1185 "Z<42 * 10 - 420 / 1> A"));
1186 // Should be: with semicolon
1187}
1188
1189TEST(DeclPrinter, TestTemplateArgumentList12) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001190 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001191 "template<const char *p> struct Z {};"
1192 "extern const char X[] = \"aaa\";"
1193 "Z<X> A;",
1194 "A",
1195 "Z<X> A"));
1196 // Should be: with semicolon
1197}
1198
1199TEST(DeclPrinter, TestTemplateArgumentList13) {
1200 ASSERT_TRUE(PrintedDeclCXX11Matches(
1201 "template<typename... T> struct Z {};"
1202 "template<typename... T> struct X {"
1203 " Z<T...> A;"
1204 "};",
1205 "A",
1206 "Z<T...> A"));
1207 // Should be: with semicolon, without extra space in "> >"
1208}
1209
1210TEST(DeclPrinter, TestTemplateArgumentList14) {
1211 ASSERT_TRUE(PrintedDeclCXX11Matches(
1212 "template<typename... T> struct Z {};"
1213 "template<typename T> struct Y {};"
1214 "template<typename... T> struct X {"
1215 " Z<Y<T>...> A;"
1216 "};",
1217 "A",
1218 "Z<Y<T>...> A"));
1219 // Should be: with semicolon, without extra space in "> >"
1220}
1221
1222TEST(DeclPrinter, TestTemplateArgumentList15) {
1223 ASSERT_TRUE(PrintedDeclCXX11Matches(
1224 "template<unsigned I> struct Z {};"
1225 "template<typename... T> struct X {"
1226 " Z<sizeof...(T)> A;"
1227 "};",
1228 "A",
1229 "Z<sizeof...(T)> A"));
1230 // Should be: with semicolon, without extra space in "> >"
1231}
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001232
Fariborz Jahaniane0586a52012-10-18 19:12:17 +00001233TEST(DeclPrinter, TestObjCMethod1) {
1234 ASSERT_TRUE(PrintedDeclObjCMatches(
1235 "__attribute__((objc_root_class)) @interface X\n"
1236 "- (int)A:(id)anObject inRange:(long)range;\n"
1237 "@end\n"
1238 "@implementation X\n"
1239 "- (int)A:(id)anObject inRange:(long)range { int printThis; return 0; }\n"
1240 "@end\n",
1241 namedDecl(hasName("A:inRange:"),
1242 hasDescendant(namedDecl(hasName("printThis")))).bind("id"),
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001243 "- (int) A:(id)anObject inRange:(long)range"));
Fariborz Jahaniane0586a52012-10-18 19:12:17 +00001244}
1245
Fariborz Jahanian44194492012-12-20 02:20:09 +00001246TEST(DeclPrinter, TestObjCProtocol1) {
1247 ASSERT_TRUE(PrintedDeclObjCMatches(
1248 "@protocol P1, P2;",
1249 namedDecl(hasName("P1")).bind("id"),
1250 "@protocol P1;\n"));
1251 ASSERT_TRUE(PrintedDeclObjCMatches(
1252 "@protocol P1, P2;",
1253 namedDecl(hasName("P2")).bind("id"),
1254 "@protocol P2;\n"));
1255}
1256
1257TEST(DeclPrinter, TestObjCProtocol2) {
1258 ASSERT_TRUE(PrintedDeclObjCMatches(
1259 "@protocol P2 @end"
1260 "@protocol P1<P2> @end",
1261 namedDecl(hasName("P1")).bind("id"),
1262 "@protocol P1<P2>\n@end"));
1263}