blob: 7bd359ae18d6778ddc7c47561a807937929dd9cc [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Preprocessor interface.
11//
12//===----------------------------------------------------------------------===//
13//
14// Options to support:
15// -H - Print the name of each header file used.
16// -d[MDNI] - Dump various things.
17// -fworking-directory - #line's with preprocessor's working dir.
18// -fpreprocessed
19// -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD
20// -W*
21// -w
22//
23// Messages to emit:
24// "Multiple include guards may be useful for:\n"
25//
26//===----------------------------------------------------------------------===//
27
28#include "clang/Lex/Preprocessor.h"
29#include "clang/Lex/HeaderSearch.h"
30#include "clang/Lex/MacroInfo.h"
31#include "clang/Lex/PPCallbacks.h"
32#include "clang/Lex/Pragma.h"
33#include "clang/Lex/ScratchBuffer.h"
34#include "clang/Basic/Diagnostic.h"
35#include "clang/Basic/FileManager.h"
36#include "clang/Basic/SourceManager.h"
37#include "clang/Basic/TargetInfo.h"
38#include "llvm/ADT/SmallVector.h"
Chris Lattner97ba77c2007-07-16 06:48:38 +000039#include "llvm/Support/MemoryBuffer.h"
Ted Kremenekbdd30c22008-01-14 16:44:48 +000040#include "llvm/Support/Streams.h"
Chris Lattner77034d32007-09-03 18:30:32 +000041#include <ctime>
Reid Spencer5f016e22007-07-11 17:01:13 +000042using namespace clang;
43
44//===----------------------------------------------------------------------===//
45
46Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
47 TargetInfo &target, SourceManager &SM,
48 HeaderSearch &Headers)
49 : Diags(diags), Features(opts), Target(target), FileMgr(Headers.getFileMgr()),
50 SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts),
51 CurLexer(0), CurDirLookup(0), CurMacroExpander(0), Callbacks(0) {
52 ScratchBuf = new ScratchBuffer(SourceMgr);
Chris Lattner9594acf2007-07-15 00:25:26 +000053
Reid Spencer5f016e22007-07-11 17:01:13 +000054 // Clear stats.
55 NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
56 NumIf = NumElse = NumEndif = 0;
57 NumEnteredSourceFiles = 0;
58 NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
59 NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
60 MaxIncludeStackDepth = 0;
61 NumSkipped = 0;
62
63 // Default to discarding comments.
64 KeepComments = false;
65 KeepMacroComments = false;
66
67 // Macro expansion is enabled.
68 DisableMacroExpansion = false;
69 InMacroArgs = false;
Chris Lattner9594acf2007-07-15 00:25:26 +000070 NumCachedMacroExpanders = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000071
72 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
73 // This gets unpoisoned where it is allowed.
74 (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
75
Chris Lattner53b0dab2007-10-09 22:10:18 +000076 Predefines = 0;
77
Reid Spencer5f016e22007-07-11 17:01:13 +000078 // Initialize the pragma handlers.
79 PragmaHandlers = new PragmaNamespace(0);
80 RegisterBuiltinPragmas();
81
82 // Initialize builtin macros like __LINE__ and friends.
83 RegisterBuiltinMacros();
84}
85
86Preprocessor::~Preprocessor() {
87 // Free any active lexers.
88 delete CurLexer;
89
90 while (!IncludeMacroStack.empty()) {
91 delete IncludeMacroStack.back().TheLexer;
92 delete IncludeMacroStack.back().TheMacroExpander;
93 IncludeMacroStack.pop_back();
94 }
Chris Lattnercc1a8752007-10-07 08:44:20 +000095
96 // Free any macro definitions.
97 for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
98 Macros.begin(), E = Macros.end(); I != E; ++I) {
99 // Free the macro definition.
100 delete I->second;
101 I->second = 0;
102 I->first->setHasMacroDefinition(false);
103 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000104
Chris Lattner9594acf2007-07-15 00:25:26 +0000105 // Free any cached macro expanders.
106 for (unsigned i = 0, e = NumCachedMacroExpanders; i != e; ++i)
107 delete MacroExpanderCache[i];
108
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 // Release pragma information.
110 delete PragmaHandlers;
111
112 // Delete the scratch buffer info.
113 delete ScratchBuf;
114}
115
116PPCallbacks::~PPCallbacks() {
117}
118
119/// Diag - Forwarding function for diagnostics. This emits a diagnostic at
Chris Lattnerd2177732007-07-20 16:59:19 +0000120/// the specified Token's location, translating the token's start
Reid Spencer5f016e22007-07-11 17:01:13 +0000121/// position in the current buffer into a SourcePosition object for rendering.
122void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000123 Diags.Report(getFullLoc(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000124}
125
126void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
127 const std::string &Msg) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000128 Diags.Report(getFullLoc(Loc), DiagID, &Msg, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000129}
130
Chris Lattnerd2177732007-07-20 16:59:19 +0000131void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000132 llvm::cerr << tok::getTokenName(Tok.getKind()) << " '"
133 << getSpelling(Tok) << "'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000134
135 if (!DumpFlags) return;
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000136
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000137 llvm::cerr << "\t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 if (Tok.isAtStartOfLine())
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000139 llvm::cerr << " [StartOfLine]";
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 if (Tok.hasLeadingSpace())
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000141 llvm::cerr << " [LeadingSpace]";
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 if (Tok.isExpandDisabled())
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000143 llvm::cerr << " [ExpandDisabled]";
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 if (Tok.needsCleaning()) {
145 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000146 llvm::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
147 << "']";
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 }
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000149
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000150 llvm::cerr << "\tLoc=<";
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000151 DumpLocation(Tok.getLocation());
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000152 llvm::cerr << ">";
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000153}
154
155void Preprocessor::DumpLocation(SourceLocation Loc) const {
156 SourceLocation LogLoc = SourceMgr.getLogicalLoc(Loc);
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000157 llvm::cerr << SourceMgr.getSourceName(LogLoc) << ':'
158 << SourceMgr.getLineNumber(LogLoc) << ':'
159 << SourceMgr.getLineNumber(LogLoc);
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000160
161 SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(Loc);
162 if (PhysLoc != LogLoc) {
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000163 llvm::cerr << " <PhysLoc=";
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000164 DumpLocation(PhysLoc);
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000165 llvm::cerr << ">";
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000166 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000167}
168
169void Preprocessor::DumpMacro(const MacroInfo &MI) const {
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000170 llvm::cerr << "MACRO: ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
172 DumpToken(MI.getReplacementToken(i));
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000173 llvm::cerr << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 }
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000175 llvm::cerr << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000176}
177
178void Preprocessor::PrintStats() {
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000179 llvm::cerr << "\n*** Preprocessor Stats:\n";
180 llvm::cerr << NumDirectives << " directives found:\n";
181 llvm::cerr << " " << NumDefined << " #define.\n";
182 llvm::cerr << " " << NumUndefined << " #undef.\n";
183 llvm::cerr << " #include/#include_next/#import:\n";
184 llvm::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
185 llvm::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
186 llvm::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
187 llvm::cerr << " " << NumElse << " #else/#elif.\n";
188 llvm::cerr << " " << NumEndif << " #endif.\n";
189 llvm::cerr << " " << NumPragma << " #pragma.\n";
190 llvm::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000191
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000192 llvm::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
193 << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
194 << NumFastMacroExpanded << " on the fast path.\n";
195 llvm::cerr << (NumFastTokenPaste+NumTokenPaste)
196 << " token paste (##) operations performed, "
197 << NumFastTokenPaste << " on the fast path.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000198}
199
200//===----------------------------------------------------------------------===//
201// Token Spelling
202//===----------------------------------------------------------------------===//
203
204
205/// getSpelling() - Return the 'spelling' of this token. The spelling of a
206/// token are the characters used to represent the token in the source file
207/// after trigraph expansion and escaped-newline folding. In particular, this
208/// wants to get the true, uncanonicalized, spelling of things like digraphs
209/// UCNs, etc.
Chris Lattnerd2177732007-07-20 16:59:19 +0000210std::string Preprocessor::getSpelling(const Token &Tok) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
212
213 // If this token contains nothing interesting, return it directly.
214 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
215 if (!Tok.needsCleaning())
216 return std::string(TokStart, TokStart+Tok.getLength());
217
218 std::string Result;
219 Result.reserve(Tok.getLength());
220
221 // Otherwise, hard case, relex the characters into the string.
222 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
223 Ptr != End; ) {
224 unsigned CharSize;
225 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
226 Ptr += CharSize;
227 }
228 assert(Result.size() != unsigned(Tok.getLength()) &&
229 "NeedsCleaning flag set on something that didn't need cleaning!");
230 return Result;
231}
232
233/// getSpelling - This method is used to get the spelling of a token into a
234/// preallocated buffer, instead of as an std::string. The caller is required
235/// to allocate enough space for the token, which is guaranteed to be at least
236/// Tok.getLength() bytes long. The actual length of the token is returned.
237///
238/// Note that this method may do two possible things: it may either fill in
239/// the buffer specified with characters, or it may *change the input pointer*
240/// to point to a constant buffer with the data already in it (avoiding a
241/// copy). The caller is not allowed to modify the returned buffer pointer
242/// if an internal buffer is returned.
Chris Lattnerd2177732007-07-20 16:59:19 +0000243unsigned Preprocessor::getSpelling(const Token &Tok,
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 const char *&Buffer) const {
245 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
246
247 // If this token is an identifier, just return the string from the identifier
248 // table, which is very quick.
249 if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
250 Buffer = II->getName();
Chris Lattner0f670322007-07-22 22:50:09 +0000251
252 // Return the length of the token. If the token needed cleaning, don't
253 // include the size of the newlines or trigraphs in it.
254 if (!Tok.needsCleaning())
255 return Tok.getLength();
256 else
257 return strlen(Buffer);
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 }
259
260 // Otherwise, compute the start of the token in the input lexer buffer.
261 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
262
263 // If this token contains nothing interesting, return it directly.
264 if (!Tok.needsCleaning()) {
265 Buffer = TokStart;
266 return Tok.getLength();
267 }
268 // Otherwise, hard case, relex the characters into the string.
269 char *OutBuf = const_cast<char*>(Buffer);
270 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
271 Ptr != End; ) {
272 unsigned CharSize;
273 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
274 Ptr += CharSize;
275 }
276 assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
277 "NeedsCleaning flag set on something that didn't need cleaning!");
278
279 return OutBuf-Buffer;
280}
281
282
283/// CreateString - Plop the specified string into a scratch buffer and return a
284/// location for it. If specified, the source location provides a source
285/// location for the token.
286SourceLocation Preprocessor::
287CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) {
288 if (SLoc.isValid())
289 return ScratchBuf->getToken(Buf, Len, SLoc);
290 return ScratchBuf->getToken(Buf, Len);
291}
292
293
Chris Lattner97ba77c2007-07-16 06:48:38 +0000294/// AdvanceToTokenCharacter - Given a location that specifies the start of a
295/// token, return a new location that specifies a character within the token.
296SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart,
297 unsigned CharNo) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000298 // If they request the first char of the token, we're trivially done. If this
299 // is a macro expansion, it doesn't make sense to point to a character within
300 // the instantiation point (the name). We could point to the source
301 // character, but without also pointing to instantiation info, this is
302 // confusing.
303 if (CharNo == 0 || TokStart.isMacroID()) return TokStart;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000304
305 // Figure out how many physical characters away the specified logical
306 // character is. This needs to take into consideration newlines and
307 // trigraphs.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000308 const char *TokPtr = SourceMgr.getCharacterData(TokStart);
309 unsigned PhysOffset = 0;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000310
311 // The usual case is that tokens don't contain anything interesting. Skip
312 // over the uninteresting characters. If a token only consists of simple
313 // chars, this method is extremely fast.
314 while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr))
Chris Lattner9dc1f532007-07-20 16:37:10 +0000315 ++TokPtr, --CharNo, ++PhysOffset;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000316
317 // If we have a character that may be a trigraph or escaped newline, create a
318 // lexer to parse it correctly.
Chris Lattner97ba77c2007-07-16 06:48:38 +0000319 if (CharNo != 0) {
320 // Create a lexer starting at this token position.
Chris Lattner25bdb512007-07-20 16:52:03 +0000321 Lexer TheLexer(TokStart, *this, TokPtr);
Chris Lattnerd2177732007-07-20 16:59:19 +0000322 Token Tok;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000323 // Skip over characters the remaining characters.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000324 const char *TokStartPtr = TokPtr;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000325 for (; CharNo; --CharNo)
326 TheLexer.getAndAdvanceChar(TokPtr, Tok);
Chris Lattner9dc1f532007-07-20 16:37:10 +0000327
328 PhysOffset += TokPtr-TokStartPtr;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000329 }
Chris Lattner9dc1f532007-07-20 16:37:10 +0000330
331 return TokStart.getFileLocWithOffset(PhysOffset);
Chris Lattner97ba77c2007-07-16 06:48:38 +0000332}
333
334
Chris Lattner53b0dab2007-10-09 22:10:18 +0000335//===----------------------------------------------------------------------===//
336// Preprocessor Initialization Methods
337//===----------------------------------------------------------------------===//
338
339// Append a #define line to Buf for Macro. Macro should be of the form XXX,
340// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
341// "#define XXX Y z W". To get a #define with no value, use "XXX=".
342static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
343 const char *Command = "#define ") {
344 Buf.insert(Buf.end(), Command, Command+strlen(Command));
345 if (const char *Equal = strchr(Macro, '=')) {
346 // Turn the = into ' '.
347 Buf.insert(Buf.end(), Macro, Equal);
348 Buf.push_back(' ');
349 Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal));
350 } else {
351 // Push "macroname 1".
352 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
353 Buf.push_back(' ');
354 Buf.push_back('1');
355 }
356 Buf.push_back('\n');
357}
358
359
360static void InitializePredefinedMacros(Preprocessor &PP,
361 std::vector<char> &Buf) {
362 // FIXME: Implement magic like cpp_init_builtins for things like __STDC__
363 // and __DATE__ etc.
364#if 0
365 /* __STDC__ has the value 1 under normal circumstances.
366 However, if (a) we are in a system header, (b) the option
367 stdc_0_in_system_headers is true (set by target config), and
368 (c) we are not in strictly conforming mode, then it has the
369 value 0. (b) and (c) are already checked in cpp_init_builtins. */
370 //case BT_STDC:
371 if (cpp_in_system_header (pfile))
372 number = 0;
373 else
374 number = 1;
375 break;
376#endif
377 // These should all be defined in the preprocessor according to the
378 // current language configuration.
379 DefineBuiltinMacro(Buf, "__STDC__=1");
380 //DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
381 if (PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus)
382 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
383 else if (0) // STDC94 ?
384 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
385
386 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");
387 if (PP.getLangOptions().ObjC1)
388 DefineBuiltinMacro(Buf, "__OBJC__=1");
389 if (PP.getLangOptions().ObjC2)
390 DefineBuiltinMacro(Buf, "__OBJC2__=1");
Steve Naroff8ee529b2007-10-31 18:42:27 +0000391
Chris Lattnerd19144b2007-10-10 17:48:53 +0000392 // Add __builtin_va_list typedef.
393 {
394 const char *VAList = PP.getTargetInfo().getVAListDeclaration();
395 Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
396 Buf.push_back('\n');
397 }
Chris Lattner53b0dab2007-10-09 22:10:18 +0000398
399 // Get the target #defines.
400 PP.getTargetInfo().getTargetDefines(Buf);
401
402 // Compiler set macros.
403 DefineBuiltinMacro(Buf, "__APPLE_CC__=5250");
Steve Naroff39d0a272007-11-10 18:06:36 +0000404 DefineBuiltinMacro(Buf, "__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=1050");
Chris Lattner53b0dab2007-10-09 22:10:18 +0000405 DefineBuiltinMacro(Buf, "__GNUC_MINOR__=0");
406 DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
407 DefineBuiltinMacro(Buf, "__GNUC__=4");
408 DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
409 DefineBuiltinMacro(Buf, "__VERSION__=\"4.0.1 (Apple Computer, Inc. "
410 "build 5250)\"");
411
412 // Build configuration options.
413 DefineBuiltinMacro(Buf, "__DYNAMIC__=1");
414 DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
415 DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
416 DefineBuiltinMacro(Buf, "__PIC__=1");
417
418
419 if (PP.getLangOptions().CPlusPlus) {
420 DefineBuiltinMacro(Buf, "__DEPRECATED=1");
421 DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
422 DefineBuiltinMacro(Buf, "__GNUG__=4");
423 DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
424 DefineBuiltinMacro(Buf, "__cplusplus=1");
425 DefineBuiltinMacro(Buf, "__private_extern__=extern");
426 }
Steve Naroffd62701b2008-02-07 03:50:06 +0000427 if (PP.getLangOptions().Microsoft) {
428 DefineBuiltinMacro(Buf, "__stdcall=");
429 DefineBuiltinMacro(Buf, "__cdecl=");
430 DefineBuiltinMacro(Buf, "_cdecl=");
431 DefineBuiltinMacro(Buf, "__ptr64=");
Steve Naroffb746ce82008-02-07 23:24:32 +0000432 DefineBuiltinMacro(Buf, "__w64=");
Steve Naroffd62701b2008-02-07 03:50:06 +0000433 DefineBuiltinMacro(Buf, "__forceinline=");
Steve Naroff419154d2008-02-07 15:26:07 +0000434 DefineBuiltinMacro(Buf, "__int8=char");
435 DefineBuiltinMacro(Buf, "__int16=short");
436 DefineBuiltinMacro(Buf, "__int32=int");
Chris Lattner9880ba92008-02-10 21:12:45 +0000437 DefineBuiltinMacro(Buf, "__int64=long long");
Steve Naroff705b5b52008-02-11 22:29:58 +0000438 DefineBuiltinMacro(Buf, "__declspec(X)=");
Steve Naroffd62701b2008-02-07 03:50:06 +0000439 }
Chris Lattner53b0dab2007-10-09 22:10:18 +0000440 // FIXME: Should emit a #line directive here.
441}
442
443
444/// EnterMainSourceFile - Enter the specified FileID as the main source file,
Nate Begeman6b616022008-01-07 04:01:26 +0000445/// which implicitly adds the builtin defines etc.
Ted Kremenek95041a22007-12-19 22:51:13 +0000446void Preprocessor::EnterMainSourceFile() {
447
448 unsigned MainFileID = SourceMgr.getMainFileID();
449
Chris Lattner53b0dab2007-10-09 22:10:18 +0000450 // Enter the main file source buffer.
451 EnterSourceFile(MainFileID, 0);
452
Chris Lattnerb2832982007-11-15 19:07:47 +0000453 // Tell the header info that the main file was entered. If the file is later
454 // #imported, it won't be re-entered.
455 if (const FileEntry *FE =
456 SourceMgr.getFileEntryForLoc(SourceLocation::getFileLoc(MainFileID, 0)))
457 HeaderInfo.IncrementIncludeCount(FE);
458
Chris Lattner53b0dab2007-10-09 22:10:18 +0000459 std::vector<char> PrologFile;
460 PrologFile.reserve(4080);
461
462 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
463 InitializePredefinedMacros(*this, PrologFile);
464
465 // Add on the predefines from the driver.
466 PrologFile.insert(PrologFile.end(), Predefines,Predefines+strlen(Predefines));
467
468 // Memory buffer must end with a null byte!
469 PrologFile.push_back(0);
470
471 // Now that we have emitted the predefined macros, #includes, etc into
472 // PrologFile, preprocess it to populate the initial preprocessor state.
473 llvm::MemoryBuffer *SB =
474 llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(),
475 "<predefines>");
476 assert(SB && "Cannot fail to create predefined source buffer");
477 unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB);
478 assert(FileID && "Could not create FileID for predefines?");
479
480 // Start parsing the predefines.
481 EnterSourceFile(FileID, 0);
482}
Chris Lattner97ba77c2007-07-16 06:48:38 +0000483
Reid Spencer5f016e22007-07-11 17:01:13 +0000484//===----------------------------------------------------------------------===//
485// Source File Location Methods.
486//===----------------------------------------------------------------------===//
487
488/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
489/// return null on failure. isAngled indicates whether the file reference is
490/// for system #include's or not (i.e. using <> instead of "").
491const FileEntry *Preprocessor::LookupFile(const char *FilenameStart,
492 const char *FilenameEnd,
493 bool isAngled,
494 const DirectoryLookup *FromDir,
495 const DirectoryLookup *&CurDir) {
496 // If the header lookup mechanism may be relative to the current file, pass in
497 // info about where the current file is.
498 const FileEntry *CurFileEnt = 0;
499 if (!FromDir) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000500 SourceLocation FileLoc = getCurrentFileLexer()->getFileLoc();
501 CurFileEnt = SourceMgr.getFileEntryForLoc(FileLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000502 }
503
504 // Do a standard file entry lookup.
505 CurDir = CurDirLookup;
506 const FileEntry *FE =
507 HeaderInfo.LookupFile(FilenameStart, FilenameEnd,
508 isAngled, FromDir, CurDir, CurFileEnt);
509 if (FE) return FE;
510
511 // Otherwise, see if this is a subframework header. If so, this is relative
512 // to one of the headers on the #include stack. Walk the list of the current
513 // headers on the #include stack and pass them to HeaderInfo.
514 if (CurLexer && !CurLexer->Is_PragmaLexer) {
Chris Lattner9415a0c2008-02-01 05:34:02 +0000515 if ((CurFileEnt = SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())))
516 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
517 CurFileEnt)))
518 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 }
520
521 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
522 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
523 if (ISEntry.TheLexer && !ISEntry.TheLexer->Is_PragmaLexer) {
Chris Lattner9415a0c2008-02-01 05:34:02 +0000524 if ((CurFileEnt =
525 SourceMgr.getFileEntryForLoc(ISEntry.TheLexer->getFileLoc())))
526 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart,
527 FilenameEnd, CurFileEnt)))
528 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000529 }
530 }
531
532 // Otherwise, we really couldn't find the file.
533 return 0;
534}
535
536/// isInPrimaryFile - Return true if we're in the top-level file, not in a
537/// #include.
538bool Preprocessor::isInPrimaryFile() const {
539 if (CurLexer && !CurLexer->Is_PragmaLexer)
Chris Lattner53b0dab2007-10-09 22:10:18 +0000540 return IncludeMacroStack.empty();
Reid Spencer5f016e22007-07-11 17:01:13 +0000541
542 // If there are any stacked lexers, we're in a #include.
Chris Lattner53b0dab2007-10-09 22:10:18 +0000543 assert(IncludeMacroStack[0].TheLexer &&
544 !IncludeMacroStack[0].TheLexer->Is_PragmaLexer &&
545 "Top level include stack isn't our primary lexer?");
546 for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i)
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 if (IncludeMacroStack[i].TheLexer &&
548 !IncludeMacroStack[i].TheLexer->Is_PragmaLexer)
Chris Lattner53b0dab2007-10-09 22:10:18 +0000549 return false;
550 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000551}
552
553/// getCurrentLexer - Return the current file lexer being lexed from. Note
554/// that this ignores any potentially active macro expansions and _Pragma
555/// expansions going on at the time.
556Lexer *Preprocessor::getCurrentFileLexer() const {
557 if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer;
558
559 // Look for a stacked lexer.
560 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
561 Lexer *L = IncludeMacroStack[i-1].TheLexer;
562 if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions.
563 return L;
564 }
565 return 0;
566}
567
568
569/// EnterSourceFile - Add a source file to the top of the include stack and
570/// start lexing tokens from it instead of the current buffer. Return true
571/// on failure.
572void Preprocessor::EnterSourceFile(unsigned FileID,
Chris Lattner53b0dab2007-10-09 22:10:18 +0000573 const DirectoryLookup *CurDir) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
575 ++NumEnteredSourceFiles;
576
577 if (MaxIncludeStackDepth < IncludeMacroStack.size())
578 MaxIncludeStackDepth = IncludeMacroStack.size();
579
Chris Lattner25bdb512007-07-20 16:52:03 +0000580 Lexer *TheLexer = new Lexer(SourceLocation::getFileLoc(FileID, 0), *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 EnterSourceFileWithLexer(TheLexer, CurDir);
582}
583
584/// EnterSourceFile - Add a source file to the top of the include stack and
585/// start lexing tokens from it instead of the current buffer.
586void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
587 const DirectoryLookup *CurDir) {
588
589 // Add the current lexer to the include stack.
590 if (CurLexer || CurMacroExpander)
591 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
592 CurMacroExpander));
593
594 CurLexer = TheLexer;
595 CurDirLookup = CurDir;
596 CurMacroExpander = 0;
597
598 // Notify the client, if desired, that we are in a new source file.
599 if (Callbacks && !CurLexer->Is_PragmaLexer) {
600 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
601
602 // Get the file entry for the current file.
603 if (const FileEntry *FE =
Chris Lattner9dc1f532007-07-20 16:37:10 +0000604 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 FileType = HeaderInfo.getFileDirFlavor(FE);
606
Chris Lattner9dc1f532007-07-20 16:37:10 +0000607 Callbacks->FileChanged(CurLexer->getFileLoc(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000608 PPCallbacks::EnterFile, FileType);
609 }
610}
611
612
613
614/// EnterMacro - Add a Macro to the top of the include stack and start lexing
615/// tokens from it instead of the current buffer.
Chris Lattnerd2177732007-07-20 16:59:19 +0000616void Preprocessor::EnterMacro(Token &Tok, MacroArgs *Args) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
618 CurMacroExpander));
619 CurLexer = 0;
620 CurDirLookup = 0;
621
Chris Lattner9594acf2007-07-15 00:25:26 +0000622 if (NumCachedMacroExpanders == 0) {
623 CurMacroExpander = new MacroExpander(Tok, Args, *this);
624 } else {
625 CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
626 CurMacroExpander->Init(Tok, Args);
627 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000628}
629
630/// EnterTokenStream - Add a "macro" context to the top of the include stack,
631/// which will cause the lexer to start returning the specified tokens. Note
632/// that these tokens will be re-macro-expanded when/if expansion is enabled.
633/// This method assumes that the specified stream of tokens has a permanent
634/// owner somewhere, so they do not need to be copied.
Chris Lattnerd2177732007-07-20 16:59:19 +0000635void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 // Save our current state.
637 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
638 CurMacroExpander));
639 CurLexer = 0;
640 CurDirLookup = 0;
641
642 // Create a macro expander to expand from the specified token stream.
Chris Lattner9594acf2007-07-15 00:25:26 +0000643 if (NumCachedMacroExpanders == 0) {
644 CurMacroExpander = new MacroExpander(Toks, NumToks, *this);
645 } else {
646 CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
647 CurMacroExpander->Init(Toks, NumToks);
648 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000649}
650
651/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
652/// lexer stack. This should only be used in situations where the current
653/// state of the top-of-stack lexer is known.
654void Preprocessor::RemoveTopOfLexerStack() {
655 assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
Chris Lattner9594acf2007-07-15 00:25:26 +0000656
657 if (CurMacroExpander) {
658 // Delete or cache the now-dead macro expander.
659 if (NumCachedMacroExpanders == MacroExpanderCacheSize)
660 delete CurMacroExpander;
661 else
662 MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander;
663 } else {
664 delete CurLexer;
665 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000666 CurLexer = IncludeMacroStack.back().TheLexer;
667 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
668 CurMacroExpander = IncludeMacroStack.back().TheMacroExpander;
669 IncludeMacroStack.pop_back();
670}
671
672//===----------------------------------------------------------------------===//
673// Macro Expansion Handling.
674//===----------------------------------------------------------------------===//
675
Chris Lattnercc1a8752007-10-07 08:44:20 +0000676/// setMacroInfo - Specify a macro for this identifier.
677///
678void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
679 if (MI == 0) {
680 if (II->hasMacroDefinition()) {
681 Macros.erase(II);
682 II->setHasMacroDefinition(false);
683 }
684 } else {
685 Macros[II] = MI;
686 II->setHasMacroDefinition(true);
687 }
688}
689
Reid Spencer5f016e22007-07-11 17:01:13 +0000690/// RegisterBuiltinMacro - Register the specified identifier in the identifier
691/// table and mark it as a builtin macro to be expanded.
692IdentifierInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) {
693 // Get the identifier.
694 IdentifierInfo *Id = getIdentifierInfo(Name);
695
696 // Mark it as being a macro that is builtin.
697 MacroInfo *MI = new MacroInfo(SourceLocation());
698 MI->setIsBuiltinMacro();
Chris Lattnercc1a8752007-10-07 08:44:20 +0000699 setMacroInfo(Id, MI);
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 return Id;
701}
702
703
704/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
705/// identifier table.
706void Preprocessor::RegisterBuiltinMacros() {
707 Ident__LINE__ = RegisterBuiltinMacro("__LINE__");
708 Ident__FILE__ = RegisterBuiltinMacro("__FILE__");
709 Ident__DATE__ = RegisterBuiltinMacro("__DATE__");
710 Ident__TIME__ = RegisterBuiltinMacro("__TIME__");
711 Ident_Pragma = RegisterBuiltinMacro("_Pragma");
712
713 // GCC Extensions.
714 Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__");
715 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__");
716 Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__");
717}
718
719/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
720/// in its expansion, currently expands to that token literally.
721static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
Chris Lattnercc1a8752007-10-07 08:44:20 +0000722 const IdentifierInfo *MacroIdent,
723 Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
725
726 // If the token isn't an identifier, it's always literally expanded.
727 if (II == 0) return true;
728
729 // If the identifier is a macro, and if that macro is enabled, it may be
730 // expanded so it's not a trivial expansion.
Chris Lattnercc1a8752007-10-07 08:44:20 +0000731 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 // Fast expanding "#define X X" is ok, because X would be disabled.
733 II != MacroIdent)
734 return false;
735
736 // If this is an object-like macro invocation, it is safe to trivially expand
737 // it.
738 if (MI->isObjectLike()) return true;
739
740 // If this is a function-like macro invocation, it's safe to trivially expand
741 // as long as the identifier is not a macro argument.
742 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
743 I != E; ++I)
744 if (*I == II)
745 return false; // Identifier is a macro argument.
746
747 return true;
748}
749
750
751/// isNextPPTokenLParen - Determine whether the next preprocessor token to be
752/// lexed is a '('. If so, consume the token and return true, if not, this
753/// method should have no observable side-effect on the lexed tokens.
754bool Preprocessor::isNextPPTokenLParen() {
755 // Do some quick tests for rejection cases.
756 unsigned Val;
757 if (CurLexer)
758 Val = CurLexer->isNextPPTokenLParen();
759 else
760 Val = CurMacroExpander->isNextTokenLParen();
761
762 if (Val == 2) {
Chris Lattner0ea793e2007-07-19 00:07:36 +0000763 // We have run off the end. If it's a source file we don't
764 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
765 // macro stack.
766 if (CurLexer)
767 return false;
768 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 IncludeStackInfo &Entry = IncludeMacroStack[i-1];
770 if (Entry.TheLexer)
771 Val = Entry.TheLexer->isNextPPTokenLParen();
772 else
773 Val = Entry.TheMacroExpander->isNextTokenLParen();
Chris Lattner0ea793e2007-07-19 00:07:36 +0000774
775 if (Val != 2)
776 break;
777
778 // Ran off the end of a source file?
779 if (Entry.TheLexer)
780 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000781 }
782 }
783
784 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
785 // have found something that isn't a '(' or we found the end of the
786 // translation unit. In either case, return false.
787 if (Val != 1)
788 return false;
789
Chris Lattnerd2177732007-07-20 16:59:19 +0000790 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 LexUnexpandedToken(Tok);
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000792 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 return true;
794}
795
796/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
797/// expanded as a macro, handle it and return the next token as 'Identifier'.
Chris Lattnerd2177732007-07-20 16:59:19 +0000798bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
Reid Spencer5f016e22007-07-11 17:01:13 +0000799 MacroInfo *MI) {
Chris Lattner4d730462008-01-07 19:50:27 +0000800 // If this is a macro exapnsion in the "#if !defined(x)" line for the file,
801 // then the macro could expand to different things in other contexts, we need
802 // to disable the optimization in this case.
803 if (CurLexer) CurLexer->MIOpt.ExpandedMacro();
Reid Spencer5f016e22007-07-11 17:01:13 +0000804
805 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
806 if (MI->isBuiltinMacro()) {
807 ExpandBuiltinMacro(Identifier);
808 return false;
809 }
810
811 // If this is the first use of a target-specific macro, warn about it.
812 if (MI->isTargetSpecific()) {
813 MI->setIsTargetSpecific(false); // Don't warn on second use.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000814 getTargetInfo().DiagnoseNonPortability(getFullLoc(Identifier.getLocation()),
Reid Spencer5f016e22007-07-11 17:01:13 +0000815 diag::port_target_macro_use);
816 }
817
818 /// Args - If this is a function-like macro expansion, this contains,
819 /// for each macro argument, the list of tokens that were provided to the
820 /// invocation.
821 MacroArgs *Args = 0;
822
823 // If this is a function-like macro, read the arguments.
824 if (MI->isFunctionLike()) {
825 // C99 6.10.3p10: If the preprocessing token immediately after the the macro
Chris Lattner2b64fdc2007-07-19 16:11:58 +0000826 // name isn't a '(', this macro should not be expanded. Otherwise, consume
827 // it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000828 if (!isNextPPTokenLParen())
829 return true;
830
831 // Remember that we are now parsing the arguments to a macro invocation.
832 // Preprocessor directives used inside macro arguments are not portable, and
833 // this enables the warning.
834 InMacroArgs = true;
835 Args = ReadFunctionLikeMacroArgs(Identifier, MI);
836
837 // Finished parsing args.
838 InMacroArgs = false;
839
840 // If there was an error parsing the arguments, bail out.
841 if (Args == 0) return false;
842
843 ++NumFnMacroExpanded;
844 } else {
845 ++NumMacroExpanded;
846 }
847
848 // Notice that this macro has been used.
849 MI->setIsUsed(true);
850
851 // If we started lexing a macro, enter the macro expansion body.
852
853 // If this macro expands to no tokens, don't bother to push it onto the
854 // expansion stack, only to take it right back off.
855 if (MI->getNumTokens() == 0) {
856 // No need for arg info.
857 if (Args) Args->destroy();
858
859 // Ignore this macro use, just return the next token in the current
860 // buffer.
861 bool HadLeadingSpace = Identifier.hasLeadingSpace();
862 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
863
864 Lex(Identifier);
865
866 // If the identifier isn't on some OTHER line, inherit the leading
867 // whitespace/first-on-a-line property of this token. This handles
868 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
869 // empty.
870 if (!Identifier.isAtStartOfLine()) {
Chris Lattnerd2177732007-07-20 16:59:19 +0000871 if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
872 if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 }
874 ++NumFastMacroExpanded;
875 return false;
876
877 } else if (MI->getNumTokens() == 1 &&
Chris Lattnercc1a8752007-10-07 08:44:20 +0000878 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
879 *this)){
Reid Spencer5f016e22007-07-11 17:01:13 +0000880 // Otherwise, if this macro expands into a single trivially-expanded
881 // token: expand it now. This handles common cases like
882 // "#define VAL 42".
883
884 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
885 // identifier to the expanded token.
886 bool isAtStartOfLine = Identifier.isAtStartOfLine();
887 bool hasLeadingSpace = Identifier.hasLeadingSpace();
888
889 // Remember where the token is instantiated.
890 SourceLocation InstantiateLoc = Identifier.getLocation();
891
892 // Replace the result token.
893 Identifier = MI->getReplacementToken(0);
894
895 // Restore the StartOfLine/LeadingSpace markers.
Chris Lattnerd2177732007-07-20 16:59:19 +0000896 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
897 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000898
899 // Update the tokens location to include both its logical and physical
900 // locations.
901 SourceLocation Loc =
902 SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc);
903 Identifier.setLocation(Loc);
904
905 // If this is #define X X, we must mark the result as unexpandible.
906 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo())
Chris Lattnercc1a8752007-10-07 08:44:20 +0000907 if (getMacroInfo(NewII) == MI)
Chris Lattnerd2177732007-07-20 16:59:19 +0000908 Identifier.setFlag(Token::DisableExpand);
Reid Spencer5f016e22007-07-11 17:01:13 +0000909
910 // Since this is not an identifier token, it can't be macro expanded, so
911 // we're done.
912 ++NumFastMacroExpanded;
913 return false;
914 }
915
916 // Start expanding the macro.
917 EnterMacro(Identifier, Args);
918
919 // Now that the macro is at the top of the include stack, ask the
920 // preprocessor to read the next token from it.
921 Lex(Identifier);
922 return false;
923}
924
925/// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
926/// invoked to read all of the actual arguments specified for the macro
927/// invocation. This returns null on error.
Chris Lattnerd2177732007-07-20 16:59:19 +0000928MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
Reid Spencer5f016e22007-07-11 17:01:13 +0000929 MacroInfo *MI) {
930 // The number of fixed arguments to parse.
931 unsigned NumFixedArgsLeft = MI->getNumArgs();
932 bool isVariadic = MI->isVariadic();
933
934 // Outer loop, while there are more arguments, keep reading them.
Chris Lattnerd2177732007-07-20 16:59:19 +0000935 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000936 Tok.setKind(tok::comma);
937 --NumFixedArgsLeft; // Start reading the first arg.
938
939 // ArgTokens - Build up a list of tokens that make up each argument. Each
940 // argument is separated by an EOF token. Use a SmallVector so we can avoid
941 // heap allocations in the common case.
Chris Lattnerd2177732007-07-20 16:59:19 +0000942 llvm::SmallVector<Token, 64> ArgTokens;
Reid Spencer5f016e22007-07-11 17:01:13 +0000943
944 unsigned NumActuals = 0;
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000945 while (Tok.is(tok::comma)) {
Chris Lattner2b64fdc2007-07-19 16:11:58 +0000946 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
947 // that we already consumed the first one.
Reid Spencer5f016e22007-07-11 17:01:13 +0000948 unsigned NumParens = 0;
949
950 while (1) {
951 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
952 // an argument value in a macro could expand to ',' or '(' or ')'.
953 LexUnexpandedToken(Tok);
954
Chris Lattnerc21d9e42008-01-22 19:34:51 +0000955 if (Tok.is(tok::eof) || Tok.is(tok::eom)) { // "#if f(<eof>" & "#if f(\n"
Reid Spencer5f016e22007-07-11 17:01:13 +0000956 Diag(MacroName, diag::err_unterm_macro_invoc);
Chris Lattnerc21d9e42008-01-22 19:34:51 +0000957 // Do not lose the EOF/EOM. Return it to the client.
Reid Spencer5f016e22007-07-11 17:01:13 +0000958 MacroName = Tok;
959 return 0;
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000960 } else if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000961 // If we found the ) token, the macro arg list is done.
962 if (NumParens-- == 0)
963 break;
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000964 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000965 ++NumParens;
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000966 } else if (Tok.is(tok::comma) && NumParens == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000967 // Comma ends this argument if there are more fixed arguments expected.
968 if (NumFixedArgsLeft)
969 break;
970
971 // If this is not a variadic macro, too many args were specified.
972 if (!isVariadic) {
973 // Emit the diagnostic at the macro name in case there is a missing ).
974 // Emitting it at the , could be far away from the macro name.
975 Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
976 return 0;
977 }
978 // Otherwise, continue to add the tokens to this variable argument.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000979 } else if (Tok.is(tok::comment) && !KeepMacroComments) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000980 // If this is a comment token in the argument list and we're just in
981 // -C mode (not -CC mode), discard the comment.
982 continue;
Chris Lattner0c3eb292007-11-23 06:50:21 +0000983 } else if (Tok.is(tok::identifier)) {
984 // Reading macro arguments can cause macros that we are currently
985 // expanding from to be popped off the expansion stack. Doing so causes
986 // them to be reenabled for expansion. Here we record whether any
987 // identifiers we lex as macro arguments correspond to disabled macros.
988 // If so, we mark the token as noexpand. This is a subtle aspect of
989 // C99 6.10.3.4p2.
990 if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
991 if (!MI->isEnabled())
992 Tok.setFlag(Token::DisableExpand);
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 }
994
995 ArgTokens.push_back(Tok);
996 }
997
998 // Empty arguments are standard in C99 and supported as an extension in
999 // other modes.
1000 if (ArgTokens.empty() && !Features.C99)
1001 Diag(Tok, diag::ext_empty_fnmacro_arg);
1002
1003 // Add a marker EOF token to the end of the token list for this argument.
Chris Lattnerd2177732007-07-20 16:59:19 +00001004 Token EOFTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 EOFTok.startToken();
1006 EOFTok.setKind(tok::eof);
1007 EOFTok.setLocation(Tok.getLocation());
1008 EOFTok.setLength(0);
1009 ArgTokens.push_back(EOFTok);
1010 ++NumActuals;
1011 --NumFixedArgsLeft;
1012 };
1013
1014 // Okay, we either found the r_paren. Check to see if we parsed too few
1015 // arguments.
1016 unsigned MinArgsExpected = MI->getNumArgs();
1017
1018 // See MacroArgs instance var for description of this.
1019 bool isVarargsElided = false;
1020
1021 if (NumActuals < MinArgsExpected) {
1022 // There are several cases where too few arguments is ok, handle them now.
1023 if (NumActuals+1 == MinArgsExpected && MI->isVariadic()) {
1024 // Varargs where the named vararg parameter is missing: ok as extension.
1025 // #define A(x, ...)
1026 // A("blah")
1027 Diag(Tok, diag::ext_missing_varargs_arg);
1028
1029 // Remember this occurred if this is a C99 macro invocation with at least
1030 // one actual argument.
1031 isVarargsElided = MI->isC99Varargs() && MI->getNumArgs() > 1;
1032 } else if (MI->getNumArgs() == 1) {
1033 // #define A(x)
1034 // A()
1035 // is ok because it is an empty argument.
1036
1037 // Empty arguments are standard in C99 and supported as an extension in
1038 // other modes.
1039 if (ArgTokens.empty() && !Features.C99)
1040 Diag(Tok, diag::ext_empty_fnmacro_arg);
1041 } else {
1042 // Otherwise, emit the error.
1043 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
1044 return 0;
1045 }
1046
1047 // Add a marker EOF token to the end of the token list for this argument.
1048 SourceLocation EndLoc = Tok.getLocation();
1049 Tok.startToken();
1050 Tok.setKind(tok::eof);
1051 Tok.setLocation(EndLoc);
1052 Tok.setLength(0);
1053 ArgTokens.push_back(Tok);
1054 }
1055
1056 return MacroArgs::create(MI, &ArgTokens[0], ArgTokens.size(),isVarargsElided);
1057}
1058
1059/// ComputeDATE_TIME - Compute the current time, enter it into the specified
1060/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
1061/// the identifier tokens inserted.
1062static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
1063 Preprocessor &PP) {
1064 time_t TT = time(0);
1065 struct tm *TM = localtime(&TT);
1066
1067 static const char * const Months[] = {
1068 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
1069 };
1070
1071 char TmpBuffer[100];
1072 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
1073 TM->tm_year+1900);
1074 DATELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
1075
1076 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
1077 TIMELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
1078}
1079
1080/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1081/// as a builtin macro, handle it and return the next token as 'Tok'.
Chris Lattnerd2177732007-07-20 16:59:19 +00001082void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 // Figure out which token this is.
1084 IdentifierInfo *II = Tok.getIdentifierInfo();
1085 assert(II && "Can't be a macro without id info!");
1086
1087 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
1088 // lex the token after it.
1089 if (II == Ident_Pragma)
1090 return Handle_Pragma(Tok);
1091
1092 ++NumBuiltinMacroExpanded;
1093
1094 char TmpBuffer[100];
1095
1096 // Set up the return result.
1097 Tok.setIdentifierInfo(0);
Chris Lattnerd2177732007-07-20 16:59:19 +00001098 Tok.clearFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +00001099
1100 if (II == Ident__LINE__) {
1101 // __LINE__ expands to a simple numeric value.
Chris Lattner9dc1f532007-07-20 16:37:10 +00001102 sprintf(TmpBuffer, "%u", SourceMgr.getLogicalLineNumber(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001103 unsigned Length = strlen(TmpBuffer);
1104 Tok.setKind(tok::numeric_constant);
1105 Tok.setLength(Length);
1106 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
1107 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
1108 SourceLocation Loc = Tok.getLocation();
1109 if (II == Ident__BASE_FILE__) {
1110 Diag(Tok, diag::ext_pp_base_file);
Chris Lattner9dc1f532007-07-20 16:37:10 +00001111 SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc);
1112 while (NextLoc.isValid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001113 Loc = NextLoc;
Chris Lattner9dc1f532007-07-20 16:37:10 +00001114 NextLoc = SourceMgr.getIncludeLoc(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001115 }
1116 }
1117
1118 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
Chris Lattner9dc1f532007-07-20 16:37:10 +00001119 std::string FN = SourceMgr.getSourceName(SourceMgr.getLogicalLoc(Loc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001120 FN = '"' + Lexer::Stringify(FN) + '"';
1121 Tok.setKind(tok::string_literal);
1122 Tok.setLength(FN.size());
1123 Tok.setLocation(CreateString(&FN[0], FN.size(), Tok.getLocation()));
1124 } else if (II == Ident__DATE__) {
1125 if (!DATELoc.isValid())
1126 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1127 Tok.setKind(tok::string_literal);
1128 Tok.setLength(strlen("\"Mmm dd yyyy\""));
1129 Tok.setLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation()));
1130 } else if (II == Ident__TIME__) {
1131 if (!TIMELoc.isValid())
1132 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1133 Tok.setKind(tok::string_literal);
1134 Tok.setLength(strlen("\"hh:mm:ss\""));
1135 Tok.setLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
1136 } else if (II == Ident__INCLUDE_LEVEL__) {
1137 Diag(Tok, diag::ext_pp_include_level);
1138
1139 // Compute the include depth of this token.
1140 unsigned Depth = 0;
Chris Lattner9dc1f532007-07-20 16:37:10 +00001141 SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation());
1142 for (; Loc.isValid(); ++Depth)
1143 Loc = SourceMgr.getIncludeLoc(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001144
1145 // __INCLUDE_LEVEL__ expands to a simple numeric value.
1146 sprintf(TmpBuffer, "%u", Depth);
1147 unsigned Length = strlen(TmpBuffer);
1148 Tok.setKind(tok::numeric_constant);
1149 Tok.setLength(Length);
1150 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
1151 } else if (II == Ident__TIMESTAMP__) {
1152 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
1153 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1154 Diag(Tok, diag::ext_pp_timestamp);
1155
1156 // Get the file that we are lexing out of. If we're currently lexing from
1157 // a macro, dig into the include stack.
1158 const FileEntry *CurFile = 0;
1159 Lexer *TheLexer = getCurrentFileLexer();
1160
1161 if (TheLexer)
Chris Lattner9dc1f532007-07-20 16:37:10 +00001162 CurFile = SourceMgr.getFileEntryForLoc(TheLexer->getFileLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +00001163
1164 // If this file is older than the file it depends on, emit a diagnostic.
1165 const char *Result;
1166 if (CurFile) {
1167 time_t TT = CurFile->getModificationTime();
1168 struct tm *TM = localtime(&TT);
1169 Result = asctime(TM);
1170 } else {
1171 Result = "??? ??? ?? ??:??:?? ????\n";
1172 }
1173 TmpBuffer[0] = '"';
1174 strcpy(TmpBuffer+1, Result);
1175 unsigned Len = strlen(TmpBuffer);
1176 TmpBuffer[Len-1] = '"'; // Replace the newline with a quote.
1177 Tok.setKind(tok::string_literal);
1178 Tok.setLength(Len);
1179 Tok.setLocation(CreateString(TmpBuffer, Len, Tok.getLocation()));
1180 } else {
1181 assert(0 && "Unknown identifier!");
Chris Lattnerc3d8d572007-12-09 20:31:55 +00001182 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001183}
1184
1185//===----------------------------------------------------------------------===//
1186// Lexer Event Handling.
1187//===----------------------------------------------------------------------===//
1188
1189/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
1190/// identifier information for the token and install it into the token.
Chris Lattnerd2177732007-07-20 16:59:19 +00001191IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
Reid Spencer5f016e22007-07-11 17:01:13 +00001192 const char *BufPtr) {
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001193 assert(Identifier.is(tok::identifier) && "Not an identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001194 assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
1195
1196 // Look up this token, see if it is a macro, or if it is a language keyword.
1197 IdentifierInfo *II;
1198 if (BufPtr && !Identifier.needsCleaning()) {
1199 // No cleaning needed, just use the characters from the lexed buffer.
1200 II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
1201 } else {
1202 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
Chris Lattnerc35717a2007-07-13 17:10:38 +00001203 llvm::SmallVector<char, 64> IdentifierBuffer;
1204 IdentifierBuffer.resize(Identifier.getLength());
1205 const char *TmpBuf = &IdentifierBuffer[0];
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 unsigned Size = getSpelling(Identifier, TmpBuf);
1207 II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
1208 }
1209 Identifier.setIdentifierInfo(II);
1210 return II;
1211}
1212
1213
1214/// HandleIdentifier - This callback is invoked when the lexer reads an
1215/// identifier. This callback looks up the identifier in the map and/or
1216/// potentially macro expands it or turns it into a named token (like 'for').
Chris Lattnerd2177732007-07-20 16:59:19 +00001217void Preprocessor::HandleIdentifier(Token &Identifier) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001218 assert(Identifier.getIdentifierInfo() &&
1219 "Can't handle identifiers without identifier info!");
1220
1221 IdentifierInfo &II = *Identifier.getIdentifierInfo();
1222
1223 // If this identifier was poisoned, and if it was not produced from a macro
1224 // expansion, emit an error.
1225 if (II.isPoisoned() && CurLexer) {
1226 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
1227 Diag(Identifier, diag::err_pp_used_poisoned_id);
1228 else
1229 Diag(Identifier, diag::ext_pp_bad_vaargs_use);
1230 }
1231
1232 // If this is a macro to be expanded, do it.
Chris Lattnercc1a8752007-10-07 08:44:20 +00001233 if (MacroInfo *MI = getMacroInfo(&II)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
1235 if (MI->isEnabled()) {
1236 if (!HandleMacroExpandedIdentifier(Identifier, MI))
1237 return;
1238 } else {
1239 // C99 6.10.3.4p2 says that a disabled macro may never again be
1240 // expanded, even if it's in a context where it could be expanded in the
1241 // future.
Chris Lattnerd2177732007-07-20 16:59:19 +00001242 Identifier.setFlag(Token::DisableExpand);
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 }
1244 }
1245 } else if (II.isOtherTargetMacro() && !DisableMacroExpansion) {
1246 // If this identifier is a macro on some other target, emit a diagnostic.
1247 // This diagnosic is only emitted when macro expansion is enabled, because
1248 // the macro would not have been expanded for the other target either.
1249 II.setIsOtherTargetMacro(false); // Don't warn on second use.
Ted Kremenek9c728dc2007-12-12 22:39:36 +00001250 getTargetInfo().DiagnoseNonPortability(getFullLoc(Identifier.getLocation()),
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 diag::port_target_macro_use);
1252
1253 }
1254
1255 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
1256 // then we act as if it is the actual operator and not the textual
1257 // representation of it.
1258 if (II.isCPlusPlusOperatorKeyword())
1259 Identifier.setIdentifierInfo(0);
1260
1261 // Change the kind of this identifier to the appropriate token kind, e.g.
1262 // turning "for" into a keyword.
1263 Identifier.setKind(II.getTokenID());
1264
1265 // If this is an extension token, diagnose its use.
1266 // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99
1267 // For now, I'm just commenting it out (while I work on attributes).
1268 if (II.isExtensionToken() && Features.C99)
1269 Diag(Identifier, diag::ext_token_used);
1270}
1271
1272/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
1273/// the current file. This either returns the EOF token or pops a level off
1274/// the include stack and keeps going.
Chris Lattnerd2177732007-07-20 16:59:19 +00001275bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001276 assert(!CurMacroExpander &&
1277 "Ending a file when currently in a macro!");
1278
1279 // See if this file had a controlling macro.
1280 if (CurLexer) { // Not ending a macro, ignore it.
1281 if (const IdentifierInfo *ControllingMacro =
1282 CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
1283 // Okay, this has a controlling macro, remember in PerFileInfo.
1284 if (const FileEntry *FE =
Chris Lattner9dc1f532007-07-20 16:37:10 +00001285 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001286 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
1287 }
1288 }
1289
1290 // If this is a #include'd file, pop it off the include stack and continue
1291 // lexing the #includer file.
1292 if (!IncludeMacroStack.empty()) {
1293 // We're done with the #included file.
1294 RemoveTopOfLexerStack();
1295
1296 // Notify the client, if desired, that we are in a new source file.
1297 if (Callbacks && !isEndOfMacro && CurLexer) {
1298 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
1299
1300 // Get the file entry for the current file.
1301 if (const FileEntry *FE =
Chris Lattner9dc1f532007-07-20 16:37:10 +00001302 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001303 FileType = HeaderInfo.getFileDirFlavor(FE);
1304
1305 Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr),
1306 PPCallbacks::ExitFile, FileType);
1307 }
1308
1309 // Client should lex another token.
1310 return false;
1311 }
Chris Lattner09cf90f2008-01-25 00:00:30 +00001312
1313 // If the file ends with a newline, form the EOF token on the newline itself,
1314 // rather than "on the line following it", which doesn't exist. This makes
1315 // diagnostics relating to the end of file include the last file that the user
1316 // actually typed, which is goodness.
1317 const char *EndPos = CurLexer->BufferEnd;
1318 if (EndPos != CurLexer->BufferStart &&
1319 (EndPos[-1] == '\n' || EndPos[-1] == '\r')) {
1320 --EndPos;
1321
1322 // Handle \n\r and \r\n:
1323 if (EndPos != CurLexer->BufferStart &&
1324 (EndPos[-1] == '\n' || EndPos[-1] == '\r') &&
1325 EndPos[-1] != EndPos[0])
1326 --EndPos;
1327 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001328
1329 Result.startToken();
Chris Lattner09cf90f2008-01-25 00:00:30 +00001330 CurLexer->BufferPtr = EndPos;
1331 CurLexer->FormTokenWithChars(Result, EndPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001332 Result.setKind(tok::eof);
1333
1334 // We're done with the #included file.
1335 delete CurLexer;
1336 CurLexer = 0;
1337
1338 // This is the end of the top-level file. If the diag::pp_macro_not_used
Chris Lattnercc1a8752007-10-07 08:44:20 +00001339 // diagnostic is enabled, look for macros that have not been used.
Reid Spencer5f016e22007-07-11 17:01:13 +00001340 if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){
Chris Lattnercc1a8752007-10-07 08:44:20 +00001341 for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
1342 Macros.begin(), E = Macros.end(); I != E; ++I) {
1343 if (!I->second->isUsed())
1344 Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used);
Reid Spencer5f016e22007-07-11 17:01:13 +00001345 }
1346 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001347 return true;
1348}
1349
1350/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
1351/// the current macro expansion or token stream expansion.
Chris Lattnerd2177732007-07-20 16:59:19 +00001352bool Preprocessor::HandleEndOfMacro(Token &Result) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001353 assert(CurMacroExpander && !CurLexer &&
1354 "Ending a macro when currently in a #include file!");
1355
Chris Lattner9594acf2007-07-15 00:25:26 +00001356 // Delete or cache the now-dead macro expander.
1357 if (NumCachedMacroExpanders == MacroExpanderCacheSize)
1358 delete CurMacroExpander;
1359 else
1360 MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander;
Reid Spencer5f016e22007-07-11 17:01:13 +00001361
1362 // Handle this like a #include file being popped off the stack.
1363 CurMacroExpander = 0;
1364 return HandleEndOfFile(Result, true);
1365}
1366
Chris Lattner3f1cc832008-02-07 06:03:59 +00001367/// HandleMicrosoftCommentPaste - When the macro expander pastes together a
1368/// comment (/##/) in microsoft mode, this method handles updating the current
1369/// state, returning the token on the next source line.
1370void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) {
1371 assert(CurMacroExpander && !CurLexer &&
1372 "Pasted comment can only be formed from macro");
1373
1374 // We handle this by scanning for the closest real lexer, switching it to
1375 // raw mode and preprocessor mode. This will cause it to return \n as an
1376 // explicit EOM token.
1377 Lexer *FoundLexer = 0;
1378 bool LexerWasInPPMode = false;
1379 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
1380 IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1);
1381 if (ISI.TheLexer == 0) continue; // Scan for a real lexer.
1382
1383 // Once we find a real lexer, mark it as raw mode (disabling macro
1384 // expansions) and preprocessor mode (return EOM). We know that the lexer
1385 // was *not* in raw mode before, because the macro that the comment came
1386 // from was expanded. However, it could have already been in preprocessor
1387 // mode (#if COMMENT) in which case we have to return it to that mode and
1388 // return EOM.
1389 FoundLexer = ISI.TheLexer;
1390 FoundLexer->LexingRawMode = true;
1391 LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective;
1392 FoundLexer->ParsingPreprocessorDirective = true;
1393 break;
1394 }
1395
1396 // Okay, we either found and switched over the lexer, or we didn't find a
1397 // lexer. In either case, finish off the macro the comment came from, getting
1398 // the next token.
1399 if (!HandleEndOfMacro(Tok)) Lex(Tok);
1400
1401 // Discarding comments as long as we don't have EOF or EOM. This 'comments
1402 // out' the rest of the line, including any tokens that came from other macros
1403 // that were active, as in:
1404 // #define submacro a COMMENT b
1405 // submacro c
1406 // which should lex to 'a' only: 'b' and 'c' should be removed.
1407 while (Tok.isNot(tok::eom) && Tok.isNot(tok::eof))
1408 Lex(Tok);
1409
1410 // If we got an eom token, then we successfully found the end of the line.
1411 if (Tok.is(tok::eom)) {
1412 assert(FoundLexer && "Can't get end of line without an active lexer");
1413 // Restore the lexer back to normal mode instead of raw mode.
1414 FoundLexer->LexingRawMode = false;
1415
1416 // If the lexer was already in preprocessor mode, just return the EOM token
1417 // to finish the preprocessor line.
1418 if (LexerWasInPPMode) return;
1419
1420 // Otherwise, switch out of PP mode and return the next lexed token.
1421 FoundLexer->ParsingPreprocessorDirective = false;
1422 return Lex(Tok);
1423 }
1424
1425 // If we got an EOF token, then we reached the end of the token stream but
1426 // didn't find an explicit \n. This can only happen if there was no lexer
1427 // active (an active lexer would return EOM at EOF if there was no \n in
1428 // preprocessor directive mode), so just return EOF as our token.
1429 assert(!FoundLexer && "Lexer should return EOM before EOF in PP mode");
1430 return;
1431}
Reid Spencer5f016e22007-07-11 17:01:13 +00001432
1433//===----------------------------------------------------------------------===//
1434// Utility Methods for Preprocessor Directive Handling.
1435//===----------------------------------------------------------------------===//
1436
1437/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
1438/// current line until the tok::eom token is found.
1439void Preprocessor::DiscardUntilEndOfDirective() {
Chris Lattnerd2177732007-07-20 16:59:19 +00001440 Token Tmp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001441 do {
1442 LexUnexpandedToken(Tmp);
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001443 } while (Tmp.isNot(tok::eom));
Reid Spencer5f016e22007-07-11 17:01:13 +00001444}
1445
1446/// isCXXNamedOperator - Returns "true" if the token is a named operator in C++.
1447static bool isCXXNamedOperator(const std::string &Spelling) {
1448 return Spelling == "and" || Spelling == "bitand" || Spelling == "bitor" ||
1449 Spelling == "compl" || Spelling == "not" || Spelling == "not_eq" ||
1450 Spelling == "or" || Spelling == "xor";
1451}
1452
1453/// ReadMacroName - Lex and validate a macro name, which occurs after a
1454/// #define or #undef. This sets the token kind to eom and discards the rest
1455/// of the macro line if the macro name is invalid. isDefineUndef is 1 if
1456/// this is due to a a #define, 2 if #undef directive, 0 if it is something
1457/// else (e.g. #ifdef).
Chris Lattnerd2177732007-07-20 16:59:19 +00001458void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001459 // Read the token, don't allow macro expansion on it.
1460 LexUnexpandedToken(MacroNameTok);
1461
1462 // Missing macro name?
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001463 if (MacroNameTok.is(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +00001464 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
1465
1466 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1467 if (II == 0) {
1468 std::string Spelling = getSpelling(MacroNameTok);
1469 if (isCXXNamedOperator(Spelling))
1470 // C++ 2.5p2: Alternative tokens behave the same as its primary token
1471 // except for their spellings.
1472 Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name, Spelling);
1473 else
1474 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
1475 // Fall through on error.
1476 } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
1477 // Error if defining "defined": C99 6.10.8.4.
1478 Diag(MacroNameTok, diag::err_defined_macro_name);
Chris Lattner0edde552007-10-07 08:04:56 +00001479 } else if (isDefineUndef && II->hasMacroDefinition() &&
Chris Lattnercc1a8752007-10-07 08:44:20 +00001480 getMacroInfo(II)->isBuiltinMacro()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001481 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
1482 if (isDefineUndef == 1)
1483 Diag(MacroNameTok, diag::pp_redef_builtin_macro);
1484 else
1485 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
1486 } else {
1487 // Okay, we got a good identifier node. Return it.
1488 return;
1489 }
1490
1491 // Invalid macro name, read and discard the rest of the line. Then set the
1492 // token kind to tok::eom.
1493 MacroNameTok.setKind(tok::eom);
1494 return DiscardUntilEndOfDirective();
1495}
1496
1497/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
1498/// not, emit a diagnostic and consume up until the eom.
1499void Preprocessor::CheckEndOfDirective(const char *DirType) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001500 Token Tmp;
Chris Lattnere9ba3232008-02-16 01:20:36 +00001501 // Lex unexpanded tokens: macros might expand to zero tokens, causing us to
1502 // miss diagnosing invalid lines.
1503 LexUnexpandedToken(Tmp);
1504
Reid Spencer5f016e22007-07-11 17:01:13 +00001505 // There should be no tokens after the directive, but we allow them as an
1506 // extension.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001507 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
Chris Lattnere9ba3232008-02-16 01:20:36 +00001508 LexUnexpandedToken(Tmp);
Reid Spencer5f016e22007-07-11 17:01:13 +00001509
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001510 if (Tmp.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001511 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
1512 DiscardUntilEndOfDirective();
1513 }
1514}
1515
1516
1517
1518/// SkipExcludedConditionalBlock - We just read a #if or related directive and
1519/// decided that the subsequent tokens are in the #if'd out portion of the
1520/// file. Lex the rest of the file, until we see an #endif. If
1521/// FoundNonSkipPortion is true, then we have already emitted code for part of
1522/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
1523/// is true, then #else directives are ok, if not, then we have already seen one
1524/// so a #else directive is a duplicate. When this returns, the caller can lex
1525/// the first valid token.
1526void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
1527 bool FoundNonSkipPortion,
1528 bool FoundElse) {
1529 ++NumSkipped;
1530 assert(CurMacroExpander == 0 && CurLexer &&
1531 "Lexing a macro, not a file?");
1532
1533 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
1534 FoundNonSkipPortion, FoundElse);
1535
1536 // Enter raw mode to disable identifier lookup (and thus macro expansion),
1537 // disabling warnings, etc.
1538 CurLexer->LexingRawMode = true;
Chris Lattnerd2177732007-07-20 16:59:19 +00001539 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001540 while (1) {
1541 CurLexer->Lex(Tok);
1542
1543 // If this is the end of the buffer, we have an error.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001544 if (Tok.is(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001545 // Emit errors for each unterminated conditional on the stack, including
1546 // the current one.
1547 while (!CurLexer->ConditionalStack.empty()) {
1548 Diag(CurLexer->ConditionalStack.back().IfLoc,
1549 diag::err_pp_unterminated_conditional);
1550 CurLexer->ConditionalStack.pop_back();
1551 }
1552
1553 // Just return and let the caller lex after this #include.
1554 break;
1555 }
1556
1557 // If this token is not a preprocessor directive, just skip it.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001558 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
Reid Spencer5f016e22007-07-11 17:01:13 +00001559 continue;
1560
1561 // We just parsed a # character at the start of a line, so we're in
1562 // directive mode. Tell the lexer this so any newlines we see will be
1563 // converted into an EOM token (this terminates the macro).
1564 CurLexer->ParsingPreprocessorDirective = true;
1565 CurLexer->KeepCommentMode = false;
1566
1567
1568 // Read the next token, the directive flavor.
1569 LexUnexpandedToken(Tok);
1570
1571 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
1572 // something bogus), skip it.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001573 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001574 CurLexer->ParsingPreprocessorDirective = false;
1575 // Restore comment saving mode.
1576 CurLexer->KeepCommentMode = KeepComments;
1577 continue;
1578 }
1579
1580 // If the first letter isn't i or e, it isn't intesting to us. We know that
1581 // this is safe in the face of spelling differences, because there is no way
1582 // to spell an i/e in a strange way that is another letter. Skipping this
1583 // allows us to avoid looking up the identifier info for #define/#undef and
1584 // other common directives.
1585 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
1586 char FirstChar = RawCharData[0];
1587 if (FirstChar >= 'a' && FirstChar <= 'z' &&
1588 FirstChar != 'i' && FirstChar != 'e') {
1589 CurLexer->ParsingPreprocessorDirective = false;
1590 // Restore comment saving mode.
1591 CurLexer->KeepCommentMode = KeepComments;
1592 continue;
1593 }
1594
1595 // Get the identifier name without trigraphs or embedded newlines. Note
1596 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
1597 // when skipping.
1598 // TODO: could do this with zero copies in the no-clean case by using
1599 // strncmp below.
1600 char Directive[20];
1601 unsigned IdLen;
1602 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
1603 IdLen = Tok.getLength();
1604 memcpy(Directive, RawCharData, IdLen);
1605 Directive[IdLen] = 0;
1606 } else {
1607 std::string DirectiveStr = getSpelling(Tok);
1608 IdLen = DirectiveStr.size();
1609 if (IdLen >= 20) {
1610 CurLexer->ParsingPreprocessorDirective = false;
1611 // Restore comment saving mode.
1612 CurLexer->KeepCommentMode = KeepComments;
1613 continue;
1614 }
1615 memcpy(Directive, &DirectiveStr[0], IdLen);
1616 Directive[IdLen] = 0;
1617 }
1618
1619 if (FirstChar == 'i' && Directive[1] == 'f') {
1620 if ((IdLen == 2) || // "if"
1621 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
1622 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
1623 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
1624 // bother parsing the condition.
1625 DiscardUntilEndOfDirective();
1626 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
1627 /*foundnonskip*/false,
1628 /*fnddelse*/false);
1629 }
1630 } else if (FirstChar == 'e') {
1631 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
1632 CheckEndOfDirective("#endif");
1633 PPConditionalInfo CondInfo;
1634 CondInfo.WasSkipping = true; // Silence bogus warning.
1635 bool InCond = CurLexer->popConditionalLevel(CondInfo);
1636 InCond = InCond; // Silence warning in no-asserts mode.
1637 assert(!InCond && "Can't be skipping if not in a conditional!");
1638
1639 // If we popped the outermost skipping block, we're done skipping!
1640 if (!CondInfo.WasSkipping)
1641 break;
1642 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
1643 // #else directive in a skipping conditional. If not in some other
1644 // skipping conditional, and if #else hasn't already been seen, enter it
1645 // as a non-skipping conditional.
1646 CheckEndOfDirective("#else");
1647 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1648
1649 // If this is a #else with a #else before it, report the error.
1650 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
1651
1652 // Note that we've seen a #else in this conditional.
1653 CondInfo.FoundElse = true;
1654
1655 // If the conditional is at the top level, and the #if block wasn't
1656 // entered, enter the #else block now.
1657 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
1658 CondInfo.FoundNonSkip = true;
1659 break;
1660 }
1661 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
1662 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1663
1664 bool ShouldEnter;
1665 // If this is in a skipping block or if we're already handled this #if
1666 // block, don't bother parsing the condition.
1667 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
1668 DiscardUntilEndOfDirective();
1669 ShouldEnter = false;
1670 } else {
1671 // Restore the value of LexingRawMode so that identifiers are
1672 // looked up, etc, inside the #elif expression.
1673 assert(CurLexer->LexingRawMode && "We have to be skipping here!");
1674 CurLexer->LexingRawMode = false;
1675 IdentifierInfo *IfNDefMacro = 0;
1676 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
1677 CurLexer->LexingRawMode = true;
1678 }
1679
1680 // If this is a #elif with a #else before it, report the error.
1681 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
1682
1683 // If this condition is true, enter it!
1684 if (ShouldEnter) {
1685 CondInfo.FoundNonSkip = true;
1686 break;
1687 }
1688 }
1689 }
1690
1691 CurLexer->ParsingPreprocessorDirective = false;
1692 // Restore comment saving mode.
1693 CurLexer->KeepCommentMode = KeepComments;
1694 }
1695
1696 // Finally, if we are out of the conditional (saw an #endif or ran off the end
1697 // of the file, just stop skipping and return to lexing whatever came after
1698 // the #if block.
1699 CurLexer->LexingRawMode = false;
1700}
1701
1702//===----------------------------------------------------------------------===//
1703// Preprocessor Directive Handling.
1704//===----------------------------------------------------------------------===//
1705
1706/// HandleDirective - This callback is invoked when the lexer sees a # token
1707/// at the start of a line. This consumes the directive, modifies the
1708/// lexer/preprocessor state, and advances the lexer(s) so that the next token
1709/// read is the correct one.
Chris Lattnerd2177732007-07-20 16:59:19 +00001710void Preprocessor::HandleDirective(Token &Result) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001711 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
1712
1713 // We just parsed a # character at the start of a line, so we're in directive
1714 // mode. Tell the lexer this so any newlines we see will be converted into an
1715 // EOM token (which terminates the directive).
1716 CurLexer->ParsingPreprocessorDirective = true;
1717
1718 ++NumDirectives;
1719
1720 // We are about to read a token. For the multiple-include optimization FA to
1721 // work, we have to remember if we had read any tokens *before* this
1722 // pp-directive.
1723 bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal();
1724
1725 // Read the next token, the directive flavor. This isn't expanded due to
1726 // C99 6.10.3p8.
1727 LexUnexpandedToken(Result);
1728
1729 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
1730 // #define A(x) #x
1731 // A(abc
1732 // #warning blah
1733 // def)
1734 // If so, the user is relying on non-portable behavior, emit a diagnostic.
1735 if (InMacroArgs)
1736 Diag(Result, diag::ext_embedded_directive);
1737
1738TryAgain:
1739 switch (Result.getKind()) {
1740 case tok::eom:
1741 return; // null directive.
1742 case tok::comment:
1743 // Handle stuff like "# /*foo*/ define X" in -E -C mode.
1744 LexUnexpandedToken(Result);
1745 goto TryAgain;
1746
1747 case tok::numeric_constant:
1748 // FIXME: implement # 7 line numbers!
1749 DiscardUntilEndOfDirective();
1750 return;
1751 default:
1752 IdentifierInfo *II = Result.getIdentifierInfo();
1753 if (II == 0) break; // Not an identifier.
1754
1755 // Ask what the preprocessor keyword ID is.
1756 switch (II->getPPKeywordID()) {
1757 default: break;
1758 // C99 6.10.1 - Conditional Inclusion.
1759 case tok::pp_if:
1760 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
1761 case tok::pp_ifdef:
1762 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
1763 case tok::pp_ifndef:
1764 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
1765 case tok::pp_elif:
1766 return HandleElifDirective(Result);
1767 case tok::pp_else:
1768 return HandleElseDirective(Result);
1769 case tok::pp_endif:
1770 return HandleEndifDirective(Result);
1771
1772 // C99 6.10.2 - Source File Inclusion.
1773 case tok::pp_include:
1774 return HandleIncludeDirective(Result); // Handle #include.
1775
1776 // C99 6.10.3 - Macro Replacement.
1777 case tok::pp_define:
1778 return HandleDefineDirective(Result, false);
1779 case tok::pp_undef:
1780 return HandleUndefDirective(Result);
1781
1782 // C99 6.10.4 - Line Control.
1783 case tok::pp_line:
1784 // FIXME: implement #line
1785 DiscardUntilEndOfDirective();
1786 return;
1787
1788 // C99 6.10.5 - Error Directive.
1789 case tok::pp_error:
1790 return HandleUserDiagnosticDirective(Result, false);
1791
1792 // C99 6.10.6 - Pragma Directive.
1793 case tok::pp_pragma:
1794 return HandlePragmaDirective();
1795
1796 // GNU Extensions.
1797 case tok::pp_import:
1798 return HandleImportDirective(Result);
1799 case tok::pp_include_next:
1800 return HandleIncludeNextDirective(Result);
1801
1802 case tok::pp_warning:
1803 Diag(Result, diag::ext_pp_warning_directive);
1804 return HandleUserDiagnosticDirective(Result, true);
1805 case tok::pp_ident:
1806 return HandleIdentSCCSDirective(Result);
1807 case tok::pp_sccs:
1808 return HandleIdentSCCSDirective(Result);
1809 case tok::pp_assert:
1810 //isExtension = true; // FIXME: implement #assert
1811 break;
1812 case tok::pp_unassert:
1813 //isExtension = true; // FIXME: implement #unassert
1814 break;
1815
1816 // clang extensions.
1817 case tok::pp_define_target:
1818 return HandleDefineDirective(Result, true);
1819 case tok::pp_define_other_target:
1820 return HandleDefineOtherTargetDirective(Result);
1821 }
1822 break;
1823 }
1824
1825 // If we reached here, the preprocessing token is not valid!
1826 Diag(Result, diag::err_pp_invalid_directive);
1827
1828 // Read the rest of the PP line.
1829 DiscardUntilEndOfDirective();
1830
1831 // Okay, we're done parsing the directive.
1832}
1833
Chris Lattnerd2177732007-07-20 16:59:19 +00001834void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
Reid Spencer5f016e22007-07-11 17:01:13 +00001835 bool isWarning) {
1836 // Read the rest of the line raw. We do this because we don't want macros
1837 // to be expanded and we don't require that the tokens be valid preprocessing
1838 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1839 // collapse multiple consequtive white space between tokens, but this isn't
1840 // specified by the standard.
1841 std::string Message = CurLexer->ReadToEndOfLine();
1842
1843 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
1844 return Diag(Tok, DiagID, Message);
1845}
1846
1847/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1848///
Chris Lattnerd2177732007-07-20 16:59:19 +00001849void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001850 // Yes, this directive is an extension.
1851 Diag(Tok, diag::ext_pp_ident_directive);
1852
1853 // Read the string argument.
Chris Lattnerd2177732007-07-20 16:59:19 +00001854 Token StrTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001855 Lex(StrTok);
1856
1857 // If the token kind isn't a string, it's a malformed directive.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001858 if (StrTok.isNot(tok::string_literal) &&
1859 StrTok.isNot(tok::wide_string_literal))
Reid Spencer5f016e22007-07-11 17:01:13 +00001860 return Diag(StrTok, diag::err_pp_malformed_ident);
1861
1862 // Verify that there is nothing after the string, other than EOM.
1863 CheckEndOfDirective("#ident");
1864
1865 if (Callbacks)
1866 Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok));
1867}
1868
1869//===----------------------------------------------------------------------===//
1870// Preprocessor Include Directive Handling.
1871//===----------------------------------------------------------------------===//
1872
1873/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1874/// checked and spelled filename, e.g. as an operand of #include. This returns
1875/// true if the input filename was in <>'s or false if it were in ""'s. The
1876/// caller is expected to provide a buffer that is large enough to hold the
1877/// spelling of the filename, but is also expected to handle the case when
1878/// this method decides to use a different buffer.
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001879bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +00001880 const char *&BufStart,
1881 const char *&BufEnd) {
1882 // Get the text form of the filename.
Reid Spencer5f016e22007-07-11 17:01:13 +00001883 assert(BufStart != BufEnd && "Can't have tokens with empty spellings!");
1884
1885 // Make sure the filename is <x> or "x".
1886 bool isAngled;
1887 if (BufStart[0] == '<') {
1888 if (BufEnd[-1] != '>') {
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001889 Diag(Loc, diag::err_pp_expects_filename);
Reid Spencer5f016e22007-07-11 17:01:13 +00001890 BufStart = 0;
1891 return true;
1892 }
1893 isAngled = true;
1894 } else if (BufStart[0] == '"') {
1895 if (BufEnd[-1] != '"') {
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001896 Diag(Loc, diag::err_pp_expects_filename);
Reid Spencer5f016e22007-07-11 17:01:13 +00001897 BufStart = 0;
1898 return true;
1899 }
1900 isAngled = false;
1901 } else {
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001902 Diag(Loc, diag::err_pp_expects_filename);
Reid Spencer5f016e22007-07-11 17:01:13 +00001903 BufStart = 0;
1904 return true;
1905 }
1906
1907 // Diagnose #include "" as invalid.
1908 if (BufEnd-BufStart <= 2) {
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001909 Diag(Loc, diag::err_pp_empty_filename);
Reid Spencer5f016e22007-07-11 17:01:13 +00001910 BufStart = 0;
1911 return "";
1912 }
1913
1914 // Skip the brackets.
1915 ++BufStart;
1916 --BufEnd;
1917 return isAngled;
1918}
1919
Chris Lattner706ab502007-07-23 04:56:47 +00001920/// ConcatenateIncludeName - Handle cases where the #include name is expanded
1921/// from a macro as multiple tokens, which need to be glued together. This
1922/// occurs for code like:
1923/// #define FOO <a/b.h>
1924/// #include FOO
1925/// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1926///
1927/// This code concatenates and consumes tokens up to the '>' token. It returns
1928/// false if the > was found, otherwise it returns true if it finds and consumes
1929/// the EOM marker.
1930static bool ConcatenateIncludeName(llvm::SmallVector<char, 128> &FilenameBuffer,
1931 Preprocessor &PP) {
1932 Token CurTok;
1933
1934 PP.Lex(CurTok);
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001935 while (CurTok.isNot(tok::eom)) {
Chris Lattner706ab502007-07-23 04:56:47 +00001936 // Append the spelling of this token to the buffer. If there was a space
1937 // before it, add it now.
1938 if (CurTok.hasLeadingSpace())
1939 FilenameBuffer.push_back(' ');
1940
1941 // Get the spelling of the token, directly into FilenameBuffer if possible.
1942 unsigned PreAppendSize = FilenameBuffer.size();
1943 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1944
1945 const char *BufPtr = &FilenameBuffer[PreAppendSize];
1946 unsigned ActualLen = PP.getSpelling(CurTok, BufPtr);
1947
1948 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1949 if (BufPtr != &FilenameBuffer[PreAppendSize])
1950 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1951
1952 // Resize FilenameBuffer to the correct size.
1953 if (CurTok.getLength() != ActualLen)
1954 FilenameBuffer.resize(PreAppendSize+ActualLen);
1955
1956 // If we found the '>' marker, return success.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001957 if (CurTok.is(tok::greater))
Chris Lattner706ab502007-07-23 04:56:47 +00001958 return false;
1959
1960 PP.Lex(CurTok);
1961 }
1962
1963 // If we hit the eom marker, emit an error and return true so that the caller
1964 // knows the EOM has been read.
1965 PP.Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1966 return true;
1967}
1968
Reid Spencer5f016e22007-07-11 17:01:13 +00001969/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1970/// file to be included from the lexer, then include it! This is a common
1971/// routine with functionality shared between #include, #include_next and
1972/// #import.
Chris Lattnerd2177732007-07-20 16:59:19 +00001973void Preprocessor::HandleIncludeDirective(Token &IncludeTok,
Reid Spencer5f016e22007-07-11 17:01:13 +00001974 const DirectoryLookup *LookupFrom,
1975 bool isImport) {
1976
Chris Lattnerd2177732007-07-20 16:59:19 +00001977 Token FilenameTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001978 CurLexer->LexIncludeFilename(FilenameTok);
1979
Reid Spencer5f016e22007-07-11 17:01:13 +00001980 // Reserve a buffer to get the spelling.
1981 llvm::SmallVector<char, 128> FilenameBuffer;
Chris Lattner706ab502007-07-23 04:56:47 +00001982 const char *FilenameStart, *FilenameEnd;
1983
1984 switch (FilenameTok.getKind()) {
1985 case tok::eom:
1986 // If the token kind is EOM, the error has already been diagnosed.
1987 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001988
Chris Lattner706ab502007-07-23 04:56:47 +00001989 case tok::angle_string_literal:
Chris Lattnerf11ccfc2007-07-23 22:23:52 +00001990 case tok::string_literal: {
Chris Lattner706ab502007-07-23 04:56:47 +00001991 FilenameBuffer.resize(FilenameTok.getLength());
1992 FilenameStart = &FilenameBuffer[0];
1993 unsigned Len = getSpelling(FilenameTok, FilenameStart);
1994 FilenameEnd = FilenameStart+Len;
1995 break;
Chris Lattnerf11ccfc2007-07-23 22:23:52 +00001996 }
Chris Lattner706ab502007-07-23 04:56:47 +00001997
1998 case tok::less:
1999 // This could be a <foo/bar.h> file coming from a macro expansion. In this
2000 // case, glue the tokens together into FilenameBuffer and interpret those.
2001 FilenameBuffer.push_back('<');
2002 if (ConcatenateIncludeName(FilenameBuffer, *this))
2003 return; // Found <eom> but no ">"? Diagnostic already emitted.
2004 FilenameStart = &FilenameBuffer[0];
2005 FilenameEnd = &FilenameBuffer[FilenameBuffer.size()];
2006 break;
2007 default:
2008 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
2009 DiscardUntilEndOfDirective();
2010 return;
2011 }
2012
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00002013 bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +00002014 FilenameStart, FilenameEnd);
2015 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
2016 // error.
Chris Lattner706ab502007-07-23 04:56:47 +00002017 if (FilenameStart == 0) {
2018 DiscardUntilEndOfDirective();
Reid Spencer5f016e22007-07-11 17:01:13 +00002019 return;
Chris Lattner706ab502007-07-23 04:56:47 +00002020 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002021
2022 // Verify that there is nothing after the filename, other than EOM. Use the
2023 // preprocessor to lex this in case lexing the filename entered a macro.
2024 CheckEndOfDirective("#include");
2025
2026 // Check that we don't have infinite #include recursion.
2027 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
2028 return Diag(FilenameTok, diag::err_pp_include_too_deep);
2029
2030 // Search include directories.
2031 const DirectoryLookup *CurDir;
2032 const FileEntry *File = LookupFile(FilenameStart, FilenameEnd,
2033 isAngled, LookupFrom, CurDir);
2034 if (File == 0)
2035 return Diag(FilenameTok, diag::err_pp_file_not_found,
2036 std::string(FilenameStart, FilenameEnd));
2037
2038 // Ask HeaderInfo if we should enter this #include file.
2039 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
2040 // If it returns true, #including this file will have no effect.
2041 return;
2042 }
2043
2044 // Look up the file, create a File ID for it.
2045 unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation());
2046 if (FileID == 0)
2047 return Diag(FilenameTok, diag::err_pp_file_not_found,
2048 std::string(FilenameStart, FilenameEnd));
2049
2050 // Finally, if all is good, enter the new file!
2051 EnterSourceFile(FileID, CurDir);
2052}
2053
2054/// HandleIncludeNextDirective - Implements #include_next.
2055///
Chris Lattnerd2177732007-07-20 16:59:19 +00002056void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002057 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
2058
2059 // #include_next is like #include, except that we start searching after
2060 // the current found directory. If we can't do this, issue a
2061 // diagnostic.
2062 const DirectoryLookup *Lookup = CurDirLookup;
2063 if (isInPrimaryFile()) {
2064 Lookup = 0;
2065 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
2066 } else if (Lookup == 0) {
2067 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
2068 } else {
2069 // Start looking up in the next directory.
2070 ++Lookup;
2071 }
2072
2073 return HandleIncludeDirective(IncludeNextTok, Lookup);
2074}
2075
2076/// HandleImportDirective - Implements #import.
2077///
Chris Lattnerd2177732007-07-20 16:59:19 +00002078void Preprocessor::HandleImportDirective(Token &ImportTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002079 Diag(ImportTok, diag::ext_pp_import_directive);
2080
2081 return HandleIncludeDirective(ImportTok, 0, true);
2082}
2083
2084//===----------------------------------------------------------------------===//
2085// Preprocessor Macro Directive Handling.
2086//===----------------------------------------------------------------------===//
2087
2088/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
2089/// definition has just been read. Lex the rest of the arguments and the
2090/// closing ), updating MI with what we learn. Return true if an error occurs
2091/// parsing the arg list.
2092bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
Chris Lattner25c96482007-07-14 22:46:43 +00002093 llvm::SmallVector<IdentifierInfo*, 32> Arguments;
2094
Chris Lattnerd2177732007-07-20 16:59:19 +00002095 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00002096 while (1) {
2097 LexUnexpandedToken(Tok);
2098 switch (Tok.getKind()) {
2099 case tok::r_paren:
2100 // Found the end of the argument list.
Chris Lattner25c96482007-07-14 22:46:43 +00002101 if (Arguments.empty()) { // #define FOO()
2102 MI->setArgumentList(Arguments.begin(), Arguments.end());
2103 return false;
2104 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002105 // Otherwise we have #define FOO(A,)
2106 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
2107 return true;
2108 case tok::ellipsis: // #define X(... -> C99 varargs
2109 // Warn if use of C99 feature in non-C99 mode.
2110 if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
2111
2112 // Lex the token after the identifier.
2113 LexUnexpandedToken(Tok);
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002114 if (Tok.isNot(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002115 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2116 return true;
2117 }
2118 // Add the __VA_ARGS__ identifier as an argument.
Chris Lattner25c96482007-07-14 22:46:43 +00002119 Arguments.push_back(Ident__VA_ARGS__);
Reid Spencer5f016e22007-07-11 17:01:13 +00002120 MI->setIsC99Varargs();
Chris Lattner25c96482007-07-14 22:46:43 +00002121 MI->setArgumentList(Arguments.begin(), Arguments.end());
Reid Spencer5f016e22007-07-11 17:01:13 +00002122 return false;
2123 case tok::eom: // #define X(
2124 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2125 return true;
2126 default:
2127 // Handle keywords and identifiers here to accept things like
2128 // #define Foo(for) for.
2129 IdentifierInfo *II = Tok.getIdentifierInfo();
2130 if (II == 0) {
2131 // #define X(1
2132 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
2133 return true;
2134 }
2135
2136 // If this is already used as an argument, it is used multiple times (e.g.
2137 // #define X(A,A.
Chris Lattner25c96482007-07-14 22:46:43 +00002138 if (std::find(Arguments.begin(), Arguments.end(), II) !=
2139 Arguments.end()) { // C99 6.10.3p6
Reid Spencer5f016e22007-07-11 17:01:13 +00002140 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName());
2141 return true;
2142 }
2143
2144 // Add the argument to the macro info.
Chris Lattner25c96482007-07-14 22:46:43 +00002145 Arguments.push_back(II);
Reid Spencer5f016e22007-07-11 17:01:13 +00002146
2147 // Lex the token after the identifier.
2148 LexUnexpandedToken(Tok);
2149
2150 switch (Tok.getKind()) {
2151 default: // #define X(A B
2152 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
2153 return true;
2154 case tok::r_paren: // #define X(A)
Chris Lattner25c96482007-07-14 22:46:43 +00002155 MI->setArgumentList(Arguments.begin(), Arguments.end());
Reid Spencer5f016e22007-07-11 17:01:13 +00002156 return false;
2157 case tok::comma: // #define X(A,
2158 break;
2159 case tok::ellipsis: // #define X(A... -> GCC extension
2160 // Diagnose extension.
2161 Diag(Tok, diag::ext_named_variadic_macro);
2162
2163 // Lex the token after the identifier.
2164 LexUnexpandedToken(Tok);
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002165 if (Tok.isNot(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002166 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2167 return true;
2168 }
2169
2170 MI->setIsGNUVarargs();
Chris Lattner25c96482007-07-14 22:46:43 +00002171 MI->setArgumentList(Arguments.begin(), Arguments.end());
Reid Spencer5f016e22007-07-11 17:01:13 +00002172 return false;
2173 }
2174 }
2175 }
2176}
2177
2178/// HandleDefineDirective - Implements #define. This consumes the entire macro
2179/// line then lets the caller lex the next real token. If 'isTargetSpecific' is
2180/// true, then this is a "#define_target", otherwise this is a "#define".
2181///
Chris Lattnerd2177732007-07-20 16:59:19 +00002182void Preprocessor::HandleDefineDirective(Token &DefineTok,
Reid Spencer5f016e22007-07-11 17:01:13 +00002183 bool isTargetSpecific) {
2184 ++NumDefined;
2185
Chris Lattnerd2177732007-07-20 16:59:19 +00002186 Token MacroNameTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00002187 ReadMacroName(MacroNameTok, 1);
2188
2189 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002190 if (MacroNameTok.is(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +00002191 return;
Chris Lattnerc215bd62007-07-14 22:11:41 +00002192
Reid Spencer5f016e22007-07-11 17:01:13 +00002193 // If we are supposed to keep comments in #defines, reenable comment saving
2194 // mode.
2195 CurLexer->KeepCommentMode = KeepMacroComments;
2196
2197 // Create the new macro.
2198 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
2199 if (isTargetSpecific) MI->setIsTargetSpecific();
2200
2201 // If the identifier is an 'other target' macro, clear this bit.
2202 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
2203
2204
Chris Lattnerd2177732007-07-20 16:59:19 +00002205 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00002206 LexUnexpandedToken(Tok);
2207
2208 // If this is a function-like macro definition, parse the argument list,
2209 // marking each of the identifiers as being used as macro arguments. Also,
2210 // check other constraints on the first token of the macro body.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002211 if (Tok.is(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002212 // If there is no body to this macro, we have no special handling here.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002213 } else if (Tok.is(tok::l_paren) && !Tok.hasLeadingSpace()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002214 // This is a function-like macro definition. Read the argument list.
2215 MI->setIsFunctionLike();
2216 if (ReadMacroDefinitionArgList(MI)) {
2217 // Forget about MI.
2218 delete MI;
2219 // Throw away the rest of the line.
2220 if (CurLexer->ParsingPreprocessorDirective)
2221 DiscardUntilEndOfDirective();
2222 return;
2223 }
2224
2225 // Read the first token after the arg list for down below.
2226 LexUnexpandedToken(Tok);
2227 } else if (!Tok.hasLeadingSpace()) {
2228 // C99 requires whitespace between the macro definition and the body. Emit
2229 // a diagnostic for something like "#define X+".
2230 if (Features.C99) {
2231 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
2232 } else {
2233 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
2234 // one in some cases!
2235 }
2236 } else {
2237 // This is a normal token with leading space. Clear the leading space
2238 // marker on the first token to get proper expansion.
Chris Lattnerd2177732007-07-20 16:59:19 +00002239 Tok.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00002240 }
2241
2242 // If this is a definition of a variadic C99 function-like macro, not using
2243 // the GNU named varargs extension, enabled __VA_ARGS__.
2244
2245 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
2246 // This gets unpoisoned where it is allowed.
2247 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
2248 if (MI->isC99Varargs())
2249 Ident__VA_ARGS__->setIsPoisoned(false);
2250
2251 // Read the rest of the macro body.
Chris Lattnerb5e240f2007-07-14 21:54:03 +00002252 if (MI->isObjectLike()) {
2253 // Object-like macros are very simple, just read their body.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002254 while (Tok.isNot(tok::eom)) {
Chris Lattnerb5e240f2007-07-14 21:54:03 +00002255 MI->AddTokenToBody(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +00002256 // Get the next token of the macro.
2257 LexUnexpandedToken(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +00002258 }
2259
Chris Lattnerb5e240f2007-07-14 21:54:03 +00002260 } else {
2261 // Otherwise, read the body of a function-like macro. This has to validate
2262 // the # (stringize) operator.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002263 while (Tok.isNot(tok::eom)) {
Chris Lattnerb5e240f2007-07-14 21:54:03 +00002264 MI->AddTokenToBody(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +00002265
Chris Lattnerb5e240f2007-07-14 21:54:03 +00002266 // Check C99 6.10.3.2p1: ensure that # operators are followed by macro
2267 // parameters in function-like macro expansions.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002268 if (Tok.isNot(tok::hash)) {
Chris Lattnerb5e240f2007-07-14 21:54:03 +00002269 // Get the next token of the macro.
2270 LexUnexpandedToken(Tok);
2271 continue;
2272 }
2273
2274 // Get the next token of the macro.
2275 LexUnexpandedToken(Tok);
2276
2277 // Not a macro arg identifier?
2278 if (!Tok.getIdentifierInfo() ||
2279 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
2280 Diag(Tok, diag::err_pp_stringize_not_parameter);
2281 delete MI;
2282
2283 // Disable __VA_ARGS__ again.
2284 Ident__VA_ARGS__->setIsPoisoned(true);
2285 return;
2286 }
2287
2288 // Things look ok, add the param name token to the macro.
2289 MI->AddTokenToBody(Tok);
2290
2291 // Get the next token of the macro.
2292 LexUnexpandedToken(Tok);
2293 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002294 }
2295
Chris Lattnerc215bd62007-07-14 22:11:41 +00002296
Reid Spencer5f016e22007-07-11 17:01:13 +00002297 // Disable __VA_ARGS__ again.
2298 Ident__VA_ARGS__->setIsPoisoned(true);
2299
2300 // Check that there is no paste (##) operator at the begining or end of the
2301 // replacement list.
2302 unsigned NumTokens = MI->getNumTokens();
2303 if (NumTokens != 0) {
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002304 if (MI->getReplacementToken(0).is(tok::hashhash)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002305 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
2306 delete MI;
2307 return;
2308 }
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002309 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002310 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
2311 delete MI;
2312 return;
2313 }
2314 }
2315
2316 // If this is the primary source file, remember that this macro hasn't been
2317 // used yet.
2318 if (isInPrimaryFile())
2319 MI->setIsUsed(false);
2320
2321 // Finally, if this identifier already had a macro defined for it, verify that
2322 // the macro bodies are identical and free the old definition.
Chris Lattnercc1a8752007-10-07 08:44:20 +00002323 if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002324 if (!OtherMI->isUsed())
2325 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2326
2327 // Macros must be identical. This means all tokes and whitespace separation
2328 // must be the same. C99 6.10.3.2.
2329 if (!MI->isIdenticalTo(*OtherMI, *this)) {
2330 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef,
2331 MacroNameTok.getIdentifierInfo()->getName());
2332 Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2);
2333 }
2334 delete OtherMI;
2335 }
2336
Chris Lattnercc1a8752007-10-07 08:44:20 +00002337 setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
Reid Spencer5f016e22007-07-11 17:01:13 +00002338}
2339
2340/// HandleDefineOtherTargetDirective - Implements #define_other_target.
Chris Lattnerd2177732007-07-20 16:59:19 +00002341void Preprocessor::HandleDefineOtherTargetDirective(Token &Tok) {
2342 Token MacroNameTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00002343 ReadMacroName(MacroNameTok, 1);
2344
2345 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002346 if (MacroNameTok.is(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +00002347 return;
2348
2349 // Check to see if this is the last token on the #undef line.
2350 CheckEndOfDirective("#define_other_target");
2351
2352 // If there is already a macro defined by this name, turn it into a
2353 // target-specific define.
Chris Lattnercc1a8752007-10-07 08:44:20 +00002354 if (MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002355 MI->setIsTargetSpecific(true);
2356 return;
2357 }
2358
2359 // Mark the identifier as being a macro on some other target.
2360 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro();
2361}
2362
2363
2364/// HandleUndefDirective - Implements #undef.
2365///
Chris Lattnerd2177732007-07-20 16:59:19 +00002366void Preprocessor::HandleUndefDirective(Token &UndefTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002367 ++NumUndefined;
2368
Chris Lattnerd2177732007-07-20 16:59:19 +00002369 Token MacroNameTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00002370 ReadMacroName(MacroNameTok, 2);
2371
2372 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002373 if (MacroNameTok.is(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +00002374 return;
2375
2376 // Check to see if this is the last token on the #undef line.
2377 CheckEndOfDirective("#undef");
2378
2379 // Okay, we finally have a valid identifier to undef.
Chris Lattnercc1a8752007-10-07 08:44:20 +00002380 MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
Reid Spencer5f016e22007-07-11 17:01:13 +00002381
2382 // #undef untaints an identifier if it were marked by define_other_target.
2383 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
2384
2385 // If the macro is not defined, this is a noop undef, just return.
2386 if (MI == 0) return;
2387
2388 if (!MI->isUsed())
2389 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
2390
2391 // Free macro definition.
2392 delete MI;
Chris Lattnercc1a8752007-10-07 08:44:20 +00002393 setMacroInfo(MacroNameTok.getIdentifierInfo(), 0);
Reid Spencer5f016e22007-07-11 17:01:13 +00002394}
2395
2396
2397//===----------------------------------------------------------------------===//
2398// Preprocessor Conditional Directive Handling.
2399//===----------------------------------------------------------------------===//
2400
2401/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
2402/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
2403/// if any tokens have been returned or pp-directives activated before this
2404/// #ifndef has been lexed.
2405///
Chris Lattnerd2177732007-07-20 16:59:19 +00002406void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
Reid Spencer5f016e22007-07-11 17:01:13 +00002407 bool ReadAnyTokensBeforeDirective) {
2408 ++NumIf;
Chris Lattnerd2177732007-07-20 16:59:19 +00002409 Token DirectiveTok = Result;
Reid Spencer5f016e22007-07-11 17:01:13 +00002410
Chris Lattnerd2177732007-07-20 16:59:19 +00002411 Token MacroNameTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00002412 ReadMacroName(MacroNameTok);
2413
2414 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner22f6bbc2007-10-09 18:02:16 +00002415 if (MacroNameTok.is(tok::eom)) {
Chris Lattnerf37bb252007-09-24 05:14:57 +00002416 // Skip code until we get to #endif. This helps with recovery by not
2417 // emitting an error when the #endif is reached.
2418 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2419 /*Foundnonskip*/false, /*FoundElse*/false);
Reid Spencer5f016e22007-07-11 17:01:13 +00002420 return;
Chris Lattnerf37bb252007-09-24 05:14:57 +00002421 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002422
2423 // Check to see if this is the last token on the #if[n]def line.
2424 CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
2425
2426 // If the start of a top-level #ifdef, inform MIOpt.
2427 if (!ReadAnyTokensBeforeDirective &&
2428 CurLexer->getConditionalStackDepth() == 0) {
2429 assert(isIfndef && "#ifdef shouldn't reach here");
2430 CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
2431 }
2432
2433 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
Chris Lattnercc1a8752007-10-07 08:44:20 +00002434 MacroInfo *MI = getMacroInfo(MII);
Reid Spencer5f016e22007-07-11 17:01:13 +00002435
2436 // If there is a macro, process it.
2437 if (MI) {
2438 // Mark it used.
2439 MI->setIsUsed(true);
2440
2441 // If this is the first use of a target-specific macro, warn about it.
2442 if (MI->isTargetSpecific()) {
2443 MI->setIsTargetSpecific(false); // Don't warn on second use.
Ted Kremenek9c728dc2007-12-12 22:39:36 +00002444 getTargetInfo().DiagnoseNonPortability(
2445 getFullLoc(MacroNameTok.getLocation()),
2446 diag::port_target_macro_use);
Reid Spencer5f016e22007-07-11 17:01:13 +00002447 }
2448 } else {
2449 // Use of a target-specific macro for some other target? If so, warn.
2450 if (MII->isOtherTargetMacro()) {
2451 MII->setIsOtherTargetMacro(false); // Don't warn on second use.
Ted Kremenek9c728dc2007-12-12 22:39:36 +00002452 getTargetInfo().DiagnoseNonPortability(
2453 getFullLoc(MacroNameTok.getLocation()),
2454 diag::port_target_macro_use);
Reid Spencer5f016e22007-07-11 17:01:13 +00002455 }
2456 }
2457
2458 // Should we include the stuff contained by this directive?
2459 if (!MI == isIfndef) {
2460 // Yes, remember that we are inside a conditional, then lex the next token.
2461 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
2462 /*foundnonskip*/true, /*foundelse*/false);
2463 } else {
2464 // No, skip the contents of this block and return the first token after it.
2465 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2466 /*Foundnonskip*/false,
2467 /*FoundElse*/false);
2468 }
2469}
2470
2471/// HandleIfDirective - Implements the #if directive.
2472///
Chris Lattnerd2177732007-07-20 16:59:19 +00002473void Preprocessor::HandleIfDirective(Token &IfToken,
Reid Spencer5f016e22007-07-11 17:01:13 +00002474 bool ReadAnyTokensBeforeDirective) {
2475 ++NumIf;
2476
2477 // Parse and evaluation the conditional expression.
2478 IdentifierInfo *IfNDefMacro = 0;
2479 bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2480
2481 // Should we include the stuff contained by this directive?
2482 if (ConditionalTrue) {
2483 // If this condition is equivalent to #ifndef X, and if this is the first
2484 // directive seen, handle it for the multiple-include optimization.
2485 if (!ReadAnyTokensBeforeDirective &&
2486 CurLexer->getConditionalStackDepth() == 0 && IfNDefMacro)
2487 CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
2488
2489 // Yes, remember that we are inside a conditional, then lex the next token.
2490 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2491 /*foundnonskip*/true, /*foundelse*/false);
2492 } else {
2493 // No, skip the contents of this block and return the first token after it.
2494 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
2495 /*FoundElse*/false);
2496 }
2497}
2498
2499/// HandleEndifDirective - Implements the #endif directive.
2500///
Chris Lattnerd2177732007-07-20 16:59:19 +00002501void Preprocessor::HandleEndifDirective(Token &EndifToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002502 ++NumEndif;
2503
2504 // Check that this is the whole directive.
2505 CheckEndOfDirective("#endif");
2506
2507 PPConditionalInfo CondInfo;
2508 if (CurLexer->popConditionalLevel(CondInfo)) {
2509 // No conditionals on the stack: this is an #endif without an #if.
2510 return Diag(EndifToken, diag::err_pp_endif_without_if);
2511 }
2512
2513 // If this the end of a top-level #endif, inform MIOpt.
2514 if (CurLexer->getConditionalStackDepth() == 0)
2515 CurLexer->MIOpt.ExitTopLevelConditional();
2516
2517 assert(!CondInfo.WasSkipping && !CurLexer->LexingRawMode &&
2518 "This code should only be reachable in the non-skipping case!");
2519}
2520
2521
Chris Lattnerd2177732007-07-20 16:59:19 +00002522void Preprocessor::HandleElseDirective(Token &Result) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002523 ++NumElse;
2524
2525 // #else directive in a non-skipping conditional... start skipping.
2526 CheckEndOfDirective("#else");
2527
2528 PPConditionalInfo CI;
2529 if (CurLexer->popConditionalLevel(CI))
2530 return Diag(Result, diag::pp_err_else_without_if);
2531
2532 // If this is a top-level #else, inform the MIOpt.
2533 if (CurLexer->getConditionalStackDepth() == 0)
2534 CurLexer->MIOpt.FoundTopLevelElse();
2535
2536 // If this is a #else with a #else before it, report the error.
2537 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2538
2539 // Finally, skip the rest of the contents of this block and return the first
2540 // token after it.
2541 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2542 /*FoundElse*/true);
2543}
2544
Chris Lattnerd2177732007-07-20 16:59:19 +00002545void Preprocessor::HandleElifDirective(Token &ElifToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002546 ++NumElse;
2547
2548 // #elif directive in a non-skipping conditional... start skipping.
2549 // We don't care what the condition is, because we will always skip it (since
2550 // the block immediately before it was included).
2551 DiscardUntilEndOfDirective();
2552
2553 PPConditionalInfo CI;
2554 if (CurLexer->popConditionalLevel(CI))
2555 return Diag(ElifToken, diag::pp_err_elif_without_if);
2556
2557 // If this is a top-level #elif, inform the MIOpt.
2558 if (CurLexer->getConditionalStackDepth() == 0)
2559 CurLexer->MIOpt.FoundTopLevelElse();
2560
2561 // If this is a #elif with a #else before it, report the error.
2562 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2563
2564 // Finally, skip the rest of the contents of this block and return the first
2565 // token after it.
2566 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2567 /*FoundElse*/CI.FoundElse);
2568}
2569