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