From b352e3edb559020e904d52941b039c5cebb26056 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 21 Nov 2006 06:17:10 +0000 Subject: [PATCH] Change KeepComments/KeepMacroComments modes to be facets of the preprocessor state, not aspects of the language standard being parsed. llvm-svn: 39209 --- clang/Driver/PrintPreprocessedOutput.cpp | 8 +++----- clang/Lex/Lexer.cpp | 6 +++--- clang/Lex/Preprocessor.cpp | 18 +++++++++++------- clang/include/clang/Basic/LangOptions.h | 4 ---- clang/include/clang/Lex/Preprocessor.h | 13 +++++++++++++ 5 files changed, 30 insertions(+), 19 deletions(-) diff --git a/clang/Driver/PrintPreprocessedOutput.cpp b/clang/Driver/PrintPreprocessedOutput.cpp index fb5208dc473c..039d0d80b6a2 100644 --- a/clang/Driver/PrintPreprocessedOutput.cpp +++ b/clang/Driver/PrintPreprocessedOutput.cpp @@ -387,11 +387,9 @@ bool PrintPPOutputPPCallbacks::AvoidConcat(const LexerToken &PrevTok, /// void clang::DoPrintPreprocessedInput(unsigned MainFileID, Preprocessor &PP, LangOptions &Options) { - // FIXME: should not mutate LangOptions!! - if (EnableCommentOutput) // -C specified? - Options.KeepComments = 1; - if (EnableMacroCommentOutput) // -CC specified? - Options.KeepComments = Options.KeepMacroComments = 1; + // Inform the preprocessor whether we want it to retain comments or not, due + // to -C or -CC. + PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput); InitOutputBuffer(); diff --git a/clang/Lex/Lexer.cpp b/clang/Lex/Lexer.cpp index 3ae7b6a4e51a..4a57ac2c74b0 100644 --- a/clang/Lex/Lexer.cpp +++ b/clang/Lex/Lexer.cpp @@ -67,7 +67,7 @@ Lexer::Lexer(const SourceBuffer *File, unsigned fileid, Preprocessor &pp, LexingRawMode = false; // Default to keeping comments if requested. - KeepCommentMode = Features.KeepComments; + KeepCommentMode = PP.getCommentRetentionState(); } /// Stringify - Convert the specified string into a C string, with surrounding @@ -970,7 +970,7 @@ bool Lexer::LexEndOfFile(LexerToken &Result, const char *CurPtr) { FormTokenWithChars(Result, CurPtr); // Restore comment saving mode, in case it was disabled for directive. - KeepCommentMode = Features.KeepComments; + KeepCommentMode = PP.getCommentRetentionState(); return true; // Have a token. } @@ -1087,7 +1087,7 @@ LexNextToken: ParsingPreprocessorDirective = false; // Restore comment saving mode, in case it was disabled for directive. - KeepCommentMode = Features.KeepComments; + KeepCommentMode = PP.getCommentRetentionState(); // Since we consumed a newline, we are back at the start of a line. IsAtStartOfLine = true; diff --git a/clang/Lex/Preprocessor.cpp b/clang/Lex/Preprocessor.cpp index b122b0a2e167..4af6d31c1865 100644 --- a/clang/Lex/Preprocessor.cpp +++ b/clang/Lex/Preprocessor.cpp @@ -58,7 +58,11 @@ Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts, NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0; MaxIncludeStackDepth = 0; NumSkipped = 0; - + + // Default to discarding comments. + KeepComments = false; + KeepMacroComments = false; + // Macro expansion is enabled. DisableMacroExpansion = false; InMacroArgs = false; @@ -686,7 +690,7 @@ MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(LexerToken &MacroName, return 0; } // Otherwise, continue to add the tokens to this variable argument. - } else if (Tok.getKind() == tok::comment && !Features.KeepMacroComments) { + } else if (Tok.getKind() == tok::comment && !KeepMacroComments) { // If this is a comment token in the argument list and we're just in // -C mode (not -CC mode), discard the comment. continue; @@ -1175,7 +1179,7 @@ void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, if (Tok.getKind() != tok::identifier) { CurLexer->ParsingPreprocessorDirective = false; // Restore comment saving mode. - CurLexer->KeepCommentMode = Features.KeepComments; + CurLexer->KeepCommentMode = KeepComments; continue; } @@ -1190,7 +1194,7 @@ void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, FirstChar != 'i' && FirstChar != 'e') { CurLexer->ParsingPreprocessorDirective = false; // Restore comment saving mode. - CurLexer->KeepCommentMode = Features.KeepComments; + CurLexer->KeepCommentMode = KeepComments; continue; } @@ -1211,7 +1215,7 @@ void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, if (IdLen >= 20) { CurLexer->ParsingPreprocessorDirective = false; // Restore comment saving mode. - CurLexer->KeepCommentMode = Features.KeepComments; + CurLexer->KeepCommentMode = KeepComments; continue; } memcpy(Directive, &DirectiveStr[0], IdLen); @@ -1292,7 +1296,7 @@ void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, CurLexer->ParsingPreprocessorDirective = false; // Restore comment saving mode. - CurLexer->KeepCommentMode = Features.KeepComments; + CurLexer->KeepCommentMode = KeepComments; } // Finally, if we are out of the conditional (saw an #endif or ran off the end @@ -1710,7 +1714,7 @@ void Preprocessor::HandleDefineDirective(LexerToken &DefineTok, // If we are supposed to keep comments in #defines, reenable comment saving // mode. - CurLexer->KeepCommentMode = Features.KeepMacroComments; + CurLexer->KeepCommentMode = KeepMacroComments; // Create the new macro. MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation()); diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h index ee290304318c..5ab312d317ab 100644 --- a/clang/include/clang/Basic/LangOptions.h +++ b/clang/include/clang/Basic/LangOptions.h @@ -33,13 +33,9 @@ struct LangOptions { unsigned ObjC1 : 1; // Objective C 1 support enabled. unsigned ObjC2 : 1; // Objective C 2 support enabled. - unsigned KeepComments : 1; // Keep comments ("-C") mode. - unsigned KeepMacroComments : 1; // Keep macro-exp comments ("-CC") mode. - LangOptions() { Trigraphs = BCPLComment = DollarIdents = Digraphs = ObjC1 = ObjC2 = 0; C99 = Microsoft = CPlusPlus = NoExtensions = 0; - KeepComments = KeepMacroComments = 0; } }; diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index d1c555af1db4..4bbf20d44322 100644 --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -62,6 +62,10 @@ class Preprocessor { MaxAllowedIncludeStackDepth = 200 }; + // State that is set before the preprocessor begins. + bool KeepComments : 1; + bool KeepMacroComments : 1; + // State that changes while the preprocessor runs: bool DisableMacroExpansion : 1; // True if macro expansion is disabled. bool InMacroArgs : 1; // True if parsing fn macro invocation args. @@ -125,6 +129,15 @@ public: IdentifierTable &getIdentifierTable() { return Identifiers; } + /// SetCommentRetentionState - Control whether or not the preprocessor retains + /// comments in output. + void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) { + this->KeepComments = KeepComments | KeepMacroComments; + this->KeepMacroComments = KeepMacroComments; + } + + bool getCommentRetentionState() const { return KeepComments; } + /// isCurrentLexer - Return true if we are lexing directly from the specified /// lexer. bool isCurrentLexer(const Lexer *L) const {