mikhail.ramalho created this revision.
Herald added a subscriber: klimek.
In our user case, we need to generate unique names for every tag, including
anonymous structs/unions.
This adds a custom PrintingPolicy option to getFullyQualifiedName and the test
case shows that different names are generated for different anonymous
unions/structs.
https://reviews.llvm.org/D36610
Files:
include/clang/Tooling/Core/QualTypeNames.h
lib/Tooling/Core/QualTypeNames.cpp
unittests/Tooling/QualTypeNamesTest.cpp
Index: unittests/Tooling/QualTypeNamesTest.cpp
===================================================================
--- unittests/Tooling/QualTypeNamesTest.cpp
+++ unittests/Tooling/QualTypeNamesTest.cpp
@@ -15,6 +15,7 @@
struct TypeNameVisitor : TestVisitor<TypeNameVisitor> {
llvm::StringMap<std::string> 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
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: 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
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits