blob: a73757f1b59e09c75a387e98a276f1f1517cdec7 [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//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
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"
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +000040#include "llvm/Support/Streams.h"
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) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000123 Diags.Report(getFullLoc(Loc), DiagID);
Chris Lattner36982e42007-05-16 17:49:37 +0000124}
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) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000128 Diags.Report(getFullLoc(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 {
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000132 llvm::cerr << tok::getTokenName(Tok.getKind()) << " '"
133 << getSpelling(Tok) << "'";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000134
135 if (!DumpFlags) return;
Chris Lattner615315f2007-12-09 20:31:55 +0000136
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000137 llvm::cerr << "\t";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000138 if (Tok.isAtStartOfLine())
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000139 llvm::cerr << " [StartOfLine]";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000140 if (Tok.hasLeadingSpace())
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000141 llvm::cerr << " [LeadingSpace]";
Chris Lattner6e4bf522006-07-27 06:59:25 +0000142 if (Tok.isExpandDisabled())
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000143 llvm::cerr << " [ExpandDisabled]";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000144 if (Tok.needsCleaning()) {
Chris Lattner50b497e2006-06-18 16:32:35 +0000145 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000146 llvm::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
147 << "']";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000148 }
Chris Lattner615315f2007-12-09 20:31:55 +0000149
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000150 llvm::cerr << "\tLoc=<";
Chris Lattner615315f2007-12-09 20:31:55 +0000151 DumpLocation(Tok.getLocation());
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000152 llvm::cerr << ">";
Chris Lattner615315f2007-12-09 20:31:55 +0000153}
154
155void Preprocessor::DumpLocation(SourceLocation Loc) const {
156 SourceLocation LogLoc = SourceMgr.getLogicalLoc(Loc);
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000157 llvm::cerr << SourceMgr.getSourceName(LogLoc) << ':'
158 << SourceMgr.getLineNumber(LogLoc) << ':'
159 << SourceMgr.getLineNumber(LogLoc);
Chris Lattner615315f2007-12-09 20:31:55 +0000160
161 SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(Loc);
162 if (PhysLoc != LogLoc) {
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000163 llvm::cerr << " <PhysLoc=";
Chris Lattner615315f2007-12-09 20:31:55 +0000164 DumpLocation(PhysLoc);
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000165 llvm::cerr << ">";
Chris Lattner615315f2007-12-09 20:31:55 +0000166 }
Chris Lattnerd01e2912006-06-18 16:22:51 +0000167}
168
169void Preprocessor::DumpMacro(const MacroInfo &MI) const {
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000170 llvm::cerr << "MACRO: ";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000171 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
172 DumpToken(MI.getReplacementToken(i));
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000173 llvm::cerr << " ";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000174 }
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000175 llvm::cerr << "\n";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000176}
177
Chris Lattner22eb9722006-06-18 05:43:12 +0000178void Preprocessor::PrintStats() {
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000179 llvm::cerr << "\n*** Preprocessor Stats:\n";
180 llvm::cerr << NumDirectives << " directives found:\n";
181 llvm::cerr << " " << NumDefined << " #define.\n";
182 llvm::cerr << " " << NumUndefined << " #undef.\n";
183 llvm::cerr << " #include/#include_next/#import:\n";
184 llvm::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
185 llvm::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
186 llvm::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
187 llvm::cerr << " " << NumElse << " #else/#elif.\n";
188 llvm::cerr << " " << NumEndif << " #endif.\n";
189 llvm::cerr << " " << NumPragma << " #pragma.\n";
190 llvm::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000191
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000192 llvm::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
193 << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
194 << NumFastMacroExpanded << " on the fast path.\n";
195 llvm::cerr << (NumFastTokenPaste+NumTokenPaste)
196 << " token paste (##) operations performed, "
197 << NumFastTokenPaste << " on the fast path.\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000198}
199
200//===----------------------------------------------------------------------===//
Chris Lattnerd01e2912006-06-18 16:22:51 +0000201// Token Spelling
202//===----------------------------------------------------------------------===//
203
204
205/// getSpelling() - Return the 'spelling' of this token. The spelling of a
206/// token are the characters used to represent the token in the source file
207/// after trigraph expansion and escaped-newline folding. In particular, this
208/// wants to get the true, uncanonicalized, spelling of things like digraphs
209/// UCNs, etc.
Chris Lattner146762e2007-07-20 16:59:19 +0000210std::string Preprocessor::getSpelling(const Token &Tok) const {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000211 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
212
213 // If this token contains nothing interesting, return it directly.
Chris Lattner50b497e2006-06-18 16:32:35 +0000214 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000215 if (!Tok.needsCleaning())
216 return std::string(TokStart, TokStart+Tok.getLength());
217
Chris Lattnerd01e2912006-06-18 16:22:51 +0000218 std::string Result;
219 Result.reserve(Tok.getLength());
220
Chris Lattneref9eae12006-07-04 22:33:12 +0000221 // Otherwise, hard case, relex the characters into the string.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000222 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
223 Ptr != End; ) {
224 unsigned CharSize;
225 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
226 Ptr += CharSize;
227 }
228 assert(Result.size() != unsigned(Tok.getLength()) &&
229 "NeedsCleaning flag set on something that didn't need cleaning!");
230 return Result;
231}
232
233/// getSpelling - This method is used to get the spelling of a token into a
234/// preallocated buffer, instead of as an std::string. The caller is required
235/// to allocate enough space for the token, which is guaranteed to be at least
236/// Tok.getLength() bytes long. The actual length of the token is returned.
Chris Lattneref9eae12006-07-04 22:33:12 +0000237///
238/// Note that this method may do two possible things: it may either fill in
239/// the buffer specified with characters, or it may *change the input pointer*
240/// to point to a constant buffer with the data already in it (avoiding a
241/// copy). The caller is not allowed to modify the returned buffer pointer
242/// if an internal buffer is returned.
Chris Lattner146762e2007-07-20 16:59:19 +0000243unsigned Preprocessor::getSpelling(const Token &Tok,
Chris Lattneref9eae12006-07-04 22:33:12 +0000244 const char *&Buffer) const {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000245 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
246
Chris Lattnerd3a15f72006-07-04 23:01:03 +0000247 // If this token is an identifier, just return the string from the identifier
248 // table, which is very quick.
249 if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
250 Buffer = II->getName();
Chris Lattner32e6d642007-07-22 22:50:09 +0000251
252 // Return the length of the token. If the token needed cleaning, don't
253 // include the size of the newlines or trigraphs in it.
254 if (!Tok.needsCleaning())
255 return Tok.getLength();
256 else
257 return strlen(Buffer);
Chris Lattnerd3a15f72006-07-04 23:01:03 +0000258 }
259
260 // Otherwise, compute the start of the token in the input lexer buffer.
Chris Lattner50b497e2006-06-18 16:32:35 +0000261 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000262
263 // If this token contains nothing interesting, return it directly.
264 if (!Tok.needsCleaning()) {
Chris Lattneref9eae12006-07-04 22:33:12 +0000265 Buffer = TokStart;
266 return Tok.getLength();
Chris Lattnerd01e2912006-06-18 16:22:51 +0000267 }
268 // Otherwise, hard case, relex the characters into the string.
Chris Lattneref9eae12006-07-04 22:33:12 +0000269 char *OutBuf = const_cast<char*>(Buffer);
Chris Lattnerd01e2912006-06-18 16:22:51 +0000270 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
271 Ptr != End; ) {
272 unsigned CharSize;
273 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
274 Ptr += CharSize;
275 }
276 assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
277 "NeedsCleaning flag set on something that didn't need cleaning!");
278
279 return OutBuf-Buffer;
280}
281
Chris Lattnerb94ec7b2006-07-14 06:54:10 +0000282
283/// CreateString - Plop the specified string into a scratch buffer and return a
284/// location for it. If specified, the source location provides a source
285/// location for the token.
286SourceLocation Preprocessor::
287CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) {
288 if (SLoc.isValid())
289 return ScratchBuf->getToken(Buf, Len, SLoc);
290 return ScratchBuf->getToken(Buf, Len);
291}
292
293
Chris Lattner8a7003c2007-07-16 06:48:38 +0000294/// AdvanceToTokenCharacter - Given a location that specifies the start of a
295/// token, return a new location that specifies a character within the token.
296SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart,
297 unsigned CharNo) {
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000298 // If they request the first char of the token, we're trivially done. If this
299 // is a macro expansion, it doesn't make sense to point to a character within
300 // the instantiation point (the name). We could point to the source
301 // character, but without also pointing to instantiation info, this is
302 // confusing.
303 if (CharNo == 0 || TokStart.isMacroID()) return TokStart;
Chris Lattner8a7003c2007-07-16 06:48:38 +0000304
305 // Figure out how many physical characters away the specified logical
306 // character is. This needs to take into consideration newlines and
307 // trigraphs.
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000308 const char *TokPtr = SourceMgr.getCharacterData(TokStart);
309 unsigned PhysOffset = 0;
Chris Lattner8a7003c2007-07-16 06:48:38 +0000310
311 // The usual case is that tokens don't contain anything interesting. Skip
312 // over the uninteresting characters. If a token only consists of simple
313 // chars, this method is extremely fast.
314 while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr))
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000315 ++TokPtr, --CharNo, ++PhysOffset;
Chris Lattner8a7003c2007-07-16 06:48:38 +0000316
317 // If we have a character that may be a trigraph or escaped newline, create a
318 // lexer to parse it correctly.
Chris Lattner8a7003c2007-07-16 06:48:38 +0000319 if (CharNo != 0) {
320 // Create a lexer starting at this token position.
Chris Lattner77e9de52007-07-20 16:52:03 +0000321 Lexer TheLexer(TokStart, *this, TokPtr);
Chris Lattner146762e2007-07-20 16:59:19 +0000322 Token Tok;
Chris Lattner8a7003c2007-07-16 06:48:38 +0000323 // Skip over characters the remaining characters.
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000324 const char *TokStartPtr = TokPtr;
Chris Lattner8a7003c2007-07-16 06:48:38 +0000325 for (; CharNo; --CharNo)
326 TheLexer.getAndAdvanceChar(TokPtr, Tok);
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000327
328 PhysOffset += TokPtr-TokStartPtr;
Chris Lattner8a7003c2007-07-16 06:48:38 +0000329 }
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000330
331 return TokStart.getFileLocWithOffset(PhysOffset);
Chris Lattner8a7003c2007-07-16 06:48:38 +0000332}
333
334
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000335//===----------------------------------------------------------------------===//
336// Preprocessor Initialization Methods
337//===----------------------------------------------------------------------===//
338
339// Append a #define line to Buf for Macro. Macro should be of the form XXX,
340// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
341// "#define XXX Y z W". To get a #define with no value, use "XXX=".
342static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
343 const char *Command = "#define ") {
344 Buf.insert(Buf.end(), Command, Command+strlen(Command));
345 if (const char *Equal = strchr(Macro, '=')) {
346 // Turn the = into ' '.
347 Buf.insert(Buf.end(), Macro, Equal);
348 Buf.push_back(' ');
349 Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal));
350 } else {
351 // Push "macroname 1".
352 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
353 Buf.push_back(' ');
354 Buf.push_back('1');
355 }
356 Buf.push_back('\n');
357}
358
359
360static void InitializePredefinedMacros(Preprocessor &PP,
361 std::vector<char> &Buf) {
362 // FIXME: Implement magic like cpp_init_builtins for things like __STDC__
363 // and __DATE__ etc.
364#if 0
365 /* __STDC__ has the value 1 under normal circumstances.
366 However, if (a) we are in a system header, (b) the option
367 stdc_0_in_system_headers is true (set by target config), and
368 (c) we are not in strictly conforming mode, then it has the
369 value 0. (b) and (c) are already checked in cpp_init_builtins. */
370 //case BT_STDC:
371 if (cpp_in_system_header (pfile))
372 number = 0;
373 else
374 number = 1;
375 break;
376#endif
377 // These should all be defined in the preprocessor according to the
378 // current language configuration.
379 DefineBuiltinMacro(Buf, "__STDC__=1");
380 //DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
381 if (PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus)
382 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
383 else if (0) // STDC94 ?
384 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
385
386 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");
387 if (PP.getLangOptions().ObjC1)
388 DefineBuiltinMacro(Buf, "__OBJC__=1");
389 if (PP.getLangOptions().ObjC2)
390 DefineBuiltinMacro(Buf, "__OBJC2__=1");
Steve Naroff6d40db02007-10-31 18:42:27 +0000391
Chris Lattnered2a9eb2007-10-10 17:48:53 +0000392 // Add __builtin_va_list typedef.
393 {
394 const char *VAList = PP.getTargetInfo().getVAListDeclaration();
395 Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
396 Buf.push_back('\n');
397 }
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000398
399 // Get the target #defines.
400 PP.getTargetInfo().getTargetDefines(Buf);
401
402 // Compiler set macros.
403 DefineBuiltinMacro(Buf, "__APPLE_CC__=5250");
Steve Naroff68754c52007-11-10 18:06:36 +0000404 DefineBuiltinMacro(Buf, "__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=1050");
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000405 DefineBuiltinMacro(Buf, "__GNUC_MINOR__=0");
406 DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
407 DefineBuiltinMacro(Buf, "__GNUC__=4");
408 DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
409 DefineBuiltinMacro(Buf, "__VERSION__=\"4.0.1 (Apple Computer, Inc. "
410 "build 5250)\"");
411
412 // Build configuration options.
413 DefineBuiltinMacro(Buf, "__DYNAMIC__=1");
414 DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
415 DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
416 DefineBuiltinMacro(Buf, "__PIC__=1");
417
418
419 if (PP.getLangOptions().CPlusPlus) {
420 DefineBuiltinMacro(Buf, "__DEPRECATED=1");
421 DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
422 DefineBuiltinMacro(Buf, "__GNUG__=4");
423 DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
424 DefineBuiltinMacro(Buf, "__cplusplus=1");
425 DefineBuiltinMacro(Buf, "__private_extern__=extern");
426 }
427
428 // FIXME: Should emit a #line directive here.
429}
430
431
432/// EnterMainSourceFile - Enter the specified FileID as the main source file,
Nate Begemanf7c3ff62008-01-07 04:01:26 +0000433/// which implicitly adds the builtin defines etc.
Ted Kremenek230bd912007-12-19 22:51:13 +0000434void Preprocessor::EnterMainSourceFile() {
435
436 unsigned MainFileID = SourceMgr.getMainFileID();
437
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000438 // Enter the main file source buffer.
439 EnterSourceFile(MainFileID, 0);
440
Chris Lattner609d4132007-11-15 19:07:47 +0000441 // Tell the header info that the main file was entered. If the file is later
442 // #imported, it won't be re-entered.
443 if (const FileEntry *FE =
444 SourceMgr.getFileEntryForLoc(SourceLocation::getFileLoc(MainFileID, 0)))
445 HeaderInfo.IncrementIncludeCount(FE);
446
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000447 std::vector<char> PrologFile;
448 PrologFile.reserve(4080);
449
450 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
451 InitializePredefinedMacros(*this, PrologFile);
452
453 // Add on the predefines from the driver.
454 PrologFile.insert(PrologFile.end(), Predefines,Predefines+strlen(Predefines));
455
456 // Memory buffer must end with a null byte!
457 PrologFile.push_back(0);
458
459 // Now that we have emitted the predefined macros, #includes, etc into
460 // PrologFile, preprocess it to populate the initial preprocessor state.
461 llvm::MemoryBuffer *SB =
462 llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(),
463 "<predefines>");
464 assert(SB && "Cannot fail to create predefined source buffer");
465 unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB);
466 assert(FileID && "Could not create FileID for predefines?");
467
468 // Start parsing the predefines.
469 EnterSourceFile(FileID, 0);
470}
Chris Lattner8a7003c2007-07-16 06:48:38 +0000471
Chris Lattnerd01e2912006-06-18 16:22:51 +0000472//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +0000473// Source File Location Methods.
474//===----------------------------------------------------------------------===//
475
Chris Lattner22eb9722006-06-18 05:43:12 +0000476/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
477/// return null on failure. isAngled indicates whether the file reference is
478/// for system #include's or not (i.e. using <> instead of "").
Chris Lattnerb8b94f12006-10-30 05:38:06 +0000479const FileEntry *Preprocessor::LookupFile(const char *FilenameStart,
480 const char *FilenameEnd,
Chris Lattnerc8997182006-06-22 05:52:16 +0000481 bool isAngled,
Chris Lattner22eb9722006-06-18 05:43:12 +0000482 const DirectoryLookup *FromDir,
Chris Lattnerc8997182006-06-22 05:52:16 +0000483 const DirectoryLookup *&CurDir) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000484 // If the header lookup mechanism may be relative to the current file, pass in
485 // info about where the current file is.
486 const FileEntry *CurFileEnt = 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000487 if (!FromDir) {
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000488 SourceLocation FileLoc = getCurrentFileLexer()->getFileLoc();
489 CurFileEnt = SourceMgr.getFileEntryForLoc(FileLoc);
Chris Lattner22eb9722006-06-18 05:43:12 +0000490 }
491
Chris Lattner63dd32b2006-10-20 04:42:40 +0000492 // Do a standard file entry lookup.
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000493 CurDir = CurDirLookup;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000494 const FileEntry *FE =
Chris Lattner7cdbad92006-10-30 05:33:15 +0000495 HeaderInfo.LookupFile(FilenameStart, FilenameEnd,
496 isAngled, FromDir, CurDir, CurFileEnt);
Chris Lattner63dd32b2006-10-20 04:42:40 +0000497 if (FE) return FE;
498
499 // Otherwise, see if this is a subframework header. If so, this is relative
500 // to one of the headers on the #include stack. Walk the list of the current
501 // headers on the #include stack and pass them to HeaderInfo.
Chris Lattner5c683b22006-10-20 05:12:14 +0000502 if (CurLexer && !CurLexer->Is_PragmaLexer) {
Chris Lattner12261882008-02-01 05:34:02 +0000503 if ((CurFileEnt = SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())))
504 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
505 CurFileEnt)))
506 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000507 }
508
509 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
510 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
Chris Lattner5c683b22006-10-20 05:12:14 +0000511 if (ISEntry.TheLexer && !ISEntry.TheLexer->Is_PragmaLexer) {
Chris Lattner12261882008-02-01 05:34:02 +0000512 if ((CurFileEnt =
513 SourceMgr.getFileEntryForLoc(ISEntry.TheLexer->getFileLoc())))
514 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart,
515 FilenameEnd, CurFileEnt)))
516 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000517 }
518 }
519
520 // Otherwise, we really couldn't find the file.
521 return 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000522}
523
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000524/// isInPrimaryFile - Return true if we're in the top-level file, not in a
525/// #include.
526bool Preprocessor::isInPrimaryFile() const {
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000527 if (CurLexer && !CurLexer->Is_PragmaLexer)
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000528 return IncludeMacroStack.empty();
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000529
Chris Lattner13044d92006-07-03 05:16:44 +0000530 // If there are any stacked lexers, we're in a #include.
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000531 assert(IncludeMacroStack[0].TheLexer &&
532 !IncludeMacroStack[0].TheLexer->Is_PragmaLexer &&
533 "Top level include stack isn't our primary lexer?");
534 for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i)
Chris Lattner13044d92006-07-03 05:16:44 +0000535 if (IncludeMacroStack[i].TheLexer &&
536 !IncludeMacroStack[i].TheLexer->Is_PragmaLexer)
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000537 return false;
538 return true;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000539}
540
541/// getCurrentLexer - Return the current file lexer being lexed from. Note
542/// that this ignores any potentially active macro expansions and _Pragma
543/// expansions going on at the time.
544Lexer *Preprocessor::getCurrentFileLexer() const {
545 if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer;
546
547 // Look for a stacked lexer.
548 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Chris Lattnerf88c53a2006-07-03 05:26:05 +0000549 Lexer *L = IncludeMacroStack[i-1].TheLexer;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000550 if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions.
551 return L;
552 }
553 return 0;
554}
555
556
Chris Lattner22eb9722006-06-18 05:43:12 +0000557/// EnterSourceFile - Add a source file to the top of the include stack and
558/// start lexing tokens from it instead of the current buffer. Return true
559/// on failure.
560void Preprocessor::EnterSourceFile(unsigned FileID,
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000561 const DirectoryLookup *CurDir) {
Chris Lattner69772b02006-07-02 20:34:39 +0000562 assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000563 ++NumEnteredSourceFiles;
564
Chris Lattner69772b02006-07-02 20:34:39 +0000565 if (MaxIncludeStackDepth < IncludeMacroStack.size())
566 MaxIncludeStackDepth = IncludeMacroStack.size();
Chris Lattner22eb9722006-06-18 05:43:12 +0000567
Chris Lattner77e9de52007-07-20 16:52:03 +0000568 Lexer *TheLexer = new Lexer(SourceLocation::getFileLoc(FileID, 0), *this);
Chris Lattner69772b02006-07-02 20:34:39 +0000569 EnterSourceFileWithLexer(TheLexer, CurDir);
570}
Chris Lattner22eb9722006-06-18 05:43:12 +0000571
Chris Lattner69772b02006-07-02 20:34:39 +0000572/// EnterSourceFile - Add a source file to the top of the include stack and
573/// start lexing tokens from it instead of the current buffer.
574void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
575 const DirectoryLookup *CurDir) {
576
577 // Add the current lexer to the include stack.
578 if (CurLexer || CurMacroExpander)
579 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
580 CurMacroExpander));
581
582 CurLexer = TheLexer;
Chris Lattnerc8997182006-06-22 05:52:16 +0000583 CurDirLookup = CurDir;
Chris Lattner69772b02006-07-02 20:34:39 +0000584 CurMacroExpander = 0;
Chris Lattner0c885f52006-06-21 06:50:18 +0000585
586 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000587 if (Callbacks && !CurLexer->Is_PragmaLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000588 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
589
590 // Get the file entry for the current file.
591 if (const FileEntry *FE =
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000592 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000593 FileType = HeaderInfo.getFileDirFlavor(FE);
Chris Lattnerc8997182006-06-22 05:52:16 +0000594
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000595 Callbacks->FileChanged(CurLexer->getFileLoc(),
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000596 PPCallbacks::EnterFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +0000597 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000598}
599
Chris Lattner69772b02006-07-02 20:34:39 +0000600
601
Chris Lattner22eb9722006-06-18 05:43:12 +0000602/// EnterMacro - Add a Macro to the top of the include stack and start lexing
Chris Lattnercb283342006-06-18 06:48:37 +0000603/// tokens from it instead of the current buffer.
Chris Lattner146762e2007-07-20 16:59:19 +0000604void Preprocessor::EnterMacro(Token &Tok, MacroArgs *Args) {
Chris Lattner69772b02006-07-02 20:34:39 +0000605 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
606 CurMacroExpander));
607 CurLexer = 0;
608 CurDirLookup = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000609
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000610 if (NumCachedMacroExpanders == 0) {
611 CurMacroExpander = new MacroExpander(Tok, Args, *this);
612 } else {
613 CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
614 CurMacroExpander->Init(Tok, Args);
615 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000616}
617
Chris Lattner7667d0d2006-07-16 18:16:58 +0000618/// EnterTokenStream - Add a "macro" context to the top of the include stack,
619/// which will cause the lexer to start returning the specified tokens. Note
620/// that these tokens will be re-macro-expanded when/if expansion is enabled.
621/// This method assumes that the specified stream of tokens has a permanent
622/// owner somewhere, so they do not need to be copied.
Chris Lattner146762e2007-07-20 16:59:19 +0000623void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks) {
Chris Lattner7667d0d2006-07-16 18:16:58 +0000624 // Save our current state.
625 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
626 CurMacroExpander));
627 CurLexer = 0;
628 CurDirLookup = 0;
629
630 // Create a macro expander to expand from the specified token stream.
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000631 if (NumCachedMacroExpanders == 0) {
632 CurMacroExpander = new MacroExpander(Toks, NumToks, *this);
633 } else {
634 CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
635 CurMacroExpander->Init(Toks, NumToks);
636 }
Chris Lattner7667d0d2006-07-16 18:16:58 +0000637}
638
639/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
640/// lexer stack. This should only be used in situations where the current
641/// state of the top-of-stack lexer is known.
642void Preprocessor::RemoveTopOfLexerStack() {
643 assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000644
645 if (CurMacroExpander) {
646 // Delete or cache the now-dead macro expander.
647 if (NumCachedMacroExpanders == MacroExpanderCacheSize)
648 delete CurMacroExpander;
649 else
650 MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander;
651 } else {
652 delete CurLexer;
653 }
Chris Lattner7667d0d2006-07-16 18:16:58 +0000654 CurLexer = IncludeMacroStack.back().TheLexer;
655 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
656 CurMacroExpander = IncludeMacroStack.back().TheMacroExpander;
657 IncludeMacroStack.pop_back();
658}
659
Chris Lattner22eb9722006-06-18 05:43:12 +0000660//===----------------------------------------------------------------------===//
Chris Lattner677757a2006-06-28 05:26:32 +0000661// Macro Expansion Handling.
Chris Lattner22eb9722006-06-18 05:43:12 +0000662//===----------------------------------------------------------------------===//
663
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000664/// setMacroInfo - Specify a macro for this identifier.
665///
666void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
667 if (MI == 0) {
668 if (II->hasMacroDefinition()) {
669 Macros.erase(II);
670 II->setHasMacroDefinition(false);
671 }
672 } else {
673 Macros[II] = MI;
674 II->setHasMacroDefinition(true);
675 }
676}
677
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000678/// RegisterBuiltinMacro - Register the specified identifier in the identifier
679/// table and mark it as a builtin macro to be expanded.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000680IdentifierInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000681 // Get the identifier.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000682 IdentifierInfo *Id = getIdentifierInfo(Name);
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000683
684 // Mark it as being a macro that is builtin.
685 MacroInfo *MI = new MacroInfo(SourceLocation());
686 MI->setIsBuiltinMacro();
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000687 setMacroInfo(Id, MI);
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000688 return Id;
689}
690
691
Chris Lattner677757a2006-06-28 05:26:32 +0000692/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
693/// identifier table.
694void Preprocessor::RegisterBuiltinMacros() {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000695 Ident__LINE__ = RegisterBuiltinMacro("__LINE__");
Chris Lattner630b33c2006-07-01 22:46:53 +0000696 Ident__FILE__ = RegisterBuiltinMacro("__FILE__");
Chris Lattnerc673f902006-06-30 06:10:41 +0000697 Ident__DATE__ = RegisterBuiltinMacro("__DATE__");
698 Ident__TIME__ = RegisterBuiltinMacro("__TIME__");
Chris Lattner69772b02006-07-02 20:34:39 +0000699 Ident_Pragma = RegisterBuiltinMacro("_Pragma");
Chris Lattnerc1283b92006-07-01 23:16:30 +0000700
701 // GCC Extensions.
702 Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__");
703 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__");
Chris Lattner847e0e42006-07-01 23:49:16 +0000704 Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__");
Chris Lattner22eb9722006-06-18 05:43:12 +0000705}
706
Chris Lattnerc2395832006-07-09 00:57:04 +0000707/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
708/// in its expansion, currently expands to that token literally.
Chris Lattner3ce1d1a2006-07-09 01:00:18 +0000709static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000710 const IdentifierInfo *MacroIdent,
711 Preprocessor &PP) {
Chris Lattnerc2395832006-07-09 00:57:04 +0000712 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
713
714 // If the token isn't an identifier, it's always literally expanded.
715 if (II == 0) return true;
716
717 // If the identifier is a macro, and if that macro is enabled, it may be
718 // expanded so it's not a trivial expansion.
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000719 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
Chris Lattner3ce1d1a2006-07-09 01:00:18 +0000720 // Fast expanding "#define X X" is ok, because X would be disabled.
721 II != MacroIdent)
Chris Lattnerc2395832006-07-09 00:57:04 +0000722 return false;
723
724 // If this is an object-like macro invocation, it is safe to trivially expand
725 // it.
726 if (MI->isObjectLike()) return true;
727
728 // If this is a function-like macro invocation, it's safe to trivially expand
729 // as long as the identifier is not a macro argument.
730 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
731 I != E; ++I)
732 if (*I == II)
733 return false; // Identifier is a macro argument.
Chris Lattner273ddd52006-07-29 07:33:01 +0000734
Chris Lattnerc2395832006-07-09 00:57:04 +0000735 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000736}
737
Chris Lattnerc2395832006-07-09 00:57:04 +0000738
Chris Lattnerafe603f2006-07-11 04:02:46 +0000739/// isNextPPTokenLParen - Determine whether the next preprocessor token to be
740/// lexed is a '('. If so, consume the token and return true, if not, this
741/// method should have no observable side-effect on the lexed tokens.
742bool Preprocessor::isNextPPTokenLParen() {
Chris Lattnerafe603f2006-07-11 04:02:46 +0000743 // Do some quick tests for rejection cases.
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000744 unsigned Val;
745 if (CurLexer)
Chris Lattner678c8802006-07-11 05:46:12 +0000746 Val = CurLexer->isNextPPTokenLParen();
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000747 else
748 Val = CurMacroExpander->isNextTokenLParen();
749
750 if (Val == 2) {
Chris Lattner5c983792007-07-19 00:07:36 +0000751 // We have run off the end. If it's a source file we don't
752 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
753 // macro stack.
754 if (CurLexer)
755 return false;
756 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000757 IncludeStackInfo &Entry = IncludeMacroStack[i-1];
758 if (Entry.TheLexer)
Chris Lattner678c8802006-07-11 05:46:12 +0000759 Val = Entry.TheLexer->isNextPPTokenLParen();
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000760 else
761 Val = Entry.TheMacroExpander->isNextTokenLParen();
Chris Lattner5c983792007-07-19 00:07:36 +0000762
763 if (Val != 2)
764 break;
765
766 // Ran off the end of a source file?
767 if (Entry.TheLexer)
768 return false;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000769 }
Chris Lattnerafe603f2006-07-11 04:02:46 +0000770 }
771
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000772 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
773 // have found something that isn't a '(' or we found the end of the
774 // translation unit. In either case, return false.
775 if (Val != 1)
776 return false;
Chris Lattnerafe603f2006-07-11 04:02:46 +0000777
Chris Lattner146762e2007-07-20 16:59:19 +0000778 Token Tok;
Chris Lattnerafe603f2006-07-11 04:02:46 +0000779 LexUnexpandedToken(Tok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000780 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000781 return true;
Chris Lattnerafe603f2006-07-11 04:02:46 +0000782}
Chris Lattner677757a2006-06-28 05:26:32 +0000783
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000784/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
785/// expanded as a macro, handle it and return the next token as 'Identifier'.
Chris Lattner146762e2007-07-20 16:59:19 +0000786bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000787 MacroInfo *MI) {
Chris Lattnera30be592008-01-07 19:50:27 +0000788 // If this is a macro exapnsion in the "#if !defined(x)" line for the file,
789 // then the macro could expand to different things in other contexts, we need
790 // to disable the optimization in this case.
791 if (CurLexer) CurLexer->MIOpt.ExpandedMacro();
Chris Lattner78186052006-07-09 00:45:31 +0000792
793 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
794 if (MI->isBuiltinMacro()) {
795 ExpandBuiltinMacro(Identifier);
796 return false;
797 }
798
Chris Lattner81278c62006-10-14 19:03:49 +0000799 // If this is the first use of a target-specific macro, warn about it.
800 if (MI->isTargetSpecific()) {
801 MI->setIsTargetSpecific(false); // Don't warn on second use.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000802 getTargetInfo().DiagnoseNonPortability(getFullLoc(Identifier.getLocation()),
Chris Lattner81278c62006-10-14 19:03:49 +0000803 diag::port_target_macro_use);
804 }
805
Chris Lattneree8760b2006-07-15 07:42:55 +0000806 /// Args - If this is a function-like macro expansion, this contains,
Chris Lattner78186052006-07-09 00:45:31 +0000807 /// for each macro argument, the list of tokens that were provided to the
808 /// invocation.
Chris Lattneree8760b2006-07-15 07:42:55 +0000809 MacroArgs *Args = 0;
Chris Lattner78186052006-07-09 00:45:31 +0000810
811 // If this is a function-like macro, read the arguments.
812 if (MI->isFunctionLike()) {
Chris Lattner78186052006-07-09 00:45:31 +0000813 // C99 6.10.3p10: If the preprocessing token immediately after the the macro
Chris Lattner24dbee72007-07-19 16:11:58 +0000814 // name isn't a '(', this macro should not be expanded. Otherwise, consume
815 // it.
Chris Lattnerafe603f2006-07-11 04:02:46 +0000816 if (!isNextPPTokenLParen())
Chris Lattner78186052006-07-09 00:45:31 +0000817 return true;
818
Chris Lattner78186052006-07-09 00:45:31 +0000819 // Remember that we are now parsing the arguments to a macro invocation.
820 // Preprocessor directives used inside macro arguments are not portable, and
821 // this enables the warning.
Chris Lattneree8760b2006-07-15 07:42:55 +0000822 InMacroArgs = true;
823 Args = ReadFunctionLikeMacroArgs(Identifier, MI);
Chris Lattner78186052006-07-09 00:45:31 +0000824
825 // Finished parsing args.
Chris Lattneree8760b2006-07-15 07:42:55 +0000826 InMacroArgs = false;
Chris Lattner78186052006-07-09 00:45:31 +0000827
828 // If there was an error parsing the arguments, bail out.
Chris Lattneree8760b2006-07-15 07:42:55 +0000829 if (Args == 0) return false;
Chris Lattner78186052006-07-09 00:45:31 +0000830
831 ++NumFnMacroExpanded;
832 } else {
833 ++NumMacroExpanded;
834 }
Chris Lattner13044d92006-07-03 05:16:44 +0000835
836 // Notice that this macro has been used.
837 MI->setIsUsed(true);
Chris Lattner69772b02006-07-02 20:34:39 +0000838
839 // If we started lexing a macro, enter the macro expansion body.
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000840
841 // If this macro expands to no tokens, don't bother to push it onto the
842 // expansion stack, only to take it right back off.
843 if (MI->getNumTokens() == 0) {
Chris Lattner2ada5d32006-07-15 07:51:24 +0000844 // No need for arg info.
Chris Lattnerc1410dc2006-07-26 05:22:49 +0000845 if (Args) Args->destroy();
Chris Lattner78186052006-07-09 00:45:31 +0000846
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000847 // Ignore this macro use, just return the next token in the current
848 // buffer.
849 bool HadLeadingSpace = Identifier.hasLeadingSpace();
850 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
851
852 Lex(Identifier);
853
854 // If the identifier isn't on some OTHER line, inherit the leading
855 // whitespace/first-on-a-line property of this token. This handles
856 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
857 // empty.
858 if (!Identifier.isAtStartOfLine()) {
Chris Lattner146762e2007-07-20 16:59:19 +0000859 if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
860 if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000861 }
862 ++NumFastMacroExpanded;
Chris Lattner78186052006-07-09 00:45:31 +0000863 return false;
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000864
Chris Lattner3ce1d1a2006-07-09 01:00:18 +0000865 } else if (MI->getNumTokens() == 1 &&
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000866 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
867 *this)){
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000868 // Otherwise, if this macro expands into a single trivially-expanded
869 // token: expand it now. This handles common cases like
870 // "#define VAL 42".
871
872 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
873 // identifier to the expanded token.
874 bool isAtStartOfLine = Identifier.isAtStartOfLine();
875 bool hasLeadingSpace = Identifier.hasLeadingSpace();
876
877 // Remember where the token is instantiated.
878 SourceLocation InstantiateLoc = Identifier.getLocation();
879
880 // Replace the result token.
881 Identifier = MI->getReplacementToken(0);
882
883 // Restore the StartOfLine/LeadingSpace markers.
Chris Lattner146762e2007-07-20 16:59:19 +0000884 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
885 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000886
887 // Update the tokens location to include both its logical and physical
888 // locations.
889 SourceLocation Loc =
Chris Lattnerc673f902006-06-30 06:10:41 +0000890 SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc);
Chris Lattner8c204872006-10-14 05:19:21 +0000891 Identifier.setLocation(Loc);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000892
Chris Lattner6e4bf522006-07-27 06:59:25 +0000893 // If this is #define X X, we must mark the result as unexpandible.
894 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo())
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000895 if (getMacroInfo(NewII) == MI)
Chris Lattner146762e2007-07-20 16:59:19 +0000896 Identifier.setFlag(Token::DisableExpand);
Chris Lattner6e4bf522006-07-27 06:59:25 +0000897
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000898 // Since this is not an identifier token, it can't be macro expanded, so
899 // we're done.
900 ++NumFastMacroExpanded;
Chris Lattner78186052006-07-09 00:45:31 +0000901 return false;
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000902 }
903
Chris Lattner78186052006-07-09 00:45:31 +0000904 // Start expanding the macro.
Chris Lattneree8760b2006-07-15 07:42:55 +0000905 EnterMacro(Identifier, Args);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000906
907 // Now that the macro is at the top of the include stack, ask the
908 // preprocessor to read the next token from it.
Chris Lattner78186052006-07-09 00:45:31 +0000909 Lex(Identifier);
910 return false;
911}
912
Chris Lattneree8760b2006-07-15 07:42:55 +0000913/// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
Chris Lattner2ada5d32006-07-15 07:51:24 +0000914/// invoked to read all of the actual arguments specified for the macro
Chris Lattner78186052006-07-09 00:45:31 +0000915/// invocation. This returns null on error.
Chris Lattner146762e2007-07-20 16:59:19 +0000916MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
Chris Lattneree8760b2006-07-15 07:42:55 +0000917 MacroInfo *MI) {
Chris Lattner78186052006-07-09 00:45:31 +0000918 // The number of fixed arguments to parse.
919 unsigned NumFixedArgsLeft = MI->getNumArgs();
920 bool isVariadic = MI->isVariadic();
921
Chris Lattner78186052006-07-09 00:45:31 +0000922 // Outer loop, while there are more arguments, keep reading them.
Chris Lattner146762e2007-07-20 16:59:19 +0000923 Token Tok;
Chris Lattner8c204872006-10-14 05:19:21 +0000924 Tok.setKind(tok::comma);
Chris Lattner78186052006-07-09 00:45:31 +0000925 --NumFixedArgsLeft; // Start reading the first arg.
Chris Lattner36b6e812006-07-21 06:38:30 +0000926
927 // ArgTokens - Build up a list of tokens that make up each argument. Each
Chris Lattner7a4af3b2006-07-26 06:26:52 +0000928 // argument is separated by an EOF token. Use a SmallVector so we can avoid
929 // heap allocations in the common case.
Chris Lattner146762e2007-07-20 16:59:19 +0000930 llvm::SmallVector<Token, 64> ArgTokens;
Chris Lattner36b6e812006-07-21 06:38:30 +0000931
932 unsigned NumActuals = 0;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000933 while (Tok.is(tok::comma)) {
Chris Lattner24dbee72007-07-19 16:11:58 +0000934 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
935 // that we already consumed the first one.
Chris Lattner78186052006-07-09 00:45:31 +0000936 unsigned NumParens = 0;
Chris Lattner36b6e812006-07-21 06:38:30 +0000937
Chris Lattner78186052006-07-09 00:45:31 +0000938 while (1) {
Chris Lattnerafe603f2006-07-11 04:02:46 +0000939 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
940 // an argument value in a macro could expand to ',' or '(' or ')'.
Chris Lattner78186052006-07-09 00:45:31 +0000941 LexUnexpandedToken(Tok);
942
Chris Lattner97ff7762008-01-22 19:34:51 +0000943 if (Tok.is(tok::eof) || Tok.is(tok::eom)) { // "#if f(<eof>" & "#if f(\n"
Chris Lattner78186052006-07-09 00:45:31 +0000944 Diag(MacroName, diag::err_unterm_macro_invoc);
Chris Lattner97ff7762008-01-22 19:34:51 +0000945 // Do not lose the EOF/EOM. Return it to the client.
Chris Lattner78186052006-07-09 00:45:31 +0000946 MacroName = Tok;
947 return 0;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000948 } else if (Tok.is(tok::r_paren)) {
Chris Lattner78186052006-07-09 00:45:31 +0000949 // If we found the ) token, the macro arg list is done.
950 if (NumParens-- == 0)
951 break;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000952 } else if (Tok.is(tok::l_paren)) {
Chris Lattner78186052006-07-09 00:45:31 +0000953 ++NumParens;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000954 } else if (Tok.is(tok::comma) && NumParens == 0) {
Chris Lattner78186052006-07-09 00:45:31 +0000955 // Comma ends this argument if there are more fixed arguments expected.
956 if (NumFixedArgsLeft)
957 break;
958
Chris Lattner2ada5d32006-07-15 07:51:24 +0000959 // If this is not a variadic macro, too many args were specified.
Chris Lattner78186052006-07-09 00:45:31 +0000960 if (!isVariadic) {
961 // Emit the diagnostic at the macro name in case there is a missing ).
962 // Emitting it at the , could be far away from the macro name.
Chris Lattner2ada5d32006-07-15 07:51:24 +0000963 Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
Chris Lattner78186052006-07-09 00:45:31 +0000964 return 0;
965 }
966 // Otherwise, continue to add the tokens to this variable argument.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000967 } else if (Tok.is(tok::comment) && !KeepMacroComments) {
Chris Lattner457fc152006-07-29 06:30:25 +0000968 // If this is a comment token in the argument list and we're just in
969 // -C mode (not -CC mode), discard the comment.
970 continue;
Chris Lattner9fcdc522007-11-23 06:50:21 +0000971 } else if (Tok.is(tok::identifier)) {
972 // Reading macro arguments can cause macros that we are currently
973 // expanding from to be popped off the expansion stack. Doing so causes
974 // them to be reenabled for expansion. Here we record whether any
975 // identifiers we lex as macro arguments correspond to disabled macros.
976 // If so, we mark the token as noexpand. This is a subtle aspect of
977 // C99 6.10.3.4p2.
978 if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
979 if (!MI->isEnabled())
980 Tok.setFlag(Token::DisableExpand);
Chris Lattner78186052006-07-09 00:45:31 +0000981 }
982
983 ArgTokens.push_back(Tok);
984 }
985
Chris Lattnera12dd152006-07-11 04:09:02 +0000986 // Empty arguments are standard in C99 and supported as an extension in
987 // other modes.
988 if (ArgTokens.empty() && !Features.C99)
989 Diag(Tok, diag::ext_empty_fnmacro_arg);
Chris Lattnerafe603f2006-07-11 04:02:46 +0000990
Chris Lattner36b6e812006-07-21 06:38:30 +0000991 // Add a marker EOF token to the end of the token list for this argument.
Chris Lattner146762e2007-07-20 16:59:19 +0000992 Token EOFTok;
Chris Lattner8c204872006-10-14 05:19:21 +0000993 EOFTok.startToken();
994 EOFTok.setKind(tok::eof);
995 EOFTok.setLocation(Tok.getLocation());
996 EOFTok.setLength(0);
Chris Lattner36b6e812006-07-21 06:38:30 +0000997 ArgTokens.push_back(EOFTok);
998 ++NumActuals;
Chris Lattner78186052006-07-09 00:45:31 +0000999 --NumFixedArgsLeft;
1000 };
1001
1002 // Okay, we either found the r_paren. Check to see if we parsed too few
1003 // arguments.
Chris Lattner78186052006-07-09 00:45:31 +00001004 unsigned MinArgsExpected = MI->getNumArgs();
1005
Chris Lattner775d8322006-07-29 04:39:41 +00001006 // See MacroArgs instance var for description of this.
1007 bool isVarargsElided = false;
1008
Chris Lattner2ada5d32006-07-15 07:51:24 +00001009 if (NumActuals < MinArgsExpected) {
Chris Lattner78186052006-07-09 00:45:31 +00001010 // There are several cases where too few arguments is ok, handle them now.
Chris Lattner2ada5d32006-07-15 07:51:24 +00001011 if (NumActuals+1 == MinArgsExpected && MI->isVariadic()) {
Chris Lattner78186052006-07-09 00:45:31 +00001012 // Varargs where the named vararg parameter is missing: ok as extension.
1013 // #define A(x, ...)
1014 // A("blah")
1015 Diag(Tok, diag::ext_missing_varargs_arg);
Chris Lattner775d8322006-07-29 04:39:41 +00001016
1017 // Remember this occurred if this is a C99 macro invocation with at least
1018 // one actual argument.
Chris Lattner95a06b32006-07-30 08:40:43 +00001019 isVarargsElided = MI->isC99Varargs() && MI->getNumArgs() > 1;
Chris Lattner78186052006-07-09 00:45:31 +00001020 } else if (MI->getNumArgs() == 1) {
1021 // #define A(x)
1022 // A()
Chris Lattnere7a51302006-07-29 01:25:12 +00001023 // is ok because it is an empty argument.
Chris Lattnera12dd152006-07-11 04:09:02 +00001024
1025 // Empty arguments are standard in C99 and supported as an extension in
1026 // other modes.
1027 if (ArgTokens.empty() && !Features.C99)
1028 Diag(Tok, diag::ext_empty_fnmacro_arg);
Chris Lattner78186052006-07-09 00:45:31 +00001029 } else {
1030 // Otherwise, emit the error.
Chris Lattner2ada5d32006-07-15 07:51:24 +00001031 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
Chris Lattner78186052006-07-09 00:45:31 +00001032 return 0;
1033 }
Chris Lattnere7a51302006-07-29 01:25:12 +00001034
1035 // Add a marker EOF token to the end of the token list for this argument.
1036 SourceLocation EndLoc = Tok.getLocation();
Chris Lattner8c204872006-10-14 05:19:21 +00001037 Tok.startToken();
1038 Tok.setKind(tok::eof);
1039 Tok.setLocation(EndLoc);
1040 Tok.setLength(0);
Chris Lattnere7a51302006-07-29 01:25:12 +00001041 ArgTokens.push_back(Tok);
Chris Lattner78186052006-07-09 00:45:31 +00001042 }
1043
Chris Lattner775d8322006-07-29 04:39:41 +00001044 return MacroArgs::create(MI, &ArgTokens[0], ArgTokens.size(),isVarargsElided);
Chris Lattnerf373a4a2006-06-26 06:16:29 +00001045}
1046
Chris Lattnerc673f902006-06-30 06:10:41 +00001047/// ComputeDATE_TIME - Compute the current time, enter it into the specified
1048/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
1049/// the identifier tokens inserted.
1050static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001051 Preprocessor &PP) {
Chris Lattnerc673f902006-06-30 06:10:41 +00001052 time_t TT = time(0);
1053 struct tm *TM = localtime(&TT);
1054
1055 static const char * const Months[] = {
1056 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
1057 };
1058
1059 char TmpBuffer[100];
1060 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
1061 TM->tm_year+1900);
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001062 DATELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
Chris Lattnerc673f902006-06-30 06:10:41 +00001063
1064 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001065 TIMELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
Chris Lattnerc673f902006-06-30 06:10:41 +00001066}
1067
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001068/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1069/// as a builtin macro, handle it and return the next token as 'Tok'.
Chris Lattner146762e2007-07-20 16:59:19 +00001070void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001071 // Figure out which token this is.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001072 IdentifierInfo *II = Tok.getIdentifierInfo();
1073 assert(II && "Can't be a macro without id info!");
Chris Lattner69772b02006-07-02 20:34:39 +00001074
1075 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
1076 // lex the token after it.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001077 if (II == Ident_Pragma)
Chris Lattner69772b02006-07-02 20:34:39 +00001078 return Handle_Pragma(Tok);
1079
Chris Lattner78186052006-07-09 00:45:31 +00001080 ++NumBuiltinMacroExpanded;
1081
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001082 char TmpBuffer[100];
Chris Lattner69772b02006-07-02 20:34:39 +00001083
1084 // Set up the return result.
Chris Lattner8c204872006-10-14 05:19:21 +00001085 Tok.setIdentifierInfo(0);
Chris Lattner146762e2007-07-20 16:59:19 +00001086 Tok.clearFlag(Token::NeedsCleaning);
Chris Lattner630b33c2006-07-01 22:46:53 +00001087
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001088 if (II == Ident__LINE__) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001089 // __LINE__ expands to a simple numeric value.
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001090 sprintf(TmpBuffer, "%u", SourceMgr.getLogicalLineNumber(Tok.getLocation()));
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001091 unsigned Length = strlen(TmpBuffer);
Chris Lattner8c204872006-10-14 05:19:21 +00001092 Tok.setKind(tok::numeric_constant);
1093 Tok.setLength(Length);
1094 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001095 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +00001096 SourceLocation Loc = Tok.getLocation();
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001097 if (II == Ident__BASE_FILE__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +00001098 Diag(Tok, diag::ext_pp_base_file);
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001099 SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc);
1100 while (NextLoc.isValid()) {
Chris Lattnerc1283b92006-07-01 23:16:30 +00001101 Loc = NextLoc;
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001102 NextLoc = SourceMgr.getIncludeLoc(Loc);
Chris Lattnerc1283b92006-07-01 23:16:30 +00001103 }
1104 }
1105
Chris Lattner0766e592006-07-03 01:07:01 +00001106 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001107 std::string FN = SourceMgr.getSourceName(SourceMgr.getLogicalLoc(Loc));
Chris Lattnerecc39e92006-07-15 05:23:31 +00001108 FN = '"' + Lexer::Stringify(FN) + '"';
Chris Lattner8c204872006-10-14 05:19:21 +00001109 Tok.setKind(tok::string_literal);
1110 Tok.setLength(FN.size());
1111 Tok.setLocation(CreateString(&FN[0], FN.size(), Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001112 } else if (II == Ident__DATE__) {
Chris Lattnerc673f902006-06-30 06:10:41 +00001113 if (!DATELoc.isValid())
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001114 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
Chris Lattner8c204872006-10-14 05:19:21 +00001115 Tok.setKind(tok::string_literal);
1116 Tok.setLength(strlen("\"Mmm dd yyyy\""));
1117 Tok.setLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001118 } else if (II == Ident__TIME__) {
Chris Lattnerc673f902006-06-30 06:10:41 +00001119 if (!TIMELoc.isValid())
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001120 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
Chris Lattner8c204872006-10-14 05:19:21 +00001121 Tok.setKind(tok::string_literal);
1122 Tok.setLength(strlen("\"hh:mm:ss\""));
1123 Tok.setLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001124 } else if (II == Ident__INCLUDE_LEVEL__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +00001125 Diag(Tok, diag::ext_pp_include_level);
1126
1127 // Compute the include depth of this token.
1128 unsigned Depth = 0;
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001129 SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation());
1130 for (; Loc.isValid(); ++Depth)
1131 Loc = SourceMgr.getIncludeLoc(Loc);
Chris Lattnerc1283b92006-07-01 23:16:30 +00001132
1133 // __INCLUDE_LEVEL__ expands to a simple numeric value.
1134 sprintf(TmpBuffer, "%u", Depth);
1135 unsigned Length = strlen(TmpBuffer);
Chris Lattner8c204872006-10-14 05:19:21 +00001136 Tok.setKind(tok::numeric_constant);
1137 Tok.setLength(Length);
1138 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001139 } else if (II == Ident__TIMESTAMP__) {
Chris Lattner847e0e42006-07-01 23:49:16 +00001140 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
1141 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1142 Diag(Tok, diag::ext_pp_timestamp);
1143
1144 // Get the file that we are lexing out of. If we're currently lexing from
1145 // a macro, dig into the include stack.
1146 const FileEntry *CurFile = 0;
Chris Lattnerecfeafe2006-07-02 21:26:45 +00001147 Lexer *TheLexer = getCurrentFileLexer();
Chris Lattner847e0e42006-07-01 23:49:16 +00001148
1149 if (TheLexer)
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001150 CurFile = SourceMgr.getFileEntryForLoc(TheLexer->getFileLoc());
Chris Lattner847e0e42006-07-01 23:49:16 +00001151
1152 // If this file is older than the file it depends on, emit a diagnostic.
1153 const char *Result;
1154 if (CurFile) {
1155 time_t TT = CurFile->getModificationTime();
1156 struct tm *TM = localtime(&TT);
1157 Result = asctime(TM);
1158 } else {
1159 Result = "??? ??? ?? ??:??:?? ????\n";
1160 }
1161 TmpBuffer[0] = '"';
1162 strcpy(TmpBuffer+1, Result);
1163 unsigned Len = strlen(TmpBuffer);
1164 TmpBuffer[Len-1] = '"'; // Replace the newline with a quote.
Chris Lattner8c204872006-10-14 05:19:21 +00001165 Tok.setKind(tok::string_literal);
1166 Tok.setLength(Len);
1167 Tok.setLocation(CreateString(TmpBuffer, Len, Tok.getLocation()));
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001168 } else {
1169 assert(0 && "Unknown identifier!");
Chris Lattner615315f2007-12-09 20:31:55 +00001170 }
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001171}
Chris Lattner677757a2006-06-28 05:26:32 +00001172
1173//===----------------------------------------------------------------------===//
1174// Lexer Event Handling.
1175//===----------------------------------------------------------------------===//
1176
Chris Lattnercefc7682006-07-08 08:28:12 +00001177/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
1178/// identifier information for the token and install it into the token.
Chris Lattner146762e2007-07-20 16:59:19 +00001179IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
Chris Lattnercefc7682006-07-08 08:28:12 +00001180 const char *BufPtr) {
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001181 assert(Identifier.is(tok::identifier) && "Not an identifier!");
Chris Lattnercefc7682006-07-08 08:28:12 +00001182 assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
1183
1184 // Look up this token, see if it is a macro, or if it is a language keyword.
1185 IdentifierInfo *II;
1186 if (BufPtr && !Identifier.needsCleaning()) {
1187 // No cleaning needed, just use the characters from the lexed buffer.
1188 II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
1189 } else {
1190 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
Chris Lattnerf9aba2c2007-07-13 17:10:38 +00001191 llvm::SmallVector<char, 64> IdentifierBuffer;
1192 IdentifierBuffer.resize(Identifier.getLength());
1193 const char *TmpBuf = &IdentifierBuffer[0];
Chris Lattnercefc7682006-07-08 08:28:12 +00001194 unsigned Size = getSpelling(Identifier, TmpBuf);
1195 II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
1196 }
Chris Lattner8c204872006-10-14 05:19:21 +00001197 Identifier.setIdentifierInfo(II);
Chris Lattnercefc7682006-07-08 08:28:12 +00001198 return II;
1199}
1200
1201
Chris Lattner677757a2006-06-28 05:26:32 +00001202/// HandleIdentifier - This callback is invoked when the lexer reads an
1203/// identifier. This callback looks up the identifier in the map and/or
1204/// potentially macro expands it or turns it into a named token (like 'for').
Chris Lattner146762e2007-07-20 16:59:19 +00001205void Preprocessor::HandleIdentifier(Token &Identifier) {
Chris Lattner0f1f5052006-07-20 04:16:23 +00001206 assert(Identifier.getIdentifierInfo() &&
1207 "Can't handle identifiers without identifier info!");
1208
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001209 IdentifierInfo &II = *Identifier.getIdentifierInfo();
Chris Lattner677757a2006-06-28 05:26:32 +00001210
1211 // If this identifier was poisoned, and if it was not produced from a macro
1212 // expansion, emit an error.
Chris Lattner8ff71992006-07-06 05:17:39 +00001213 if (II.isPoisoned() && CurLexer) {
1214 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
1215 Diag(Identifier, diag::err_pp_used_poisoned_id);
1216 else
1217 Diag(Identifier, diag::ext_pp_bad_vaargs_use);
1218 }
Chris Lattner677757a2006-06-28 05:26:32 +00001219
Chris Lattner78186052006-07-09 00:45:31 +00001220 // If this is a macro to be expanded, do it.
Chris Lattnerc43ddc82007-10-07 08:44:20 +00001221 if (MacroInfo *MI = getMacroInfo(&II)) {
Chris Lattner6e4bf522006-07-27 06:59:25 +00001222 if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
1223 if (MI->isEnabled()) {
1224 if (!HandleMacroExpandedIdentifier(Identifier, MI))
1225 return;
1226 } else {
1227 // C99 6.10.3.4p2 says that a disabled macro may never again be
1228 // expanded, even if it's in a context where it could be expanded in the
1229 // future.
Chris Lattner146762e2007-07-20 16:59:19 +00001230 Identifier.setFlag(Token::DisableExpand);
Chris Lattner6e4bf522006-07-27 06:59:25 +00001231 }
1232 }
Chris Lattner063400e2006-10-14 19:54:15 +00001233 } else if (II.isOtherTargetMacro() && !DisableMacroExpansion) {
1234 // If this identifier is a macro on some other target, emit a diagnostic.
1235 // This diagnosic is only emitted when macro expansion is enabled, because
1236 // the macro would not have been expanded for the other target either.
1237 II.setIsOtherTargetMacro(false); // Don't warn on second use.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +00001238 getTargetInfo().DiagnoseNonPortability(getFullLoc(Identifier.getLocation()),
Chris Lattner063400e2006-10-14 19:54:15 +00001239 diag::port_target_macro_use);
1240
1241 }
Chris Lattner677757a2006-06-28 05:26:32 +00001242
Chris Lattner5b9f4892006-11-21 17:23:33 +00001243 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
1244 // then we act as if it is the actual operator and not the textual
1245 // representation of it.
1246 if (II.isCPlusPlusOperatorKeyword())
1247 Identifier.setIdentifierInfo(0);
1248
Chris Lattner677757a2006-06-28 05:26:32 +00001249 // Change the kind of this identifier to the appropriate token kind, e.g.
1250 // turning "for" into a keyword.
Chris Lattner8c204872006-10-14 05:19:21 +00001251 Identifier.setKind(II.getTokenID());
Chris Lattner677757a2006-06-28 05:26:32 +00001252
1253 // If this is an extension token, diagnose its use.
Steve Naroffa8fd9732007-06-11 00:35:03 +00001254 // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99
1255 // For now, I'm just commenting it out (while I work on attributes).
Chris Lattner53621a52007-06-13 20:44:40 +00001256 if (II.isExtensionToken() && Features.C99)
1257 Diag(Identifier, diag::ext_token_used);
Chris Lattner677757a2006-06-28 05:26:32 +00001258}
1259
Chris Lattner22eb9722006-06-18 05:43:12 +00001260/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
1261/// the current file. This either returns the EOF token or pops a level off
1262/// the include stack and keeps going.
Chris Lattner146762e2007-07-20 16:59:19 +00001263bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001264 assert(!CurMacroExpander &&
1265 "Ending a file when currently in a macro!");
1266
Chris Lattner371ac8a2006-07-04 07:11:10 +00001267 // See if this file had a controlling macro.
Chris Lattner3665f162006-07-04 07:26:10 +00001268 if (CurLexer) { // Not ending a macro, ignore it.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001269 if (const IdentifierInfo *ControllingMacro =
Chris Lattner371ac8a2006-07-04 07:11:10 +00001270 CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
Chris Lattner3665f162006-07-04 07:26:10 +00001271 // Okay, this has a controlling macro, remember in PerFileInfo.
1272 if (const FileEntry *FE =
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001273 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001274 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
Chris Lattner371ac8a2006-07-04 07:11:10 +00001275 }
1276 }
1277
Chris Lattner22eb9722006-06-18 05:43:12 +00001278 // If this is a #include'd file, pop it off the include stack and continue
1279 // lexing the #includer file.
Chris Lattner69772b02006-07-02 20:34:39 +00001280 if (!IncludeMacroStack.empty()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001281 // We're done with the #included file.
Chris Lattner7667d0d2006-07-16 18:16:58 +00001282 RemoveTopOfLexerStack();
Chris Lattner0c885f52006-06-21 06:50:18 +00001283
1284 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +00001285 if (Callbacks && !isEndOfMacro && CurLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +00001286 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
1287
1288 // Get the file entry for the current file.
1289 if (const FileEntry *FE =
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001290 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001291 FileType = HeaderInfo.getFileDirFlavor(FE);
Chris Lattnerc8997182006-06-22 05:52:16 +00001292
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +00001293 Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr),
1294 PPCallbacks::ExitFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +00001295 }
Chris Lattner2183a6e2006-07-18 06:36:12 +00001296
1297 // Client should lex another token.
1298 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001299 }
Chris Lattner9046dc12008-01-25 00:00:30 +00001300
1301 // If the file ends with a newline, form the EOF token on the newline itself,
1302 // rather than "on the line following it", which doesn't exist. This makes
1303 // diagnostics relating to the end of file include the last file that the user
1304 // actually typed, which is goodness.
1305 const char *EndPos = CurLexer->BufferEnd;
1306 if (EndPos != CurLexer->BufferStart &&
1307 (EndPos[-1] == '\n' || EndPos[-1] == '\r')) {
1308 --EndPos;
1309
1310 // Handle \n\r and \r\n:
1311 if (EndPos != CurLexer->BufferStart &&
1312 (EndPos[-1] == '\n' || EndPos[-1] == '\r') &&
1313 EndPos[-1] != EndPos[0])
1314 --EndPos;
1315 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001316
Chris Lattner8c204872006-10-14 05:19:21 +00001317 Result.startToken();
Chris Lattner9046dc12008-01-25 00:00:30 +00001318 CurLexer->BufferPtr = EndPos;
1319 CurLexer->FormTokenWithChars(Result, EndPos);
Chris Lattner8c204872006-10-14 05:19:21 +00001320 Result.setKind(tok::eof);
Chris Lattner22eb9722006-06-18 05:43:12 +00001321
1322 // We're done with the #included file.
1323 delete CurLexer;
1324 CurLexer = 0;
Chris Lattner13044d92006-07-03 05:16:44 +00001325
Chris Lattner03f83482006-07-10 06:16:26 +00001326 // This is the end of the top-level file. If the diag::pp_macro_not_used
Chris Lattnerc43ddc82007-10-07 08:44:20 +00001327 // diagnostic is enabled, look for macros that have not been used.
Chris Lattnerb055f2d2007-02-11 08:19:57 +00001328 if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){
Chris Lattnerc43ddc82007-10-07 08:44:20 +00001329 for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
1330 Macros.begin(), E = Macros.end(); I != E; ++I) {
1331 if (!I->second->isUsed())
1332 Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattnerb055f2d2007-02-11 08:19:57 +00001333 }
1334 }
Chris Lattner2183a6e2006-07-18 06:36:12 +00001335 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001336}
1337
1338/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
Chris Lattner7667d0d2006-07-16 18:16:58 +00001339/// the current macro expansion or token stream expansion.
Chris Lattner146762e2007-07-20 16:59:19 +00001340bool Preprocessor::HandleEndOfMacro(Token &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001341 assert(CurMacroExpander && !CurLexer &&
1342 "Ending a macro when currently in a #include file!");
1343
Chris Lattnerc02c4ab2007-07-15 00:25:26 +00001344 // Delete or cache the now-dead macro expander.
1345 if (NumCachedMacroExpanders == MacroExpanderCacheSize)
1346 delete CurMacroExpander;
1347 else
1348 MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander;
Chris Lattner22eb9722006-06-18 05:43:12 +00001349
Chris Lattner69772b02006-07-02 20:34:39 +00001350 // Handle this like a #include file being popped off the stack.
1351 CurMacroExpander = 0;
1352 return HandleEndOfFile(Result, true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001353}
1354
1355
1356//===----------------------------------------------------------------------===//
1357// Utility Methods for Preprocessor Directive Handling.
1358//===----------------------------------------------------------------------===//
1359
1360/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
1361/// current line until the tok::eom token is found.
Chris Lattnercb283342006-06-18 06:48:37 +00001362void Preprocessor::DiscardUntilEndOfDirective() {
Chris Lattner146762e2007-07-20 16:59:19 +00001363 Token Tmp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001364 do {
Chris Lattnercb283342006-06-18 06:48:37 +00001365 LexUnexpandedToken(Tmp);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001366 } while (Tmp.isNot(tok::eom));
Chris Lattner22eb9722006-06-18 05:43:12 +00001367}
1368
Chris Lattner652c1692006-11-21 23:47:30 +00001369/// isCXXNamedOperator - Returns "true" if the token is a named operator in C++.
1370static bool isCXXNamedOperator(const std::string &Spelling) {
1371 return Spelling == "and" || Spelling == "bitand" || Spelling == "bitor" ||
1372 Spelling == "compl" || Spelling == "not" || Spelling == "not_eq" ||
1373 Spelling == "or" || Spelling == "xor";
1374}
1375
Chris Lattner22eb9722006-06-18 05:43:12 +00001376/// ReadMacroName - Lex and validate a macro name, which occurs after a
1377/// #define or #undef. This sets the token kind to eom and discards the rest
Chris Lattnere8eef322006-07-08 07:01:00 +00001378/// of the macro line if the macro name is invalid. isDefineUndef is 1 if
1379/// this is due to a a #define, 2 if #undef directive, 0 if it is something
Chris Lattner44f8a662006-07-03 01:27:27 +00001380/// else (e.g. #ifdef).
Chris Lattner146762e2007-07-20 16:59:19 +00001381void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001382 // Read the token, don't allow macro expansion on it.
Chris Lattnercb283342006-06-18 06:48:37 +00001383 LexUnexpandedToken(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001384
1385 // Missing macro name?
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001386 if (MacroNameTok.is(tok::eom))
Chris Lattner22eb9722006-06-18 05:43:12 +00001387 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
1388
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001389 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1390 if (II == 0) {
Chris Lattner652c1692006-11-21 23:47:30 +00001391 std::string Spelling = getSpelling(MacroNameTok);
1392 if (isCXXNamedOperator(Spelling))
1393 // C++ 2.5p2: Alternative tokens behave the same as its primary token
1394 // except for their spellings.
1395 Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name, Spelling);
1396 else
1397 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +00001398 // Fall through on error.
Chris Lattner2bb8a952006-11-21 22:24:17 +00001399 } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
Chris Lattner44f8a662006-07-03 01:27:27 +00001400 // Error if defining "defined": C99 6.10.8.4.
Chris Lattneraaf09112006-07-03 01:17:59 +00001401 Diag(MacroNameTok, diag::err_defined_macro_name);
Chris Lattner259716a2007-10-07 08:04:56 +00001402 } else if (isDefineUndef && II->hasMacroDefinition() &&
Chris Lattnerc43ddc82007-10-07 08:44:20 +00001403 getMacroInfo(II)->isBuiltinMacro()) {
Chris Lattner44f8a662006-07-03 01:27:27 +00001404 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
Chris Lattnere8eef322006-07-08 07:01:00 +00001405 if (isDefineUndef == 1)
1406 Diag(MacroNameTok, diag::pp_redef_builtin_macro);
1407 else
1408 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
Chris Lattner22eb9722006-06-18 05:43:12 +00001409 } else {
1410 // Okay, we got a good identifier node. Return it.
Chris Lattnercb283342006-06-18 06:48:37 +00001411 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001412 }
1413
Chris Lattner22eb9722006-06-18 05:43:12 +00001414 // Invalid macro name, read and discard the rest of the line. Then set the
1415 // token kind to tok::eom.
Chris Lattner8c204872006-10-14 05:19:21 +00001416 MacroNameTok.setKind(tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +00001417 return DiscardUntilEndOfDirective();
1418}
1419
1420/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
1421/// not, emit a diagnostic and consume up until the eom.
Chris Lattnercb283342006-06-18 06:48:37 +00001422void Preprocessor::CheckEndOfDirective(const char *DirType) {
Chris Lattner146762e2007-07-20 16:59:19 +00001423 Token Tmp;
Chris Lattnercb283342006-06-18 06:48:37 +00001424 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001425 // There should be no tokens after the directive, but we allow them as an
1426 // extension.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001427 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
Chris Lattnerbcb416b2006-10-27 05:43:50 +00001428 Lex(Tmp);
1429
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001430 if (Tmp.isNot(tok::eom)) {
Chris Lattnercb283342006-06-18 06:48:37 +00001431 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
1432 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001433 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001434}
1435
1436
1437
1438/// SkipExcludedConditionalBlock - We just read a #if or related directive and
1439/// decided that the subsequent tokens are in the #if'd out portion of the
1440/// file. Lex the rest of the file, until we see an #endif. If
1441/// FoundNonSkipPortion is true, then we have already emitted code for part of
1442/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
1443/// is true, then #else directives are ok, if not, then we have already seen one
1444/// so a #else directive is a duplicate. When this returns, the caller can lex
1445/// the first valid token.
Chris Lattnerd01e2912006-06-18 16:22:51 +00001446void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
Chris Lattner22eb9722006-06-18 05:43:12 +00001447 bool FoundNonSkipPortion,
1448 bool FoundElse) {
1449 ++NumSkipped;
Chris Lattner69772b02006-07-02 20:34:39 +00001450 assert(CurMacroExpander == 0 && CurLexer &&
Chris Lattner22eb9722006-06-18 05:43:12 +00001451 "Lexing a macro, not a file?");
1452
1453 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
1454 FoundNonSkipPortion, FoundElse);
1455
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001456 // Enter raw mode to disable identifier lookup (and thus macro expansion),
1457 // disabling warnings, etc.
1458 CurLexer->LexingRawMode = true;
Chris Lattner146762e2007-07-20 16:59:19 +00001459 Token Tok;
Chris Lattner22eb9722006-06-18 05:43:12 +00001460 while (1) {
Chris Lattnercb283342006-06-18 06:48:37 +00001461 CurLexer->Lex(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001462
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001463 // If this is the end of the buffer, we have an error.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001464 if (Tok.is(tok::eof)) {
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001465 // Emit errors for each unterminated conditional on the stack, including
1466 // the current one.
1467 while (!CurLexer->ConditionalStack.empty()) {
1468 Diag(CurLexer->ConditionalStack.back().IfLoc,
1469 diag::err_pp_unterminated_conditional);
1470 CurLexer->ConditionalStack.pop_back();
1471 }
1472
1473 // Just return and let the caller lex after this #include.
1474 break;
1475 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001476
1477 // If this token is not a preprocessor directive, just skip it.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001478 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
Chris Lattner22eb9722006-06-18 05:43:12 +00001479 continue;
1480
1481 // We just parsed a # character at the start of a line, so we're in
1482 // directive mode. Tell the lexer this so any newlines we see will be
1483 // converted into an EOM token (this terminates the macro).
1484 CurLexer->ParsingPreprocessorDirective = true;
Chris Lattner457fc152006-07-29 06:30:25 +00001485 CurLexer->KeepCommentMode = false;
1486
Chris Lattner22eb9722006-06-18 05:43:12 +00001487
1488 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +00001489 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001490
1491 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
1492 // something bogus), skip it.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001493 if (Tok.isNot(tok::identifier)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001494 CurLexer->ParsingPreprocessorDirective = false;
Chris Lattner457fc152006-07-29 06:30:25 +00001495 // Restore comment saving mode.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001496 CurLexer->KeepCommentMode = KeepComments;
Chris Lattner22eb9722006-06-18 05:43:12 +00001497 continue;
1498 }
Chris Lattnere60165f2006-06-22 06:36:29 +00001499
Chris Lattner22eb9722006-06-18 05:43:12 +00001500 // If the first letter isn't i or e, it isn't intesting to us. We know that
1501 // this is safe in the face of spelling differences, because there is no way
1502 // to spell an i/e in a strange way that is another letter. Skipping this
Chris Lattnere60165f2006-06-22 06:36:29 +00001503 // allows us to avoid looking up the identifier info for #define/#undef and
1504 // other common directives.
1505 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
1506 char FirstChar = RawCharData[0];
Chris Lattner22eb9722006-06-18 05:43:12 +00001507 if (FirstChar >= 'a' && FirstChar <= 'z' &&
1508 FirstChar != 'i' && FirstChar != 'e') {
1509 CurLexer->ParsingPreprocessorDirective = false;
Chris Lattner457fc152006-07-29 06:30:25 +00001510 // Restore comment saving mode.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001511 CurLexer->KeepCommentMode = KeepComments;
Chris Lattner22eb9722006-06-18 05:43:12 +00001512 continue;
1513 }
1514
Chris Lattnere60165f2006-06-22 06:36:29 +00001515 // Get the identifier name without trigraphs or embedded newlines. Note
1516 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
1517 // when skipping.
1518 // TODO: could do this with zero copies in the no-clean case by using
1519 // strncmp below.
1520 char Directive[20];
1521 unsigned IdLen;
1522 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
1523 IdLen = Tok.getLength();
1524 memcpy(Directive, RawCharData, IdLen);
1525 Directive[IdLen] = 0;
1526 } else {
1527 std::string DirectiveStr = getSpelling(Tok);
1528 IdLen = DirectiveStr.size();
1529 if (IdLen >= 20) {
1530 CurLexer->ParsingPreprocessorDirective = false;
Chris Lattner457fc152006-07-29 06:30:25 +00001531 // Restore comment saving mode.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001532 CurLexer->KeepCommentMode = KeepComments;
Chris Lattnere60165f2006-06-22 06:36:29 +00001533 continue;
1534 }
1535 memcpy(Directive, &DirectiveStr[0], IdLen);
1536 Directive[IdLen] = 0;
1537 }
1538
Chris Lattner22eb9722006-06-18 05:43:12 +00001539 if (FirstChar == 'i' && Directive[1] == 'f') {
Chris Lattnere60165f2006-06-22 06:36:29 +00001540 if ((IdLen == 2) || // "if"
1541 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
1542 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
Chris Lattner22eb9722006-06-18 05:43:12 +00001543 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
1544 // bother parsing the condition.
Chris Lattnercb283342006-06-18 06:48:37 +00001545 DiscardUntilEndOfDirective();
Chris Lattner50b497e2006-06-18 16:32:35 +00001546 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerd01e2912006-06-18 16:22:51 +00001547 /*foundnonskip*/false,
1548 /*fnddelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001549 }
1550 } else if (FirstChar == 'e') {
Chris Lattnere60165f2006-06-22 06:36:29 +00001551 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
Chris Lattnercb283342006-06-18 06:48:37 +00001552 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00001553 PPConditionalInfo CondInfo;
1554 CondInfo.WasSkipping = true; // Silence bogus warning.
1555 bool InCond = CurLexer->popConditionalLevel(CondInfo);
Chris Lattnercf6bc662006-11-05 07:59:08 +00001556 InCond = InCond; // Silence warning in no-asserts mode.
Chris Lattner22eb9722006-06-18 05:43:12 +00001557 assert(!InCond && "Can't be skipping if not in a conditional!");
1558
1559 // If we popped the outermost skipping block, we're done skipping!
1560 if (!CondInfo.WasSkipping)
1561 break;
Chris Lattnere60165f2006-06-22 06:36:29 +00001562 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
Chris Lattner22eb9722006-06-18 05:43:12 +00001563 // #else directive in a skipping conditional. If not in some other
1564 // skipping conditional, and if #else hasn't already been seen, enter it
1565 // as a non-skipping conditional.
Chris Lattnercb283342006-06-18 06:48:37 +00001566 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00001567 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1568
1569 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001570 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001571
1572 // Note that we've seen a #else in this conditional.
1573 CondInfo.FoundElse = true;
1574
1575 // If the conditional is at the top level, and the #if block wasn't
1576 // entered, enter the #else block now.
1577 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
1578 CondInfo.FoundNonSkip = true;
1579 break;
1580 }
Chris Lattnere60165f2006-06-22 06:36:29 +00001581 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
Chris Lattner22eb9722006-06-18 05:43:12 +00001582 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1583
1584 bool ShouldEnter;
1585 // If this is in a skipping block or if we're already handled this #if
1586 // block, don't bother parsing the condition.
1587 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
Chris Lattnercb283342006-06-18 06:48:37 +00001588 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001589 ShouldEnter = false;
1590 } else {
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001591 // Restore the value of LexingRawMode so that identifiers are
Chris Lattner22eb9722006-06-18 05:43:12 +00001592 // looked up, etc, inside the #elif expression.
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001593 assert(CurLexer->LexingRawMode && "We have to be skipping here!");
1594 CurLexer->LexingRawMode = false;
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001595 IdentifierInfo *IfNDefMacro = 0;
Chris Lattnera8654ca2006-07-04 17:42:08 +00001596 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001597 CurLexer->LexingRawMode = true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001598 }
1599
1600 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001601 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001602
1603 // If this condition is true, enter it!
1604 if (ShouldEnter) {
1605 CondInfo.FoundNonSkip = true;
1606 break;
1607 }
1608 }
1609 }
1610
1611 CurLexer->ParsingPreprocessorDirective = false;
Chris Lattner457fc152006-07-29 06:30:25 +00001612 // Restore comment saving mode.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001613 CurLexer->KeepCommentMode = KeepComments;
Chris Lattner22eb9722006-06-18 05:43:12 +00001614 }
1615
1616 // Finally, if we are out of the conditional (saw an #endif or ran off the end
1617 // of the file, just stop skipping and return to lexing whatever came after
1618 // the #if block.
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001619 CurLexer->LexingRawMode = false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001620}
1621
1622//===----------------------------------------------------------------------===//
1623// Preprocessor Directive Handling.
1624//===----------------------------------------------------------------------===//
1625
1626/// HandleDirective - This callback is invoked when the lexer sees a # token
1627/// at the start of a line. This consumes the directive, modifies the
1628/// lexer/preprocessor state, and advances the lexer(s) so that the next token
1629/// read is the correct one.
Chris Lattner146762e2007-07-20 16:59:19 +00001630void Preprocessor::HandleDirective(Token &Result) {
Chris Lattner4d5e1a72006-07-03 01:01:29 +00001631 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
Chris Lattner22eb9722006-06-18 05:43:12 +00001632
1633 // We just parsed a # character at the start of a line, so we're in directive
1634 // mode. Tell the lexer this so any newlines we see will be converted into an
Chris Lattner78186052006-07-09 00:45:31 +00001635 // EOM token (which terminates the directive).
Chris Lattner22eb9722006-06-18 05:43:12 +00001636 CurLexer->ParsingPreprocessorDirective = true;
1637
1638 ++NumDirectives;
1639
Chris Lattner371ac8a2006-07-04 07:11:10 +00001640 // We are about to read a token. For the multiple-include optimization FA to
1641 // work, we have to remember if we had read any tokens *before* this
1642 // pp-directive.
1643 bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal();
1644
Chris Lattner78186052006-07-09 00:45:31 +00001645 // Read the next token, the directive flavor. This isn't expanded due to
1646 // C99 6.10.3p8.
Chris Lattnercb283342006-06-18 06:48:37 +00001647 LexUnexpandedToken(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001648
Chris Lattner78186052006-07-09 00:45:31 +00001649 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
1650 // #define A(x) #x
1651 // A(abc
1652 // #warning blah
1653 // def)
1654 // If so, the user is relying on non-portable behavior, emit a diagnostic.
Chris Lattneree8760b2006-07-15 07:42:55 +00001655 if (InMacroArgs)
Chris Lattner78186052006-07-09 00:45:31 +00001656 Diag(Result, diag::ext_embedded_directive);
1657
Chris Lattnerbcb416b2006-10-27 05:43:50 +00001658TryAgain:
Chris Lattner22eb9722006-06-18 05:43:12 +00001659 switch (Result.getKind()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001660 case tok::eom:
Chris Lattnercb283342006-06-18 06:48:37 +00001661 return; // null directive.
Chris Lattnerbcb416b2006-10-27 05:43:50 +00001662 case tok::comment:
1663 // Handle stuff like "# /*foo*/ define X" in -E -C mode.
1664 LexUnexpandedToken(Result);
1665 goto TryAgain;
Chris Lattner22eb9722006-06-18 05:43:12 +00001666
Chris Lattner22eb9722006-06-18 05:43:12 +00001667 case tok::numeric_constant:
1668 // FIXME: implement # 7 line numbers!
Chris Lattner6e5b2a02006-10-17 02:53:32 +00001669 DiscardUntilEndOfDirective();
1670 return;
Chris Lattner87d3bec2006-10-17 03:44:32 +00001671 default:
1672 IdentifierInfo *II = Result.getIdentifierInfo();
1673 if (II == 0) break; // Not an identifier.
1674
1675 // Ask what the preprocessor keyword ID is.
1676 switch (II->getPPKeywordID()) {
1677 default: break;
1678 // C99 6.10.1 - Conditional Inclusion.
1679 case tok::pp_if:
1680 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
1681 case tok::pp_ifdef:
1682 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
1683 case tok::pp_ifndef:
1684 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
1685 case tok::pp_elif:
1686 return HandleElifDirective(Result);
1687 case tok::pp_else:
1688 return HandleElseDirective(Result);
1689 case tok::pp_endif:
1690 return HandleEndifDirective(Result);
1691
1692 // C99 6.10.2 - Source File Inclusion.
1693 case tok::pp_include:
1694 return HandleIncludeDirective(Result); // Handle #include.
1695
1696 // C99 6.10.3 - Macro Replacement.
1697 case tok::pp_define:
1698 return HandleDefineDirective(Result, false);
1699 case tok::pp_undef:
1700 return HandleUndefDirective(Result);
1701
1702 // C99 6.10.4 - Line Control.
1703 case tok::pp_line:
1704 // FIXME: implement #line
1705 DiscardUntilEndOfDirective();
1706 return;
1707
1708 // C99 6.10.5 - Error Directive.
1709 case tok::pp_error:
1710 return HandleUserDiagnosticDirective(Result, false);
1711
1712 // C99 6.10.6 - Pragma Directive.
1713 case tok::pp_pragma:
1714 return HandlePragmaDirective();
1715
1716 // GNU Extensions.
1717 case tok::pp_import:
1718 return HandleImportDirective(Result);
1719 case tok::pp_include_next:
1720 return HandleIncludeNextDirective(Result);
1721
1722 case tok::pp_warning:
1723 Diag(Result, diag::ext_pp_warning_directive);
1724 return HandleUserDiagnosticDirective(Result, true);
1725 case tok::pp_ident:
1726 return HandleIdentSCCSDirective(Result);
1727 case tok::pp_sccs:
1728 return HandleIdentSCCSDirective(Result);
1729 case tok::pp_assert:
1730 //isExtension = true; // FIXME: implement #assert
Chris Lattner22eb9722006-06-18 05:43:12 +00001731 break;
Chris Lattner87d3bec2006-10-17 03:44:32 +00001732 case tok::pp_unassert:
1733 //isExtension = true; // FIXME: implement #unassert
Chris Lattner22eb9722006-06-18 05:43:12 +00001734 break;
Chris Lattner87d3bec2006-10-17 03:44:32 +00001735
1736 // clang extensions.
1737 case tok::pp_define_target:
1738 return HandleDefineDirective(Result, true);
1739 case tok::pp_define_other_target:
1740 return HandleDefineOtherTargetDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001741 }
1742 break;
1743 }
1744
1745 // If we reached here, the preprocessing token is not valid!
Chris Lattnercb283342006-06-18 06:48:37 +00001746 Diag(Result, diag::err_pp_invalid_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001747
1748 // Read the rest of the PP line.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001749 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001750
1751 // Okay, we're done parsing the directive.
Chris Lattner22eb9722006-06-18 05:43:12 +00001752}
1753
Chris Lattner146762e2007-07-20 16:59:19 +00001754void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001755 bool isWarning) {
1756 // Read the rest of the line raw. We do this because we don't want macros
1757 // to be expanded and we don't require that the tokens be valid preprocessing
1758 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1759 // collapse multiple consequtive white space between tokens, but this isn't
1760 // specified by the standard.
1761 std::string Message = CurLexer->ReadToEndOfLine();
1762
1763 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
Chris Lattner01d66cc2006-07-03 22:16:27 +00001764 return Diag(Tok, DiagID, Message);
1765}
1766
1767/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1768///
Chris Lattner146762e2007-07-20 16:59:19 +00001769void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001770 // Yes, this directive is an extension.
Chris Lattner01d66cc2006-07-03 22:16:27 +00001771 Diag(Tok, diag::ext_pp_ident_directive);
1772
Chris Lattner371ac8a2006-07-04 07:11:10 +00001773 // Read the string argument.
Chris Lattner146762e2007-07-20 16:59:19 +00001774 Token StrTok;
Chris Lattner01d66cc2006-07-03 22:16:27 +00001775 Lex(StrTok);
1776
1777 // If the token kind isn't a string, it's a malformed directive.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001778 if (StrTok.isNot(tok::string_literal) &&
1779 StrTok.isNot(tok::wide_string_literal))
Chris Lattner01d66cc2006-07-03 22:16:27 +00001780 return Diag(StrTok, diag::err_pp_malformed_ident);
1781
1782 // Verify that there is nothing after the string, other than EOM.
1783 CheckEndOfDirective("#ident");
1784
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +00001785 if (Callbacks)
1786 Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok));
Chris Lattner22eb9722006-06-18 05:43:12 +00001787}
1788
Chris Lattnerb8761832006-06-24 21:31:03 +00001789//===----------------------------------------------------------------------===//
1790// Preprocessor Include Directive Handling.
1791//===----------------------------------------------------------------------===//
1792
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001793/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1794/// checked and spelled filename, e.g. as an operand of #include. This returns
1795/// true if the input filename was in <>'s or false if it were in ""'s. The
1796/// caller is expected to provide a buffer that is large enough to hold the
1797/// spelling of the filename, but is also expected to handle the case when
1798/// this method decides to use a different buffer.
Chris Lattner93ab9f12007-07-23 04:15:27 +00001799bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001800 const char *&BufStart,
1801 const char *&BufEnd) {
1802 // Get the text form of the filename.
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001803 assert(BufStart != BufEnd && "Can't have tokens with empty spellings!");
1804
1805 // Make sure the filename is <x> or "x".
1806 bool isAngled;
1807 if (BufStart[0] == '<') {
1808 if (BufEnd[-1] != '>') {
Chris Lattner93ab9f12007-07-23 04:15:27 +00001809 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001810 BufStart = 0;
1811 return true;
1812 }
1813 isAngled = true;
1814 } else if (BufStart[0] == '"') {
1815 if (BufEnd[-1] != '"') {
Chris Lattner93ab9f12007-07-23 04:15:27 +00001816 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001817 BufStart = 0;
1818 return true;
1819 }
1820 isAngled = false;
1821 } else {
Chris Lattner93ab9f12007-07-23 04:15:27 +00001822 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001823 BufStart = 0;
1824 return true;
1825 }
1826
1827 // Diagnose #include "" as invalid.
1828 if (BufEnd-BufStart <= 2) {
Chris Lattner93ab9f12007-07-23 04:15:27 +00001829 Diag(Loc, diag::err_pp_empty_filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001830 BufStart = 0;
1831 return "";
1832 }
1833
1834 // Skip the brackets.
1835 ++BufStart;
1836 --BufEnd;
1837 return isAngled;
1838}
1839
Chris Lattner43eafb42007-07-23 04:56:47 +00001840/// ConcatenateIncludeName - Handle cases where the #include name is expanded
1841/// from a macro as multiple tokens, which need to be glued together. This
1842/// occurs for code like:
1843/// #define FOO <a/b.h>
1844/// #include FOO
1845/// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1846///
1847/// This code concatenates and consumes tokens up to the '>' token. It returns
1848/// false if the > was found, otherwise it returns true if it finds and consumes
1849/// the EOM marker.
1850static bool ConcatenateIncludeName(llvm::SmallVector<char, 128> &FilenameBuffer,
1851 Preprocessor &PP) {
1852 Token CurTok;
1853
1854 PP.Lex(CurTok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001855 while (CurTok.isNot(tok::eom)) {
Chris Lattner43eafb42007-07-23 04:56:47 +00001856 // Append the spelling of this token to the buffer. If there was a space
1857 // before it, add it now.
1858 if (CurTok.hasLeadingSpace())
1859 FilenameBuffer.push_back(' ');
1860
1861 // Get the spelling of the token, directly into FilenameBuffer if possible.
1862 unsigned PreAppendSize = FilenameBuffer.size();
1863 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1864
1865 const char *BufPtr = &FilenameBuffer[PreAppendSize];
1866 unsigned ActualLen = PP.getSpelling(CurTok, BufPtr);
1867
1868 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1869 if (BufPtr != &FilenameBuffer[PreAppendSize])
1870 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1871
1872 // Resize FilenameBuffer to the correct size.
1873 if (CurTok.getLength() != ActualLen)
1874 FilenameBuffer.resize(PreAppendSize+ActualLen);
1875
1876 // If we found the '>' marker, return success.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001877 if (CurTok.is(tok::greater))
Chris Lattner43eafb42007-07-23 04:56:47 +00001878 return false;
1879
1880 PP.Lex(CurTok);
1881 }
1882
1883 // If we hit the eom marker, emit an error and return true so that the caller
1884 // knows the EOM has been read.
1885 PP.Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1886 return true;
1887}
1888
Chris Lattner22eb9722006-06-18 05:43:12 +00001889/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1890/// file to be included from the lexer, then include it! This is a common
1891/// routine with functionality shared between #include, #include_next and
1892/// #import.
Chris Lattner146762e2007-07-20 16:59:19 +00001893void Preprocessor::HandleIncludeDirective(Token &IncludeTok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001894 const DirectoryLookup *LookupFrom,
1895 bool isImport) {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001896
Chris Lattner146762e2007-07-20 16:59:19 +00001897 Token FilenameTok;
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001898 CurLexer->LexIncludeFilename(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001899
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001900 // Reserve a buffer to get the spelling.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001901 llvm::SmallVector<char, 128> FilenameBuffer;
Chris Lattner43eafb42007-07-23 04:56:47 +00001902 const char *FilenameStart, *FilenameEnd;
1903
1904 switch (FilenameTok.getKind()) {
1905 case tok::eom:
1906 // If the token kind is EOM, the error has already been diagnosed.
1907 return;
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001908
Chris Lattner43eafb42007-07-23 04:56:47 +00001909 case tok::angle_string_literal:
Chris Lattnerf97dbcb2007-07-23 22:23:52 +00001910 case tok::string_literal: {
Chris Lattner43eafb42007-07-23 04:56:47 +00001911 FilenameBuffer.resize(FilenameTok.getLength());
1912 FilenameStart = &FilenameBuffer[0];
1913 unsigned Len = getSpelling(FilenameTok, FilenameStart);
1914 FilenameEnd = FilenameStart+Len;
1915 break;
Chris Lattnerf97dbcb2007-07-23 22:23:52 +00001916 }
Chris Lattner43eafb42007-07-23 04:56:47 +00001917
1918 case tok::less:
1919 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1920 // case, glue the tokens together into FilenameBuffer and interpret those.
1921 FilenameBuffer.push_back('<');
1922 if (ConcatenateIncludeName(FilenameBuffer, *this))
1923 return; // Found <eom> but no ">"? Diagnostic already emitted.
1924 FilenameStart = &FilenameBuffer[0];
1925 FilenameEnd = &FilenameBuffer[FilenameBuffer.size()];
1926 break;
1927 default:
1928 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1929 DiscardUntilEndOfDirective();
1930 return;
1931 }
1932
Chris Lattner93ab9f12007-07-23 04:15:27 +00001933 bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(),
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001934 FilenameStart, FilenameEnd);
1935 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1936 // error.
Chris Lattner43eafb42007-07-23 04:56:47 +00001937 if (FilenameStart == 0) {
1938 DiscardUntilEndOfDirective();
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001939 return;
Chris Lattner43eafb42007-07-23 04:56:47 +00001940 }
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001941
Chris Lattner269c2322006-06-25 06:23:00 +00001942 // Verify that there is nothing after the filename, other than EOM. Use the
1943 // preprocessor to lex this in case lexing the filename entered a macro.
1944 CheckEndOfDirective("#include");
Chris Lattner22eb9722006-06-18 05:43:12 +00001945
1946 // Check that we don't have infinite #include recursion.
Chris Lattner69772b02006-07-02 20:34:39 +00001947 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
Chris Lattner22eb9722006-06-18 05:43:12 +00001948 return Diag(FilenameTok, diag::err_pp_include_too_deep);
1949
Chris Lattner22eb9722006-06-18 05:43:12 +00001950 // Search include directories.
Chris Lattnerc8997182006-06-22 05:52:16 +00001951 const DirectoryLookup *CurDir;
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001952 const FileEntry *File = LookupFile(FilenameStart, FilenameEnd,
Chris Lattnerb8b94f12006-10-30 05:38:06 +00001953 isAngled, LookupFrom, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001954 if (File == 0)
Chris Lattner7c718bd2007-04-10 06:02:46 +00001955 return Diag(FilenameTok, diag::err_pp_file_not_found,
1956 std::string(FilenameStart, FilenameEnd));
Chris Lattner22eb9722006-06-18 05:43:12 +00001957
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001958 // Ask HeaderInfo if we should enter this #include file.
1959 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
1960 // If it returns true, #including this file will have no effect.
Chris Lattner3665f162006-07-04 07:26:10 +00001961 return;
1962 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001963
1964 // Look up the file, create a File ID for it.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001965 unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001966 if (FileID == 0)
Chris Lattner7c718bd2007-04-10 06:02:46 +00001967 return Diag(FilenameTok, diag::err_pp_file_not_found,
1968 std::string(FilenameStart, FilenameEnd));
Chris Lattner22eb9722006-06-18 05:43:12 +00001969
1970 // Finally, if all is good, enter the new file!
Chris Lattnerc8997182006-06-22 05:52:16 +00001971 EnterSourceFile(FileID, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001972}
1973
1974/// HandleIncludeNextDirective - Implements #include_next.
1975///
Chris Lattner146762e2007-07-20 16:59:19 +00001976void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) {
Chris Lattnercb283342006-06-18 06:48:37 +00001977 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001978
1979 // #include_next is like #include, except that we start searching after
1980 // the current found directory. If we can't do this, issue a
1981 // diagnostic.
Chris Lattnerc8997182006-06-22 05:52:16 +00001982 const DirectoryLookup *Lookup = CurDirLookup;
Chris Lattner69772b02006-07-02 20:34:39 +00001983 if (isInPrimaryFile()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001984 Lookup = 0;
Chris Lattnercb283342006-06-18 06:48:37 +00001985 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
Chris Lattner22eb9722006-06-18 05:43:12 +00001986 } else if (Lookup == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +00001987 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
Chris Lattnerc8997182006-06-22 05:52:16 +00001988 } else {
1989 // Start looking up in the next directory.
1990 ++Lookup;
Chris Lattner22eb9722006-06-18 05:43:12 +00001991 }
1992
1993 return HandleIncludeDirective(IncludeNextTok, Lookup);
1994}
1995
1996/// HandleImportDirective - Implements #import.
1997///
Chris Lattner146762e2007-07-20 16:59:19 +00001998void Preprocessor::HandleImportDirective(Token &ImportTok) {
Chris Lattnercb283342006-06-18 06:48:37 +00001999 Diag(ImportTok, diag::ext_pp_import_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00002000
2001 return HandleIncludeDirective(ImportTok, 0, true);
2002}
2003
Chris Lattnerb8761832006-06-24 21:31:03 +00002004//===----------------------------------------------------------------------===//
2005// Preprocessor Macro Directive Handling.
2006//===----------------------------------------------------------------------===//
2007
Chris Lattnercefc7682006-07-08 08:28:12 +00002008/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
2009/// definition has just been read. Lex the rest of the arguments and the
2010/// closing ), updating MI with what we learn. Return true if an error occurs
2011/// parsing the arg list.
2012bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
Chris Lattner564f4782007-07-14 22:46:43 +00002013 llvm::SmallVector<IdentifierInfo*, 32> Arguments;
2014
Chris Lattner146762e2007-07-20 16:59:19 +00002015 Token Tok;
Chris Lattnercefc7682006-07-08 08:28:12 +00002016 while (1) {
2017 LexUnexpandedToken(Tok);
2018 switch (Tok.getKind()) {
2019 case tok::r_paren:
2020 // Found the end of the argument list.
Chris Lattner564f4782007-07-14 22:46:43 +00002021 if (Arguments.empty()) { // #define FOO()
2022 MI->setArgumentList(Arguments.begin(), Arguments.end());
2023 return false;
2024 }
Chris Lattnercefc7682006-07-08 08:28:12 +00002025 // Otherwise we have #define FOO(A,)
2026 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
2027 return true;
2028 case tok::ellipsis: // #define X(... -> C99 varargs
2029 // Warn if use of C99 feature in non-C99 mode.
2030 if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
2031
2032 // Lex the token after the identifier.
2033 LexUnexpandedToken(Tok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002034 if (Tok.isNot(tok::r_paren)) {
Chris Lattnercefc7682006-07-08 08:28:12 +00002035 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2036 return true;
2037 }
Chris Lattner95a06b32006-07-30 08:40:43 +00002038 // Add the __VA_ARGS__ identifier as an argument.
Chris Lattner564f4782007-07-14 22:46:43 +00002039 Arguments.push_back(Ident__VA_ARGS__);
Chris Lattnercefc7682006-07-08 08:28:12 +00002040 MI->setIsC99Varargs();
Chris Lattner564f4782007-07-14 22:46:43 +00002041 MI->setArgumentList(Arguments.begin(), Arguments.end());
Chris Lattnercefc7682006-07-08 08:28:12 +00002042 return false;
2043 case tok::eom: // #define X(
2044 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2045 return true;
Chris Lattner62aa0d42006-10-20 05:08:24 +00002046 default:
2047 // Handle keywords and identifiers here to accept things like
2048 // #define Foo(for) for.
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002049 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner62aa0d42006-10-20 05:08:24 +00002050 if (II == 0) {
2051 // #define X(1
2052 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
2053 return true;
2054 }
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002055
2056 // If this is already used as an argument, it is used multiple times (e.g.
2057 // #define X(A,A.
Chris Lattner564f4782007-07-14 22:46:43 +00002058 if (std::find(Arguments.begin(), Arguments.end(), II) !=
2059 Arguments.end()) { // C99 6.10.3p6
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002060 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName());
2061 return true;
2062 }
2063
2064 // Add the argument to the macro info.
Chris Lattner564f4782007-07-14 22:46:43 +00002065 Arguments.push_back(II);
Chris Lattnercefc7682006-07-08 08:28:12 +00002066
2067 // Lex the token after the identifier.
2068 LexUnexpandedToken(Tok);
2069
2070 switch (Tok.getKind()) {
2071 default: // #define X(A B
2072 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
2073 return true;
2074 case tok::r_paren: // #define X(A)
Chris Lattner564f4782007-07-14 22:46:43 +00002075 MI->setArgumentList(Arguments.begin(), Arguments.end());
Chris Lattnercefc7682006-07-08 08:28:12 +00002076 return false;
2077 case tok::comma: // #define X(A,
2078 break;
2079 case tok::ellipsis: // #define X(A... -> GCC extension
2080 // Diagnose extension.
2081 Diag(Tok, diag::ext_named_variadic_macro);
2082
2083 // Lex the token after the identifier.
2084 LexUnexpandedToken(Tok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002085 if (Tok.isNot(tok::r_paren)) {
Chris Lattnercefc7682006-07-08 08:28:12 +00002086 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2087 return true;
2088 }
2089
2090 MI->setIsGNUVarargs();
Chris Lattner564f4782007-07-14 22:46:43 +00002091 MI->setArgumentList(Arguments.begin(), Arguments.end());
Chris Lattnercefc7682006-07-08 08:28:12 +00002092 return false;
2093 }
2094 }
2095 }
2096}
2097
Chris Lattner22eb9722006-06-18 05:43:12 +00002098/// HandleDefineDirective - Implements #define. This consumes the entire macro
Chris Lattner81278c62006-10-14 19:03:49 +00002099/// line then lets the caller lex the next real token. If 'isTargetSpecific' is
2100/// true, then this is a "#define_target", otherwise this is a "#define".
Chris Lattner22eb9722006-06-18 05:43:12 +00002101///
Chris Lattner146762e2007-07-20 16:59:19 +00002102void Preprocessor::HandleDefineDirective(Token &DefineTok,
Chris Lattner81278c62006-10-14 19:03:49 +00002103 bool isTargetSpecific) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002104 ++NumDefined;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002105
Chris Lattner146762e2007-07-20 16:59:19 +00002106 Token MacroNameTok;
Chris Lattnere8eef322006-07-08 07:01:00 +00002107 ReadMacroName(MacroNameTok, 1);
Chris Lattner22eb9722006-06-18 05:43:12 +00002108
2109 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002110 if (MacroNameTok.is(tok::eom))
Chris Lattnercb283342006-06-18 06:48:37 +00002111 return;
Chris Lattnerf40fe992007-07-14 22:11:41 +00002112
Chris Lattner457fc152006-07-29 06:30:25 +00002113 // If we are supposed to keep comments in #defines, reenable comment saving
2114 // mode.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00002115 CurLexer->KeepCommentMode = KeepMacroComments;
Chris Lattner457fc152006-07-29 06:30:25 +00002116
Chris Lattner063400e2006-10-14 19:54:15 +00002117 // Create the new macro.
Chris Lattner50b497e2006-06-18 16:32:35 +00002118 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
Chris Lattner81278c62006-10-14 19:03:49 +00002119 if (isTargetSpecific) MI->setIsTargetSpecific();
Chris Lattner22eb9722006-06-18 05:43:12 +00002120
Chris Lattner063400e2006-10-14 19:54:15 +00002121 // If the identifier is an 'other target' macro, clear this bit.
2122 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
2123
2124
Chris Lattner146762e2007-07-20 16:59:19 +00002125 Token Tok;
Chris Lattnercb283342006-06-18 06:48:37 +00002126 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00002127
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002128 // If this is a function-like macro definition, parse the argument list,
2129 // marking each of the identifiers as being used as macro arguments. Also,
2130 // check other constraints on the first token of the macro body.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002131 if (Tok.is(tok::eom)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002132 // If there is no body to this macro, we have no special handling here.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002133 } else if (Tok.is(tok::l_paren) && !Tok.hasLeadingSpace()) {
Chris Lattnercefc7682006-07-08 08:28:12 +00002134 // This is a function-like macro definition. Read the argument list.
2135 MI->setIsFunctionLike();
2136 if (ReadMacroDefinitionArgList(MI)) {
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002137 // Forget about MI.
Chris Lattnercefc7682006-07-08 08:28:12 +00002138 delete MI;
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002139 // Throw away the rest of the line.
Chris Lattnercefc7682006-07-08 08:28:12 +00002140 if (CurLexer->ParsingPreprocessorDirective)
2141 DiscardUntilEndOfDirective();
2142 return;
2143 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002144
Chris Lattner815a1f92006-07-08 20:48:04 +00002145 // Read the first token after the arg list for down below.
2146 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00002147 } else if (!Tok.hasLeadingSpace()) {
2148 // C99 requires whitespace between the macro definition and the body. Emit
2149 // a diagnostic for something like "#define X+".
2150 if (Features.C99) {
Chris Lattnercb283342006-06-18 06:48:37 +00002151 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattner22eb9722006-06-18 05:43:12 +00002152 } else {
2153 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
2154 // one in some cases!
2155 }
2156 } else {
2157 // This is a normal token with leading space. Clear the leading space
2158 // marker on the first token to get proper expansion.
Chris Lattner146762e2007-07-20 16:59:19 +00002159 Tok.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00002160 }
2161
Chris Lattner7e374832006-07-29 03:46:57 +00002162 // If this is a definition of a variadic C99 function-like macro, not using
2163 // the GNU named varargs extension, enabled __VA_ARGS__.
2164
2165 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
2166 // This gets unpoisoned where it is allowed.
2167 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
2168 if (MI->isC99Varargs())
2169 Ident__VA_ARGS__->setIsPoisoned(false);
2170
Chris Lattner22eb9722006-06-18 05:43:12 +00002171 // Read the rest of the macro body.
Chris Lattnera3834342007-07-14 21:54:03 +00002172 if (MI->isObjectLike()) {
2173 // Object-like macros are very simple, just read their body.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002174 while (Tok.isNot(tok::eom)) {
Chris Lattnera3834342007-07-14 21:54:03 +00002175 MI->AddTokenToBody(Tok);
Chris Lattner815a1f92006-07-08 20:48:04 +00002176 // Get the next token of the macro.
2177 LexUnexpandedToken(Tok);
Chris Lattner815a1f92006-07-08 20:48:04 +00002178 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002179
Chris Lattnera3834342007-07-14 21:54:03 +00002180 } else {
2181 // Otherwise, read the body of a function-like macro. This has to validate
2182 // the # (stringize) operator.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002183 while (Tok.isNot(tok::eom)) {
Chris Lattnera3834342007-07-14 21:54:03 +00002184 MI->AddTokenToBody(Tok);
Chris Lattnerbff18d52006-07-06 04:49:18 +00002185
Chris Lattnera3834342007-07-14 21:54:03 +00002186 // Check C99 6.10.3.2p1: ensure that # operators are followed by macro
2187 // parameters in function-like macro expansions.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002188 if (Tok.isNot(tok::hash)) {
Chris Lattnera3834342007-07-14 21:54:03 +00002189 // Get the next token of the macro.
2190 LexUnexpandedToken(Tok);
2191 continue;
2192 }
2193
2194 // Get the next token of the macro.
2195 LexUnexpandedToken(Tok);
2196
2197 // Not a macro arg identifier?
2198 if (!Tok.getIdentifierInfo() ||
2199 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
2200 Diag(Tok, diag::err_pp_stringize_not_parameter);
2201 delete MI;
2202
2203 // Disable __VA_ARGS__ again.
2204 Ident__VA_ARGS__->setIsPoisoned(true);
2205 return;
2206 }
2207
2208 // Things look ok, add the param name token to the macro.
2209 MI->AddTokenToBody(Tok);
2210
2211 // Get the next token of the macro.
2212 LexUnexpandedToken(Tok);
2213 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002214 }
Chris Lattner7e374832006-07-29 03:46:57 +00002215
Chris Lattnerf40fe992007-07-14 22:11:41 +00002216
Chris Lattner7e374832006-07-29 03:46:57 +00002217 // Disable __VA_ARGS__ again.
2218 Ident__VA_ARGS__->setIsPoisoned(true);
Chris Lattnerbff18d52006-07-06 04:49:18 +00002219
Chris Lattnerbff18d52006-07-06 04:49:18 +00002220 // Check that there is no paste (##) operator at the begining or end of the
2221 // replacement list.
Chris Lattner78186052006-07-09 00:45:31 +00002222 unsigned NumTokens = MI->getNumTokens();
Chris Lattnerbff18d52006-07-06 04:49:18 +00002223 if (NumTokens != 0) {
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002224 if (MI->getReplacementToken(0).is(tok::hashhash)) {
Chris Lattner815a1f92006-07-08 20:48:04 +00002225 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
Chris Lattnerbff18d52006-07-06 04:49:18 +00002226 delete MI;
Chris Lattner815a1f92006-07-08 20:48:04 +00002227 return;
Chris Lattnerbff18d52006-07-06 04:49:18 +00002228 }
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002229 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
Chris Lattner815a1f92006-07-08 20:48:04 +00002230 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
Chris Lattnerbff18d52006-07-06 04:49:18 +00002231 delete MI;
Chris Lattner815a1f92006-07-08 20:48:04 +00002232 return;
Chris Lattnerbff18d52006-07-06 04:49:18 +00002233 }
2234 }
2235
Chris Lattner13044d92006-07-03 05:16:44 +00002236 // If this is the primary source file, remember that this macro hasn't been
2237 // used yet.
2238 if (isInPrimaryFile())
2239 MI->setIsUsed(false);
2240
Chris Lattner22eb9722006-06-18 05:43:12 +00002241 // Finally, if this identifier already had a macro defined for it, verify that
2242 // the macro bodies are identical and free the old definition.
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002243 if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Chris Lattner13044d92006-07-03 05:16:44 +00002244 if (!OtherMI->isUsed())
2245 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2246
Chris Lattner22eb9722006-06-18 05:43:12 +00002247 // Macros must be identical. This means all tokes and whitespace separation
Chris Lattner21284df2006-07-08 07:16:08 +00002248 // must be the same. C99 6.10.3.2.
2249 if (!MI->isIdenticalTo(*OtherMI, *this)) {
Chris Lattnere8eef322006-07-08 07:01:00 +00002250 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef,
2251 MacroNameTok.getIdentifierInfo()->getName());
2252 Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2);
2253 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002254 delete OtherMI;
2255 }
2256
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002257 setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
Chris Lattner22eb9722006-06-18 05:43:12 +00002258}
2259
Chris Lattner063400e2006-10-14 19:54:15 +00002260/// HandleDefineOtherTargetDirective - Implements #define_other_target.
Chris Lattner146762e2007-07-20 16:59:19 +00002261void Preprocessor::HandleDefineOtherTargetDirective(Token &Tok) {
2262 Token MacroNameTok;
Chris Lattner063400e2006-10-14 19:54:15 +00002263 ReadMacroName(MacroNameTok, 1);
2264
2265 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002266 if (MacroNameTok.is(tok::eom))
Chris Lattner063400e2006-10-14 19:54:15 +00002267 return;
2268
2269 // Check to see if this is the last token on the #undef line.
2270 CheckEndOfDirective("#define_other_target");
2271
2272 // If there is already a macro defined by this name, turn it into a
2273 // target-specific define.
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002274 if (MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Chris Lattner063400e2006-10-14 19:54:15 +00002275 MI->setIsTargetSpecific(true);
2276 return;
2277 }
2278
2279 // Mark the identifier as being a macro on some other target.
2280 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro();
2281}
2282
Chris Lattner22eb9722006-06-18 05:43:12 +00002283
2284/// HandleUndefDirective - Implements #undef.
2285///
Chris Lattner146762e2007-07-20 16:59:19 +00002286void Preprocessor::HandleUndefDirective(Token &UndefTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002287 ++NumUndefined;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002288
Chris Lattner146762e2007-07-20 16:59:19 +00002289 Token MacroNameTok;
Chris Lattnere8eef322006-07-08 07:01:00 +00002290 ReadMacroName(MacroNameTok, 2);
Chris Lattner22eb9722006-06-18 05:43:12 +00002291
2292 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002293 if (MacroNameTok.is(tok::eom))
Chris Lattnercb283342006-06-18 06:48:37 +00002294 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00002295
2296 // Check to see if this is the last token on the #undef line.
Chris Lattnercb283342006-06-18 06:48:37 +00002297 CheckEndOfDirective("#undef");
Chris Lattner22eb9722006-06-18 05:43:12 +00002298
2299 // Okay, we finally have a valid identifier to undef.
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002300 MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
Chris Lattner22eb9722006-06-18 05:43:12 +00002301
Chris Lattner063400e2006-10-14 19:54:15 +00002302 // #undef untaints an identifier if it were marked by define_other_target.
2303 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
2304
Chris Lattner22eb9722006-06-18 05:43:12 +00002305 // If the macro is not defined, this is a noop undef, just return.
Chris Lattnercb283342006-06-18 06:48:37 +00002306 if (MI == 0) return;
Chris Lattner677757a2006-06-28 05:26:32 +00002307
Chris Lattner13044d92006-07-03 05:16:44 +00002308 if (!MI->isUsed())
2309 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattner22eb9722006-06-18 05:43:12 +00002310
2311 // Free macro definition.
2312 delete MI;
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002313 setMacroInfo(MacroNameTok.getIdentifierInfo(), 0);
Chris Lattner22eb9722006-06-18 05:43:12 +00002314}
2315
2316
Chris Lattnerb8761832006-06-24 21:31:03 +00002317//===----------------------------------------------------------------------===//
2318// Preprocessor Conditional Directive Handling.
2319//===----------------------------------------------------------------------===//
2320
Chris Lattner22eb9722006-06-18 05:43:12 +00002321/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
Chris Lattner371ac8a2006-07-04 07:11:10 +00002322/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
2323/// if any tokens have been returned or pp-directives activated before this
2324/// #ifndef has been lexed.
Chris Lattner22eb9722006-06-18 05:43:12 +00002325///
Chris Lattner146762e2007-07-20 16:59:19 +00002326void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
Chris Lattner371ac8a2006-07-04 07:11:10 +00002327 bool ReadAnyTokensBeforeDirective) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002328 ++NumIf;
Chris Lattner146762e2007-07-20 16:59:19 +00002329 Token DirectiveTok = Result;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002330
Chris Lattner146762e2007-07-20 16:59:19 +00002331 Token MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00002332 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00002333
2334 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002335 if (MacroNameTok.is(tok::eom)) {
Chris Lattnerd05e44e2007-09-24 05:14:57 +00002336 // Skip code until we get to #endif. This helps with recovery by not
2337 // emitting an error when the #endif is reached.
2338 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2339 /*Foundnonskip*/false, /*FoundElse*/false);
Chris Lattnercb283342006-06-18 06:48:37 +00002340 return;
Chris Lattnerd05e44e2007-09-24 05:14:57 +00002341 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002342
2343 // Check to see if this is the last token on the #if[n]def line.
Chris Lattner371ac8a2006-07-04 07:11:10 +00002344 CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
2345
2346 // If the start of a top-level #ifdef, inform MIOpt.
2347 if (!ReadAnyTokensBeforeDirective &&
2348 CurLexer->getConditionalStackDepth() == 0) {
2349 assert(isIfndef && "#ifdef shouldn't reach here");
2350 CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
2351 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002352
Chris Lattner063400e2006-10-14 19:54:15 +00002353 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002354 MacroInfo *MI = getMacroInfo(MII);
Chris Lattnera78a97e2006-07-03 05:42:18 +00002355
Chris Lattner81278c62006-10-14 19:03:49 +00002356 // If there is a macro, process it.
2357 if (MI) {
2358 // Mark it used.
2359 MI->setIsUsed(true);
2360
2361 // If this is the first use of a target-specific macro, warn about it.
2362 if (MI->isTargetSpecific()) {
2363 MI->setIsTargetSpecific(false); // Don't warn on second use.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +00002364 getTargetInfo().DiagnoseNonPortability(
2365 getFullLoc(MacroNameTok.getLocation()),
2366 diag::port_target_macro_use);
Chris Lattner81278c62006-10-14 19:03:49 +00002367 }
Chris Lattner063400e2006-10-14 19:54:15 +00002368 } else {
2369 // Use of a target-specific macro for some other target? If so, warn.
2370 if (MII->isOtherTargetMacro()) {
2371 MII->setIsOtherTargetMacro(false); // Don't warn on second use.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +00002372 getTargetInfo().DiagnoseNonPortability(
2373 getFullLoc(MacroNameTok.getLocation()),
2374 diag::port_target_macro_use);
Chris Lattner063400e2006-10-14 19:54:15 +00002375 }
Chris Lattner81278c62006-10-14 19:03:49 +00002376 }
Chris Lattnera78a97e2006-07-03 05:42:18 +00002377
Chris Lattner22eb9722006-06-18 05:43:12 +00002378 // Should we include the stuff contained by this directive?
Chris Lattnera78a97e2006-07-03 05:42:18 +00002379 if (!MI == isIfndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002380 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00002381 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00002382 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00002383 } else {
2384 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00002385 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Chris Lattnercb283342006-06-18 06:48:37 +00002386 /*Foundnonskip*/false,
2387 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00002388 }
2389}
2390
2391/// HandleIfDirective - Implements the #if directive.
2392///
Chris Lattner146762e2007-07-20 16:59:19 +00002393void Preprocessor::HandleIfDirective(Token &IfToken,
Chris Lattnera8654ca2006-07-04 17:42:08 +00002394 bool ReadAnyTokensBeforeDirective) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002395 ++NumIf;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002396
Chris Lattner371ac8a2006-07-04 07:11:10 +00002397 // Parse and evaluation the conditional expression.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00002398 IdentifierInfo *IfNDefMacro = 0;
Chris Lattnera8654ca2006-07-04 17:42:08 +00002399 bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
Chris Lattner22eb9722006-06-18 05:43:12 +00002400
2401 // Should we include the stuff contained by this directive?
2402 if (ConditionalTrue) {
Chris Lattnera8654ca2006-07-04 17:42:08 +00002403 // If this condition is equivalent to #ifndef X, and if this is the first
2404 // directive seen, handle it for the multiple-include optimization.
2405 if (!ReadAnyTokensBeforeDirective &&
2406 CurLexer->getConditionalStackDepth() == 0 && IfNDefMacro)
2407 CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
2408
Chris Lattner22eb9722006-06-18 05:43:12 +00002409 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00002410 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00002411 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00002412 } else {
2413 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00002414 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnercb283342006-06-18 06:48:37 +00002415 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00002416 }
2417}
2418
2419/// HandleEndifDirective - Implements the #endif directive.
2420///
Chris Lattner146762e2007-07-20 16:59:19 +00002421void Preprocessor::HandleEndifDirective(Token &EndifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002422 ++NumEndif;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002423
Chris Lattner22eb9722006-06-18 05:43:12 +00002424 // Check that this is the whole directive.
Chris Lattnercb283342006-06-18 06:48:37 +00002425 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00002426
2427 PPConditionalInfo CondInfo;
2428 if (CurLexer->popConditionalLevel(CondInfo)) {
2429 // No conditionals on the stack: this is an #endif without an #if.
2430 return Diag(EndifToken, diag::err_pp_endif_without_if);
2431 }
2432
Chris Lattner371ac8a2006-07-04 07:11:10 +00002433 // If this the end of a top-level #endif, inform MIOpt.
2434 if (CurLexer->getConditionalStackDepth() == 0)
2435 CurLexer->MIOpt.ExitTopLevelConditional();
2436
Chris Lattner538d7f32006-07-20 04:31:52 +00002437 assert(!CondInfo.WasSkipping && !CurLexer->LexingRawMode &&
Chris Lattner22eb9722006-06-18 05:43:12 +00002438 "This code should only be reachable in the non-skipping case!");
Chris Lattner22eb9722006-06-18 05:43:12 +00002439}
2440
2441
Chris Lattner146762e2007-07-20 16:59:19 +00002442void Preprocessor::HandleElseDirective(Token &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002443 ++NumElse;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002444
Chris Lattner22eb9722006-06-18 05:43:12 +00002445 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnercb283342006-06-18 06:48:37 +00002446 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00002447
2448 PPConditionalInfo CI;
2449 if (CurLexer->popConditionalLevel(CI))
2450 return Diag(Result, diag::pp_err_else_without_if);
Chris Lattner371ac8a2006-07-04 07:11:10 +00002451
2452 // If this is a top-level #else, inform the MIOpt.
2453 if (CurLexer->getConditionalStackDepth() == 0)
2454 CurLexer->MIOpt.FoundTopLevelElse();
Chris Lattner22eb9722006-06-18 05:43:12 +00002455
2456 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00002457 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00002458
2459 // Finally, skip the rest of the contents of this block and return the first
2460 // token after it.
2461 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2462 /*FoundElse*/true);
2463}
2464
Chris Lattner146762e2007-07-20 16:59:19 +00002465void Preprocessor::HandleElifDirective(Token &ElifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002466 ++NumElse;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002467
Chris Lattner22eb9722006-06-18 05:43:12 +00002468 // #elif directive in a non-skipping conditional... start skipping.
2469 // We don't care what the condition is, because we will always skip it (since
2470 // the block immediately before it was included).
Chris Lattnercb283342006-06-18 06:48:37 +00002471 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00002472
2473 PPConditionalInfo CI;
2474 if (CurLexer->popConditionalLevel(CI))
2475 return Diag(ElifToken, diag::pp_err_elif_without_if);
2476
Chris Lattner371ac8a2006-07-04 07:11:10 +00002477 // If this is a top-level #elif, inform the MIOpt.
2478 if (CurLexer->getConditionalStackDepth() == 0)
2479 CurLexer->MIOpt.FoundTopLevelElse();
2480
Chris Lattner22eb9722006-06-18 05:43:12 +00002481 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00002482 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00002483
2484 // Finally, skip the rest of the contents of this block and return the first
2485 // token after it.
2486 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2487 /*FoundElse*/CI.FoundElse);
2488}
Chris Lattnerb8761832006-06-24 21:31:03 +00002489