blob: 758bfc4bb0fdd528b29ba86293503c6ac91b2fbb [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"
Chris Lattner8c32b1a2008-03-09 04:10:46 +000019#include "clang/Basic/SourceManager.h"
Ted Kremenek15ba2af2008-11-20 07:56:33 +000020#include "llvm/Support/MemoryBuffer.h"
Chris Lattner8c32b1a2008-03-09 04:10:46 +000021using namespace clang;
22
Ted Kremenek4b391082008-11-18 01:33:13 +000023PPCallbacks::~PPCallbacks() {}
Chris Lattner8c32b1a2008-03-09 04:10:46 +000024
25//===----------------------------------------------------------------------===//
Chris Lattner6b884502008-03-10 06:06:04 +000026// Miscellaneous Methods.
Chris Lattner8c32b1a2008-03-09 04:10:46 +000027//===----------------------------------------------------------------------===//
28
Chris Lattner8c32b1a2008-03-09 04:10:46 +000029/// isInPrimaryFile - Return true if we're in the top-level file, not in a
Chris Lattner7d39d742008-03-09 04:49:35 +000030/// #include. This looks through macro expansions and active _Pragma lexers.
Chris Lattner8c32b1a2008-03-09 04:10:46 +000031bool Preprocessor::isInPrimaryFile() const {
Ted Kremenek81d24e12008-11-20 16:19:53 +000032 if (IsFileLexer())
Chris Lattner8c32b1a2008-03-09 04:10:46 +000033 return IncludeMacroStack.empty();
Mike Stump1eb44332009-09-09 15:08:12 +000034
Chris Lattner8c32b1a2008-03-09 04:10:46 +000035 // If there are any stacked lexers, we're in a #include.
Ted Kremenek81d24e12008-11-20 16:19:53 +000036 assert(IsFileLexer(IncludeMacroStack[0]) &&
Chris Lattner8c32b1a2008-03-09 04:10:46 +000037 "Top level include stack isn't our primary lexer?");
38 for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i)
Ted Kremenek81d24e12008-11-20 16:19:53 +000039 if (IsFileLexer(IncludeMacroStack[i]))
Chris Lattner8c32b1a2008-03-09 04:10:46 +000040 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 Kremenek68e48e42008-11-20 01:49:44 +000047PreprocessorLexer *Preprocessor::getCurrentFileLexer() const {
Ted Kremenek81d24e12008-11-20 16:19:53 +000048 if (IsFileLexer())
Ted Kremenek68e48e42008-11-20 01:49:44 +000049 return CurPPLexer;
Mike Stump1eb44332009-09-09 15:08:12 +000050
Chris Lattner8c32b1a2008-03-09 04:10:46 +000051 // Look for a stacked lexer.
52 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Ted Kremenek68e48e42008-11-20 01:49:44 +000053 const IncludeStackInfo& ISI = IncludeMacroStack[i-1];
Ted Kremenek81d24e12008-11-20 16:19:53 +000054 if (IsFileLexer(ISI))
Ted Kremenek68e48e42008-11-20 01:49:44 +000055 return ISI.ThePPLexer;
Chris Lattner8c32b1a2008-03-09 04:10:46 +000056 }
57 return 0;
58}
59
Chris Lattner6b884502008-03-10 06:06:04 +000060
61//===----------------------------------------------------------------------===//
62// Methods for Entering and Callbacks for leaving various contexts
63//===----------------------------------------------------------------------===//
Chris Lattner8c32b1a2008-03-09 04:10:46 +000064
65/// EnterSourceFile - Add a source file to the top of the include stack and
Nuno Lopese7f2cbd2009-11-29 17:07:16 +000066/// start lexing tokens from it instead of the current buffer.
Chris Lattnere127a0d2010-04-20 20:35:58 +000067void Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir,
68 SourceLocation Loc) {
Chris Lattner8c32b1a2008-03-09 04:10:46 +000069 assert(CurTokenLexer == 0 && "Cannot #include a file inside a macro!");
70 ++NumEnteredSourceFiles;
Mike Stump1eb44332009-09-09 15:08:12 +000071
Chris Lattner8c32b1a2008-03-09 04:10:46 +000072 if (MaxIncludeStackDepth < IncludeMacroStack.size())
73 MaxIncludeStackDepth = IncludeMacroStack.size();
74
Ted Kremenek6137dc92008-12-02 19:46:31 +000075 if (PTH) {
Chris Lattner6e290142009-11-30 04:18:44 +000076 if (PTHLexer *PL = PTH->CreateLexer(FID)) {
77 EnterSourceFileWithPTH(PL, CurDir);
Chris Lattnere127a0d2010-04-20 20:35:58 +000078 return;
Chris Lattner6e290142009-11-30 04:18:44 +000079 }
Ted Kremenek6137dc92008-12-02 19:46:31 +000080 }
Chris Lattner6e290142009-11-30 04:18:44 +000081
82 // Get the MemoryBuffer for this FID, if it fails, we fail.
Douglas Gregoraae58b02010-03-16 20:01:30 +000083 bool Invalid = false;
Chris Lattnere127a0d2010-04-20 20:35:58 +000084 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 Kyrtzidis7d100872011-09-04 03:32:15 +000092
93 if (isCodeCompletionEnabled() &&
94 SourceMgr.getFileEntryForID(FID) == CodeCompletionFile) {
95 CodeCompletionFileLoc = SourceMgr.getLocForStartOfFile(FID);
96 CodeCompletionLoc =
97 CodeCompletionFileLoc.getFileLocWithOffset(CodeCompletionOffset);
98 }
99
Chris Lattner6e290142009-11-30 04:18:44 +0000100 EnterSourceFileWithLexer(new Lexer(FID, InputFile, *this), CurDir);
Chris Lattnere127a0d2010-04-20 20:35:58 +0000101 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000102}
Chris Lattner72181832008-09-26 20:12:23 +0000103
Ted Kremenek6137dc92008-12-02 19:46:31 +0000104/// 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 Stump1eb44332009-09-09 15:08:12 +0000106void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000107 const DirectoryLookup *CurDir) {
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000109 // Add the current lexer to the include stack.
Ted Kremenek41938c82008-11-19 21:57:25 +0000110 if (CurPPLexer || CurTokenLexer)
Ted Kremeneked04c4c2008-11-13 16:51:03 +0000111 PushIncludeMacroStack();
112
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000113 CurLexer.reset(TheLexer);
Ted Kremenek9c1b7502008-11-18 00:12:49 +0000114 CurPPLexer = TheLexer;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000115 CurDirLookup = CurDir;
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000117 // Notify the client, if desired, that we are in a new source file.
118 if (Callbacks && !CurLexer->Is_PragmaLexer) {
Chris Lattner9d728512008-10-27 01:19:25 +0000119 SrcMgr::CharacteristicKind FileType =
Chris Lattner0b9e7362008-09-26 21:18:42 +0000120 SourceMgr.getFileCharacteristic(CurLexer->getFileLoc());
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000122 Callbacks->FileChanged(CurLexer->getFileLoc(),
123 PPCallbacks::EnterFile, FileType);
124 }
125}
126
Ted Kremenek6137dc92008-12-02 19:46:31 +0000127/// EnterSourceFileWithPTH - Add a source file to the top of the include stack
128/// and start getting tokens from it using the PTH cache.
Mike Stump1eb44332009-09-09 15:08:12 +0000129void Preprocessor::EnterSourceFileWithPTH(PTHLexer *PL,
Ted Kremenek6137dc92008-12-02 19:46:31 +0000130 const DirectoryLookup *CurDir) {
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Ted Kremenek6137dc92008-12-02 19:46:31 +0000132 if (CurPPLexer || CurTokenLexer)
133 PushIncludeMacroStack();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000134
Ted Kremenek6137dc92008-12-02 19:46:31 +0000135 CurDirLookup = CurDir;
136 CurPTHLexer.reset(PL);
137 CurPPLexer = CurPTHLexer.get();
138
139 // Notify the client, if desired, that we are in a new source file.
140 if (Callbacks) {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000141 FileID FID = CurPPLexer->getFileID();
Chris Lattner8c61b532009-01-19 08:01:53 +0000142 SourceLocation EnterLoc = SourceMgr.getLocForStartOfFile(FID);
143 SrcMgr::CharacteristicKind FileType =
144 SourceMgr.getFileCharacteristic(EnterLoc);
145 Callbacks->FileChanged(EnterLoc, PPCallbacks::EnterFile, FileType);
Ted Kremenek6137dc92008-12-02 19:46:31 +0000146 }
147}
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000148
149/// EnterMacro - Add a Macro to the top of the include stack and start lexing
150/// tokens from it instead of the current buffer.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000151void Preprocessor::EnterMacro(Token &Tok, SourceLocation ILEnd,
152 MacroArgs *Args) {
Ted Kremeneked04c4c2008-11-13 16:51:03 +0000153 PushIncludeMacroStack();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000154 CurDirLookup = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000156 if (NumCachedTokenLexers == 0) {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000157 CurTokenLexer.reset(new TokenLexer(Tok, ILEnd, Args, *this));
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000158 } else {
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000159 CurTokenLexer.reset(TokenLexerCache[--NumCachedTokenLexers]);
Chris Lattnere7fb4842009-02-15 20:52:18 +0000160 CurTokenLexer->Init(Tok, ILEnd, Args);
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000161 }
162}
163
164/// EnterTokenStream - Add a "macro" context to the top of the include stack,
Chris Lattner6b884502008-03-10 06:06:04 +0000165/// which will cause the lexer to start returning the specified tokens.
166///
167/// If DisableMacroExpansion is true, tokens lexed from the token stream will
168/// not be subject to further macro expansion. Otherwise, these tokens will
169/// be re-macro-expanded when/if expansion is enabled.
170///
171/// If OwnsTokens is false, this method assumes that the specified stream of
172/// tokens has a permanent owner somewhere, so they do not need to be copied.
173/// If it is true, it assumes the array of tokens is allocated with new[] and
174/// must be freed.
175///
176void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks,
177 bool DisableMacroExpansion,
178 bool OwnsTokens) {
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000179 // Save our current state.
Ted Kremeneked04c4c2008-11-13 16:51:03 +0000180 PushIncludeMacroStack();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000181 CurDirLookup = 0;
182
183 // Create a macro expander to expand from the specified token stream.
184 if (NumCachedTokenLexers == 0) {
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000185 CurTokenLexer.reset(new TokenLexer(Toks, NumToks, DisableMacroExpansion,
186 OwnsTokens, *this));
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000187 } else {
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000188 CurTokenLexer.reset(TokenLexerCache[--NumCachedTokenLexers]);
Chris Lattner6b884502008-03-10 06:06:04 +0000189 CurTokenLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens);
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000190 }
191}
192
193/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
194/// the current file. This either returns the EOF token or pops a level off
195/// the include stack and keeps going.
196bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
197 assert(!CurTokenLexer &&
198 "Ending a file when currently in a macro!");
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000200 // See if this file had a controlling macro.
Ted Kremenek1a531572008-11-19 22:43:49 +0000201 if (CurPPLexer) { // Not ending a macro, ignore it.
Mike Stump1eb44332009-09-09 15:08:12 +0000202 if (const IdentifierInfo *ControllingMacro =
Ted Kremenek1a531572008-11-19 22:43:49 +0000203 CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
Steve Naroff83d63c72009-04-24 20:03:17 +0000204 // Okay, this has a controlling macro, remember in HeaderFileInfo.
Mike Stump1eb44332009-09-09 15:08:12 +0000205 if (const FileEntry *FE =
Ted Kremenek1a531572008-11-19 22:43:49 +0000206 SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000207 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
208 }
209 }
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000211 // If this is a #include'd file, pop it off the include stack and continue
212 // lexing the #includer file.
213 if (!IncludeMacroStack.empty()) {
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000214
215 // If we lexed the code-completion file, act as if we reached EOF.
216 if (isCodeCompletionEnabled() && CurPPLexer &&
217 SourceMgr.getLocForStartOfFile(CurPPLexer->getFileID()) ==
218 CodeCompletionFileLoc) {
219 if (CurLexer) {
220 Result.startToken();
221 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
222 CurLexer.reset();
223 } else {
224 assert(CurPTHLexer && "Got EOF but no current lexer set!");
225 CurPTHLexer->getEOF(Result);
226 CurPTHLexer.reset();
227 }
228
229 CurPPLexer = 0;
230 return true;
231 }
232
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000233 if (!isEndOfMacro && CurPPLexer &&
234 SourceMgr.getIncludeLoc(CurPPLexer->getFileID()).isValid()) {
235 // Notify SourceManager to record the number of FileIDs that were created
236 // during lexing of the #include'd file.
237 unsigned NumFIDs =
238 SourceMgr.local_sloc_entry_size() -
239 CurPPLexer->getInitialNumSLocEntries() + 1/*#include'd file*/;
240 SourceMgr.setNumCreatedFIDsForFileID(CurPPLexer->getFileID(), NumFIDs);
241 }
242
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000243 // We're done with the #included file.
244 RemoveTopOfLexerStack();
245
246 // Notify the client, if desired, that we are in a new source file.
Ted Kremenek1a531572008-11-19 22:43:49 +0000247 if (Callbacks && !isEndOfMacro && CurPPLexer) {
Chris Lattner9d728512008-10-27 01:19:25 +0000248 SrcMgr::CharacteristicKind FileType =
Chris Lattner8c61b532009-01-19 08:01:53 +0000249 SourceMgr.getFileCharacteristic(CurPPLexer->getSourceLocation());
250 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000251 PPCallbacks::ExitFile, FileType);
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000252 }
253
254 // Client should lex another token.
255 return false;
256 }
257
258 // If the file ends with a newline, form the EOF token on the newline itself,
259 // rather than "on the line following it", which doesn't exist. This makes
260 // diagnostics relating to the end of file include the last file that the user
261 // actually typed, which is goodness.
Ted Kremenek1a531572008-11-19 22:43:49 +0000262 if (CurLexer) {
263 const char *EndPos = CurLexer->BufferEnd;
Mike Stump1eb44332009-09-09 15:08:12 +0000264 if (EndPos != CurLexer->BufferStart &&
Ted Kremenek1a531572008-11-19 22:43:49 +0000265 (EndPos[-1] == '\n' || EndPos[-1] == '\r')) {
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000266 --EndPos;
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Ted Kremenek1a531572008-11-19 22:43:49 +0000268 // Handle \n\r and \r\n:
Mike Stump1eb44332009-09-09 15:08:12 +0000269 if (EndPos != CurLexer->BufferStart &&
Ted Kremenek1a531572008-11-19 22:43:49 +0000270 (EndPos[-1] == '\n' || EndPos[-1] == '\r') &&
271 EndPos[-1] != EndPos[0])
272 --EndPos;
273 }
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Ted Kremenek1a531572008-11-19 22:43:49 +0000275 Result.startToken();
276 CurLexer->BufferPtr = EndPos;
277 CurLexer->FormTokenWithChars(Result, EndPos, tok::eof);
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Ted Kremenek1a531572008-11-19 22:43:49 +0000279 // We're done with the #included file.
280 CurLexer.reset();
Chris Lattner67116082009-02-13 23:06:48 +0000281 } else {
282 assert(CurPTHLexer && "Got EOF but no current lexer set!");
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000283 CurPTHLexer->getEOF(Result);
Ted Kremenek1a531572008-11-19 22:43:49 +0000284 CurPTHLexer.reset();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000285 }
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Ted Kremenek41938c82008-11-19 21:57:25 +0000287 CurPPLexer = 0;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000288
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000289 // This is the end of the top-level file. 'WarnUnusedMacroLocs' has collected
290 // all macro locations that we need to warn because they are not used.
291 for (WarnUnusedMacroLocsTy::iterator
292 I=WarnUnusedMacroLocs.begin(), E=WarnUnusedMacroLocs.end(); I!=E; ++I)
293 Diag(*I, diag::pp_macro_not_used);
Daniel Dunbardbd82092010-03-23 05:09:10 +0000294
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000295 return true;
296}
297
298/// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer
299/// hits the end of its token stream.
300bool Preprocessor::HandleEndOfTokenLexer(Token &Result) {
Ted Kremenek1a531572008-11-19 22:43:49 +0000301 assert(CurTokenLexer && !CurPPLexer &&
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000302 "Ending a macro when currently in a #include file!");
303
Argyrios Kyrtzidis5b3284a2011-06-29 22:20:11 +0000304 if (!MacroExpandingLexersStack.empty() &&
305 MacroExpandingLexersStack.back().first == CurTokenLexer.get())
306 removeCachedMacroExpandedTokensOfLastLexer();
307
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000308 // Delete or cache the now-dead macro expander.
309 if (NumCachedTokenLexers == TokenLexerCacheSize)
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000310 CurTokenLexer.reset();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000311 else
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000312 TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000313
314 // Handle this like a #include file being popped off the stack.
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000315 return HandleEndOfFile(Result, true);
316}
317
318/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
319/// lexer stack. This should only be used in situations where the current
320/// state of the top-of-stack lexer is unknown.
321void Preprocessor::RemoveTopOfLexerStack() {
322 assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000324 if (CurTokenLexer) {
325 // Delete or cache the now-dead macro expander.
326 if (NumCachedTokenLexers == TokenLexerCacheSize)
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000327 CurTokenLexer.reset();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000328 else
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000329 TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take();
Mike Stump1eb44332009-09-09 15:08:12 +0000330 }
331
Ted Kremeneked04c4c2008-11-13 16:51:03 +0000332 PopIncludeMacroStack();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000333}
334
335/// HandleMicrosoftCommentPaste - When the macro expander pastes together a
336/// comment (/##/) in microsoft mode, this method handles updating the current
337/// state, returning the token on the next source line.
338void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) {
Ted Kremenek1a531572008-11-19 22:43:49 +0000339 assert(CurTokenLexer && !CurPPLexer &&
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000340 "Pasted comment can only be formed from macro");
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000342 // We handle this by scanning for the closest real lexer, switching it to
343 // raw mode and preprocessor mode. This will cause it to return \n as an
Peter Collingbourne84021552011-02-28 02:37:51 +0000344 // explicit EOD token.
Ted Kremenek1a531572008-11-19 22:43:49 +0000345 PreprocessorLexer *FoundLexer = 0;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000346 bool LexerWasInPPMode = false;
347 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
348 IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1);
Ted Kremenek1a531572008-11-19 22:43:49 +0000349 if (ISI.ThePPLexer == 0) continue; // Scan for a real lexer.
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000351 // Once we find a real lexer, mark it as raw mode (disabling macro
Peter Collingbourne84021552011-02-28 02:37:51 +0000352 // expansions) and preprocessor mode (return EOD). We know that the lexer
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000353 // was *not* in raw mode before, because the macro that the comment came
354 // from was expanded. However, it could have already been in preprocessor
355 // mode (#if COMMENT) in which case we have to return it to that mode and
Peter Collingbourne84021552011-02-28 02:37:51 +0000356 // return EOD.
Ted Kremenek1a531572008-11-19 22:43:49 +0000357 FoundLexer = ISI.ThePPLexer;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000358 FoundLexer->LexingRawMode = true;
359 LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective;
360 FoundLexer->ParsingPreprocessorDirective = true;
361 break;
362 }
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000364 // Okay, we either found and switched over the lexer, or we didn't find a
365 // lexer. In either case, finish off the macro the comment came from, getting
366 // the next token.
367 if (!HandleEndOfTokenLexer(Tok)) Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Peter Collingbourne84021552011-02-28 02:37:51 +0000369 // Discarding comments as long as we don't have EOF or EOD. This 'comments
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000370 // out' the rest of the line, including any tokens that came from other macros
371 // that were active, as in:
372 // #define submacro a COMMENT b
373 // submacro c
374 // which should lex to 'a' only: 'b' and 'c' should be removed.
Peter Collingbourne84021552011-02-28 02:37:51 +0000375 while (Tok.isNot(tok::eod) && Tok.isNot(tok::eof))
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000376 Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Peter Collingbourne84021552011-02-28 02:37:51 +0000378 // If we got an eod token, then we successfully found the end of the line.
379 if (Tok.is(tok::eod)) {
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000380 assert(FoundLexer && "Can't get end of line without an active lexer");
381 // Restore the lexer back to normal mode instead of raw mode.
382 FoundLexer->LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Peter Collingbourne84021552011-02-28 02:37:51 +0000384 // If the lexer was already in preprocessor mode, just return the EOD token
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000385 // to finish the preprocessor line.
386 if (LexerWasInPPMode) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000388 // Otherwise, switch out of PP mode and return the next lexed token.
389 FoundLexer->ParsingPreprocessorDirective = false;
390 return Lex(Tok);
391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000393 // If we got an EOF token, then we reached the end of the token stream but
394 // didn't find an explicit \n. This can only happen if there was no lexer
Peter Collingbourne84021552011-02-28 02:37:51 +0000395 // active (an active lexer would return EOD at EOF if there was no \n in
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000396 // preprocessor directive mode), so just return EOF as our token.
Peter Collingbourne84021552011-02-28 02:37:51 +0000397 assert(!FoundLexer && "Lexer should return EOD before EOF in PP mode");
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000398}