blob: 1cc4e6ecea431934cb78b0e4b8969011992b54d7 [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 Lattner285c0c12008-03-09 02:26:03 +000051 CurLexer(0), CurDirLookup(0), CurTokenLexer(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 Lattner285c0c12008-03-09 02:26:03 +000070 NumCachedTokenLexers = 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;
Chris Lattner285c0c12008-03-09 02:26:03 +000092 delete IncludeMacroStack.back().TheTokenLexer;
Chris Lattner69772b02006-07-02 20:34:39 +000093 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.
Chris Lattner285c0c12008-03-09 02:26:03 +0000106 for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i)
107 delete TokenLexerCache[i];
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000108
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=");
Steve Naroff4e79d342008-02-07 23:24:32 +0000432 DefineBuiltinMacro(Buf, "__w64=");
Steve Naroffb2c80c72008-02-07 03:50:06 +0000433 DefineBuiltinMacro(Buf, "__forceinline=");
Steve Naroff6936a082008-02-07 15:26:07 +0000434 DefineBuiltinMacro(Buf, "__int8=char");
435 DefineBuiltinMacro(Buf, "__int16=short");
436 DefineBuiltinMacro(Buf, "__int32=int");
Chris Lattner00c5b282008-02-10 21:12:45 +0000437 DefineBuiltinMacro(Buf, "__int64=long long");
Steve Naroff5915777f2008-02-11 22:29:58 +0000438 DefineBuiltinMacro(Buf, "__declspec(X)=");
Steve Naroffb2c80c72008-02-07 03:50:06 +0000439 }
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000440 // FIXME: Should emit a #line directive here.
441}
442
443
444/// EnterMainSourceFile - Enter the specified FileID as the main source file,
Nate Begemanf7c3ff62008-01-07 04:01:26 +0000445/// which implicitly adds the builtin defines etc.
Ted Kremenek230bd912007-12-19 22:51:13 +0000446void Preprocessor::EnterMainSourceFile() {
447
448 unsigned MainFileID = SourceMgr.getMainFileID();
449
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000450 // Enter the main file source buffer.
451 EnterSourceFile(MainFileID, 0);
452
Chris Lattner609d4132007-11-15 19:07:47 +0000453 // Tell the header info that the main file was entered. If the file is later
454 // #imported, it won't be re-entered.
455 if (const FileEntry *FE =
456 SourceMgr.getFileEntryForLoc(SourceLocation::getFileLoc(MainFileID, 0)))
457 HeaderInfo.IncrementIncludeCount(FE);
458
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000459 std::vector<char> PrologFile;
460 PrologFile.reserve(4080);
461
462 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
463 InitializePredefinedMacros(*this, PrologFile);
464
465 // Add on the predefines from the driver.
466 PrologFile.insert(PrologFile.end(), Predefines,Predefines+strlen(Predefines));
467
468 // Memory buffer must end with a null byte!
469 PrologFile.push_back(0);
470
471 // Now that we have emitted the predefined macros, #includes, etc into
472 // PrologFile, preprocess it to populate the initial preprocessor state.
473 llvm::MemoryBuffer *SB =
474 llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(),
475 "<predefines>");
476 assert(SB && "Cannot fail to create predefined source buffer");
477 unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB);
478 assert(FileID && "Could not create FileID for predefines?");
479
480 // Start parsing the predefines.
481 EnterSourceFile(FileID, 0);
482}
Chris Lattner8a7003c2007-07-16 06:48:38 +0000483
Chris Lattnerd01e2912006-06-18 16:22:51 +0000484//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +0000485// Source File Location Methods.
486//===----------------------------------------------------------------------===//
487
Chris Lattner22eb9722006-06-18 05:43:12 +0000488/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
489/// return null on failure. isAngled indicates whether the file reference is
490/// for system #include's or not (i.e. using <> instead of "").
Chris Lattnerb8b94f12006-10-30 05:38:06 +0000491const FileEntry *Preprocessor::LookupFile(const char *FilenameStart,
492 const char *FilenameEnd,
Chris Lattnerc8997182006-06-22 05:52:16 +0000493 bool isAngled,
Chris Lattner22eb9722006-06-18 05:43:12 +0000494 const DirectoryLookup *FromDir,
Chris Lattnerc8997182006-06-22 05:52:16 +0000495 const DirectoryLookup *&CurDir) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000496 // If the header lookup mechanism may be relative to the current file, pass in
497 // info about where the current file is.
498 const FileEntry *CurFileEnt = 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000499 if (!FromDir) {
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000500 SourceLocation FileLoc = getCurrentFileLexer()->getFileLoc();
501 CurFileEnt = SourceMgr.getFileEntryForLoc(FileLoc);
Chris Lattner22eb9722006-06-18 05:43:12 +0000502 }
503
Chris Lattner63dd32b2006-10-20 04:42:40 +0000504 // Do a standard file entry lookup.
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000505 CurDir = CurDirLookup;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000506 const FileEntry *FE =
Chris Lattner7cdbad92006-10-30 05:33:15 +0000507 HeaderInfo.LookupFile(FilenameStart, FilenameEnd,
508 isAngled, FromDir, CurDir, CurFileEnt);
Chris Lattner63dd32b2006-10-20 04:42:40 +0000509 if (FE) return FE;
510
511 // Otherwise, see if this is a subframework header. If so, this is relative
512 // to one of the headers on the #include stack. Walk the list of the current
513 // headers on the #include stack and pass them to HeaderInfo.
Chris Lattner5c683b22006-10-20 05:12:14 +0000514 if (CurLexer && !CurLexer->Is_PragmaLexer) {
Chris Lattner12261882008-02-01 05:34:02 +0000515 if ((CurFileEnt = SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())))
516 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
517 CurFileEnt)))
518 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000519 }
520
521 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
522 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
Chris Lattner5c683b22006-10-20 05:12:14 +0000523 if (ISEntry.TheLexer && !ISEntry.TheLexer->Is_PragmaLexer) {
Chris Lattner12261882008-02-01 05:34:02 +0000524 if ((CurFileEnt =
525 SourceMgr.getFileEntryForLoc(ISEntry.TheLexer->getFileLoc())))
526 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart,
527 FilenameEnd, CurFileEnt)))
528 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000529 }
530 }
531
532 // Otherwise, we really couldn't find the file.
533 return 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000534}
535
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000536/// isInPrimaryFile - Return true if we're in the top-level file, not in a
537/// #include.
538bool Preprocessor::isInPrimaryFile() const {
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000539 if (CurLexer && !CurLexer->Is_PragmaLexer)
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000540 return IncludeMacroStack.empty();
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000541
Chris Lattner13044d92006-07-03 05:16:44 +0000542 // If there are any stacked lexers, we're in a #include.
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000543 assert(IncludeMacroStack[0].TheLexer &&
544 !IncludeMacroStack[0].TheLexer->Is_PragmaLexer &&
545 "Top level include stack isn't our primary lexer?");
546 for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i)
Chris Lattner13044d92006-07-03 05:16:44 +0000547 if (IncludeMacroStack[i].TheLexer &&
548 !IncludeMacroStack[i].TheLexer->Is_PragmaLexer)
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000549 return false;
550 return true;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000551}
552
553/// getCurrentLexer - Return the current file lexer being lexed from. Note
554/// that this ignores any potentially active macro expansions and _Pragma
555/// expansions going on at the time.
556Lexer *Preprocessor::getCurrentFileLexer() const {
557 if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer;
558
559 // Look for a stacked lexer.
560 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Chris Lattnerf88c53a2006-07-03 05:26:05 +0000561 Lexer *L = IncludeMacroStack[i-1].TheLexer;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000562 if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions.
563 return L;
564 }
565 return 0;
566}
567
568
Chris Lattner22eb9722006-06-18 05:43:12 +0000569/// EnterSourceFile - Add a source file to the top of the include stack and
570/// start lexing tokens from it instead of the current buffer. Return true
571/// on failure.
572void Preprocessor::EnterSourceFile(unsigned FileID,
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000573 const DirectoryLookup *CurDir) {
Chris Lattner285c0c12008-03-09 02:26:03 +0000574 assert(CurTokenLexer == 0 && "Cannot #include a file inside a macro!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000575 ++NumEnteredSourceFiles;
576
Chris Lattner69772b02006-07-02 20:34:39 +0000577 if (MaxIncludeStackDepth < IncludeMacroStack.size())
578 MaxIncludeStackDepth = IncludeMacroStack.size();
Chris Lattner22eb9722006-06-18 05:43:12 +0000579
Chris Lattner77e9de52007-07-20 16:52:03 +0000580 Lexer *TheLexer = new Lexer(SourceLocation::getFileLoc(FileID, 0), *this);
Chris Lattner69772b02006-07-02 20:34:39 +0000581 EnterSourceFileWithLexer(TheLexer, CurDir);
582}
Chris Lattner22eb9722006-06-18 05:43:12 +0000583
Chris Lattner69772b02006-07-02 20:34:39 +0000584/// EnterSourceFile - Add a source file to the top of the include stack and
585/// start lexing tokens from it instead of the current buffer.
586void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
587 const DirectoryLookup *CurDir) {
588
589 // Add the current lexer to the include stack.
Chris Lattner285c0c12008-03-09 02:26:03 +0000590 if (CurLexer || CurTokenLexer)
Chris Lattner69772b02006-07-02 20:34:39 +0000591 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
Chris Lattner285c0c12008-03-09 02:26:03 +0000592 CurTokenLexer));
Chris Lattner69772b02006-07-02 20:34:39 +0000593
594 CurLexer = TheLexer;
Chris Lattnerc8997182006-06-22 05:52:16 +0000595 CurDirLookup = CurDir;
Chris Lattner285c0c12008-03-09 02:26:03 +0000596 CurTokenLexer = 0;
Chris Lattner0c885f52006-06-21 06:50:18 +0000597
598 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000599 if (Callbacks && !CurLexer->Is_PragmaLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000600 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
601
602 // Get the file entry for the current file.
603 if (const FileEntry *FE =
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000604 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000605 FileType = HeaderInfo.getFileDirFlavor(FE);
Chris Lattnerc8997182006-06-22 05:52:16 +0000606
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000607 Callbacks->FileChanged(CurLexer->getFileLoc(),
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000608 PPCallbacks::EnterFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +0000609 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000610}
611
Chris Lattner69772b02006-07-02 20:34:39 +0000612
613
Chris Lattner22eb9722006-06-18 05:43:12 +0000614/// EnterMacro - Add a Macro to the top of the include stack and start lexing
Chris Lattnercb283342006-06-18 06:48:37 +0000615/// tokens from it instead of the current buffer.
Chris Lattner146762e2007-07-20 16:59:19 +0000616void Preprocessor::EnterMacro(Token &Tok, MacroArgs *Args) {
Chris Lattner69772b02006-07-02 20:34:39 +0000617 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
Chris Lattner285c0c12008-03-09 02:26:03 +0000618 CurTokenLexer));
Chris Lattner69772b02006-07-02 20:34:39 +0000619 CurLexer = 0;
620 CurDirLookup = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000621
Chris Lattner285c0c12008-03-09 02:26:03 +0000622 if (NumCachedTokenLexers == 0) {
623 CurTokenLexer = new TokenLexer(Tok, Args, *this);
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000624 } else {
Chris Lattner285c0c12008-03-09 02:26:03 +0000625 CurTokenLexer = TokenLexerCache[--NumCachedTokenLexers];
626 CurTokenLexer->Init(Tok, Args);
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000627 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000628}
629
Chris Lattner7667d0d2006-07-16 18:16:58 +0000630/// EnterTokenStream - Add a "macro" context to the top of the include stack,
631/// which will cause the lexer to start returning the specified tokens. Note
632/// that these tokens will be re-macro-expanded when/if expansion is enabled.
633/// This method assumes that the specified stream of tokens has a permanent
634/// owner somewhere, so they do not need to be copied.
Chris Lattner146762e2007-07-20 16:59:19 +0000635void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks) {
Chris Lattner7667d0d2006-07-16 18:16:58 +0000636 // Save our current state.
637 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
Chris Lattner285c0c12008-03-09 02:26:03 +0000638 CurTokenLexer));
Chris Lattner7667d0d2006-07-16 18:16:58 +0000639 CurLexer = 0;
640 CurDirLookup = 0;
641
642 // Create a macro expander to expand from the specified token stream.
Chris Lattner285c0c12008-03-09 02:26:03 +0000643 if (NumCachedTokenLexers == 0) {
644 CurTokenLexer = new TokenLexer(Toks, NumToks, *this);
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000645 } else {
Chris Lattner285c0c12008-03-09 02:26:03 +0000646 CurTokenLexer = TokenLexerCache[--NumCachedTokenLexers];
647 CurTokenLexer->Init(Toks, NumToks);
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000648 }
Chris Lattner7667d0d2006-07-16 18:16:58 +0000649}
650
651/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
652/// lexer stack. This should only be used in situations where the current
653/// state of the top-of-stack lexer is known.
654void Preprocessor::RemoveTopOfLexerStack() {
655 assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000656
Chris Lattner285c0c12008-03-09 02:26:03 +0000657 if (CurTokenLexer) {
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000658 // Delete or cache the now-dead macro expander.
Chris Lattner285c0c12008-03-09 02:26:03 +0000659 if (NumCachedTokenLexers == TokenLexerCacheSize)
660 delete CurTokenLexer;
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000661 else
Chris Lattner285c0c12008-03-09 02:26:03 +0000662 TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer;
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000663 } else {
664 delete CurLexer;
665 }
Chris Lattner285c0c12008-03-09 02:26:03 +0000666 CurLexer = IncludeMacroStack.back().TheLexer;
667 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
668 CurTokenLexer = IncludeMacroStack.back().TheTokenLexer;
Chris Lattner7667d0d2006-07-16 18:16:58 +0000669 IncludeMacroStack.pop_back();
670}
671
Chris Lattner22eb9722006-06-18 05:43:12 +0000672//===----------------------------------------------------------------------===//
Chris Lattner677757a2006-06-28 05:26:32 +0000673// Macro Expansion Handling.
Chris Lattner22eb9722006-06-18 05:43:12 +0000674//===----------------------------------------------------------------------===//
675
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000676/// setMacroInfo - Specify a macro for this identifier.
677///
678void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
679 if (MI == 0) {
680 if (II->hasMacroDefinition()) {
681 Macros.erase(II);
682 II->setHasMacroDefinition(false);
683 }
684 } else {
685 Macros[II] = MI;
686 II->setHasMacroDefinition(true);
687 }
688}
689
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000690/// RegisterBuiltinMacro - Register the specified identifier in the identifier
691/// table and mark it as a builtin macro to be expanded.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000692IdentifierInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000693 // Get the identifier.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000694 IdentifierInfo *Id = getIdentifierInfo(Name);
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000695
696 // Mark it as being a macro that is builtin.
697 MacroInfo *MI = new MacroInfo(SourceLocation());
698 MI->setIsBuiltinMacro();
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000699 setMacroInfo(Id, MI);
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000700 return Id;
701}
702
703
Chris Lattner677757a2006-06-28 05:26:32 +0000704/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
705/// identifier table.
706void Preprocessor::RegisterBuiltinMacros() {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000707 Ident__LINE__ = RegisterBuiltinMacro("__LINE__");
Chris Lattner630b33c2006-07-01 22:46:53 +0000708 Ident__FILE__ = RegisterBuiltinMacro("__FILE__");
Chris Lattnerc673f902006-06-30 06:10:41 +0000709 Ident__DATE__ = RegisterBuiltinMacro("__DATE__");
710 Ident__TIME__ = RegisterBuiltinMacro("__TIME__");
Chris Lattner69772b02006-07-02 20:34:39 +0000711 Ident_Pragma = RegisterBuiltinMacro("_Pragma");
Chris Lattnerc1283b92006-07-01 23:16:30 +0000712
713 // GCC Extensions.
714 Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__");
715 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__");
Chris Lattner847e0e42006-07-01 23:49:16 +0000716 Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__");
Chris Lattner22eb9722006-06-18 05:43:12 +0000717}
718
Chris Lattnerc2395832006-07-09 00:57:04 +0000719/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
720/// in its expansion, currently expands to that token literally.
Chris Lattner3ce1d1a2006-07-09 01:00:18 +0000721static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000722 const IdentifierInfo *MacroIdent,
723 Preprocessor &PP) {
Chris Lattnerc2395832006-07-09 00:57:04 +0000724 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
725
726 // If the token isn't an identifier, it's always literally expanded.
727 if (II == 0) return true;
728
729 // If the identifier is a macro, and if that macro is enabled, it may be
730 // expanded so it's not a trivial expansion.
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000731 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
Chris Lattner3ce1d1a2006-07-09 01:00:18 +0000732 // Fast expanding "#define X X" is ok, because X would be disabled.
733 II != MacroIdent)
Chris Lattnerc2395832006-07-09 00:57:04 +0000734 return false;
735
736 // If this is an object-like macro invocation, it is safe to trivially expand
737 // it.
738 if (MI->isObjectLike()) return true;
739
740 // If this is a function-like macro invocation, it's safe to trivially expand
741 // as long as the identifier is not a macro argument.
742 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
743 I != E; ++I)
744 if (*I == II)
745 return false; // Identifier is a macro argument.
Chris Lattner273ddd52006-07-29 07:33:01 +0000746
Chris Lattnerc2395832006-07-09 00:57:04 +0000747 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000748}
749
Chris Lattnerc2395832006-07-09 00:57:04 +0000750
Chris Lattnerafe603f2006-07-11 04:02:46 +0000751/// isNextPPTokenLParen - Determine whether the next preprocessor token to be
752/// lexed is a '('. If so, consume the token and return true, if not, this
753/// method should have no observable side-effect on the lexed tokens.
754bool Preprocessor::isNextPPTokenLParen() {
Chris Lattnerafe603f2006-07-11 04:02:46 +0000755 // Do some quick tests for rejection cases.
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000756 unsigned Val;
757 if (CurLexer)
Chris Lattner678c8802006-07-11 05:46:12 +0000758 Val = CurLexer->isNextPPTokenLParen();
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000759 else
Chris Lattner285c0c12008-03-09 02:26:03 +0000760 Val = CurTokenLexer->isNextTokenLParen();
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000761
762 if (Val == 2) {
Chris Lattner5c983792007-07-19 00:07:36 +0000763 // We have run off the end. If it's a source file we don't
764 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
765 // macro stack.
766 if (CurLexer)
767 return false;
768 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000769 IncludeStackInfo &Entry = IncludeMacroStack[i-1];
770 if (Entry.TheLexer)
Chris Lattner678c8802006-07-11 05:46:12 +0000771 Val = Entry.TheLexer->isNextPPTokenLParen();
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000772 else
Chris Lattner285c0c12008-03-09 02:26:03 +0000773 Val = Entry.TheTokenLexer->isNextTokenLParen();
Chris Lattner5c983792007-07-19 00:07:36 +0000774
775 if (Val != 2)
776 break;
777
778 // Ran off the end of a source file?
779 if (Entry.TheLexer)
780 return false;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000781 }
Chris Lattnerafe603f2006-07-11 04:02:46 +0000782 }
783
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000784 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
785 // have found something that isn't a '(' or we found the end of the
786 // translation unit. In either case, return false.
787 if (Val != 1)
788 return false;
Chris Lattnerafe603f2006-07-11 04:02:46 +0000789
Chris Lattner146762e2007-07-20 16:59:19 +0000790 Token Tok;
Chris Lattnerafe603f2006-07-11 04:02:46 +0000791 LexUnexpandedToken(Tok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000792 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000793 return true;
Chris Lattnerafe603f2006-07-11 04:02:46 +0000794}
Chris Lattner677757a2006-06-28 05:26:32 +0000795
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000796/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
797/// expanded as a macro, handle it and return the next token as 'Identifier'.
Chris Lattner146762e2007-07-20 16:59:19 +0000798bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000799 MacroInfo *MI) {
Chris Lattnera30be592008-01-07 19:50:27 +0000800 // If this is a macro exapnsion in the "#if !defined(x)" line for the file,
801 // then the macro could expand to different things in other contexts, we need
802 // to disable the optimization in this case.
803 if (CurLexer) CurLexer->MIOpt.ExpandedMacro();
Chris Lattner78186052006-07-09 00:45:31 +0000804
805 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
806 if (MI->isBuiltinMacro()) {
807 ExpandBuiltinMacro(Identifier);
808 return false;
809 }
810
Chris Lattneree8760b2006-07-15 07:42:55 +0000811 /// Args - If this is a function-like macro expansion, this contains,
Chris Lattner78186052006-07-09 00:45:31 +0000812 /// for each macro argument, the list of tokens that were provided to the
813 /// invocation.
Chris Lattneree8760b2006-07-15 07:42:55 +0000814 MacroArgs *Args = 0;
Chris Lattner78186052006-07-09 00:45:31 +0000815
816 // If this is a function-like macro, read the arguments.
817 if (MI->isFunctionLike()) {
Chris Lattner78186052006-07-09 00:45:31 +0000818 // C99 6.10.3p10: If the preprocessing token immediately after the the macro
Chris Lattner24dbee72007-07-19 16:11:58 +0000819 // name isn't a '(', this macro should not be expanded. Otherwise, consume
820 // it.
Chris Lattnerafe603f2006-07-11 04:02:46 +0000821 if (!isNextPPTokenLParen())
Chris Lattner78186052006-07-09 00:45:31 +0000822 return true;
823
Chris Lattner78186052006-07-09 00:45:31 +0000824 // Remember that we are now parsing the arguments to a macro invocation.
825 // Preprocessor directives used inside macro arguments are not portable, and
826 // this enables the warning.
Chris Lattneree8760b2006-07-15 07:42:55 +0000827 InMacroArgs = true;
828 Args = ReadFunctionLikeMacroArgs(Identifier, MI);
Chris Lattner78186052006-07-09 00:45:31 +0000829
830 // Finished parsing args.
Chris Lattneree8760b2006-07-15 07:42:55 +0000831 InMacroArgs = false;
Chris Lattner78186052006-07-09 00:45:31 +0000832
833 // If there was an error parsing the arguments, bail out.
Chris Lattneree8760b2006-07-15 07:42:55 +0000834 if (Args == 0) return false;
Chris Lattner78186052006-07-09 00:45:31 +0000835
836 ++NumFnMacroExpanded;
837 } else {
838 ++NumMacroExpanded;
839 }
Chris Lattner13044d92006-07-03 05:16:44 +0000840
841 // Notice that this macro has been used.
842 MI->setIsUsed(true);
Chris Lattner69772b02006-07-02 20:34:39 +0000843
844 // If we started lexing a macro, enter the macro expansion body.
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000845
846 // If this macro expands to no tokens, don't bother to push it onto the
847 // expansion stack, only to take it right back off.
848 if (MI->getNumTokens() == 0) {
Chris Lattner2ada5d32006-07-15 07:51:24 +0000849 // No need for arg info.
Chris Lattnerc1410dc2006-07-26 05:22:49 +0000850 if (Args) Args->destroy();
Chris Lattner78186052006-07-09 00:45:31 +0000851
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000852 // Ignore this macro use, just return the next token in the current
853 // buffer.
854 bool HadLeadingSpace = Identifier.hasLeadingSpace();
855 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
856
857 Lex(Identifier);
858
859 // If the identifier isn't on some OTHER line, inherit the leading
860 // whitespace/first-on-a-line property of this token. This handles
861 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
862 // empty.
863 if (!Identifier.isAtStartOfLine()) {
Chris Lattner146762e2007-07-20 16:59:19 +0000864 if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
865 if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000866 }
867 ++NumFastMacroExpanded;
Chris Lattner78186052006-07-09 00:45:31 +0000868 return false;
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000869
Chris Lattner3ce1d1a2006-07-09 01:00:18 +0000870 } else if (MI->getNumTokens() == 1 &&
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000871 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
872 *this)){
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000873 // Otherwise, if this macro expands into a single trivially-expanded
874 // token: expand it now. This handles common cases like
875 // "#define VAL 42".
876
877 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
878 // identifier to the expanded token.
879 bool isAtStartOfLine = Identifier.isAtStartOfLine();
880 bool hasLeadingSpace = Identifier.hasLeadingSpace();
881
882 // Remember where the token is instantiated.
883 SourceLocation InstantiateLoc = Identifier.getLocation();
884
885 // Replace the result token.
886 Identifier = MI->getReplacementToken(0);
887
888 // Restore the StartOfLine/LeadingSpace markers.
Chris Lattner146762e2007-07-20 16:59:19 +0000889 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
890 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000891
892 // Update the tokens location to include both its logical and physical
893 // locations.
894 SourceLocation Loc =
Chris Lattnerc673f902006-06-30 06:10:41 +0000895 SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc);
Chris Lattner8c204872006-10-14 05:19:21 +0000896 Identifier.setLocation(Loc);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000897
Chris Lattner6e4bf522006-07-27 06:59:25 +0000898 // If this is #define X X, we must mark the result as unexpandible.
899 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo())
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000900 if (getMacroInfo(NewII) == MI)
Chris Lattner146762e2007-07-20 16:59:19 +0000901 Identifier.setFlag(Token::DisableExpand);
Chris Lattner6e4bf522006-07-27 06:59:25 +0000902
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000903 // Since this is not an identifier token, it can't be macro expanded, so
904 // we're done.
905 ++NumFastMacroExpanded;
Chris Lattner78186052006-07-09 00:45:31 +0000906 return false;
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000907 }
908
Chris Lattner78186052006-07-09 00:45:31 +0000909 // Start expanding the macro.
Chris Lattneree8760b2006-07-15 07:42:55 +0000910 EnterMacro(Identifier, Args);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000911
912 // Now that the macro is at the top of the include stack, ask the
913 // preprocessor to read the next token from it.
Chris Lattner78186052006-07-09 00:45:31 +0000914 Lex(Identifier);
915 return false;
916}
917
Chris Lattneree8760b2006-07-15 07:42:55 +0000918/// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
Chris Lattner2ada5d32006-07-15 07:51:24 +0000919/// invoked to read all of the actual arguments specified for the macro
Chris Lattner78186052006-07-09 00:45:31 +0000920/// invocation. This returns null on error.
Chris Lattner146762e2007-07-20 16:59:19 +0000921MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
Chris Lattneree8760b2006-07-15 07:42:55 +0000922 MacroInfo *MI) {
Chris Lattner78186052006-07-09 00:45:31 +0000923 // The number of fixed arguments to parse.
924 unsigned NumFixedArgsLeft = MI->getNumArgs();
925 bool isVariadic = MI->isVariadic();
926
Chris Lattner78186052006-07-09 00:45:31 +0000927 // Outer loop, while there are more arguments, keep reading them.
Chris Lattner146762e2007-07-20 16:59:19 +0000928 Token Tok;
Chris Lattner8c204872006-10-14 05:19:21 +0000929 Tok.setKind(tok::comma);
Chris Lattner78186052006-07-09 00:45:31 +0000930 --NumFixedArgsLeft; // Start reading the first arg.
Chris Lattner36b6e812006-07-21 06:38:30 +0000931
932 // ArgTokens - Build up a list of tokens that make up each argument. Each
Chris Lattner7a4af3b2006-07-26 06:26:52 +0000933 // argument is separated by an EOF token. Use a SmallVector so we can avoid
934 // heap allocations in the common case.
Chris Lattner146762e2007-07-20 16:59:19 +0000935 llvm::SmallVector<Token, 64> ArgTokens;
Chris Lattner36b6e812006-07-21 06:38:30 +0000936
937 unsigned NumActuals = 0;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000938 while (Tok.is(tok::comma)) {
Chris Lattner24dbee72007-07-19 16:11:58 +0000939 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
940 // that we already consumed the first one.
Chris Lattner78186052006-07-09 00:45:31 +0000941 unsigned NumParens = 0;
Chris Lattner36b6e812006-07-21 06:38:30 +0000942
Chris Lattner78186052006-07-09 00:45:31 +0000943 while (1) {
Chris Lattnerafe603f2006-07-11 04:02:46 +0000944 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
945 // an argument value in a macro could expand to ',' or '(' or ')'.
Chris Lattner78186052006-07-09 00:45:31 +0000946 LexUnexpandedToken(Tok);
947
Chris Lattner97ff7762008-01-22 19:34:51 +0000948 if (Tok.is(tok::eof) || Tok.is(tok::eom)) { // "#if f(<eof>" & "#if f(\n"
Chris Lattner78186052006-07-09 00:45:31 +0000949 Diag(MacroName, diag::err_unterm_macro_invoc);
Chris Lattner97ff7762008-01-22 19:34:51 +0000950 // Do not lose the EOF/EOM. Return it to the client.
Chris Lattner78186052006-07-09 00:45:31 +0000951 MacroName = Tok;
952 return 0;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000953 } else if (Tok.is(tok::r_paren)) {
Chris Lattner78186052006-07-09 00:45:31 +0000954 // If we found the ) token, the macro arg list is done.
955 if (NumParens-- == 0)
956 break;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000957 } else if (Tok.is(tok::l_paren)) {
Chris Lattner78186052006-07-09 00:45:31 +0000958 ++NumParens;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000959 } else if (Tok.is(tok::comma) && NumParens == 0) {
Chris Lattner78186052006-07-09 00:45:31 +0000960 // Comma ends this argument if there are more fixed arguments expected.
961 if (NumFixedArgsLeft)
962 break;
963
Chris Lattner2ada5d32006-07-15 07:51:24 +0000964 // If this is not a variadic macro, too many args were specified.
Chris Lattner78186052006-07-09 00:45:31 +0000965 if (!isVariadic) {
966 // Emit the diagnostic at the macro name in case there is a missing ).
967 // Emitting it at the , could be far away from the macro name.
Chris Lattner2ada5d32006-07-15 07:51:24 +0000968 Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
Chris Lattner78186052006-07-09 00:45:31 +0000969 return 0;
970 }
971 // Otherwise, continue to add the tokens to this variable argument.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000972 } else if (Tok.is(tok::comment) && !KeepMacroComments) {
Chris Lattner457fc152006-07-29 06:30:25 +0000973 // If this is a comment token in the argument list and we're just in
974 // -C mode (not -CC mode), discard the comment.
975 continue;
Chris Lattner9fcdc522007-11-23 06:50:21 +0000976 } else if (Tok.is(tok::identifier)) {
977 // Reading macro arguments can cause macros that we are currently
978 // expanding from to be popped off the expansion stack. Doing so causes
979 // them to be reenabled for expansion. Here we record whether any
980 // identifiers we lex as macro arguments correspond to disabled macros.
981 // If so, we mark the token as noexpand. This is a subtle aspect of
982 // C99 6.10.3.4p2.
983 if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
984 if (!MI->isEnabled())
985 Tok.setFlag(Token::DisableExpand);
Chris Lattner78186052006-07-09 00:45:31 +0000986 }
987
988 ArgTokens.push_back(Tok);
989 }
990
Chris Lattnera12dd152006-07-11 04:09:02 +0000991 // Empty arguments are standard in C99 and supported as an extension in
992 // other modes.
993 if (ArgTokens.empty() && !Features.C99)
994 Diag(Tok, diag::ext_empty_fnmacro_arg);
Chris Lattnerafe603f2006-07-11 04:02:46 +0000995
Chris Lattner36b6e812006-07-21 06:38:30 +0000996 // Add a marker EOF token to the end of the token list for this argument.
Chris Lattner146762e2007-07-20 16:59:19 +0000997 Token EOFTok;
Chris Lattner8c204872006-10-14 05:19:21 +0000998 EOFTok.startToken();
999 EOFTok.setKind(tok::eof);
1000 EOFTok.setLocation(Tok.getLocation());
1001 EOFTok.setLength(0);
Chris Lattner36b6e812006-07-21 06:38:30 +00001002 ArgTokens.push_back(EOFTok);
1003 ++NumActuals;
Chris Lattner78186052006-07-09 00:45:31 +00001004 --NumFixedArgsLeft;
1005 };
1006
1007 // Okay, we either found the r_paren. Check to see if we parsed too few
1008 // arguments.
Chris Lattner78186052006-07-09 00:45:31 +00001009 unsigned MinArgsExpected = MI->getNumArgs();
1010
Chris Lattner775d8322006-07-29 04:39:41 +00001011 // See MacroArgs instance var for description of this.
1012 bool isVarargsElided = false;
1013
Chris Lattner2ada5d32006-07-15 07:51:24 +00001014 if (NumActuals < MinArgsExpected) {
Chris Lattner78186052006-07-09 00:45:31 +00001015 // There are several cases where too few arguments is ok, handle them now.
Chris Lattner2ada5d32006-07-15 07:51:24 +00001016 if (NumActuals+1 == MinArgsExpected && MI->isVariadic()) {
Chris Lattner78186052006-07-09 00:45:31 +00001017 // Varargs where the named vararg parameter is missing: ok as extension.
1018 // #define A(x, ...)
1019 // A("blah")
1020 Diag(Tok, diag::ext_missing_varargs_arg);
Chris Lattner775d8322006-07-29 04:39:41 +00001021
1022 // Remember this occurred if this is a C99 macro invocation with at least
1023 // one actual argument.
Chris Lattner95a06b32006-07-30 08:40:43 +00001024 isVarargsElided = MI->isC99Varargs() && MI->getNumArgs() > 1;
Chris Lattner78186052006-07-09 00:45:31 +00001025 } else if (MI->getNumArgs() == 1) {
1026 // #define A(x)
1027 // A()
Chris Lattnere7a51302006-07-29 01:25:12 +00001028 // is ok because it is an empty argument.
Chris Lattnera12dd152006-07-11 04:09:02 +00001029
1030 // Empty arguments are standard in C99 and supported as an extension in
1031 // other modes.
1032 if (ArgTokens.empty() && !Features.C99)
1033 Diag(Tok, diag::ext_empty_fnmacro_arg);
Chris Lattner78186052006-07-09 00:45:31 +00001034 } else {
1035 // Otherwise, emit the error.
Chris Lattner2ada5d32006-07-15 07:51:24 +00001036 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
Chris Lattner78186052006-07-09 00:45:31 +00001037 return 0;
1038 }
Chris Lattnere7a51302006-07-29 01:25:12 +00001039
1040 // Add a marker EOF token to the end of the token list for this argument.
1041 SourceLocation EndLoc = Tok.getLocation();
Chris Lattner8c204872006-10-14 05:19:21 +00001042 Tok.startToken();
1043 Tok.setKind(tok::eof);
1044 Tok.setLocation(EndLoc);
1045 Tok.setLength(0);
Chris Lattnere7a51302006-07-29 01:25:12 +00001046 ArgTokens.push_back(Tok);
Chris Lattner78186052006-07-09 00:45:31 +00001047 }
1048
Chris Lattner775d8322006-07-29 04:39:41 +00001049 return MacroArgs::create(MI, &ArgTokens[0], ArgTokens.size(),isVarargsElided);
Chris Lattnerf373a4a2006-06-26 06:16:29 +00001050}
1051
Chris Lattnerc673f902006-06-30 06:10:41 +00001052/// ComputeDATE_TIME - Compute the current time, enter it into the specified
1053/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
1054/// the identifier tokens inserted.
1055static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001056 Preprocessor &PP) {
Chris Lattnerc673f902006-06-30 06:10:41 +00001057 time_t TT = time(0);
1058 struct tm *TM = localtime(&TT);
1059
1060 static const char * const Months[] = {
1061 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
1062 };
1063
1064 char TmpBuffer[100];
1065 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
1066 TM->tm_year+1900);
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001067 DATELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
Chris Lattnerc673f902006-06-30 06:10:41 +00001068
1069 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001070 TIMELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
Chris Lattnerc673f902006-06-30 06:10:41 +00001071}
1072
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001073/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1074/// as a builtin macro, handle it and return the next token as 'Tok'.
Chris Lattner146762e2007-07-20 16:59:19 +00001075void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001076 // Figure out which token this is.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001077 IdentifierInfo *II = Tok.getIdentifierInfo();
1078 assert(II && "Can't be a macro without id info!");
Chris Lattner69772b02006-07-02 20:34:39 +00001079
1080 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
1081 // lex the token after it.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001082 if (II == Ident_Pragma)
Chris Lattner69772b02006-07-02 20:34:39 +00001083 return Handle_Pragma(Tok);
1084
Chris Lattner78186052006-07-09 00:45:31 +00001085 ++NumBuiltinMacroExpanded;
1086
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001087 char TmpBuffer[100];
Chris Lattner69772b02006-07-02 20:34:39 +00001088
1089 // Set up the return result.
Chris Lattner8c204872006-10-14 05:19:21 +00001090 Tok.setIdentifierInfo(0);
Chris Lattner146762e2007-07-20 16:59:19 +00001091 Tok.clearFlag(Token::NeedsCleaning);
Chris Lattner630b33c2006-07-01 22:46:53 +00001092
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001093 if (II == Ident__LINE__) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001094 // __LINE__ expands to a simple numeric value.
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001095 sprintf(TmpBuffer, "%u", SourceMgr.getLogicalLineNumber(Tok.getLocation()));
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001096 unsigned Length = strlen(TmpBuffer);
Chris Lattner8c204872006-10-14 05:19:21 +00001097 Tok.setKind(tok::numeric_constant);
1098 Tok.setLength(Length);
1099 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001100 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +00001101 SourceLocation Loc = Tok.getLocation();
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001102 if (II == Ident__BASE_FILE__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +00001103 Diag(Tok, diag::ext_pp_base_file);
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001104 SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc);
1105 while (NextLoc.isValid()) {
Chris Lattnerc1283b92006-07-01 23:16:30 +00001106 Loc = NextLoc;
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001107 NextLoc = SourceMgr.getIncludeLoc(Loc);
Chris Lattnerc1283b92006-07-01 23:16:30 +00001108 }
1109 }
1110
Chris Lattner0766e592006-07-03 01:07:01 +00001111 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001112 std::string FN = SourceMgr.getSourceName(SourceMgr.getLogicalLoc(Loc));
Chris Lattnerecc39e92006-07-15 05:23:31 +00001113 FN = '"' + Lexer::Stringify(FN) + '"';
Chris Lattner8c204872006-10-14 05:19:21 +00001114 Tok.setKind(tok::string_literal);
1115 Tok.setLength(FN.size());
1116 Tok.setLocation(CreateString(&FN[0], FN.size(), Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001117 } else if (II == Ident__DATE__) {
Chris Lattnerc673f902006-06-30 06:10:41 +00001118 if (!DATELoc.isValid())
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001119 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
Chris Lattner8c204872006-10-14 05:19:21 +00001120 Tok.setKind(tok::string_literal);
1121 Tok.setLength(strlen("\"Mmm dd yyyy\""));
1122 Tok.setLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001123 } else if (II == Ident__TIME__) {
Chris Lattnerc673f902006-06-30 06:10:41 +00001124 if (!TIMELoc.isValid())
Chris Lattnerb94ec7b2006-07-14 06:54:10 +00001125 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
Chris Lattner8c204872006-10-14 05:19:21 +00001126 Tok.setKind(tok::string_literal);
1127 Tok.setLength(strlen("\"hh:mm:ss\""));
1128 Tok.setLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001129 } else if (II == Ident__INCLUDE_LEVEL__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +00001130 Diag(Tok, diag::ext_pp_include_level);
1131
1132 // Compute the include depth of this token.
1133 unsigned Depth = 0;
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001134 SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation());
1135 for (; Loc.isValid(); ++Depth)
1136 Loc = SourceMgr.getIncludeLoc(Loc);
Chris Lattnerc1283b92006-07-01 23:16:30 +00001137
1138 // __INCLUDE_LEVEL__ expands to a simple numeric value.
1139 sprintf(TmpBuffer, "%u", Depth);
1140 unsigned Length = strlen(TmpBuffer);
Chris Lattner8c204872006-10-14 05:19:21 +00001141 Tok.setKind(tok::numeric_constant);
1142 Tok.setLength(Length);
1143 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001144 } else if (II == Ident__TIMESTAMP__) {
Chris Lattner847e0e42006-07-01 23:49:16 +00001145 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
1146 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1147 Diag(Tok, diag::ext_pp_timestamp);
1148
1149 // Get the file that we are lexing out of. If we're currently lexing from
1150 // a macro, dig into the include stack.
1151 const FileEntry *CurFile = 0;
Chris Lattnerecfeafe2006-07-02 21:26:45 +00001152 Lexer *TheLexer = getCurrentFileLexer();
Chris Lattner847e0e42006-07-01 23:49:16 +00001153
1154 if (TheLexer)
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001155 CurFile = SourceMgr.getFileEntryForLoc(TheLexer->getFileLoc());
Chris Lattner847e0e42006-07-01 23:49:16 +00001156
1157 // If this file is older than the file it depends on, emit a diagnostic.
1158 const char *Result;
1159 if (CurFile) {
1160 time_t TT = CurFile->getModificationTime();
1161 struct tm *TM = localtime(&TT);
1162 Result = asctime(TM);
1163 } else {
1164 Result = "??? ??? ?? ??:??:?? ????\n";
1165 }
1166 TmpBuffer[0] = '"';
1167 strcpy(TmpBuffer+1, Result);
1168 unsigned Len = strlen(TmpBuffer);
1169 TmpBuffer[Len-1] = '"'; // Replace the newline with a quote.
Chris Lattner8c204872006-10-14 05:19:21 +00001170 Tok.setKind(tok::string_literal);
1171 Tok.setLength(Len);
1172 Tok.setLocation(CreateString(TmpBuffer, Len, Tok.getLocation()));
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001173 } else {
1174 assert(0 && "Unknown identifier!");
Chris Lattner615315f2007-12-09 20:31:55 +00001175 }
Chris Lattner0b8cfc22006-06-28 06:49:17 +00001176}
Chris Lattner677757a2006-06-28 05:26:32 +00001177
1178//===----------------------------------------------------------------------===//
1179// Lexer Event Handling.
1180//===----------------------------------------------------------------------===//
1181
Chris Lattnercefc7682006-07-08 08:28:12 +00001182/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
1183/// identifier information for the token and install it into the token.
Chris Lattner146762e2007-07-20 16:59:19 +00001184IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
Chris Lattnercefc7682006-07-08 08:28:12 +00001185 const char *BufPtr) {
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001186 assert(Identifier.is(tok::identifier) && "Not an identifier!");
Chris Lattnercefc7682006-07-08 08:28:12 +00001187 assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
1188
1189 // Look up this token, see if it is a macro, or if it is a language keyword.
1190 IdentifierInfo *II;
1191 if (BufPtr && !Identifier.needsCleaning()) {
1192 // No cleaning needed, just use the characters from the lexed buffer.
1193 II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
1194 } else {
1195 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
Chris Lattnerf9aba2c2007-07-13 17:10:38 +00001196 llvm::SmallVector<char, 64> IdentifierBuffer;
1197 IdentifierBuffer.resize(Identifier.getLength());
1198 const char *TmpBuf = &IdentifierBuffer[0];
Chris Lattnercefc7682006-07-08 08:28:12 +00001199 unsigned Size = getSpelling(Identifier, TmpBuf);
1200 II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
1201 }
Chris Lattner8c204872006-10-14 05:19:21 +00001202 Identifier.setIdentifierInfo(II);
Chris Lattnercefc7682006-07-08 08:28:12 +00001203 return II;
1204}
1205
1206
Chris Lattner677757a2006-06-28 05:26:32 +00001207/// HandleIdentifier - This callback is invoked when the lexer reads an
1208/// identifier. This callback looks up the identifier in the map and/or
1209/// potentially macro expands it or turns it into a named token (like 'for').
Chris Lattner146762e2007-07-20 16:59:19 +00001210void Preprocessor::HandleIdentifier(Token &Identifier) {
Chris Lattner0f1f5052006-07-20 04:16:23 +00001211 assert(Identifier.getIdentifierInfo() &&
1212 "Can't handle identifiers without identifier info!");
1213
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001214 IdentifierInfo &II = *Identifier.getIdentifierInfo();
Chris Lattner677757a2006-06-28 05:26:32 +00001215
1216 // If this identifier was poisoned, and if it was not produced from a macro
1217 // expansion, emit an error.
Chris Lattner8ff71992006-07-06 05:17:39 +00001218 if (II.isPoisoned() && CurLexer) {
1219 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
1220 Diag(Identifier, diag::err_pp_used_poisoned_id);
1221 else
1222 Diag(Identifier, diag::ext_pp_bad_vaargs_use);
1223 }
Chris Lattner677757a2006-06-28 05:26:32 +00001224
Chris Lattner78186052006-07-09 00:45:31 +00001225 // If this is a macro to be expanded, do it.
Chris Lattnerc43ddc82007-10-07 08:44:20 +00001226 if (MacroInfo *MI = getMacroInfo(&II)) {
Chris Lattner6e4bf522006-07-27 06:59:25 +00001227 if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
1228 if (MI->isEnabled()) {
1229 if (!HandleMacroExpandedIdentifier(Identifier, MI))
1230 return;
1231 } else {
1232 // C99 6.10.3.4p2 says that a disabled macro may never again be
1233 // expanded, even if it's in a context where it could be expanded in the
1234 // future.
Chris Lattner146762e2007-07-20 16:59:19 +00001235 Identifier.setFlag(Token::DisableExpand);
Chris Lattner6e4bf522006-07-27 06:59:25 +00001236 }
1237 }
Chris Lattner063400e2006-10-14 19:54:15 +00001238 }
Chris Lattner677757a2006-06-28 05:26:32 +00001239
Chris Lattner5b9f4892006-11-21 17:23:33 +00001240 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
1241 // then we act as if it is the actual operator and not the textual
1242 // representation of it.
1243 if (II.isCPlusPlusOperatorKeyword())
1244 Identifier.setIdentifierInfo(0);
1245
Chris Lattner677757a2006-06-28 05:26:32 +00001246 // Change the kind of this identifier to the appropriate token kind, e.g.
1247 // turning "for" into a keyword.
Chris Lattner8c204872006-10-14 05:19:21 +00001248 Identifier.setKind(II.getTokenID());
Chris Lattner677757a2006-06-28 05:26:32 +00001249
1250 // If this is an extension token, diagnose its use.
Steve Naroffa8fd9732007-06-11 00:35:03 +00001251 // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99
1252 // For now, I'm just commenting it out (while I work on attributes).
Chris Lattner53621a52007-06-13 20:44:40 +00001253 if (II.isExtensionToken() && Features.C99)
1254 Diag(Identifier, diag::ext_token_used);
Chris Lattner677757a2006-06-28 05:26:32 +00001255}
1256
Chris Lattner22eb9722006-06-18 05:43:12 +00001257/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
1258/// the current file. This either returns the EOF token or pops a level off
1259/// the include stack and keeps going.
Chris Lattner146762e2007-07-20 16:59:19 +00001260bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
Chris Lattner285c0c12008-03-09 02:26:03 +00001261 assert(!CurTokenLexer &&
Chris Lattner22eb9722006-06-18 05:43:12 +00001262 "Ending a file when currently in a macro!");
1263
Chris Lattner371ac8a2006-07-04 07:11:10 +00001264 // See if this file had a controlling macro.
Chris Lattner3665f162006-07-04 07:26:10 +00001265 if (CurLexer) { // Not ending a macro, ignore it.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001266 if (const IdentifierInfo *ControllingMacro =
Chris Lattner371ac8a2006-07-04 07:11:10 +00001267 CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
Chris Lattner3665f162006-07-04 07:26:10 +00001268 // Okay, this has a controlling macro, remember in PerFileInfo.
1269 if (const FileEntry *FE =
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001270 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001271 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
Chris Lattner371ac8a2006-07-04 07:11:10 +00001272 }
1273 }
1274
Chris Lattner22eb9722006-06-18 05:43:12 +00001275 // If this is a #include'd file, pop it off the include stack and continue
1276 // lexing the #includer file.
Chris Lattner69772b02006-07-02 20:34:39 +00001277 if (!IncludeMacroStack.empty()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001278 // We're done with the #included file.
Chris Lattner7667d0d2006-07-16 18:16:58 +00001279 RemoveTopOfLexerStack();
Chris Lattner0c885f52006-06-21 06:50:18 +00001280
1281 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +00001282 if (Callbacks && !isEndOfMacro && CurLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +00001283 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
1284
1285 // Get the file entry for the current file.
1286 if (const FileEntry *FE =
Chris Lattnerdc5c0552007-07-20 16:37:10 +00001287 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001288 FileType = HeaderInfo.getFileDirFlavor(FE);
Chris Lattnerc8997182006-06-22 05:52:16 +00001289
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +00001290 Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr),
1291 PPCallbacks::ExitFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +00001292 }
Chris Lattner2183a6e2006-07-18 06:36:12 +00001293
1294 // Client should lex another token.
1295 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001296 }
Chris Lattner9046dc12008-01-25 00:00:30 +00001297
1298 // If the file ends with a newline, form the EOF token on the newline itself,
1299 // rather than "on the line following it", which doesn't exist. This makes
1300 // diagnostics relating to the end of file include the last file that the user
1301 // actually typed, which is goodness.
1302 const char *EndPos = CurLexer->BufferEnd;
1303 if (EndPos != CurLexer->BufferStart &&
1304 (EndPos[-1] == '\n' || EndPos[-1] == '\r')) {
1305 --EndPos;
1306
1307 // Handle \n\r and \r\n:
1308 if (EndPos != CurLexer->BufferStart &&
1309 (EndPos[-1] == '\n' || EndPos[-1] == '\r') &&
1310 EndPos[-1] != EndPos[0])
1311 --EndPos;
1312 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001313
Chris Lattner8c204872006-10-14 05:19:21 +00001314 Result.startToken();
Chris Lattner9046dc12008-01-25 00:00:30 +00001315 CurLexer->BufferPtr = EndPos;
1316 CurLexer->FormTokenWithChars(Result, EndPos);
Chris Lattner8c204872006-10-14 05:19:21 +00001317 Result.setKind(tok::eof);
Chris Lattner22eb9722006-06-18 05:43:12 +00001318
1319 // We're done with the #included file.
1320 delete CurLexer;
1321 CurLexer = 0;
Chris Lattner13044d92006-07-03 05:16:44 +00001322
Chris Lattner03f83482006-07-10 06:16:26 +00001323 // This is the end of the top-level file. If the diag::pp_macro_not_used
Chris Lattnerc43ddc82007-10-07 08:44:20 +00001324 // diagnostic is enabled, look for macros that have not been used.
Chris Lattnerb055f2d2007-02-11 08:19:57 +00001325 if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){
Chris Lattnerc43ddc82007-10-07 08:44:20 +00001326 for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
1327 Macros.begin(), E = Macros.end(); I != E; ++I) {
1328 if (!I->second->isUsed())
1329 Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattnerb055f2d2007-02-11 08:19:57 +00001330 }
1331 }
Chris Lattner2183a6e2006-07-18 06:36:12 +00001332 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001333}
1334
1335/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
Chris Lattner7667d0d2006-07-16 18:16:58 +00001336/// the current macro expansion or token stream expansion.
Chris Lattner146762e2007-07-20 16:59:19 +00001337bool Preprocessor::HandleEndOfMacro(Token &Result) {
Chris Lattner285c0c12008-03-09 02:26:03 +00001338 assert(CurTokenLexer && !CurLexer &&
Chris Lattner22eb9722006-06-18 05:43:12 +00001339 "Ending a macro when currently in a #include file!");
1340
Chris Lattnerc02c4ab2007-07-15 00:25:26 +00001341 // Delete or cache the now-dead macro expander.
Chris Lattner285c0c12008-03-09 02:26:03 +00001342 if (NumCachedTokenLexers == TokenLexerCacheSize)
1343 delete CurTokenLexer;
Chris Lattnerc02c4ab2007-07-15 00:25:26 +00001344 else
Chris Lattner285c0c12008-03-09 02:26:03 +00001345 TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer;
Chris Lattner22eb9722006-06-18 05:43:12 +00001346
Chris Lattner69772b02006-07-02 20:34:39 +00001347 // Handle this like a #include file being popped off the stack.
Chris Lattner285c0c12008-03-09 02:26:03 +00001348 CurTokenLexer = 0;
Chris Lattner69772b02006-07-02 20:34:39 +00001349 return HandleEndOfFile(Result, true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001350}
1351
Chris Lattner3b5054d2008-02-07 06:03:59 +00001352/// HandleMicrosoftCommentPaste - When the macro expander pastes together a
1353/// comment (/##/) in microsoft mode, this method handles updating the current
1354/// state, returning the token on the next source line.
1355void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) {
Chris Lattner285c0c12008-03-09 02:26:03 +00001356 assert(CurTokenLexer && !CurLexer &&
Chris Lattner3b5054d2008-02-07 06:03:59 +00001357 "Pasted comment can only be formed from macro");
1358
1359 // We handle this by scanning for the closest real lexer, switching it to
1360 // raw mode and preprocessor mode. This will cause it to return \n as an
1361 // explicit EOM token.
1362 Lexer *FoundLexer = 0;
1363 bool LexerWasInPPMode = false;
1364 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
1365 IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1);
1366 if (ISI.TheLexer == 0) continue; // Scan for a real lexer.
1367
1368 // Once we find a real lexer, mark it as raw mode (disabling macro
1369 // expansions) and preprocessor mode (return EOM). We know that the lexer
1370 // was *not* in raw mode before, because the macro that the comment came
1371 // from was expanded. However, it could have already been in preprocessor
1372 // mode (#if COMMENT) in which case we have to return it to that mode and
1373 // return EOM.
1374 FoundLexer = ISI.TheLexer;
1375 FoundLexer->LexingRawMode = true;
1376 LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective;
1377 FoundLexer->ParsingPreprocessorDirective = true;
1378 break;
1379 }
1380
1381 // Okay, we either found and switched over the lexer, or we didn't find a
1382 // lexer. In either case, finish off the macro the comment came from, getting
1383 // the next token.
1384 if (!HandleEndOfMacro(Tok)) Lex(Tok);
1385
1386 // Discarding comments as long as we don't have EOF or EOM. This 'comments
1387 // out' the rest of the line, including any tokens that came from other macros
1388 // that were active, as in:
1389 // #define submacro a COMMENT b
1390 // submacro c
1391 // which should lex to 'a' only: 'b' and 'c' should be removed.
1392 while (Tok.isNot(tok::eom) && Tok.isNot(tok::eof))
1393 Lex(Tok);
1394
1395 // If we got an eom token, then we successfully found the end of the line.
1396 if (Tok.is(tok::eom)) {
1397 assert(FoundLexer && "Can't get end of line without an active lexer");
1398 // Restore the lexer back to normal mode instead of raw mode.
1399 FoundLexer->LexingRawMode = false;
1400
1401 // If the lexer was already in preprocessor mode, just return the EOM token
1402 // to finish the preprocessor line.
1403 if (LexerWasInPPMode) return;
1404
1405 // Otherwise, switch out of PP mode and return the next lexed token.
1406 FoundLexer->ParsingPreprocessorDirective = false;
1407 return Lex(Tok);
1408 }
1409
1410 // If we got an EOF token, then we reached the end of the token stream but
1411 // didn't find an explicit \n. This can only happen if there was no lexer
1412 // active (an active lexer would return EOM at EOF if there was no \n in
1413 // preprocessor directive mode), so just return EOF as our token.
1414 assert(!FoundLexer && "Lexer should return EOM before EOF in PP mode");
1415 return;
1416}
Chris Lattner22eb9722006-06-18 05:43:12 +00001417