blob: 79b0d62edbe9b79f8b38a6649c0689a309cec138 [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 Lattnerb8761832006-06-24 21:31:03 +000031#include "clang/Lex/Pragma.h"
Chris Lattner0b8cfc22006-06-28 06:49:17 +000032#include "clang/Lex/ScratchBuffer.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000033#include "clang/Basic/Diagnostic.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000034#include "clang/Basic/SourceManager.h"
Chris Lattner81278c62006-10-14 19:03:49 +000035#include "clang/Basic/TargetInfo.h"
Chris Lattner7a4af3b2006-07-26 06:26:52 +000036#include "llvm/ADT/SmallVector.h"
Chris Lattner8a7003c2007-07-16 06:48:38 +000037#include "llvm/Support/MemoryBuffer.h"
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +000038#include "llvm/Support/Streams.h"
Chris Lattnera2633932007-09-03 18:30:32 +000039#include <ctime>
Chris Lattner22eb9722006-06-18 05:43:12 +000040using namespace clang;
41
42//===----------------------------------------------------------------------===//
43
Chris Lattner02dffbd2006-10-14 07:50:21 +000044Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
Chris Lattnerad7cdd32006-11-21 06:08:20 +000045 TargetInfo &target, SourceManager &SM,
Chris Lattner59a9ebd2006-10-18 05:34:33 +000046 HeaderSearch &Headers)
Chris Lattnerad7cdd32006-11-21 06:08:20 +000047 : Diags(diags), Features(opts), Target(target), FileMgr(Headers.getFileMgr()),
48 SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts),
Chris Lattner285c0c12008-03-09 02:26:03 +000049 CurLexer(0), CurDirLookup(0), CurTokenLexer(0), Callbacks(0) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +000050 ScratchBuf = new ScratchBuffer(SourceMgr);
Chris Lattnerc02c4ab2007-07-15 00:25:26 +000051
Chris Lattner22eb9722006-06-18 05:43:12 +000052 // Clear stats.
Chris Lattner59a9ebd2006-10-18 05:34:33 +000053 NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +000054 NumIf = NumElse = NumEndif = 0;
Chris Lattner78186052006-07-09 00:45:31 +000055 NumEnteredSourceFiles = 0;
56 NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
Chris Lattner510ab612006-07-20 04:47:30 +000057 NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
Chris Lattner59a9ebd2006-10-18 05:34:33 +000058 MaxIncludeStackDepth = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +000059 NumSkipped = 0;
Chris Lattnerb352e3e2006-11-21 06:17:10 +000060
61 // Default to discarding comments.
62 KeepComments = false;
63 KeepMacroComments = false;
64
Chris Lattner22eb9722006-06-18 05:43:12 +000065 // Macro expansion is enabled.
66 DisableMacroExpansion = false;
Chris Lattneree8760b2006-07-15 07:42:55 +000067 InMacroArgs = false;
Chris Lattner285c0c12008-03-09 02:26:03 +000068 NumCachedTokenLexers = 0;
Chris Lattner0c885f52006-06-21 06:50:18 +000069
Chris Lattner8ff71992006-07-06 05:17:39 +000070 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
71 // This gets unpoisoned where it is allowed.
72 (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
73
Chris Lattner1f1b0db2007-10-09 22:10:18 +000074 Predefines = 0;
75
Chris Lattnerb8761832006-06-24 21:31:03 +000076 // Initialize the pragma handlers.
77 PragmaHandlers = new PragmaNamespace(0);
78 RegisterBuiltinPragmas();
Chris Lattner677757a2006-06-28 05:26:32 +000079
80 // Initialize builtin macros like __LINE__ and friends.
81 RegisterBuiltinMacros();
Chris Lattner22eb9722006-06-18 05:43:12 +000082}
83
84Preprocessor::~Preprocessor() {
85 // Free any active lexers.
86 delete CurLexer;
87
Chris Lattner69772b02006-07-02 20:34:39 +000088 while (!IncludeMacroStack.empty()) {
89 delete IncludeMacroStack.back().TheLexer;
Chris Lattner285c0c12008-03-09 02:26:03 +000090 delete IncludeMacroStack.back().TheTokenLexer;
Chris Lattner69772b02006-07-02 20:34:39 +000091 IncludeMacroStack.pop_back();
Chris Lattner22eb9722006-06-18 05:43:12 +000092 }
Chris Lattnerc43ddc82007-10-07 08:44:20 +000093
94 // Free any macro definitions.
95 for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
96 Macros.begin(), E = Macros.end(); I != E; ++I) {
97 // Free the macro definition.
98 delete I->second;
99 I->second = 0;
100 I->first->setHasMacroDefinition(false);
101 }
Chris Lattnerb8761832006-06-24 21:31:03 +0000102
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000103 // Free any cached macro expanders.
Chris Lattner285c0c12008-03-09 02:26:03 +0000104 for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i)
105 delete TokenLexerCache[i];
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000106
Chris Lattnerb8761832006-06-24 21:31:03 +0000107 // Release pragma information.
108 delete PragmaHandlers;
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000109
110 // Delete the scratch buffer info.
111 delete ScratchBuf;
Chris Lattner22eb9722006-06-18 05:43:12 +0000112}
113
Chris Lattner22eb9722006-06-18 05:43:12 +0000114/// Diag - Forwarding function for diagnostics. This emits a diagnostic at
Chris Lattner146762e2007-07-20 16:59:19 +0000115/// the specified Token's location, translating the token's start
Chris Lattner22eb9722006-06-18 05:43:12 +0000116/// position in the current buffer into a SourcePosition object for rendering.
Chris Lattner36982e42007-05-16 17:49:37 +0000117void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000118 Diags.Report(getFullLoc(Loc), DiagID);
Chris Lattner36982e42007-05-16 17:49:37 +0000119}
120
Chris Lattnercb283342006-06-18 06:48:37 +0000121void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000122 const std::string &Msg) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000123 Diags.Report(getFullLoc(Loc), DiagID, &Msg, 1);
Chris Lattner22eb9722006-06-18 05:43:12 +0000124}
Chris Lattnerd01e2912006-06-18 16:22:51 +0000125
Chris Lattner146762e2007-07-20 16:59:19 +0000126void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000127 llvm::cerr << tok::getTokenName(Tok.getKind()) << " '"
128 << getSpelling(Tok) << "'";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000129
130 if (!DumpFlags) return;
Chris Lattner615315f2007-12-09 20:31:55 +0000131
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000132 llvm::cerr << "\t";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000133 if (Tok.isAtStartOfLine())
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000134 llvm::cerr << " [StartOfLine]";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000135 if (Tok.hasLeadingSpace())
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000136 llvm::cerr << " [LeadingSpace]";
Chris Lattner6e4bf522006-07-27 06:59:25 +0000137 if (Tok.isExpandDisabled())
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000138 llvm::cerr << " [ExpandDisabled]";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000139 if (Tok.needsCleaning()) {
Chris Lattner50b497e2006-06-18 16:32:35 +0000140 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000141 llvm::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
142 << "']";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000143 }
Chris Lattner615315f2007-12-09 20:31:55 +0000144
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000145 llvm::cerr << "\tLoc=<";
Chris Lattner615315f2007-12-09 20:31:55 +0000146 DumpLocation(Tok.getLocation());
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000147 llvm::cerr << ">";
Chris Lattner615315f2007-12-09 20:31:55 +0000148}
149
150void Preprocessor::DumpLocation(SourceLocation Loc) const {
151 SourceLocation LogLoc = SourceMgr.getLogicalLoc(Loc);
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000152 llvm::cerr << SourceMgr.getSourceName(LogLoc) << ':'
153 << SourceMgr.getLineNumber(LogLoc) << ':'
154 << SourceMgr.getLineNumber(LogLoc);
Chris Lattner615315f2007-12-09 20:31:55 +0000155
156 SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(Loc);
157 if (PhysLoc != LogLoc) {
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000158 llvm::cerr << " <PhysLoc=";
Chris Lattner615315f2007-12-09 20:31:55 +0000159 DumpLocation(PhysLoc);
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000160 llvm::cerr << ">";
Chris Lattner615315f2007-12-09 20:31:55 +0000161 }
Chris Lattnerd01e2912006-06-18 16:22:51 +0000162}
163
164void Preprocessor::DumpMacro(const MacroInfo &MI) const {
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000165 llvm::cerr << "MACRO: ";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000166 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
167 DumpToken(MI.getReplacementToken(i));
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000168 llvm::cerr << " ";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000169 }
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000170 llvm::cerr << "\n";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000171}
172
Chris Lattner22eb9722006-06-18 05:43:12 +0000173void Preprocessor::PrintStats() {
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000174 llvm::cerr << "\n*** Preprocessor Stats:\n";
175 llvm::cerr << NumDirectives << " directives found:\n";
176 llvm::cerr << " " << NumDefined << " #define.\n";
177 llvm::cerr << " " << NumUndefined << " #undef.\n";
178 llvm::cerr << " #include/#include_next/#import:\n";
179 llvm::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
180 llvm::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
181 llvm::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
182 llvm::cerr << " " << NumElse << " #else/#elif.\n";
183 llvm::cerr << " " << NumEndif << " #endif.\n";
184 llvm::cerr << " " << NumPragma << " #pragma.\n";
185 llvm::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000186
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000187 llvm::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
188 << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
189 << NumFastMacroExpanded << " on the fast path.\n";
190 llvm::cerr << (NumFastTokenPaste+NumTokenPaste)
191 << " token paste (##) operations performed, "
192 << NumFastTokenPaste << " on the fast path.\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000193}
194
195//===----------------------------------------------------------------------===//
Chris Lattnerd01e2912006-06-18 16:22:51 +0000196// Token Spelling
197//===----------------------------------------------------------------------===//
198
199
200/// getSpelling() - Return the 'spelling' of this token. The spelling of a
201/// token are the characters used to represent the token in the source file
202/// after trigraph expansion and escaped-newline folding. In particular, this
203/// wants to get the true, uncanonicalized, spelling of things like digraphs
204/// UCNs, etc.
Chris Lattner146762e2007-07-20 16:59:19 +0000205std::string Preprocessor::getSpelling(const Token &Tok) const {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000206 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
207
208 // If this token contains nothing interesting, return it directly.
Chris Lattner50b497e2006-06-18 16:32:35 +0000209 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000210 if (!Tok.needsCleaning())
211 return std::string(TokStart, TokStart+Tok.getLength());
212
Chris Lattnerd01e2912006-06-18 16:22:51 +0000213 std::string Result;
214 Result.reserve(Tok.getLength());
215
Chris Lattneref9eae12006-07-04 22:33:12 +0000216 // Otherwise, hard case, relex the characters into the string.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000217 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
218 Ptr != End; ) {
219 unsigned CharSize;
220 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
221 Ptr += CharSize;
222 }
223 assert(Result.size() != unsigned(Tok.getLength()) &&
224 "NeedsCleaning flag set on something that didn't need cleaning!");
225 return Result;
226}
227
228/// getSpelling - This method is used to get the spelling of a token into a
229/// preallocated buffer, instead of as an std::string. The caller is required
230/// to allocate enough space for the token, which is guaranteed to be at least
231/// Tok.getLength() bytes long. The actual length of the token is returned.
Chris Lattneref9eae12006-07-04 22:33:12 +0000232///
233/// Note that this method may do two possible things: it may either fill in
234/// the buffer specified with characters, or it may *change the input pointer*
235/// to point to a constant buffer with the data already in it (avoiding a
236/// copy). The caller is not allowed to modify the returned buffer pointer
237/// if an internal buffer is returned.
Chris Lattner146762e2007-07-20 16:59:19 +0000238unsigned Preprocessor::getSpelling(const Token &Tok,
Chris Lattneref9eae12006-07-04 22:33:12 +0000239 const char *&Buffer) const {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000240 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
241
Chris Lattnerd3a15f72006-07-04 23:01:03 +0000242 // If this token is an identifier, just return the string from the identifier
243 // table, which is very quick.
244 if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
245 Buffer = II->getName();
Chris Lattner32e6d642007-07-22 22:50:09 +0000246
247 // Return the length of the token. If the token needed cleaning, don't
248 // include the size of the newlines or trigraphs in it.
249 if (!Tok.needsCleaning())
250 return Tok.getLength();
251 else
252 return strlen(Buffer);
Chris Lattnerd3a15f72006-07-04 23:01:03 +0000253 }
254
255 // Otherwise, compute the start of the token in the input lexer buffer.
Chris Lattner50b497e2006-06-18 16:32:35 +0000256 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000257
258 // If this token contains nothing interesting, return it directly.
259 if (!Tok.needsCleaning()) {
Chris Lattneref9eae12006-07-04 22:33:12 +0000260 Buffer = TokStart;
261 return Tok.getLength();
Chris Lattnerd01e2912006-06-18 16:22:51 +0000262 }
263 // Otherwise, hard case, relex the characters into the string.
Chris Lattneref9eae12006-07-04 22:33:12 +0000264 char *OutBuf = const_cast<char*>(Buffer);
Chris Lattnerd01e2912006-06-18 16:22:51 +0000265 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
266 Ptr != End; ) {
267 unsigned CharSize;
268 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
269 Ptr += CharSize;
270 }
271 assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
272 "NeedsCleaning flag set on something that didn't need cleaning!");
273
274 return OutBuf-Buffer;
275}
276
Chris Lattnerb94ec7b2006-07-14 06:54:10 +0000277
278/// CreateString - Plop the specified string into a scratch buffer and return a
279/// location for it. If specified, the source location provides a source
280/// location for the token.
281SourceLocation Preprocessor::
282CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) {
283 if (SLoc.isValid())
284 return ScratchBuf->getToken(Buf, Len, SLoc);
285 return ScratchBuf->getToken(Buf, Len);
286}
287
288
Chris Lattner8a7003c2007-07-16 06:48:38 +0000289/// AdvanceToTokenCharacter - Given a location that specifies the start of a
290/// token, return a new location that specifies a character within the token.
291SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart,
292 unsigned CharNo) {
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000293 // If they request the first char of the token, we're trivially done. If this
294 // is a macro expansion, it doesn't make sense to point to a character within
295 // the instantiation point (the name). We could point to the source
296 // character, but without also pointing to instantiation info, this is
297 // confusing.
298 if (CharNo == 0 || TokStart.isMacroID()) return TokStart;
Chris Lattner8a7003c2007-07-16 06:48:38 +0000299
300 // Figure out how many physical characters away the specified logical
301 // character is. This needs to take into consideration newlines and
302 // trigraphs.
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000303 const char *TokPtr = SourceMgr.getCharacterData(TokStart);
304 unsigned PhysOffset = 0;
Chris Lattner8a7003c2007-07-16 06:48:38 +0000305
306 // The usual case is that tokens don't contain anything interesting. Skip
307 // over the uninteresting characters. If a token only consists of simple
308 // chars, this method is extremely fast.
309 while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr))
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000310 ++TokPtr, --CharNo, ++PhysOffset;
Chris Lattner8a7003c2007-07-16 06:48:38 +0000311
312 // If we have a character that may be a trigraph or escaped newline, create a
313 // lexer to parse it correctly.
Chris Lattner8a7003c2007-07-16 06:48:38 +0000314 if (CharNo != 0) {
315 // Create a lexer starting at this token position.
Chris Lattner77e9de52007-07-20 16:52:03 +0000316 Lexer TheLexer(TokStart, *this, TokPtr);
Chris Lattner146762e2007-07-20 16:59:19 +0000317 Token Tok;
Chris Lattner8a7003c2007-07-16 06:48:38 +0000318 // Skip over characters the remaining characters.
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000319 const char *TokStartPtr = TokPtr;
Chris Lattner8a7003c2007-07-16 06:48:38 +0000320 for (; CharNo; --CharNo)
321 TheLexer.getAndAdvanceChar(TokPtr, Tok);
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000322
323 PhysOffset += TokPtr-TokStartPtr;
Chris Lattner8a7003c2007-07-16 06:48:38 +0000324 }
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000325
326 return TokStart.getFileLocWithOffset(PhysOffset);
Chris Lattner8a7003c2007-07-16 06:48:38 +0000327}
328
329
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000330//===----------------------------------------------------------------------===//
331// Preprocessor Initialization Methods
332//===----------------------------------------------------------------------===//
333
334// Append a #define line to Buf for Macro. Macro should be of the form XXX,
335// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
336// "#define XXX Y z W". To get a #define with no value, use "XXX=".
337static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
338 const char *Command = "#define ") {
339 Buf.insert(Buf.end(), Command, Command+strlen(Command));
340 if (const char *Equal = strchr(Macro, '=')) {
341 // Turn the = into ' '.
342 Buf.insert(Buf.end(), Macro, Equal);
343 Buf.push_back(' ');
344 Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal));
345 } else {
346 // Push "macroname 1".
347 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
348 Buf.push_back(' ');
349 Buf.push_back('1');
350 }
351 Buf.push_back('\n');
352}
353
354
355static void InitializePredefinedMacros(Preprocessor &PP,
356 std::vector<char> &Buf) {
357 // FIXME: Implement magic like cpp_init_builtins for things like __STDC__
358 // and __DATE__ etc.
359#if 0
360 /* __STDC__ has the value 1 under normal circumstances.
361 However, if (a) we are in a system header, (b) the option
362 stdc_0_in_system_headers is true (set by target config), and
363 (c) we are not in strictly conforming mode, then it has the
364 value 0. (b) and (c) are already checked in cpp_init_builtins. */
365 //case BT_STDC:
366 if (cpp_in_system_header (pfile))
367 number = 0;
368 else
369 number = 1;
370 break;
371#endif
372 // These should all be defined in the preprocessor according to the
373 // current language configuration.
374 DefineBuiltinMacro(Buf, "__STDC__=1");
375 //DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
376 if (PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus)
377 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
378 else if (0) // STDC94 ?
379 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
380
381 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");
382 if (PP.getLangOptions().ObjC1)
383 DefineBuiltinMacro(Buf, "__OBJC__=1");
384 if (PP.getLangOptions().ObjC2)
385 DefineBuiltinMacro(Buf, "__OBJC2__=1");
Steve Naroff6d40db02007-10-31 18:42:27 +0000386
Chris Lattnered2a9eb2007-10-10 17:48:53 +0000387 // Add __builtin_va_list typedef.
388 {
389 const char *VAList = PP.getTargetInfo().getVAListDeclaration();
390 Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
391 Buf.push_back('\n');
392 }
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000393
394 // Get the target #defines.
395 PP.getTargetInfo().getTargetDefines(Buf);
396
397 // Compiler set macros.
398 DefineBuiltinMacro(Buf, "__APPLE_CC__=5250");
Steve Naroff68754c52007-11-10 18:06:36 +0000399 DefineBuiltinMacro(Buf, "__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=1050");
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000400 DefineBuiltinMacro(Buf, "__GNUC_MINOR__=0");
401 DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
402 DefineBuiltinMacro(Buf, "__GNUC__=4");
403 DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
404 DefineBuiltinMacro(Buf, "__VERSION__=\"4.0.1 (Apple Computer, Inc. "
405 "build 5250)\"");
406
407 // Build configuration options.
408 DefineBuiltinMacro(Buf, "__DYNAMIC__=1");
409 DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
410 DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
411 DefineBuiltinMacro(Buf, "__PIC__=1");
412
413
414 if (PP.getLangOptions().CPlusPlus) {
415 DefineBuiltinMacro(Buf, "__DEPRECATED=1");
416 DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
417 DefineBuiltinMacro(Buf, "__GNUG__=4");
418 DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
419 DefineBuiltinMacro(Buf, "__cplusplus=1");
420 DefineBuiltinMacro(Buf, "__private_extern__=extern");
421 }
Steve Naroffb2c80c72008-02-07 03:50:06 +0000422 if (PP.getLangOptions().Microsoft) {
423 DefineBuiltinMacro(Buf, "__stdcall=");
424 DefineBuiltinMacro(Buf, "__cdecl=");
425 DefineBuiltinMacro(Buf, "_cdecl=");
426 DefineBuiltinMacro(Buf, "__ptr64=");
Steve Naroff4e79d342008-02-07 23:24:32 +0000427 DefineBuiltinMacro(Buf, "__w64=");
Steve Naroffb2c80c72008-02-07 03:50:06 +0000428 DefineBuiltinMacro(Buf, "__forceinline=");
Steve Naroff6936a082008-02-07 15:26:07 +0000429 DefineBuiltinMacro(Buf, "__int8=char");
430 DefineBuiltinMacro(Buf, "__int16=short");
431 DefineBuiltinMacro(Buf, "__int32=int");
Chris Lattner00c5b282008-02-10 21:12:45 +0000432 DefineBuiltinMacro(Buf, "__int64=long long");
Steve Naroff5915777f2008-02-11 22:29:58 +0000433 DefineBuiltinMacro(Buf, "__declspec(X)=");
Steve Naroffb2c80c72008-02-07 03:50:06 +0000434 }
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000435 // FIXME: Should emit a #line directive here.
436}
437
438
439/// EnterMainSourceFile - Enter the specified FileID as the main source file,
Nate Begemanf7c3ff62008-01-07 04:01:26 +0000440/// which implicitly adds the builtin defines etc.
Ted Kremenek230bd912007-12-19 22:51:13 +0000441void Preprocessor::EnterMainSourceFile() {
442
443 unsigned MainFileID = SourceMgr.getMainFileID();
444
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000445 // Enter the main file source buffer.
446 EnterSourceFile(MainFileID, 0);
447
Chris Lattner609d4132007-11-15 19:07:47 +0000448 // Tell the header info that the main file was entered. If the file is later
449 // #imported, it won't be re-entered.
450 if (const FileEntry *FE =
451 SourceMgr.getFileEntryForLoc(SourceLocation::getFileLoc(MainFileID, 0)))
452 HeaderInfo.IncrementIncludeCount(FE);
453
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000454 std::vector<char> PrologFile;
455 PrologFile.reserve(4080);
456
457 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
458 InitializePredefinedMacros(*this, PrologFile);
459
460 // Add on the predefines from the driver.
461 PrologFile.insert(PrologFile.end(), Predefines,Predefines+strlen(Predefines));
462
463 // Memory buffer must end with a null byte!
464 PrologFile.push_back(0);
465
466 // Now that we have emitted the predefined macros, #includes, etc into
467 // PrologFile, preprocess it to populate the initial preprocessor state.
468 llvm::MemoryBuffer *SB =
469 llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(),
470 "<predefines>");
471 assert(SB && "Cannot fail to create predefined source buffer");
472 unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB);
473 assert(FileID && "Could not create FileID for predefines?");
474
475 // Start parsing the predefines.
476 EnterSourceFile(FileID, 0);
477}
Chris Lattner8a7003c2007-07-16 06:48:38 +0000478
Chris Lattner677757a2006-06-28 05:26:32 +0000479
480//===----------------------------------------------------------------------===//
481// Lexer Event Handling.
482//===----------------------------------------------------------------------===//
483
Chris Lattnercefc7682006-07-08 08:28:12 +0000484/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
485/// identifier information for the token and install it into the token.
Chris Lattner146762e2007-07-20 16:59:19 +0000486IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
Chris Lattnercefc7682006-07-08 08:28:12 +0000487 const char *BufPtr) {
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000488 assert(Identifier.is(tok::identifier) && "Not an identifier!");
Chris Lattnercefc7682006-07-08 08:28:12 +0000489 assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
490
491 // Look up this token, see if it is a macro, or if it is a language keyword.
492 IdentifierInfo *II;
493 if (BufPtr && !Identifier.needsCleaning()) {
494 // No cleaning needed, just use the characters from the lexed buffer.
495 II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
496 } else {
497 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
Chris Lattnerf9aba2c2007-07-13 17:10:38 +0000498 llvm::SmallVector<char, 64> IdentifierBuffer;
499 IdentifierBuffer.resize(Identifier.getLength());
500 const char *TmpBuf = &IdentifierBuffer[0];
Chris Lattnercefc7682006-07-08 08:28:12 +0000501 unsigned Size = getSpelling(Identifier, TmpBuf);
502 II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
503 }
Chris Lattner8c204872006-10-14 05:19:21 +0000504 Identifier.setIdentifierInfo(II);
Chris Lattnercefc7682006-07-08 08:28:12 +0000505 return II;
506}
507
508
Chris Lattner677757a2006-06-28 05:26:32 +0000509/// HandleIdentifier - This callback is invoked when the lexer reads an
510/// identifier. This callback looks up the identifier in the map and/or
511/// potentially macro expands it or turns it into a named token (like 'for').
Chris Lattner146762e2007-07-20 16:59:19 +0000512void Preprocessor::HandleIdentifier(Token &Identifier) {
Chris Lattner0f1f5052006-07-20 04:16:23 +0000513 assert(Identifier.getIdentifierInfo() &&
514 "Can't handle identifiers without identifier info!");
515
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000516 IdentifierInfo &II = *Identifier.getIdentifierInfo();
Chris Lattner677757a2006-06-28 05:26:32 +0000517
518 // If this identifier was poisoned, and if it was not produced from a macro
519 // expansion, emit an error.
Chris Lattner8ff71992006-07-06 05:17:39 +0000520 if (II.isPoisoned() && CurLexer) {
521 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
522 Diag(Identifier, diag::err_pp_used_poisoned_id);
523 else
524 Diag(Identifier, diag::ext_pp_bad_vaargs_use);
525 }
Chris Lattner677757a2006-06-28 05:26:32 +0000526
Chris Lattner78186052006-07-09 00:45:31 +0000527 // If this is a macro to be expanded, do it.
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000528 if (MacroInfo *MI = getMacroInfo(&II)) {
Chris Lattner6e4bf522006-07-27 06:59:25 +0000529 if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
530 if (MI->isEnabled()) {
531 if (!HandleMacroExpandedIdentifier(Identifier, MI))
532 return;
533 } else {
534 // C99 6.10.3.4p2 says that a disabled macro may never again be
535 // expanded, even if it's in a context where it could be expanded in the
536 // future.
Chris Lattner146762e2007-07-20 16:59:19 +0000537 Identifier.setFlag(Token::DisableExpand);
Chris Lattner6e4bf522006-07-27 06:59:25 +0000538 }
539 }
Chris Lattner063400e2006-10-14 19:54:15 +0000540 }
Chris Lattner677757a2006-06-28 05:26:32 +0000541
Chris Lattner5b9f4892006-11-21 17:23:33 +0000542 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
543 // then we act as if it is the actual operator and not the textual
544 // representation of it.
545 if (II.isCPlusPlusOperatorKeyword())
546 Identifier.setIdentifierInfo(0);
547
Chris Lattner677757a2006-06-28 05:26:32 +0000548 // Change the kind of this identifier to the appropriate token kind, e.g.
549 // turning "for" into a keyword.
Chris Lattner8c204872006-10-14 05:19:21 +0000550 Identifier.setKind(II.getTokenID());
Chris Lattner677757a2006-06-28 05:26:32 +0000551
552 // If this is an extension token, diagnose its use.
Steve Naroffa8fd9732007-06-11 00:35:03 +0000553 // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99
554 // For now, I'm just commenting it out (while I work on attributes).
Chris Lattner53621a52007-06-13 20:44:40 +0000555 if (II.isExtensionToken() && Features.C99)
556 Diag(Identifier, diag::ext_token_used);
Chris Lattner677757a2006-06-28 05:26:32 +0000557}
558