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