Skip to content

Commit d3de950

Browse files
Use PrintingPolicy to exclude any initializer when printing a VarDecl.
This is much nicer than temporarily mutating the VarDecl to remove the initializer.
1 parent 0c2b38c commit d3de950

File tree

1 file changed

+4
-16
lines changed

1 file changed

+4
-16
lines changed

clang/lib/3C/RewriteUtils.cpp

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,13 @@ std::string mkStringForDeclWithUnchangedType(MultiDeclMemberDecl *MMD,
6060
ProgramInfo &Info) {
6161
bool BaseTypeRenamed = Info.TheMultiDeclsInfo.wasBaseTypeRenamed(MMD);
6262
if (!BaseTypeRenamed) {
63-
// As far as we know, we can let Clang generate the declaration string. To
64-
// get it without any initializer, we temporarily mutate the Decl to remove
65-
// the initializer: a hack, but there isn't an obvious better way.
66-
auto *VD = dyn_cast<VarDecl>(MMD);
67-
Expr *Init = nullptr;
68-
if (VD && VD->hasInit()) {
69-
Init = VD->getInit();
70-
VD->setInit(nullptr);
71-
}
72-
63+
// As far as we know, we can let Clang generate the declaration string.
64+
PrintingPolicy Policy = MMD->getASTContext().getPrintingPolicy();
65+
Policy.SuppressInitializers = true;
7366
std::string DeclStr = "";
7467
raw_string_ostream DeclStream(DeclStr);
75-
MMD->print(DeclStream);
68+
MMD->print(DeclStream, Policy);
7669
assert("Original decl string empty." && !DeclStr.empty());
77-
78-
// Undo the mutation, if applicable.
79-
if (VD && Init)
80-
VD->setInit(Init);
81-
8270
return DeclStr;
8371
}
8472

0 commit comments

Comments
 (0)