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" |
Ted Kremenek | 15ba2af | 2008-11-20 07:56:33 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MemoryBuffer.h" |
| 21 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Ted Kremenek | 4b39108 | 2008-11-18 01:33:13 +0000 | [diff] [blame] | 24 | PPCallbacks::~PPCallbacks() {} |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 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 { |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 33 | if (IsFileLexer()) |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 34 | return IncludeMacroStack.empty(); |
| 35 | |
| 36 | // If there are any stacked lexers, we're in a #include. |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 37 | assert(IsFileLexer(IncludeMacroStack[0]) && |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 38 | "Top level include stack isn't our primary lexer?"); |
| 39 | for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i) |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 40 | if (IsFileLexer(IncludeMacroStack[i])) |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 41 | return false; |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | /// getCurrentLexer - Return the current file lexer being lexed from. Note |
| 46 | /// that this ignores any potentially active macro expansions and _Pragma |
| 47 | /// expansions going on at the time. |
Ted Kremenek | 68e48e4 | 2008-11-20 01:49:44 +0000 | [diff] [blame] | 48 | PreprocessorLexer *Preprocessor::getCurrentFileLexer() const { |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 49 | if (IsFileLexer()) |
Ted Kremenek | 68e48e4 | 2008-11-20 01:49:44 +0000 | [diff] [blame] | 50 | return CurPPLexer; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 51 | |
| 52 | // Look for a stacked lexer. |
| 53 | for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { |
Ted Kremenek | 68e48e4 | 2008-11-20 01:49:44 +0000 | [diff] [blame] | 54 | const IncludeStackInfo& ISI = IncludeMacroStack[i-1]; |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 55 | if (IsFileLexer(ISI)) |
Ted Kremenek | 68e48e4 | 2008-11-20 01:49:44 +0000 | [diff] [blame] | 56 | return ISI.ThePPLexer; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 57 | } |
| 58 | return 0; |
| 59 | } |
| 60 | |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 61 | |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | // Methods for Entering and Callbacks for leaving various contexts |
| 64 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 65 | |
| 66 | /// EnterSourceFile - Add a source file to the top of the include stack and |
| 67 | /// start lexing tokens from it instead of the current buffer. Return true |
| 68 | /// on failure. |
| 69 | void Preprocessor::EnterSourceFile(unsigned FileID, |
| 70 | const DirectoryLookup *CurDir) { |
| 71 | assert(CurTokenLexer == 0 && "Cannot #include a file inside a macro!"); |
| 72 | ++NumEnteredSourceFiles; |
| 73 | |
| 74 | if (MaxIncludeStackDepth < IncludeMacroStack.size()) |
| 75 | MaxIncludeStackDepth = IncludeMacroStack.size(); |
| 76 | |
Ted Kremenek | b93efa3 | 2008-11-21 20:51:59 +0000 | [diff] [blame] | 77 | #if 1 |
Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 78 | if (PTH) { |
| 79 | PTHLexer* PL = |
| 80 | PTH->CreateLexer(FileID, getSourceManager().getFileEntryForID(FileID)); |
| 81 | |
| 82 | if (PL) { |
| 83 | EnterSourceFileWithPTH(PL, CurDir); |
| 84 | return; |
| 85 | } |
| 86 | } |
| 87 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 88 | Lexer *TheLexer = new Lexer(SourceLocation::getFileLoc(FileID, 0), *this); |
| 89 | EnterSourceFileWithLexer(TheLexer, CurDir); |
Ted Kremenek | 15ba2af | 2008-11-20 07:56:33 +0000 | [diff] [blame] | 90 | #else |
Ted Kremenek | 82a500b | 2008-11-27 00:38:24 +0000 | [diff] [blame] | 91 | if (CurPPLexer || CurTokenLexer) |
| 92 | PushIncludeMacroStack(); |
| 93 | |
| 94 | CurDirLookup = CurDir; |
| 95 | SourceLocation Loc = SourceLocation::getFileLoc(FileID, 0); |
| 96 | CurPTHLexer.reset(new PTHLexer(*this, Loc)); |
| 97 | CurPPLexer = CurPTHLexer.get(); |
| 98 | |
| 99 | // Generate the tokens. |
| 100 | |
Ted Kremenek | 15ba2af | 2008-11-20 07:56:33 +0000 | [diff] [blame] | 101 | const llvm::MemoryBuffer* B = getSourceManager().getBuffer(FileID); |
| 102 | |
| 103 | // Create a raw lexer. |
| 104 | Lexer L(SourceLocation::getFileLoc(FileID, 0), getLangOptions(), |
| 105 | B->getBufferStart(), B->getBufferEnd(), B); |
| 106 | |
| 107 | // Ignore whitespace. |
| 108 | L.SetKeepWhitespaceMode(false); |
| 109 | L.SetCommentRetentionState(false); |
| 110 | |
| 111 | // Lex the file, populating our data structures. |
Ted Kremenek | 82a500b | 2008-11-27 00:38:24 +0000 | [diff] [blame] | 112 | std::vector<Token>& Tokens = CurPTHLexer->getTokens(); |
Ted Kremenek | c840f0c | 2008-11-21 19:41:29 +0000 | [diff] [blame] | 113 | Token Tok; |
Ted Kremenek | 15ba2af | 2008-11-20 07:56:33 +0000 | [diff] [blame] | 114 | |
| 115 | do { |
| 116 | L.LexFromRawLexer(Tok); |
| 117 | |
Ted Kremenek | c840f0c | 2008-11-21 19:41:29 +0000 | [diff] [blame] | 118 | if (Tok.is(tok::identifier)) { |
Ted Kremenek | 15ba2af | 2008-11-20 07:56:33 +0000 | [diff] [blame] | 119 | Tok.setIdentifierInfo(LookUpIdentifierInfo(Tok)); |
Ted Kremenek | c840f0c | 2008-11-21 19:41:29 +0000 | [diff] [blame] | 120 | } |
| 121 | else if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
| 122 | // Special processing for #include. Store the '#' token and lex |
| 123 | // the next token. |
Ted Kremenek | 82a500b | 2008-11-27 00:38:24 +0000 | [diff] [blame] | 124 | Tokens.push_back(Tok); |
Ted Kremenek | c840f0c | 2008-11-21 19:41:29 +0000 | [diff] [blame] | 125 | L.LexFromRawLexer(Tok); |
| 126 | |
| 127 | // Did we see 'include'/'import'/'include_next'? |
| 128 | if (!Tok.is(tok::identifier)) |
| 129 | continue; |
| 130 | |
| 131 | IdentifierInfo* II = LookUpIdentifierInfo(Tok); |
| 132 | Tok.setIdentifierInfo(II); |
| 133 | tok::PPKeywordKind K = II->getPPKeywordID(); |
| 134 | |
| 135 | if (K == tok::pp_include || K == tok::pp_import || |
| 136 | K == tok::pp_include_next) { |
| 137 | |
| 138 | // Save the 'include' token. |
Ted Kremenek | 82a500b | 2008-11-27 00:38:24 +0000 | [diff] [blame] | 139 | Tokens.push_back(Tok); |
Ted Kremenek | c840f0c | 2008-11-21 19:41:29 +0000 | [diff] [blame] | 140 | |
| 141 | // Lex the next token as an include string. |
| 142 | L.ParsingPreprocessorDirective = true; |
| 143 | L.LexIncludeFilename(Tok); |
| 144 | L.ParsingPreprocessorDirective = false; |
Ted Kremenek | d5a8f0b | 2008-11-21 20:51:15 +0000 | [diff] [blame] | 145 | |
| 146 | if (Tok.is(tok::identifier)) |
| 147 | Tok.setIdentifierInfo(LookUpIdentifierInfo(Tok)); |
Ted Kremenek | c840f0c | 2008-11-21 19:41:29 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
Ted Kremenek | 15ba2af | 2008-11-20 07:56:33 +0000 | [diff] [blame] | 150 | } |
Ted Kremenek | 82a500b | 2008-11-27 00:38:24 +0000 | [diff] [blame] | 151 | while (Tokens.push_back(Tok), Tok.isNot(tok::eof)); |
Ted Kremenek | 15ba2af | 2008-11-20 07:56:33 +0000 | [diff] [blame] | 152 | |
| 153 | // Notify the client, if desired, that we are in a new source file. |
| 154 | if (Callbacks) { |
| 155 | SrcMgr::CharacteristicKind FileType = |
| 156 | SourceMgr.getFileCharacteristic(CurPPLexer->getFileID()); |
| 157 | Callbacks->FileChanged(Loc, PPCallbacks::EnterFile, FileType); |
| 158 | } |
| 159 | #endif |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 160 | } |
Chris Lattner | 7218183 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 161 | |
Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 162 | /// EnterSourceFileWithLexer - Add a source file to the top of the include stack |
| 163 | /// and start lexing tokens from it instead of the current buffer. |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 164 | void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer, |
| 165 | const DirectoryLookup *CurDir) { |
| 166 | |
| 167 | // Add the current lexer to the include stack. |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 168 | if (CurPPLexer || CurTokenLexer) |
Ted Kremenek | ed04c4c | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 169 | PushIncludeMacroStack(); |
| 170 | |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 171 | CurLexer.reset(TheLexer); |
Ted Kremenek | 9c1b750 | 2008-11-18 00:12:49 +0000 | [diff] [blame] | 172 | CurPPLexer = TheLexer; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 173 | CurDirLookup = CurDir; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 174 | |
| 175 | // Notify the client, if desired, that we are in a new source file. |
| 176 | if (Callbacks && !CurLexer->Is_PragmaLexer) { |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 177 | SrcMgr::CharacteristicKind FileType = |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 178 | SourceMgr.getFileCharacteristic(CurLexer->getFileLoc()); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 179 | |
| 180 | Callbacks->FileChanged(CurLexer->getFileLoc(), |
| 181 | PPCallbacks::EnterFile, FileType); |
| 182 | } |
| 183 | } |
| 184 | |
Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 185 | /// EnterSourceFileWithPTH - Add a source file to the top of the include stack |
| 186 | /// and start getting tokens from it using the PTH cache. |
| 187 | void Preprocessor::EnterSourceFileWithPTH(PTHLexer *PL, |
| 188 | const DirectoryLookup *CurDir) { |
| 189 | |
| 190 | if (CurPPLexer || CurTokenLexer) |
| 191 | PushIncludeMacroStack(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 192 | |
Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 193 | CurDirLookup = CurDir; |
| 194 | CurPTHLexer.reset(PL); |
| 195 | CurPPLexer = CurPTHLexer.get(); |
| 196 | |
| 197 | // Notify the client, if desired, that we are in a new source file. |
| 198 | if (Callbacks) { |
| 199 | unsigned FileID = CurPPLexer->getFileID(); |
| 200 | SrcMgr::CharacteristicKind FileType = |
| 201 | SourceMgr.getFileCharacteristic(CurPPLexer->getFileID()); |
| 202 | Callbacks->FileChanged(SourceLocation::getFileLoc(FileID, 0), |
| 203 | PPCallbacks::EnterFile, FileType); |
| 204 | } |
| 205 | } |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 206 | |
| 207 | /// EnterMacro - Add a Macro to the top of the include stack and start lexing |
| 208 | /// tokens from it instead of the current buffer. |
| 209 | void Preprocessor::EnterMacro(Token &Tok, MacroArgs *Args) { |
Ted Kremenek | ed04c4c | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 210 | PushIncludeMacroStack(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 211 | CurDirLookup = 0; |
| 212 | |
| 213 | if (NumCachedTokenLexers == 0) { |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 214 | CurTokenLexer.reset(new TokenLexer(Tok, Args, *this)); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 215 | } else { |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 216 | CurTokenLexer.reset(TokenLexerCache[--NumCachedTokenLexers]); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 217 | CurTokenLexer->Init(Tok, Args); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /// EnterTokenStream - Add a "macro" context to the top of the include stack, |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 222 | /// which will cause the lexer to start returning the specified tokens. |
| 223 | /// |
| 224 | /// If DisableMacroExpansion is true, tokens lexed from the token stream will |
| 225 | /// not be subject to further macro expansion. Otherwise, these tokens will |
| 226 | /// be re-macro-expanded when/if expansion is enabled. |
| 227 | /// |
| 228 | /// If OwnsTokens is false, this method assumes that the specified stream of |
| 229 | /// tokens has a permanent owner somewhere, so they do not need to be copied. |
| 230 | /// If it is true, it assumes the array of tokens is allocated with new[] and |
| 231 | /// must be freed. |
| 232 | /// |
| 233 | void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks, |
| 234 | bool DisableMacroExpansion, |
| 235 | bool OwnsTokens) { |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 236 | // Save our current state. |
Ted Kremenek | ed04c4c | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 237 | PushIncludeMacroStack(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 238 | CurDirLookup = 0; |
| 239 | |
| 240 | // Create a macro expander to expand from the specified token stream. |
| 241 | if (NumCachedTokenLexers == 0) { |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 242 | CurTokenLexer.reset(new TokenLexer(Toks, NumToks, DisableMacroExpansion, |
| 243 | OwnsTokens, *this)); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 244 | } else { |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 245 | CurTokenLexer.reset(TokenLexerCache[--NumCachedTokenLexers]); |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 246 | CurTokenLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
| 250 | /// HandleEndOfFile - This callback is invoked when the lexer hits the end of |
| 251 | /// the current file. This either returns the EOF token or pops a level off |
| 252 | /// the include stack and keeps going. |
| 253 | bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) { |
| 254 | assert(!CurTokenLexer && |
| 255 | "Ending a file when currently in a macro!"); |
| 256 | |
| 257 | // See if this file had a controlling macro. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 258 | if (CurPPLexer) { // Not ending a macro, ignore it. |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 259 | if (const IdentifierInfo *ControllingMacro = |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 260 | CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) { |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 261 | // Okay, this has a controlling macro, remember in PerFileInfo. |
| 262 | if (const FileEntry *FE = |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 263 | SourceMgr.getFileEntryForID(CurPPLexer->getFileID())) |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 264 | HeaderInfo.SetFileControllingMacro(FE, ControllingMacro); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // If this is a #include'd file, pop it off the include stack and continue |
| 269 | // lexing the #includer file. |
| 270 | if (!IncludeMacroStack.empty()) { |
| 271 | // We're done with the #included file. |
| 272 | RemoveTopOfLexerStack(); |
| 273 | |
| 274 | // 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] | 275 | if (Callbacks && !isEndOfMacro && CurPPLexer) { |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 276 | SrcMgr::CharacteristicKind FileType = |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 277 | SourceMgr.getFileCharacteristic(CurPPLexer->getFileID()); |
Ted Kremenek | bc0f6bc | 2008-12-10 23:20:59 +0000 | [diff] [blame^] | 278 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), |
| 279 | PPCallbacks::ExitFile, FileType); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | // Client should lex another token. |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | // If the file ends with a newline, form the EOF token on the newline itself, |
| 287 | // rather than "on the line following it", which doesn't exist. This makes |
| 288 | // diagnostics relating to the end of file include the last file that the user |
| 289 | // actually typed, which is goodness. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 290 | if (CurLexer) { |
| 291 | const char *EndPos = CurLexer->BufferEnd; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 292 | if (EndPos != CurLexer->BufferStart && |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 293 | (EndPos[-1] == '\n' || EndPos[-1] == '\r')) { |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 294 | --EndPos; |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 295 | |
| 296 | // Handle \n\r and \r\n: |
| 297 | if (EndPos != CurLexer->BufferStart && |
| 298 | (EndPos[-1] == '\n' || EndPos[-1] == '\r') && |
| 299 | EndPos[-1] != EndPos[0]) |
| 300 | --EndPos; |
| 301 | } |
| 302 | |
| 303 | Result.startToken(); |
| 304 | CurLexer->BufferPtr = EndPos; |
| 305 | CurLexer->FormTokenWithChars(Result, EndPos, tok::eof); |
| 306 | |
| 307 | // We're done with the #included file. |
| 308 | CurLexer.reset(); |
| 309 | } |
| 310 | else { |
| 311 | CurPTHLexer->setEOF(Result); |
| 312 | CurPTHLexer.reset(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 315 | CurPPLexer = 0; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 316 | |
| 317 | // This is the end of the top-level file. If the diag::pp_macro_not_used |
| 318 | // diagnostic is enabled, look for macros that have not been used. |
| 319 | if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){ |
| 320 | for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I = |
| 321 | Macros.begin(), E = Macros.end(); I != E; ++I) { |
| 322 | if (!I->second->isUsed()) |
| 323 | Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used); |
| 324 | } |
| 325 | } |
| 326 | return true; |
| 327 | } |
| 328 | |
| 329 | /// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer |
| 330 | /// hits the end of its token stream. |
| 331 | bool Preprocessor::HandleEndOfTokenLexer(Token &Result) { |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 332 | assert(CurTokenLexer && !CurPPLexer && |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 333 | "Ending a macro when currently in a #include file!"); |
| 334 | |
| 335 | // Delete or cache the now-dead macro expander. |
| 336 | if (NumCachedTokenLexers == TokenLexerCacheSize) |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 337 | CurTokenLexer.reset(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 338 | else |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 339 | TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 340 | |
| 341 | // Handle this like a #include file being popped off the stack. |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 342 | return HandleEndOfFile(Result, true); |
| 343 | } |
| 344 | |
| 345 | /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the |
| 346 | /// lexer stack. This should only be used in situations where the current |
| 347 | /// state of the top-of-stack lexer is unknown. |
| 348 | void Preprocessor::RemoveTopOfLexerStack() { |
| 349 | assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load"); |
| 350 | |
| 351 | if (CurTokenLexer) { |
| 352 | // Delete or cache the now-dead macro expander. |
| 353 | if (NumCachedTokenLexers == TokenLexerCacheSize) |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 354 | CurTokenLexer.reset(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 355 | else |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 356 | TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take(); |
Ted Kremenek | 15ba2af | 2008-11-20 07:56:33 +0000 | [diff] [blame] | 357 | } |
Ted Kremenek | ed04c4c | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 358 | |
| 359 | PopIncludeMacroStack(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /// HandleMicrosoftCommentPaste - When the macro expander pastes together a |
| 363 | /// comment (/##/) in microsoft mode, this method handles updating the current |
| 364 | /// state, returning the token on the next source line. |
| 365 | void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) { |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 366 | assert(CurTokenLexer && !CurPPLexer && |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 367 | "Pasted comment can only be formed from macro"); |
| 368 | |
| 369 | // We handle this by scanning for the closest real lexer, switching it to |
| 370 | // raw mode and preprocessor mode. This will cause it to return \n as an |
| 371 | // explicit EOM token. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 372 | PreprocessorLexer *FoundLexer = 0; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 373 | bool LexerWasInPPMode = false; |
| 374 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { |
| 375 | IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1); |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 376 | if (ISI.ThePPLexer == 0) continue; // Scan for a real lexer. |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 377 | |
| 378 | // Once we find a real lexer, mark it as raw mode (disabling macro |
| 379 | // expansions) and preprocessor mode (return EOM). We know that the lexer |
| 380 | // was *not* in raw mode before, because the macro that the comment came |
| 381 | // from was expanded. However, it could have already been in preprocessor |
| 382 | // mode (#if COMMENT) in which case we have to return it to that mode and |
| 383 | // return EOM. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 384 | FoundLexer = ISI.ThePPLexer; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 385 | FoundLexer->LexingRawMode = true; |
| 386 | LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective; |
| 387 | FoundLexer->ParsingPreprocessorDirective = true; |
| 388 | break; |
| 389 | } |
| 390 | |
| 391 | // Okay, we either found and switched over the lexer, or we didn't find a |
| 392 | // lexer. In either case, finish off the macro the comment came from, getting |
| 393 | // the next token. |
| 394 | if (!HandleEndOfTokenLexer(Tok)) Lex(Tok); |
| 395 | |
| 396 | // Discarding comments as long as we don't have EOF or EOM. This 'comments |
| 397 | // out' the rest of the line, including any tokens that came from other macros |
| 398 | // that were active, as in: |
| 399 | // #define submacro a COMMENT b |
| 400 | // submacro c |
| 401 | // which should lex to 'a' only: 'b' and 'c' should be removed. |
| 402 | while (Tok.isNot(tok::eom) && Tok.isNot(tok::eof)) |
| 403 | Lex(Tok); |
| 404 | |
| 405 | // If we got an eom token, then we successfully found the end of the line. |
| 406 | if (Tok.is(tok::eom)) { |
| 407 | assert(FoundLexer && "Can't get end of line without an active lexer"); |
| 408 | // Restore the lexer back to normal mode instead of raw mode. |
| 409 | FoundLexer->LexingRawMode = false; |
| 410 | |
| 411 | // If the lexer was already in preprocessor mode, just return the EOM token |
| 412 | // to finish the preprocessor line. |
| 413 | if (LexerWasInPPMode) return; |
| 414 | |
| 415 | // Otherwise, switch out of PP mode and return the next lexed token. |
| 416 | FoundLexer->ParsingPreprocessorDirective = false; |
| 417 | return Lex(Tok); |
| 418 | } |
| 419 | |
| 420 | // If we got an EOF token, then we reached the end of the token stream but |
| 421 | // didn't find an explicit \n. This can only happen if there was no lexer |
| 422 | // active (an active lexer would return EOM at EOF if there was no \n in |
| 423 | // preprocessor directive mode), so just return EOF as our token. |
| 424 | assert(!FoundLexer && "Lexer should return EOM before EOF in PP mode"); |
| 425 | } |