@@ -405,36 +405,27 @@ void DeclRewriter::rewriteMultiDecl(DeclReplacement *N, RSet &ToRewrite,
405
405
// When the type hasn't changed, we still need to insert the original
406
406
// type for the variable.
407
407
408
- // This is a bit of trickery needed to get a string representation of
409
- // the declaration without the initializer. We don't want to rewrite to
410
- // initializer because this causes problems when rewriting casts and
411
- // generic function calls later on. (issue 267)
412
- auto *VD = dyn_cast<VarDecl>(DL);
413
- Expr *Init = nullptr ;
414
- if (VD && VD->hasInit ()) {
415
- Init = VD->getInit ();
416
- VD->setInit (nullptr );
417
- }
418
-
419
- // Dump the declaration (without the initializer) to a string. Printing
420
- // the AST node gives the full declaration including the base type which
421
- // is not present in the multi-decl source code.
422
- std::string DeclStr = " " ;
423
- raw_string_ostream DeclStream (DeclStr);
424
- DL->print (DeclStream);
425
- assert (" Original decl string empty." && !DeclStream.str ().empty ());
408
+ // Note: rewriteMultiDecl is currently called only by
409
+ // rewriteFieldOrVarDecl with VarDecls or FieldDecls, and DeclaratorDecl
410
+ // is a superclass of both. When we add support for typedef multi-decls,
411
+ // we'll need to change this code.
412
+ DeclaratorDecl *DD = cast<DeclaratorDecl>(DL);
413
+ std::string NewDeclStr = mkStringForDeclWithUnchangedType (DD, A, Info);
414
+
415
+ // If the variable has an initializer, we want this rewrite to end
416
+ // before the initializer to avoid interfering with any other rewrites
417
+ // that 3C needs to make inside the initializer expression (issue 267).
418
+ // VarDecl's implementation of the getSourceRange virtual method
419
+ // includes the initializer, but we can manually call DeclaratorDecl's
420
+ // implementation, which excludes the initializer.
421
+ SourceLocation EndLoc = DD->DeclaratorDecl ::getSourceRange ().getEnd ();
426
422
427
423
// Do the replacement. PrevEnd is setup to be the source location of the
428
424
// comma after the previous declaration in the multi-decl. getEndLoc is
429
425
// either the end of the declaration or just before the initializer if
430
426
// one is present.
431
- SourceRange SR (PrevEnd, DL->getEndLoc ());
432
- rewriteSourceRange (R, SR, DeclStream.str ());
433
-
434
- // Undo prior trickery. This need to happen so that the PSL for the decl
435
- // is not changed since the PSL is used as a map key in a few places.
436
- if (VD && Init)
437
- VD->setInit (Init);
427
+ SourceRange SR (PrevEnd, DD->DeclaratorDecl ::getSourceRange ().getEnd ());
428
+ rewriteSourceRange (R, SR, NewDeclStr);
438
429
}
439
430
}
440
431
0 commit comments