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 @@ -168,6 +168,35 @@ return ::testing::AssertionSuccess(); } + ::testing::AssertionResult + treeDumpEqualOnAnnotations(StringRef CodeWithAnnotations, + ArrayRef TreeDumps) { + SCOPED_TRACE(llvm::join(GetParam().getCommandLineArgs(), " ")); + + auto AnnotatedCode = llvm::Annotations(CodeWithAnnotations); + auto *Root = buildTree(AnnotatedCode.code(), GetParam()); + + if (Diags->getClient()->getNumErrors() != 0) { + return ::testing::AssertionFailure() + << "Source file has syntax errors, they were printed to the test " + "log"; + } + + auto AnnotatedRanges = AnnotatedCode.ranges(); + assert(AnnotatedRanges.size() == TreeDumps.size()); + for (auto i = 0ul; i < AnnotatedRanges.size(); i++) { + auto *AnnotatedNode = nodeByRange(AnnotatedRanges[i], Root); + assert(AnnotatedNode); + auto AnnotatedNodeDump = + std::string(StringRef(AnnotatedNode->dump(*Arena)).trim()); + // EXPECT_EQ shows the diff between the two strings if they are different. + EXPECT_EQ(TreeDumps[i].trim().str(), AnnotatedNodeDump); + if (AnnotatedNodeDump != TreeDumps[i].trim().str()) + return ::testing::AssertionFailure(); + } + return ::testing::AssertionSuccess(); + } + // Adds a file to the test VFS. void addFile(StringRef Path, StringRef Contents) { if (!FS->addFile(Path, time_t(), @@ -214,34 +243,23 @@ }; TEST_P(SyntaxTreeTest, Simple) { - EXPECT_TRUE(treeDumpEqual( + EXPECT_TRUE(treeDumpEqualOnAnnotations( R"cpp( int main() {} -void foo() {} +[[void foo() {}]] )cpp", - R"txt( -*: TranslationUnit -|-SimpleDeclaration -| |-int -| |-SimpleDeclarator -| | |-main -| | `-ParametersAndQualifiers -| | |-( -| | `-) -| `-CompoundStatement -| |-{ -| `-} -`-SimpleDeclaration - |-void - |-SimpleDeclarator - | |-foo - | `-ParametersAndQualifiers - | |-( - | `-) - `-CompoundStatement - |-{ - `-} -)txt")); + {R"txt( +SimpleDeclaration +|-void +|-SimpleDeclarator +| |-foo +| `-ParametersAndQualifiers +| |-( +| `-) +`-CompoundStatement + |-{ + `-} +)txt"})); } TEST_P(SyntaxTreeTest, SimpleVariable) { @@ -871,7 +889,7 @@ if (!GetParam().isCXX()) { return; } - EXPECT_TRUE(treeDumpEqual( + EXPECT_TRUE(treeDumpEqualOnAnnotations( R"cpp( namespace n { struct S { @@ -889,194 +907,71 @@ }; }; void test() { - :: // global-namespace-specifier - n:: // namespace-specifier - S:: // type-name-specifier - template ST:: // type-template-instantiation-specifier - f(); - - n:: // namespace-specifier - S:: // type-name-specifier - ST:: // type-template-instantiation-specifier - f(); - - ST:: // type-name-specifier - S:: // type-name-specifier - f(); - - ST:: // type-name-specifier - S:: // type-name-specifier - template f(); +[[::n::S::template ST::]]f(); + +[[n::S::ST::]]f(); + +[[ST::S::]]f(); + +[[ST::S::]]template f(); } )cpp", - R"txt( -*: TranslationUnit -|-NamespaceDefinition -| |-namespace -| |-n -| |-{ -| |-SimpleDeclaration -| | |-struct -| | |-S -| | |-{ -| | |-TemplateDeclaration -| | | |-template -| | | |-< -| | | |-UnknownDeclaration -| | | | |-typename -| | | | `-T -| | | |-> -| | | `-SimpleDeclaration -| | | |-struct -| | | |-ST -| | | |-{ -| | | |-SimpleDeclaration -| | | | |-static -| | | | |-void -| | | | |-SimpleDeclarator -| | | | | |-f -| | | | | `-ParametersAndQualifiers -| | | | | |-( -| | | | | `-) -| | | | `-; -| | | |-} -| | | `-; -| | |-} -| | `-; -| `-} -|-TemplateDeclaration + {R"txt( +NestedNameSpecifier +|-:: +|-IdentifierNameSpecifier +| `-n +|-:: +|-IdentifierNameSpecifier +| `-S +|-:: +|-SimpleTemplateNameSpecifier | |-template +| |-ST | |-< -| |-UnknownDeclaration -| | |-typename -| | `-T -| |-> -| `-SimpleDeclaration -| |-struct -| |-ST -| |-{ -| |-SimpleDeclaration -| | |-struct -| | |-S -| | |-{ -| | |-TemplateDeclaration -| | | |-template -| | | |-< -| | | |-UnknownDeclaration -| | | | |-typename -| | | | `-U -| | | |-> -| | | `-SimpleDeclaration -| | | |-static -| | | |-U -| | | |-SimpleDeclarator -| | | | |-f -| | | | `-ParametersAndQualifiers -| | | | |-( -| | | | `-) -| | | `-; -| | |-} -| | `-; -| |-} -| `-; -`-SimpleDeclaration - |-void - |-SimpleDeclarator - | |-test - | `-ParametersAndQualifiers - | |-( - | `-) - `-CompoundStatement - |-{ - |-ExpressionStatement - | |-UnknownExpression - | | |-IdExpression - | | | |-NestedNameSpecifier - | | | | |-:: - | | | | |-IdentifierNameSpecifier - | | | | | `-n - | | | | |-:: - | | | | |-IdentifierNameSpecifier - | | | | | `-S - | | | | |-:: - | | | | |-SimpleTemplateNameSpecifier - | | | | | |-template - | | | | | |-ST - | | | | | |-< - | | | | | |-int - | | | | | `-> - | | | | `-:: - | | | `-UnqualifiedId - | | | `-f - | | |-( - | | `-) - | `-; - |-ExpressionStatement - | |-UnknownExpression - | | |-IdExpression - | | | |-NestedNameSpecifier - | | | | |-IdentifierNameSpecifier - | | | | | `-n - | | | | |-:: - | | | | |-IdentifierNameSpecifier - | | | | | `-S - | | | | |-:: - | | | | |-SimpleTemplateNameSpecifier - | | | | | |-ST - | | | | | |-< - | | | | | |-int - | | | | | `-> - | | | | `-:: - | | | `-UnqualifiedId - | | | `-f - | | |-( - | | `-) - | `-; - |-ExpressionStatement - | |-UnknownExpression - | | |-IdExpression - | | | |-NestedNameSpecifier - | | | | |-SimpleTemplateNameSpecifier - | | | | | |-ST - | | | | | |-< - | | | | | |-int - | | | | | `-> - | | | | |-:: - | | | | |-IdentifierNameSpecifier - | | | | | `-S - | | | | `-:: - | | | `-UnqualifiedId - | | | |-f - | | | |-< - | | | |-int - | | | `-> - | | |-( - | | `-) - | `-; - |-ExpressionStatement - | |-UnknownExpression - | | |-IdExpression - | | | |-NestedNameSpecifier - | | | | |-SimpleTemplateNameSpecifier - | | | | | |-ST - | | | | | |-< - | | | | | |-int - | | | | | `-> - | | | | |-:: - | | | | |-IdentifierNameSpecifier - | | | | | `-S - | | | | `-:: - | | | |-template - | | | `-UnqualifiedId - | | | |-f - | | | |-< - | | | |-int - | | | `-> - | | |-( - | | `-) - | `-; - `-} -)txt")); +| |-int +| `-> +`-:: +)txt", + R"txt( +NestedNameSpecifier +|-IdentifierNameSpecifier +| `-n +|-:: +|-IdentifierNameSpecifier +| `-S +|-:: +|-SimpleTemplateNameSpecifier +| |-ST +| |-< +| |-int +| `-> +`-:: +)txt", + R"txt( +NestedNameSpecifier +|-SimpleTemplateNameSpecifier +| |-ST +| |-< +| |-int +| `-> +|-:: +|-IdentifierNameSpecifier +| `-S +`-:: +)txt", + R"txt( +NestedNameSpecifier +|-SimpleTemplateNameSpecifier +| |-ST +| |-< +| |-int +| `-> +|-:: +|-IdentifierNameSpecifier +| `-S +`-:: +)txt"})); } TEST_P(SyntaxTreeTest, QualifiedIdWithDependentType) { @@ -1476,34 +1371,21 @@ if (!GetParam().isCXX11OrLater()) { return; } - EXPECT_TRUE(treeDumpEqual( + EXPECT_TRUE(treeDumpEqualOnAnnotations( R"cpp( void test() { - 12ll; - 12ull; + [[12ll]]; + [[12ull]]; } )cpp", - R"txt( -*: TranslationUnit -`-SimpleDeclaration - |-void - |-SimpleDeclarator - | |-test - | `-ParametersAndQualifiers - | |-( - | `-) - `-CompoundStatement - |-{ - |-ExpressionStatement - | |-IntegerLiteralExpression - | | `-12ll - | `-; - |-ExpressionStatement - | |-IntegerLiteralExpression - | | `-12ull - | `-; - `-} -)txt")); + {R"txt( +IntegerLiteralExpression +`-12ll +)txt", + R"txt( +IntegerLiteralExpression +`-12ull +)txt"})); } TEST_P(SyntaxTreeTest, IntegerLiteralBinary) {