1 //===- unittests/AST/DeclPrinterTest.cpp --- Declaration printer tests ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains tests for Decl::print() and related methods.
12 // Search this file for WRONG to see test cases that are producing something
13 // completely wrong, invalid C++ or just misleading.
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.
20 //===----------------------------------------------------------------------===//
22 #include "clang/AST/ASTContext.h"
23 #include "clang/ASTMatchers/ASTMatchFinder.h"
24 #include "clang/Tooling/Tooling.h"
25 #include "llvm/ADT/SmallString.h"
26 #include "gtest/gtest.h"
28 using namespace clang;
29 using namespace ast_matchers;
30 using namespace tooling;
34 void PrintDecl(raw_ostream &Out, const ASTContext *Context, const Decl *D) {
35 PrintingPolicy Policy = Context->getPrintingPolicy();
36 Policy.TerseOutput = true;
37 D->print(Out, Policy, /*Indentation*/ 0, /*PrintInstantiation*/ false);
40 class PrintMatch : public MatchFinder::MatchCallback {
41 SmallString<1024> Printed;
42 unsigned NumFoundDecls;
45 PrintMatch() : NumFoundDecls(0) {}
47 virtual void run(const MatchFinder::MatchResult &Result) {
48 const Decl *D = Result.Nodes.getDeclAs<Decl>("id");
49 if (!D || D->isImplicit())
52 if (NumFoundDecls > 1)
55 llvm::raw_svector_ostream Out(Printed);
56 PrintDecl(Out, Result.Context, D);
59 StringRef getPrinted() const {
63 unsigned getNumFoundDecls() const {
68 ::testing::AssertionResult PrintedDeclMatches(
70 const std::vector<std::string> &Args,
71 const DeclarationMatcher &NodeMatch,
72 StringRef ExpectedPrinted,
76 Finder.addMatcher(NodeMatch, &Printer);
77 std::unique_ptr<FrontendActionFactory> Factory(
78 newFrontendActionFactory(&Finder));
80 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args, FileName))
81 return testing::AssertionFailure()
82 << "Parsing error in \"" << Code.str() << "\"";
84 if (Printer.getNumFoundDecls() == 0)
85 return testing::AssertionFailure()
86 << "Matcher didn't find any declarations";
88 if (Printer.getNumFoundDecls() > 1)
89 return testing::AssertionFailure()
90 << "Matcher should match only one declaration "
91 "(found " << Printer.getNumFoundDecls() << ")";
93 if (Printer.getPrinted() != ExpectedPrinted)
94 return ::testing::AssertionFailure()
95 << "Expected \"" << ExpectedPrinted.str() << "\", "
96 "got \"" << Printer.getPrinted().str() << "\"";
98 return ::testing::AssertionSuccess();
101 ::testing::AssertionResult PrintedDeclCXX98Matches(StringRef Code,
103 StringRef ExpectedPrinted) {
104 std::vector<std::string> Args(1, "-std=c++98");
105 return PrintedDeclMatches(Code,
107 namedDecl(hasName(DeclName)).bind("id"),
112 ::testing::AssertionResult PrintedDeclCXX98Matches(
114 const DeclarationMatcher &NodeMatch,
115 StringRef ExpectedPrinted) {
116 std::vector<std::string> Args(1, "-std=c++98");
117 return PrintedDeclMatches(Code,
124 ::testing::AssertionResult PrintedDeclCXX11Matches(StringRef Code,
126 StringRef ExpectedPrinted) {
127 std::vector<std::string> Args(1, "-std=c++11");
128 return PrintedDeclMatches(Code,
130 namedDecl(hasName(DeclName)).bind("id"),
135 ::testing::AssertionResult PrintedDeclCXX11Matches(
137 const DeclarationMatcher &NodeMatch,
138 StringRef ExpectedPrinted) {
139 std::vector<std::string> Args(1, "-std=c++11");
140 return PrintedDeclMatches(Code,
147 ::testing::AssertionResult PrintedDeclCXX11nonMSCMatches(
149 const DeclarationMatcher &NodeMatch,
150 StringRef ExpectedPrinted) {
151 std::vector<std::string> Args(1, "-std=c++11");
152 Args.push_back("-fno-delayed-template-parsing");
153 return PrintedDeclMatches(Code,
160 ::testing::AssertionResult PrintedDeclObjCMatches(
162 const DeclarationMatcher &NodeMatch,
163 StringRef ExpectedPrinted) {
164 std::vector<std::string> Args(1, "");
165 return PrintedDeclMatches(Code,
172 } // unnamed namespace
174 TEST(DeclPrinter, TestTypedef1) {
175 ASSERT_TRUE(PrintedDeclCXX98Matches(
179 // Should be: with semicolon
182 TEST(DeclPrinter, TestTypedef2) {
183 ASSERT_TRUE(PrintedDeclCXX98Matches(
184 "typedef const char *A;",
186 "typedef const char *A"));
187 // Should be: with semicolon
190 TEST(DeclPrinter, TestTypedef3) {
191 ASSERT_TRUE(PrintedDeclCXX98Matches(
192 "template <typename Y> class X {};"
195 "typedef X<int> A"));
196 // Should be: with semicolon
199 TEST(DeclPrinter, TestTypedef4) {
200 ASSERT_TRUE(PrintedDeclCXX98Matches(
201 "namespace X { class Y {}; }"
205 // Should be: with semicolon
208 TEST(DeclPrinter, TestNamespace1) {
209 ASSERT_TRUE(PrintedDeclCXX98Matches(
210 "namespace A { int B; }",
212 "namespace A {\n}"));
213 // Should be: with { ... }
216 TEST(DeclPrinter, TestNamespace2) {
217 ASSERT_TRUE(PrintedDeclCXX11Matches(
218 "inline namespace A { int B; }",
220 "inline namespace A {\n}"));
221 // Should be: with { ... }
224 TEST(DeclPrinter, TestNamespaceAlias1) {
225 ASSERT_TRUE(PrintedDeclCXX98Matches(
230 // Should be: with semicolon
233 TEST(DeclPrinter, TestNamespaceAlias2) {
234 ASSERT_TRUE(PrintedDeclCXX98Matches(
235 "namespace X { namespace Y {} }"
236 "namespace A = X::Y;",
238 "namespace A = X::Y"));
239 // Should be: with semicolon
242 TEST(DeclPrinter, TestCXXRecordDecl1) {
243 ASSERT_TRUE(PrintedDeclCXX98Matches(
244 "class A { int a; };",
247 // Should be: with semicolon, with { ... }
250 TEST(DeclPrinter, TestCXXRecordDecl2) {
251 ASSERT_TRUE(PrintedDeclCXX98Matches(
252 "struct A { int a; };",
255 // Should be: with semicolon, with { ... }
258 TEST(DeclPrinter, TestCXXRecordDecl3) {
259 ASSERT_TRUE(PrintedDeclCXX98Matches(
260 "union A { int a; };",
263 // Should be: with semicolon, with { ... }
266 TEST(DeclPrinter, TestCXXRecordDecl4) {
267 ASSERT_TRUE(PrintedDeclCXX98Matches(
268 "class Z { int a; };"
269 "class A : Z { int b; };",
271 "class A : Z {\n}"));
272 // Should be: with semicolon, with { ... }
275 TEST(DeclPrinter, TestCXXRecordDecl5) {
276 ASSERT_TRUE(PrintedDeclCXX98Matches(
277 "struct Z { int a; };"
278 "struct A : Z { int b; };",
280 "struct A : Z {\n}"));
281 // Should be: with semicolon, with { ... }
284 TEST(DeclPrinter, TestCXXRecordDecl6) {
285 ASSERT_TRUE(PrintedDeclCXX98Matches(
286 "class Z { int a; };"
287 "class A : public Z { int b; };",
289 "class A : public Z {\n}"));
290 // Should be: with semicolon, with { ... }
293 TEST(DeclPrinter, TestCXXRecordDecl7) {
294 ASSERT_TRUE(PrintedDeclCXX98Matches(
295 "class Z { int a; };"
296 "class A : protected Z { int b; };",
298 "class A : protected Z {\n}"));
299 // Should be: with semicolon, with { ... }
302 TEST(DeclPrinter, TestCXXRecordDecl8) {
303 ASSERT_TRUE(PrintedDeclCXX98Matches(
304 "class Z { int a; };"
305 "class A : private Z { int b; };",
307 "class A : private Z {\n}"));
308 // Should be: with semicolon, with { ... }
311 TEST(DeclPrinter, TestCXXRecordDecl9) {
312 ASSERT_TRUE(PrintedDeclCXX98Matches(
313 "class Z { int a; };"
314 "class A : virtual Z { int b; };",
316 "class A : virtual Z {\n}"));
317 // Should be: with semicolon, with { ... }
320 TEST(DeclPrinter, TestCXXRecordDecl10) {
321 ASSERT_TRUE(PrintedDeclCXX98Matches(
322 "class Z { int a; };"
323 "class A : virtual public Z { int b; };",
325 "class A : virtual public Z {\n}"));
326 // Should be: with semicolon, with { ... }
329 TEST(DeclPrinter, TestCXXRecordDecl11) {
330 ASSERT_TRUE(PrintedDeclCXX98Matches(
331 "class Z { int a; };"
332 "class Y : virtual public Z { int b; };"
333 "class A : virtual public Z, private Y { int c; };",
335 "class A : virtual public Z, private Y {\n}"));
336 // Should be: with semicolon, with { ... }
339 TEST(DeclPrinter, TestFunctionDecl1) {
340 ASSERT_TRUE(PrintedDeclCXX98Matches(
344 // Should be: with semicolon
347 TEST(DeclPrinter, TestFunctionDecl2) {
348 ASSERT_TRUE(PrintedDeclCXX98Matches(
352 // Should be: with semicolon
355 TEST(DeclPrinter, TestFunctionDecl3) {
356 ASSERT_TRUE(PrintedDeclCXX98Matches(
361 // Should be: with semicolon
364 TEST(DeclPrinter, TestFunctionDecl4) {
365 ASSERT_TRUE(PrintedDeclCXX98Matches(
369 // Should be: with semicolon
372 TEST(DeclPrinter, TestFunctionDecl5) {
373 ASSERT_TRUE(PrintedDeclCXX98Matches(
377 // Should be: with semicolon
380 TEST(DeclPrinter, TestFunctionDecl6) {
381 ASSERT_TRUE(PrintedDeclCXX98Matches(
385 // Should be: with semicolon
388 TEST(DeclPrinter, TestFunctionDecl7) {
389 ASSERT_TRUE(PrintedDeclCXX11Matches(
390 "constexpr int A(int a);",
392 "constexpr int A(int a)"));
393 // Should be: with semicolon
396 TEST(DeclPrinter, TestFunctionDecl8) {
397 ASSERT_TRUE(PrintedDeclCXX98Matches(
401 // Should be: with semicolon
404 TEST(DeclPrinter, TestFunctionDecl9) {
405 ASSERT_TRUE(PrintedDeclCXX98Matches(
409 // Should be: with semicolon
412 TEST(DeclPrinter, TestFunctionDecl10) {
413 ASSERT_TRUE(PrintedDeclCXX98Matches(
414 "void A(int a, ...);",
416 "void A(int a, ...)"));
417 // Should be: with semicolon
420 TEST(DeclPrinter, TestFunctionDecl11) {
421 ASSERT_TRUE(PrintedDeclCXX98Matches(
422 "typedef long ssize_t;"
424 "void A(int a, pInt b, ssize_t c);",
426 "void A(int a, pInt b, ssize_t c)"));
427 // Should be: with semicolon
430 TEST(DeclPrinter, TestFunctionDecl12) {
431 ASSERT_TRUE(PrintedDeclCXX98Matches(
432 "void A(int a, int b = 0);",
434 "void A(int a, int b = 0)"));
435 // Should be: with semicolon
438 TEST(DeclPrinter, TestFunctionDecl13) {
439 ASSERT_TRUE(PrintedDeclCXX98Matches(
440 "void (*A(int a))(int b);",
442 "void (*A(int a))(int)"));
443 // Should be: with semicolon, with parameter name (?)
446 TEST(DeclPrinter, TestFunctionDecl14) {
447 ASSERT_TRUE(PrintedDeclCXX98Matches(
448 "template<typename T>"
452 functionDecl(hasName("A"), isExplicitTemplateSpecialization()).bind("id"),
454 // WRONG; Should be: "template <> void A(int N);"));
458 TEST(DeclPrinter, TestCXXConstructorDecl1) {
459 ASSERT_TRUE(PrintedDeclCXX98Matches(
463 constructorDecl(ofClass(hasName("A"))).bind("id"),
467 TEST(DeclPrinter, TestCXXConstructorDecl2) {
468 ASSERT_TRUE(PrintedDeclCXX98Matches(
472 constructorDecl(ofClass(hasName("A"))).bind("id"),
476 TEST(DeclPrinter, TestCXXConstructorDecl3) {
477 ASSERT_TRUE(PrintedDeclCXX98Matches(
481 constructorDecl(ofClass(hasName("A"))).bind("id"),
485 TEST(DeclPrinter, TestCXXConstructorDecl4) {
486 ASSERT_TRUE(PrintedDeclCXX98Matches(
488 " A(const A &a, int = 0);"
490 constructorDecl(ofClass(hasName("A"))).bind("id"),
491 "A(const A &a, int = 0)"));
494 TEST(DeclPrinter, TestCXXConstructorDecl5) {
495 ASSERT_TRUE(PrintedDeclCXX11Matches(
499 constructorDecl(ofClass(hasName("A"))).bind("id"),
503 TEST(DeclPrinter, TestCXXConstructorDecl6) {
504 ASSERT_TRUE(PrintedDeclCXX98Matches(
506 " explicit A(int a);"
508 constructorDecl(ofClass(hasName("A"))).bind("id"),
509 "explicit A(int a)"));
512 TEST(DeclPrinter, TestCXXConstructorDecl7) {
513 ASSERT_TRUE(PrintedDeclCXX11Matches(
517 constructorDecl(ofClass(hasName("A"))).bind("id"),
521 TEST(DeclPrinter, TestCXXConstructorDecl8) {
522 ASSERT_TRUE(PrintedDeclCXX11Matches(
526 constructorDecl(ofClass(hasName("A"))).bind("id"),
530 TEST(DeclPrinter, TestCXXConstructorDecl9) {
531 ASSERT_TRUE(PrintedDeclCXX11Matches(
535 constructorDecl(ofClass(hasName("A"))).bind("id"),
539 TEST(DeclPrinter, TestCXXConstructorDecl10) {
540 ASSERT_TRUE(PrintedDeclCXX11Matches(
541 "template<typename... T>"
545 constructorDecl(ofClass(hasName("A"))).bind("id"),
546 "A<T...>(const A<T...> &a)"));
547 // WRONG; Should be: "A(const A<T...> &a);"
550 TEST(DeclPrinter, TestCXXConstructorDecl11) {
551 ASSERT_TRUE(PrintedDeclCXX11nonMSCMatches(
552 "template<typename... T>"
553 "struct A : public T... {"
554 " A(T&&... ts) : T(ts)... {}"
556 constructorDecl(ofClass(hasName("A"))).bind("id"),
557 "A<T...>(T &&...ts) : T(ts)..."));
558 // WRONG; Should be: "A(T &&...ts) : T(ts)... {}"
561 TEST(DeclPrinter, TestCXXDestructorDecl1) {
562 ASSERT_TRUE(PrintedDeclCXX98Matches(
566 destructorDecl(ofClass(hasName("A"))).bind("id"),
570 TEST(DeclPrinter, TestCXXDestructorDecl2) {
571 ASSERT_TRUE(PrintedDeclCXX98Matches(
575 destructorDecl(ofClass(hasName("A"))).bind("id"),
579 TEST(DeclPrinter, TestCXXConversionDecl1) {
580 ASSERT_TRUE(PrintedDeclCXX98Matches(
584 methodDecl(ofClass(hasName("A"))).bind("id"),
588 TEST(DeclPrinter, TestCXXConversionDecl2) {
589 ASSERT_TRUE(PrintedDeclCXX98Matches(
593 methodDecl(ofClass(hasName("A"))).bind("id"),
597 TEST(DeclPrinter, TestCXXConversionDecl3) {
598 ASSERT_TRUE(PrintedDeclCXX98Matches(
603 methodDecl(ofClass(hasName("A"))).bind("id"),
607 TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction1) {
608 ASSERT_TRUE(PrintedDeclCXX11Matches(
609 "namespace std { typedef decltype(sizeof(int)) size_t; }"
611 " void *operator new(std::size_t);"
613 methodDecl(ofClass(hasName("Z"))).bind("id"),
614 "void *operator new(std::size_t)"));
615 // Should be: with semicolon
618 TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction2) {
619 ASSERT_TRUE(PrintedDeclCXX11Matches(
620 "namespace std { typedef decltype(sizeof(int)) size_t; }"
622 " void *operator new[](std::size_t);"
624 methodDecl(ofClass(hasName("Z"))).bind("id"),
625 "void *operator new[](std::size_t)"));
626 // Should be: with semicolon
629 TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction3) {
630 ASSERT_TRUE(PrintedDeclCXX11Matches(
632 " void operator delete(void *);"
634 methodDecl(ofClass(hasName("Z"))).bind("id"),
635 "void operator delete(void *) noexcept"));
636 // Should be: with semicolon, without noexcept?
639 TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction4) {
640 ASSERT_TRUE(PrintedDeclCXX98Matches(
642 " void operator delete(void *);"
644 methodDecl(ofClass(hasName("Z"))).bind("id"),
645 "void operator delete(void *)"));
646 // Should be: with semicolon
649 TEST(DeclPrinter, TestCXXMethodDecl_AllocationFunction5) {
650 ASSERT_TRUE(PrintedDeclCXX11Matches(
652 " void operator delete[](void *);"
654 methodDecl(ofClass(hasName("Z"))).bind("id"),
655 "void operator delete[](void *) noexcept"));
656 // Should be: with semicolon, without noexcept?
659 TEST(DeclPrinter, TestCXXMethodDecl_Operator1) {
660 const char *OperatorNames[] = {
661 "+", "-", "*", "/", "%", "^", "&", "|",
662 "=", "<", ">", "+=", "-=", "*=", "/=", "%=",
663 "^=", "&=", "|=", "<<", ">>", ">>=", "<<=", "==", "!=",
664 "<=", ">=", "&&", "||", ",", "->*",
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("(Z z); };");
674 SmallString<128> Expected;
675 Expected.append("void operator");
676 Expected.append(OperatorNames[i]);
677 Expected.append("(Z z)");
678 // Should be: with semicolon
680 ASSERT_TRUE(PrintedDeclCXX98Matches(
682 methodDecl(ofClass(hasName("Z"))).bind("id"),
687 TEST(DeclPrinter, TestCXXMethodDecl_Operator2) {
688 const char *OperatorNames[] = {
689 "~", "!", "++", "--", "->"
692 for (unsigned i = 0, e = llvm::array_lengthof(OperatorNames); i != e; ++i) {
693 SmallString<128> Code;
694 Code.append("struct Z { void operator");
695 Code.append(OperatorNames[i]);
696 Code.append("(); };");
698 SmallString<128> Expected;
699 Expected.append("void operator");
700 Expected.append(OperatorNames[i]);
701 Expected.append("()");
702 // Should be: with semicolon
704 ASSERT_TRUE(PrintedDeclCXX98Matches(
706 methodDecl(ofClass(hasName("Z"))).bind("id"),
711 TEST(DeclPrinter, TestCXXMethodDecl1) {
712 ASSERT_TRUE(PrintedDeclCXX98Matches(
718 // Should be: with semicolon
721 TEST(DeclPrinter, TestCXXMethodDecl2) {
722 ASSERT_TRUE(PrintedDeclCXX98Matches(
724 " virtual void A(int a);"
727 "virtual void A(int a)"));
728 // Should be: with semicolon
731 TEST(DeclPrinter, TestCXXMethodDecl3) {
732 ASSERT_TRUE(PrintedDeclCXX98Matches(
734 " virtual void A(int a);"
741 // Should be: with semicolon
742 // TODO: should we print "virtual"?
745 TEST(DeclPrinter, TestCXXMethodDecl4) {
746 ASSERT_TRUE(PrintedDeclCXX98Matches(
748 " inline void A(int a);"
751 "inline void A(int a)"));
752 // Should be: with semicolon
755 TEST(DeclPrinter, TestCXXMethodDecl5) {
756 ASSERT_TRUE(PrintedDeclCXX98Matches(
758 " virtual void A(int a) = 0;"
761 "virtual void A(int a) = 0"));
762 // Should be: with semicolon
765 TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier1) {
766 ASSERT_TRUE(PrintedDeclCXX98Matches(
768 " void A(int a) const;"
771 "void A(int a) const"));
772 // Should be: with semicolon
775 TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier2) {
776 ASSERT_TRUE(PrintedDeclCXX98Matches(
778 " void A(int a) volatile;"
781 "void A(int a) volatile"));
782 // Should be: with semicolon
785 TEST(DeclPrinter, TestCXXMethodDecl_CVQualifier3) {
786 ASSERT_TRUE(PrintedDeclCXX98Matches(
788 " void A(int a) const volatile;"
791 "void A(int a) const volatile"));
792 // Should be: with semicolon
795 TEST(DeclPrinter, TestCXXMethodDecl_RefQualifier1) {
796 ASSERT_TRUE(PrintedDeclCXX11Matches(
802 // Should be: with semicolon
805 TEST(DeclPrinter, TestCXXMethodDecl_RefQualifier2) {
806 ASSERT_TRUE(PrintedDeclCXX11Matches(
811 "void A(int a) &&"));
812 // Should be: with semicolon
815 TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification1) {
816 ASSERT_TRUE(PrintedDeclCXX98Matches(
818 " void A(int a) throw();"
821 "void A(int a) throw()"));
822 // Should be: with semicolon
825 TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification2) {
826 ASSERT_TRUE(PrintedDeclCXX98Matches(
828 " void A(int a) throw(int);"
831 "void A(int a) throw(int)"));
832 // Should be: with semicolon
835 TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification3) {
836 ASSERT_TRUE(PrintedDeclCXX98Matches(
839 " void A(int a) throw(ZZ, int);"
842 "void A(int a) throw(ZZ, int)"));
843 // Should be: with semicolon
846 TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification4) {
847 ASSERT_TRUE(PrintedDeclCXX11Matches(
849 " void A(int a) noexcept;"
852 "void A(int a) noexcept"));
853 // Should be: with semicolon
856 TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification5) {
857 ASSERT_TRUE(PrintedDeclCXX11Matches(
859 " void A(int a) noexcept(true);"
862 "void A(int a) noexcept(trueA(int a) noexcept(true)"));
863 // WRONG; Should be: "void A(int a) noexcept(true);"
866 TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification6) {
867 ASSERT_TRUE(PrintedDeclCXX11Matches(
869 " void A(int a) noexcept(1 < 2);"
872 "void A(int a) noexcept(1 < 2A(int a) noexcept(1 < 2)"));
873 // WRONG; Should be: "void A(int a) noexcept(1 < 2);"
876 TEST(DeclPrinter, TestFunctionDecl_ExceptionSpecification7) {
877 ASSERT_TRUE(PrintedDeclCXX11Matches(
880 " void A(int a) noexcept(N < 2);"
883 "void A(int a) noexcept(N < 2A(int a) noexcept(N < 2)"));
884 // WRONG; Should be: "void A(int a) noexcept(N < 2);"
887 TEST(DeclPrinter, TestVarDecl1) {
888 ASSERT_TRUE(PrintedDeclCXX98Matches(
889 "char *const (*(*A)[5])(int);",
891 "char *const (*(*A)[5])(int)"));
892 // Should be: with semicolon
895 TEST(DeclPrinter, TestVarDecl2) {
896 ASSERT_TRUE(PrintedDeclCXX98Matches(
897 "void (*A)() throw(int);",
899 "void (*A)() throw(int)"));
900 // Should be: with semicolon
903 TEST(DeclPrinter, TestVarDecl3) {
904 ASSERT_TRUE(PrintedDeclCXX11Matches(
905 "void (*A)() noexcept;",
907 "void (*A)() noexcept"));
908 // Should be: with semicolon
911 TEST(DeclPrinter, TestFieldDecl1) {
912 ASSERT_TRUE(PrintedDeclCXX98Matches(
913 "template<typename T>"
914 "struct Z { T A; };",
917 // Should be: with semicolon
920 TEST(DeclPrinter, TestFieldDecl2) {
921 ASSERT_TRUE(PrintedDeclCXX98Matches(
923 "struct Z { int A[N]; };",
926 // Should be: with semicolon
929 TEST(DeclPrinter, TestClassTemplateDecl1) {
930 ASSERT_TRUE(PrintedDeclCXX98Matches(
931 "template<typename T>"
932 "struct A { T a; };",
933 classTemplateDecl(hasName("A")).bind("id"),
934 "template <typename T> struct A {\n}"));
935 // Should be: with semicolon, with { ... }
938 TEST(DeclPrinter, TestClassTemplateDecl2) {
939 ASSERT_TRUE(PrintedDeclCXX98Matches(
940 "template<typename T = int>"
941 "struct A { T a; };",
942 classTemplateDecl(hasName("A")).bind("id"),
943 "template <typename T = int> struct A {\n}"));
944 // Should be: with semicolon, with { ... }
947 TEST(DeclPrinter, TestClassTemplateDecl3) {
948 ASSERT_TRUE(PrintedDeclCXX98Matches(
950 "struct A { T a; };",
951 classTemplateDecl(hasName("A")).bind("id"),
952 "template <class T> struct A {\n}"));
953 // Should be: with semicolon, with { ... }
956 TEST(DeclPrinter, TestClassTemplateDecl4) {
957 ASSERT_TRUE(PrintedDeclCXX98Matches(
958 "template<typename T, typename U>"
959 "struct A { T a; U b; };",
960 classTemplateDecl(hasName("A")).bind("id"),
961 "template <typename T, typename U> struct A {\n}"));
962 // Should be: with semicolon, with { ... }
965 TEST(DeclPrinter, TestClassTemplateDecl5) {
966 ASSERT_TRUE(PrintedDeclCXX98Matches(
968 "struct A { int a[N]; };",
969 classTemplateDecl(hasName("A")).bind("id"),
970 "template <int N> struct A {\n}"));
971 // Should be: with semicolon, with { ... }
974 TEST(DeclPrinter, TestClassTemplateDecl6) {
975 ASSERT_TRUE(PrintedDeclCXX98Matches(
976 "template<int N = 42>"
977 "struct A { int a[N]; };",
978 classTemplateDecl(hasName("A")).bind("id"),
979 "template <int N = 42> struct A {\n}"));
980 // Should be: with semicolon, with { ... }
983 TEST(DeclPrinter, TestClassTemplateDecl7) {
984 ASSERT_TRUE(PrintedDeclCXX98Matches(
987 "struct A { int a[N]; };",
988 classTemplateDecl(hasName("A")).bind("id"),
989 "template <MyInt N> struct A {\n}"));
990 // Should be: with semicolon, with { ... }
993 TEST(DeclPrinter, TestClassTemplateDecl8) {
994 ASSERT_TRUE(PrintedDeclCXX98Matches(
995 "template<template<typename U> class T> struct A { };",
996 classTemplateDecl(hasName("A")).bind("id"),
997 "template <template <typename U> class T> struct A {\n}"));
998 // Should be: with semicolon, with { ... }
1001 TEST(DeclPrinter, TestClassTemplateDecl9) {
1002 ASSERT_TRUE(PrintedDeclCXX98Matches(
1003 "template<typename T> struct Z { };"
1004 "template<template<typename U> class T = Z> struct A { };",
1005 classTemplateDecl(hasName("A")).bind("id"),
1006 "template <template <typename U> class T> struct A {\n}"));
1007 // Should be: with semicolon, with { ... }
1010 TEST(DeclPrinter, TestClassTemplateDecl10) {
1011 ASSERT_TRUE(PrintedDeclCXX11Matches(
1012 "template<typename... T>"
1013 "struct A { int a; };",
1014 classTemplateDecl(hasName("A")).bind("id"),
1015 "template <typename ...T> struct A {\n}"));
1016 // Should be: with semicolon, with { ... }
1019 TEST(DeclPrinter, TestClassTemplateDecl11) {
1020 ASSERT_TRUE(PrintedDeclCXX11Matches(
1021 "template<typename... T>"
1022 "struct A : public T... { int a; };",
1023 classTemplateDecl(hasName("A")).bind("id"),
1024 "template <typename ...T> struct A : public T... {\n}"));
1025 // Should be: with semicolon, with { ... }
1028 TEST(DeclPrinter, TestClassTemplatePartialSpecializationDecl1) {
1029 ASSERT_TRUE(PrintedDeclCXX98Matches(
1030 "template<typename T, typename U>"
1031 "struct A { T a; U b; };"
1032 "template<typename T>"
1033 "struct A<T, int> { T a; };",
1034 classTemplateSpecializationDecl().bind("id"),
1036 // WRONG; Should be: "template<typename T> struct A<T, int> { ... }"
1039 TEST(DeclPrinter, TestClassTemplatePartialSpecializationDecl2) {
1040 ASSERT_TRUE(PrintedDeclCXX98Matches(
1041 "template<typename T>"
1042 "struct A { T a; };"
1043 "template<typename T>"
1044 "struct A<T *> { T a; };",
1045 classTemplateSpecializationDecl().bind("id"),
1047 // WRONG; Should be: "template<typename T> struct A<T *> { ... }"
1050 TEST(DeclPrinter, TestClassTemplateSpecializationDecl1) {
1051 ASSERT_TRUE(PrintedDeclCXX98Matches(
1052 "template<typename T>"
1053 "struct A { T a; };"
1055 "struct A<int> { int a; };",
1056 classTemplateSpecializationDecl().bind("id"),
1058 // WRONG; Should be: "template<> struct A<int> { ... }"
1061 TEST(DeclPrinter, TestFunctionTemplateDecl1) {
1062 ASSERT_TRUE(PrintedDeclCXX98Matches(
1063 "template<typename T>"
1065 functionTemplateDecl(hasName("A")).bind("id"),
1066 "template <typename T> void A(T &t)"));
1067 // Should be: with semicolon
1070 TEST(DeclPrinter, TestFunctionTemplateDecl2) {
1071 ASSERT_TRUE(PrintedDeclCXX98Matches(
1072 "template<typename T>"
1074 functionTemplateDecl(hasName("A")).bind("id"),
1075 "template <typename T> void A(T &t)"));
1076 // Should be: with semicolon
1079 TEST(DeclPrinter, TestFunctionTemplateDecl3) {
1080 ASSERT_TRUE(PrintedDeclCXX11Matches(
1081 "template<typename... T>"
1083 functionTemplateDecl(hasName("A")).bind("id"),
1084 "template <typename ...T> void A(T ...a)"));
1085 // Should be: with semicolon.
1088 TEST(DeclPrinter, TestFunctionTemplateDecl4) {
1089 ASSERT_TRUE(PrintedDeclCXX98Matches(
1090 "struct Z { template<typename T> void A(T t); };",
1091 functionTemplateDecl(hasName("A")).bind("id"),
1092 "template <typename T> void A(T t)"));
1093 // Should be: with semicolon
1096 TEST(DeclPrinter, TestFunctionTemplateDecl5) {
1097 ASSERT_TRUE(PrintedDeclCXX98Matches(
1098 "struct Z { template<typename T> void A(T t) {} };",
1099 functionTemplateDecl(hasName("A")).bind("id"),
1100 "template <typename T> void A(T t)"));
1101 // Should be: with semicolon
1104 TEST(DeclPrinter, TestFunctionTemplateDecl6) {
1105 ASSERT_TRUE(PrintedDeclCXX98Matches(
1106 "template<typename T >struct Z {"
1107 " template<typename U> void A(U t) {}"
1109 functionTemplateDecl(hasName("A")).bind("id"),
1110 "template <typename U> void A(U t)"));
1111 // Should be: with semicolon
1114 TEST(DeclPrinter, TestTemplateArgumentList1) {
1115 ASSERT_TRUE(PrintedDeclCXX98Matches(
1116 "template<typename T> struct Z {};"
1121 // Should be: with semicolon
1124 TEST(DeclPrinter, TestTemplateArgumentList2) {
1125 ASSERT_TRUE(PrintedDeclCXX98Matches(
1126 "template<typename T, typename U> struct Z {};"
1132 // Should be: with semicolon
1135 TEST(DeclPrinter, TestTemplateArgumentList3) {
1136 ASSERT_TRUE(PrintedDeclCXX98Matches(
1137 "template<typename T> struct Z {};"
1138 "template<typename T> struct X {};"
1142 // Should be: with semicolon
1145 TEST(DeclPrinter, TestTemplateArgumentList4) {
1146 ASSERT_TRUE(PrintedDeclCXX11Matches(
1147 "template<typename T> struct Z {};"
1148 "template<typename T> struct X {};"
1152 // Should be: with semicolon, without extra space in "> >"
1155 TEST(DeclPrinter, TestTemplateArgumentList5) {
1156 ASSERT_TRUE(PrintedDeclCXX98Matches(
1157 "template<typename T> struct Z {};"
1158 "template<typename T> struct X { Z<T> A; };",
1161 // Should be: with semicolon
1164 TEST(DeclPrinter, TestTemplateArgumentList6) {
1165 ASSERT_TRUE(PrintedDeclCXX98Matches(
1166 "template<template<typename T> class U> struct Z {};"
1167 "template<typename T> struct X {};"
1171 // Should be: with semicolon
1174 TEST(DeclPrinter, TestTemplateArgumentList7) {
1175 ASSERT_TRUE(PrintedDeclCXX98Matches(
1176 "template<template<typename T> class U> struct Z {};"
1177 "template<template<typename T> class U> struct Y {"
1182 // Should be: with semicolon
1185 TEST(DeclPrinter, TestTemplateArgumentList8) {
1186 ASSERT_TRUE(PrintedDeclCXX98Matches(
1187 "template<typename T> struct Z {};"
1188 "template<template<typename T> class U> struct Y {"
1193 // Should be: with semicolon
1196 TEST(DeclPrinter, TestTemplateArgumentList9) {
1197 ASSERT_TRUE(PrintedDeclCXX98Matches(
1198 "template<unsigned I> struct Z {};"
1202 // Should be: with semicolon
1205 TEST(DeclPrinter, TestTemplateArgumentList10) {
1206 ASSERT_TRUE(PrintedDeclCXX98Matches(
1207 "template<unsigned I> struct Z {};"
1208 "template<unsigned I> struct X { Z<I> A; };",
1211 // Should be: with semicolon
1214 TEST(DeclPrinter, TestTemplateArgumentList11) {
1215 ASSERT_TRUE(PrintedDeclCXX98Matches(
1216 "template<int I> struct Z {};"
1217 "Z<42 * 10 - 420 / 1> A;",
1219 "Z<42 * 10 - 420 / 1> A"));
1220 // Should be: with semicolon
1223 TEST(DeclPrinter, TestTemplateArgumentList12) {
1224 ASSERT_TRUE(PrintedDeclCXX98Matches(
1225 "template<const char *p> struct Z {};"
1226 "extern const char X[] = \"aaa\";"
1230 // Should be: with semicolon
1233 TEST(DeclPrinter, TestTemplateArgumentList13) {
1234 ASSERT_TRUE(PrintedDeclCXX11Matches(
1235 "template<typename... T> struct Z {};"
1236 "template<typename... T> struct X {"
1241 // Should be: with semicolon
1244 TEST(DeclPrinter, TestTemplateArgumentList14) {
1245 ASSERT_TRUE(PrintedDeclCXX11Matches(
1246 "template<typename... T> struct Z {};"
1247 "template<typename T> struct Y {};"
1248 "template<typename... T> struct X {"
1253 // Should be: with semicolon
1256 TEST(DeclPrinter, TestTemplateArgumentList15) {
1257 ASSERT_TRUE(PrintedDeclCXX11Matches(
1258 "template<unsigned I> struct Z {};"
1259 "template<typename... T> struct X {"
1260 " Z<sizeof...(T)> A;"
1263 "Z<sizeof...(T)> A"));
1264 // Should be: with semicolon
1267 TEST(DeclPrinter, TestObjCMethod1) {
1268 ASSERT_TRUE(PrintedDeclObjCMatches(
1269 "__attribute__((objc_root_class)) @interface X\n"
1270 "- (int)A:(id)anObject inRange:(long)range;\n"
1272 "@implementation X\n"
1273 "- (int)A:(id)anObject inRange:(long)range { int printThis; return 0; }\n"
1275 namedDecl(hasName("A:inRange:"),
1276 hasDescendant(namedDecl(hasName("printThis")))).bind("id"),
1277 "- (int) A:(id)anObject inRange:(long)range"));
1280 TEST(DeclPrinter, TestObjCProtocol1) {
1281 ASSERT_TRUE(PrintedDeclObjCMatches(
1282 "@protocol P1, P2;",
1283 namedDecl(hasName("P1")).bind("id"),
1284 "@protocol P1;\n"));
1285 ASSERT_TRUE(PrintedDeclObjCMatches(
1286 "@protocol P1, P2;",
1287 namedDecl(hasName("P2")).bind("id"),
1288 "@protocol P2;\n"));
1291 TEST(DeclPrinter, TestObjCProtocol2) {
1292 ASSERT_TRUE(PrintedDeclObjCMatches(
1294 "@protocol P1<P2> @end",
1295 namedDecl(hasName("P1")).bind("id"),
1296 "@protocol P1<P2>\n@end"));