From: Daniel Dunbar Date: Thu, 5 Nov 2009 01:54:02 +0000 (+0000) Subject: Make html::{SyntaxHighlight,HighlightMacros} take a const Preprocessor. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ff6912b5a9d5073221956fe4d6367b14f3f4b68f;p=clang 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@86107 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Rewrite/HTMLRewrite.h b/include/clang/Rewrite/HTMLRewrite.h index 8069356d99..88caf85e60 100644 --- a/include/clang/Rewrite/HTMLRewrite.h +++ b/include/clang/Rewrite/HTMLRewrite.h @@ -67,13 +67,13 @@ namespace html { /// 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 diff --git a/lib/Rewrite/HTMLRewrite.cpp b/lib/Rewrite/HTMLRewrite.cpp index 445bf30a1f..b4bf419bc5 100644 --- a/lib/Rewrite/HTMLRewrite.cpp +++ b/lib/Rewrite/HTMLRewrite.cpp @@ -349,7 +349,7 @@ void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, FileID FID, /// 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 @@ void html::SyntaxHighlight(Rewriter &R, FileID FID, Preprocessor &PP) { 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 @@ class IgnoringDiagClient : public DiagnosticClient { /// 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 TokenStream; @@ -481,25 +482,29 @@ void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) { 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(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 @@ void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) { // 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 @@ void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) { 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 @@ void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) { } // Restore diagnostics object back to its own thing. - PP.setDiagnostics(*OldDiags); + TmpPP.setDiagnostics(*OldDiags); }