blob: 24dda4f84c893eede720cd3f5c4dafcca57d74de [file] [log] [blame]
Chris Lattner8c32b1a2008-03-09 04:10:46 +00001//===--- 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 Lattner500d3292009-01-29 05:15:15 +000018#include "clang/Lex/LexDiagnostic.h"
Douglas Gregoraa93a872011-10-17 15:32:29 +000019#include "clang/Basic/FileManager.h"
Chris Lattner8c32b1a2008-03-09 04:10:46 +000020#include "clang/Basic/SourceManager.h"
Ted Kremenek15ba2af2008-11-20 07:56:33 +000021#include "llvm/Support/MemoryBuffer.h"
Chris Lattner8c32b1a2008-03-09 04:10:46 +000022using namespace clang;
23
Ted Kremenek4b391082008-11-18 01:33:13 +000024PPCallbacks::~PPCallbacks() {}
Chris Lattner8c32b1a2008-03-09 04:10:46 +000025
26//===----------------------------------------------------------------------===//
Chris Lattner6b884502008-03-10 06:06:04 +000027// Miscellaneous Methods.
Chris Lattner8c32b1a2008-03-09 04:10:46 +000028//===----------------------------------------------------------------------===//
29
Chris Lattner8c32b1a2008-03-09 04:10:46 +000030/// isInPrimaryFile - Return true if we're in the top-level file, not in a
Chris Lattner7d39d742008-03-09 04:49:35 +000031/// #include. This looks through macro expansions and active _Pragma lexers.
Chris Lattner8c32b1a2008-03-09 04:10:46 +000032bool Preprocessor::isInPrimaryFile() const {
Ted Kremenek81d24e12008-11-20 16:19:53 +000033 if (IsFileLexer())
Chris Lattner8c32b1a2008-03-09 04:10:46 +000034 return IncludeMacroStack.empty();
Mike Stump1eb44332009-09-09 15:08:12 +000035
Chris Lattner8c32b1a2008-03-09 04:10:46 +000036 // If there are any stacked lexers, we're in a #include.
Ted Kremenek81d24e12008-11-20 16:19:53 +000037 assert(IsFileLexer(IncludeMacroStack[0]) &&
Chris Lattner8c32b1a2008-03-09 04:10:46 +000038 "Top level include stack isn't our primary lexer?");
39 for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i)
Ted Kremenek81d24e12008-11-20 16:19:53 +000040 if (IsFileLexer(IncludeMacroStack[i]))
Chris Lattner8c32b1a2008-03-09 04:10:46 +000041 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 Kremenek68e48e42008-11-20 01:49:44 +000048PreprocessorLexer *Preprocessor::getCurrentFileLexer() const {
Ted Kremenek81d24e12008-11-20 16:19:53 +000049 if (IsFileLexer())
Ted Kremenek68e48e42008-11-20 01:49:44 +000050 return CurPPLexer;
Mike Stump1eb44332009-09-09 15:08:12 +000051
Chris Lattner8c32b1a2008-03-09 04:10:46 +000052 // Look for a stacked lexer.
53 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Ted Kremenek68e48e42008-11-20 01:49:44 +000054 const IncludeStackInfo& ISI = IncludeMacroStack[i-1];
Ted Kremenek81d24e12008-11-20 16:19:53 +000055 if (IsFileLexer(ISI))
Ted Kremenek68e48e42008-11-20 01:49:44 +000056 return ISI.ThePPLexer;
Chris Lattner8c32b1a2008-03-09 04:10:46 +000057 }
58 return 0;
59}
60
Chris Lattner6b884502008-03-10 06:06:04 +000061
62//===----------------------------------------------------------------------===//
63// Methods for Entering and Callbacks for leaving various contexts
64//===----------------------------------------------------------------------===//
Chris Lattner8c32b1a2008-03-09 04:10:46 +000065
66/// EnterSourceFile - Add a source file to the top of the include stack and
Nuno Lopese7f2cbd2009-11-29 17:07:16 +000067/// start lexing tokens from it instead of the current buffer.
Chris Lattnere127a0d2010-04-20 20:35:58 +000068void Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir,
69 SourceLocation Loc) {
Chris Lattner8c32b1a2008-03-09 04:10:46 +000070 assert(CurTokenLexer == 0 && "Cannot #include a file inside a macro!");
71 ++NumEnteredSourceFiles;
Mike Stump1eb44332009-09-09 15:08:12 +000072
Chris Lattner8c32b1a2008-03-09 04:10:46 +000073 if (MaxIncludeStackDepth < IncludeMacroStack.size())
74 MaxIncludeStackDepth = IncludeMacroStack.size();
75
Ted Kremenek6137dc92008-12-02 19:46:31 +000076 if (PTH) {
Chris Lattner6e290142009-11-30 04:18:44 +000077 if (PTHLexer *PL = PTH->CreateLexer(FID)) {
78 EnterSourceFileWithPTH(PL, CurDir);
Chris Lattnere127a0d2010-04-20 20:35:58 +000079 return;
Chris Lattner6e290142009-11-30 04:18:44 +000080 }
Ted Kremenek6137dc92008-12-02 19:46:31 +000081 }
Chris Lattner6e290142009-11-30 04:18:44 +000082
83 // Get the MemoryBuffer for this FID, if it fails, we fail.
Douglas Gregoraae58b02010-03-16 20:01:30 +000084 bool Invalid = false;
Chris Lattnere127a0d2010-04-20 20:35:58 +000085 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 Kyrtzidis7d100872011-09-04 03:32:15 +000093
94 if (isCodeCompletionEnabled() &&
95 SourceMgr.getFileEntryForID(FID) == CodeCompletionFile) {
96 CodeCompletionFileLoc = SourceMgr.getLocForStartOfFile(FID);
97 CodeCompletionLoc =
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000098 CodeCompletionFileLoc.getLocWithOffset(CodeCompletionOffset);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000099 }
100
Chris Lattner6e290142009-11-30 04:18:44 +0000101 EnterSourceFileWithLexer(new Lexer(FID, InputFile, *this), CurDir);
Chris Lattnere127a0d2010-04-20 20:35:58 +0000102 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000103}
Chris Lattner72181832008-09-26 20:12:23 +0000104
Ted Kremenek6137dc92008-12-02 19:46:31 +0000105/// 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 Stump1eb44332009-09-09 15:08:12 +0000107void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000108 const DirectoryLookup *CurDir) {
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000110 // Add the current lexer to the include stack.
Ted Kremenek41938c82008-11-19 21:57:25 +0000111 if (CurPPLexer || CurTokenLexer)
Ted Kremeneked04c4c2008-11-13 16:51:03 +0000112 PushIncludeMacroStack();
113
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000114 CurLexer.reset(TheLexer);
Ted Kremenek9c1b7502008-11-18 00:12:49 +0000115 CurPPLexer = TheLexer;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000116 CurDirLookup = CurDir;
Douglas Gregorb8db7cd2011-09-07 23:11:54 +0000117 if (CurLexerKind != CLK_LexAfterModuleImport)
118 CurLexerKind = CLK_Lexer;
119
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000120 // Notify the client, if desired, that we are in a new source file.
121 if (Callbacks && !CurLexer->Is_PragmaLexer) {
Chris Lattner9d728512008-10-27 01:19:25 +0000122 SrcMgr::CharacteristicKind FileType =
Chris Lattner0b9e7362008-09-26 21:18:42 +0000123 SourceMgr.getFileCharacteristic(CurLexer->getFileLoc());
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000125 Callbacks->FileChanged(CurLexer->getFileLoc(),
126 PPCallbacks::EnterFile, FileType);
127 }
128}
129
Ted Kremenek6137dc92008-12-02 19:46:31 +0000130/// EnterSourceFileWithPTH - Add a source file to the top of the include stack
131/// and start getting tokens from it using the PTH cache.
Mike Stump1eb44332009-09-09 15:08:12 +0000132void Preprocessor::EnterSourceFileWithPTH(PTHLexer *PL,
Ted Kremenek6137dc92008-12-02 19:46:31 +0000133 const DirectoryLookup *CurDir) {
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Ted Kremenek6137dc92008-12-02 19:46:31 +0000135 if (CurPPLexer || CurTokenLexer)
136 PushIncludeMacroStack();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000137
Ted Kremenek6137dc92008-12-02 19:46:31 +0000138 CurDirLookup = CurDir;
139 CurPTHLexer.reset(PL);
140 CurPPLexer = CurPTHLexer.get();
Douglas Gregorb8db7cd2011-09-07 23:11:54 +0000141 if (CurLexerKind != CLK_LexAfterModuleImport)
142 CurLexerKind = CLK_PTHLexer;
143
Ted Kremenek6137dc92008-12-02 19:46:31 +0000144 // Notify the client, if desired, that we are in a new source file.
145 if (Callbacks) {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000146 FileID FID = CurPPLexer->getFileID();
Chris Lattner8c61b532009-01-19 08:01:53 +0000147 SourceLocation EnterLoc = SourceMgr.getLocForStartOfFile(FID);
148 SrcMgr::CharacteristicKind FileType =
149 SourceMgr.getFileCharacteristic(EnterLoc);
150 Callbacks->FileChanged(EnterLoc, PPCallbacks::EnterFile, FileType);
Ted Kremenek6137dc92008-12-02 19:46:31 +0000151 }
152}
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000153
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 Lattnere7fb4842009-02-15 20:52:18 +0000156void Preprocessor::EnterMacro(Token &Tok, SourceLocation ILEnd,
157 MacroArgs *Args) {
Ted Kremeneked04c4c2008-11-13 16:51:03 +0000158 PushIncludeMacroStack();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000159 CurDirLookup = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000161 if (NumCachedTokenLexers == 0) {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000162 CurTokenLexer.reset(new TokenLexer(Tok, ILEnd, Args, *this));
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000163 } else {
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000164 CurTokenLexer.reset(TokenLexerCache[--NumCachedTokenLexers]);
Chris Lattnere7fb4842009-02-15 20:52:18 +0000165 CurTokenLexer->Init(Tok, ILEnd, Args);
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000166 }
Douglas Gregorb8db7cd2011-09-07 23:11:54 +0000167 if (CurLexerKind != CLK_LexAfterModuleImport)
168 CurLexerKind = CLK_TokenLexer;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000169}
170
171/// EnterTokenStream - Add a "macro" context to the top of the include stack,
Chris Lattner6b884502008-03-10 06:06:04 +0000172/// 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///
183void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks,
184 bool DisableMacroExpansion,
185 bool OwnsTokens) {
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000186 // Save our current state.
Ted Kremeneked04c4c2008-11-13 16:51:03 +0000187 PushIncludeMacroStack();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000188 CurDirLookup = 0;
189
190 // Create a macro expander to expand from the specified token stream.
191 if (NumCachedTokenLexers == 0) {
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000192 CurTokenLexer.reset(new TokenLexer(Toks, NumToks, DisableMacroExpansion,
193 OwnsTokens, *this));
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000194 } else {
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000195 CurTokenLexer.reset(TokenLexerCache[--NumCachedTokenLexers]);
Chris Lattner6b884502008-03-10 06:06:04 +0000196 CurTokenLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens);
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000197 }
Douglas Gregorb8db7cd2011-09-07 23:11:54 +0000198 if (CurLexerKind != CLK_LexAfterModuleImport)
199 CurLexerKind = CLK_TokenLexer;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000200}
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.
205bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
206 assert(!CurTokenLexer &&
207 "Ending a file when currently in a macro!");
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000209 // See if this file had a controlling macro.
Ted Kremenek1a531572008-11-19 22:43:49 +0000210 if (CurPPLexer) { // Not ending a macro, ignore it.
Mike Stump1eb44332009-09-09 15:08:12 +0000211 if (const IdentifierInfo *ControllingMacro =
Ted Kremenek1a531572008-11-19 22:43:49 +0000212 CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
Steve Naroff83d63c72009-04-24 20:03:17 +0000213 // Okay, this has a controlling macro, remember in HeaderFileInfo.
Mike Stump1eb44332009-09-09 15:08:12 +0000214 if (const FileEntry *FE =
Douglas Gregord83d2e72011-12-12 18:47:39 +0000215 SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000216 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
217 }
218 }
Mike Stump1eb44332009-09-09 15:08:12 +0000219
John McCall3eff3212011-10-18 00:44:04 +0000220 // Complain about reaching a true EOF within arc_cf_code_audited.
221 // We don't want to complain about reaching the end of a macro
222 // instantiation or a _Pragma.
223 if (PragmaARCCFCodeAuditedLoc.isValid() &&
John McCalld80d90d2011-10-18 01:36:41 +0000224 !isEndOfMacro && !(CurLexer && CurLexer->Is_PragmaLexer)) {
John McCall8dfac0b2011-09-30 05:12:12 +0000225 Diag(PragmaARCCFCodeAuditedLoc, diag::err_pp_eof_in_arc_cf_code_audited);
226
227 // Recover by leaving immediately.
228 PragmaARCCFCodeAuditedLoc = SourceLocation();
229 }
230
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000231 // If this is a #include'd file, pop it off the include stack and continue
232 // lexing the #includer file.
233 if (!IncludeMacroStack.empty()) {
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000234
235 // If we lexed the code-completion file, act as if we reached EOF.
236 if (isCodeCompletionEnabled() && CurPPLexer &&
237 SourceMgr.getLocForStartOfFile(CurPPLexer->getFileID()) ==
238 CodeCompletionFileLoc) {
239 if (CurLexer) {
240 Result.startToken();
241 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
242 CurLexer.reset();
243 } else {
244 assert(CurPTHLexer && "Got EOF but no current lexer set!");
245 CurPTHLexer->getEOF(Result);
246 CurPTHLexer.reset();
247 }
248
249 CurPPLexer = 0;
250 return true;
251 }
252
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000253 if (!isEndOfMacro && CurPPLexer &&
254 SourceMgr.getIncludeLoc(CurPPLexer->getFileID()).isValid()) {
255 // Notify SourceManager to record the number of FileIDs that were created
256 // during lexing of the #include'd file.
257 unsigned NumFIDs =
258 SourceMgr.local_sloc_entry_size() -
259 CurPPLexer->getInitialNumSLocEntries() + 1/*#include'd file*/;
260 SourceMgr.setNumCreatedFIDsForFileID(CurPPLexer->getFileID(), NumFIDs);
261 }
262
Argyrios Kyrtzidisc892c5f2011-10-11 17:29:44 +0000263 FileID ExitedFID;
264 if (Callbacks && !isEndOfMacro && CurPPLexer)
265 ExitedFID = CurPPLexer->getFileID();
266
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000267 // We're done with the #included file.
268 RemoveTopOfLexerStack();
269
270 // Notify the client, if desired, that we are in a new source file.
Ted Kremenek1a531572008-11-19 22:43:49 +0000271 if (Callbacks && !isEndOfMacro && CurPPLexer) {
Chris Lattner9d728512008-10-27 01:19:25 +0000272 SrcMgr::CharacteristicKind FileType =
Chris Lattner8c61b532009-01-19 08:01:53 +0000273 SourceMgr.getFileCharacteristic(CurPPLexer->getSourceLocation());
274 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
Argyrios Kyrtzidisc892c5f2011-10-11 17:29:44 +0000275 PPCallbacks::ExitFile, FileType, ExitedFID);
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000276 }
277
278 // Client should lex another token.
279 return false;
280 }
281
282 // If the file ends with a newline, form the EOF token on the newline itself,
283 // rather than "on the line following it", which doesn't exist. This makes
284 // diagnostics relating to the end of file include the last file that the user
285 // actually typed, which is goodness.
Ted Kremenek1a531572008-11-19 22:43:49 +0000286 if (CurLexer) {
287 const char *EndPos = CurLexer->BufferEnd;
Mike Stump1eb44332009-09-09 15:08:12 +0000288 if (EndPos != CurLexer->BufferStart &&
Ted Kremenek1a531572008-11-19 22:43:49 +0000289 (EndPos[-1] == '\n' || EndPos[-1] == '\r')) {
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000290 --EndPos;
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Ted Kremenek1a531572008-11-19 22:43:49 +0000292 // Handle \n\r and \r\n:
Mike Stump1eb44332009-09-09 15:08:12 +0000293 if (EndPos != CurLexer->BufferStart &&
Ted Kremenek1a531572008-11-19 22:43:49 +0000294 (EndPos[-1] == '\n' || EndPos[-1] == '\r') &&
295 EndPos[-1] != EndPos[0])
296 --EndPos;
297 }
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Ted Kremenek1a531572008-11-19 22:43:49 +0000299 Result.startToken();
300 CurLexer->BufferPtr = EndPos;
301 CurLexer->FormTokenWithChars(Result, EndPos, tok::eof);
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Ted Kremenek1a531572008-11-19 22:43:49 +0000303 // We're done with the #included file.
304 CurLexer.reset();
Chris Lattner67116082009-02-13 23:06:48 +0000305 } else {
306 assert(CurPTHLexer && "Got EOF but no current lexer set!");
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000307 CurPTHLexer->getEOF(Result);
Ted Kremenek1a531572008-11-19 22:43:49 +0000308 CurPTHLexer.reset();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000309 }
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Ted Kremenek41938c82008-11-19 21:57:25 +0000311 CurPPLexer = 0;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000312
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000313 // This is the end of the top-level file. 'WarnUnusedMacroLocs' has collected
314 // all macro locations that we need to warn because they are not used.
315 for (WarnUnusedMacroLocsTy::iterator
316 I=WarnUnusedMacroLocs.begin(), E=WarnUnusedMacroLocs.end(); I!=E; ++I)
317 Diag(*I, diag::pp_macro_not_used);
Daniel Dunbardbd82092010-03-23 05:09:10 +0000318
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000319 return true;
320}
321
322/// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer
323/// hits the end of its token stream.
324bool Preprocessor::HandleEndOfTokenLexer(Token &Result) {
Ted Kremenek1a531572008-11-19 22:43:49 +0000325 assert(CurTokenLexer && !CurPPLexer &&
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000326 "Ending a macro when currently in a #include file!");
327
Argyrios Kyrtzidis5b3284a2011-06-29 22:20:11 +0000328 if (!MacroExpandingLexersStack.empty() &&
329 MacroExpandingLexersStack.back().first == CurTokenLexer.get())
330 removeCachedMacroExpandedTokensOfLastLexer();
331
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000332 // Delete or cache the now-dead macro expander.
333 if (NumCachedTokenLexers == TokenLexerCacheSize)
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000334 CurTokenLexer.reset();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000335 else
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000336 TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000337
338 // Handle this like a #include file being popped off the stack.
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000339 return HandleEndOfFile(Result, true);
340}
341
342/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
343/// lexer stack. This should only be used in situations where the current
344/// state of the top-of-stack lexer is unknown.
345void Preprocessor::RemoveTopOfLexerStack() {
346 assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000348 if (CurTokenLexer) {
349 // Delete or cache the now-dead macro expander.
350 if (NumCachedTokenLexers == TokenLexerCacheSize)
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000351 CurTokenLexer.reset();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000352 else
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000353 TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take();
Mike Stump1eb44332009-09-09 15:08:12 +0000354 }
355
Ted Kremeneked04c4c2008-11-13 16:51:03 +0000356 PopIncludeMacroStack();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000357}
358
359/// HandleMicrosoftCommentPaste - When the macro expander pastes together a
360/// comment (/##/) in microsoft mode, this method handles updating the current
361/// state, returning the token on the next source line.
362void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) {
Ted Kremenek1a531572008-11-19 22:43:49 +0000363 assert(CurTokenLexer && !CurPPLexer &&
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000364 "Pasted comment can only be formed from macro");
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000366 // We handle this by scanning for the closest real lexer, switching it to
367 // raw mode and preprocessor mode. This will cause it to return \n as an
Peter Collingbourne84021552011-02-28 02:37:51 +0000368 // explicit EOD token.
Ted Kremenek1a531572008-11-19 22:43:49 +0000369 PreprocessorLexer *FoundLexer = 0;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000370 bool LexerWasInPPMode = false;
371 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
372 IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1);
Ted Kremenek1a531572008-11-19 22:43:49 +0000373 if (ISI.ThePPLexer == 0) continue; // Scan for a real lexer.
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000375 // Once we find a real lexer, mark it as raw mode (disabling macro
Peter Collingbourne84021552011-02-28 02:37:51 +0000376 // expansions) and preprocessor mode (return EOD). We know that the lexer
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000377 // was *not* in raw mode before, because the macro that the comment came
378 // from was expanded. However, it could have already been in preprocessor
379 // mode (#if COMMENT) in which case we have to return it to that mode and
Peter Collingbourne84021552011-02-28 02:37:51 +0000380 // return EOD.
Ted Kremenek1a531572008-11-19 22:43:49 +0000381 FoundLexer = ISI.ThePPLexer;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000382 FoundLexer->LexingRawMode = true;
383 LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective;
384 FoundLexer->ParsingPreprocessorDirective = true;
385 break;
386 }
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000388 // Okay, we either found and switched over the lexer, or we didn't find a
389 // lexer. In either case, finish off the macro the comment came from, getting
390 // the next token.
391 if (!HandleEndOfTokenLexer(Tok)) Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Peter Collingbourne84021552011-02-28 02:37:51 +0000393 // Discarding comments as long as we don't have EOF or EOD. This 'comments
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000394 // out' the rest of the line, including any tokens that came from other macros
395 // that were active, as in:
396 // #define submacro a COMMENT b
397 // submacro c
398 // which should lex to 'a' only: 'b' and 'c' should be removed.
Peter Collingbourne84021552011-02-28 02:37:51 +0000399 while (Tok.isNot(tok::eod) && Tok.isNot(tok::eof))
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000400 Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Peter Collingbourne84021552011-02-28 02:37:51 +0000402 // If we got an eod token, then we successfully found the end of the line.
403 if (Tok.is(tok::eod)) {
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000404 assert(FoundLexer && "Can't get end of line without an active lexer");
405 // Restore the lexer back to normal mode instead of raw mode.
406 FoundLexer->LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Peter Collingbourne84021552011-02-28 02:37:51 +0000408 // If the lexer was already in preprocessor mode, just return the EOD token
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000409 // to finish the preprocessor line.
410 if (LexerWasInPPMode) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000412 // Otherwise, switch out of PP mode and return the next lexed token.
413 FoundLexer->ParsingPreprocessorDirective = false;
414 return Lex(Tok);
415 }
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000417 // If we got an EOF token, then we reached the end of the token stream but
418 // didn't find an explicit \n. This can only happen if there was no lexer
Peter Collingbourne84021552011-02-28 02:37:51 +0000419 // active (an active lexer would return EOD at EOF if there was no \n in
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000420 // preprocessor directive mode), so just return EOF as our token.
Peter Collingbourne84021552011-02-28 02:37:51 +0000421 assert(!FoundLexer && "Lexer should return EOD before EOF in PP mode");
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000422}