diff options
Diffstat (limited to 'clang/Lex')
| -rw-r--r-- | clang/Lex/Lexer.cpp | 20 | ||||
| -rw-r--r-- | clang/Lex/Pragma.cpp | 6 | ||||
| -rw-r--r-- | clang/Lex/Preprocessor.cpp | 31 |
3 files changed, 28 insertions, 29 deletions
diff --git a/clang/Lex/Lexer.cpp b/clang/Lex/Lexer.cpp index 893ff04d82e..2a1bcc0248f 100644 --- a/clang/Lex/Lexer.cpp +++ b/clang/Lex/Lexer.cpp @@ -58,6 +58,12 @@ Lexer::Lexer(const SourceBuffer *File, unsigned fileid, Preprocessor &pp, // We are not after parsing #include. ParsingFilename = false; + + // We are not in raw mode. Raw mode disables diagnostics and interpretation + // of tokens (e.g. identifiers, thus disabling macro expansion). It is used + // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block + // or otherwise skipping over tokens. + LexingRawMode = false; } /// Stringify - Convert the specified string into a C string, with surrounding @@ -896,10 +902,10 @@ void Lexer::LexEndOfFile(LexerToken &Result, const char *CurPtr) { return; } - // If we aren't skipping, issue diagnostics. If we are skipping, let the - // skipping code do this: there are multiple possible reasons for skipping, - // and not all want these diagnostics. - if (!PP.isSkipping()) { + // If we aren't in raw mode, issue diagnostics. If we are in raw mode, let the + // code that put us into raw mode do this: there are multiple possible reasons + // for raw mode, and not all want these diagnostics. + if (!LexingRawMode) { // If we are in a #if directive, emit an error. while (!ConditionalStack.empty()) { PP.Diag(ConditionalStack.back().IfLoc, @@ -1176,7 +1182,7 @@ LexNextToken: // it's actually the start of a preprocessing directive. Callback to // the preprocessor to handle it. // FIXME: -fpreprocessed mode?? - if (Result.isAtStartOfLine() && !PP.isSkipping()) { + if (Result.isAtStartOfLine() && !LexingRawMode) { BufferPtr = CurPtr; PP.HandleDirective(Result); @@ -1321,7 +1327,7 @@ LexNextToken: // it's actually the start of a preprocessing directive. Callback to // the preprocessor to handle it. // FIXME: -fpreprocessed mode?? - if (Result.isAtStartOfLine() && !PP.isSkipping()) { + if (Result.isAtStartOfLine() && !LexingRawMode) { BufferPtr = CurPtr; PP.HandleDirective(Result); @@ -1357,7 +1363,7 @@ LexNextToken: return LexIdentifier(Result, CurPtr); } - if (!PP.isSkipping()) Diag(CurPtr-1, diag::err_stray_character); + if (!LexingRawMode) Diag(CurPtr-1, diag::err_stray_character); BufferPtr = CurPtr; goto LexNextToken; // GCC isn't tail call eliminating. } diff --git a/clang/Lex/Pragma.cpp b/clang/Lex/Pragma.cpp index f4269a84eda..8e7e7e38d89 100644 --- a/clang/Lex/Pragma.cpp +++ b/clang/Lex/Pragma.cpp @@ -184,16 +184,16 @@ void Preprocessor::HandlePragmaOnce(LexerToken &OnceTok) { /// void Preprocessor::HandlePragmaPoison(LexerToken &PoisonTok) { LexerToken Tok; - assert(!SkippingContents && "Why are we handling pragmas while skipping?"); + assert(!isSkipping() && "Why are we handling pragmas while skipping?"); while (1) { // Read the next token to poison. While doing this, pretend that we are // skipping while reading the identifier to poison. // This avoids errors on code like: // #pragma GCC poison X // #pragma GCC poison X - SkippingContents = true; + if (CurLexer) CurLexer->LexingRawMode = true; LexUnexpandedToken(Tok); - SkippingContents = false; + if (CurLexer) CurLexer->LexingRawMode = false; // If we reached the end of line, we're done. if (Tok.getKind() == tok::eom) return; diff --git a/clang/Lex/Preprocessor.cpp b/clang/Lex/Preprocessor.cpp index b87c153b159..da8b9d15114 100644 --- a/clang/Lex/Preprocessor.cpp +++ b/clang/Lex/Preprocessor.cpp @@ -57,7 +57,6 @@ Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts, // Macro expansion is enabled. DisableMacroExpansion = false; - SkippingContents = false; InMacroFormalArgs = false; // There is no file-change handler yet. @@ -512,12 +511,13 @@ static bool isTrivialSingleTokenExpansion(const MacroInfo *MI, /// else and 2 if there are no more tokens in the buffer controlled by the /// lexer. unsigned Preprocessor::isNextPPTokenLParen(Lexer *L) { - assert(!isSkipping() && "How can we expand a macro from a skipping buffer?"); + assert(!L->LexingRawMode && + "How can we expand a macro from a skipping buffer?"); // Set the lexer to 'skipping' mode. This will ensure that we can lex a token // without emitting diagnostics, disables macro expansion, and will cause EOF // to return an EOF token instead of popping the include stack. - SkippingContents = true; + L->LexingRawMode = true; // Save state that can be changed while lexing so that we can restore it. const char *BufferPtr = L->BufferPtr; @@ -530,7 +530,7 @@ unsigned Preprocessor::isNextPPTokenLParen(Lexer *L) { L->BufferPtr = BufferPtr; // Restore the lexer back to non-skipping mode. - SkippingContents = false; + L->LexingRawMode = false; if (Tok.getKind() == tok::eof) return 2; @@ -1172,16 +1172,9 @@ void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false, FoundNonSkipPortion, FoundElse); - // Know that we are going to be skipping tokens. Set this flag to indicate - // this, which has a couple of effects: - // 1. If EOF of the current lexer is found, the include stack isn't popped. - // 2. Identifier information is not looked up for identifier tokens. As an - // effect of this, implicit macro expansion is naturally disabled. - // 3. "#" tokens at the start of a line are treated as normal tokens, not - // implicitly transformed by the lexer. - // 4. All notes, warnings, and extension messages are disabled. - // - SkippingContents = true; + // Enter raw mode to disable identifier lookup (and thus macro expansion), + // disabling warnings, etc. + CurLexer->LexingRawMode = true; LexerToken Tok; while (1) { CurLexer->Lex(Tok); @@ -1305,13 +1298,13 @@ void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, DiscardUntilEndOfDirective(); ShouldEnter = false; } else { - // Restore the value of SkippingContents so that identifiers are + // Restore the value of LexingRawMode so that identifiers are // looked up, etc, inside the #elif expression. - assert(SkippingContents && "We have to be skipping here!"); - SkippingContents = false; + assert(CurLexer->LexingRawMode && "We have to be skipping here!"); + CurLexer->LexingRawMode = false; IdentifierInfo *IfNDefMacro = 0; ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); - SkippingContents = true; + CurLexer->LexingRawMode = true; } // If this is a #elif with a #else before it, report the error. @@ -1331,7 +1324,7 @@ void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, // Finally, if we are out of the conditional (saw an #endif or ran off the end // of the file, just stop skipping and return to lexing whatever came after // the #if block. - SkippingContents = false; + CurLexer->LexingRawMode = false; } //===----------------------------------------------------------------------===// |

