blob: 78e4496a5d55946599b7998f3286a85b2126c497 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Preprocessor interface.
11//
12//===----------------------------------------------------------------------===//
13//
Chris Lattner22eb9722006-06-18 05:43:12 +000014// Options to support:
15// -H - Print the name of each header file used.
Chris Lattner22eb9722006-06-18 05:43:12 +000016// -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//
Chris Lattner22eb9722006-06-18 05:43:12 +000026//===----------------------------------------------------------------------===//
27
28#include "clang/Lex/Preprocessor.h"
Chris Lattner07b019a2006-10-22 07:28:56 +000029#include "clang/Lex/HeaderSearch.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000030#include "clang/Lex/MacroInfo.h"
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +000031#include "clang/Lex/PPCallbacks.h"
Chris Lattnerb8761832006-06-24 21:31:03 +000032#include "clang/Lex/Pragma.h"
Chris Lattner0b8cfc22006-06-28 06:49:17 +000033#include "clang/Lex/ScratchBuffer.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000034#include "clang/Basic/Diagnostic.h"
35#include "clang/Basic/FileManager.h"
36#include "clang/Basic/SourceManager.h"
Chris Lattner81278c62006-10-14 19:03:49 +000037#include "clang/Basic/TargetInfo.h"
Chris Lattner7a4af3b2006-07-26 06:26:52 +000038#include "llvm/ADT/SmallVector.h"
Chris Lattner8a7003c2007-07-16 06:48:38 +000039#include "llvm/Support/MemoryBuffer.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000040#include <iostream>
Chris Lattnera2633932007-09-03 18:30:32 +000041#include <ctime>
Chris Lattner22eb9722006-06-18 05:43:12 +000042using namespace clang;
43
44//===----------------------------------------------------------------------===//
45
Chris Lattner02dffbd2006-10-14 07:50:21 +000046Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
Chris Lattnerad7cdd32006-11-21 06:08:20 +000047 TargetInfo &target, SourceManager &SM,
Chris Lattner59a9ebd2006-10-18 05:34:33 +000048 HeaderSearch &Headers)
Chris Lattnerad7cdd32006-11-21 06:08:20 +000049 : Diags(diags), Features(opts), Target(target), FileMgr(Headers.getFileMgr()),
50 SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts),
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +000051 CurLexer(0), CurDirLookup(0), CurMacroExpander(0), Callbacks(0) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +000052 ScratchBuf = new ScratchBuffer(SourceMgr);
Chris Lattnerc02c4ab2007-07-15 00:25:26 +000053
Chris Lattner22eb9722006-06-18 05:43:12 +000054 // Clear stats.
Chris Lattner59a9ebd2006-10-18 05:34:33 +000055 NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +000056 NumIf = NumElse = NumEndif = 0;
Chris Lattner78186052006-07-09 00:45:31 +000057 NumEnteredSourceFiles = 0;
58 NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
Chris Lattner510ab612006-07-20 04:47:30 +000059 NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
Chris Lattner59a9ebd2006-10-18 05:34:33 +000060 MaxIncludeStackDepth = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +000061 NumSkipped = 0;
Chris Lattnerb352e3e2006-11-21 06:17:10 +000062
63 // Default to discarding comments.
64 KeepComments = false;
65 KeepMacroComments = false;
66
Chris Lattner22eb9722006-06-18 05:43:12 +000067 // Macro expansion is enabled.
68 DisableMacroExpansion = false;
Chris Lattneree8760b2006-07-15 07:42:55 +000069 InMacroArgs = false;
Chris Lattnerc02c4ab2007-07-15 00:25:26 +000070 NumCachedMacroExpanders = 0;
Chris Lattner0c885f52006-06-21 06:50:18 +000071
Chris Lattner8ff71992006-07-06 05:17:39 +000072 // "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 Lattner1f1b0db2007-10-09 22:10:18 +000076 Predefines = 0;
77
Chris Lattnerb8761832006-06-24 21:31:03 +000078 // Initialize the pragma handlers.
79 PragmaHandlers = new PragmaNamespace(0);
80 RegisterBuiltinPragmas();
Chris Lattner677757a2006-06-28 05:26:32 +000081
82 // Initialize builtin macros like __LINE__ and friends.
83 RegisterBuiltinMacros();
Chris Lattner22eb9722006-06-18 05:43:12 +000084}
85
86Preprocessor::~Preprocessor() {
87 // Free any active lexers.
88 delete CurLexer;
89
Chris Lattner69772b02006-07-02 20:34:39 +000090 while (!IncludeMacroStack.empty()) {
91 delete IncludeMacroStack.back().TheLexer;
92 delete IncludeMacroStack.back().TheMacroExpander;
93 IncludeMacroStack.pop_back();
Chris Lattner22eb9722006-06-18 05:43:12 +000094 }
Chris Lattnerc43ddc82007-10-07 08:44:20 +000095
96 // Free any macro definitions.
97 for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
98 Macros.begin(), E = Macros.end(); I != E; ++I) {
99 // Free the macro definition.
100 delete I->second;
101 I->second = 0;
102 I->first->setHasMacroDefinition(false);
103 }
Chris Lattnerb8761832006-06-24 21:31:03 +0000104
Chris Lattnerc02c4ab2007-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
Chris Lattnerb8761832006-06-24 21:31:03 +0000109 // Release pragma information.
110 delete PragmaHandlers;
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000111
112 // Delete the scratch buffer info.
113 delete ScratchBuf;
Chris Lattner22eb9722006-06-18 05:43:12 +0000114}
115
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000116PPCallbacks::~PPCallbacks() {
117}
Chris Lattner87d3bec2006-10-17 03:44:32 +0000118
Chris Lattner22eb9722006-06-18 05:43:12 +0000119/// Diag - Forwarding function for diagnostics. This emits a diagnostic at
Chris Lattner146762e2007-07-20 16:59:19 +0000120/// the specified Token's location, translating the token's start
Chris Lattner22eb9722006-06-18 05:43:12 +0000121/// position in the current buffer into a SourcePosition object for rendering.
Chris Lattner36982e42007-05-16 17:49:37 +0000122void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID) {
123 Diags.Report(Loc, DiagID);
124}
125
Chris Lattnercb283342006-06-18 06:48:37 +0000126void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000127 const std::string &Msg) {
Chris Lattner36982e42007-05-16 17:49:37 +0000128 Diags.Report(Loc, DiagID, &Msg, 1);
Chris Lattner22eb9722006-06-18 05:43:12 +0000129}
Chris Lattnerd01e2912006-06-18 16:22:51 +0000130
Chris Lattner146762e2007-07-20 16:59:19 +0000131void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000132 std::cerr << tok::getTokenName(Tok.getKind()) << " '"
133 << getSpelling(Tok) << "'";
134
135 if (!DumpFlags) return;
136 std::cerr << "\t";
137 if (Tok.isAtStartOfLine())
138 std::cerr << " [StartOfLine]";
139 if (Tok.hasLeadingSpace())
140 std::cerr << " [LeadingSpace]";
Chris Lattner6e4bf522006-07-27 06:59:25 +0000141 if (Tok.isExpandDisabled())
142 std::cerr << " [ExpandDisabled]";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000143 if (Tok.needsCleaning()) {
Chris Lattner50b497e2006-06-18 16:32:35 +0000144 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000145 std::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
146 << "']";
147 }
148}
149
150void Preprocessor::DumpMacro(const MacroInfo &MI) const {
151 std::cerr << "MACRO: ";
152 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
153 DumpToken(MI.getReplacementToken(i));
154 std::cerr << " ";
155 }
156 std::cerr << "\n";
157}
158
Chris Lattner22eb9722006-06-18 05:43:12 +0000159void Preprocessor::PrintStats() {
160 std::cerr << "\n*** Preprocessor Stats:\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000161 std::cerr << NumDirectives << " directives found:\n";
162 std::cerr << " " << NumDefined << " #define.\n";
163 std::cerr << " " << NumUndefined << " #undef.\n";
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000164 std::cerr << " #include/#include_next/#import:\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000165 std::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
166 std::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
167 std::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
168 std::cerr << " " << NumElse << " #else/#elif.\n";
169 std::cerr << " " << NumEndif << " #endif.\n";
170 std::cerr << " " << NumPragma << " #pragma.\n";
171 std::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
172
Chris Lattner78186052006-07-09 00:45:31 +0000173 std::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
174 << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
Chris Lattner22eb9722006-06-18 05:43:12 +0000175 << NumFastMacroExpanded << " on the fast path.\n";
Chris Lattner510ab612006-07-20 04:47:30 +0000176 std::cerr << (NumFastTokenPaste+NumTokenPaste)
177 << " token paste (##) operations performed, "
178 << NumFastTokenPaste << " on the fast path.\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000179}
180
181//===----------------------------------------------------------------------===//
Chris Lattnerd01e2912006-06-18 16:22:51 +0000182// Token Spelling
183//===----------------------------------------------------------------------===//
184
185
186/// getSpelling() - Return the 'spelling' of this token. The spelling of a
187/// token are the characters used to represent the token in the source file
188/// after trigraph expansion and escaped-newline folding. In particular, this
189/// wants to get the true, uncanonicalized, spelling of things like digraphs
190/// UCNs, etc.
Chris Lattner146762e2007-07-20 16:59:19 +0000191std::string Preprocessor::getSpelling(const Token &Tok) const {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000192 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
193
194 // If this token contains nothing interesting, return it directly.
Chris Lattner50b497e2006-06-18 16:32:35 +0000195 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000196 if (!Tok.needsCleaning())
197 return std::string(TokStart, TokStart+Tok.getLength());
198
Chris Lattnerd01e2912006-06-18 16:22:51 +0000199 std::string Result;
200 Result.reserve(Tok.getLength());
201
Chris Lattneref9eae12006-07-04 22:33:12 +0000202 // Otherwise, hard case, relex the characters into the string.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000203 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
204 Ptr != End; ) {
205 unsigned CharSize;
206 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
207 Ptr += CharSize;
208 }
209 assert(Result.size() != unsigned(Tok.getLength()) &&
210 "NeedsCleaning flag set on something that didn't need cleaning!");
211 return Result;
212}
213
214/// getSpelling - This method is used to get the spelling of a token into a
215/// preallocated buffer, instead of as an std::string. The caller is required
216/// to allocate enough space for the token, which is guaranteed to be at least
217/// Tok.getLength() bytes long. The actual length of the token is returned.
Chris Lattneref9eae12006-07-04 22:33:12 +0000218///
219/// Note that this method may do two possible things: it may either fill in
220/// the buffer specified with characters, or it may *change the input pointer*
221/// to point to a constant buffer with the data already in it (avoiding a
222/// copy). The caller is not allowed to modify the returned buffer pointer
223/// if an internal buffer is returned.
Chris Lattner146762e2007-07-20 16:59:19 +0000224unsigned Preprocessor::getSpelling(const Token &Tok,
Chris Lattneref9eae12006-07-04 22:33:12 +0000225 const char *&Buffer) const {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000226 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
227
Chris Lattnerd3a15f72006-07-04 23:01:03 +0000228 // If this token is an identifier, just return the string from the identifier
229 // table, which is very quick.
230 if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
231 Buffer = II->getName();
Chris Lattner32e6d642007-07-22 22:50:09 +0000232
233 // Return the length of the token. If the token needed cleaning, don't
234 // include the size of the newlines or trigraphs in it.
235 if (!Tok.needsCleaning())
236 return Tok.getLength();
237 else
238 return strlen(Buffer);
Chris Lattnerd3a15f72006-07-04 23:01:03 +0000239 }
240
241 // Otherwise, compute the start of the token in the input lexer buffer.
Chris Lattner50b497e2006-06-18 16:32:35 +0000242 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000243
244 // If this token contains nothing interesting, return it directly.
245 if (!Tok.needsCleaning()) {
Chris Lattneref9eae12006-07-04 22:33:12 +0000246 Buffer = TokStart;
247 return Tok.getLength();
Chris Lattnerd01e2912006-06-18 16:22:51 +0000248 }
249 // Otherwise, hard case, relex the characters into the string.
Chris Lattneref9eae12006-07-04 22:33:12 +0000250 char *OutBuf = const_cast<char*>(Buffer);
Chris Lattnerd01e2912006-06-18 16:22:51 +0000251 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
252 Ptr != End; ) {
253 unsigned CharSize;
254 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
255 Ptr += CharSize;
256 }
257 assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
258 "NeedsCleaning flag set on something that didn't need cleaning!");
259
260 return OutBuf-Buffer;
261}
262
Chris Lattnerb94ec7b2006-07-14 06:54:10 +0000263
264/// CreateString - Plop the specified string into a scratch buffer and return a
265/// location for it. If specified, the source location provides a source
266/// location for the token.
267SourceLocation Preprocessor::
268CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) {
269 if (SLoc.isValid())
270 return ScratchBuf->getToken(Buf, Len, SLoc);
271 return ScratchBuf->getToken(Buf, Len);
272}
273
274
Chris Lattner8a7003c2007-07-16 06:48:38 +0000275/// AdvanceToTokenCharacter - Given a location that specifies the start of a
276/// token, return a new location that specifies a character within the token.
277SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart,
278 unsigned CharNo) {
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000279 // If they request the first char of the token, we're trivially done. If this
280 // is a macro expansion, it doesn't make sense to point to a character within
281 // the instantiation point (the name). We could point to the source
282 // character, but without also pointing to instantiation info, this is
283 // confusing.
284 if (CharNo == 0 || TokStart.isMacroID()) return TokStart;
Chris Lattner8a7003c2007-07-16 06:48:38 +0000285
286 // Figure out how many physical characters away the specified logical
287 // character is. This needs to take into consideration newlines and
288 // trigraphs.
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000289 const char *TokPtr = SourceMgr.getCharacterData(TokStart);
290 unsigned PhysOffset = 0;
Chris Lattner8a7003c2007-07-16 06:48:38 +0000291
292 // The usual case is that tokens don't contain anything interesting. Skip
293 // over the uninteresting characters. If a token only consists of simple
294 // chars, this method is extremely fast.
295 while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr))
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000296 ++TokPtr, --CharNo, ++PhysOffset;
Chris Lattner8a7003c2007-07-16 06:48:38 +0000297
298 // If we have a character that may be a trigraph or escaped newline, create a
299 // lexer to parse it correctly.
Chris Lattner8a7003c2007-07-16 06:48:38 +0000300 if (CharNo != 0) {
301 // Create a lexer starting at this token position.
Chris Lattner77e9de52007-07-20 16:52:03 +0000302 Lexer TheLexer(TokStart, *this, TokPtr);
Chris Lattner146762e2007-07-20 16:59:19 +0000303 Token Tok;
Chris Lattner8a7003c2007-07-16 06:48:38 +0000304 // Skip over characters the remaining characters.
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000305 const char *TokStartPtr = TokPtr;
Chris Lattner8a7003c2007-07-16 06:48:38 +0000306 for (; CharNo; --CharNo)
307 TheLexer.getAndAdvanceChar(TokPtr, Tok);
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000308
309 PhysOffset += TokPtr-TokStartPtr;
Chris Lattner8a7003c2007-07-16 06:48:38 +0000310 }
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000311
312 return TokStart.getFileLocWithOffset(PhysOffset);
Chris Lattner8a7003c2007-07-16 06:48:38 +0000313}
314
315
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000316//===----------------------------------------------------------------------===//
317// Preprocessor Initialization Methods
318//===----------------------------------------------------------------------===//
319
320// Append a #define line to Buf for Macro. Macro should be of the form XXX,
321// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
322// "#define XXX Y z W". To get a #define with no value, use "XXX=".
323static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
324 const char *Command = "#define ") {
325 Buf.insert(Buf.end(), Command, Command+strlen(Command));
326 if (const char *Equal = strchr(Macro, '=')) {
327 // Turn the = into ' '.
328 Buf.insert(Buf.end(), Macro, Equal);
329 Buf.push_back(' ');
330 Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal));
331 } else {
332 // Push "macroname 1".
333 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
334 Buf.push_back(' ');
335 Buf.push_back('1');
336 }
337 Buf.push_back('\n');
338}
339
340
341static void InitializePredefinedMacros(Preprocessor &PP,
342 std::vector<char> &Buf) {
343 // FIXME: Implement magic like cpp_init_builtins for things like __STDC__
344 // and __DATE__ etc.
345#if 0
346 /* __STDC__ has the value 1 under normal circumstances.
347 However, if (a) we are in a system header, (b) the option
348 stdc_0_in_system_headers is true (set by target config), and
349 (c) we are not in strictly conforming mode, then it has the
350 value 0. (b) and (c) are already checked in cpp_init_builtins. */
351 //case BT_STDC:
352 if (cpp_in_system_header (pfile))
353 number = 0;
354 else
355 number = 1;
356 break;
357#endif
358 // These should all be defined in the preprocessor according to the
359 // current language configuration.
360 DefineBuiltinMacro(Buf, "__STDC__=1");
361 //DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
362 if (PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus)
363 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
364 else if (0) // STDC94 ?
365 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
366
367 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");
368 if (PP.getLangOptions().ObjC1)
369 DefineBuiltinMacro(Buf, "__OBJC__=1");
370 if (PP.getLangOptions().ObjC2)
371 DefineBuiltinMacro(Buf, "__OBJC2__=1");
Chris Lattner5d451782007-10-09 22:58:09 +0000372
373 if (PP.getLangOptions().ObjC1) {
374 // FIXME: make this the right thing.
375 const char *IDTypedef = "/*typedef int id;*/\n";
376 Buf.insert(Buf.end(), IDTypedef, IDTypedef+strlen(IDTypedef));
377 }
378
Chris Lattnered2a9eb2007-10-10 17:48:53 +0000379 // Add __builtin_va_list typedef.
380 {
381 const char *VAList = PP.getTargetInfo().getVAListDeclaration();
382 Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
383 Buf.push_back('\n');
384 }
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000385
386 // Get the target #defines.
387 PP.getTargetInfo().getTargetDefines(Buf);
388
389 // Compiler set macros.
390 DefineBuiltinMacro(Buf, "__APPLE_CC__=5250");
391 DefineBuiltinMacro(Buf, "__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=1030");
392 DefineBuiltinMacro(Buf, "__GNUC_MINOR__=0");
393 DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
394 DefineBuiltinMacro(Buf, "__GNUC__=4");
395 DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
396 DefineBuiltinMacro(Buf, "__VERSION__=\"4.0.1 (Apple Computer, Inc. "
397 "build 5250)\"");
398
399 // Build configuration options.
400 DefineBuiltinMacro(Buf, "__DYNAMIC__=1");
401 DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
402 DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
403 DefineBuiltinMacro(Buf, "__PIC__=1");
404
405
406 if (PP.getLangOptions().CPlusPlus) {
407 DefineBuiltinMacro(Buf, "__DEPRECATED=1");
408 DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
409 DefineBuiltinMacro(Buf, "__GNUG__=4");
410 DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
411 DefineBuiltinMacro(Buf, "__cplusplus=1");
412 DefineBuiltinMacro(Buf, "__private_extern__=extern");
413 }
414
415 // FIXME: Should emit a #line directive here.
416}
417
418
419/// EnterMainSourceFile - Enter the specified FileID as the main source file,
420/// which implicitly adds the builting defines etc.
421void Preprocessor::EnterMainSourceFile(unsigned MainFileID) {
422 // Enter the main file source buffer.
423 EnterSourceFile(MainFileID, 0);
424
425
426 std::vector<char> PrologFile;
427 PrologFile.reserve(4080);
428
429 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
430 InitializePredefinedMacros(*this, PrologFile);
431
432 // Add on the predefines from the driver.
433 PrologFile.insert(PrologFile.end(), Predefines,Predefines+strlen(Predefines));
434
435 // Memory buffer must end with a null byte!
436 PrologFile.push_back(0);
437
438 // Now that we have emitted the predefined macros, #includes, etc into
439 // PrologFile, preprocess it to populate the initial preprocessor state.
440 llvm::MemoryBuffer *SB =
441 llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(),
442 "<predefines>");
443 assert(SB && "Cannot fail to create predefined source buffer");
444 unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB);
445 assert(FileID && "Could not create FileID for predefines?");
446
447 // Start parsing the predefines.
448 EnterSourceFile(FileID, 0);
449}
Chris Lattner8a7003c2007-07-16 06:48:38 +0000450
Chris Lattnerd01e2912006-06-18 16:22:51 +0000451//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +0000452// Source File Location Methods.
453//===----------------------------------------------------------------------===//
454
Chris Lattner22eb9722006-06-18 05:43:12 +0000455/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
456/// return null on failure. isAngled indicates whether the file reference is
457/// for system #include's or not (i.e. using <> instead of "").
Chris Lattnerb8b94f12006-10-30 05:38:06 +0000458const FileEntry *Preprocessor::LookupFile(const char *FilenameStart,
459 const char *FilenameEnd,
Chris Lattnerc8997182006-06-22 05:52:16 +0000460 bool isAngled,
Chris Lattner22eb9722006-06-18 05:43:12 +0000461 const DirectoryLookup *FromDir,
Chris Lattnerc8997182006-06-22 05:52:16 +0000462 const DirectoryLookup *&CurDir) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000463 // If the header lookup mechanism may be relative to the current file, pass in
464 // info about where the current file is.
465 const FileEntry *CurFileEnt = 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000466 if (!FromDir) {
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000467 SourceLocation FileLoc = getCurrentFileLexer()->getFileLoc();
468 CurFileEnt = SourceMgr.getFileEntryForLoc(FileLoc);
Chris Lattner22eb9722006-06-18 05:43:12 +0000469 }
470
Chris Lattner63dd32b2006-10-20 04:42:40 +0000471 // Do a standard file entry lookup.
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000472 CurDir = CurDirLookup;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000473 const FileEntry *FE =
Chris Lattner7cdbad92006-10-30 05:33:15 +0000474 HeaderInfo.LookupFile(FilenameStart, FilenameEnd,
475 isAngled, FromDir, CurDir, CurFileEnt);
Chris Lattner63dd32b2006-10-20 04:42:40 +0000476 if (FE) return FE;
477
478 // Otherwise, see if this is a subframework header. If so, this is relative
479 // to one of the headers on the #include stack. Walk the list of the current
480 // headers on the #include stack and pass them to HeaderInfo.
Chris Lattner5c683b22006-10-20 05:12:14 +0000481 if (CurLexer && !CurLexer->Is_PragmaLexer) {
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000482 CurFileEnt = SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc());
Chris Lattner7cdbad92006-10-30 05:33:15 +0000483 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
484 CurFileEnt)))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000485 return FE;
486 }
487
488 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
489 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
Chris Lattner5c683b22006-10-20 05:12:14 +0000490 if (ISEntry.TheLexer && !ISEntry.TheLexer->Is_PragmaLexer) {
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000491 CurFileEnt = SourceMgr.getFileEntryForLoc(ISEntry.TheLexer->getFileLoc());
Chris Lattner7cdbad92006-10-30 05:33:15 +0000492 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
493 CurFileEnt)))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000494 return FE;
495 }
496 }
497
498 // Otherwise, we really couldn't find the file.
499 return 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000500}
501
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000502/// isInPrimaryFile - Return true if we're in the top-level file, not in a
503/// #include.
504bool Preprocessor::isInPrimaryFile() const {
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000505 if (CurLexer && !CurLexer->Is_PragmaLexer)
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000506 return IncludeMacroStack.empty();
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000507
Chris Lattner13044d92006-07-03 05:16:44 +0000508 // If there are any stacked lexers, we're in a #include.
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000509 assert(IncludeMacroStack[0].TheLexer &&
510 !IncludeMacroStack[0].TheLexer->Is_PragmaLexer &&
511 "Top level include stack isn't our primary lexer?");
512 for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i)
Chris Lattner13044d92006-07-03 05:16:44 +0000513 if (IncludeMacroStack[i].TheLexer &&
514 !IncludeMacroStack[i].TheLexer->Is_PragmaLexer)
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000515 return false;
516 return true;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000517}
518
519/// getCurrentLexer - Return the current file lexer being lexed from. Note
520/// that this ignores any potentially active macro expansions and _Pragma
521/// expansions going on at the time.
522Lexer *Preprocessor::getCurrentFileLexer() const {
523 if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer;
524
525 // Look for a stacked lexer.
526 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Chris Lattnerf88c53a2006-07-03 05:26:05 +0000527 Lexer *L = IncludeMacroStack[i-1].TheLexer;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000528 if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions.
529 return L;
530 }
531 return 0;
532}
533
534
Chris Lattner22eb9722006-06-18 05:43:12 +0000535/// EnterSourceFile - Add a source file to the top of the include stack and
536/// start lexing tokens from it instead of the current buffer. Return true
537/// on failure.
538void Preprocessor::EnterSourceFile(unsigned FileID,
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000539 const DirectoryLookup *CurDir) {
Chris Lattner69772b02006-07-02 20:34:39 +0000540 assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000541 ++NumEnteredSourceFiles;
542
Chris Lattner69772b02006-07-02 20:34:39 +0000543 if (MaxIncludeStackDepth < IncludeMacroStack.size())
544 MaxIncludeStackDepth = IncludeMacroStack.size();
Chris Lattner22eb9722006-06-18 05:43:12 +0000545
Chris Lattner77e9de52007-07-20 16:52:03 +0000546 Lexer *TheLexer = new Lexer(SourceLocation::getFileLoc(FileID, 0), *this);
Chris Lattner69772b02006-07-02 20:34:39 +0000547 EnterSourceFileWithLexer(TheLexer, CurDir);
548}
Chris Lattner22eb9722006-06-18 05:43:12 +0000549
Chris Lattner69772b02006-07-02 20:34:39 +0000550/// EnterSourceFile - Add a source file to the top of the include stack and
551/// start lexing tokens from it instead of the current buffer.
552void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
553 const DirectoryLookup *CurDir) {
554
555 // Add the current lexer to the include stack.
556 if (CurLexer || CurMacroExpander)
557 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
558 CurMacroExpander));
559
560 CurLexer = TheLexer;
Chris Lattnerc8997182006-06-22 05:52:16 +0000561 CurDirLookup = CurDir;
Chris Lattner69772b02006-07-02 20:34:39 +0000562 CurMacroExpander = 0;
Chris Lattner0c885f52006-06-21 06:50:18 +0000563
564 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000565 if (Callbacks && !CurLexer->Is_PragmaLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000566 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
567
568 // Get the file entry for the current file.
569 if (const FileEntry *FE =
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000570 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000571 FileType = HeaderInfo.getFileDirFlavor(FE);
Chris Lattnerc8997182006-06-22 05:52:16 +0000572
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000573 Callbacks->FileChanged(CurLexer->getFileLoc(),
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000574 PPCallbacks::EnterFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +0000575 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000576}
577
Chris Lattner69772b02006-07-02 20:34:39 +0000578
579
Chris Lattner22eb9722006-06-18 05:43:12 +0000580/// EnterMacro - Add a Macro to the top of the include stack and start lexing
Chris Lattnercb283342006-06-18 06:48:37 +0000581/// tokens from it instead of the current buffer.
Chris Lattner146762e2007-07-20 16:59:19 +0000582void Preprocessor::EnterMacro(Token &Tok, MacroArgs *Args) {
Chris Lattner69772b02006-07-02 20:34:39 +0000583 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
584 CurMacroExpander));
585 CurLexer = 0;
586 CurDirLookup = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000587
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000588 if (NumCachedMacroExpanders == 0) {
589 CurMacroExpander = new MacroExpander(Tok, Args, *this);
590 } else {
591 CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
592 CurMacroExpander->Init(Tok, Args);
593 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000594}
595
Chris Lattner7667d0d2006-07-16 18:16:58 +0000596/// EnterTokenStream - Add a "macro" context to the top of the include stack,
597/// which will cause the lexer to start returning the specified tokens. Note
598/// that these tokens will be re-macro-expanded when/if expansion is enabled.
599/// This method assumes that the specified stream of tokens has a permanent
600/// owner somewhere, so they do not need to be copied.
Chris Lattner146762e2007-07-20 16:59:19 +0000601void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks) {
Chris Lattner7667d0d2006-07-16 18:16:58 +0000602 // Save our current state.
603 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
604 CurMacroExpander));
605 CurLexer = 0;
606 CurDirLookup = 0;
607
608 // Create a macro expander to expand from the specified token stream.
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000609 if (NumCachedMacroExpanders == 0) {
610 CurMacroExpander = new MacroExpander(Toks, NumToks, *this);
611 } else {
612 CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
613 CurMacroExpander->Init(Toks, NumToks);
614 }
Chris Lattner7667d0d2006-07-16 18:16:58 +0000615}
616
617/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
618/// lexer stack. This should only be used in situations where the current
619/// state of the top-of-stack lexer is known.
620void Preprocessor::RemoveTopOfLexerStack() {
621 assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000622
623 if (CurMacroExpander) {
624 // Delete or cache the now-dead macro expander.
625 if (NumCachedMacroExpanders == MacroExpanderCacheSize)
626 delete CurMacroExpander;
627 else
628 MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander;
629 } else {
630 delete CurLexer;
631 }
Chris Lattner7667d0d2006-07-16 18:16:58 +0000632 CurLexer = IncludeMacroStack.back().TheLexer;
633 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
634 CurMacroExpander = IncludeMacroStack.back().TheMacroExpander;
635 IncludeMacroStack.pop_back();
636}
637
Chris Lattner22eb9722006-06-18 05:43:12 +0000638//===----------------------------------------------------------------------===//
Chris Lattner677757a2006-06-28 05:26:32 +0000639// Macro Expansion Handling.
Chris Lattner22eb9722006-06-18 05:43:12 +0000640//===----------------------------------------------------------------------===//
641
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000642/// setMacroInfo - Specify a macro for this identifier.
643///
644void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
645 if (MI == 0) {
646 if (II->hasMacroDefinition()) {
647 Macros.erase(II);
648 II->setHasMacroDefinition(false);
649 }
650 } else {
651 Macros[II] = MI;
652 II->setHasMacroDefinition(true);
653 }
654}
655
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000656/// RegisterBuiltinMacro - Register the specified identifier in the identifier
657/// table and mark it as a builtin macro to be expanded.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000658IdentifierInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000659 // Get the identifier.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000660 IdentifierInfo *Id = getIdentifierInfo(Name);
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000661
662 // Mark it as being a macro that is builtin.
663 MacroInfo *MI = new MacroInfo(SourceLocation());
664 MI->setIsBuiltinMacro();
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000665 setMacroInfo(Id, MI);
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000666 return Id;
667}
668
669
Chris Lattner677757a2006-06-28 05:26:32 +0000670/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
671/// identifier table.
672void Preprocessor::RegisterBuiltinMacros() {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000673 Ident__LINE__ = RegisterBuiltinMacro("__LINE__");
Chris Lattner630b33c2006-07-01 22:46:53 +0000674 Ident__FILE__ = RegisterBuiltinMacro("__FILE__");
Chris Lattnerc673f902006-06-30 06:10:41 +0000675 Ident__DATE__ = RegisterBuiltinMacro("__DATE__");
676 Ident__TIME__ = RegisterBuiltinMacro("__TIME__");
Chris Lattner69772b02006-07-02 20:34:39 +0000677 Ident_Pragma = RegisterBuiltinMacro("_Pragma");
Chris Lattnerc1283b92006-07-01 23:16:30 +0000678
679 // GCC Extensions.
680 Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__");
681 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__");
Chris Lattner847e0e42006-07-01 23:49:16 +0000682 Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__");
Chris Lattner22eb9722006-06-18 05:43:12 +0000683}
684
Chris Lattnerc2395832006-07-09 00:57:04 +0000685/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
686/// in its expansion, currently expands to that token literally.
Chris Lattner3ce1d1a2006-07-09 01:00:18 +0000687static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000688 const IdentifierInfo *MacroIdent,
689 Preprocessor &PP) {
Chris Lattnerc2395832006-07-09 00:57:04 +0000690 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
691
692 // If the token isn't an identifier, it's always literally expanded.
693 if (II == 0) return true;
694
695 // If the identifier is a macro, and if that macro is enabled, it may be
696 // expanded so it's not a trivial expansion.
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000697 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
Chris Lattner3ce1d1a2006-07-09 01:00:18 +0000698 // Fast expanding "#define X X" is ok, because X would be disabled.
699 II != MacroIdent)
Chris Lattnerc2395832006-07-09 00:57:04 +0000700 return false;
701
702 // If this is an object-like macro invocation, it is safe to trivially expand
703 // it.
704 if (MI->isObjectLike()) return true;
705
706 // If this is a function-like macro invocation, it's safe to trivially expand
707 // as long as the identifier is not a macro argument.
708 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
709 I != E; ++I)
710 if (*I == II)
711 return false; // Identifier is a macro argument.
Chris Lattner273ddd52006-07-29 07:33:01 +0000712
Chris Lattnerc2395832006-07-09 00:57:04 +0000713 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000714}
715
Chris Lattnerc2395832006-07-09 00:57:04 +0000716
Chris Lattnerafe603f2006-07-11 04:02:46 +0000717/// isNextPPTokenLParen - Determine whether the next preprocessor token to be
718/// lexed is a '('. If so, consume the token and return true, if not, this
719/// method should have no observable side-effect on the lexed tokens.
720bool Preprocessor::isNextPPTokenLParen() {
Chris Lattnerafe603f2006-07-11 04:02:46 +0000721 // Do some quick tests for rejection cases.
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000722 unsigned Val;
723 if (CurLexer)
Chris Lattner678c8802006-07-11 05:46:12 +0000724 Val = CurLexer->isNextPPTokenLParen();
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000725 else
726 Val = CurMacroExpander->isNextTokenLParen();
727
728 if (Val == 2) {
Chris Lattner5c983792007-07-19 00:07:36 +0000729 // We have run off the end. If it's a source file we don't
730 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
731 // macro stack.
732 if (CurLexer)
733 return false;
734 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000735 IncludeStackInfo &Entry = IncludeMacroStack[i-1];
736 if (Entry.TheLexer)
Chris Lattner678c8802006-07-11 05:46:12 +0000737 Val = Entry.TheLexer->isNextPPTokenLParen();
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000738 else
739 Val = Entry.TheMacroExpander->isNextTokenLParen();
Chris Lattner5c983792007-07-19 00:07:36 +0000740
741 if (Val != 2)
742 break;
743
744 // Ran off the end of a source file?
745 if (Entry.TheLexer)
746 return false;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000747 }
Chris Lattnerafe603f2006-07-11 04:02:46 +0000748 }
749
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000750 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
751 // have found something that isn't a '(' or we found the end of the
752 // translation unit. In either case, return false.
753 if (Val != 1)
754 return false;
Chris Lattnerafe603f2006-07-11 04:02:46 +0000755
Chris Lattner146762e2007-07-20 16:59:19 +0000756 Token Tok;
Chris Lattnerafe603f2006-07-11 04:02:46 +0000757 LexUnexpandedToken(Tok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000758 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000759 return true;
Chris Lattnerafe603f2006-07-11 04:02:46 +0000760}
Chris Lattner677757a2006-06-28 05:26:32 +0000761
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000762/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
763/// expanded as a macro, handle it and return the next token as 'Identifier'.
Chris Lattner146762e2007-07-20 16:59:19 +0000764bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000765 MacroInfo *MI) {
Chris Lattner78186052006-07-09 00:45:31 +0000766
767 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
768 if (MI->isBuiltinMacro()) {
769 ExpandBuiltinMacro(Identifier);
770 return false;
771 }
772
Chris Lattner81278c62006-10-14 19:03:49 +0000773 // If this is the first use of a target-specific macro, warn about it.
774 if (MI->isTargetSpecific()) {
775 MI->setIsTargetSpecific(false); // Don't warn on second use.
776 getTargetInfo().DiagnoseNonPortability(Identifier.getLocation(),
777 diag::port_target_macro_use);
778 }
779
Chris Lattneree8760b2006-07-15 07:42:55 +0000780 /// Args - If this is a function-like macro expansion, this contains,
Chris Lattner78186052006-07-09 00:45:31 +0000781 /// for each macro argument, the list of tokens that were provided to the
782 /// invocation.
Chris Lattneree8760b2006-07-15 07:42:55 +0000783 MacroArgs *Args = 0;
Chris Lattner78186052006-07-09 00:45:31 +0000784
785 // If this is a function-like macro, read the arguments.
786 if (MI->isFunctionLike()) {
Chris Lattner78186052006-07-09 00:45:31 +0000787 // C99 6.10.3p10: If the preprocessing token immediately after the the macro
Chris Lattner24dbee72007-07-19 16:11:58 +0000788 // name isn't a '(', this macro should not be expanded. Otherwise, consume
789 // it.
Chris Lattnerafe603f2006-07-11 04:02:46 +0000790 if (!isNextPPTokenLParen())
Chris Lattner78186052006-07-09 00:45:31 +0000791 return true;
792
Chris Lattner78186052006-07-09 00:45:31 +0000793 // Remember that we are now parsing the arguments to a macro invocation.
794 // Preprocessor directives used inside macro arguments are not portable, and
795 // this enables the warning.
Chris Lattneree8760b2006-07-15 07:42:55 +0000796 InMacroArgs = true;
797 Args = ReadFunctionLikeMacroArgs(Identifier, MI);
Chris Lattner78186052006-07-09 00:45:31 +0000798
799 // Finished parsing args.
Chris Lattneree8760b2006-07-15 07:42:55 +0000800 InMacroArgs = false;
Chris Lattner78186052006-07-09 00:45:31 +0000801
802 // If there was an error parsing the arguments, bail out.
Chris Lattneree8760b2006-07-15 07:42:55 +0000803 if (Args == 0) return false;
Chris Lattner78186052006-07-09 00:45:31 +0000804
805 ++NumFnMacroExpanded;
806 } else {
807 ++NumMacroExpanded;
808 }
Chris Lattner13044d92006-07-03 05:16:44 +0000809
810 // Notice that this macro has been used.
811 MI->setIsUsed(true);
Chris Lattner69772b02006-07-02 20:34:39 +0000812
813 // If we started lexing a macro, enter the macro expansion body.
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000814
815 // If this macro expands to no tokens, don't bother to push it onto the
816 // expansion stack, only to take it right back off.
817 if (MI->getNumTokens() == 0) {
Chris Lattner2ada5d32006-07-15 07:51:24 +0000818 // No need for arg info.
Chris Lattnerc1410dc2006-07-26 05:22:49 +0000819 if (Args) Args->destroy();
Chris Lattner78186052006-07-09 00:45:31 +0000820
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000821 // Ignore this macro use, just return the next token in the current
822 // buffer.
823 bool HadLeadingSpace = Identifier.hasLeadingSpace();
824 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
825
826 Lex(Identifier);
827
828 // If the identifier isn't on some OTHER line, inherit the leading
829 // whitespace/first-on-a-line property of this token. This handles
830 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
831 // empty.
832 if (!Identifier.isAtStartOfLine()) {
Chris Lattner146762e2007-07-20 16:59:19 +0000833 if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
834 if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000835 }
836 ++NumFastMacroExpanded;
Chris Lattner78186052006-07-09 00:45:31 +0000837 return false;
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000838
Chris Lattner3ce1d1a2006-07-09 01:00:18 +0000839 } else if (MI->getNumTokens() == 1 &&
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000840 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
841 *this)){
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000842 // Otherwise, if this macro expands into a single trivially-expanded
843 // token: expand it now. This handles common cases like
844 // "#define VAL 42".
845
846 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
847 // identifier to the expanded token.
848 bool isAtStartOfLine = Identifier.isAtStartOfLine();
849 bool hasLeadingSpace = Identifier.hasLeadingSpace();
850
851 // Remember where the token is instantiated.
852 SourceLocation InstantiateLoc = Identifier.getLocation();
853
854 // Replace the result token.
855 Identifier = MI->getReplacementToken(0);
856
857 // Restore the StartOfLine/LeadingSpace markers.
Chris Lattner146762e2007-07-20 16:59:19 +0000858 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
859 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000860
861 // Update the tokens location to include both its logical and physical
862 // locations.
863 SourceLocation Loc =
Chris Lattnerc673f902006-06-30 06:10:41 +0000864 SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc);
Chris Lattner8c204872006-10-14 05:19:21 +0000865 Identifier.setLocation(Loc);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000866
Chris Lattner6e4bf522006-07-27 06:59:25 +0000867 // If this is #define X X, we must mark the result as unexpandible.
868 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo())
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000869 if (getMacroInfo(NewII) == MI)
Chris Lattner146762e2007-07-20 16:59:19 +0000870 Identifier.setFlag(Token::DisableExpand);
Chris Lattner6e4bf522006-07-27 06:59:25 +0000871
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000872 // Since this is not an identifier token, it can't be macro expanded, so
873 // we're done.
874 ++NumFastMacroExpanded;
Chris Lattner78186052006-07-09 00:45:31 +0000875 return false;
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000876 }
877
Chris Lattner78186052006-07-09 00:45:31 +0000878 // Start expanding the macro.
Chris Lattneree8760b2006-07-15 07:42:55 +0000879 EnterMacro(Identifier, Args);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000880
881 // Now that the macro is at the top of the include stack, ask the
882 // preprocessor to read the next token from it.
Chris Lattner78186052006-07-09 00:45:31 +0000883 Lex(Identifier);
884 return false;
885}
886
Chris Lattneree8760b2006-07-15 07:42:55 +0000887/// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
Chris Lattner2ada5d32006-07-15 07:51:24 +0000888/// invoked to read all of the actual arguments specified for the macro
Chris Lattner78186052006-07-09 00:45:31 +0000889/// invocation. This returns null on error.
Chris Lattner146762e2007-07-20 16:59:19 +0000890MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
Chris Lattneree8760b2006-07-15 07:42:55 +0000891 MacroInfo *MI) {
Chris Lattner78186052006-07-09 00:45:31 +0000892 // The number of fixed arguments to parse.
893 unsigned NumFixedArgsLeft = MI->getNumArgs();
894 bool isVariadic = MI->isVariadic();
895
Chris Lattner78186052006-07-09 00:45:31 +0000896 // Outer loop, while there are more arguments, keep reading them.
Chris Lattner146762e2007-07-20 16:59:19 +0000897 Token Tok;
Chris Lattner8c204872006-10-14 05:19:21 +0000898 Tok.setKind(tok::comma);
Chris Lattner78186052006-07-09 00:45:31 +0000899 --NumFixedArgsLeft; // Start reading the first arg.
Chris Lattner36b6e812006-07-21 06:38:30 +0000900
901 // ArgTokens - Build up a list of tokens that make up each argument. Each
Chris Lattner7a4af3b2006-07-26 06:26:52 +0000902 // argument is separated by an EOF token. Use a SmallVector so we can avoid
903 // heap allocations in the common case.
Chris Lattner146762e2007-07-20 16:59:19 +0000904 llvm::SmallVector<Token, 64> ArgTokens;
Chris Lattner36b6e812006-07-21 06:38:30 +0000905
906 unsigned NumActuals = 0;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000907 while (Tok.is(tok::comma)) {
Chris Lattner24dbee72007-07-19 16:11:58 +0000908 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
909 // that we already consumed the first one.
Chris Lattner78186052006-07-09 00:45:31 +0000910 unsigned NumParens = 0;
Chris Lattner36b6e812006-07-21 06:38:30 +0000911
Chris Lattner78186052006-07-09 00:45:31 +0000912 while (1) {
Chris Lattnerafe603f2006-07-11 04:02:46 +0000913 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
914 // an argument value in a macro could expand to ',' or '(' or ')'.
Chris Lattner78186052006-07-09 00:45:31 +0000915 LexUnexpandedToken(Tok);
916
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000917 if (Tok.is(tok::eof)) {
Chris Lattner78186052006-07-09 00:45:31 +0000918 Diag(MacroName, diag::err_unterm_macro_invoc);
919 // Do not lose the EOF. Return it to the client.
920 MacroName = Tok;
921 return 0;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000922 } else if (Tok.is(tok::r_paren)) {
Chris Lattner78186052006-07-09 00:45:31 +0000923 // If we found the ) token, the macro arg list is done.
924 if (NumParens-- == 0)
925 break;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000926 } else if (Tok.is(tok::l_paren)) {
Chris Lattner78186052006-07-09 00:45:31 +0000927 ++NumParens;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000928 } else if (Tok.is(tok::comma) && NumParens == 0) {
Chris Lattner78186052006-07-09 00:45:31 +0000929 // Comma ends this argument if there are more fixed arguments expected.
930 if (NumFixedArgsLeft)
931 break;
932
Chris Lattner2ada5d32006-07-15 07:51:24 +0000933 // If this is not a variadic macro, too many args were specified.
Chris Lattner78186052006-07-09 00:45:31 +0000934 if (!isVariadic) {
935 // Emit the diagnostic at the macro name in case there is a missing ).
936 // Emitting it at the , could be far away from the macro name.
Chris Lattner2ada5d32006-07-15 07:51:24 +0000937 Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
Chris Lattner78186052006-07-09 00:45:31 +0000938 return 0;
939 }
940 // Otherwise, continue to add the tokens to this variable argument.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000941 } else if (Tok.is(tok::comment) && !KeepMacroComments) {
Chris Lattner457fc152006-07-29 06:30:25 +0000942 // If this is a comment token in the argument list and we're just in
943 // -C mode (not -CC mode), discard the comment.
944 continue;
Chris Lattner78186052006-07-09 00:45:31 +0000945 }
946
947 ArgTokens.push_back(Tok);
948 }
949
Chris Lattnera12dd152006-07-11 04:09:02 +0000950 // Empty arguments are standard in C99 and supported as an extension in
951 // other modes.
952 if (ArgTokens.empty() && !Features.C99)
953 Diag(Tok, diag::ext_empty_fnmacro_arg);
Chris Lattnerafe603f2006-07-11 04:02:46 +0000954
Chris Lattner36b6e812006-07-21 06:38:30 +0000955 // Add a marker EOF token to the end of the token list for this argument.
Chris Lattner146762e2007-07-20 16:59:19 +0000956 Token EOFTok;
Chris Lattner8c204872006-10-14 05:19:21 +0000957 EOFTok.startToken();
958 EOFTok.setKind(tok::eof);
959 EOFTok.setLocation(Tok.getLocation());
960 EOFTok.setLength(0);
Chris Lattner36b6e812006-07-21 06:38:30 +0000961 ArgTokens.push_back(EOFTok);
962 ++NumActuals;
Chris Lattner78186052006-07-09 00:45:31 +0000963 --NumFixedArgsLeft;
964 };
965
966 // Okay, we either found the r_paren. Check to see if we parsed too few
967 // arguments.
Chris Lattner78186052006-07-09 00:45:31 +0000968 unsigned MinArgsExpected = MI->getNumArgs();
969
Chris Lattner775d8322006-07-29 04:39:41 +0000970 // See MacroArgs instance var for description of this.
971 bool isVarargsElided = false;
972
Chris Lattner2ada5d32006-07-15 07:51:24 +0000973 if (NumActuals < MinArgsExpected) {
Chris Lattner78186052006-07-09 00:45:31 +0000974 // There are several cases where too few arguments is ok, handle them now.
Chris Lattner2ada5d32006-07-15 07:51:24 +0000975 if (NumActuals+1 == MinArgsExpected && MI->isVariadic()) {
Chris Lattner78186052006-07-09 00:45:31 +0000976 // Varargs where the named vararg parameter is missing: ok as extension.
977 // #define A(x, ...)
978 // A("blah")
979 Diag(Tok, diag::ext_missing_varargs_arg);
Chris Lattner775d8322006-07-29 04:39:41 +0000980
981 // Remember this occurred if this is a C99 macro invocation with at least
982 // one actual argument.
Chris Lattner95a06b32006-07-30 08:40:43 +0000983 isVarargsElided = MI->isC99Varargs() && MI->getNumArgs() > 1;
Chris Lattner78186052006-07-09 00:45:31 +0000984 } else if (MI->getNumArgs() == 1) {
985 // #define A(x)
986 // A()
Chris Lattnere7a51302006-07-29 01:25:12 +0000987 // is ok because it is an empty argument.
Chris Lattnera12dd152006-07-11 04:09:02 +0000988
989 // Empty arguments are standard in C99 and supported as an extension in
990 // other modes.
991 if (ArgTokens.empty() && !Features.C99)
992 Diag(Tok, diag::ext_empty_fnmacro_arg);
Chris Lattner78186052006-07-09 00:45:31 +0000993 } else {
994 // Otherwise, emit the error.
Chris Lattner2ada5d32006-07-15 07:51:24 +0000995 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
Chris Lattner78186052006-07-09 00:45:31 +0000996 return 0;
997 }
Chris Lattnere7a51302006-07-29 01:25:12 +0000998
999 // Add a marker EOF token to the end of the token list for this argument.
1000 SourceLocation EndLoc = Tok.getLocation();
Chris Lattner8c204872006-10-14 05:19:21 +00001001 Tok.startToken();
1002 Tok.setKind(tok::eof);
1003 Tok.setLocation(EndLoc);
1004 Tok.setLength(0);
Chris Lattnere7a51302006-07-29 01:25:12 +00001005 ArgTokens.push_back(Tok);
Chris Lattner78186052006-07-09 00:45:31 +00001006 }
1007
Chris Lattner775d8322006-07-29 04:39:41 +00001008 return MacroArgs::create(MI, &ArgTokens[0], ArgTokens.size(),isVarargsElided);
Chris Lattnerf373a4a2006-06-26 06:16:29 +00001009}
1010
Chris Lattnerc673f902006-06-30 06:10:41 +00001011/// ComputeDATE_TIME - Compute the current time, enter it into the specified
1012/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
1013/// the identifier tokens inserted.
1014static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001015 Preprocessor &PP) {
Chris Lattnerc673f902006-06-30 06:10:41 +00001016 time_t TT = time(0);
1017 struct tm *TM = localtime(&TT);
1018
1019 static const char * const Months[] = {
1020 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
1021 };
1022
1023 char TmpBuffer[100];
1024 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
1025 TM->tm_year+1900);
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001026 DATELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
Chris Lattnerc673f902006-06-30 06:10:41 +00001027
1028 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001029 TIMELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
Chris Lattnerc673f902006-06-30 06:10:41 +00001030}
1031
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001032/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1033/// as a builtin macro, handle it and return the next token as 'Tok'.
Chris Lattner146762e2007-07-20 16:59:19 +00001034void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001035 // Figure out which token this is.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001036 IdentifierInfo *II = Tok.getIdentifierInfo();
1037 assert(II && "Can't be a macro without id info!");
Chris Lattner69772b02006-07-02 20:34:39 +00001038
1039 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
1040 // lex the token after it.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001041 if (II == Ident_Pragma)
Chris Lattner69772b02006-07-02 20:34:39 +00001042 return Handle_Pragma(Tok);
1043
Chris Lattner78186052006-07-09 00:45:31 +00001044 ++NumBuiltinMacroExpanded;
1045
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001046 char TmpBuffer[100];
Chris Lattner69772b02006-07-02 20:34:39 +00001047
1048 // Set up the return result.
Chris Lattner8c204872006-10-14 05:19:21 +00001049 Tok.setIdentifierInfo(0);
Chris Lattner146762e2007-07-20 16:59:19 +00001050 Tok.clearFlag(Token::NeedsCleaning);
Chris Lattner630b33c2006-07-01 22:46:53 +00001051
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001052 if (II == Ident__LINE__) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001053 // __LINE__ expands to a simple numeric value.
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001054 sprintf(TmpBuffer, "%u", SourceMgr.getLogicalLineNumber(Tok.getLocation()));
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001055 unsigned Length = strlen(TmpBuffer);
Chris Lattner8c204872006-10-14 05:19:21 +00001056 Tok.setKind(tok::numeric_constant);
1057 Tok.setLength(Length);
1058 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001059 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +00001060 SourceLocation Loc = Tok.getLocation();
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001061 if (II == Ident__BASE_FILE__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +00001062 Diag(Tok, diag::ext_pp_base_file);
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001063 SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc);
1064 while (NextLoc.isValid()) {
Chris Lattnerc1283b92006-07-01 23:16:30 +00001065 Loc = NextLoc;
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001066 NextLoc = SourceMgr.getIncludeLoc(Loc);
Chris Lattnerc1283b92006-07-01 23:16:30 +00001067 }
1068 }
1069
Chris Lattner0766e592006-07-03 01:07:01 +00001070 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001071 std::string FN = SourceMgr.getSourceName(SourceMgr.getLogicalLoc(Loc));
Chris Lattnerecc39e92006-07-15 05:23:31 +00001072 FN = '"' + Lexer::Stringify(FN) + '"';
Chris Lattner8c204872006-10-14 05:19:21 +00001073 Tok.setKind(tok::string_literal);
1074 Tok.setLength(FN.size());
1075 Tok.setLocation(CreateString(&FN[0], FN.size(), Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001076 } else if (II == Ident__DATE__) {
Chris Lattnerc673f902006-06-30 06:10:41 +00001077 if (!DATELoc.isValid())
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001078 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
Chris Lattner8c204872006-10-14 05:19:21 +00001079 Tok.setKind(tok::string_literal);
1080 Tok.setLength(strlen("\"Mmm dd yyyy\""));
1081 Tok.setLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001082 } else if (II == Ident__TIME__) {
Chris Lattnerc673f902006-06-30 06:10:41 +00001083 if (!TIMELoc.isValid())
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001084 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
Chris Lattner8c204872006-10-14 05:19:21 +00001085 Tok.setKind(tok::string_literal);
1086 Tok.setLength(strlen("\"hh:mm:ss\""));
1087 Tok.setLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001088 } else if (II == Ident__INCLUDE_LEVEL__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +00001089 Diag(Tok, diag::ext_pp_include_level);
1090
1091 // Compute the include depth of this token.
1092 unsigned Depth = 0;
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001093 SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation());
1094 for (; Loc.isValid(); ++Depth)
1095 Loc = SourceMgr.getIncludeLoc(Loc);
Chris Lattnerc1283b92006-07-01 23:16:30 +00001096
1097 // __INCLUDE_LEVEL__ expands to a simple numeric value.
1098 sprintf(TmpBuffer, "%u", Depth);
1099 unsigned Length = strlen(TmpBuffer);
Chris Lattner8c204872006-10-14 05:19:21 +00001100 Tok.setKind(tok::numeric_constant);
1101 Tok.setLength(Length);
1102 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001103 } else if (II == Ident__TIMESTAMP__) {
Chris Lattner847e0e42006-07-01 23:49:16 +00001104 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
1105 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1106 Diag(Tok, diag::ext_pp_timestamp);
1107
1108 // Get the file that we are lexing out of. If we're currently lexing from
1109 // a macro, dig into the include stack.
1110 const FileEntry *CurFile = 0;
Chris Lattnerecfeafe2006-07-02 21:26:45 +00001111 Lexer *TheLexer = getCurrentFileLexer();
Chris Lattner847e0e42006-07-01 23:49:16 +00001112
1113 if (TheLexer)
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001114 CurFile = SourceMgr.getFileEntryForLoc(TheLexer->getFileLoc());
Chris Lattner847e0e42006-07-01 23:49:16 +00001115
1116 // If this file is older than the file it depends on, emit a diagnostic.
1117 const char *Result;
1118 if (CurFile) {
1119 time_t TT = CurFile->getModificationTime();
1120 struct tm *TM = localtime(&TT);
1121 Result = asctime(TM);
1122 } else {
1123 Result = "??? ??? ?? ??:??:?? ????\n";
1124 }
1125 TmpBuffer[0] = '"';
1126 strcpy(TmpBuffer+1, Result);
1127 unsigned Len = strlen(TmpBuffer);
1128 TmpBuffer[Len-1] = '"'; // Replace the newline with a quote.
Chris Lattner8c204872006-10-14 05:19:21 +00001129 Tok.setKind(tok::string_literal);
1130 Tok.setLength(Len);
1131 Tok.setLocation(CreateString(TmpBuffer, Len, Tok.getLocation()));
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001132 } else {
1133 assert(0 && "Unknown identifier!");
1134 }
1135}
Chris Lattner677757a2006-06-28 05:26:32 +00001136
1137//===----------------------------------------------------------------------===//
1138// Lexer Event Handling.
1139//===----------------------------------------------------------------------===//
1140
Chris Lattnercefc7682006-07-08 08:28:12 +00001141/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
1142/// identifier information for the token and install it into the token.
Chris Lattner146762e2007-07-20 16:59:19 +00001143IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
Chris Lattnercefc7682006-07-08 08:28:12 +00001144 const char *BufPtr) {
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001145 assert(Identifier.is(tok::identifier) && "Not an identifier!");
Chris Lattnercefc7682006-07-08 08:28:12 +00001146 assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
1147
1148 // Look up this token, see if it is a macro, or if it is a language keyword.
1149 IdentifierInfo *II;
1150 if (BufPtr && !Identifier.needsCleaning()) {
1151 // No cleaning needed, just use the characters from the lexed buffer.
1152 II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
1153 } else {
1154 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
Chris Lattnerf9aba2c2007-07-13 17:10:38 +00001155 llvm::SmallVector<char, 64> IdentifierBuffer;
1156 IdentifierBuffer.resize(Identifier.getLength());
1157 const char *TmpBuf = &IdentifierBuffer[0];
Chris Lattnercefc7682006-07-08 08:28:12 +00001158 unsigned Size = getSpelling(Identifier, TmpBuf);
1159 II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
1160 }
Chris Lattner8c204872006-10-14 05:19:21 +00001161 Identifier.setIdentifierInfo(II);
Chris Lattnercefc7682006-07-08 08:28:12 +00001162 return II;
1163}
1164
1165
Chris Lattner677757a2006-06-28 05:26:32 +00001166/// HandleIdentifier - This callback is invoked when the lexer reads an
1167/// identifier. This callback looks up the identifier in the map and/or
1168/// potentially macro expands it or turns it into a named token (like 'for').
Chris Lattner146762e2007-07-20 16:59:19 +00001169void Preprocessor::HandleIdentifier(Token &Identifier) {
Chris Lattner0f1f5052006-07-20 04:16:23 +00001170 assert(Identifier.getIdentifierInfo() &&
1171 "Can't handle identifiers without identifier info!");
1172
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001173 IdentifierInfo &II = *Identifier.getIdentifierInfo();
Chris Lattner677757a2006-06-28 05:26:32 +00001174
1175 // If this identifier was poisoned, and if it was not produced from a macro
1176 // expansion, emit an error.
Chris Lattner8ff71992006-07-06 05:17:39 +00001177 if (II.isPoisoned() && CurLexer) {
1178 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
1179 Diag(Identifier, diag::err_pp_used_poisoned_id);
1180 else
1181 Diag(Identifier, diag::ext_pp_bad_vaargs_use);
1182 }
Chris Lattner677757a2006-06-28 05:26:32 +00001183
Chris Lattner78186052006-07-09 00:45:31 +00001184 // If this is a macro to be expanded, do it.
Chris Lattnerc43ddc82007-10-07 08:44:20 +00001185 if (MacroInfo *MI = getMacroInfo(&II)) {
Chris Lattner6e4bf522006-07-27 06:59:25 +00001186 if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
1187 if (MI->isEnabled()) {
1188 if (!HandleMacroExpandedIdentifier(Identifier, MI))
1189 return;
1190 } else {
1191 // C99 6.10.3.4p2 says that a disabled macro may never again be
1192 // expanded, even if it's in a context where it could be expanded in the
1193 // future.
Chris Lattner146762e2007-07-20 16:59:19 +00001194 Identifier.setFlag(Token::DisableExpand);
Chris Lattner6e4bf522006-07-27 06:59:25 +00001195 }
1196 }
Chris Lattner063400e2006-10-14 19:54:15 +00001197 } else if (II.isOtherTargetMacro() && !DisableMacroExpansion) {
1198 // If this identifier is a macro on some other target, emit a diagnostic.
1199 // This diagnosic is only emitted when macro expansion is enabled, because
1200 // the macro would not have been expanded for the other target either.
1201 II.setIsOtherTargetMacro(false); // Don't warn on second use.
1202 getTargetInfo().DiagnoseNonPortability(Identifier.getLocation(),
1203 diag::port_target_macro_use);
1204
1205 }
Chris Lattner677757a2006-06-28 05:26:32 +00001206
Chris Lattner5b9f4892006-11-21 17:23:33 +00001207 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
1208 // then we act as if it is the actual operator and not the textual
1209 // representation of it.
1210 if (II.isCPlusPlusOperatorKeyword())
1211 Identifier.setIdentifierInfo(0);
1212
Chris Lattner677757a2006-06-28 05:26:32 +00001213 // Change the kind of this identifier to the appropriate token kind, e.g.
1214 // turning "for" into a keyword.
Chris Lattner8c204872006-10-14 05:19:21 +00001215 Identifier.setKind(II.getTokenID());
Chris Lattner677757a2006-06-28 05:26:32 +00001216
1217 // If this is an extension token, diagnose its use.
Steve Naroffa8fd9732007-06-11 00:35:03 +00001218 // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99
1219 // For now, I'm just commenting it out (while I work on attributes).
Chris Lattner53621a52007-06-13 20:44:40 +00001220 if (II.isExtensionToken() && Features.C99)
1221 Diag(Identifier, diag::ext_token_used);
Chris Lattner677757a2006-06-28 05:26:32 +00001222}
1223
Chris Lattner22eb9722006-06-18 05:43:12 +00001224/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
1225/// the current file. This either returns the EOF token or pops a level off
1226/// the include stack and keeps going.
Chris Lattner146762e2007-07-20 16:59:19 +00001227bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001228 assert(!CurMacroExpander &&
1229 "Ending a file when currently in a macro!");
1230
Chris Lattner371ac8a2006-07-04 07:11:10 +00001231 // See if this file had a controlling macro.
Chris Lattner3665f162006-07-04 07:26:10 +00001232 if (CurLexer) { // Not ending a macro, ignore it.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001233 if (const IdentifierInfo *ControllingMacro =
Chris Lattner371ac8a2006-07-04 07:11:10 +00001234 CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
Chris Lattner3665f162006-07-04 07:26:10 +00001235 // Okay, this has a controlling macro, remember in PerFileInfo.
1236 if (const FileEntry *FE =
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001237 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001238 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
Chris Lattner371ac8a2006-07-04 07:11:10 +00001239 }
1240 }
1241
Chris Lattner22eb9722006-06-18 05:43:12 +00001242 // If this is a #include'd file, pop it off the include stack and continue
1243 // lexing the #includer file.
Chris Lattner69772b02006-07-02 20:34:39 +00001244 if (!IncludeMacroStack.empty()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001245 // We're done with the #included file.
Chris Lattner7667d0d2006-07-16 18:16:58 +00001246 RemoveTopOfLexerStack();
Chris Lattner0c885f52006-06-21 06:50:18 +00001247
1248 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +00001249 if (Callbacks && !isEndOfMacro && CurLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +00001250 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
1251
1252 // Get the file entry for the current file.
1253 if (const FileEntry *FE =
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001254 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001255 FileType = HeaderInfo.getFileDirFlavor(FE);
Chris Lattnerc8997182006-06-22 05:52:16 +00001256
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +00001257 Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr),
1258 PPCallbacks::ExitFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +00001259 }
Chris Lattner2183a6e2006-07-18 06:36:12 +00001260
1261 // Client should lex another token.
1262 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001263 }
1264
Chris Lattner8c204872006-10-14 05:19:21 +00001265 Result.startToken();
Chris Lattnerd01e2912006-06-18 16:22:51 +00001266 CurLexer->BufferPtr = CurLexer->BufferEnd;
1267 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner8c204872006-10-14 05:19:21 +00001268 Result.setKind(tok::eof);
Chris Lattner22eb9722006-06-18 05:43:12 +00001269
1270 // We're done with the #included file.
1271 delete CurLexer;
1272 CurLexer = 0;
Chris Lattner13044d92006-07-03 05:16:44 +00001273
Chris Lattner03f83482006-07-10 06:16:26 +00001274 // This is the end of the top-level file. If the diag::pp_macro_not_used
Chris Lattnerc43ddc82007-10-07 08:44:20 +00001275 // diagnostic is enabled, look for macros that have not been used.
Chris Lattnerb055f2d2007-02-11 08:19:57 +00001276 if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){
Chris Lattnerc43ddc82007-10-07 08:44:20 +00001277 for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
1278 Macros.begin(), E = Macros.end(); I != E; ++I) {
1279 if (!I->second->isUsed())
1280 Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattnerb055f2d2007-02-11 08:19:57 +00001281 }
1282 }
Chris Lattner2183a6e2006-07-18 06:36:12 +00001283 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001284}
1285
1286/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
Chris Lattner7667d0d2006-07-16 18:16:58 +00001287/// the current macro expansion or token stream expansion.
Chris Lattner146762e2007-07-20 16:59:19 +00001288bool Preprocessor::HandleEndOfMacro(Token &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001289 assert(CurMacroExpander && !CurLexer &&
1290 "Ending a macro when currently in a #include file!");
1291
Chris Lattnerc02c4ab2007-07-15 00:25:26 +00001292 // Delete or cache the now-dead macro expander.
1293 if (NumCachedMacroExpanders == MacroExpanderCacheSize)
1294 delete CurMacroExpander;
1295 else
1296 MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander;
Chris Lattner22eb9722006-06-18 05:43:12 +00001297
Chris Lattner69772b02006-07-02 20:34:39 +00001298 // Handle this like a #include file being popped off the stack.
1299 CurMacroExpander = 0;
1300 return HandleEndOfFile(Result, true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001301}
1302
1303
1304//===----------------------------------------------------------------------===//
1305// Utility Methods for Preprocessor Directive Handling.
1306//===----------------------------------------------------------------------===//
1307
1308/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
1309/// current line until the tok::eom token is found.
Chris Lattnercb283342006-06-18 06:48:37 +00001310void Preprocessor::DiscardUntilEndOfDirective() {
Chris Lattner146762e2007-07-20 16:59:19 +00001311 Token Tmp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001312 do {
Chris Lattnercb283342006-06-18 06:48:37 +00001313 LexUnexpandedToken(Tmp);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001314 } while (Tmp.isNot(tok::eom));
Chris Lattner22eb9722006-06-18 05:43:12 +00001315}
1316
Chris Lattner652c1692006-11-21 23:47:30 +00001317/// isCXXNamedOperator - Returns "true" if the token is a named operator in C++.
1318static bool isCXXNamedOperator(const std::string &Spelling) {
1319 return Spelling == "and" || Spelling == "bitand" || Spelling == "bitor" ||
1320 Spelling == "compl" || Spelling == "not" || Spelling == "not_eq" ||
1321 Spelling == "or" || Spelling == "xor";
1322}
1323
Chris Lattner22eb9722006-06-18 05:43:12 +00001324/// ReadMacroName - Lex and validate a macro name, which occurs after a
1325/// #define or #undef. This sets the token kind to eom and discards the rest
Chris Lattnere8eef322006-07-08 07:01:00 +00001326/// of the macro line if the macro name is invalid. isDefineUndef is 1 if
1327/// this is due to a a #define, 2 if #undef directive, 0 if it is something
Chris Lattner44f8a662006-07-03 01:27:27 +00001328/// else (e.g. #ifdef).
Chris Lattner146762e2007-07-20 16:59:19 +00001329void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001330 // Read the token, don't allow macro expansion on it.
Chris Lattnercb283342006-06-18 06:48:37 +00001331 LexUnexpandedToken(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001332
1333 // Missing macro name?
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001334 if (MacroNameTok.is(tok::eom))
Chris Lattner22eb9722006-06-18 05:43:12 +00001335 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
1336
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001337 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1338 if (II == 0) {
Chris Lattner652c1692006-11-21 23:47:30 +00001339 std::string Spelling = getSpelling(MacroNameTok);
1340 if (isCXXNamedOperator(Spelling))
1341 // C++ 2.5p2: Alternative tokens behave the same as its primary token
1342 // except for their spellings.
1343 Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name, Spelling);
1344 else
1345 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +00001346 // Fall through on error.
Chris Lattner2bb8a952006-11-21 22:24:17 +00001347 } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
Chris Lattner44f8a662006-07-03 01:27:27 +00001348 // Error if defining "defined": C99 6.10.8.4.
Chris Lattneraaf09112006-07-03 01:17:59 +00001349 Diag(MacroNameTok, diag::err_defined_macro_name);
Chris Lattner259716a2007-10-07 08:04:56 +00001350 } else if (isDefineUndef && II->hasMacroDefinition() &&
Chris Lattnerc43ddc82007-10-07 08:44:20 +00001351 getMacroInfo(II)->isBuiltinMacro()) {
Chris Lattner44f8a662006-07-03 01:27:27 +00001352 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
Chris Lattnere8eef322006-07-08 07:01:00 +00001353 if (isDefineUndef == 1)
1354 Diag(MacroNameTok, diag::pp_redef_builtin_macro);
1355 else
1356 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
Chris Lattner22eb9722006-06-18 05:43:12 +00001357 } else {
1358 // Okay, we got a good identifier node. Return it.
Chris Lattnercb283342006-06-18 06:48:37 +00001359 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001360 }
1361
Chris Lattner22eb9722006-06-18 05:43:12 +00001362 // Invalid macro name, read and discard the rest of the line. Then set the
1363 // token kind to tok::eom.
Chris Lattner8c204872006-10-14 05:19:21 +00001364 MacroNameTok.setKind(tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +00001365 return DiscardUntilEndOfDirective();
1366}
1367
1368/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
1369/// not, emit a diagnostic and consume up until the eom.
Chris Lattnercb283342006-06-18 06:48:37 +00001370void Preprocessor::CheckEndOfDirective(const char *DirType) {
Chris Lattner146762e2007-07-20 16:59:19 +00001371 Token Tmp;
Chris Lattnercb283342006-06-18 06:48:37 +00001372 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001373 // There should be no tokens after the directive, but we allow them as an
1374 // extension.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001375 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
Chris Lattnerbcb416b2006-10-27 05:43:50 +00001376 Lex(Tmp);
1377
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001378 if (Tmp.isNot(tok::eom)) {
Chris Lattnercb283342006-06-18 06:48:37 +00001379 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
1380 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001381 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001382}
1383
1384
1385
1386/// SkipExcludedConditionalBlock - We just read a #if or related directive and
1387/// decided that the subsequent tokens are in the #if'd out portion of the
1388/// file. Lex the rest of the file, until we see an #endif. If
1389/// FoundNonSkipPortion is true, then we have already emitted code for part of
1390/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
1391/// is true, then #else directives are ok, if not, then we have already seen one
1392/// so a #else directive is a duplicate. When this returns, the caller can lex
1393/// the first valid token.
Chris Lattnerd01e2912006-06-18 16:22:51 +00001394void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
Chris Lattner22eb9722006-06-18 05:43:12 +00001395 bool FoundNonSkipPortion,
1396 bool FoundElse) {
1397 ++NumSkipped;
Chris Lattner69772b02006-07-02 20:34:39 +00001398 assert(CurMacroExpander == 0 && CurLexer &&
Chris Lattner22eb9722006-06-18 05:43:12 +00001399 "Lexing a macro, not a file?");
1400
1401 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
1402 FoundNonSkipPortion, FoundElse);
1403
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001404 // Enter raw mode to disable identifier lookup (and thus macro expansion),
1405 // disabling warnings, etc.
1406 CurLexer->LexingRawMode = true;
Chris Lattner146762e2007-07-20 16:59:19 +00001407 Token Tok;
Chris Lattner22eb9722006-06-18 05:43:12 +00001408 while (1) {
Chris Lattnercb283342006-06-18 06:48:37 +00001409 CurLexer->Lex(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001410
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001411 // If this is the end of the buffer, we have an error.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001412 if (Tok.is(tok::eof)) {
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001413 // Emit errors for each unterminated conditional on the stack, including
1414 // the current one.
1415 while (!CurLexer->ConditionalStack.empty()) {
1416 Diag(CurLexer->ConditionalStack.back().IfLoc,
1417 diag::err_pp_unterminated_conditional);
1418 CurLexer->ConditionalStack.pop_back();
1419 }
1420
1421 // Just return and let the caller lex after this #include.
1422 break;
1423 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001424
1425 // If this token is not a preprocessor directive, just skip it.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001426 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
Chris Lattner22eb9722006-06-18 05:43:12 +00001427 continue;
1428
1429 // We just parsed a # character at the start of a line, so we're in
1430 // directive mode. Tell the lexer this so any newlines we see will be
1431 // converted into an EOM token (this terminates the macro).
1432 CurLexer->ParsingPreprocessorDirective = true;
Chris Lattner457fc152006-07-29 06:30:25 +00001433 CurLexer->KeepCommentMode = false;
1434
Chris Lattner22eb9722006-06-18 05:43:12 +00001435
1436 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +00001437 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001438
1439 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
1440 // something bogus), skip it.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001441 if (Tok.isNot(tok::identifier)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001442 CurLexer->ParsingPreprocessorDirective = false;
Chris Lattner457fc152006-07-29 06:30:25 +00001443 // Restore comment saving mode.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001444 CurLexer->KeepCommentMode = KeepComments;
Chris Lattner22eb9722006-06-18 05:43:12 +00001445 continue;
1446 }
Chris Lattnere60165f2006-06-22 06:36:29 +00001447
Chris Lattner22eb9722006-06-18 05:43:12 +00001448 // If the first letter isn't i or e, it isn't intesting to us. We know that
1449 // this is safe in the face of spelling differences, because there is no way
1450 // to spell an i/e in a strange way that is another letter. Skipping this
Chris Lattnere60165f2006-06-22 06:36:29 +00001451 // allows us to avoid looking up the identifier info for #define/#undef and
1452 // other common directives.
1453 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
1454 char FirstChar = RawCharData[0];
Chris Lattner22eb9722006-06-18 05:43:12 +00001455 if (FirstChar >= 'a' && FirstChar <= 'z' &&
1456 FirstChar != 'i' && FirstChar != 'e') {
1457 CurLexer->ParsingPreprocessorDirective = false;
Chris Lattner457fc152006-07-29 06:30:25 +00001458 // Restore comment saving mode.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001459 CurLexer->KeepCommentMode = KeepComments;
Chris Lattner22eb9722006-06-18 05:43:12 +00001460 continue;
1461 }
1462
Chris Lattnere60165f2006-06-22 06:36:29 +00001463 // Get the identifier name without trigraphs or embedded newlines. Note
1464 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
1465 // when skipping.
1466 // TODO: could do this with zero copies in the no-clean case by using
1467 // strncmp below.
1468 char Directive[20];
1469 unsigned IdLen;
1470 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
1471 IdLen = Tok.getLength();
1472 memcpy(Directive, RawCharData, IdLen);
1473 Directive[IdLen] = 0;
1474 } else {
1475 std::string DirectiveStr = getSpelling(Tok);
1476 IdLen = DirectiveStr.size();
1477 if (IdLen >= 20) {
1478 CurLexer->ParsingPreprocessorDirective = false;
Chris Lattner457fc152006-07-29 06:30:25 +00001479 // Restore comment saving mode.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001480 CurLexer->KeepCommentMode = KeepComments;
Chris Lattnere60165f2006-06-22 06:36:29 +00001481 continue;
1482 }
1483 memcpy(Directive, &DirectiveStr[0], IdLen);
1484 Directive[IdLen] = 0;
1485 }
1486
Chris Lattner22eb9722006-06-18 05:43:12 +00001487 if (FirstChar == 'i' && Directive[1] == 'f') {
Chris Lattnere60165f2006-06-22 06:36:29 +00001488 if ((IdLen == 2) || // "if"
1489 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
1490 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
Chris Lattner22eb9722006-06-18 05:43:12 +00001491 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
1492 // bother parsing the condition.
Chris Lattnercb283342006-06-18 06:48:37 +00001493 DiscardUntilEndOfDirective();
Chris Lattner50b497e2006-06-18 16:32:35 +00001494 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerd01e2912006-06-18 16:22:51 +00001495 /*foundnonskip*/false,
1496 /*fnddelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001497 }
1498 } else if (FirstChar == 'e') {
Chris Lattnere60165f2006-06-22 06:36:29 +00001499 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
Chris Lattnercb283342006-06-18 06:48:37 +00001500 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00001501 PPConditionalInfo CondInfo;
1502 CondInfo.WasSkipping = true; // Silence bogus warning.
1503 bool InCond = CurLexer->popConditionalLevel(CondInfo);
Chris Lattnercf6bc662006-11-05 07:59:08 +00001504 InCond = InCond; // Silence warning in no-asserts mode.
Chris Lattner22eb9722006-06-18 05:43:12 +00001505 assert(!InCond && "Can't be skipping if not in a conditional!");
1506
1507 // If we popped the outermost skipping block, we're done skipping!
1508 if (!CondInfo.WasSkipping)
1509 break;
Chris Lattnere60165f2006-06-22 06:36:29 +00001510 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
Chris Lattner22eb9722006-06-18 05:43:12 +00001511 // #else directive in a skipping conditional. If not in some other
1512 // skipping conditional, and if #else hasn't already been seen, enter it
1513 // as a non-skipping conditional.
Chris Lattnercb283342006-06-18 06:48:37 +00001514 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00001515 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1516
1517 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001518 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001519
1520 // Note that we've seen a #else in this conditional.
1521 CondInfo.FoundElse = true;
1522
1523 // If the conditional is at the top level, and the #if block wasn't
1524 // entered, enter the #else block now.
1525 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
1526 CondInfo.FoundNonSkip = true;
1527 break;
1528 }
Chris Lattnere60165f2006-06-22 06:36:29 +00001529 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
Chris Lattner22eb9722006-06-18 05:43:12 +00001530 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1531
1532 bool ShouldEnter;
1533 // If this is in a skipping block or if we're already handled this #if
1534 // block, don't bother parsing the condition.
1535 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
Chris Lattnercb283342006-06-18 06:48:37 +00001536 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001537 ShouldEnter = false;
1538 } else {
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001539 // Restore the value of LexingRawMode so that identifiers are
Chris Lattner22eb9722006-06-18 05:43:12 +00001540 // looked up, etc, inside the #elif expression.
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001541 assert(CurLexer->LexingRawMode && "We have to be skipping here!");
1542 CurLexer->LexingRawMode = false;
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001543 IdentifierInfo *IfNDefMacro = 0;
Chris Lattnera8654ca2006-07-04 17:42:08 +00001544 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001545 CurLexer->LexingRawMode = true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001546 }
1547
1548 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001549 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001550
1551 // If this condition is true, enter it!
1552 if (ShouldEnter) {
1553 CondInfo.FoundNonSkip = true;
1554 break;
1555 }
1556 }
1557 }
1558
1559 CurLexer->ParsingPreprocessorDirective = false;
Chris Lattner457fc152006-07-29 06:30:25 +00001560 // Restore comment saving mode.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001561 CurLexer->KeepCommentMode = KeepComments;
Chris Lattner22eb9722006-06-18 05:43:12 +00001562 }
1563
1564 // Finally, if we are out of the conditional (saw an #endif or ran off the end
1565 // of the file, just stop skipping and return to lexing whatever came after
1566 // the #if block.
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001567 CurLexer->LexingRawMode = false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001568}
1569
1570//===----------------------------------------------------------------------===//
1571// Preprocessor Directive Handling.
1572//===----------------------------------------------------------------------===//
1573
1574/// HandleDirective - This callback is invoked when the lexer sees a # token
1575/// at the start of a line. This consumes the directive, modifies the
1576/// lexer/preprocessor state, and advances the lexer(s) so that the next token
1577/// read is the correct one.
Chris Lattner146762e2007-07-20 16:59:19 +00001578void Preprocessor::HandleDirective(Token &Result) {
Chris Lattner4d5e1a72006-07-03 01:01:29 +00001579 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
Chris Lattner22eb9722006-06-18 05:43:12 +00001580
1581 // We just parsed a # character at the start of a line, so we're in directive
1582 // mode. Tell the lexer this so any newlines we see will be converted into an
Chris Lattner78186052006-07-09 00:45:31 +00001583 // EOM token (which terminates the directive).
Chris Lattner22eb9722006-06-18 05:43:12 +00001584 CurLexer->ParsingPreprocessorDirective = true;
1585
1586 ++NumDirectives;
1587
Chris Lattner371ac8a2006-07-04 07:11:10 +00001588 // We are about to read a token. For the multiple-include optimization FA to
1589 // work, we have to remember if we had read any tokens *before* this
1590 // pp-directive.
1591 bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal();
1592
Chris Lattner78186052006-07-09 00:45:31 +00001593 // Read the next token, the directive flavor. This isn't expanded due to
1594 // C99 6.10.3p8.
Chris Lattnercb283342006-06-18 06:48:37 +00001595 LexUnexpandedToken(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001596
Chris Lattner78186052006-07-09 00:45:31 +00001597 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
1598 // #define A(x) #x
1599 // A(abc
1600 // #warning blah
1601 // def)
1602 // If so, the user is relying on non-portable behavior, emit a diagnostic.
Chris Lattneree8760b2006-07-15 07:42:55 +00001603 if (InMacroArgs)
Chris Lattner78186052006-07-09 00:45:31 +00001604 Diag(Result, diag::ext_embedded_directive);
1605
Chris Lattnerbcb416b2006-10-27 05:43:50 +00001606TryAgain:
Chris Lattner22eb9722006-06-18 05:43:12 +00001607 switch (Result.getKind()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001608 case tok::eom:
Chris Lattnercb283342006-06-18 06:48:37 +00001609 return; // null directive.
Chris Lattnerbcb416b2006-10-27 05:43:50 +00001610 case tok::comment:
1611 // Handle stuff like "# /*foo*/ define X" in -E -C mode.
1612 LexUnexpandedToken(Result);
1613 goto TryAgain;
Chris Lattner22eb9722006-06-18 05:43:12 +00001614
Chris Lattner22eb9722006-06-18 05:43:12 +00001615 case tok::numeric_constant:
1616 // FIXME: implement # 7 line numbers!
Chris Lattner6e5b2a02006-10-17 02:53:32 +00001617 DiscardUntilEndOfDirective();
1618 return;
Chris Lattner87d3bec2006-10-17 03:44:32 +00001619 default:
1620 IdentifierInfo *II = Result.getIdentifierInfo();
1621 if (II == 0) break; // Not an identifier.
1622
1623 // Ask what the preprocessor keyword ID is.
1624 switch (II->getPPKeywordID()) {
1625 default: break;
1626 // C99 6.10.1 - Conditional Inclusion.
1627 case tok::pp_if:
1628 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
1629 case tok::pp_ifdef:
1630 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
1631 case tok::pp_ifndef:
1632 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
1633 case tok::pp_elif:
1634 return HandleElifDirective(Result);
1635 case tok::pp_else:
1636 return HandleElseDirective(Result);
1637 case tok::pp_endif:
1638 return HandleEndifDirective(Result);
1639
1640 // C99 6.10.2 - Source File Inclusion.
1641 case tok::pp_include:
1642 return HandleIncludeDirective(Result); // Handle #include.
1643
1644 // C99 6.10.3 - Macro Replacement.
1645 case tok::pp_define:
1646 return HandleDefineDirective(Result, false);
1647 case tok::pp_undef:
1648 return HandleUndefDirective(Result);
1649
1650 // C99 6.10.4 - Line Control.
1651 case tok::pp_line:
1652 // FIXME: implement #line
1653 DiscardUntilEndOfDirective();
1654 return;
1655
1656 // C99 6.10.5 - Error Directive.
1657 case tok::pp_error:
1658 return HandleUserDiagnosticDirective(Result, false);
1659
1660 // C99 6.10.6 - Pragma Directive.
1661 case tok::pp_pragma:
1662 return HandlePragmaDirective();
1663
1664 // GNU Extensions.
1665 case tok::pp_import:
1666 return HandleImportDirective(Result);
1667 case tok::pp_include_next:
1668 return HandleIncludeNextDirective(Result);
1669
1670 case tok::pp_warning:
1671 Diag(Result, diag::ext_pp_warning_directive);
1672 return HandleUserDiagnosticDirective(Result, true);
1673 case tok::pp_ident:
1674 return HandleIdentSCCSDirective(Result);
1675 case tok::pp_sccs:
1676 return HandleIdentSCCSDirective(Result);
1677 case tok::pp_assert:
1678 //isExtension = true; // FIXME: implement #assert
Chris Lattner22eb9722006-06-18 05:43:12 +00001679 break;
Chris Lattner87d3bec2006-10-17 03:44:32 +00001680 case tok::pp_unassert:
1681 //isExtension = true; // FIXME: implement #unassert
Chris Lattner22eb9722006-06-18 05:43:12 +00001682 break;
Chris Lattner87d3bec2006-10-17 03:44:32 +00001683
1684 // clang extensions.
1685 case tok::pp_define_target:
1686 return HandleDefineDirective(Result, true);
1687 case tok::pp_define_other_target:
1688 return HandleDefineOtherTargetDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001689 }
1690 break;
1691 }
1692
1693 // If we reached here, the preprocessing token is not valid!
Chris Lattnercb283342006-06-18 06:48:37 +00001694 Diag(Result, diag::err_pp_invalid_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001695
1696 // Read the rest of the PP line.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001697 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001698
1699 // Okay, we're done parsing the directive.
Chris Lattner22eb9722006-06-18 05:43:12 +00001700}
1701
Chris Lattner146762e2007-07-20 16:59:19 +00001702void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001703 bool isWarning) {
1704 // Read the rest of the line raw. We do this because we don't want macros
1705 // to be expanded and we don't require that the tokens be valid preprocessing
1706 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1707 // collapse multiple consequtive white space between tokens, but this isn't
1708 // specified by the standard.
1709 std::string Message = CurLexer->ReadToEndOfLine();
1710
1711 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
Chris Lattner01d66cc2006-07-03 22:16:27 +00001712 return Diag(Tok, DiagID, Message);
1713}
1714
1715/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1716///
Chris Lattner146762e2007-07-20 16:59:19 +00001717void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001718 // Yes, this directive is an extension.
Chris Lattner01d66cc2006-07-03 22:16:27 +00001719 Diag(Tok, diag::ext_pp_ident_directive);
1720
Chris Lattner371ac8a2006-07-04 07:11:10 +00001721 // Read the string argument.
Chris Lattner146762e2007-07-20 16:59:19 +00001722 Token StrTok;
Chris Lattner01d66cc2006-07-03 22:16:27 +00001723 Lex(StrTok);
1724
1725 // If the token kind isn't a string, it's a malformed directive.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001726 if (StrTok.isNot(tok::string_literal) &&
1727 StrTok.isNot(tok::wide_string_literal))
Chris Lattner01d66cc2006-07-03 22:16:27 +00001728 return Diag(StrTok, diag::err_pp_malformed_ident);
1729
1730 // Verify that there is nothing after the string, other than EOM.
1731 CheckEndOfDirective("#ident");
1732
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +00001733 if (Callbacks)
1734 Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok));
Chris Lattner22eb9722006-06-18 05:43:12 +00001735}
1736
Chris Lattnerb8761832006-06-24 21:31:03 +00001737//===----------------------------------------------------------------------===//
1738// Preprocessor Include Directive Handling.
1739//===----------------------------------------------------------------------===//
1740
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001741/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1742/// checked and spelled filename, e.g. as an operand of #include. This returns
1743/// true if the input filename was in <>'s or false if it were in ""'s. The
1744/// caller is expected to provide a buffer that is large enough to hold the
1745/// spelling of the filename, but is also expected to handle the case when
1746/// this method decides to use a different buffer.
Chris Lattner93ab9f12007-07-23 04:15:27 +00001747bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001748 const char *&BufStart,
1749 const char *&BufEnd) {
1750 // Get the text form of the filename.
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001751 assert(BufStart != BufEnd && "Can't have tokens with empty spellings!");
1752
1753 // Make sure the filename is <x> or "x".
1754 bool isAngled;
1755 if (BufStart[0] == '<') {
1756 if (BufEnd[-1] != '>') {
Chris Lattner93ab9f12007-07-23 04:15:27 +00001757 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001758 BufStart = 0;
1759 return true;
1760 }
1761 isAngled = true;
1762 } else if (BufStart[0] == '"') {
1763 if (BufEnd[-1] != '"') {
Chris Lattner93ab9f12007-07-23 04:15:27 +00001764 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001765 BufStart = 0;
1766 return true;
1767 }
1768 isAngled = false;
1769 } else {
Chris Lattner93ab9f12007-07-23 04:15:27 +00001770 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001771 BufStart = 0;
1772 return true;
1773 }
1774
1775 // Diagnose #include "" as invalid.
1776 if (BufEnd-BufStart <= 2) {
Chris Lattner93ab9f12007-07-23 04:15:27 +00001777 Diag(Loc, diag::err_pp_empty_filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001778 BufStart = 0;
1779 return "";
1780 }
1781
1782 // Skip the brackets.
1783 ++BufStart;
1784 --BufEnd;
1785 return isAngled;
1786}
1787
Chris Lattner43eafb42007-07-23 04:56:47 +00001788/// ConcatenateIncludeName - Handle cases where the #include name is expanded
1789/// from a macro as multiple tokens, which need to be glued together. This
1790/// occurs for code like:
1791/// #define FOO <a/b.h>
1792/// #include FOO
1793/// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1794///
1795/// This code concatenates and consumes tokens up to the '>' token. It returns
1796/// false if the > was found, otherwise it returns true if it finds and consumes
1797/// the EOM marker.
1798static bool ConcatenateIncludeName(llvm::SmallVector<char, 128> &FilenameBuffer,
1799 Preprocessor &PP) {
1800 Token CurTok;
1801
1802 PP.Lex(CurTok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001803 while (CurTok.isNot(tok::eom)) {
Chris Lattner43eafb42007-07-23 04:56:47 +00001804 // Append the spelling of this token to the buffer. If there was a space
1805 // before it, add it now.
1806 if (CurTok.hasLeadingSpace())
1807 FilenameBuffer.push_back(' ');
1808
1809 // Get the spelling of the token, directly into FilenameBuffer if possible.
1810 unsigned PreAppendSize = FilenameBuffer.size();
1811 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1812
1813 const char *BufPtr = &FilenameBuffer[PreAppendSize];
1814 unsigned ActualLen = PP.getSpelling(CurTok, BufPtr);
1815
1816 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1817 if (BufPtr != &FilenameBuffer[PreAppendSize])
1818 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1819
1820 // Resize FilenameBuffer to the correct size.
1821 if (CurTok.getLength() != ActualLen)
1822 FilenameBuffer.resize(PreAppendSize+ActualLen);
1823
1824 // If we found the '>' marker, return success.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001825 if (CurTok.is(tok::greater))
Chris Lattner43eafb42007-07-23 04:56:47 +00001826 return false;
1827
1828 PP.Lex(CurTok);
1829 }
1830
1831 // If we hit the eom marker, emit an error and return true so that the caller
1832 // knows the EOM has been read.
1833 PP.Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1834 return true;
1835}
1836
Chris Lattner22eb9722006-06-18 05:43:12 +00001837/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1838/// file to be included from the lexer, then include it! This is a common
1839/// routine with functionality shared between #include, #include_next and
1840/// #import.
Chris Lattner146762e2007-07-20 16:59:19 +00001841void Preprocessor::HandleIncludeDirective(Token &IncludeTok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001842 const DirectoryLookup *LookupFrom,
1843 bool isImport) {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001844
Chris Lattner146762e2007-07-20 16:59:19 +00001845 Token FilenameTok;
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001846 CurLexer->LexIncludeFilename(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001847
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001848 // Reserve a buffer to get the spelling.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001849 llvm::SmallVector<char, 128> FilenameBuffer;
Chris Lattner43eafb42007-07-23 04:56:47 +00001850 const char *FilenameStart, *FilenameEnd;
1851
1852 switch (FilenameTok.getKind()) {
1853 case tok::eom:
1854 // If the token kind is EOM, the error has already been diagnosed.
1855 return;
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001856
Chris Lattner43eafb42007-07-23 04:56:47 +00001857 case tok::angle_string_literal:
Chris Lattnerf97dbcb2007-07-23 22:23:52 +00001858 case tok::string_literal: {
Chris Lattner43eafb42007-07-23 04:56:47 +00001859 FilenameBuffer.resize(FilenameTok.getLength());
1860 FilenameStart = &FilenameBuffer[0];
1861 unsigned Len = getSpelling(FilenameTok, FilenameStart);
1862 FilenameEnd = FilenameStart+Len;
1863 break;
Chris Lattnerf97dbcb2007-07-23 22:23:52 +00001864 }
Chris Lattner43eafb42007-07-23 04:56:47 +00001865
1866 case tok::less:
1867 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1868 // case, glue the tokens together into FilenameBuffer and interpret those.
1869 FilenameBuffer.push_back('<');
1870 if (ConcatenateIncludeName(FilenameBuffer, *this))
1871 return; // Found <eom> but no ">"? Diagnostic already emitted.
1872 FilenameStart = &FilenameBuffer[0];
1873 FilenameEnd = &FilenameBuffer[FilenameBuffer.size()];
1874 break;
1875 default:
1876 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1877 DiscardUntilEndOfDirective();
1878 return;
1879 }
1880
Chris Lattner93ab9f12007-07-23 04:15:27 +00001881 bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(),
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001882 FilenameStart, FilenameEnd);
1883 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1884 // error.
Chris Lattner43eafb42007-07-23 04:56:47 +00001885 if (FilenameStart == 0) {
1886 DiscardUntilEndOfDirective();
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001887 return;
Chris Lattner43eafb42007-07-23 04:56:47 +00001888 }
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001889
Chris Lattner269c2322006-06-25 06:23:00 +00001890 // Verify that there is nothing after the filename, other than EOM. Use the
1891 // preprocessor to lex this in case lexing the filename entered a macro.
1892 CheckEndOfDirective("#include");
Chris Lattner22eb9722006-06-18 05:43:12 +00001893
1894 // Check that we don't have infinite #include recursion.
Chris Lattner69772b02006-07-02 20:34:39 +00001895 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
Chris Lattner22eb9722006-06-18 05:43:12 +00001896 return Diag(FilenameTok, diag::err_pp_include_too_deep);
1897
Chris Lattner22eb9722006-06-18 05:43:12 +00001898 // Search include directories.
Chris Lattnerc8997182006-06-22 05:52:16 +00001899 const DirectoryLookup *CurDir;
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001900 const FileEntry *File = LookupFile(FilenameStart, FilenameEnd,
Chris Lattnerb8b94f12006-10-30 05:38:06 +00001901 isAngled, LookupFrom, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001902 if (File == 0)
Chris Lattner7c718bd2007-04-10 06:02:46 +00001903 return Diag(FilenameTok, diag::err_pp_file_not_found,
1904 std::string(FilenameStart, FilenameEnd));
Chris Lattner22eb9722006-06-18 05:43:12 +00001905
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001906 // Ask HeaderInfo if we should enter this #include file.
1907 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
1908 // If it returns true, #including this file will have no effect.
Chris Lattner3665f162006-07-04 07:26:10 +00001909 return;
1910 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001911
1912 // Look up the file, create a File ID for it.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001913 unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001914 if (FileID == 0)
Chris Lattner7c718bd2007-04-10 06:02:46 +00001915 return Diag(FilenameTok, diag::err_pp_file_not_found,
1916 std::string(FilenameStart, FilenameEnd));
Chris Lattner22eb9722006-06-18 05:43:12 +00001917
1918 // Finally, if all is good, enter the new file!
Chris Lattnerc8997182006-06-22 05:52:16 +00001919 EnterSourceFile(FileID, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001920}
1921
1922/// HandleIncludeNextDirective - Implements #include_next.
1923///
Chris Lattner146762e2007-07-20 16:59:19 +00001924void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) {
Chris Lattnercb283342006-06-18 06:48:37 +00001925 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001926
1927 // #include_next is like #include, except that we start searching after
1928 // the current found directory. If we can't do this, issue a
1929 // diagnostic.
Chris Lattnerc8997182006-06-22 05:52:16 +00001930 const DirectoryLookup *Lookup = CurDirLookup;
Chris Lattner69772b02006-07-02 20:34:39 +00001931 if (isInPrimaryFile()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001932 Lookup = 0;
Chris Lattnercb283342006-06-18 06:48:37 +00001933 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
Chris Lattner22eb9722006-06-18 05:43:12 +00001934 } else if (Lookup == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +00001935 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
Chris Lattnerc8997182006-06-22 05:52:16 +00001936 } else {
1937 // Start looking up in the next directory.
1938 ++Lookup;
Chris Lattner22eb9722006-06-18 05:43:12 +00001939 }
1940
1941 return HandleIncludeDirective(IncludeNextTok, Lookup);
1942}
1943
1944/// HandleImportDirective - Implements #import.
1945///
Chris Lattner146762e2007-07-20 16:59:19 +00001946void Preprocessor::HandleImportDirective(Token &ImportTok) {
Chris Lattnercb283342006-06-18 06:48:37 +00001947 Diag(ImportTok, diag::ext_pp_import_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001948
1949 return HandleIncludeDirective(ImportTok, 0, true);
1950}
1951
Chris Lattnerb8761832006-06-24 21:31:03 +00001952//===----------------------------------------------------------------------===//
1953// Preprocessor Macro Directive Handling.
1954//===----------------------------------------------------------------------===//
1955
Chris Lattnercefc7682006-07-08 08:28:12 +00001956/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1957/// definition has just been read. Lex the rest of the arguments and the
1958/// closing ), updating MI with what we learn. Return true if an error occurs
1959/// parsing the arg list.
1960bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
Chris Lattner564f4782007-07-14 22:46:43 +00001961 llvm::SmallVector<IdentifierInfo*, 32> Arguments;
1962
Chris Lattner146762e2007-07-20 16:59:19 +00001963 Token Tok;
Chris Lattnercefc7682006-07-08 08:28:12 +00001964 while (1) {
1965 LexUnexpandedToken(Tok);
1966 switch (Tok.getKind()) {
1967 case tok::r_paren:
1968 // Found the end of the argument list.
Chris Lattner564f4782007-07-14 22:46:43 +00001969 if (Arguments.empty()) { // #define FOO()
1970 MI->setArgumentList(Arguments.begin(), Arguments.end());
1971 return false;
1972 }
Chris Lattnercefc7682006-07-08 08:28:12 +00001973 // Otherwise we have #define FOO(A,)
1974 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1975 return true;
1976 case tok::ellipsis: // #define X(... -> C99 varargs
1977 // Warn if use of C99 feature in non-C99 mode.
1978 if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
1979
1980 // Lex the token after the identifier.
1981 LexUnexpandedToken(Tok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001982 if (Tok.isNot(tok::r_paren)) {
Chris Lattnercefc7682006-07-08 08:28:12 +00001983 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1984 return true;
1985 }
Chris Lattner95a06b32006-07-30 08:40:43 +00001986 // Add the __VA_ARGS__ identifier as an argument.
Chris Lattner564f4782007-07-14 22:46:43 +00001987 Arguments.push_back(Ident__VA_ARGS__);
Chris Lattnercefc7682006-07-08 08:28:12 +00001988 MI->setIsC99Varargs();
Chris Lattner564f4782007-07-14 22:46:43 +00001989 MI->setArgumentList(Arguments.begin(), Arguments.end());
Chris Lattnercefc7682006-07-08 08:28:12 +00001990 return false;
1991 case tok::eom: // #define X(
1992 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1993 return true;
Chris Lattner62aa0d42006-10-20 05:08:24 +00001994 default:
1995 // Handle keywords and identifiers here to accept things like
1996 // #define Foo(for) for.
Chris Lattner6e0d42c2006-07-08 20:32:52 +00001997 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner62aa0d42006-10-20 05:08:24 +00001998 if (II == 0) {
1999 // #define X(1
2000 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
2001 return true;
2002 }
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002003
2004 // If this is already used as an argument, it is used multiple times (e.g.
2005 // #define X(A,A.
Chris Lattner564f4782007-07-14 22:46:43 +00002006 if (std::find(Arguments.begin(), Arguments.end(), II) !=
2007 Arguments.end()) { // C99 6.10.3p6
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002008 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName());
2009 return true;
2010 }
2011
2012 // Add the argument to the macro info.
Chris Lattner564f4782007-07-14 22:46:43 +00002013 Arguments.push_back(II);
Chris Lattnercefc7682006-07-08 08:28:12 +00002014
2015 // Lex the token after the identifier.
2016 LexUnexpandedToken(Tok);
2017
2018 switch (Tok.getKind()) {
2019 default: // #define X(A B
2020 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
2021 return true;
2022 case tok::r_paren: // #define X(A)
Chris Lattner564f4782007-07-14 22:46:43 +00002023 MI->setArgumentList(Arguments.begin(), Arguments.end());
Chris Lattnercefc7682006-07-08 08:28:12 +00002024 return false;
2025 case tok::comma: // #define X(A,
2026 break;
2027 case tok::ellipsis: // #define X(A... -> GCC extension
2028 // Diagnose extension.
2029 Diag(Tok, diag::ext_named_variadic_macro);
2030
2031 // Lex the token after the identifier.
2032 LexUnexpandedToken(Tok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002033 if (Tok.isNot(tok::r_paren)) {
Chris Lattnercefc7682006-07-08 08:28:12 +00002034 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2035 return true;
2036 }
2037
2038 MI->setIsGNUVarargs();
Chris Lattner564f4782007-07-14 22:46:43 +00002039 MI->setArgumentList(Arguments.begin(), Arguments.end());
Chris Lattnercefc7682006-07-08 08:28:12 +00002040 return false;
2041 }
2042 }
2043 }
2044}
2045
Chris Lattner22eb9722006-06-18 05:43:12 +00002046/// HandleDefineDirective - Implements #define. This consumes the entire macro
Chris Lattner81278c62006-10-14 19:03:49 +00002047/// line then lets the caller lex the next real token. If 'isTargetSpecific' is
2048/// true, then this is a "#define_target", otherwise this is a "#define".
Chris Lattner22eb9722006-06-18 05:43:12 +00002049///
Chris Lattner146762e2007-07-20 16:59:19 +00002050void Preprocessor::HandleDefineDirective(Token &DefineTok,
Chris Lattner81278c62006-10-14 19:03:49 +00002051 bool isTargetSpecific) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002052 ++NumDefined;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002053
Chris Lattner146762e2007-07-20 16:59:19 +00002054 Token MacroNameTok;
Chris Lattnere8eef322006-07-08 07:01:00 +00002055 ReadMacroName(MacroNameTok, 1);
Chris Lattner22eb9722006-06-18 05:43:12 +00002056
2057 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002058 if (MacroNameTok.is(tok::eom))
Chris Lattnercb283342006-06-18 06:48:37 +00002059 return;
Chris Lattnerf40fe992007-07-14 22:11:41 +00002060
Chris Lattner457fc152006-07-29 06:30:25 +00002061 // If we are supposed to keep comments in #defines, reenable comment saving
2062 // mode.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00002063 CurLexer->KeepCommentMode = KeepMacroComments;
Chris Lattner457fc152006-07-29 06:30:25 +00002064
Chris Lattner063400e2006-10-14 19:54:15 +00002065 // Create the new macro.
Chris Lattner50b497e2006-06-18 16:32:35 +00002066 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
Chris Lattner81278c62006-10-14 19:03:49 +00002067 if (isTargetSpecific) MI->setIsTargetSpecific();
Chris Lattner22eb9722006-06-18 05:43:12 +00002068
Chris Lattner063400e2006-10-14 19:54:15 +00002069 // If the identifier is an 'other target' macro, clear this bit.
2070 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
2071
2072
Chris Lattner146762e2007-07-20 16:59:19 +00002073 Token Tok;
Chris Lattnercb283342006-06-18 06:48:37 +00002074 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00002075
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002076 // If this is a function-like macro definition, parse the argument list,
2077 // marking each of the identifiers as being used as macro arguments. Also,
2078 // check other constraints on the first token of the macro body.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002079 if (Tok.is(tok::eom)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002080 // If there is no body to this macro, we have no special handling here.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002081 } else if (Tok.is(tok::l_paren) && !Tok.hasLeadingSpace()) {
Chris Lattnercefc7682006-07-08 08:28:12 +00002082 // This is a function-like macro definition. Read the argument list.
2083 MI->setIsFunctionLike();
2084 if (ReadMacroDefinitionArgList(MI)) {
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002085 // Forget about MI.
Chris Lattnercefc7682006-07-08 08:28:12 +00002086 delete MI;
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002087 // Throw away the rest of the line.
Chris Lattnercefc7682006-07-08 08:28:12 +00002088 if (CurLexer->ParsingPreprocessorDirective)
2089 DiscardUntilEndOfDirective();
2090 return;
2091 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002092
Chris Lattner815a1f92006-07-08 20:48:04 +00002093 // Read the first token after the arg list for down below.
2094 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00002095 } else if (!Tok.hasLeadingSpace()) {
2096 // C99 requires whitespace between the macro definition and the body. Emit
2097 // a diagnostic for something like "#define X+".
2098 if (Features.C99) {
Chris Lattnercb283342006-06-18 06:48:37 +00002099 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattner22eb9722006-06-18 05:43:12 +00002100 } else {
2101 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
2102 // one in some cases!
2103 }
2104 } else {
2105 // This is a normal token with leading space. Clear the leading space
2106 // marker on the first token to get proper expansion.
Chris Lattner146762e2007-07-20 16:59:19 +00002107 Tok.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00002108 }
2109
Chris Lattner7e374832006-07-29 03:46:57 +00002110 // If this is a definition of a variadic C99 function-like macro, not using
2111 // the GNU named varargs extension, enabled __VA_ARGS__.
2112
2113 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
2114 // This gets unpoisoned where it is allowed.
2115 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
2116 if (MI->isC99Varargs())
2117 Ident__VA_ARGS__->setIsPoisoned(false);
2118
Chris Lattner22eb9722006-06-18 05:43:12 +00002119 // Read the rest of the macro body.
Chris Lattnera3834342007-07-14 21:54:03 +00002120 if (MI->isObjectLike()) {
2121 // Object-like macros are very simple, just read their body.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002122 while (Tok.isNot(tok::eom)) {
Chris Lattnera3834342007-07-14 21:54:03 +00002123 MI->AddTokenToBody(Tok);
Chris Lattner815a1f92006-07-08 20:48:04 +00002124 // Get the next token of the macro.
2125 LexUnexpandedToken(Tok);
Chris Lattner815a1f92006-07-08 20:48:04 +00002126 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002127
Chris Lattnera3834342007-07-14 21:54:03 +00002128 } else {
2129 // Otherwise, read the body of a function-like macro. This has to validate
2130 // the # (stringize) operator.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002131 while (Tok.isNot(tok::eom)) {
Chris Lattnera3834342007-07-14 21:54:03 +00002132 MI->AddTokenToBody(Tok);
Chris Lattnerbff18d52006-07-06 04:49:18 +00002133
Chris Lattnera3834342007-07-14 21:54:03 +00002134 // Check C99 6.10.3.2p1: ensure that # operators are followed by macro
2135 // parameters in function-like macro expansions.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002136 if (Tok.isNot(tok::hash)) {
Chris Lattnera3834342007-07-14 21:54:03 +00002137 // Get the next token of the macro.
2138 LexUnexpandedToken(Tok);
2139 continue;
2140 }
2141
2142 // Get the next token of the macro.
2143 LexUnexpandedToken(Tok);
2144
2145 // Not a macro arg identifier?
2146 if (!Tok.getIdentifierInfo() ||
2147 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
2148 Diag(Tok, diag::err_pp_stringize_not_parameter);
2149 delete MI;
2150
2151 // Disable __VA_ARGS__ again.
2152 Ident__VA_ARGS__->setIsPoisoned(true);
2153 return;
2154 }
2155
2156 // Things look ok, add the param name token to the macro.
2157 MI->AddTokenToBody(Tok);
2158
2159 // Get the next token of the macro.
2160 LexUnexpandedToken(Tok);
2161 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002162 }
Chris Lattner7e374832006-07-29 03:46:57 +00002163
Chris Lattnerf40fe992007-07-14 22:11:41 +00002164
Chris Lattner7e374832006-07-29 03:46:57 +00002165 // Disable __VA_ARGS__ again.
2166 Ident__VA_ARGS__->setIsPoisoned(true);
Chris Lattnerbff18d52006-07-06 04:49:18 +00002167
Chris Lattnerbff18d52006-07-06 04:49:18 +00002168 // Check that there is no paste (##) operator at the begining or end of the
2169 // replacement list.
Chris Lattner78186052006-07-09 00:45:31 +00002170 unsigned NumTokens = MI->getNumTokens();
Chris Lattnerbff18d52006-07-06 04:49:18 +00002171 if (NumTokens != 0) {
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002172 if (MI->getReplacementToken(0).is(tok::hashhash)) {
Chris Lattner815a1f92006-07-08 20:48:04 +00002173 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
Chris Lattnerbff18d52006-07-06 04:49:18 +00002174 delete MI;
Chris Lattner815a1f92006-07-08 20:48:04 +00002175 return;
Chris Lattnerbff18d52006-07-06 04:49:18 +00002176 }
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002177 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
Chris Lattner815a1f92006-07-08 20:48:04 +00002178 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
Chris Lattnerbff18d52006-07-06 04:49:18 +00002179 delete MI;
Chris Lattner815a1f92006-07-08 20:48:04 +00002180 return;
Chris Lattnerbff18d52006-07-06 04:49:18 +00002181 }
2182 }
2183
Chris Lattner13044d92006-07-03 05:16:44 +00002184 // If this is the primary source file, remember that this macro hasn't been
2185 // used yet.
2186 if (isInPrimaryFile())
2187 MI->setIsUsed(false);
2188
Chris Lattner22eb9722006-06-18 05:43:12 +00002189 // Finally, if this identifier already had a macro defined for it, verify that
2190 // the macro bodies are identical and free the old definition.
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002191 if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Chris Lattner13044d92006-07-03 05:16:44 +00002192 if (!OtherMI->isUsed())
2193 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2194
Chris Lattner22eb9722006-06-18 05:43:12 +00002195 // Macros must be identical. This means all tokes and whitespace separation
Chris Lattner21284df2006-07-08 07:16:08 +00002196 // must be the same. C99 6.10.3.2.
2197 if (!MI->isIdenticalTo(*OtherMI, *this)) {
Chris Lattnere8eef322006-07-08 07:01:00 +00002198 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef,
2199 MacroNameTok.getIdentifierInfo()->getName());
2200 Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2);
2201 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002202 delete OtherMI;
2203 }
2204
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002205 setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
Chris Lattner22eb9722006-06-18 05:43:12 +00002206}
2207
Chris Lattner063400e2006-10-14 19:54:15 +00002208/// HandleDefineOtherTargetDirective - Implements #define_other_target.
Chris Lattner146762e2007-07-20 16:59:19 +00002209void Preprocessor::HandleDefineOtherTargetDirective(Token &Tok) {
2210 Token MacroNameTok;
Chris Lattner063400e2006-10-14 19:54:15 +00002211 ReadMacroName(MacroNameTok, 1);
2212
2213 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002214 if (MacroNameTok.is(tok::eom))
Chris Lattner063400e2006-10-14 19:54:15 +00002215 return;
2216
2217 // Check to see if this is the last token on the #undef line.
2218 CheckEndOfDirective("#define_other_target");
2219
2220 // If there is already a macro defined by this name, turn it into a
2221 // target-specific define.
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002222 if (MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Chris Lattner063400e2006-10-14 19:54:15 +00002223 MI->setIsTargetSpecific(true);
2224 return;
2225 }
2226
2227 // Mark the identifier as being a macro on some other target.
2228 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro();
2229}
2230
Chris Lattner22eb9722006-06-18 05:43:12 +00002231
2232/// HandleUndefDirective - Implements #undef.
2233///
Chris Lattner146762e2007-07-20 16:59:19 +00002234void Preprocessor::HandleUndefDirective(Token &UndefTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002235 ++NumUndefined;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002236
Chris Lattner146762e2007-07-20 16:59:19 +00002237 Token MacroNameTok;
Chris Lattnere8eef322006-07-08 07:01:00 +00002238 ReadMacroName(MacroNameTok, 2);
Chris Lattner22eb9722006-06-18 05:43:12 +00002239
2240 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002241 if (MacroNameTok.is(tok::eom))
Chris Lattnercb283342006-06-18 06:48:37 +00002242 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00002243
2244 // Check to see if this is the last token on the #undef line.
Chris Lattnercb283342006-06-18 06:48:37 +00002245 CheckEndOfDirective("#undef");
Chris Lattner22eb9722006-06-18 05:43:12 +00002246
2247 // Okay, we finally have a valid identifier to undef.
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002248 MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
Chris Lattner22eb9722006-06-18 05:43:12 +00002249
Chris Lattner063400e2006-10-14 19:54:15 +00002250 // #undef untaints an identifier if it were marked by define_other_target.
2251 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
2252
Chris Lattner22eb9722006-06-18 05:43:12 +00002253 // If the macro is not defined, this is a noop undef, just return.
Chris Lattnercb283342006-06-18 06:48:37 +00002254 if (MI == 0) return;
Chris Lattner677757a2006-06-28 05:26:32 +00002255
Chris Lattner13044d92006-07-03 05:16:44 +00002256 if (!MI->isUsed())
2257 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattner22eb9722006-06-18 05:43:12 +00002258
2259 // Free macro definition.
2260 delete MI;
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002261 setMacroInfo(MacroNameTok.getIdentifierInfo(), 0);
Chris Lattner22eb9722006-06-18 05:43:12 +00002262}
2263
2264
Chris Lattnerb8761832006-06-24 21:31:03 +00002265//===----------------------------------------------------------------------===//
2266// Preprocessor Conditional Directive Handling.
2267//===----------------------------------------------------------------------===//
2268
Chris Lattner22eb9722006-06-18 05:43:12 +00002269/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
Chris Lattner371ac8a2006-07-04 07:11:10 +00002270/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
2271/// if any tokens have been returned or pp-directives activated before this
2272/// #ifndef has been lexed.
Chris Lattner22eb9722006-06-18 05:43:12 +00002273///
Chris Lattner146762e2007-07-20 16:59:19 +00002274void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
Chris Lattner371ac8a2006-07-04 07:11:10 +00002275 bool ReadAnyTokensBeforeDirective) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002276 ++NumIf;
Chris Lattner146762e2007-07-20 16:59:19 +00002277 Token DirectiveTok = Result;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002278
Chris Lattner146762e2007-07-20 16:59:19 +00002279 Token MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00002280 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00002281
2282 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002283 if (MacroNameTok.is(tok::eom)) {
Chris Lattnerd05e44e2007-09-24 05:14:57 +00002284 // Skip code until we get to #endif. This helps with recovery by not
2285 // emitting an error when the #endif is reached.
2286 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2287 /*Foundnonskip*/false, /*FoundElse*/false);
Chris Lattnercb283342006-06-18 06:48:37 +00002288 return;
Chris Lattnerd05e44e2007-09-24 05:14:57 +00002289 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002290
2291 // Check to see if this is the last token on the #if[n]def line.
Chris Lattner371ac8a2006-07-04 07:11:10 +00002292 CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
2293
2294 // If the start of a top-level #ifdef, inform MIOpt.
2295 if (!ReadAnyTokensBeforeDirective &&
2296 CurLexer->getConditionalStackDepth() == 0) {
2297 assert(isIfndef && "#ifdef shouldn't reach here");
2298 CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
2299 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002300
Chris Lattner063400e2006-10-14 19:54:15 +00002301 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002302 MacroInfo *MI = getMacroInfo(MII);
Chris Lattnera78a97e2006-07-03 05:42:18 +00002303
Chris Lattner81278c62006-10-14 19:03:49 +00002304 // If there is a macro, process it.
2305 if (MI) {
2306 // Mark it used.
2307 MI->setIsUsed(true);
2308
2309 // If this is the first use of a target-specific macro, warn about it.
2310 if (MI->isTargetSpecific()) {
2311 MI->setIsTargetSpecific(false); // Don't warn on second use.
2312 getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(),
2313 diag::port_target_macro_use);
2314 }
Chris Lattner063400e2006-10-14 19:54:15 +00002315 } else {
2316 // Use of a target-specific macro for some other target? If so, warn.
2317 if (MII->isOtherTargetMacro()) {
2318 MII->setIsOtherTargetMacro(false); // Don't warn on second use.
2319 getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(),
2320 diag::port_target_macro_use);
2321 }
Chris Lattner81278c62006-10-14 19:03:49 +00002322 }
Chris Lattnera78a97e2006-07-03 05:42:18 +00002323
Chris Lattner22eb9722006-06-18 05:43:12 +00002324 // Should we include the stuff contained by this directive?
Chris Lattnera78a97e2006-07-03 05:42:18 +00002325 if (!MI == isIfndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002326 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00002327 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00002328 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00002329 } else {
2330 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00002331 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Chris Lattnercb283342006-06-18 06:48:37 +00002332 /*Foundnonskip*/false,
2333 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00002334 }
2335}
2336
2337/// HandleIfDirective - Implements the #if directive.
2338///
Chris Lattner146762e2007-07-20 16:59:19 +00002339void Preprocessor::HandleIfDirective(Token &IfToken,
Chris Lattnera8654ca2006-07-04 17:42:08 +00002340 bool ReadAnyTokensBeforeDirective) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002341 ++NumIf;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002342
Chris Lattner371ac8a2006-07-04 07:11:10 +00002343 // Parse and evaluation the conditional expression.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00002344 IdentifierInfo *IfNDefMacro = 0;
Chris Lattnera8654ca2006-07-04 17:42:08 +00002345 bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
Chris Lattner22eb9722006-06-18 05:43:12 +00002346
2347 // Should we include the stuff contained by this directive?
2348 if (ConditionalTrue) {
Chris Lattnera8654ca2006-07-04 17:42:08 +00002349 // If this condition is equivalent to #ifndef X, and if this is the first
2350 // directive seen, handle it for the multiple-include optimization.
2351 if (!ReadAnyTokensBeforeDirective &&
2352 CurLexer->getConditionalStackDepth() == 0 && IfNDefMacro)
2353 CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
2354
Chris Lattner22eb9722006-06-18 05:43:12 +00002355 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00002356 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00002357 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00002358 } else {
2359 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00002360 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnercb283342006-06-18 06:48:37 +00002361 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00002362 }
2363}
2364
2365/// HandleEndifDirective - Implements the #endif directive.
2366///
Chris Lattner146762e2007-07-20 16:59:19 +00002367void Preprocessor::HandleEndifDirective(Token &EndifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002368 ++NumEndif;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002369
Chris Lattner22eb9722006-06-18 05:43:12 +00002370 // Check that this is the whole directive.
Chris Lattnercb283342006-06-18 06:48:37 +00002371 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00002372
2373 PPConditionalInfo CondInfo;
2374 if (CurLexer->popConditionalLevel(CondInfo)) {
2375 // No conditionals on the stack: this is an #endif without an #if.
2376 return Diag(EndifToken, diag::err_pp_endif_without_if);
2377 }
2378
Chris Lattner371ac8a2006-07-04 07:11:10 +00002379 // If this the end of a top-level #endif, inform MIOpt.
2380 if (CurLexer->getConditionalStackDepth() == 0)
2381 CurLexer->MIOpt.ExitTopLevelConditional();
2382
Chris Lattner538d7f32006-07-20 04:31:52 +00002383 assert(!CondInfo.WasSkipping && !CurLexer->LexingRawMode &&
Chris Lattner22eb9722006-06-18 05:43:12 +00002384 "This code should only be reachable in the non-skipping case!");
Chris Lattner22eb9722006-06-18 05:43:12 +00002385}
2386
2387
Chris Lattner146762e2007-07-20 16:59:19 +00002388void Preprocessor::HandleElseDirective(Token &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002389 ++NumElse;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002390
Chris Lattner22eb9722006-06-18 05:43:12 +00002391 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnercb283342006-06-18 06:48:37 +00002392 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00002393
2394 PPConditionalInfo CI;
2395 if (CurLexer->popConditionalLevel(CI))
2396 return Diag(Result, diag::pp_err_else_without_if);
Chris Lattner371ac8a2006-07-04 07:11:10 +00002397
2398 // If this is a top-level #else, inform the MIOpt.
2399 if (CurLexer->getConditionalStackDepth() == 0)
2400 CurLexer->MIOpt.FoundTopLevelElse();
Chris Lattner22eb9722006-06-18 05:43:12 +00002401
2402 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00002403 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00002404
2405 // Finally, skip the rest of the contents of this block and return the first
2406 // token after it.
2407 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2408 /*FoundElse*/true);
2409}
2410
Chris Lattner146762e2007-07-20 16:59:19 +00002411void Preprocessor::HandleElifDirective(Token &ElifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002412 ++NumElse;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002413
Chris Lattner22eb9722006-06-18 05:43:12 +00002414 // #elif directive in a non-skipping conditional... start skipping.
2415 // We don't care what the condition is, because we will always skip it (since
2416 // the block immediately before it was included).
Chris Lattnercb283342006-06-18 06:48:37 +00002417 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00002418
2419 PPConditionalInfo CI;
2420 if (CurLexer->popConditionalLevel(CI))
2421 return Diag(ElifToken, diag::pp_err_elif_without_if);
2422
Chris Lattner371ac8a2006-07-04 07:11:10 +00002423 // If this is a top-level #elif, inform the MIOpt.
2424 if (CurLexer->getConditionalStackDepth() == 0)
2425 CurLexer->MIOpt.FoundTopLevelElse();
2426
Chris Lattner22eb9722006-06-18 05:43:12 +00002427 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00002428 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00002429
2430 // Finally, skip the rest of the contents of this block and return the first
2431 // token after it.
2432 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2433 /*FoundElse*/CI.FoundElse);
2434}
Chris Lattnerb8761832006-06-24 21:31:03 +00002435