blob: f4e737413abefbc61819c2ab8c9d38b55712f8a7 [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 }
Steve Naroffb2c80c72008-02-07 03:50:06 +0000427 if (PP.getLangOptions().Microsoft) {
428 DefineBuiltinMacro(Buf, "__stdcall=");
429 DefineBuiltinMacro(Buf, "__cdecl=");
430 DefineBuiltinMacro(Buf, "_cdecl=");
431 DefineBuiltinMacro(Buf, "__ptr64=");
432 DefineBuiltinMacro(Buf, "__forceinline=");
433 }
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000434 // FIXME: Should emit a #line directive here.
435}
436
437
438/// EnterMainSourceFile - Enter the specified FileID as the main source file,
Nate Begemanf7c3ff62008-01-07 04:01:26 +0000439/// which implicitly adds the builtin defines etc.
Ted Kremenek230bd912007-12-19 22:51:13 +0000440void Preprocessor::EnterMainSourceFile() {
441
442 unsigned MainFileID = SourceMgr.getMainFileID();
443
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000444 // Enter the main file source buffer.
445 EnterSourceFile(MainFileID, 0);
446
Chris Lattner609d4132007-11-15 19:07:47 +0000447 // Tell the header info that the main file was entered. If the file is later
448 // #imported, it won't be re-entered.
449 if (const FileEntry *FE =
450 SourceMgr.getFileEntryForLoc(SourceLocation::getFileLoc(MainFileID, 0)))
451 HeaderInfo.IncrementIncludeCount(FE);
452
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000453 std::vector<char> PrologFile;
454 PrologFile.reserve(4080);
455
456 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
457 InitializePredefinedMacros(*this, PrologFile);
458
459 // Add on the predefines from the driver.
460 PrologFile.insert(PrologFile.end(), Predefines,Predefines+strlen(Predefines));
461
462 // Memory buffer must end with a null byte!
463 PrologFile.push_back(0);
464
465 // Now that we have emitted the predefined macros, #includes, etc into
466 // PrologFile, preprocess it to populate the initial preprocessor state.
467 llvm::MemoryBuffer *SB =
468 llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(),
469 "<predefines>");
470 assert(SB && "Cannot fail to create predefined source buffer");
471 unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB);
472 assert(FileID && "Could not create FileID for predefines?");
473
474 // Start parsing the predefines.
475 EnterSourceFile(FileID, 0);
476}
Chris Lattner8a7003c2007-07-16 06:48:38 +0000477
Chris Lattnerd01e2912006-06-18 16:22:51 +0000478//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +0000479// Source File Location Methods.
480//===----------------------------------------------------------------------===//
481
Chris Lattner22eb9722006-06-18 05:43:12 +0000482/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
483/// return null on failure. isAngled indicates whether the file reference is
484/// for system #include's or not (i.e. using <> instead of "").
Chris Lattnerb8b94f12006-10-30 05:38:06 +0000485const FileEntry *Preprocessor::LookupFile(const char *FilenameStart,
486 const char *FilenameEnd,
Chris Lattnerc8997182006-06-22 05:52:16 +0000487 bool isAngled,
Chris Lattner22eb9722006-06-18 05:43:12 +0000488 const DirectoryLookup *FromDir,
Chris Lattnerc8997182006-06-22 05:52:16 +0000489 const DirectoryLookup *&CurDir) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000490 // If the header lookup mechanism may be relative to the current file, pass in
491 // info about where the current file is.
492 const FileEntry *CurFileEnt = 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000493 if (!FromDir) {
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000494 SourceLocation FileLoc = getCurrentFileLexer()->getFileLoc();
495 CurFileEnt = SourceMgr.getFileEntryForLoc(FileLoc);
Chris Lattner22eb9722006-06-18 05:43:12 +0000496 }
497
Chris Lattner63dd32b2006-10-20 04:42:40 +0000498 // Do a standard file entry lookup.
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000499 CurDir = CurDirLookup;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000500 const FileEntry *FE =
Chris Lattner7cdbad92006-10-30 05:33:15 +0000501 HeaderInfo.LookupFile(FilenameStart, FilenameEnd,
502 isAngled, FromDir, CurDir, CurFileEnt);
Chris Lattner63dd32b2006-10-20 04:42:40 +0000503 if (FE) return FE;
504
505 // Otherwise, see if this is a subframework header. If so, this is relative
506 // to one of the headers on the #include stack. Walk the list of the current
507 // headers on the #include stack and pass them to HeaderInfo.
Chris Lattner5c683b22006-10-20 05:12:14 +0000508 if (CurLexer && !CurLexer->Is_PragmaLexer) {
Chris Lattner12261882008-02-01 05:34:02 +0000509 if ((CurFileEnt = SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())))
510 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
511 CurFileEnt)))
512 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000513 }
514
515 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
516 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
Chris Lattner5c683b22006-10-20 05:12:14 +0000517 if (ISEntry.TheLexer && !ISEntry.TheLexer->Is_PragmaLexer) {
Chris Lattner12261882008-02-01 05:34:02 +0000518 if ((CurFileEnt =
519 SourceMgr.getFileEntryForLoc(ISEntry.TheLexer->getFileLoc())))
520 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart,
521 FilenameEnd, CurFileEnt)))
522 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000523 }
524 }
525
526 // Otherwise, we really couldn't find the file.
527 return 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000528}
529
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000530/// isInPrimaryFile - Return true if we're in the top-level file, not in a
531/// #include.
532bool Preprocessor::isInPrimaryFile() const {
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000533 if (CurLexer && !CurLexer->Is_PragmaLexer)
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000534 return IncludeMacroStack.empty();
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000535
Chris Lattner13044d92006-07-03 05:16:44 +0000536 // If there are any stacked lexers, we're in a #include.
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000537 assert(IncludeMacroStack[0].TheLexer &&
538 !IncludeMacroStack[0].TheLexer->Is_PragmaLexer &&
539 "Top level include stack isn't our primary lexer?");
540 for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i)
Chris Lattner13044d92006-07-03 05:16:44 +0000541 if (IncludeMacroStack[i].TheLexer &&
542 !IncludeMacroStack[i].TheLexer->Is_PragmaLexer)
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000543 return false;
544 return true;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000545}
546
547/// getCurrentLexer - Return the current file lexer being lexed from. Note
548/// that this ignores any potentially active macro expansions and _Pragma
549/// expansions going on at the time.
550Lexer *Preprocessor::getCurrentFileLexer() const {
551 if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer;
552
553 // Look for a stacked lexer.
554 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Chris Lattnerf88c53a2006-07-03 05:26:05 +0000555 Lexer *L = IncludeMacroStack[i-1].TheLexer;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000556 if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions.
557 return L;
558 }
559 return 0;
560}
561
562
Chris Lattner22eb9722006-06-18 05:43:12 +0000563/// EnterSourceFile - Add a source file to the top of the include stack and
564/// start lexing tokens from it instead of the current buffer. Return true
565/// on failure.
566void Preprocessor::EnterSourceFile(unsigned FileID,
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000567 const DirectoryLookup *CurDir) {
Chris Lattner69772b02006-07-02 20:34:39 +0000568 assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000569 ++NumEnteredSourceFiles;
570
Chris Lattner69772b02006-07-02 20:34:39 +0000571 if (MaxIncludeStackDepth < IncludeMacroStack.size())
572 MaxIncludeStackDepth = IncludeMacroStack.size();
Chris Lattner22eb9722006-06-18 05:43:12 +0000573
Chris Lattner77e9de52007-07-20 16:52:03 +0000574 Lexer *TheLexer = new Lexer(SourceLocation::getFileLoc(FileID, 0), *this);
Chris Lattner69772b02006-07-02 20:34:39 +0000575 EnterSourceFileWithLexer(TheLexer, CurDir);
576}
Chris Lattner22eb9722006-06-18 05:43:12 +0000577
Chris Lattner69772b02006-07-02 20:34:39 +0000578/// EnterSourceFile - Add a source file to the top of the include stack and
579/// start lexing tokens from it instead of the current buffer.
580void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
581 const DirectoryLookup *CurDir) {
582
583 // Add the current lexer to the include stack.
584 if (CurLexer || CurMacroExpander)
585 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
586 CurMacroExpander));
587
588 CurLexer = TheLexer;
Chris Lattnerc8997182006-06-22 05:52:16 +0000589 CurDirLookup = CurDir;
Chris Lattner69772b02006-07-02 20:34:39 +0000590 CurMacroExpander = 0;
Chris Lattner0c885f52006-06-21 06:50:18 +0000591
592 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000593 if (Callbacks && !CurLexer->Is_PragmaLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000594 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
595
596 // Get the file entry for the current file.
597 if (const FileEntry *FE =
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000598 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000599 FileType = HeaderInfo.getFileDirFlavor(FE);
Chris Lattnerc8997182006-06-22 05:52:16 +0000600
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000601 Callbacks->FileChanged(CurLexer->getFileLoc(),
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000602 PPCallbacks::EnterFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +0000603 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000604}
605
Chris Lattner69772b02006-07-02 20:34:39 +0000606
607
Chris Lattner22eb9722006-06-18 05:43:12 +0000608/// EnterMacro - Add a Macro to the top of the include stack and start lexing
Chris Lattnercb283342006-06-18 06:48:37 +0000609/// tokens from it instead of the current buffer.
Chris Lattner146762e2007-07-20 16:59:19 +0000610void Preprocessor::EnterMacro(Token &Tok, MacroArgs *Args) {
Chris Lattner69772b02006-07-02 20:34:39 +0000611 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
612 CurMacroExpander));
613 CurLexer = 0;
614 CurDirLookup = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000615
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000616 if (NumCachedMacroExpanders == 0) {
617 CurMacroExpander = new MacroExpander(Tok, Args, *this);
618 } else {
619 CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
620 CurMacroExpander->Init(Tok, Args);
621 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000622}
623
Chris Lattner7667d0d2006-07-16 18:16:58 +0000624/// EnterTokenStream - Add a "macro" context to the top of the include stack,
625/// which will cause the lexer to start returning the specified tokens. Note
626/// that these tokens will be re-macro-expanded when/if expansion is enabled.
627/// This method assumes that the specified stream of tokens has a permanent
628/// owner somewhere, so they do not need to be copied.
Chris Lattner146762e2007-07-20 16:59:19 +0000629void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks) {
Chris Lattner7667d0d2006-07-16 18:16:58 +0000630 // Save our current state.
631 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
632 CurMacroExpander));
633 CurLexer = 0;
634 CurDirLookup = 0;
635
636 // Create a macro expander to expand from the specified token stream.
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000637 if (NumCachedMacroExpanders == 0) {
638 CurMacroExpander = new MacroExpander(Toks, NumToks, *this);
639 } else {
640 CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
641 CurMacroExpander->Init(Toks, NumToks);
642 }
Chris Lattner7667d0d2006-07-16 18:16:58 +0000643}
644
645/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
646/// lexer stack. This should only be used in situations where the current
647/// state of the top-of-stack lexer is known.
648void Preprocessor::RemoveTopOfLexerStack() {
649 assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000650
651 if (CurMacroExpander) {
652 // Delete or cache the now-dead macro expander.
653 if (NumCachedMacroExpanders == MacroExpanderCacheSize)
654 delete CurMacroExpander;
655 else
656 MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander;
657 } else {
658 delete CurLexer;
659 }
Chris Lattner7667d0d2006-07-16 18:16:58 +0000660 CurLexer = IncludeMacroStack.back().TheLexer;
661 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
662 CurMacroExpander = IncludeMacroStack.back().TheMacroExpander;
663 IncludeMacroStack.pop_back();
664}
665
Chris Lattner22eb9722006-06-18 05:43:12 +0000666//===----------------------------------------------------------------------===//
Chris Lattner677757a2006-06-28 05:26:32 +0000667// Macro Expansion Handling.
Chris Lattner22eb9722006-06-18 05:43:12 +0000668//===----------------------------------------------------------------------===//
669
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000670/// setMacroInfo - Specify a macro for this identifier.
671///
672void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
673 if (MI == 0) {
674 if (II->hasMacroDefinition()) {
675 Macros.erase(II);
676 II->setHasMacroDefinition(false);
677 }
678 } else {
679 Macros[II] = MI;
680 II->setHasMacroDefinition(true);
681 }
682}
683
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000684/// RegisterBuiltinMacro - Register the specified identifier in the identifier
685/// table and mark it as a builtin macro to be expanded.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000686IdentifierInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000687 // Get the identifier.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000688 IdentifierInfo *Id = getIdentifierInfo(Name);
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000689
690 // Mark it as being a macro that is builtin.
691 MacroInfo *MI = new MacroInfo(SourceLocation());
692 MI->setIsBuiltinMacro();
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000693 setMacroInfo(Id, MI);
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000694 return Id;
695}
696
697
Chris Lattner677757a2006-06-28 05:26:32 +0000698/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
699/// identifier table.
700void Preprocessor::RegisterBuiltinMacros() {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000701 Ident__LINE__ = RegisterBuiltinMacro("__LINE__");
Chris Lattner630b33c2006-07-01 22:46:53 +0000702 Ident__FILE__ = RegisterBuiltinMacro("__FILE__");
Chris Lattnerc673f902006-06-30 06:10:41 +0000703 Ident__DATE__ = RegisterBuiltinMacro("__DATE__");
704 Ident__TIME__ = RegisterBuiltinMacro("__TIME__");
Chris Lattner69772b02006-07-02 20:34:39 +0000705 Ident_Pragma = RegisterBuiltinMacro("_Pragma");
Chris Lattnerc1283b92006-07-01 23:16:30 +0000706
707 // GCC Extensions.
708 Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__");
709 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__");
Chris Lattner847e0e42006-07-01 23:49:16 +0000710 Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__");
Chris Lattner22eb9722006-06-18 05:43:12 +0000711}
712
Chris Lattnerc2395832006-07-09 00:57:04 +0000713/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
714/// in its expansion, currently expands to that token literally.
Chris Lattner3ce1d1a2006-07-09 01:00:18 +0000715static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000716 const IdentifierInfo *MacroIdent,
717 Preprocessor &PP) {
Chris Lattnerc2395832006-07-09 00:57:04 +0000718 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
719
720 // If the token isn't an identifier, it's always literally expanded.
721 if (II == 0) return true;
722
723 // If the identifier is a macro, and if that macro is enabled, it may be
724 // expanded so it's not a trivial expansion.
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000725 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
Chris Lattner3ce1d1a2006-07-09 01:00:18 +0000726 // Fast expanding "#define X X" is ok, because X would be disabled.
727 II != MacroIdent)
Chris Lattnerc2395832006-07-09 00:57:04 +0000728 return false;
729
730 // If this is an object-like macro invocation, it is safe to trivially expand
731 // it.
732 if (MI->isObjectLike()) return true;
733
734 // If this is a function-like macro invocation, it's safe to trivially expand
735 // as long as the identifier is not a macro argument.
736 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
737 I != E; ++I)
738 if (*I == II)
739 return false; // Identifier is a macro argument.
Chris Lattner273ddd52006-07-29 07:33:01 +0000740
Chris Lattnerc2395832006-07-09 00:57:04 +0000741 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000742}
743
Chris Lattnerc2395832006-07-09 00:57:04 +0000744
Chris Lattnerafe603f2006-07-11 04:02:46 +0000745/// isNextPPTokenLParen - Determine whether the next preprocessor token to be
746/// lexed is a '('. If so, consume the token and return true, if not, this
747/// method should have no observable side-effect on the lexed tokens.
748bool Preprocessor::isNextPPTokenLParen() {
Chris Lattnerafe603f2006-07-11 04:02:46 +0000749 // Do some quick tests for rejection cases.
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000750 unsigned Val;
751 if (CurLexer)
Chris Lattner678c8802006-07-11 05:46:12 +0000752 Val = CurLexer->isNextPPTokenLParen();
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000753 else
754 Val = CurMacroExpander->isNextTokenLParen();
755
756 if (Val == 2) {
Chris Lattner5c983792007-07-19 00:07:36 +0000757 // We have run off the end. If it's a source file we don't
758 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
759 // macro stack.
760 if (CurLexer)
761 return false;
762 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000763 IncludeStackInfo &Entry = IncludeMacroStack[i-1];
764 if (Entry.TheLexer)
Chris Lattner678c8802006-07-11 05:46:12 +0000765 Val = Entry.TheLexer->isNextPPTokenLParen();
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000766 else
767 Val = Entry.TheMacroExpander->isNextTokenLParen();
Chris Lattner5c983792007-07-19 00:07:36 +0000768
769 if (Val != 2)
770 break;
771
772 // Ran off the end of a source file?
773 if (Entry.TheLexer)
774 return false;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000775 }
Chris Lattnerafe603f2006-07-11 04:02:46 +0000776 }
777
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000778 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
779 // have found something that isn't a '(' or we found the end of the
780 // translation unit. In either case, return false.
781 if (Val != 1)
782 return false;
Chris Lattnerafe603f2006-07-11 04:02:46 +0000783
Chris Lattner146762e2007-07-20 16:59:19 +0000784 Token Tok;
Chris Lattnerafe603f2006-07-11 04:02:46 +0000785 LexUnexpandedToken(Tok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000786 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000787 return true;
Chris Lattnerafe603f2006-07-11 04:02:46 +0000788}
Chris Lattner677757a2006-06-28 05:26:32 +0000789
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000790/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
791/// expanded as a macro, handle it and return the next token as 'Identifier'.
Chris Lattner146762e2007-07-20 16:59:19 +0000792bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000793 MacroInfo *MI) {
Chris Lattnera30be592008-01-07 19:50:27 +0000794 // If this is a macro exapnsion in the "#if !defined(x)" line for the file,
795 // then the macro could expand to different things in other contexts, we need
796 // to disable the optimization in this case.
797 if (CurLexer) CurLexer->MIOpt.ExpandedMacro();
Chris Lattner78186052006-07-09 00:45:31 +0000798
799 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
800 if (MI->isBuiltinMacro()) {
801 ExpandBuiltinMacro(Identifier);
802 return false;
803 }
804
Chris Lattner81278c62006-10-14 19:03:49 +0000805 // If this is the first use of a target-specific macro, warn about it.
806 if (MI->isTargetSpecific()) {
807 MI->setIsTargetSpecific(false); // Don't warn on second use.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000808 getTargetInfo().DiagnoseNonPortability(getFullLoc(Identifier.getLocation()),
Chris Lattner81278c62006-10-14 19:03:49 +0000809 diag::port_target_macro_use);
810 }
811
Chris Lattneree8760b2006-07-15 07:42:55 +0000812 /// Args - If this is a function-like macro expansion, this contains,
Chris Lattner78186052006-07-09 00:45:31 +0000813 /// for each macro argument, the list of tokens that were provided to the
814 /// invocation.
Chris Lattneree8760b2006-07-15 07:42:55 +0000815 MacroArgs *Args = 0;
Chris Lattner78186052006-07-09 00:45:31 +0000816
817 // If this is a function-like macro, read the arguments.
818 if (MI->isFunctionLike()) {
Chris Lattner78186052006-07-09 00:45:31 +0000819 // C99 6.10.3p10: If the preprocessing token immediately after the the macro
Chris Lattner24dbee72007-07-19 16:11:58 +0000820 // name isn't a '(', this macro should not be expanded. Otherwise, consume
821 // it.
Chris Lattnerafe603f2006-07-11 04:02:46 +0000822 if (!isNextPPTokenLParen())
Chris Lattner78186052006-07-09 00:45:31 +0000823 return true;
824
Chris Lattner78186052006-07-09 00:45:31 +0000825 // Remember that we are now parsing the arguments to a macro invocation.
826 // Preprocessor directives used inside macro arguments are not portable, and
827 // this enables the warning.
Chris Lattneree8760b2006-07-15 07:42:55 +0000828 InMacroArgs = true;
829 Args = ReadFunctionLikeMacroArgs(Identifier, MI);
Chris Lattner78186052006-07-09 00:45:31 +0000830
831 // Finished parsing args.
Chris Lattneree8760b2006-07-15 07:42:55 +0000832 InMacroArgs = false;
Chris Lattner78186052006-07-09 00:45:31 +0000833
834 // If there was an error parsing the arguments, bail out.
Chris Lattneree8760b2006-07-15 07:42:55 +0000835 if (Args == 0) return false;
Chris Lattner78186052006-07-09 00:45:31 +0000836
837 ++NumFnMacroExpanded;
838 } else {
839 ++NumMacroExpanded;
840 }
Chris Lattner13044d92006-07-03 05:16:44 +0000841
842 // Notice that this macro has been used.
843 MI->setIsUsed(true);
Chris Lattner69772b02006-07-02 20:34:39 +0000844
845 // If we started lexing a macro, enter the macro expansion body.
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000846
847 // If this macro expands to no tokens, don't bother to push it onto the
848 // expansion stack, only to take it right back off.
849 if (MI->getNumTokens() == 0) {
Chris Lattner2ada5d32006-07-15 07:51:24 +0000850 // No need for arg info.
Chris Lattnerc1410dc2006-07-26 05:22:49 +0000851 if (Args) Args->destroy();
Chris Lattner78186052006-07-09 00:45:31 +0000852
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000853 // Ignore this macro use, just return the next token in the current
854 // buffer.
855 bool HadLeadingSpace = Identifier.hasLeadingSpace();
856 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
857
858 Lex(Identifier);
859
860 // If the identifier isn't on some OTHER line, inherit the leading
861 // whitespace/first-on-a-line property of this token. This handles
862 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
863 // empty.
864 if (!Identifier.isAtStartOfLine()) {
Chris Lattner146762e2007-07-20 16:59:19 +0000865 if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
866 if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000867 }
868 ++NumFastMacroExpanded;
Chris Lattner78186052006-07-09 00:45:31 +0000869 return false;
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000870
Chris Lattner3ce1d1a2006-07-09 01:00:18 +0000871 } else if (MI->getNumTokens() == 1 &&
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000872 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
873 *this)){
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000874 // Otherwise, if this macro expands into a single trivially-expanded
875 // token: expand it now. This handles common cases like
876 // "#define VAL 42".
877
878 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
879 // identifier to the expanded token.
880 bool isAtStartOfLine = Identifier.isAtStartOfLine();
881 bool hasLeadingSpace = Identifier.hasLeadingSpace();
882
883 // Remember where the token is instantiated.
884 SourceLocation InstantiateLoc = Identifier.getLocation();
885
886 // Replace the result token.
887 Identifier = MI->getReplacementToken(0);
888
889 // Restore the StartOfLine/LeadingSpace markers.
Chris Lattner146762e2007-07-20 16:59:19 +0000890 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
891 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000892
893 // Update the tokens location to include both its logical and physical
894 // locations.
895 SourceLocation Loc =
Chris Lattnerc673f902006-06-30 06:10:41 +0000896 SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc);
Chris Lattner8c204872006-10-14 05:19:21 +0000897 Identifier.setLocation(Loc);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000898
Chris Lattner6e4bf522006-07-27 06:59:25 +0000899 // If this is #define X X, we must mark the result as unexpandible.
900 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo())
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000901 if (getMacroInfo(NewII) == MI)
Chris Lattner146762e2007-07-20 16:59:19 +0000902 Identifier.setFlag(Token::DisableExpand);
Chris Lattner6e4bf522006-07-27 06:59:25 +0000903
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000904 // Since this is not an identifier token, it can't be macro expanded, so
905 // we're done.
906 ++NumFastMacroExpanded;
Chris Lattner78186052006-07-09 00:45:31 +0000907 return false;
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000908 }
909
Chris Lattner78186052006-07-09 00:45:31 +0000910 // Start expanding the macro.
Chris Lattneree8760b2006-07-15 07:42:55 +0000911 EnterMacro(Identifier, Args);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000912
913 // Now that the macro is at the top of the include stack, ask the
914 // preprocessor to read the next token from it.
Chris Lattner78186052006-07-09 00:45:31 +0000915 Lex(Identifier);
916 return false;
917}
918
Chris Lattneree8760b2006-07-15 07:42:55 +0000919/// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
Chris Lattner2ada5d32006-07-15 07:51:24 +0000920/// invoked to read all of the actual arguments specified for the macro
Chris Lattner78186052006-07-09 00:45:31 +0000921/// invocation. This returns null on error.
Chris Lattner146762e2007-07-20 16:59:19 +0000922MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
Chris Lattneree8760b2006-07-15 07:42:55 +0000923 MacroInfo *MI) {
Chris Lattner78186052006-07-09 00:45:31 +0000924 // The number of fixed arguments to parse.
925 unsigned NumFixedArgsLeft = MI->getNumArgs();
926 bool isVariadic = MI->isVariadic();
927
Chris Lattner78186052006-07-09 00:45:31 +0000928 // Outer loop, while there are more arguments, keep reading them.
Chris Lattner146762e2007-07-20 16:59:19 +0000929 Token Tok;
Chris Lattner8c204872006-10-14 05:19:21 +0000930 Tok.setKind(tok::comma);
Chris Lattner78186052006-07-09 00:45:31 +0000931 --NumFixedArgsLeft; // Start reading the first arg.
Chris Lattner36b6e812006-07-21 06:38:30 +0000932
933 // ArgTokens - Build up a list of tokens that make up each argument. Each
Chris Lattner7a4af3b2006-07-26 06:26:52 +0000934 // argument is separated by an EOF token. Use a SmallVector so we can avoid
935 // heap allocations in the common case.
Chris Lattner146762e2007-07-20 16:59:19 +0000936 llvm::SmallVector<Token, 64> ArgTokens;
Chris Lattner36b6e812006-07-21 06:38:30 +0000937
938 unsigned NumActuals = 0;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000939 while (Tok.is(tok::comma)) {
Chris Lattner24dbee72007-07-19 16:11:58 +0000940 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
941 // that we already consumed the first one.
Chris Lattner78186052006-07-09 00:45:31 +0000942 unsigned NumParens = 0;
Chris Lattner36b6e812006-07-21 06:38:30 +0000943
Chris Lattner78186052006-07-09 00:45:31 +0000944 while (1) {
Chris Lattnerafe603f2006-07-11 04:02:46 +0000945 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
946 // an argument value in a macro could expand to ',' or '(' or ')'.
Chris Lattner78186052006-07-09 00:45:31 +0000947 LexUnexpandedToken(Tok);
948
Chris Lattner97ff7762008-01-22 19:34:51 +0000949 if (Tok.is(tok::eof) || Tok.is(tok::eom)) { // "#if f(<eof>" & "#if f(\n"
Chris Lattner78186052006-07-09 00:45:31 +0000950 Diag(MacroName, diag::err_unterm_macro_invoc);
Chris Lattner97ff7762008-01-22 19:34:51 +0000951 // Do not lose the EOF/EOM. Return it to the client.
Chris Lattner78186052006-07-09 00:45:31 +0000952 MacroName = Tok;
953 return 0;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000954 } else if (Tok.is(tok::r_paren)) {
Chris Lattner78186052006-07-09 00:45:31 +0000955 // If we found the ) token, the macro arg list is done.
956 if (NumParens-- == 0)
957 break;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000958 } else if (Tok.is(tok::l_paren)) {
Chris Lattner78186052006-07-09 00:45:31 +0000959 ++NumParens;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000960 } else if (Tok.is(tok::comma) && NumParens == 0) {
Chris Lattner78186052006-07-09 00:45:31 +0000961 // Comma ends this argument if there are more fixed arguments expected.
962 if (NumFixedArgsLeft)
963 break;
964
Chris Lattner2ada5d32006-07-15 07:51:24 +0000965 // If this is not a variadic macro, too many args were specified.
Chris Lattner78186052006-07-09 00:45:31 +0000966 if (!isVariadic) {
967 // Emit the diagnostic at the macro name in case there is a missing ).
968 // Emitting it at the , could be far away from the macro name.
Chris Lattner2ada5d32006-07-15 07:51:24 +0000969 Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
Chris Lattner78186052006-07-09 00:45:31 +0000970 return 0;
971 }
972 // Otherwise, continue to add the tokens to this variable argument.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000973 } else if (Tok.is(tok::comment) && !KeepMacroComments) {
Chris Lattner457fc152006-07-29 06:30:25 +0000974 // If this is a comment token in the argument list and we're just in
975 // -C mode (not -CC mode), discard the comment.
976 continue;
Chris Lattner9fcdc522007-11-23 06:50:21 +0000977 } else if (Tok.is(tok::identifier)) {
978 // Reading macro arguments can cause macros that we are currently
979 // expanding from to be popped off the expansion stack. Doing so causes
980 // them to be reenabled for expansion. Here we record whether any
981 // identifiers we lex as macro arguments correspond to disabled macros.
982 // If so, we mark the token as noexpand. This is a subtle aspect of
983 // C99 6.10.3.4p2.
984 if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
985 if (!MI->isEnabled())
986 Tok.setFlag(Token::DisableExpand);
Chris Lattner78186052006-07-09 00:45:31 +0000987 }
988
989 ArgTokens.push_back(Tok);
990 }
991
Chris Lattnera12dd152006-07-11 04:09:02 +0000992 // Empty arguments are standard in C99 and supported as an extension in
993 // other modes.
994 if (ArgTokens.empty() && !Features.C99)
995 Diag(Tok, diag::ext_empty_fnmacro_arg);
Chris Lattnerafe603f2006-07-11 04:02:46 +0000996
Chris Lattner36b6e812006-07-21 06:38:30 +0000997 // Add a marker EOF token to the end of the token list for this argument.
Chris Lattner146762e2007-07-20 16:59:19 +0000998 Token EOFTok;
Chris Lattner8c204872006-10-14 05:19:21 +0000999 EOFTok.startToken();
1000 EOFTok.setKind(tok::eof);
1001 EOFTok.setLocation(Tok.getLocation());
1002 EOFTok.setLength(0);
Chris Lattner36b6e812006-07-21 06:38:30 +00001003 ArgTokens.push_back(EOFTok);
1004 ++NumActuals;
Chris Lattner78186052006-07-09 00:45:31 +00001005 --NumFixedArgsLeft;
1006 };
1007
1008 // Okay, we either found the r_paren. Check to see if we parsed too few
1009 // arguments.
Chris Lattner78186052006-07-09 00:45:31 +00001010 unsigned MinArgsExpected = MI->getNumArgs();
1011
Chris Lattner775d8322006-07-29 04:39:41 +00001012 // See MacroArgs instance var for description of this.
1013 bool isVarargsElided = false;
1014
Chris Lattner2ada5d32006-07-15 07:51:24 +00001015 if (NumActuals < MinArgsExpected) {
Chris Lattner78186052006-07-09 00:45:31 +00001016 // There are several cases where too few arguments is ok, handle them now.
Chris Lattner2ada5d32006-07-15 07:51:24 +00001017 if (NumActuals+1 == MinArgsExpected && MI->isVariadic()) {
Chris Lattner78186052006-07-09 00:45:31 +00001018 // Varargs where the named vararg parameter is missing: ok as extension.
1019 // #define A(x, ...)
1020 // A("blah")
1021 Diag(Tok, diag::ext_missing_varargs_arg);
Chris Lattner775d8322006-07-29 04:39:41 +00001022
1023 // Remember this occurred if this is a C99 macro invocation with at least
1024 // one actual argument.
Chris Lattner95a06b32006-07-30 08:40:43 +00001025 isVarargsElided = MI->isC99Varargs() && MI->getNumArgs() > 1;
Chris Lattner78186052006-07-09 00:45:31 +00001026 } else if (MI->getNumArgs() == 1) {
1027 // #define A(x)
1028 // A()
Chris Lattnere7a51302006-07-29 01:25:12 +00001029 // is ok because it is an empty argument.
Chris Lattnera12dd152006-07-11 04:09:02 +00001030
1031 // Empty arguments are standard in C99 and supported as an extension in
1032 // other modes.
1033 if (ArgTokens.empty() && !Features.C99)
1034 Diag(Tok, diag::ext_empty_fnmacro_arg);
Chris Lattner78186052006-07-09 00:45:31 +00001035 } else {
1036 // Otherwise, emit the error.
Chris Lattner2ada5d32006-07-15 07:51:24 +00001037 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
Chris Lattner78186052006-07-09 00:45:31 +00001038 return 0;
1039 }
Chris Lattnere7a51302006-07-29 01:25:12 +00001040
1041 // Add a marker EOF token to the end of the token list for this argument.
1042 SourceLocation EndLoc = Tok.getLocation();
Chris Lattner8c204872006-10-14 05:19:21 +00001043 Tok.startToken();
1044 Tok.setKind(tok::eof);
1045 Tok.setLocation(EndLoc);
1046 Tok.setLength(0);
Chris Lattnere7a51302006-07-29 01:25:12 +00001047 ArgTokens.push_back(Tok);
Chris Lattner78186052006-07-09 00:45:31 +00001048 }
1049
Chris Lattner775d8322006-07-29 04:39:41 +00001050 return MacroArgs::create(MI, &ArgTokens[0], ArgTokens.size(),isVarargsElided);
Chris Lattnerf373a4a2006-06-26 06:16:29 +00001051}
1052
Chris Lattnerc673f902006-06-30 06:10:41 +00001053/// ComputeDATE_TIME - Compute the current time, enter it into the specified
1054/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
1055/// the identifier tokens inserted.
1056static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001057 Preprocessor &PP) {
Chris Lattnerc673f902006-06-30 06:10:41 +00001058 time_t TT = time(0);
1059 struct tm *TM = localtime(&TT);
1060
1061 static const char * const Months[] = {
1062 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
1063 };
1064
1065 char TmpBuffer[100];
1066 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
1067 TM->tm_year+1900);
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001068 DATELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
Chris Lattnerc673f902006-06-30 06:10:41 +00001069
1070 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001071 TIMELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
Chris Lattnerc673f902006-06-30 06:10:41 +00001072}
1073
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001074/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1075/// as a builtin macro, handle it and return the next token as 'Tok'.
Chris Lattner146762e2007-07-20 16:59:19 +00001076void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001077 // Figure out which token this is.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001078 IdentifierInfo *II = Tok.getIdentifierInfo();
1079 assert(II && "Can't be a macro without id info!");
Chris Lattner69772b02006-07-02 20:34:39 +00001080
1081 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
1082 // lex the token after it.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001083 if (II == Ident_Pragma)
Chris Lattner69772b02006-07-02 20:34:39 +00001084 return Handle_Pragma(Tok);
1085
Chris Lattner78186052006-07-09 00:45:31 +00001086 ++NumBuiltinMacroExpanded;
1087
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001088 char TmpBuffer[100];
Chris Lattner69772b02006-07-02 20:34:39 +00001089
1090 // Set up the return result.
Chris Lattner8c204872006-10-14 05:19:21 +00001091 Tok.setIdentifierInfo(0);
Chris Lattner146762e2007-07-20 16:59:19 +00001092 Tok.clearFlag(Token::NeedsCleaning);
Chris Lattner630b33c2006-07-01 22:46:53 +00001093
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001094 if (II == Ident__LINE__) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001095 // __LINE__ expands to a simple numeric value.
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001096 sprintf(TmpBuffer, "%u", SourceMgr.getLogicalLineNumber(Tok.getLocation()));
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001097 unsigned Length = strlen(TmpBuffer);
Chris Lattner8c204872006-10-14 05:19:21 +00001098 Tok.setKind(tok::numeric_constant);
1099 Tok.setLength(Length);
1100 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001101 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +00001102 SourceLocation Loc = Tok.getLocation();
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001103 if (II == Ident__BASE_FILE__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +00001104 Diag(Tok, diag::ext_pp_base_file);
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001105 SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc);
1106 while (NextLoc.isValid()) {
Chris Lattnerc1283b92006-07-01 23:16:30 +00001107 Loc = NextLoc;
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001108 NextLoc = SourceMgr.getIncludeLoc(Loc);
Chris Lattnerc1283b92006-07-01 23:16:30 +00001109 }
1110 }
1111
Chris Lattner0766e592006-07-03 01:07:01 +00001112 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001113 std::string FN = SourceMgr.getSourceName(SourceMgr.getLogicalLoc(Loc));
Chris Lattnerecc39e92006-07-15 05:23:31 +00001114 FN = '"' + Lexer::Stringify(FN) + '"';
Chris Lattner8c204872006-10-14 05:19:21 +00001115 Tok.setKind(tok::string_literal);
1116 Tok.setLength(FN.size());
1117 Tok.setLocation(CreateString(&FN[0], FN.size(), Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001118 } else if (II == Ident__DATE__) {
Chris Lattnerc673f902006-06-30 06:10:41 +00001119 if (!DATELoc.isValid())
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001120 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
Chris Lattner8c204872006-10-14 05:19:21 +00001121 Tok.setKind(tok::string_literal);
1122 Tok.setLength(strlen("\"Mmm dd yyyy\""));
1123 Tok.setLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001124 } else if (II == Ident__TIME__) {
Chris Lattnerc673f902006-06-30 06:10:41 +00001125 if (!TIMELoc.isValid())
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001126 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
Chris Lattner8c204872006-10-14 05:19:21 +00001127 Tok.setKind(tok::string_literal);
1128 Tok.setLength(strlen("\"hh:mm:ss\""));
1129 Tok.setLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001130 } else if (II == Ident__INCLUDE_LEVEL__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +00001131 Diag(Tok, diag::ext_pp_include_level);
1132
1133 // Compute the include depth of this token.
1134 unsigned Depth = 0;
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001135 SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation());
1136 for (; Loc.isValid(); ++Depth)
1137 Loc = SourceMgr.getIncludeLoc(Loc);
Chris Lattnerc1283b92006-07-01 23:16:30 +00001138
1139 // __INCLUDE_LEVEL__ expands to a simple numeric value.
1140 sprintf(TmpBuffer, "%u", Depth);
1141 unsigned Length = strlen(TmpBuffer);
Chris Lattner8c204872006-10-14 05:19:21 +00001142 Tok.setKind(tok::numeric_constant);
1143 Tok.setLength(Length);
1144 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001145 } else if (II == Ident__TIMESTAMP__) {
Chris Lattner847e0e42006-07-01 23:49:16 +00001146 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
1147 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1148 Diag(Tok, diag::ext_pp_timestamp);
1149
1150 // Get the file that we are lexing out of. If we're currently lexing from
1151 // a macro, dig into the include stack.
1152 const FileEntry *CurFile = 0;
Chris Lattnerecfeafe2006-07-02 21:26:45 +00001153 Lexer *TheLexer = getCurrentFileLexer();
Chris Lattner847e0e42006-07-01 23:49:16 +00001154
1155 if (TheLexer)
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001156 CurFile = SourceMgr.getFileEntryForLoc(TheLexer->getFileLoc());
Chris Lattner847e0e42006-07-01 23:49:16 +00001157
1158 // If this file is older than the file it depends on, emit a diagnostic.
1159 const char *Result;
1160 if (CurFile) {
1161 time_t TT = CurFile->getModificationTime();
1162 struct tm *TM = localtime(&TT);
1163 Result = asctime(TM);
1164 } else {
1165 Result = "??? ??? ?? ??:??:?? ????\n";
1166 }
1167 TmpBuffer[0] = '"';
1168 strcpy(TmpBuffer+1, Result);
1169 unsigned Len = strlen(TmpBuffer);
1170 TmpBuffer[Len-1] = '"'; // Replace the newline with a quote.
Chris Lattner8c204872006-10-14 05:19:21 +00001171 Tok.setKind(tok::string_literal);
1172 Tok.setLength(Len);
1173 Tok.setLocation(CreateString(TmpBuffer, Len, Tok.getLocation()));
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001174 } else {
1175 assert(0 && "Unknown identifier!");
Chris Lattner615315f2007-12-09 20:31:55 +00001176 }
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001177}
Chris Lattner677757a2006-06-28 05:26:32 +00001178
1179//===----------------------------------------------------------------------===//
1180// Lexer Event Handling.
1181//===----------------------------------------------------------------------===//
1182
Chris Lattnercefc7682006-07-08 08:28:12 +00001183/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
1184/// identifier information for the token and install it into the token.
Chris Lattner146762e2007-07-20 16:59:19 +00001185IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
Chris Lattnercefc7682006-07-08 08:28:12 +00001186 const char *BufPtr) {
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001187 assert(Identifier.is(tok::identifier) && "Not an identifier!");
Chris Lattnercefc7682006-07-08 08:28:12 +00001188 assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
1189
1190 // Look up this token, see if it is a macro, or if it is a language keyword.
1191 IdentifierInfo *II;
1192 if (BufPtr && !Identifier.needsCleaning()) {
1193 // No cleaning needed, just use the characters from the lexed buffer.
1194 II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
1195 } else {
1196 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
Chris Lattnerf9aba2c2007-07-13 17:10:38 +00001197 llvm::SmallVector<char, 64> IdentifierBuffer;
1198 IdentifierBuffer.resize(Identifier.getLength());
1199 const char *TmpBuf = &IdentifierBuffer[0];
Chris Lattnercefc7682006-07-08 08:28:12 +00001200 unsigned Size = getSpelling(Identifier, TmpBuf);
1201 II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
1202 }
Chris Lattner8c204872006-10-14 05:19:21 +00001203 Identifier.setIdentifierInfo(II);
Chris Lattnercefc7682006-07-08 08:28:12 +00001204 return II;
1205}
1206
1207
Chris Lattner677757a2006-06-28 05:26:32 +00001208/// HandleIdentifier - This callback is invoked when the lexer reads an
1209/// identifier. This callback looks up the identifier in the map and/or
1210/// potentially macro expands it or turns it into a named token (like 'for').
Chris Lattner146762e2007-07-20 16:59:19 +00001211void Preprocessor::HandleIdentifier(Token &Identifier) {
Chris Lattner0f1f5052006-07-20 04:16:23 +00001212 assert(Identifier.getIdentifierInfo() &&
1213 "Can't handle identifiers without identifier info!");
1214
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001215 IdentifierInfo &II = *Identifier.getIdentifierInfo();
Chris Lattner677757a2006-06-28 05:26:32 +00001216
1217 // If this identifier was poisoned, and if it was not produced from a macro
1218 // expansion, emit an error.
Chris Lattner8ff71992006-07-06 05:17:39 +00001219 if (II.isPoisoned() && CurLexer) {
1220 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
1221 Diag(Identifier, diag::err_pp_used_poisoned_id);
1222 else
1223 Diag(Identifier, diag::ext_pp_bad_vaargs_use);
1224 }
Chris Lattner677757a2006-06-28 05:26:32 +00001225
Chris Lattner78186052006-07-09 00:45:31 +00001226 // If this is a macro to be expanded, do it.
Chris Lattnerc43ddc82007-10-07 08:44:20 +00001227 if (MacroInfo *MI = getMacroInfo(&II)) {
Chris Lattner6e4bf522006-07-27 06:59:25 +00001228 if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
1229 if (MI->isEnabled()) {
1230 if (!HandleMacroExpandedIdentifier(Identifier, MI))
1231 return;
1232 } else {
1233 // C99 6.10.3.4p2 says that a disabled macro may never again be
1234 // expanded, even if it's in a context where it could be expanded in the
1235 // future.
Chris Lattner146762e2007-07-20 16:59:19 +00001236 Identifier.setFlag(Token::DisableExpand);
Chris Lattner6e4bf522006-07-27 06:59:25 +00001237 }
1238 }
Chris Lattner063400e2006-10-14 19:54:15 +00001239 } else if (II.isOtherTargetMacro() && !DisableMacroExpansion) {
1240 // If this identifier is a macro on some other target, emit a diagnostic.
1241 // This diagnosic is only emitted when macro expansion is enabled, because
1242 // the macro would not have been expanded for the other target either.
1243 II.setIsOtherTargetMacro(false); // Don't warn on second use.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +00001244 getTargetInfo().DiagnoseNonPortability(getFullLoc(Identifier.getLocation()),
Chris Lattner063400e2006-10-14 19:54:15 +00001245 diag::port_target_macro_use);
1246
1247 }
Chris Lattner677757a2006-06-28 05:26:32 +00001248
Chris Lattner5b9f4892006-11-21 17:23:33 +00001249 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
1250 // then we act as if it is the actual operator and not the textual
1251 // representation of it.
1252 if (II.isCPlusPlusOperatorKeyword())
1253 Identifier.setIdentifierInfo(0);
1254
Chris Lattner677757a2006-06-28 05:26:32 +00001255 // Change the kind of this identifier to the appropriate token kind, e.g.
1256 // turning "for" into a keyword.
Chris Lattner8c204872006-10-14 05:19:21 +00001257 Identifier.setKind(II.getTokenID());
Chris Lattner677757a2006-06-28 05:26:32 +00001258
1259 // If this is an extension token, diagnose its use.
Steve Naroffa8fd9732007-06-11 00:35:03 +00001260 // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99
1261 // For now, I'm just commenting it out (while I work on attributes).
Chris Lattner53621a52007-06-13 20:44:40 +00001262 if (II.isExtensionToken() && Features.C99)
1263 Diag(Identifier, diag::ext_token_used);
Chris Lattner677757a2006-06-28 05:26:32 +00001264}
1265
Chris Lattner22eb9722006-06-18 05:43:12 +00001266/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
1267/// the current file. This either returns the EOF token or pops a level off
1268/// the include stack and keeps going.
Chris Lattner146762e2007-07-20 16:59:19 +00001269bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001270 assert(!CurMacroExpander &&
1271 "Ending a file when currently in a macro!");
1272
Chris Lattner371ac8a2006-07-04 07:11:10 +00001273 // See if this file had a controlling macro.
Chris Lattner3665f162006-07-04 07:26:10 +00001274 if (CurLexer) { // Not ending a macro, ignore it.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001275 if (const IdentifierInfo *ControllingMacro =
Chris Lattner371ac8a2006-07-04 07:11:10 +00001276 CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
Chris Lattner3665f162006-07-04 07:26:10 +00001277 // Okay, this has a controlling macro, remember in PerFileInfo.
1278 if (const FileEntry *FE =
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001279 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001280 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
Chris Lattner371ac8a2006-07-04 07:11:10 +00001281 }
1282 }
1283
Chris Lattner22eb9722006-06-18 05:43:12 +00001284 // If this is a #include'd file, pop it off the include stack and continue
1285 // lexing the #includer file.
Chris Lattner69772b02006-07-02 20:34:39 +00001286 if (!IncludeMacroStack.empty()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001287 // We're done with the #included file.
Chris Lattner7667d0d2006-07-16 18:16:58 +00001288 RemoveTopOfLexerStack();
Chris Lattner0c885f52006-06-21 06:50:18 +00001289
1290 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +00001291 if (Callbacks && !isEndOfMacro && CurLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +00001292 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
1293
1294 // Get the file entry for the current file.
1295 if (const FileEntry *FE =
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001296 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001297 FileType = HeaderInfo.getFileDirFlavor(FE);
Chris Lattnerc8997182006-06-22 05:52:16 +00001298
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +00001299 Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr),
1300 PPCallbacks::ExitFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +00001301 }
Chris Lattner2183a6e2006-07-18 06:36:12 +00001302
1303 // Client should lex another token.
1304 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001305 }
Chris Lattner9046dc12008-01-25 00:00:30 +00001306
1307 // If the file ends with a newline, form the EOF token on the newline itself,
1308 // rather than "on the line following it", which doesn't exist. This makes
1309 // diagnostics relating to the end of file include the last file that the user
1310 // actually typed, which is goodness.
1311 const char *EndPos = CurLexer->BufferEnd;
1312 if (EndPos != CurLexer->BufferStart &&
1313 (EndPos[-1] == '\n' || EndPos[-1] == '\r')) {
1314 --EndPos;
1315
1316 // Handle \n\r and \r\n:
1317 if (EndPos != CurLexer->BufferStart &&
1318 (EndPos[-1] == '\n' || EndPos[-1] == '\r') &&
1319 EndPos[-1] != EndPos[0])
1320 --EndPos;
1321 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001322
Chris Lattner8c204872006-10-14 05:19:21 +00001323 Result.startToken();
Chris Lattner9046dc12008-01-25 00:00:30 +00001324 CurLexer->BufferPtr = EndPos;
1325 CurLexer->FormTokenWithChars(Result, EndPos);
Chris Lattner8c204872006-10-14 05:19:21 +00001326 Result.setKind(tok::eof);
Chris Lattner22eb9722006-06-18 05:43:12 +00001327
1328 // We're done with the #included file.
1329 delete CurLexer;
1330 CurLexer = 0;
Chris Lattner13044d92006-07-03 05:16:44 +00001331
Chris Lattner03f83482006-07-10 06:16:26 +00001332 // This is the end of the top-level file. If the diag::pp_macro_not_used
Chris Lattnerc43ddc82007-10-07 08:44:20 +00001333 // diagnostic is enabled, look for macros that have not been used.
Chris Lattnerb055f2d2007-02-11 08:19:57 +00001334 if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){
Chris Lattnerc43ddc82007-10-07 08:44:20 +00001335 for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
1336 Macros.begin(), E = Macros.end(); I != E; ++I) {
1337 if (!I->second->isUsed())
1338 Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattnerb055f2d2007-02-11 08:19:57 +00001339 }
1340 }
Chris Lattner2183a6e2006-07-18 06:36:12 +00001341 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001342}
1343
1344/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
Chris Lattner7667d0d2006-07-16 18:16:58 +00001345/// the current macro expansion or token stream expansion.
Chris Lattner146762e2007-07-20 16:59:19 +00001346bool Preprocessor::HandleEndOfMacro(Token &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001347 assert(CurMacroExpander && !CurLexer &&
1348 "Ending a macro when currently in a #include file!");
1349
Chris Lattnerc02c4ab2007-07-15 00:25:26 +00001350 // Delete or cache the now-dead macro expander.
1351 if (NumCachedMacroExpanders == MacroExpanderCacheSize)
1352 delete CurMacroExpander;
1353 else
1354 MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander;
Chris Lattner22eb9722006-06-18 05:43:12 +00001355
Chris Lattner69772b02006-07-02 20:34:39 +00001356 // Handle this like a #include file being popped off the stack.
1357 CurMacroExpander = 0;
1358 return HandleEndOfFile(Result, true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001359}
1360
Chris Lattner3b5054d2008-02-07 06:03:59 +00001361/// HandleMicrosoftCommentPaste - When the macro expander pastes together a
1362/// comment (/##/) in microsoft mode, this method handles updating the current
1363/// state, returning the token on the next source line.
1364void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) {
1365 assert(CurMacroExpander && !CurLexer &&
1366 "Pasted comment can only be formed from macro");
1367
1368 // We handle this by scanning for the closest real lexer, switching it to
1369 // raw mode and preprocessor mode. This will cause it to return \n as an
1370 // explicit EOM token.
1371 Lexer *FoundLexer = 0;
1372 bool LexerWasInPPMode = false;
1373 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
1374 IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1);
1375 if (ISI.TheLexer == 0) continue; // Scan for a real lexer.
1376
1377 // Once we find a real lexer, mark it as raw mode (disabling macro
1378 // expansions) and preprocessor mode (return EOM). We know that the lexer
1379 // was *not* in raw mode before, because the macro that the comment came
1380 // from was expanded. However, it could have already been in preprocessor
1381 // mode (#if COMMENT) in which case we have to return it to that mode and
1382 // return EOM.
1383 FoundLexer = ISI.TheLexer;
1384 FoundLexer->LexingRawMode = true;
1385 LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective;
1386 FoundLexer->ParsingPreprocessorDirective = true;
1387 break;
1388 }
1389
1390 // Okay, we either found and switched over the lexer, or we didn't find a
1391 // lexer. In either case, finish off the macro the comment came from, getting
1392 // the next token.
1393 if (!HandleEndOfMacro(Tok)) Lex(Tok);
1394
1395 // Discarding comments as long as we don't have EOF or EOM. This 'comments
1396 // out' the rest of the line, including any tokens that came from other macros
1397 // that were active, as in:
1398 // #define submacro a COMMENT b
1399 // submacro c
1400 // which should lex to 'a' only: 'b' and 'c' should be removed.
1401 while (Tok.isNot(tok::eom) && Tok.isNot(tok::eof))
1402 Lex(Tok);
1403
1404 // If we got an eom token, then we successfully found the end of the line.
1405 if (Tok.is(tok::eom)) {
1406 assert(FoundLexer && "Can't get end of line without an active lexer");
1407 // Restore the lexer back to normal mode instead of raw mode.
1408 FoundLexer->LexingRawMode = false;
1409
1410 // If the lexer was already in preprocessor mode, just return the EOM token
1411 // to finish the preprocessor line.
1412 if (LexerWasInPPMode) return;
1413
1414 // Otherwise, switch out of PP mode and return the next lexed token.
1415 FoundLexer->ParsingPreprocessorDirective = false;
1416 return Lex(Tok);
1417 }
1418
1419 // If we got an EOF token, then we reached the end of the token stream but
1420 // didn't find an explicit \n. This can only happen if there was no lexer
1421 // active (an active lexer would return EOM at EOF if there was no \n in
1422 // preprocessor directive mode), so just return EOF as our token.
1423 assert(!FoundLexer && "Lexer should return EOM before EOF in PP mode");
1424 return;
1425}
Chris Lattner22eb9722006-06-18 05:43:12 +00001426
1427//===----------------------------------------------------------------------===//
1428// Utility Methods for Preprocessor Directive Handling.
1429//===----------------------------------------------------------------------===//
1430
1431/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
1432/// current line until the tok::eom token is found.
Chris Lattnercb283342006-06-18 06:48:37 +00001433void Preprocessor::DiscardUntilEndOfDirective() {
Chris Lattner146762e2007-07-20 16:59:19 +00001434 Token Tmp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001435 do {
Chris Lattnercb283342006-06-18 06:48:37 +00001436 LexUnexpandedToken(Tmp);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001437 } while (Tmp.isNot(tok::eom));
Chris Lattner22eb9722006-06-18 05:43:12 +00001438}
1439
Chris Lattner652c1692006-11-21 23:47:30 +00001440/// isCXXNamedOperator - Returns "true" if the token is a named operator in C++.
1441static bool isCXXNamedOperator(const std::string &Spelling) {
1442 return Spelling == "and" || Spelling == "bitand" || Spelling == "bitor" ||
1443 Spelling == "compl" || Spelling == "not" || Spelling == "not_eq" ||
1444 Spelling == "or" || Spelling == "xor";
1445}
1446
Chris Lattner22eb9722006-06-18 05:43:12 +00001447/// ReadMacroName - Lex and validate a macro name, which occurs after a
1448/// #define or #undef. This sets the token kind to eom and discards the rest
Chris Lattnere8eef322006-07-08 07:01:00 +00001449/// of the macro line if the macro name is invalid. isDefineUndef is 1 if
1450/// this is due to a a #define, 2 if #undef directive, 0 if it is something
Chris Lattner44f8a662006-07-03 01:27:27 +00001451/// else (e.g. #ifdef).
Chris Lattner146762e2007-07-20 16:59:19 +00001452void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001453 // Read the token, don't allow macro expansion on it.
Chris Lattnercb283342006-06-18 06:48:37 +00001454 LexUnexpandedToken(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001455
1456 // Missing macro name?
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001457 if (MacroNameTok.is(tok::eom))
Chris Lattner22eb9722006-06-18 05:43:12 +00001458 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
1459
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001460 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1461 if (II == 0) {
Chris Lattner652c1692006-11-21 23:47:30 +00001462 std::string Spelling = getSpelling(MacroNameTok);
1463 if (isCXXNamedOperator(Spelling))
1464 // C++ 2.5p2: Alternative tokens behave the same as its primary token
1465 // except for their spellings.
1466 Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name, Spelling);
1467 else
1468 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +00001469 // Fall through on error.
Chris Lattner2bb8a952006-11-21 22:24:17 +00001470 } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
Chris Lattner44f8a662006-07-03 01:27:27 +00001471 // Error if defining "defined": C99 6.10.8.4.
Chris Lattneraaf09112006-07-03 01:17:59 +00001472 Diag(MacroNameTok, diag::err_defined_macro_name);
Chris Lattner259716a2007-10-07 08:04:56 +00001473 } else if (isDefineUndef && II->hasMacroDefinition() &&
Chris Lattnerc43ddc82007-10-07 08:44:20 +00001474 getMacroInfo(II)->isBuiltinMacro()) {
Chris Lattner44f8a662006-07-03 01:27:27 +00001475 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
Chris Lattnere8eef322006-07-08 07:01:00 +00001476 if (isDefineUndef == 1)
1477 Diag(MacroNameTok, diag::pp_redef_builtin_macro);
1478 else
1479 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
Chris Lattner22eb9722006-06-18 05:43:12 +00001480 } else {
1481 // Okay, we got a good identifier node. Return it.
Chris Lattnercb283342006-06-18 06:48:37 +00001482 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001483 }
1484
Chris Lattner22eb9722006-06-18 05:43:12 +00001485 // Invalid macro name, read and discard the rest of the line. Then set the
1486 // token kind to tok::eom.
Chris Lattner8c204872006-10-14 05:19:21 +00001487 MacroNameTok.setKind(tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +00001488 return DiscardUntilEndOfDirective();
1489}
1490
1491/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
1492/// not, emit a diagnostic and consume up until the eom.
Chris Lattnercb283342006-06-18 06:48:37 +00001493void Preprocessor::CheckEndOfDirective(const char *DirType) {
Chris Lattner146762e2007-07-20 16:59:19 +00001494 Token Tmp;
Chris Lattnercb283342006-06-18 06:48:37 +00001495 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001496 // There should be no tokens after the directive, but we allow them as an
1497 // extension.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001498 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
Chris Lattnerbcb416b2006-10-27 05:43:50 +00001499 Lex(Tmp);
1500
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001501 if (Tmp.isNot(tok::eom)) {
Chris Lattnercb283342006-06-18 06:48:37 +00001502 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
1503 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001504 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001505}
1506
1507
1508
1509/// SkipExcludedConditionalBlock - We just read a #if or related directive and
1510/// decided that the subsequent tokens are in the #if'd out portion of the
1511/// file. Lex the rest of the file, until we see an #endif. If
1512/// FoundNonSkipPortion is true, then we have already emitted code for part of
1513/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
1514/// is true, then #else directives are ok, if not, then we have already seen one
1515/// so a #else directive is a duplicate. When this returns, the caller can lex
1516/// the first valid token.
Chris Lattnerd01e2912006-06-18 16:22:51 +00001517void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
Chris Lattner22eb9722006-06-18 05:43:12 +00001518 bool FoundNonSkipPortion,
1519 bool FoundElse) {
1520 ++NumSkipped;
Chris Lattner69772b02006-07-02 20:34:39 +00001521 assert(CurMacroExpander == 0 && CurLexer &&
Chris Lattner22eb9722006-06-18 05:43:12 +00001522 "Lexing a macro, not a file?");
1523
1524 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
1525 FoundNonSkipPortion, FoundElse);
1526
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001527 // Enter raw mode to disable identifier lookup (and thus macro expansion),
1528 // disabling warnings, etc.
1529 CurLexer->LexingRawMode = true;
Chris Lattner146762e2007-07-20 16:59:19 +00001530 Token Tok;
Chris Lattner22eb9722006-06-18 05:43:12 +00001531 while (1) {
Chris Lattnercb283342006-06-18 06:48:37 +00001532 CurLexer->Lex(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001533
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001534 // If this is the end of the buffer, we have an error.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001535 if (Tok.is(tok::eof)) {
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001536 // Emit errors for each unterminated conditional on the stack, including
1537 // the current one.
1538 while (!CurLexer->ConditionalStack.empty()) {
1539 Diag(CurLexer->ConditionalStack.back().IfLoc,
1540 diag::err_pp_unterminated_conditional);
1541 CurLexer->ConditionalStack.pop_back();
1542 }
1543
1544 // Just return and let the caller lex after this #include.
1545 break;
1546 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001547
1548 // If this token is not a preprocessor directive, just skip it.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001549 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
Chris Lattner22eb9722006-06-18 05:43:12 +00001550 continue;
1551
1552 // We just parsed a # character at the start of a line, so we're in
1553 // directive mode. Tell the lexer this so any newlines we see will be
1554 // converted into an EOM token (this terminates the macro).
1555 CurLexer->ParsingPreprocessorDirective = true;
Chris Lattner457fc152006-07-29 06:30:25 +00001556 CurLexer->KeepCommentMode = false;
1557
Chris Lattner22eb9722006-06-18 05:43:12 +00001558
1559 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +00001560 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001561
1562 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
1563 // something bogus), skip it.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001564 if (Tok.isNot(tok::identifier)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001565 CurLexer->ParsingPreprocessorDirective = false;
Chris Lattner457fc152006-07-29 06:30:25 +00001566 // Restore comment saving mode.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001567 CurLexer->KeepCommentMode = KeepComments;
Chris Lattner22eb9722006-06-18 05:43:12 +00001568 continue;
1569 }
Chris Lattnere60165f2006-06-22 06:36:29 +00001570
Chris Lattner22eb9722006-06-18 05:43:12 +00001571 // If the first letter isn't i or e, it isn't intesting to us. We know that
1572 // this is safe in the face of spelling differences, because there is no way
1573 // to spell an i/e in a strange way that is another letter. Skipping this
Chris Lattnere60165f2006-06-22 06:36:29 +00001574 // allows us to avoid looking up the identifier info for #define/#undef and
1575 // other common directives.
1576 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
1577 char FirstChar = RawCharData[0];
Chris Lattner22eb9722006-06-18 05:43:12 +00001578 if (FirstChar >= 'a' && FirstChar <= 'z' &&
1579 FirstChar != 'i' && FirstChar != 'e') {
1580 CurLexer->ParsingPreprocessorDirective = false;
Chris Lattner457fc152006-07-29 06:30:25 +00001581 // Restore comment saving mode.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001582 CurLexer->KeepCommentMode = KeepComments;
Chris Lattner22eb9722006-06-18 05:43:12 +00001583 continue;
1584 }
1585
Chris Lattnere60165f2006-06-22 06:36:29 +00001586 // Get the identifier name without trigraphs or embedded newlines. Note
1587 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
1588 // when skipping.
1589 // TODO: could do this with zero copies in the no-clean case by using
1590 // strncmp below.
1591 char Directive[20];
1592 unsigned IdLen;
1593 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
1594 IdLen = Tok.getLength();
1595 memcpy(Directive, RawCharData, IdLen);
1596 Directive[IdLen] = 0;
1597 } else {
1598 std::string DirectiveStr = getSpelling(Tok);
1599 IdLen = DirectiveStr.size();
1600 if (IdLen >= 20) {
1601 CurLexer->ParsingPreprocessorDirective = false;
Chris Lattner457fc152006-07-29 06:30:25 +00001602 // Restore comment saving mode.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001603 CurLexer->KeepCommentMode = KeepComments;
Chris Lattnere60165f2006-06-22 06:36:29 +00001604 continue;
1605 }
1606 memcpy(Directive, &DirectiveStr[0], IdLen);
1607 Directive[IdLen] = 0;
1608 }
1609
Chris Lattner22eb9722006-06-18 05:43:12 +00001610 if (FirstChar == 'i' && Directive[1] == 'f') {
Chris Lattnere60165f2006-06-22 06:36:29 +00001611 if ((IdLen == 2) || // "if"
1612 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
1613 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
Chris Lattner22eb9722006-06-18 05:43:12 +00001614 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
1615 // bother parsing the condition.
Chris Lattnercb283342006-06-18 06:48:37 +00001616 DiscardUntilEndOfDirective();
Chris Lattner50b497e2006-06-18 16:32:35 +00001617 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerd01e2912006-06-18 16:22:51 +00001618 /*foundnonskip*/false,
1619 /*fnddelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001620 }
1621 } else if (FirstChar == 'e') {
Chris Lattnere60165f2006-06-22 06:36:29 +00001622 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
Chris Lattnercb283342006-06-18 06:48:37 +00001623 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00001624 PPConditionalInfo CondInfo;
1625 CondInfo.WasSkipping = true; // Silence bogus warning.
1626 bool InCond = CurLexer->popConditionalLevel(CondInfo);
Chris Lattnercf6bc662006-11-05 07:59:08 +00001627 InCond = InCond; // Silence warning in no-asserts mode.
Chris Lattner22eb9722006-06-18 05:43:12 +00001628 assert(!InCond && "Can't be skipping if not in a conditional!");
1629
1630 // If we popped the outermost skipping block, we're done skipping!
1631 if (!CondInfo.WasSkipping)
1632 break;
Chris Lattnere60165f2006-06-22 06:36:29 +00001633 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
Chris Lattner22eb9722006-06-18 05:43:12 +00001634 // #else directive in a skipping conditional. If not in some other
1635 // skipping conditional, and if #else hasn't already been seen, enter it
1636 // as a non-skipping conditional.
Chris Lattnercb283342006-06-18 06:48:37 +00001637 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00001638 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1639
1640 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001641 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001642
1643 // Note that we've seen a #else in this conditional.
1644 CondInfo.FoundElse = true;
1645
1646 // If the conditional is at the top level, and the #if block wasn't
1647 // entered, enter the #else block now.
1648 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
1649 CondInfo.FoundNonSkip = true;
1650 break;
1651 }
Chris Lattnere60165f2006-06-22 06:36:29 +00001652 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
Chris Lattner22eb9722006-06-18 05:43:12 +00001653 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1654
1655 bool ShouldEnter;
1656 // If this is in a skipping block or if we're already handled this #if
1657 // block, don't bother parsing the condition.
1658 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
Chris Lattnercb283342006-06-18 06:48:37 +00001659 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001660 ShouldEnter = false;
1661 } else {
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001662 // Restore the value of LexingRawMode so that identifiers are
Chris Lattner22eb9722006-06-18 05:43:12 +00001663 // looked up, etc, inside the #elif expression.
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001664 assert(CurLexer->LexingRawMode && "We have to be skipping here!");
1665 CurLexer->LexingRawMode = false;
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001666 IdentifierInfo *IfNDefMacro = 0;
Chris Lattnera8654ca2006-07-04 17:42:08 +00001667 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001668 CurLexer->LexingRawMode = true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001669 }
1670
1671 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001672 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001673
1674 // If this condition is true, enter it!
1675 if (ShouldEnter) {
1676 CondInfo.FoundNonSkip = true;
1677 break;
1678 }
1679 }
1680 }
1681
1682 CurLexer->ParsingPreprocessorDirective = false;
Chris Lattner457fc152006-07-29 06:30:25 +00001683 // Restore comment saving mode.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001684 CurLexer->KeepCommentMode = KeepComments;
Chris Lattner22eb9722006-06-18 05:43:12 +00001685 }
1686
1687 // Finally, if we are out of the conditional (saw an #endif or ran off the end
1688 // of the file, just stop skipping and return to lexing whatever came after
1689 // the #if block.
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001690 CurLexer->LexingRawMode = false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001691}
1692
1693//===----------------------------------------------------------------------===//
1694// Preprocessor Directive Handling.
1695//===----------------------------------------------------------------------===//
1696
1697/// HandleDirective - This callback is invoked when the lexer sees a # token
1698/// at the start of a line. This consumes the directive, modifies the
1699/// lexer/preprocessor state, and advances the lexer(s) so that the next token
1700/// read is the correct one.
Chris Lattner146762e2007-07-20 16:59:19 +00001701void Preprocessor::HandleDirective(Token &Result) {
Chris Lattner4d5e1a72006-07-03 01:01:29 +00001702 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
Chris Lattner22eb9722006-06-18 05:43:12 +00001703
1704 // We just parsed a # character at the start of a line, so we're in directive
1705 // mode. Tell the lexer this so any newlines we see will be converted into an
Chris Lattner78186052006-07-09 00:45:31 +00001706 // EOM token (which terminates the directive).
Chris Lattner22eb9722006-06-18 05:43:12 +00001707 CurLexer->ParsingPreprocessorDirective = true;
1708
1709 ++NumDirectives;
1710
Chris Lattner371ac8a2006-07-04 07:11:10 +00001711 // We are about to read a token. For the multiple-include optimization FA to
1712 // work, we have to remember if we had read any tokens *before* this
1713 // pp-directive.
1714 bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal();
1715
Chris Lattner78186052006-07-09 00:45:31 +00001716 // Read the next token, the directive flavor. This isn't expanded due to
1717 // C99 6.10.3p8.
Chris Lattnercb283342006-06-18 06:48:37 +00001718 LexUnexpandedToken(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001719
Chris Lattner78186052006-07-09 00:45:31 +00001720 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
1721 // #define A(x) #x
1722 // A(abc
1723 // #warning blah
1724 // def)
1725 // If so, the user is relying on non-portable behavior, emit a diagnostic.
Chris Lattneree8760b2006-07-15 07:42:55 +00001726 if (InMacroArgs)
Chris Lattner78186052006-07-09 00:45:31 +00001727 Diag(Result, diag::ext_embedded_directive);
1728
Chris Lattnerbcb416b2006-10-27 05:43:50 +00001729TryAgain:
Chris Lattner22eb9722006-06-18 05:43:12 +00001730 switch (Result.getKind()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001731 case tok::eom:
Chris Lattnercb283342006-06-18 06:48:37 +00001732 return; // null directive.
Chris Lattnerbcb416b2006-10-27 05:43:50 +00001733 case tok::comment:
1734 // Handle stuff like "# /*foo*/ define X" in -E -C mode.
1735 LexUnexpandedToken(Result);
1736 goto TryAgain;
Chris Lattner22eb9722006-06-18 05:43:12 +00001737
Chris Lattner22eb9722006-06-18 05:43:12 +00001738 case tok::numeric_constant:
1739 // FIXME: implement # 7 line numbers!
Chris Lattner6e5b2a02006-10-17 02:53:32 +00001740 DiscardUntilEndOfDirective();
1741 return;
Chris Lattner87d3bec2006-10-17 03:44:32 +00001742 default:
1743 IdentifierInfo *II = Result.getIdentifierInfo();
1744 if (II == 0) break; // Not an identifier.
1745
1746 // Ask what the preprocessor keyword ID is.
1747 switch (II->getPPKeywordID()) {
1748 default: break;
1749 // C99 6.10.1 - Conditional Inclusion.
1750 case tok::pp_if:
1751 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
1752 case tok::pp_ifdef:
1753 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
1754 case tok::pp_ifndef:
1755 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
1756 case tok::pp_elif:
1757 return HandleElifDirective(Result);
1758 case tok::pp_else:
1759 return HandleElseDirective(Result);
1760 case tok::pp_endif:
1761 return HandleEndifDirective(Result);
1762
1763 // C99 6.10.2 - Source File Inclusion.
1764 case tok::pp_include:
1765 return HandleIncludeDirective(Result); // Handle #include.
1766
1767 // C99 6.10.3 - Macro Replacement.
1768 case tok::pp_define:
1769 return HandleDefineDirective(Result, false);
1770 case tok::pp_undef:
1771 return HandleUndefDirective(Result);
1772
1773 // C99 6.10.4 - Line Control.
1774 case tok::pp_line:
1775 // FIXME: implement #line
1776 DiscardUntilEndOfDirective();
1777 return;
1778
1779 // C99 6.10.5 - Error Directive.
1780 case tok::pp_error:
1781 return HandleUserDiagnosticDirective(Result, false);
1782
1783 // C99 6.10.6 - Pragma Directive.
1784 case tok::pp_pragma:
1785 return HandlePragmaDirective();
1786
1787 // GNU Extensions.
1788 case tok::pp_import:
1789 return HandleImportDirective(Result);
1790 case tok::pp_include_next:
1791 return HandleIncludeNextDirective(Result);
1792
1793 case tok::pp_warning:
1794 Diag(Result, diag::ext_pp_warning_directive);
1795 return HandleUserDiagnosticDirective(Result, true);
1796 case tok::pp_ident:
1797 return HandleIdentSCCSDirective(Result);
1798 case tok::pp_sccs:
1799 return HandleIdentSCCSDirective(Result);
1800 case tok::pp_assert:
1801 //isExtension = true; // FIXME: implement #assert
Chris Lattner22eb9722006-06-18 05:43:12 +00001802 break;
Chris Lattner87d3bec2006-10-17 03:44:32 +00001803 case tok::pp_unassert:
1804 //isExtension = true; // FIXME: implement #unassert
Chris Lattner22eb9722006-06-18 05:43:12 +00001805 break;
Chris Lattner87d3bec2006-10-17 03:44:32 +00001806
1807 // clang extensions.
1808 case tok::pp_define_target:
1809 return HandleDefineDirective(Result, true);
1810 case tok::pp_define_other_target:
1811 return HandleDefineOtherTargetDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001812 }
1813 break;
1814 }
1815
1816 // If we reached here, the preprocessing token is not valid!
Chris Lattnercb283342006-06-18 06:48:37 +00001817 Diag(Result, diag::err_pp_invalid_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001818
1819 // Read the rest of the PP line.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001820 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001821
1822 // Okay, we're done parsing the directive.
Chris Lattner22eb9722006-06-18 05:43:12 +00001823}
1824
Chris Lattner146762e2007-07-20 16:59:19 +00001825void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001826 bool isWarning) {
1827 // Read the rest of the line raw. We do this because we don't want macros
1828 // to be expanded and we don't require that the tokens be valid preprocessing
1829 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1830 // collapse multiple consequtive white space between tokens, but this isn't
1831 // specified by the standard.
1832 std::string Message = CurLexer->ReadToEndOfLine();
1833
1834 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
Chris Lattner01d66cc2006-07-03 22:16:27 +00001835 return Diag(Tok, DiagID, Message);
1836}
1837
1838/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1839///
Chris Lattner146762e2007-07-20 16:59:19 +00001840void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001841 // Yes, this directive is an extension.
Chris Lattner01d66cc2006-07-03 22:16:27 +00001842 Diag(Tok, diag::ext_pp_ident_directive);
1843
Chris Lattner371ac8a2006-07-04 07:11:10 +00001844 // Read the string argument.
Chris Lattner146762e2007-07-20 16:59:19 +00001845 Token StrTok;
Chris Lattner01d66cc2006-07-03 22:16:27 +00001846 Lex(StrTok);
1847
1848 // If the token kind isn't a string, it's a malformed directive.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001849 if (StrTok.isNot(tok::string_literal) &&
1850 StrTok.isNot(tok::wide_string_literal))
Chris Lattner01d66cc2006-07-03 22:16:27 +00001851 return Diag(StrTok, diag::err_pp_malformed_ident);
1852
1853 // Verify that there is nothing after the string, other than EOM.
1854 CheckEndOfDirective("#ident");
1855
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +00001856 if (Callbacks)
1857 Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok));
Chris Lattner22eb9722006-06-18 05:43:12 +00001858}
1859
Chris Lattnerb8761832006-06-24 21:31:03 +00001860//===----------------------------------------------------------------------===//
1861// Preprocessor Include Directive Handling.
1862//===----------------------------------------------------------------------===//
1863
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001864/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1865/// checked and spelled filename, e.g. as an operand of #include. This returns
1866/// true if the input filename was in <>'s or false if it were in ""'s. The
1867/// caller is expected to provide a buffer that is large enough to hold the
1868/// spelling of the filename, but is also expected to handle the case when
1869/// this method decides to use a different buffer.
Chris Lattner93ab9f12007-07-23 04:15:27 +00001870bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001871 const char *&BufStart,
1872 const char *&BufEnd) {
1873 // Get the text form of the filename.
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001874 assert(BufStart != BufEnd && "Can't have tokens with empty spellings!");
1875
1876 // Make sure the filename is <x> or "x".
1877 bool isAngled;
1878 if (BufStart[0] == '<') {
1879 if (BufEnd[-1] != '>') {
Chris Lattner93ab9f12007-07-23 04:15:27 +00001880 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001881 BufStart = 0;
1882 return true;
1883 }
1884 isAngled = true;
1885 } else if (BufStart[0] == '"') {
1886 if (BufEnd[-1] != '"') {
Chris Lattner93ab9f12007-07-23 04:15:27 +00001887 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001888 BufStart = 0;
1889 return true;
1890 }
1891 isAngled = false;
1892 } else {
Chris Lattner93ab9f12007-07-23 04:15:27 +00001893 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001894 BufStart = 0;
1895 return true;
1896 }
1897
1898 // Diagnose #include "" as invalid.
1899 if (BufEnd-BufStart <= 2) {
Chris Lattner93ab9f12007-07-23 04:15:27 +00001900 Diag(Loc, diag::err_pp_empty_filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001901 BufStart = 0;
1902 return "";
1903 }
1904
1905 // Skip the brackets.
1906 ++BufStart;
1907 --BufEnd;
1908 return isAngled;
1909}
1910
Chris Lattner43eafb42007-07-23 04:56:47 +00001911/// ConcatenateIncludeName - Handle cases where the #include name is expanded
1912/// from a macro as multiple tokens, which need to be glued together. This
1913/// occurs for code like:
1914/// #define FOO <a/b.h>
1915/// #include FOO
1916/// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1917///
1918/// This code concatenates and consumes tokens up to the '>' token. It returns
1919/// false if the > was found, otherwise it returns true if it finds and consumes
1920/// the EOM marker.
1921static bool ConcatenateIncludeName(llvm::SmallVector<char, 128> &FilenameBuffer,
1922 Preprocessor &PP) {
1923 Token CurTok;
1924
1925 PP.Lex(CurTok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001926 while (CurTok.isNot(tok::eom)) {
Chris Lattner43eafb42007-07-23 04:56:47 +00001927 // Append the spelling of this token to the buffer. If there was a space
1928 // before it, add it now.
1929 if (CurTok.hasLeadingSpace())
1930 FilenameBuffer.push_back(' ');
1931
1932 // Get the spelling of the token, directly into FilenameBuffer if possible.
1933 unsigned PreAppendSize = FilenameBuffer.size();
1934 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1935
1936 const char *BufPtr = &FilenameBuffer[PreAppendSize];
1937 unsigned ActualLen = PP.getSpelling(CurTok, BufPtr);
1938
1939 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1940 if (BufPtr != &FilenameBuffer[PreAppendSize])
1941 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1942
1943 // Resize FilenameBuffer to the correct size.
1944 if (CurTok.getLength() != ActualLen)
1945 FilenameBuffer.resize(PreAppendSize+ActualLen);
1946
1947 // If we found the '>' marker, return success.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001948 if (CurTok.is(tok::greater))
Chris Lattner43eafb42007-07-23 04:56:47 +00001949 return false;
1950
1951 PP.Lex(CurTok);
1952 }
1953
1954 // If we hit the eom marker, emit an error and return true so that the caller
1955 // knows the EOM has been read.
1956 PP.Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1957 return true;
1958}
1959
Chris Lattner22eb9722006-06-18 05:43:12 +00001960/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1961/// file to be included from the lexer, then include it! This is a common
1962/// routine with functionality shared between #include, #include_next and
1963/// #import.
Chris Lattner146762e2007-07-20 16:59:19 +00001964void Preprocessor::HandleIncludeDirective(Token &IncludeTok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001965 const DirectoryLookup *LookupFrom,
1966 bool isImport) {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001967
Chris Lattner146762e2007-07-20 16:59:19 +00001968 Token FilenameTok;
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001969 CurLexer->LexIncludeFilename(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001970
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001971 // Reserve a buffer to get the spelling.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001972 llvm::SmallVector<char, 128> FilenameBuffer;
Chris Lattner43eafb42007-07-23 04:56:47 +00001973 const char *FilenameStart, *FilenameEnd;
1974
1975 switch (FilenameTok.getKind()) {
1976 case tok::eom:
1977 // If the token kind is EOM, the error has already been diagnosed.
1978 return;
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001979
Chris Lattner43eafb42007-07-23 04:56:47 +00001980 case tok::angle_string_literal:
Chris Lattnerf97dbcb2007-07-23 22:23:52 +00001981 case tok::string_literal: {
Chris Lattner43eafb42007-07-23 04:56:47 +00001982 FilenameBuffer.resize(FilenameTok.getLength());
1983 FilenameStart = &FilenameBuffer[0];
1984 unsigned Len = getSpelling(FilenameTok, FilenameStart);
1985 FilenameEnd = FilenameStart+Len;
1986 break;
Chris Lattnerf97dbcb2007-07-23 22:23:52 +00001987 }
Chris Lattner43eafb42007-07-23 04:56:47 +00001988
1989 case tok::less:
1990 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1991 // case, glue the tokens together into FilenameBuffer and interpret those.
1992 FilenameBuffer.push_back('<');
1993 if (ConcatenateIncludeName(FilenameBuffer, *this))
1994 return; // Found <eom> but no ">"? Diagnostic already emitted.
1995 FilenameStart = &FilenameBuffer[0];
1996 FilenameEnd = &FilenameBuffer[FilenameBuffer.size()];
1997 break;
1998 default:
1999 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
2000 DiscardUntilEndOfDirective();
2001 return;
2002 }
2003
Chris Lattner93ab9f12007-07-23 04:15:27 +00002004 bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(),
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00002005 FilenameStart, FilenameEnd);
2006 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
2007 // error.
Chris Lattner43eafb42007-07-23 04:56:47 +00002008 if (FilenameStart == 0) {
2009 DiscardUntilEndOfDirective();
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00002010 return;
Chris Lattner43eafb42007-07-23 04:56:47 +00002011 }
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00002012
Chris Lattner269c2322006-06-25 06:23:00 +00002013 // Verify that there is nothing after the filename, other than EOM. Use the
2014 // preprocessor to lex this in case lexing the filename entered a macro.
2015 CheckEndOfDirective("#include");
Chris Lattner22eb9722006-06-18 05:43:12 +00002016
2017 // Check that we don't have infinite #include recursion.
Chris Lattner69772b02006-07-02 20:34:39 +00002018 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
Chris Lattner22eb9722006-06-18 05:43:12 +00002019 return Diag(FilenameTok, diag::err_pp_include_too_deep);
2020
Chris Lattner22eb9722006-06-18 05:43:12 +00002021 // Search include directories.
Chris Lattnerc8997182006-06-22 05:52:16 +00002022 const DirectoryLookup *CurDir;
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00002023 const FileEntry *File = LookupFile(FilenameStart, FilenameEnd,
Chris Lattnerb8b94f12006-10-30 05:38:06 +00002024 isAngled, LookupFrom, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00002025 if (File == 0)
Chris Lattner7c718bd2007-04-10 06:02:46 +00002026 return Diag(FilenameTok, diag::err_pp_file_not_found,
2027 std::string(FilenameStart, FilenameEnd));
Chris Lattner22eb9722006-06-18 05:43:12 +00002028
Chris Lattner59a9ebd2006-10-18 05:34:33 +00002029 // Ask HeaderInfo if we should enter this #include file.
2030 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
2031 // If it returns true, #including this file will have no effect.
Chris Lattner3665f162006-07-04 07:26:10 +00002032 return;
2033 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002034
2035 // Look up the file, create a File ID for it.
Chris Lattner371ac8a2006-07-04 07:11:10 +00002036 unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00002037 if (FileID == 0)
Chris Lattner7c718bd2007-04-10 06:02:46 +00002038 return Diag(FilenameTok, diag::err_pp_file_not_found,
2039 std::string(FilenameStart, FilenameEnd));
Chris Lattner22eb9722006-06-18 05:43:12 +00002040
2041 // Finally, if all is good, enter the new file!
Chris Lattnerc8997182006-06-22 05:52:16 +00002042 EnterSourceFile(FileID, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00002043}
2044
2045/// HandleIncludeNextDirective - Implements #include_next.
2046///
Chris Lattner146762e2007-07-20 16:59:19 +00002047void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) {
Chris Lattnercb283342006-06-18 06:48:37 +00002048 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00002049
2050 // #include_next is like #include, except that we start searching after
2051 // the current found directory. If we can't do this, issue a
2052 // diagnostic.
Chris Lattnerc8997182006-06-22 05:52:16 +00002053 const DirectoryLookup *Lookup = CurDirLookup;
Chris Lattner69772b02006-07-02 20:34:39 +00002054 if (isInPrimaryFile()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002055 Lookup = 0;
Chris Lattnercb283342006-06-18 06:48:37 +00002056 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
Chris Lattner22eb9722006-06-18 05:43:12 +00002057 } else if (Lookup == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +00002058 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
Chris Lattnerc8997182006-06-22 05:52:16 +00002059 } else {
2060 // Start looking up in the next directory.
2061 ++Lookup;
Chris Lattner22eb9722006-06-18 05:43:12 +00002062 }
2063
2064 return HandleIncludeDirective(IncludeNextTok, Lookup);
2065}
2066
2067/// HandleImportDirective - Implements #import.
2068///
Chris Lattner146762e2007-07-20 16:59:19 +00002069void Preprocessor::HandleImportDirective(Token &ImportTok) {
Chris Lattnercb283342006-06-18 06:48:37 +00002070 Diag(ImportTok, diag::ext_pp_import_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00002071
2072 return HandleIncludeDirective(ImportTok, 0, true);
2073}
2074
Chris Lattnerb8761832006-06-24 21:31:03 +00002075//===----------------------------------------------------------------------===//
2076// Preprocessor Macro Directive Handling.
2077//===----------------------------------------------------------------------===//
2078
Chris Lattnercefc7682006-07-08 08:28:12 +00002079/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
2080/// definition has just been read. Lex the rest of the arguments and the
2081/// closing ), updating MI with what we learn. Return true if an error occurs
2082/// parsing the arg list.
2083bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
Chris Lattner564f4782007-07-14 22:46:43 +00002084 llvm::SmallVector<IdentifierInfo*, 32> Arguments;
2085
Chris Lattner146762e2007-07-20 16:59:19 +00002086 Token Tok;
Chris Lattnercefc7682006-07-08 08:28:12 +00002087 while (1) {
2088 LexUnexpandedToken(Tok);
2089 switch (Tok.getKind()) {
2090 case tok::r_paren:
2091 // Found the end of the argument list.
Chris Lattner564f4782007-07-14 22:46:43 +00002092 if (Arguments.empty()) { // #define FOO()
2093 MI->setArgumentList(Arguments.begin(), Arguments.end());
2094 return false;
2095 }
Chris Lattnercefc7682006-07-08 08:28:12 +00002096 // Otherwise we have #define FOO(A,)
2097 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
2098 return true;
2099 case tok::ellipsis: // #define X(... -> C99 varargs
2100 // Warn if use of C99 feature in non-C99 mode.
2101 if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
2102
2103 // Lex the token after the identifier.
2104 LexUnexpandedToken(Tok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002105 if (Tok.isNot(tok::r_paren)) {
Chris Lattnercefc7682006-07-08 08:28:12 +00002106 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2107 return true;
2108 }
Chris Lattner95a06b32006-07-30 08:40:43 +00002109 // Add the __VA_ARGS__ identifier as an argument.
Chris Lattner564f4782007-07-14 22:46:43 +00002110 Arguments.push_back(Ident__VA_ARGS__);
Chris Lattnercefc7682006-07-08 08:28:12 +00002111 MI->setIsC99Varargs();
Chris Lattner564f4782007-07-14 22:46:43 +00002112 MI->setArgumentList(Arguments.begin(), Arguments.end());
Chris Lattnercefc7682006-07-08 08:28:12 +00002113 return false;
2114 case tok::eom: // #define X(
2115 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2116 return true;
Chris Lattner62aa0d42006-10-20 05:08:24 +00002117 default:
2118 // Handle keywords and identifiers here to accept things like
2119 // #define Foo(for) for.
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002120 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner62aa0d42006-10-20 05:08:24 +00002121 if (II == 0) {
2122 // #define X(1
2123 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
2124 return true;
2125 }
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002126
2127 // If this is already used as an argument, it is used multiple times (e.g.
2128 // #define X(A,A.
Chris Lattner564f4782007-07-14 22:46:43 +00002129 if (std::find(Arguments.begin(), Arguments.end(), II) !=
2130 Arguments.end()) { // C99 6.10.3p6
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002131 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName());
2132 return true;
2133 }
2134
2135 // Add the argument to the macro info.
Chris Lattner564f4782007-07-14 22:46:43 +00002136 Arguments.push_back(II);
Chris Lattnercefc7682006-07-08 08:28:12 +00002137
2138 // Lex the token after the identifier.
2139 LexUnexpandedToken(Tok);
2140
2141 switch (Tok.getKind()) {
2142 default: // #define X(A B
2143 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
2144 return true;
2145 case tok::r_paren: // #define X(A)
Chris Lattner564f4782007-07-14 22:46:43 +00002146 MI->setArgumentList(Arguments.begin(), Arguments.end());
Chris Lattnercefc7682006-07-08 08:28:12 +00002147 return false;
2148 case tok::comma: // #define X(A,
2149 break;
2150 case tok::ellipsis: // #define X(A... -> GCC extension
2151 // Diagnose extension.
2152 Diag(Tok, diag::ext_named_variadic_macro);
2153
2154 // Lex the token after the identifier.
2155 LexUnexpandedToken(Tok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002156 if (Tok.isNot(tok::r_paren)) {
Chris Lattnercefc7682006-07-08 08:28:12 +00002157 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2158 return true;
2159 }
2160
2161 MI->setIsGNUVarargs();
Chris Lattner564f4782007-07-14 22:46:43 +00002162 MI->setArgumentList(Arguments.begin(), Arguments.end());
Chris Lattnercefc7682006-07-08 08:28:12 +00002163 return false;
2164 }
2165 }
2166 }
2167}
2168
Chris Lattner22eb9722006-06-18 05:43:12 +00002169/// HandleDefineDirective - Implements #define. This consumes the entire macro
Chris Lattner81278c62006-10-14 19:03:49 +00002170/// line then lets the caller lex the next real token. If 'isTargetSpecific' is
2171/// true, then this is a "#define_target", otherwise this is a "#define".
Chris Lattner22eb9722006-06-18 05:43:12 +00002172///
Chris Lattner146762e2007-07-20 16:59:19 +00002173void Preprocessor::HandleDefineDirective(Token &DefineTok,
Chris Lattner81278c62006-10-14 19:03:49 +00002174 bool isTargetSpecific) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002175 ++NumDefined;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002176
Chris Lattner146762e2007-07-20 16:59:19 +00002177 Token MacroNameTok;
Chris Lattnere8eef322006-07-08 07:01:00 +00002178 ReadMacroName(MacroNameTok, 1);
Chris Lattner22eb9722006-06-18 05:43:12 +00002179
2180 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002181 if (MacroNameTok.is(tok::eom))
Chris Lattnercb283342006-06-18 06:48:37 +00002182 return;
Chris Lattnerf40fe992007-07-14 22:11:41 +00002183
Chris Lattner457fc152006-07-29 06:30:25 +00002184 // If we are supposed to keep comments in #defines, reenable comment saving
2185 // mode.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00002186 CurLexer->KeepCommentMode = KeepMacroComments;
Chris Lattner457fc152006-07-29 06:30:25 +00002187
Chris Lattner063400e2006-10-14 19:54:15 +00002188 // Create the new macro.
Chris Lattner50b497e2006-06-18 16:32:35 +00002189 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
Chris Lattner81278c62006-10-14 19:03:49 +00002190 if (isTargetSpecific) MI->setIsTargetSpecific();
Chris Lattner22eb9722006-06-18 05:43:12 +00002191
Chris Lattner063400e2006-10-14 19:54:15 +00002192 // If the identifier is an 'other target' macro, clear this bit.
2193 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
2194
2195
Chris Lattner146762e2007-07-20 16:59:19 +00002196 Token Tok;
Chris Lattnercb283342006-06-18 06:48:37 +00002197 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00002198
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002199 // If this is a function-like macro definition, parse the argument list,
2200 // marking each of the identifiers as being used as macro arguments. Also,
2201 // check other constraints on the first token of the macro body.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002202 if (Tok.is(tok::eom)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002203 // If there is no body to this macro, we have no special handling here.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002204 } else if (Tok.is(tok::l_paren) && !Tok.hasLeadingSpace()) {
Chris Lattnercefc7682006-07-08 08:28:12 +00002205 // This is a function-like macro definition. Read the argument list.
2206 MI->setIsFunctionLike();
2207 if (ReadMacroDefinitionArgList(MI)) {
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002208 // Forget about MI.
Chris Lattnercefc7682006-07-08 08:28:12 +00002209 delete MI;
Chris Lattner6e0d42c2006-07-08 20:32:52 +00002210 // Throw away the rest of the line.
Chris Lattnercefc7682006-07-08 08:28:12 +00002211 if (CurLexer->ParsingPreprocessorDirective)
2212 DiscardUntilEndOfDirective();
2213 return;
2214 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002215
Chris Lattner815a1f92006-07-08 20:48:04 +00002216 // Read the first token after the arg list for down below.
2217 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00002218 } else if (!Tok.hasLeadingSpace()) {
2219 // C99 requires whitespace between the macro definition and the body. Emit
2220 // a diagnostic for something like "#define X+".
2221 if (Features.C99) {
Chris Lattnercb283342006-06-18 06:48:37 +00002222 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattner22eb9722006-06-18 05:43:12 +00002223 } else {
2224 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
2225 // one in some cases!
2226 }
2227 } else {
2228 // This is a normal token with leading space. Clear the leading space
2229 // marker on the first token to get proper expansion.
Chris Lattner146762e2007-07-20 16:59:19 +00002230 Tok.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00002231 }
2232
Chris Lattner7e374832006-07-29 03:46:57 +00002233 // If this is a definition of a variadic C99 function-like macro, not using
2234 // the GNU named varargs extension, enabled __VA_ARGS__.
2235
2236 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
2237 // This gets unpoisoned where it is allowed.
2238 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
2239 if (MI->isC99Varargs())
2240 Ident__VA_ARGS__->setIsPoisoned(false);
2241
Chris Lattner22eb9722006-06-18 05:43:12 +00002242 // Read the rest of the macro body.
Chris Lattnera3834342007-07-14 21:54:03 +00002243 if (MI->isObjectLike()) {
2244 // Object-like macros are very simple, just read their body.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002245 while (Tok.isNot(tok::eom)) {
Chris Lattnera3834342007-07-14 21:54:03 +00002246 MI->AddTokenToBody(Tok);
Chris Lattner815a1f92006-07-08 20:48:04 +00002247 // Get the next token of the macro.
2248 LexUnexpandedToken(Tok);
Chris Lattner815a1f92006-07-08 20:48:04 +00002249 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002250
Chris Lattnera3834342007-07-14 21:54:03 +00002251 } else {
2252 // Otherwise, read the body of a function-like macro. This has to validate
2253 // the # (stringize) operator.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002254 while (Tok.isNot(tok::eom)) {
Chris Lattnera3834342007-07-14 21:54:03 +00002255 MI->AddTokenToBody(Tok);
Chris Lattnerbff18d52006-07-06 04:49:18 +00002256
Chris Lattnera3834342007-07-14 21:54:03 +00002257 // Check C99 6.10.3.2p1: ensure that # operators are followed by macro
2258 // parameters in function-like macro expansions.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002259 if (Tok.isNot(tok::hash)) {
Chris Lattnera3834342007-07-14 21:54:03 +00002260 // Get the next token of the macro.
2261 LexUnexpandedToken(Tok);
2262 continue;
2263 }
2264
2265 // Get the next token of the macro.
2266 LexUnexpandedToken(Tok);
2267
2268 // Not a macro arg identifier?
2269 if (!Tok.getIdentifierInfo() ||
2270 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
2271 Diag(Tok, diag::err_pp_stringize_not_parameter);
2272 delete MI;
2273
2274 // Disable __VA_ARGS__ again.
2275 Ident__VA_ARGS__->setIsPoisoned(true);
2276 return;
2277 }
2278
2279 // Things look ok, add the param name token to the macro.
2280 MI->AddTokenToBody(Tok);
2281
2282 // Get the next token of the macro.
2283 LexUnexpandedToken(Tok);
2284 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002285 }
Chris Lattner7e374832006-07-29 03:46:57 +00002286
Chris Lattnerf40fe992007-07-14 22:11:41 +00002287
Chris Lattner7e374832006-07-29 03:46:57 +00002288 // Disable __VA_ARGS__ again.
2289 Ident__VA_ARGS__->setIsPoisoned(true);
Chris Lattnerbff18d52006-07-06 04:49:18 +00002290
Chris Lattnerbff18d52006-07-06 04:49:18 +00002291 // Check that there is no paste (##) operator at the begining or end of the
2292 // replacement list.
Chris Lattner78186052006-07-09 00:45:31 +00002293 unsigned NumTokens = MI->getNumTokens();
Chris Lattnerbff18d52006-07-06 04:49:18 +00002294 if (NumTokens != 0) {
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002295 if (MI->getReplacementToken(0).is(tok::hashhash)) {
Chris Lattner815a1f92006-07-08 20:48:04 +00002296 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
Chris Lattnerbff18d52006-07-06 04:49:18 +00002297 delete MI;
Chris Lattner815a1f92006-07-08 20:48:04 +00002298 return;
Chris Lattnerbff18d52006-07-06 04:49:18 +00002299 }
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002300 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
Chris Lattner815a1f92006-07-08 20:48:04 +00002301 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
Chris Lattnerbff18d52006-07-06 04:49:18 +00002302 delete MI;
Chris Lattner815a1f92006-07-08 20:48:04 +00002303 return;
Chris Lattnerbff18d52006-07-06 04:49:18 +00002304 }
2305 }
2306
Chris Lattner13044d92006-07-03 05:16:44 +00002307 // If this is the primary source file, remember that this macro hasn't been
2308 // used yet.
2309 if (isInPrimaryFile())
2310 MI->setIsUsed(false);
2311
Chris Lattner22eb9722006-06-18 05:43:12 +00002312 // Finally, if this identifier already had a macro defined for it, verify that
2313 // the macro bodies are identical and free the old definition.
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002314 if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Chris Lattner13044d92006-07-03 05:16:44 +00002315 if (!OtherMI->isUsed())
2316 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2317
Chris Lattner22eb9722006-06-18 05:43:12 +00002318 // Macros must be identical. This means all tokes and whitespace separation
Chris Lattner21284df2006-07-08 07:16:08 +00002319 // must be the same. C99 6.10.3.2.
2320 if (!MI->isIdenticalTo(*OtherMI, *this)) {
Chris Lattnere8eef322006-07-08 07:01:00 +00002321 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef,
2322 MacroNameTok.getIdentifierInfo()->getName());
2323 Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2);
2324 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002325 delete OtherMI;
2326 }
2327
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002328 setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
Chris Lattner22eb9722006-06-18 05:43:12 +00002329}
2330
Chris Lattner063400e2006-10-14 19:54:15 +00002331/// HandleDefineOtherTargetDirective - Implements #define_other_target.
Chris Lattner146762e2007-07-20 16:59:19 +00002332void Preprocessor::HandleDefineOtherTargetDirective(Token &Tok) {
2333 Token MacroNameTok;
Chris Lattner063400e2006-10-14 19:54:15 +00002334 ReadMacroName(MacroNameTok, 1);
2335
2336 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002337 if (MacroNameTok.is(tok::eom))
Chris Lattner063400e2006-10-14 19:54:15 +00002338 return;
2339
2340 // Check to see if this is the last token on the #undef line.
2341 CheckEndOfDirective("#define_other_target");
2342
2343 // If there is already a macro defined by this name, turn it into a
2344 // target-specific define.
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002345 if (MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Chris Lattner063400e2006-10-14 19:54:15 +00002346 MI->setIsTargetSpecific(true);
2347 return;
2348 }
2349
2350 // Mark the identifier as being a macro on some other target.
2351 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro();
2352}
2353
Chris Lattner22eb9722006-06-18 05:43:12 +00002354
2355/// HandleUndefDirective - Implements #undef.
2356///
Chris Lattner146762e2007-07-20 16:59:19 +00002357void Preprocessor::HandleUndefDirective(Token &UndefTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002358 ++NumUndefined;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002359
Chris Lattner146762e2007-07-20 16:59:19 +00002360 Token MacroNameTok;
Chris Lattnere8eef322006-07-08 07:01:00 +00002361 ReadMacroName(MacroNameTok, 2);
Chris Lattner22eb9722006-06-18 05:43:12 +00002362
2363 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002364 if (MacroNameTok.is(tok::eom))
Chris Lattnercb283342006-06-18 06:48:37 +00002365 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00002366
2367 // Check to see if this is the last token on the #undef line.
Chris Lattnercb283342006-06-18 06:48:37 +00002368 CheckEndOfDirective("#undef");
Chris Lattner22eb9722006-06-18 05:43:12 +00002369
2370 // Okay, we finally have a valid identifier to undef.
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002371 MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
Chris Lattner22eb9722006-06-18 05:43:12 +00002372
Chris Lattner063400e2006-10-14 19:54:15 +00002373 // #undef untaints an identifier if it were marked by define_other_target.
2374 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
2375
Chris Lattner22eb9722006-06-18 05:43:12 +00002376 // If the macro is not defined, this is a noop undef, just return.
Chris Lattnercb283342006-06-18 06:48:37 +00002377 if (MI == 0) return;
Chris Lattner677757a2006-06-28 05:26:32 +00002378
Chris Lattner13044d92006-07-03 05:16:44 +00002379 if (!MI->isUsed())
2380 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattner22eb9722006-06-18 05:43:12 +00002381
2382 // Free macro definition.
2383 delete MI;
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002384 setMacroInfo(MacroNameTok.getIdentifierInfo(), 0);
Chris Lattner22eb9722006-06-18 05:43:12 +00002385}
2386
2387
Chris Lattnerb8761832006-06-24 21:31:03 +00002388//===----------------------------------------------------------------------===//
2389// Preprocessor Conditional Directive Handling.
2390//===----------------------------------------------------------------------===//
2391
Chris Lattner22eb9722006-06-18 05:43:12 +00002392/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
Chris Lattner371ac8a2006-07-04 07:11:10 +00002393/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
2394/// if any tokens have been returned or pp-directives activated before this
2395/// #ifndef has been lexed.
Chris Lattner22eb9722006-06-18 05:43:12 +00002396///
Chris Lattner146762e2007-07-20 16:59:19 +00002397void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
Chris Lattner371ac8a2006-07-04 07:11:10 +00002398 bool ReadAnyTokensBeforeDirective) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002399 ++NumIf;
Chris Lattner146762e2007-07-20 16:59:19 +00002400 Token DirectiveTok = Result;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002401
Chris Lattner146762e2007-07-20 16:59:19 +00002402 Token MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00002403 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00002404
2405 // Error reading macro name? If so, diagnostic already issued.
Chris Lattner98c1f7c2007-10-09 18:02:16 +00002406 if (MacroNameTok.is(tok::eom)) {
Chris Lattnerd05e44e2007-09-24 05:14:57 +00002407 // Skip code until we get to #endif. This helps with recovery by not
2408 // emitting an error when the #endif is reached.
2409 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2410 /*Foundnonskip*/false, /*FoundElse*/false);
Chris Lattnercb283342006-06-18 06:48:37 +00002411 return;
Chris Lattnerd05e44e2007-09-24 05:14:57 +00002412 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002413
2414 // Check to see if this is the last token on the #if[n]def line.
Chris Lattner371ac8a2006-07-04 07:11:10 +00002415 CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
2416
2417 // If the start of a top-level #ifdef, inform MIOpt.
2418 if (!ReadAnyTokensBeforeDirective &&
2419 CurLexer->getConditionalStackDepth() == 0) {
2420 assert(isIfndef && "#ifdef shouldn't reach here");
2421 CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
2422 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002423
Chris Lattner063400e2006-10-14 19:54:15 +00002424 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
Chris Lattnerc43ddc82007-10-07 08:44:20 +00002425 MacroInfo *MI = getMacroInfo(MII);
Chris Lattnera78a97e2006-07-03 05:42:18 +00002426
Chris Lattner81278c62006-10-14 19:03:49 +00002427 // If there is a macro, process it.
2428 if (MI) {
2429 // Mark it used.
2430 MI->setIsUsed(true);
2431
2432 // If this is the first use of a target-specific macro, warn about it.
2433 if (MI->isTargetSpecific()) {
2434 MI->setIsTargetSpecific(false); // Don't warn on second use.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +00002435 getTargetInfo().DiagnoseNonPortability(
2436 getFullLoc(MacroNameTok.getLocation()),
2437 diag::port_target_macro_use);
Chris Lattner81278c62006-10-14 19:03:49 +00002438 }
Chris Lattner063400e2006-10-14 19:54:15 +00002439 } else {
2440 // Use of a target-specific macro for some other target? If so, warn.
2441 if (MII->isOtherTargetMacro()) {
2442 MII->setIsOtherTargetMacro(false); // Don't warn on second use.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +00002443 getTargetInfo().DiagnoseNonPortability(
2444 getFullLoc(MacroNameTok.getLocation()),
2445 diag::port_target_macro_use);
Chris Lattner063400e2006-10-14 19:54:15 +00002446 }
Chris Lattner81278c62006-10-14 19:03:49 +00002447 }
Chris Lattnera78a97e2006-07-03 05:42:18 +00002448
Chris Lattner22eb9722006-06-18 05:43:12 +00002449 // Should we include the stuff contained by this directive?
Chris Lattnera78a97e2006-07-03 05:42:18 +00002450 if (!MI == isIfndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002451 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00002452 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00002453 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00002454 } else {
2455 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00002456 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Chris Lattnercb283342006-06-18 06:48:37 +00002457 /*Foundnonskip*/false,
2458 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00002459 }
2460}
2461
2462/// HandleIfDirective - Implements the #if directive.
2463///
Chris Lattner146762e2007-07-20 16:59:19 +00002464void Preprocessor::HandleIfDirective(Token &IfToken,
Chris Lattnera8654ca2006-07-04 17:42:08 +00002465 bool ReadAnyTokensBeforeDirective) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002466 ++NumIf;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002467
Chris Lattner371ac8a2006-07-04 07:11:10 +00002468 // Parse and evaluation the conditional expression.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00002469 IdentifierInfo *IfNDefMacro = 0;
Chris Lattnera8654ca2006-07-04 17:42:08 +00002470 bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
Chris Lattner22eb9722006-06-18 05:43:12 +00002471
2472 // Should we include the stuff contained by this directive?
2473 if (ConditionalTrue) {
Chris Lattnera8654ca2006-07-04 17:42:08 +00002474 // If this condition is equivalent to #ifndef X, and if this is the first
2475 // directive seen, handle it for the multiple-include optimization.
2476 if (!ReadAnyTokensBeforeDirective &&
2477 CurLexer->getConditionalStackDepth() == 0 && IfNDefMacro)
2478 CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
2479
Chris Lattner22eb9722006-06-18 05:43:12 +00002480 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00002481 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00002482 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00002483 } else {
2484 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00002485 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnercb283342006-06-18 06:48:37 +00002486 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00002487 }
2488}
2489
2490/// HandleEndifDirective - Implements the #endif directive.
2491///
Chris Lattner146762e2007-07-20 16:59:19 +00002492void Preprocessor::HandleEndifDirective(Token &EndifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002493 ++NumEndif;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002494
Chris Lattner22eb9722006-06-18 05:43:12 +00002495 // Check that this is the whole directive.
Chris Lattnercb283342006-06-18 06:48:37 +00002496 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00002497
2498 PPConditionalInfo CondInfo;
2499 if (CurLexer->popConditionalLevel(CondInfo)) {
2500 // No conditionals on the stack: this is an #endif without an #if.
2501 return Diag(EndifToken, diag::err_pp_endif_without_if);
2502 }
2503
Chris Lattner371ac8a2006-07-04 07:11:10 +00002504 // If this the end of a top-level #endif, inform MIOpt.
2505 if (CurLexer->getConditionalStackDepth() == 0)
2506 CurLexer->MIOpt.ExitTopLevelConditional();
2507
Chris Lattner538d7f32006-07-20 04:31:52 +00002508 assert(!CondInfo.WasSkipping && !CurLexer->LexingRawMode &&
Chris Lattner22eb9722006-06-18 05:43:12 +00002509 "This code should only be reachable in the non-skipping case!");
Chris Lattner22eb9722006-06-18 05:43:12 +00002510}
2511
2512
Chris Lattner146762e2007-07-20 16:59:19 +00002513void Preprocessor::HandleElseDirective(Token &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002514 ++NumElse;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002515
Chris Lattner22eb9722006-06-18 05:43:12 +00002516 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnercb283342006-06-18 06:48:37 +00002517 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00002518
2519 PPConditionalInfo CI;
2520 if (CurLexer->popConditionalLevel(CI))
2521 return Diag(Result, diag::pp_err_else_without_if);
Chris Lattner371ac8a2006-07-04 07:11:10 +00002522
2523 // If this is a top-level #else, inform the MIOpt.
2524 if (CurLexer->getConditionalStackDepth() == 0)
2525 CurLexer->MIOpt.FoundTopLevelElse();
Chris Lattner22eb9722006-06-18 05:43:12 +00002526
2527 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00002528 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00002529
2530 // Finally, skip the rest of the contents of this block and return the first
2531 // token after it.
2532 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2533 /*FoundElse*/true);
2534}
2535
Chris Lattner146762e2007-07-20 16:59:19 +00002536void Preprocessor::HandleElifDirective(Token &ElifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002537 ++NumElse;
Chris Lattner371ac8a2006-07-04 07:11:10 +00002538
Chris Lattner22eb9722006-06-18 05:43:12 +00002539 // #elif directive in a non-skipping conditional... start skipping.
2540 // We don't care what the condition is, because we will always skip it (since
2541 // the block immediately before it was included).
Chris Lattnercb283342006-06-18 06:48:37 +00002542 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00002543
2544 PPConditionalInfo CI;
2545 if (CurLexer->popConditionalLevel(CI))
2546 return Diag(ElifToken, diag::pp_err_elif_without_if);
2547
Chris Lattner371ac8a2006-07-04 07:11:10 +00002548 // If this is a top-level #elif, inform the MIOpt.
2549 if (CurLexer->getConditionalStackDepth() == 0)
2550 CurLexer->MIOpt.FoundTopLevelElse();
2551
Chris Lattner22eb9722006-06-18 05:43:12 +00002552 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00002553 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00002554
2555 // Finally, skip the rest of the contents of this block and return the first
2556 // token after it.
2557 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2558 /*FoundElse*/CI.FoundElse);
2559}
Chris Lattnerb8761832006-06-24 21:31:03 +00002560