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

Chris Lattner sabre at nondot.org
Wed Apr 16 16:06:45 PDT 2008


Author: lattner
Date: Wed Apr 16 18:06:45 2008
New Revision: 49827

URL: http://llvm.org/viewvc/llvm-project?rev=49827&view=rev
Log:
Make HighlightRange correctly handle multi-line ranges.  This causes us to 
correctly handle multi-line comments.


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

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

==============================================================================
--- cfe/trunk/include/clang/Rewrite/HTMLRewrite.h (original)
+++ cfe/trunk/include/clang/Rewrite/HTMLRewrite.h Wed Apr 16 18:06:45 2008
@@ -24,7 +24,7 @@
 class Preprocessor;
   
 namespace html {
-
+  
   /// HighlightRange - Highlight a range in the source code with the specified
   /// start/end tags.  B/E must be in the same file.  This ensures that
   /// start/end tags are placed at the start/end of each line if the range is
@@ -32,6 +32,15 @@
   void HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E,
                       const char *StartTag, const char *EndTag);
   
+  /// HighlightRange - Highlight a range in the source code with the specified
+  /// start/end tags.  The Start/end of the range must be in the same file.  
+  /// This ensures that start/end tags are placed at the start/end of each line
+  /// if the range is multiline.
+  inline void HighlightRange(Rewriter &R, SourceRange Range,
+                             const char *StartTag, const char *EndTag) {
+    HighlightRange(R, Range.getBegin(), Range.getEnd(), StartTag, EndTag);
+  }
+  
   /// HighlightRange - This is the same as the above method, but takes
   /// decomposed file locations.
   void HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E,

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

==============================================================================
--- cfe/trunk/lib/Rewrite/HTMLRewrite.cpp (original)
+++ cfe/trunk/lib/Rewrite/HTMLRewrite.cpp Wed Apr 16 18:06:45 2008
@@ -21,6 +21,7 @@
 #include <sstream>
 using namespace clang;
 
+
 /// HighlightRange - Highlight a range in the source code with the specified
 /// start/end tags.  B/E must be in the same file.  This ensures that
 /// start/end tags are placed at the start/end of each line if the range is
@@ -48,9 +49,50 @@
 void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E,
                           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));
   
+  // 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.
+  bool HadOpenTag = true;
+  
+  unsigned LastNonWhiteSpace = B;
+  for (unsigned i = B; i != E; ++i) {
+    switch (BufferStart[i]) {
+    case '\r':
+    case '\n':
+      // 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));
+        
+      // Instead of inserting an open tag immediately after the newline, we
+      // wait until we see a non-whitespace character.  This prevents us from
+      // inserting tags around blank lines, and also allows the open tag to
+      // be put *after* whitespace on a non-blank line.
+      HadOpenTag = false;
+      break;
+    case '\0':
+    case ' ':
+    case '\t':
+    case '\f':
+    case '\v':
+      // Ignore whitespace.
+      break;
+    
+    default:
+      // If there is no tag open, do it now.
+      if (!HadOpenTag) {
+        RB.InsertTextAfter(i, StartTag, strlen(StartTag));
+        HadOpenTag = true;
+      }
+        
+      // Remember this character.
+      LastNonWhiteSpace = i;
+      break;
+    }
+  }
 }
 
 void html::EscapeText(Rewriter& R, unsigned FileID,





More information about the cfe-commits mailing list