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