blob: f7b1d60facbdbeb5cdaecd8bfbaf409ef9e28db6 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Preprocessor interface.
11//
12//===----------------------------------------------------------------------===//
13//
14// Options to support:
15// -H - Print the name of each header file used.
16// -d[MDNI] - Dump various things.
17// -fworking-directory - #line's with preprocessor's working dir.
18// -fpreprocessed
19// -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD
20// -W*
21// -w
22//
23// Messages to emit:
24// "Multiple include guards may be useful for:\n"
25//
26//===----------------------------------------------------------------------===//
27
28#include "clang/Lex/Preprocessor.h"
29#include "clang/Lex/HeaderSearch.h"
30#include "clang/Lex/MacroInfo.h"
31#include "clang/Lex/PPCallbacks.h"
32#include "clang/Lex/Pragma.h"
33#include "clang/Lex/ScratchBuffer.h"
34#include "clang/Basic/Diagnostic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000035#include "clang/Basic/SourceManager.h"
36#include "clang/Basic/TargetInfo.h"
37#include "llvm/ADT/SmallVector.h"
38#include "llvm/Support/MemoryBuffer.h"
Ted Kremenekce4c64e2008-01-14 16:44:48 +000039#include "llvm/Support/Streams.h"
Chris Lattner1b023182007-09-03 18:30:32 +000040#include <ctime>
Chris Lattner4b009652007-07-25 00:24:17 +000041using namespace clang;
42
43//===----------------------------------------------------------------------===//
44
45Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
46 TargetInfo &target, SourceManager &SM,
47 HeaderSearch &Headers)
48 : Diags(diags), Features(opts), Target(target), FileMgr(Headers.getFileMgr()),
49 SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts),
Chris Lattner5b54ed92008-03-09 02:26:03 +000050 CurLexer(0), CurDirLookup(0), CurTokenLexer(0), Callbacks(0) {
Chris Lattner4b009652007-07-25 00:24:17 +000051 ScratchBuf = new ScratchBuffer(SourceMgr);
52
53 // Clear stats.
54 NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
55 NumIf = NumElse = NumEndif = 0;
56 NumEnteredSourceFiles = 0;
57 NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
58 NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
59 MaxIncludeStackDepth = 0;
60 NumSkipped = 0;
61
62 // Default to discarding comments.
63 KeepComments = false;
64 KeepMacroComments = false;
65
66 // Macro expansion is enabled.
67 DisableMacroExpansion = false;
68 InMacroArgs = false;
Chris Lattner5b54ed92008-03-09 02:26:03 +000069 NumCachedTokenLexers = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000070
71 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
72 // This gets unpoisoned where it is allowed.
73 (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
74
Chris Lattnerd1f21e12007-10-09 22:10:18 +000075 Predefines = 0;
76
Chris Lattner4b009652007-07-25 00:24:17 +000077 // Initialize the pragma handlers.
78 PragmaHandlers = new PragmaNamespace(0);
79 RegisterBuiltinPragmas();
80
81 // Initialize builtin macros like __LINE__ and friends.
82 RegisterBuiltinMacros();
83}
84
85Preprocessor::~Preprocessor() {
86 // Free any active lexers.
87 delete CurLexer;
88
89 while (!IncludeMacroStack.empty()) {
90 delete IncludeMacroStack.back().TheLexer;
Chris Lattner5b54ed92008-03-09 02:26:03 +000091 delete IncludeMacroStack.back().TheTokenLexer;
Chris Lattner4b009652007-07-25 00:24:17 +000092 IncludeMacroStack.pop_back();
93 }
Chris Lattner7a1b0882007-10-07 08:44:20 +000094
95 // Free any macro definitions.
96 for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
97 Macros.begin(), E = Macros.end(); I != E; ++I) {
98 // Free the macro definition.
99 delete I->second;
100 I->second = 0;
101 I->first->setHasMacroDefinition(false);
102 }
Chris Lattner4b009652007-07-25 00:24:17 +0000103
104 // Free any cached macro expanders.
Chris Lattner5b54ed92008-03-09 02:26:03 +0000105 for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i)
106 delete TokenLexerCache[i];
Chris Lattner4b009652007-07-25 00:24:17 +0000107
108 // Release pragma information.
109 delete PragmaHandlers;
110
111 // Delete the scratch buffer info.
112 delete ScratchBuf;
113}
114
115PPCallbacks::~PPCallbacks() {
116}
117
118/// Diag - Forwarding function for diagnostics. This emits a diagnostic at
119/// the specified Token's location, translating the token's start
120/// position in the current buffer into a SourcePosition object for rendering.
121void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000122 Diags.Report(getFullLoc(Loc), DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000123}
124
125void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
126 const std::string &Msg) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000127 Diags.Report(getFullLoc(Loc), DiagID, &Msg, 1);
Chris Lattner4b009652007-07-25 00:24:17 +0000128}
129
130void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000131 llvm::cerr << tok::getTokenName(Tok.getKind()) << " '"
132 << getSpelling(Tok) << "'";
Chris Lattner4b009652007-07-25 00:24:17 +0000133
134 if (!DumpFlags) return;
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000135
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000136 llvm::cerr << "\t";
Chris Lattner4b009652007-07-25 00:24:17 +0000137 if (Tok.isAtStartOfLine())
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000138 llvm::cerr << " [StartOfLine]";
Chris Lattner4b009652007-07-25 00:24:17 +0000139 if (Tok.hasLeadingSpace())
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000140 llvm::cerr << " [LeadingSpace]";
Chris Lattner4b009652007-07-25 00:24:17 +0000141 if (Tok.isExpandDisabled())
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000142 llvm::cerr << " [ExpandDisabled]";
Chris Lattner4b009652007-07-25 00:24:17 +0000143 if (Tok.needsCleaning()) {
144 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000145 llvm::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
146 << "']";
Chris Lattner4b009652007-07-25 00:24:17 +0000147 }
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000148
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000149 llvm::cerr << "\tLoc=<";
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000150 DumpLocation(Tok.getLocation());
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000151 llvm::cerr << ">";
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000152}
153
154void Preprocessor::DumpLocation(SourceLocation Loc) const {
155 SourceLocation LogLoc = SourceMgr.getLogicalLoc(Loc);
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000156 llvm::cerr << SourceMgr.getSourceName(LogLoc) << ':'
157 << SourceMgr.getLineNumber(LogLoc) << ':'
158 << SourceMgr.getLineNumber(LogLoc);
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000159
160 SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(Loc);
161 if (PhysLoc != LogLoc) {
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000162 llvm::cerr << " <PhysLoc=";
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000163 DumpLocation(PhysLoc);
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000164 llvm::cerr << ">";
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000165 }
Chris Lattner4b009652007-07-25 00:24:17 +0000166}
167
168void Preprocessor::DumpMacro(const MacroInfo &MI) const {
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000169 llvm::cerr << "MACRO: ";
Chris Lattner4b009652007-07-25 00:24:17 +0000170 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
171 DumpToken(MI.getReplacementToken(i));
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000172 llvm::cerr << " ";
Chris Lattner4b009652007-07-25 00:24:17 +0000173 }
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000174 llvm::cerr << "\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000175}
176
177void Preprocessor::PrintStats() {
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000178 llvm::cerr << "\n*** Preprocessor Stats:\n";
179 llvm::cerr << NumDirectives << " directives found:\n";
180 llvm::cerr << " " << NumDefined << " #define.\n";
181 llvm::cerr << " " << NumUndefined << " #undef.\n";
182 llvm::cerr << " #include/#include_next/#import:\n";
183 llvm::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
184 llvm::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
185 llvm::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
186 llvm::cerr << " " << NumElse << " #else/#elif.\n";
187 llvm::cerr << " " << NumEndif << " #endif.\n";
188 llvm::cerr << " " << NumPragma << " #pragma.\n";
189 llvm::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000190
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000191 llvm::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
192 << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
193 << NumFastMacroExpanded << " on the fast path.\n";
194 llvm::cerr << (NumFastTokenPaste+NumTokenPaste)
195 << " token paste (##) operations performed, "
196 << NumFastTokenPaste << " on the fast path.\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000197}
198
199//===----------------------------------------------------------------------===//
200// Token Spelling
201//===----------------------------------------------------------------------===//
202
203
204/// getSpelling() - Return the 'spelling' of this token. The spelling of a
205/// token are the characters used to represent the token in the source file
206/// after trigraph expansion and escaped-newline folding. In particular, this
207/// wants to get the true, uncanonicalized, spelling of things like digraphs
208/// UCNs, etc.
209std::string Preprocessor::getSpelling(const Token &Tok) const {
210 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
211
212 // If this token contains nothing interesting, return it directly.
213 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
214 if (!Tok.needsCleaning())
215 return std::string(TokStart, TokStart+Tok.getLength());
216
217 std::string Result;
218 Result.reserve(Tok.getLength());
219
220 // Otherwise, hard case, relex the characters into the string.
221 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
222 Ptr != End; ) {
223 unsigned CharSize;
224 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
225 Ptr += CharSize;
226 }
227 assert(Result.size() != unsigned(Tok.getLength()) &&
228 "NeedsCleaning flag set on something that didn't need cleaning!");
229 return Result;
230}
231
232/// getSpelling - This method is used to get the spelling of a token into a
233/// preallocated buffer, instead of as an std::string. The caller is required
234/// to allocate enough space for the token, which is guaranteed to be at least
235/// Tok.getLength() bytes long. The actual length of the token is returned.
236///
237/// Note that this method may do two possible things: it may either fill in
238/// the buffer specified with characters, or it may *change the input pointer*
239/// to point to a constant buffer with the data already in it (avoiding a
240/// copy). The caller is not allowed to modify the returned buffer pointer
241/// if an internal buffer is returned.
242unsigned Preprocessor::getSpelling(const Token &Tok,
243 const char *&Buffer) const {
244 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
245
246 // If this token is an identifier, just return the string from the identifier
247 // table, which is very quick.
248 if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
249 Buffer = II->getName();
250
251 // Return the length of the token. If the token needed cleaning, don't
252 // include the size of the newlines or trigraphs in it.
253 if (!Tok.needsCleaning())
254 return Tok.getLength();
255 else
256 return strlen(Buffer);
257 }
258
259 // Otherwise, compute the start of the token in the input lexer buffer.
260 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
261
262 // If this token contains nothing interesting, return it directly.
263 if (!Tok.needsCleaning()) {
264 Buffer = TokStart;
265 return Tok.getLength();
266 }
267 // Otherwise, hard case, relex the characters into the string.
268 char *OutBuf = const_cast<char*>(Buffer);
269 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
270 Ptr != End; ) {
271 unsigned CharSize;
272 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
273 Ptr += CharSize;
274 }
275 assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
276 "NeedsCleaning flag set on something that didn't need cleaning!");
277
278 return OutBuf-Buffer;
279}
280
281
282/// CreateString - Plop the specified string into a scratch buffer and return a
283/// location for it. If specified, the source location provides a source
284/// location for the token.
285SourceLocation Preprocessor::
286CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) {
287 if (SLoc.isValid())
288 return ScratchBuf->getToken(Buf, Len, SLoc);
289 return ScratchBuf->getToken(Buf, Len);
290}
291
292
293/// AdvanceToTokenCharacter - Given a location that specifies the start of a
294/// token, return a new location that specifies a character within the token.
295SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart,
296 unsigned CharNo) {
297 // If they request the first char of the token, we're trivially done. If this
298 // is a macro expansion, it doesn't make sense to point to a character within
299 // the instantiation point (the name). We could point to the source
300 // character, but without also pointing to instantiation info, this is
301 // confusing.
302 if (CharNo == 0 || TokStart.isMacroID()) return TokStart;
303
304 // Figure out how many physical characters away the specified logical
305 // character is. This needs to take into consideration newlines and
306 // trigraphs.
307 const char *TokPtr = SourceMgr.getCharacterData(TokStart);
308 unsigned PhysOffset = 0;
309
310 // The usual case is that tokens don't contain anything interesting. Skip
311 // over the uninteresting characters. If a token only consists of simple
312 // chars, this method is extremely fast.
313 while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr))
314 ++TokPtr, --CharNo, ++PhysOffset;
315
316 // If we have a character that may be a trigraph or escaped newline, create a
317 // lexer to parse it correctly.
318 if (CharNo != 0) {
319 // Create a lexer starting at this token position.
320 Lexer TheLexer(TokStart, *this, TokPtr);
321 Token Tok;
322 // Skip over characters the remaining characters.
323 const char *TokStartPtr = TokPtr;
324 for (; CharNo; --CharNo)
325 TheLexer.getAndAdvanceChar(TokPtr, Tok);
326
327 PhysOffset += TokPtr-TokStartPtr;
328 }
329
330 return TokStart.getFileLocWithOffset(PhysOffset);
331}
332
333
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000334//===----------------------------------------------------------------------===//
335// Preprocessor Initialization Methods
336//===----------------------------------------------------------------------===//
337
338// Append a #define line to Buf for Macro. Macro should be of the form XXX,
339// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
340// "#define XXX Y z W". To get a #define with no value, use "XXX=".
341static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
342 const char *Command = "#define ") {
343 Buf.insert(Buf.end(), Command, Command+strlen(Command));
344 if (const char *Equal = strchr(Macro, '=')) {
345 // Turn the = into ' '.
346 Buf.insert(Buf.end(), Macro, Equal);
347 Buf.push_back(' ');
348 Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal));
349 } else {
350 // Push "macroname 1".
351 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
352 Buf.push_back(' ');
353 Buf.push_back('1');
354 }
355 Buf.push_back('\n');
356}
357
358
359static void InitializePredefinedMacros(Preprocessor &PP,
360 std::vector<char> &Buf) {
361 // FIXME: Implement magic like cpp_init_builtins for things like __STDC__
362 // and __DATE__ etc.
363#if 0
364 /* __STDC__ has the value 1 under normal circumstances.
365 However, if (a) we are in a system header, (b) the option
366 stdc_0_in_system_headers is true (set by target config), and
367 (c) we are not in strictly conforming mode, then it has the
368 value 0. (b) and (c) are already checked in cpp_init_builtins. */
369 //case BT_STDC:
370 if (cpp_in_system_header (pfile))
371 number = 0;
372 else
373 number = 1;
374 break;
375#endif
376 // These should all be defined in the preprocessor according to the
377 // current language configuration.
378 DefineBuiltinMacro(Buf, "__STDC__=1");
379 //DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
380 if (PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus)
381 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
382 else if (0) // STDC94 ?
383 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
384
385 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");
386 if (PP.getLangOptions().ObjC1)
387 DefineBuiltinMacro(Buf, "__OBJC__=1");
388 if (PP.getLangOptions().ObjC2)
389 DefineBuiltinMacro(Buf, "__OBJC2__=1");
Steve Naroffae84af82007-10-31 18:42:27 +0000390
Chris Lattner77cec472007-10-10 17:48:53 +0000391 // Add __builtin_va_list typedef.
392 {
393 const char *VAList = PP.getTargetInfo().getVAListDeclaration();
394 Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
395 Buf.push_back('\n');
396 }
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000397
398 // Get the target #defines.
399 PP.getTargetInfo().getTargetDefines(Buf);
400
401 // Compiler set macros.
402 DefineBuiltinMacro(Buf, "__APPLE_CC__=5250");
Steve Naroffb5a086e2007-11-10 18:06:36 +0000403 DefineBuiltinMacro(Buf, "__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=1050");
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000404 DefineBuiltinMacro(Buf, "__GNUC_MINOR__=0");
405 DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
406 DefineBuiltinMacro(Buf, "__GNUC__=4");
407 DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
408 DefineBuiltinMacro(Buf, "__VERSION__=\"4.0.1 (Apple Computer, Inc. "
409 "build 5250)\"");
410
411 // Build configuration options.
412 DefineBuiltinMacro(Buf, "__DYNAMIC__=1");
413 DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
414 DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
415 DefineBuiltinMacro(Buf, "__PIC__=1");
416
417
418 if (PP.getLangOptions().CPlusPlus) {
419 DefineBuiltinMacro(Buf, "__DEPRECATED=1");
420 DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
421 DefineBuiltinMacro(Buf, "__GNUG__=4");
422 DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
423 DefineBuiltinMacro(Buf, "__cplusplus=1");
424 DefineBuiltinMacro(Buf, "__private_extern__=extern");
425 }
Steve Naroff73a07032008-02-07 03:50:06 +0000426 if (PP.getLangOptions().Microsoft) {
427 DefineBuiltinMacro(Buf, "__stdcall=");
428 DefineBuiltinMacro(Buf, "__cdecl=");
429 DefineBuiltinMacro(Buf, "_cdecl=");
430 DefineBuiltinMacro(Buf, "__ptr64=");
Steve Naroffbe880ec2008-02-07 23:24:32 +0000431 DefineBuiltinMacro(Buf, "__w64=");
Steve Naroff73a07032008-02-07 03:50:06 +0000432 DefineBuiltinMacro(Buf, "__forceinline=");
Steve Narofff9bba132008-02-07 15:26:07 +0000433 DefineBuiltinMacro(Buf, "__int8=char");
434 DefineBuiltinMacro(Buf, "__int16=short");
435 DefineBuiltinMacro(Buf, "__int32=int");
Chris Lattnerd1a552b2008-02-10 21:12:45 +0000436 DefineBuiltinMacro(Buf, "__int64=long long");
Steve Naroffcfe78212008-02-11 22:29:58 +0000437 DefineBuiltinMacro(Buf, "__declspec(X)=");
Steve Naroff73a07032008-02-07 03:50:06 +0000438 }
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000439 // FIXME: Should emit a #line directive here.
440}
441
442
443/// EnterMainSourceFile - Enter the specified FileID as the main source file,
Nate Begeman886bf132008-01-07 04:01:26 +0000444/// which implicitly adds the builtin defines etc.
Ted Kremenek17861c52007-12-19 22:51:13 +0000445void Preprocessor::EnterMainSourceFile() {
446
447 unsigned MainFileID = SourceMgr.getMainFileID();
448
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000449 // Enter the main file source buffer.
450 EnterSourceFile(MainFileID, 0);
451
Chris Lattnerb45f05c2007-11-15 19:07:47 +0000452 // Tell the header info that the main file was entered. If the file is later
453 // #imported, it won't be re-entered.
454 if (const FileEntry *FE =
455 SourceMgr.getFileEntryForLoc(SourceLocation::getFileLoc(MainFileID, 0)))
456 HeaderInfo.IncrementIncludeCount(FE);
457
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000458 std::vector<char> PrologFile;
459 PrologFile.reserve(4080);
460
461 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
462 InitializePredefinedMacros(*this, PrologFile);
463
464 // Add on the predefines from the driver.
465 PrologFile.insert(PrologFile.end(), Predefines,Predefines+strlen(Predefines));
466
467 // Memory buffer must end with a null byte!
468 PrologFile.push_back(0);
469
470 // Now that we have emitted the predefined macros, #includes, etc into
471 // PrologFile, preprocess it to populate the initial preprocessor state.
472 llvm::MemoryBuffer *SB =
473 llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(),
474 "<predefines>");
475 assert(SB && "Cannot fail to create predefined source buffer");
476 unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB);
477 assert(FileID && "Could not create FileID for predefines?");
478
479 // Start parsing the predefines.
480 EnterSourceFile(FileID, 0);
481}
Chris Lattner4b009652007-07-25 00:24:17 +0000482
483//===----------------------------------------------------------------------===//
484// Source File Location Methods.
485//===----------------------------------------------------------------------===//
486
487/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
488/// return null on failure. isAngled indicates whether the file reference is
489/// for system #include's or not (i.e. using <> instead of "").
490const FileEntry *Preprocessor::LookupFile(const char *FilenameStart,
491 const char *FilenameEnd,
492 bool isAngled,
493 const DirectoryLookup *FromDir,
494 const DirectoryLookup *&CurDir) {
495 // If the header lookup mechanism may be relative to the current file, pass in
496 // info about where the current file is.
497 const FileEntry *CurFileEnt = 0;
498 if (!FromDir) {
499 SourceLocation FileLoc = getCurrentFileLexer()->getFileLoc();
500 CurFileEnt = SourceMgr.getFileEntryForLoc(FileLoc);
501 }
502
503 // Do a standard file entry lookup.
504 CurDir = CurDirLookup;
505 const FileEntry *FE =
506 HeaderInfo.LookupFile(FilenameStart, FilenameEnd,
507 isAngled, FromDir, CurDir, CurFileEnt);
508 if (FE) return FE;
509
510 // Otherwise, see if this is a subframework header. If so, this is relative
511 // to one of the headers on the #include stack. Walk the list of the current
512 // headers on the #include stack and pass them to HeaderInfo.
513 if (CurLexer && !CurLexer->Is_PragmaLexer) {
Chris Lattner017d65b2008-02-01 05:34:02 +0000514 if ((CurFileEnt = SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())))
515 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
516 CurFileEnt)))
517 return FE;
Chris Lattner4b009652007-07-25 00:24:17 +0000518 }
519
520 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
521 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
522 if (ISEntry.TheLexer && !ISEntry.TheLexer->Is_PragmaLexer) {
Chris Lattner017d65b2008-02-01 05:34:02 +0000523 if ((CurFileEnt =
524 SourceMgr.getFileEntryForLoc(ISEntry.TheLexer->getFileLoc())))
525 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart,
526 FilenameEnd, CurFileEnt)))
527 return FE;
Chris Lattner4b009652007-07-25 00:24:17 +0000528 }
529 }
530
531 // Otherwise, we really couldn't find the file.
532 return 0;
533}
534
535/// isInPrimaryFile - Return true if we're in the top-level file, not in a
536/// #include.
537bool Preprocessor::isInPrimaryFile() const {
538 if (CurLexer && !CurLexer->Is_PragmaLexer)
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000539 return IncludeMacroStack.empty();
Chris Lattner4b009652007-07-25 00:24:17 +0000540
541 // If there are any stacked lexers, we're in a #include.
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000542 assert(IncludeMacroStack[0].TheLexer &&
543 !IncludeMacroStack[0].TheLexer->Is_PragmaLexer &&
544 "Top level include stack isn't our primary lexer?");
545 for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i)
Chris Lattner4b009652007-07-25 00:24:17 +0000546 if (IncludeMacroStack[i].TheLexer &&
547 !IncludeMacroStack[i].TheLexer->Is_PragmaLexer)
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000548 return false;
549 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000550}
551
552/// getCurrentLexer - Return the current file lexer being lexed from. Note
553/// that this ignores any potentially active macro expansions and _Pragma
554/// expansions going on at the time.
555Lexer *Preprocessor::getCurrentFileLexer() const {
556 if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer;
557
558 // Look for a stacked lexer.
559 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
560 Lexer *L = IncludeMacroStack[i-1].TheLexer;
561 if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions.
562 return L;
563 }
564 return 0;
565}
566
567
568/// EnterSourceFile - Add a source file to the top of the include stack and
569/// start lexing tokens from it instead of the current buffer. Return true
570/// on failure.
571void Preprocessor::EnterSourceFile(unsigned FileID,
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000572 const DirectoryLookup *CurDir) {
Chris Lattner5b54ed92008-03-09 02:26:03 +0000573 assert(CurTokenLexer == 0 && "Cannot #include a file inside a macro!");
Chris Lattner4b009652007-07-25 00:24:17 +0000574 ++NumEnteredSourceFiles;
575
576 if (MaxIncludeStackDepth < IncludeMacroStack.size())
577 MaxIncludeStackDepth = IncludeMacroStack.size();
578
579 Lexer *TheLexer = new Lexer(SourceLocation::getFileLoc(FileID, 0), *this);
Chris Lattner4b009652007-07-25 00:24:17 +0000580 EnterSourceFileWithLexer(TheLexer, CurDir);
581}
582
583/// EnterSourceFile - Add a source file to the top of the include stack and
584/// start lexing tokens from it instead of the current buffer.
585void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
586 const DirectoryLookup *CurDir) {
587
588 // Add the current lexer to the include stack.
Chris Lattner5b54ed92008-03-09 02:26:03 +0000589 if (CurLexer || CurTokenLexer)
Chris Lattner4b009652007-07-25 00:24:17 +0000590 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
Chris Lattner5b54ed92008-03-09 02:26:03 +0000591 CurTokenLexer));
Chris Lattner4b009652007-07-25 00:24:17 +0000592
593 CurLexer = TheLexer;
594 CurDirLookup = CurDir;
Chris Lattner5b54ed92008-03-09 02:26:03 +0000595 CurTokenLexer = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000596
597 // Notify the client, if desired, that we are in a new source file.
598 if (Callbacks && !CurLexer->Is_PragmaLexer) {
599 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
600
601 // Get the file entry for the current file.
602 if (const FileEntry *FE =
603 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
604 FileType = HeaderInfo.getFileDirFlavor(FE);
605
606 Callbacks->FileChanged(CurLexer->getFileLoc(),
607 PPCallbacks::EnterFile, FileType);
608 }
609}
610
611
612
613/// EnterMacro - Add a Macro to the top of the include stack and start lexing
614/// tokens from it instead of the current buffer.
615void Preprocessor::EnterMacro(Token &Tok, MacroArgs *Args) {
616 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
Chris Lattner5b54ed92008-03-09 02:26:03 +0000617 CurTokenLexer));
Chris Lattner4b009652007-07-25 00:24:17 +0000618 CurLexer = 0;
619 CurDirLookup = 0;
620
Chris Lattner5b54ed92008-03-09 02:26:03 +0000621 if (NumCachedTokenLexers == 0) {
622 CurTokenLexer = new TokenLexer(Tok, Args, *this);
Chris Lattner4b009652007-07-25 00:24:17 +0000623 } else {
Chris Lattner5b54ed92008-03-09 02:26:03 +0000624 CurTokenLexer = TokenLexerCache[--NumCachedTokenLexers];
625 CurTokenLexer->Init(Tok, Args);
Chris Lattner4b009652007-07-25 00:24:17 +0000626 }
627}
628
629/// EnterTokenStream - Add a "macro" context to the top of the include stack,
630/// which will cause the lexer to start returning the specified tokens. Note
631/// that these tokens will be re-macro-expanded when/if expansion is enabled.
632/// This method assumes that the specified stream of tokens has a permanent
633/// owner somewhere, so they do not need to be copied.
634void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks) {
635 // Save our current state.
636 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
Chris Lattner5b54ed92008-03-09 02:26:03 +0000637 CurTokenLexer));
Chris Lattner4b009652007-07-25 00:24:17 +0000638 CurLexer = 0;
639 CurDirLookup = 0;
640
641 // Create a macro expander to expand from the specified token stream.
Chris Lattner5b54ed92008-03-09 02:26:03 +0000642 if (NumCachedTokenLexers == 0) {
643 CurTokenLexer = new TokenLexer(Toks, NumToks, *this);
Chris Lattner4b009652007-07-25 00:24:17 +0000644 } else {
Chris Lattner5b54ed92008-03-09 02:26:03 +0000645 CurTokenLexer = TokenLexerCache[--NumCachedTokenLexers];
646 CurTokenLexer->Init(Toks, NumToks);
Chris Lattner4b009652007-07-25 00:24:17 +0000647 }
648}
649
650/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
651/// lexer stack. This should only be used in situations where the current
652/// state of the top-of-stack lexer is known.
653void Preprocessor::RemoveTopOfLexerStack() {
654 assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
655
Chris Lattner5b54ed92008-03-09 02:26:03 +0000656 if (CurTokenLexer) {
Chris Lattner4b009652007-07-25 00:24:17 +0000657 // Delete or cache the now-dead macro expander.
Chris Lattner5b54ed92008-03-09 02:26:03 +0000658 if (NumCachedTokenLexers == TokenLexerCacheSize)
659 delete CurTokenLexer;
Chris Lattner4b009652007-07-25 00:24:17 +0000660 else
Chris Lattner5b54ed92008-03-09 02:26:03 +0000661 TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer;
Chris Lattner4b009652007-07-25 00:24:17 +0000662 } else {
663 delete CurLexer;
664 }
Chris Lattner5b54ed92008-03-09 02:26:03 +0000665 CurLexer = IncludeMacroStack.back().TheLexer;
666 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
667 CurTokenLexer = IncludeMacroStack.back().TheTokenLexer;
Chris Lattner4b009652007-07-25 00:24:17 +0000668 IncludeMacroStack.pop_back();
669}
670
Chris Lattner4b009652007-07-25 00:24:17 +0000671
672//===----------------------------------------------------------------------===//
673// Lexer Event Handling.
674//===----------------------------------------------------------------------===//
675
676/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
677/// identifier information for the token and install it into the token.
678IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
679 const char *BufPtr) {
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000680 assert(Identifier.is(tok::identifier) && "Not an identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +0000681 assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
682
683 // Look up this token, see if it is a macro, or if it is a language keyword.
684 IdentifierInfo *II;
685 if (BufPtr && !Identifier.needsCleaning()) {
686 // No cleaning needed, just use the characters from the lexed buffer.
687 II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
688 } else {
689 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
690 llvm::SmallVector<char, 64> IdentifierBuffer;
691 IdentifierBuffer.resize(Identifier.getLength());
692 const char *TmpBuf = &IdentifierBuffer[0];
693 unsigned Size = getSpelling(Identifier, TmpBuf);
694 II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
695 }
696 Identifier.setIdentifierInfo(II);
697 return II;
698}
699
700
701/// HandleIdentifier - This callback is invoked when the lexer reads an
702/// identifier. This callback looks up the identifier in the map and/or
703/// potentially macro expands it or turns it into a named token (like 'for').
704void Preprocessor::HandleIdentifier(Token &Identifier) {
705 assert(Identifier.getIdentifierInfo() &&
706 "Can't handle identifiers without identifier info!");
707
708 IdentifierInfo &II = *Identifier.getIdentifierInfo();
709
710 // If this identifier was poisoned, and if it was not produced from a macro
711 // expansion, emit an error.
712 if (II.isPoisoned() && CurLexer) {
713 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
714 Diag(Identifier, diag::err_pp_used_poisoned_id);
715 else
716 Diag(Identifier, diag::ext_pp_bad_vaargs_use);
717 }
718
719 // If this is a macro to be expanded, do it.
Chris Lattner7a1b0882007-10-07 08:44:20 +0000720 if (MacroInfo *MI = getMacroInfo(&II)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000721 if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
722 if (MI->isEnabled()) {
723 if (!HandleMacroExpandedIdentifier(Identifier, MI))
724 return;
725 } else {
726 // C99 6.10.3.4p2 says that a disabled macro may never again be
727 // expanded, even if it's in a context where it could be expanded in the
728 // future.
729 Identifier.setFlag(Token::DisableExpand);
730 }
731 }
Chris Lattner4b009652007-07-25 00:24:17 +0000732 }
733
734 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
735 // then we act as if it is the actual operator and not the textual
736 // representation of it.
737 if (II.isCPlusPlusOperatorKeyword())
738 Identifier.setIdentifierInfo(0);
739
740 // Change the kind of this identifier to the appropriate token kind, e.g.
741 // turning "for" into a keyword.
742 Identifier.setKind(II.getTokenID());
743
744 // If this is an extension token, diagnose its use.
745 // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99
746 // For now, I'm just commenting it out (while I work on attributes).
747 if (II.isExtensionToken() && Features.C99)
748 Diag(Identifier, diag::ext_token_used);
749}
750
751/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
752/// the current file. This either returns the EOF token or pops a level off
753/// the include stack and keeps going.
754bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
Chris Lattner5b54ed92008-03-09 02:26:03 +0000755 assert(!CurTokenLexer &&
Chris Lattner4b009652007-07-25 00:24:17 +0000756 "Ending a file when currently in a macro!");
757
758 // See if this file had a controlling macro.
759 if (CurLexer) { // Not ending a macro, ignore it.
760 if (const IdentifierInfo *ControllingMacro =
761 CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
762 // Okay, this has a controlling macro, remember in PerFileInfo.
763 if (const FileEntry *FE =
764 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
765 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
766 }
767 }
768
769 // If this is a #include'd file, pop it off the include stack and continue
770 // lexing the #includer file.
771 if (!IncludeMacroStack.empty()) {
772 // We're done with the #included file.
773 RemoveTopOfLexerStack();
774
775 // Notify the client, if desired, that we are in a new source file.
776 if (Callbacks && !isEndOfMacro && CurLexer) {
777 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
778
779 // Get the file entry for the current file.
780 if (const FileEntry *FE =
781 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
782 FileType = HeaderInfo.getFileDirFlavor(FE);
783
784 Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr),
785 PPCallbacks::ExitFile, FileType);
786 }
787
788 // Client should lex another token.
789 return false;
790 }
Chris Lattner1d34a7c2008-01-25 00:00:30 +0000791
792 // If the file ends with a newline, form the EOF token on the newline itself,
793 // rather than "on the line following it", which doesn't exist. This makes
794 // diagnostics relating to the end of file include the last file that the user
795 // actually typed, which is goodness.
796 const char *EndPos = CurLexer->BufferEnd;
797 if (EndPos != CurLexer->BufferStart &&
798 (EndPos[-1] == '\n' || EndPos[-1] == '\r')) {
799 --EndPos;
800
801 // Handle \n\r and \r\n:
802 if (EndPos != CurLexer->BufferStart &&
803 (EndPos[-1] == '\n' || EndPos[-1] == '\r') &&
804 EndPos[-1] != EndPos[0])
805 --EndPos;
806 }
Chris Lattner4b009652007-07-25 00:24:17 +0000807
808 Result.startToken();
Chris Lattner1d34a7c2008-01-25 00:00:30 +0000809 CurLexer->BufferPtr = EndPos;
810 CurLexer->FormTokenWithChars(Result, EndPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000811 Result.setKind(tok::eof);
812
813 // We're done with the #included file.
814 delete CurLexer;
815 CurLexer = 0;
816
817 // This is the end of the top-level file. If the diag::pp_macro_not_used
Chris Lattner7a1b0882007-10-07 08:44:20 +0000818 // diagnostic is enabled, look for macros that have not been used.
Chris Lattner4b009652007-07-25 00:24:17 +0000819 if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){
Chris Lattner7a1b0882007-10-07 08:44:20 +0000820 for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
821 Macros.begin(), E = Macros.end(); I != E; ++I) {
822 if (!I->second->isUsed())
823 Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattner4b009652007-07-25 00:24:17 +0000824 }
825 }
Chris Lattner4b009652007-07-25 00:24:17 +0000826 return true;
827}
828
Chris Lattner325c1972008-03-09 03:04:16 +0000829/// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer
830/// hits the end of its token stream.
831bool Preprocessor::HandleEndOfTokenLexer(Token &Result) {
Chris Lattner5b54ed92008-03-09 02:26:03 +0000832 assert(CurTokenLexer && !CurLexer &&
Chris Lattner4b009652007-07-25 00:24:17 +0000833 "Ending a macro when currently in a #include file!");
834
835 // Delete or cache the now-dead macro expander.
Chris Lattner5b54ed92008-03-09 02:26:03 +0000836 if (NumCachedTokenLexers == TokenLexerCacheSize)
837 delete CurTokenLexer;
Chris Lattner4b009652007-07-25 00:24:17 +0000838 else
Chris Lattner5b54ed92008-03-09 02:26:03 +0000839 TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer;
Chris Lattner4b009652007-07-25 00:24:17 +0000840
841 // Handle this like a #include file being popped off the stack.
Chris Lattner5b54ed92008-03-09 02:26:03 +0000842 CurTokenLexer = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000843 return HandleEndOfFile(Result, true);
844}
845
Chris Lattner64b32ec2008-02-07 06:03:59 +0000846/// HandleMicrosoftCommentPaste - When the macro expander pastes together a
847/// comment (/##/) in microsoft mode, this method handles updating the current
848/// state, returning the token on the next source line.
849void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) {
Chris Lattner5b54ed92008-03-09 02:26:03 +0000850 assert(CurTokenLexer && !CurLexer &&
Chris Lattner64b32ec2008-02-07 06:03:59 +0000851 "Pasted comment can only be formed from macro");
852
853 // We handle this by scanning for the closest real lexer, switching it to
854 // raw mode and preprocessor mode. This will cause it to return \n as an
855 // explicit EOM token.
856 Lexer *FoundLexer = 0;
857 bool LexerWasInPPMode = false;
858 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
859 IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1);
860 if (ISI.TheLexer == 0) continue; // Scan for a real lexer.
861
862 // Once we find a real lexer, mark it as raw mode (disabling macro
863 // expansions) and preprocessor mode (return EOM). We know that the lexer
864 // was *not* in raw mode before, because the macro that the comment came
865 // from was expanded. However, it could have already been in preprocessor
866 // mode (#if COMMENT) in which case we have to return it to that mode and
867 // return EOM.
868 FoundLexer = ISI.TheLexer;
869 FoundLexer->LexingRawMode = true;
870 LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective;
871 FoundLexer->ParsingPreprocessorDirective = true;
872 break;
873 }
874
875 // Okay, we either found and switched over the lexer, or we didn't find a
876 // lexer. In either case, finish off the macro the comment came from, getting
877 // the next token.
Chris Lattner325c1972008-03-09 03:04:16 +0000878 if (!HandleEndOfTokenLexer(Tok)) Lex(Tok);
Chris Lattner64b32ec2008-02-07 06:03:59 +0000879
880 // Discarding comments as long as we don't have EOF or EOM. This 'comments
881 // out' the rest of the line, including any tokens that came from other macros
882 // that were active, as in:
883 // #define submacro a COMMENT b
884 // submacro c
885 // which should lex to 'a' only: 'b' and 'c' should be removed.
886 while (Tok.isNot(tok::eom) && Tok.isNot(tok::eof))
887 Lex(Tok);
888
889 // If we got an eom token, then we successfully found the end of the line.
890 if (Tok.is(tok::eom)) {
891 assert(FoundLexer && "Can't get end of line without an active lexer");
892 // Restore the lexer back to normal mode instead of raw mode.
893 FoundLexer->LexingRawMode = false;
894
895 // If the lexer was already in preprocessor mode, just return the EOM token
896 // to finish the preprocessor line.
897 if (LexerWasInPPMode) return;
898
899 // Otherwise, switch out of PP mode and return the next lexed token.
900 FoundLexer->ParsingPreprocessorDirective = false;
901 return Lex(Tok);
902 }
903
904 // If we got an EOF token, then we reached the end of the token stream but
905 // didn't find an explicit \n. This can only happen if there was no lexer
906 // active (an active lexer would return EOM at EOF if there was no \n in
907 // preprocessor directive mode), so just return EOF as our token.
908 assert(!FoundLexer && "Lexer should return EOM before EOF in PP mode");
909 return;
910}
Chris Lattner4b009652007-07-25 00:24:17 +0000911