blob: 33c94b6e8ad572d4b856a1c2cf8d4129f65183a8 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Preprocessor interface.
11//
12//===----------------------------------------------------------------------===//
13//
14// Options to support:
15// -H - Print the name of each header file used.
16// -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//
26//===----------------------------------------------------------------------===//
27
28#include "clang/Lex/Preprocessor.h"
29#include "clang/Lex/HeaderSearch.h"
30#include "clang/Lex/MacroInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000031#include "clang/Lex/Pragma.h"
32#include "clang/Lex/ScratchBuffer.h"
33#include "clang/Basic/Diagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000034#include "clang/Basic/SourceManager.h"
35#include "clang/Basic/TargetInfo.h"
36#include "llvm/ADT/SmallVector.h"
Chris Lattner97ba77c2007-07-16 06:48:38 +000037#include "llvm/Support/MemoryBuffer.h"
Ted Kremenekbdd30c22008-01-14 16:44:48 +000038#include "llvm/Support/Streams.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000039using namespace clang;
40
41//===----------------------------------------------------------------------===//
42
Ted Kremenekec6c5742008-04-17 21:23:07 +000043PreprocessorFactory::~PreprocessorFactory() {}
44
Reid Spencer5f016e22007-07-11 17:01:13 +000045Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
46 TargetInfo &target, SourceManager &SM,
47 HeaderSearch &Headers)
48 : Diags(diags), Features(opts), Target(target), FileMgr(Headers.getFileMgr()),
49 SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts),
Chris Lattner6cfe7592008-03-09 02:26:03 +000050 CurLexer(0), CurDirLookup(0), CurTokenLexer(0), Callbacks(0) {
Reid Spencer5f016e22007-07-11 17:01:13 +000051 ScratchBuf = new ScratchBuffer(SourceMgr);
Chris Lattner9594acf2007-07-15 00:25:26 +000052
Reid Spencer5f016e22007-07-11 17:01:13 +000053 // Clear stats.
54 NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
55 NumIf = NumElse = NumEndif = 0;
56 NumEnteredSourceFiles = 0;
57 NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
58 NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
59 MaxIncludeStackDepth = 0;
60 NumSkipped = 0;
61
62 // Default to discarding comments.
63 KeepComments = false;
64 KeepMacroComments = false;
65
66 // Macro expansion is enabled.
67 DisableMacroExpansion = false;
68 InMacroArgs = false;
Chris Lattner6cfe7592008-03-09 02:26:03 +000069 NumCachedTokenLexers = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000070
Argyrios Kyrtzidis03db1b32008-08-10 13:15:22 +000071 CacheTokens = false;
72 CachedLexPos = 0;
73
Reid Spencer5f016e22007-07-11 17:01:13 +000074 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
75 // This gets unpoisoned where it is allowed.
76 (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
77
78 // Initialize the pragma handlers.
79 PragmaHandlers = new PragmaNamespace(0);
80 RegisterBuiltinPragmas();
81
82 // Initialize builtin macros like __LINE__ and friends.
83 RegisterBuiltinMacros();
84}
85
86Preprocessor::~Preprocessor() {
87 // Free any active lexers.
88 delete CurLexer;
89
90 while (!IncludeMacroStack.empty()) {
91 delete IncludeMacroStack.back().TheLexer;
Chris Lattner6cfe7592008-03-09 02:26:03 +000092 delete IncludeMacroStack.back().TheTokenLexer;
Reid Spencer5f016e22007-07-11 17:01:13 +000093 IncludeMacroStack.pop_back();
94 }
Chris Lattnercc1a8752007-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 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000104
Chris Lattner9594acf2007-07-15 00:25:26 +0000105 // Free any cached macro expanders.
Chris Lattner6cfe7592008-03-09 02:26:03 +0000106 for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i)
107 delete TokenLexerCache[i];
Chris Lattner9594acf2007-07-15 00:25:26 +0000108
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 // Release pragma information.
110 delete PragmaHandlers;
111
112 // Delete the scratch buffer info.
113 delete ScratchBuf;
Chris Lattnereb50ed82008-03-14 06:07:05 +0000114
115 delete Callbacks;
Reid Spencer5f016e22007-07-11 17:01:13 +0000116}
117
Reid Spencer5f016e22007-07-11 17:01:13 +0000118/// Diag - Forwarding function for diagnostics. This emits a diagnostic at
Chris Lattnerd2177732007-07-20 16:59:19 +0000119/// the specified Token's location, translating the token's start
Reid Spencer5f016e22007-07-11 17:01:13 +0000120/// position in the current buffer into a SourcePosition object for rendering.
121void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000122 Diags.Report(getFullLoc(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000123}
124
125void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
126 const std::string &Msg) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000127 Diags.Report(getFullLoc(Loc), DiagID, &Msg, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000128}
129
Chris Lattner8ed30442008-05-05 06:45:50 +0000130void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
131 const std::string &Msg,
132 const SourceRange &R1, const SourceRange &R2) {
133 SourceRange R[] = {R1, R2};
134 Diags.Report(getFullLoc(Loc), DiagID, &Msg, 1, R, 2);
135}
136
137
138void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
139 const SourceRange &R) {
140 Diags.Report(getFullLoc(Loc), DiagID, 0, 0, &R, 1);
141}
142
143void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
144 const SourceRange &R1, const SourceRange &R2) {
145 SourceRange R[] = {R1, R2};
146 Diags.Report(getFullLoc(Loc), DiagID, 0, 0, R, 2);
147}
148
149
Chris Lattnerd2177732007-07-20 16:59:19 +0000150void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000151 llvm::cerr << tok::getTokenName(Tok.getKind()) << " '"
152 << getSpelling(Tok) << "'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000153
154 if (!DumpFlags) return;
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000155
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000156 llvm::cerr << "\t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 if (Tok.isAtStartOfLine())
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000158 llvm::cerr << " [StartOfLine]";
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 if (Tok.hasLeadingSpace())
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000160 llvm::cerr << " [LeadingSpace]";
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 if (Tok.isExpandDisabled())
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000162 llvm::cerr << " [ExpandDisabled]";
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 if (Tok.needsCleaning()) {
164 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000165 llvm::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
166 << "']";
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 }
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000168
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000169 llvm::cerr << "\tLoc=<";
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000170 DumpLocation(Tok.getLocation());
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000171 llvm::cerr << ">";
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000172}
173
174void Preprocessor::DumpLocation(SourceLocation Loc) const {
175 SourceLocation LogLoc = SourceMgr.getLogicalLoc(Loc);
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000176 llvm::cerr << SourceMgr.getSourceName(LogLoc) << ':'
177 << SourceMgr.getLineNumber(LogLoc) << ':'
Ted Kremenek109949a2008-07-19 19:10:04 +0000178 << SourceMgr.getColumnNumber(LogLoc);
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000179
180 SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(Loc);
181 if (PhysLoc != LogLoc) {
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000182 llvm::cerr << " <PhysLoc=";
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000183 DumpLocation(PhysLoc);
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000184 llvm::cerr << ">";
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000185 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000186}
187
188void Preprocessor::DumpMacro(const MacroInfo &MI) const {
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000189 llvm::cerr << "MACRO: ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
191 DumpToken(MI.getReplacementToken(i));
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000192 llvm::cerr << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 }
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000194 llvm::cerr << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000195}
196
197void Preprocessor::PrintStats() {
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000198 llvm::cerr << "\n*** Preprocessor Stats:\n";
199 llvm::cerr << NumDirectives << " directives found:\n";
200 llvm::cerr << " " << NumDefined << " #define.\n";
201 llvm::cerr << " " << NumUndefined << " #undef.\n";
202 llvm::cerr << " #include/#include_next/#import:\n";
203 llvm::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
204 llvm::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
205 llvm::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
206 llvm::cerr << " " << NumElse << " #else/#elif.\n";
207 llvm::cerr << " " << NumEndif << " #endif.\n";
208 llvm::cerr << " " << NumPragma << " #pragma.\n";
209 llvm::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000210
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000211 llvm::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
212 << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
213 << NumFastMacroExpanded << " on the fast path.\n";
214 llvm::cerr << (NumFastTokenPaste+NumTokenPaste)
215 << " token paste (##) operations performed, "
216 << NumFastTokenPaste << " on the fast path.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000217}
218
219//===----------------------------------------------------------------------===//
220// Token Spelling
221//===----------------------------------------------------------------------===//
222
223
224/// getSpelling() - Return the 'spelling' of this token. The spelling of a
225/// token are the characters used to represent the token in the source file
226/// after trigraph expansion and escaped-newline folding. In particular, this
227/// wants to get the true, uncanonicalized, spelling of things like digraphs
228/// UCNs, etc.
Chris Lattnerd2177732007-07-20 16:59:19 +0000229std::string Preprocessor::getSpelling(const Token &Tok) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
231
232 // If this token contains nothing interesting, return it directly.
233 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
234 if (!Tok.needsCleaning())
235 return std::string(TokStart, TokStart+Tok.getLength());
236
237 std::string Result;
238 Result.reserve(Tok.getLength());
239
240 // Otherwise, hard case, relex the characters into the string.
241 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
242 Ptr != End; ) {
243 unsigned CharSize;
244 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
245 Ptr += CharSize;
246 }
247 assert(Result.size() != unsigned(Tok.getLength()) &&
248 "NeedsCleaning flag set on something that didn't need cleaning!");
249 return Result;
250}
251
252/// getSpelling - This method is used to get the spelling of a token into a
253/// preallocated buffer, instead of as an std::string. The caller is required
254/// to allocate enough space for the token, which is guaranteed to be at least
255/// Tok.getLength() bytes long. The actual length of the token is returned.
256///
257/// Note that this method may do two possible things: it may either fill in
258/// the buffer specified with characters, or it may *change the input pointer*
259/// to point to a constant buffer with the data already in it (avoiding a
260/// copy). The caller is not allowed to modify the returned buffer pointer
261/// if an internal buffer is returned.
Chris Lattnerd2177732007-07-20 16:59:19 +0000262unsigned Preprocessor::getSpelling(const Token &Tok,
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 const char *&Buffer) const {
264 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
265
266 // If this token is an identifier, just return the string from the identifier
267 // table, which is very quick.
268 if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
269 Buffer = II->getName();
Chris Lattner0f670322007-07-22 22:50:09 +0000270
271 // Return the length of the token. If the token needed cleaning, don't
272 // include the size of the newlines or trigraphs in it.
273 if (!Tok.needsCleaning())
274 return Tok.getLength();
275 else
276 return strlen(Buffer);
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 }
278
279 // Otherwise, compute the start of the token in the input lexer buffer.
280 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
281
282 // If this token contains nothing interesting, return it directly.
283 if (!Tok.needsCleaning()) {
284 Buffer = TokStart;
285 return Tok.getLength();
286 }
287 // Otherwise, hard case, relex the characters into the string.
288 char *OutBuf = const_cast<char*>(Buffer);
289 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
290 Ptr != End; ) {
291 unsigned CharSize;
292 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
293 Ptr += CharSize;
294 }
295 assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
296 "NeedsCleaning flag set on something that didn't need cleaning!");
297
298 return OutBuf-Buffer;
299}
300
301
302/// CreateString - Plop the specified string into a scratch buffer and return a
303/// location for it. If specified, the source location provides a source
304/// location for the token.
305SourceLocation Preprocessor::
306CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) {
307 if (SLoc.isValid())
308 return ScratchBuf->getToken(Buf, Len, SLoc);
309 return ScratchBuf->getToken(Buf, Len);
310}
311
312
Chris Lattner97ba77c2007-07-16 06:48:38 +0000313/// AdvanceToTokenCharacter - Given a location that specifies the start of a
314/// token, return a new location that specifies a character within the token.
315SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart,
316 unsigned CharNo) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000317 // If they request the first char of the token, we're trivially done. If this
318 // is a macro expansion, it doesn't make sense to point to a character within
319 // the instantiation point (the name). We could point to the source
320 // character, but without also pointing to instantiation info, this is
321 // confusing.
322 if (CharNo == 0 || TokStart.isMacroID()) return TokStart;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000323
324 // Figure out how many physical characters away the specified logical
325 // character is. This needs to take into consideration newlines and
326 // trigraphs.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000327 const char *TokPtr = SourceMgr.getCharacterData(TokStart);
328 unsigned PhysOffset = 0;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000329
330 // The usual case is that tokens don't contain anything interesting. Skip
331 // over the uninteresting characters. If a token only consists of simple
332 // chars, this method is extremely fast.
333 while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr))
Chris Lattner9dc1f532007-07-20 16:37:10 +0000334 ++TokPtr, --CharNo, ++PhysOffset;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000335
336 // If we have a character that may be a trigraph or escaped newline, create a
337 // lexer to parse it correctly.
Chris Lattner97ba77c2007-07-16 06:48:38 +0000338 if (CharNo != 0) {
339 // Create a lexer starting at this token position.
Chris Lattner25bdb512007-07-20 16:52:03 +0000340 Lexer TheLexer(TokStart, *this, TokPtr);
Chris Lattnerd2177732007-07-20 16:59:19 +0000341 Token Tok;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000342 // Skip over characters the remaining characters.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000343 const char *TokStartPtr = TokPtr;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000344 for (; CharNo; --CharNo)
345 TheLexer.getAndAdvanceChar(TokPtr, Tok);
Chris Lattner9dc1f532007-07-20 16:37:10 +0000346
347 PhysOffset += TokPtr-TokStartPtr;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000348 }
Chris Lattner9dc1f532007-07-20 16:37:10 +0000349
350 return TokStart.getFileLocWithOffset(PhysOffset);
Chris Lattner97ba77c2007-07-16 06:48:38 +0000351}
352
353
Chris Lattner53b0dab2007-10-09 22:10:18 +0000354//===----------------------------------------------------------------------===//
355// Preprocessor Initialization Methods
356//===----------------------------------------------------------------------===//
357
358// Append a #define line to Buf for Macro. Macro should be of the form XXX,
359// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
360// "#define XXX Y z W". To get a #define with no value, use "XXX=".
361static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
362 const char *Command = "#define ") {
363 Buf.insert(Buf.end(), Command, Command+strlen(Command));
364 if (const char *Equal = strchr(Macro, '=')) {
365 // Turn the = into ' '.
366 Buf.insert(Buf.end(), Macro, Equal);
367 Buf.push_back(' ');
368 Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal));
369 } else {
370 // Push "macroname 1".
371 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
372 Buf.push_back(' ');
373 Buf.push_back('1');
374 }
375 Buf.push_back('\n');
376}
377
378
379static void InitializePredefinedMacros(Preprocessor &PP,
380 std::vector<char> &Buf) {
381 // FIXME: Implement magic like cpp_init_builtins for things like __STDC__
382 // and __DATE__ etc.
383#if 0
384 /* __STDC__ has the value 1 under normal circumstances.
385 However, if (a) we are in a system header, (b) the option
386 stdc_0_in_system_headers is true (set by target config), and
387 (c) we are not in strictly conforming mode, then it has the
388 value 0. (b) and (c) are already checked in cpp_init_builtins. */
389 //case BT_STDC:
390 if (cpp_in_system_header (pfile))
391 number = 0;
392 else
393 number = 1;
394 break;
395#endif
396 // These should all be defined in the preprocessor according to the
397 // current language configuration.
398 DefineBuiltinMacro(Buf, "__STDC__=1");
399 //DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
400 if (PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus)
401 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
402 else if (0) // STDC94 ?
403 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
404
405 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");
406 if (PP.getLangOptions().ObjC1)
407 DefineBuiltinMacro(Buf, "__OBJC__=1");
Steve Naroff73b17cd2008-05-15 21:12:10 +0000408 if (PP.getLangOptions().ObjC2)
409 DefineBuiltinMacro(Buf, "OBJC_NEW_PROPERTIES");
Steve Naroff8ee529b2007-10-31 18:42:27 +0000410
Chris Lattnerd19144b2007-10-10 17:48:53 +0000411 // Add __builtin_va_list typedef.
412 {
413 const char *VAList = PP.getTargetInfo().getVAListDeclaration();
414 Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
415 Buf.push_back('\n');
416 }
Chris Lattner53b0dab2007-10-09 22:10:18 +0000417
418 // Get the target #defines.
419 PP.getTargetInfo().getTargetDefines(Buf);
Chris Lattnerd86522a2008-06-26 17:26:01 +0000420
421 DefineBuiltinMacro(Buf, "__llvm__=1"); // LLVM Backend
422 DefineBuiltinMacro(Buf, "__clang__=1"); // Clang Frontend
Chris Lattner53b0dab2007-10-09 22:10:18 +0000423
424 // Compiler set macros.
425 DefineBuiltinMacro(Buf, "__APPLE_CC__=5250");
Steve Naroff39d0a272007-11-10 18:06:36 +0000426 DefineBuiltinMacro(Buf, "__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=1050");
Chris Lattner53b0dab2007-10-09 22:10:18 +0000427 DefineBuiltinMacro(Buf, "__GNUC_MINOR__=0");
428 DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
429 DefineBuiltinMacro(Buf, "__GNUC__=4");
430 DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
431 DefineBuiltinMacro(Buf, "__VERSION__=\"4.0.1 (Apple Computer, Inc. "
432 "build 5250)\"");
433
434 // Build configuration options.
435 DefineBuiltinMacro(Buf, "__DYNAMIC__=1");
436 DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
437 DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
438 DefineBuiltinMacro(Buf, "__PIC__=1");
439
440
441 if (PP.getLangOptions().CPlusPlus) {
442 DefineBuiltinMacro(Buf, "__DEPRECATED=1");
443 DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
444 DefineBuiltinMacro(Buf, "__GNUG__=4");
445 DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
446 DefineBuiltinMacro(Buf, "__cplusplus=1");
447 DefineBuiltinMacro(Buf, "__private_extern__=extern");
448 }
Steve Naroffd62701b2008-02-07 03:50:06 +0000449 if (PP.getLangOptions().Microsoft) {
450 DefineBuiltinMacro(Buf, "__stdcall=");
451 DefineBuiltinMacro(Buf, "__cdecl=");
452 DefineBuiltinMacro(Buf, "_cdecl=");
453 DefineBuiltinMacro(Buf, "__ptr64=");
Steve Naroffb746ce82008-02-07 23:24:32 +0000454 DefineBuiltinMacro(Buf, "__w64=");
Steve Naroffd62701b2008-02-07 03:50:06 +0000455 DefineBuiltinMacro(Buf, "__forceinline=");
Steve Naroff419154d2008-02-07 15:26:07 +0000456 DefineBuiltinMacro(Buf, "__int8=char");
457 DefineBuiltinMacro(Buf, "__int16=short");
458 DefineBuiltinMacro(Buf, "__int32=int");
Chris Lattner9880ba92008-02-10 21:12:45 +0000459 DefineBuiltinMacro(Buf, "__int64=long long");
Steve Naroff705b5b52008-02-11 22:29:58 +0000460 DefineBuiltinMacro(Buf, "__declspec(X)=");
Steve Naroffd62701b2008-02-07 03:50:06 +0000461 }
Chris Lattner53b0dab2007-10-09 22:10:18 +0000462 // FIXME: Should emit a #line directive here.
463}
464
465
466/// EnterMainSourceFile - Enter the specified FileID as the main source file,
Nate Begeman6b616022008-01-07 04:01:26 +0000467/// which implicitly adds the builtin defines etc.
Ted Kremenek95041a22007-12-19 22:51:13 +0000468void Preprocessor::EnterMainSourceFile() {
469
470 unsigned MainFileID = SourceMgr.getMainFileID();
471
Chris Lattner53b0dab2007-10-09 22:10:18 +0000472 // Enter the main file source buffer.
473 EnterSourceFile(MainFileID, 0);
474
Chris Lattnerb2832982007-11-15 19:07:47 +0000475 // Tell the header info that the main file was entered. If the file is later
476 // #imported, it won't be re-entered.
477 if (const FileEntry *FE =
478 SourceMgr.getFileEntryForLoc(SourceLocation::getFileLoc(MainFileID, 0)))
479 HeaderInfo.IncrementIncludeCount(FE);
480
Chris Lattner53b0dab2007-10-09 22:10:18 +0000481 std::vector<char> PrologFile;
482 PrologFile.reserve(4080);
483
484 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
485 InitializePredefinedMacros(*this, PrologFile);
486
487 // Add on the predefines from the driver.
Chris Lattneraa391972008-04-19 23:09:31 +0000488 PrologFile.insert(PrologFile.end(), Predefines.begin(), Predefines.end());
Chris Lattner53b0dab2007-10-09 22:10:18 +0000489
490 // Memory buffer must end with a null byte!
491 PrologFile.push_back(0);
492
493 // Now that we have emitted the predefined macros, #includes, etc into
494 // PrologFile, preprocess it to populate the initial preprocessor state.
495 llvm::MemoryBuffer *SB =
496 llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(),
497 "<predefines>");
498 assert(SB && "Cannot fail to create predefined source buffer");
499 unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB);
500 assert(FileID && "Could not create FileID for predefines?");
501
502 // Start parsing the predefines.
503 EnterSourceFile(FileID, 0);
504}
Chris Lattner97ba77c2007-07-16 06:48:38 +0000505
Reid Spencer5f016e22007-07-11 17:01:13 +0000506
507//===----------------------------------------------------------------------===//
508// Lexer Event Handling.
509//===----------------------------------------------------------------------===//
510
511/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
512/// identifier information for the token and install it into the token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000513IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
Reid Spencer5f016e22007-07-11 17:01:13 +0000514 const char *BufPtr) {
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000515 assert(Identifier.is(tok::identifier) && "Not an identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
517
518 // Look up this token, see if it is a macro, or if it is a language keyword.
519 IdentifierInfo *II;
520 if (BufPtr && !Identifier.needsCleaning()) {
521 // No cleaning needed, just use the characters from the lexed buffer.
522 II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
523 } else {
524 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
Chris Lattnerc35717a2007-07-13 17:10:38 +0000525 llvm::SmallVector<char, 64> IdentifierBuffer;
526 IdentifierBuffer.resize(Identifier.getLength());
527 const char *TmpBuf = &IdentifierBuffer[0];
Reid Spencer5f016e22007-07-11 17:01:13 +0000528 unsigned Size = getSpelling(Identifier, TmpBuf);
529 II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
530 }
531 Identifier.setIdentifierInfo(II);
532 return II;
533}
534
535
536/// HandleIdentifier - This callback is invoked when the lexer reads an
537/// identifier. This callback looks up the identifier in the map and/or
538/// potentially macro expands it or turns it into a named token (like 'for').
Chris Lattnerd2177732007-07-20 16:59:19 +0000539void Preprocessor::HandleIdentifier(Token &Identifier) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 assert(Identifier.getIdentifierInfo() &&
541 "Can't handle identifiers without identifier info!");
542
543 IdentifierInfo &II = *Identifier.getIdentifierInfo();
544
545 // If this identifier was poisoned, and if it was not produced from a macro
546 // expansion, emit an error.
547 if (II.isPoisoned() && CurLexer) {
548 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
549 Diag(Identifier, diag::err_pp_used_poisoned_id);
550 else
551 Diag(Identifier, diag::ext_pp_bad_vaargs_use);
552 }
553
554 // If this is a macro to be expanded, do it.
Chris Lattnercc1a8752007-10-07 08:44:20 +0000555 if (MacroInfo *MI = getMacroInfo(&II)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
557 if (MI->isEnabled()) {
558 if (!HandleMacroExpandedIdentifier(Identifier, MI))
559 return;
560 } else {
561 // C99 6.10.3.4p2 says that a disabled macro may never again be
562 // expanded, even if it's in a context where it could be expanded in the
563 // future.
Chris Lattnerd2177732007-07-20 16:59:19 +0000564 Identifier.setFlag(Token::DisableExpand);
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 }
566 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 }
568
569 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
570 // then we act as if it is the actual operator and not the textual
571 // representation of it.
572 if (II.isCPlusPlusOperatorKeyword())
573 Identifier.setIdentifierInfo(0);
574
575 // Change the kind of this identifier to the appropriate token kind, e.g.
576 // turning "for" into a keyword.
577 Identifier.setKind(II.getTokenID());
578
579 // If this is an extension token, diagnose its use.
580 // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99
581 // For now, I'm just commenting it out (while I work on attributes).
582 if (II.isExtensionToken() && Features.C99)
583 Diag(Identifier, diag::ext_token_used);
584}