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