blob: 40abfe5cfc3f211204ec146208f1997ef0331f36 [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;
136 std::cerr << "\t";
137 if (Tok.isAtStartOfLine())
138 std::cerr << " [StartOfLine]";
139 if (Tok.hasLeadingSpace())
140 std::cerr << " [LeadingSpace]";
141 if (Tok.isExpandDisabled())
142 std::cerr << " [ExpandDisabled]";
143 if (Tok.needsCleaning()) {
144 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
145 std::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
146 << "']";
147 }
148}
149
150void Preprocessor::DumpMacro(const MacroInfo &MI) const {
151 std::cerr << "MACRO: ";
152 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
153 DumpToken(MI.getReplacementToken(i));
154 std::cerr << " ";
155 }
156 std::cerr << "\n";
157}
158
159void Preprocessor::PrintStats() {
160 std::cerr << "\n*** Preprocessor Stats:\n";
161 std::cerr << NumDirectives << " directives found:\n";
162 std::cerr << " " << NumDefined << " #define.\n";
163 std::cerr << " " << NumUndefined << " #undef.\n";
164 std::cerr << " #include/#include_next/#import:\n";
165 std::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
166 std::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
167 std::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
168 std::cerr << " " << NumElse << " #else/#elif.\n";
169 std::cerr << " " << NumEndif << " #endif.\n";
170 std::cerr << " " << NumPragma << " #pragma.\n";
171 std::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
172
173 std::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
174 << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
175 << NumFastMacroExpanded << " on the fast path.\n";
176 std::cerr << (NumFastTokenPaste+NumTokenPaste)
177 << " token paste (##) operations performed, "
178 << NumFastTokenPaste << " on the fast path.\n";
179}
180
181//===----------------------------------------------------------------------===//
182// Token Spelling
183//===----------------------------------------------------------------------===//
184
185
186/// getSpelling() - Return the 'spelling' of this token. The spelling of a
187/// token are the characters used to represent the token in the source file
188/// after trigraph expansion and escaped-newline folding. In particular, this
189/// wants to get the true, uncanonicalized, spelling of things like digraphs
190/// UCNs, etc.
Chris Lattnerd2177732007-07-20 16:59:19 +0000191std::string Preprocessor::getSpelling(const Token &Tok) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
193
194 // If this token contains nothing interesting, return it directly.
195 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
196 if (!Tok.needsCleaning())
197 return std::string(TokStart, TokStart+Tok.getLength());
198
199 std::string Result;
200 Result.reserve(Tok.getLength());
201
202 // Otherwise, hard case, relex the characters into the string.
203 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
204 Ptr != End; ) {
205 unsigned CharSize;
206 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
207 Ptr += CharSize;
208 }
209 assert(Result.size() != unsigned(Tok.getLength()) &&
210 "NeedsCleaning flag set on something that didn't need cleaning!");
211 return Result;
212}
213
214/// getSpelling - This method is used to get the spelling of a token into a
215/// preallocated buffer, instead of as an std::string. The caller is required
216/// to allocate enough space for the token, which is guaranteed to be at least
217/// Tok.getLength() bytes long. The actual length of the token is returned.
218///
219/// Note that this method may do two possible things: it may either fill in
220/// the buffer specified with characters, or it may *change the input pointer*
221/// to point to a constant buffer with the data already in it (avoiding a
222/// copy). The caller is not allowed to modify the returned buffer pointer
223/// if an internal buffer is returned.
Chris Lattnerd2177732007-07-20 16:59:19 +0000224unsigned Preprocessor::getSpelling(const Token &Tok,
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 const char *&Buffer) const {
226 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
227
228 // If this token is an identifier, just return the string from the identifier
229 // table, which is very quick.
230 if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
231 Buffer = II->getName();
Chris Lattner0f670322007-07-22 22:50:09 +0000232
233 // Return the length of the token. If the token needed cleaning, don't
234 // include the size of the newlines or trigraphs in it.
235 if (!Tok.needsCleaning())
236 return Tok.getLength();
237 else
238 return strlen(Buffer);
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 }
240
241 // Otherwise, compute the start of the token in the input lexer buffer.
242 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
243
244 // If this token contains nothing interesting, return it directly.
245 if (!Tok.needsCleaning()) {
246 Buffer = TokStart;
247 return Tok.getLength();
248 }
249 // Otherwise, hard case, relex the characters into the string.
250 char *OutBuf = const_cast<char*>(Buffer);
251 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
252 Ptr != End; ) {
253 unsigned CharSize;
254 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
255 Ptr += CharSize;
256 }
257 assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
258 "NeedsCleaning flag set on something that didn't need cleaning!");
259
260 return OutBuf-Buffer;
261}
262
263
264/// CreateString - Plop the specified string into a scratch buffer and return a
265/// location for it. If specified, the source location provides a source
266/// location for the token.
267SourceLocation Preprocessor::
268CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) {
269 if (SLoc.isValid())
270 return ScratchBuf->getToken(Buf, Len, SLoc);
271 return ScratchBuf->getToken(Buf, Len);
272}
273
274
Chris Lattner97ba77c2007-07-16 06:48:38 +0000275/// AdvanceToTokenCharacter - Given a location that specifies the start of a
276/// token, return a new location that specifies a character within the token.
277SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart,
278 unsigned CharNo) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000279 // If they request the first char of the token, we're trivially done. If this
280 // is a macro expansion, it doesn't make sense to point to a character within
281 // the instantiation point (the name). We could point to the source
282 // character, but without also pointing to instantiation info, this is
283 // confusing.
284 if (CharNo == 0 || TokStart.isMacroID()) return TokStart;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000285
286 // Figure out how many physical characters away the specified logical
287 // character is. This needs to take into consideration newlines and
288 // trigraphs.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000289 const char *TokPtr = SourceMgr.getCharacterData(TokStart);
290 unsigned PhysOffset = 0;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000291
292 // The usual case is that tokens don't contain anything interesting. Skip
293 // over the uninteresting characters. If a token only consists of simple
294 // chars, this method is extremely fast.
295 while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr))
Chris Lattner9dc1f532007-07-20 16:37:10 +0000296 ++TokPtr, --CharNo, ++PhysOffset;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000297
298 // If we have a character that may be a trigraph or escaped newline, create a
299 // lexer to parse it correctly.
Chris Lattner97ba77c2007-07-16 06:48:38 +0000300 if (CharNo != 0) {
301 // Create a lexer starting at this token position.
Chris Lattner25bdb512007-07-20 16:52:03 +0000302 Lexer TheLexer(TokStart, *this, TokPtr);
Chris Lattnerd2177732007-07-20 16:59:19 +0000303 Token Tok;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000304 // Skip over characters the remaining characters.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000305 const char *TokStartPtr = TokPtr;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000306 for (; CharNo; --CharNo)
307 TheLexer.getAndAdvanceChar(TokPtr, Tok);
Chris Lattner9dc1f532007-07-20 16:37:10 +0000308
309 PhysOffset += TokPtr-TokStartPtr;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000310 }
Chris Lattner9dc1f532007-07-20 16:37:10 +0000311
312 return TokStart.getFileLocWithOffset(PhysOffset);
Chris Lattner97ba77c2007-07-16 06:48:38 +0000313}
314
315
Chris Lattner53b0dab2007-10-09 22:10:18 +0000316//===----------------------------------------------------------------------===//
317// Preprocessor Initialization Methods
318//===----------------------------------------------------------------------===//
319
320// Append a #define line to Buf for Macro. Macro should be of the form XXX,
321// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
322// "#define XXX Y z W". To get a #define with no value, use "XXX=".
323static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
324 const char *Command = "#define ") {
325 Buf.insert(Buf.end(), Command, Command+strlen(Command));
326 if (const char *Equal = strchr(Macro, '=')) {
327 // Turn the = into ' '.
328 Buf.insert(Buf.end(), Macro, Equal);
329 Buf.push_back(' ');
330 Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal));
331 } else {
332 // Push "macroname 1".
333 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
334 Buf.push_back(' ');
335 Buf.push_back('1');
336 }
337 Buf.push_back('\n');
338}
339
340
341static void InitializePredefinedMacros(Preprocessor &PP,
342 std::vector<char> &Buf) {
343 // FIXME: Implement magic like cpp_init_builtins for things like __STDC__
344 // and __DATE__ etc.
345#if 0
346 /* __STDC__ has the value 1 under normal circumstances.
347 However, if (a) we are in a system header, (b) the option
348 stdc_0_in_system_headers is true (set by target config), and
349 (c) we are not in strictly conforming mode, then it has the
350 value 0. (b) and (c) are already checked in cpp_init_builtins. */
351 //case BT_STDC:
352 if (cpp_in_system_header (pfile))
353 number = 0;
354 else
355 number = 1;
356 break;
357#endif
358 // These should all be defined in the preprocessor according to the
359 // current language configuration.
360 DefineBuiltinMacro(Buf, "__STDC__=1");
361 //DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
362 if (PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus)
363 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
364 else if (0) // STDC94 ?
365 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
366
367 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");
368 if (PP.getLangOptions().ObjC1)
369 DefineBuiltinMacro(Buf, "__OBJC__=1");
370 if (PP.getLangOptions().ObjC2)
371 DefineBuiltinMacro(Buf, "__OBJC2__=1");
Chris Lattnerf1af9472007-10-09 22:58:09 +0000372
373 if (PP.getLangOptions().ObjC1) {
Steve Naroff3b8e8912007-10-17 17:53:50 +0000374 // Predefine all the ObjC goodies (traditionally declared in <objc/objc.h>).
375 // We define the following header guard for source compatibility. It has
376 // the effect of ignoring any explicit inclusion of <objc/objc.h>:-)
377 DefineBuiltinMacro(Buf, "_OBJC_OBJC_H_=1");
378 DefineBuiltinMacro(Buf, "OBJC_EXPORT=extern");
379 DefineBuiltinMacro(Buf, "OBJC_IMPORT=extern");
380 const char *ObjcType;
381 ObjcType = "typedef struct objc_class *Class;\n";
382 Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
383 ObjcType = "typedef struct objc_object { Class isa; } *id;\n";
384 Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
385 ObjcType = "typedef struct objc_selector *SEL;\n";
386 Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
387 ObjcType = "typedef id (*IMP)(id, SEL, ...);\n";
388 Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
389 ObjcType = "typedef signed char BOOL;\n";
390 Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
391 DefineBuiltinMacro(Buf, "YES=(BOOL)1");
392 DefineBuiltinMacro(Buf, "NO=(BOOL)0");
393 DefineBuiltinMacro(Buf, "Nil=0");
394 DefineBuiltinMacro(Buf, "nil=0");
Chris Lattnerd65d8ee2007-10-30 17:45:43 +0000395 ObjcType = "extern const char *sel_getName(SEL sel);\n";
Steve Naroff7e158912007-10-23 20:20:08 +0000396 Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
Chris Lattnerd65d8ee2007-10-30 17:45:43 +0000397 ObjcType = "extern SEL sel_getUid(const char *str);\n";
Steve Naroff7e158912007-10-23 20:20:08 +0000398 Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
399
400 // Predefine ObjC primitive functions, traditionally declared in
401 // <objc/objc-runtime.h>. Unlike the declarations above, we don't protect
402 // these with a header guard (since multiple identical function declarations
Steve Naroffc0006092007-10-24 01:09:48 +0000403 // don't result in an error. FIXME: don't predefine these...
Chris Lattnerd65d8ee2007-10-30 17:45:43 +0000404 ObjcType = "extern id objc_getClass(const char *name);\n";
Steve Naroff7e158912007-10-23 20:20:08 +0000405 Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
Chris Lattnerd65d8ee2007-10-30 17:45:43 +0000406 ObjcType = "extern id objc_getMetaClass(const char *name);\n";
Steve Naroff7e158912007-10-23 20:20:08 +0000407 Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
Chris Lattnerd65d8ee2007-10-30 17:45:43 +0000408 ObjcType = "extern id objc_msgSend(id self, SEL op, ...);\n";
Steve Naroff7e158912007-10-23 20:20:08 +0000409 Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
Chris Lattnerd65d8ee2007-10-30 17:45:43 +0000410 ObjcType = "extern id objc_msgSendSuper(struct objc_super *super, SEL op, ...);\n";
Steve Naroff7e158912007-10-23 20:20:08 +0000411 Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
Chris Lattnerf1af9472007-10-09 22:58:09 +0000412 }
413
Chris Lattnerd19144b2007-10-10 17:48:53 +0000414 // Add __builtin_va_list typedef.
415 {
416 const char *VAList = PP.getTargetInfo().getVAListDeclaration();
417 Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
418 Buf.push_back('\n');
419 }
Chris Lattner53b0dab2007-10-09 22:10:18 +0000420
421 // Get the target #defines.
422 PP.getTargetInfo().getTargetDefines(Buf);
423
424 // Compiler set macros.
425 DefineBuiltinMacro(Buf, "__APPLE_CC__=5250");
426 DefineBuiltinMacro(Buf, "__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=1030");
427 DefineBuiltinMacro(Buf, "__GNUC_MINOR__=0");
428 DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
429 DefineBuiltinMacro(Buf, "__GNUC__=4");
430 DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
431 DefineBuiltinMacro(Buf, "__VERSION__=\"4.0.1 (Apple Computer, Inc. "
432 "build 5250)\"");
433
434 // Build configuration options.
435 DefineBuiltinMacro(Buf, "__DYNAMIC__=1");
436 DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
437 DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
438 DefineBuiltinMacro(Buf, "__PIC__=1");
439
440
441 if (PP.getLangOptions().CPlusPlus) {
442 DefineBuiltinMacro(Buf, "__DEPRECATED=1");
443 DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
444 DefineBuiltinMacro(Buf, "__GNUG__=4");
445 DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
446 DefineBuiltinMacro(Buf, "__cplusplus=1");
447 DefineBuiltinMacro(Buf, "__private_extern__=extern");
448 }
449
450 // FIXME: Should emit a #line directive here.
451}
452
453
454/// EnterMainSourceFile - Enter the specified FileID as the main source file,
455/// which implicitly adds the builting defines etc.
456void Preprocessor::EnterMainSourceFile(unsigned MainFileID) {
457 // Enter the main file source buffer.
458 EnterSourceFile(MainFileID, 0);
459
460
461 std::vector<char> PrologFile;
462 PrologFile.reserve(4080);
463
464 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
465 InitializePredefinedMacros(*this, PrologFile);
466
467 // Add on the predefines from the driver.
468 PrologFile.insert(PrologFile.end(), Predefines,Predefines+strlen(Predefines));
469
470 // Memory buffer must end with a null byte!
471 PrologFile.push_back(0);
472
473 // Now that we have emitted the predefined macros, #includes, etc into
474 // PrologFile, preprocess it to populate the initial preprocessor state.
475 llvm::MemoryBuffer *SB =
476 llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(),
477 "<predefines>");
478 assert(SB && "Cannot fail to create predefined source buffer");
479 unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB);
480 assert(FileID && "Could not create FileID for predefines?");
481
482 // Start parsing the predefines.
483 EnterSourceFile(FileID, 0);
484}
Chris Lattner97ba77c2007-07-16 06:48:38 +0000485
Reid Spencer5f016e22007-07-11 17:01:13 +0000486//===----------------------------------------------------------------------===//
487// Source File Location Methods.
488//===----------------------------------------------------------------------===//
489
490/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
491/// return null on failure. isAngled indicates whether the file reference is
492/// for system #include's or not (i.e. using <> instead of "").
493const FileEntry *Preprocessor::LookupFile(const char *FilenameStart,
494 const char *FilenameEnd,
495 bool isAngled,
496 const DirectoryLookup *FromDir,
497 const DirectoryLookup *&CurDir) {
498 // If the header lookup mechanism may be relative to the current file, pass in
499 // info about where the current file is.
500 const FileEntry *CurFileEnt = 0;
501 if (!FromDir) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000502 SourceLocation FileLoc = getCurrentFileLexer()->getFileLoc();
503 CurFileEnt = SourceMgr.getFileEntryForLoc(FileLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000504 }
505
506 // Do a standard file entry lookup.
507 CurDir = CurDirLookup;
508 const FileEntry *FE =
509 HeaderInfo.LookupFile(FilenameStart, FilenameEnd,
510 isAngled, FromDir, CurDir, CurFileEnt);
511 if (FE) return FE;
512
513 // Otherwise, see if this is a subframework header. If so, this is relative
514 // to one of the headers on the #include stack. Walk the list of the current
515 // headers on the #include stack and pass them to HeaderInfo.
516 if (CurLexer && !CurLexer->Is_PragmaLexer) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000517 CurFileEnt = SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000518 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
519 CurFileEnt)))
520 return FE;
521 }
522
523 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
524 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
525 if (ISEntry.TheLexer && !ISEntry.TheLexer->Is_PragmaLexer) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000526 CurFileEnt = SourceMgr.getFileEntryForLoc(ISEntry.TheLexer->getFileLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000527 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
528 CurFileEnt)))
529 return FE;
530 }
531 }
532
533 // Otherwise, we really couldn't find the file.
534 return 0;
535}
536
537/// isInPrimaryFile - Return true if we're in the top-level file, not in a
538/// #include.
539bool Preprocessor::isInPrimaryFile() const {
540 if (CurLexer && !CurLexer->Is_PragmaLexer)
Chris Lattner53b0dab2007-10-09 22:10:18 +0000541 return IncludeMacroStack.empty();
Reid Spencer5f016e22007-07-11 17:01:13 +0000542
543 // If there are any stacked lexers, we're in a #include.
Chris Lattner53b0dab2007-10-09 22:10:18 +0000544 assert(IncludeMacroStack[0].TheLexer &&
545 !IncludeMacroStack[0].TheLexer->Is_PragmaLexer &&
546 "Top level include stack isn't our primary lexer?");
547 for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i)
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 if (IncludeMacroStack[i].TheLexer &&
549 !IncludeMacroStack[i].TheLexer->Is_PragmaLexer)
Chris Lattner53b0dab2007-10-09 22:10:18 +0000550 return false;
551 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000552}
553
554/// getCurrentLexer - Return the current file lexer being lexed from. Note
555/// that this ignores any potentially active macro expansions and _Pragma
556/// expansions going on at the time.
557Lexer *Preprocessor::getCurrentFileLexer() const {
558 if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer;
559
560 // Look for a stacked lexer.
561 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
562 Lexer *L = IncludeMacroStack[i-1].TheLexer;
563 if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions.
564 return L;
565 }
566 return 0;
567}
568
569
570/// EnterSourceFile - Add a source file to the top of the include stack and
571/// start lexing tokens from it instead of the current buffer. Return true
572/// on failure.
573void Preprocessor::EnterSourceFile(unsigned FileID,
Chris Lattner53b0dab2007-10-09 22:10:18 +0000574 const DirectoryLookup *CurDir) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
576 ++NumEnteredSourceFiles;
577
578 if (MaxIncludeStackDepth < IncludeMacroStack.size())
579 MaxIncludeStackDepth = IncludeMacroStack.size();
580
Chris Lattner25bdb512007-07-20 16:52:03 +0000581 Lexer *TheLexer = new Lexer(SourceLocation::getFileLoc(FileID, 0), *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000582 EnterSourceFileWithLexer(TheLexer, CurDir);
583}
584
585/// EnterSourceFile - Add a source file to the top of the include stack and
586/// start lexing tokens from it instead of the current buffer.
587void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
588 const DirectoryLookup *CurDir) {
589
590 // Add the current lexer to the include stack.
591 if (CurLexer || CurMacroExpander)
592 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
593 CurMacroExpander));
594
595 CurLexer = TheLexer;
596 CurDirLookup = CurDir;
597 CurMacroExpander = 0;
598
599 // Notify the client, if desired, that we are in a new source file.
600 if (Callbacks && !CurLexer->Is_PragmaLexer) {
601 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
602
603 // Get the file entry for the current file.
604 if (const FileEntry *FE =
Chris Lattner9dc1f532007-07-20 16:37:10 +0000605 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 FileType = HeaderInfo.getFileDirFlavor(FE);
607
Chris Lattner9dc1f532007-07-20 16:37:10 +0000608 Callbacks->FileChanged(CurLexer->getFileLoc(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 PPCallbacks::EnterFile, FileType);
610 }
611}
612
613
614
615/// EnterMacro - Add a Macro to the top of the include stack and start lexing
616/// tokens from it instead of the current buffer.
Chris Lattnerd2177732007-07-20 16:59:19 +0000617void Preprocessor::EnterMacro(Token &Tok, MacroArgs *Args) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
619 CurMacroExpander));
620 CurLexer = 0;
621 CurDirLookup = 0;
622
Chris Lattner9594acf2007-07-15 00:25:26 +0000623 if (NumCachedMacroExpanders == 0) {
624 CurMacroExpander = new MacroExpander(Tok, Args, *this);
625 } else {
626 CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
627 CurMacroExpander->Init(Tok, Args);
628 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000629}
630
631/// EnterTokenStream - Add a "macro" context to the top of the include stack,
632/// which will cause the lexer to start returning the specified tokens. Note
633/// that these tokens will be re-macro-expanded when/if expansion is enabled.
634/// This method assumes that the specified stream of tokens has a permanent
635/// owner somewhere, so they do not need to be copied.
Chris Lattnerd2177732007-07-20 16:59:19 +0000636void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 // Save our current state.
638 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
639 CurMacroExpander));
640 CurLexer = 0;
641 CurDirLookup = 0;
642
643 // Create a macro expander to expand from the specified token stream.
Chris Lattner9594acf2007-07-15 00:25:26 +0000644 if (NumCachedMacroExpanders == 0) {
645 CurMacroExpander = new MacroExpander(Toks, NumToks, *this);
646 } else {
647 CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
648 CurMacroExpander->Init(Toks, NumToks);
649 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000650}
651
652/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
653/// lexer stack. This should only be used in situations where the current
654/// state of the top-of-stack lexer is known.
655void Preprocessor::RemoveTopOfLexerStack() {
656 assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
Chris Lattner9594acf2007-07-15 00:25:26 +0000657
658 if (CurMacroExpander) {
659 // Delete or cache the now-dead macro expander.
660 if (NumCachedMacroExpanders == MacroExpanderCacheSize)
661 delete CurMacroExpander;
662 else
663 MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander;
664 } else {
665 delete CurLexer;
666 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 CurLexer = IncludeMacroStack.back().TheLexer;
668 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
669 CurMacroExpander = IncludeMacroStack.back().TheMacroExpander;
670 IncludeMacroStack.pop_back();
671}
672
673//===----------------------------------------------------------------------===//
674// Macro Expansion Handling.
675//===----------------------------------------------------------------------===//
676
Chris Lattnercc1a8752007-10-07 08:44:20 +0000677/// setMacroInfo - Specify a macro for this identifier.
678///
679void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
680 if (MI == 0) {
681 if (II->hasMacroDefinition()) {
682 Macros.erase(II);
683 II->setHasMacroDefinition(false);
684 }
685 } else {
686 Macros[II] = MI;
687 II->setHasMacroDefinition(true);
688 }
689}
690
Reid Spencer5f016e22007-07-11 17:01:13 +0000691/// RegisterBuiltinMacro - Register the specified identifier in the identifier
692/// table and mark it as a builtin macro to be expanded.
693IdentifierInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) {
694 // Get the identifier.
695 IdentifierInfo *Id = getIdentifierInfo(Name);
696
697 // Mark it as being a macro that is builtin.
698 MacroInfo *MI = new MacroInfo(SourceLocation());
699 MI->setIsBuiltinMacro();
Chris Lattnercc1a8752007-10-07 08:44:20 +0000700 setMacroInfo(Id, MI);
Reid Spencer5f016e22007-07-11 17:01:13 +0000701 return Id;
702}
703
704
705/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
706/// identifier table.
707void Preprocessor::RegisterBuiltinMacros() {
708 Ident__LINE__ = RegisterBuiltinMacro("__LINE__");
709 Ident__FILE__ = RegisterBuiltinMacro("__FILE__");
710 Ident__DATE__ = RegisterBuiltinMacro("__DATE__");
711 Ident__TIME__ = RegisterBuiltinMacro("__TIME__");
712 Ident_Pragma = RegisterBuiltinMacro("_Pragma");
713
714 // GCC Extensions.
715 Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__");
716 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__");
717 Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__");
718}
719
720/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
721/// in its expansion, currently expands to that token literally.
722static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
Chris Lattnercc1a8752007-10-07 08:44:20 +0000723 const IdentifierInfo *MacroIdent,
724 Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
726
727 // If the token isn't an identifier, it's always literally expanded.
728 if (II == 0) return true;
729
730 // If the identifier is a macro, and if that macro is enabled, it may be
731 // expanded so it's not a trivial expansion.
Chris Lattnercc1a8752007-10-07 08:44:20 +0000732 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 // Fast expanding "#define X X" is ok, because X would be disabled.
734 II != MacroIdent)
735 return false;
736
737 // If this is an object-like macro invocation, it is safe to trivially expand
738 // it.
739 if (MI->isObjectLike()) return true;
740
741 // If this is a function-like macro invocation, it's safe to trivially expand
742 // as long as the identifier is not a macro argument.
743 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
744 I != E; ++I)
745 if (*I == II)
746 return false; // Identifier is a macro argument.
747
748 return true;
749}
750
751
752/// isNextPPTokenLParen - Determine whether the next preprocessor token to be
753/// lexed is a '('. If so, consume the token and return true, if not, this
754/// method should have no observable side-effect on the lexed tokens.
755bool Preprocessor::isNextPPTokenLParen() {
756 // Do some quick tests for rejection cases.
757 unsigned Val;
758 if (CurLexer)
759 Val = CurLexer->isNextPPTokenLParen();
760 else
761 Val = CurMacroExpander->isNextTokenLParen();
762
763 if (Val == 2) {
Chris Lattner0ea793e2007-07-19 00:07:36 +0000764 // We have run off the end. If it's a source file we don't
765 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
766 // macro stack.
767 if (CurLexer)
768 return false;
769 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000770 IncludeStackInfo &Entry = IncludeMacroStack[i-1];
771 if (Entry.TheLexer)
772 Val = Entry.TheLexer->isNextPPTokenLParen();
773 else
774 Val = Entry.TheMacroExpander->isNextTokenLParen();
Chris Lattner0ea793e2007-07-19 00:07:36 +0000775
776 if (Val != 2)
777 break;
778
779 // Ran off the end of a source file?
780 if (Entry.TheLexer)
781 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000782 }
783 }
784
785 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
786 // have found something that isn't a '(' or we found the end of the
787 // translation unit. In either case, return false.
788 if (Val != 1)
789 return false;
790
Chris Lattnerd2177732007-07-20 16:59:19 +0000791 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000792 LexUnexpandedToken(Tok);
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000793 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000794 return true;
795}
796
797/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
798/// expanded as a macro, handle it and return the next token as 'Identifier'.
Chris Lattnerd2177732007-07-20 16:59:19 +0000799bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 MacroInfo *MI) {
801
802 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
803 if (MI->isBuiltinMacro()) {
804 ExpandBuiltinMacro(Identifier);
805 return false;
806 }
807
808 // If this is the first use of a target-specific macro, warn about it.
809 if (MI->isTargetSpecific()) {
810 MI->setIsTargetSpecific(false); // Don't warn on second use.
811 getTargetInfo().DiagnoseNonPortability(Identifier.getLocation(),
812 diag::port_target_macro_use);
813 }
814
815 /// Args - If this is a function-like macro expansion, this contains,
816 /// for each macro argument, the list of tokens that were provided to the
817 /// invocation.
818 MacroArgs *Args = 0;
819
820 // If this is a function-like macro, read the arguments.
821 if (MI->isFunctionLike()) {
822 // C99 6.10.3p10: If the preprocessing token immediately after the the macro
Chris Lattner2b64fdc2007-07-19 16:11:58 +0000823 // name isn't a '(', this macro should not be expanded. Otherwise, consume
824 // it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000825 if (!isNextPPTokenLParen())
826 return true;
827
828 // Remember that we are now parsing the arguments to a macro invocation.
829 // Preprocessor directives used inside macro arguments are not portable, and
830 // this enables the warning.
831 InMacroArgs = true;
832 Args = ReadFunctionLikeMacroArgs(Identifier, MI);
833
834 // Finished parsing args.
835 InMacroArgs = false;
836
837 // If there was an error parsing the arguments, bail out.
838 if (Args == 0) return false;
839
840 ++NumFnMacroExpanded;
841 } else {
842 ++NumMacroExpanded;
843 }
844
845 // Notice that this macro has been used.
846 MI->setIsUsed(true);
847
848 // If we started lexing a macro, enter the macro expansion body.
849
850 // If this macro expands to no tokens, don't bother to push it onto the
851 // expansion stack, only to take it right back off.
852 if (MI->getNumTokens() == 0) {
853 // No need for arg info.
854 if (Args) Args->destroy();
855
856 // Ignore this macro use, just return the next token in the current
857 // buffer.
858 bool HadLeadingSpace = Identifier.hasLeadingSpace();
859 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
860
861 Lex(Identifier);
862
863 // If the identifier isn't on some OTHER line, inherit the leading
864 // whitespace/first-on-a-line property of this token. This handles
865 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
866 // empty.
867 if (!Identifier.isAtStartOfLine()) {
Chris Lattnerd2177732007-07-20 16:59:19 +0000868 if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
869 if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000870 }
871 ++NumFastMacroExpanded;
872 return false;
873
874 } else if (MI->getNumTokens() == 1 &&
Chris Lattnercc1a8752007-10-07 08:44:20 +0000875 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
876 *this)){
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 // Otherwise, if this macro expands into a single trivially-expanded
878 // token: expand it now. This handles common cases like
879 // "#define VAL 42".
880
881 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
882 // identifier to the expanded token.
883 bool isAtStartOfLine = Identifier.isAtStartOfLine();
884 bool hasLeadingSpace = Identifier.hasLeadingSpace();
885
886 // Remember where the token is instantiated.
887 SourceLocation InstantiateLoc = Identifier.getLocation();
888
889 // Replace the result token.
890 Identifier = MI->getReplacementToken(0);
891
892 // Restore the StartOfLine/LeadingSpace markers.
Chris Lattnerd2177732007-07-20 16:59:19 +0000893 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
894 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000895
896 // Update the tokens location to include both its logical and physical
897 // locations.
898 SourceLocation Loc =
899 SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc);
900 Identifier.setLocation(Loc);
901
902 // If this is #define X X, we must mark the result as unexpandible.
903 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo())
Chris Lattnercc1a8752007-10-07 08:44:20 +0000904 if (getMacroInfo(NewII) == MI)
Chris Lattnerd2177732007-07-20 16:59:19 +0000905 Identifier.setFlag(Token::DisableExpand);
Reid Spencer5f016e22007-07-11 17:01:13 +0000906
907 // Since this is not an identifier token, it can't be macro expanded, so
908 // we're done.
909 ++NumFastMacroExpanded;
910 return false;
911 }
912
913 // Start expanding the macro.
914 EnterMacro(Identifier, Args);
915
916 // Now that the macro is at the top of the include stack, ask the
917 // preprocessor to read the next token from it.
918 Lex(Identifier);
919 return false;
920}
921
922/// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
923/// invoked to read all of the actual arguments specified for the macro
924/// invocation. This returns null on error.
Chris Lattnerd2177732007-07-20 16:59:19 +0000925MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
Reid Spencer5f016e22007-07-11 17:01:13 +0000926 MacroInfo *MI) {
927 // The number of fixed arguments to parse.
928 unsigned NumFixedArgsLeft = MI->getNumArgs();
929 bool isVariadic = MI->isVariadic();
930
931 // Outer loop, while there are more arguments, keep reading them.
Chris Lattnerd2177732007-07-20 16:59:19 +0000932 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000933 Tok.setKind(tok::comma);
934 --NumFixedArgsLeft; // Start reading the first arg.
935
936 // ArgTokens - Build up a list of tokens that make up each argument. Each
937 // argument is separated by an EOF token. Use a SmallVector so we can avoid
938 // heap allocations in the common case.
Chris Lattnerd2177732007-07-20 16:59:19 +0000939 llvm::SmallVector<Token, 64> ArgTokens;
Reid Spencer5f016e22007-07-11 17:01:13 +0000940
941 unsigned NumActuals = 0;
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000942 while (Tok.is(tok::comma)) {
Chris Lattner2b64fdc2007-07-19 16:11:58 +0000943 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
944 // that we already consumed the first one.
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 unsigned NumParens = 0;
946
947 while (1) {
948 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
949 // an argument value in a macro could expand to ',' or '(' or ')'.
950 LexUnexpandedToken(Tok);
951
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000952 if (Tok.is(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000953 Diag(MacroName, diag::err_unterm_macro_invoc);
954 // Do not lose the EOF. Return it to the client.
955 MacroName = Tok;
956 return 0;
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000957 } else if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000958 // If we found the ) token, the macro arg list is done.
959 if (NumParens-- == 0)
960 break;
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000961 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 ++NumParens;
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000963 } else if (Tok.is(tok::comma) && NumParens == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000964 // Comma ends this argument if there are more fixed arguments expected.
965 if (NumFixedArgsLeft)
966 break;
967
968 // If this is not a variadic macro, too many args were specified.
969 if (!isVariadic) {
970 // Emit the diagnostic at the macro name in case there is a missing ).
971 // Emitting it at the , could be far away from the macro name.
972 Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
973 return 0;
974 }
975 // Otherwise, continue to add the tokens to this variable argument.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000976 } else if (Tok.is(tok::comment) && !KeepMacroComments) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000977 // If this is a comment token in the argument list and we're just in
978 // -C mode (not -CC mode), discard the comment.
979 continue;
980 }
981
982 ArgTokens.push_back(Tok);
983 }
984
985 // Empty arguments are standard in C99 and supported as an extension in
986 // other modes.
987 if (ArgTokens.empty() && !Features.C99)
988 Diag(Tok, diag::ext_empty_fnmacro_arg);
989
990 // Add a marker EOF token to the end of the token list for this argument.
Chris Lattnerd2177732007-07-20 16:59:19 +0000991 Token EOFTok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000992 EOFTok.startToken();
993 EOFTok.setKind(tok::eof);
994 EOFTok.setLocation(Tok.getLocation());
995 EOFTok.setLength(0);
996 ArgTokens.push_back(EOFTok);
997 ++NumActuals;
998 --NumFixedArgsLeft;
999 };
1000
1001 // Okay, we either found the r_paren. Check to see if we parsed too few
1002 // arguments.
1003 unsigned MinArgsExpected = MI->getNumArgs();
1004
1005 // See MacroArgs instance var for description of this.
1006 bool isVarargsElided = false;
1007
1008 if (NumActuals < MinArgsExpected) {
1009 // There are several cases where too few arguments is ok, handle them now.
1010 if (NumActuals+1 == MinArgsExpected && MI->isVariadic()) {
1011 // Varargs where the named vararg parameter is missing: ok as extension.
1012 // #define A(x, ...)
1013 // A("blah")
1014 Diag(Tok, diag::ext_missing_varargs_arg);
1015
1016 // Remember this occurred if this is a C99 macro invocation with at least
1017 // one actual argument.
1018 isVarargsElided = MI->isC99Varargs() && MI->getNumArgs() > 1;
1019 } else if (MI->getNumArgs() == 1) {
1020 // #define A(x)
1021 // A()
1022 // is ok because it is an empty argument.
1023
1024 // Empty arguments are standard in C99 and supported as an extension in
1025 // other modes.
1026 if (ArgTokens.empty() && !Features.C99)
1027 Diag(Tok, diag::ext_empty_fnmacro_arg);
1028 } else {
1029 // Otherwise, emit the error.
1030 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
1031 return 0;
1032 }
1033
1034 // Add a marker EOF token to the end of the token list for this argument.
1035 SourceLocation EndLoc = Tok.getLocation();
1036 Tok.startToken();
1037 Tok.setKind(tok::eof);
1038 Tok.setLocation(EndLoc);
1039 Tok.setLength(0);
1040 ArgTokens.push_back(Tok);
1041 }
1042
1043 return MacroArgs::create(MI, &ArgTokens[0], ArgTokens.size(),isVarargsElided);
1044}
1045
1046/// ComputeDATE_TIME - Compute the current time, enter it into the specified
1047/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
1048/// the identifier tokens inserted.
1049static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
1050 Preprocessor &PP) {
1051 time_t TT = time(0);
1052 struct tm *TM = localtime(&TT);
1053
1054 static const char * const Months[] = {
1055 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
1056 };
1057
1058 char TmpBuffer[100];
1059 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
1060 TM->tm_year+1900);
1061 DATELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
1062
1063 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
1064 TIMELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
1065}
1066
1067/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1068/// as a builtin macro, handle it and return the next token as 'Tok'.
Chris Lattnerd2177732007-07-20 16:59:19 +00001069void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001070 // Figure out which token this is.
1071 IdentifierInfo *II = Tok.getIdentifierInfo();
1072 assert(II && "Can't be a macro without id info!");
1073
1074 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
1075 // lex the token after it.
1076 if (II == Ident_Pragma)
1077 return Handle_Pragma(Tok);
1078
1079 ++NumBuiltinMacroExpanded;
1080
1081 char TmpBuffer[100];
1082
1083 // Set up the return result.
1084 Tok.setIdentifierInfo(0);
Chris Lattnerd2177732007-07-20 16:59:19 +00001085 Tok.clearFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +00001086
1087 if (II == Ident__LINE__) {
1088 // __LINE__ expands to a simple numeric value.
Chris Lattner9dc1f532007-07-20 16:37:10 +00001089 sprintf(TmpBuffer, "%u", SourceMgr.getLogicalLineNumber(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 unsigned Length = strlen(TmpBuffer);
1091 Tok.setKind(tok::numeric_constant);
1092 Tok.setLength(Length);
1093 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
1094 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
1095 SourceLocation Loc = Tok.getLocation();
1096 if (II == Ident__BASE_FILE__) {
1097 Diag(Tok, diag::ext_pp_base_file);
Chris Lattner9dc1f532007-07-20 16:37:10 +00001098 SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc);
1099 while (NextLoc.isValid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 Loc = NextLoc;
Chris Lattner9dc1f532007-07-20 16:37:10 +00001101 NextLoc = SourceMgr.getIncludeLoc(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001102 }
1103 }
1104
1105 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
Chris Lattner9dc1f532007-07-20 16:37:10 +00001106 std::string FN = SourceMgr.getSourceName(SourceMgr.getLogicalLoc(Loc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001107 FN = '"' + Lexer::Stringify(FN) + '"';
1108 Tok.setKind(tok::string_literal);
1109 Tok.setLength(FN.size());
1110 Tok.setLocation(CreateString(&FN[0], FN.size(), Tok.getLocation()));
1111 } else if (II == Ident__DATE__) {
1112 if (!DATELoc.isValid())
1113 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1114 Tok.setKind(tok::string_literal);
1115 Tok.setLength(strlen("\"Mmm dd yyyy\""));
1116 Tok.setLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation()));
1117 } else if (II == Ident__TIME__) {
1118 if (!TIMELoc.isValid())
1119 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1120 Tok.setKind(tok::string_literal);
1121 Tok.setLength(strlen("\"hh:mm:ss\""));
1122 Tok.setLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
1123 } else if (II == Ident__INCLUDE_LEVEL__) {
1124 Diag(Tok, diag::ext_pp_include_level);
1125
1126 // Compute the include depth of this token.
1127 unsigned Depth = 0;
Chris Lattner9dc1f532007-07-20 16:37:10 +00001128 SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation());
1129 for (; Loc.isValid(); ++Depth)
1130 Loc = SourceMgr.getIncludeLoc(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001131
1132 // __INCLUDE_LEVEL__ expands to a simple numeric value.
1133 sprintf(TmpBuffer, "%u", Depth);
1134 unsigned Length = strlen(TmpBuffer);
1135 Tok.setKind(tok::numeric_constant);
1136 Tok.setLength(Length);
1137 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
1138 } else if (II == Ident__TIMESTAMP__) {
1139 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
1140 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1141 Diag(Tok, diag::ext_pp_timestamp);
1142
1143 // Get the file that we are lexing out of. If we're currently lexing from
1144 // a macro, dig into the include stack.
1145 const FileEntry *CurFile = 0;
1146 Lexer *TheLexer = getCurrentFileLexer();
1147
1148 if (TheLexer)
Chris Lattner9dc1f532007-07-20 16:37:10 +00001149 CurFile = SourceMgr.getFileEntryForLoc(TheLexer->getFileLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +00001150
1151 // If this file is older than the file it depends on, emit a diagnostic.
1152 const char *Result;
1153 if (CurFile) {
1154 time_t TT = CurFile->getModificationTime();
1155 struct tm *TM = localtime(&TT);
1156 Result = asctime(TM);
1157 } else {
1158 Result = "??? ??? ?? ??:??:?? ????\n";
1159 }
1160 TmpBuffer[0] = '"';
1161 strcpy(TmpBuffer+1, Result);
1162 unsigned Len = strlen(TmpBuffer);
1163 TmpBuffer[Len-1] = '"'; // Replace the newline with a quote.
1164 Tok.setKind(tok::string_literal);
1165 Tok.setLength(Len);
1166 Tok.setLocation(CreateString(TmpBuffer, Len, Tok.getLocation()));
1167 } else {
1168 assert(0 && "Unknown identifier!");
1169 }
1170}
1171
1172//===----------------------------------------------------------------------===//
1173// Lexer Event Handling.
1174//===----------------------------------------------------------------------===//
1175
1176/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
1177/// identifier information for the token and install it into the token.
Chris Lattnerd2177732007-07-20 16:59:19 +00001178IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
Reid Spencer5f016e22007-07-11 17:01:13 +00001179 const char *BufPtr) {
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001180 assert(Identifier.is(tok::identifier) && "Not an identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
1182
1183 // Look up this token, see if it is a macro, or if it is a language keyword.
1184 IdentifierInfo *II;
1185 if (BufPtr && !Identifier.needsCleaning()) {
1186 // No cleaning needed, just use the characters from the lexed buffer.
1187 II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
1188 } else {
1189 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
Chris Lattnerc35717a2007-07-13 17:10:38 +00001190 llvm::SmallVector<char, 64> IdentifierBuffer;
1191 IdentifierBuffer.resize(Identifier.getLength());
1192 const char *TmpBuf = &IdentifierBuffer[0];
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 unsigned Size = getSpelling(Identifier, TmpBuf);
1194 II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
1195 }
1196 Identifier.setIdentifierInfo(II);
1197 return II;
1198}
1199
1200
1201/// HandleIdentifier - This callback is invoked when the lexer reads an
1202/// identifier. This callback looks up the identifier in the map and/or
1203/// potentially macro expands it or turns it into a named token (like 'for').
Chris Lattnerd2177732007-07-20 16:59:19 +00001204void Preprocessor::HandleIdentifier(Token &Identifier) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001205 assert(Identifier.getIdentifierInfo() &&
1206 "Can't handle identifiers without identifier info!");
1207
1208 IdentifierInfo &II = *Identifier.getIdentifierInfo();
1209
1210 // If this identifier was poisoned, and if it was not produced from a macro
1211 // expansion, emit an error.
1212 if (II.isPoisoned() && CurLexer) {
1213 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
1214 Diag(Identifier, diag::err_pp_used_poisoned_id);
1215 else
1216 Diag(Identifier, diag::ext_pp_bad_vaargs_use);
1217 }
1218
1219 // If this is a macro to be expanded, do it.
Chris Lattnercc1a8752007-10-07 08:44:20 +00001220 if (MacroInfo *MI = getMacroInfo(&II)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
1222 if (MI->isEnabled()) {
1223 if (!HandleMacroExpandedIdentifier(Identifier, MI))
1224 return;
1225 } else {
1226 // C99 6.10.3.4p2 says that a disabled macro may never again be
1227 // expanded, even if it's in a context where it could be expanded in the
1228 // future.
Chris Lattnerd2177732007-07-20 16:59:19 +00001229 Identifier.setFlag(Token::DisableExpand);
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 }
1231 }
1232 } else if (II.isOtherTargetMacro() && !DisableMacroExpansion) {
1233 // If this identifier is a macro on some other target, emit a diagnostic.
1234 // This diagnosic is only emitted when macro expansion is enabled, because
1235 // the macro would not have been expanded for the other target either.
1236 II.setIsOtherTargetMacro(false); // Don't warn on second use.
1237 getTargetInfo().DiagnoseNonPortability(Identifier.getLocation(),
1238 diag::port_target_macro_use);
1239
1240 }
1241
1242 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
1243 // then we act as if it is the actual operator and not the textual
1244 // representation of it.
1245 if (II.isCPlusPlusOperatorKeyword())
1246 Identifier.setIdentifierInfo(0);
1247
1248 // Change the kind of this identifier to the appropriate token kind, e.g.
1249 // turning "for" into a keyword.
1250 Identifier.setKind(II.getTokenID());
1251
1252 // If this is an extension token, diagnose its use.
1253 // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99
1254 // For now, I'm just commenting it out (while I work on attributes).
1255 if (II.isExtensionToken() && Features.C99)
1256 Diag(Identifier, diag::ext_token_used);
1257}
1258
1259/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
1260/// the current file. This either returns the EOF token or pops a level off
1261/// the include stack and keeps going.
Chris Lattnerd2177732007-07-20 16:59:19 +00001262bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 assert(!CurMacroExpander &&
1264 "Ending a file when currently in a macro!");
1265
1266 // See if this file had a controlling macro.
1267 if (CurLexer) { // Not ending a macro, ignore it.
1268 if (const IdentifierInfo *ControllingMacro =
1269 CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
1270 // Okay, this has a controlling macro, remember in PerFileInfo.
1271 if (const FileEntry *FE =
Chris Lattner9dc1f532007-07-20 16:37:10 +00001272 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
1274 }
1275 }
1276
1277 // If this is a #include'd file, pop it off the include stack and continue
1278 // lexing the #includer file.
1279 if (!IncludeMacroStack.empty()) {
1280 // We're done with the #included file.
1281 RemoveTopOfLexerStack();
1282
1283 // Notify the client, if desired, that we are in a new source file.
1284 if (Callbacks && !isEndOfMacro && CurLexer) {
1285 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
1286
1287 // Get the file entry for the current file.
1288 if (const FileEntry *FE =
Chris Lattner9dc1f532007-07-20 16:37:10 +00001289 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001290 FileType = HeaderInfo.getFileDirFlavor(FE);
1291
1292 Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr),
1293 PPCallbacks::ExitFile, FileType);
1294 }
1295
1296 // Client should lex another token.
1297 return false;
1298 }
1299
1300 Result.startToken();
1301 CurLexer->BufferPtr = CurLexer->BufferEnd;
1302 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
1303 Result.setKind(tok::eof);
1304
1305 // We're done with the #included file.
1306 delete CurLexer;
1307 CurLexer = 0;
1308
1309 // This is the end of the top-level file. If the diag::pp_macro_not_used
Chris Lattnercc1a8752007-10-07 08:44:20 +00001310 // diagnostic is enabled, look for macros that have not been used.
Reid Spencer5f016e22007-07-11 17:01:13 +00001311 if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){
Chris Lattnercc1a8752007-10-07 08:44:20 +00001312 for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
1313 Macros.begin(), E = Macros.end(); I != E; ++I) {
1314 if (!I->second->isUsed())
1315 Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used);
Reid Spencer5f016e22007-07-11 17:01:13 +00001316 }
1317 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001318 return true;
1319}
1320
1321/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
1322/// the current macro expansion or token stream expansion.
Chris Lattnerd2177732007-07-20 16:59:19 +00001323bool Preprocessor::HandleEndOfMacro(Token &Result) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001324 assert(CurMacroExpander && !CurLexer &&
1325 "Ending a macro when currently in a #include file!");
1326
Chris Lattner9594acf2007-07-15 00:25:26 +00001327 // Delete or cache the now-dead macro expander.
1328 if (NumCachedMacroExpanders == MacroExpanderCacheSize)
1329 delete CurMacroExpander;
1330 else
1331 MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander;
Reid Spencer5f016e22007-07-11 17:01:13 +00001332
1333 // Handle this like a #include file being popped off the stack.
1334 CurMacroExpander = 0;
1335 return HandleEndOfFile(Result, true);
1336}
1337
1338
1339//===----------------------------------------------------------------------===//
1340// Utility Methods for Preprocessor Directive Handling.
1341//===----------------------------------------------------------------------===//
1342
1343/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
1344/// current line until the tok::eom token is found.
1345void Preprocessor::DiscardUntilEndOfDirective() {
Chris Lattnerd2177732007-07-20 16:59:19 +00001346 Token Tmp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001347 do {
1348 LexUnexpandedToken(Tmp);
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001349 } while (Tmp.isNot(tok::eom));
Reid Spencer5f016e22007-07-11 17:01:13 +00001350}
1351
1352/// isCXXNamedOperator - Returns "true" if the token is a named operator in C++.
1353static bool isCXXNamedOperator(const std::string &Spelling) {
1354 return Spelling == "and" || Spelling == "bitand" || Spelling == "bitor" ||
1355 Spelling == "compl" || Spelling == "not" || Spelling == "not_eq" ||
1356 Spelling == "or" || Spelling == "xor";
1357}
1358
1359/// ReadMacroName - Lex and validate a macro name, which occurs after a
1360/// #define or #undef. This sets the token kind to eom and discards the rest
1361/// of the macro line if the macro name is invalid. isDefineUndef is 1 if
1362/// this is due to a a #define, 2 if #undef directive, 0 if it is something
1363/// else (e.g. #ifdef).
Chris Lattnerd2177732007-07-20 16:59:19 +00001364void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001365 // Read the token, don't allow macro expansion on it.
1366 LexUnexpandedToken(MacroNameTok);
1367
1368 // Missing macro name?
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001369 if (MacroNameTok.is(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +00001370 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
1371
1372 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1373 if (II == 0) {
1374 std::string Spelling = getSpelling(MacroNameTok);
1375 if (isCXXNamedOperator(Spelling))
1376 // C++ 2.5p2: Alternative tokens behave the same as its primary token
1377 // except for their spellings.
1378 Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name, Spelling);
1379 else
1380 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
1381 // Fall through on error.
1382 } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
1383 // Error if defining "defined": C99 6.10.8.4.
1384 Diag(MacroNameTok, diag::err_defined_macro_name);
Chris Lattner0edde552007-10-07 08:04:56 +00001385 } else if (isDefineUndef && II->hasMacroDefinition() &&
Chris Lattnercc1a8752007-10-07 08:44:20 +00001386 getMacroInfo(II)->isBuiltinMacro()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001387 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
1388 if (isDefineUndef == 1)
1389 Diag(MacroNameTok, diag::pp_redef_builtin_macro);
1390 else
1391 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
1392 } else {
1393 // Okay, we got a good identifier node. Return it.
1394 return;
1395 }
1396
1397 // Invalid macro name, read and discard the rest of the line. Then set the
1398 // token kind to tok::eom.
1399 MacroNameTok.setKind(tok::eom);
1400 return DiscardUntilEndOfDirective();
1401}
1402
1403/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
1404/// not, emit a diagnostic and consume up until the eom.
1405void Preprocessor::CheckEndOfDirective(const char *DirType) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001406 Token Tmp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001407 Lex(Tmp);
1408 // There should be no tokens after the directive, but we allow them as an
1409 // extension.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001410 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
Reid Spencer5f016e22007-07-11 17:01:13 +00001411 Lex(Tmp);
1412
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001413 if (Tmp.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001414 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
1415 DiscardUntilEndOfDirective();
1416 }
1417}
1418
1419
1420
1421/// SkipExcludedConditionalBlock - We just read a #if or related directive and
1422/// decided that the subsequent tokens are in the #if'd out portion of the
1423/// file. Lex the rest of the file, until we see an #endif. If
1424/// FoundNonSkipPortion is true, then we have already emitted code for part of
1425/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
1426/// is true, then #else directives are ok, if not, then we have already seen one
1427/// so a #else directive is a duplicate. When this returns, the caller can lex
1428/// the first valid token.
1429void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
1430 bool FoundNonSkipPortion,
1431 bool FoundElse) {
1432 ++NumSkipped;
1433 assert(CurMacroExpander == 0 && CurLexer &&
1434 "Lexing a macro, not a file?");
1435
1436 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
1437 FoundNonSkipPortion, FoundElse);
1438
1439 // Enter raw mode to disable identifier lookup (and thus macro expansion),
1440 // disabling warnings, etc.
1441 CurLexer->LexingRawMode = true;
Chris Lattnerd2177732007-07-20 16:59:19 +00001442 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001443 while (1) {
1444 CurLexer->Lex(Tok);
1445
1446 // If this is the end of the buffer, we have an error.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001447 if (Tok.is(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001448 // Emit errors for each unterminated conditional on the stack, including
1449 // the current one.
1450 while (!CurLexer->ConditionalStack.empty()) {
1451 Diag(CurLexer->ConditionalStack.back().IfLoc,
1452 diag::err_pp_unterminated_conditional);
1453 CurLexer->ConditionalStack.pop_back();
1454 }
1455
1456 // Just return and let the caller lex after this #include.
1457 break;
1458 }
1459
1460 // If this token is not a preprocessor directive, just skip it.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001461 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
Reid Spencer5f016e22007-07-11 17:01:13 +00001462 continue;
1463
1464 // We just parsed a # character at the start of a line, so we're in
1465 // directive mode. Tell the lexer this so any newlines we see will be
1466 // converted into an EOM token (this terminates the macro).
1467 CurLexer->ParsingPreprocessorDirective = true;
1468 CurLexer->KeepCommentMode = false;
1469
1470
1471 // Read the next token, the directive flavor.
1472 LexUnexpandedToken(Tok);
1473
1474 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
1475 // something bogus), skip it.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001476 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001477 CurLexer->ParsingPreprocessorDirective = false;
1478 // Restore comment saving mode.
1479 CurLexer->KeepCommentMode = KeepComments;
1480 continue;
1481 }
1482
1483 // If the first letter isn't i or e, it isn't intesting to us. We know that
1484 // this is safe in the face of spelling differences, because there is no way
1485 // to spell an i/e in a strange way that is another letter. Skipping this
1486 // allows us to avoid looking up the identifier info for #define/#undef and
1487 // other common directives.
1488 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
1489 char FirstChar = RawCharData[0];
1490 if (FirstChar >= 'a' && FirstChar <= 'z' &&
1491 FirstChar != 'i' && FirstChar != 'e') {
1492 CurLexer->ParsingPreprocessorDirective = false;
1493 // Restore comment saving mode.
1494 CurLexer->KeepCommentMode = KeepComments;
1495 continue;
1496 }
1497
1498 // Get the identifier name without trigraphs or embedded newlines. Note
1499 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
1500 // when skipping.
1501 // TODO: could do this with zero copies in the no-clean case by using
1502 // strncmp below.
1503 char Directive[20];
1504 unsigned IdLen;
1505 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
1506 IdLen = Tok.getLength();
1507 memcpy(Directive, RawCharData, IdLen);
1508 Directive[IdLen] = 0;
1509 } else {
1510 std::string DirectiveStr = getSpelling(Tok);
1511 IdLen = DirectiveStr.size();
1512 if (IdLen >= 20) {
1513 CurLexer->ParsingPreprocessorDirective = false;
1514 // Restore comment saving mode.
1515 CurLexer->KeepCommentMode = KeepComments;
1516 continue;
1517 }
1518 memcpy(Directive, &DirectiveStr[0], IdLen);
1519 Directive[IdLen] = 0;
1520 }
1521
1522 if (FirstChar == 'i' && Directive[1] == 'f') {
1523 if ((IdLen == 2) || // "if"
1524 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
1525 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
1526 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
1527 // bother parsing the condition.
1528 DiscardUntilEndOfDirective();
1529 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
1530 /*foundnonskip*/false,
1531 /*fnddelse*/false);
1532 }
1533 } else if (FirstChar == 'e') {
1534 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
1535 CheckEndOfDirective("#endif");
1536 PPConditionalInfo CondInfo;
1537 CondInfo.WasSkipping = true; // Silence bogus warning.
1538 bool InCond = CurLexer->popConditionalLevel(CondInfo);
1539 InCond = InCond; // Silence warning in no-asserts mode.
1540 assert(!InCond && "Can't be skipping if not in a conditional!");
1541
1542 // If we popped the outermost skipping block, we're done skipping!
1543 if (!CondInfo.WasSkipping)
1544 break;
1545 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
1546 // #else directive in a skipping conditional. If not in some other
1547 // skipping conditional, and if #else hasn't already been seen, enter it
1548 // as a non-skipping conditional.
1549 CheckEndOfDirective("#else");
1550 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1551
1552 // If this is a #else with a #else before it, report the error.
1553 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
1554
1555 // Note that we've seen a #else in this conditional.
1556 CondInfo.FoundElse = true;
1557
1558 // If the conditional is at the top level, and the #if block wasn't
1559 // entered, enter the #else block now.
1560 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
1561 CondInfo.FoundNonSkip = true;
1562 break;
1563 }
1564 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
1565 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1566
1567 bool ShouldEnter;
1568 // If this is in a skipping block or if we're already handled this #if
1569 // block, don't bother parsing the condition.
1570 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
1571 DiscardUntilEndOfDirective();
1572 ShouldEnter = false;
1573 } else {
1574 // Restore the value of LexingRawMode so that identifiers are
1575 // looked up, etc, inside the #elif expression.
1576 assert(CurLexer->LexingRawMode && "We have to be skipping here!");
1577 CurLexer->LexingRawMode = false;
1578 IdentifierInfo *IfNDefMacro = 0;
1579 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
1580 CurLexer->LexingRawMode = true;
1581 }
1582
1583 // If this is a #elif with a #else before it, report the error.
1584 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
1585
1586 // If this condition is true, enter it!
1587 if (ShouldEnter) {
1588 CondInfo.FoundNonSkip = true;
1589 break;
1590 }
1591 }
1592 }
1593
1594 CurLexer->ParsingPreprocessorDirective = false;
1595 // Restore comment saving mode.
1596 CurLexer->KeepCommentMode = KeepComments;
1597 }
1598
1599 // Finally, if we are out of the conditional (saw an #endif or ran off the end
1600 // of the file, just stop skipping and return to lexing whatever came after
1601 // the #if block.
1602 CurLexer->LexingRawMode = false;
1603}
1604
1605//===----------------------------------------------------------------------===//
1606// Preprocessor Directive Handling.
1607//===----------------------------------------------------------------------===//
1608
1609/// HandleDirective - This callback is invoked when the lexer sees a # token
1610/// at the start of a line. This consumes the directive, modifies the
1611/// lexer/preprocessor state, and advances the lexer(s) so that the next token
1612/// read is the correct one.
Chris Lattnerd2177732007-07-20 16:59:19 +00001613void Preprocessor::HandleDirective(Token &Result) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001614 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
1615
1616 // We just parsed a # character at the start of a line, so we're in directive
1617 // mode. Tell the lexer this so any newlines we see will be converted into an
1618 // EOM token (which terminates the directive).
1619 CurLexer->ParsingPreprocessorDirective = true;
1620
1621 ++NumDirectives;
1622
1623 // We are about to read a token. For the multiple-include optimization FA to
1624 // work, we have to remember if we had read any tokens *before* this
1625 // pp-directive.
1626 bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal();
1627
1628 // Read the next token, the directive flavor. This isn't expanded due to
1629 // C99 6.10.3p8.
1630 LexUnexpandedToken(Result);
1631
1632 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
1633 // #define A(x) #x
1634 // A(abc
1635 // #warning blah
1636 // def)
1637 // If so, the user is relying on non-portable behavior, emit a diagnostic.
1638 if (InMacroArgs)
1639 Diag(Result, diag::ext_embedded_directive);
1640
1641TryAgain:
1642 switch (Result.getKind()) {
1643 case tok::eom:
1644 return; // null directive.
1645 case tok::comment:
1646 // Handle stuff like "# /*foo*/ define X" in -E -C mode.
1647 LexUnexpandedToken(Result);
1648 goto TryAgain;
1649
1650 case tok::numeric_constant:
1651 // FIXME: implement # 7 line numbers!
1652 DiscardUntilEndOfDirective();
1653 return;
1654 default:
1655 IdentifierInfo *II = Result.getIdentifierInfo();
1656 if (II == 0) break; // Not an identifier.
1657
1658 // Ask what the preprocessor keyword ID is.
1659 switch (II->getPPKeywordID()) {
1660 default: break;
1661 // C99 6.10.1 - Conditional Inclusion.
1662 case tok::pp_if:
1663 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
1664 case tok::pp_ifdef:
1665 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
1666 case tok::pp_ifndef:
1667 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
1668 case tok::pp_elif:
1669 return HandleElifDirective(Result);
1670 case tok::pp_else:
1671 return HandleElseDirective(Result);
1672 case tok::pp_endif:
1673 return HandleEndifDirective(Result);
1674
1675 // C99 6.10.2 - Source File Inclusion.
1676 case tok::pp_include:
1677 return HandleIncludeDirective(Result); // Handle #include.
1678
1679 // C99 6.10.3 - Macro Replacement.
1680 case tok::pp_define:
1681 return HandleDefineDirective(Result, false);
1682 case tok::pp_undef:
1683 return HandleUndefDirective(Result);
1684
1685 // C99 6.10.4 - Line Control.
1686 case tok::pp_line:
1687 // FIXME: implement #line
1688 DiscardUntilEndOfDirective();
1689 return;
1690
1691 // C99 6.10.5 - Error Directive.
1692 case tok::pp_error:
1693 return HandleUserDiagnosticDirective(Result, false);
1694
1695 // C99 6.10.6 - Pragma Directive.
1696 case tok::pp_pragma:
1697 return HandlePragmaDirective();
1698
1699 // GNU Extensions.
1700 case tok::pp_import:
1701 return HandleImportDirective(Result);
1702 case tok::pp_include_next:
1703 return HandleIncludeNextDirective(Result);
1704
1705 case tok::pp_warning:
1706 Diag(Result, diag::ext_pp_warning_directive);
1707 return HandleUserDiagnosticDirective(Result, true);
1708 case tok::pp_ident:
1709 return HandleIdentSCCSDirective(Result);
1710 case tok::pp_sccs:
1711 return HandleIdentSCCSDirective(Result);
1712 case tok::pp_assert:
1713 //isExtension = true; // FIXME: implement #assert
1714 break;
1715 case tok::pp_unassert:
1716 //isExtension = true; // FIXME: implement #unassert
1717 break;
1718
1719 // clang extensions.
1720 case tok::pp_define_target:
1721 return HandleDefineDirective(Result, true);
1722 case tok::pp_define_other_target:
1723 return HandleDefineOtherTargetDirective(Result);
1724 }
1725 break;
1726 }
1727
1728 // If we reached here, the preprocessing token is not valid!
1729 Diag(Result, diag::err_pp_invalid_directive);
1730
1731 // Read the rest of the PP line.
1732 DiscardUntilEndOfDirective();
1733
1734 // Okay, we're done parsing the directive.
1735}
1736
Chris Lattnerd2177732007-07-20 16:59:19 +00001737void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
Reid Spencer5f016e22007-07-11 17:01:13 +00001738 bool isWarning) {
1739 // Read the rest of the line raw. We do this because we don't want macros
1740 // to be expanded and we don't require that the tokens be valid preprocessing
1741 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1742 // collapse multiple consequtive white space between tokens, but this isn't
1743 // specified by the standard.
1744 std::string Message = CurLexer->ReadToEndOfLine();
1745
1746 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
1747 return Diag(Tok, DiagID, Message);
1748}
1749
1750/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1751///
Chris Lattnerd2177732007-07-20 16:59:19 +00001752void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001753 // Yes, this directive is an extension.
1754 Diag(Tok, diag::ext_pp_ident_directive);
1755
1756 // Read the string argument.
Chris Lattnerd2177732007-07-20 16:59:19 +00001757 Token StrTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001758 Lex(StrTok);
1759
1760 // If the token kind isn't a string, it's a malformed directive.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001761 if (StrTok.isNot(tok::string_literal) &&
1762 StrTok.isNot(tok::wide_string_literal))
Reid Spencer5f016e22007-07-11 17:01:13 +00001763 return Diag(StrTok, diag::err_pp_malformed_ident);
1764
1765 // Verify that there is nothing after the string, other than EOM.
1766 CheckEndOfDirective("#ident");
1767
1768 if (Callbacks)
1769 Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok));
1770}
1771
1772//===----------------------------------------------------------------------===//
1773// Preprocessor Include Directive Handling.
1774//===----------------------------------------------------------------------===//
1775
1776/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1777/// checked and spelled filename, e.g. as an operand of #include. This returns
1778/// true if the input filename was in <>'s or false if it were in ""'s. The
1779/// caller is expected to provide a buffer that is large enough to hold the
1780/// spelling of the filename, but is also expected to handle the case when
1781/// this method decides to use a different buffer.
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001782bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +00001783 const char *&BufStart,
1784 const char *&BufEnd) {
1785 // Get the text form of the filename.
Reid Spencer5f016e22007-07-11 17:01:13 +00001786 assert(BufStart != BufEnd && "Can't have tokens with empty spellings!");
1787
1788 // Make sure the filename is <x> or "x".
1789 bool isAngled;
1790 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 = true;
1797 } else if (BufStart[0] == '"') {
1798 if (BufEnd[-1] != '"') {
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001799 Diag(Loc, diag::err_pp_expects_filename);
Reid Spencer5f016e22007-07-11 17:01:13 +00001800 BufStart = 0;
1801 return true;
1802 }
1803 isAngled = false;
1804 } else {
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001805 Diag(Loc, diag::err_pp_expects_filename);
Reid Spencer5f016e22007-07-11 17:01:13 +00001806 BufStart = 0;
1807 return true;
1808 }
1809
1810 // Diagnose #include "" as invalid.
1811 if (BufEnd-BufStart <= 2) {
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001812 Diag(Loc, diag::err_pp_empty_filename);
Reid Spencer5f016e22007-07-11 17:01:13 +00001813 BufStart = 0;
1814 return "";
1815 }
1816
1817 // Skip the brackets.
1818 ++BufStart;
1819 --BufEnd;
1820 return isAngled;
1821}
1822
Chris Lattner706ab502007-07-23 04:56:47 +00001823/// ConcatenateIncludeName - Handle cases where the #include name is expanded
1824/// from a macro as multiple tokens, which need to be glued together. This
1825/// occurs for code like:
1826/// #define FOO <a/b.h>
1827/// #include FOO
1828/// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1829///
1830/// This code concatenates and consumes tokens up to the '>' token. It returns
1831/// false if the > was found, otherwise it returns true if it finds and consumes
1832/// the EOM marker.
1833static bool ConcatenateIncludeName(llvm::SmallVector<char, 128> &FilenameBuffer,
1834 Preprocessor &PP) {
1835 Token CurTok;
1836
1837 PP.Lex(CurTok);
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001838 while (CurTok.isNot(tok::eom)) {
Chris Lattner706ab502007-07-23 04:56:47 +00001839 // Append the spelling of this token to the buffer. If there was a space
1840 // before it, add it now.
1841 if (CurTok.hasLeadingSpace())
1842 FilenameBuffer.push_back(' ');
1843
1844 // Get the spelling of the token, directly into FilenameBuffer if possible.
1845 unsigned PreAppendSize = FilenameBuffer.size();
1846 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1847
1848 const char *BufPtr = &FilenameBuffer[PreAppendSize];
1849 unsigned ActualLen = PP.getSpelling(CurTok, BufPtr);
1850
1851 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1852 if (BufPtr != &FilenameBuffer[PreAppendSize])
1853 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1854
1855 // Resize FilenameBuffer to the correct size.
1856 if (CurTok.getLength() != ActualLen)
1857 FilenameBuffer.resize(PreAppendSize+ActualLen);
1858
1859 // If we found the '>' marker, return success.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001860 if (CurTok.is(tok::greater))
Chris Lattner706ab502007-07-23 04:56:47 +00001861 return false;
1862
1863 PP.Lex(CurTok);
1864 }
1865
1866 // If we hit the eom marker, emit an error and return true so that the caller
1867 // knows the EOM has been read.
1868 PP.Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1869 return true;
1870}
1871
Reid Spencer5f016e22007-07-11 17:01:13 +00001872/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1873/// file to be included from the lexer, then include it! This is a common
1874/// routine with functionality shared between #include, #include_next and
1875/// #import.
Chris Lattnerd2177732007-07-20 16:59:19 +00001876void Preprocessor::HandleIncludeDirective(Token &IncludeTok,
Reid Spencer5f016e22007-07-11 17:01:13 +00001877 const DirectoryLookup *LookupFrom,
1878 bool isImport) {
1879
Chris Lattnerd2177732007-07-20 16:59:19 +00001880 Token FilenameTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001881 CurLexer->LexIncludeFilename(FilenameTok);
1882
Reid Spencer5f016e22007-07-11 17:01:13 +00001883 // Reserve a buffer to get the spelling.
1884 llvm::SmallVector<char, 128> FilenameBuffer;
Chris Lattner706ab502007-07-23 04:56:47 +00001885 const char *FilenameStart, *FilenameEnd;
1886
1887 switch (FilenameTok.getKind()) {
1888 case tok::eom:
1889 // If the token kind is EOM, the error has already been diagnosed.
1890 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001891
Chris Lattner706ab502007-07-23 04:56:47 +00001892 case tok::angle_string_literal:
Chris Lattnerf11ccfc2007-07-23 22:23:52 +00001893 case tok::string_literal: {
Chris Lattner706ab502007-07-23 04:56:47 +00001894 FilenameBuffer.resize(FilenameTok.getLength());
1895 FilenameStart = &FilenameBuffer[0];
1896 unsigned Len = getSpelling(FilenameTok, FilenameStart);
1897 FilenameEnd = FilenameStart+Len;
1898 break;
Chris Lattnerf11ccfc2007-07-23 22:23:52 +00001899 }
Chris Lattner706ab502007-07-23 04:56:47 +00001900
1901 case tok::less:
1902 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1903 // case, glue the tokens together into FilenameBuffer and interpret those.
1904 FilenameBuffer.push_back('<');
1905 if (ConcatenateIncludeName(FilenameBuffer, *this))
1906 return; // Found <eom> but no ">"? Diagnostic already emitted.
1907 FilenameStart = &FilenameBuffer[0];
1908 FilenameEnd = &FilenameBuffer[FilenameBuffer.size()];
1909 break;
1910 default:
1911 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1912 DiscardUntilEndOfDirective();
1913 return;
1914 }
1915
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001916 bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +00001917 FilenameStart, FilenameEnd);
1918 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1919 // error.
Chris Lattner706ab502007-07-23 04:56:47 +00001920 if (FilenameStart == 0) {
1921 DiscardUntilEndOfDirective();
Reid Spencer5f016e22007-07-11 17:01:13 +00001922 return;
Chris Lattner706ab502007-07-23 04:56:47 +00001923 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001924
1925 // Verify that there is nothing after the filename, other than EOM. Use the
1926 // preprocessor to lex this in case lexing the filename entered a macro.
1927 CheckEndOfDirective("#include");
1928
1929 // Check that we don't have infinite #include recursion.
1930 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
1931 return Diag(FilenameTok, diag::err_pp_include_too_deep);
1932
1933 // Search include directories.
1934 const DirectoryLookup *CurDir;
1935 const FileEntry *File = LookupFile(FilenameStart, FilenameEnd,
1936 isAngled, LookupFrom, CurDir);
1937 if (File == 0)
1938 return Diag(FilenameTok, diag::err_pp_file_not_found,
1939 std::string(FilenameStart, FilenameEnd));
1940
1941 // Ask HeaderInfo if we should enter this #include file.
1942 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
1943 // If it returns true, #including this file will have no effect.
1944 return;
1945 }
1946
1947 // Look up the file, create a File ID for it.
1948 unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation());
1949 if (FileID == 0)
1950 return Diag(FilenameTok, diag::err_pp_file_not_found,
1951 std::string(FilenameStart, FilenameEnd));
1952
1953 // Finally, if all is good, enter the new file!
1954 EnterSourceFile(FileID, CurDir);
1955}
1956
1957/// HandleIncludeNextDirective - Implements #include_next.
1958///
Chris Lattnerd2177732007-07-20 16:59:19 +00001959void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001960 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
1961
1962 // #include_next is like #include, except that we start searching after
1963 // the current found directory. If we can't do this, issue a
1964 // diagnostic.
1965 const DirectoryLookup *Lookup = CurDirLookup;
1966 if (isInPrimaryFile()) {
1967 Lookup = 0;
1968 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1969 } else if (Lookup == 0) {
1970 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1971 } else {
1972 // Start looking up in the next directory.
1973 ++Lookup;
1974 }
1975
1976 return HandleIncludeDirective(IncludeNextTok, Lookup);
1977}
1978
1979/// HandleImportDirective - Implements #import.
1980///
Chris Lattnerd2177732007-07-20 16:59:19 +00001981void Preprocessor::HandleImportDirective(Token &ImportTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001982 Diag(ImportTok, diag::ext_pp_import_directive);
1983
1984 return HandleIncludeDirective(ImportTok, 0, true);
1985}
1986
1987//===----------------------------------------------------------------------===//
1988// Preprocessor Macro Directive Handling.
1989//===----------------------------------------------------------------------===//
1990
1991/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1992/// definition has just been read. Lex the rest of the arguments and the
1993/// closing ), updating MI with what we learn. Return true if an error occurs
1994/// parsing the arg list.
1995bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
Chris Lattner25c96482007-07-14 22:46:43 +00001996 llvm::SmallVector<IdentifierInfo*, 32> Arguments;
1997
Chris Lattnerd2177732007-07-20 16:59:19 +00001998 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001999 while (1) {
2000 LexUnexpandedToken(Tok);
2001 switch (Tok.getKind()) {
2002 case tok::r_paren:
2003 // Found the end of the argument list.
Chris Lattner25c96482007-07-14 22:46:43 +00002004 if (Arguments.empty()) { // #define FOO()
2005 MI->setArgumentList(Arguments.begin(), Arguments.end());
2006 return false;
2007 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002008 // Otherwise we have #define FOO(A,)
2009 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
2010 return true;
2011 case tok::ellipsis: // #define X(... -> C99 varargs
2012 // Warn if use of C99 feature in non-C99 mode.
2013 if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
2014
2015 // Lex the token after the identifier.
2016 LexUnexpandedToken(Tok);
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002017 if (Tok.isNot(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002018 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2019 return true;
2020 }
2021 // Add the __VA_ARGS__ identifier as an argument.
Chris Lattner25c96482007-07-14 22:46:43 +00002022 Arguments.push_back(Ident__VA_ARGS__);
Reid Spencer5f016e22007-07-11 17:01:13 +00002023 MI->setIsC99Varargs();
Chris Lattner25c96482007-07-14 22:46:43 +00002024 MI->setArgumentList(Arguments.begin(), Arguments.end());
Reid Spencer5f016e22007-07-11 17:01:13 +00002025 return false;
2026 case tok::eom: // #define X(
2027 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2028 return true;
2029 default:
2030 // Handle keywords and identifiers here to accept things like
2031 // #define Foo(for) for.
2032 IdentifierInfo *II = Tok.getIdentifierInfo();
2033 if (II == 0) {
2034 // #define X(1
2035 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
2036 return true;
2037 }
2038
2039 // If this is already used as an argument, it is used multiple times (e.g.
2040 // #define X(A,A.
Chris Lattner25c96482007-07-14 22:46:43 +00002041 if (std::find(Arguments.begin(), Arguments.end(), II) !=
2042 Arguments.end()) { // C99 6.10.3p6
Reid Spencer5f016e22007-07-11 17:01:13 +00002043 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName());
2044 return true;
2045 }
2046
2047 // Add the argument to the macro info.
Chris Lattner25c96482007-07-14 22:46:43 +00002048 Arguments.push_back(II);
Reid Spencer5f016e22007-07-11 17:01:13 +00002049
2050 // Lex the token after the identifier.
2051 LexUnexpandedToken(Tok);
2052
2053 switch (Tok.getKind()) {
2054 default: // #define X(A B
2055 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
2056 return true;
2057 case tok::r_paren: // #define X(A)
Chris Lattner25c96482007-07-14 22:46:43 +00002058 MI->setArgumentList(Arguments.begin(), Arguments.end());
Reid Spencer5f016e22007-07-11 17:01:13 +00002059 return false;
2060 case tok::comma: // #define X(A,
2061 break;
2062 case tok::ellipsis: // #define X(A... -> GCC extension
2063 // Diagnose extension.
2064 Diag(Tok, diag::ext_named_variadic_macro);
2065
2066 // Lex the token after the identifier.
2067 LexUnexpandedToken(Tok);
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002068 if (Tok.isNot(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002069 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2070 return true;
2071 }
2072
2073 MI->setIsGNUVarargs();
Chris Lattner25c96482007-07-14 22:46:43 +00002074 MI->setArgumentList(Arguments.begin(), Arguments.end());
Reid Spencer5f016e22007-07-11 17:01:13 +00002075 return false;
2076 }
2077 }
2078 }
2079}
2080
2081/// HandleDefineDirective - Implements #define. This consumes the entire macro
2082/// line then lets the caller lex the next real token. If 'isTargetSpecific' is
2083/// true, then this is a "#define_target", otherwise this is a "#define".
2084///
Chris Lattnerd2177732007-07-20 16:59:19 +00002085void Preprocessor::HandleDefineDirective(Token &DefineTok,
Reid Spencer5f016e22007-07-11 17:01:13 +00002086 bool isTargetSpecific) {
2087 ++NumDefined;
2088
Chris Lattnerd2177732007-07-20 16:59:19 +00002089 Token MacroNameTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00002090 ReadMacroName(MacroNameTok, 1);
2091
2092 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002093 if (MacroNameTok.is(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +00002094 return;
Chris Lattnerc215bd62007-07-14 22:11:41 +00002095
Reid Spencer5f016e22007-07-11 17:01:13 +00002096 // If we are supposed to keep comments in #defines, reenable comment saving
2097 // mode.
2098 CurLexer->KeepCommentMode = KeepMacroComments;
2099
2100 // Create the new macro.
2101 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
2102 if (isTargetSpecific) MI->setIsTargetSpecific();
2103
2104 // If the identifier is an 'other target' macro, clear this bit.
2105 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
2106
2107
Chris Lattnerd2177732007-07-20 16:59:19 +00002108 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00002109 LexUnexpandedToken(Tok);
2110
2111 // If this is a function-like macro definition, parse the argument list,
2112 // marking each of the identifiers as being used as macro arguments. Also,
2113 // check other constraints on the first token of the macro body.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002114 if (Tok.is(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002115 // If there is no body to this macro, we have no special handling here.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002116 } else if (Tok.is(tok::l_paren) && !Tok.hasLeadingSpace()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002117 // This is a function-like macro definition. Read the argument list.
2118 MI->setIsFunctionLike();
2119 if (ReadMacroDefinitionArgList(MI)) {
2120 // Forget about MI.
2121 delete MI;
2122 // Throw away the rest of the line.
2123 if (CurLexer->ParsingPreprocessorDirective)
2124 DiscardUntilEndOfDirective();
2125 return;
2126 }
2127
2128 // Read the first token after the arg list for down below.
2129 LexUnexpandedToken(Tok);
2130 } else if (!Tok.hasLeadingSpace()) {
2131 // C99 requires whitespace between the macro definition and the body. Emit
2132 // a diagnostic for something like "#define X+".
2133 if (Features.C99) {
2134 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
2135 } else {
2136 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
2137 // one in some cases!
2138 }
2139 } else {
2140 // This is a normal token with leading space. Clear the leading space
2141 // marker on the first token to get proper expansion.
Chris Lattnerd2177732007-07-20 16:59:19 +00002142 Tok.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00002143 }
2144
2145 // If this is a definition of a variadic C99 function-like macro, not using
2146 // the GNU named varargs extension, enabled __VA_ARGS__.
2147
2148 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
2149 // This gets unpoisoned where it is allowed.
2150 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
2151 if (MI->isC99Varargs())
2152 Ident__VA_ARGS__->setIsPoisoned(false);
2153
2154 // Read the rest of the macro body.
Chris Lattnerb5e240f2007-07-14 21:54:03 +00002155 if (MI->isObjectLike()) {
2156 // Object-like macros are very simple, just read their body.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002157 while (Tok.isNot(tok::eom)) {
Chris Lattnerb5e240f2007-07-14 21:54:03 +00002158 MI->AddTokenToBody(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +00002159 // Get the next token of the macro.
2160 LexUnexpandedToken(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +00002161 }
2162
Chris Lattnerb5e240f2007-07-14 21:54:03 +00002163 } else {
2164 // Otherwise, read the body of a function-like macro. This has to validate
2165 // the # (stringize) operator.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002166 while (Tok.isNot(tok::eom)) {
Chris Lattnerb5e240f2007-07-14 21:54:03 +00002167 MI->AddTokenToBody(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +00002168
Chris Lattnerb5e240f2007-07-14 21:54:03 +00002169 // Check C99 6.10.3.2p1: ensure that # operators are followed by macro
2170 // parameters in function-like macro expansions.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002171 if (Tok.isNot(tok::hash)) {
Chris Lattnerb5e240f2007-07-14 21:54:03 +00002172 // Get the next token of the macro.
2173 LexUnexpandedToken(Tok);
2174 continue;
2175 }
2176
2177 // Get the next token of the macro.
2178 LexUnexpandedToken(Tok);
2179
2180 // Not a macro arg identifier?
2181 if (!Tok.getIdentifierInfo() ||
2182 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
2183 Diag(Tok, diag::err_pp_stringize_not_parameter);
2184 delete MI;
2185
2186 // Disable __VA_ARGS__ again.
2187 Ident__VA_ARGS__->setIsPoisoned(true);
2188 return;
2189 }
2190
2191 // Things look ok, add the param name token to the macro.
2192 MI->AddTokenToBody(Tok);
2193
2194 // Get the next token of the macro.
2195 LexUnexpandedToken(Tok);
2196 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002197 }
2198
Chris Lattnerc215bd62007-07-14 22:11:41 +00002199
Reid Spencer5f016e22007-07-11 17:01:13 +00002200 // Disable __VA_ARGS__ again.
2201 Ident__VA_ARGS__->setIsPoisoned(true);
2202
2203 // Check that there is no paste (##) operator at the begining or end of the
2204 // replacement list.
2205 unsigned NumTokens = MI->getNumTokens();
2206 if (NumTokens != 0) {
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002207 if (MI->getReplacementToken(0).is(tok::hashhash)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002208 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
2209 delete MI;
2210 return;
2211 }
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002212 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002213 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
2214 delete MI;
2215 return;
2216 }
2217 }
2218
2219 // If this is the primary source file, remember that this macro hasn't been
2220 // used yet.
2221 if (isInPrimaryFile())
2222 MI->setIsUsed(false);
2223
2224 // Finally, if this identifier already had a macro defined for it, verify that
2225 // the macro bodies are identical and free the old definition.
Chris Lattnercc1a8752007-10-07 08:44:20 +00002226 if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002227 if (!OtherMI->isUsed())
2228 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2229
2230 // Macros must be identical. This means all tokes and whitespace separation
2231 // must be the same. C99 6.10.3.2.
2232 if (!MI->isIdenticalTo(*OtherMI, *this)) {
2233 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef,
2234 MacroNameTok.getIdentifierInfo()->getName());
2235 Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2);
2236 }
2237 delete OtherMI;
2238 }
2239
Chris Lattnercc1a8752007-10-07 08:44:20 +00002240 setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
Reid Spencer5f016e22007-07-11 17:01:13 +00002241}
2242
2243/// HandleDefineOtherTargetDirective - Implements #define_other_target.
Chris Lattnerd2177732007-07-20 16:59:19 +00002244void Preprocessor::HandleDefineOtherTargetDirective(Token &Tok) {
2245 Token MacroNameTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00002246 ReadMacroName(MacroNameTok, 1);
2247
2248 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002249 if (MacroNameTok.is(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +00002250 return;
2251
2252 // Check to see if this is the last token on the #undef line.
2253 CheckEndOfDirective("#define_other_target");
2254
2255 // If there is already a macro defined by this name, turn it into a
2256 // target-specific define.
Chris Lattnercc1a8752007-10-07 08:44:20 +00002257 if (MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002258 MI->setIsTargetSpecific(true);
2259 return;
2260 }
2261
2262 // Mark the identifier as being a macro on some other target.
2263 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro();
2264}
2265
2266
2267/// HandleUndefDirective - Implements #undef.
2268///
Chris Lattnerd2177732007-07-20 16:59:19 +00002269void Preprocessor::HandleUndefDirective(Token &UndefTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002270 ++NumUndefined;
2271
Chris Lattnerd2177732007-07-20 16:59:19 +00002272 Token MacroNameTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00002273 ReadMacroName(MacroNameTok, 2);
2274
2275 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002276 if (MacroNameTok.is(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +00002277 return;
2278
2279 // Check to see if this is the last token on the #undef line.
2280 CheckEndOfDirective("#undef");
2281
2282 // Okay, we finally have a valid identifier to undef.
Chris Lattnercc1a8752007-10-07 08:44:20 +00002283 MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
Reid Spencer5f016e22007-07-11 17:01:13 +00002284
2285 // #undef untaints an identifier if it were marked by define_other_target.
2286 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
2287
2288 // If the macro is not defined, this is a noop undef, just return.
2289 if (MI == 0) return;
2290
2291 if (!MI->isUsed())
2292 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
2293
2294 // Free macro definition.
2295 delete MI;
Chris Lattnercc1a8752007-10-07 08:44:20 +00002296 setMacroInfo(MacroNameTok.getIdentifierInfo(), 0);
Reid Spencer5f016e22007-07-11 17:01:13 +00002297}
2298
2299
2300//===----------------------------------------------------------------------===//
2301// Preprocessor Conditional Directive Handling.
2302//===----------------------------------------------------------------------===//
2303
2304/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
2305/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
2306/// if any tokens have been returned or pp-directives activated before this
2307/// #ifndef has been lexed.
2308///
Chris Lattnerd2177732007-07-20 16:59:19 +00002309void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
Reid Spencer5f016e22007-07-11 17:01:13 +00002310 bool ReadAnyTokensBeforeDirective) {
2311 ++NumIf;
Chris Lattnerd2177732007-07-20 16:59:19 +00002312 Token DirectiveTok = Result;
Reid Spencer5f016e22007-07-11 17:01:13 +00002313
Chris Lattnerd2177732007-07-20 16:59:19 +00002314 Token MacroNameTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00002315 ReadMacroName(MacroNameTok);
2316
2317 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002318 if (MacroNameTok.is(tok::eom)) {
Chris Lattnerf37bb252007-09-24 05:14:57 +00002319 // Skip code until we get to #endif. This helps with recovery by not
2320 // emitting an error when the #endif is reached.
2321 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2322 /*Foundnonskip*/false, /*FoundElse*/false);
Reid Spencer5f016e22007-07-11 17:01:13 +00002323 return;
Chris Lattnerf37bb252007-09-24 05:14:57 +00002324 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002325
2326 // Check to see if this is the last token on the #if[n]def line.
2327 CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
2328
2329 // If the start of a top-level #ifdef, inform MIOpt.
2330 if (!ReadAnyTokensBeforeDirective &&
2331 CurLexer->getConditionalStackDepth() == 0) {
2332 assert(isIfndef && "#ifdef shouldn't reach here");
2333 CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
2334 }
2335
2336 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
Chris Lattnercc1a8752007-10-07 08:44:20 +00002337 MacroInfo *MI = getMacroInfo(MII);
Reid Spencer5f016e22007-07-11 17:01:13 +00002338
2339 // If there is a macro, process it.
2340 if (MI) {
2341 // Mark it used.
2342 MI->setIsUsed(true);
2343
2344 // If this is the first use of a target-specific macro, warn about it.
2345 if (MI->isTargetSpecific()) {
2346 MI->setIsTargetSpecific(false); // Don't warn on second use.
2347 getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(),
2348 diag::port_target_macro_use);
2349 }
2350 } else {
2351 // Use of a target-specific macro for some other target? If so, warn.
2352 if (MII->isOtherTargetMacro()) {
2353 MII->setIsOtherTargetMacro(false); // Don't warn on second use.
2354 getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(),
2355 diag::port_target_macro_use);
2356 }
2357 }
2358
2359 // Should we include the stuff contained by this directive?
2360 if (!MI == isIfndef) {
2361 // Yes, remember that we are inside a conditional, then lex the next token.
2362 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
2363 /*foundnonskip*/true, /*foundelse*/false);
2364 } else {
2365 // No, skip the contents of this block and return the first token after it.
2366 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2367 /*Foundnonskip*/false,
2368 /*FoundElse*/false);
2369 }
2370}
2371
2372/// HandleIfDirective - Implements the #if directive.
2373///
Chris Lattnerd2177732007-07-20 16:59:19 +00002374void Preprocessor::HandleIfDirective(Token &IfToken,
Reid Spencer5f016e22007-07-11 17:01:13 +00002375 bool ReadAnyTokensBeforeDirective) {
2376 ++NumIf;
2377
2378 // Parse and evaluation the conditional expression.
2379 IdentifierInfo *IfNDefMacro = 0;
2380 bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2381
2382 // Should we include the stuff contained by this directive?
2383 if (ConditionalTrue) {
2384 // If this condition is equivalent to #ifndef X, and if this is the first
2385 // directive seen, handle it for the multiple-include optimization.
2386 if (!ReadAnyTokensBeforeDirective &&
2387 CurLexer->getConditionalStackDepth() == 0 && IfNDefMacro)
2388 CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
2389
2390 // Yes, remember that we are inside a conditional, then lex the next token.
2391 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2392 /*foundnonskip*/true, /*foundelse*/false);
2393 } else {
2394 // No, skip the contents of this block and return the first token after it.
2395 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
2396 /*FoundElse*/false);
2397 }
2398}
2399
2400/// HandleEndifDirective - Implements the #endif directive.
2401///
Chris Lattnerd2177732007-07-20 16:59:19 +00002402void Preprocessor::HandleEndifDirective(Token &EndifToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002403 ++NumEndif;
2404
2405 // Check that this is the whole directive.
2406 CheckEndOfDirective("#endif");
2407
2408 PPConditionalInfo CondInfo;
2409 if (CurLexer->popConditionalLevel(CondInfo)) {
2410 // No conditionals on the stack: this is an #endif without an #if.
2411 return Diag(EndifToken, diag::err_pp_endif_without_if);
2412 }
2413
2414 // If this the end of a top-level #endif, inform MIOpt.
2415 if (CurLexer->getConditionalStackDepth() == 0)
2416 CurLexer->MIOpt.ExitTopLevelConditional();
2417
2418 assert(!CondInfo.WasSkipping && !CurLexer->LexingRawMode &&
2419 "This code should only be reachable in the non-skipping case!");
2420}
2421
2422
Chris Lattnerd2177732007-07-20 16:59:19 +00002423void Preprocessor::HandleElseDirective(Token &Result) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002424 ++NumElse;
2425
2426 // #else directive in a non-skipping conditional... start skipping.
2427 CheckEndOfDirective("#else");
2428
2429 PPConditionalInfo CI;
2430 if (CurLexer->popConditionalLevel(CI))
2431 return Diag(Result, diag::pp_err_else_without_if);
2432
2433 // If this is a top-level #else, inform the MIOpt.
2434 if (CurLexer->getConditionalStackDepth() == 0)
2435 CurLexer->MIOpt.FoundTopLevelElse();
2436
2437 // If this is a #else with a #else before it, report the error.
2438 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2439
2440 // Finally, skip the rest of the contents of this block and return the first
2441 // token after it.
2442 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2443 /*FoundElse*/true);
2444}
2445
Chris Lattnerd2177732007-07-20 16:59:19 +00002446void Preprocessor::HandleElifDirective(Token &ElifToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002447 ++NumElse;
2448
2449 // #elif directive in a non-skipping conditional... start skipping.
2450 // We don't care what the condition is, because we will always skip it (since
2451 // the block immediately before it was included).
2452 DiscardUntilEndOfDirective();
2453
2454 PPConditionalInfo CI;
2455 if (CurLexer->popConditionalLevel(CI))
2456 return Diag(ElifToken, diag::pp_err_elif_without_if);
2457
2458 // If this is a top-level #elif, inform the MIOpt.
2459 if (CurLexer->getConditionalStackDepth() == 0)
2460 CurLexer->MIOpt.FoundTopLevelElse();
2461
2462 // If this is a #elif with a #else before it, report the error.
2463 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2464
2465 // Finally, skip the rest of the contents of this block and return the first
2466 // token after it.
2467 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2468 /*FoundElse*/CI.FoundElse);
2469}
2470