Skip to content
Snippets Groups Projects
Commit ff6912b5 authored by Daniel Dunbar's avatar Daniel Dunbar
Browse files

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
parent c3222091
No related branches found
No related tags found
No related merge requests found
...@@ -67,13 +67,13 @@ namespace html { ...@@ -67,13 +67,13 @@ namespace html {
/// SyntaxHighlight - Relex the specified FileID and annotate the HTML with /// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
/// information about keywords, comments, etc. /// 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 /// HighlightMacros - This uses the macro table state from the end of the
/// file, to reexpand macros and insert (into the HTML) information about 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 /// macro expansions. This won't be perfectly perfect, but it will be
/// reasonably close. /// reasonably close.
void HighlightMacros(Rewriter &R, FileID FID, Preprocessor &PP); void HighlightMacros(Rewriter &R, FileID FID, const Preprocessor &PP);
} // end html namespace } // end html namespace
} // end clang namespace } // end clang namespace
......
...@@ -349,7 +349,7 @@ void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, FileID FID, ...@@ -349,7 +349,7 @@ void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, FileID FID,
/// information about keywords, macro expansions etc. This uses the macro /// 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, /// table state from the end of the file, so it won't be perfectly perfect,
/// but it will be reasonably close. /// 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); RewriteBuffer &RB = R.getEditBuffer(FID);
const SourceManager &SM = PP.getSourceManager(); const SourceManager &SM = PP.getSourceManager();
...@@ -375,7 +375,8 @@ void html::SyntaxHighlight(Rewriter &R, FileID FID, Preprocessor &PP) { ...@@ -375,7 +375,8 @@ void html::SyntaxHighlight(Rewriter &R, FileID FID, Preprocessor &PP) {
case tok::identifier: { case tok::identifier: {
// Fill in Result.IdentifierInfo, looking up the identifier in the // Fill in Result.IdentifierInfo, looking up the identifier in the
// identifier table. // 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 this is a pp-identifier, for a keyword, highlight it as such.
if (II->getTokenID() != tok::identifier) if (II->getTokenID() != tok::identifier)
...@@ -438,7 +439,7 @@ class IgnoringDiagClient : public DiagnosticClient { ...@@ -438,7 +439,7 @@ class IgnoringDiagClient : public DiagnosticClient {
/// file, to re-expand macros and insert (into the HTML) information about the /// 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 /// macro expansions. This won't be perfectly perfect, but it will be
/// reasonably close. /// 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. // Re-lex the raw token stream into a token buffer.
const SourceManager &SM = PP.getSourceManager(); const SourceManager &SM = PP.getSourceManager();
std::vector<Token> TokenStream; std::vector<Token> TokenStream;
...@@ -481,25 +482,29 @@ void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) { ...@@ -481,25 +482,29 @@ void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) {
IgnoringDiagClient TmpDC; IgnoringDiagClient TmpDC;
Diagnostic TmpDiags(&TmpDC); Diagnostic TmpDiags(&TmpDC);
Diagnostic *OldDiags = &PP.getDiagnostics(); // FIXME: This is a huge hack; we reuse the input preprocessor because we want
PP.setDiagnostics(TmpDiags); // 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. // 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 // Enter the tokens we just lexed. This will cause them to be macro expanded
// but won't enter sub-files (because we removed #'s). // 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. // Lex all the tokens.
Token Tok; Token Tok;
PP.Lex(Tok); TmpPP.Lex(Tok);
while (Tok.isNot(tok::eof)) { while (Tok.isNot(tok::eof)) {
// Ignore non-macro tokens. // Ignore non-macro tokens.
if (!Tok.getLocation().isMacroID()) { if (!Tok.getLocation().isMacroID()) {
PP.Lex(Tok); TmpPP.Lex(Tok);
continue; continue;
} }
...@@ -511,19 +516,19 @@ void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) { ...@@ -511,19 +516,19 @@ void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) {
// Ignore tokens whose instantiation location was not the main file. // Ignore tokens whose instantiation location was not the main file.
if (SM.getFileID(LLoc.first) != FID) { if (SM.getFileID(LLoc.first) != FID) {
PP.Lex(Tok); TmpPP.Lex(Tok);
continue; continue;
} }
assert(SM.getFileID(LLoc.second) == FID && assert(SM.getFileID(LLoc.second) == FID &&
"Start and end of expansion must be in the same ultimate file!"); "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(); unsigned LineLen = Expansion.size();
Token PrevTok = Tok; Token PrevTok = Tok;
// Okay, eat this token, getting the next one. // 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 // 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 // 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) { ...@@ -545,11 +550,11 @@ void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) {
Expansion += ' '; Expansion += ' ';
// Escape any special characters in the token text. // Escape any special characters in the token text.
Expansion += EscapeText(PP.getSpelling(Tok)); Expansion += EscapeText(TmpPP.getSpelling(Tok));
LineLen += Expansion.size(); LineLen += Expansion.size();
PrevTok = Tok; PrevTok = Tok;
PP.Lex(Tok); TmpPP.Lex(Tok);
} }
...@@ -562,5 +567,5 @@ void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) { ...@@ -562,5 +567,5 @@ void html::HighlightMacros(Rewriter &R, FileID FID, Preprocessor& PP) {
} }
// Restore diagnostics object back to its own thing. // Restore diagnostics object back to its own thing.
PP.setDiagnostics(*OldDiags); TmpPP.setDiagnostics(*OldDiags);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment