Index: include/clang/Tooling/Core/QualTypeNames.h =================================================================== --- include/clang/Tooling/Core/QualTypeNames.h +++ include/clang/Tooling/Core/QualTypeNames.h @@ -74,6 +74,11 @@ std::string getFullyQualifiedName(QualType QT, const ASTContext &Ctx, bool WithGlobalNsPrefix = false); + +std::string getFullyQualifiedName(QualType QT, + const ASTContext &Ctx, + const PrintingPolicy &Policy, + bool WithGlobalNsPrefix = false); } // end namespace TypeName } // end namespace clang #endif // LLVM_CLANG_TOOLING_CORE_QUALTYPENAMES_H Index: lib/Tooling/Core/QualTypeNames.cpp =================================================================== --- lib/Tooling/Core/QualTypeNames.cpp +++ lib/Tooling/Core/QualTypeNames.cpp @@ -463,14 +463,21 @@ std::string getFullyQualifiedName(QualType QT, const ASTContext &Ctx, + const PrintingPolicy &Policy, + bool WithGlobalNsPrefix) { + QualType FQQT = getFullyQualifiedType(QT, Ctx, WithGlobalNsPrefix); + return FQQT.getAsString(Policy); +} + +std::string getFullyQualifiedName(QualType QT, + const ASTContext &Ctx, bool WithGlobalNsPrefix) { PrintingPolicy Policy(Ctx.getPrintingPolicy()); Policy.SuppressScope = false; Policy.AnonymousTagLocations = false; Policy.PolishForDeclaration = true; Policy.SuppressUnwrittenScope = true; - QualType FQQT = getFullyQualifiedType(QT, Ctx, WithGlobalNsPrefix); - return FQQT.getAsString(Policy); + return getFullyQualifiedName(QT, Ctx, Policy, WithGlobalNsPrefix); } } // end namespace TypeName Index: unittests/Tooling/QualTypeNamesTest.cpp =================================================================== --- unittests/Tooling/QualTypeNamesTest.cpp +++ unittests/Tooling/QualTypeNamesTest.cpp @@ -15,6 +15,7 @@ struct TypeNameVisitor : TestVisitor { llvm::StringMap ExpectedQualTypeNames; bool WithGlobalNsPrefix = false; + bool CustomPrintingPolicy = false; // ValueDecls are the least-derived decl with both a qualtype and a // name. @@ -26,9 +27,22 @@ std::string ExpectedName = ExpectedQualTypeNames.lookup(VD->getNameAsString()); if (ExpectedName != "") { - std::string ActualName = - TypeName::getFullyQualifiedName(VD->getType(), *Context, - WithGlobalNsPrefix); + std::string ActualName; + if (!CustomPrintingPolicy) + ActualName = + TypeName::getFullyQualifiedName(VD->getType(), *Context, + WithGlobalNsPrefix); + else { + PrintingPolicy Policy(Context->getPrintingPolicy()); + Policy.SuppressScope = false; + Policy.AnonymousTagLocations = true; + Policy.PolishForDeclaration = true; + Policy.SuppressUnwrittenScope = true; + ActualName = + TypeName::getFullyQualifiedName(VD->getType(), *Context, + Policy, WithGlobalNsPrefix); + } + if (ExpectedName != ActualName) { // A custom message makes it much easier to see what declaration // failed compared to EXPECT_EQ. @@ -217,6 +231,32 @@ " }\n" "}\n" ); + + TypeNameVisitor PrintingPolicy; + PrintingPolicy.CustomPrintingPolicy = true; + PrintingPolicy.ExpectedQualTypeNames["a"] = "short"; + PrintingPolicy.ExpectedQualTypeNames["un_in_st_1"] = + "union (anonymous struct at input.cc:1:1)::(anonymous union at " + "input.cc:3:3)"; + PrintingPolicy.ExpectedQualTypeNames["b"] = "short"; + PrintingPolicy.ExpectedQualTypeNames["un_in_st_2"] = + "union (anonymous struct at input.cc:1:1)::(anonymous union at " + "input.cc:7:3)"; + PrintingPolicy.ExpectedQualTypeNames["anon_st"] = + "struct (anonymous struct at input.cc:1:1)"; + PrintingPolicy.runOver( + "struct\n" + "{\n" + " union\n" + " {\n" + " short a;\n" + " } un_in_st_1;\n" + " union\n" + " {\n" + " short b;\n" + " } un_in_st_2;\n" + "} anon_st;\n" + ); } } // end anonymous namespace