Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 1 | //===- 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 Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 26 | #include "gtest/gtest.h" |
| 27 | |
| 28 | using namespace clang; |
| 29 | using namespace ast_matchers; |
| 30 | using namespace tooling; |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | void PrintDecl(raw_ostream &Out, const ASTContext *Context, const Decl *D) { |
| 35 | PrintingPolicy Policy = Context->getPrintingPolicy(); |
Dmitri Gribenko | d1fc82e | 2012-08-21 17:36:32 +0000 | [diff] [blame] | 36 | Policy.TerseOutput = true; |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 37 | D->print(Out, Policy, /*Indentation*/ 0, /*PrintInstantiation*/ false); |
| 38 | } |
| 39 | |
| 40 | class PrintMatch : public MatchFinder::MatchCallback { |
| 41 | SmallString<1024> Printed; |
| 42 | unsigned NumFoundDecls; |
| 43 | |
| 44 | public: |
| 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 | |
| 68 | bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code, |
| 69 | ArrayRef<const char *> ClangArgs) { |
| 70 | SmallString<16> FileNameStorage; |
| 71 | StringRef FileNameRef = "input.cc"; |
| 72 | |
| 73 | std::vector<std::string> ArgVector; |
| 74 | ArgVector.push_back("clang-tool"); |
| 75 | ArgVector.push_back("-fsyntax-only"); |
| 76 | ArgVector.push_back(FileNameRef.data()); |
| 77 | for (unsigned i = 0, e = ClangArgs.size(); i != e; ++i) |
| 78 | ArgVector.push_back(ClangArgs[i]); |
| 79 | |
| 80 | FileManager Files((FileSystemOptions())); |
| 81 | ToolInvocation Invocation(ArgVector, ToolAction, &Files); |
| 82 | |
| 83 | SmallString<1024> CodeStorage; |
| 84 | Invocation.mapVirtualFile(FileNameRef, |
| 85 | Code.toNullTerminatedStringRef(CodeStorage)); |
| 86 | return Invocation.run(); |
| 87 | } |
| 88 | |
| 89 | ::testing::AssertionResult PrintedDeclMatches( |
| 90 | StringRef Code, |
| 91 | ArrayRef<const char *> ClangArgs, |
| 92 | const DeclarationMatcher &NodeMatch, |
| 93 | StringRef ExpectedPrinted) { |
| 94 | PrintMatch Printer; |
| 95 | MatchFinder Finder; |
| 96 | Finder.addMatcher(NodeMatch, &Printer); |
| 97 | OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder)); |
| 98 | |
| 99 | if (!runToolOnCode(Factory->create(), Code, ClangArgs)) |
| 100 | return testing::AssertionFailure() << "Parsing error in \"" << Code << "\""; |
| 101 | |
| 102 | if (Printer.getNumFoundDecls() == 0) |
| 103 | return testing::AssertionFailure() |
| 104 | << "Matcher didn't find any declarations"; |
| 105 | |
| 106 | if (Printer.getNumFoundDecls() > 1) |
| 107 | return testing::AssertionFailure() |
| 108 | << "Matcher should match only one declaration " |
| 109 | "(found " << Printer.getNumFoundDecls() << ")"; |
| 110 | |
| 111 | if (Printer.getPrinted() != ExpectedPrinted) |
| 112 | return ::testing::AssertionFailure() |
| 113 | << "Expected \"" << ExpectedPrinted << "\", " |
| 114 | "got \"" << Printer.getPrinted() << "\""; |
| 115 | |
| 116 | return ::testing::AssertionSuccess(); |
| 117 | } |
| 118 | |
| 119 | ::testing::AssertionResult PrintedDeclMatches(StringRef Code, |
| 120 | StringRef DeclName, |
| 121 | StringRef ExpectedPrinted) { |
| 122 | return PrintedDeclMatches(Code, |
| 123 | ArrayRef<const char *>(), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 124 | namedDecl(hasName(DeclName)).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 125 | ExpectedPrinted); |
| 126 | } |
| 127 | |
| 128 | ::testing::AssertionResult PrintedDeclMatches( |
| 129 | StringRef Code, |
| 130 | const DeclarationMatcher &NodeMatch, |
| 131 | StringRef ExpectedPrinted) { |
| 132 | return PrintedDeclMatches(Code, |
| 133 | ArrayRef<const char *>(), |
| 134 | NodeMatch, |
| 135 | ExpectedPrinted); |
| 136 | } |
| 137 | |
| 138 | ::testing::AssertionResult PrintedDeclCXX11Matches(StringRef Code, |
| 139 | StringRef DeclName, |
| 140 | StringRef ExpectedPrinted) { |
| 141 | return PrintedDeclMatches(Code, |
| 142 | ArrayRef<const char *>("-std=c++11"), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 143 | namedDecl(hasName(DeclName)).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 144 | ExpectedPrinted); |
| 145 | } |
| 146 | |
| 147 | ::testing::AssertionResult PrintedDeclCXX11Matches( |
| 148 | StringRef Code, |
| 149 | const DeclarationMatcher &NodeMatch, |
| 150 | StringRef ExpectedPrinted) { |
| 151 | return PrintedDeclMatches(Code, |
| 152 | ArrayRef<const char *>("-std=c++11"), |
| 153 | NodeMatch, |
| 154 | ExpectedPrinted); |
| 155 | } |
| 156 | |
| 157 | } // unnamed namespace |
| 158 | |
| 159 | TEST(DeclPrinter, TestNamespace1) { |
| 160 | ASSERT_TRUE(PrintedDeclMatches( |
| 161 | "namespace A { int B; }", |
| 162 | "A", |
| 163 | "namespace A {\n}")); |
| 164 | // Should be: with { ... } |
| 165 | } |
| 166 | |
| 167 | TEST(DeclPrinter, TestNamespace2) { |
| 168 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 169 | "inline namespace A { int B; }", |
| 170 | "A", |
| 171 | "inline namespace A {\n}")); |
| 172 | // Should be: with { ... } |
| 173 | } |
| 174 | |
| 175 | TEST(DeclPrinter, TestNamespaceAlias1) { |
| 176 | ASSERT_TRUE(PrintedDeclMatches( |
| 177 | "namespace Z { }" |
| 178 | "namespace A = Z;", |
| 179 | "A", |
| 180 | "namespace A = Z")); |
| 181 | // Should be: with semicolon |
| 182 | } |
| 183 | |
| 184 | TEST(DeclPrinter, TestNamespaceAlias2) { |
| 185 | ASSERT_TRUE(PrintedDeclMatches( |
| 186 | "namespace X { namespace Y {} }" |
| 187 | "namespace A = X::Y;", |
| 188 | "A", |
| 189 | "namespace A = X::Y")); |
| 190 | // Should be: with semicolon |
| 191 | } |
| 192 | |
| 193 | TEST(DeclPrinter, TestCXXRecordDecl1) { |
| 194 | ASSERT_TRUE(PrintedDeclMatches( |
| 195 | "class A { int a; };", |
| 196 | "A", |
| 197 | "class A {\n}")); |
| 198 | // Should be: with semicolon, with { ... } |
| 199 | } |
| 200 | |
| 201 | TEST(DeclPrinter, TestCXXRecordDecl2) { |
| 202 | ASSERT_TRUE(PrintedDeclMatches( |
| 203 | "struct A { int a; };", |
| 204 | "A", |
| 205 | "struct A {\n}")); |
| 206 | // Should be: with semicolon, with { ... } |
| 207 | } |
| 208 | |
| 209 | TEST(DeclPrinter, TestCXXRecordDecl3) { |
| 210 | ASSERT_TRUE(PrintedDeclMatches( |
| 211 | "union A { int a; };", |
| 212 | "A", |
| 213 | "union A {\n}")); |
| 214 | // Should be: with semicolon, with { ... } |
| 215 | } |
| 216 | |
| 217 | TEST(DeclPrinter, TestCXXRecordDecl4) { |
| 218 | ASSERT_TRUE(PrintedDeclMatches( |
| 219 | "class Z { int a; };" |
| 220 | "class A : Z { int b; };", |
| 221 | "A", |
| 222 | "class A : Z {\n}")); |
| 223 | // Should be: with semicolon, with { ... }, without two spaces |
| 224 | } |
| 225 | |
| 226 | TEST(DeclPrinter, TestCXXRecordDecl5) { |
| 227 | ASSERT_TRUE(PrintedDeclMatches( |
| 228 | "struct Z { int a; };" |
| 229 | "struct A : Z { int b; };", |
| 230 | "A", |
| 231 | "struct A : Z {\n}")); |
| 232 | // Should be: with semicolon, with { ... }, without two spaces |
| 233 | } |
| 234 | |
| 235 | TEST(DeclPrinter, TestCXXRecordDecl6) { |
| 236 | ASSERT_TRUE(PrintedDeclMatches( |
| 237 | "class Z { int a; };" |
| 238 | "class A : public Z { int b; };", |
| 239 | "A", |
| 240 | "class A : public Z {\n}")); |
| 241 | // Should be: with semicolon, with { ... } |
| 242 | } |
| 243 | |
| 244 | TEST(DeclPrinter, TestCXXRecordDecl7) { |
| 245 | ASSERT_TRUE(PrintedDeclMatches( |
| 246 | "class Z { int a; };" |
| 247 | "class A : protected Z { int b; };", |
| 248 | "A", |
| 249 | "class A : protected Z {\n}")); |
| 250 | // Should be: with semicolon, with { ... } |
| 251 | } |
| 252 | |
| 253 | TEST(DeclPrinter, TestCXXRecordDecl8) { |
| 254 | ASSERT_TRUE(PrintedDeclMatches( |
| 255 | "class Z { int a; };" |
| 256 | "class A : private Z { int b; };", |
| 257 | "A", |
| 258 | "class A : private Z {\n}")); |
| 259 | // Should be: with semicolon, with { ... } |
| 260 | } |
| 261 | |
| 262 | TEST(DeclPrinter, TestCXXRecordDecl9) { |
| 263 | ASSERT_TRUE(PrintedDeclMatches( |
| 264 | "class Z { int a; };" |
| 265 | "class A : virtual Z { int b; };", |
| 266 | "A", |
| 267 | "class A : virtual Z {\n}")); |
| 268 | // Should be: with semicolon, with { ... }, without two spaces |
| 269 | } |
| 270 | |
| 271 | TEST(DeclPrinter, TestCXXRecordDecl10) { |
| 272 | ASSERT_TRUE(PrintedDeclMatches( |
| 273 | "class Z { int a; };" |
| 274 | "class A : virtual public Z { int b; };", |
| 275 | "A", |
| 276 | "class A : virtual public Z {\n}")); |
| 277 | // Should be: with semicolon, with { ... } |
| 278 | } |
| 279 | |
| 280 | TEST(DeclPrinter, TestCXXRecordDecl11) { |
| 281 | ASSERT_TRUE(PrintedDeclMatches( |
| 282 | "class Z { int a; };" |
| 283 | "class Y : virtual public Z { int b; };" |
| 284 | "class A : virtual public Z, private Y { int c; };", |
| 285 | "A", |
| 286 | "class A : virtual public Z, private Y {\n}")); |
| 287 | // Should be: with semicolon, with { ... } |
| 288 | } |
| 289 | |
| 290 | TEST(DeclPrinter, TestFunctionDecl1) { |
| 291 | ASSERT_TRUE(PrintedDeclMatches( |
| 292 | "void A();", |
| 293 | "A", |
| 294 | "void A()")); |
| 295 | // Should be: with semicolon |
| 296 | } |
| 297 | |
| 298 | TEST(DeclPrinter, TestFunctionDecl2) { |
| 299 | ASSERT_TRUE(PrintedDeclMatches( |
Dmitri Gribenko | 2e0b8d9 | 2012-08-21 17:47:24 +0000 | [diff] [blame] | 300 | "void A() {}", |
| 301 | "A", |
| 302 | "void A()")); |
| 303 | // Should be: with semicolon |
| 304 | } |
| 305 | |
| 306 | TEST(DeclPrinter, TestFunctionDecl3) { |
| 307 | ASSERT_TRUE(PrintedDeclMatches( |
| 308 | "void Z();" |
| 309 | "void A() { Z(); }", |
| 310 | "A", |
| 311 | "void A()")); |
| 312 | // Should be: with semicolon |
| 313 | } |
| 314 | |
| 315 | TEST(DeclPrinter, TestFunctionDecl4) { |
| 316 | ASSERT_TRUE(PrintedDeclMatches( |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 317 | "extern void A();", |
| 318 | "A", |
| 319 | "extern void A()")); |
| 320 | // Should be: with semicolon |
| 321 | } |
| 322 | |
Dmitri Gribenko | 2e0b8d9 | 2012-08-21 17:47:24 +0000 | [diff] [blame] | 323 | TEST(DeclPrinter, TestFunctionDecl5) { |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 324 | ASSERT_TRUE(PrintedDeclMatches( |
| 325 | "static void A();", |
| 326 | "A", |
| 327 | "static void A()")); |
| 328 | // Should be: with semicolon |
| 329 | } |
| 330 | |
Dmitri Gribenko | 2e0b8d9 | 2012-08-21 17:47:24 +0000 | [diff] [blame] | 331 | TEST(DeclPrinter, TestFunctionDecl6) { |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 332 | ASSERT_TRUE(PrintedDeclMatches( |
| 333 | "inline void A();", |
| 334 | "A", |
| 335 | "inline void A()")); |
| 336 | // Should be: with semicolon |
| 337 | } |
| 338 | |
Dmitri Gribenko | 2e0b8d9 | 2012-08-21 17:47:24 +0000 | [diff] [blame] | 339 | TEST(DeclPrinter, TestFunctionDecl7) { |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 340 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 341 | "constexpr int A(int a);", |
| 342 | "A", |
| 343 | "int A(int a)")); |
| 344 | // WRONG; Should be: "constexpr int A(int a);" |
| 345 | } |
| 346 | |
Dmitri Gribenko | 2e0b8d9 | 2012-08-21 17:47:24 +0000 | [diff] [blame] | 347 | TEST(DeclPrinter, TestFunctionDecl8) { |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 348 | ASSERT_TRUE(PrintedDeclMatches( |
| 349 | "void A(int a);", |
| 350 | "A", |
| 351 | "void A(int a)")); |
| 352 | // Should be: with semicolon |
| 353 | } |
| 354 | |
Dmitri Gribenko | 2e0b8d9 | 2012-08-21 17:47:24 +0000 | [diff] [blame] | 355 | TEST(DeclPrinter, TestFunctionDecl9) { |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 356 | ASSERT_TRUE(PrintedDeclMatches( |
| 357 | "void A(...);", |
| 358 | "A", |
| 359 | "void A(...)")); |
| 360 | // Should be: with semicolon |
| 361 | } |
| 362 | |
Dmitri Gribenko | 2e0b8d9 | 2012-08-21 17:47:24 +0000 | [diff] [blame] | 363 | TEST(DeclPrinter, TestFunctionDecl10) { |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 364 | ASSERT_TRUE(PrintedDeclMatches( |
| 365 | "void A(int a, ...);", |
| 366 | "A", |
| 367 | "void A(int a, ...)")); |
| 368 | // Should be: with semicolon |
| 369 | } |
| 370 | |
Dmitri Gribenko | 2e0b8d9 | 2012-08-21 17:47:24 +0000 | [diff] [blame] | 371 | TEST(DeclPrinter, TestFunctionDecl11) { |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 372 | ASSERT_TRUE(PrintedDeclMatches( |
| 373 | "typedef long size_t;" |
| 374 | "typedef int *pInt;" |
| 375 | "void A(int a, pInt b, size_t c);", |
| 376 | "A", |
| 377 | "void A(int a, pInt b, size_t c)")); |
| 378 | // Should be: with semicolon |
| 379 | } |
| 380 | |
Dmitri Gribenko | 2e0b8d9 | 2012-08-21 17:47:24 +0000 | [diff] [blame] | 381 | TEST(DeclPrinter, TestFunctionDecl12) { |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 382 | ASSERT_TRUE(PrintedDeclMatches( |
| 383 | "void A(int a, int b = 0);", |
| 384 | "A", |
| 385 | "void A(int a, int b = 0)")); |
| 386 | // Should be: with semicolon |
| 387 | } |
| 388 | |
Dmitri Gribenko | 2e0b8d9 | 2012-08-21 17:47:24 +0000 | [diff] [blame] | 389 | TEST(DeclPrinter, TestFunctionDecl13) { |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 390 | ASSERT_TRUE(PrintedDeclMatches( |
| 391 | "void (*A(int a))(int b);", |
| 392 | "A", |
| 393 | "void (*A(int a))(int)")); |
| 394 | // Should be: with semicolon, with parameter name (?) |
| 395 | } |
| 396 | |
Dmitri Gribenko | 2e0b8d9 | 2012-08-21 17:47:24 +0000 | [diff] [blame] | 397 | TEST(DeclPrinter, TestFunctionDecl14) { |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 398 | ASSERT_TRUE(PrintedDeclMatches( |
| 399 | "template<typename T>" |
| 400 | "void A(T t) { }" |
| 401 | "template<>" |
| 402 | "void A(int N) { }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 403 | functionDecl(hasName("A"), isExplicitTemplateSpecialization()).bind("id"), |
Dmitri Gribenko | 2e0b8d9 | 2012-08-21 17:47:24 +0000 | [diff] [blame] | 404 | "void A(int N)")); |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 405 | // WRONG; Should be: "template <> void A(int N);")); |
| 406 | } |
| 407 | |
| 408 | |
| 409 | TEST(DeclPrinter, TestCXXConstructorDecl1) { |
| 410 | ASSERT_TRUE(PrintedDeclMatches( |
| 411 | "struct A {" |
| 412 | " A();" |
| 413 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 414 | constructorDecl(ofClass(hasName("A"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 415 | "")); |
| 416 | // WRONG; Should be: "A();" |
| 417 | } |
| 418 | |
| 419 | TEST(DeclPrinter, TestCXXConstructorDecl2) { |
| 420 | ASSERT_TRUE(PrintedDeclMatches( |
| 421 | "struct A {" |
| 422 | " A(int a);" |
| 423 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 424 | constructorDecl(ofClass(hasName("A"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 425 | "")); |
| 426 | // WRONG; Should be: "A(int a);" |
| 427 | } |
| 428 | |
| 429 | TEST(DeclPrinter, TestCXXConstructorDecl3) { |
| 430 | ASSERT_TRUE(PrintedDeclMatches( |
| 431 | "struct A {" |
| 432 | " A(const A &a);" |
| 433 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 434 | constructorDecl(ofClass(hasName("A"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 435 | "")); |
| 436 | // WRONG; Should be: "A(const A &a);" |
| 437 | } |
| 438 | |
| 439 | TEST(DeclPrinter, TestCXXConstructorDecl4) { |
| 440 | ASSERT_TRUE(PrintedDeclMatches( |
| 441 | "struct A {" |
| 442 | " A(const A &a, int = 0);" |
| 443 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 444 | constructorDecl(ofClass(hasName("A"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 445 | "")); |
| 446 | // WRONG; Should be: "A(const A &a, int = 0);" |
| 447 | } |
| 448 | |
| 449 | TEST(DeclPrinter, TestCXXConstructorDecl5) { |
| 450 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 451 | "struct A {" |
| 452 | " A(const A &&a);" |
| 453 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 454 | constructorDecl(ofClass(hasName("A"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 455 | "")); |
| 456 | // WRONG; Should be: "A(const A &&a);" |
| 457 | } |
| 458 | |
| 459 | TEST(DeclPrinter, TestCXXConstructorDecl6) { |
| 460 | ASSERT_TRUE(PrintedDeclMatches( |
| 461 | "struct A {" |
| 462 | " explicit A(int a);" |
| 463 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 464 | constructorDecl(ofClass(hasName("A"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 465 | "")); |
| 466 | // WRONG; Should be: "explicit A(int a);" |
| 467 | } |
| 468 | |
| 469 | TEST(DeclPrinter, TestCXXConstructorDecl7) { |
| 470 | ASSERT_TRUE(PrintedDeclMatches( |
| 471 | "struct A {" |
| 472 | " constexpr A();" |
| 473 | "};", |
| 474 | ArrayRef<const char *>("-std=c++11"), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 475 | constructorDecl(ofClass(hasName("A"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 476 | "")); |
| 477 | // WRONG; Should be: "constexpr A();" |
| 478 | } |
| 479 | |
| 480 | TEST(DeclPrinter, TestCXXConstructorDecl8) { |
| 481 | ASSERT_TRUE(PrintedDeclMatches( |
| 482 | "struct A {" |
| 483 | " A() = default;" |
| 484 | "};", |
| 485 | ArrayRef<const char *>("-std=c++11"), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 486 | constructorDecl(ofClass(hasName("A"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 487 | "")); |
| 488 | // WRONG; Should be: "A() = default;" |
| 489 | } |
| 490 | |
| 491 | TEST(DeclPrinter, TestCXXConstructorDecl9) { |
| 492 | ASSERT_TRUE(PrintedDeclMatches( |
| 493 | "struct A {" |
| 494 | " A() = delete;" |
| 495 | "};", |
| 496 | ArrayRef<const char *>("-std=c++11"), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 497 | constructorDecl(ofClass(hasName("A"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 498 | " = delete")); |
| 499 | // WRONG; Should be: "A() = delete;" |
| 500 | } |
| 501 | |
Dmitri Gribenko | c468424 | 2012-08-24 00:26:25 +0000 | [diff] [blame] | 502 | TEST(DeclPrinter, TestCXXConstructorDecl10) { |
| 503 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 504 | "template<typename... T>" |
| 505 | "struct A {" |
| 506 | " A(const A &a);" |
| 507 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 508 | constructorDecl(ofClass(hasName("A"))).bind("id"), |
Dmitri Gribenko | c468424 | 2012-08-24 00:26:25 +0000 | [diff] [blame] | 509 | "")); |
| 510 | // WRONG; Should be: "A(const A &a);" |
| 511 | } |
| 512 | |
NAKAMURA Takumi | 29b1f68 | 2012-08-25 00:05:56 +0000 | [diff] [blame^] | 513 | #if !defined(_MSC_VER) |
Dmitri Gribenko | c468424 | 2012-08-24 00:26:25 +0000 | [diff] [blame] | 514 | TEST(DeclPrinter, TestCXXConstructorDecl11) { |
| 515 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 516 | "template<typename... T>" |
| 517 | "struct A : public T... {" |
| 518 | " A(T&&... ts) : T(ts)... {}" |
| 519 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 520 | constructorDecl(ofClass(hasName("A"))).bind("id"), |
Dmitri Gribenko | c468424 | 2012-08-24 00:26:25 +0000 | [diff] [blame] | 521 | "A<T...>(T &&ts...) : T(ts)")); |
Dmitri Gribenko | f6ec15a | 2012-08-24 00:27:50 +0000 | [diff] [blame] | 522 | // WRONG; Should be: "A(T&&... ts) : T(ts)..." |
Dmitri Gribenko | c468424 | 2012-08-24 00:26:25 +0000 | [diff] [blame] | 523 | } |
NAKAMURA Takumi | 29b1f68 | 2012-08-25 00:05:56 +0000 | [diff] [blame^] | 524 | #endif |
Dmitri Gribenko | c468424 | 2012-08-24 00:26:25 +0000 | [diff] [blame] | 525 | |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 526 | TEST(DeclPrinter, TestCXXDestructorDecl1) { |
| 527 | ASSERT_TRUE(PrintedDeclMatches( |
| 528 | "struct A {" |
| 529 | " ~A();" |
| 530 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 531 | destructorDecl(ofClass(hasName("A"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 532 | "void ~A()")); |
| 533 | // WRONG; Should be: "~A();" |
| 534 | } |
| 535 | |
| 536 | TEST(DeclPrinter, TestCXXDestructorDecl2) { |
| 537 | ASSERT_TRUE(PrintedDeclMatches( |
| 538 | "struct A {" |
| 539 | " virtual ~A();" |
| 540 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 541 | destructorDecl(ofClass(hasName("A"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 542 | "virtual void ~A()")); |
| 543 | // WRONG; Should be: "virtual ~A();" |
| 544 | } |
| 545 | |
| 546 | TEST(DeclPrinter, TestCXXConversionDecl1) { |
| 547 | ASSERT_TRUE(PrintedDeclMatches( |
| 548 | "struct A {" |
| 549 | " operator int();" |
| 550 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 551 | methodDecl(ofClass(hasName("A"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 552 | "int operator int()")); |
| 553 | // WRONG; Should be: "operator int();" |
| 554 | } |
| 555 | |
| 556 | TEST(DeclPrinter, TestCXXConversionDecl2) { |
| 557 | ASSERT_TRUE(PrintedDeclMatches( |
| 558 | "struct A {" |
| 559 | " operator bool();" |
| 560 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 561 | methodDecl(ofClass(hasName("A"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 562 | "bool operator _Bool()")); |
| 563 | // WRONG; Should be: "operator bool();" |
| 564 | } |
| 565 | |
| 566 | TEST(DeclPrinter, TestCXXConversionDecl3) { |
| 567 | ASSERT_TRUE(PrintedDeclMatches( |
| 568 | "struct Z {};" |
| 569 | "struct A {" |
| 570 | " operator Z();" |
| 571 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 572 | methodDecl(ofClass(hasName("A"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 573 | "Z operator struct Z()")); |
| 574 | // WRONG; Should be: "operator Z();" |
| 575 | } |
| 576 | |
| 577 | TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction1) { |
| 578 | ASSERT_TRUE(PrintedDeclMatches( |
| 579 | "namespace std { typedef decltype(sizeof(int)) size_t; }" |
| 580 | "struct Z {" |
| 581 | " void *operator new(std::size_t);" |
| 582 | "};", |
| 583 | ArrayRef<const char *>("-std=c++11"), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 584 | methodDecl(ofClass(hasName("Z"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 585 | "void *operator new(std::size_t)")); |
| 586 | // Should be: with semicolon |
| 587 | } |
| 588 | |
| 589 | TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction2) { |
| 590 | ASSERT_TRUE(PrintedDeclMatches( |
| 591 | "namespace std { typedef decltype(sizeof(int)) size_t; }" |
| 592 | "struct Z {" |
| 593 | " void *operator new[](std::size_t);" |
| 594 | "};", |
| 595 | ArrayRef<const char *>("-std=c++11"), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 596 | methodDecl(ofClass(hasName("Z"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 597 | "void *operator new[](std::size_t)")); |
| 598 | // Should be: with semicolon |
| 599 | } |
| 600 | |
| 601 | TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction3) { |
| 602 | ASSERT_TRUE(PrintedDeclMatches( |
| 603 | "struct Z {" |
| 604 | " void operator delete(void *);" |
| 605 | "};", |
| 606 | ArrayRef<const char *>("-std=c++11"), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 607 | methodDecl(ofClass(hasName("Z"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 608 | "void operator delete(void *) noexcept")); |
| 609 | // Should be: with semicolon, without noexcept? |
| 610 | } |
| 611 | |
| 612 | TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction4) { |
| 613 | ASSERT_TRUE(PrintedDeclMatches( |
| 614 | "struct Z {" |
| 615 | " void operator delete(void *);" |
| 616 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 617 | methodDecl(ofClass(hasName("Z"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 618 | "void operator delete(void *)")); |
| 619 | // Should be: with semicolon |
| 620 | } |
| 621 | |
| 622 | TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction5) { |
| 623 | ASSERT_TRUE(PrintedDeclMatches( |
| 624 | "struct Z {" |
| 625 | " void operator delete[](void *);" |
| 626 | "};", |
| 627 | ArrayRef<const char *>("-std=c++11"), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 628 | methodDecl(ofClass(hasName("Z"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 629 | "void operator delete[](void *) noexcept")); |
| 630 | // Should be: with semicolon, without noexcept? |
| 631 | } |
| 632 | |
| 633 | TEST(DeclPrinter, TestCXXMethodDecl_Operator1) { |
| 634 | const char *OperatorNames[] = { |
| 635 | "+", "-", "*", "/", "%", "^", "&", "|", |
| 636 | "=", "<", ">", "+=", "-=", "*=", "/=", "%=", |
| 637 | "^=", "&=", "|=", "<<", ">>", ">>=", "<<=", "==", "!=", |
| 638 | "<=", ">=", "&&", "||", ",", "->*", |
| 639 | "()", "[]" |
| 640 | }; |
| 641 | |
| 642 | for (unsigned i = 0, e = llvm::array_lengthof(OperatorNames); i != e; ++i) { |
| 643 | SmallString<128> Code; |
| 644 | Code.append("struct Z { void operator"); |
| 645 | Code.append(OperatorNames[i]); |
| 646 | Code.append("(Z z); };"); |
| 647 | |
| 648 | SmallString<128> Expected; |
| 649 | Expected.append("void operator"); |
| 650 | Expected.append(OperatorNames[i]); |
| 651 | Expected.append("(Z z)"); |
| 652 | // Should be: with semicolon |
| 653 | |
| 654 | ASSERT_TRUE(PrintedDeclMatches( |
| 655 | Code, |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 656 | methodDecl(ofClass(hasName("Z"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 657 | Expected)); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | TEST(DeclPrinter, TestCXXMethodDecl_Operator2) { |
| 662 | const char *OperatorNames[] = { |
| 663 | "~", "!", "++", "--", "->" |
| 664 | }; |
| 665 | |
| 666 | for (unsigned i = 0, e = llvm::array_lengthof(OperatorNames); i != e; ++i) { |
| 667 | SmallString<128> Code; |
| 668 | Code.append("struct Z { void operator"); |
| 669 | Code.append(OperatorNames[i]); |
| 670 | Code.append("(); };"); |
| 671 | |
| 672 | SmallString<128> Expected; |
| 673 | Expected.append("void operator"); |
| 674 | Expected.append(OperatorNames[i]); |
| 675 | Expected.append("()"); |
| 676 | // Should be: with semicolon |
| 677 | |
| 678 | ASSERT_TRUE(PrintedDeclMatches( |
| 679 | Code, |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 680 | methodDecl(ofClass(hasName("Z"))).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 681 | Expected)); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | TEST(DeclPrinter, TestCXXMethodDecl1) { |
| 686 | ASSERT_TRUE(PrintedDeclMatches( |
| 687 | "struct Z {" |
| 688 | " void A(int a);" |
| 689 | "};", |
| 690 | "A", |
| 691 | "void A(int a)")); |
| 692 | // Should be: with semicolon |
| 693 | } |
| 694 | |
| 695 | TEST(DeclPrinter, TestCXXMethodDecl2) { |
| 696 | ASSERT_TRUE(PrintedDeclMatches( |
| 697 | "struct Z {" |
| 698 | " virtual void A(int a);" |
| 699 | "};", |
| 700 | "A", |
| 701 | "virtual void A(int a)")); |
| 702 | // Should be: with semicolon |
| 703 | } |
| 704 | |
| 705 | TEST(DeclPrinter, TestCXXMethodDecl3) { |
| 706 | ASSERT_TRUE(PrintedDeclMatches( |
| 707 | "struct Z {" |
| 708 | " virtual void A(int a);" |
| 709 | "};" |
| 710 | "struct ZZ : Z {" |
| 711 | " void A(int a);" |
| 712 | "};", |
| 713 | "ZZ::A", |
| 714 | "void A(int a)")); |
| 715 | // Should be: with semicolon |
| 716 | // TODO: should we print "virtual"? |
| 717 | } |
| 718 | |
| 719 | TEST(DeclPrinter, TestCXXMethodDecl4) { |
| 720 | ASSERT_TRUE(PrintedDeclMatches( |
| 721 | "struct Z {" |
| 722 | " inline void A(int a);" |
| 723 | "};", |
| 724 | "A", |
| 725 | "inline void A(int a)")); |
| 726 | // Should be: with semicolon |
| 727 | } |
| 728 | |
| 729 | TEST(DeclPrinter, TestCXXMethodDecl5) { |
| 730 | ASSERT_TRUE(PrintedDeclMatches( |
| 731 | "struct Z {" |
| 732 | " virtual void A(int a) = 0;" |
| 733 | "};", |
| 734 | "A", |
| 735 | "virtual void A(int a) = 0")); |
| 736 | // Should be: with semicolon |
| 737 | } |
| 738 | |
| 739 | TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier1) { |
| 740 | ASSERT_TRUE(PrintedDeclMatches( |
| 741 | "struct Z {" |
| 742 | " void A(int a) const;" |
| 743 | "};", |
| 744 | "A", |
| 745 | "void A(int a) const")); |
| 746 | // Should be: with semicolon |
| 747 | } |
| 748 | |
| 749 | TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier2) { |
| 750 | ASSERT_TRUE(PrintedDeclMatches( |
| 751 | "struct Z {" |
| 752 | " void A(int a) volatile;" |
| 753 | "};", |
| 754 | "A", |
| 755 | "void A(int a) volatile")); |
| 756 | // Should be: with semicolon |
| 757 | } |
| 758 | |
| 759 | TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier3) { |
| 760 | ASSERT_TRUE(PrintedDeclMatches( |
| 761 | "struct Z {" |
| 762 | " void A(int a) const volatile;" |
| 763 | "};", |
| 764 | "A", |
| 765 | "void A(int a) const volatile")); |
| 766 | // Should be: with semicolon |
| 767 | } |
| 768 | |
| 769 | TEST(DeclPrinter, TestCXXMethodDecl_RefQualifier1) { |
| 770 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 771 | "struct Z {" |
| 772 | " void A(int a) &;" |
| 773 | "};", |
| 774 | "A", |
| 775 | "void A(int a)")); |
| 776 | // WRONG; Should be: "void A(int a) &;" |
| 777 | } |
| 778 | |
| 779 | TEST(DeclPrinter, TestCXXMethodDecl_RefQualifier2) { |
| 780 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 781 | "struct Z {" |
| 782 | " void A(int a) &&;" |
| 783 | "};", |
| 784 | "A", |
| 785 | "void A(int a)")); |
| 786 | // WRONG; Should be: "void A(int a) &&;" |
| 787 | } |
| 788 | |
| 789 | TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification1) { |
| 790 | ASSERT_TRUE(PrintedDeclMatches( |
| 791 | "struct Z {" |
| 792 | " void A(int a) throw();" |
| 793 | "};", |
| 794 | "A", |
| 795 | "void A(int a) throw()")); |
| 796 | // Should be: with semicolon |
| 797 | } |
| 798 | |
| 799 | TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification2) { |
| 800 | ASSERT_TRUE(PrintedDeclMatches( |
| 801 | "struct Z {" |
| 802 | " void A(int a) throw(int);" |
| 803 | "};", |
| 804 | "A", |
| 805 | "void A(int a) throw(int)")); |
| 806 | // Should be: with semicolon |
| 807 | } |
| 808 | |
| 809 | TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification3) { |
| 810 | ASSERT_TRUE(PrintedDeclMatches( |
| 811 | "class ZZ {};" |
| 812 | "struct Z {" |
| 813 | " void A(int a) throw(ZZ, int);" |
| 814 | "};", |
| 815 | "A", |
| 816 | "void A(int a) throw(ZZ, int)")); |
| 817 | // Should be: with semicolon |
| 818 | } |
| 819 | |
| 820 | TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification4) { |
| 821 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 822 | "struct Z {" |
| 823 | " void A(int a) noexcept;" |
| 824 | "};", |
| 825 | "A", |
| 826 | "void A(int a) noexcept")); |
| 827 | // Should be: with semicolon |
| 828 | } |
| 829 | |
| 830 | TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification5) { |
| 831 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 832 | "struct Z {" |
| 833 | " void A(int a) noexcept(true);" |
| 834 | "};", |
| 835 | "A", |
| 836 | "void A(int a) noexcept(trueA(int a) noexcept(true)")); |
| 837 | // WRONG; Should be: "void A(int a) noexcept(true);" |
| 838 | } |
| 839 | |
| 840 | TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification6) { |
| 841 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 842 | "struct Z {" |
| 843 | " void A(int a) noexcept(1 < 2);" |
| 844 | "};", |
| 845 | "A", |
| 846 | "void A(int a) noexcept(1 < 2A(int a) noexcept(1 < 2)")); |
| 847 | // WRONG; Should be: "void A(int a) noexcept(1 < 2);" |
| 848 | } |
| 849 | |
| 850 | TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification7) { |
| 851 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 852 | "template<int N>" |
| 853 | "struct Z {" |
| 854 | " void A(int a) noexcept(N < 2);" |
| 855 | "};", |
| 856 | "A", |
| 857 | "void A(int a) noexcept(N < 2A(int a) noexcept(N < 2)")); |
| 858 | // WRONG; Should be: "void A(int a) noexcept(N < 2);" |
| 859 | } |
| 860 | |
| 861 | TEST(DeclPrinter, TestVarDecl1) { |
| 862 | ASSERT_TRUE(PrintedDeclMatches( |
| 863 | "char *const (*(*A)[5])(int);", |
| 864 | "A", |
| 865 | "char *const (*(*A)[5])(int)")); |
| 866 | // Should be: with semicolon |
| 867 | } |
| 868 | |
| 869 | TEST(DeclPrinter, TestVarDecl2) { |
| 870 | ASSERT_TRUE(PrintedDeclMatches( |
| 871 | "void (*A)() throw(int);", |
| 872 | "A", |
| 873 | "void (*A)() throw(int)")); |
| 874 | // Should be: with semicolon |
| 875 | } |
| 876 | |
| 877 | TEST(DeclPrinter, TestVarDecl3) { |
| 878 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 879 | "void (*A)() noexcept;", |
| 880 | "A", |
| 881 | "void (*A)() noexcept")); |
| 882 | // Should be: with semicolon |
| 883 | } |
| 884 | |
| 885 | TEST(DeclPrinter, TestFieldDecl1) { |
| 886 | ASSERT_TRUE(PrintedDeclMatches( |
| 887 | "template<typename T>" |
| 888 | "struct Z { T A; };", |
| 889 | "A", |
| 890 | "T A")); |
| 891 | // Should be: with semicolon |
| 892 | } |
| 893 | |
| 894 | TEST(DeclPrinter, TestFieldDecl2) { |
| 895 | ASSERT_TRUE(PrintedDeclMatches( |
| 896 | "template<int N>" |
| 897 | "struct Z { int A[N]; };", |
| 898 | "A", |
| 899 | "int A[N]")); |
| 900 | // Should be: with semicolon |
| 901 | } |
| 902 | |
| 903 | TEST(DeclPrinter, TestClassTemplateDecl1) { |
| 904 | ASSERT_TRUE(PrintedDeclMatches( |
| 905 | "template<typename T>" |
| 906 | "struct A { T a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 907 | classTemplateDecl(hasName("A")).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 908 | "template <typename T> struct A {\n}")); |
| 909 | // Should be: with semicolon, with { ... } |
| 910 | } |
| 911 | |
| 912 | TEST(DeclPrinter, TestClassTemplateDecl2) { |
| 913 | ASSERT_TRUE(PrintedDeclMatches( |
| 914 | "template<typename T = int>" |
| 915 | "struct A { T a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 916 | classTemplateDecl(hasName("A")).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 917 | "template <typename T = int> struct A {\n}")); |
| 918 | // Should be: with semicolon, with { ... } |
| 919 | } |
| 920 | |
| 921 | TEST(DeclPrinter, TestClassTemplateDecl3) { |
| 922 | ASSERT_TRUE(PrintedDeclMatches( |
| 923 | "template<class T>" |
| 924 | "struct A { T a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 925 | classTemplateDecl(hasName("A")).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 926 | "template <class T> struct A {\n}")); |
| 927 | // Should be: with semicolon, with { ... } |
| 928 | } |
| 929 | |
| 930 | TEST(DeclPrinter, TestClassTemplateDecl4) { |
| 931 | ASSERT_TRUE(PrintedDeclMatches( |
| 932 | "template<typename T, typename U>" |
| 933 | "struct A { T a; U b; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 934 | classTemplateDecl(hasName("A")).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 935 | "template <typename T, typename U> struct A {\n}")); |
| 936 | // Should be: with semicolon, with { ... } |
| 937 | } |
| 938 | |
| 939 | TEST(DeclPrinter, TestClassTemplateDecl5) { |
| 940 | ASSERT_TRUE(PrintedDeclMatches( |
| 941 | "template<int N>" |
| 942 | "struct A { int a[N]; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 943 | classTemplateDecl(hasName("A")).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 944 | "template <int N> struct A {\n}")); |
| 945 | // Should be: with semicolon, with { ... } |
| 946 | } |
| 947 | |
| 948 | TEST(DeclPrinter, TestClassTemplateDecl6) { |
| 949 | ASSERT_TRUE(PrintedDeclMatches( |
| 950 | "template<int N = 42>" |
| 951 | "struct A { int a[N]; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 952 | classTemplateDecl(hasName("A")).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 953 | "template <int N = 42> struct A {\n}")); |
| 954 | // Should be: with semicolon, with { ... } |
| 955 | } |
| 956 | |
| 957 | TEST(DeclPrinter, TestClassTemplateDecl7) { |
| 958 | ASSERT_TRUE(PrintedDeclMatches( |
| 959 | "typedef int MyInt;" |
| 960 | "template<MyInt N>" |
| 961 | "struct A { int a[N]; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 962 | classTemplateDecl(hasName("A")).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 963 | "template <MyInt N> struct A {\n}")); |
| 964 | // Should be: with semicolon, with { ... } |
| 965 | } |
| 966 | |
| 967 | TEST(DeclPrinter, TestClassTemplateDecl8) { |
| 968 | ASSERT_TRUE(PrintedDeclMatches( |
| 969 | "template<template<typename U> class T> struct A { };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 970 | classTemplateDecl(hasName("A")).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 971 | "template <template <typename U> class T> struct A {\n}")); |
| 972 | // Should be: with semicolon, with { ... } |
| 973 | } |
| 974 | |
| 975 | TEST(DeclPrinter, TestClassTemplateDecl9) { |
| 976 | ASSERT_TRUE(PrintedDeclMatches( |
| 977 | "template<typename T> struct Z { };" |
| 978 | "template<template<typename U> class T = Z> struct A { };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 979 | classTemplateDecl(hasName("A")).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 980 | "template <template <typename U> class T> struct A {\n}")); |
| 981 | // Should be: with semicolon, with { ... } |
| 982 | } |
| 983 | |
| 984 | TEST(DeclPrinter, TestClassTemplateDecl10) { |
| 985 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 986 | "template<typename... T>" |
| 987 | "struct A { int a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 988 | classTemplateDecl(hasName("A")).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 989 | "template <typename ... T> struct A {\n}")); |
| 990 | // Should be: with semicolon, with { ... }, without spaces before '...' |
| 991 | } |
| 992 | |
| 993 | TEST(DeclPrinter, TestClassTemplateDecl11) { |
| 994 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 995 | "template<typename... T>" |
| 996 | "struct A : public T... { int a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 997 | classTemplateDecl(hasName("A")).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 998 | "template <typename ... T> struct A : public T... {\n}")); |
| 999 | // Should be: with semicolon, with { ... }, without spaces before '...' |
| 1000 | } |
| 1001 | |
| 1002 | TEST(DeclPrinter, TestClassTemplatePartialSpecializationDecl1) { |
| 1003 | ASSERT_TRUE(PrintedDeclMatches( |
| 1004 | "template<typename T, typename U>" |
| 1005 | "struct A { T a; U b; };" |
| 1006 | "template<typename T>" |
| 1007 | "struct A<T, int> { T a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1008 | classTemplateSpecializationDecl().bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 1009 | "struct A {\n}")); |
| 1010 | // WRONG; Should be: "template<typename T> struct A<T, int> { ... }" |
| 1011 | } |
| 1012 | |
| 1013 | TEST(DeclPrinter, TestClassTemplatePartialSpecializationDecl2) { |
| 1014 | ASSERT_TRUE(PrintedDeclMatches( |
| 1015 | "template<typename T>" |
| 1016 | "struct A { T a; };" |
| 1017 | "template<typename T>" |
| 1018 | "struct A<T *> { T a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1019 | classTemplateSpecializationDecl().bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 1020 | "struct A {\n}")); |
| 1021 | // WRONG; Should be: "template<typename T> struct A<T *> { ... }" |
| 1022 | } |
| 1023 | |
| 1024 | TEST(DeclPrinter, TestClassTemplateSpecializationDecl1) { |
| 1025 | ASSERT_TRUE(PrintedDeclMatches( |
| 1026 | "template<typename T>" |
| 1027 | "struct A { T a; };" |
| 1028 | "template<>" |
| 1029 | "struct A<int> { int a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1030 | classTemplateSpecializationDecl().bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 1031 | "struct A {\n}")); |
| 1032 | // WRONG; Should be: "template<> struct A<int> { ... }" |
| 1033 | } |
| 1034 | |
| 1035 | TEST(DeclPrinter, TestFunctionTemplateDecl1) { |
| 1036 | ASSERT_TRUE(PrintedDeclMatches( |
| 1037 | "template<typename T>" |
| 1038 | "void A(T &t);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1039 | functionTemplateDecl(hasName("A")).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 1040 | "template <typename T> void A(T &t)")); |
| 1041 | // Should be: with semicolon |
| 1042 | } |
| 1043 | |
| 1044 | TEST(DeclPrinter, TestFunctionTemplateDecl2) { |
| 1045 | ASSERT_TRUE(PrintedDeclMatches( |
| 1046 | "template<typename T>" |
| 1047 | "void A(T &t) { }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1048 | functionTemplateDecl(hasName("A")).bind("id"), |
Dmitri Gribenko | 2e0b8d9 | 2012-08-21 17:47:24 +0000 | [diff] [blame] | 1049 | "template <typename T> void A(T &t)")); |
| 1050 | // Should be: with semicolon |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | TEST(DeclPrinter, TestFunctionTemplateDecl3) { |
| 1054 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 1055 | "template<typename... T>" |
| 1056 | "void A(T... a);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1057 | functionTemplateDecl(hasName("A")).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 1058 | "template <typename ... T> void A(T a...)")); |
| 1059 | // WRONG; Should be: "template <typename ... T> void A(T... a)" |
| 1060 | // (not "T a...") |
| 1061 | // Should be: with semicolon. |
| 1062 | } |
| 1063 | |
| 1064 | TEST(DeclPrinter, TestFunctionTemplateDecl4) { |
| 1065 | ASSERT_TRUE(PrintedDeclMatches( |
| 1066 | "struct Z { template<typename T> void A(T t); };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1067 | functionTemplateDecl(hasName("A")).bind("id"), |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 1068 | "template <typename T> void A(T t)")); |
| 1069 | // Should be: with semicolon |
| 1070 | } |
| 1071 | |
| 1072 | TEST(DeclPrinter, TestFunctionTemplateDecl5) { |
| 1073 | ASSERT_TRUE(PrintedDeclMatches( |
| 1074 | "struct Z { template<typename T> void A(T t) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1075 | functionTemplateDecl(hasName("A")).bind("id"), |
Dmitri Gribenko | 2e0b8d9 | 2012-08-21 17:47:24 +0000 | [diff] [blame] | 1076 | "template <typename T> void A(T t)")); |
| 1077 | // Should be: with semicolon |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | TEST(DeclPrinter, TestFunctionTemplateDecl6) { |
| 1081 | ASSERT_TRUE(PrintedDeclMatches( |
| 1082 | "template<typename T >struct Z {" |
| 1083 | " template<typename U> void A(U t) {}" |
| 1084 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1085 | functionTemplateDecl(hasName("A")).bind("id"), |
Dmitri Gribenko | 2e0b8d9 | 2012-08-21 17:47:24 +0000 | [diff] [blame] | 1086 | "template <typename U> void A(U t)")); |
| 1087 | // Should be: with semicolon |
Dmitri Gribenko | 49795ae | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | TEST(DeclPrinter, TestTemplateArgumentList1) { |
| 1091 | ASSERT_TRUE(PrintedDeclMatches( |
| 1092 | "template<typename T> struct Z {};" |
| 1093 | "struct X {};" |
| 1094 | "Z<X> A;", |
| 1095 | "A", |
| 1096 | "Z<X> A")); |
| 1097 | // Should be: with semicolon |
| 1098 | } |
| 1099 | |
| 1100 | TEST(DeclPrinter, TestTemplateArgumentList2) { |
| 1101 | ASSERT_TRUE(PrintedDeclMatches( |
| 1102 | "template<typename T, typename U> struct Z {};" |
| 1103 | "struct X {};" |
| 1104 | "typedef int Y;" |
| 1105 | "Z<X, Y> A;", |
| 1106 | "A", |
| 1107 | "Z<X, Y> A")); |
| 1108 | // Should be: with semicolon |
| 1109 | } |
| 1110 | |
| 1111 | TEST(DeclPrinter, TestTemplateArgumentList3) { |
| 1112 | ASSERT_TRUE(PrintedDeclMatches( |
| 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 |
| 1119 | } |
| 1120 | |
| 1121 | TEST(DeclPrinter, TestTemplateArgumentList4) { |
| 1122 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 1123 | "template<typename T> struct Z {};" |
| 1124 | "template<typename T> struct X {};" |
| 1125 | "Z<X<int>> A;", |
| 1126 | "A", |
| 1127 | "Z<X<int> > A")); |
| 1128 | // Should be: with semicolon, without extra space in "> >" |
| 1129 | } |
| 1130 | |
| 1131 | TEST(DeclPrinter, TestTemplateArgumentList5) { |
| 1132 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 1133 | "template<typename T> struct Z {};" |
| 1134 | "template<typename T> struct X { Z<T> A; };", |
| 1135 | "A", |
| 1136 | "Z<T> A")); |
| 1137 | // Should be: with semicolon |
| 1138 | } |
| 1139 | |
| 1140 | TEST(DeclPrinter, TestTemplateArgumentList6) { |
| 1141 | ASSERT_TRUE(PrintedDeclMatches( |
| 1142 | "template<template<typename T> class U> struct Z {};" |
| 1143 | "template<typename T> struct X {};" |
| 1144 | "Z<X> A;", |
| 1145 | "A", |
| 1146 | "Z<X> A")); |
| 1147 | // Should be: with semicolon |
| 1148 | } |
| 1149 | |
| 1150 | TEST(DeclPrinter, TestTemplateArgumentList7) { |
| 1151 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 1152 | "template<template<typename T> class U> struct Z {};" |
| 1153 | "template<template<typename T> class U> struct Y {" |
| 1154 | " Z<U> A;" |
| 1155 | "};", |
| 1156 | "A", |
| 1157 | "Z<U> A")); |
| 1158 | // Should be: with semicolon |
| 1159 | } |
| 1160 | |
| 1161 | TEST(DeclPrinter, TestTemplateArgumentList8) { |
| 1162 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 1163 | "template<typename T> struct Z {};" |
| 1164 | "template<template<typename T> class U> struct Y {" |
| 1165 | " Z<U<int> > A;" |
| 1166 | "};", |
| 1167 | "A", |
| 1168 | "Z<U<int> > A")); |
| 1169 | // Should be: with semicolon |
| 1170 | } |
| 1171 | |
| 1172 | TEST(DeclPrinter, TestTemplateArgumentList9) { |
| 1173 | ASSERT_TRUE(PrintedDeclMatches( |
| 1174 | "template<unsigned I> struct Z {};" |
| 1175 | "Z<0> A;", |
| 1176 | "A", |
| 1177 | "Z<0> A")); |
| 1178 | // Should be: with semicolon |
| 1179 | } |
| 1180 | |
| 1181 | TEST(DeclPrinter, TestTemplateArgumentList10) { |
| 1182 | ASSERT_TRUE(PrintedDeclMatches( |
| 1183 | "template<unsigned I> struct Z {};" |
| 1184 | "template<unsigned I> struct X { Z<I> A; };", |
| 1185 | "A", |
| 1186 | "Z<I> A")); |
| 1187 | // Should be: with semicolon |
| 1188 | } |
| 1189 | |
| 1190 | TEST(DeclPrinter, TestTemplateArgumentList11) { |
| 1191 | ASSERT_TRUE(PrintedDeclMatches( |
| 1192 | "template<int I> struct Z {};" |
| 1193 | "Z<42 * 10 - 420 / 1> A;", |
| 1194 | "A", |
| 1195 | "Z<42 * 10 - 420 / 1> A")); |
| 1196 | // Should be: with semicolon |
| 1197 | } |
| 1198 | |
| 1199 | TEST(DeclPrinter, TestTemplateArgumentList12) { |
| 1200 | ASSERT_TRUE(PrintedDeclMatches( |
| 1201 | "template<const char *p> struct Z {};" |
| 1202 | "extern const char X[] = \"aaa\";" |
| 1203 | "Z<X> A;", |
| 1204 | "A", |
| 1205 | "Z<X> A")); |
| 1206 | // Should be: with semicolon |
| 1207 | } |
| 1208 | |
| 1209 | TEST(DeclPrinter, TestTemplateArgumentList13) { |
| 1210 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 1211 | "template<typename... T> struct Z {};" |
| 1212 | "template<typename... T> struct X {" |
| 1213 | " Z<T...> A;" |
| 1214 | "};", |
| 1215 | "A", |
| 1216 | "Z<T...> A")); |
| 1217 | // Should be: with semicolon, without extra space in "> >" |
| 1218 | } |
| 1219 | |
| 1220 | TEST(DeclPrinter, TestTemplateArgumentList14) { |
| 1221 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 1222 | "template<typename... T> struct Z {};" |
| 1223 | "template<typename T> struct Y {};" |
| 1224 | "template<typename... T> struct X {" |
| 1225 | " Z<Y<T>...> A;" |
| 1226 | "};", |
| 1227 | "A", |
| 1228 | "Z<Y<T>...> A")); |
| 1229 | // Should be: with semicolon, without extra space in "> >" |
| 1230 | } |
| 1231 | |
| 1232 | TEST(DeclPrinter, TestTemplateArgumentList15) { |
| 1233 | ASSERT_TRUE(PrintedDeclCXX11Matches( |
| 1234 | "template<unsigned I> struct Z {};" |
| 1235 | "template<typename... T> struct X {" |
| 1236 | " Z<sizeof...(T)> A;" |
| 1237 | "};", |
| 1238 | "A", |
| 1239 | "Z<sizeof...(T)> A")); |
| 1240 | // Should be: with semicolon, without extra space in "> >" |
| 1241 | } |