diff options
author | Eli Friedman <eli.friedman@gmail.com> | 2013-09-19 00:41:32 +0000 |
---|---|---|
committer | Eli Friedman <eli.friedman@gmail.com> | 2013-09-19 00:41:32 +0000 |
commit | 0834a4b90139341eacdfff167b052595a95cf2e5 (patch) | |
tree | faeeee0d2c1fa35cfaf80c16b4e4ff862e749a85 /clang/lib/Lex/TokenLexer.cpp | |
parent | 5cc15addbec0797e8c8e2b8dc099415762b4b8de (diff) | |
download | bcm5719-llvm-0834a4b90139341eacdfff167b052595a95cf2e5.tar.gz bcm5719-llvm-0834a4b90139341eacdfff167b052595a95cf2e5.zip |
Make Preprocessor::Lex non-recursive.
Before this patch, Lex() would recurse whenever the current lexer changed (e.g.
upon entry into a macro). This patch turns the recursion into a loop: the
various lex routines now don't return a token when the current lexer changes,
and at the top level Preprocessor::Lex() now loops until it finds a token.
Normally, the recursion wouldn't end up being very deep, but the recursion depth
can explode in edge cases like a bunch of consecutive macros which expand to
nothing (like in the testcase test/Preprocessor/macro_expand_empty.c in this
patch).
<rdar://problem/14569770>
llvm-svn: 190980
Diffstat (limited to 'clang/lib/Lex/TokenLexer.cpp')
-rw-r--r-- | clang/lib/Lex/TokenLexer.cpp | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/clang/lib/Lex/TokenLexer.cpp b/clang/lib/Lex/TokenLexer.cpp index b83e76639d0..0213afcee92 100644 --- a/clang/lib/Lex/TokenLexer.cpp +++ b/clang/lib/Lex/TokenLexer.cpp @@ -416,22 +416,19 @@ void TokenLexer::ExpandFunctionArguments() { /// Lex - Lex and return a token from this macro stream. /// -void TokenLexer::Lex(Token &Tok) { +bool TokenLexer::Lex(Token &Tok) { // Lexing off the end of the macro, pop this macro off the expansion stack. if (isAtEnd()) { // If this is a macro (not a token stream), mark the macro enabled now // that it is no longer being expanded. if (Macro) Macro->EnableMacro(); - // Pop this context off the preprocessors lexer stack and get the next - // token. This will delete "this" so remember the PP instance var. - Preprocessor &PPCache = PP; - if (PP.HandleEndOfTokenLexer(Tok)) - return; - - // HandleEndOfTokenLexer may not return a token. If it doesn't, lex - // whatever is next. - return PPCache.Lex(Tok); + Tok.startToken(); + Tok.setFlagValue(Token::StartOfLine , AtStartOfLine); + Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace); + if (CurToken == 0) + Tok.setFlag(Token::LeadingEmptyMacro); + return PP.HandleEndOfTokenLexer(Tok); } SourceManager &SM = PP.getSourceManager(); @@ -451,7 +448,7 @@ void TokenLexer::Lex(Token &Tok) { // When handling the microsoft /##/ extension, the final token is // returned by PasteTokens, not the pasted token. if (PasteTokens(Tok)) - return; + return true; TokenIsFromPaste = true; } @@ -482,6 +479,8 @@ void TokenLexer::Lex(Token &Tok) { if (isFirstToken) { Tok.setFlagValue(Token::StartOfLine , AtStartOfLine); Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace); + AtStartOfLine = false; + HasLeadingSpace = false; } // Handle recursive expansion! @@ -499,10 +498,11 @@ void TokenLexer::Lex(Token &Tok) { } if (!DisableMacroExpansion && II->isHandleIdentifierCase()) - PP.HandleIdentifier(Tok); + return PP.HandleIdentifier(Tok); } // Otherwise, return a normal token. + return true; } /// PasteTokens - Tok is the LHS of a ## operator, and CurToken is the ## @@ -824,3 +824,8 @@ void TokenLexer::updateLocForMacroArgTokens(SourceLocation ArgIdSpellLoc, updateConsecutiveMacroArgTokens(SM, InstLoc, begin_tokens, end_tokens); } } + +void TokenLexer::PropagateLineStartLeadingSpaceInfo(Token &Result) { + AtStartOfLine = Result.isAtStartOfLine(); + HasLeadingSpace = Result.hasLeadingSpace(); +} |