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

Ted Kremenek kremenek at apple.com
Tue Mar 18 16:55:46 PDT 2008


Author: kremenek
Date: Tue Mar 18 18:55:46 2008
New Revision: 48518

URL: http://llvm.org/viewvc/llvm-project?rev=48518&view=rev
Log:
More HTML rewriter cleanups.  Preliminary CSS support in code pretty-printing.

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

Modified: cfe/trunk/Driver/HTMLPrint.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/HTMLPrint.cpp?rev=48518&r1=48517&r2=48518&view=diff

==============================================================================
--- cfe/trunk/Driver/HTMLPrint.cpp (original)
+++ cfe/trunk/Driver/HTMLPrint.cpp Tue Mar 18 18:55:46 2008
@@ -18,6 +18,7 @@
 #include "clang/Basic/SourceManager.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "clang/AST/ASTContext.h"
+#include <sstream>
 
 using namespace clang;
 
@@ -49,10 +50,22 @@
   
   html::EscapeText(R, FileID);
   html::AddLineNumbers(R, FileID);
-  html::InsertTag(R, html::PRE, StartLoc, EndLoc, 0, 0, true);
-  html::InsertTag(R, html::BODY, StartLoc, EndLoc, NULL, "\n", true);
-  html::InsertTag(R, html::HEAD, StartLoc, StartLoc, 0, 0, true);
-  html::InsertTag(R, html::HTML, StartLoc, EndLoc, NULL, "\n", true);
+  html::InsertOuterTag(R, html::PRE, StartLoc, EndLoc, 0, 0, true);
+  html::InsertOuterTag(R, html::BODY, StartLoc, EndLoc, NULL, "\n", true);
+  
+  // Generate CSS.
+  
+  std::ostringstream css;
+  css << "\n <style type=\"text/css\">\n";
+  css << "  .nums, .lines { vertical-align:top }\n";
+  css << "  .nums { padding-right:.5em; width:2.5em }\n";
+  css << "  </style>\n";
+
+  
+  // Add <head> and <html> tags.
+  
+  html::InsertTagBefore(R, html::HEAD, StartLoc, StartLoc, 0,css.str().c_str());
+  html::InsertOuterTag(R, html::HTML, StartLoc, EndLoc, 0, "\n");
   
   // Emit the HTML.
   

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

==============================================================================
--- cfe/trunk/include/clang/Rewrite/HTMLRewrite.h (original)
+++ cfe/trunk/include/clang/Rewrite/HTMLRewrite.h Tue Mar 18 18:55:46 2008
@@ -30,15 +30,33 @@
               HEAD,
               HTML,
               PRE,
-              SPAN };
+              SPAN,
+              STYLE };
   
   void EscapeText(Rewriter& R, unsigned FileID, bool EscapeSpaces = false);
 
   void InsertTag(Rewriter& R, Tags tag,
                  SourceLocation OpenLoc, SourceLocation CloseLoc,
-                 const char* Attributes = NULL, const char* Content = NULL,
+                 const char* Attrs = NULL, const char* Content = NULL,
                  bool Newline = false,
-                 bool OpenInsertBefore = true, bool CloseInsertAfter = true);
+                 bool OpenInsertBefore = true, bool CloseInsertBefore = false);
+  
+  static inline
+  void InsertTagBefore(Rewriter& R, Tags tag,
+                      SourceLocation OpenLoc, SourceLocation CloseLoc,
+                      const char* Attrs = NULL, const char* Content = NULL,
+                      bool Newline = false) {
+    InsertTag(R, tag, OpenLoc, CloseLoc, Attrs, Content, Newline, true, true);    
+  }  
+  
+  static inline
+  void InsertOuterTag(Rewriter& R, Tags tag,
+                      SourceLocation OpenLoc, SourceLocation CloseLoc,
+                      const char* Attrs = NULL, const char* Content = NULL,
+                      bool Newline = false) {
+    
+    InsertTag(R, tag, OpenLoc, CloseLoc, Attrs, Content, Newline, true, false);    
+  }
   
   // High-level operations.
   

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

==============================================================================
--- cfe/trunk/lib/Rewrite/HTMLRewrite.cpp (original)
+++ cfe/trunk/lib/Rewrite/HTMLRewrite.cpp Tue Mar 18 18:55:46 2008
@@ -51,24 +51,39 @@
 }
 
 
-static void TagOpen(std::ostringstream& os, const char* TagStr,
-                    const char* Attr, const char* Content) {
+static void TagOpen(Rewriter& R, const char* TagStr,
+                    const char* Attr, const char* Content,
+                    SourceLocation L, bool InsertBefore) {
   
+  std::ostringstream os;
   os << '<' << TagStr;
   if (Attr) os << ' ' << Attr;
   os << '>';
   if (Content) os << Content;
+  
+  if (InsertBefore)
+    R.InsertTextBefore(L, os.str().c_str(), os.str().size());
+  else
+    R.InsertTextAfter(L, os.str().c_str(), os.str().size());
 }
 
-static void TagClose(std::ostringstream& os, const char* TagStr) {
+static void TagClose(Rewriter& R, const char* TagStr, SourceLocation L,
+                     bool Newline, bool InsertBefore) {
+  
+  std::ostringstream os;
   os << "</" << TagStr << ">";
+  if (Newline) os << '\n';
+  
+  if (InsertBefore)
+    R.InsertTextBefore(L, os.str().c_str(), os.str().size());
+  else
+    R.InsertTextAfter(L, os.str().c_str(), os.str().size());
 }
 
 void html::InsertTag(Rewriter& R, html::Tags tag,
                      SourceLocation B, SourceLocation E,
-                     const char* Attributes,
-                     const char* Content, bool Newline,
-                     bool OpenInsertBefore, bool CloseInsertAfter) {
+                     const char* Attr, const char* Content, bool Newline,
+                     bool OpenInsertBefore, bool CloseInsertBefore) {
   
   const char* TagStr = 0;
   
@@ -80,6 +95,7 @@
     case HTML: TagStr = "html"; break;
     case PRE:  TagStr = "pre";  break;
     case SPAN: TagStr = "span"; break;
+    case STYLE: TagStr = "style"; break;
   }
   
   assert (TagStr && "Tag not supported.");
@@ -87,32 +103,13 @@
   // Generate the opening tag.  We also generate the closing
   // tag of the start and end SourceLocations are the same.
 
-  { 
-    std::ostringstream os;
-    TagOpen(os, TagStr, Attributes, Content);
-    if (B == E)  {
-      TagClose(os, TagStr);
-      if (Newline) os << '\n';
-    }
-    
-    if (OpenInsertBefore)    
-      R.InsertTextBefore(B, os.str().c_str(), os.str().size());
-    else
-      R.InsertTextAfter(B, os.str().c_str(), os.str().size());
+  if (OpenInsertBefore) {    
+    TagClose(R, TagStr, E, Newline, CloseInsertBefore);
+    TagOpen(R, TagStr, Attr, Content, B, true);
   }
-  
-  // Generate the closing tag if the start and end SourceLocations
-  // are different.
-  
-  if (B != E) {
-    std::ostringstream os;
-    TagClose(os, TagStr);
-    if (Newline) os << '\n';
-    
-    if (CloseInsertAfter)    
-      R.InsertTextAfter(E, os.str().c_str(), os.str().size());
-    else
-      R.InsertTextBefore(E, os.str().c_str(), os.str().size());
+  else {
+    TagOpen(R, TagStr, Attr, Content, B, false);
+    TagClose(R, TagStr, E, Newline, true);
   }
 }
 
@@ -128,8 +125,8 @@
 
   std::ostringstream os;
   os << LineNo;
-  html::InsertTag(R, html::SPAN, B, E, "class=Line");  
-  html::InsertTag(R, html::SPAN, B, B, "class=Num", os.str().c_str());  
+  html::InsertTag(R, html::SPAN, B, E, "style=lines");  
+  html::InsertTag(R, html::SPAN, B, B, "style=nums", os.str().c_str());  
 }
 
 void html::AddLineNumbers(Rewriter& R, unsigned FileID) {





More information about the cfe-commits mailing list