diff options
author | Bruno Ricci <riccibrun@gmail.com> | 2020-08-05 12:10:16 +0100 |
---|---|---|
committer | Bruno Ricci <riccibrun@gmail.com> | 2020-08-05 13:54:38 +0100 |
commit | f7a039de7af7b83105f3e0345d65dceda1a0e0d4 (patch) | |
tree | 7a41bef626cb6f45ad290e1eb665134e6ff27451 /clang/unittests/AST/DeclPrinterTest.cpp | |
parent | 94b43118e2203fed8ca0377ae762c08189aa6f3d (diff) | |
download | llvm-f7a039de7af7b83105f3e0345d65dceda1a0e0d4.zip llvm-f7a039de7af7b83105f3e0345d65dceda1a0e0d4.tar.gz llvm-f7a039de7af7b83105f3e0345d65dceda1a0e0d4.tar.bz2 |
[clang][NFC] DeclPrinter: use NamedDecl::getDeclName instead of NamedDecl::printName to print the name of enumerations, namespaces and template parameters.
NamedDecl::printName will print the pretty-printed name of the entity, which
is not what we want here (we should print "enum { e };" instead of "enum
(unnamed enum at input.cc:1:5) { e };").
For now only DecompositionDecl and MDGuidDecl have an overloaded printName so
this does not result in any functional change, but this change is needed since
I will be adding overloads to better handle unnamed entities in diagnostics.
Diffstat (limited to 'clang/unittests/AST/DeclPrinterTest.cpp')
-rw-r--r-- | clang/unittests/AST/DeclPrinterTest.cpp | 130 |
1 files changed, 118 insertions, 12 deletions
diff --git a/clang/unittests/AST/DeclPrinterTest.cpp b/clang/unittests/AST/DeclPrinterTest.cpp index 939c8b5..38e46a3 100644 --- a/clang/unittests/AST/DeclPrinterTest.cpp +++ b/clang/unittests/AST/DeclPrinterTest.cpp @@ -37,6 +37,7 @@ void PrintDecl(raw_ostream &Out, const ASTContext *Context, const Decl *D, PrintingPolicyModifier PolicyModifier) { PrintingPolicy Policy = Context->getPrintingPolicy(); Policy.TerseOutput = true; + Policy.Indentation = 0; if (PolicyModifier) PolicyModifier(Policy); D->print(Out, Policy, /*Indentation*/ 0, /*PrintInstantiation*/ false); @@ -162,14 +163,21 @@ PrintedDeclCXX98Matches(StringRef Code, const DeclarationMatcher &NodeMatch, } ::testing::AssertionResult -PrintedDeclCXX1ZMatches(StringRef Code, const DeclarationMatcher &NodeMatch, - StringRef ExpectedPrinted) { - std::vector<std::string> Args(1, "-std=c++1z"); - return PrintedDeclMatches(Code, - Args, - NodeMatch, - ExpectedPrinted, - "input.cc"); +PrintedDeclCXX17Matches(StringRef Code, const DeclarationMatcher &NodeMatch, + StringRef ExpectedPrinted, + PrintingPolicyModifier PolicyModifier = nullptr) { + std::vector<std::string> Args(1, "-std=c++17"); + return PrintedDeclMatches(Code, Args, NodeMatch, ExpectedPrinted, "input.cc", + PolicyModifier); +} + +::testing::AssertionResult +PrintedDeclC11Matches(StringRef Code, const DeclarationMatcher &NodeMatch, + StringRef ExpectedPrinted, + PrintingPolicyModifier PolicyModifier = nullptr) { + std::vector<std::string> Args(1, "-std=c11"); + return PrintedDeclMatches(Code, Args, NodeMatch, ExpectedPrinted, "input.c", + PolicyModifier); } ::testing::AssertionResult @@ -250,6 +258,72 @@ TEST(DeclPrinter, TestNamespaceAlias2) { // Should be: with semicolon } +TEST(DeclPrinter, TestNamespaceUnnamed) { + ASSERT_TRUE(PrintedDeclCXX17Matches( + "namespace { int X; }", + namespaceDecl(has(varDecl(hasName("X")))).bind("id"), + "namespace {\nint X;\n}", + [](PrintingPolicy &Policy) { Policy.TerseOutput = false; })); +} + +TEST(DeclPrinter, TestNamespaceUsingDirective) { + ASSERT_TRUE(PrintedDeclCXX17Matches( + "namespace X { namespace A {} }" + "using namespace X::A;", + usingDirectiveDecl().bind("id"), "using namespace X::A", + [](PrintingPolicy &Policy) { Policy.TerseOutput = false; })); +} + +TEST(DeclPrinter, TestEnumDecl1) { + ASSERT_TRUE(PrintedDeclCXX17Matches( + "enum A { a0, a1, a2 };", enumDecl(hasName("A")).bind("id"), + "enum A {\na0,\na1,\na2\n}", + [](PrintingPolicy &Policy) { Policy.TerseOutput = false; })); +} + +TEST(DeclPrinter, TestEnumDecl2) { + ASSERT_TRUE(PrintedDeclCXX17Matches( + "enum A { a0 = -1, a1, a2 = 1 };", enumDecl(hasName("A")).bind("id"), + "enum A {\na0 = -1,\na1,\na2 = 1\n}", + [](PrintingPolicy &Policy) { Policy.TerseOutput = false; })); +} + +TEST(DeclPrinter, TestEnumDecl3) { + ASSERT_TRUE(PrintedDeclCXX17Matches( + "enum { a0, a1, a2 };", + enumDecl(has(enumConstantDecl(hasName("a0")))).bind("id"), + "enum {\na0,\na1,\na2\n}", + [](PrintingPolicy &Policy) { Policy.TerseOutput = false; })); +} + +TEST(DeclPrinter, TestEnumDecl4) { + ASSERT_TRUE(PrintedDeclCXX17Matches( + "enum class A { a0, a1, a2 };", enumDecl(hasName("A")).bind("id"), + "enum class A : int {\na0,\na1,\na2\n}", + [](PrintingPolicy &Policy) { Policy.TerseOutput = false; })); +} + +TEST(DeclPrinter, TestRecordDecl1) { + ASSERT_TRUE(PrintedDeclC11Matches( + "struct A { int a; };", recordDecl(hasName("A")).bind("id"), + "struct A {\nint a;\n}", + [](PrintingPolicy &Policy) { Policy.TerseOutput = false; })); +} + +TEST(DeclPrinter, TestRecordDecl2) { + ASSERT_TRUE(PrintedDeclC11Matches( + "struct A { struct { int i; }; };", recordDecl(hasName("A")).bind("id"), + "struct A {\nstruct {\nint i;\n};\n}", + [](PrintingPolicy &Policy) { Policy.TerseOutput = false; })); +} + +TEST(DeclPrinter, TestRecordDecl3) { + ASSERT_TRUE(PrintedDeclC11Matches( + "union { int A; } u;", + recordDecl(has(fieldDecl(hasName("A")))).bind("id"), "union {\nint A;\n}", + [](PrintingPolicy &Policy) { Policy.TerseOutput = false; })); +} + TEST(DeclPrinter, TestCXXRecordDecl1) { ASSERT_TRUE(PrintedDeclCXX98Matches( "class A { int a; };", @@ -1119,6 +1193,39 @@ TEST(DeclPrinter, TestFunctionTemplateDecl6) { "template <typename U> void A(U t)")); } +TEST(DeclPrinter, TestUnnamedTemplateParameters) { + ASSERT_TRUE(PrintedDeclCXX17Matches( + "template <typename, int, template <typename, bool> class> void A();", + functionTemplateDecl(hasName("A")).bind("id"), + "template <typename, int, template <typename, bool> class> void A()")); +} + +TEST(DeclPrinter, TestUnnamedTemplateParametersPacks) { + ASSERT_TRUE(PrintedDeclCXX17Matches( + "template <typename ..., int ...," + " template <typename ..., bool ...> class ...> void A();", + functionTemplateDecl(hasName("A")).bind("id"), + "template <typename ..., int ...," + " template <typename ..., bool ...> class ...> void A()")); +} + +TEST(DeclPrinter, TestNamedTemplateParametersPacks) { + ASSERT_TRUE(PrintedDeclCXX17Matches( + "template <typename ...T, int ...I," + " template <typename ...X, bool ...B> class ...Z> void A();", + functionTemplateDecl(hasName("A")).bind("id"), + "template <typename ...T, int ...I," + " template <typename ...X, bool ...B> class ...Z> void A()")); +} + +TEST(DeclPrinter, TestTemplateTemplateParameterWrittenWithTypename) { + ASSERT_TRUE(PrintedDeclCXX17Matches( + "template <template <typename> typename Z> void A();", + functionTemplateDecl(hasName("A")).bind("id"), + "template <template <typename> class Z> void A()")); + // WRONG: We should use typename if the parameter was written with it. +} + TEST(DeclPrinter, TestTemplateArgumentList1) { ASSERT_TRUE(PrintedDeclCXX98Matches( "template<typename T> struct Z {};" @@ -1282,10 +1389,9 @@ TEST(DeclPrinter, TestTemplateArgumentList16) { } TEST(DeclPrinter, TestStaticAssert1) { - ASSERT_TRUE(PrintedDeclCXX1ZMatches( - "static_assert(true);", - staticAssertDecl().bind("id"), - "static_assert(true)")); + ASSERT_TRUE(PrintedDeclCXX17Matches("static_assert(true);", + staticAssertDecl().bind("id"), + "static_assert(true)")); } TEST(DeclPrinter, TestObjCMethod1) { |