[cfe-commits] r42944 - in /cfe/trunk: Driver/RewriteTest.cpp Rewrite/Rewriter.cpp

Chris Lattner sabre at nondot.org
Fri Oct 12 17:46:29 PDT 2007


Author: lattner
Date: Fri Oct 12 19:46:29 2007
New Revision: 42944

URL: http://llvm.org/viewvc/llvm-project?rev=42944&view=rev
Log:
haha, my devious plot is complete:

$ clang rewrite.c -rewrite-test
prints:

int foo() {
        b:      foo();
f:      foo();
    foo();
}

for:

int foo() {
	b:	foo();
f:	foo();
    foo();
}

amazing.


Modified:
    cfe/trunk/Driver/RewriteTest.cpp
    cfe/trunk/Rewrite/Rewriter.cpp

Modified: cfe/trunk/Driver/RewriteTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/RewriteTest.cpp?rev=42944&r1=42943&r2=42944&view=diff

==============================================================================
--- cfe/trunk/Driver/RewriteTest.cpp (original)
+++ cfe/trunk/Driver/RewriteTest.cpp Fri Oct 12 19:46:29 2007
@@ -78,7 +78,7 @@
       SourceLocation::getFileLoc(MainFileID, BufPtr-MainBufStart);
     
     // Rewrite the single tab character into a sequence of spaces.
-    Rewrite.ReplaceText(TabLoc, 1, "xxxxxxxxxxx", Spaces);
+    Rewrite.ReplaceText(TabLoc, 1, "        ", Spaces);
   }
   
   // Get the buffer corresponding to MainFileID.  If we haven't changed it, then

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

==============================================================================
--- cfe/trunk/Rewrite/Rewriter.cpp (original)
+++ cfe/trunk/Rewrite/Rewriter.cpp Fri Oct 12 19:46:29 2007
@@ -114,17 +114,26 @@
 /// operation.
 void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength,
                                 const char *NewStr, unsigned NewLength) {
-  InsertText(OrigOffset, NewStr, NewLength);
-  return;
+  unsigned RealOffset = getMappedOffset(OrigOffset);
+  assert(RealOffset+OrigLength <= Buffer.size() && "Invalid location");
+
+  // Overwrite the common piece.
+  memcpy(&Buffer[RealOffset], NewStr, std::min(OrigLength, NewLength));
   
-  unsigned MappedOffs = getMappedOffset(OrigOffset);
-  // TODO: FIXME location.
-  assert(OrigOffset+OrigLength <= Buffer.size() && "Invalid location");
-  if (OrigLength == NewLength) {
-    // If replacing without shifting around, just overwrite the text.
-    memcpy(&Buffer[OrigOffset], NewStr, NewLength);
+  // If replacing without shifting around, just overwrite the text.
+  if (OrigLength == NewLength)
     return;
+
+  // If inserting more than existed before, this is like an insertion.
+  if (NewLength > OrigLength) {
+    Buffer.insert(Buffer.begin()+RealOffset+OrigLength,
+                  NewStr+OrigLength, NewStr+NewLength);
+  } else {
+    // If insertion less than existed before, this is like a removal.
+    Buffer.erase(Buffer.begin()+RealOffset+NewLength,
+                 Buffer.begin()+RealOffset+OrigLength);
   }
+  AddDelta(OrigOffset, NewLength-OrigLength);
 }
 
 





More information about the cfe-commits mailing list