diff --git a/clang/unittests/Tooling/Syntax/TreeTest.cpp b/clang/unittests/Tooling/Syntax/TreeTest.cpp --- a/clang/unittests/Tooling/Syntax/TreeTest.cpp +++ b/clang/unittests/Tooling/Syntax/TreeTest.cpp @@ -209,15 +209,22 @@ return Root; } - void expectTreeDumpEqual(StringRef Code, StringRef Tree) { + ::testing::AssertionResult treeDumpEqual(StringRef Code, StringRef Tree) { SCOPED_TRACE(llvm::join(GetParam().getCommandLineArgs(), " ")); - SCOPED_TRACE(Code); auto *Root = buildTree(Code, GetParam()); - EXPECT_EQ(Diags->getClient()->getNumErrors(), 0u) - << "Source file has syntax errors, they were printed to the test log"; + if (Diags->getClient()->getNumErrors() != 0) { + return ::testing::AssertionFailure() + << "Source file has syntax errors, they were printed to the test " + "log"; + } std::string Actual = std::string(StringRef(Root->dump(*Arena)).trim()); + // EXPECT_EQ shows the diff between the two strings if they are different. EXPECT_EQ(Tree.trim().str(), Actual); + if (Actual != Tree.trim().str()) { + return ::testing::AssertionFailure(); + } + return ::testing::AssertionSuccess(); } // Adds a file to the test VFS. @@ -266,11 +273,11 @@ }; TEST_P(SyntaxTreeTest, Simple) { - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( int main() {} void foo() {} - )cpp", +)cpp", R"txt( *: TranslationUnit |-SimpleDeclaration @@ -293,15 +300,15 @@ `-CompoundStatement |-{ `-} -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, SimpleVariable) { - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( int a; int b = 42; - )cpp", +)cpp", R"txt( *: TranslationUnit |-SimpleDeclaration @@ -317,14 +324,14 @@ | `-UnknownExpression | `-42 `-; -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, SimpleFunction) { - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( void foo(int a, int b) {} - )cpp", +)cpp", R"txt( *: TranslationUnit `-SimpleDeclaration @@ -346,17 +353,17 @@ `-CompoundStatement |-{ `-} -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, If) { - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( int main() { if (1) {} if (1) {} else if (0) {} } - )cpp", +)cpp", R"txt( *: TranslationUnit `-SimpleDeclaration @@ -397,11 +404,11 @@ | |-{ | `-} `-} - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, For) { - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( void test() { for (;;) {} @@ -428,21 +435,21 @@ | |-{ | `-} `-} - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, RangeBasedFor) { if (!GetParam().isCXX11OrLater()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( void test() { int a[3]; for (int x : a) ; } - )cpp", +)cpp", R"txt( *: TranslationUnit `-SimpleDeclaration @@ -479,12 +486,17 @@ | `-EmptyStatement | `-; `-} - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, DeclarationStatement) { - expectTreeDumpEqual("void test() { int a = 10; }", - R"txt( + EXPECT_TRUE(treeDumpEqual( + R"cpp( +void test() { + int a = 10; +} +)cpp", + R"txt( *: TranslationUnit `-SimpleDeclaration |-void @@ -505,11 +517,11 @@ | | `-10 | `-; `-} -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, Switch) { - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( void test() { switch (1) { @@ -549,11 +561,11 @@ | | `-; | `-} `-} -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, While) { - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( void test() { while (1) { continue; break; } @@ -586,15 +598,20 @@ | | `-; | `-} `-} -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, UnhandledStatement) { // Unhandled statements should end up as 'unknown statement'. // This example uses a 'label statement', which does not yet have a syntax // counterpart. - expectTreeDumpEqual("int main() { foo: return 100; }", - R"txt( + EXPECT_TRUE(treeDumpEqual( + R"cpp( +int main() { + foo: return 100; +} +)cpp", + R"txt( *: TranslationUnit `-SimpleDeclaration |-int @@ -614,19 +631,19 @@ | | `-100 | `-; `-} -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, Expressions) { // expressions should be wrapped in 'ExpressionStatement' when they appear // in a statement position. - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( void test() { test(); if (1) test(); else test(); } - )cpp", +)cpp", R"txt( *: TranslationUnit `-SimpleDeclaration @@ -667,19 +684,19 @@ | | `-) | `-; `-} -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, CxxNullPtrLiteral) { if (!GetParam().isCXX11OrLater()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( void test() { nullptr; } - )cpp", +)cpp", R"txt( *: TranslationUnit `-SimpleDeclaration @@ -696,17 +713,17 @@ | | `-nullptr | `-; `-} -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, PostfixUnaryOperator) { - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( void test(int a) { a++; a--; } - )cpp", +)cpp", R"txt( *: TranslationUnit `-SimpleDeclaration @@ -735,11 +752,11 @@ | | `--- | `-; `-} -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, PrefixUnaryOperator) { - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( void test(int a, int *ap) { --a; ++a; @@ -751,7 +768,7 @@ !a; __real a; __imag a; } - )cpp", +)cpp", R"txt( *: TranslationUnit `-SimpleDeclaration @@ -834,20 +851,20 @@ | | `-a | `-; `-} -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, PrefixUnaryOperatorCxx) { if (!GetParam().isCXX()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( void test(int a, bool b) { compl a; not b; } - )cpp", +)cpp", R"txt( *: TranslationUnit `-SimpleDeclaration @@ -881,11 +898,11 @@ | | `-b | `-; `-} -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, BinaryOperator) { - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( void test(int a) { 1 - 2; @@ -896,7 +913,7 @@ 1 & 2; a ^= 3; } - )cpp", +)cpp", R"txt( *: TranslationUnit `-SimpleDeclaration @@ -969,14 +986,14 @@ | | `-3 | `-; `-} -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, BinaryOperatorCxx) { if (!GetParam().isCXX()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( void test(int a) { true || false; @@ -984,7 +1001,7 @@ 1 bitand 2; a xor_eq 3; } - )cpp", +)cpp", R"txt( *: TranslationUnit `-SimpleDeclaration @@ -1033,11 +1050,11 @@ | | `-3 | `-; `-} -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, NestedBinaryOperator) { - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( void test(int a, int b) { (1 + 2) * (4 / 2); @@ -1046,7 +1063,7 @@ a + b * 4 + 2; a % 2 + b * 42; } - )cpp", +)cpp", R"txt( *: TranslationUnit `-SimpleDeclaration @@ -1146,14 +1163,14 @@ | | `-42 | `-; `-} -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, UserDefinedBinaryOperator) { if (!GetParam().isCXX()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( struct X { X& operator=(const X&); @@ -1165,7 +1182,7 @@ x + y; x < y; } - )cpp", +)cpp", R"txt( *: TranslationUnit |-SimpleDeclaration @@ -1277,14 +1294,15 @@ | | `-y | `-; `-} -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, MultipleDeclaratorsGrouping) { - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( - int *a, b; int *c, d; - )cpp", +int *a, b; +int *c, d; +)cpp", R"txt( *: TranslationUnit |-SimpleDeclaration @@ -1305,14 +1323,14 @@ |-SimpleDeclarator | `-d `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, MultipleDeclaratorsGroupingTypedef) { - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( - typedef int *a, b; - )cpp", +typedef int *a, b; +)cpp", R"txt( *: TranslationUnit `-SimpleDeclaration @@ -1325,17 +1343,17 @@ |-SimpleDeclarator | `-b `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, MultipleDeclaratorsInsideStatement) { - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( void foo() { - int *a, b; - typedef int *ta, tb; + int *a, b; + typedef int *ta, tb; } - )cpp", +)cpp", R"txt( *: TranslationUnit `-SimpleDeclaration @@ -1369,21 +1387,21 @@ | | `-tb | `-; `-} - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, Namespaces) { if (!GetParam().isCXX()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( namespace a { namespace b {} } namespace a::b {} namespace {} namespace foo = a; - )cpp", +)cpp", R"txt( *: TranslationUnit |-NamespaceDefinition @@ -1413,18 +1431,18 @@ |-= |-a `-; -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, UsingDirective) { if (!GetParam().isCXX()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( namespace ns {} using namespace ::ns; - )cpp", +)cpp", R"txt( *: TranslationUnit |-NamespaceDefinition @@ -1438,18 +1456,18 @@ |-:: |-ns `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, UsingDeclaration) { if (!GetParam().isCXX()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( namespace ns { int a; } using ns::a; - )cpp", +)cpp", R"txt( *: TranslationUnit |-NamespaceDefinition @@ -1468,12 +1486,12 @@ |-:: |-a `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, FreeStandingClasses) { // Free-standing classes, must live inside a SimpleDeclaration. - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( struct X; struct X {}; @@ -1482,7 +1500,7 @@ struct Y {} *y2; struct {} *a1; - )cpp", +)cpp", R"txt( *: TranslationUnit |-SimpleDeclaration @@ -1519,7 +1537,7 @@ | |-* | `-a1 `-; -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, Templates) { @@ -1531,12 +1549,12 @@ // tree when `-fdelayed-template-parsing` is active. return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( template struct cls {}; template int var = 10; template int fun() {} - )cpp", +)cpp", R"txt( *: TranslationUnit |-TemplateDeclaration @@ -1584,21 +1602,21 @@ `-CompoundStatement |-{ `-} -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, NestedTemplates) { if (!GetParam().isCXX()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( template struct X { template U foo(); }; - )cpp", +)cpp", R"txt( *: TranslationUnit `-TemplateDeclaration @@ -1629,18 +1647,18 @@ | `-; |-} `-; -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, Templates2) { if (!GetParam().isCXX()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( template struct X { struct Y; }; template struct X::Y {}; - )cpp", +)cpp", R"txt( *: TranslationUnit |-TemplateDeclaration @@ -1678,20 +1696,20 @@ |-{ |-} `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, TemplatesUsingUsing) { if (!GetParam().isCXX()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( template struct X { using T::foo; using typename T::bar; }; - )cpp", +)cpp", R"txt( *: TranslationUnit `-TemplateDeclaration @@ -1720,14 +1738,14 @@ | `-; |-} `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, ExplicitTemplateInstantations) { if (!GetParam().isCXX()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( template struct X {}; template struct X {}; @@ -1800,17 +1818,17 @@ |-float |-> `-; -)txt"); +)txt")); } TEST_P(SyntaxTreeTest, UsingType) { if (!GetParam().isCXX()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( using type = int; - )cpp", +)cpp", R"txt( *: TranslationUnit `-TypeAliasDeclaration @@ -1819,30 +1837,30 @@ |-= |-int `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, EmptyDeclaration) { - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( ; - )cpp", +)cpp", R"txt( *: TranslationUnit `-EmptyDeclaration `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, StaticAssert) { if (!GetParam().isCXX11OrLater()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( static_assert(true, "message"); static_assert(true); - )cpp", +)cpp", R"txt( *: TranslationUnit |-StaticAssertDeclaration @@ -1862,18 +1880,18 @@ | `-true |-) `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, ExternC) { if (!GetParam().isCXX()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( extern "C" int a; extern "C" { int b; int c; } - )cpp", +)cpp", R"txt( *: TranslationUnit |-LinkageSpecificationDeclaration @@ -1899,12 +1917,12 @@ | | `-c | `-; `-} - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, NonModifiableNodes) { // Some nodes are non-modifiable, they are marked with 'I:'. - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( #define HALF_IF if (1+ #define HALF_IF_2 1) {} @@ -1940,12 +1958,12 @@ | |-{ | `-} `-} - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, ModifiableNodes) { // All nodes can be mutated. - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( #define OPEN { #define CLOSE } @@ -1986,16 +2004,16 @@ | | `-; | `-} `-} - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, ArraySubscriptsInDeclarators) { - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( int a[10]; int b[1][2][3]; int c[] = {1,2,3}; - )cpp", +)cpp", R"txt( *: TranslationUnit |-SimpleDeclaration @@ -2048,17 +2066,18 @@ | |-UnknownExpression | | `-3 | `-} - `-; )txt"); + `-; +)txt")); } TEST_P(SyntaxTreeTest, StaticArraySubscriptsInDeclarators) { if (!GetParam().isC99OrLater()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( void f(int xs[static 10]); - )cpp", +)cpp", R"txt( *: TranslationUnit `-SimpleDeclaration @@ -2078,14 +2097,15 @@ | | | `-10 | | `-] | `-) - `-; )txt"); + `-; +)txt")); } TEST_P(SyntaxTreeTest, ParametersAndQualifiersInFreeFunctions) { if (!GetParam().isCXX()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( int func1(); int func2a(int a); @@ -2094,7 +2114,7 @@ int func3b(int *); int func4a(int a, float b); int func4b(int, float); - )cpp", +)cpp", R"txt( *: TranslationUnit |-SimpleDeclaration @@ -2182,18 +2202,18 @@ | | `-float | `-) `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, ParametersAndQualifiersInFreeFunctionsCxx) { if (!GetParam().isCXX()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( int func1(const int a, volatile int b, const volatile int c); int func2(int& a); - )cpp", +)cpp", R"txt( *: TranslationUnit |-SimpleDeclaration @@ -2235,17 +2255,17 @@ | | `-a | `-) `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, ParametersAndQualifiersInFreeFunctionsCxx11) { if (!GetParam().isCXX11OrLater()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( int func1(int&& a); - )cpp", +)cpp", R"txt( *: TranslationUnit `-SimpleDeclaration @@ -2261,14 +2281,14 @@ | | `-a | `-) `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, ParametersAndQualifiersInMemberFunctions) { if (!GetParam().isCXX()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( struct Test { int a(); @@ -2278,7 +2298,7 @@ int e() &; int f() &&; }; - )cpp", +)cpp", R"txt( *: TranslationUnit `-SimpleDeclaration @@ -2341,17 +2361,17 @@ | `-; |-} `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, TrailingReturn) { if (!GetParam().isCXX11OrLater()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( auto foo() -> int; - )cpp", +)cpp", R"txt( *: TranslationUnit `-SimpleDeclaration @@ -2365,14 +2385,14 @@ | |--> | `-int `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, DynamicExceptionSpecification) { if (!GetParam().supportsCXXDynamicExceptionSpecification()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( struct MyException1 {}; struct MyException2 {}; @@ -2380,7 +2400,7 @@ int b() throw(...); int c() throw(MyException1); int d() throw(MyException1, MyException2); - )cpp", +)cpp", R"txt( *: TranslationUnit |-SimpleDeclaration @@ -2444,18 +2464,18 @@ | |-MyException2 | `-) `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, NoexceptExceptionSpecification) { if (!GetParam().isCXX11OrLater()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( int a() noexcept; int b() noexcept(true); - )cpp", +)cpp", R"txt( *: TranslationUnit |-SimpleDeclaration @@ -2480,17 +2500,17 @@ | | `-true | `-) `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, DeclaratorsInParentheses) { - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( int (a); int *(b); int (*c)(int); int *(d)(int); - )cpp", +)cpp", R"txt( *: TranslationUnit |-SimpleDeclaration @@ -2538,17 +2558,17 @@ | | `-int | `-) `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, ConstVolatileQualifiers) { - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( const int west = -1; int const east = 1; const int const universal = 0; const int const *const *volatile b; - )cpp", +)cpp", R"txt( *: TranslationUnit |-SimpleDeclaration @@ -2592,17 +2612,17 @@ | |-volatile | `-b `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, RangesOfDeclaratorsWithTrailingReturnTypes) { if (!GetParam().isCXX11OrLater()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( auto foo() -> auto(*)(int) -> double*; - )cpp", +)cpp", R"txt( *: TranslationUnit `-SimpleDeclaration @@ -2631,19 +2651,19 @@ | `-SimpleDeclarator | `-* `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, MemberPointers) { if (!GetParam().isCXX()) { return; } - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( struct X {}; int X::* a; const int X::* b; - )cpp", +)cpp", R"txt( *: TranslationUnit |-SimpleDeclaration @@ -2671,14 +2691,14 @@ | | `-* | `-b `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, ComplexDeclarator) { - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( void x(char a, short (*b)(int)); - )cpp", +)cpp", R"txt( *: TranslationUnit `-SimpleDeclaration @@ -2707,14 +2727,14 @@ | | `-) | `-) `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, ComplexDeclarator2) { - expectTreeDumpEqual( + EXPECT_TRUE(treeDumpEqual( R"cpp( void x(char a, short (*b)(int), long (**c)(long long)); - )cpp", +)cpp", R"txt( *: TranslationUnit `-SimpleDeclaration @@ -2759,7 +2779,7 @@ | | `-) | `-) `-; - )txt"); +)txt")); } TEST_P(SyntaxTreeTest, Mutations) {