[cfe-commits] r79448 - in /cfe/trunk: include/clang/Rewrite/Rewriter.h lib/Frontend/FixItRewriter.cpp lib/Frontend/RewriteBlocks.cpp lib/Frontend/RewriteMacros.cpp lib/Frontend/RewriteObjC.cpp lib/Rewrite/HTMLRewrite.cpp lib/Rewrite/Rewriter.cpp

Daniel Dunbar daniel at zuster.org
Wed Aug 19 12:10:30 PDT 2009


Author: ddunbar
Date: Wed Aug 19 14:10:30 2009
New Revision: 79448

URL: http://llvm.org/viewvc/llvm-project?rev=79448&view=rev
Log:
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.

Modified:
    cfe/trunk/include/clang/Rewrite/Rewriter.h
    cfe/trunk/lib/Frontend/FixItRewriter.cpp
    cfe/trunk/lib/Frontend/RewriteBlocks.cpp
    cfe/trunk/lib/Frontend/RewriteMacros.cpp
    cfe/trunk/lib/Frontend/RewriteObjC.cpp
    cfe/trunk/lib/Rewrite/HTMLRewrite.cpp
    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=79448&r1=79447&r2=79448&view=diff

==============================================================================
--- cfe/trunk/include/clang/Rewrite/Rewriter.h (original)
+++ cfe/trunk/include/clang/Rewrite/Rewriter.h Wed Aug 19 14:10:30 2009
@@ -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 @@
   /// 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 @@
   /// 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 @@
   /// 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 @@
   ///  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 @@
   /// 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 @@
   /// 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

Modified: cfe/trunk/lib/Frontend/FixItRewriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/FixItRewriter.cpp?rev=79448&r1=79447&r2=79448&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/FixItRewriter.cpp (original)
+++ cfe/trunk/lib/Frontend/FixItRewriter.cpp Wed Aug 19 14:10:30 2009
@@ -175,8 +175,7 @@
     // 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;
   }
 

Modified: cfe/trunk/lib/Frontend/RewriteBlocks.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/RewriteBlocks.cpp?rev=79448&r1=79447&r2=79448&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/RewriteBlocks.cpp (original)
+++ cfe/trunk/lib/Frontend/RewriteBlocks.cpp Wed Aug 19 14:10:30 2009
@@ -236,7 +236,8 @@
 
 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);
 }

Modified: cfe/trunk/lib/Frontend/RewriteMacros.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/RewriteMacros.cpp?rev=79448&r1=79447&r2=79448&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/RewriteMacros.cpp (original)
+++ cfe/trunk/lib/Frontend/RewriteMacros.cpp Wed Aug 19 14:10:30 2009
@@ -129,13 +129,13 @@
         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 @@
       // 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 @@
       } while (RawOffs <= PPOffs && !RawTok.isAtStartOfLine() &&
                (PPOffs != RawOffs || !isSameToken(RawTok, PPTok)));
 
-      RB.InsertTextBefore(EndPos, "*/", 2);
+      RB.InsertTextBefore(EndPos, "*/");
       continue;
     }
     
@@ -199,7 +199,7 @@
       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

Modified: cfe/trunk/lib/Frontend/RewriteObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/RewriteObjC.cpp?rev=79448&r1=79447&r2=79448&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/RewriteObjC.cpp (original)
+++ cfe/trunk/lib/Frontend/RewriteObjC.cpp Wed Aug 19 14:10:30 2009
@@ -175,7 +175,7 @@
       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 @@
     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 @@
     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 @@
       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()) {

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

==============================================================================
--- cfe/trunk/lib/Rewrite/HTMLRewrite.cpp (original)
+++ cfe/trunk/lib/Rewrite/HTMLRewrite.cpp Wed Aug 19 14:10:30 2009
@@ -53,8 +53,8 @@
                           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 @@
       // 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 @@
     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 @@
       
     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 @@
         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 @@
   
   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 @@
   }
   
   // 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, 

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

==============================================================================
--- cfe/trunk/lib/Rewrite/Rewriter.cpp (original)
+++ cfe/trunk/lib/Rewrite/Rewriter.cpp Wed Aug 19 14:10:30 2009
@@ -34,30 +34,29 @@
   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 @@
 
 /// 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 @@
 /// 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 @@
   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;
 }
 





More information about the cfe-commits mailing list