[cfe-commits] r72000 - in /cfe/trunk: include/clang/Rewrite/Rewriter.h lib/Rewrite/Rewriter.cpp test/Misc/emit-html-insert.c

Eli Friedman eli.friedman at gmail.com
Mon May 18 06:56:58 PDT 2009


Author: efriedma
Date: Mon May 18 08:56:52 2009
New Revision: 72000

URL: http://llvm.org/viewvc/llvm-project?rev=72000&view=rev
Log:
Fix for PR2386: distinguish between insertion and replacements in the 
delta tree.

The issue is roughly a conflict in ReplaceText between two kinds of 
uses. One, it should be possible to replace a replacement: for example, the
ObjC rewriter calls ReplaceStmt for an expression, then replaces the resulting
expression with another expression.  Two, it should be possible to 
replace text that already has text inserted before it: for example, the 
HTML rewriter inserts a bunch of tags at the beginning of the line, then 
tries to escape the first character on the line.  This patch 
distinguishes the two cases by storing the deltas separately; 
essentially, replacements and insertions no longer interfere with 
each other.

Another possibility would be to add some sort of flag to ReplaceText, but
this seems a bit more intuitive and flexible.

There are a few downsides to the current solution: one is that there isn't
any way to remove/replace an insertion without touching additional
surrounding text; if such an operation turns out to be useful, an
additional method or flag can be added.  Another is that an insertion 
and replacing a string of length zero are distinct operations; I'm not 
sure how to resolve this, or whether it will be confusing in practice.

This is relatively sensitive code, so please test and tell me if 
anything breaks.


Added:
    cfe/trunk/test/Misc/emit-html-insert.c
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=72000&r1=71999&r2=72000&view=diff

==============================================================================
--- cfe/trunk/include/clang/Rewrite/Rewriter.h (original)
+++ cfe/trunk/include/clang/Rewrite/Rewriter.h Mon May 18 08:56:52 2009
@@ -102,13 +102,19 @@
   /// inserted text at the position.
   unsigned getMappedOffset(unsigned OrigOffset,
                            bool AfterInserts = false) const{
-    return Deltas.getDeltaAt(OrigOffset+AfterInserts)+OrigOffset;
+    return Deltas.getDeltaAt(2*OrigOffset+AfterInserts)+OrigOffset;
   }
   
-  /// AddDelta - When a change is made that shifts around the text buffer, this
-  /// method is used to record that info.
-  void AddDelta(unsigned OrigOffset, int Change) {
-    return Deltas.AddDelta(OrigOffset, Change);
+  /// AddInsertDelta - When an insertion is made at a position, this
+  /// method is used to record that information.
+  void AddInsertDelta(unsigned OrigOffset, int Change) {
+    return Deltas.AddDelta(2*OrigOffset, Change);
+  }
+
+  /// AddReplaceDelta - When a replacement/deletion is made at a position, this
+  /// method is used to record that information.
+  void AddReplaceDelta(unsigned OrigOffset, int Change) {
+    return Deltas.AddDelta(2*OrigOffset+1, Change);
   }
 };
   

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

==============================================================================
--- cfe/trunk/lib/Rewrite/Rewriter.cpp (original)
+++ cfe/trunk/lib/Rewrite/Rewriter.cpp Mon May 18 08:56:52 2009
@@ -31,7 +31,7 @@
   Buffer.erase(RealOffset, Size);
 
   // Add a delta so that future changes are offset correctly.
-  AddDelta(OrigOffset, -Size);
+  AddReplaceDelta(OrigOffset, -Size);
 }
 
 void RewriteBuffer::InsertText(unsigned OrigOffset,
@@ -45,7 +45,7 @@
   Buffer.insert(RealOffset, StrData, StrData+StrLen);
   
   // Add a delta so that future changes are offset correctly.
-  AddDelta(OrigOffset, StrLen);
+  AddInsertDelta(OrigOffset, StrLen);
 }
 
 /// ReplaceText - This method replaces a range of characters in the input
@@ -53,11 +53,11 @@
 /// operation.
 void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength,
                                 const char *NewStr, unsigned NewLength) {
-  unsigned RealOffset = getMappedOffset(OrigOffset, false);
+  unsigned RealOffset = getMappedOffset(OrigOffset, true);
   Buffer.erase(RealOffset, OrigLength);
   Buffer.insert(RealOffset, NewStr, NewStr+NewLength);
   if (OrigLength != NewLength)
-    AddDelta(OrigOffset, NewLength-OrigLength);
+    AddReplaceDelta(OrigOffset, NewLength-OrigLength);
 }
 
 

Added: cfe/trunk/test/Misc/emit-html-insert.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Misc/emit-html-insert.c?rev=72000&view=auto

==============================================================================
--- cfe/trunk/test/Misc/emit-html-insert.c (added)
+++ cfe/trunk/test/Misc/emit-html-insert.c Mon May 18 08:56:52 2009
@@ -0,0 +1,4 @@
+// RUN: clang-cc %s -emit-html -o - | grep ">< 10; }"
+
+int a(int x) { return x
+< 10; }





More information about the cfe-commits mailing list