Change KeepComments/KeepMacroComments modes to be facets of the preprocessor

state, not aspects of the language standard being parsed.

llvm-svn: 39209
This commit is contained in:
Chris Lattner 2006-11-21 06:17:10 +00:00
parent ad7cdd37b3
commit b352e3edb5
5 changed files with 30 additions and 19 deletions

View File

@ -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();

View File

@ -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;

View File

@ -59,6 +59,10 @@ Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
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());

View File

@ -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;
}
};

View File

@ -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 {