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

Daniel Dunbar daniel at zuster.org
Wed Nov 4 17:54:03 PST 2009


Author: ddunbar
Date: Wed Nov  4 19:54:02 2009
New Revision: 86107

URL: http://llvm.org/viewvc/llvm-project?rev=86107&view=rev
Log:
Make html::{SyntaxHighlight,HighlightMacros} take a const Preprocessor.

This is conceptually correct, but adds a huge hack to HighlightMacros which is
in fact doing all sorts of mutation to the Preprocessor. See FIXME.

Chris, please review.

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=86107&r1=86106&r2=86107&view=diff

==============================================================================
--- cfe/trunk/include/clang/Rewrite/HTMLRewrite.h (original)
+++ cfe/trunk/include/clang/Rewrite/HTMLRewrite.h Wed Nov  4 19:54:02 2009
@@ -67,13 +67,13 @@
 
   /// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
   /// information about keywords, comments, etc.
-  void SyntaxHighlight(Rewriter &R, FileID FID, Preprocessor &PP);
+  void SyntaxHighlight(Rewriter &R, FileID FID, const 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, FileID FID, Preprocessor &PP);
+  void HighlightMacros(Rewriter &R, FileID FID, const 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=86107&r1=86106&r2=86107&view=diff

==============================================================================
--- cfe/trunk/lib/Rewrite/HTMLRewrite.cpp (original)
+++ cfe/trunk/lib/Rewrite/HTMLRewrite.cpp Wed Nov  4 19:54:02 2009
@@ -349,7 +349,7 @@
 /// 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.
-void html::SyntaxHighlight(Rewriter &R, FileID FID, Preprocessor &PP) {
+void html::SyntaxHighlight(Rewriter &R, FileID FID, const Preprocessor &PP) {
   RewriteBuffer &RB = R.getEditBuffer(FID);
 
   const SourceManager &SM = PP.getSourceManager();
@@ -375,7 +375,8 @@
     case tok::identifier: {
       // Fill in Result.IdentifierInfo, looking up the identifier in the
       // identifier table.
-      IdentifierInfo *II = PP.LookUpIdentifierInfo(Tok, BufferStart+TokOffs);
+      const IdentifierInfo *II =
+        PP.LookUpIdentifierInfo(Tok, BufferStart+TokOffs);
 
       // If this is a pp-identifier, for a keyword, highlight it as such.
       if (II->getTokenID() != tok::identifier)
@@ -438,7 +439,7 @@
 /// file, to re-expand 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, FileID FID, Preprocessor& PP) {
+void html::HighlightMacros(Rewriter &R, FileID FID, const Preprocessor& PP) {
   // Re-lex the raw token stream into a token buffer.
   const SourceManager &SM = PP.getSourceManager();
   std::vector<Token> TokenStream;
@@ -481,25 +482,29 @@
   IgnoringDiagClient TmpDC;
   Diagnostic TmpDiags(&TmpDC);
 
-  Diagnostic *OldDiags = &PP.getDiagnostics();
-  PP.setDiagnostics(TmpDiags);
+  // FIXME: This is a huge hack; we reuse the input preprocessor because we want
+  // its state, but we aren't actually changing it (we hope). This should really
+  // construct a copy of the preprocessor.
+  Preprocessor &TmpPP = const_cast<Preprocessor&>(PP);
+  Diagnostic *OldDiags = &TmpPP.getDiagnostics();
+  TmpPP.setDiagnostics(TmpDiags);
 
   // Inform the preprocessor that we don't want comments.
-  PP.SetCommentRetentionState(false, false);
+  TmpPP.SetCommentRetentionState(false, false);
 
   // Enter the tokens we just lexed.  This will cause them to be macro expanded
   // but won't enter sub-files (because we removed #'s).
-  PP.EnterTokenStream(&TokenStream[0], TokenStream.size(), false, false);
+  TmpPP.EnterTokenStream(&TokenStream[0], TokenStream.size(), false, false);
 
-  TokenConcatenation ConcatInfo(PP);
+  TokenConcatenation ConcatInfo(TmpPP);
 
   // Lex all the tokens.
   Token Tok;
-  PP.Lex(Tok);
+  TmpPP.Lex(Tok);
   while (Tok.isNot(tok::eof)) {
     // Ignore non-macro tokens.
     if (!Tok.getLocation().isMacroID()) {
-      PP.Lex(Tok);
+      TmpPP.Lex(Tok);
       continue;
     }
 
@@ -511,19 +516,19 @@
 
     // Ignore tokens whose instantiation location was not the main file.
     if (SM.getFileID(LLoc.first) != FID) {
-      PP.Lex(Tok);
+      TmpPP.Lex(Tok);
       continue;
     }
 
     assert(SM.getFileID(LLoc.second) == FID &&
            "Start and end of expansion must be in the same ultimate file!");
 
-    std::string Expansion = EscapeText(PP.getSpelling(Tok));
+    std::string Expansion = EscapeText(TmpPP.getSpelling(Tok));
     unsigned LineLen = Expansion.size();
 
     Token PrevTok = Tok;
     // Okay, eat this token, getting the next one.
-    PP.Lex(Tok);
+    TmpPP.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
@@ -545,11 +550,11 @@
         Expansion += ' ';
 
       // Escape any special characters in the token text.
-      Expansion += EscapeText(PP.getSpelling(Tok));
+      Expansion += EscapeText(TmpPP.getSpelling(Tok));
       LineLen += Expansion.size();
 
       PrevTok = Tok;
-      PP.Lex(Tok);
+      TmpPP.Lex(Tok);
     }
 
 
@@ -562,5 +567,5 @@
   }
 
   // Restore diagnostics object back to its own thing.
-  PP.setDiagnostics(*OldDiags);
+  TmpPP.setDiagnostics(*OldDiags);
 }





More information about the cfe-commits mailing list