blob: 34b5fe21833dc62c81ae2241052d22f4cf702154 [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 Lattnerdc5c0552007-07-20 16:37:10 +0000503 CurFileEnt = SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc());
Chris Lattner7cdbad92006-10-30 05:33:15 +0000504 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
505 CurFileEnt)))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000506 return FE;
507 }
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 Lattnerdc5c0552007-07-20 16:37:10 +0000512 CurFileEnt = SourceMgr.getFileEntryForLoc(ISEntry.TheLexer->getFileLoc());
Chris Lattner7cdbad92006-10-30 05:33:15 +0000513 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
514 CurFileEnt)))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000515 return FE;
516 }
517 }
518
519 // Otherwise, we really couldn't find the file.
520 return 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000521}
522
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000523/// isInPrimaryFile - Return true if we're in the top-level file, not in a
524/// #include.
525bool Preprocessor::isInPrimaryFile() const {
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000526 if (CurLexer && !CurLexer->Is_PragmaLexer)
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000527 return IncludeMacroStack.empty();
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000528
Chris Lattner13044d92006-07-03 05:16:44 +0000529 // If there are any stacked lexers, we're in a #include.
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000530 assert(IncludeMacroStack[0].TheLexer &&
531 !IncludeMacroStack[0].TheLexer->Is_PragmaLexer &&
532 "Top level include stack isn't our primary lexer?");
533 for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i)
Chris Lattner13044d92006-07-03 05:16:44 +0000534 if (IncludeMacroStack[i].TheLexer &&
535 !IncludeMacroStack[i].TheLexer->Is_PragmaLexer)
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000536 return false;
537 return true;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000538}
539
540/// getCurrentLexer - Return the current file lexer being lexed from. Note
541/// that this ignores any potentially active macro expansions and _Pragma
542/// expansions going on at the time.
543Lexer *Preprocessor::getCurrentFileLexer() const {
544 if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer;
545
546 // Look for a stacked lexer.
547 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Chris Lattnerf88c53a2006-07-03 05:26:05 +0000548 Lexer *L = IncludeMacroStack[i-1].TheLexer;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000549 if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions.
550 return L;
551 }
552 return 0;
553}
554
555
Chris Lattner22eb9722006-06-18 05:43:12 +0000556/// EnterSourceFile - Add a source file to the top of the include stack and
557/// start lexing tokens from it instead of the current buffer. Return true
558/// on failure.
559void Preprocessor::EnterSourceFile(unsigned FileID,
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000560 const DirectoryLookup *CurDir) {
Chris Lattner69772b02006-07-02 20:34:39 +0000561 assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000562 ++NumEnteredSourceFiles;
563
Chris Lattner69772b02006-07-02 20:34:39 +0000564 if (MaxIncludeStackDepth < IncludeMacroStack.size())
565 MaxIncludeStackDepth = IncludeMacroStack.size();
Chris Lattner22eb9722006-06-18 05:43:12 +0000566
Chris Lattner77e9de52007-07-20 16:52:03 +0000567 Lexer *TheLexer = new Lexer(SourceLocation::getFileLoc(FileID, 0), *this);
Chris Lattner69772b02006-07-02 20:34:39 +0000568 EnterSourceFileWithLexer(TheLexer, CurDir);
569}
Chris Lattner22eb9722006-06-18 05:43:12 +0000570
Chris Lattner69772b02006-07-02 20:34:39 +0000571/// EnterSourceFile - Add a source file to the top of the include stack and
572/// start lexing tokens from it instead of the current buffer.
573void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
574 const DirectoryLookup *CurDir) {
575
576 // Add the current lexer to the include stack.
577 if (CurLexer || CurMacroExpander)
578 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
579 CurMacroExpander));
580
581 CurLexer = TheLexer;
Chris Lattnerc8997182006-06-22 05:52:16 +0000582 CurDirLookup = CurDir;
Chris Lattner69772b02006-07-02 20:34:39 +0000583 CurMacroExpander = 0;
Chris Lattner0c885f52006-06-21 06:50:18 +0000584
585 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000586 if (Callbacks && !CurLexer->Is_PragmaLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000587 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
588
589 // Get the file entry for the current file.
590 if (const FileEntry *FE =
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000591 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000592 FileType = HeaderInfo.getFileDirFlavor(FE);
Chris Lattnerc8997182006-06-22 05:52:16 +0000593
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000594 Callbacks->FileChanged(CurLexer->getFileLoc(),
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000595 PPCallbacks::EnterFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +0000596 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000597}
598
Chris Lattner69772b02006-07-02 20:34:39 +0000599
600
Chris Lattner22eb9722006-06-18 05:43:12 +0000601/// EnterMacro - Add a Macro to the top of the include stack and start lexing
Chris Lattnercb283342006-06-18 06:48:37 +0000602/// tokens from it instead of the current buffer.
Chris Lattner146762e2007-07-20 16:59:19 +0000603void Preprocessor::EnterMacro(Token &Tok, MacroArgs *Args) {
Chris Lattner69772b02006-07-02 20:34:39 +0000604 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
605 CurMacroExpander));
606 CurLexer = 0;
607 CurDirLookup = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000608
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000609 if (NumCachedMacroExpanders == 0) {
610 CurMacroExpander = new MacroExpander(Tok, Args, *this);
611 } else {
612 CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
613 CurMacroExpander->Init(Tok, Args);
614 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000615}
616
Chris Lattner7667d0d2006-07-16 18:16:58 +0000617/// EnterTokenStream - Add a "macro" context to the top of the include stack,
618/// which will cause the lexer to start returning the specified tokens. Note
619/// that these tokens will be re-macro-expanded when/if expansion is enabled.
620/// This method assumes that the specified stream of tokens has a permanent
621/// owner somewhere, so they do not need to be copied.
Chris Lattner146762e2007-07-20 16:59:19 +0000622void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks) {
Chris Lattner7667d0d2006-07-16 18:16:58 +0000623 // Save our current state.
624 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
625 CurMacroExpander));
626 CurLexer = 0;
627 CurDirLookup = 0;
628
629 // Create a macro expander to expand from the specified token stream.
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000630 if (NumCachedMacroExpanders == 0) {
631 CurMacroExpander = new MacroExpander(Toks, NumToks, *this);
632 } else {
633 CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
634 CurMacroExpander->Init(Toks, NumToks);
635 }
Chris Lattner7667d0d2006-07-16 18:16:58 +0000636}
637
638/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
639/// lexer stack. This should only be used in situations where the current
640/// state of the top-of-stack lexer is known.
641void Preprocessor::RemoveTopOfLexerStack() {
642 assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000643
644 if (CurMacroExpander) {
645 // Delete or cache the now-dead macro expander.
646 if (NumCachedMacroExpanders == MacroExpanderCacheSize)
647 delete CurMacroExpander;
648 else
649 MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander;
650 } else {
651 delete CurLexer;
652 }
Chris Lattner7667d0d2006-07-16 18:16:58 +0000653 CurLexer = IncludeMacroStack.back().TheLexer;
654 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
655 CurMacroExpander = IncludeMacroStack.back().TheMacroExpander;
656 IncludeMacroStack.pop_back();
657}
658
Chris Lattner22eb9722006-06-18 05:43:12 +0000659//===----------------------------------------------------------------------===//
Chris Lattner677757a2006-06-28 05:26:32 +0000660// Macro Expansion Handling.
Chris Lattner22eb9722006-06-18 05:43:12 +0000661//===----------------------------------------------------------------------===//
662
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000663/// setMacroInfo - Specify a macro for this identifier.
664///
665void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
666 if (MI == 0) {
667 if (II->hasMacroDefinition()) {
668 Macros.erase(II);
669 II->setHasMacroDefinition(false);
670 }
671 } else {
672 Macros[II] = MI;
673 II->setHasMacroDefinition(true);
674 }
675}
676
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000677/// RegisterBuiltinMacro - Register the specified identifier in the identifier
678/// table and mark it as a builtin macro to be expanded.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000679IdentifierInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000680 // Get the identifier.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000681 IdentifierInfo *Id = getIdentifierInfo(Name);
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000682
683 // Mark it as being a macro that is builtin.
684 MacroInfo *MI = new MacroInfo(SourceLocation());
685 MI->setIsBuiltinMacro();
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000686 setMacroInfo(Id, MI);
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000687 return Id;
688}
689
690
Chris Lattner677757a2006-06-28 05:26:32 +0000691/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
692/// identifier table.
693void Preprocessor::RegisterBuiltinMacros() {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000694 Ident__LINE__ = RegisterBuiltinMacro("__LINE__");
Chris Lattner630b33c2006-07-01 22:46:53 +0000695 Ident__FILE__ = RegisterBuiltinMacro("__FILE__");
Chris Lattnerc673f902006-06-30 06:10:41 +0000696 Ident__DATE__ = RegisterBuiltinMacro("__DATE__");
697 Ident__TIME__ = RegisterBuiltinMacro("__TIME__");
Chris Lattner69772b02006-07-02 20:34:39 +0000698 Ident_Pragma = RegisterBuiltinMacro("_Pragma");
Chris Lattnerc1283b92006-07-01 23:16:30 +0000699
700 // GCC Extensions.
701 Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__");
702 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__");
Chris Lattner847e0e42006-07-01 23:49:16 +0000703 Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__");
Chris Lattner22eb9722006-06-18 05:43:12 +0000704}
705
Chris Lattnerc2395832006-07-09 00:57:04 +0000706/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
707/// in its expansion, currently expands to that token literally.
Chris Lattner3ce1d1a2006-07-09 01:00:18 +0000708static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000709 const IdentifierInfo *MacroIdent,
710 Preprocessor &PP) {
Chris Lattnerc2395832006-07-09 00:57:04 +0000711 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
712
713 // If the token isn't an identifier, it's always literally expanded.
714 if (II == 0) return true;
715
716 // If the identifier is a macro, and if that macro is enabled, it may be
717 // expanded so it's not a trivial expansion.
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000718 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
Chris Lattner3ce1d1a2006-07-09 01:00:18 +0000719 // Fast expanding "#define X X" is ok, because X would be disabled.
720 II != MacroIdent)
Chris Lattnerc2395832006-07-09 00:57:04 +0000721 return false;
722
723 // If this is an object-like macro invocation, it is safe to trivially expand
724 // it.
725 if (MI->isObjectLike()) return true;
726
727 // If this is a function-like macro invocation, it's safe to trivially expand
728 // as long as the identifier is not a macro argument.
729 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
730 I != E; ++I)
731 if (*I == II)
732 return false; // Identifier is a macro argument.
Chris Lattner273ddd52006-07-29 07:33:01 +0000733
Chris Lattnerc2395832006-07-09 00:57:04 +0000734 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000735}
736
Chris Lattnerc2395832006-07-09 00:57:04 +0000737
Chris Lattnerafe603f2006-07-11 04:02:46 +0000738/// isNextPPTokenLParen - Determine whether the next preprocessor token to be
739/// lexed is a '('. If so, consume the token and return true, if not, this
740/// method should have no observable side-effect on the lexed tokens.
741bool Preprocessor::isNextPPTokenLParen() {
Chris Lattnerafe603f2006-07-11 04:02:46 +0000742 // Do some quick tests for rejection cases.
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000743 unsigned Val;
744 if (CurLexer)
Chris Lattner678c8802006-07-11 05:46:12 +0000745 Val = CurLexer->isNextPPTokenLParen();
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000746 else
747 Val = CurMacroExpander->isNextTokenLParen();
748
749 if (Val == 2) {
Chris Lattner5c983792007-07-19 00:07:36 +0000750 // We have run off the end. If it's a source file we don't
751 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
752 // macro stack.
753 if (CurLexer)
754 return false;
755 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000756 IncludeStackInfo &Entry = IncludeMacroStack[i-1];
757 if (Entry.TheLexer)
Chris Lattner678c8802006-07-11 05:46:12 +0000758 Val = Entry.TheLexer->isNextPPTokenLParen();
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000759 else
760 Val = Entry.TheMacroExpander->isNextTokenLParen();
Chris Lattner5c983792007-07-19 00:07:36 +0000761
762 if (Val != 2)
763 break;
764
765 // Ran off the end of a source file?
766 if (Entry.TheLexer)
767 return false;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000768 }
Chris Lattnerafe603f2006-07-11 04:02:46 +0000769 }
770
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000771 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
772 // have found something that isn't a '(' or we found the end of the
773 // translation unit. In either case, return false.
774 if (Val != 1)
775 return false;
Chris Lattnerafe603f2006-07-11 04:02:46 +0000776
Chris Lattner146762e2007-07-20 16:59:19 +0000777 Token Tok;
Chris Lattnerafe603f2006-07-11 04:02:46 +0000778 LexUnexpandedToken(Tok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000779 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000780 return true;
Chris Lattnerafe603f2006-07-11 04:02:46 +0000781}
Chris Lattner677757a2006-06-28 05:26:32 +0000782
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000783/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
784/// expanded as a macro, handle it and return the next token as 'Identifier'.
Chris Lattner146762e2007-07-20 16:59:19 +0000785bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000786 MacroInfo *MI) {
Chris Lattnera30be592008-01-07 19:50:27 +0000787 // If this is a macro exapnsion in the "#if !defined(x)" line for the file,
788 // then the macro could expand to different things in other contexts, we need
789 // to disable the optimization in this case.
790 if (CurLexer) CurLexer->MIOpt.ExpandedMacro();
Chris Lattner78186052006-07-09 00:45:31 +0000791
792 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
793 if (MI->isBuiltinMacro()) {
794 ExpandBuiltinMacro(Identifier);
795 return false;
796 }
797
Chris Lattner81278c62006-10-14 19:03:49 +0000798 // If this is the first use of a target-specific macro, warn about it.
799 if (MI->isTargetSpecific()) {
800 MI->setIsTargetSpecific(false); // Don't warn on second use.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000801 getTargetInfo().DiagnoseNonPortability(getFullLoc(Identifier.getLocation()),
Chris Lattner81278c62006-10-14 19:03:49 +0000802 diag::port_target_macro_use);
803 }
804
Chris Lattneree8760b2006-07-15 07:42:55 +0000805 /// Args - If this is a function-like macro expansion, this contains,
Chris Lattner78186052006-07-09 00:45:31 +0000806 /// for each macro argument, the list of tokens that were provided to the
807 /// invocation.
Chris Lattneree8760b2006-07-15 07:42:55 +0000808 MacroArgs *Args = 0;
Chris Lattner78186052006-07-09 00:45:31 +0000809
810 // If this is a function-like macro, read the arguments.
811 if (MI->isFunctionLike()) {
Chris Lattner78186052006-07-09 00:45:31 +0000812 // C99 6.10.3p10: If the preprocessing token immediately after the the macro
Chris Lattner24dbee72007-07-19 16:11:58 +0000813 // name isn't a '(', this macro should not be expanded. Otherwise, consume
814 // it.
Chris Lattnerafe603f2006-07-11 04:02:46 +0000815 if (!isNextPPTokenLParen())
Chris Lattner78186052006-07-09 00:45:31 +0000816 return true;
817
Chris Lattner78186052006-07-09 00:45:31 +0000818 // Remember that we are now parsing the arguments to a macro invocation.
819 // Preprocessor directives used inside macro arguments are not portable, and
820 // this enables the warning.
Chris Lattneree8760b2006-07-15 07:42:55 +0000821 InMacroArgs = true;
822 Args = ReadFunctionLikeMacroArgs(Identifier, MI);
Chris Lattner78186052006-07-09 00:45:31 +0000823
824 // Finished parsing args.
Chris Lattneree8760b2006-07-15 07:42:55 +0000825 InMacroArgs = false;
Chris Lattner78186052006-07-09 00:45:31 +0000826
827 // If there was an error parsing the arguments, bail out.
Chris Lattneree8760b2006-07-15 07:42:55 +0000828 if (Args == 0) return false;
Chris Lattner78186052006-07-09 00:45:31 +0000829
830 ++NumFnMacroExpanded;
831 } else {
832 ++NumMacroExpanded;
833 }
Chris Lattner13044d92006-07-03 05:16:44 +0000834
835 // Notice that this macro has been used.
836 MI->setIsUsed(true);
Chris Lattner69772b02006-07-02 20:34:39 +0000837
838 // If we started lexing a macro, enter the macro expansion body.
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000839
840 // If this macro expands to no tokens, don't bother to push it onto the
841 // expansion stack, only to take it right back off.
842 if (MI->getNumTokens() == 0) {
Chris Lattner2ada5d32006-07-15 07:51:24 +0000843 // No need for arg info.
Chris Lattnerc1410dc2006-07-26 05:22:49 +0000844 if (Args) Args->destroy();
Chris Lattner78186052006-07-09 00:45:31 +0000845
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000846 // Ignore this macro use, just return the next token in the current
847 // buffer.
848 bool HadLeadingSpace = Identifier.hasLeadingSpace();
849 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
850
851 Lex(Identifier);
852
853 // If the identifier isn't on some OTHER line, inherit the leading
854 // whitespace/first-on-a-line property of this token. This handles
855 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
856 // empty.
857 if (!Identifier.isAtStartOfLine()) {
Chris Lattner146762e2007-07-20 16:59:19 +0000858 if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
859 if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000860 }
861 ++NumFastMacroExpanded;
Chris Lattner78186052006-07-09 00:45:31 +0000862 return false;
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000863
Chris Lattner3ce1d1a2006-07-09 01:00:18 +0000864 } else if (MI->getNumTokens() == 1 &&
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000865 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
866 *this)){
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000867 // Otherwise, if this macro expands into a single trivially-expanded
868 // token: expand it now. This handles common cases like
869 // "#define VAL 42".
870
871 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
872 // identifier to the expanded token.
873 bool isAtStartOfLine = Identifier.isAtStartOfLine();
874 bool hasLeadingSpace = Identifier.hasLeadingSpace();
875
876 // Remember where the token is instantiated.
877 SourceLocation InstantiateLoc = Identifier.getLocation();
878
879 // Replace the result token.
880 Identifier = MI->getReplacementToken(0);
881
882 // Restore the StartOfLine/LeadingSpace markers.
Chris Lattner146762e2007-07-20 16:59:19 +0000883 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
884 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000885
886 // Update the tokens location to include both its logical and physical
887 // locations.
888 SourceLocation Loc =
Chris Lattnerc673f902006-06-30 06:10:41 +0000889 SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc);
Chris Lattner8c204872006-10-14 05:19:21 +0000890 Identifier.setLocation(Loc);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000891
Chris Lattner6e4bf522006-07-27 06:59:25 +0000892 // If this is #define X X, we must mark the result as unexpandible.
893 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo())
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000894 if (getMacroInfo(NewII) == MI)
Chris Lattner146762e2007-07-20 16:59:19 +0000895 Identifier.setFlag(Token::DisableExpand);
Chris Lattner6e4bf522006-07-27 06:59:25 +0000896
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000897 // Since this is not an identifier token, it can't be macro expanded, so
898 // we're done.
899 ++NumFastMacroExpanded;
Chris Lattner78186052006-07-09 00:45:31 +0000900 return false;
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000901 }
902
Chris Lattner78186052006-07-09 00:45:31 +0000903 // Start expanding the macro.
Chris Lattneree8760b2006-07-15 07:42:55 +0000904 EnterMacro(Identifier, Args);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000905
906 // Now that the macro is at the top of the include stack, ask the
907 // preprocessor to read the next token from it.
Chris Lattner78186052006-07-09 00:45:31 +0000908 Lex(Identifier);
909 return false;
910}
911
Chris Lattneree8760b2006-07-15 07:42:55 +0000912/// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
Chris Lattner2ada5d32006-07-15 07:51:24 +0000913/// invoked to read all of the actual arguments specified for the macro
Chris Lattner78186052006-07-09 00:45:31 +0000914/// invocation. This returns null on error.
Chris Lattner146762e2007-07-20 16:59:19 +0000915MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
Chris Lattneree8760b2006-07-15 07:42:55 +0000916 MacroInfo *MI) {
Chris Lattner78186052006-07-09 00:45:31 +0000917 // The number of fixed arguments to parse.
918 unsigned NumFixedArgsLeft = MI->getNumArgs();
919 bool isVariadic = MI->isVariadic();
920
Chris Lattner78186052006-07-09 00:45:31 +0000921 // Outer loop, while there are more arguments, keep reading them.
Chris Lattner146762e2007-07-20 16:59:19 +0000922 Token Tok;
Chris Lattner8c204872006-10-14 05:19:21 +0000923 Tok.setKind(tok::comma);
Chris Lattner78186052006-07-09 00:45:31 +0000924 --NumFixedArgsLeft; // Start reading the first arg.
Chris Lattner36b6e812006-07-21 06:38:30 +0000925
926 // ArgTokens - Build up a list of tokens that make up each argument. Each
Chris Lattner7a4af3b2006-07-26 06:26:52 +0000927 // argument is separated by an EOF token. Use a SmallVector so we can avoid
928 // heap allocations in the common case.
Chris Lattner146762e2007-07-20 16:59:19 +0000929 llvm::SmallVector<Token, 64> ArgTokens;
Chris Lattner36b6e812006-07-21 06:38:30 +0000930
931 unsigned NumActuals = 0;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000932 while (Tok.is(tok::comma)) {
Chris Lattner24dbee72007-07-19 16:11:58 +0000933 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
934 // that we already consumed the first one.
Chris Lattner78186052006-07-09 00:45:31 +0000935 unsigned NumParens = 0;
Chris Lattner36b6e812006-07-21 06:38:30 +0000936
Chris Lattner78186052006-07-09 00:45:31 +0000937 while (1) {
Chris Lattnerafe603f2006-07-11 04:02:46 +0000938 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
939 // an argument value in a macro could expand to ',' or '(' or ')'.
Chris Lattner78186052006-07-09 00:45:31 +0000940 LexUnexpandedToken(Tok);
941
Chris Lattner97ff7762008-01-22 19:34:51 +0000942 if (Tok.is(tok::eof) || Tok.is(tok::eom)) { // "#if f(<eof>" & "#if f(\n"
Chris Lattner78186052006-07-09 00:45:31 +0000943 Diag(MacroName, diag::err_unterm_macro_invoc);
Chris Lattner97ff7762008-01-22 19:34:51 +0000944 // Do not lose the EOF/EOM. Return it to the client.
Chris Lattner78186052006-07-09 00:45:31 +0000945 MacroName = Tok;
946 return 0;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000947 } else if (Tok.is(tok::r_paren)) {
Chris Lattner78186052006-07-09 00:45:31 +0000948 // If we found the ) token, the macro arg list is done.
949 if (NumParens-- == 0)
950 break;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000951 } else if (Tok.is(tok::l_paren)) {
Chris Lattner78186052006-07-09 00:45:31 +0000952 ++NumParens;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000953 } else if (Tok.is(tok::comma) && NumParens == 0) {
Chris Lattner78186052006-07-09 00:45:31 +0000954 // Comma ends this argument if there are more fixed arguments expected.
955 if (NumFixedArgsLeft)
956 break;
957
Chris Lattner2ada5d32006-07-15 07:51:24 +0000958 // If this is not a variadic macro, too many args were specified.
Chris Lattner78186052006-07-09 00:45:31 +0000959 if (!isVariadic) {
960 // Emit the diagnostic at the macro name in case there is a missing ).
961 // Emitting it at the , could be far away from the macro name.
Chris Lattner2ada5d32006-07-15 07:51:24 +0000962 Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
Chris Lattner78186052006-07-09 00:45:31 +0000963 return 0;
964 }
965 // Otherwise, continue to add the tokens to this variable argument.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000966 } else if (Tok.is(tok::comment) && !KeepMacroComments) {
Chris Lattner457fc152006-07-29 06:30:25 +0000967 // If this is a comment token in the argument list and we're just in
968 // -C mode (not -CC mode), discard the comment.
969 continue;
Chris Lattner9fcdc522007-11-23 06:50:21 +0000970 } else if (Tok.is(tok::identifier)) {
971 // Reading macro arguments can cause macros that we are currently
972 // expanding from to be popped off the expansion stack. Doing so causes
973 // them to be reenabled for expansion. Here we record whether any
974 // identifiers we lex as macro arguments correspond to disabled macros.
975 // If so, we mark the token as noexpand. This is a subtle aspect of
976 // C99 6.10.3.4p2.
977 if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
978 if (!MI->isEnabled())
979 Tok.setFlag(Token::DisableExpand);
Chris Lattner78186052006-07-09 00:45:31 +0000980 }
981
982 ArgTokens.push_back(Tok);
983 }
984
Chris Lattnera12dd152006-07-11 04:09:02 +0000985 // Empty arguments are standard in C99 and supported as an extension in
986 // other modes.
987 if (ArgTokens.empty() && !Features.C99)
988 Diag(Tok, diag::ext_empty_fnmacro_arg);
Chris Lattnerafe603f2006-07-11 04:02:46 +0000989
Chris Lattner36b6e812006-07-21 06:38:30 +0000990 // Add a marker EOF token to the end of the token list for this argument.
Chris Lattner146762e2007-07-20 16:59:19 +0000991 Token EOFTok;
Chris Lattner8c204872006-10-14 05:19:21 +0000992 EOFTok.startToken();
993 EOFTok.setKind(tok::eof);
994 EOFTok.setLocation(Tok.getLocation());
995 EOFTok.setLength(0);
Chris Lattner36b6e812006-07-21 06:38:30 +0000996 ArgTokens.push_back(EOFTok);
997 ++NumActuals;
Chris Lattner78186052006-07-09 00:45:31 +0000998 --NumFixedArgsLeft;
999 };
1000
1001 // Okay, we either found the r_paren. Check to see if we parsed too few
1002 // arguments.
Chris Lattner78186052006-07-09 00:45:31 +00001003 unsigned MinArgsExpected = MI->getNumArgs();
1004
Chris Lattner775d8322006-07-29 04:39:41 +00001005 // See MacroArgs instance var for description of this.
1006 bool isVarargsElided = false;
1007
Chris Lattner2ada5d32006-07-15 07:51:24 +00001008 if (NumActuals < MinArgsExpected) {
Chris Lattner78186052006-07-09 00:45:31 +00001009 // There are several cases where too few arguments is ok, handle them now.
Chris Lattner2ada5d32006-07-15 07:51:24 +00001010 if (NumActuals+1 == MinArgsExpected && MI->isVariadic()) {
Chris Lattner78186052006-07-09 00:45:31 +00001011 // Varargs where the named vararg parameter is missing: ok as extension.
1012 // #define A(x, ...)
1013 // A("blah")
1014 Diag(Tok, diag::ext_missing_varargs_arg);
Chris Lattner775d8322006-07-29 04:39:41 +00001015
1016 // Remember this occurred if this is a C99 macro invocation with at least
1017 // one actual argument.
Chris Lattner95a06b32006-07-30 08:40:43 +00001018 isVarargsElided = MI->isC99Varargs() && MI->getNumArgs() > 1;
Chris Lattner78186052006-07-09 00:45:31 +00001019 } else if (MI->getNumArgs() == 1) {
1020 // #define A(x)
1021 // A()
Chris Lattnere7a51302006-07-29 01:25:12 +00001022 // is ok because it is an empty argument.
Chris Lattnera12dd152006-07-11 04:09:02 +00001023
1024 // Empty arguments are standard in C99 and supported as an extension in
1025 // other modes.
1026 if (ArgTokens.empty() && !Features.C99)
1027 Diag(Tok, diag::ext_empty_fnmacro_arg);
Chris Lattner78186052006-07-09 00:45:31 +00001028 } else {
1029 // Otherwise, emit the error.
Chris Lattner2ada5d32006-07-15 07:51:24 +00001030 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
Chris Lattner78186052006-07-09 00:45:31 +00001031 return 0;
1032 }
Chris Lattnere7a51302006-07-29 01:25:12 +00001033
1034 // Add a marker EOF token to the end of the token list for this argument.
1035 SourceLocation EndLoc = Tok.getLocation();
Chris Lattner8c204872006-10-14 05:19:21 +00001036 Tok.startToken();
1037 Tok.setKind(tok::eof);
1038 Tok.setLocation(EndLoc);
1039 Tok.setLength(0);
Chris Lattnere7a51302006-07-29 01:25:12 +00001040 ArgTokens.push_back(Tok);
Chris Lattner78186052006-07-09 00:45:31 +00001041 }
1042
Chris Lattner775d8322006-07-29 04:39:41 +00001043 return MacroArgs::create(MI, &ArgTokens[0], ArgTokens.size(),isVarargsElided);
Chris Lattnerf373a4a2006-06-26 06:16:29 +00001044}
1045
Chris Lattnerc673f902006-06-30 06:10:41 +00001046/// ComputeDATE_TIME - Compute the current time, enter it into the specified
1047/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
1048/// the identifier tokens inserted.
1049static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001050 Preprocessor &PP) {
Chris Lattnerc673f902006-06-30 06:10:41 +00001051 time_t TT = time(0);
1052 struct tm *TM = localtime(&TT);
1053
1054 static const char * const Months[] = {
1055 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
1056 };
1057
1058 char TmpBuffer[100];
1059 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
1060 TM->tm_year+1900);
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001061 DATELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
Chris Lattnerc673f902006-06-30 06:10:41 +00001062
1063 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001064 TIMELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
Chris Lattnerc673f902006-06-30 06:10:41 +00001065}
1066
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001067/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1068/// as a builtin macro, handle it and return the next token as 'Tok'.
Chris Lattner146762e2007-07-20 16:59:19 +00001069void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001070 // Figure out which token this is.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001071 IdentifierInfo *II = Tok.getIdentifierInfo();
1072 assert(II && "Can't be a macro without id info!");
Chris Lattner69772b02006-07-02 20:34:39 +00001073
1074 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
1075 // lex the token after it.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001076 if (II == Ident_Pragma)
Chris Lattner69772b02006-07-02 20:34:39 +00001077 return Handle_Pragma(Tok);
1078
Chris Lattner78186052006-07-09 00:45:31 +00001079 ++NumBuiltinMacroExpanded;
1080
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001081 char TmpBuffer[100];
Chris Lattner69772b02006-07-02 20:34:39 +00001082
1083 // Set up the return result.
Chris Lattner8c204872006-10-14 05:19:21 +00001084 Tok.setIdentifierInfo(0);
Chris Lattner146762e2007-07-20 16:59:19 +00001085 Tok.clearFlag(Token::NeedsCleaning);
Chris Lattner630b33c2006-07-01 22:46:53 +00001086
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001087 if (II == Ident__LINE__) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001088 // __LINE__ expands to a simple numeric value.
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001089 sprintf(TmpBuffer, "%u", SourceMgr.getLogicalLineNumber(Tok.getLocation()));
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001090 unsigned Length = strlen(TmpBuffer);
Chris Lattner8c204872006-10-14 05:19:21 +00001091 Tok.setKind(tok::numeric_constant);
1092 Tok.setLength(Length);
1093 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001094 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +00001095 SourceLocation Loc = Tok.getLocation();
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001096 if (II == Ident__BASE_FILE__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +00001097 Diag(Tok, diag::ext_pp_base_file);
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001098 SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc);
1099 while (NextLoc.isValid()) {
Chris Lattnerc1283b92006-07-01 23:16:30 +00001100 Loc = NextLoc;
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001101 NextLoc = SourceMgr.getIncludeLoc(Loc);
Chris Lattnerc1283b92006-07-01 23:16:30 +00001102 }
1103 }
1104
Chris Lattner0766e592006-07-03 01:07:01 +00001105 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001106 std::string FN = SourceMgr.getSourceName(SourceMgr.getLogicalLoc(Loc));
Chris Lattnerecc39e92006-07-15 05:23:31 +00001107 FN = '"' + Lexer::Stringify(FN) + '"';
Chris Lattner8c204872006-10-14 05:19:21 +00001108 Tok.setKind(tok::string_literal);
1109 Tok.setLength(FN.size());
1110 Tok.setLocation(CreateString(&FN[0], FN.size(), Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001111 } else if (II == Ident__DATE__) {
Chris Lattnerc673f902006-06-30 06:10:41 +00001112 if (!DATELoc.isValid())
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001113 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
Chris Lattner8c204872006-10-14 05:19:21 +00001114 Tok.setKind(tok::string_literal);
1115 Tok.setLength(strlen("\"Mmm dd yyyy\""));
1116 Tok.setLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001117 } else if (II == Ident__TIME__) {
Chris Lattnerc673f902006-06-30 06:10:41 +00001118 if (!TIMELoc.isValid())
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001119 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
Chris Lattner8c204872006-10-14 05:19:21 +00001120 Tok.setKind(tok::string_literal);
1121 Tok.setLength(strlen("\"hh:mm:ss\""));
1122 Tok.setLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001123 } else if (II == Ident__INCLUDE_LEVEL__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +00001124 Diag(Tok, diag::ext_pp_include_level);
1125
1126 // Compute the include depth of this token.
1127 unsigned Depth = 0;
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001128 SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation());
1129 for (; Loc.isValid(); ++Depth)
1130 Loc = SourceMgr.getIncludeLoc(Loc);
Chris Lattnerc1283b92006-07-01 23:16:30 +00001131
1132 // __INCLUDE_LEVEL__ expands to a simple numeric value.
1133 sprintf(TmpBuffer, "%u", Depth);
1134 unsigned Length = strlen(TmpBuffer);
Chris Lattner8c204872006-10-14 05:19:21 +00001135 Tok.setKind(tok::numeric_constant);
1136 Tok.setLength(Length);
1137 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001138 } else if (II == Ident__TIMESTAMP__) {
Chris Lattner847e0e42006-07-01 23:49:16 +00001139 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
1140 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1141 Diag(Tok, diag::ext_pp_timestamp);
1142
1143 // Get the file that we are lexing out of. If we're currently lexing from
1144 // a macro, dig into the include stack.
1145 const FileEntry *CurFile = 0;
Chris Lattnerecfeafe2006-07-02 21:26:45 +00001146 Lexer *TheLexer = getCurrentFileLexer();
Chris Lattner847e0e42006-07-01 23:49:16 +00001147
1148 if (TheLexer)
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001149 CurFile = SourceMgr.getFileEntryForLoc(TheLexer->getFileLoc());
Chris Lattner847e0e42006-07-01 23:49:16 +00001150
1151 // If this file is older than the file it depends on, emit a diagnostic.
1152 const char *Result;
1153 if (CurFile) {
1154 time_t TT = CurFile->getModificationTime();
1155 struct tm *TM = localtime(&TT);
1156 Result = asctime(TM);
1157 } else {
1158 Result = "??? ??? ?? ??:??:?? ????\n";
1159 }
1160 TmpBuffer[0] = '"';
1161 strcpy(TmpBuffer+1, Result);
1162 unsigned Len = strlen(TmpBuffer);
1163 TmpBuffer[Len-1] = '"'; // Replace the newline with a quote.
Chris Lattner8c204872006-10-14 05:19:21 +00001164 Tok.setKind(tok::string_literal);
1165 Tok.setLength(Len);
1166 Tok.setLocation(CreateString(TmpBuffer, Len, Tok.getLocation()));
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001167 } else {
1168 assert(0 && "Unknown identifier!");
Chris Lattner615315f2007-12-09 20:31:55 +00001169 }
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001170}
Chris Lattner677757a2006-06-28 05:26:32 +00001171
1172//===----------------------------------------------------------------------===//
1173// Lexer Event Handling.
1174//===----------------------------------------------------------------------===//
1175
Chris Lattnercefc7682006-07-08 08:28:12 +00001176/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
1177/// identifier information for the token and install it into the token.
Chris Lattner146762e2007-07-20 16:59:19 +00001178IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
Chris Lattnercefc7682006-07-08 08:28:12 +00001179 const char *BufPtr) {
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001180 assert(Identifier.is(tok::identifier) && "Not an identifier!");
Chris Lattnercefc7682006-07-08 08:28:12 +00001181 assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
1182
1183 // Look up this token, see if it is a macro, or if it is a language keyword.
1184 IdentifierInfo *II;
1185 if (BufPtr && !Identifier.needsCleaning()) {
1186 // No cleaning needed, just use the characters from the lexed buffer.
1187 II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
1188 } else {
1189 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
Chris Lattnerf9aba2c2007-07-13 17:10:38 +00001190 llvm::SmallVector<char, 64> IdentifierBuffer;
1191 IdentifierBuffer.resize(Identifier.getLength());
1192 const char *TmpBuf = &IdentifierBuffer[0];
Chris Lattnercefc7682006-07-08 08:28:12 +00001193 unsigned Size = getSpelling(Identifier, TmpBuf);
1194 II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
1195 }
Chris Lattner8c204872006-10-14 05:19:21 +00001196 Identifier.setIdentifierInfo(II);
Chris Lattnercefc7682006-07-08 08:28:12 +00001197 return II;
1198}
1199
1200
Chris Lattner677757a2006-06-28 05:26:32 +00001201/// HandleIdentifier - This callback is invoked when the lexer reads an
1202/// identifier. This callback looks up the identifier in the map and/or
1203/// potentially macro expands it or turns it into a named token (like 'for').
Chris Lattner146762e2007-07-20 16:59:19 +00001204void Preprocessor::HandleIdentifier(Token &Identifier) {
Chris Lattner0f1f5052006-07-20 04:16:23 +00001205 assert(Identifier.getIdentifierInfo() &&
1206 "Can't handle identifiers without identifier info!");
1207
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001208 IdentifierInfo &II = *Identifier.getIdentifierInfo();
Chris Lattner677757a2006-06-28 05:26:32 +00001209
1210 // If this identifier was poisoned, and if it was not produced from a macro
1211 // expansion, emit an error.
Chris Lattner8ff71992006-07-06 05:17:39 +00001212 if (II.isPoisoned() && CurLexer) {
1213 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
1214 Diag(Identifier, diag::err_pp_used_poisoned_id);
1215 else
1216 Diag(Identifier, diag::ext_pp_bad_vaargs_use);
1217 }
Chris Lattner677757a2006-06-28 05:26:32 +00001218
Chris Lattner78186052006-07-09 00:45:31 +00001219 // If this is a macro to be expanded, do it.
Chris Lattnerc43ddc82007-10-07 08:44:20 +00001220 if (MacroInfo *MI = getMacroInfo(&II)) {
Chris Lattner6e4bf522006-07-27 06:59:25 +00001221 if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
1222 if (MI->isEnabled()) {
1223 if (!HandleMacroExpandedIdentifier(Identifier, MI))
1224 return;
1225 } else {
1226 // C99 6.10.3.4p2 says that a disabled macro may never again be
1227 // expanded, even if it's in a context where it could be expanded in the
1228 // future.
Chris Lattner146762e2007-07-20 16:59:19 +00001229 Identifier.setFlag(Token::DisableExpand);
Chris Lattner6e4bf522006-07-27 06:59:25 +00001230 }
1231 }
Chris Lattner063400e2006-10-14 19:54:15 +00001232 } else if (II.isOtherTargetMacro() && !DisableMacroExpansion) {
1233 // If this identifier is a macro on some other target, emit a diagnostic.
1234 // This diagnosic is only emitted when macro expansion is enabled, because
1235 // the macro would not have been expanded for the other target either.
1236 II.setIsOtherTargetMacro(false); // Don't warn on second use.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +00001237 getTargetInfo().DiagnoseNonPortability(getFullLoc(Identifier.getLocation()),
Chris Lattner063400e2006-10-14 19:54:15 +00001238 diag::port_target_macro_use);
1239
1240 }
Chris Lattner677757a2006-06-28 05:26:32 +00001241
Chris Lattner5b9f4892006-11-21 17:23:33 +00001242 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
1243 // then we act as if it is the actual operator and not the textual
1244 // representation of it.
1245 if (II.isCPlusPlusOperatorKeyword())
1246 Identifier.setIdentifierInfo(0);
1247
Chris Lattner677757a2006-06-28 05:26:32 +00001248 // Change the kind of this identifier to the appropriate token kind, e.g.
1249 // turning "for" into a keyword.
Chris Lattner8c204872006-10-14 05:19:21 +00001250 Identifier.setKind(II.getTokenID());
Chris Lattner677757a2006-06-28 05:26:32 +00001251
1252 // If this is an extension token, diagnose its use.
Steve Naroffa8fd9732007-06-11 00:35:03 +00001253 // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99
1254 // For now, I'm just commenting it out (while I work on attributes).
Chris Lattner53621a52007-06-13 20:44:40 +00001255 if (II.isExtensionToken() && Features.C99)
1256 Diag(Identifier, diag::ext_token_used);
Chris Lattner677757a2006-06-28 05:26:32 +00001257}
1258
Chris Lattner22eb9722006-06-18 05:43:12 +00001259/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
1260/// the current file. This either returns the EOF token or pops a level off
1261/// the include stack and keeps going.
Chris Lattner146762e2007-07-20 16:59:19 +00001262bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001263 assert(!CurMacroExpander &&
1264 "Ending a file when currently in a macro!");
1265
Chris Lattner371ac8a2006-07-04 07:11:10 +00001266 // See if this file had a controlling macro.
Chris Lattner3665f162006-07-04 07:26:10 +00001267 if (CurLexer) { // Not ending a macro, ignore it.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001268 if (const IdentifierInfo *ControllingMacro =
Chris Lattner371ac8a2006-07-04 07:11:10 +00001269 CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
Chris Lattner3665f162006-07-04 07:26:10 +00001270 // Okay, this has a controlling macro, remember in PerFileInfo.
1271 if (const FileEntry *FE =
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001272 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001273 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
Chris Lattner371ac8a2006-07-04 07:11:10 +00001274 }
1275 }
1276
Chris Lattner22eb9722006-06-18 05:43:12 +00001277 // If this is a #include'd file, pop it off the include stack and continue
1278 // lexing the #includer file.
Chris Lattner69772b02006-07-02 20:34:39 +00001279 if (!IncludeMacroStack.empty()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001280 // We're done with the #included file.
Chris Lattner7667d0d2006-07-16 18:16:58 +00001281 RemoveTopOfLexerStack();
Chris Lattner0c885f52006-06-21 06:50:18 +00001282
1283 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +00001284 if (Callbacks && !isEndOfMacro && CurLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +00001285 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
1286
1287 // Get the file entry for the current file.
1288 if (const FileEntry *FE =
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001289 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001290 FileType = HeaderInfo.getFileDirFlavor(FE);
Chris Lattnerc8997182006-06-22 05:52:16 +00001291
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +00001292 Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr),
1293 PPCallbacks::ExitFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +00001294 }
Chris Lattner2183a6e2006-07-18 06:36:12 +00001295
1296 // Client should lex another token.
1297 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001298 }
Chris Lattner9046dc12008-01-25 00:00:30 +00001299
1300 // If the file ends with a newline, form the EOF token on the newline itself,
1301 // rather than "on the line following it", which doesn't exist. This makes
1302 // diagnostics relating to the end of file include the last file that the user
1303 // actually typed, which is goodness.
1304 const char *EndPos = CurLexer->BufferEnd;
1305 if (EndPos != CurLexer->BufferStart &&
1306 (EndPos[-1] == '\n' || EndPos[-1] == '\r')) {
1307 --EndPos;
1308
1309 // Handle \n\r and \r\n:
1310 if (EndPos != CurLexer->BufferStart &&
1311 (EndPos[-1] == '\n' || EndPos[-1] == '\r') &&
1312 EndPos[-1] != EndPos[0])
1313 --EndPos;
1314 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001315
Chris Lattner8c204872006-10-14 05:19:21 +00001316 Result.startToken();
Chris Lattner9046dc12008-01-25 00:00:30 +00001317 CurLexer->BufferPtr = EndPos;
1318 CurLexer->FormTokenWithChars(Result, EndPos);
Chris Lattner8c204872006-10-14 05:19:21 +00001319 Result.setKind(tok::eof);
Chris Lattner22eb9722006-06-18 05:43:12 +00001320
1321 // We're done with the #included file.
1322 delete CurLexer;
1323 CurLexer = 0;
Chris Lattner13044d92006-07-03 05:16:44 +00001324
Chris Lattner03f83482006-07-10 06:16:26 +00001325 // This is the end of the top-level file. If the diag::pp_macro_not_used
Chris Lattnerc43ddc82007-10-07 08:44:20 +00001326 // diagnostic is enabled, look for macros that have not been used.
Chris Lattnerb055f2d2007-02-11 08:19:57 +00001327 if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){
Chris Lattnerc43ddc82007-10-07 08:44:20 +00001328 for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
1329 Macros.begin(), E = Macros.end(); I != E; ++I) {
1330 if (!I->second->isUsed())
1331 Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattnerb055f2d2007-02-11 08:19:57 +00001332 }
1333 }
Chris Lattner2183a6e2006-07-18 06:36:12 +00001334 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001335}
1336
1337/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
Chris Lattner7667d0d2006-07-16 18:16:58 +00001338/// the current macro expansion or token stream expansion.
Chris Lattner146762e2007-07-20 16:59:19 +00001339bool Preprocessor::HandleEndOfMacro(Token &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001340 assert(CurMacroExpander && !CurLexer &&
1341 "Ending a macro when currently in a #include file!");
1342
Chris Lattnerc02c4ab2007-07-15 00:25:26 +00001343 // Delete or cache the now-dead macro expander.
1344 if (NumCachedMacroExpanders == MacroExpanderCacheSize)
1345 delete CurMacroExpander;
1346 else
1347 MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander;
Chris Lattner22eb9722006-06-18 05:43:12 +00001348
Chris Lattner69772b02006-07-02 20:34:39 +00001349 // Handle this like a #include file being popped off the stack.
1350 CurMacroExpander = 0;
1351 return HandleEndOfFile(Result, true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001352}
1353
1354
1355//===----------------------------------------------------------------------===//
1356// Utility Methods for Preprocessor Directive Handling.
1357//===----------------------------------------------------------------------===//
1358
1359/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
1360/// current line until the tok::eom token is found.
Chris Lattnercb283342006-06-18 06:48:37 +00001361void Preprocessor::DiscardUntilEndOfDirective() {
Chris Lattner146762e2007-07-20 16:59:19 +00001362 Token Tmp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001363 do {
Chris Lattnercb283342006-06-18 06:48:37 +00001364 LexUnexpandedToken(Tmp);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001365 } while (Tmp.isNot(tok::eom));
Chris Lattner22eb9722006-06-18 05:43:12 +00001366}
1367
Chris Lattner652c1692006-11-21 23:47:30 +00001368/// isCXXNamedOperator - Returns "true" if the token is a named operator in C++.
1369static bool isCXXNamedOperator(const std::string &Spelling) {
1370 return Spelling == "and" || Spelling == "bitand" || Spelling == "bitor" ||
1371 Spelling == "compl" || Spelling == "not" || Spelling == "not_eq" ||
1372 Spelling == "or" || Spelling == "xor";
1373}
1374
Chris Lattner22eb9722006-06-18 05:43:12 +00001375/// ReadMacroName - Lex and validate a macro name, which occurs after a
1376/// #define or #undef. This sets the token kind to eom and discards the rest
Chris Lattnere8eef322006-07-08 07:01:00 +00001377/// of the macro line if the macro name is invalid. isDefineUndef is 1 if
1378/// this is due to a a #define, 2 if #undef directive, 0 if it is something
Chris Lattner44f8a662006-07-03 01:27:27 +00001379/// else (e.g. #ifdef).
Chris Lattner146762e2007-07-20 16:59:19 +00001380void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001381 // Read the token, don't allow macro expansion on it.
Chris Lattnercb283342006-06-18 06:48:37 +00001382 LexUnexpandedToken(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001383
1384 // Missing macro name?
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001385 if (MacroNameTok.is(tok::eom))
Chris Lattner22eb9722006-06-18 05:43:12 +00001386 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
1387
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001388 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1389 if (II == 0) {
Chris Lattner652c1692006-11-21 23:47:30 +00001390 std::string Spelling = getSpelling(MacroNameTok);
1391 if (isCXXNamedOperator(Spelling))
1392 // C++ 2.5p2: Alternative tokens behave the same as its primary token
1393 // except for their spellings.
1394 Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name, Spelling);
1395 else
1396 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +00001397 // Fall through on error.
Chris Lattner2bb8a952006-11-21 22:24:17 +00001398 } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
Chris Lattner44f8a662006-07-03 01:27:27 +00001399 // Error if defining "defined": C99 6.10.8.4.
Chris Lattneraaf09112006-07-03 01:17:59 +00001400 Diag(MacroNameTok, diag::err_defined_macro_name);
Chris Lattner259716a2007-10-07 08:04:56 +00001401 } else if (isDefineUndef && II->hasMacroDefinition() &&
Chris Lattnerc43ddc82007-10-07 08:44:20 +00001402 getMacroInfo(II)->isBuiltinMacro()) {
Chris Lattner44f8a662006-07-03 01:27:27 +00001403 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
Chris Lattnere8eef322006-07-08 07:01:00 +00001404 if (isDefineUndef == 1)
1405 Diag(MacroNameTok, diag::pp_redef_builtin_macro);
1406 else
1407 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
Chris Lattner22eb9722006-06-18 05:43:12 +00001408 } else {
1409 // Okay, we got a good identifier node. Return it.
Chris Lattnercb283342006-06-18 06:48:37 +00001410 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001411 }
1412
Chris Lattner22eb9722006-06-18 05:43:12 +00001413 // Invalid macro name, read and discard the rest of the line. Then set the
1414 // token kind to tok::eom.
Chris Lattner8c204872006-10-14 05:19:21 +00001415 MacroNameTok.setKind(tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +00001416 return DiscardUntilEndOfDirective();
1417}
1418
1419/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
1420/// not, emit a diagnostic and consume up until the eom.
Chris Lattnercb283342006-06-18 06:48:37 +00001421void Preprocessor::CheckEndOfDirective(const char *DirType) {
Chris Lattner146762e2007-07-20 16:59:19 +00001422 Token Tmp;
Chris Lattnercb283342006-06-18 06:48:37 +00001423 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001424 // There should be no tokens after the directive, but we allow them as an
1425 // extension.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001426 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
Chris Lattnerbcb416b2006-10-27 05:43:50 +00001427 Lex(Tmp);
1428
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001429 if (Tmp.isNot(tok::eom)) {
Chris Lattnercb283342006-06-18 06:48:37 +00001430 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
1431 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001432 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001433}
1434
1435
1436
1437/// SkipExcludedConditionalBlock - We just read a #if or related directive and
1438/// decided that the subsequent tokens are in the #if'd out portion of the
1439/// file. Lex the rest of the file, until we see an #endif. If
1440/// FoundNonSkipPortion is true, then we have already emitted code for part of
1441/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
1442/// is true, then #else directives are ok, if not, then we have already seen one
1443/// so a #else directive is a duplicate. When this returns, the caller can lex
1444/// the first valid token.
Chris Lattnerd01e2912006-06-18 16:22:51 +00001445void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
Chris Lattner22eb9722006-06-18 05:43:12 +00001446 bool FoundNonSkipPortion,
1447 bool FoundElse) {
1448 ++NumSkipped;
Chris Lattner69772b02006-07-02 20:34:39 +00001449 assert(CurMacroExpander == 0 && CurLexer &&
Chris Lattner22eb9722006-06-18 05:43:12 +00001450 "Lexing a macro, not a file?");
1451
1452 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
1453 FoundNonSkipPortion, FoundElse);
1454
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001455 // Enter raw mode to disable identifier lookup (and thus macro expansion),
1456 // disabling warnings, etc.
1457 CurLexer->LexingRawMode = true;
Chris Lattner146762e2007-07-20 16:59:19 +00001458 Token Tok;
Chris Lattner22eb9722006-06-18 05:43:12 +00001459 while (1) {
Chris Lattnercb283342006-06-18 06:48:37 +00001460 CurLexer->Lex(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001461
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001462 // If this is the end of the buffer, we have an error.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001463 if (Tok.is(tok::eof)) {
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001464 // Emit errors for each unterminated conditional on the stack, including
1465 // the current one.
1466 while (!CurLexer->ConditionalStack.empty()) {
1467 Diag(CurLexer->ConditionalStack.back().IfLoc,
1468 diag::err_pp_unterminated_conditional);
1469 CurLexer->ConditionalStack.pop_back();
1470 }
1471
1472 // Just return and let the caller lex after this #include.
1473 break;
1474 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001475
1476 // If this token is not a preprocessor directive, just skip it.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001477 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
Chris Lattner22eb9722006-06-18 05:43:12 +00001478 continue;
1479
1480 // We just parsed a # character at the start of a line, so we're in
1481 // directive mode. Tell the lexer this so any newlines we see will be
1482 // converted into an EOM token (this terminates the macro).
1483 CurLexer->ParsingPreprocessorDirective = true;
Chris Lattner457fc152006-07-29 06:30:25 +00001484 CurLexer->KeepCommentMode = false;
1485
Chris Lattner22eb9722006-06-18 05:43:12 +00001486
1487 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +00001488 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001489
1490 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
1491 // something bogus), skip it.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001492 if (Tok.isNot(tok::identifier)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001493 CurLexer->ParsingPreprocessorDirective = false;
Chris Lattner457fc152006-07-29 06:30:25 +00001494 // Restore comment saving mode.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001495 CurLexer->KeepCommentMode = KeepComments;
Chris Lattner22eb9722006-06-18 05:43:12 +00001496 continue;
1497 }
Chris Lattnere60165f2006-06-22 06:36:29 +00001498
Chris Lattner22eb9722006-06-18 05:43:12 +00001499 // If the first letter isn't i or e, it isn't intesting to us. We know that
1500 // this is safe in the face of spelling differences, because there is no way
1501 // to spell an i/e in a strange way that is another letter. Skipping this
Chris Lattnere60165f2006-06-22 06:36:29 +00001502 // allows us to avoid looking up the identifier info for #define/#undef and
1503 // other common directives.
1504 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
1505 char FirstChar = RawCharData[0];
Chris Lattner22eb9722006-06-18 05:43:12 +00001506 if (FirstChar >= 'a' && FirstChar <= 'z' &&
1507 FirstChar != 'i' && FirstChar != 'e') {
1508 CurLexer->ParsingPreprocessorDirective = false;
Chris Lattner457fc152006-07-29 06:30:25 +00001509 // Restore comment saving mode.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001510 CurLexer->KeepCommentMode = KeepComments;
Chris Lattner22eb9722006-06-18 05:43:12 +00001511 continue;
1512 }
1513
Chris Lattnere60165f2006-06-22 06:36:29 +00001514 // Get the identifier name without trigraphs or embedded newlines. Note
1515 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
1516 // when skipping.
1517 // TODO: could do this with zero copies in the no-clean case by using
1518 // strncmp below.
1519 char Directive[20];
1520 unsigned IdLen;
1521 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
1522 IdLen = Tok.getLength();
1523 memcpy(Directive, RawCharData, IdLen);
1524 Directive[IdLen] = 0;
1525 } else {
1526 std::string DirectiveStr = getSpelling(Tok);
1527 IdLen = DirectiveStr.size();
1528 if (IdLen >= 20) {
1529 CurLexer->ParsingPreprocessorDirective = false;
Chris Lattner457fc152006-07-29 06:30:25 +00001530 // Restore comment saving mode.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001531 CurLexer->KeepCommentMode = KeepComments;
Chris Lattnere60165f2006-06-22 06:36:29 +00001532 continue;
1533 }
1534 memcpy(Directive, &DirectiveStr[0], IdLen);
1535 Directive[IdLen] = 0;
1536 }
1537
Chris Lattner22eb9722006-06-18 05:43:12 +00001538 if (FirstChar == 'i' && Directive[1] == 'f') {
Chris Lattnere60165f2006-06-22 06:36:29 +00001539 if ((IdLen == 2) || // "if"
1540 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
1541 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
Chris Lattner22eb9722006-06-18 05:43:12 +00001542 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
1543 // bother parsing the condition.
Chris Lattnercb283342006-06-18 06:48:37 +00001544 DiscardUntilEndOfDirective();
Chris Lattner50b497e2006-06-18 16:32:35 +00001545 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerd01e2912006-06-18 16:22:51 +00001546 /*foundnonskip*/false,
1547 /*fnddelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001548 }
1549 } else if (FirstChar == 'e') {
Chris Lattnere60165f2006-06-22 06:36:29 +00001550 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
Chris Lattnercb283342006-06-18 06:48:37 +00001551 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00001552 PPConditionalInfo CondInfo;
1553 CondInfo.WasSkipping = true; // Silence bogus warning.
1554 bool InCond = CurLexer->popConditionalLevel(CondInfo);
Chris Lattnercf6bc662006-11-05 07:59:08 +00001555 InCond = InCond; // Silence warning in no-asserts mode.
Chris Lattner22eb9722006-06-18 05:43:12 +00001556 assert(!InCond && "Can't be skipping if not in a conditional!");
1557
1558 // If we popped the outermost skipping block, we're done skipping!
1559 if (!CondInfo.WasSkipping)
1560 break;
Chris Lattnere60165f2006-06-22 06:36:29 +00001561 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
Chris Lattner22eb9722006-06-18 05:43:12 +00001562 // #else directive in a skipping conditional. If not in some other
1563 // skipping conditional, and if #else hasn't already been seen, enter it
1564 // as a non-skipping conditional.
Chris Lattnercb283342006-06-18 06:48:37 +00001565 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00001566 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1567
1568 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001569 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001570
1571 // Note that we've seen a #else in this conditional.
1572 CondInfo.FoundElse = true;
1573
1574 // If the conditional is at the top level, and the #if block wasn't
1575 // entered, enter the #else block now.
1576 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
1577 CondInfo.FoundNonSkip = true;
1578 break;
1579 }
Chris Lattnere60165f2006-06-22 06:36:29 +00001580 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
Chris Lattner22eb9722006-06-18 05:43:12 +00001581 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1582
1583 bool ShouldEnter;
1584 // If this is in a skipping block or if we're already handled this #if
1585 // block, don't bother parsing the condition.
1586 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
Chris Lattnercb283342006-06-18 06:48:37 +00001587 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001588 ShouldEnter = false;
1589 } else {
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001590 // Restore the value of LexingRawMode so that identifiers are
Chris Lattner22eb9722006-06-18 05:43:12 +00001591 // looked up, etc, inside the #elif expression.
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001592 assert(CurLexer->LexingRawMode && "We have to be skipping here!");
1593 CurLexer->LexingRawMode = false;
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001594 IdentifierInfo *IfNDefMacro = 0;
Chris Lattnera8654ca2006-07-04 17:42:08 +00001595 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001596 CurLexer->LexingRawMode = true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001597 }
1598
1599 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001600 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001601
1602 // If this condition is true, enter it!
1603 if (ShouldEnter) {
1604 CondInfo.FoundNonSkip = true;
1605 break;
1606 }
1607 }
1608 }
1609
1610 CurLexer->ParsingPreprocessorDirective = false;
Chris Lattner457fc152006-07-29 06:30:25 +00001611 // Restore comment saving mode.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001612 CurLexer->KeepCommentMode = KeepComments;
Chris Lattner22eb9722006-06-18 05:43:12 +00001613 }
1614
1615 // Finally, if we are out of the conditional (saw an #endif or ran off the end
1616 // of the file, just stop skipping and return to lexing whatever came after
1617 // the #if block.
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001618 CurLexer->LexingRawMode = false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001619}
1620
1621//===----------------------------------------------------------------------===//
1622// Preprocessor Directive Handling.
1623//===----------------------------------------------------------------------===//
1624
1625/// HandleDirective - This callback is invoked when the lexer sees a # token
1626/// at the start of a line. This consumes the directive, modifies the
1627/// lexer/preprocessor state, and advances the lexer(s) so that the next token
1628/// read is the correct one.
Chris Lattner146762e2007-07-20 16:59:19 +00001629void Preprocessor::HandleDirective(Token &Result) {
Chris Lattner4d5e1a72006-07-03 01:01:29 +00001630 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
Chris Lattner22eb9722006-06-18 05:43:12 +00001631
1632 // We just parsed a # character at the start of a line, so we're in directive
1633 // mode. Tell the lexer this so any newlines we see will be converted into an
Chris Lattner78186052006-07-09 00:45:31 +00001634 // EOM token (which terminates the directive).
Chris Lattner22eb9722006-06-18 05:43:12 +00001635 CurLexer->ParsingPreprocessorDirective = true;
1636
1637 ++NumDirectives;
1638
Chris Lattner371ac8a2006-07-04 07:11:10 +00001639 // We are about to read a token. For the multiple-include optimization FA to
1640 // work, we have to remember if we had read any tokens *before* this
1641 // pp-directive.
1642 bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal();
1643
Chris Lattner78186052006-07-09 00:45:31 +00001644 // Read the next token, the directive flavor. This isn't expanded due to
1645 // C99 6.10.3p8.
Chris Lattnercb283342006-06-18 06:48:37 +00001646 LexUnexpandedToken(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001647
Chris Lattner78186052006-07-09 00:45:31 +00001648 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
1649 // #define A(x) #x
1650 // A(abc
1651 // #warning blah
1652 // def)
1653 // If so, the user is relying on non-portable behavior, emit a diagnostic.
Chris Lattneree8760b2006-07-15 07:42:55 +00001654 if (InMacroArgs)
Chris Lattner78186052006-07-09 00:45:31 +00001655 Diag(Result, diag::ext_embedded_directive);
1656
Chris Lattnerbcb416b2006-10-27 05:43:50 +00001657TryAgain:
Chris Lattner22eb9722006-06-18 05:43:12 +00001658 switch (Result.getKind()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001659 case tok::eom:
Chris Lattnercb283342006-06-18 06:48:37 +00001660 return; // null directive.
Chris Lattnerbcb416b2006-10-27 05:43:50 +00001661 case tok::comment:
1662 // Handle stuff like "# /*foo*/ define X" in -E -C mode.
1663 LexUnexpandedToken(Result);
1664 goto TryAgain;
Chris Lattner22eb9722006-06-18 05:43:12 +00001665
Chris Lattner22eb9722006-06-18 05:43:12 +00001666 case tok::numeric_constant:
1667 // FIXME: implement # 7 line numbers!
Chris Lattner6e5b2a02006-10-17 02:53:32 +00001668 DiscardUntilEndOfDirective();
1669 return;
Chris Lattner87d3bec2006-10-17 03:44:32 +00001670 default:
1671 IdentifierInfo *II = Result.getIdentifierInfo();
1672 if (II == 0) break; // Not an identifier.
1673
1674 // Ask what the preprocessor keyword ID is.
1675 switch (II->getPPKeywordID()) {
1676 default: break;
1677 // C99 6.10.1 - Conditional Inclusion.
1678 case tok::pp_if:
1679 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
1680 case tok::pp_ifdef:
1681 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
1682 case tok::pp_ifndef:
1683 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
1684 case tok::pp_elif:
1685 return HandleElifDirective(Result);
1686 case tok::pp_else:
1687 return HandleElseDirective(Result);
1688 case tok::pp_endif:
1689 return HandleEndifDirective(Result);
1690
1691 // C99 6.10.2 - Source File Inclusion.
1692 case tok::pp_include:
1693 return HandleIncludeDirective(Result); // Handle #include.
1694
1695 // C99 6.10.3 - Macro Replacement.
1696 case tok::pp_define:
1697 return HandleDefineDirective(Result, false);
1698 case tok::pp_undef:
1699 return HandleUndefDirective(Result);
1700
1701 // C99 6.10.4 - Line Control.
1702 case tok::pp_line:
1703 // FIXME: implement #line
1704 DiscardUntilEndOfDirective();
1705 return;
1706
1707 // C99 6.10.5 - Error Directive.
1708 case tok::pp_error:
1709 return HandleUserDiagnosticDirective(Result, false);
1710
1711 // C99 6.10.6 - Pragma Directive.
1712 case tok::pp_pragma:
1713 return HandlePragmaDirective();
1714
1715 // GNU Extensions.
1716 case tok::pp_import:
1717 return HandleImportDirective(Result);
1718 case tok::pp_include_next:
1719 return HandleIncludeNextDirective(Result);
1720
1721 case tok::pp_warning:
1722 Diag(Result, diag::ext_pp_warning_directive);
1723 return HandleUserDiagnosticDirective(Result, true);
1724 case tok::pp_ident:
1725 return HandleIdentSCCSDirective(Result);
1726 case tok::pp_sccs:
1727 return HandleIdentSCCSDirective(Result);
1728 case tok::pp_assert:
1729 //isExtension = true; // FIXME: implement #assert
Chris Lattner22eb9722006-06-18 05:43:12 +00001730 break;
Chris Lattner87d3bec2006-10-17 03:44:32 +00001731 case tok::pp_unassert:
1732 //isExtension = true; // FIXME: implement #unassert
Chris Lattner22eb9722006-06-18 05:43:12 +00001733 break;
Chris Lattner87d3bec2006-10-17 03:44:32 +00001734
1735 // clang extensions.
1736 case tok::pp_define_target:
1737 return HandleDefineDirective(Result, true);
1738 case tok::pp_define_other_target:
1739 return HandleDefineOtherTargetDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001740 }
1741 break;
1742 }
1743
1744 // If we reached here, the preprocessing token is not valid!
Chris Lattnercb283342006-06-18 06:48:37 +00001745 Diag(Result, diag::err_pp_invalid_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001746
1747 // Read the rest of the PP line.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001748 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001749
1750 // Okay, we're done parsing the directive.
Chris Lattner22eb9722006-06-18 05:43:12 +00001751}
1752
Chris Lattner146762e2007-07-20 16:59:19 +00001753void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001754 bool isWarning) {
1755 // Read the rest of the line raw. We do this because we don't want macros
1756 // to be expanded and we don't require that the tokens be valid preprocessing
1757 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1758 // collapse multiple consequtive white space between tokens, but this isn't
1759 // specified by the standard.
1760 std::string Message = CurLexer->ReadToEndOfLine();
1761
1762 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
Chris Lattner01d66cc2006-07-03 22:16:27 +00001763 return Diag(Tok, DiagID, Message);
1764}
1765
1766/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1767///
Chris Lattner146762e2007-07-20 16:59:19 +00001768void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001769 // Yes, this directive is an extension.
Chris Lattner01d66cc2006-07-03 22:16:27 +00001770 Diag(Tok, diag::ext_pp_ident_directive);
1771
Chris Lattner371ac8a2006-07-04 07:11:10 +00001772 // Read the string argument.
Chris Lattner146762e2007-07-20 16:59:19 +00001773 Token StrTok;
Chris Lattner01d66cc2006-07-03 22:16:27 +00001774 Lex(StrTok);
1775
1776 // If the token kind isn't a string, it's a malformed directive.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001777 if (StrTok.isNot(tok::string_literal) &&
1778 StrTok.isNot(tok::wide_string_literal))
Chris Lattner01d66cc2006-07-03 22:16:27 +00001779 return Diag(StrTok, diag::err_pp_malformed_ident);
1780
1781 // Verify that there is nothing after the string, other than EOM.
1782 CheckEndOfDirective("#ident");
1783
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +00001784 if (Callbacks)
1785 Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok));
Chris Lattner22eb9722006-06-18 05:43:12 +00001786}
1787
Chris Lattnerb8761832006-06-24 21:31:03 +00001788//===----------------------------------------------------------------------===//
1789// Preprocessor Include Directive Handling.
1790//===----------------------------------------------------------------------===//
1791
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001792/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1793/// checked and spelled filename, e.g. as an operand of #include. This returns
1794/// true if the input filename was in <>'s or false if it were in ""'s. The
1795/// caller is expected to provide a buffer that is large enough to hold the
1796/// spelling of the filename, but is also expected to handle the case when
1797/// this method decides to use a different buffer.
Chris Lattner93ab9f12007-07-23 04:15:27 +00001798bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001799 const char *&BufStart,
1800 const char *&BufEnd) {
1801 // Get the text form of the filename.
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001802 assert(BufStart != BufEnd && "Can't have tokens with empty spellings!");
1803
1804 // Make sure the filename is <x> or "x".
1805 bool isAngled;
1806 if (BufStart[0] == '<') {
1807 if (BufEnd[-1] != '>') {
Chris Lattner93ab9f12007-07-23 04:15:27 +00001808 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001809 BufStart = 0;
1810 return true;
1811 }
1812 isAngled = true;
1813 } else if (BufStart[0] == '"') {
1814 if (BufEnd[-1] != '"') {
Chris Lattner93ab9f12007-07-23 04:15:27 +00001815 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001816 BufStart = 0;
1817 return true;
1818 }
1819 isAngled = false;
1820 } else {
Chris Lattner93ab9f12007-07-23 04:15:27 +00001821 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001822 BufStart = 0;
1823 return true;
1824 }
1825
1826 // Diagnose #include "" as invalid.
1827 if (BufEnd-BufStart <= 2) {
Chris Lattner93ab9f12007-07-23 04:15:27 +00001828 Diag(Loc, diag::err_pp_empty_filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001829 BufStart = 0;
1830 return "";
1831 }
1832
1833 // Skip the brackets.
1834 ++BufStart;
1835 --BufEnd;
1836 return isAngled;
1837}
1838
Chris Lattner43eafb42007-07-23 04:56:47 +00001839/// ConcatenateIncludeName - Handle cases where the #include name is expanded
1840/// from a macro as multiple tokens, which need to be glued together. This
1841/// occurs for code like:
1842/// #define FOO <a/b.h>
1843/// #include FOO
1844/// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1845///
1846/// This code concatenates and consumes tokens up to the '>' token. It returns
1847/// false if the > was found, otherwise it returns true if it finds and consumes
1848/// the EOM marker.
1849static bool ConcatenateIncludeName(llvm::SmallVector<char, 128> &FilenameBuffer,
1850 Preprocessor &PP) {
1851 Token CurTok;
1852
1853 PP.Lex(CurTok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001854 while (CurTok.isNot(tok::eom)) {
Chris Lattner43eafb42007-07-23 04:56:47 +00001855 // Append the spelling of this token to the buffer. If there was a space
1856 // before it, add it now.
1857 if (CurTok.hasLeadingSpace())
1858 FilenameBuffer.push_back(' ');
1859
1860 // Get the spelling of the token, directly into FilenameBuffer if possible.
1861 unsigned PreAppendSize = FilenameBuffer.size();
1862 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1863
1864 const char *BufPtr = &FilenameBuffer[PreAppendSize];
1865 unsigned ActualLen = PP.getSpelling(CurTok, BufPtr);
1866
1867 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1868 if (BufPtr != &FilenameBuffer[PreAppendSize])
1869 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1870
1871 // Resize FilenameBuffer to the correct size.
1872 if (CurTok.getLength() != ActualLen)
1873 FilenameBuffer.resize(PreAppendSize+ActualLen);
1874
1875 // If we found the '>' marker, return success.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001876 if (CurTok.is(tok::greater))
Chris Lattner43eafb42007-07-23 04:56:47 +00001877 return false;
1878
1879 PP.Lex(CurTok);
1880 }
1881
1882 // If we hit the eom marker, emit an error and return true so that the caller
1883 // knows the EOM has been read.
1884 PP.Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1885 return true;
1886}
1887
Chris Lattner22eb9722006-06-18 05:43:12 +00001888/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1889/// file to be included from the lexer, then include it! This is a common
1890/// routine with functionality shared between #include, #include_next and
1891/// #import.
Chris Lattner146762e2007-07-20 16:59:19 +00001892void Preprocessor::HandleIncludeDirective(Token &IncludeTok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001893 const DirectoryLookup *LookupFrom,
1894 bool isImport) {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001895
Chris Lattner146762e2007-07-20 16:59:19 +00001896 Token FilenameTok;
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001897 CurLexer->LexIncludeFilename(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001898
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001899 // Reserve a buffer to get the spelling.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001900 llvm::SmallVector<char, 128> FilenameBuffer;
Chris Lattner43eafb42007-07-23 04:56:47 +00001901 const char *FilenameStart, *FilenameEnd;
1902
1903 switch (FilenameTok.getKind()) {
1904 case tok::eom:
1905 // If the token kind is EOM, the error has already been diagnosed.
1906 return;
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001907
Chris Lattner43eafb42007-07-23 04:56:47 +00001908 case tok::angle_string_literal:
Chris Lattnerf97dbcb2007-07-23 22:23:52 +00001909 case tok::string_literal: {
Chris Lattner43eafb42007-07-23 04:56:47 +00001910 FilenameBuffer.resize(FilenameTok.getLength());
1911 FilenameStart = &FilenameBuffer[0];
1912 unsigned Len = getSpelling(FilenameTok, FilenameStart);
1913 FilenameEnd = FilenameStart+Len;
1914 break;
Chris Lattnerf97dbcb2007-07-23 22:23:52 +00001915 }
Chris Lattner43eafb42007-07-23 04:56:47 +00001916
1917 case tok::less:
1918 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1919 // case, glue the tokens together into FilenameBuffer and interpret those.
1920 FilenameBuffer.push_back('<');
1921 if (ConcatenateIncludeName(FilenameBuffer, *this))
1922 return; // Found <eom> but no ">"? Diagnostic already emitted.
1923 FilenameStart = &FilenameBuffer[0];
1924 FilenameEnd = &FilenameBuffer[FilenameBuffer.size()];
1925 break;
1926 default:
1927 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1928 DiscardUntilEndOfDirective();
1929 return;
1930 }
1931
Chris Lattner93ab9f12007-07-23 04:15:27 +00001932 bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(),
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001933 FilenameStart, FilenameEnd);
1934 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1935 // error.
Chris Lattner43eafb42007-07-23 04:56:47 +00001936 if (FilenameStart == 0) {
1937 DiscardUntilEndOfDirective();
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001938 return;
Chris Lattner43eafb42007-07-23 04:56:47 +00001939 }
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001940
Chris Lattner269c2322006-06-25 06:23:00 +00001941 // Verify that there is nothing after the filename, other than EOM. Use the
1942 // preprocessor to lex this in case lexing the filename entered a macro.
1943 CheckEndOfDirective("#include");
Chris Lattner22eb9722006-06-18 05:43:12 +00001944
1945 // Check that we don't have infinite #include recursion.
Chris Lattner69772b02006-07-02 20:34:39 +00001946 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
Chris Lattner22eb9722006-06-18 05:43:12 +00001947 return Diag(FilenameTok, diag::err_pp_include_too_deep);
1948
Chris Lattner22eb9722006-06-18 05:43:12 +00001949 // Search include directories.
Chris Lattnerc8997182006-06-22 05:52:16 +00001950 const DirectoryLookup *CurDir;
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001951 const FileEntry *File = LookupFile(FilenameStart, FilenameEnd,
Chris Lattnerb8b94f12006-10-30 05:38:06 +00001952 isAngled, LookupFrom, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001953 if (File == 0)
Chris Lattner7c718bd2007-04-10 06:02:46 +00001954 return Diag(FilenameTok, diag::err_pp_file_not_found,
1955 std::string(FilenameStart, FilenameEnd));
Chris Lattner22eb9722006-06-18 05:43:12 +00001956
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001957 // Ask HeaderInfo if we should enter this #include file.
1958 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
1959 // If it returns true, #including this file will have no effect.
Chris Lattner3665f162006-07-04 07:26:10 +00001960 return;
1961 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001962
1963 // Look up the file, create a File ID for it.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001964 unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001965 if (FileID == 0)
Chris Lattner7c718bd2007-04-10 06:02:46 +00001966 return Diag(FilenameTok, diag::err_pp_file_not_found,
1967 std::string(FilenameStart, FilenameEnd));
Chris Lattner22eb9722006-06-18 05:43:12 +00001968
1969 // Finally, if all is good, enter the new file!
Chris Lattnerc8997182006-06-22 05:52:16 +00001970 EnterSourceFile(FileID, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001971}
1972
1973/// HandleIncludeNextDirective - Implements #include_next.
1974///
Chris Lattner146762e2007-07-20 16:59:19 +00001975void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) {
Chris Lattnercb283342006-06-18 06:48:37 +00001976 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001977
1978 // #include_next is like #include, except that we start searching after
1979 // the current found directory. If we can't do this, issue a
1980 // diagnostic.
Chris Lattnerc8997182006-06-22 05:52:16 +00001981 const DirectoryLookup *Lookup = CurDirLookup;
Chris Lattner69772b02006-07-02 20:34:39 +00001982 if (isInPrimaryFile()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001983 Lookup = 0;
Chris Lattnercb283342006-06-18 06:48:37 +00001984 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
Chris Lattner22eb9722006-06-18 05:43:12 +00001985 } else if (Lookup == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +00001986 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
Chris Lattnerc8997182006-06-22 05:52:16 +00001987 } else {
1988 // Start looking up in the next directory.
1989 ++Lookup;
Chris Lattner22eb9722006-06-18 05:43:12 +00001990 }
1991
1992 return HandleIncludeDirective(IncludeNextTok, Lookup);
1993}
1994
1995/// HandleImportDirective - Implements #import.
1996///
Chris Lattner146762e2007-07-20 16:59:19 +00001997void Preprocessor::HandleImportDirective(Token &ImportTok) {
Chris Lattnercb283342006-06-18 06:48:37 +00001998 Diag(ImportTok, diag::ext_pp_import_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001999
2000 return HandleIncludeDirective(ImportTok, 0, true);
2001}
2002
Chris Lattnerb8761832006-06-24 21:31:03 +00002003//===----------------------------------------------------------------------===//
2004// Preprocessor Macro Directive Handling.
2005//===----------------------------------------------------------------------===//
2006
Chris Lattnercefc7682006-07-08 08:28:12 +00002007/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
2008/// definition has just been read. Lex the rest of the arguments and the
2009/// closing ), updating MI with what we learn. Return true if an error occurs
2010/// parsing the arg list.
2011bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
Chris Lattner564f4782007-07-14 22:46:43 +00002012 llvm::SmallVector<IdentifierInfo*, 32> Arguments;
2013
Chris Lattner146762e2007-07-20 16:59:19 +00002014 Token Tok;
Chris Lattnercefc7682006-07-08 08:28:12 +00002015 while (1) {
2016 LexUnexpandedToken(Tok);
2017 switch (Tok.getKind()) {
2018 case tok::r_paren:
2019 // Found the end of the argument list.
Chris Lattner564f4782007-07-14 22:46:43 +00002020 if (Arguments.empty()) { // #define FOO()
2021 MI->setArgumentList(Arguments.begin(), Arguments.end());
2022 return false;
2023 }
Chris Lattnercefc7682006-07-08 08:28:12 +00002024 // Otherwise we have #define FOO(A,)
2025 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
2026 return true;
2027 case tok::ellipsis: // #define X(... -> C99 varargs
2028 // Warn if use of C99 feature in non-C99 mode.
2029 if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
2030
2031 // Lex the token after the identifier.
2032 LexUnexpandedToken(Tok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002033 if (Tok.isNot(tok::r_paren)) {
Chris Lattnercefc7682006-07-08 08:28:12 +00002034 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2035 return true;
2036 }
Chris Lattner95a06b32006-07-30 08:40:43 +00002037 // Add the __VA_ARGS__ identifier as an argument.
Chris Lattner564f4782007-07-14 22:46:43 +00002038 Arguments.push_back(Ident__VA_ARGS__);
Chris Lattnercefc7682006-07-08 08:28:12 +00002039 MI->setIsC99Varargs();
Chris Lattner564f4782007-07-14 22:46:43 +00002040 MI->setArgumentList(Arguments.begin(), Arguments.end());
Chris Lattnercefc7682006-07-08 08:28:12 +00002041 return false;
2042 case tok::eom: // #define X(
2043 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2044 return true;
Chris Lattner62aa0d42006-10-20 05:08:24 +00002045 default:
2046 // Handle keywords and identifiers here to accept things like
2047 // #define Foo(for) for.
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002048 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner62aa0d42006-10-20 05:08:24 +00002049 if (II == 0) {
2050 // #define X(1
2051 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
2052 return true;
2053 }
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002054
2055 // If this is already used as an argument, it is used multiple times (e.g.
2056 // #define X(A,A.
Chris Lattner564f4782007-07-14 22:46:43 +00002057 if (std::find(Arguments.begin(), Arguments.end(), II) !=
2058 Arguments.end()) { // C99 6.10.3p6
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002059 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName());
2060 return true;
2061 }
2062
2063 // Add the argument to the macro info.
Chris Lattner564f4782007-07-14 22:46:43 +00002064 Arguments.push_back(II);
Chris Lattnercefc7682006-07-08 08:28:12 +00002065
2066 // Lex the token after the identifier.
2067 LexUnexpandedToken(Tok);
2068
2069 switch (Tok.getKind()) {
2070 default: // #define X(A B
2071 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
2072 return true;
2073 case tok::r_paren: // #define X(A)
Chris Lattner564f4782007-07-14 22:46:43 +00002074 MI->setArgumentList(Arguments.begin(), Arguments.end());
Chris Lattnercefc7682006-07-08 08:28:12 +00002075 return false;
2076 case tok::comma: // #define X(A,
2077 break;
2078 case tok::ellipsis: // #define X(A... -> GCC extension
2079 // Diagnose extension.
2080 Diag(Tok, diag::ext_named_variadic_macro);
2081
2082 // Lex the token after the identifier.
2083 LexUnexpandedToken(Tok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002084 if (Tok.isNot(tok::r_paren)) {
Chris Lattnercefc7682006-07-08 08:28:12 +00002085 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2086 return true;
2087 }
2088
2089 MI->setIsGNUVarargs();
Chris Lattner564f4782007-07-14 22:46:43 +00002090 MI->setArgumentList(Arguments.begin(), Arguments.end());
Chris Lattnercefc7682006-07-08 08:28:12 +00002091 return false;
2092 }
2093 }
2094 }
2095}
2096
Chris Lattner22eb9722006-06-18 05:43:12 +00002097/// HandleDefineDirective - Implements #define. This consumes the entire macro
Chris Lattner81278c62006-10-14 19:03:49 +00002098/// line then lets the caller lex the next real token. If 'isTargetSpecific' is
2099/// true, then this is a "#define_target", otherwise this is a "#define".
Chris Lattner22eb9722006-06-18 05:43:12 +00002100///
Chris Lattner146762e2007-07-20 16:59:19 +00002101void Preprocessor::HandleDefineDirective(Token &DefineTok,
Chris Lattner81278c62006-10-14 19:03:49 +00002102 bool isTargetSpecific) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002103 ++NumDefined;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002104
Chris Lattner146762e2007-07-20 16:59:19 +00002105 Token MacroNameTok;
Chris Lattnere8eef322006-07-08 07:01:00 +00002106 ReadMacroName(MacroNameTok, 1);
Chris Lattner22eb9722006-06-18 05:43:12 +00002107
2108 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002109 if (MacroNameTok.is(tok::eom))
Chris Lattnercb283342006-06-18 06:48:37 +00002110 return;
Chris Lattnerf40fe992007-07-14 22:11:41 +00002111
Chris Lattner457fc152006-07-29 06:30:25 +00002112 // If we are supposed to keep comments in #defines, reenable comment saving
2113 // mode.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00002114 CurLexer->KeepCommentMode = KeepMacroComments;
Chris Lattner457fc152006-07-29 06:30:25 +00002115
Chris Lattner063400e2006-10-14 19:54:15 +00002116 // Create the new macro.
Chris Lattner50b497e2006-06-18 16:32:35 +00002117 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
Chris Lattner81278c62006-10-14 19:03:49 +00002118 if (isTargetSpecific) MI->setIsTargetSpecific();
Chris Lattner22eb9722006-06-18 05:43:12 +00002119
Chris Lattner063400e2006-10-14 19:54:15 +00002120 // If the identifier is an 'other target' macro, clear this bit.
2121 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
2122
2123
Chris Lattner146762e2007-07-20 16:59:19 +00002124 Token Tok;
Chris Lattnercb283342006-06-18 06:48:37 +00002125 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00002126
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002127 // If this is a function-like macro definition, parse the argument list,
2128 // marking each of the identifiers as being used as macro arguments. Also,
2129 // check other constraints on the first token of the macro body.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002130 if (Tok.is(tok::eom)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002131 // If there is no body to this macro, we have no special handling here.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002132 } else if (Tok.is(tok::l_paren) && !Tok.hasLeadingSpace()) {
Chris Lattnercefc7682006-07-08 08:28:12 +00002133 // This is a function-like macro definition. Read the argument list.
2134 MI->setIsFunctionLike();
2135 if (ReadMacroDefinitionArgList(MI)) {
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002136 // Forget about MI.
Chris Lattnercefc7682006-07-08 08:28:12 +00002137 delete MI;
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002138 // Throw away the rest of the line.
Chris Lattnercefc7682006-07-08 08:28:12 +00002139 if (CurLexer->ParsingPreprocessorDirective)
2140 DiscardUntilEndOfDirective();
2141 return;
2142 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002143
Chris Lattner815a1f92006-07-08 20:48:04 +00002144 // Read the first token after the arg list for down below.
2145 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00002146 } else if (!Tok.hasLeadingSpace()) {
2147 // C99 requires whitespace between the macro definition and the body. Emit
2148 // a diagnostic for something like "#define X+".
2149 if (Features.C99) {
Chris Lattnercb283342006-06-18 06:48:37 +00002150 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattner22eb9722006-06-18 05:43:12 +00002151 } else {
2152 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
2153 // one in some cases!
2154 }
2155 } else {
2156 // This is a normal token with leading space. Clear the leading space
2157 // marker on the first token to get proper expansion.
Chris Lattner146762e2007-07-20 16:59:19 +00002158 Tok.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00002159 }
2160
Chris Lattner7e374832006-07-29 03:46:57 +00002161 // If this is a definition of a variadic C99 function-like macro, not using
2162 // the GNU named varargs extension, enabled __VA_ARGS__.
2163
2164 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
2165 // This gets unpoisoned where it is allowed.
2166 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
2167 if (MI->isC99Varargs())
2168 Ident__VA_ARGS__->setIsPoisoned(false);
2169
Chris Lattner22eb9722006-06-18 05:43:12 +00002170 // Read the rest of the macro body.
Chris Lattnera3834342007-07-14 21:54:03 +00002171 if (MI->isObjectLike()) {
2172 // Object-like macros are very simple, just read their body.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002173 while (Tok.isNot(tok::eom)) {
Chris Lattnera3834342007-07-14 21:54:03 +00002174 MI->AddTokenToBody(Tok);
Chris Lattner815a1f92006-07-08 20:48:04 +00002175 // Get the next token of the macro.
2176 LexUnexpandedToken(Tok);
Chris Lattner815a1f92006-07-08 20:48:04 +00002177 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002178
Chris Lattnera3834342007-07-14 21:54:03 +00002179 } else {
2180 // Otherwise, read the body of a function-like macro. This has to validate
2181 // the # (stringize) operator.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002182 while (Tok.isNot(tok::eom)) {
Chris Lattnera3834342007-07-14 21:54:03 +00002183 MI->AddTokenToBody(Tok);
Chris Lattnerbff18d52006-07-06 04:49:18 +00002184
Chris Lattnera3834342007-07-14 21:54:03 +00002185 // Check C99 6.10.3.2p1: ensure that # operators are followed by macro
2186 // parameters in function-like macro expansions.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002187 if (Tok.isNot(tok::hash)) {
Chris Lattnera3834342007-07-14 21:54:03 +00002188 // Get the next token of the macro.
2189 LexUnexpandedToken(Tok);
2190 continue;
2191 }
2192
2193 // Get the next token of the macro.
2194 LexUnexpandedToken(Tok);
2195
2196 // Not a macro arg identifier?
2197 if (!Tok.getIdentifierInfo() ||
2198 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
2199 Diag(Tok, diag::err_pp_stringize_not_parameter);
2200 delete MI;
2201
2202 // Disable __VA_ARGS__ again.
2203 Ident__VA_ARGS__->setIsPoisoned(true);
2204 return;
2205 }
2206
2207 // Things look ok, add the param name token to the macro.
2208 MI->AddTokenToBody(Tok);
2209
2210 // Get the next token of the macro.
2211 LexUnexpandedToken(Tok);
2212 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002213 }
Chris Lattner7e374832006-07-29 03:46:57 +00002214
Chris Lattnerf40fe992007-07-14 22:11:41 +00002215
Chris Lattner7e374832006-07-29 03:46:57 +00002216 // Disable __VA_ARGS__ again.
2217 Ident__VA_ARGS__->setIsPoisoned(true);
Chris Lattnerbff18d52006-07-06 04:49:18 +00002218
Chris Lattnerbff18d52006-07-06 04:49:18 +00002219 // Check that there is no paste (##) operator at the begining or end of the
2220 // replacement list.
Chris Lattner78186052006-07-09 00:45:31 +00002221 unsigned NumTokens = MI->getNumTokens();
Chris Lattnerbff18d52006-07-06 04:49:18 +00002222 if (NumTokens != 0) {
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002223 if (MI->getReplacementToken(0).is(tok::hashhash)) {
Chris Lattner815a1f92006-07-08 20:48:04 +00002224 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
Chris Lattnerbff18d52006-07-06 04:49:18 +00002225 delete MI;
Chris Lattner815a1f92006-07-08 20:48:04 +00002226 return;
Chris Lattnerbff18d52006-07-06 04:49:18 +00002227 }
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002228 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
Chris Lattner815a1f92006-07-08 20:48:04 +00002229 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
Chris Lattnerbff18d52006-07-06 04:49:18 +00002230 delete MI;
Chris Lattner815a1f92006-07-08 20:48:04 +00002231 return;
Chris Lattnerbff18d52006-07-06 04:49:18 +00002232 }
2233 }
2234
Chris Lattner13044d92006-07-03 05:16:44 +00002235 // If this is the primary source file, remember that this macro hasn't been
2236 // used yet.
2237 if (isInPrimaryFile())
2238 MI->setIsUsed(false);
2239
Chris Lattner22eb9722006-06-18 05:43:12 +00002240 // Finally, if this identifier already had a macro defined for it, verify that
2241 // the macro bodies are identical and free the old definition.
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002242 if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Chris Lattner13044d92006-07-03 05:16:44 +00002243 if (!OtherMI->isUsed())
2244 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2245
Chris Lattner22eb9722006-06-18 05:43:12 +00002246 // Macros must be identical. This means all tokes and whitespace separation
Chris Lattner21284df2006-07-08 07:16:08 +00002247 // must be the same. C99 6.10.3.2.
2248 if (!MI->isIdenticalTo(*OtherMI, *this)) {
Chris Lattnere8eef322006-07-08 07:01:00 +00002249 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef,
2250 MacroNameTok.getIdentifierInfo()->getName());
2251 Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2);
2252 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002253 delete OtherMI;
2254 }
2255
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002256 setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
Chris Lattner22eb9722006-06-18 05:43:12 +00002257}
2258
Chris Lattner063400e2006-10-14 19:54:15 +00002259/// HandleDefineOtherTargetDirective - Implements #define_other_target.
Chris Lattner146762e2007-07-20 16:59:19 +00002260void Preprocessor::HandleDefineOtherTargetDirective(Token &Tok) {
2261 Token MacroNameTok;
Chris Lattner063400e2006-10-14 19:54:15 +00002262 ReadMacroName(MacroNameTok, 1);
2263
2264 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002265 if (MacroNameTok.is(tok::eom))
Chris Lattner063400e2006-10-14 19:54:15 +00002266 return;
2267
2268 // Check to see if this is the last token on the #undef line.
2269 CheckEndOfDirective("#define_other_target");
2270
2271 // If there is already a macro defined by this name, turn it into a
2272 // target-specific define.
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002273 if (MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Chris Lattner063400e2006-10-14 19:54:15 +00002274 MI->setIsTargetSpecific(true);
2275 return;
2276 }
2277
2278 // Mark the identifier as being a macro on some other target.
2279 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro();
2280}
2281
Chris Lattner22eb9722006-06-18 05:43:12 +00002282
2283/// HandleUndefDirective - Implements #undef.
2284///
Chris Lattner146762e2007-07-20 16:59:19 +00002285void Preprocessor::HandleUndefDirective(Token &UndefTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002286 ++NumUndefined;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002287
Chris Lattner146762e2007-07-20 16:59:19 +00002288 Token MacroNameTok;
Chris Lattnere8eef322006-07-08 07:01:00 +00002289 ReadMacroName(MacroNameTok, 2);
Chris Lattner22eb9722006-06-18 05:43:12 +00002290
2291 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002292 if (MacroNameTok.is(tok::eom))
Chris Lattnercb283342006-06-18 06:48:37 +00002293 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00002294
2295 // Check to see if this is the last token on the #undef line.
Chris Lattnercb283342006-06-18 06:48:37 +00002296 CheckEndOfDirective("#undef");
Chris Lattner22eb9722006-06-18 05:43:12 +00002297
2298 // Okay, we finally have a valid identifier to undef.
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002299 MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
Chris Lattner22eb9722006-06-18 05:43:12 +00002300
Chris Lattner063400e2006-10-14 19:54:15 +00002301 // #undef untaints an identifier if it were marked by define_other_target.
2302 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
2303
Chris Lattner22eb9722006-06-18 05:43:12 +00002304 // If the macro is not defined, this is a noop undef, just return.
Chris Lattnercb283342006-06-18 06:48:37 +00002305 if (MI == 0) return;
Chris Lattner677757a2006-06-28 05:26:32 +00002306
Chris Lattner13044d92006-07-03 05:16:44 +00002307 if (!MI->isUsed())
2308 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattner22eb9722006-06-18 05:43:12 +00002309
2310 // Free macro definition.
2311 delete MI;
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002312 setMacroInfo(MacroNameTok.getIdentifierInfo(), 0);
Chris Lattner22eb9722006-06-18 05:43:12 +00002313}
2314
2315
Chris Lattnerb8761832006-06-24 21:31:03 +00002316//===----------------------------------------------------------------------===//
2317// Preprocessor Conditional Directive Handling.
2318//===----------------------------------------------------------------------===//
2319
Chris Lattner22eb9722006-06-18 05:43:12 +00002320/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
Chris Lattner371ac8a2006-07-04 07:11:10 +00002321/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
2322/// if any tokens have been returned or pp-directives activated before this
2323/// #ifndef has been lexed.
Chris Lattner22eb9722006-06-18 05:43:12 +00002324///
Chris Lattner146762e2007-07-20 16:59:19 +00002325void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
Chris Lattner371ac8a2006-07-04 07:11:10 +00002326 bool ReadAnyTokensBeforeDirective) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002327 ++NumIf;
Chris Lattner146762e2007-07-20 16:59:19 +00002328 Token DirectiveTok = Result;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002329
Chris Lattner146762e2007-07-20 16:59:19 +00002330 Token MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00002331 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00002332
2333 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002334 if (MacroNameTok.is(tok::eom)) {
Chris Lattnerd05e44e2007-09-24 05:14:57 +00002335 // Skip code until we get to #endif. This helps with recovery by not
2336 // emitting an error when the #endif is reached.
2337 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2338 /*Foundnonskip*/false, /*FoundElse*/false);
Chris Lattnercb283342006-06-18 06:48:37 +00002339 return;
Chris Lattnerd05e44e2007-09-24 05:14:57 +00002340 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002341
2342 // Check to see if this is the last token on the #if[n]def line.
Chris Lattner371ac8a2006-07-04 07:11:10 +00002343 CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
2344
2345 // If the start of a top-level #ifdef, inform MIOpt.
2346 if (!ReadAnyTokensBeforeDirective &&
2347 CurLexer->getConditionalStackDepth() == 0) {
2348 assert(isIfndef && "#ifdef shouldn't reach here");
2349 CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
2350 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002351
Chris Lattner063400e2006-10-14 19:54:15 +00002352 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002353 MacroInfo *MI = getMacroInfo(MII);
Chris Lattnera78a97e2006-07-03 05:42:18 +00002354
Chris Lattner81278c62006-10-14 19:03:49 +00002355 // If there is a macro, process it.
2356 if (MI) {
2357 // Mark it used.
2358 MI->setIsUsed(true);
2359
2360 // If this is the first use of a target-specific macro, warn about it.
2361 if (MI->isTargetSpecific()) {
2362 MI->setIsTargetSpecific(false); // Don't warn on second use.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +00002363 getTargetInfo().DiagnoseNonPortability(
2364 getFullLoc(MacroNameTok.getLocation()),
2365 diag::port_target_macro_use);
Chris Lattner81278c62006-10-14 19:03:49 +00002366 }
Chris Lattner063400e2006-10-14 19:54:15 +00002367 } else {
2368 // Use of a target-specific macro for some other target? If so, warn.
2369 if (MII->isOtherTargetMacro()) {
2370 MII->setIsOtherTargetMacro(false); // Don't warn on second use.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +00002371 getTargetInfo().DiagnoseNonPortability(
2372 getFullLoc(MacroNameTok.getLocation()),
2373 diag::port_target_macro_use);
Chris Lattner063400e2006-10-14 19:54:15 +00002374 }
Chris Lattner81278c62006-10-14 19:03:49 +00002375 }
Chris Lattnera78a97e2006-07-03 05:42:18 +00002376
Chris Lattner22eb9722006-06-18 05:43:12 +00002377 // Should we include the stuff contained by this directive?
Chris Lattnera78a97e2006-07-03 05:42:18 +00002378 if (!MI == isIfndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002379 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00002380 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00002381 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00002382 } else {
2383 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00002384 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Chris Lattnercb283342006-06-18 06:48:37 +00002385 /*Foundnonskip*/false,
2386 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00002387 }
2388}
2389
2390/// HandleIfDirective - Implements the #if directive.
2391///
Chris Lattner146762e2007-07-20 16:59:19 +00002392void Preprocessor::HandleIfDirective(Token &IfToken,
Chris Lattnera8654ca2006-07-04 17:42:08 +00002393 bool ReadAnyTokensBeforeDirective) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002394 ++NumIf;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002395
Chris Lattner371ac8a2006-07-04 07:11:10 +00002396 // Parse and evaluation the conditional expression.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00002397 IdentifierInfo *IfNDefMacro = 0;
Chris Lattnera8654ca2006-07-04 17:42:08 +00002398 bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
Chris Lattner22eb9722006-06-18 05:43:12 +00002399
2400 // Should we include the stuff contained by this directive?
2401 if (ConditionalTrue) {
Chris Lattnera8654ca2006-07-04 17:42:08 +00002402 // If this condition is equivalent to #ifndef X, and if this is the first
2403 // directive seen, handle it for the multiple-include optimization.
2404 if (!ReadAnyTokensBeforeDirective &&
2405 CurLexer->getConditionalStackDepth() == 0 && IfNDefMacro)
2406 CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
2407
Chris Lattner22eb9722006-06-18 05:43:12 +00002408 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00002409 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00002410 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00002411 } else {
2412 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00002413 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnercb283342006-06-18 06:48:37 +00002414 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00002415 }
2416}
2417
2418/// HandleEndifDirective - Implements the #endif directive.
2419///
Chris Lattner146762e2007-07-20 16:59:19 +00002420void Preprocessor::HandleEndifDirective(Token &EndifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002421 ++NumEndif;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002422
Chris Lattner22eb9722006-06-18 05:43:12 +00002423 // Check that this is the whole directive.
Chris Lattnercb283342006-06-18 06:48:37 +00002424 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00002425
2426 PPConditionalInfo CondInfo;
2427 if (CurLexer->popConditionalLevel(CondInfo)) {
2428 // No conditionals on the stack: this is an #endif without an #if.
2429 return Diag(EndifToken, diag::err_pp_endif_without_if);
2430 }
2431
Chris Lattner371ac8a2006-07-04 07:11:10 +00002432 // If this the end of a top-level #endif, inform MIOpt.
2433 if (CurLexer->getConditionalStackDepth() == 0)
2434 CurLexer->MIOpt.ExitTopLevelConditional();
2435
Chris Lattner538d7f32006-07-20 04:31:52 +00002436 assert(!CondInfo.WasSkipping && !CurLexer->LexingRawMode &&
Chris Lattner22eb9722006-06-18 05:43:12 +00002437 "This code should only be reachable in the non-skipping case!");
Chris Lattner22eb9722006-06-18 05:43:12 +00002438}
2439
2440
Chris Lattner146762e2007-07-20 16:59:19 +00002441void Preprocessor::HandleElseDirective(Token &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002442 ++NumElse;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002443
Chris Lattner22eb9722006-06-18 05:43:12 +00002444 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnercb283342006-06-18 06:48:37 +00002445 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00002446
2447 PPConditionalInfo CI;
2448 if (CurLexer->popConditionalLevel(CI))
2449 return Diag(Result, diag::pp_err_else_without_if);
Chris Lattner371ac8a2006-07-04 07:11:10 +00002450
2451 // If this is a top-level #else, inform the MIOpt.
2452 if (CurLexer->getConditionalStackDepth() == 0)
2453 CurLexer->MIOpt.FoundTopLevelElse();
Chris Lattner22eb9722006-06-18 05:43:12 +00002454
2455 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00002456 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00002457
2458 // Finally, skip the rest of the contents of this block and return the first
2459 // token after it.
2460 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2461 /*FoundElse*/true);
2462}
2463
Chris Lattner146762e2007-07-20 16:59:19 +00002464void Preprocessor::HandleElifDirective(Token &ElifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002465 ++NumElse;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002466
Chris Lattner22eb9722006-06-18 05:43:12 +00002467 // #elif directive in a non-skipping conditional... start skipping.
2468 // We don't care what the condition is, because we will always skip it (since
2469 // the block immediately before it was included).
Chris Lattnercb283342006-06-18 06:48:37 +00002470 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00002471
2472 PPConditionalInfo CI;
2473 if (CurLexer->popConditionalLevel(CI))
2474 return Diag(ElifToken, diag::pp_err_elif_without_if);
2475
Chris Lattner371ac8a2006-07-04 07:11:10 +00002476 // If this is a top-level #elif, inform the MIOpt.
2477 if (CurLexer->getConditionalStackDepth() == 0)
2478 CurLexer->MIOpt.FoundTopLevelElse();
2479
Chris Lattner22eb9722006-06-18 05:43:12 +00002480 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00002481 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00002482
2483 // Finally, skip the rest of the contents of this block and return the first
2484 // token after it.
2485 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2486 /*FoundElse*/CI.FoundElse);
2487}
Chris Lattnerb8761832006-06-24 21:31:03 +00002488