blob: 4a404059926305146915b18f96730c975eb60384 [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 }
Chris Lattner6e290142009-11-30 04:18:44 +000092
93 EnterSourceFileWithLexer(new Lexer(FID, InputFile, *this), CurDir);
Chris Lattnere127a0d2010-04-20 20:35:58 +000094 return;
Mike Stump1eb44332009-09-09 15:08:12 +000095}
Chris Lattner72181832008-09-26 20:12:23 +000096
Ted Kremenek6137dc92008-12-02 19:46:31 +000097/// EnterSourceFileWithLexer - Add a source file to the top of the include stack
98/// and start lexing tokens from it instead of the current buffer.
Mike Stump1eb44332009-09-09 15:08:12 +000099void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000100 const DirectoryLookup *CurDir) {
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000102 // Add the current lexer to the include stack.
Ted Kremenek41938c82008-11-19 21:57:25 +0000103 if (CurPPLexer || CurTokenLexer)
Ted Kremeneked04c4c2008-11-13 16:51:03 +0000104 PushIncludeMacroStack();
105
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000106 CurLexer.reset(TheLexer);
Ted Kremenek9c1b7502008-11-18 00:12:49 +0000107 CurPPLexer = TheLexer;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000108 CurDirLookup = CurDir;
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000110 // Notify the client, if desired, that we are in a new source file.
111 if (Callbacks && !CurLexer->Is_PragmaLexer) {
Chris Lattner9d728512008-10-27 01:19:25 +0000112 SrcMgr::CharacteristicKind FileType =
Chris Lattner0b9e7362008-09-26 21:18:42 +0000113 SourceMgr.getFileCharacteristic(CurLexer->getFileLoc());
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000115 Callbacks->FileChanged(CurLexer->getFileLoc(),
116 PPCallbacks::EnterFile, FileType);
117 }
118}
119
Ted Kremenek6137dc92008-12-02 19:46:31 +0000120/// EnterSourceFileWithPTH - Add a source file to the top of the include stack
121/// and start getting tokens from it using the PTH cache.
Mike Stump1eb44332009-09-09 15:08:12 +0000122void Preprocessor::EnterSourceFileWithPTH(PTHLexer *PL,
Ted Kremenek6137dc92008-12-02 19:46:31 +0000123 const DirectoryLookup *CurDir) {
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Ted Kremenek6137dc92008-12-02 19:46:31 +0000125 if (CurPPLexer || CurTokenLexer)
126 PushIncludeMacroStack();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000127
Ted Kremenek6137dc92008-12-02 19:46:31 +0000128 CurDirLookup = CurDir;
129 CurPTHLexer.reset(PL);
130 CurPPLexer = CurPTHLexer.get();
131
132 // Notify the client, if desired, that we are in a new source file.
133 if (Callbacks) {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000134 FileID FID = CurPPLexer->getFileID();
Chris Lattner8c61b532009-01-19 08:01:53 +0000135 SourceLocation EnterLoc = SourceMgr.getLocForStartOfFile(FID);
136 SrcMgr::CharacteristicKind FileType =
137 SourceMgr.getFileCharacteristic(EnterLoc);
138 Callbacks->FileChanged(EnterLoc, PPCallbacks::EnterFile, FileType);
Ted Kremenek6137dc92008-12-02 19:46:31 +0000139 }
140}
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000141
142/// EnterMacro - Add a Macro to the top of the include stack and start lexing
143/// tokens from it instead of the current buffer.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000144void Preprocessor::EnterMacro(Token &Tok, SourceLocation ILEnd,
145 MacroArgs *Args) {
Ted Kremeneked04c4c2008-11-13 16:51:03 +0000146 PushIncludeMacroStack();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000147 CurDirLookup = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000149 if (NumCachedTokenLexers == 0) {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000150 CurTokenLexer.reset(new TokenLexer(Tok, ILEnd, Args, *this));
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000151 } else {
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000152 CurTokenLexer.reset(TokenLexerCache[--NumCachedTokenLexers]);
Chris Lattnere7fb4842009-02-15 20:52:18 +0000153 CurTokenLexer->Init(Tok, ILEnd, Args);
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000154 }
155}
156
157/// EnterTokenStream - Add a "macro" context to the top of the include stack,
Chris Lattner6b884502008-03-10 06:06:04 +0000158/// which will cause the lexer to start returning the specified tokens.
159///
160/// If DisableMacroExpansion is true, tokens lexed from the token stream will
161/// not be subject to further macro expansion. Otherwise, these tokens will
162/// be re-macro-expanded when/if expansion is enabled.
163///
164/// If OwnsTokens is false, this method assumes that the specified stream of
165/// tokens has a permanent owner somewhere, so they do not need to be copied.
166/// If it is true, it assumes the array of tokens is allocated with new[] and
167/// must be freed.
168///
169void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks,
170 bool DisableMacroExpansion,
171 bool OwnsTokens) {
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000172 // Save our current state.
Ted Kremeneked04c4c2008-11-13 16:51:03 +0000173 PushIncludeMacroStack();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000174 CurDirLookup = 0;
175
176 // Create a macro expander to expand from the specified token stream.
177 if (NumCachedTokenLexers == 0) {
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000178 CurTokenLexer.reset(new TokenLexer(Toks, NumToks, DisableMacroExpansion,
179 OwnsTokens, *this));
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000180 } else {
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000181 CurTokenLexer.reset(TokenLexerCache[--NumCachedTokenLexers]);
Chris Lattner6b884502008-03-10 06:06:04 +0000182 CurTokenLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens);
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000183 }
184}
185
186/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
187/// the current file. This either returns the EOF token or pops a level off
188/// the include stack and keeps going.
189bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
190 assert(!CurTokenLexer &&
191 "Ending a file when currently in a macro!");
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000193 // See if this file had a controlling macro.
Ted Kremenek1a531572008-11-19 22:43:49 +0000194 if (CurPPLexer) { // Not ending a macro, ignore it.
Mike Stump1eb44332009-09-09 15:08:12 +0000195 if (const IdentifierInfo *ControllingMacro =
Ted Kremenek1a531572008-11-19 22:43:49 +0000196 CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
Steve Naroff83d63c72009-04-24 20:03:17 +0000197 // Okay, this has a controlling macro, remember in HeaderFileInfo.
Mike Stump1eb44332009-09-09 15:08:12 +0000198 if (const FileEntry *FE =
Ted Kremenek1a531572008-11-19 22:43:49 +0000199 SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000200 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
201 }
202 }
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000204 // If this is a #include'd file, pop it off the include stack and continue
205 // lexing the #includer file.
206 if (!IncludeMacroStack.empty()) {
207 // We're done with the #included file.
208 RemoveTopOfLexerStack();
209
210 // Notify the client, if desired, that we are in a new source file.
Ted Kremenek1a531572008-11-19 22:43:49 +0000211 if (Callbacks && !isEndOfMacro && CurPPLexer) {
Chris Lattner9d728512008-10-27 01:19:25 +0000212 SrcMgr::CharacteristicKind FileType =
Chris Lattner8c61b532009-01-19 08:01:53 +0000213 SourceMgr.getFileCharacteristic(CurPPLexer->getSourceLocation());
214 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000215 PPCallbacks::ExitFile, FileType);
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000216 }
217
218 // Client should lex another token.
219 return false;
220 }
221
222 // If the file ends with a newline, form the EOF token on the newline itself,
223 // rather than "on the line following it", which doesn't exist. This makes
224 // diagnostics relating to the end of file include the last file that the user
225 // actually typed, which is goodness.
Ted Kremenek1a531572008-11-19 22:43:49 +0000226 if (CurLexer) {
227 const char *EndPos = CurLexer->BufferEnd;
Mike Stump1eb44332009-09-09 15:08:12 +0000228 if (EndPos != CurLexer->BufferStart &&
Ted Kremenek1a531572008-11-19 22:43:49 +0000229 (EndPos[-1] == '\n' || EndPos[-1] == '\r')) {
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000230 --EndPos;
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Ted Kremenek1a531572008-11-19 22:43:49 +0000232 // Handle \n\r and \r\n:
Mike Stump1eb44332009-09-09 15:08:12 +0000233 if (EndPos != CurLexer->BufferStart &&
Ted Kremenek1a531572008-11-19 22:43:49 +0000234 (EndPos[-1] == '\n' || EndPos[-1] == '\r') &&
235 EndPos[-1] != EndPos[0])
236 --EndPos;
237 }
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Ted Kremenek1a531572008-11-19 22:43:49 +0000239 Result.startToken();
240 CurLexer->BufferPtr = EndPos;
241 CurLexer->FormTokenWithChars(Result, EndPos, tok::eof);
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Ted Kremenek1a531572008-11-19 22:43:49 +0000243 // We're done with the #included file.
244 CurLexer.reset();
Chris Lattner67116082009-02-13 23:06:48 +0000245 } else {
246 assert(CurPTHLexer && "Got EOF but no current lexer set!");
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000247 CurPTHLexer->getEOF(Result);
Ted Kremenek1a531572008-11-19 22:43:49 +0000248 CurPTHLexer.reset();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000249 }
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Ted Kremenek41938c82008-11-19 21:57:25 +0000251 CurPPLexer = 0;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000252
253 // This is the end of the top-level file. If the diag::pp_macro_not_used
254 // diagnostic is enabled, look for macros that have not been used.
Mike Stump1eb44332009-09-09 15:08:12 +0000255 if (getDiagnostics().getDiagnosticLevel(diag::pp_macro_not_used) !=
Chris Lattner7f549df2009-03-13 21:17:23 +0000256 Diagnostic::Ignored) {
Douglas Gregor88a35862010-01-04 19:18:44 +0000257 for (macro_iterator I = macro_begin(false), E = macro_end(false);
258 I != E; ++I)
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000259 if (!I->second->isUsed())
260 Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000261 }
Daniel Dunbardbd82092010-03-23 05:09:10 +0000262
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000263 return true;
264}
265
266/// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer
267/// hits the end of its token stream.
268bool Preprocessor::HandleEndOfTokenLexer(Token &Result) {
Ted Kremenek1a531572008-11-19 22:43:49 +0000269 assert(CurTokenLexer && !CurPPLexer &&
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000270 "Ending a macro when currently in a #include file!");
271
272 // Delete or cache the now-dead macro expander.
273 if (NumCachedTokenLexers == TokenLexerCacheSize)
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000274 CurTokenLexer.reset();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000275 else
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000276 TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000277
278 // Handle this like a #include file being popped off the stack.
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000279 return HandleEndOfFile(Result, true);
280}
281
282/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
283/// lexer stack. This should only be used in situations where the current
284/// state of the top-of-stack lexer is unknown.
285void Preprocessor::RemoveTopOfLexerStack() {
286 assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000288 if (CurTokenLexer) {
289 // Delete or cache the now-dead macro expander.
290 if (NumCachedTokenLexers == TokenLexerCacheSize)
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000291 CurTokenLexer.reset();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000292 else
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000293 TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take();
Mike Stump1eb44332009-09-09 15:08:12 +0000294 }
295
Ted Kremeneked04c4c2008-11-13 16:51:03 +0000296 PopIncludeMacroStack();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000297}
298
299/// HandleMicrosoftCommentPaste - When the macro expander pastes together a
300/// comment (/##/) in microsoft mode, this method handles updating the current
301/// state, returning the token on the next source line.
302void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) {
Ted Kremenek1a531572008-11-19 22:43:49 +0000303 assert(CurTokenLexer && !CurPPLexer &&
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000304 "Pasted comment can only be formed from macro");
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000306 // We handle this by scanning for the closest real lexer, switching it to
307 // raw mode and preprocessor mode. This will cause it to return \n as an
308 // explicit EOM token.
Ted Kremenek1a531572008-11-19 22:43:49 +0000309 PreprocessorLexer *FoundLexer = 0;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000310 bool LexerWasInPPMode = false;
311 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
312 IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1);
Ted Kremenek1a531572008-11-19 22:43:49 +0000313 if (ISI.ThePPLexer == 0) continue; // Scan for a real lexer.
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000315 // Once we find a real lexer, mark it as raw mode (disabling macro
316 // expansions) and preprocessor mode (return EOM). We know that the lexer
317 // was *not* in raw mode before, because the macro that the comment came
318 // from was expanded. However, it could have already been in preprocessor
319 // mode (#if COMMENT) in which case we have to return it to that mode and
320 // return EOM.
Ted Kremenek1a531572008-11-19 22:43:49 +0000321 FoundLexer = ISI.ThePPLexer;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000322 FoundLexer->LexingRawMode = true;
323 LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective;
324 FoundLexer->ParsingPreprocessorDirective = true;
325 break;
326 }
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000328 // Okay, we either found and switched over the lexer, or we didn't find a
329 // lexer. In either case, finish off the macro the comment came from, getting
330 // the next token.
331 if (!HandleEndOfTokenLexer(Tok)) Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000333 // Discarding comments as long as we don't have EOF or EOM. This 'comments
334 // out' the rest of the line, including any tokens that came from other macros
335 // that were active, as in:
336 // #define submacro a COMMENT b
337 // submacro c
338 // which should lex to 'a' only: 'b' and 'c' should be removed.
339 while (Tok.isNot(tok::eom) && Tok.isNot(tok::eof))
340 Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000342 // If we got an eom token, then we successfully found the end of the line.
343 if (Tok.is(tok::eom)) {
344 assert(FoundLexer && "Can't get end of line without an active lexer");
345 // Restore the lexer back to normal mode instead of raw mode.
346 FoundLexer->LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000348 // If the lexer was already in preprocessor mode, just return the EOM token
349 // to finish the preprocessor line.
350 if (LexerWasInPPMode) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000352 // Otherwise, switch out of PP mode and return the next lexed token.
353 FoundLexer->ParsingPreprocessorDirective = false;
354 return Lex(Tok);
355 }
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000357 // If we got an EOF token, then we reached the end of the token stream but
358 // didn't find an explicit \n. This can only happen if there was no lexer
359 // active (an active lexer would return EOM at EOF if there was no \n in
360 // preprocessor directive mode), so just return EOF as our token.
361 assert(!FoundLexer && "Lexer should return EOM before EOF in PP mode");
362}