[cfe-commits] r48504 - in /cfe/trunk: include/clang/Rewrite/Rewriter.h lib/Rewrite/Rewriter.cpp

Ted Kremenek kremenek at apple.com
Tue Mar 18 14:17:59 PDT 2008


Author: kremenek
Date: Tue Mar 18 16:17:59 2008
New Revision: 48504

URL: http://llvm.org/viewvc/llvm-project?rev=48504&view=rev
Log:
Added variant of "InsertText" in the Rewriter to support inserting text both
*before* and after a specific location.

Modified:
    cfe/trunk/include/clang/Rewrite/Rewriter.h
    cfe/trunk/lib/Rewrite/Rewriter.cpp

Modified: cfe/trunk/include/clang/Rewrite/Rewriter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Rewrite/Rewriter.h?rev=48504&r1=48503&r2=48504&view=diff

==============================================================================
--- cfe/trunk/include/clang/Rewrite/Rewriter.h (original)
+++ cfe/trunk/include/clang/Rewrite/Rewriter.h Tue Mar 18 16:17:59 2008
@@ -87,12 +87,30 @@
   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 @@
   /// 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);

Modified: cfe/trunk/lib/Rewrite/Rewriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Rewrite/Rewriter.cpp?rev=48504&r1=48503&r2=48504&view=diff

==============================================================================
--- cfe/trunk/lib/Rewrite/Rewriter.cpp (original)
+++ cfe/trunk/lib/Rewrite/Rewriter.cpp Tue Mar 18 16:17:59 2008
@@ -101,11 +101,13 @@
 }
 
 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 @@
 
 /// 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;
 }
 





More information about the cfe-commits mailing list