Remove code duplication and cut dependency from clangRewrite on

clangAST.

llvm-svn: 220502
This commit is contained in:
Daniel Jasper 2014-10-23 19:47:36 +00:00
parent 957eae22f4
commit 4475a249e7
5 changed files with 12 additions and 61 deletions

View File

@ -245,11 +245,6 @@ public:
/// operation.
bool ReplaceText(SourceRange range, SourceRange replacementRange);
/// ReplaceStmt - This replaces a Stmt/Expr with another, using the pretty
/// printer to generate the replacement code. This returns true if the input
/// could not be rewritten, or false if successful.
bool ReplaceStmt(Stmt *From, Stmt *To);
/// \brief Increase indentation for the lines between the given source range.
/// To determine what the indentation should be, 'parentIndent' is used
/// that should be at a source location with an indentation one degree

View File

@ -249,27 +249,16 @@ namespace {
void HandleTranslationUnit(ASTContext &C) override;
void ReplaceStmt(Stmt *Old, Stmt *New) {
Stmt *ReplacingStmt = ReplacedNodes[Old];
if (ReplacingStmt)
return; // We can't rewrite the same node twice.
if (DisableReplaceStmt)
return;
// If replacement succeeded or warning disabled return with no warning.
if (!Rewrite.ReplaceStmt(Old, New)) {
ReplacedNodes[Old] = New;
return;
}
if (SilenceRewriteMacroWarning)
return;
Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag)
<< Old->getSourceRange();
ReplaceStmtWithRange(Old, New, Old->getSourceRange());
}
void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) {
assert(Old != nullptr && New != nullptr && "Expected non-null Stmt's");
Stmt *ReplacingStmt = ReplacedNodes[Old];
if (ReplacingStmt)
return; // We can't rewrite the same node twice.
if (DisableReplaceStmt)
return;

View File

@ -198,27 +198,16 @@ namespace {
void HandleTranslationUnit(ASTContext &C) override;
void ReplaceStmt(Stmt *Old, Stmt *New) {
Stmt *ReplacingStmt = ReplacedNodes[Old];
if (ReplacingStmt)
return; // We can't rewrite the same node twice.
if (DisableReplaceStmt)
return;
// If replacement succeeded or warning disabled return with no warning.
if (!Rewrite.ReplaceStmt(Old, New)) {
ReplacedNodes[Old] = New;
return;
}
if (SilenceRewriteMacroWarning)
return;
Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag)
<< Old->getSourceRange();
ReplaceStmtWithRange(Old, New, Old->getSourceRange());
}
void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) {
assert(Old != nullptr && New != nullptr && "Expected non-null Stmt's");
Stmt *ReplacingStmt = ReplacedNodes[Old];
if (ReplacingStmt)
return; // We can't rewrite the same node twice.
if (DisableReplaceStmt)
return;

View File

@ -10,7 +10,6 @@ add_clang_library(clangRewrite
TokenRewriter.cpp
LINK_LIBS
clangAST
clangBasic
clangLex
)

View File

@ -328,27 +328,6 @@ bool Rewriter::ReplaceText(SourceRange range, SourceRange replacementRange) {
return ReplaceText(start, origLength, MB.substr(newOffs, newLength));
}
/// ReplaceStmt - This replaces a Stmt/Expr with another, using the pretty
/// printer to generate the replacement code. This returns true if the input
/// could not be rewritten, or false if successful.
bool Rewriter::ReplaceStmt(Stmt *From, Stmt *To) {
assert(From != nullptr && To != nullptr && "Expected non-null Stmt's");
// Measaure the old text.
int Size = getRangeSize(From->getSourceRange());
if (Size == -1)
return true;
// Get the new text.
std::string SStr;
llvm::raw_string_ostream S(SStr);
To->printPretty(S, nullptr, PrintingPolicy(*LangOpts));
const std::string &Str = S.str();
ReplaceText(From->getLocStart(), Size, Str);
return false;
}
std::string Rewriter::ConvertToString(Stmt *From) {
assert(From != nullptr && "Expected non-null Stmt");
std::string SStr;