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" |
| 18 | #include "clang/Lex/PPCallbacks.h" |
| 19 | #include "clang/Basic/Diagnostic.h" |
| 20 | #include "clang/Basic/SourceManager.h" |
| 21 | using namespace clang; |
| 22 | |
| 23 | PPCallbacks::~PPCallbacks() { |
| 24 | } |
| 25 | |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame^] | 28 | // Miscellaneous Methods. |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 31 | /// 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] | 32 | /// #include. This looks through macro expansions and active _Pragma lexers. |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 33 | bool Preprocessor::isInPrimaryFile() const { |
| 34 | if (CurLexer && !CurLexer->Is_PragmaLexer) |
| 35 | return IncludeMacroStack.empty(); |
| 36 | |
| 37 | // If there are any stacked lexers, we're in a #include. |
| 38 | assert(IncludeMacroStack[0].TheLexer && |
| 39 | !IncludeMacroStack[0].TheLexer->Is_PragmaLexer && |
| 40 | "Top level include stack isn't our primary lexer?"); |
| 41 | for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i) |
| 42 | if (IncludeMacroStack[i].TheLexer && |
| 43 | !IncludeMacroStack[i].TheLexer->Is_PragmaLexer) |
| 44 | return false; |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | /// getCurrentLexer - Return the current file lexer being lexed from. Note |
| 49 | /// that this ignores any potentially active macro expansions and _Pragma |
| 50 | /// expansions going on at the time. |
| 51 | Lexer *Preprocessor::getCurrentFileLexer() const { |
| 52 | if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer; |
| 53 | |
| 54 | // Look for a stacked lexer. |
| 55 | for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { |
| 56 | Lexer *L = IncludeMacroStack[i-1].TheLexer; |
| 57 | if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions. |
| 58 | return L; |
| 59 | } |
| 60 | return 0; |
| 61 | } |
| 62 | |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame^] | 63 | /// LookAhead - This peeks ahead N tokens and returns that token without |
| 64 | /// consuming any tokens. LookAhead(0) returns 'Tok', LookAhead(1) returns |
| 65 | /// the token after Tok, etc. |
| 66 | /// |
| 67 | /// NOTE: is a relatively expensive method, so it should not be used in common |
| 68 | /// code paths if possible! |
| 69 | /// |
| 70 | Token Preprocessor::LookAhead(unsigned N) { |
| 71 | Token *LookaheadTokens = new Token[N]; |
| 72 | |
| 73 | // Read N+1 tokens into LookaheadTokens. After this loop, Tok is the token |
| 74 | // to return. |
| 75 | Token Tok; |
| 76 | unsigned NumTokens = 0; |
| 77 | for (; N != ~0U; --N, ++NumTokens) { |
| 78 | Lex(Tok); |
| 79 | LookaheadTokens[NumTokens] = Tok; |
| 80 | |
| 81 | // If we got to EOF, don't lex past it. This will cause LookAhead to return |
| 82 | // the EOF token. |
| 83 | if (Tok.is(tok::eof)) |
| 84 | break; |
| 85 | } |
| 86 | |
| 87 | // Okay, at this point, we have the token we want to return in Tok. However, |
| 88 | // we read it and a bunch of other stuff (in LookaheadTokens) that we must |
| 89 | // allow subsequent calls to 'Lex' to return. To do this, we push a new token |
| 90 | // lexer onto the lexer stack with the tokens we read here. This passes |
| 91 | // ownership of LookaheadTokens to EnterTokenStream. |
| 92 | // |
| 93 | // Note that we disable macro expansion of the tokens from this buffer, since |
| 94 | // any macros have already been expanded, and the internal preprocessor state |
| 95 | // may already read past new macros. Consider something like LookAhead(1) on |
| 96 | // X |
| 97 | // #define X 14 |
| 98 | // Y |
| 99 | // The lookahead call should return 'Y', and the next Lex call should return |
| 100 | // 'X' even though X -> 14 has already been entered as a macro. |
| 101 | // |
| 102 | EnterTokenStream(LookaheadTokens, NumTokens, true /*DisableExpansion*/, |
| 103 | true /*OwnsTokens*/); |
| 104 | return Tok; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | //===----------------------------------------------------------------------===// |
| 109 | // Methods for Entering and Callbacks for leaving various contexts |
| 110 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 111 | |
| 112 | /// EnterSourceFile - Add a source file to the top of the include stack and |
| 113 | /// start lexing tokens from it instead of the current buffer. Return true |
| 114 | /// on failure. |
| 115 | void Preprocessor::EnterSourceFile(unsigned FileID, |
| 116 | const DirectoryLookup *CurDir) { |
| 117 | assert(CurTokenLexer == 0 && "Cannot #include a file inside a macro!"); |
| 118 | ++NumEnteredSourceFiles; |
| 119 | |
| 120 | if (MaxIncludeStackDepth < IncludeMacroStack.size()) |
| 121 | MaxIncludeStackDepth = IncludeMacroStack.size(); |
| 122 | |
| 123 | Lexer *TheLexer = new Lexer(SourceLocation::getFileLoc(FileID, 0), *this); |
| 124 | EnterSourceFileWithLexer(TheLexer, CurDir); |
| 125 | } |
| 126 | |
| 127 | /// EnterSourceFile - Add a source file to the top of the include stack and |
| 128 | /// start lexing tokens from it instead of the current buffer. |
| 129 | void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer, |
| 130 | const DirectoryLookup *CurDir) { |
| 131 | |
| 132 | // Add the current lexer to the include stack. |
| 133 | if (CurLexer || CurTokenLexer) |
| 134 | IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup, |
| 135 | CurTokenLexer)); |
| 136 | |
| 137 | CurLexer = TheLexer; |
| 138 | CurDirLookup = CurDir; |
| 139 | CurTokenLexer = 0; |
| 140 | |
| 141 | // Notify the client, if desired, that we are in a new source file. |
| 142 | if (Callbacks && !CurLexer->Is_PragmaLexer) { |
| 143 | DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir; |
| 144 | |
| 145 | // Get the file entry for the current file. |
| 146 | if (const FileEntry *FE = |
| 147 | SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())) |
| 148 | FileType = HeaderInfo.getFileDirFlavor(FE); |
| 149 | |
| 150 | Callbacks->FileChanged(CurLexer->getFileLoc(), |
| 151 | PPCallbacks::EnterFile, FileType); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | |
| 156 | |
| 157 | /// EnterMacro - Add a Macro to the top of the include stack and start lexing |
| 158 | /// tokens from it instead of the current buffer. |
| 159 | void Preprocessor::EnterMacro(Token &Tok, MacroArgs *Args) { |
| 160 | IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup, |
| 161 | CurTokenLexer)); |
| 162 | CurLexer = 0; |
| 163 | CurDirLookup = 0; |
| 164 | |
| 165 | if (NumCachedTokenLexers == 0) { |
| 166 | CurTokenLexer = new TokenLexer(Tok, Args, *this); |
| 167 | } else { |
| 168 | CurTokenLexer = TokenLexerCache[--NumCachedTokenLexers]; |
| 169 | CurTokenLexer->Init(Tok, Args); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /// EnterTokenStream - Add a "macro" context to the top of the include stack, |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame^] | 174 | /// which will cause the lexer to start returning the specified tokens. |
| 175 | /// |
| 176 | /// If DisableMacroExpansion is true, tokens lexed from the token stream will |
| 177 | /// not be subject to further macro expansion. Otherwise, these tokens will |
| 178 | /// be re-macro-expanded when/if expansion is enabled. |
| 179 | /// |
| 180 | /// If OwnsTokens is false, this method assumes that the specified stream of |
| 181 | /// tokens has a permanent owner somewhere, so they do not need to be copied. |
| 182 | /// If it is true, it assumes the array of tokens is allocated with new[] and |
| 183 | /// must be freed. |
| 184 | /// |
| 185 | void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks, |
| 186 | bool DisableMacroExpansion, |
| 187 | bool OwnsTokens) { |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 188 | // Save our current state. |
| 189 | IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup, |
| 190 | CurTokenLexer)); |
| 191 | CurLexer = 0; |
| 192 | CurDirLookup = 0; |
| 193 | |
| 194 | // Create a macro expander to expand from the specified token stream. |
| 195 | if (NumCachedTokenLexers == 0) { |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame^] | 196 | CurTokenLexer = new TokenLexer(Toks, NumToks, DisableMacroExpansion, |
| 197 | OwnsTokens, *this); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 198 | } else { |
| 199 | CurTokenLexer = TokenLexerCache[--NumCachedTokenLexers]; |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame^] | 200 | CurTokenLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | /// HandleEndOfFile - This callback is invoked when the lexer hits the end of |
| 205 | /// the current file. This either returns the EOF token or pops a level off |
| 206 | /// the include stack and keeps going. |
| 207 | bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) { |
| 208 | assert(!CurTokenLexer && |
| 209 | "Ending a file when currently in a macro!"); |
| 210 | |
| 211 | // See if this file had a controlling macro. |
| 212 | if (CurLexer) { // Not ending a macro, ignore it. |
| 213 | if (const IdentifierInfo *ControllingMacro = |
| 214 | CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) { |
| 215 | // Okay, this has a controlling macro, remember in PerFileInfo. |
| 216 | if (const FileEntry *FE = |
| 217 | SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())) |
| 218 | HeaderInfo.SetFileControllingMacro(FE, ControllingMacro); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // If this is a #include'd file, pop it off the include stack and continue |
| 223 | // lexing the #includer file. |
| 224 | if (!IncludeMacroStack.empty()) { |
| 225 | // We're done with the #included file. |
| 226 | RemoveTopOfLexerStack(); |
| 227 | |
| 228 | // Notify the client, if desired, that we are in a new source file. |
| 229 | if (Callbacks && !isEndOfMacro && CurLexer) { |
| 230 | DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir; |
| 231 | |
| 232 | // Get the file entry for the current file. |
| 233 | if (const FileEntry *FE = |
| 234 | SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())) |
| 235 | FileType = HeaderInfo.getFileDirFlavor(FE); |
| 236 | |
| 237 | Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr), |
| 238 | PPCallbacks::ExitFile, FileType); |
| 239 | } |
| 240 | |
| 241 | // Client should lex another token. |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | // If the file ends with a newline, form the EOF token on the newline itself, |
| 246 | // rather than "on the line following it", which doesn't exist. This makes |
| 247 | // diagnostics relating to the end of file include the last file that the user |
| 248 | // actually typed, which is goodness. |
| 249 | const char *EndPos = CurLexer->BufferEnd; |
| 250 | if (EndPos != CurLexer->BufferStart && |
| 251 | (EndPos[-1] == '\n' || EndPos[-1] == '\r')) { |
| 252 | --EndPos; |
| 253 | |
| 254 | // Handle \n\r and \r\n: |
| 255 | if (EndPos != CurLexer->BufferStart && |
| 256 | (EndPos[-1] == '\n' || EndPos[-1] == '\r') && |
| 257 | EndPos[-1] != EndPos[0]) |
| 258 | --EndPos; |
| 259 | } |
| 260 | |
| 261 | Result.startToken(); |
| 262 | CurLexer->BufferPtr = EndPos; |
| 263 | CurLexer->FormTokenWithChars(Result, EndPos); |
| 264 | Result.setKind(tok::eof); |
| 265 | |
| 266 | // We're done with the #included file. |
| 267 | delete CurLexer; |
| 268 | CurLexer = 0; |
| 269 | |
| 270 | // This is the end of the top-level file. If the diag::pp_macro_not_used |
| 271 | // diagnostic is enabled, look for macros that have not been used. |
| 272 | if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){ |
| 273 | for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I = |
| 274 | Macros.begin(), E = Macros.end(); I != E; ++I) { |
| 275 | if (!I->second->isUsed()) |
| 276 | Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used); |
| 277 | } |
| 278 | } |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | /// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer |
| 283 | /// hits the end of its token stream. |
| 284 | bool Preprocessor::HandleEndOfTokenLexer(Token &Result) { |
| 285 | assert(CurTokenLexer && !CurLexer && |
| 286 | "Ending a macro when currently in a #include file!"); |
| 287 | |
| 288 | // Delete or cache the now-dead macro expander. |
| 289 | if (NumCachedTokenLexers == TokenLexerCacheSize) |
| 290 | delete CurTokenLexer; |
| 291 | else |
| 292 | TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer; |
| 293 | |
| 294 | // Handle this like a #include file being popped off the stack. |
| 295 | CurTokenLexer = 0; |
| 296 | return HandleEndOfFile(Result, true); |
| 297 | } |
| 298 | |
| 299 | /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the |
| 300 | /// lexer stack. This should only be used in situations where the current |
| 301 | /// state of the top-of-stack lexer is unknown. |
| 302 | void Preprocessor::RemoveTopOfLexerStack() { |
| 303 | assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load"); |
| 304 | |
| 305 | if (CurTokenLexer) { |
| 306 | // Delete or cache the now-dead macro expander. |
| 307 | if (NumCachedTokenLexers == TokenLexerCacheSize) |
| 308 | delete CurTokenLexer; |
| 309 | else |
| 310 | TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer; |
| 311 | } else { |
| 312 | delete CurLexer; |
| 313 | } |
| 314 | CurLexer = IncludeMacroStack.back().TheLexer; |
| 315 | CurDirLookup = IncludeMacroStack.back().TheDirLookup; |
| 316 | CurTokenLexer = IncludeMacroStack.back().TheTokenLexer; |
| 317 | IncludeMacroStack.pop_back(); |
| 318 | } |
| 319 | |
| 320 | /// HandleMicrosoftCommentPaste - When the macro expander pastes together a |
| 321 | /// comment (/##/) in microsoft mode, this method handles updating the current |
| 322 | /// state, returning the token on the next source line. |
| 323 | void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) { |
| 324 | assert(CurTokenLexer && !CurLexer && |
| 325 | "Pasted comment can only be formed from macro"); |
| 326 | |
| 327 | // We handle this by scanning for the closest real lexer, switching it to |
| 328 | // raw mode and preprocessor mode. This will cause it to return \n as an |
| 329 | // explicit EOM token. |
| 330 | Lexer *FoundLexer = 0; |
| 331 | bool LexerWasInPPMode = false; |
| 332 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { |
| 333 | IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1); |
| 334 | if (ISI.TheLexer == 0) continue; // Scan for a real lexer. |
| 335 | |
| 336 | // Once we find a real lexer, mark it as raw mode (disabling macro |
| 337 | // expansions) and preprocessor mode (return EOM). We know that the lexer |
| 338 | // was *not* in raw mode before, because the macro that the comment came |
| 339 | // from was expanded. However, it could have already been in preprocessor |
| 340 | // mode (#if COMMENT) in which case we have to return it to that mode and |
| 341 | // return EOM. |
| 342 | FoundLexer = ISI.TheLexer; |
| 343 | FoundLexer->LexingRawMode = true; |
| 344 | LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective; |
| 345 | FoundLexer->ParsingPreprocessorDirective = true; |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | // Okay, we either found and switched over the lexer, or we didn't find a |
| 350 | // lexer. In either case, finish off the macro the comment came from, getting |
| 351 | // the next token. |
| 352 | if (!HandleEndOfTokenLexer(Tok)) Lex(Tok); |
| 353 | |
| 354 | // Discarding comments as long as we don't have EOF or EOM. This 'comments |
| 355 | // out' the rest of the line, including any tokens that came from other macros |
| 356 | // that were active, as in: |
| 357 | // #define submacro a COMMENT b |
| 358 | // submacro c |
| 359 | // which should lex to 'a' only: 'b' and 'c' should be removed. |
| 360 | while (Tok.isNot(tok::eom) && Tok.isNot(tok::eof)) |
| 361 | Lex(Tok); |
| 362 | |
| 363 | // If we got an eom token, then we successfully found the end of the line. |
| 364 | if (Tok.is(tok::eom)) { |
| 365 | assert(FoundLexer && "Can't get end of line without an active lexer"); |
| 366 | // Restore the lexer back to normal mode instead of raw mode. |
| 367 | FoundLexer->LexingRawMode = false; |
| 368 | |
| 369 | // If the lexer was already in preprocessor mode, just return the EOM token |
| 370 | // to finish the preprocessor line. |
| 371 | if (LexerWasInPPMode) return; |
| 372 | |
| 373 | // Otherwise, switch out of PP mode and return the next lexed token. |
| 374 | FoundLexer->ParsingPreprocessorDirective = false; |
| 375 | return Lex(Tok); |
| 376 | } |
| 377 | |
| 378 | // If we got an EOF token, then we reached the end of the token stream but |
| 379 | // didn't find an explicit \n. This can only happen if there was no lexer |
| 380 | // active (an active lexer would return EOM at EOF if there was no \n in |
| 381 | // preprocessor directive mode), so just return EOF as our token. |
| 382 | assert(!FoundLexer && "Lexer should return EOM before EOF in PP mode"); |
| 383 | } |