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

Chris Lattner sabre at nondot.org
Tue Apr 15 23:32:08 PDT 2008


Author: lattner
Date: Wed Apr 16 01:32:08 2008
New Revision: 49779

URL: http://llvm.org/viewvc/llvm-project?rev=49779&view=rev
Log:
split syntax highlighting of macros from keywords and comments,
allowing us to use a cheaper means to highlight keywords and making
it so that comments won't foul up macro expansions.

Start highlighting macro expansions.

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=49779&r1=49778&r2=49779&view=diff

==============================================================================
--- cfe/trunk/Driver/HTMLPrint.cpp (original)
+++ cfe/trunk/Driver/HTMLPrint.cpp Wed Apr 16 01:32:08 2008
@@ -61,8 +61,10 @@
 
   // If we have a preprocessor, relex the file and syntax hilight.  We might not
   // have a preprocessor if we come from a deserialized AST file, for example.
-  if (PP)
+  if (PP) {
     html::SyntaxHighlight(R, FileID, *PP);
+    html::HighlightMacros(R, FileID, *PP);
+  }
   
   
   // Open the output.

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

==============================================================================
--- cfe/trunk/include/clang/Rewrite/HTMLRewrite.h (original)
+++ cfe/trunk/include/clang/Rewrite/HTMLRewrite.h Wed Apr 16 01:32:08 2008
@@ -45,11 +45,16 @@
   void AddHeaderFooterInternalBuiltinCSS(Rewriter& R, unsigned FileID);
 
   /// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
-  /// information about keywords, macro expansions etc.  This uses the macro
-  /// table state from the end of the file, so it won't be perfectly perfect,
-  /// but it will be reasonably close.
+  /// information about keywords, comments, etc.
   void SyntaxHighlight(Rewriter &R, unsigned FileID, Preprocessor &PP);
 
+  /// HighlightMacros - This uses the macro table state from the end of the
+  /// file, to reexpand macros and insert (into the HTML) information about the
+  /// macro expansions.  This won't be perfectly perfect, but it will be
+  /// reasonably close.
+  void HighlightMacros(Rewriter &R, unsigned FileID, Preprocessor &PP);
+  
+
 } // end html namespace
 } // end clang namespace
 

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

==============================================================================
--- cfe/trunk/lib/Rewrite/HTMLRewrite.cpp (original)
+++ cfe/trunk/lib/Rewrite/HTMLRewrite.cpp Wed Apr 16 01:32:08 2008
@@ -185,7 +185,8 @@
       " .code { border-spacing:0px; width:100%; }\n"
       " .code { font-family: \"Andale Mono\", monospace; font-size:10pt }\n"
       " .code { line-height: 1.2em }\n"
-      " .comment { color:#A0A0A0 }\n"
+      " .comment { color: #A0A0A0 }\n"
+      " .macro { color: #FF0000; background-color:#FFC0C0 }\n"
       " .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }\n"
       " .num { text-align:right; font-size: smaller }\n"
       " .num { color:#444444 }\n"
@@ -232,11 +233,12 @@
   // Start parsing the specified input file.
   PP.EnterMainSourceFile();
 
-  // Lex all the tokens.
+  // Lex all the tokens in raw mode, to avoid entering #includes or expanding
+  // macros.
   const SourceManager &SourceMgr = PP.getSourceManager();
   Token Tok;
   do {
-    PP.Lex(Tok);
+    PP.LexUnexpandedToken(Tok);
     // Ignore tokens whose logical location was not the main file.
     SourceLocation LLoc = SourceMgr.getLogicalLoc(Tok.getLocation());
     std::pair<unsigned, unsigned> LLocInfo = 
@@ -249,6 +251,8 @@
     unsigned TokLen = Tok.getLength();
     switch (Tok.getKind()) {
     default: break;
+      // FIXME: Add keywords here.
+        
     case tok::comment:
       RB.InsertTextAfter(TokOffs, "<span class='comment'>",
                          strlen("<span class='comment'>"));
@@ -257,4 +261,66 @@
     }
     
   } while (Tok.isNot(tok::eof));
+  PP.SetCommentRetentionState(false, false);
+}
+
+/// HighlightMacros - This uses the macro table state from the end of the
+/// file, to reexpand macros and insert (into the HTML) information about the
+/// macro expansions.  This won't be perfectly perfect, but it will be
+/// reasonably close.
+void html::HighlightMacros(Rewriter &R, unsigned FileID, Preprocessor &PP) {
+  RewriteBuffer &RB = R.getEditBuffer(FileID);
+  
+  // Inform the preprocessor that we don't want comments.
+  PP.SetCommentRetentionState(false, false);
+  
+  // Start parsing the specified input file.
+  PP.EnterMainSourceFile();
+  
+  // Lex all the tokens.
+  const SourceManager &SourceMgr = PP.getSourceManager();
+  Token Tok;
+  PP.Lex(Tok);
+  while (Tok.isNot(tok::eof)) {
+    // Ignore non-macro tokens.
+    if (!Tok.getLocation().isMacroID()) {
+      PP.Lex(Tok);
+      continue;
+    }
+    
+    // Ignore tokens whose logical location was not the main file.
+    SourceLocation LLoc = SourceMgr.getLogicalLoc(Tok.getLocation());
+    std::pair<unsigned, unsigned> LLocInfo = 
+      SourceMgr.getDecomposedFileLoc(LLoc);
+    
+    if (LLocInfo.first != FileID) {
+      PP.Lex(Tok);
+      continue;
+    }
+    
+    // Okay, we have the first token of a macro expansion: highlight the
+    // instantiation.
+  
+    // Get the size of current macro call itself.
+    // FIXME: This should highlight the args of a function-like
+    // macro, using a heuristic.
+    unsigned TokLen = Lexer::MeasureTokenLength(LLoc, SourceMgr);
+    
+    unsigned TokOffs = LLocInfo.second;
+    RB.InsertTextAfter(TokOffs, "<span class='macro'>",
+                       strlen("<span class='macro'>"));
+    RB.InsertTextBefore(TokOffs+TokLen, "</span>", strlen("</span>"));
+    
+    // Okay, eat this token, getting the next one.
+    PP.Lex(Tok);
+    
+    // Skip all the rest of the tokens that are part of this macro
+    // instantiation.  It would be really nice to pop up a window with all the
+    // spelling of the tokens or something.
+    while (!Tok.is(tok::eof) &&
+           SourceMgr.getLogicalLoc(Tok.getLocation()) == LLoc)
+      PP.Lex(Tok);
+  }
 }
+
+





More information about the cfe-commits mailing list