Skip to content

Commit 5891064

Browse files
Allow modifying the PrintingPolicy for fully qualified names.
Author: [email protected] llvm-svn: 331552
1 parent c2c2eb7 commit 5891064

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

clang/include/clang/AST/QualTypeNames.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ namespace TypeName {
7272
/// \param[in] WithGlobalNsPrefix - If true, then the global namespace
7373
/// specifier "::" will be prepended to the fully qualified name.
7474
std::string getFullyQualifiedName(QualType QT, const ASTContext &Ctx,
75+
const PrintingPolicy &Policy,
7576
bool WithGlobalNsPrefix = false);
7677

7778
/// \brief Generates a QualType that can be used to name the same type

clang/lib/AST/QualTypeNames.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,8 @@ QualType getFullyQualifiedType(QualType QT, const ASTContext &Ctx,
452452

453453
std::string getFullyQualifiedName(QualType QT,
454454
const ASTContext &Ctx,
455+
const PrintingPolicy &Policy,
455456
bool WithGlobalNsPrefix) {
456-
PrintingPolicy Policy(Ctx.getPrintingPolicy());
457-
Policy.SuppressScope = false;
458-
Policy.AnonymousTagLocations = false;
459-
Policy.PolishForDeclaration = true;
460-
Policy.SuppressUnwrittenScope = true;
461457
QualType FQQT = getFullyQualifiedType(QT, Ctx, WithGlobalNsPrefix);
462458
return FQQT.getAsString(Policy);
463459
}

clang/unittests/Tooling/QualTypeNamesTest.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ struct TypeNameVisitor : TestVisitor<TypeNameVisitor> {
2626
std::string ExpectedName =
2727
ExpectedQualTypeNames.lookup(VD->getNameAsString());
2828
if (ExpectedName != "") {
29-
std::string ActualName =
30-
TypeName::getFullyQualifiedName(VD->getType(), *Context,
31-
WithGlobalNsPrefix);
29+
PrintingPolicy Policy(Context->getPrintingPolicy());
30+
Policy.SuppressScope = false;
31+
Policy.AnonymousTagLocations = true;
32+
Policy.PolishForDeclaration = true;
33+
Policy.SuppressUnwrittenScope = true;
34+
std::string ActualName = TypeName::getFullyQualifiedName(
35+
VD->getType(), *Context, Policy, WithGlobalNsPrefix);
3236
if (ExpectedName != ActualName) {
3337
// A custom message makes it much easier to see what declaration
3438
// failed compared to EXPECT_EQ.
@@ -217,6 +221,26 @@ TEST(QualTypeNameTest, getFullyQualifiedName) {
217221
" }\n"
218222
"}\n"
219223
);
224+
225+
TypeNameVisitor AnonStrucs;
226+
AnonStrucs.ExpectedQualTypeNames["a"] = "short";
227+
AnonStrucs.ExpectedQualTypeNames["un_in_st_1"] =
228+
"union (anonymous struct at input.cc:1:1)::(anonymous union at "
229+
"input.cc:2:27)";
230+
AnonStrucs.ExpectedQualTypeNames["b"] = "short";
231+
AnonStrucs.ExpectedQualTypeNames["un_in_st_2"] =
232+
"union (anonymous struct at input.cc:1:1)::(anonymous union at "
233+
"input.cc:5:27)";
234+
AnonStrucs.ExpectedQualTypeNames["anon_st"] =
235+
"struct (anonymous struct at input.cc:1:1)";
236+
AnonStrucs.runOver(R"(struct {
237+
union {
238+
short a;
239+
} un_in_st_1;
240+
union {
241+
short b;
242+
} un_in_st_2;
243+
} anon_st;)");
220244
}
221245

222246
} // end anonymous namespace

0 commit comments

Comments
 (0)