blob: c1f4970f30d94bc2cbbd378761618618ab84ac6e [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
2//
3// The LLVM Compiler Infrastructure
4//
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"
39#include "llvm/Support/MemoryBuffer.h"
40#include <iostream>
Chris Lattner1b023182007-09-03 18:30:32 +000041#include <ctime>
Chris Lattner4b009652007-07-25 00:24:17 +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);
53
54 // 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;
70 NumCachedMacroExpanders = 0;
71
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 Lattnerd1f21e12007-10-09 22:10:18 +000076 Predefines = 0;
77
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner7a1b0882007-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 }
Chris Lattner4b009652007-07-25 00:24:17 +0000104
105 // Free any cached macro expanders.
106 for (unsigned i = 0, e = NumCachedMacroExpanders; i != e; ++i)
107 delete MacroExpanderCache[i];
108
109 // 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
120/// the specified Token's location, translating the token's start
121/// 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
131void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
132 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.
191std::string Preprocessor::getSpelling(const Token &Tok) const {
192 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.
224unsigned Preprocessor::getSpelling(const Token &Tok,
225 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();
232
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);
239 }
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
275/// 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) {
279 // 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;
285
286 // Figure out how many physical characters away the specified logical
287 // character is. This needs to take into consideration newlines and
288 // trigraphs.
289 const char *TokPtr = SourceMgr.getCharacterData(TokStart);
290 unsigned PhysOffset = 0;
291
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))
296 ++TokPtr, --CharNo, ++PhysOffset;
297
298 // If we have a character that may be a trigraph or escaped newline, create a
299 // lexer to parse it correctly.
300 if (CharNo != 0) {
301 // Create a lexer starting at this token position.
302 Lexer TheLexer(TokStart, *this, TokPtr);
303 Token Tok;
304 // Skip over characters the remaining characters.
305 const char *TokStartPtr = TokPtr;
306 for (; CharNo; --CharNo)
307 TheLexer.getAndAdvanceChar(TokPtr, Tok);
308
309 PhysOffset += TokPtr-TokStartPtr;
310 }
311
312 return TokStart.getFileLocWithOffset(PhysOffset);
313}
314
315
Chris Lattnerd1f21e12007-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 Lattner5c222c82007-10-09 22:58:09 +0000372 if (PP.getLangOptions().ObjC1) {
Steve Naroff02a82aa2007-10-30 23:14:51 +0000373 const char *ObjcType;
Steve Naroff6e1406c2007-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");
Steve Naroff6e1406c2007-10-17 17:53:50 +0000380 ObjcType = "typedef struct objc_class *Class;\n";
381 Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
382 ObjcType = "typedef struct objc_object { Class isa; } *id;\n";
383 Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
384 ObjcType = "typedef struct objc_selector *SEL;\n";
385 Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
386 ObjcType = "typedef id (*IMP)(id, SEL, ...);\n";
387 Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
388 ObjcType = "typedef signed char BOOL;\n";
389 Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
390 DefineBuiltinMacro(Buf, "YES=(BOOL)1");
391 DefineBuiltinMacro(Buf, "NO=(BOOL)0");
392 DefineBuiltinMacro(Buf, "Nil=0");
393 DefineBuiltinMacro(Buf, "nil=0");
Chris Lattnerfe547702007-10-30 17:45:43 +0000394 ObjcType = "extern const char *sel_getName(SEL sel);\n";
Steve Naroff4c4abed2007-10-23 20:20:08 +0000395 Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
Chris Lattnerfe547702007-10-30 17:45:43 +0000396 ObjcType = "extern SEL sel_getUid(const char *str);\n";
Steve Naroff4c4abed2007-10-23 20:20:08 +0000397 Buf.insert(Buf.end(), ObjcType, ObjcType+strlen(ObjcType));
Chris Lattner5c222c82007-10-09 22:58:09 +0000398 }
Chris Lattner77cec472007-10-10 17:48:53 +0000399 // Add __builtin_va_list typedef.
400 {
401 const char *VAList = PP.getTargetInfo().getVAListDeclaration();
402 Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
403 Buf.push_back('\n');
404 }
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000405
406 // Get the target #defines.
407 PP.getTargetInfo().getTargetDefines(Buf);
408
409 // Compiler set macros.
410 DefineBuiltinMacro(Buf, "__APPLE_CC__=5250");
411 DefineBuiltinMacro(Buf, "__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=1030");
412 DefineBuiltinMacro(Buf, "__GNUC_MINOR__=0");
413 DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
414 DefineBuiltinMacro(Buf, "__GNUC__=4");
415 DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
416 DefineBuiltinMacro(Buf, "__VERSION__=\"4.0.1 (Apple Computer, Inc. "
417 "build 5250)\"");
418
419 // Build configuration options.
420 DefineBuiltinMacro(Buf, "__DYNAMIC__=1");
421 DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
422 DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
423 DefineBuiltinMacro(Buf, "__PIC__=1");
424
425
426 if (PP.getLangOptions().CPlusPlus) {
427 DefineBuiltinMacro(Buf, "__DEPRECATED=1");
428 DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
429 DefineBuiltinMacro(Buf, "__GNUG__=4");
430 DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
431 DefineBuiltinMacro(Buf, "__cplusplus=1");
432 DefineBuiltinMacro(Buf, "__private_extern__=extern");
433 }
434
435 // FIXME: Should emit a #line directive here.
436}
437
438
439/// EnterMainSourceFile - Enter the specified FileID as the main source file,
440/// which implicitly adds the builting defines etc.
441void Preprocessor::EnterMainSourceFile(unsigned MainFileID) {
442 // Enter the main file source buffer.
443 EnterSourceFile(MainFileID, 0);
444
445
446 std::vector<char> PrologFile;
447 PrologFile.reserve(4080);
448
449 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
450 InitializePredefinedMacros(*this, PrologFile);
451
452 // Add on the predefines from the driver.
453 PrologFile.insert(PrologFile.end(), Predefines,Predefines+strlen(Predefines));
454
455 // Memory buffer must end with a null byte!
456 PrologFile.push_back(0);
457
458 // Now that we have emitted the predefined macros, #includes, etc into
459 // PrologFile, preprocess it to populate the initial preprocessor state.
460 llvm::MemoryBuffer *SB =
461 llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(),
462 "<predefines>");
463 assert(SB && "Cannot fail to create predefined source buffer");
464 unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB);
465 assert(FileID && "Could not create FileID for predefines?");
466
467 // Start parsing the predefines.
468 EnterSourceFile(FileID, 0);
469}
Chris Lattner4b009652007-07-25 00:24:17 +0000470
471//===----------------------------------------------------------------------===//
472// Source File Location Methods.
473//===----------------------------------------------------------------------===//
474
475/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
476/// return null on failure. isAngled indicates whether the file reference is
477/// for system #include's or not (i.e. using <> instead of "").
478const FileEntry *Preprocessor::LookupFile(const char *FilenameStart,
479 const char *FilenameEnd,
480 bool isAngled,
481 const DirectoryLookup *FromDir,
482 const DirectoryLookup *&CurDir) {
483 // If the header lookup mechanism may be relative to the current file, pass in
484 // info about where the current file is.
485 const FileEntry *CurFileEnt = 0;
486 if (!FromDir) {
487 SourceLocation FileLoc = getCurrentFileLexer()->getFileLoc();
488 CurFileEnt = SourceMgr.getFileEntryForLoc(FileLoc);
489 }
490
491 // Do a standard file entry lookup.
492 CurDir = CurDirLookup;
493 const FileEntry *FE =
494 HeaderInfo.LookupFile(FilenameStart, FilenameEnd,
495 isAngled, FromDir, CurDir, CurFileEnt);
496 if (FE) return FE;
497
498 // Otherwise, see if this is a subframework header. If so, this is relative
499 // to one of the headers on the #include stack. Walk the list of the current
500 // headers on the #include stack and pass them to HeaderInfo.
501 if (CurLexer && !CurLexer->Is_PragmaLexer) {
502 CurFileEnt = SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc());
503 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
504 CurFileEnt)))
505 return FE;
506 }
507
508 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
509 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
510 if (ISEntry.TheLexer && !ISEntry.TheLexer->Is_PragmaLexer) {
511 CurFileEnt = SourceMgr.getFileEntryForLoc(ISEntry.TheLexer->getFileLoc());
512 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
513 CurFileEnt)))
514 return FE;
515 }
516 }
517
518 // Otherwise, we really couldn't find the file.
519 return 0;
520}
521
522/// isInPrimaryFile - Return true if we're in the top-level file, not in a
523/// #include.
524bool Preprocessor::isInPrimaryFile() const {
525 if (CurLexer && !CurLexer->Is_PragmaLexer)
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000526 return IncludeMacroStack.empty();
Chris Lattner4b009652007-07-25 00:24:17 +0000527
528 // If there are any stacked lexers, we're in a #include.
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000529 assert(IncludeMacroStack[0].TheLexer &&
530 !IncludeMacroStack[0].TheLexer->Is_PragmaLexer &&
531 "Top level include stack isn't our primary lexer?");
532 for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i)
Chris Lattner4b009652007-07-25 00:24:17 +0000533 if (IncludeMacroStack[i].TheLexer &&
534 !IncludeMacroStack[i].TheLexer->Is_PragmaLexer)
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000535 return false;
536 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000537}
538
539/// getCurrentLexer - Return the current file lexer being lexed from. Note
540/// that this ignores any potentially active macro expansions and _Pragma
541/// expansions going on at the time.
542Lexer *Preprocessor::getCurrentFileLexer() const {
543 if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer;
544
545 // Look for a stacked lexer.
546 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
547 Lexer *L = IncludeMacroStack[i-1].TheLexer;
548 if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions.
549 return L;
550 }
551 return 0;
552}
553
554
555/// EnterSourceFile - Add a source file to the top of the include stack and
556/// start lexing tokens from it instead of the current buffer. Return true
557/// on failure.
558void Preprocessor::EnterSourceFile(unsigned FileID,
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000559 const DirectoryLookup *CurDir) {
Chris Lattner4b009652007-07-25 00:24:17 +0000560 assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
561 ++NumEnteredSourceFiles;
562
563 if (MaxIncludeStackDepth < IncludeMacroStack.size())
564 MaxIncludeStackDepth = IncludeMacroStack.size();
565
566 Lexer *TheLexer = new Lexer(SourceLocation::getFileLoc(FileID, 0), *this);
Chris Lattner4b009652007-07-25 00:24:17 +0000567 EnterSourceFileWithLexer(TheLexer, CurDir);
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.
572void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
573 const DirectoryLookup *CurDir) {
574
575 // Add the current lexer to the include stack.
576 if (CurLexer || CurMacroExpander)
577 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
578 CurMacroExpander));
579
580 CurLexer = TheLexer;
581 CurDirLookup = CurDir;
582 CurMacroExpander = 0;
583
584 // Notify the client, if desired, that we are in a new source file.
585 if (Callbacks && !CurLexer->Is_PragmaLexer) {
586 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
587
588 // Get the file entry for the current file.
589 if (const FileEntry *FE =
590 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
591 FileType = HeaderInfo.getFileDirFlavor(FE);
592
593 Callbacks->FileChanged(CurLexer->getFileLoc(),
594 PPCallbacks::EnterFile, FileType);
595 }
596}
597
598
599
600/// EnterMacro - Add a Macro to the top of the include stack and start lexing
601/// tokens from it instead of the current buffer.
602void Preprocessor::EnterMacro(Token &Tok, MacroArgs *Args) {
603 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
604 CurMacroExpander));
605 CurLexer = 0;
606 CurDirLookup = 0;
607
608 if (NumCachedMacroExpanders == 0) {
609 CurMacroExpander = new MacroExpander(Tok, Args, *this);
610 } else {
611 CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
612 CurMacroExpander->Init(Tok, Args);
613 }
614}
615
616/// EnterTokenStream - Add a "macro" context to the top of the include stack,
617/// which will cause the lexer to start returning the specified tokens. Note
618/// that these tokens will be re-macro-expanded when/if expansion is enabled.
619/// This method assumes that the specified stream of tokens has a permanent
620/// owner somewhere, so they do not need to be copied.
621void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks) {
622 // Save our current state.
623 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
624 CurMacroExpander));
625 CurLexer = 0;
626 CurDirLookup = 0;
627
628 // Create a macro expander to expand from the specified token stream.
629 if (NumCachedMacroExpanders == 0) {
630 CurMacroExpander = new MacroExpander(Toks, NumToks, *this);
631 } else {
632 CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
633 CurMacroExpander->Init(Toks, NumToks);
634 }
635}
636
637/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
638/// lexer stack. This should only be used in situations where the current
639/// state of the top-of-stack lexer is known.
640void Preprocessor::RemoveTopOfLexerStack() {
641 assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
642
643 if (CurMacroExpander) {
644 // Delete or cache the now-dead macro expander.
645 if (NumCachedMacroExpanders == MacroExpanderCacheSize)
646 delete CurMacroExpander;
647 else
648 MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander;
649 } else {
650 delete CurLexer;
651 }
652 CurLexer = IncludeMacroStack.back().TheLexer;
653 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
654 CurMacroExpander = IncludeMacroStack.back().TheMacroExpander;
655 IncludeMacroStack.pop_back();
656}
657
658//===----------------------------------------------------------------------===//
659// Macro Expansion Handling.
660//===----------------------------------------------------------------------===//
661
Chris Lattner7a1b0882007-10-07 08:44:20 +0000662/// setMacroInfo - Specify a macro for this identifier.
663///
664void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
665 if (MI == 0) {
666 if (II->hasMacroDefinition()) {
667 Macros.erase(II);
668 II->setHasMacroDefinition(false);
669 }
670 } else {
671 Macros[II] = MI;
672 II->setHasMacroDefinition(true);
673 }
674}
675
Chris Lattner4b009652007-07-25 00:24:17 +0000676/// RegisterBuiltinMacro - Register the specified identifier in the identifier
677/// table and mark it as a builtin macro to be expanded.
678IdentifierInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) {
679 // Get the identifier.
680 IdentifierInfo *Id = getIdentifierInfo(Name);
681
682 // Mark it as being a macro that is builtin.
683 MacroInfo *MI = new MacroInfo(SourceLocation());
684 MI->setIsBuiltinMacro();
Chris Lattner7a1b0882007-10-07 08:44:20 +0000685 setMacroInfo(Id, MI);
Chris Lattner4b009652007-07-25 00:24:17 +0000686 return Id;
687}
688
689
690/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
691/// identifier table.
692void Preprocessor::RegisterBuiltinMacros() {
693 Ident__LINE__ = RegisterBuiltinMacro("__LINE__");
694 Ident__FILE__ = RegisterBuiltinMacro("__FILE__");
695 Ident__DATE__ = RegisterBuiltinMacro("__DATE__");
696 Ident__TIME__ = RegisterBuiltinMacro("__TIME__");
697 Ident_Pragma = RegisterBuiltinMacro("_Pragma");
698
699 // GCC Extensions.
700 Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__");
701 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__");
702 Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__");
703}
704
705/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
706/// in its expansion, currently expands to that token literally.
707static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
Chris Lattner7a1b0882007-10-07 08:44:20 +0000708 const IdentifierInfo *MacroIdent,
709 Preprocessor &PP) {
Chris Lattner4b009652007-07-25 00:24:17 +0000710 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
711
712 // If the token isn't an identifier, it's always literally expanded.
713 if (II == 0) return true;
714
715 // If the identifier is a macro, and if that macro is enabled, it may be
716 // expanded so it's not a trivial expansion.
Chris Lattner7a1b0882007-10-07 08:44:20 +0000717 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
Chris Lattner4b009652007-07-25 00:24:17 +0000718 // Fast expanding "#define X X" is ok, because X would be disabled.
719 II != MacroIdent)
720 return false;
721
722 // If this is an object-like macro invocation, it is safe to trivially expand
723 // it.
724 if (MI->isObjectLike()) return true;
725
726 // If this is a function-like macro invocation, it's safe to trivially expand
727 // as long as the identifier is not a macro argument.
728 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
729 I != E; ++I)
730 if (*I == II)
731 return false; // Identifier is a macro argument.
732
733 return true;
734}
735
736
737/// isNextPPTokenLParen - Determine whether the next preprocessor token to be
738/// lexed is a '('. If so, consume the token and return true, if not, this
739/// method should have no observable side-effect on the lexed tokens.
740bool Preprocessor::isNextPPTokenLParen() {
741 // Do some quick tests for rejection cases.
742 unsigned Val;
743 if (CurLexer)
744 Val = CurLexer->isNextPPTokenLParen();
745 else
746 Val = CurMacroExpander->isNextTokenLParen();
747
748 if (Val == 2) {
749 // We have run off the end. If it's a source file we don't
750 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
751 // macro stack.
752 if (CurLexer)
753 return false;
754 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
755 IncludeStackInfo &Entry = IncludeMacroStack[i-1];
756 if (Entry.TheLexer)
757 Val = Entry.TheLexer->isNextPPTokenLParen();
758 else
759 Val = Entry.TheMacroExpander->isNextTokenLParen();
760
761 if (Val != 2)
762 break;
763
764 // Ran off the end of a source file?
765 if (Entry.TheLexer)
766 return false;
767 }
768 }
769
770 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
771 // have found something that isn't a '(' or we found the end of the
772 // translation unit. In either case, return false.
773 if (Val != 1)
774 return false;
775
776 Token Tok;
777 LexUnexpandedToken(Tok);
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000778 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
Chris Lattner4b009652007-07-25 00:24:17 +0000779 return true;
780}
781
782/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
783/// expanded as a macro, handle it and return the next token as 'Identifier'.
784bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
785 MacroInfo *MI) {
786
787 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
788 if (MI->isBuiltinMacro()) {
789 ExpandBuiltinMacro(Identifier);
790 return false;
791 }
792
793 // If this is the first use of a target-specific macro, warn about it.
794 if (MI->isTargetSpecific()) {
795 MI->setIsTargetSpecific(false); // Don't warn on second use.
796 getTargetInfo().DiagnoseNonPortability(Identifier.getLocation(),
797 diag::port_target_macro_use);
798 }
799
800 /// Args - If this is a function-like macro expansion, this contains,
801 /// for each macro argument, the list of tokens that were provided to the
802 /// invocation.
803 MacroArgs *Args = 0;
804
805 // If this is a function-like macro, read the arguments.
806 if (MI->isFunctionLike()) {
807 // C99 6.10.3p10: If the preprocessing token immediately after the the macro
808 // name isn't a '(', this macro should not be expanded. Otherwise, consume
809 // it.
810 if (!isNextPPTokenLParen())
811 return true;
812
813 // Remember that we are now parsing the arguments to a macro invocation.
814 // Preprocessor directives used inside macro arguments are not portable, and
815 // this enables the warning.
816 InMacroArgs = true;
817 Args = ReadFunctionLikeMacroArgs(Identifier, MI);
818
819 // Finished parsing args.
820 InMacroArgs = false;
821
822 // If there was an error parsing the arguments, bail out.
823 if (Args == 0) return false;
824
825 ++NumFnMacroExpanded;
826 } else {
827 ++NumMacroExpanded;
828 }
829
830 // Notice that this macro has been used.
831 MI->setIsUsed(true);
832
833 // If we started lexing a macro, enter the macro expansion body.
834
835 // If this macro expands to no tokens, don't bother to push it onto the
836 // expansion stack, only to take it right back off.
837 if (MI->getNumTokens() == 0) {
838 // No need for arg info.
839 if (Args) Args->destroy();
840
841 // Ignore this macro use, just return the next token in the current
842 // buffer.
843 bool HadLeadingSpace = Identifier.hasLeadingSpace();
844 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
845
846 Lex(Identifier);
847
848 // If the identifier isn't on some OTHER line, inherit the leading
849 // whitespace/first-on-a-line property of this token. This handles
850 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
851 // empty.
852 if (!Identifier.isAtStartOfLine()) {
853 if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
854 if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
855 }
856 ++NumFastMacroExpanded;
857 return false;
858
859 } else if (MI->getNumTokens() == 1 &&
Chris Lattner7a1b0882007-10-07 08:44:20 +0000860 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
861 *this)){
Chris Lattner4b009652007-07-25 00:24:17 +0000862 // Otherwise, if this macro expands into a single trivially-expanded
863 // token: expand it now. This handles common cases like
864 // "#define VAL 42".
865
866 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
867 // identifier to the expanded token.
868 bool isAtStartOfLine = Identifier.isAtStartOfLine();
869 bool hasLeadingSpace = Identifier.hasLeadingSpace();
870
871 // Remember where the token is instantiated.
872 SourceLocation InstantiateLoc = Identifier.getLocation();
873
874 // Replace the result token.
875 Identifier = MI->getReplacementToken(0);
876
877 // Restore the StartOfLine/LeadingSpace markers.
878 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
879 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
880
881 // Update the tokens location to include both its logical and physical
882 // locations.
883 SourceLocation Loc =
884 SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc);
885 Identifier.setLocation(Loc);
886
887 // If this is #define X X, we must mark the result as unexpandible.
888 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo())
Chris Lattner7a1b0882007-10-07 08:44:20 +0000889 if (getMacroInfo(NewII) == MI)
Chris Lattner4b009652007-07-25 00:24:17 +0000890 Identifier.setFlag(Token::DisableExpand);
891
892 // Since this is not an identifier token, it can't be macro expanded, so
893 // we're done.
894 ++NumFastMacroExpanded;
895 return false;
896 }
897
898 // Start expanding the macro.
899 EnterMacro(Identifier, Args);
900
901 // Now that the macro is at the top of the include stack, ask the
902 // preprocessor to read the next token from it.
903 Lex(Identifier);
904 return false;
905}
906
907/// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
908/// invoked to read all of the actual arguments specified for the macro
909/// invocation. This returns null on error.
910MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
911 MacroInfo *MI) {
912 // The number of fixed arguments to parse.
913 unsigned NumFixedArgsLeft = MI->getNumArgs();
914 bool isVariadic = MI->isVariadic();
915
916 // Outer loop, while there are more arguments, keep reading them.
917 Token Tok;
918 Tok.setKind(tok::comma);
919 --NumFixedArgsLeft; // Start reading the first arg.
920
921 // ArgTokens - Build up a list of tokens that make up each argument. Each
922 // argument is separated by an EOF token. Use a SmallVector so we can avoid
923 // heap allocations in the common case.
924 llvm::SmallVector<Token, 64> ArgTokens;
925
926 unsigned NumActuals = 0;
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000927 while (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000928 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
929 // that we already consumed the first one.
930 unsigned NumParens = 0;
931
932 while (1) {
933 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
934 // an argument value in a macro could expand to ',' or '(' or ')'.
935 LexUnexpandedToken(Tok);
936
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000937 if (Tok.is(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000938 Diag(MacroName, diag::err_unterm_macro_invoc);
939 // Do not lose the EOF. Return it to the client.
940 MacroName = Tok;
941 return 0;
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000942 } else if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000943 // If we found the ) token, the macro arg list is done.
944 if (NumParens-- == 0)
945 break;
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000946 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000947 ++NumParens;
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000948 } else if (Tok.is(tok::comma) && NumParens == 0) {
Chris Lattner4b009652007-07-25 00:24:17 +0000949 // Comma ends this argument if there are more fixed arguments expected.
950 if (NumFixedArgsLeft)
951 break;
952
953 // If this is not a variadic macro, too many args were specified.
954 if (!isVariadic) {
955 // Emit the diagnostic at the macro name in case there is a missing ).
956 // Emitting it at the , could be far away from the macro name.
957 Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
958 return 0;
959 }
960 // Otherwise, continue to add the tokens to this variable argument.
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000961 } else if (Tok.is(tok::comment) && !KeepMacroComments) {
Chris Lattner4b009652007-07-25 00:24:17 +0000962 // If this is a comment token in the argument list and we're just in
963 // -C mode (not -CC mode), discard the comment.
964 continue;
965 }
966
967 ArgTokens.push_back(Tok);
968 }
969
970 // Empty arguments are standard in C99 and supported as an extension in
971 // other modes.
972 if (ArgTokens.empty() && !Features.C99)
973 Diag(Tok, diag::ext_empty_fnmacro_arg);
974
975 // Add a marker EOF token to the end of the token list for this argument.
976 Token EOFTok;
977 EOFTok.startToken();
978 EOFTok.setKind(tok::eof);
979 EOFTok.setLocation(Tok.getLocation());
980 EOFTok.setLength(0);
981 ArgTokens.push_back(EOFTok);
982 ++NumActuals;
983 --NumFixedArgsLeft;
984 };
985
986 // Okay, we either found the r_paren. Check to see if we parsed too few
987 // arguments.
988 unsigned MinArgsExpected = MI->getNumArgs();
989
990 // See MacroArgs instance var for description of this.
991 bool isVarargsElided = false;
992
993 if (NumActuals < MinArgsExpected) {
994 // There are several cases where too few arguments is ok, handle them now.
995 if (NumActuals+1 == MinArgsExpected && MI->isVariadic()) {
996 // Varargs where the named vararg parameter is missing: ok as extension.
997 // #define A(x, ...)
998 // A("blah")
999 Diag(Tok, diag::ext_missing_varargs_arg);
1000
1001 // Remember this occurred if this is a C99 macro invocation with at least
1002 // one actual argument.
1003 isVarargsElided = MI->isC99Varargs() && MI->getNumArgs() > 1;
1004 } else if (MI->getNumArgs() == 1) {
1005 // #define A(x)
1006 // A()
1007 // is ok because it is an empty argument.
1008
1009 // Empty arguments are standard in C99 and supported as an extension in
1010 // other modes.
1011 if (ArgTokens.empty() && !Features.C99)
1012 Diag(Tok, diag::ext_empty_fnmacro_arg);
1013 } else {
1014 // Otherwise, emit the error.
1015 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
1016 return 0;
1017 }
1018
1019 // Add a marker EOF token to the end of the token list for this argument.
1020 SourceLocation EndLoc = Tok.getLocation();
1021 Tok.startToken();
1022 Tok.setKind(tok::eof);
1023 Tok.setLocation(EndLoc);
1024 Tok.setLength(0);
1025 ArgTokens.push_back(Tok);
1026 }
1027
1028 return MacroArgs::create(MI, &ArgTokens[0], ArgTokens.size(),isVarargsElided);
1029}
1030
1031/// ComputeDATE_TIME - Compute the current time, enter it into the specified
1032/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
1033/// the identifier tokens inserted.
1034static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
1035 Preprocessor &PP) {
1036 time_t TT = time(0);
1037 struct tm *TM = localtime(&TT);
1038
1039 static const char * const Months[] = {
1040 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
1041 };
1042
1043 char TmpBuffer[100];
1044 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
1045 TM->tm_year+1900);
1046 DATELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
1047
1048 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
1049 TIMELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
1050}
1051
1052/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1053/// as a builtin macro, handle it and return the next token as 'Tok'.
1054void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1055 // Figure out which token this is.
1056 IdentifierInfo *II = Tok.getIdentifierInfo();
1057 assert(II && "Can't be a macro without id info!");
1058
1059 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
1060 // lex the token after it.
1061 if (II == Ident_Pragma)
1062 return Handle_Pragma(Tok);
1063
1064 ++NumBuiltinMacroExpanded;
1065
1066 char TmpBuffer[100];
1067
1068 // Set up the return result.
1069 Tok.setIdentifierInfo(0);
1070 Tok.clearFlag(Token::NeedsCleaning);
1071
1072 if (II == Ident__LINE__) {
1073 // __LINE__ expands to a simple numeric value.
1074 sprintf(TmpBuffer, "%u", SourceMgr.getLogicalLineNumber(Tok.getLocation()));
1075 unsigned Length = strlen(TmpBuffer);
1076 Tok.setKind(tok::numeric_constant);
1077 Tok.setLength(Length);
1078 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
1079 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
1080 SourceLocation Loc = Tok.getLocation();
1081 if (II == Ident__BASE_FILE__) {
1082 Diag(Tok, diag::ext_pp_base_file);
1083 SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc);
1084 while (NextLoc.isValid()) {
1085 Loc = NextLoc;
1086 NextLoc = SourceMgr.getIncludeLoc(Loc);
1087 }
1088 }
1089
1090 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
1091 std::string FN = SourceMgr.getSourceName(SourceMgr.getLogicalLoc(Loc));
1092 FN = '"' + Lexer::Stringify(FN) + '"';
1093 Tok.setKind(tok::string_literal);
1094 Tok.setLength(FN.size());
1095 Tok.setLocation(CreateString(&FN[0], FN.size(), Tok.getLocation()));
1096 } else if (II == Ident__DATE__) {
1097 if (!DATELoc.isValid())
1098 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1099 Tok.setKind(tok::string_literal);
1100 Tok.setLength(strlen("\"Mmm dd yyyy\""));
1101 Tok.setLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation()));
1102 } else if (II == Ident__TIME__) {
1103 if (!TIMELoc.isValid())
1104 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1105 Tok.setKind(tok::string_literal);
1106 Tok.setLength(strlen("\"hh:mm:ss\""));
1107 Tok.setLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
1108 } else if (II == Ident__INCLUDE_LEVEL__) {
1109 Diag(Tok, diag::ext_pp_include_level);
1110
1111 // Compute the include depth of this token.
1112 unsigned Depth = 0;
1113 SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation());
1114 for (; Loc.isValid(); ++Depth)
1115 Loc = SourceMgr.getIncludeLoc(Loc);
1116
1117 // __INCLUDE_LEVEL__ expands to a simple numeric value.
1118 sprintf(TmpBuffer, "%u", Depth);
1119 unsigned Length = strlen(TmpBuffer);
1120 Tok.setKind(tok::numeric_constant);
1121 Tok.setLength(Length);
1122 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
1123 } else if (II == Ident__TIMESTAMP__) {
1124 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
1125 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1126 Diag(Tok, diag::ext_pp_timestamp);
1127
1128 // Get the file that we are lexing out of. If we're currently lexing from
1129 // a macro, dig into the include stack.
1130 const FileEntry *CurFile = 0;
1131 Lexer *TheLexer = getCurrentFileLexer();
1132
1133 if (TheLexer)
1134 CurFile = SourceMgr.getFileEntryForLoc(TheLexer->getFileLoc());
1135
1136 // If this file is older than the file it depends on, emit a diagnostic.
1137 const char *Result;
1138 if (CurFile) {
1139 time_t TT = CurFile->getModificationTime();
1140 struct tm *TM = localtime(&TT);
1141 Result = asctime(TM);
1142 } else {
1143 Result = "??? ??? ?? ??:??:?? ????\n";
1144 }
1145 TmpBuffer[0] = '"';
1146 strcpy(TmpBuffer+1, Result);
1147 unsigned Len = strlen(TmpBuffer);
1148 TmpBuffer[Len-1] = '"'; // Replace the newline with a quote.
1149 Tok.setKind(tok::string_literal);
1150 Tok.setLength(Len);
1151 Tok.setLocation(CreateString(TmpBuffer, Len, Tok.getLocation()));
1152 } else {
1153 assert(0 && "Unknown identifier!");
1154 }
1155}
1156
1157//===----------------------------------------------------------------------===//
1158// Lexer Event Handling.
1159//===----------------------------------------------------------------------===//
1160
1161/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
1162/// identifier information for the token and install it into the token.
1163IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
1164 const char *BufPtr) {
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001165 assert(Identifier.is(tok::identifier) && "Not an identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001166 assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
1167
1168 // Look up this token, see if it is a macro, or if it is a language keyword.
1169 IdentifierInfo *II;
1170 if (BufPtr && !Identifier.needsCleaning()) {
1171 // No cleaning needed, just use the characters from the lexed buffer.
1172 II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
1173 } else {
1174 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
1175 llvm::SmallVector<char, 64> IdentifierBuffer;
1176 IdentifierBuffer.resize(Identifier.getLength());
1177 const char *TmpBuf = &IdentifierBuffer[0];
1178 unsigned Size = getSpelling(Identifier, TmpBuf);
1179 II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
1180 }
1181 Identifier.setIdentifierInfo(II);
1182 return II;
1183}
1184
1185
1186/// HandleIdentifier - This callback is invoked when the lexer reads an
1187/// identifier. This callback looks up the identifier in the map and/or
1188/// potentially macro expands it or turns it into a named token (like 'for').
1189void Preprocessor::HandleIdentifier(Token &Identifier) {
1190 assert(Identifier.getIdentifierInfo() &&
1191 "Can't handle identifiers without identifier info!");
1192
1193 IdentifierInfo &II = *Identifier.getIdentifierInfo();
1194
1195 // If this identifier was poisoned, and if it was not produced from a macro
1196 // expansion, emit an error.
1197 if (II.isPoisoned() && CurLexer) {
1198 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
1199 Diag(Identifier, diag::err_pp_used_poisoned_id);
1200 else
1201 Diag(Identifier, diag::ext_pp_bad_vaargs_use);
1202 }
1203
1204 // If this is a macro to be expanded, do it.
Chris Lattner7a1b0882007-10-07 08:44:20 +00001205 if (MacroInfo *MI = getMacroInfo(&II)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001206 if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
1207 if (MI->isEnabled()) {
1208 if (!HandleMacroExpandedIdentifier(Identifier, MI))
1209 return;
1210 } else {
1211 // C99 6.10.3.4p2 says that a disabled macro may never again be
1212 // expanded, even if it's in a context where it could be expanded in the
1213 // future.
1214 Identifier.setFlag(Token::DisableExpand);
1215 }
1216 }
1217 } else if (II.isOtherTargetMacro() && !DisableMacroExpansion) {
1218 // If this identifier is a macro on some other target, emit a diagnostic.
1219 // This diagnosic is only emitted when macro expansion is enabled, because
1220 // the macro would not have been expanded for the other target either.
1221 II.setIsOtherTargetMacro(false); // Don't warn on second use.
1222 getTargetInfo().DiagnoseNonPortability(Identifier.getLocation(),
1223 diag::port_target_macro_use);
1224
1225 }
1226
1227 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
1228 // then we act as if it is the actual operator and not the textual
1229 // representation of it.
1230 if (II.isCPlusPlusOperatorKeyword())
1231 Identifier.setIdentifierInfo(0);
1232
1233 // Change the kind of this identifier to the appropriate token kind, e.g.
1234 // turning "for" into a keyword.
1235 Identifier.setKind(II.getTokenID());
1236
1237 // If this is an extension token, diagnose its use.
1238 // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99
1239 // For now, I'm just commenting it out (while I work on attributes).
1240 if (II.isExtensionToken() && Features.C99)
1241 Diag(Identifier, diag::ext_token_used);
1242}
1243
1244/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
1245/// the current file. This either returns the EOF token or pops a level off
1246/// the include stack and keeps going.
1247bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
1248 assert(!CurMacroExpander &&
1249 "Ending a file when currently in a macro!");
1250
1251 // See if this file had a controlling macro.
1252 if (CurLexer) { // Not ending a macro, ignore it.
1253 if (const IdentifierInfo *ControllingMacro =
1254 CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
1255 // Okay, this has a controlling macro, remember in PerFileInfo.
1256 if (const FileEntry *FE =
1257 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
1258 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
1259 }
1260 }
1261
1262 // If this is a #include'd file, pop it off the include stack and continue
1263 // lexing the #includer file.
1264 if (!IncludeMacroStack.empty()) {
1265 // We're done with the #included file.
1266 RemoveTopOfLexerStack();
1267
1268 // Notify the client, if desired, that we are in a new source file.
1269 if (Callbacks && !isEndOfMacro && CurLexer) {
1270 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
1271
1272 // Get the file entry for the current file.
1273 if (const FileEntry *FE =
1274 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
1275 FileType = HeaderInfo.getFileDirFlavor(FE);
1276
1277 Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr),
1278 PPCallbacks::ExitFile, FileType);
1279 }
1280
1281 // Client should lex another token.
1282 return false;
1283 }
1284
1285 Result.startToken();
1286 CurLexer->BufferPtr = CurLexer->BufferEnd;
1287 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
1288 Result.setKind(tok::eof);
1289
1290 // We're done with the #included file.
1291 delete CurLexer;
1292 CurLexer = 0;
1293
1294 // This is the end of the top-level file. If the diag::pp_macro_not_used
Chris Lattner7a1b0882007-10-07 08:44:20 +00001295 // diagnostic is enabled, look for macros that have not been used.
Chris Lattner4b009652007-07-25 00:24:17 +00001296 if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){
Chris Lattner7a1b0882007-10-07 08:44:20 +00001297 for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
1298 Macros.begin(), E = Macros.end(); I != E; ++I) {
1299 if (!I->second->isUsed())
1300 Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattner4b009652007-07-25 00:24:17 +00001301 }
1302 }
Chris Lattner4b009652007-07-25 00:24:17 +00001303 return true;
1304}
1305
1306/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
1307/// the current macro expansion or token stream expansion.
1308bool Preprocessor::HandleEndOfMacro(Token &Result) {
1309 assert(CurMacroExpander && !CurLexer &&
1310 "Ending a macro when currently in a #include file!");
1311
1312 // Delete or cache the now-dead macro expander.
1313 if (NumCachedMacroExpanders == MacroExpanderCacheSize)
1314 delete CurMacroExpander;
1315 else
1316 MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander;
1317
1318 // Handle this like a #include file being popped off the stack.
1319 CurMacroExpander = 0;
1320 return HandleEndOfFile(Result, true);
1321}
1322
1323
1324//===----------------------------------------------------------------------===//
1325// Utility Methods for Preprocessor Directive Handling.
1326//===----------------------------------------------------------------------===//
1327
1328/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
1329/// current line until the tok::eom token is found.
1330void Preprocessor::DiscardUntilEndOfDirective() {
1331 Token Tmp;
1332 do {
1333 LexUnexpandedToken(Tmp);
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001334 } while (Tmp.isNot(tok::eom));
Chris Lattner4b009652007-07-25 00:24:17 +00001335}
1336
1337/// isCXXNamedOperator - Returns "true" if the token is a named operator in C++.
1338static bool isCXXNamedOperator(const std::string &Spelling) {
1339 return Spelling == "and" || Spelling == "bitand" || Spelling == "bitor" ||
1340 Spelling == "compl" || Spelling == "not" || Spelling == "not_eq" ||
1341 Spelling == "or" || Spelling == "xor";
1342}
1343
1344/// ReadMacroName - Lex and validate a macro name, which occurs after a
1345/// #define or #undef. This sets the token kind to eom and discards the rest
1346/// of the macro line if the macro name is invalid. isDefineUndef is 1 if
1347/// this is due to a a #define, 2 if #undef directive, 0 if it is something
1348/// else (e.g. #ifdef).
1349void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
1350 // Read the token, don't allow macro expansion on it.
1351 LexUnexpandedToken(MacroNameTok);
1352
1353 // Missing macro name?
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001354 if (MacroNameTok.is(tok::eom))
Chris Lattner4b009652007-07-25 00:24:17 +00001355 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
1356
1357 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1358 if (II == 0) {
1359 std::string Spelling = getSpelling(MacroNameTok);
1360 if (isCXXNamedOperator(Spelling))
1361 // C++ 2.5p2: Alternative tokens behave the same as its primary token
1362 // except for their spellings.
1363 Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name, Spelling);
1364 else
1365 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
1366 // Fall through on error.
1367 } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
1368 // Error if defining "defined": C99 6.10.8.4.
1369 Diag(MacroNameTok, diag::err_defined_macro_name);
Chris Lattner3b56a012007-10-07 08:04:56 +00001370 } else if (isDefineUndef && II->hasMacroDefinition() &&
Chris Lattner7a1b0882007-10-07 08:44:20 +00001371 getMacroInfo(II)->isBuiltinMacro()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001372 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
1373 if (isDefineUndef == 1)
1374 Diag(MacroNameTok, diag::pp_redef_builtin_macro);
1375 else
1376 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
1377 } else {
1378 // Okay, we got a good identifier node. Return it.
1379 return;
1380 }
1381
1382 // Invalid macro name, read and discard the rest of the line. Then set the
1383 // token kind to tok::eom.
1384 MacroNameTok.setKind(tok::eom);
1385 return DiscardUntilEndOfDirective();
1386}
1387
1388/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
1389/// not, emit a diagnostic and consume up until the eom.
1390void Preprocessor::CheckEndOfDirective(const char *DirType) {
1391 Token Tmp;
1392 Lex(Tmp);
1393 // There should be no tokens after the directive, but we allow them as an
1394 // extension.
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001395 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
Chris Lattner4b009652007-07-25 00:24:17 +00001396 Lex(Tmp);
1397
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001398 if (Tmp.isNot(tok::eom)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001399 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
1400 DiscardUntilEndOfDirective();
1401 }
1402}
1403
1404
1405
1406/// SkipExcludedConditionalBlock - We just read a #if or related directive and
1407/// decided that the subsequent tokens are in the #if'd out portion of the
1408/// file. Lex the rest of the file, until we see an #endif. If
1409/// FoundNonSkipPortion is true, then we have already emitted code for part of
1410/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
1411/// is true, then #else directives are ok, if not, then we have already seen one
1412/// so a #else directive is a duplicate. When this returns, the caller can lex
1413/// the first valid token.
1414void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
1415 bool FoundNonSkipPortion,
1416 bool FoundElse) {
1417 ++NumSkipped;
1418 assert(CurMacroExpander == 0 && CurLexer &&
1419 "Lexing a macro, not a file?");
1420
1421 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
1422 FoundNonSkipPortion, FoundElse);
1423
1424 // Enter raw mode to disable identifier lookup (and thus macro expansion),
1425 // disabling warnings, etc.
1426 CurLexer->LexingRawMode = true;
1427 Token Tok;
1428 while (1) {
1429 CurLexer->Lex(Tok);
1430
1431 // If this is the end of the buffer, we have an error.
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001432 if (Tok.is(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001433 // Emit errors for each unterminated conditional on the stack, including
1434 // the current one.
1435 while (!CurLexer->ConditionalStack.empty()) {
1436 Diag(CurLexer->ConditionalStack.back().IfLoc,
1437 diag::err_pp_unterminated_conditional);
1438 CurLexer->ConditionalStack.pop_back();
1439 }
1440
1441 // Just return and let the caller lex after this #include.
1442 break;
1443 }
1444
1445 // If this token is not a preprocessor directive, just skip it.
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001446 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
Chris Lattner4b009652007-07-25 00:24:17 +00001447 continue;
1448
1449 // We just parsed a # character at the start of a line, so we're in
1450 // directive mode. Tell the lexer this so any newlines we see will be
1451 // converted into an EOM token (this terminates the macro).
1452 CurLexer->ParsingPreprocessorDirective = true;
1453 CurLexer->KeepCommentMode = false;
1454
1455
1456 // Read the next token, the directive flavor.
1457 LexUnexpandedToken(Tok);
1458
1459 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
1460 // something bogus), skip it.
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001461 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001462 CurLexer->ParsingPreprocessorDirective = false;
1463 // Restore comment saving mode.
1464 CurLexer->KeepCommentMode = KeepComments;
1465 continue;
1466 }
1467
1468 // If the first letter isn't i or e, it isn't intesting to us. We know that
1469 // this is safe in the face of spelling differences, because there is no way
1470 // to spell an i/e in a strange way that is another letter. Skipping this
1471 // allows us to avoid looking up the identifier info for #define/#undef and
1472 // other common directives.
1473 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
1474 char FirstChar = RawCharData[0];
1475 if (FirstChar >= 'a' && FirstChar <= 'z' &&
1476 FirstChar != 'i' && FirstChar != 'e') {
1477 CurLexer->ParsingPreprocessorDirective = false;
1478 // Restore comment saving mode.
1479 CurLexer->KeepCommentMode = KeepComments;
1480 continue;
1481 }
1482
1483 // Get the identifier name without trigraphs or embedded newlines. Note
1484 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
1485 // when skipping.
1486 // TODO: could do this with zero copies in the no-clean case by using
1487 // strncmp below.
1488 char Directive[20];
1489 unsigned IdLen;
1490 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
1491 IdLen = Tok.getLength();
1492 memcpy(Directive, RawCharData, IdLen);
1493 Directive[IdLen] = 0;
1494 } else {
1495 std::string DirectiveStr = getSpelling(Tok);
1496 IdLen = DirectiveStr.size();
1497 if (IdLen >= 20) {
1498 CurLexer->ParsingPreprocessorDirective = false;
1499 // Restore comment saving mode.
1500 CurLexer->KeepCommentMode = KeepComments;
1501 continue;
1502 }
1503 memcpy(Directive, &DirectiveStr[0], IdLen);
1504 Directive[IdLen] = 0;
1505 }
1506
1507 if (FirstChar == 'i' && Directive[1] == 'f') {
1508 if ((IdLen == 2) || // "if"
1509 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
1510 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
1511 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
1512 // bother parsing the condition.
1513 DiscardUntilEndOfDirective();
1514 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
1515 /*foundnonskip*/false,
1516 /*fnddelse*/false);
1517 }
1518 } else if (FirstChar == 'e') {
1519 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
1520 CheckEndOfDirective("#endif");
1521 PPConditionalInfo CondInfo;
1522 CondInfo.WasSkipping = true; // Silence bogus warning.
1523 bool InCond = CurLexer->popConditionalLevel(CondInfo);
1524 InCond = InCond; // Silence warning in no-asserts mode.
1525 assert(!InCond && "Can't be skipping if not in a conditional!");
1526
1527 // If we popped the outermost skipping block, we're done skipping!
1528 if (!CondInfo.WasSkipping)
1529 break;
1530 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
1531 // #else directive in a skipping conditional. If not in some other
1532 // skipping conditional, and if #else hasn't already been seen, enter it
1533 // as a non-skipping conditional.
1534 CheckEndOfDirective("#else");
1535 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1536
1537 // If this is a #else with a #else before it, report the error.
1538 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
1539
1540 // Note that we've seen a #else in this conditional.
1541 CondInfo.FoundElse = true;
1542
1543 // If the conditional is at the top level, and the #if block wasn't
1544 // entered, enter the #else block now.
1545 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
1546 CondInfo.FoundNonSkip = true;
1547 break;
1548 }
1549 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
1550 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1551
1552 bool ShouldEnter;
1553 // If this is in a skipping block or if we're already handled this #if
1554 // block, don't bother parsing the condition.
1555 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
1556 DiscardUntilEndOfDirective();
1557 ShouldEnter = false;
1558 } else {
1559 // Restore the value of LexingRawMode so that identifiers are
1560 // looked up, etc, inside the #elif expression.
1561 assert(CurLexer->LexingRawMode && "We have to be skipping here!");
1562 CurLexer->LexingRawMode = false;
1563 IdentifierInfo *IfNDefMacro = 0;
1564 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
1565 CurLexer->LexingRawMode = true;
1566 }
1567
1568 // If this is a #elif with a #else before it, report the error.
1569 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
1570
1571 // If this condition is true, enter it!
1572 if (ShouldEnter) {
1573 CondInfo.FoundNonSkip = true;
1574 break;
1575 }
1576 }
1577 }
1578
1579 CurLexer->ParsingPreprocessorDirective = false;
1580 // Restore comment saving mode.
1581 CurLexer->KeepCommentMode = KeepComments;
1582 }
1583
1584 // Finally, if we are out of the conditional (saw an #endif or ran off the end
1585 // of the file, just stop skipping and return to lexing whatever came after
1586 // the #if block.
1587 CurLexer->LexingRawMode = false;
1588}
1589
1590//===----------------------------------------------------------------------===//
1591// Preprocessor Directive Handling.
1592//===----------------------------------------------------------------------===//
1593
1594/// HandleDirective - This callback is invoked when the lexer sees a # token
1595/// at the start of a line. This consumes the directive, modifies the
1596/// lexer/preprocessor state, and advances the lexer(s) so that the next token
1597/// read is the correct one.
1598void Preprocessor::HandleDirective(Token &Result) {
1599 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
1600
1601 // We just parsed a # character at the start of a line, so we're in directive
1602 // mode. Tell the lexer this so any newlines we see will be converted into an
1603 // EOM token (which terminates the directive).
1604 CurLexer->ParsingPreprocessorDirective = true;
1605
1606 ++NumDirectives;
1607
1608 // We are about to read a token. For the multiple-include optimization FA to
1609 // work, we have to remember if we had read any tokens *before* this
1610 // pp-directive.
1611 bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal();
1612
1613 // Read the next token, the directive flavor. This isn't expanded due to
1614 // C99 6.10.3p8.
1615 LexUnexpandedToken(Result);
1616
1617 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
1618 // #define A(x) #x
1619 // A(abc
1620 // #warning blah
1621 // def)
1622 // If so, the user is relying on non-portable behavior, emit a diagnostic.
1623 if (InMacroArgs)
1624 Diag(Result, diag::ext_embedded_directive);
1625
1626TryAgain:
1627 switch (Result.getKind()) {
1628 case tok::eom:
1629 return; // null directive.
1630 case tok::comment:
1631 // Handle stuff like "# /*foo*/ define X" in -E -C mode.
1632 LexUnexpandedToken(Result);
1633 goto TryAgain;
1634
1635 case tok::numeric_constant:
1636 // FIXME: implement # 7 line numbers!
1637 DiscardUntilEndOfDirective();
1638 return;
1639 default:
1640 IdentifierInfo *II = Result.getIdentifierInfo();
1641 if (II == 0) break; // Not an identifier.
1642
1643 // Ask what the preprocessor keyword ID is.
1644 switch (II->getPPKeywordID()) {
1645 default: break;
1646 // C99 6.10.1 - Conditional Inclusion.
1647 case tok::pp_if:
1648 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
1649 case tok::pp_ifdef:
1650 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
1651 case tok::pp_ifndef:
1652 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
1653 case tok::pp_elif:
1654 return HandleElifDirective(Result);
1655 case tok::pp_else:
1656 return HandleElseDirective(Result);
1657 case tok::pp_endif:
1658 return HandleEndifDirective(Result);
1659
1660 // C99 6.10.2 - Source File Inclusion.
1661 case tok::pp_include:
1662 return HandleIncludeDirective(Result); // Handle #include.
1663
1664 // C99 6.10.3 - Macro Replacement.
1665 case tok::pp_define:
1666 return HandleDefineDirective(Result, false);
1667 case tok::pp_undef:
1668 return HandleUndefDirective(Result);
1669
1670 // C99 6.10.4 - Line Control.
1671 case tok::pp_line:
1672 // FIXME: implement #line
1673 DiscardUntilEndOfDirective();
1674 return;
1675
1676 // C99 6.10.5 - Error Directive.
1677 case tok::pp_error:
1678 return HandleUserDiagnosticDirective(Result, false);
1679
1680 // C99 6.10.6 - Pragma Directive.
1681 case tok::pp_pragma:
1682 return HandlePragmaDirective();
1683
1684 // GNU Extensions.
1685 case tok::pp_import:
1686 return HandleImportDirective(Result);
1687 case tok::pp_include_next:
1688 return HandleIncludeNextDirective(Result);
1689
1690 case tok::pp_warning:
1691 Diag(Result, diag::ext_pp_warning_directive);
1692 return HandleUserDiagnosticDirective(Result, true);
1693 case tok::pp_ident:
1694 return HandleIdentSCCSDirective(Result);
1695 case tok::pp_sccs:
1696 return HandleIdentSCCSDirective(Result);
1697 case tok::pp_assert:
1698 //isExtension = true; // FIXME: implement #assert
1699 break;
1700 case tok::pp_unassert:
1701 //isExtension = true; // FIXME: implement #unassert
1702 break;
1703
1704 // clang extensions.
1705 case tok::pp_define_target:
1706 return HandleDefineDirective(Result, true);
1707 case tok::pp_define_other_target:
1708 return HandleDefineOtherTargetDirective(Result);
1709 }
1710 break;
1711 }
1712
1713 // If we reached here, the preprocessing token is not valid!
1714 Diag(Result, diag::err_pp_invalid_directive);
1715
1716 // Read the rest of the PP line.
1717 DiscardUntilEndOfDirective();
1718
1719 // Okay, we're done parsing the directive.
1720}
1721
1722void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
1723 bool isWarning) {
1724 // Read the rest of the line raw. We do this because we don't want macros
1725 // to be expanded and we don't require that the tokens be valid preprocessing
1726 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1727 // collapse multiple consequtive white space between tokens, but this isn't
1728 // specified by the standard.
1729 std::string Message = CurLexer->ReadToEndOfLine();
1730
1731 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
1732 return Diag(Tok, DiagID, Message);
1733}
1734
1735/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1736///
1737void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1738 // Yes, this directive is an extension.
1739 Diag(Tok, diag::ext_pp_ident_directive);
1740
1741 // Read the string argument.
1742 Token StrTok;
1743 Lex(StrTok);
1744
1745 // If the token kind isn't a string, it's a malformed directive.
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001746 if (StrTok.isNot(tok::string_literal) &&
1747 StrTok.isNot(tok::wide_string_literal))
Chris Lattner4b009652007-07-25 00:24:17 +00001748 return Diag(StrTok, diag::err_pp_malformed_ident);
1749
1750 // Verify that there is nothing after the string, other than EOM.
1751 CheckEndOfDirective("#ident");
1752
1753 if (Callbacks)
1754 Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok));
1755}
1756
1757//===----------------------------------------------------------------------===//
1758// Preprocessor Include Directive Handling.
1759//===----------------------------------------------------------------------===//
1760
1761/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1762/// checked and spelled filename, e.g. as an operand of #include. This returns
1763/// true if the input filename was in <>'s or false if it were in ""'s. The
1764/// caller is expected to provide a buffer that is large enough to hold the
1765/// spelling of the filename, but is also expected to handle the case when
1766/// this method decides to use a different buffer.
1767bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
1768 const char *&BufStart,
1769 const char *&BufEnd) {
1770 // Get the text form of the filename.
1771 assert(BufStart != BufEnd && "Can't have tokens with empty spellings!");
1772
1773 // Make sure the filename is <x> or "x".
1774 bool isAngled;
1775 if (BufStart[0] == '<') {
1776 if (BufEnd[-1] != '>') {
1777 Diag(Loc, diag::err_pp_expects_filename);
1778 BufStart = 0;
1779 return true;
1780 }
1781 isAngled = true;
1782 } else if (BufStart[0] == '"') {
1783 if (BufEnd[-1] != '"') {
1784 Diag(Loc, diag::err_pp_expects_filename);
1785 BufStart = 0;
1786 return true;
1787 }
1788 isAngled = false;
1789 } else {
1790 Diag(Loc, diag::err_pp_expects_filename);
1791 BufStart = 0;
1792 return true;
1793 }
1794
1795 // Diagnose #include "" as invalid.
1796 if (BufEnd-BufStart <= 2) {
1797 Diag(Loc, diag::err_pp_empty_filename);
1798 BufStart = 0;
1799 return "";
1800 }
1801
1802 // Skip the brackets.
1803 ++BufStart;
1804 --BufEnd;
1805 return isAngled;
1806}
1807
1808/// ConcatenateIncludeName - Handle cases where the #include name is expanded
1809/// from a macro as multiple tokens, which need to be glued together. This
1810/// occurs for code like:
1811/// #define FOO <a/b.h>
1812/// #include FOO
1813/// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1814///
1815/// This code concatenates and consumes tokens up to the '>' token. It returns
1816/// false if the > was found, otherwise it returns true if it finds and consumes
1817/// the EOM marker.
1818static bool ConcatenateIncludeName(llvm::SmallVector<char, 128> &FilenameBuffer,
1819 Preprocessor &PP) {
1820 Token CurTok;
1821
1822 PP.Lex(CurTok);
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001823 while (CurTok.isNot(tok::eom)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001824 // Append the spelling of this token to the buffer. If there was a space
1825 // before it, add it now.
1826 if (CurTok.hasLeadingSpace())
1827 FilenameBuffer.push_back(' ');
1828
1829 // Get the spelling of the token, directly into FilenameBuffer if possible.
1830 unsigned PreAppendSize = FilenameBuffer.size();
1831 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1832
1833 const char *BufPtr = &FilenameBuffer[PreAppendSize];
1834 unsigned ActualLen = PP.getSpelling(CurTok, BufPtr);
1835
1836 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1837 if (BufPtr != &FilenameBuffer[PreAppendSize])
1838 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1839
1840 // Resize FilenameBuffer to the correct size.
1841 if (CurTok.getLength() != ActualLen)
1842 FilenameBuffer.resize(PreAppendSize+ActualLen);
1843
1844 // If we found the '>' marker, return success.
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001845 if (CurTok.is(tok::greater))
Chris Lattner4b009652007-07-25 00:24:17 +00001846 return false;
1847
1848 PP.Lex(CurTok);
1849 }
1850
1851 // If we hit the eom marker, emit an error and return true so that the caller
1852 // knows the EOM has been read.
1853 PP.Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1854 return true;
1855}
1856
1857/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1858/// file to be included from the lexer, then include it! This is a common
1859/// routine with functionality shared between #include, #include_next and
1860/// #import.
1861void Preprocessor::HandleIncludeDirective(Token &IncludeTok,
1862 const DirectoryLookup *LookupFrom,
1863 bool isImport) {
1864
1865 Token FilenameTok;
1866 CurLexer->LexIncludeFilename(FilenameTok);
1867
1868 // Reserve a buffer to get the spelling.
1869 llvm::SmallVector<char, 128> FilenameBuffer;
1870 const char *FilenameStart, *FilenameEnd;
1871
1872 switch (FilenameTok.getKind()) {
1873 case tok::eom:
1874 // If the token kind is EOM, the error has already been diagnosed.
1875 return;
1876
1877 case tok::angle_string_literal:
1878 case tok::string_literal: {
1879 FilenameBuffer.resize(FilenameTok.getLength());
1880 FilenameStart = &FilenameBuffer[0];
1881 unsigned Len = getSpelling(FilenameTok, FilenameStart);
1882 FilenameEnd = FilenameStart+Len;
1883 break;
1884 }
1885
1886 case tok::less:
1887 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1888 // case, glue the tokens together into FilenameBuffer and interpret those.
1889 FilenameBuffer.push_back('<');
1890 if (ConcatenateIncludeName(FilenameBuffer, *this))
1891 return; // Found <eom> but no ">"? Diagnostic already emitted.
1892 FilenameStart = &FilenameBuffer[0];
1893 FilenameEnd = &FilenameBuffer[FilenameBuffer.size()];
1894 break;
1895 default:
1896 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1897 DiscardUntilEndOfDirective();
1898 return;
1899 }
1900
1901 bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(),
1902 FilenameStart, FilenameEnd);
1903 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1904 // error.
1905 if (FilenameStart == 0) {
1906 DiscardUntilEndOfDirective();
1907 return;
1908 }
1909
1910 // Verify that there is nothing after the filename, other than EOM. Use the
1911 // preprocessor to lex this in case lexing the filename entered a macro.
1912 CheckEndOfDirective("#include");
1913
1914 // Check that we don't have infinite #include recursion.
1915 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
1916 return Diag(FilenameTok, diag::err_pp_include_too_deep);
1917
1918 // Search include directories.
1919 const DirectoryLookup *CurDir;
1920 const FileEntry *File = LookupFile(FilenameStart, FilenameEnd,
1921 isAngled, LookupFrom, CurDir);
1922 if (File == 0)
1923 return Diag(FilenameTok, diag::err_pp_file_not_found,
1924 std::string(FilenameStart, FilenameEnd));
1925
1926 // Ask HeaderInfo if we should enter this #include file.
1927 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
1928 // If it returns true, #including this file will have no effect.
1929 return;
1930 }
1931
1932 // Look up the file, create a File ID for it.
1933 unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation());
1934 if (FileID == 0)
1935 return Diag(FilenameTok, diag::err_pp_file_not_found,
1936 std::string(FilenameStart, FilenameEnd));
1937
1938 // Finally, if all is good, enter the new file!
1939 EnterSourceFile(FileID, CurDir);
1940}
1941
1942/// HandleIncludeNextDirective - Implements #include_next.
1943///
1944void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) {
1945 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
1946
1947 // #include_next is like #include, except that we start searching after
1948 // the current found directory. If we can't do this, issue a
1949 // diagnostic.
1950 const DirectoryLookup *Lookup = CurDirLookup;
1951 if (isInPrimaryFile()) {
1952 Lookup = 0;
1953 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1954 } else if (Lookup == 0) {
1955 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1956 } else {
1957 // Start looking up in the next directory.
1958 ++Lookup;
1959 }
1960
1961 return HandleIncludeDirective(IncludeNextTok, Lookup);
1962}
1963
1964/// HandleImportDirective - Implements #import.
1965///
1966void Preprocessor::HandleImportDirective(Token &ImportTok) {
1967 Diag(ImportTok, diag::ext_pp_import_directive);
1968
1969 return HandleIncludeDirective(ImportTok, 0, true);
1970}
1971
1972//===----------------------------------------------------------------------===//
1973// Preprocessor Macro Directive Handling.
1974//===----------------------------------------------------------------------===//
1975
1976/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1977/// definition has just been read. Lex the rest of the arguments and the
1978/// closing ), updating MI with what we learn. Return true if an error occurs
1979/// parsing the arg list.
1980bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
1981 llvm::SmallVector<IdentifierInfo*, 32> Arguments;
1982
1983 Token Tok;
1984 while (1) {
1985 LexUnexpandedToken(Tok);
1986 switch (Tok.getKind()) {
1987 case tok::r_paren:
1988 // Found the end of the argument list.
1989 if (Arguments.empty()) { // #define FOO()
1990 MI->setArgumentList(Arguments.begin(), Arguments.end());
1991 return false;
1992 }
1993 // Otherwise we have #define FOO(A,)
1994 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1995 return true;
1996 case tok::ellipsis: // #define X(... -> C99 varargs
1997 // Warn if use of C99 feature in non-C99 mode.
1998 if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
1999
2000 // Lex the token after the identifier.
2001 LexUnexpandedToken(Tok);
Chris Lattnercb8e41c2007-10-09 18:02:16 +00002002 if (Tok.isNot(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002003 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2004 return true;
2005 }
2006 // Add the __VA_ARGS__ identifier as an argument.
2007 Arguments.push_back(Ident__VA_ARGS__);
2008 MI->setIsC99Varargs();
2009 MI->setArgumentList(Arguments.begin(), Arguments.end());
2010 return false;
2011 case tok::eom: // #define X(
2012 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2013 return true;
2014 default:
2015 // Handle keywords and identifiers here to accept things like
2016 // #define Foo(for) for.
2017 IdentifierInfo *II = Tok.getIdentifierInfo();
2018 if (II == 0) {
2019 // #define X(1
2020 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
2021 return true;
2022 }
2023
2024 // If this is already used as an argument, it is used multiple times (e.g.
2025 // #define X(A,A.
2026 if (std::find(Arguments.begin(), Arguments.end(), II) !=
2027 Arguments.end()) { // C99 6.10.3p6
2028 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName());
2029 return true;
2030 }
2031
2032 // Add the argument to the macro info.
2033 Arguments.push_back(II);
2034
2035 // Lex the token after the identifier.
2036 LexUnexpandedToken(Tok);
2037
2038 switch (Tok.getKind()) {
2039 default: // #define X(A B
2040 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
2041 return true;
2042 case tok::r_paren: // #define X(A)
2043 MI->setArgumentList(Arguments.begin(), Arguments.end());
2044 return false;
2045 case tok::comma: // #define X(A,
2046 break;
2047 case tok::ellipsis: // #define X(A... -> GCC extension
2048 // Diagnose extension.
2049 Diag(Tok, diag::ext_named_variadic_macro);
2050
2051 // Lex the token after the identifier.
2052 LexUnexpandedToken(Tok);
Chris Lattnercb8e41c2007-10-09 18:02:16 +00002053 if (Tok.isNot(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002054 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2055 return true;
2056 }
2057
2058 MI->setIsGNUVarargs();
2059 MI->setArgumentList(Arguments.begin(), Arguments.end());
2060 return false;
2061 }
2062 }
2063 }
2064}
2065
2066/// HandleDefineDirective - Implements #define. This consumes the entire macro
2067/// line then lets the caller lex the next real token. If 'isTargetSpecific' is
2068/// true, then this is a "#define_target", otherwise this is a "#define".
2069///
2070void Preprocessor::HandleDefineDirective(Token &DefineTok,
2071 bool isTargetSpecific) {
2072 ++NumDefined;
2073
2074 Token MacroNameTok;
2075 ReadMacroName(MacroNameTok, 1);
2076
2077 // Error reading macro name? If so, diagnostic already issued.
Chris Lattnercb8e41c2007-10-09 18:02:16 +00002078 if (MacroNameTok.is(tok::eom))
Chris Lattner4b009652007-07-25 00:24:17 +00002079 return;
2080
2081 // If we are supposed to keep comments in #defines, reenable comment saving
2082 // mode.
2083 CurLexer->KeepCommentMode = KeepMacroComments;
2084
2085 // Create the new macro.
2086 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
2087 if (isTargetSpecific) MI->setIsTargetSpecific();
2088
2089 // If the identifier is an 'other target' macro, clear this bit.
2090 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
2091
2092
2093 Token Tok;
2094 LexUnexpandedToken(Tok);
2095
2096 // If this is a function-like macro definition, parse the argument list,
2097 // marking each of the identifiers as being used as macro arguments. Also,
2098 // check other constraints on the first token of the macro body.
Chris Lattnercb8e41c2007-10-09 18:02:16 +00002099 if (Tok.is(tok::eom)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002100 // If there is no body to this macro, we have no special handling here.
Chris Lattnercb8e41c2007-10-09 18:02:16 +00002101 } else if (Tok.is(tok::l_paren) && !Tok.hasLeadingSpace()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002102 // This is a function-like macro definition. Read the argument list.
2103 MI->setIsFunctionLike();
2104 if (ReadMacroDefinitionArgList(MI)) {
2105 // Forget about MI.
2106 delete MI;
2107 // Throw away the rest of the line.
2108 if (CurLexer->ParsingPreprocessorDirective)
2109 DiscardUntilEndOfDirective();
2110 return;
2111 }
2112
2113 // Read the first token after the arg list for down below.
2114 LexUnexpandedToken(Tok);
2115 } else if (!Tok.hasLeadingSpace()) {
2116 // C99 requires whitespace between the macro definition and the body. Emit
2117 // a diagnostic for something like "#define X+".
2118 if (Features.C99) {
2119 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
2120 } else {
2121 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
2122 // one in some cases!
2123 }
2124 } else {
2125 // This is a normal token with leading space. Clear the leading space
2126 // marker on the first token to get proper expansion.
2127 Tok.clearFlag(Token::LeadingSpace);
2128 }
2129
2130 // If this is a definition of a variadic C99 function-like macro, not using
2131 // the GNU named varargs extension, enabled __VA_ARGS__.
2132
2133 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
2134 // This gets unpoisoned where it is allowed.
2135 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
2136 if (MI->isC99Varargs())
2137 Ident__VA_ARGS__->setIsPoisoned(false);
2138
2139 // Read the rest of the macro body.
2140 if (MI->isObjectLike()) {
2141 // Object-like macros are very simple, just read their body.
Chris Lattnercb8e41c2007-10-09 18:02:16 +00002142 while (Tok.isNot(tok::eom)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002143 MI->AddTokenToBody(Tok);
2144 // Get the next token of the macro.
2145 LexUnexpandedToken(Tok);
2146 }
2147
2148 } else {
2149 // Otherwise, read the body of a function-like macro. This has to validate
2150 // the # (stringize) operator.
Chris Lattnercb8e41c2007-10-09 18:02:16 +00002151 while (Tok.isNot(tok::eom)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002152 MI->AddTokenToBody(Tok);
2153
2154 // Check C99 6.10.3.2p1: ensure that # operators are followed by macro
2155 // parameters in function-like macro expansions.
Chris Lattnercb8e41c2007-10-09 18:02:16 +00002156 if (Tok.isNot(tok::hash)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002157 // Get the next token of the macro.
2158 LexUnexpandedToken(Tok);
2159 continue;
2160 }
2161
2162 // Get the next token of the macro.
2163 LexUnexpandedToken(Tok);
2164
2165 // Not a macro arg identifier?
2166 if (!Tok.getIdentifierInfo() ||
2167 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
2168 Diag(Tok, diag::err_pp_stringize_not_parameter);
2169 delete MI;
2170
2171 // Disable __VA_ARGS__ again.
2172 Ident__VA_ARGS__->setIsPoisoned(true);
2173 return;
2174 }
2175
2176 // Things look ok, add the param name token to the macro.
2177 MI->AddTokenToBody(Tok);
2178
2179 // Get the next token of the macro.
2180 LexUnexpandedToken(Tok);
2181 }
2182 }
2183
2184
2185 // Disable __VA_ARGS__ again.
2186 Ident__VA_ARGS__->setIsPoisoned(true);
2187
2188 // Check that there is no paste (##) operator at the begining or end of the
2189 // replacement list.
2190 unsigned NumTokens = MI->getNumTokens();
2191 if (NumTokens != 0) {
Chris Lattnercb8e41c2007-10-09 18:02:16 +00002192 if (MI->getReplacementToken(0).is(tok::hashhash)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002193 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
2194 delete MI;
2195 return;
2196 }
Chris Lattnercb8e41c2007-10-09 18:02:16 +00002197 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002198 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
2199 delete MI;
2200 return;
2201 }
2202 }
2203
2204 // If this is the primary source file, remember that this macro hasn't been
2205 // used yet.
2206 if (isInPrimaryFile())
2207 MI->setIsUsed(false);
2208
2209 // Finally, if this identifier already had a macro defined for it, verify that
2210 // the macro bodies are identical and free the old definition.
Chris Lattner7a1b0882007-10-07 08:44:20 +00002211 if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Chris Lattner4b009652007-07-25 00:24:17 +00002212 if (!OtherMI->isUsed())
2213 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2214
2215 // Macros must be identical. This means all tokes and whitespace separation
2216 // must be the same. C99 6.10.3.2.
2217 if (!MI->isIdenticalTo(*OtherMI, *this)) {
2218 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef,
2219 MacroNameTok.getIdentifierInfo()->getName());
2220 Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2);
2221 }
2222 delete OtherMI;
2223 }
2224
Chris Lattner7a1b0882007-10-07 08:44:20 +00002225 setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
Chris Lattner4b009652007-07-25 00:24:17 +00002226}
2227
2228/// HandleDefineOtherTargetDirective - Implements #define_other_target.
2229void Preprocessor::HandleDefineOtherTargetDirective(Token &Tok) {
2230 Token MacroNameTok;
2231 ReadMacroName(MacroNameTok, 1);
2232
2233 // Error reading macro name? If so, diagnostic already issued.
Chris Lattnercb8e41c2007-10-09 18:02:16 +00002234 if (MacroNameTok.is(tok::eom))
Chris Lattner4b009652007-07-25 00:24:17 +00002235 return;
2236
2237 // Check to see if this is the last token on the #undef line.
2238 CheckEndOfDirective("#define_other_target");
2239
2240 // If there is already a macro defined by this name, turn it into a
2241 // target-specific define.
Chris Lattner7a1b0882007-10-07 08:44:20 +00002242 if (MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Chris Lattner4b009652007-07-25 00:24:17 +00002243 MI->setIsTargetSpecific(true);
2244 return;
2245 }
2246
2247 // Mark the identifier as being a macro on some other target.
2248 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro();
2249}
2250
2251
2252/// HandleUndefDirective - Implements #undef.
2253///
2254void Preprocessor::HandleUndefDirective(Token &UndefTok) {
2255 ++NumUndefined;
2256
2257 Token MacroNameTok;
2258 ReadMacroName(MacroNameTok, 2);
2259
2260 // Error reading macro name? If so, diagnostic already issued.
Chris Lattnercb8e41c2007-10-09 18:02:16 +00002261 if (MacroNameTok.is(tok::eom))
Chris Lattner4b009652007-07-25 00:24:17 +00002262 return;
2263
2264 // Check to see if this is the last token on the #undef line.
2265 CheckEndOfDirective("#undef");
2266
2267 // Okay, we finally have a valid identifier to undef.
Chris Lattner7a1b0882007-10-07 08:44:20 +00002268 MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
Chris Lattner4b009652007-07-25 00:24:17 +00002269
2270 // #undef untaints an identifier if it were marked by define_other_target.
2271 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
2272
2273 // If the macro is not defined, this is a noop undef, just return.
2274 if (MI == 0) return;
2275
2276 if (!MI->isUsed())
2277 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
2278
2279 // Free macro definition.
2280 delete MI;
Chris Lattner7a1b0882007-10-07 08:44:20 +00002281 setMacroInfo(MacroNameTok.getIdentifierInfo(), 0);
Chris Lattner4b009652007-07-25 00:24:17 +00002282}
2283
2284
2285//===----------------------------------------------------------------------===//
2286// Preprocessor Conditional Directive Handling.
2287//===----------------------------------------------------------------------===//
2288
2289/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
2290/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
2291/// if any tokens have been returned or pp-directives activated before this
2292/// #ifndef has been lexed.
2293///
2294void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
2295 bool ReadAnyTokensBeforeDirective) {
2296 ++NumIf;
2297 Token DirectiveTok = Result;
2298
2299 Token MacroNameTok;
2300 ReadMacroName(MacroNameTok);
2301
2302 // Error reading macro name? If so, diagnostic already issued.
Chris Lattnercb8e41c2007-10-09 18:02:16 +00002303 if (MacroNameTok.is(tok::eom)) {
Chris Lattnere6cdeb52007-09-24 05:14:57 +00002304 // Skip code until we get to #endif. This helps with recovery by not
2305 // emitting an error when the #endif is reached.
2306 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2307 /*Foundnonskip*/false, /*FoundElse*/false);
Chris Lattner4b009652007-07-25 00:24:17 +00002308 return;
Chris Lattnere6cdeb52007-09-24 05:14:57 +00002309 }
Chris Lattner4b009652007-07-25 00:24:17 +00002310
2311 // Check to see if this is the last token on the #if[n]def line.
2312 CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
2313
2314 // If the start of a top-level #ifdef, inform MIOpt.
2315 if (!ReadAnyTokensBeforeDirective &&
2316 CurLexer->getConditionalStackDepth() == 0) {
2317 assert(isIfndef && "#ifdef shouldn't reach here");
2318 CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
2319 }
2320
2321 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
Chris Lattner7a1b0882007-10-07 08:44:20 +00002322 MacroInfo *MI = getMacroInfo(MII);
Chris Lattner4b009652007-07-25 00:24:17 +00002323
2324 // If there is a macro, process it.
2325 if (MI) {
2326 // Mark it used.
2327 MI->setIsUsed(true);
2328
2329 // If this is the first use of a target-specific macro, warn about it.
2330 if (MI->isTargetSpecific()) {
2331 MI->setIsTargetSpecific(false); // Don't warn on second use.
2332 getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(),
2333 diag::port_target_macro_use);
2334 }
2335 } else {
2336 // Use of a target-specific macro for some other target? If so, warn.
2337 if (MII->isOtherTargetMacro()) {
2338 MII->setIsOtherTargetMacro(false); // Don't warn on second use.
2339 getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(),
2340 diag::port_target_macro_use);
2341 }
2342 }
2343
2344 // Should we include the stuff contained by this directive?
2345 if (!MI == isIfndef) {
2346 // Yes, remember that we are inside a conditional, then lex the next token.
2347 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
2348 /*foundnonskip*/true, /*foundelse*/false);
2349 } else {
2350 // No, skip the contents of this block and return the first token after it.
2351 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2352 /*Foundnonskip*/false,
2353 /*FoundElse*/false);
2354 }
2355}
2356
2357/// HandleIfDirective - Implements the #if directive.
2358///
2359void Preprocessor::HandleIfDirective(Token &IfToken,
2360 bool ReadAnyTokensBeforeDirective) {
2361 ++NumIf;
2362
2363 // Parse and evaluation the conditional expression.
2364 IdentifierInfo *IfNDefMacro = 0;
2365 bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2366
2367 // Should we include the stuff contained by this directive?
2368 if (ConditionalTrue) {
2369 // If this condition is equivalent to #ifndef X, and if this is the first
2370 // directive seen, handle it for the multiple-include optimization.
2371 if (!ReadAnyTokensBeforeDirective &&
2372 CurLexer->getConditionalStackDepth() == 0 && IfNDefMacro)
2373 CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
2374
2375 // Yes, remember that we are inside a conditional, then lex the next token.
2376 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2377 /*foundnonskip*/true, /*foundelse*/false);
2378 } else {
2379 // No, skip the contents of this block and return the first token after it.
2380 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
2381 /*FoundElse*/false);
2382 }
2383}
2384
2385/// HandleEndifDirective - Implements the #endif directive.
2386///
2387void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2388 ++NumEndif;
2389
2390 // Check that this is the whole directive.
2391 CheckEndOfDirective("#endif");
2392
2393 PPConditionalInfo CondInfo;
2394 if (CurLexer->popConditionalLevel(CondInfo)) {
2395 // No conditionals on the stack: this is an #endif without an #if.
2396 return Diag(EndifToken, diag::err_pp_endif_without_if);
2397 }
2398
2399 // If this the end of a top-level #endif, inform MIOpt.
2400 if (CurLexer->getConditionalStackDepth() == 0)
2401 CurLexer->MIOpt.ExitTopLevelConditional();
2402
2403 assert(!CondInfo.WasSkipping && !CurLexer->LexingRawMode &&
2404 "This code should only be reachable in the non-skipping case!");
2405}
2406
2407
2408void Preprocessor::HandleElseDirective(Token &Result) {
2409 ++NumElse;
2410
2411 // #else directive in a non-skipping conditional... start skipping.
2412 CheckEndOfDirective("#else");
2413
2414 PPConditionalInfo CI;
2415 if (CurLexer->popConditionalLevel(CI))
2416 return Diag(Result, diag::pp_err_else_without_if);
2417
2418 // If this is a top-level #else, inform the MIOpt.
2419 if (CurLexer->getConditionalStackDepth() == 0)
2420 CurLexer->MIOpt.FoundTopLevelElse();
2421
2422 // If this is a #else with a #else before it, report the error.
2423 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2424
2425 // Finally, skip the rest of the contents of this block and return the first
2426 // token after it.
2427 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2428 /*FoundElse*/true);
2429}
2430
2431void Preprocessor::HandleElifDirective(Token &ElifToken) {
2432 ++NumElse;
2433
2434 // #elif directive in a non-skipping conditional... start skipping.
2435 // We don't care what the condition is, because we will always skip it (since
2436 // the block immediately before it was included).
2437 DiscardUntilEndOfDirective();
2438
2439 PPConditionalInfo CI;
2440 if (CurLexer->popConditionalLevel(CI))
2441 return Diag(ElifToken, diag::pp_err_elif_without_if);
2442
2443 // If this is a top-level #elif, inform the MIOpt.
2444 if (CurLexer->getConditionalStackDepth() == 0)
2445 CurLexer->MIOpt.FoundTopLevelElse();
2446
2447 // If this is a #elif with a #else before it, report the error.
2448 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2449
2450 // Finally, skip the rest of the contents of this block and return the first
2451 // token after it.
2452 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2453 /*FoundElse*/CI.FoundElse);
2454}
2455