Skip to content

Commit d0e99cc

Browse files
Add insertText function to insert text with same checks as rewriteSourceRange.
1 parent f8ee74e commit d0e99cc

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

clang/include/clang/3C/RewriteUtils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,7 @@ void rewriteSourceRange(Rewriter &R, const CharSourceRange &Range,
186186

187187
void rewriteSourceRange(Rewriter &R, const SourceRange &Range,
188188
const std::string &NewText, bool ErrFail = true);
189+
void insertText(Rewriter &R, SourceLocation S, const std::string &NewText,
190+
bool ErrFail = true);
189191

190192
#endif // LLVM_CLANG_3C_REWRITEUTILS_H

clang/lib/3C/DeclRewriter.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -566,23 +566,17 @@ void DeclRewriter::rewriteFunctionDecl(FunctionDeclReplacement *N) {
566566
void DeclRewriter::emitSupplementaryDeclarations(
567567
const std::vector<std::string> &SDecls, SourceLocation Loc) {
568568
// There are no supplementary declarations to emit. The AllDecls String
569-
// will remain empty, so R.InsertTextAfterToken should no-op, but it's still
570-
// and error to insert an empty string and an invalid source location,
571-
// so short circuit here to be safe.
569+
// will remain empty, so insertText should no-op, but it's still an error to
570+
// insert an empty string at an invalid source location, so short circuit here
571+
// to be safe.
572572
if (SDecls.empty())
573573
return;
574574

575575
std::string AllDecls;
576576
for (std::string D : SDecls)
577577
AllDecls += "\n" + D;
578578

579-
bool RewriteSucess = !R.InsertTextAfterToken(Loc, AllDecls);
580-
if (!RewriteSucess) {
581-
// FIXME: Use rewriteSourceRange so that it handle emitting error messages.
582-
llvm::errs()
583-
<< "Rewriting failed while emitting supplementary declaration.\n";
584-
Loc.print(llvm::errs(), R.getSourceMgr());
585-
}
579+
insertText(R, Loc, AllDecls);
586580
}
587581

588582
// A function to detect the presence of inline struct declarations

clang/lib/3C/RewriteUtils.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ void rewriteSourceRange(Rewriter &R, const CharSourceRange &Range,
116116
}
117117
}
118118

119+
void insertText(Rewriter &R, SourceLocation S, const std::string &NewText,
120+
bool ErrFail) {
121+
SourceRange SR(
122+
Lexer::getLocForEndOfToken(S, 0, R.getSourceMgr(), R.getLangOpts()), S);
123+
rewriteSourceRange(R, SR, NewText, ErrFail);
124+
}
125+
119126
static void emit(Rewriter &R, ASTContext &C, bool &StdoutModeEmittedMainFile) {
120127
if (_3COpts.Verbose)
121128
errs() << "Writing files out\n";
@@ -450,15 +457,8 @@ class AssignmentUpdater : public RecursiveASTVisitor<AssignmentUpdater> {
450457
if (ABInfo.needsRangeBound(CV)) {
451458
std::string TmpVarName = "__3c_tmp_" + CV->getName();
452459
rewriteSourceRange(R, O->getLHS()->getSourceRange(), TmpVarName);
453-
bool InsertFail = R.InsertTextAfterToken(O->getEndLoc(),
454-
", " + CV->getName() +
455-
" = " + TmpVarName);
456-
if (InsertFail) {
457-
// FIXME: Use rewriteSourceRange so that it handle emitting error messages.
458-
llvm::errs()
459-
<< "Rewriting failed while updating assignment!\n";
460-
O->getEndLoc().print(llvm::errs(), R.getSourceMgr());
461-
}
460+
insertText(R, O->getEndLoc(),
461+
", " + CV->getName() + " = " + TmpVarName);
462462
}
463463
}
464464
}

0 commit comments

Comments
 (0)