Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 1 | //===--- PPLexerChange.cpp - Handle changing lexers in the preprocessor ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements pieces of the Preprocessor interface that manage the |
| 11 | // current lexer stack. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Lex/Preprocessor.h" |
| 16 | #include "clang/Lex/HeaderSearch.h" |
| 17 | #include "clang/Lex/MacroInfo.h" |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
| 19 | #include "clang/Basic/SourceManager.h" |
| 20 | using namespace clang; |
| 21 | |
Ted Kremenek | 4b39108 | 2008-11-18 01:33:13 +0000 | [diff] [blame] | 22 | PPCallbacks::~PPCallbacks() {} |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 23 | |
| 24 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 25 | // Miscellaneous Methods. |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 28 | /// isInPrimaryFile - Return true if we're in the top-level file, not in a |
Chris Lattner | 7d39d74 | 2008-03-09 04:49:35 +0000 | [diff] [blame] | 29 | /// #include. This looks through macro expansions and active _Pragma lexers. |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 30 | bool Preprocessor::isInPrimaryFile() const { |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 31 | if (IsNonPragmaNonMacroLexer()) |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 32 | return IncludeMacroStack.empty(); |
| 33 | |
| 34 | // If there are any stacked lexers, we're in a #include. |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 35 | assert(IsNonPragmaNonMacroLexer(IncludeMacroStack[0]) && |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 36 | "Top level include stack isn't our primary lexer?"); |
| 37 | for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i) |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 38 | if (IsNonPragmaNonMacroLexer(IncludeMacroStack[i])) |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 39 | return false; |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | /// getCurrentLexer - Return the current file lexer being lexed from. Note |
| 44 | /// that this ignores any potentially active macro expansions and _Pragma |
| 45 | /// expansions going on at the time. |
Ted Kremenek | 68e48e4 | 2008-11-20 01:49:44 +0000 | [diff] [blame] | 46 | PreprocessorLexer *Preprocessor::getCurrentFileLexer() const { |
| 47 | if (IsNonPragmaNonMacroLexer()) |
| 48 | return CurPPLexer; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 49 | |
| 50 | // Look for a stacked lexer. |
| 51 | for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { |
Ted Kremenek | 68e48e4 | 2008-11-20 01:49:44 +0000 | [diff] [blame] | 52 | const IncludeStackInfo& ISI = IncludeMacroStack[i-1]; |
| 53 | if (IsNonPragmaNonMacroLexer(ISI)) |
| 54 | return ISI.ThePPLexer; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 55 | } |
| 56 | return 0; |
| 57 | } |
| 58 | |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 59 | |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | // Methods for Entering and Callbacks for leaving various contexts |
| 62 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 63 | |
| 64 | /// EnterSourceFile - Add a source file to the top of the include stack and |
| 65 | /// start lexing tokens from it instead of the current buffer. Return true |
| 66 | /// on failure. |
| 67 | void Preprocessor::EnterSourceFile(unsigned FileID, |
| 68 | const DirectoryLookup *CurDir) { |
| 69 | assert(CurTokenLexer == 0 && "Cannot #include a file inside a macro!"); |
| 70 | ++NumEnteredSourceFiles; |
| 71 | |
| 72 | if (MaxIncludeStackDepth < IncludeMacroStack.size()) |
| 73 | MaxIncludeStackDepth = IncludeMacroStack.size(); |
| 74 | |
| 75 | Lexer *TheLexer = new Lexer(SourceLocation::getFileLoc(FileID, 0), *this); |
| 76 | EnterSourceFileWithLexer(TheLexer, CurDir); |
| 77 | } |
Chris Lattner | 7218183 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 79 | /// EnterSourceFile - Add a source file to the top of the include stack and |
| 80 | /// start lexing tokens from it instead of the current buffer. |
| 81 | void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer, |
| 82 | const DirectoryLookup *CurDir) { |
| 83 | |
| 84 | // Add the current lexer to the include stack. |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 85 | if (CurPPLexer || CurTokenLexer) |
Ted Kremenek | ed04c4c | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 86 | PushIncludeMacroStack(); |
| 87 | |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 88 | CurLexer.reset(TheLexer); |
Ted Kremenek | 9c1b750 | 2008-11-18 00:12:49 +0000 | [diff] [blame] | 89 | CurPPLexer = TheLexer; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 90 | CurDirLookup = CurDir; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 91 | |
| 92 | // Notify the client, if desired, that we are in a new source file. |
| 93 | if (Callbacks && !CurLexer->Is_PragmaLexer) { |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 94 | SrcMgr::CharacteristicKind FileType = |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 95 | SourceMgr.getFileCharacteristic(CurLexer->getFileLoc()); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 96 | |
| 97 | Callbacks->FileChanged(CurLexer->getFileLoc(), |
| 98 | PPCallbacks::EnterFile, FileType); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | |
| 103 | |
| 104 | /// EnterMacro - Add a Macro to the top of the include stack and start lexing |
| 105 | /// tokens from it instead of the current buffer. |
| 106 | void Preprocessor::EnterMacro(Token &Tok, MacroArgs *Args) { |
Ted Kremenek | ed04c4c | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 107 | PushIncludeMacroStack(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 108 | CurDirLookup = 0; |
| 109 | |
| 110 | if (NumCachedTokenLexers == 0) { |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 111 | CurTokenLexer.reset(new TokenLexer(Tok, Args, *this)); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 112 | } else { |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 113 | CurTokenLexer.reset(TokenLexerCache[--NumCachedTokenLexers]); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 114 | CurTokenLexer->Init(Tok, Args); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /// EnterTokenStream - Add a "macro" context to the top of the include stack, |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 119 | /// which will cause the lexer to start returning the specified tokens. |
| 120 | /// |
| 121 | /// If DisableMacroExpansion is true, tokens lexed from the token stream will |
| 122 | /// not be subject to further macro expansion. Otherwise, these tokens will |
| 123 | /// be re-macro-expanded when/if expansion is enabled. |
| 124 | /// |
| 125 | /// If OwnsTokens is false, this method assumes that the specified stream of |
| 126 | /// tokens has a permanent owner somewhere, so they do not need to be copied. |
| 127 | /// If it is true, it assumes the array of tokens is allocated with new[] and |
| 128 | /// must be freed. |
| 129 | /// |
| 130 | void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks, |
| 131 | bool DisableMacroExpansion, |
| 132 | bool OwnsTokens) { |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 133 | // Save our current state. |
Ted Kremenek | ed04c4c | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 134 | PushIncludeMacroStack(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 135 | CurDirLookup = 0; |
| 136 | |
| 137 | // Create a macro expander to expand from the specified token stream. |
| 138 | if (NumCachedTokenLexers == 0) { |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 139 | CurTokenLexer.reset(new TokenLexer(Toks, NumToks, DisableMacroExpansion, |
| 140 | OwnsTokens, *this)); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 141 | } else { |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 142 | CurTokenLexer.reset(TokenLexerCache[--NumCachedTokenLexers]); |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 143 | CurTokenLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | /// HandleEndOfFile - This callback is invoked when the lexer hits the end of |
| 148 | /// the current file. This either returns the EOF token or pops a level off |
| 149 | /// the include stack and keeps going. |
| 150 | bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) { |
| 151 | assert(!CurTokenLexer && |
| 152 | "Ending a file when currently in a macro!"); |
| 153 | |
| 154 | // See if this file had a controlling macro. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 155 | if (CurPPLexer) { // Not ending a macro, ignore it. |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 156 | if (const IdentifierInfo *ControllingMacro = |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 157 | CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) { |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 158 | // Okay, this has a controlling macro, remember in PerFileInfo. |
| 159 | if (const FileEntry *FE = |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 160 | SourceMgr.getFileEntryForID(CurPPLexer->getFileID())) |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 161 | HeaderInfo.SetFileControllingMacro(FE, ControllingMacro); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // If this is a #include'd file, pop it off the include stack and continue |
| 166 | // lexing the #includer file. |
| 167 | if (!IncludeMacroStack.empty()) { |
| 168 | // We're done with the #included file. |
| 169 | RemoveTopOfLexerStack(); |
| 170 | |
| 171 | // Notify the client, if desired, that we are in a new source file. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 172 | if (Callbacks && !isEndOfMacro && CurPPLexer) { |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 173 | SrcMgr::CharacteristicKind FileType = |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 174 | SourceMgr.getFileCharacteristic(CurPPLexer->getFileID()); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 175 | |
Ted Kremenek | a751217 | 2008-11-20 01:52:55 +0000 | [diff] [blame] | 176 | if (CurLexer) { |
| 177 | // FIXME: Should we use the location of 'Result'? |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 178 | Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr), |
| 179 | PPCallbacks::ExitFile, FileType); |
Ted Kremenek | a751217 | 2008-11-20 01:52:55 +0000 | [diff] [blame] | 180 | } |
| 181 | else { |
| 182 | assert (0 && "FIXME: Add callback support for PTHLexer."); |
| 183 | } |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // Client should lex another token. |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | // If the file ends with a newline, form the EOF token on the newline itself, |
| 191 | // rather than "on the line following it", which doesn't exist. This makes |
| 192 | // diagnostics relating to the end of file include the last file that the user |
| 193 | // actually typed, which is goodness. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 194 | if (CurLexer) { |
| 195 | const char *EndPos = CurLexer->BufferEnd; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 196 | if (EndPos != CurLexer->BufferStart && |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 197 | (EndPos[-1] == '\n' || EndPos[-1] == '\r')) { |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 198 | --EndPos; |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 199 | |
| 200 | // Handle \n\r and \r\n: |
| 201 | if (EndPos != CurLexer->BufferStart && |
| 202 | (EndPos[-1] == '\n' || EndPos[-1] == '\r') && |
| 203 | EndPos[-1] != EndPos[0]) |
| 204 | --EndPos; |
| 205 | } |
| 206 | |
| 207 | Result.startToken(); |
| 208 | CurLexer->BufferPtr = EndPos; |
| 209 | CurLexer->FormTokenWithChars(Result, EndPos, tok::eof); |
| 210 | |
| 211 | // We're done with the #included file. |
| 212 | CurLexer.reset(); |
| 213 | } |
| 214 | else { |
| 215 | CurPTHLexer->setEOF(Result); |
| 216 | CurPTHLexer.reset(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 219 | CurPPLexer = 0; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 220 | |
| 221 | // This is the end of the top-level file. If the diag::pp_macro_not_used |
| 222 | // diagnostic is enabled, look for macros that have not been used. |
| 223 | if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){ |
| 224 | for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I = |
| 225 | Macros.begin(), E = Macros.end(); I != E; ++I) { |
| 226 | if (!I->second->isUsed()) |
| 227 | Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used); |
| 228 | } |
| 229 | } |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | /// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer |
| 234 | /// hits the end of its token stream. |
| 235 | bool Preprocessor::HandleEndOfTokenLexer(Token &Result) { |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 236 | assert(CurTokenLexer && !CurPPLexer && |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 237 | "Ending a macro when currently in a #include file!"); |
| 238 | |
| 239 | // Delete or cache the now-dead macro expander. |
| 240 | if (NumCachedTokenLexers == TokenLexerCacheSize) |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 241 | CurTokenLexer.reset(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 242 | else |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 243 | TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 244 | |
| 245 | // Handle this like a #include file being popped off the stack. |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 246 | return HandleEndOfFile(Result, true); |
| 247 | } |
| 248 | |
| 249 | /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the |
| 250 | /// lexer stack. This should only be used in situations where the current |
| 251 | /// state of the top-of-stack lexer is unknown. |
| 252 | void Preprocessor::RemoveTopOfLexerStack() { |
| 253 | assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load"); |
| 254 | |
| 255 | if (CurTokenLexer) { |
| 256 | // Delete or cache the now-dead macro expander. |
| 257 | if (NumCachedTokenLexers == TokenLexerCacheSize) |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 258 | CurTokenLexer.reset(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 259 | else |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 260 | TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 261 | } else { |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 262 | CurLexer.reset(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 263 | } |
Ted Kremenek | ed04c4c | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 264 | |
| 265 | PopIncludeMacroStack(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /// HandleMicrosoftCommentPaste - When the macro expander pastes together a |
| 269 | /// comment (/##/) in microsoft mode, this method handles updating the current |
| 270 | /// state, returning the token on the next source line. |
| 271 | void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) { |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 272 | assert(CurTokenLexer && !CurPPLexer && |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 273 | "Pasted comment can only be formed from macro"); |
| 274 | |
| 275 | // We handle this by scanning for the closest real lexer, switching it to |
| 276 | // raw mode and preprocessor mode. This will cause it to return \n as an |
| 277 | // explicit EOM token. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 278 | PreprocessorLexer *FoundLexer = 0; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 279 | bool LexerWasInPPMode = false; |
| 280 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { |
| 281 | IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1); |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 282 | if (ISI.ThePPLexer == 0) continue; // Scan for a real lexer. |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 283 | |
| 284 | // Once we find a real lexer, mark it as raw mode (disabling macro |
| 285 | // expansions) and preprocessor mode (return EOM). We know that the lexer |
| 286 | // was *not* in raw mode before, because the macro that the comment came |
| 287 | // from was expanded. However, it could have already been in preprocessor |
| 288 | // mode (#if COMMENT) in which case we have to return it to that mode and |
| 289 | // return EOM. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 290 | FoundLexer = ISI.ThePPLexer; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 291 | FoundLexer->LexingRawMode = true; |
| 292 | LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective; |
| 293 | FoundLexer->ParsingPreprocessorDirective = true; |
| 294 | break; |
| 295 | } |
| 296 | |
| 297 | // Okay, we either found and switched over the lexer, or we didn't find a |
| 298 | // lexer. In either case, finish off the macro the comment came from, getting |
| 299 | // the next token. |
| 300 | if (!HandleEndOfTokenLexer(Tok)) Lex(Tok); |
| 301 | |
| 302 | // Discarding comments as long as we don't have EOF or EOM. This 'comments |
| 303 | // out' the rest of the line, including any tokens that came from other macros |
| 304 | // that were active, as in: |
| 305 | // #define submacro a COMMENT b |
| 306 | // submacro c |
| 307 | // which should lex to 'a' only: 'b' and 'c' should be removed. |
| 308 | while (Tok.isNot(tok::eom) && Tok.isNot(tok::eof)) |
| 309 | Lex(Tok); |
| 310 | |
| 311 | // If we got an eom token, then we successfully found the end of the line. |
| 312 | if (Tok.is(tok::eom)) { |
| 313 | assert(FoundLexer && "Can't get end of line without an active lexer"); |
| 314 | // Restore the lexer back to normal mode instead of raw mode. |
| 315 | FoundLexer->LexingRawMode = false; |
| 316 | |
| 317 | // If the lexer was already in preprocessor mode, just return the EOM token |
| 318 | // to finish the preprocessor line. |
| 319 | if (LexerWasInPPMode) return; |
| 320 | |
| 321 | // Otherwise, switch out of PP mode and return the next lexed token. |
| 322 | FoundLexer->ParsingPreprocessorDirective = false; |
| 323 | return Lex(Tok); |
| 324 | } |
| 325 | |
| 326 | // If we got an EOF token, then we reached the end of the token stream but |
| 327 | // didn't find an explicit \n. This can only happen if there was no lexer |
| 328 | // active (an active lexer would return EOM at EOF if there was no \n in |
| 329 | // preprocessor directive mode), so just return EOF as our token. |
| 330 | assert(!FoundLexer && "Lexer should return EOM before EOF in PP mode"); |
| 331 | } |