Convert parts of Rewriter to StringRef based API.
- Please accept my sincere apologies for the gratuitous elimination of code duplication, manual string length counting, unnecessary strlen calls, etc. llvm-svn: 79448
This commit is contained in:
parent
6c528bc7ae
commit
dec484abfb
|
@ -22,6 +22,7 @@
|
|||
#include <cstring>
|
||||
#include <string>
|
||||
#include "clang/Rewrite/DeltaTree.h"
|
||||
#include "llvm/ADT/StringRef.h"
|
||||
|
||||
namespace clang {
|
||||
class SourceManager;
|
||||
|
@ -59,7 +60,7 @@ public:
|
|||
/// the buffer is specified relative to the original SourceBuffer. The
|
||||
/// text is inserted after the specified location.
|
||||
///
|
||||
void InsertText(unsigned OrigOffset, const char *StrData, unsigned StrLen,
|
||||
void InsertText(unsigned OrigOffset, const llvm::StringRef &Str,
|
||||
bool InsertAfter = true);
|
||||
|
||||
|
||||
|
@ -67,25 +68,23 @@ public:
|
|||
/// where the offset in the buffer is specified relative to the original
|
||||
/// SourceBuffer.
|
||||
///
|
||||
void InsertTextBefore(unsigned OrigOffset, const char *StrData,
|
||||
unsigned StrLen) {
|
||||
InsertText(OrigOffset, StrData, StrLen, false);
|
||||
void InsertTextBefore(unsigned OrigOffset, const llvm::StringRef &Str) {
|
||||
InsertText(OrigOffset, Str, false);
|
||||
}
|
||||
|
||||
/// InsertText - Insert some text at the specified point, where the offset in
|
||||
/// the buffer is specified relative to the original SourceBuffer. The
|
||||
/// text is inserted after the specified location. This is method is the
|
||||
/// same as InsertText with "InsertAfter == false".
|
||||
void InsertTextAfter(unsigned OrigOffset, const char *StrData,
|
||||
unsigned StrLen) {
|
||||
InsertText(OrigOffset, StrData, StrLen);
|
||||
void InsertTextAfter(unsigned OrigOffset, const llvm::StringRef &Str) {
|
||||
InsertText(OrigOffset, Str);
|
||||
}
|
||||
|
||||
/// ReplaceText - This method replaces a range of characters in the input
|
||||
/// buffer with a new string. This is effectively a combined "remove/insert"
|
||||
/// operation.
|
||||
void ReplaceText(unsigned OrigOffset, unsigned OrigLength,
|
||||
const char *NewStr, unsigned NewLength);
|
||||
const llvm::StringRef &NewStr);
|
||||
|
||||
private: // Methods only usable by Rewriter.
|
||||
|
||||
|
@ -159,7 +158,7 @@ public:
|
|||
/// InsertText - Insert the specified string at the specified location in the
|
||||
/// original buffer. This method returns true (and does nothing) if the input
|
||||
/// location was not rewritable, false otherwise.
|
||||
bool InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen,
|
||||
bool InsertText(SourceLocation Loc, const llvm::StringRef &Str,
|
||||
bool InsertAfter = true);
|
||||
|
||||
/// InsertTextAfter - Insert the specified string at the specified location in
|
||||
|
@ -167,9 +166,8 @@ public:
|
|||
/// the input location was not rewritable, false otherwise. Text is
|
||||
/// inserted after any other text that has been previously inserted
|
||||
/// at the some point (the default behavior for InsertText).
|
||||
bool InsertTextAfter(SourceLocation Loc, const char *StrData,
|
||||
unsigned StrLen) {
|
||||
return InsertText(Loc, StrData, StrLen);
|
||||
bool InsertTextAfter(SourceLocation Loc, const llvm::StringRef &Str) {
|
||||
return InsertText(Loc, Str, false);
|
||||
}
|
||||
|
||||
/// InsertText - Insert the specified string at the specified location in the
|
||||
|
@ -177,27 +175,26 @@ public:
|
|||
/// location was not rewritable, false otherwise. Text is
|
||||
/// inserted before any other text that has been previously inserted
|
||||
/// at the some point.
|
||||
bool InsertTextBefore(SourceLocation Loc, const char *StrData,
|
||||
unsigned StrLen) {
|
||||
return InsertText(Loc, StrData, StrLen, false);
|
||||
bool InsertTextBefore(SourceLocation Loc, const llvm::StringRef &Str) {
|
||||
return InsertText(Loc, Str, false);
|
||||
}
|
||||
|
||||
|
||||
bool InsertCStrBefore(SourceLocation Loc, const char* Str) {
|
||||
return InsertTextBefore(Loc, Str, strlen(Str));
|
||||
return InsertTextBefore(Loc, Str);
|
||||
}
|
||||
|
||||
|
||||
bool InsertCStrAfter(SourceLocation Loc, const char* Str) {
|
||||
return InsertTextAfter(Loc, Str, strlen(Str));
|
||||
return InsertTextAfter(Loc, Str);
|
||||
}
|
||||
|
||||
bool InsertStrBefore(SourceLocation Loc, const std::string& S) {
|
||||
return S.empty() ? false : InsertTextBefore(Loc, &S[0], S.size());
|
||||
bool InsertStrBefore(SourceLocation Loc, const std::string& Str) {
|
||||
return InsertTextBefore(Loc, Str);
|
||||
}
|
||||
|
||||
bool InsertStrAfter(SourceLocation Loc, const std::string& S) {
|
||||
return S.empty() ? false : InsertTextAfter(Loc, &S[0], S.size());
|
||||
bool InsertStrAfter(SourceLocation Loc, const std::string& Str) {
|
||||
return InsertTextAfter(Loc, Str);
|
||||
}
|
||||
|
||||
|
||||
|
@ -208,7 +205,7 @@ public:
|
|||
/// buffer with a new string. This is effectively a combined "remove/insert"
|
||||
/// operation.
|
||||
bool ReplaceText(SourceLocation Start, unsigned OrigLength,
|
||||
const char *NewStr, unsigned NewLength);
|
||||
const llvm::StringRef &NewStr);
|
||||
|
||||
/// ReplaceStmt - This replaces a Stmt/Expr with another, using the pretty
|
||||
/// printer to generate the replacement code. This returns true if the input
|
||||
|
|
|
@ -175,8 +175,7 @@ void FixItRewriter::HandleDiagnostic(Diagnostic::Level DiagLevel,
|
|||
// We're replacing code.
|
||||
if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(),
|
||||
Rewrite.getRangeSize(Hint.RemoveRange),
|
||||
Hint.CodeToInsert.c_str(),
|
||||
Hint.CodeToInsert.size()))
|
||||
Hint.CodeToInsert))
|
||||
Failed = true;
|
||||
}
|
||||
|
||||
|
|
|
@ -236,7 +236,8 @@ void RewriteBlocks::InsertText(SourceLocation Loc, const char *StrData,
|
|||
|
||||
void RewriteBlocks::ReplaceText(SourceLocation Start, unsigned OrigLength,
|
||||
const char *NewStr, unsigned NewLength) {
|
||||
if (!Rewrite.ReplaceText(Start, OrigLength, NewStr, NewLength))
|
||||
if (!Rewrite.ReplaceText(Start, OrigLength,
|
||||
llvm::StringRef(NewStr, NewLength)))
|
||||
return;
|
||||
Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag);
|
||||
}
|
||||
|
|
|
@ -129,13 +129,13 @@ void clang::RewriteMacrosInInput(Preprocessor &PP, llvm::raw_ostream *OS) {
|
|||
const IdentifierInfo *II = RawTokens[CurRawTok].getIdentifierInfo();
|
||||
if (!strcmp(II->getName(), "warning")) {
|
||||
// Comment out #warning.
|
||||
RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//", 2);
|
||||
RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//");
|
||||
} else if (!strcmp(II->getName(), "pragma") &&
|
||||
RawTokens[CurRawTok+1].is(tok::identifier) &&
|
||||
!strcmp(RawTokens[CurRawTok+1].getIdentifierInfo()->getName(),
|
||||
"mark")){
|
||||
// Comment out #pragma mark.
|
||||
RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//", 2);
|
||||
RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -165,7 +165,7 @@ void clang::RewriteMacrosInInput(Preprocessor &PP, llvm::raw_ostream *OS) {
|
|||
// Comment out a whole run of tokens instead of bracketing each one with
|
||||
// comments. Add a leading space if RawTok didn't have one.
|
||||
bool HasSpace = RawTok.hasLeadingSpace();
|
||||
RB.InsertTextAfter(RawOffs, " /*"+HasSpace, 2+!HasSpace);
|
||||
RB.InsertTextAfter(RawOffs, " /*"+HasSpace);
|
||||
unsigned EndPos;
|
||||
|
||||
do {
|
||||
|
@ -183,7 +183,7 @@ void clang::RewriteMacrosInInput(Preprocessor &PP, llvm::raw_ostream *OS) {
|
|||
} while (RawOffs <= PPOffs && !RawTok.isAtStartOfLine() &&
|
||||
(PPOffs != RawOffs || !isSameToken(RawTok, PPTok)));
|
||||
|
||||
RB.InsertTextBefore(EndPos, "*/", 2);
|
||||
RB.InsertTextBefore(EndPos, "*/");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -199,7 +199,7 @@ void clang::RewriteMacrosInInput(Preprocessor &PP, llvm::raw_ostream *OS) {
|
|||
PPOffs = SM.getFileOffset(PPLoc);
|
||||
}
|
||||
Expansion += ' ';
|
||||
RB.InsertTextBefore(InsertPos, &Expansion[0], Expansion.size());
|
||||
RB.InsertTextBefore(InsertPos, Expansion);
|
||||
}
|
||||
|
||||
// Get the buffer corresponding to MainFileID. If we haven't changed it, then
|
||||
|
|
|
@ -175,7 +175,7 @@ namespace {
|
|||
const std::string &Str = S.str();
|
||||
|
||||
// If replacement succeeded or warning disabled return with no warning.
|
||||
if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, &Str[0], Str.size())) {
|
||||
if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) {
|
||||
ReplacedNodes[Old] = New;
|
||||
return;
|
||||
}
|
||||
|
@ -188,7 +188,8 @@ namespace {
|
|||
void InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen,
|
||||
bool InsertAfter = true) {
|
||||
// If insertion succeeded or warning disabled return with no warning.
|
||||
if (!Rewrite.InsertText(Loc, StrData, StrLen, InsertAfter) ||
|
||||
if (!Rewrite.InsertText(Loc, llvm::StringRef(StrData, StrLen),
|
||||
InsertAfter) ||
|
||||
SilenceRewriteMacroWarning)
|
||||
return;
|
||||
|
||||
|
@ -206,7 +207,8 @@ namespace {
|
|||
void ReplaceText(SourceLocation Start, unsigned OrigLength,
|
||||
const char *NewStr, unsigned NewLength) {
|
||||
// If removal succeeded or warning disabled return with no warning.
|
||||
if (!Rewrite.ReplaceText(Start, OrigLength, NewStr, NewLength) ||
|
||||
if (!Rewrite.ReplaceText(Start, OrigLength,
|
||||
llvm::StringRef(NewStr, NewLength)) ||
|
||||
SilenceRewriteMacroWarning)
|
||||
return;
|
||||
|
||||
|
@ -1605,8 +1607,7 @@ Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) {
|
|||
assert((*bodyBuf == '{') && "bogus @catch body location");
|
||||
|
||||
buf += "1) { id _tmp = _caught;";
|
||||
Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1,
|
||||
buf.c_str(), buf.size());
|
||||
Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf);
|
||||
} else if (catchDecl) {
|
||||
QualType t = catchDecl->getType();
|
||||
if (t == Context->getObjCIdType()) {
|
||||
|
|
|
@ -53,8 +53,8 @@ void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E,
|
|||
const char *BufferStart,
|
||||
const char *StartTag, const char *EndTag) {
|
||||
// Insert the tag at the absolute start/end of the range.
|
||||
RB.InsertTextAfter(B, StartTag, strlen(StartTag));
|
||||
RB.InsertTextBefore(E, EndTag, strlen(EndTag));
|
||||
RB.InsertTextAfter(B, StartTag);
|
||||
RB.InsertTextBefore(E, EndTag);
|
||||
|
||||
// Scan the range to see if there is a \r or \n. If so, and if the line is
|
||||
// not blank, insert tags on that line as well.
|
||||
|
@ -68,7 +68,7 @@ void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E,
|
|||
// Okay, we found a newline in the range. If we have an open tag, we need
|
||||
// to insert a close tag at the first non-whitespace before the newline.
|
||||
if (HadOpenTag)
|
||||
RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag, strlen(EndTag));
|
||||
RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag);
|
||||
|
||||
// Instead of inserting an open tag immediately after the newline, we
|
||||
// wait until we see a non-whitespace character. This prevents us from
|
||||
|
@ -87,7 +87,7 @@ void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E,
|
|||
default:
|
||||
// If there is no tag open, do it now.
|
||||
if (!HadOpenTag) {
|
||||
RB.InsertTextAfter(i, StartTag, strlen(StartTag));
|
||||
RB.InsertTextAfter(i, StartTag);
|
||||
HadOpenTag = true;
|
||||
}
|
||||
|
||||
|
@ -120,11 +120,11 @@ void html::EscapeText(Rewriter &R, FileID FID,
|
|||
|
||||
case ' ':
|
||||
if (EscapeSpaces)
|
||||
RB.ReplaceText(FilePos, 1, " ", 6);
|
||||
RB.ReplaceText(FilePos, 1, " ");
|
||||
++ColNo;
|
||||
break;
|
||||
case '\f':
|
||||
RB.ReplaceText(FilePos, 1, "<hr>", 4);
|
||||
RB.ReplaceText(FilePos, 1, "<hr>");
|
||||
ColNo = 0;
|
||||
break;
|
||||
|
||||
|
@ -133,25 +133,26 @@ void html::EscapeText(Rewriter &R, FileID FID,
|
|||
break;
|
||||
unsigned NumSpaces = 8-(ColNo&7);
|
||||
if (EscapeSpaces)
|
||||
RB.ReplaceText(FilePos, 1, " "
|
||||
" ", 6*NumSpaces);
|
||||
RB.ReplaceText(FilePos, 1,
|
||||
llvm::StringRef(" "
|
||||
" ", 6*NumSpaces));
|
||||
else
|
||||
RB.ReplaceText(FilePos, 1, " ", NumSpaces);
|
||||
RB.ReplaceText(FilePos, 1, llvm::StringRef(" ", NumSpaces));
|
||||
ColNo += NumSpaces;
|
||||
break;
|
||||
}
|
||||
case '<':
|
||||
RB.ReplaceText(FilePos, 1, "<", 4);
|
||||
RB.ReplaceText(FilePos, 1, "<");
|
||||
++ColNo;
|
||||
break;
|
||||
|
||||
case '>':
|
||||
RB.ReplaceText(FilePos, 1, ">", 4);
|
||||
RB.ReplaceText(FilePos, 1, ">");
|
||||
++ColNo;
|
||||
break;
|
||||
|
||||
case '&':
|
||||
RB.ReplaceText(FilePos, 1, "&", 5);
|
||||
RB.ReplaceText(FilePos, 1, "&");
|
||||
++ColNo;
|
||||
break;
|
||||
}
|
||||
|
@ -211,12 +212,10 @@ static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo,
|
|||
|
||||
if (B == E) { // Handle empty lines.
|
||||
OS << " </td></tr>";
|
||||
OS.flush();
|
||||
RB.InsertTextBefore(B, &Str[0], Str.size());
|
||||
RB.InsertTextBefore(B, OS.str());
|
||||
} else {
|
||||
OS.flush();
|
||||
RB.InsertTextBefore(B, &Str[0], Str.size());
|
||||
RB.InsertTextBefore(E, "</td></tr>", strlen("</td></tr>"));
|
||||
RB.InsertTextBefore(B, OS.str());
|
||||
RB.InsertTextBefore(E, "</td></tr>");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -260,10 +259,8 @@ void html::AddLineNumbers(Rewriter& R, FileID FID) {
|
|||
}
|
||||
|
||||
// Add one big table tag that surrounds all of the code.
|
||||
RB.InsertTextBefore(0, "<table class=\"code\">\n",
|
||||
strlen("<table class=\"code\">\n"));
|
||||
|
||||
RB.InsertTextAfter(FileEnd - FileBeg, "</table>", strlen("</table>"));
|
||||
RB.InsertTextBefore(0, "<table class=\"code\">\n");
|
||||
RB.InsertTextAfter(FileEnd - FileBeg, "</table>");
|
||||
}
|
||||
|
||||
void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, FileID FID,
|
||||
|
|
|
@ -34,30 +34,29 @@ void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size) {
|
|||
AddReplaceDelta(OrigOffset, -Size);
|
||||
}
|
||||
|
||||
void RewriteBuffer::InsertText(unsigned OrigOffset,
|
||||
const char *StrData, unsigned StrLen,
|
||||
void RewriteBuffer::InsertText(unsigned OrigOffset, const llvm::StringRef &Str,
|
||||
bool InsertAfter) {
|
||||
|
||||
// Nothing to insert, exit early.
|
||||
if (StrLen == 0) return;
|
||||
if (Str.empty()) return;
|
||||
|
||||
unsigned RealOffset = getMappedOffset(OrigOffset, InsertAfter);
|
||||
Buffer.insert(RealOffset, StrData, StrData+StrLen);
|
||||
Buffer.insert(RealOffset, Str.begin(), Str.end());
|
||||
|
||||
// Add a delta so that future changes are offset correctly.
|
||||
AddInsertDelta(OrigOffset, StrLen);
|
||||
AddInsertDelta(OrigOffset, Str.size());
|
||||
}
|
||||
|
||||
/// ReplaceText - This method replaces a range of characters in the input
|
||||
/// buffer with a new string. This is effectively a combined "remove+insert"
|
||||
/// operation.
|
||||
void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength,
|
||||
const char *NewStr, unsigned NewLength) {
|
||||
const llvm::StringRef &NewStr) {
|
||||
unsigned RealOffset = getMappedOffset(OrigOffset, true);
|
||||
Buffer.erase(RealOffset, OrigLength);
|
||||
Buffer.insert(RealOffset, NewStr, NewStr+NewLength);
|
||||
if (OrigLength != NewLength)
|
||||
AddReplaceDelta(OrigOffset, NewLength-OrigLength);
|
||||
Buffer.insert(RealOffset, NewStr.begin(), NewStr.end());
|
||||
if (OrigLength != NewStr.size())
|
||||
AddReplaceDelta(OrigOffset, NewStr.size() - OrigLength);
|
||||
}
|
||||
|
||||
|
||||
|
@ -174,12 +173,12 @@ RewriteBuffer &Rewriter::getEditBuffer(FileID FID) {
|
|||
|
||||
/// InsertText - Insert the specified string at the specified location in the
|
||||
/// original buffer.
|
||||
bool Rewriter::InsertText(SourceLocation Loc, const char *StrData,
|
||||
unsigned StrLen, bool InsertAfter) {
|
||||
bool Rewriter::InsertText(SourceLocation Loc, const llvm::StringRef &Str,
|
||||
bool InsertAfter) {
|
||||
if (!isRewritable(Loc)) return true;
|
||||
FileID FID;
|
||||
unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
|
||||
getEditBuffer(FID).InsertText(StartOffs, StrData, StrLen, InsertAfter);
|
||||
getEditBuffer(FID).InsertText(StartOffs, Str, InsertAfter);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -196,13 +195,12 @@ bool Rewriter::RemoveText(SourceLocation Start, unsigned Length) {
|
|||
/// buffer with a new string. This is effectively a combined "remove/insert"
|
||||
/// operation.
|
||||
bool Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength,
|
||||
const char *NewStr, unsigned NewLength) {
|
||||
const llvm::StringRef &NewStr) {
|
||||
if (!isRewritable(Start)) return true;
|
||||
FileID StartFileID;
|
||||
unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID);
|
||||
|
||||
getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength,
|
||||
NewStr, NewLength);
|
||||
getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength, NewStr);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -221,7 +219,7 @@ bool Rewriter::ReplaceStmt(Stmt *From, Stmt *To) {
|
|||
To->printPretty(S, 0, PrintingPolicy(*LangOpts));
|
||||
const std::string &Str = S.str();
|
||||
|
||||
ReplaceText(From->getLocStart(), Size, &Str[0], Str.size());
|
||||
ReplaceText(From->getLocStart(), Size, Str);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue