From 886c8db545170850f7806f47b5f6120864effd09 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Tue, 18 Mar 2008 21:17:59 +0000 Subject: [PATCH] Added variant of "InsertText" in the Rewriter to support inserting text both *before* and after a specific location. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@48504 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Rewrite/Rewriter.h | 51 ++++++++++++++++++++++++++++---- lib/Rewrite/Rewriter.cpp | 12 ++++---- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/include/clang/Rewrite/Rewriter.h b/include/clang/Rewrite/Rewriter.h index ffa5db968c..70a863c73d 100644 --- a/include/clang/Rewrite/Rewriter.h +++ b/include/clang/Rewrite/Rewriter.h @@ -87,12 +87,30 @@ private: // Methods only usable by Rewriter. void RemoveText(unsigned OrigOffset, unsigned Size); /// InsertText - Insert some text at the specified point, where the offset in - /// the buffer is specified relative to the original SourceBuffer. + /// the buffer is specified relative to the original SourceBuffer. The + /// text is inserted after the specified location. /// - /// TODO: Consider a bool to indicate whether the text is inserted 'before' or - /// after the atomic point: i.e. whether the atomic point is moved to after - /// the inserted text or not. - void InsertText(unsigned OrigOffset, const char *StrData, unsigned StrLen); + void InsertText(unsigned OrigOffset, const char *StrData, unsigned StrLen, + bool InsertAfter = true); + + + /// InsertTextBefore - Insert some text before the specified point, + /// 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); + } + + /// 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); + } /// ReplaceText - This method replaces a range of characters in the input /// buffer with a new string. This is effectively a combined "remove/insert" @@ -130,7 +148,28 @@ 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 char *StrData, unsigned StrLen, + bool InsertAfter = true); + + /// InsertTextAfter - 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. 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); + } + + /// 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. 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); + } /// RemoveText - Remove the specified text region. bool RemoveText(SourceLocation Start, unsigned Length); diff --git a/lib/Rewrite/Rewriter.cpp b/lib/Rewrite/Rewriter.cpp index e3cc977bea..7b35cf1c59 100644 --- a/lib/Rewrite/Rewriter.cpp +++ b/lib/Rewrite/Rewriter.cpp @@ -101,11 +101,13 @@ void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size) { } void RewriteBuffer::InsertText(unsigned OrigOffset, - const char *StrData, unsigned StrLen) { + const char *StrData, unsigned StrLen, + bool InsertAfter) { + // Nothing to insert, exit early. if (StrLen == 0) return; - unsigned RealOffset = getMappedOffset(OrigOffset, true); + unsigned RealOffset = getMappedOffset(OrigOffset, InsertAfter); assert(RealOffset <= Buffer.size() && "Invalid location"); // Insert the new characters. @@ -207,12 +209,12 @@ RewriteBuffer &Rewriter::getEditBuffer(unsigned FileID) { /// InsertText - Insert the specified string at the specified location in the /// original buffer. -bool Rewriter::InsertText(SourceLocation Loc, - const char *StrData, unsigned StrLen) { +bool Rewriter::InsertText(SourceLocation Loc, const char *StrData, + unsigned StrLen, bool InsertAfter) { if (!isRewritable(Loc)) return true; unsigned FileID; unsigned StartOffs = getLocationOffsetAndFileID(Loc, FileID); - getEditBuffer(FileID).InsertText(StartOffs, StrData, StrLen); + getEditBuffer(FileID).InsertText(StartOffs, StrData, StrLen, InsertAfter); return false; } -- 2.40.0