blob: 3bb17aba96c262c354ef5952fb6254e57b9367e0 [file] [log] [blame]
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001//===- unittests/AST/DeclPrinterTest.cpp --- Declaration printer tests ----===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Dmitri Gribenko309856a2012-08-20 23:39:06 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains tests for Decl::print() and related methods.
10//
11// Search this file for WRONG to see test cases that are producing something
12// completely wrong, invalid C++ or just misleading.
13//
14// These tests have a coding convention:
15// * declaration to be printed is named 'A' unless it should have some special
16// name (e.g., 'operator+');
17// * additional helper declarations are 'Z', 'Y', 'X' and so on.
18//
19//===----------------------------------------------------------------------===//
20
21#include "clang/AST/ASTContext.h"
22#include "clang/ASTMatchers/ASTMatchFinder.h"
23#include "clang/Tooling/Tooling.h"
Daniel Jasper6ed1f852012-08-24 05:50:27 +000024#include "llvm/ADT/SmallString.h"
Dmitri Gribenko309856a2012-08-20 23:39:06 +000025#include "gtest/gtest.h"
26
27using namespace clang;
28using namespace ast_matchers;
29using namespace tooling;
30
31namespace {
32
Alex Lorenz35019db2017-11-16 01:28:25 +000033using PrintingPolicyModifier = void (*)(PrintingPolicy &policy);
34
35void PrintDecl(raw_ostream &Out, const ASTContext *Context, const Decl *D,
36 PrintingPolicyModifier PolicyModifier) {
Dmitri Gribenko309856a2012-08-20 23:39:06 +000037 PrintingPolicy Policy = Context->getPrintingPolicy();
Dmitri Gribenkoa93a7e82012-08-21 17:36:32 +000038 Policy.TerseOutput = true;
Alex Lorenz35019db2017-11-16 01:28:25 +000039 if (PolicyModifier)
40 PolicyModifier(Policy);
Dmitri Gribenko309856a2012-08-20 23:39:06 +000041 D->print(Out, Policy, /*Indentation*/ 0, /*PrintInstantiation*/ false);
42}
43
44class PrintMatch : public MatchFinder::MatchCallback {
45 SmallString<1024> Printed;
46 unsigned NumFoundDecls;
Alex Lorenz35019db2017-11-16 01:28:25 +000047 PrintingPolicyModifier PolicyModifier;
Dmitri Gribenko309856a2012-08-20 23:39:06 +000048
49public:
Alex Lorenz35019db2017-11-16 01:28:25 +000050 PrintMatch(PrintingPolicyModifier PolicyModifier)
51 : NumFoundDecls(0), PolicyModifier(PolicyModifier) {}
Dmitri Gribenko309856a2012-08-20 23:39:06 +000052
Alexander Kornienko34eb2072015-04-11 02:00:23 +000053 void run(const MatchFinder::MatchResult &Result) override {
Alexander Kornienko7cdc7052016-12-13 16:19:34 +000054 const Decl *D = Result.Nodes.getNodeAs<Decl>("id");
Dmitri Gribenko309856a2012-08-20 23:39:06 +000055 if (!D || D->isImplicit())
56 return;
57 NumFoundDecls++;
58 if (NumFoundDecls > 1)
59 return;
60
61 llvm::raw_svector_ostream Out(Printed);
Alex Lorenz35019db2017-11-16 01:28:25 +000062 PrintDecl(Out, Result.Context, D, PolicyModifier);
Dmitri Gribenko309856a2012-08-20 23:39:06 +000063 }
64
65 StringRef getPrinted() const {
66 return Printed;
67 }
68
69 unsigned getNumFoundDecls() const {
70 return NumFoundDecls;
71 }
72};
73
Alex Lorenz35019db2017-11-16 01:28:25 +000074::testing::AssertionResult
75PrintedDeclMatches(StringRef Code, const std::vector<std::string> &Args,
76 const DeclarationMatcher &NodeMatch,
77 StringRef ExpectedPrinted, StringRef FileName,
78 PrintingPolicyModifier PolicyModifier = nullptr) {
79 PrintMatch Printer(PolicyModifier);
Dmitri Gribenko309856a2012-08-20 23:39:06 +000080 MatchFinder Finder;
81 Finder.addMatcher(NodeMatch, &Printer);
Ahmed Charlesb8984322014-03-07 20:03:18 +000082 std::unique_ptr<FrontendActionFactory> Factory(
83 newFrontendActionFactory(&Finder));
Dmitri Gribenko309856a2012-08-20 23:39:06 +000084
Fariborz Jahaniane0586a52012-10-18 19:12:17 +000085 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args, FileName))
Saleem Abdulrasool8a8454b2014-01-25 20:04:44 +000086 return testing::AssertionFailure()
87 << "Parsing error in \"" << Code.str() << "\"";
Dmitri Gribenko309856a2012-08-20 23:39:06 +000088
89 if (Printer.getNumFoundDecls() == 0)
90 return testing::AssertionFailure()
91 << "Matcher didn't find any declarations";
92
93 if (Printer.getNumFoundDecls() > 1)
94 return testing::AssertionFailure()
95 << "Matcher should match only one declaration "
96 "(found " << Printer.getNumFoundDecls() << ")";
97
98 if (Printer.getPrinted() != ExpectedPrinted)
99 return ::testing::AssertionFailure()
Saleem Abdulrasool8a8454b2014-01-25 20:04:44 +0000100 << "Expected \"" << ExpectedPrinted.str() << "\", "
101 "got \"" << Printer.getPrinted().str() << "\"";
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000102
103 return ::testing::AssertionSuccess();
104}
105
Serge Pavlov842022a2017-11-23 05:38:20 +0000106::testing::AssertionResult
107PrintedDeclCXX98Matches(StringRef Code, StringRef DeclName,
108 StringRef ExpectedPrinted,
109 PrintingPolicyModifier PolicyModifier = nullptr) {
Dmitri Gribenko454a43c2012-08-31 03:23:26 +0000110 std::vector<std::string> Args(1, "-std=c++98");
Benjamin Krameradcd0262020-01-28 20:23:46 +0100111 return PrintedDeclMatches(
112 Code, Args, namedDecl(hasName(std::string(DeclName))).bind("id"),
113 ExpectedPrinted, "input.cc", PolicyModifier);
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000114}
115
Alex Lorenz35019db2017-11-16 01:28:25 +0000116::testing::AssertionResult
117PrintedDeclCXX98Matches(StringRef Code, const DeclarationMatcher &NodeMatch,
118 StringRef ExpectedPrinted,
119 PrintingPolicyModifier PolicyModifier = nullptr) {
Dmitri Gribenko454a43c2012-08-31 03:23:26 +0000120 std::vector<std::string> Args(1, "-std=c++98");
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000121 return PrintedDeclMatches(Code,
Dmitri Gribenko454a43c2012-08-31 03:23:26 +0000122 Args,
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000123 NodeMatch,
Fariborz Jahaniane0586a52012-10-18 19:12:17 +0000124 ExpectedPrinted,
Alex Lorenz35019db2017-11-16 01:28:25 +0000125 "input.cc",
126 PolicyModifier);
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000127}
128
129::testing::AssertionResult PrintedDeclCXX11Matches(StringRef Code,
130 StringRef DeclName,
131 StringRef ExpectedPrinted) {
Dmitri Gribenko454a43c2012-08-31 03:23:26 +0000132 std::vector<std::string> Args(1, "-std=c++11");
Benjamin Krameradcd0262020-01-28 20:23:46 +0100133 return PrintedDeclMatches(
134 Code, Args, namedDecl(hasName(std::string(DeclName))).bind("id"),
135 ExpectedPrinted, "input.cc");
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000136}
137
138::testing::AssertionResult PrintedDeclCXX11Matches(
139 StringRef Code,
140 const DeclarationMatcher &NodeMatch,
141 StringRef ExpectedPrinted) {
Dmitri Gribenko454a43c2012-08-31 03:23:26 +0000142 std::vector<std::string> Args(1, "-std=c++11");
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000143 return PrintedDeclMatches(Code,
Dmitri Gribenko454a43c2012-08-31 03:23:26 +0000144 Args,
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000145 NodeMatch,
Fariborz Jahaniane0586a52012-10-18 19:12:17 +0000146 ExpectedPrinted,
147 "input.cc");
148}
149
NAKAMURA Takumi7d2da0b2014-02-16 10:16:09 +0000150::testing::AssertionResult PrintedDeclCXX11nonMSCMatches(
151 StringRef Code,
152 const DeclarationMatcher &NodeMatch,
153 StringRef ExpectedPrinted) {
154 std::vector<std::string> Args(1, "-std=c++11");
155 Args.push_back("-fno-delayed-template-parsing");
156 return PrintedDeclMatches(Code,
157 Args,
158 NodeMatch,
159 ExpectedPrinted,
160 "input.cc");
161}
162
David Majnemer8423df92015-06-05 22:40:53 +0000163::testing::AssertionResult
164PrintedDeclCXX1ZMatches(StringRef Code, const DeclarationMatcher &NodeMatch,
165 StringRef ExpectedPrinted) {
166 std::vector<std::string> Args(1, "-std=c++1z");
167 return PrintedDeclMatches(Code,
168 Args,
169 NodeMatch,
170 ExpectedPrinted,
171 "input.cc");
172}
173
Fariborz Jahaniane0586a52012-10-18 19:12:17 +0000174::testing::AssertionResult PrintedDeclObjCMatches(
175 StringRef Code,
176 const DeclarationMatcher &NodeMatch,
177 StringRef ExpectedPrinted) {
178 std::vector<std::string> Args(1, "");
179 return PrintedDeclMatches(Code,
180 Args,
181 NodeMatch,
182 ExpectedPrinted,
183 "input.m");
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000184}
185
186} // unnamed namespace
187
Dmitri Gribenko5ea34fc2014-03-03 13:21:00 +0000188TEST(DeclPrinter, TestTypedef1) {
189 ASSERT_TRUE(PrintedDeclCXX98Matches(
190 "typedef int A;",
191 "A",
192 "typedef int A"));
193 // Should be: with semicolon
194}
195
196TEST(DeclPrinter, TestTypedef2) {
197 ASSERT_TRUE(PrintedDeclCXX98Matches(
198 "typedef const char *A;",
199 "A",
200 "typedef const char *A"));
201 // Should be: with semicolon
202}
203
204TEST(DeclPrinter, TestTypedef3) {
205 ASSERT_TRUE(PrintedDeclCXX98Matches(
206 "template <typename Y> class X {};"
207 "typedef X<int> A;",
208 "A",
209 "typedef X<int> A"));
210 // Should be: with semicolon
211}
212
213TEST(DeclPrinter, TestTypedef4) {
214 ASSERT_TRUE(PrintedDeclCXX98Matches(
215 "namespace X { class Y {}; }"
216 "typedef X::Y A;",
217 "A",
218 "typedef X::Y A"));
219 // Should be: with semicolon
220}
221
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000222TEST(DeclPrinter, TestNamespace1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000223 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000224 "namespace A { int B; }",
225 "A",
226 "namespace A {\n}"));
227 // Should be: with { ... }
228}
229
230TEST(DeclPrinter, TestNamespace2) {
231 ASSERT_TRUE(PrintedDeclCXX11Matches(
232 "inline namespace A { int B; }",
233 "A",
234 "inline namespace A {\n}"));
235 // Should be: with { ... }
236}
237
238TEST(DeclPrinter, TestNamespaceAlias1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000239 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000240 "namespace Z { }"
241 "namespace A = Z;",
242 "A",
243 "namespace A = Z"));
244 // Should be: with semicolon
245}
246
247TEST(DeclPrinter, TestNamespaceAlias2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000248 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000249 "namespace X { namespace Y {} }"
250 "namespace A = X::Y;",
251 "A",
252 "namespace A = X::Y"));
253 // Should be: with semicolon
254}
255
256TEST(DeclPrinter, TestCXXRecordDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000257 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000258 "class A { int a; };",
259 "A",
Serge Pavlova67a4d22016-11-10 08:49:37 +0000260 "class A {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000261}
262
263TEST(DeclPrinter, TestCXXRecordDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000264 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000265 "struct A { int a; };",
266 "A",
Serge Pavlova67a4d22016-11-10 08:49:37 +0000267 "struct A {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000268}
269
270TEST(DeclPrinter, TestCXXRecordDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000271 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000272 "union A { int a; };",
273 "A",
Serge Pavlova67a4d22016-11-10 08:49:37 +0000274 "union A {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000275}
276
277TEST(DeclPrinter, TestCXXRecordDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000278 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000279 "class Z { int a; };"
280 "class A : Z { int b; };",
281 "A",
Serge Pavlova67a4d22016-11-10 08:49:37 +0000282 "class A : Z {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000283}
284
285TEST(DeclPrinter, TestCXXRecordDecl5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000286 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000287 "struct Z { int a; };"
288 "struct A : Z { int b; };",
289 "A",
Serge Pavlova67a4d22016-11-10 08:49:37 +0000290 "struct A : Z {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000291}
292
293TEST(DeclPrinter, TestCXXRecordDecl6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000294 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000295 "class Z { int a; };"
296 "class A : public Z { int b; };",
297 "A",
Serge Pavlova67a4d22016-11-10 08:49:37 +0000298 "class A : public Z {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000299}
300
301TEST(DeclPrinter, TestCXXRecordDecl7) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000302 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000303 "class Z { int a; };"
304 "class A : protected Z { int b; };",
305 "A",
Serge Pavlova67a4d22016-11-10 08:49:37 +0000306 "class A : protected Z {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000307}
308
309TEST(DeclPrinter, TestCXXRecordDecl8) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000310 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000311 "class Z { int a; };"
312 "class A : private Z { int b; };",
313 "A",
Serge Pavlova67a4d22016-11-10 08:49:37 +0000314 "class A : private Z {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000315}
316
317TEST(DeclPrinter, TestCXXRecordDecl9) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000318 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000319 "class Z { int a; };"
320 "class A : virtual Z { int b; };",
321 "A",
Serge Pavlova67a4d22016-11-10 08:49:37 +0000322 "class A : virtual Z {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000323}
324
325TEST(DeclPrinter, TestCXXRecordDecl10) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000326 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000327 "class Z { int a; };"
328 "class A : virtual public Z { int b; };",
329 "A",
Serge Pavlova67a4d22016-11-10 08:49:37 +0000330 "class A : virtual public Z {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000331}
332
333TEST(DeclPrinter, TestCXXRecordDecl11) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000334 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000335 "class Z { int a; };"
336 "class Y : virtual public Z { int b; };"
337 "class A : virtual public Z, private Y { int c; };",
338 "A",
Serge Pavlova67a4d22016-11-10 08:49:37 +0000339 "class A : virtual public Z, private Y {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000340}
341
342TEST(DeclPrinter, TestFunctionDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000343 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000344 "void A();",
345 "A",
346 "void A()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000347}
348
Serge Pavlov842022a2017-11-23 05:38:20 +0000349TEST(DeclPrinter, TestFreeFunctionDecl_FullyQualifiedName) {
350 ASSERT_TRUE(PrintedDeclCXX98Matches(
351 "void A();",
352 "A",
353 "void A()",
354 [](PrintingPolicy &Policy){ Policy.FullyQualifiedName = true; }));
355}
356
357TEST(DeclPrinter, TestFreeFunctionDeclInNamespace_FullyQualifiedName) {
358 ASSERT_TRUE(PrintedDeclCXX98Matches(
359 "namespace X { void A(); };",
360 "A",
361 "void X::A()",
362 [](PrintingPolicy &Policy){ Policy.FullyQualifiedName = true; }));
363}
364
365TEST(DeclPrinter, TestMemberFunction_FullyQualifiedName) {
366 ASSERT_TRUE(PrintedDeclCXX98Matches(
367 "struct X { void A(); };",
368 "A",
369 "void X::A()",
370 [](PrintingPolicy &Policy){ Policy.FullyQualifiedName = true; }));
371}
372
373TEST(DeclPrinter, TestMemberFunctionInNamespace_FullyQualifiedName) {
374 ASSERT_TRUE(PrintedDeclCXX98Matches(
375 "namespace Z { struct X { void A(); }; }",
376 "A",
377 "void Z::X::A()",
378 [](PrintingPolicy &Policy){ Policy.FullyQualifiedName = true; }));
379}
380
381TEST(DeclPrinter, TestMemberFunctionOutside_FullyQualifiedName) {
382 ASSERT_TRUE(PrintedDeclCXX98Matches(
383 "struct X { void A(); };"
384 "void X::A() {}",
385 functionDecl(hasName("A"), isDefinition()).bind("id"),
386 "void X::A()",
387 [](PrintingPolicy &Policy){ Policy.FullyQualifiedName = true; }));
388}
389
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000390TEST(DeclPrinter, TestFunctionDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000391 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000392 "void A() {}",
393 "A",
394 "void A()"));
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000395}
396
397TEST(DeclPrinter, TestFunctionDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000398 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000399 "void Z();"
400 "void A() { Z(); }",
401 "A",
402 "void A()"));
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000403}
404
405TEST(DeclPrinter, TestFunctionDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000406 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000407 "extern void A();",
408 "A",
409 "extern void A()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000410}
411
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000412TEST(DeclPrinter, TestFunctionDecl5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000413 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000414 "static void A();",
415 "A",
416 "static void A()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000417}
418
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000419TEST(DeclPrinter, TestFunctionDecl6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000420 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000421 "inline void A();",
422 "A",
423 "inline void A()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000424}
425
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000426TEST(DeclPrinter, TestFunctionDecl7) {
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000427 ASSERT_TRUE(PrintedDeclCXX11Matches(
428 "constexpr int A(int a);",
429 "A",
Benjamin Kramer2907b082014-02-25 18:49:49 +0000430 "constexpr int A(int a)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000431}
432
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000433TEST(DeclPrinter, TestFunctionDecl8) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000434 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000435 "void A(int a);",
436 "A",
437 "void A(int a)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000438}
439
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000440TEST(DeclPrinter, TestFunctionDecl9) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000441 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000442 "void A(...);",
443 "A",
444 "void A(...)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000445}
446
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000447TEST(DeclPrinter, TestFunctionDecl10) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000448 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000449 "void A(int a, ...);",
450 "A",
451 "void A(int a, ...)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000452}
453
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000454TEST(DeclPrinter, TestFunctionDecl11) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000455 ASSERT_TRUE(PrintedDeclCXX98Matches(
David Majnemerd4328de2014-01-14 08:18:49 +0000456 "typedef long ssize_t;"
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000457 "typedef int *pInt;"
David Majnemerd4328de2014-01-14 08:18:49 +0000458 "void A(int a, pInt b, ssize_t c);",
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000459 "A",
David Majnemerd4328de2014-01-14 08:18:49 +0000460 "void A(int a, pInt b, ssize_t c)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000461}
462
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000463TEST(DeclPrinter, TestFunctionDecl12) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000464 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000465 "void A(int a, int b = 0);",
466 "A",
467 "void A(int a, int b = 0)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000468}
469
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000470TEST(DeclPrinter, TestFunctionDecl13) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000471 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000472 "void (*A(int a))(int b);",
473 "A",
474 "void (*A(int a))(int)"));
Serge Pavlova67a4d22016-11-10 08:49:37 +0000475 // Should be: with parameter name (?)
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000476}
477
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000478TEST(DeclPrinter, TestFunctionDecl14) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000479 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000480 "template<typename T>"
481 "void A(T t) { }"
482 "template<>"
483 "void A(int N) { }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000484 functionDecl(hasName("A"), isExplicitTemplateSpecialization()).bind("id"),
Serge Pavlova67a4d22016-11-10 08:49:37 +0000485 "template<> void A<int>(int N)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000486}
487
488
489TEST(DeclPrinter, TestCXXConstructorDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000490 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000491 "struct A {"
492 " A();"
493 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000494 cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000495 "A()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000496}
497
498TEST(DeclPrinter, TestCXXConstructorDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000499 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000500 "struct A {"
501 " A(int a);"
502 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000503 cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000504 "A(int a)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000505}
506
507TEST(DeclPrinter, TestCXXConstructorDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000508 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000509 "struct A {"
510 " A(const A &a);"
511 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000512 cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000513 "A(const A &a)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000514}
515
516TEST(DeclPrinter, TestCXXConstructorDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000517 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000518 "struct A {"
519 " A(const A &a, int = 0);"
520 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000521 cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000522 "A(const A &a, int = 0)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000523}
524
Alex Lorenz35019db2017-11-16 01:28:25 +0000525TEST(DeclPrinter, TestCXXConstructorDeclWithMemberInitializer) {
526 ASSERT_TRUE(PrintedDeclCXX98Matches(
527 "struct A {"
528 " int m;"
529 " A() : m(2) {}"
530 "};",
531 cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
532 "A()"));
533}
534
535TEST(DeclPrinter, TestCXXConstructorDeclWithMemberInitializer_NoTerseOutput) {
536 ASSERT_TRUE(PrintedDeclCXX98Matches(
537 "struct A {"
538 " int m;"
539 " A() : m(2) {}"
540 "};",
541 cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
542 "A() : m(2) {\n}\n",
543 [](PrintingPolicy &Policy){ Policy.TerseOutput = false; }));
544}
545
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000546TEST(DeclPrinter, TestCXXConstructorDecl5) {
547 ASSERT_TRUE(PrintedDeclCXX11Matches(
548 "struct A {"
549 " A(const A &&a);"
550 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000551 cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000552 "A(const A &&a)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000553}
554
555TEST(DeclPrinter, TestCXXConstructorDecl6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000556 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000557 "struct A {"
558 " explicit A(int a);"
559 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000560 cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000561 "explicit A(int a)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000562}
563
564TEST(DeclPrinter, TestCXXConstructorDecl7) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000565 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000566 "struct A {"
567 " constexpr A();"
568 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000569 cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer2907b082014-02-25 18:49:49 +0000570 "constexpr A()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000571}
572
573TEST(DeclPrinter, TestCXXConstructorDecl8) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000574 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000575 "struct A {"
576 " A() = default;"
577 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000578 cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
Richard Smithbd305122012-12-11 01:14:52 +0000579 "A() = default"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000580}
581
582TEST(DeclPrinter, TestCXXConstructorDecl9) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000583 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000584 "struct A {"
585 " A() = delete;"
586 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000587 cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000588 "A() = delete"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000589}
590
Dmitri Gribenko340c0f62012-08-24 00:26:25 +0000591TEST(DeclPrinter, TestCXXConstructorDecl10) {
592 ASSERT_TRUE(PrintedDeclCXX11Matches(
593 "template<typename... T>"
594 "struct A {"
595 " A(const A &a);"
596 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000597 cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
Fariborz Jahanian14ef4792012-12-05 19:54:11 +0000598 "A<T...>(const A<T...> &a)"));
Dmitri Gribenko340c0f62012-08-24 00:26:25 +0000599}
600
601TEST(DeclPrinter, TestCXXConstructorDecl11) {
NAKAMURA Takumi7d2da0b2014-02-16 10:16:09 +0000602 ASSERT_TRUE(PrintedDeclCXX11nonMSCMatches(
Dmitri Gribenko340c0f62012-08-24 00:26:25 +0000603 "template<typename... T>"
604 "struct A : public T... {"
605 " A(T&&... ts) : T(ts)... {}"
606 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000607 cxxConstructorDecl(ofClass(hasName("A"))).bind("id"),
Alex Lorenz35019db2017-11-16 01:28:25 +0000608 "A<T...>(T &&...ts)"));
Dmitri Gribenko340c0f62012-08-24 00:26:25 +0000609}
Dmitri Gribenko340c0f62012-08-24 00:26:25 +0000610
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000611TEST(DeclPrinter, TestCXXDestructorDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000612 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000613 "struct A {"
614 " ~A();"
615 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000616 cxxDestructorDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer2907b082014-02-25 18:49:49 +0000617 "~A()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000618}
619
620TEST(DeclPrinter, TestCXXDestructorDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000621 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000622 "struct A {"
623 " virtual ~A();"
624 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000625 cxxDestructorDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer2907b082014-02-25 18:49:49 +0000626 "virtual ~A()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000627}
628
629TEST(DeclPrinter, TestCXXConversionDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000630 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000631 "struct A {"
632 " operator int();"
633 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000634 cxxMethodDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000635 "operator int()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000636}
637
638TEST(DeclPrinter, TestCXXConversionDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000639 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000640 "struct A {"
641 " operator bool();"
642 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000643 cxxMethodDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000644 "operator bool()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000645}
646
647TEST(DeclPrinter, TestCXXConversionDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000648 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000649 "struct Z {};"
650 "struct A {"
651 " operator Z();"
652 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000653 cxxMethodDecl(ofClass(hasName("A"))).bind("id"),
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000654 "operator Z()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000655}
656
657TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000658 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000659 "namespace std { typedef decltype(sizeof(int)) size_t; }"
660 "struct Z {"
661 " void *operator new(std::size_t);"
662 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000663 cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000664 "void *operator new(std::size_t)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000665}
666
667TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000668 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000669 "namespace std { typedef decltype(sizeof(int)) size_t; }"
670 "struct Z {"
671 " void *operator new[](std::size_t);"
672 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000673 cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000674 "void *operator new[](std::size_t)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000675}
676
677TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000678 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000679 "struct Z {"
680 " void operator delete(void *);"
681 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000682 cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000683 "void operator delete(void *) noexcept"));
Serge Pavlova67a4d22016-11-10 08:49:37 +0000684 // Should be: without noexcept?
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000685}
686
687TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000688 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000689 "struct Z {"
690 " void operator delete(void *);"
691 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000692 cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000693 "void operator delete(void *)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000694}
695
696TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000697 ASSERT_TRUE(PrintedDeclCXX11Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000698 "struct Z {"
699 " void operator delete[](void *);"
700 "};",
Aaron Ballman512fb642015-09-17 13:30:52 +0000701 cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000702 "void operator delete[](void *) noexcept"));
Serge Pavlova67a4d22016-11-10 08:49:37 +0000703 // Should be: without noexcept?
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000704}
705
706TEST(DeclPrinter, TestCXXMethodDecl_Operator1) {
707 const char *OperatorNames[] = {
708 "+", "-", "*", "/", "%", "^", "&", "|",
709 "=", "<", ">", "+=", "-=", "*=", "/=", "%=",
710 "^=", "&=", "|=", "<<", ">>", ">>=", "<<=", "==", "!=",
711 "<=", ">=", "&&", "||", ",", "->*",
712 "()", "[]"
713 };
714
715 for (unsigned i = 0, e = llvm::array_lengthof(OperatorNames); i != e; ++i) {
716 SmallString<128> Code;
717 Code.append("struct Z { void operator");
718 Code.append(OperatorNames[i]);
719 Code.append("(Z z); };");
720
721 SmallString<128> Expected;
722 Expected.append("void operator");
723 Expected.append(OperatorNames[i]);
724 Expected.append("(Z z)");
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000725
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000726 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000727 Code,
Aaron Ballman512fb642015-09-17 13:30:52 +0000728 cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000729 Expected));
730 }
731}
732
733TEST(DeclPrinter, TestCXXMethodDecl_Operator2) {
734 const char *OperatorNames[] = {
735 "~", "!", "++", "--", "->"
736 };
737
738 for (unsigned i = 0, e = llvm::array_lengthof(OperatorNames); i != e; ++i) {
739 SmallString<128> Code;
740 Code.append("struct Z { void operator");
741 Code.append(OperatorNames[i]);
742 Code.append("(); };");
743
744 SmallString<128> Expected;
745 Expected.append("void operator");
746 Expected.append(OperatorNames[i]);
747 Expected.append("()");
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000748
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000749 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000750 Code,
Aaron Ballman512fb642015-09-17 13:30:52 +0000751 cxxMethodDecl(ofClass(hasName("Z"))).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000752 Expected));
753 }
754}
755
756TEST(DeclPrinter, TestCXXMethodDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000757 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000758 "struct Z {"
759 " void A(int a);"
760 "};",
761 "A",
762 "void A(int a)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000763}
764
765TEST(DeclPrinter, TestCXXMethodDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000766 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000767 "struct Z {"
768 " virtual void A(int a);"
769 "};",
770 "A",
771 "virtual void A(int a)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000772}
773
774TEST(DeclPrinter, TestCXXMethodDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000775 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000776 "struct Z {"
777 " virtual void A(int a);"
778 "};"
779 "struct ZZ : Z {"
780 " void A(int a);"
781 "};",
782 "ZZ::A",
783 "void A(int a)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000784 // TODO: should we print "virtual"?
785}
786
787TEST(DeclPrinter, TestCXXMethodDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000788 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000789 "struct Z {"
790 " inline void A(int a);"
791 "};",
792 "A",
793 "inline void A(int a)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000794}
795
796TEST(DeclPrinter, TestCXXMethodDecl5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000797 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000798 "struct Z {"
799 " virtual void A(int a) = 0;"
800 "};",
801 "A",
802 "virtual void A(int a) = 0"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000803}
804
805TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000806 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000807 "struct Z {"
808 " void A(int a) const;"
809 "};",
810 "A",
811 "void A(int a) const"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000812}
813
814TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000815 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000816 "struct Z {"
817 " void A(int a) volatile;"
818 "};",
819 "A",
820 "void A(int a) volatile"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000821}
822
823TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000824 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000825 "struct Z {"
826 " void A(int a) const volatile;"
827 "};",
828 "A",
829 "void A(int a) const volatile"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000830}
831
832TEST(DeclPrinter, TestCXXMethodDecl_RefQualifier1) {
833 ASSERT_TRUE(PrintedDeclCXX11Matches(
834 "struct Z {"
835 " void A(int a) &;"
836 "};",
837 "A",
Benjamin Kramer2907b082014-02-25 18:49:49 +0000838 "void A(int a) &"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000839}
840
841TEST(DeclPrinter, TestCXXMethodDecl_RefQualifier2) {
842 ASSERT_TRUE(PrintedDeclCXX11Matches(
843 "struct Z {"
844 " void A(int a) &&;"
845 "};",
846 "A",
Benjamin Kramer2907b082014-02-25 18:49:49 +0000847 "void A(int a) &&"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000848}
849
850TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000851 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000852 "struct Z {"
853 " void A(int a) throw();"
854 "};",
855 "A",
856 "void A(int a) throw()"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000857}
858
859TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000860 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000861 "struct Z {"
862 " void A(int a) throw(int);"
863 "};",
864 "A",
865 "void A(int a) throw(int)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000866}
867
868TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000869 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000870 "class ZZ {};"
871 "struct Z {"
872 " void A(int a) throw(ZZ, int);"
873 "};",
874 "A",
875 "void A(int a) throw(ZZ, int)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000876}
877
878TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification4) {
879 ASSERT_TRUE(PrintedDeclCXX11Matches(
880 "struct Z {"
881 " void A(int a) noexcept;"
882 "};",
883 "A",
884 "void A(int a) noexcept"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000885}
886
887TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification5) {
888 ASSERT_TRUE(PrintedDeclCXX11Matches(
889 "struct Z {"
890 " void A(int a) noexcept(true);"
891 "};",
892 "A",
893 "void A(int a) noexcept(trueA(int a) noexcept(true)"));
894 // WRONG; Should be: "void A(int a) noexcept(true);"
895}
896
897TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification6) {
898 ASSERT_TRUE(PrintedDeclCXX11Matches(
899 "struct Z {"
900 " void A(int a) noexcept(1 < 2);"
901 "};",
902 "A",
903 "void A(int a) noexcept(1 < 2A(int a) noexcept(1 < 2)"));
904 // WRONG; Should be: "void A(int a) noexcept(1 < 2);"
905}
906
907TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification7) {
908 ASSERT_TRUE(PrintedDeclCXX11Matches(
909 "template<int N>"
910 "struct Z {"
911 " void A(int a) noexcept(N < 2);"
912 "};",
913 "A",
914 "void A(int a) noexcept(N < 2A(int a) noexcept(N < 2)"));
915 // WRONG; Should be: "void A(int a) noexcept(N < 2);"
916}
917
918TEST(DeclPrinter, TestVarDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000919 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000920 "char *const (*(*A)[5])(int);",
921 "A",
922 "char *const (*(*A)[5])(int)"));
923 // Should be: with semicolon
924}
925
926TEST(DeclPrinter, TestVarDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000927 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000928 "void (*A)() throw(int);",
929 "A",
930 "void (*A)() throw(int)"));
931 // Should be: with semicolon
932}
933
934TEST(DeclPrinter, TestVarDecl3) {
935 ASSERT_TRUE(PrintedDeclCXX11Matches(
936 "void (*A)() noexcept;",
937 "A",
938 "void (*A)() noexcept"));
939 // Should be: with semicolon
940}
941
942TEST(DeclPrinter, TestFieldDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000943 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000944 "template<typename T>"
945 "struct Z { T A; };",
946 "A",
947 "T A"));
948 // Should be: with semicolon
949}
950
951TEST(DeclPrinter, TestFieldDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000952 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000953 "template<int N>"
954 "struct Z { int A[N]; };",
955 "A",
956 "int A[N]"));
957 // Should be: with semicolon
958}
959
960TEST(DeclPrinter, TestClassTemplateDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000961 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000962 "template<typename T>"
963 "struct A { T a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000964 classTemplateDecl(hasName("A")).bind("id"),
Serge Pavlova67a4d22016-11-10 08:49:37 +0000965 "template <typename T> struct A {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000966}
967
968TEST(DeclPrinter, TestClassTemplateDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000969 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000970 "template<typename T = int>"
971 "struct A { T a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000972 classTemplateDecl(hasName("A")).bind("id"),
Serge Pavlova67a4d22016-11-10 08:49:37 +0000973 "template <typename T = int> struct A {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000974}
975
976TEST(DeclPrinter, TestClassTemplateDecl3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000977 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000978 "template<class T>"
979 "struct A { T a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000980 classTemplateDecl(hasName("A")).bind("id"),
Serge Pavlova67a4d22016-11-10 08:49:37 +0000981 "template <class T> struct A {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000982}
983
984TEST(DeclPrinter, TestClassTemplateDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000985 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000986 "template<typename T, typename U>"
987 "struct A { T a; U b; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000988 classTemplateDecl(hasName("A")).bind("id"),
Serge Pavlova67a4d22016-11-10 08:49:37 +0000989 "template <typename T, typename U> struct A {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000990}
991
992TEST(DeclPrinter, TestClassTemplateDecl5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +0000993 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000994 "template<int N>"
995 "struct A { int a[N]; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000996 classTemplateDecl(hasName("A")).bind("id"),
Serge Pavlova67a4d22016-11-10 08:49:37 +0000997 "template <int N> struct A {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000998}
999
1000TEST(DeclPrinter, TestClassTemplateDecl6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001001 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001002 "template<int N = 42>"
1003 "struct A { int a[N]; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001004 classTemplateDecl(hasName("A")).bind("id"),
Serge Pavlova67a4d22016-11-10 08:49:37 +00001005 "template <int N = 42> struct A {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001006}
1007
1008TEST(DeclPrinter, TestClassTemplateDecl7) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001009 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001010 "typedef int MyInt;"
1011 "template<MyInt N>"
1012 "struct A { int a[N]; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001013 classTemplateDecl(hasName("A")).bind("id"),
Serge Pavlova67a4d22016-11-10 08:49:37 +00001014 "template <MyInt N> struct A {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001015}
1016
1017TEST(DeclPrinter, TestClassTemplateDecl8) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001018 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001019 "template<template<typename U> class T> struct A { };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001020 classTemplateDecl(hasName("A")).bind("id"),
Serge Pavlova67a4d22016-11-10 08:49:37 +00001021 "template <template <typename U> class T> struct A {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001022}
1023
1024TEST(DeclPrinter, TestClassTemplateDecl9) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001025 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001026 "template<typename T> struct Z { };"
1027 "template<template<typename U> class T = Z> struct A { };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001028 classTemplateDecl(hasName("A")).bind("id"),
Serge Pavlova67a4d22016-11-10 08:49:37 +00001029 "template <template <typename U> class T> struct A {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001030}
1031
1032TEST(DeclPrinter, TestClassTemplateDecl10) {
1033 ASSERT_TRUE(PrintedDeclCXX11Matches(
1034 "template<typename... T>"
1035 "struct A { int a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001036 classTemplateDecl(hasName("A")).bind("id"),
Serge Pavlova67a4d22016-11-10 08:49:37 +00001037 "template <typename ...T> struct A {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001038}
1039
1040TEST(DeclPrinter, TestClassTemplateDecl11) {
1041 ASSERT_TRUE(PrintedDeclCXX11Matches(
1042 "template<typename... T>"
1043 "struct A : public T... { int a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001044 classTemplateDecl(hasName("A")).bind("id"),
Serge Pavlova67a4d22016-11-10 08:49:37 +00001045 "template <typename ...T> struct A : public T... {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001046}
1047
1048TEST(DeclPrinter, TestClassTemplatePartialSpecializationDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001049 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001050 "template<typename T, typename U>"
1051 "struct A { T a; U b; };"
1052 "template<typename T>"
1053 "struct A<T, int> { T a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001054 classTemplateSpecializationDecl().bind("id"),
Serge Pavlova67a4d22016-11-10 08:49:37 +00001055 "template <typename T> struct A<T, int> {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001056}
1057
1058TEST(DeclPrinter, TestClassTemplatePartialSpecializationDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001059 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001060 "template<typename T>"
1061 "struct A { T a; };"
1062 "template<typename T>"
1063 "struct A<T *> { T a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001064 classTemplateSpecializationDecl().bind("id"),
Sam McCall87054ec2019-11-14 14:16:14 +01001065 "template <typename T> struct A<T *> {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001066}
1067
1068TEST(DeclPrinter, TestClassTemplateSpecializationDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001069 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001070 "template<typename T>"
1071 "struct A { T a; };"
1072 "template<>"
1073 "struct A<int> { int a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001074 classTemplateSpecializationDecl().bind("id"),
Serge Pavlova67a4d22016-11-10 08:49:37 +00001075 "template<> struct A<int> {}"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001076}
1077
1078TEST(DeclPrinter, TestFunctionTemplateDecl1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001079 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001080 "template<typename T>"
1081 "void A(T &t);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001082 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001083 "template <typename T> void A(T &t)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001084}
1085
1086TEST(DeclPrinter, TestFunctionTemplateDecl2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001087 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001088 "template<typename T>"
1089 "void A(T &t) { }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001090 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +00001091 "template <typename T> void A(T &t)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001092}
1093
1094TEST(DeclPrinter, TestFunctionTemplateDecl3) {
1095 ASSERT_TRUE(PrintedDeclCXX11Matches(
1096 "template<typename... T>"
1097 "void A(T... a);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001098 functionTemplateDecl(hasName("A")).bind("id"),
Richard Smitha4bb2922014-07-23 03:17:06 +00001099 "template <typename ...T> void A(T ...a)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001100}
1101
1102TEST(DeclPrinter, TestFunctionTemplateDecl4) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001103 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001104 "struct Z { template<typename T> void A(T t); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001105 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001106 "template <typename T> void A(T t)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001107}
1108
1109TEST(DeclPrinter, TestFunctionTemplateDecl5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001110 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001111 "struct Z { template<typename T> void A(T t) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001112 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +00001113 "template <typename T> void A(T t)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001114}
1115
1116TEST(DeclPrinter, TestFunctionTemplateDecl6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001117 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001118 "template<typename T >struct Z {"
1119 " template<typename U> void A(U t) {}"
1120 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001121 functionTemplateDecl(hasName("A")).bind("id"),
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +00001122 "template <typename U> void A(U t)"));
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001123}
1124
1125TEST(DeclPrinter, TestTemplateArgumentList1) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001126 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001127 "template<typename T> struct Z {};"
1128 "struct X {};"
1129 "Z<X> A;",
1130 "A",
1131 "Z<X> A"));
1132 // Should be: with semicolon
1133}
1134
1135TEST(DeclPrinter, TestTemplateArgumentList2) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001136 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001137 "template<typename T, typename U> struct Z {};"
1138 "struct X {};"
1139 "typedef int Y;"
1140 "Z<X, Y> A;",
1141 "A",
1142 "Z<X, Y> A"));
1143 // Should be: with semicolon
1144}
1145
1146TEST(DeclPrinter, TestTemplateArgumentList3) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001147 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001148 "template<typename T> struct Z {};"
1149 "template<typename T> struct X {};"
1150 "Z<X<int> > A;",
1151 "A",
1152 "Z<X<int> > A"));
1153 // Should be: with semicolon
1154}
1155
1156TEST(DeclPrinter, TestTemplateArgumentList4) {
1157 ASSERT_TRUE(PrintedDeclCXX11Matches(
1158 "template<typename T> struct Z {};"
1159 "template<typename T> struct X {};"
1160 "Z<X<int>> A;",
1161 "A",
1162 "Z<X<int> > A"));
1163 // Should be: with semicolon, without extra space in "> >"
1164}
1165
1166TEST(DeclPrinter, TestTemplateArgumentList5) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001167 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001168 "template<typename T> struct Z {};"
1169 "template<typename T> struct X { Z<T> A; };",
1170 "A",
1171 "Z<T> A"));
1172 // Should be: with semicolon
1173}
1174
1175TEST(DeclPrinter, TestTemplateArgumentList6) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001176 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001177 "template<template<typename T> class U> struct Z {};"
1178 "template<typename T> struct X {};"
1179 "Z<X> A;",
1180 "A",
1181 "Z<X> A"));
1182 // Should be: with semicolon
1183}
1184
1185TEST(DeclPrinter, TestTemplateArgumentList7) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001186 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001187 "template<template<typename T> class U> struct Z {};"
1188 "template<template<typename T> class U> struct Y {"
1189 " Z<U> A;"
1190 "};",
1191 "A",
1192 "Z<U> A"));
1193 // Should be: with semicolon
1194}
1195
1196TEST(DeclPrinter, TestTemplateArgumentList8) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001197 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001198 "template<typename T> struct Z {};"
1199 "template<template<typename T> class U> struct Y {"
1200 " Z<U<int> > A;"
1201 "};",
1202 "A",
1203 "Z<U<int> > A"));
1204 // Should be: with semicolon
1205}
1206
1207TEST(DeclPrinter, TestTemplateArgumentList9) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001208 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001209 "template<unsigned I> struct Z {};"
1210 "Z<0> A;",
1211 "A",
1212 "Z<0> A"));
1213 // Should be: with semicolon
1214}
1215
1216TEST(DeclPrinter, TestTemplateArgumentList10) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001217 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001218 "template<unsigned I> struct Z {};"
1219 "template<unsigned I> struct X { Z<I> A; };",
1220 "A",
1221 "Z<I> A"));
1222 // Should be: with semicolon
1223}
1224
1225TEST(DeclPrinter, TestTemplateArgumentList11) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001226 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001227 "template<int I> struct Z {};"
1228 "Z<42 * 10 - 420 / 1> A;",
1229 "A",
1230 "Z<42 * 10 - 420 / 1> A"));
1231 // Should be: with semicolon
1232}
1233
1234TEST(DeclPrinter, TestTemplateArgumentList12) {
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001235 ASSERT_TRUE(PrintedDeclCXX98Matches(
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001236 "template<const char *p> struct Z {};"
1237 "extern const char X[] = \"aaa\";"
1238 "Z<X> A;",
1239 "A",
1240 "Z<X> A"));
1241 // Should be: with semicolon
1242}
1243
1244TEST(DeclPrinter, TestTemplateArgumentList13) {
1245 ASSERT_TRUE(PrintedDeclCXX11Matches(
1246 "template<typename... T> struct Z {};"
1247 "template<typename... T> struct X {"
1248 " Z<T...> A;"
1249 "};",
1250 "A",
1251 "Z<T...> A"));
Richard Smitha4bb2922014-07-23 03:17:06 +00001252 // Should be: with semicolon
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001253}
1254
1255TEST(DeclPrinter, TestTemplateArgumentList14) {
1256 ASSERT_TRUE(PrintedDeclCXX11Matches(
1257 "template<typename... T> struct Z {};"
1258 "template<typename T> struct Y {};"
1259 "template<typename... T> struct X {"
1260 " Z<Y<T>...> A;"
1261 "};",
1262 "A",
1263 "Z<Y<T>...> A"));
Richard Smitha4bb2922014-07-23 03:17:06 +00001264 // Should be: with semicolon
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001265}
1266
1267TEST(DeclPrinter, TestTemplateArgumentList15) {
1268 ASSERT_TRUE(PrintedDeclCXX11Matches(
1269 "template<unsigned I> struct Z {};"
1270 "template<typename... T> struct X {"
1271 " Z<sizeof...(T)> A;"
1272 "};",
1273 "A",
1274 "Z<sizeof...(T)> A"));
Richard Smitha4bb2922014-07-23 03:17:06 +00001275 // Should be: with semicolon
Dmitri Gribenko309856a2012-08-20 23:39:06 +00001276}
Dmitri Gribenkobda79e52012-08-31 03:05:44 +00001277
David Majnemer8423df92015-06-05 22:40:53 +00001278TEST(DeclPrinter, TestStaticAssert1) {
1279 ASSERT_TRUE(PrintedDeclCXX1ZMatches(
1280 "static_assert(true);",
1281 staticAssertDecl().bind("id"),
1282 "static_assert(true)"));
1283}
1284
Fariborz Jahaniane0586a52012-10-18 19:12:17 +00001285TEST(DeclPrinter, TestObjCMethod1) {
1286 ASSERT_TRUE(PrintedDeclObjCMatches(
1287 "__attribute__((objc_root_class)) @interface X\n"
1288 "- (int)A:(id)anObject inRange:(long)range;\n"
1289 "@end\n"
1290 "@implementation X\n"
1291 "- (int)A:(id)anObject inRange:(long)range { int printThis; return 0; }\n"
1292 "@end\n",
1293 namedDecl(hasName("A:inRange:"),
1294 hasDescendant(namedDecl(hasName("printThis")))).bind("id"),
Alex Lorenzbbf4f702017-06-02 15:02:59 +00001295 "- (int)A:(id)anObject inRange:(long)range"));
Fariborz Jahaniane0586a52012-10-18 19:12:17 +00001296}
1297
Fariborz Jahanian44194492012-12-20 02:20:09 +00001298TEST(DeclPrinter, TestObjCProtocol1) {
1299 ASSERT_TRUE(PrintedDeclObjCMatches(
1300 "@protocol P1, P2;",
1301 namedDecl(hasName("P1")).bind("id"),
1302 "@protocol P1;\n"));
1303 ASSERT_TRUE(PrintedDeclObjCMatches(
1304 "@protocol P1, P2;",
1305 namedDecl(hasName("P2")).bind("id"),
1306 "@protocol P2;\n"));
1307}
1308
1309TEST(DeclPrinter, TestObjCProtocol2) {
1310 ASSERT_TRUE(PrintedDeclObjCMatches(
1311 "@protocol P2 @end"
1312 "@protocol P1<P2> @end",
1313 namedDecl(hasName("P1")).bind("id"),
1314 "@protocol P1<P2>\n@end"));
1315}