blob: d5d6a6e972a49de4a827431424f839197bd8e780 [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 Lattner2b2453a2009-01-17 06:22:33 +000067void Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir) {
Chris Lattner8c32b1a2008-03-09 04:10:46 +000068 assert(CurTokenLexer == 0 && "Cannot #include a file inside a macro!");
69 ++NumEnteredSourceFiles;
Mike Stump1eb44332009-09-09 15:08:12 +000070
Chris Lattner8c32b1a2008-03-09 04:10:46 +000071 if (MaxIncludeStackDepth < IncludeMacroStack.size())
72 MaxIncludeStackDepth = IncludeMacroStack.size();
73
Ted Kremenek6137dc92008-12-02 19:46:31 +000074 if (PTH) {
Chris Lattnerf056d922009-01-17 08:06:50 +000075 if (PTHLexer *PL = PTH->CreateLexer(FID))
Chris Lattner88d3ac12009-01-17 08:03:42 +000076 return EnterSourceFileWithPTH(PL, CurDir);
Ted Kremenek6137dc92008-12-02 19:46:31 +000077 }
Chris Lattner88d3ac12009-01-17 08:03:42 +000078 EnterSourceFileWithLexer(new Lexer(FID, *this), CurDir);
Mike Stump1eb44332009-09-09 15:08:12 +000079}
Chris Lattner72181832008-09-26 20:12:23 +000080
Ted Kremenek6137dc92008-12-02 19:46:31 +000081/// EnterSourceFileWithLexer - Add a source file to the top of the include stack
82/// and start lexing tokens from it instead of the current buffer.
Mike Stump1eb44332009-09-09 15:08:12 +000083void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
Chris Lattner8c32b1a2008-03-09 04:10:46 +000084 const DirectoryLookup *CurDir) {
Mike Stump1eb44332009-09-09 15:08:12 +000085
Chris Lattner8c32b1a2008-03-09 04:10:46 +000086 // Add the current lexer to the include stack.
Ted Kremenek41938c82008-11-19 21:57:25 +000087 if (CurPPLexer || CurTokenLexer)
Ted Kremeneked04c4c2008-11-13 16:51:03 +000088 PushIncludeMacroStack();
89
Ted Kremenekcaaa7df2008-11-13 17:11:24 +000090 CurLexer.reset(TheLexer);
Ted Kremenek9c1b7502008-11-18 00:12:49 +000091 CurPPLexer = TheLexer;
Chris Lattner8c32b1a2008-03-09 04:10:46 +000092 CurDirLookup = CurDir;
Mike Stump1eb44332009-09-09 15:08:12 +000093
Chris Lattner8c32b1a2008-03-09 04:10:46 +000094 // Notify the client, if desired, that we are in a new source file.
95 if (Callbacks && !CurLexer->Is_PragmaLexer) {
Chris Lattner9d728512008-10-27 01:19:25 +000096 SrcMgr::CharacteristicKind FileType =
Chris Lattner0b9e7362008-09-26 21:18:42 +000097 SourceMgr.getFileCharacteristic(CurLexer->getFileLoc());
Mike Stump1eb44332009-09-09 15:08:12 +000098
Chris Lattner8c32b1a2008-03-09 04:10:46 +000099 Callbacks->FileChanged(CurLexer->getFileLoc(),
100 PPCallbacks::EnterFile, FileType);
101 }
102}
103
Ted Kremenek6137dc92008-12-02 19:46:31 +0000104/// EnterSourceFileWithPTH - Add a source file to the top of the include stack
105/// and start getting tokens from it using the PTH cache.
Mike Stump1eb44332009-09-09 15:08:12 +0000106void Preprocessor::EnterSourceFileWithPTH(PTHLexer *PL,
Ted Kremenek6137dc92008-12-02 19:46:31 +0000107 const DirectoryLookup *CurDir) {
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Ted Kremenek6137dc92008-12-02 19:46:31 +0000109 if (CurPPLexer || CurTokenLexer)
110 PushIncludeMacroStack();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000111
Ted Kremenek6137dc92008-12-02 19:46:31 +0000112 CurDirLookup = CurDir;
113 CurPTHLexer.reset(PL);
114 CurPPLexer = CurPTHLexer.get();
115
116 // Notify the client, if desired, that we are in a new source file.
117 if (Callbacks) {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000118 FileID FID = CurPPLexer->getFileID();
Chris Lattner8c61b532009-01-19 08:01:53 +0000119 SourceLocation EnterLoc = SourceMgr.getLocForStartOfFile(FID);
120 SrcMgr::CharacteristicKind FileType =
121 SourceMgr.getFileCharacteristic(EnterLoc);
122 Callbacks->FileChanged(EnterLoc, PPCallbacks::EnterFile, FileType);
Ted Kremenek6137dc92008-12-02 19:46:31 +0000123 }
124}
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000125
126/// EnterMacro - Add a Macro to the top of the include stack and start lexing
127/// tokens from it instead of the current buffer.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000128void Preprocessor::EnterMacro(Token &Tok, SourceLocation ILEnd,
129 MacroArgs *Args) {
Ted Kremeneked04c4c2008-11-13 16:51:03 +0000130 PushIncludeMacroStack();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000131 CurDirLookup = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000133 if (NumCachedTokenLexers == 0) {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000134 CurTokenLexer.reset(new TokenLexer(Tok, ILEnd, Args, *this));
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000135 } else {
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000136 CurTokenLexer.reset(TokenLexerCache[--NumCachedTokenLexers]);
Chris Lattnere7fb4842009-02-15 20:52:18 +0000137 CurTokenLexer->Init(Tok, ILEnd, Args);
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000138 }
139}
140
141/// EnterTokenStream - Add a "macro" context to the top of the include stack,
Chris Lattner6b884502008-03-10 06:06:04 +0000142/// which will cause the lexer to start returning the specified tokens.
143///
144/// If DisableMacroExpansion is true, tokens lexed from the token stream will
145/// not be subject to further macro expansion. Otherwise, these tokens will
146/// be re-macro-expanded when/if expansion is enabled.
147///
148/// If OwnsTokens is false, this method assumes that the specified stream of
149/// tokens has a permanent owner somewhere, so they do not need to be copied.
150/// If it is true, it assumes the array of tokens is allocated with new[] and
151/// must be freed.
152///
153void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks,
154 bool DisableMacroExpansion,
155 bool OwnsTokens) {
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000156 // Save our current state.
Ted Kremeneked04c4c2008-11-13 16:51:03 +0000157 PushIncludeMacroStack();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000158 CurDirLookup = 0;
159
160 // Create a macro expander to expand from the specified token stream.
161 if (NumCachedTokenLexers == 0) {
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000162 CurTokenLexer.reset(new TokenLexer(Toks, NumToks, DisableMacroExpansion,
163 OwnsTokens, *this));
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000164 } else {
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000165 CurTokenLexer.reset(TokenLexerCache[--NumCachedTokenLexers]);
Chris Lattner6b884502008-03-10 06:06:04 +0000166 CurTokenLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens);
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000167 }
168}
169
170/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
171/// the current file. This either returns the EOF token or pops a level off
172/// the include stack and keeps going.
173bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
174 assert(!CurTokenLexer &&
175 "Ending a file when currently in a macro!");
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000177 // See if this file had a controlling macro.
Ted Kremenek1a531572008-11-19 22:43:49 +0000178 if (CurPPLexer) { // Not ending a macro, ignore it.
Mike Stump1eb44332009-09-09 15:08:12 +0000179 if (const IdentifierInfo *ControllingMacro =
Ted Kremenek1a531572008-11-19 22:43:49 +0000180 CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
Steve Naroff83d63c72009-04-24 20:03:17 +0000181 // Okay, this has a controlling macro, remember in HeaderFileInfo.
Mike Stump1eb44332009-09-09 15:08:12 +0000182 if (const FileEntry *FE =
Ted Kremenek1a531572008-11-19 22:43:49 +0000183 SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000184 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
185 }
186 }
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000188 // If this is a #include'd file, pop it off the include stack and continue
189 // lexing the #includer file.
190 if (!IncludeMacroStack.empty()) {
191 // We're done with the #included file.
192 RemoveTopOfLexerStack();
193
194 // Notify the client, if desired, that we are in a new source file.
Ted Kremenek1a531572008-11-19 22:43:49 +0000195 if (Callbacks && !isEndOfMacro && CurPPLexer) {
Chris Lattner9d728512008-10-27 01:19:25 +0000196 SrcMgr::CharacteristicKind FileType =
Chris Lattner8c61b532009-01-19 08:01:53 +0000197 SourceMgr.getFileCharacteristic(CurPPLexer->getSourceLocation());
198 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000199 PPCallbacks::ExitFile, FileType);
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000200 }
201
202 // Client should lex another token.
203 return false;
204 }
205
206 // If the file ends with a newline, form the EOF token on the newline itself,
207 // rather than "on the line following it", which doesn't exist. This makes
208 // diagnostics relating to the end of file include the last file that the user
209 // actually typed, which is goodness.
Ted Kremenek1a531572008-11-19 22:43:49 +0000210 if (CurLexer) {
211 const char *EndPos = CurLexer->BufferEnd;
Mike Stump1eb44332009-09-09 15:08:12 +0000212 if (EndPos != CurLexer->BufferStart &&
Ted Kremenek1a531572008-11-19 22:43:49 +0000213 (EndPos[-1] == '\n' || EndPos[-1] == '\r')) {
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000214 --EndPos;
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Ted Kremenek1a531572008-11-19 22:43:49 +0000216 // Handle \n\r and \r\n:
Mike Stump1eb44332009-09-09 15:08:12 +0000217 if (EndPos != CurLexer->BufferStart &&
Ted Kremenek1a531572008-11-19 22:43:49 +0000218 (EndPos[-1] == '\n' || EndPos[-1] == '\r') &&
219 EndPos[-1] != EndPos[0])
220 --EndPos;
221 }
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Ted Kremenek1a531572008-11-19 22:43:49 +0000223 Result.startToken();
224 CurLexer->BufferPtr = EndPos;
225 CurLexer->FormTokenWithChars(Result, EndPos, tok::eof);
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Ted Kremenek1a531572008-11-19 22:43:49 +0000227 // We're done with the #included file.
228 CurLexer.reset();
Chris Lattner67116082009-02-13 23:06:48 +0000229 } else {
230 assert(CurPTHLexer && "Got EOF but no current lexer set!");
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000231 CurPTHLexer->getEOF(Result);
Ted Kremenek1a531572008-11-19 22:43:49 +0000232 CurPTHLexer.reset();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000233 }
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Ted Kremenek41938c82008-11-19 21:57:25 +0000235 CurPPLexer = 0;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000236
237 // This is the end of the top-level file. If the diag::pp_macro_not_used
238 // diagnostic is enabled, look for macros that have not been used.
Mike Stump1eb44332009-09-09 15:08:12 +0000239 if (getDiagnostics().getDiagnosticLevel(diag::pp_macro_not_used) !=
Chris Lattner7f549df2009-03-13 21:17:23 +0000240 Diagnostic::Ignored) {
Chris Lattner09b5c1d2009-02-06 06:26:42 +0000241 for (macro_iterator I = macro_begin(), E = macro_end(); I != E; ++I)
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000242 if (!I->second->isUsed())
243 Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000244 }
245 return true;
246}
247
248/// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer
249/// hits the end of its token stream.
250bool Preprocessor::HandleEndOfTokenLexer(Token &Result) {
Ted Kremenek1a531572008-11-19 22:43:49 +0000251 assert(CurTokenLexer && !CurPPLexer &&
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000252 "Ending a macro when currently in a #include file!");
253
254 // Delete or cache the now-dead macro expander.
255 if (NumCachedTokenLexers == TokenLexerCacheSize)
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000256 CurTokenLexer.reset();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000257 else
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000258 TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000259
260 // Handle this like a #include file being popped off the stack.
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000261 return HandleEndOfFile(Result, true);
262}
263
264/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
265/// lexer stack. This should only be used in situations where the current
266/// state of the top-of-stack lexer is unknown.
267void Preprocessor::RemoveTopOfLexerStack() {
268 assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000270 if (CurTokenLexer) {
271 // Delete or cache the now-dead macro expander.
272 if (NumCachedTokenLexers == TokenLexerCacheSize)
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000273 CurTokenLexer.reset();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000274 else
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000275 TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take();
Mike Stump1eb44332009-09-09 15:08:12 +0000276 }
277
Ted Kremeneked04c4c2008-11-13 16:51:03 +0000278 PopIncludeMacroStack();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000279}
280
281/// HandleMicrosoftCommentPaste - When the macro expander pastes together a
282/// comment (/##/) in microsoft mode, this method handles updating the current
283/// state, returning the token on the next source line.
284void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) {
Ted Kremenek1a531572008-11-19 22:43:49 +0000285 assert(CurTokenLexer && !CurPPLexer &&
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000286 "Pasted comment can only be formed from macro");
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000288 // We handle this by scanning for the closest real lexer, switching it to
289 // raw mode and preprocessor mode. This will cause it to return \n as an
290 // explicit EOM token.
Ted Kremenek1a531572008-11-19 22:43:49 +0000291 PreprocessorLexer *FoundLexer = 0;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000292 bool LexerWasInPPMode = false;
293 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
294 IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1);
Ted Kremenek1a531572008-11-19 22:43:49 +0000295 if (ISI.ThePPLexer == 0) continue; // Scan for a real lexer.
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000297 // Once we find a real lexer, mark it as raw mode (disabling macro
298 // expansions) and preprocessor mode (return EOM). We know that the lexer
299 // was *not* in raw mode before, because the macro that the comment came
300 // from was expanded. However, it could have already been in preprocessor
301 // mode (#if COMMENT) in which case we have to return it to that mode and
302 // return EOM.
Ted Kremenek1a531572008-11-19 22:43:49 +0000303 FoundLexer = ISI.ThePPLexer;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000304 FoundLexer->LexingRawMode = true;
305 LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective;
306 FoundLexer->ParsingPreprocessorDirective = true;
307 break;
308 }
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000310 // Okay, we either found and switched over the lexer, or we didn't find a
311 // lexer. In either case, finish off the macro the comment came from, getting
312 // the next token.
313 if (!HandleEndOfTokenLexer(Tok)) Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000315 // Discarding comments as long as we don't have EOF or EOM. This 'comments
316 // out' the rest of the line, including any tokens that came from other macros
317 // that were active, as in:
318 // #define submacro a COMMENT b
319 // submacro c
320 // which should lex to 'a' only: 'b' and 'c' should be removed.
321 while (Tok.isNot(tok::eom) && Tok.isNot(tok::eof))
322 Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000324 // If we got an eom token, then we successfully found the end of the line.
325 if (Tok.is(tok::eom)) {
326 assert(FoundLexer && "Can't get end of line without an active lexer");
327 // Restore the lexer back to normal mode instead of raw mode.
328 FoundLexer->LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000330 // If the lexer was already in preprocessor mode, just return the EOM token
331 // to finish the preprocessor line.
332 if (LexerWasInPPMode) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000334 // Otherwise, switch out of PP mode and return the next lexed token.
335 FoundLexer->ParsingPreprocessorDirective = false;
336 return Lex(Tok);
337 }
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000339 // If we got an EOF token, then we reached the end of the token stream but
340 // didn't find an explicit \n. This can only happen if there was no lexer
341 // active (an active lexer would return EOM at EOF if there was no \n in
342 // preprocessor directive mode), so just return EOF as our token.
343 assert(!FoundLexer && "Lexer should return EOM before EOF in PP mode");
344}