blob: d2630584a59811e18e58002c0176bc5f5ac76eac [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
35#include "clang/Basic/FileManager.h"
36#include "clang/Basic/SourceManager.h"
37#include "clang/Basic/TargetInfo.h"
38#include "llvm/ADT/SmallVector.h"
Chris Lattner97ba77c2007-07-16 06:48:38 +000039#include "llvm/Support/MemoryBuffer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000040#include <iostream>
Chris Lattner77034d32007-09-03 18:30:32 +000041#include <ctime>
Reid Spencer5f016e22007-07-11 17:01:13 +000042using namespace clang;
43
44//===----------------------------------------------------------------------===//
45
46Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
47 TargetInfo &target, SourceManager &SM,
48 HeaderSearch &Headers)
49 : Diags(diags), Features(opts), Target(target), FileMgr(Headers.getFileMgr()),
50 SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts),
51 CurLexer(0), CurDirLookup(0), CurMacroExpander(0), Callbacks(0) {
52 ScratchBuf = new ScratchBuffer(SourceMgr);
Chris Lattner9594acf2007-07-15 00:25:26 +000053
Reid Spencer5f016e22007-07-11 17:01:13 +000054 // Clear stats.
55 NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
56 NumIf = NumElse = NumEndif = 0;
57 NumEnteredSourceFiles = 0;
58 NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
59 NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
60 MaxIncludeStackDepth = 0;
61 NumSkipped = 0;
62
63 // Default to discarding comments.
64 KeepComments = false;
65 KeepMacroComments = false;
66
67 // Macro expansion is enabled.
68 DisableMacroExpansion = false;
69 InMacroArgs = false;
Chris Lattner9594acf2007-07-15 00:25:26 +000070 NumCachedMacroExpanders = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000071
72 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
73 // This gets unpoisoned where it is allowed.
74 (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
75
Chris Lattner53b0dab2007-10-09 22:10:18 +000076 Predefines = 0;
77
Reid Spencer5f016e22007-07-11 17:01:13 +000078 // Initialize the pragma handlers.
79 PragmaHandlers = new PragmaNamespace(0);
80 RegisterBuiltinPragmas();
81
82 // Initialize builtin macros like __LINE__ and friends.
83 RegisterBuiltinMacros();
84}
85
86Preprocessor::~Preprocessor() {
87 // Free any active lexers.
88 delete CurLexer;
89
90 while (!IncludeMacroStack.empty()) {
91 delete IncludeMacroStack.back().TheLexer;
92 delete IncludeMacroStack.back().TheMacroExpander;
93 IncludeMacroStack.pop_back();
94 }
Chris Lattnercc1a8752007-10-07 08:44:20 +000095
96 // Free any macro definitions.
97 for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
98 Macros.begin(), E = Macros.end(); I != E; ++I) {
99 // Free the macro definition.
100 delete I->second;
101 I->second = 0;
102 I->first->setHasMacroDefinition(false);
103 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000104
Chris Lattner9594acf2007-07-15 00:25:26 +0000105 // Free any cached macro expanders.
106 for (unsigned i = 0, e = NumCachedMacroExpanders; i != e; ++i)
107 delete MacroExpanderCache[i];
108
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 // Release pragma information.
110 delete PragmaHandlers;
111
112 // Delete the scratch buffer info.
113 delete ScratchBuf;
114}
115
116PPCallbacks::~PPCallbacks() {
117}
118
119/// Diag - Forwarding function for diagnostics. This emits a diagnostic at
Chris Lattnerd2177732007-07-20 16:59:19 +0000120/// the specified Token's location, translating the token's start
Reid Spencer5f016e22007-07-11 17:01:13 +0000121/// position in the current buffer into a SourcePosition object for rendering.
122void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID) {
123 Diags.Report(Loc, DiagID);
124}
125
126void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
127 const std::string &Msg) {
128 Diags.Report(Loc, DiagID, &Msg, 1);
129}
130
Chris Lattnerd2177732007-07-20 16:59:19 +0000131void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 std::cerr << tok::getTokenName(Tok.getKind()) << " '"
133 << getSpelling(Tok) << "'";
134
135 if (!DumpFlags) return;
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000136
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 std::cerr << "\t";
138 if (Tok.isAtStartOfLine())
139 std::cerr << " [StartOfLine]";
140 if (Tok.hasLeadingSpace())
141 std::cerr << " [LeadingSpace]";
142 if (Tok.isExpandDisabled())
143 std::cerr << " [ExpandDisabled]";
144 if (Tok.needsCleaning()) {
145 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
146 std::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
147 << "']";
148 }
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000149
150 std::cerr << "\tLoc=<";
151 DumpLocation(Tok.getLocation());
152 std::cerr << ">";
153}
154
155void Preprocessor::DumpLocation(SourceLocation Loc) const {
156 SourceLocation LogLoc = SourceMgr.getLogicalLoc(Loc);
157 std::cerr << SourceMgr.getSourceName(LogLoc) << ':'
158 << SourceMgr.getLineNumber(LogLoc) << ':'
159 << SourceMgr.getLineNumber(LogLoc);
160
161 SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(Loc);
162 if (PhysLoc != LogLoc) {
163 std::cerr << " <PhysLoc=";
164 DumpLocation(PhysLoc);
165 std::cerr << ">";
166 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000167}
168
169void Preprocessor::DumpMacro(const MacroInfo &MI) const {
170 std::cerr << "MACRO: ";
171 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
172 DumpToken(MI.getReplacementToken(i));
173 std::cerr << " ";
174 }
175 std::cerr << "\n";
176}
177
178void Preprocessor::PrintStats() {
179 std::cerr << "\n*** Preprocessor Stats:\n";
180 std::cerr << NumDirectives << " directives found:\n";
181 std::cerr << " " << NumDefined << " #define.\n";
182 std::cerr << " " << NumUndefined << " #undef.\n";
183 std::cerr << " #include/#include_next/#import:\n";
184 std::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
185 std::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
186 std::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
187 std::cerr << " " << NumElse << " #else/#elif.\n";
188 std::cerr << " " << NumEndif << " #endif.\n";
189 std::cerr << " " << NumPragma << " #pragma.\n";
190 std::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
191
192 std::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
193 << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
194 << NumFastMacroExpanded << " on the fast path.\n";
195 std::cerr << (NumFastTokenPaste+NumTokenPaste)
196 << " token paste (##) operations performed, "
197 << NumFastTokenPaste << " on the fast path.\n";
198}
199
200//===----------------------------------------------------------------------===//
201// Token Spelling
202//===----------------------------------------------------------------------===//
203
204
205/// getSpelling() - Return the 'spelling' of this token. The spelling of a
206/// token are the characters used to represent the token in the source file
207/// after trigraph expansion and escaped-newline folding. In particular, this
208/// wants to get the true, uncanonicalized, spelling of things like digraphs
209/// UCNs, etc.
Chris Lattnerd2177732007-07-20 16:59:19 +0000210std::string Preprocessor::getSpelling(const Token &Tok) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
212
213 // If this token contains nothing interesting, return it directly.
214 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
215 if (!Tok.needsCleaning())
216 return std::string(TokStart, TokStart+Tok.getLength());
217
218 std::string Result;
219 Result.reserve(Tok.getLength());
220
221 // Otherwise, hard case, relex the characters into the string.
222 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
223 Ptr != End; ) {
224 unsigned CharSize;
225 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
226 Ptr += CharSize;
227 }
228 assert(Result.size() != unsigned(Tok.getLength()) &&
229 "NeedsCleaning flag set on something that didn't need cleaning!");
230 return Result;
231}
232
233/// getSpelling - This method is used to get the spelling of a token into a
234/// preallocated buffer, instead of as an std::string. The caller is required
235/// to allocate enough space for the token, which is guaranteed to be at least
236/// Tok.getLength() bytes long. The actual length of the token is returned.
237///
238/// Note that this method may do two possible things: it may either fill in
239/// the buffer specified with characters, or it may *change the input pointer*
240/// to point to a constant buffer with the data already in it (avoiding a
241/// copy). The caller is not allowed to modify the returned buffer pointer
242/// if an internal buffer is returned.
Chris Lattnerd2177732007-07-20 16:59:19 +0000243unsigned Preprocessor::getSpelling(const Token &Tok,
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 const char *&Buffer) const {
245 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
246
247 // If this token is an identifier, just return the string from the identifier
248 // table, which is very quick.
249 if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
250 Buffer = II->getName();
Chris Lattner0f670322007-07-22 22:50:09 +0000251
252 // Return the length of the token. If the token needed cleaning, don't
253 // include the size of the newlines or trigraphs in it.
254 if (!Tok.needsCleaning())
255 return Tok.getLength();
256 else
257 return strlen(Buffer);
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 }
259
260 // Otherwise, compute the start of the token in the input lexer buffer.
261 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
262
263 // If this token contains nothing interesting, return it directly.
264 if (!Tok.needsCleaning()) {
265 Buffer = TokStart;
266 return Tok.getLength();
267 }
268 // Otherwise, hard case, relex the characters into the string.
269 char *OutBuf = const_cast<char*>(Buffer);
270 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
271 Ptr != End; ) {
272 unsigned CharSize;
273 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
274 Ptr += CharSize;
275 }
276 assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
277 "NeedsCleaning flag set on something that didn't need cleaning!");
278
279 return OutBuf-Buffer;
280}
281
282
283/// CreateString - Plop the specified string into a scratch buffer and return a
284/// location for it. If specified, the source location provides a source
285/// location for the token.
286SourceLocation Preprocessor::
287CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) {
288 if (SLoc.isValid())
289 return ScratchBuf->getToken(Buf, Len, SLoc);
290 return ScratchBuf->getToken(Buf, Len);
291}
292
293
Chris Lattner97ba77c2007-07-16 06:48:38 +0000294/// AdvanceToTokenCharacter - Given a location that specifies the start of a
295/// token, return a new location that specifies a character within the token.
296SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart,
297 unsigned CharNo) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000298 // If they request the first char of the token, we're trivially done. If this
299 // is a macro expansion, it doesn't make sense to point to a character within
300 // the instantiation point (the name). We could point to the source
301 // character, but without also pointing to instantiation info, this is
302 // confusing.
303 if (CharNo == 0 || TokStart.isMacroID()) return TokStart;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000304
305 // Figure out how many physical characters away the specified logical
306 // character is. This needs to take into consideration newlines and
307 // trigraphs.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000308 const char *TokPtr = SourceMgr.getCharacterData(TokStart);
309 unsigned PhysOffset = 0;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000310
311 // The usual case is that tokens don't contain anything interesting. Skip
312 // over the uninteresting characters. If a token only consists of simple
313 // chars, this method is extremely fast.
314 while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr))
Chris Lattner9dc1f532007-07-20 16:37:10 +0000315 ++TokPtr, --CharNo, ++PhysOffset;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000316
317 // If we have a character that may be a trigraph or escaped newline, create a
318 // lexer to parse it correctly.
Chris Lattner97ba77c2007-07-16 06:48:38 +0000319 if (CharNo != 0) {
320 // Create a lexer starting at this token position.
Chris Lattner25bdb512007-07-20 16:52:03 +0000321 Lexer TheLexer(TokStart, *this, TokPtr);
Chris Lattnerd2177732007-07-20 16:59:19 +0000322 Token Tok;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000323 // Skip over characters the remaining characters.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000324 const char *TokStartPtr = TokPtr;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000325 for (; CharNo; --CharNo)
326 TheLexer.getAndAdvanceChar(TokPtr, Tok);
Chris Lattner9dc1f532007-07-20 16:37:10 +0000327
328 PhysOffset += TokPtr-TokStartPtr;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000329 }
Chris Lattner9dc1f532007-07-20 16:37:10 +0000330
331 return TokStart.getFileLocWithOffset(PhysOffset);
Chris Lattner97ba77c2007-07-16 06:48:38 +0000332}
333
334
Chris Lattner53b0dab2007-10-09 22:10:18 +0000335//===----------------------------------------------------------------------===//
336// Preprocessor Initialization Methods
337//===----------------------------------------------------------------------===//
338
339// Append a #define line to Buf for Macro. Macro should be of the form XXX,
340// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
341// "#define XXX Y z W". To get a #define with no value, use "XXX=".
342static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
343 const char *Command = "#define ") {
344 Buf.insert(Buf.end(), Command, Command+strlen(Command));
345 if (const char *Equal = strchr(Macro, '=')) {
346 // Turn the = into ' '.
347 Buf.insert(Buf.end(), Macro, Equal);
348 Buf.push_back(' ');
349 Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal));
350 } else {
351 // Push "macroname 1".
352 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
353 Buf.push_back(' ');
354 Buf.push_back('1');
355 }
356 Buf.push_back('\n');
357}
358
359
360static void InitializePredefinedMacros(Preprocessor &PP,
361 std::vector<char> &Buf) {
362 // FIXME: Implement magic like cpp_init_builtins for things like __STDC__
363 // and __DATE__ etc.
364#if 0
365 /* __STDC__ has the value 1 under normal circumstances.
366 However, if (a) we are in a system header, (b) the option
367 stdc_0_in_system_headers is true (set by target config), and
368 (c) we are not in strictly conforming mode, then it has the
369 value 0. (b) and (c) are already checked in cpp_init_builtins. */
370 //case BT_STDC:
371 if (cpp_in_system_header (pfile))
372 number = 0;
373 else
374 number = 1;
375 break;
376#endif
377 // These should all be defined in the preprocessor according to the
378 // current language configuration.
379 DefineBuiltinMacro(Buf, "__STDC__=1");
380 //DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
381 if (PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus)
382 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
383 else if (0) // STDC94 ?
384 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
385
386 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");
387 if (PP.getLangOptions().ObjC1)
388 DefineBuiltinMacro(Buf, "__OBJC__=1");
389 if (PP.getLangOptions().ObjC2)
390 DefineBuiltinMacro(Buf, "__OBJC2__=1");
Steve Naroff8ee529b2007-10-31 18:42:27 +0000391
Chris Lattnerd19144b2007-10-10 17:48:53 +0000392 // Add __builtin_va_list typedef.
393 {
394 const char *VAList = PP.getTargetInfo().getVAListDeclaration();
395 Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
396 Buf.push_back('\n');
397 }
Chris Lattner53b0dab2007-10-09 22:10:18 +0000398
399 // Get the target #defines.
400 PP.getTargetInfo().getTargetDefines(Buf);
401
402 // Compiler set macros.
403 DefineBuiltinMacro(Buf, "__APPLE_CC__=5250");
Steve Naroff39d0a272007-11-10 18:06:36 +0000404 DefineBuiltinMacro(Buf, "__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=1050");
Chris Lattner53b0dab2007-10-09 22:10:18 +0000405 DefineBuiltinMacro(Buf, "__GNUC_MINOR__=0");
406 DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
407 DefineBuiltinMacro(Buf, "__GNUC__=4");
408 DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
409 DefineBuiltinMacro(Buf, "__VERSION__=\"4.0.1 (Apple Computer, Inc. "
410 "build 5250)\"");
411
412 // Build configuration options.
413 DefineBuiltinMacro(Buf, "__DYNAMIC__=1");
414 DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
415 DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
416 DefineBuiltinMacro(Buf, "__PIC__=1");
417
418
419 if (PP.getLangOptions().CPlusPlus) {
420 DefineBuiltinMacro(Buf, "__DEPRECATED=1");
421 DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
422 DefineBuiltinMacro(Buf, "__GNUG__=4");
423 DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
424 DefineBuiltinMacro(Buf, "__cplusplus=1");
425 DefineBuiltinMacro(Buf, "__private_extern__=extern");
426 }
427
428 // FIXME: Should emit a #line directive here.
429}
430
431
432/// EnterMainSourceFile - Enter the specified FileID as the main source file,
433/// which implicitly adds the builting defines etc.
434void Preprocessor::EnterMainSourceFile(unsigned MainFileID) {
435 // Enter the main file source buffer.
436 EnterSourceFile(MainFileID, 0);
437
Chris Lattnerb2832982007-11-15 19:07:47 +0000438 // Tell the header info that the main file was entered. If the file is later
439 // #imported, it won't be re-entered.
440 if (const FileEntry *FE =
441 SourceMgr.getFileEntryForLoc(SourceLocation::getFileLoc(MainFileID, 0)))
442 HeaderInfo.IncrementIncludeCount(FE);
443
Chris Lattner53b0dab2007-10-09 22:10:18 +0000444 std::vector<char> PrologFile;
445 PrologFile.reserve(4080);
446
447 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
448 InitializePredefinedMacros(*this, PrologFile);
449
450 // Add on the predefines from the driver.
451 PrologFile.insert(PrologFile.end(), Predefines,Predefines+strlen(Predefines));
452
453 // Memory buffer must end with a null byte!
454 PrologFile.push_back(0);
455
456 // Now that we have emitted the predefined macros, #includes, etc into
457 // PrologFile, preprocess it to populate the initial preprocessor state.
458 llvm::MemoryBuffer *SB =
459 llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(),
460 "<predefines>");
461 assert(SB && "Cannot fail to create predefined source buffer");
462 unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB);
463 assert(FileID && "Could not create FileID for predefines?");
464
465 // Start parsing the predefines.
466 EnterSourceFile(FileID, 0);
467}
Chris Lattner97ba77c2007-07-16 06:48:38 +0000468
Reid Spencer5f016e22007-07-11 17:01:13 +0000469//===----------------------------------------------------------------------===//
470// Source File Location Methods.
471//===----------------------------------------------------------------------===//
472
473/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
474/// return null on failure. isAngled indicates whether the file reference is
475/// for system #include's or not (i.e. using <> instead of "").
476const FileEntry *Preprocessor::LookupFile(const char *FilenameStart,
477 const char *FilenameEnd,
478 bool isAngled,
479 const DirectoryLookup *FromDir,
480 const DirectoryLookup *&CurDir) {
481 // If the header lookup mechanism may be relative to the current file, pass in
482 // info about where the current file is.
483 const FileEntry *CurFileEnt = 0;
484 if (!FromDir) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000485 SourceLocation FileLoc = getCurrentFileLexer()->getFileLoc();
486 CurFileEnt = SourceMgr.getFileEntryForLoc(FileLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000487 }
488
489 // Do a standard file entry lookup.
490 CurDir = CurDirLookup;
491 const FileEntry *FE =
492 HeaderInfo.LookupFile(FilenameStart, FilenameEnd,
493 isAngled, FromDir, CurDir, CurFileEnt);
494 if (FE) return FE;
495
496 // Otherwise, see if this is a subframework header. If so, this is relative
497 // to one of the headers on the #include stack. Walk the list of the current
498 // headers on the #include stack and pass them to HeaderInfo.
499 if (CurLexer && !CurLexer->Is_PragmaLexer) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000500 CurFileEnt = SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000501 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
502 CurFileEnt)))
503 return FE;
504 }
505
506 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
507 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
508 if (ISEntry.TheLexer && !ISEntry.TheLexer->Is_PragmaLexer) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000509 CurFileEnt = SourceMgr.getFileEntryForLoc(ISEntry.TheLexer->getFileLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000510 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
511 CurFileEnt)))
512 return FE;
513 }
514 }
515
516 // Otherwise, we really couldn't find the file.
517 return 0;
518}
519
520/// isInPrimaryFile - Return true if we're in the top-level file, not in a
521/// #include.
522bool Preprocessor::isInPrimaryFile() const {
523 if (CurLexer && !CurLexer->Is_PragmaLexer)
Chris Lattner53b0dab2007-10-09 22:10:18 +0000524 return IncludeMacroStack.empty();
Reid Spencer5f016e22007-07-11 17:01:13 +0000525
526 // If there are any stacked lexers, we're in a #include.
Chris Lattner53b0dab2007-10-09 22:10:18 +0000527 assert(IncludeMacroStack[0].TheLexer &&
528 !IncludeMacroStack[0].TheLexer->Is_PragmaLexer &&
529 "Top level include stack isn't our primary lexer?");
530 for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i)
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 if (IncludeMacroStack[i].TheLexer &&
532 !IncludeMacroStack[i].TheLexer->Is_PragmaLexer)
Chris Lattner53b0dab2007-10-09 22:10:18 +0000533 return false;
534 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000535}
536
537/// getCurrentLexer - Return the current file lexer being lexed from. Note
538/// that this ignores any potentially active macro expansions and _Pragma
539/// expansions going on at the time.
540Lexer *Preprocessor::getCurrentFileLexer() const {
541 if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer;
542
543 // Look for a stacked lexer.
544 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
545 Lexer *L = IncludeMacroStack[i-1].TheLexer;
546 if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions.
547 return L;
548 }
549 return 0;
550}
551
552
553/// EnterSourceFile - Add a source file to the top of the include stack and
554/// start lexing tokens from it instead of the current buffer. Return true
555/// on failure.
556void Preprocessor::EnterSourceFile(unsigned FileID,
Chris Lattner53b0dab2007-10-09 22:10:18 +0000557 const DirectoryLookup *CurDir) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000558 assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
559 ++NumEnteredSourceFiles;
560
561 if (MaxIncludeStackDepth < IncludeMacroStack.size())
562 MaxIncludeStackDepth = IncludeMacroStack.size();
563
Chris Lattner25bdb512007-07-20 16:52:03 +0000564 Lexer *TheLexer = new Lexer(SourceLocation::getFileLoc(FileID, 0), *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 EnterSourceFileWithLexer(TheLexer, CurDir);
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.
570void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
571 const DirectoryLookup *CurDir) {
572
573 // Add the current lexer to the include stack.
574 if (CurLexer || CurMacroExpander)
575 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
576 CurMacroExpander));
577
578 CurLexer = TheLexer;
579 CurDirLookup = CurDir;
580 CurMacroExpander = 0;
581
582 // Notify the client, if desired, that we are in a new source file.
583 if (Callbacks && !CurLexer->Is_PragmaLexer) {
584 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
585
586 // Get the file entry for the current file.
587 if (const FileEntry *FE =
Chris Lattner9dc1f532007-07-20 16:37:10 +0000588 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 FileType = HeaderInfo.getFileDirFlavor(FE);
590
Chris Lattner9dc1f532007-07-20 16:37:10 +0000591 Callbacks->FileChanged(CurLexer->getFileLoc(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 PPCallbacks::EnterFile, FileType);
593 }
594}
595
596
597
598/// EnterMacro - Add a Macro to the top of the include stack and start lexing
599/// tokens from it instead of the current buffer.
Chris Lattnerd2177732007-07-20 16:59:19 +0000600void Preprocessor::EnterMacro(Token &Tok, MacroArgs *Args) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000601 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
602 CurMacroExpander));
603 CurLexer = 0;
604 CurDirLookup = 0;
605
Chris Lattner9594acf2007-07-15 00:25:26 +0000606 if (NumCachedMacroExpanders == 0) {
607 CurMacroExpander = new MacroExpander(Tok, Args, *this);
608 } else {
609 CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
610 CurMacroExpander->Init(Tok, Args);
611 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000612}
613
614/// EnterTokenStream - Add a "macro" context to the top of the include stack,
615/// which will cause the lexer to start returning the specified tokens. Note
616/// that these tokens will be re-macro-expanded when/if expansion is enabled.
617/// This method assumes that the specified stream of tokens has a permanent
618/// owner somewhere, so they do not need to be copied.
Chris Lattnerd2177732007-07-20 16:59:19 +0000619void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 // Save our current state.
621 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
622 CurMacroExpander));
623 CurLexer = 0;
624 CurDirLookup = 0;
625
626 // Create a macro expander to expand from the specified token stream.
Chris Lattner9594acf2007-07-15 00:25:26 +0000627 if (NumCachedMacroExpanders == 0) {
628 CurMacroExpander = new MacroExpander(Toks, NumToks, *this);
629 } else {
630 CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
631 CurMacroExpander->Init(Toks, NumToks);
632 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000633}
634
635/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
636/// lexer stack. This should only be used in situations where the current
637/// state of the top-of-stack lexer is known.
638void Preprocessor::RemoveTopOfLexerStack() {
639 assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
Chris Lattner9594acf2007-07-15 00:25:26 +0000640
641 if (CurMacroExpander) {
642 // Delete or cache the now-dead macro expander.
643 if (NumCachedMacroExpanders == MacroExpanderCacheSize)
644 delete CurMacroExpander;
645 else
646 MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander;
647 } else {
648 delete CurLexer;
649 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 CurLexer = IncludeMacroStack.back().TheLexer;
651 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
652 CurMacroExpander = IncludeMacroStack.back().TheMacroExpander;
653 IncludeMacroStack.pop_back();
654}
655
656//===----------------------------------------------------------------------===//
657// Macro Expansion Handling.
658//===----------------------------------------------------------------------===//
659
Chris Lattnercc1a8752007-10-07 08:44:20 +0000660/// setMacroInfo - Specify a macro for this identifier.
661///
662void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
663 if (MI == 0) {
664 if (II->hasMacroDefinition()) {
665 Macros.erase(II);
666 II->setHasMacroDefinition(false);
667 }
668 } else {
669 Macros[II] = MI;
670 II->setHasMacroDefinition(true);
671 }
672}
673
Reid Spencer5f016e22007-07-11 17:01:13 +0000674/// RegisterBuiltinMacro - Register the specified identifier in the identifier
675/// table and mark it as a builtin macro to be expanded.
676IdentifierInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) {
677 // Get the identifier.
678 IdentifierInfo *Id = getIdentifierInfo(Name);
679
680 // Mark it as being a macro that is builtin.
681 MacroInfo *MI = new MacroInfo(SourceLocation());
682 MI->setIsBuiltinMacro();
Chris Lattnercc1a8752007-10-07 08:44:20 +0000683 setMacroInfo(Id, MI);
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 return Id;
685}
686
687
688/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
689/// identifier table.
690void Preprocessor::RegisterBuiltinMacros() {
691 Ident__LINE__ = RegisterBuiltinMacro("__LINE__");
692 Ident__FILE__ = RegisterBuiltinMacro("__FILE__");
693 Ident__DATE__ = RegisterBuiltinMacro("__DATE__");
694 Ident__TIME__ = RegisterBuiltinMacro("__TIME__");
695 Ident_Pragma = RegisterBuiltinMacro("_Pragma");
696
697 // GCC Extensions.
698 Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__");
699 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__");
700 Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__");
701}
702
703/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
704/// in its expansion, currently expands to that token literally.
705static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
Chris Lattnercc1a8752007-10-07 08:44:20 +0000706 const IdentifierInfo *MacroIdent,
707 Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000708 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
709
710 // If the token isn't an identifier, it's always literally expanded.
711 if (II == 0) return true;
712
713 // If the identifier is a macro, and if that macro is enabled, it may be
714 // expanded so it's not a trivial expansion.
Chris Lattnercc1a8752007-10-07 08:44:20 +0000715 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 // Fast expanding "#define X X" is ok, because X would be disabled.
717 II != MacroIdent)
718 return false;
719
720 // If this is an object-like macro invocation, it is safe to trivially expand
721 // it.
722 if (MI->isObjectLike()) return true;
723
724 // If this is a function-like macro invocation, it's safe to trivially expand
725 // as long as the identifier is not a macro argument.
726 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
727 I != E; ++I)
728 if (*I == II)
729 return false; // Identifier is a macro argument.
730
731 return true;
732}
733
734
735/// isNextPPTokenLParen - Determine whether the next preprocessor token to be
736/// lexed is a '('. If so, consume the token and return true, if not, this
737/// method should have no observable side-effect on the lexed tokens.
738bool Preprocessor::isNextPPTokenLParen() {
739 // Do some quick tests for rejection cases.
740 unsigned Val;
741 if (CurLexer)
742 Val = CurLexer->isNextPPTokenLParen();
743 else
744 Val = CurMacroExpander->isNextTokenLParen();
745
746 if (Val == 2) {
Chris Lattner0ea793e2007-07-19 00:07:36 +0000747 // We have run off the end. If it's a source file we don't
748 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
749 // macro stack.
750 if (CurLexer)
751 return false;
752 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 IncludeStackInfo &Entry = IncludeMacroStack[i-1];
754 if (Entry.TheLexer)
755 Val = Entry.TheLexer->isNextPPTokenLParen();
756 else
757 Val = Entry.TheMacroExpander->isNextTokenLParen();
Chris Lattner0ea793e2007-07-19 00:07:36 +0000758
759 if (Val != 2)
760 break;
761
762 // Ran off the end of a source file?
763 if (Entry.TheLexer)
764 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000765 }
766 }
767
768 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
769 // have found something that isn't a '(' or we found the end of the
770 // translation unit. In either case, return false.
771 if (Val != 1)
772 return false;
773
Chris Lattnerd2177732007-07-20 16:59:19 +0000774 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 LexUnexpandedToken(Tok);
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000776 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000777 return true;
778}
779
780/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
781/// expanded as a macro, handle it and return the next token as 'Identifier'.
Chris Lattnerd2177732007-07-20 16:59:19 +0000782bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
Reid Spencer5f016e22007-07-11 17:01:13 +0000783 MacroInfo *MI) {
784
785 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
786 if (MI->isBuiltinMacro()) {
787 ExpandBuiltinMacro(Identifier);
788 return false;
789 }
790
791 // If this is the first use of a target-specific macro, warn about it.
792 if (MI->isTargetSpecific()) {
793 MI->setIsTargetSpecific(false); // Don't warn on second use.
794 getTargetInfo().DiagnoseNonPortability(Identifier.getLocation(),
795 diag::port_target_macro_use);
796 }
797
798 /// Args - If this is a function-like macro expansion, this contains,
799 /// for each macro argument, the list of tokens that were provided to the
800 /// invocation.
801 MacroArgs *Args = 0;
802
803 // If this is a function-like macro, read the arguments.
804 if (MI->isFunctionLike()) {
805 // C99 6.10.3p10: If the preprocessing token immediately after the the macro
Chris Lattner2b64fdc2007-07-19 16:11:58 +0000806 // name isn't a '(', this macro should not be expanded. Otherwise, consume
807 // it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000808 if (!isNextPPTokenLParen())
809 return true;
810
811 // Remember that we are now parsing the arguments to a macro invocation.
812 // Preprocessor directives used inside macro arguments are not portable, and
813 // this enables the warning.
814 InMacroArgs = true;
815 Args = ReadFunctionLikeMacroArgs(Identifier, MI);
816
817 // Finished parsing args.
818 InMacroArgs = false;
819
820 // If there was an error parsing the arguments, bail out.
821 if (Args == 0) return false;
822
823 ++NumFnMacroExpanded;
824 } else {
825 ++NumMacroExpanded;
826 }
827
828 // Notice that this macro has been used.
829 MI->setIsUsed(true);
830
831 // If we started lexing a macro, enter the macro expansion body.
832
833 // If this macro expands to no tokens, don't bother to push it onto the
834 // expansion stack, only to take it right back off.
835 if (MI->getNumTokens() == 0) {
836 // No need for arg info.
837 if (Args) Args->destroy();
838
839 // Ignore this macro use, just return the next token in the current
840 // buffer.
841 bool HadLeadingSpace = Identifier.hasLeadingSpace();
842 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
843
844 Lex(Identifier);
845
846 // If the identifier isn't on some OTHER line, inherit the leading
847 // whitespace/first-on-a-line property of this token. This handles
848 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
849 // empty.
850 if (!Identifier.isAtStartOfLine()) {
Chris Lattnerd2177732007-07-20 16:59:19 +0000851 if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
852 if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 }
854 ++NumFastMacroExpanded;
855 return false;
856
857 } else if (MI->getNumTokens() == 1 &&
Chris Lattnercc1a8752007-10-07 08:44:20 +0000858 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
859 *this)){
Reid Spencer5f016e22007-07-11 17:01:13 +0000860 // Otherwise, if this macro expands into a single trivially-expanded
861 // token: expand it now. This handles common cases like
862 // "#define VAL 42".
863
864 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
865 // identifier to the expanded token.
866 bool isAtStartOfLine = Identifier.isAtStartOfLine();
867 bool hasLeadingSpace = Identifier.hasLeadingSpace();
868
869 // Remember where the token is instantiated.
870 SourceLocation InstantiateLoc = Identifier.getLocation();
871
872 // Replace the result token.
873 Identifier = MI->getReplacementToken(0);
874
875 // Restore the StartOfLine/LeadingSpace markers.
Chris Lattnerd2177732007-07-20 16:59:19 +0000876 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
877 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000878
879 // Update the tokens location to include both its logical and physical
880 // locations.
881 SourceLocation Loc =
882 SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc);
883 Identifier.setLocation(Loc);
884
885 // If this is #define X X, we must mark the result as unexpandible.
886 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo())
Chris Lattnercc1a8752007-10-07 08:44:20 +0000887 if (getMacroInfo(NewII) == MI)
Chris Lattnerd2177732007-07-20 16:59:19 +0000888 Identifier.setFlag(Token::DisableExpand);
Reid Spencer5f016e22007-07-11 17:01:13 +0000889
890 // Since this is not an identifier token, it can't be macro expanded, so
891 // we're done.
892 ++NumFastMacroExpanded;
893 return false;
894 }
895
896 // Start expanding the macro.
897 EnterMacro(Identifier, Args);
898
899 // Now that the macro is at the top of the include stack, ask the
900 // preprocessor to read the next token from it.
901 Lex(Identifier);
902 return false;
903}
904
905/// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
906/// invoked to read all of the actual arguments specified for the macro
907/// invocation. This returns null on error.
Chris Lattnerd2177732007-07-20 16:59:19 +0000908MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
Reid Spencer5f016e22007-07-11 17:01:13 +0000909 MacroInfo *MI) {
910 // The number of fixed arguments to parse.
911 unsigned NumFixedArgsLeft = MI->getNumArgs();
912 bool isVariadic = MI->isVariadic();
913
914 // Outer loop, while there are more arguments, keep reading them.
Chris Lattnerd2177732007-07-20 16:59:19 +0000915 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000916 Tok.setKind(tok::comma);
917 --NumFixedArgsLeft; // Start reading the first arg.
918
919 // ArgTokens - Build up a list of tokens that make up each argument. Each
920 // argument is separated by an EOF token. Use a SmallVector so we can avoid
921 // heap allocations in the common case.
Chris Lattnerd2177732007-07-20 16:59:19 +0000922 llvm::SmallVector<Token, 64> ArgTokens;
Reid Spencer5f016e22007-07-11 17:01:13 +0000923
924 unsigned NumActuals = 0;
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000925 while (Tok.is(tok::comma)) {
Chris Lattner2b64fdc2007-07-19 16:11:58 +0000926 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
927 // that we already consumed the first one.
Reid Spencer5f016e22007-07-11 17:01:13 +0000928 unsigned NumParens = 0;
929
930 while (1) {
931 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
932 // an argument value in a macro could expand to ',' or '(' or ')'.
933 LexUnexpandedToken(Tok);
934
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000935 if (Tok.is(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000936 Diag(MacroName, diag::err_unterm_macro_invoc);
937 // Do not lose the EOF. Return it to the client.
938 MacroName = Tok;
939 return 0;
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000940 } else if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000941 // If we found the ) token, the macro arg list is done.
942 if (NumParens-- == 0)
943 break;
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000944 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 ++NumParens;
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000946 } else if (Tok.is(tok::comma) && NumParens == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000947 // Comma ends this argument if there are more fixed arguments expected.
948 if (NumFixedArgsLeft)
949 break;
950
951 // If this is not a variadic macro, too many args were specified.
952 if (!isVariadic) {
953 // Emit the diagnostic at the macro name in case there is a missing ).
954 // Emitting it at the , could be far away from the macro name.
955 Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
956 return 0;
957 }
958 // Otherwise, continue to add the tokens to this variable argument.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000959 } else if (Tok.is(tok::comment) && !KeepMacroComments) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 // If this is a comment token in the argument list and we're just in
961 // -C mode (not -CC mode), discard the comment.
962 continue;
Chris Lattner0c3eb292007-11-23 06:50:21 +0000963 } else if (Tok.is(tok::identifier)) {
964 // Reading macro arguments can cause macros that we are currently
965 // expanding from to be popped off the expansion stack. Doing so causes
966 // them to be reenabled for expansion. Here we record whether any
967 // identifiers we lex as macro arguments correspond to disabled macros.
968 // If so, we mark the token as noexpand. This is a subtle aspect of
969 // C99 6.10.3.4p2.
970 if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
971 if (!MI->isEnabled())
972 Tok.setFlag(Token::DisableExpand);
Reid Spencer5f016e22007-07-11 17:01:13 +0000973 }
974
975 ArgTokens.push_back(Tok);
976 }
977
978 // Empty arguments are standard in C99 and supported as an extension in
979 // other modes.
980 if (ArgTokens.empty() && !Features.C99)
981 Diag(Tok, diag::ext_empty_fnmacro_arg);
982
983 // Add a marker EOF token to the end of the token list for this argument.
Chris Lattnerd2177732007-07-20 16:59:19 +0000984 Token EOFTok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 EOFTok.startToken();
986 EOFTok.setKind(tok::eof);
987 EOFTok.setLocation(Tok.getLocation());
988 EOFTok.setLength(0);
989 ArgTokens.push_back(EOFTok);
990 ++NumActuals;
991 --NumFixedArgsLeft;
992 };
993
994 // Okay, we either found the r_paren. Check to see if we parsed too few
995 // arguments.
996 unsigned MinArgsExpected = MI->getNumArgs();
997
998 // See MacroArgs instance var for description of this.
999 bool isVarargsElided = false;
1000
1001 if (NumActuals < MinArgsExpected) {
1002 // There are several cases where too few arguments is ok, handle them now.
1003 if (NumActuals+1 == MinArgsExpected && MI->isVariadic()) {
1004 // Varargs where the named vararg parameter is missing: ok as extension.
1005 // #define A(x, ...)
1006 // A("blah")
1007 Diag(Tok, diag::ext_missing_varargs_arg);
1008
1009 // Remember this occurred if this is a C99 macro invocation with at least
1010 // one actual argument.
1011 isVarargsElided = MI->isC99Varargs() && MI->getNumArgs() > 1;
1012 } else if (MI->getNumArgs() == 1) {
1013 // #define A(x)
1014 // A()
1015 // is ok because it is an empty argument.
1016
1017 // Empty arguments are standard in C99 and supported as an extension in
1018 // other modes.
1019 if (ArgTokens.empty() && !Features.C99)
1020 Diag(Tok, diag::ext_empty_fnmacro_arg);
1021 } else {
1022 // Otherwise, emit the error.
1023 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
1024 return 0;
1025 }
1026
1027 // Add a marker EOF token to the end of the token list for this argument.
1028 SourceLocation EndLoc = Tok.getLocation();
1029 Tok.startToken();
1030 Tok.setKind(tok::eof);
1031 Tok.setLocation(EndLoc);
1032 Tok.setLength(0);
1033 ArgTokens.push_back(Tok);
1034 }
1035
1036 return MacroArgs::create(MI, &ArgTokens[0], ArgTokens.size(),isVarargsElided);
1037}
1038
1039/// ComputeDATE_TIME - Compute the current time, enter it into the specified
1040/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
1041/// the identifier tokens inserted.
1042static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
1043 Preprocessor &PP) {
1044 time_t TT = time(0);
1045 struct tm *TM = localtime(&TT);
1046
1047 static const char * const Months[] = {
1048 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
1049 };
1050
1051 char TmpBuffer[100];
1052 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
1053 TM->tm_year+1900);
1054 DATELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
1055
1056 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
1057 TIMELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
1058}
1059
1060/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1061/// as a builtin macro, handle it and return the next token as 'Tok'.
Chris Lattnerd2177732007-07-20 16:59:19 +00001062void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001063 // Figure out which token this is.
1064 IdentifierInfo *II = Tok.getIdentifierInfo();
1065 assert(II && "Can't be a macro without id info!");
1066
1067 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
1068 // lex the token after it.
1069 if (II == Ident_Pragma)
1070 return Handle_Pragma(Tok);
1071
1072 ++NumBuiltinMacroExpanded;
1073
1074 char TmpBuffer[100];
1075
1076 // Set up the return result.
1077 Tok.setIdentifierInfo(0);
Chris Lattnerd2177732007-07-20 16:59:19 +00001078 Tok.clearFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +00001079
1080 if (II == Ident__LINE__) {
1081 // __LINE__ expands to a simple numeric value.
Chris Lattner9dc1f532007-07-20 16:37:10 +00001082 sprintf(TmpBuffer, "%u", SourceMgr.getLogicalLineNumber(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 unsigned Length = strlen(TmpBuffer);
1084 Tok.setKind(tok::numeric_constant);
1085 Tok.setLength(Length);
1086 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
1087 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
1088 SourceLocation Loc = Tok.getLocation();
1089 if (II == Ident__BASE_FILE__) {
1090 Diag(Tok, diag::ext_pp_base_file);
Chris Lattner9dc1f532007-07-20 16:37:10 +00001091 SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc);
1092 while (NextLoc.isValid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 Loc = NextLoc;
Chris Lattner9dc1f532007-07-20 16:37:10 +00001094 NextLoc = SourceMgr.getIncludeLoc(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001095 }
1096 }
1097
1098 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
Chris Lattner9dc1f532007-07-20 16:37:10 +00001099 std::string FN = SourceMgr.getSourceName(SourceMgr.getLogicalLoc(Loc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 FN = '"' + Lexer::Stringify(FN) + '"';
1101 Tok.setKind(tok::string_literal);
1102 Tok.setLength(FN.size());
1103 Tok.setLocation(CreateString(&FN[0], FN.size(), Tok.getLocation()));
1104 } else if (II == Ident__DATE__) {
1105 if (!DATELoc.isValid())
1106 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1107 Tok.setKind(tok::string_literal);
1108 Tok.setLength(strlen("\"Mmm dd yyyy\""));
1109 Tok.setLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation()));
1110 } else if (II == Ident__TIME__) {
1111 if (!TIMELoc.isValid())
1112 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1113 Tok.setKind(tok::string_literal);
1114 Tok.setLength(strlen("\"hh:mm:ss\""));
1115 Tok.setLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
1116 } else if (II == Ident__INCLUDE_LEVEL__) {
1117 Diag(Tok, diag::ext_pp_include_level);
1118
1119 // Compute the include depth of this token.
1120 unsigned Depth = 0;
Chris Lattner9dc1f532007-07-20 16:37:10 +00001121 SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation());
1122 for (; Loc.isValid(); ++Depth)
1123 Loc = SourceMgr.getIncludeLoc(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001124
1125 // __INCLUDE_LEVEL__ expands to a simple numeric value.
1126 sprintf(TmpBuffer, "%u", Depth);
1127 unsigned Length = strlen(TmpBuffer);
1128 Tok.setKind(tok::numeric_constant);
1129 Tok.setLength(Length);
1130 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
1131 } else if (II == Ident__TIMESTAMP__) {
1132 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
1133 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1134 Diag(Tok, diag::ext_pp_timestamp);
1135
1136 // Get the file that we are lexing out of. If we're currently lexing from
1137 // a macro, dig into the include stack.
1138 const FileEntry *CurFile = 0;
1139 Lexer *TheLexer = getCurrentFileLexer();
1140
1141 if (TheLexer)
Chris Lattner9dc1f532007-07-20 16:37:10 +00001142 CurFile = SourceMgr.getFileEntryForLoc(TheLexer->getFileLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +00001143
1144 // If this file is older than the file it depends on, emit a diagnostic.
1145 const char *Result;
1146 if (CurFile) {
1147 time_t TT = CurFile->getModificationTime();
1148 struct tm *TM = localtime(&TT);
1149 Result = asctime(TM);
1150 } else {
1151 Result = "??? ??? ?? ??:??:?? ????\n";
1152 }
1153 TmpBuffer[0] = '"';
1154 strcpy(TmpBuffer+1, Result);
1155 unsigned Len = strlen(TmpBuffer);
1156 TmpBuffer[Len-1] = '"'; // Replace the newline with a quote.
1157 Tok.setKind(tok::string_literal);
1158 Tok.setLength(Len);
1159 Tok.setLocation(CreateString(TmpBuffer, Len, Tok.getLocation()));
1160 } else {
1161 assert(0 && "Unknown identifier!");
Chris Lattnerc3d8d572007-12-09 20:31:55 +00001162 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001163}
1164
1165//===----------------------------------------------------------------------===//
1166// Lexer Event Handling.
1167//===----------------------------------------------------------------------===//
1168
1169/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
1170/// identifier information for the token and install it into the token.
Chris Lattnerd2177732007-07-20 16:59:19 +00001171IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 const char *BufPtr) {
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001173 assert(Identifier.is(tok::identifier) && "Not an identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001174 assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
1175
1176 // Look up this token, see if it is a macro, or if it is a language keyword.
1177 IdentifierInfo *II;
1178 if (BufPtr && !Identifier.needsCleaning()) {
1179 // No cleaning needed, just use the characters from the lexed buffer.
1180 II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
1181 } else {
1182 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
Chris Lattnerc35717a2007-07-13 17:10:38 +00001183 llvm::SmallVector<char, 64> IdentifierBuffer;
1184 IdentifierBuffer.resize(Identifier.getLength());
1185 const char *TmpBuf = &IdentifierBuffer[0];
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 unsigned Size = getSpelling(Identifier, TmpBuf);
1187 II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
1188 }
1189 Identifier.setIdentifierInfo(II);
1190 return II;
1191}
1192
1193
1194/// HandleIdentifier - This callback is invoked when the lexer reads an
1195/// identifier. This callback looks up the identifier in the map and/or
1196/// potentially macro expands it or turns it into a named token (like 'for').
Chris Lattnerd2177732007-07-20 16:59:19 +00001197void Preprocessor::HandleIdentifier(Token &Identifier) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 assert(Identifier.getIdentifierInfo() &&
1199 "Can't handle identifiers without identifier info!");
1200
1201 IdentifierInfo &II = *Identifier.getIdentifierInfo();
1202
1203 // If this identifier was poisoned, and if it was not produced from a macro
1204 // expansion, emit an error.
1205 if (II.isPoisoned() && CurLexer) {
1206 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
1207 Diag(Identifier, diag::err_pp_used_poisoned_id);
1208 else
1209 Diag(Identifier, diag::ext_pp_bad_vaargs_use);
1210 }
1211
1212 // If this is a macro to be expanded, do it.
Chris Lattnercc1a8752007-10-07 08:44:20 +00001213 if (MacroInfo *MI = getMacroInfo(&II)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
1215 if (MI->isEnabled()) {
1216 if (!HandleMacroExpandedIdentifier(Identifier, MI))
1217 return;
1218 } else {
1219 // C99 6.10.3.4p2 says that a disabled macro may never again be
1220 // expanded, even if it's in a context where it could be expanded in the
1221 // future.
Chris Lattnerd2177732007-07-20 16:59:19 +00001222 Identifier.setFlag(Token::DisableExpand);
Reid Spencer5f016e22007-07-11 17:01:13 +00001223 }
1224 }
1225 } else if (II.isOtherTargetMacro() && !DisableMacroExpansion) {
1226 // If this identifier is a macro on some other target, emit a diagnostic.
1227 // This diagnosic is only emitted when macro expansion is enabled, because
1228 // the macro would not have been expanded for the other target either.
1229 II.setIsOtherTargetMacro(false); // Don't warn on second use.
1230 getTargetInfo().DiagnoseNonPortability(Identifier.getLocation(),
1231 diag::port_target_macro_use);
1232
1233 }
1234
1235 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
1236 // then we act as if it is the actual operator and not the textual
1237 // representation of it.
1238 if (II.isCPlusPlusOperatorKeyword())
1239 Identifier.setIdentifierInfo(0);
1240
1241 // Change the kind of this identifier to the appropriate token kind, e.g.
1242 // turning "for" into a keyword.
1243 Identifier.setKind(II.getTokenID());
1244
1245 // If this is an extension token, diagnose its use.
1246 // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99
1247 // For now, I'm just commenting it out (while I work on attributes).
1248 if (II.isExtensionToken() && Features.C99)
1249 Diag(Identifier, diag::ext_token_used);
1250}
1251
1252/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
1253/// the current file. This either returns the EOF token or pops a level off
1254/// the include stack and keeps going.
Chris Lattnerd2177732007-07-20 16:59:19 +00001255bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 assert(!CurMacroExpander &&
1257 "Ending a file when currently in a macro!");
1258
1259 // See if this file had a controlling macro.
1260 if (CurLexer) { // Not ending a macro, ignore it.
1261 if (const IdentifierInfo *ControllingMacro =
1262 CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
1263 // Okay, this has a controlling macro, remember in PerFileInfo.
1264 if (const FileEntry *FE =
Chris Lattner9dc1f532007-07-20 16:37:10 +00001265 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
1267 }
1268 }
1269
1270 // If this is a #include'd file, pop it off the include stack and continue
1271 // lexing the #includer file.
1272 if (!IncludeMacroStack.empty()) {
1273 // We're done with the #included file.
1274 RemoveTopOfLexerStack();
1275
1276 // Notify the client, if desired, that we are in a new source file.
1277 if (Callbacks && !isEndOfMacro && CurLexer) {
1278 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
1279
1280 // Get the file entry for the current file.
1281 if (const FileEntry *FE =
Chris Lattner9dc1f532007-07-20 16:37:10 +00001282 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 FileType = HeaderInfo.getFileDirFlavor(FE);
1284
1285 Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr),
1286 PPCallbacks::ExitFile, FileType);
1287 }
1288
1289 // Client should lex another token.
1290 return false;
1291 }
1292
1293 Result.startToken();
1294 CurLexer->BufferPtr = CurLexer->BufferEnd;
1295 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
1296 Result.setKind(tok::eof);
1297
1298 // We're done with the #included file.
1299 delete CurLexer;
1300 CurLexer = 0;
1301
1302 // This is the end of the top-level file. If the diag::pp_macro_not_used
Chris Lattnercc1a8752007-10-07 08:44:20 +00001303 // diagnostic is enabled, look for macros that have not been used.
Reid Spencer5f016e22007-07-11 17:01:13 +00001304 if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){
Chris Lattnercc1a8752007-10-07 08:44:20 +00001305 for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
1306 Macros.begin(), E = Macros.end(); I != E; ++I) {
1307 if (!I->second->isUsed())
1308 Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used);
Reid Spencer5f016e22007-07-11 17:01:13 +00001309 }
1310 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001311 return true;
1312}
1313
1314/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
1315/// the current macro expansion or token stream expansion.
Chris Lattnerd2177732007-07-20 16:59:19 +00001316bool Preprocessor::HandleEndOfMacro(Token &Result) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001317 assert(CurMacroExpander && !CurLexer &&
1318 "Ending a macro when currently in a #include file!");
1319
Chris Lattner9594acf2007-07-15 00:25:26 +00001320 // Delete or cache the now-dead macro expander.
1321 if (NumCachedMacroExpanders == MacroExpanderCacheSize)
1322 delete CurMacroExpander;
1323 else
1324 MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander;
Reid Spencer5f016e22007-07-11 17:01:13 +00001325
1326 // Handle this like a #include file being popped off the stack.
1327 CurMacroExpander = 0;
1328 return HandleEndOfFile(Result, true);
1329}
1330
1331
1332//===----------------------------------------------------------------------===//
1333// Utility Methods for Preprocessor Directive Handling.
1334//===----------------------------------------------------------------------===//
1335
1336/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
1337/// current line until the tok::eom token is found.
1338void Preprocessor::DiscardUntilEndOfDirective() {
Chris Lattnerd2177732007-07-20 16:59:19 +00001339 Token Tmp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001340 do {
1341 LexUnexpandedToken(Tmp);
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001342 } while (Tmp.isNot(tok::eom));
Reid Spencer5f016e22007-07-11 17:01:13 +00001343}
1344
1345/// isCXXNamedOperator - Returns "true" if the token is a named operator in C++.
1346static bool isCXXNamedOperator(const std::string &Spelling) {
1347 return Spelling == "and" || Spelling == "bitand" || Spelling == "bitor" ||
1348 Spelling == "compl" || Spelling == "not" || Spelling == "not_eq" ||
1349 Spelling == "or" || Spelling == "xor";
1350}
1351
1352/// ReadMacroName - Lex and validate a macro name, which occurs after a
1353/// #define or #undef. This sets the token kind to eom and discards the rest
1354/// of the macro line if the macro name is invalid. isDefineUndef is 1 if
1355/// this is due to a a #define, 2 if #undef directive, 0 if it is something
1356/// else (e.g. #ifdef).
Chris Lattnerd2177732007-07-20 16:59:19 +00001357void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001358 // Read the token, don't allow macro expansion on it.
1359 LexUnexpandedToken(MacroNameTok);
1360
1361 // Missing macro name?
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001362 if (MacroNameTok.is(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +00001363 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
1364
1365 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1366 if (II == 0) {
1367 std::string Spelling = getSpelling(MacroNameTok);
1368 if (isCXXNamedOperator(Spelling))
1369 // C++ 2.5p2: Alternative tokens behave the same as its primary token
1370 // except for their spellings.
1371 Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name, Spelling);
1372 else
1373 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
1374 // Fall through on error.
1375 } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
1376 // Error if defining "defined": C99 6.10.8.4.
1377 Diag(MacroNameTok, diag::err_defined_macro_name);
Chris Lattner0edde552007-10-07 08:04:56 +00001378 } else if (isDefineUndef && II->hasMacroDefinition() &&
Chris Lattnercc1a8752007-10-07 08:44:20 +00001379 getMacroInfo(II)->isBuiltinMacro()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001380 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
1381 if (isDefineUndef == 1)
1382 Diag(MacroNameTok, diag::pp_redef_builtin_macro);
1383 else
1384 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
1385 } else {
1386 // Okay, we got a good identifier node. Return it.
1387 return;
1388 }
1389
1390 // Invalid macro name, read and discard the rest of the line. Then set the
1391 // token kind to tok::eom.
1392 MacroNameTok.setKind(tok::eom);
1393 return DiscardUntilEndOfDirective();
1394}
1395
1396/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
1397/// not, emit a diagnostic and consume up until the eom.
1398void Preprocessor::CheckEndOfDirective(const char *DirType) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001399 Token Tmp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001400 Lex(Tmp);
1401 // There should be no tokens after the directive, but we allow them as an
1402 // extension.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001403 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
Reid Spencer5f016e22007-07-11 17:01:13 +00001404 Lex(Tmp);
1405
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001406 if (Tmp.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001407 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
1408 DiscardUntilEndOfDirective();
1409 }
1410}
1411
1412
1413
1414/// SkipExcludedConditionalBlock - We just read a #if or related directive and
1415/// decided that the subsequent tokens are in the #if'd out portion of the
1416/// file. Lex the rest of the file, until we see an #endif. If
1417/// FoundNonSkipPortion is true, then we have already emitted code for part of
1418/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
1419/// is true, then #else directives are ok, if not, then we have already seen one
1420/// so a #else directive is a duplicate. When this returns, the caller can lex
1421/// the first valid token.
1422void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
1423 bool FoundNonSkipPortion,
1424 bool FoundElse) {
1425 ++NumSkipped;
1426 assert(CurMacroExpander == 0 && CurLexer &&
1427 "Lexing a macro, not a file?");
1428
1429 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
1430 FoundNonSkipPortion, FoundElse);
1431
1432 // Enter raw mode to disable identifier lookup (and thus macro expansion),
1433 // disabling warnings, etc.
1434 CurLexer->LexingRawMode = true;
Chris Lattnerd2177732007-07-20 16:59:19 +00001435 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001436 while (1) {
1437 CurLexer->Lex(Tok);
1438
1439 // If this is the end of the buffer, we have an error.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001440 if (Tok.is(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001441 // Emit errors for each unterminated conditional on the stack, including
1442 // the current one.
1443 while (!CurLexer->ConditionalStack.empty()) {
1444 Diag(CurLexer->ConditionalStack.back().IfLoc,
1445 diag::err_pp_unterminated_conditional);
1446 CurLexer->ConditionalStack.pop_back();
1447 }
1448
1449 // Just return and let the caller lex after this #include.
1450 break;
1451 }
1452
1453 // If this token is not a preprocessor directive, just skip it.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001454 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
Reid Spencer5f016e22007-07-11 17:01:13 +00001455 continue;
1456
1457 // We just parsed a # character at the start of a line, so we're in
1458 // directive mode. Tell the lexer this so any newlines we see will be
1459 // converted into an EOM token (this terminates the macro).
1460 CurLexer->ParsingPreprocessorDirective = true;
1461 CurLexer->KeepCommentMode = false;
1462
1463
1464 // Read the next token, the directive flavor.
1465 LexUnexpandedToken(Tok);
1466
1467 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
1468 // something bogus), skip it.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001469 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001470 CurLexer->ParsingPreprocessorDirective = false;
1471 // Restore comment saving mode.
1472 CurLexer->KeepCommentMode = KeepComments;
1473 continue;
1474 }
1475
1476 // If the first letter isn't i or e, it isn't intesting to us. We know that
1477 // this is safe in the face of spelling differences, because there is no way
1478 // to spell an i/e in a strange way that is another letter. Skipping this
1479 // allows us to avoid looking up the identifier info for #define/#undef and
1480 // other common directives.
1481 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
1482 char FirstChar = RawCharData[0];
1483 if (FirstChar >= 'a' && FirstChar <= 'z' &&
1484 FirstChar != 'i' && FirstChar != 'e') {
1485 CurLexer->ParsingPreprocessorDirective = false;
1486 // Restore comment saving mode.
1487 CurLexer->KeepCommentMode = KeepComments;
1488 continue;
1489 }
1490
1491 // Get the identifier name without trigraphs or embedded newlines. Note
1492 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
1493 // when skipping.
1494 // TODO: could do this with zero copies in the no-clean case by using
1495 // strncmp below.
1496 char Directive[20];
1497 unsigned IdLen;
1498 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
1499 IdLen = Tok.getLength();
1500 memcpy(Directive, RawCharData, IdLen);
1501 Directive[IdLen] = 0;
1502 } else {
1503 std::string DirectiveStr = getSpelling(Tok);
1504 IdLen = DirectiveStr.size();
1505 if (IdLen >= 20) {
1506 CurLexer->ParsingPreprocessorDirective = false;
1507 // Restore comment saving mode.
1508 CurLexer->KeepCommentMode = KeepComments;
1509 continue;
1510 }
1511 memcpy(Directive, &DirectiveStr[0], IdLen);
1512 Directive[IdLen] = 0;
1513 }
1514
1515 if (FirstChar == 'i' && Directive[1] == 'f') {
1516 if ((IdLen == 2) || // "if"
1517 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
1518 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
1519 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
1520 // bother parsing the condition.
1521 DiscardUntilEndOfDirective();
1522 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
1523 /*foundnonskip*/false,
1524 /*fnddelse*/false);
1525 }
1526 } else if (FirstChar == 'e') {
1527 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
1528 CheckEndOfDirective("#endif");
1529 PPConditionalInfo CondInfo;
1530 CondInfo.WasSkipping = true; // Silence bogus warning.
1531 bool InCond = CurLexer->popConditionalLevel(CondInfo);
1532 InCond = InCond; // Silence warning in no-asserts mode.
1533 assert(!InCond && "Can't be skipping if not in a conditional!");
1534
1535 // If we popped the outermost skipping block, we're done skipping!
1536 if (!CondInfo.WasSkipping)
1537 break;
1538 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
1539 // #else directive in a skipping conditional. If not in some other
1540 // skipping conditional, and if #else hasn't already been seen, enter it
1541 // as a non-skipping conditional.
1542 CheckEndOfDirective("#else");
1543 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1544
1545 // If this is a #else with a #else before it, report the error.
1546 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
1547
1548 // Note that we've seen a #else in this conditional.
1549 CondInfo.FoundElse = true;
1550
1551 // If the conditional is at the top level, and the #if block wasn't
1552 // entered, enter the #else block now.
1553 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
1554 CondInfo.FoundNonSkip = true;
1555 break;
1556 }
1557 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
1558 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1559
1560 bool ShouldEnter;
1561 // If this is in a skipping block or if we're already handled this #if
1562 // block, don't bother parsing the condition.
1563 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
1564 DiscardUntilEndOfDirective();
1565 ShouldEnter = false;
1566 } else {
1567 // Restore the value of LexingRawMode so that identifiers are
1568 // looked up, etc, inside the #elif expression.
1569 assert(CurLexer->LexingRawMode && "We have to be skipping here!");
1570 CurLexer->LexingRawMode = false;
1571 IdentifierInfo *IfNDefMacro = 0;
1572 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
1573 CurLexer->LexingRawMode = true;
1574 }
1575
1576 // If this is a #elif with a #else before it, report the error.
1577 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
1578
1579 // If this condition is true, enter it!
1580 if (ShouldEnter) {
1581 CondInfo.FoundNonSkip = true;
1582 break;
1583 }
1584 }
1585 }
1586
1587 CurLexer->ParsingPreprocessorDirective = false;
1588 // Restore comment saving mode.
1589 CurLexer->KeepCommentMode = KeepComments;
1590 }
1591
1592 // Finally, if we are out of the conditional (saw an #endif or ran off the end
1593 // of the file, just stop skipping and return to lexing whatever came after
1594 // the #if block.
1595 CurLexer->LexingRawMode = false;
1596}
1597
1598//===----------------------------------------------------------------------===//
1599// Preprocessor Directive Handling.
1600//===----------------------------------------------------------------------===//
1601
1602/// HandleDirective - This callback is invoked when the lexer sees a # token
1603/// at the start of a line. This consumes the directive, modifies the
1604/// lexer/preprocessor state, and advances the lexer(s) so that the next token
1605/// read is the correct one.
Chris Lattnerd2177732007-07-20 16:59:19 +00001606void Preprocessor::HandleDirective(Token &Result) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001607 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
1608
1609 // We just parsed a # character at the start of a line, so we're in directive
1610 // mode. Tell the lexer this so any newlines we see will be converted into an
1611 // EOM token (which terminates the directive).
1612 CurLexer->ParsingPreprocessorDirective = true;
1613
1614 ++NumDirectives;
1615
1616 // We are about to read a token. For the multiple-include optimization FA to
1617 // work, we have to remember if we had read any tokens *before* this
1618 // pp-directive.
1619 bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal();
1620
1621 // Read the next token, the directive flavor. This isn't expanded due to
1622 // C99 6.10.3p8.
1623 LexUnexpandedToken(Result);
1624
1625 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
1626 // #define A(x) #x
1627 // A(abc
1628 // #warning blah
1629 // def)
1630 // If so, the user is relying on non-portable behavior, emit a diagnostic.
1631 if (InMacroArgs)
1632 Diag(Result, diag::ext_embedded_directive);
1633
1634TryAgain:
1635 switch (Result.getKind()) {
1636 case tok::eom:
1637 return; // null directive.
1638 case tok::comment:
1639 // Handle stuff like "# /*foo*/ define X" in -E -C mode.
1640 LexUnexpandedToken(Result);
1641 goto TryAgain;
1642
1643 case tok::numeric_constant:
1644 // FIXME: implement # 7 line numbers!
1645 DiscardUntilEndOfDirective();
1646 return;
1647 default:
1648 IdentifierInfo *II = Result.getIdentifierInfo();
1649 if (II == 0) break; // Not an identifier.
1650
1651 // Ask what the preprocessor keyword ID is.
1652 switch (II->getPPKeywordID()) {
1653 default: break;
1654 // C99 6.10.1 - Conditional Inclusion.
1655 case tok::pp_if:
1656 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
1657 case tok::pp_ifdef:
1658 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
1659 case tok::pp_ifndef:
1660 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
1661 case tok::pp_elif:
1662 return HandleElifDirective(Result);
1663 case tok::pp_else:
1664 return HandleElseDirective(Result);
1665 case tok::pp_endif:
1666 return HandleEndifDirective(Result);
1667
1668 // C99 6.10.2 - Source File Inclusion.
1669 case tok::pp_include:
1670 return HandleIncludeDirective(Result); // Handle #include.
1671
1672 // C99 6.10.3 - Macro Replacement.
1673 case tok::pp_define:
1674 return HandleDefineDirective(Result, false);
1675 case tok::pp_undef:
1676 return HandleUndefDirective(Result);
1677
1678 // C99 6.10.4 - Line Control.
1679 case tok::pp_line:
1680 // FIXME: implement #line
1681 DiscardUntilEndOfDirective();
1682 return;
1683
1684 // C99 6.10.5 - Error Directive.
1685 case tok::pp_error:
1686 return HandleUserDiagnosticDirective(Result, false);
1687
1688 // C99 6.10.6 - Pragma Directive.
1689 case tok::pp_pragma:
1690 return HandlePragmaDirective();
1691
1692 // GNU Extensions.
1693 case tok::pp_import:
1694 return HandleImportDirective(Result);
1695 case tok::pp_include_next:
1696 return HandleIncludeNextDirective(Result);
1697
1698 case tok::pp_warning:
1699 Diag(Result, diag::ext_pp_warning_directive);
1700 return HandleUserDiagnosticDirective(Result, true);
1701 case tok::pp_ident:
1702 return HandleIdentSCCSDirective(Result);
1703 case tok::pp_sccs:
1704 return HandleIdentSCCSDirective(Result);
1705 case tok::pp_assert:
1706 //isExtension = true; // FIXME: implement #assert
1707 break;
1708 case tok::pp_unassert:
1709 //isExtension = true; // FIXME: implement #unassert
1710 break;
1711
1712 // clang extensions.
1713 case tok::pp_define_target:
1714 return HandleDefineDirective(Result, true);
1715 case tok::pp_define_other_target:
1716 return HandleDefineOtherTargetDirective(Result);
1717 }
1718 break;
1719 }
1720
1721 // If we reached here, the preprocessing token is not valid!
1722 Diag(Result, diag::err_pp_invalid_directive);
1723
1724 // Read the rest of the PP line.
1725 DiscardUntilEndOfDirective();
1726
1727 // Okay, we're done parsing the directive.
1728}
1729
Chris Lattnerd2177732007-07-20 16:59:19 +00001730void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
Reid Spencer5f016e22007-07-11 17:01:13 +00001731 bool isWarning) {
1732 // Read the rest of the line raw. We do this because we don't want macros
1733 // to be expanded and we don't require that the tokens be valid preprocessing
1734 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1735 // collapse multiple consequtive white space between tokens, but this isn't
1736 // specified by the standard.
1737 std::string Message = CurLexer->ReadToEndOfLine();
1738
1739 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
1740 return Diag(Tok, DiagID, Message);
1741}
1742
1743/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1744///
Chris Lattnerd2177732007-07-20 16:59:19 +00001745void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001746 // Yes, this directive is an extension.
1747 Diag(Tok, diag::ext_pp_ident_directive);
1748
1749 // Read the string argument.
Chris Lattnerd2177732007-07-20 16:59:19 +00001750 Token StrTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001751 Lex(StrTok);
1752
1753 // If the token kind isn't a string, it's a malformed directive.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001754 if (StrTok.isNot(tok::string_literal) &&
1755 StrTok.isNot(tok::wide_string_literal))
Reid Spencer5f016e22007-07-11 17:01:13 +00001756 return Diag(StrTok, diag::err_pp_malformed_ident);
1757
1758 // Verify that there is nothing after the string, other than EOM.
1759 CheckEndOfDirective("#ident");
1760
1761 if (Callbacks)
1762 Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok));
1763}
1764
1765//===----------------------------------------------------------------------===//
1766// Preprocessor Include Directive Handling.
1767//===----------------------------------------------------------------------===//
1768
1769/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1770/// checked and spelled filename, e.g. as an operand of #include. This returns
1771/// true if the input filename was in <>'s or false if it were in ""'s. The
1772/// caller is expected to provide a buffer that is large enough to hold the
1773/// spelling of the filename, but is also expected to handle the case when
1774/// this method decides to use a different buffer.
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001775bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +00001776 const char *&BufStart,
1777 const char *&BufEnd) {
1778 // Get the text form of the filename.
Reid Spencer5f016e22007-07-11 17:01:13 +00001779 assert(BufStart != BufEnd && "Can't have tokens with empty spellings!");
1780
1781 // Make sure the filename is <x> or "x".
1782 bool isAngled;
1783 if (BufStart[0] == '<') {
1784 if (BufEnd[-1] != '>') {
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001785 Diag(Loc, diag::err_pp_expects_filename);
Reid Spencer5f016e22007-07-11 17:01:13 +00001786 BufStart = 0;
1787 return true;
1788 }
1789 isAngled = true;
1790 } else if (BufStart[0] == '"') {
1791 if (BufEnd[-1] != '"') {
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001792 Diag(Loc, diag::err_pp_expects_filename);
Reid Spencer5f016e22007-07-11 17:01:13 +00001793 BufStart = 0;
1794 return true;
1795 }
1796 isAngled = false;
1797 } else {
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001798 Diag(Loc, diag::err_pp_expects_filename);
Reid Spencer5f016e22007-07-11 17:01:13 +00001799 BufStart = 0;
1800 return true;
1801 }
1802
1803 // Diagnose #include "" as invalid.
1804 if (BufEnd-BufStart <= 2) {
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001805 Diag(Loc, diag::err_pp_empty_filename);
Reid Spencer5f016e22007-07-11 17:01:13 +00001806 BufStart = 0;
1807 return "";
1808 }
1809
1810 // Skip the brackets.
1811 ++BufStart;
1812 --BufEnd;
1813 return isAngled;
1814}
1815
Chris Lattner706ab502007-07-23 04:56:47 +00001816/// ConcatenateIncludeName - Handle cases where the #include name is expanded
1817/// from a macro as multiple tokens, which need to be glued together. This
1818/// occurs for code like:
1819/// #define FOO <a/b.h>
1820/// #include FOO
1821/// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1822///
1823/// This code concatenates and consumes tokens up to the '>' token. It returns
1824/// false if the > was found, otherwise it returns true if it finds and consumes
1825/// the EOM marker.
1826static bool ConcatenateIncludeName(llvm::SmallVector<char, 128> &FilenameBuffer,
1827 Preprocessor &PP) {
1828 Token CurTok;
1829
1830 PP.Lex(CurTok);
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001831 while (CurTok.isNot(tok::eom)) {
Chris Lattner706ab502007-07-23 04:56:47 +00001832 // Append the spelling of this token to the buffer. If there was a space
1833 // before it, add it now.
1834 if (CurTok.hasLeadingSpace())
1835 FilenameBuffer.push_back(' ');
1836
1837 // Get the spelling of the token, directly into FilenameBuffer if possible.
1838 unsigned PreAppendSize = FilenameBuffer.size();
1839 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1840
1841 const char *BufPtr = &FilenameBuffer[PreAppendSize];
1842 unsigned ActualLen = PP.getSpelling(CurTok, BufPtr);
1843
1844 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1845 if (BufPtr != &FilenameBuffer[PreAppendSize])
1846 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1847
1848 // Resize FilenameBuffer to the correct size.
1849 if (CurTok.getLength() != ActualLen)
1850 FilenameBuffer.resize(PreAppendSize+ActualLen);
1851
1852 // If we found the '>' marker, return success.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001853 if (CurTok.is(tok::greater))
Chris Lattner706ab502007-07-23 04:56:47 +00001854 return false;
1855
1856 PP.Lex(CurTok);
1857 }
1858
1859 // If we hit the eom marker, emit an error and return true so that the caller
1860 // knows the EOM has been read.
1861 PP.Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1862 return true;
1863}
1864
Reid Spencer5f016e22007-07-11 17:01:13 +00001865/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1866/// file to be included from the lexer, then include it! This is a common
1867/// routine with functionality shared between #include, #include_next and
1868/// #import.
Chris Lattnerd2177732007-07-20 16:59:19 +00001869void Preprocessor::HandleIncludeDirective(Token &IncludeTok,
Reid Spencer5f016e22007-07-11 17:01:13 +00001870 const DirectoryLookup *LookupFrom,
1871 bool isImport) {
1872
Chris Lattnerd2177732007-07-20 16:59:19 +00001873 Token FilenameTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001874 CurLexer->LexIncludeFilename(FilenameTok);
1875
Reid Spencer5f016e22007-07-11 17:01:13 +00001876 // Reserve a buffer to get the spelling.
1877 llvm::SmallVector<char, 128> FilenameBuffer;
Chris Lattner706ab502007-07-23 04:56:47 +00001878 const char *FilenameStart, *FilenameEnd;
1879
1880 switch (FilenameTok.getKind()) {
1881 case tok::eom:
1882 // If the token kind is EOM, the error has already been diagnosed.
1883 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001884
Chris Lattner706ab502007-07-23 04:56:47 +00001885 case tok::angle_string_literal:
Chris Lattnerf11ccfc2007-07-23 22:23:52 +00001886 case tok::string_literal: {
Chris Lattner706ab502007-07-23 04:56:47 +00001887 FilenameBuffer.resize(FilenameTok.getLength());
1888 FilenameStart = &FilenameBuffer[0];
1889 unsigned Len = getSpelling(FilenameTok, FilenameStart);
1890 FilenameEnd = FilenameStart+Len;
1891 break;
Chris Lattnerf11ccfc2007-07-23 22:23:52 +00001892 }
Chris Lattner706ab502007-07-23 04:56:47 +00001893
1894 case tok::less:
1895 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1896 // case, glue the tokens together into FilenameBuffer and interpret those.
1897 FilenameBuffer.push_back('<');
1898 if (ConcatenateIncludeName(FilenameBuffer, *this))
1899 return; // Found <eom> but no ">"? Diagnostic already emitted.
1900 FilenameStart = &FilenameBuffer[0];
1901 FilenameEnd = &FilenameBuffer[FilenameBuffer.size()];
1902 break;
1903 default:
1904 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1905 DiscardUntilEndOfDirective();
1906 return;
1907 }
1908
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001909 bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +00001910 FilenameStart, FilenameEnd);
1911 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1912 // error.
Chris Lattner706ab502007-07-23 04:56:47 +00001913 if (FilenameStart == 0) {
1914 DiscardUntilEndOfDirective();
Reid Spencer5f016e22007-07-11 17:01:13 +00001915 return;
Chris Lattner706ab502007-07-23 04:56:47 +00001916 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001917
1918 // Verify that there is nothing after the filename, other than EOM. Use the
1919 // preprocessor to lex this in case lexing the filename entered a macro.
1920 CheckEndOfDirective("#include");
1921
1922 // Check that we don't have infinite #include recursion.
1923 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
1924 return Diag(FilenameTok, diag::err_pp_include_too_deep);
1925
1926 // Search include directories.
1927 const DirectoryLookup *CurDir;
1928 const FileEntry *File = LookupFile(FilenameStart, FilenameEnd,
1929 isAngled, LookupFrom, CurDir);
1930 if (File == 0)
1931 return Diag(FilenameTok, diag::err_pp_file_not_found,
1932 std::string(FilenameStart, FilenameEnd));
1933
1934 // Ask HeaderInfo if we should enter this #include file.
1935 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
1936 // If it returns true, #including this file will have no effect.
1937 return;
1938 }
1939
1940 // Look up the file, create a File ID for it.
1941 unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation());
1942 if (FileID == 0)
1943 return Diag(FilenameTok, diag::err_pp_file_not_found,
1944 std::string(FilenameStart, FilenameEnd));
1945
1946 // Finally, if all is good, enter the new file!
1947 EnterSourceFile(FileID, CurDir);
1948}
1949
1950/// HandleIncludeNextDirective - Implements #include_next.
1951///
Chris Lattnerd2177732007-07-20 16:59:19 +00001952void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001953 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
1954
1955 // #include_next is like #include, except that we start searching after
1956 // the current found directory. If we can't do this, issue a
1957 // diagnostic.
1958 const DirectoryLookup *Lookup = CurDirLookup;
1959 if (isInPrimaryFile()) {
1960 Lookup = 0;
1961 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1962 } else if (Lookup == 0) {
1963 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1964 } else {
1965 // Start looking up in the next directory.
1966 ++Lookup;
1967 }
1968
1969 return HandleIncludeDirective(IncludeNextTok, Lookup);
1970}
1971
1972/// HandleImportDirective - Implements #import.
1973///
Chris Lattnerd2177732007-07-20 16:59:19 +00001974void Preprocessor::HandleImportDirective(Token &ImportTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001975 Diag(ImportTok, diag::ext_pp_import_directive);
1976
1977 return HandleIncludeDirective(ImportTok, 0, true);
1978}
1979
1980//===----------------------------------------------------------------------===//
1981// Preprocessor Macro Directive Handling.
1982//===----------------------------------------------------------------------===//
1983
1984/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1985/// definition has just been read. Lex the rest of the arguments and the
1986/// closing ), updating MI with what we learn. Return true if an error occurs
1987/// parsing the arg list.
1988bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
Chris Lattner25c96482007-07-14 22:46:43 +00001989 llvm::SmallVector<IdentifierInfo*, 32> Arguments;
1990
Chris Lattnerd2177732007-07-20 16:59:19 +00001991 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001992 while (1) {
1993 LexUnexpandedToken(Tok);
1994 switch (Tok.getKind()) {
1995 case tok::r_paren:
1996 // Found the end of the argument list.
Chris Lattner25c96482007-07-14 22:46:43 +00001997 if (Arguments.empty()) { // #define FOO()
1998 MI->setArgumentList(Arguments.begin(), Arguments.end());
1999 return false;
2000 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002001 // Otherwise we have #define FOO(A,)
2002 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
2003 return true;
2004 case tok::ellipsis: // #define X(... -> C99 varargs
2005 // Warn if use of C99 feature in non-C99 mode.
2006 if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
2007
2008 // Lex the token after the identifier.
2009 LexUnexpandedToken(Tok);
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002010 if (Tok.isNot(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002011 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2012 return true;
2013 }
2014 // Add the __VA_ARGS__ identifier as an argument.
Chris Lattner25c96482007-07-14 22:46:43 +00002015 Arguments.push_back(Ident__VA_ARGS__);
Reid Spencer5f016e22007-07-11 17:01:13 +00002016 MI->setIsC99Varargs();
Chris Lattner25c96482007-07-14 22:46:43 +00002017 MI->setArgumentList(Arguments.begin(), Arguments.end());
Reid Spencer5f016e22007-07-11 17:01:13 +00002018 return false;
2019 case tok::eom: // #define X(
2020 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2021 return true;
2022 default:
2023 // Handle keywords and identifiers here to accept things like
2024 // #define Foo(for) for.
2025 IdentifierInfo *II = Tok.getIdentifierInfo();
2026 if (II == 0) {
2027 // #define X(1
2028 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
2029 return true;
2030 }
2031
2032 // If this is already used as an argument, it is used multiple times (e.g.
2033 // #define X(A,A.
Chris Lattner25c96482007-07-14 22:46:43 +00002034 if (std::find(Arguments.begin(), Arguments.end(), II) !=
2035 Arguments.end()) { // C99 6.10.3p6
Reid Spencer5f016e22007-07-11 17:01:13 +00002036 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName());
2037 return true;
2038 }
2039
2040 // Add the argument to the macro info.
Chris Lattner25c96482007-07-14 22:46:43 +00002041 Arguments.push_back(II);
Reid Spencer5f016e22007-07-11 17:01:13 +00002042
2043 // Lex the token after the identifier.
2044 LexUnexpandedToken(Tok);
2045
2046 switch (Tok.getKind()) {
2047 default: // #define X(A B
2048 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
2049 return true;
2050 case tok::r_paren: // #define X(A)
Chris Lattner25c96482007-07-14 22:46:43 +00002051 MI->setArgumentList(Arguments.begin(), Arguments.end());
Reid Spencer5f016e22007-07-11 17:01:13 +00002052 return false;
2053 case tok::comma: // #define X(A,
2054 break;
2055 case tok::ellipsis: // #define X(A... -> GCC extension
2056 // Diagnose extension.
2057 Diag(Tok, diag::ext_named_variadic_macro);
2058
2059 // Lex the token after the identifier.
2060 LexUnexpandedToken(Tok);
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002061 if (Tok.isNot(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002062 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2063 return true;
2064 }
2065
2066 MI->setIsGNUVarargs();
Chris Lattner25c96482007-07-14 22:46:43 +00002067 MI->setArgumentList(Arguments.begin(), Arguments.end());
Reid Spencer5f016e22007-07-11 17:01:13 +00002068 return false;
2069 }
2070 }
2071 }
2072}
2073
2074/// HandleDefineDirective - Implements #define. This consumes the entire macro
2075/// line then lets the caller lex the next real token. If 'isTargetSpecific' is
2076/// true, then this is a "#define_target", otherwise this is a "#define".
2077///
Chris Lattnerd2177732007-07-20 16:59:19 +00002078void Preprocessor::HandleDefineDirective(Token &DefineTok,
Reid Spencer5f016e22007-07-11 17:01:13 +00002079 bool isTargetSpecific) {
2080 ++NumDefined;
2081
Chris Lattnerd2177732007-07-20 16:59:19 +00002082 Token MacroNameTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00002083 ReadMacroName(MacroNameTok, 1);
2084
2085 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002086 if (MacroNameTok.is(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +00002087 return;
Chris Lattnerc215bd62007-07-14 22:11:41 +00002088
Reid Spencer5f016e22007-07-11 17:01:13 +00002089 // If we are supposed to keep comments in #defines, reenable comment saving
2090 // mode.
2091 CurLexer->KeepCommentMode = KeepMacroComments;
2092
2093 // Create the new macro.
2094 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
2095 if (isTargetSpecific) MI->setIsTargetSpecific();
2096
2097 // If the identifier is an 'other target' macro, clear this bit.
2098 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
2099
2100
Chris Lattnerd2177732007-07-20 16:59:19 +00002101 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00002102 LexUnexpandedToken(Tok);
2103
2104 // If this is a function-like macro definition, parse the argument list,
2105 // marking each of the identifiers as being used as macro arguments. Also,
2106 // check other constraints on the first token of the macro body.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002107 if (Tok.is(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002108 // If there is no body to this macro, we have no special handling here.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002109 } else if (Tok.is(tok::l_paren) && !Tok.hasLeadingSpace()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002110 // This is a function-like macro definition. Read the argument list.
2111 MI->setIsFunctionLike();
2112 if (ReadMacroDefinitionArgList(MI)) {
2113 // Forget about MI.
2114 delete MI;
2115 // Throw away the rest of the line.
2116 if (CurLexer->ParsingPreprocessorDirective)
2117 DiscardUntilEndOfDirective();
2118 return;
2119 }
2120
2121 // Read the first token after the arg list for down below.
2122 LexUnexpandedToken(Tok);
2123 } else if (!Tok.hasLeadingSpace()) {
2124 // C99 requires whitespace between the macro definition and the body. Emit
2125 // a diagnostic for something like "#define X+".
2126 if (Features.C99) {
2127 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
2128 } else {
2129 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
2130 // one in some cases!
2131 }
2132 } else {
2133 // This is a normal token with leading space. Clear the leading space
2134 // marker on the first token to get proper expansion.
Chris Lattnerd2177732007-07-20 16:59:19 +00002135 Tok.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00002136 }
2137
2138 // If this is a definition of a variadic C99 function-like macro, not using
2139 // the GNU named varargs extension, enabled __VA_ARGS__.
2140
2141 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
2142 // This gets unpoisoned where it is allowed.
2143 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
2144 if (MI->isC99Varargs())
2145 Ident__VA_ARGS__->setIsPoisoned(false);
2146
2147 // Read the rest of the macro body.
Chris Lattnerb5e240f2007-07-14 21:54:03 +00002148 if (MI->isObjectLike()) {
2149 // Object-like macros are very simple, just read their body.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002150 while (Tok.isNot(tok::eom)) {
Chris Lattnerb5e240f2007-07-14 21:54:03 +00002151 MI->AddTokenToBody(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +00002152 // Get the next token of the macro.
2153 LexUnexpandedToken(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +00002154 }
2155
Chris Lattnerb5e240f2007-07-14 21:54:03 +00002156 } else {
2157 // Otherwise, read the body of a function-like macro. This has to validate
2158 // the # (stringize) operator.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002159 while (Tok.isNot(tok::eom)) {
Chris Lattnerb5e240f2007-07-14 21:54:03 +00002160 MI->AddTokenToBody(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +00002161
Chris Lattnerb5e240f2007-07-14 21:54:03 +00002162 // Check C99 6.10.3.2p1: ensure that # operators are followed by macro
2163 // parameters in function-like macro expansions.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002164 if (Tok.isNot(tok::hash)) {
Chris Lattnerb5e240f2007-07-14 21:54:03 +00002165 // Get the next token of the macro.
2166 LexUnexpandedToken(Tok);
2167 continue;
2168 }
2169
2170 // Get the next token of the macro.
2171 LexUnexpandedToken(Tok);
2172
2173 // Not a macro arg identifier?
2174 if (!Tok.getIdentifierInfo() ||
2175 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
2176 Diag(Tok, diag::err_pp_stringize_not_parameter);
2177 delete MI;
2178
2179 // Disable __VA_ARGS__ again.
2180 Ident__VA_ARGS__->setIsPoisoned(true);
2181 return;
2182 }
2183
2184 // Things look ok, add the param name token to the macro.
2185 MI->AddTokenToBody(Tok);
2186
2187 // Get the next token of the macro.
2188 LexUnexpandedToken(Tok);
2189 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002190 }
2191
Chris Lattnerc215bd62007-07-14 22:11:41 +00002192
Reid Spencer5f016e22007-07-11 17:01:13 +00002193 // Disable __VA_ARGS__ again.
2194 Ident__VA_ARGS__->setIsPoisoned(true);
2195
2196 // Check that there is no paste (##) operator at the begining or end of the
2197 // replacement list.
2198 unsigned NumTokens = MI->getNumTokens();
2199 if (NumTokens != 0) {
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002200 if (MI->getReplacementToken(0).is(tok::hashhash)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002201 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
2202 delete MI;
2203 return;
2204 }
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002205 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002206 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
2207 delete MI;
2208 return;
2209 }
2210 }
2211
2212 // If this is the primary source file, remember that this macro hasn't been
2213 // used yet.
2214 if (isInPrimaryFile())
2215 MI->setIsUsed(false);
2216
2217 // Finally, if this identifier already had a macro defined for it, verify that
2218 // the macro bodies are identical and free the old definition.
Chris Lattnercc1a8752007-10-07 08:44:20 +00002219 if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002220 if (!OtherMI->isUsed())
2221 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2222
2223 // Macros must be identical. This means all tokes and whitespace separation
2224 // must be the same. C99 6.10.3.2.
2225 if (!MI->isIdenticalTo(*OtherMI, *this)) {
2226 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef,
2227 MacroNameTok.getIdentifierInfo()->getName());
2228 Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2);
2229 }
2230 delete OtherMI;
2231 }
2232
Chris Lattnercc1a8752007-10-07 08:44:20 +00002233 setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
Reid Spencer5f016e22007-07-11 17:01:13 +00002234}
2235
2236/// HandleDefineOtherTargetDirective - Implements #define_other_target.
Chris Lattnerd2177732007-07-20 16:59:19 +00002237void Preprocessor::HandleDefineOtherTargetDirective(Token &Tok) {
2238 Token MacroNameTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00002239 ReadMacroName(MacroNameTok, 1);
2240
2241 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002242 if (MacroNameTok.is(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +00002243 return;
2244
2245 // Check to see if this is the last token on the #undef line.
2246 CheckEndOfDirective("#define_other_target");
2247
2248 // If there is already a macro defined by this name, turn it into a
2249 // target-specific define.
Chris Lattnercc1a8752007-10-07 08:44:20 +00002250 if (MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002251 MI->setIsTargetSpecific(true);
2252 return;
2253 }
2254
2255 // Mark the identifier as being a macro on some other target.
2256 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro();
2257}
2258
2259
2260/// HandleUndefDirective - Implements #undef.
2261///
Chris Lattnerd2177732007-07-20 16:59:19 +00002262void Preprocessor::HandleUndefDirective(Token &UndefTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002263 ++NumUndefined;
2264
Chris Lattnerd2177732007-07-20 16:59:19 +00002265 Token MacroNameTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00002266 ReadMacroName(MacroNameTok, 2);
2267
2268 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002269 if (MacroNameTok.is(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +00002270 return;
2271
2272 // Check to see if this is the last token on the #undef line.
2273 CheckEndOfDirective("#undef");
2274
2275 // Okay, we finally have a valid identifier to undef.
Chris Lattnercc1a8752007-10-07 08:44:20 +00002276 MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
Reid Spencer5f016e22007-07-11 17:01:13 +00002277
2278 // #undef untaints an identifier if it were marked by define_other_target.
2279 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
2280
2281 // If the macro is not defined, this is a noop undef, just return.
2282 if (MI == 0) return;
2283
2284 if (!MI->isUsed())
2285 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
2286
2287 // Free macro definition.
2288 delete MI;
Chris Lattnercc1a8752007-10-07 08:44:20 +00002289 setMacroInfo(MacroNameTok.getIdentifierInfo(), 0);
Reid Spencer5f016e22007-07-11 17:01:13 +00002290}
2291
2292
2293//===----------------------------------------------------------------------===//
2294// Preprocessor Conditional Directive Handling.
2295//===----------------------------------------------------------------------===//
2296
2297/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
2298/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
2299/// if any tokens have been returned or pp-directives activated before this
2300/// #ifndef has been lexed.
2301///
Chris Lattnerd2177732007-07-20 16:59:19 +00002302void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
Reid Spencer5f016e22007-07-11 17:01:13 +00002303 bool ReadAnyTokensBeforeDirective) {
2304 ++NumIf;
Chris Lattnerd2177732007-07-20 16:59:19 +00002305 Token DirectiveTok = Result;
Reid Spencer5f016e22007-07-11 17:01:13 +00002306
Chris Lattnerd2177732007-07-20 16:59:19 +00002307 Token MacroNameTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00002308 ReadMacroName(MacroNameTok);
2309
2310 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002311 if (MacroNameTok.is(tok::eom)) {
Chris Lattnerf37bb252007-09-24 05:14:57 +00002312 // Skip code until we get to #endif. This helps with recovery by not
2313 // emitting an error when the #endif is reached.
2314 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2315 /*Foundnonskip*/false, /*FoundElse*/false);
Reid Spencer5f016e22007-07-11 17:01:13 +00002316 return;
Chris Lattnerf37bb252007-09-24 05:14:57 +00002317 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002318
2319 // Check to see if this is the last token on the #if[n]def line.
2320 CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
2321
2322 // If the start of a top-level #ifdef, inform MIOpt.
2323 if (!ReadAnyTokensBeforeDirective &&
2324 CurLexer->getConditionalStackDepth() == 0) {
2325 assert(isIfndef && "#ifdef shouldn't reach here");
2326 CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
2327 }
2328
2329 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
Chris Lattnercc1a8752007-10-07 08:44:20 +00002330 MacroInfo *MI = getMacroInfo(MII);
Reid Spencer5f016e22007-07-11 17:01:13 +00002331
2332 // If there is a macro, process it.
2333 if (MI) {
2334 // Mark it used.
2335 MI->setIsUsed(true);
2336
2337 // If this is the first use of a target-specific macro, warn about it.
2338 if (MI->isTargetSpecific()) {
2339 MI->setIsTargetSpecific(false); // Don't warn on second use.
2340 getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(),
2341 diag::port_target_macro_use);
2342 }
2343 } else {
2344 // Use of a target-specific macro for some other target? If so, warn.
2345 if (MII->isOtherTargetMacro()) {
2346 MII->setIsOtherTargetMacro(false); // Don't warn on second use.
2347 getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(),
2348 diag::port_target_macro_use);
2349 }
2350 }
2351
2352 // Should we include the stuff contained by this directive?
2353 if (!MI == isIfndef) {
2354 // Yes, remember that we are inside a conditional, then lex the next token.
2355 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
2356 /*foundnonskip*/true, /*foundelse*/false);
2357 } else {
2358 // No, skip the contents of this block and return the first token after it.
2359 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2360 /*Foundnonskip*/false,
2361 /*FoundElse*/false);
2362 }
2363}
2364
2365/// HandleIfDirective - Implements the #if directive.
2366///
Chris Lattnerd2177732007-07-20 16:59:19 +00002367void Preprocessor::HandleIfDirective(Token &IfToken,
Reid Spencer5f016e22007-07-11 17:01:13 +00002368 bool ReadAnyTokensBeforeDirective) {
2369 ++NumIf;
2370
2371 // Parse and evaluation the conditional expression.
2372 IdentifierInfo *IfNDefMacro = 0;
2373 bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2374
2375 // Should we include the stuff contained by this directive?
2376 if (ConditionalTrue) {
2377 // If this condition is equivalent to #ifndef X, and if this is the first
2378 // directive seen, handle it for the multiple-include optimization.
2379 if (!ReadAnyTokensBeforeDirective &&
2380 CurLexer->getConditionalStackDepth() == 0 && IfNDefMacro)
2381 CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
2382
2383 // Yes, remember that we are inside a conditional, then lex the next token.
2384 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2385 /*foundnonskip*/true, /*foundelse*/false);
2386 } else {
2387 // No, skip the contents of this block and return the first token after it.
2388 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
2389 /*FoundElse*/false);
2390 }
2391}
2392
2393/// HandleEndifDirective - Implements the #endif directive.
2394///
Chris Lattnerd2177732007-07-20 16:59:19 +00002395void Preprocessor::HandleEndifDirective(Token &EndifToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002396 ++NumEndif;
2397
2398 // Check that this is the whole directive.
2399 CheckEndOfDirective("#endif");
2400
2401 PPConditionalInfo CondInfo;
2402 if (CurLexer->popConditionalLevel(CondInfo)) {
2403 // No conditionals on the stack: this is an #endif without an #if.
2404 return Diag(EndifToken, diag::err_pp_endif_without_if);
2405 }
2406
2407 // If this the end of a top-level #endif, inform MIOpt.
2408 if (CurLexer->getConditionalStackDepth() == 0)
2409 CurLexer->MIOpt.ExitTopLevelConditional();
2410
2411 assert(!CondInfo.WasSkipping && !CurLexer->LexingRawMode &&
2412 "This code should only be reachable in the non-skipping case!");
2413}
2414
2415
Chris Lattnerd2177732007-07-20 16:59:19 +00002416void Preprocessor::HandleElseDirective(Token &Result) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002417 ++NumElse;
2418
2419 // #else directive in a non-skipping conditional... start skipping.
2420 CheckEndOfDirective("#else");
2421
2422 PPConditionalInfo CI;
2423 if (CurLexer->popConditionalLevel(CI))
2424 return Diag(Result, diag::pp_err_else_without_if);
2425
2426 // If this is a top-level #else, inform the MIOpt.
2427 if (CurLexer->getConditionalStackDepth() == 0)
2428 CurLexer->MIOpt.FoundTopLevelElse();
2429
2430 // If this is a #else with a #else before it, report the error.
2431 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2432
2433 // Finally, skip the rest of the contents of this block and return the first
2434 // token after it.
2435 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2436 /*FoundElse*/true);
2437}
2438
Chris Lattnerd2177732007-07-20 16:59:19 +00002439void Preprocessor::HandleElifDirective(Token &ElifToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002440 ++NumElse;
2441
2442 // #elif directive in a non-skipping conditional... start skipping.
2443 // We don't care what the condition is, because we will always skip it (since
2444 // the block immediately before it was included).
2445 DiscardUntilEndOfDirective();
2446
2447 PPConditionalInfo CI;
2448 if (CurLexer->popConditionalLevel(CI))
2449 return Diag(ElifToken, diag::pp_err_elif_without_if);
2450
2451 // If this is a top-level #elif, inform the MIOpt.
2452 if (CurLexer->getConditionalStackDepth() == 0)
2453 CurLexer->MIOpt.FoundTopLevelElse();
2454
2455 // If this is a #elif with a #else before it, report the error.
2456 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2457
2458 // Finally, skip the rest of the contents of this block and return the first
2459 // token after it.
2460 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2461 /*FoundElse*/CI.FoundElse);
2462}
2463