blob: 86156a07728ed8c3809aa4b2b26ffef53826d5f9 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner4b009652007-07-25 00:24:17 +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"
Chris Lattner4b009652007-07-25 00:24:17 +000031#include "clang/Lex/Pragma.h"
32#include "clang/Lex/ScratchBuffer.h"
33#include "clang/Basic/Diagnostic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000034#include "clang/Basic/SourceManager.h"
35#include "clang/Basic/TargetInfo.h"
36#include "llvm/ADT/SmallVector.h"
37#include "llvm/Support/MemoryBuffer.h"
Ted Kremenekce4c64e2008-01-14 16:44:48 +000038#include "llvm/Support/Streams.h"
Chris Lattner1b023182007-09-03 18:30:32 +000039#include <ctime>
Chris Lattner4b009652007-07-25 00:24:17 +000040using namespace clang;
41
42//===----------------------------------------------------------------------===//
43
44Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
45 TargetInfo &target, SourceManager &SM,
46 HeaderSearch &Headers)
47 : Diags(diags), Features(opts), Target(target), FileMgr(Headers.getFileMgr()),
48 SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts),
Chris Lattner5b54ed92008-03-09 02:26:03 +000049 CurLexer(0), CurDirLookup(0), CurTokenLexer(0), Callbacks(0) {
Chris Lattner4b009652007-07-25 00:24:17 +000050 ScratchBuf = new ScratchBuffer(SourceMgr);
51
52 // Clear stats.
53 NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
54 NumIf = NumElse = NumEndif = 0;
55 NumEnteredSourceFiles = 0;
56 NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
57 NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
58 MaxIncludeStackDepth = 0;
59 NumSkipped = 0;
60
61 // Default to discarding comments.
62 KeepComments = false;
63 KeepMacroComments = false;
64
65 // Macro expansion is enabled.
66 DisableMacroExpansion = false;
67 InMacroArgs = false;
Chris Lattner5b54ed92008-03-09 02:26:03 +000068 NumCachedTokenLexers = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000069
70 // "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 Lattnerd1f21e12007-10-09 22:10:18 +000074 Predefines = 0;
75
Chris Lattner4b009652007-07-25 00:24:17 +000076 // Initialize the pragma handlers.
77 PragmaHandlers = new PragmaNamespace(0);
78 RegisterBuiltinPragmas();
79
80 // Initialize builtin macros like __LINE__ and friends.
81 RegisterBuiltinMacros();
82}
83
84Preprocessor::~Preprocessor() {
85 // Free any active lexers.
86 delete CurLexer;
87
88 while (!IncludeMacroStack.empty()) {
89 delete IncludeMacroStack.back().TheLexer;
Chris Lattner5b54ed92008-03-09 02:26:03 +000090 delete IncludeMacroStack.back().TheTokenLexer;
Chris Lattner4b009652007-07-25 00:24:17 +000091 IncludeMacroStack.pop_back();
92 }
Chris Lattner7a1b0882007-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 Lattner4b009652007-07-25 00:24:17 +0000102
103 // Free any cached macro expanders.
Chris Lattner5b54ed92008-03-09 02:26:03 +0000104 for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i)
105 delete TokenLexerCache[i];
Chris Lattner4b009652007-07-25 00:24:17 +0000106
107 // Release pragma information.
108 delete PragmaHandlers;
109
110 // Delete the scratch buffer info.
111 delete ScratchBuf;
Chris Lattner65829812008-03-14 06:07:05 +0000112
113 delete Callbacks;
Chris Lattner4b009652007-07-25 00:24:17 +0000114}
115
Chris Lattner4b009652007-07-25 00:24:17 +0000116/// Diag - Forwarding function for diagnostics. This emits a diagnostic at
117/// the specified Token's location, translating the token's start
118/// position in the current buffer into a SourcePosition object for rendering.
119void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000120 Diags.Report(getFullLoc(Loc), DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000121}
122
123void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
124 const std::string &Msg) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000125 Diags.Report(getFullLoc(Loc), DiagID, &Msg, 1);
Chris Lattner4b009652007-07-25 00:24:17 +0000126}
127
128void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000129 llvm::cerr << tok::getTokenName(Tok.getKind()) << " '"
130 << getSpelling(Tok) << "'";
Chris Lattner4b009652007-07-25 00:24:17 +0000131
132 if (!DumpFlags) return;
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000133
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000134 llvm::cerr << "\t";
Chris Lattner4b009652007-07-25 00:24:17 +0000135 if (Tok.isAtStartOfLine())
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000136 llvm::cerr << " [StartOfLine]";
Chris Lattner4b009652007-07-25 00:24:17 +0000137 if (Tok.hasLeadingSpace())
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000138 llvm::cerr << " [LeadingSpace]";
Chris Lattner4b009652007-07-25 00:24:17 +0000139 if (Tok.isExpandDisabled())
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000140 llvm::cerr << " [ExpandDisabled]";
Chris Lattner4b009652007-07-25 00:24:17 +0000141 if (Tok.needsCleaning()) {
142 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000143 llvm::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
144 << "']";
Chris Lattner4b009652007-07-25 00:24:17 +0000145 }
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000146
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000147 llvm::cerr << "\tLoc=<";
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000148 DumpLocation(Tok.getLocation());
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000149 llvm::cerr << ">";
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000150}
151
152void Preprocessor::DumpLocation(SourceLocation Loc) const {
153 SourceLocation LogLoc = SourceMgr.getLogicalLoc(Loc);
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000154 llvm::cerr << SourceMgr.getSourceName(LogLoc) << ':'
155 << SourceMgr.getLineNumber(LogLoc) << ':'
156 << SourceMgr.getLineNumber(LogLoc);
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000157
158 SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(Loc);
159 if (PhysLoc != LogLoc) {
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000160 llvm::cerr << " <PhysLoc=";
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000161 DumpLocation(PhysLoc);
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000162 llvm::cerr << ">";
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000163 }
Chris Lattner4b009652007-07-25 00:24:17 +0000164}
165
166void Preprocessor::DumpMacro(const MacroInfo &MI) const {
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000167 llvm::cerr << "MACRO: ";
Chris Lattner4b009652007-07-25 00:24:17 +0000168 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
169 DumpToken(MI.getReplacementToken(i));
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000170 llvm::cerr << " ";
Chris Lattner4b009652007-07-25 00:24:17 +0000171 }
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000172 llvm::cerr << "\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000173}
174
175void Preprocessor::PrintStats() {
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000176 llvm::cerr << "\n*** Preprocessor Stats:\n";
177 llvm::cerr << NumDirectives << " directives found:\n";
178 llvm::cerr << " " << NumDefined << " #define.\n";
179 llvm::cerr << " " << NumUndefined << " #undef.\n";
180 llvm::cerr << " #include/#include_next/#import:\n";
181 llvm::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
182 llvm::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
183 llvm::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
184 llvm::cerr << " " << NumElse << " #else/#elif.\n";
185 llvm::cerr << " " << NumEndif << " #endif.\n";
186 llvm::cerr << " " << NumPragma << " #pragma.\n";
187 llvm::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000188
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000189 llvm::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
190 << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
191 << NumFastMacroExpanded << " on the fast path.\n";
192 llvm::cerr << (NumFastTokenPaste+NumTokenPaste)
193 << " token paste (##) operations performed, "
194 << NumFastTokenPaste << " on the fast path.\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000195}
196
197//===----------------------------------------------------------------------===//
198// Token Spelling
199//===----------------------------------------------------------------------===//
200
201
202/// getSpelling() - Return the 'spelling' of this token. The spelling of a
203/// token are the characters used to represent the token in the source file
204/// after trigraph expansion and escaped-newline folding. In particular, this
205/// wants to get the true, uncanonicalized, spelling of things like digraphs
206/// UCNs, etc.
207std::string Preprocessor::getSpelling(const Token &Tok) const {
208 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
209
210 // If this token contains nothing interesting, return it directly.
211 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
212 if (!Tok.needsCleaning())
213 return std::string(TokStart, TokStart+Tok.getLength());
214
215 std::string Result;
216 Result.reserve(Tok.getLength());
217
218 // Otherwise, hard case, relex the characters into the string.
219 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
220 Ptr != End; ) {
221 unsigned CharSize;
222 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
223 Ptr += CharSize;
224 }
225 assert(Result.size() != unsigned(Tok.getLength()) &&
226 "NeedsCleaning flag set on something that didn't need cleaning!");
227 return Result;
228}
229
230/// getSpelling - This method is used to get the spelling of a token into a
231/// preallocated buffer, instead of as an std::string. The caller is required
232/// to allocate enough space for the token, which is guaranteed to be at least
233/// Tok.getLength() bytes long. The actual length of the token is returned.
234///
235/// Note that this method may do two possible things: it may either fill in
236/// the buffer specified with characters, or it may *change the input pointer*
237/// to point to a constant buffer with the data already in it (avoiding a
238/// copy). The caller is not allowed to modify the returned buffer pointer
239/// if an internal buffer is returned.
240unsigned Preprocessor::getSpelling(const Token &Tok,
241 const char *&Buffer) const {
242 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
243
244 // If this token is an identifier, just return the string from the identifier
245 // table, which is very quick.
246 if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
247 Buffer = II->getName();
248
249 // Return the length of the token. If the token needed cleaning, don't
250 // include the size of the newlines or trigraphs in it.
251 if (!Tok.needsCleaning())
252 return Tok.getLength();
253 else
254 return strlen(Buffer);
255 }
256
257 // Otherwise, compute the start of the token in the input lexer buffer.
258 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
259
260 // If this token contains nothing interesting, return it directly.
261 if (!Tok.needsCleaning()) {
262 Buffer = TokStart;
263 return Tok.getLength();
264 }
265 // Otherwise, hard case, relex the characters into the string.
266 char *OutBuf = const_cast<char*>(Buffer);
267 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
268 Ptr != End; ) {
269 unsigned CharSize;
270 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
271 Ptr += CharSize;
272 }
273 assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
274 "NeedsCleaning flag set on something that didn't need cleaning!");
275
276 return OutBuf-Buffer;
277}
278
279
280/// CreateString - Plop the specified string into a scratch buffer and return a
281/// location for it. If specified, the source location provides a source
282/// location for the token.
283SourceLocation Preprocessor::
284CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) {
285 if (SLoc.isValid())
286 return ScratchBuf->getToken(Buf, Len, SLoc);
287 return ScratchBuf->getToken(Buf, Len);
288}
289
290
291/// AdvanceToTokenCharacter - Given a location that specifies the start of a
292/// token, return a new location that specifies a character within the token.
293SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart,
294 unsigned CharNo) {
295 // If they request the first char of the token, we're trivially done. If this
296 // is a macro expansion, it doesn't make sense to point to a character within
297 // the instantiation point (the name). We could point to the source
298 // character, but without also pointing to instantiation info, this is
299 // confusing.
300 if (CharNo == 0 || TokStart.isMacroID()) return TokStart;
301
302 // Figure out how many physical characters away the specified logical
303 // character is. This needs to take into consideration newlines and
304 // trigraphs.
305 const char *TokPtr = SourceMgr.getCharacterData(TokStart);
306 unsigned PhysOffset = 0;
307
308 // The usual case is that tokens don't contain anything interesting. Skip
309 // over the uninteresting characters. If a token only consists of simple
310 // chars, this method is extremely fast.
311 while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr))
312 ++TokPtr, --CharNo, ++PhysOffset;
313
314 // If we have a character that may be a trigraph or escaped newline, create a
315 // lexer to parse it correctly.
316 if (CharNo != 0) {
317 // Create a lexer starting at this token position.
318 Lexer TheLexer(TokStart, *this, TokPtr);
319 Token Tok;
320 // Skip over characters the remaining characters.
321 const char *TokStartPtr = TokPtr;
322 for (; CharNo; --CharNo)
323 TheLexer.getAndAdvanceChar(TokPtr, Tok);
324
325 PhysOffset += TokPtr-TokStartPtr;
326 }
327
328 return TokStart.getFileLocWithOffset(PhysOffset);
329}
330
331
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000332//===----------------------------------------------------------------------===//
333// Preprocessor Initialization Methods
334//===----------------------------------------------------------------------===//
335
336// Append a #define line to Buf for Macro. Macro should be of the form XXX,
337// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
338// "#define XXX Y z W". To get a #define with no value, use "XXX=".
339static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
340 const char *Command = "#define ") {
341 Buf.insert(Buf.end(), Command, Command+strlen(Command));
342 if (const char *Equal = strchr(Macro, '=')) {
343 // Turn the = into ' '.
344 Buf.insert(Buf.end(), Macro, Equal);
345 Buf.push_back(' ');
346 Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal));
347 } else {
348 // Push "macroname 1".
349 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
350 Buf.push_back(' ');
351 Buf.push_back('1');
352 }
353 Buf.push_back('\n');
354}
355
356
357static void InitializePredefinedMacros(Preprocessor &PP,
358 std::vector<char> &Buf) {
359 // FIXME: Implement magic like cpp_init_builtins for things like __STDC__
360 // and __DATE__ etc.
361#if 0
362 /* __STDC__ has the value 1 under normal circumstances.
363 However, if (a) we are in a system header, (b) the option
364 stdc_0_in_system_headers is true (set by target config), and
365 (c) we are not in strictly conforming mode, then it has the
366 value 0. (b) and (c) are already checked in cpp_init_builtins. */
367 //case BT_STDC:
368 if (cpp_in_system_header (pfile))
369 number = 0;
370 else
371 number = 1;
372 break;
373#endif
374 // These should all be defined in the preprocessor according to the
375 // current language configuration.
376 DefineBuiltinMacro(Buf, "__STDC__=1");
377 //DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
378 if (PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus)
379 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
380 else if (0) // STDC94 ?
381 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
382
383 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");
384 if (PP.getLangOptions().ObjC1)
385 DefineBuiltinMacro(Buf, "__OBJC__=1");
386 if (PP.getLangOptions().ObjC2)
387 DefineBuiltinMacro(Buf, "__OBJC2__=1");
Steve Naroffae84af82007-10-31 18:42:27 +0000388
Chris Lattner77cec472007-10-10 17:48:53 +0000389 // Add __builtin_va_list typedef.
390 {
391 const char *VAList = PP.getTargetInfo().getVAListDeclaration();
392 Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
393 Buf.push_back('\n');
394 }
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000395
396 // Get the target #defines.
397 PP.getTargetInfo().getTargetDefines(Buf);
398
399 // Compiler set macros.
400 DefineBuiltinMacro(Buf, "__APPLE_CC__=5250");
Steve Naroffb5a086e2007-11-10 18:06:36 +0000401 DefineBuiltinMacro(Buf, "__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=1050");
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000402 DefineBuiltinMacro(Buf, "__GNUC_MINOR__=0");
403 DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
404 DefineBuiltinMacro(Buf, "__GNUC__=4");
405 DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
406 DefineBuiltinMacro(Buf, "__VERSION__=\"4.0.1 (Apple Computer, Inc. "
407 "build 5250)\"");
408
409 // Build configuration options.
410 DefineBuiltinMacro(Buf, "__DYNAMIC__=1");
411 DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
412 DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
413 DefineBuiltinMacro(Buf, "__PIC__=1");
414
415
416 if (PP.getLangOptions().CPlusPlus) {
417 DefineBuiltinMacro(Buf, "__DEPRECATED=1");
418 DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
419 DefineBuiltinMacro(Buf, "__GNUG__=4");
420 DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
421 DefineBuiltinMacro(Buf, "__cplusplus=1");
422 DefineBuiltinMacro(Buf, "__private_extern__=extern");
423 }
Steve Naroff73a07032008-02-07 03:50:06 +0000424 if (PP.getLangOptions().Microsoft) {
425 DefineBuiltinMacro(Buf, "__stdcall=");
426 DefineBuiltinMacro(Buf, "__cdecl=");
427 DefineBuiltinMacro(Buf, "_cdecl=");
428 DefineBuiltinMacro(Buf, "__ptr64=");
Steve Naroffbe880ec2008-02-07 23:24:32 +0000429 DefineBuiltinMacro(Buf, "__w64=");
Steve Naroff73a07032008-02-07 03:50:06 +0000430 DefineBuiltinMacro(Buf, "__forceinline=");
Steve Narofff9bba132008-02-07 15:26:07 +0000431 DefineBuiltinMacro(Buf, "__int8=char");
432 DefineBuiltinMacro(Buf, "__int16=short");
433 DefineBuiltinMacro(Buf, "__int32=int");
Chris Lattnerd1a552b2008-02-10 21:12:45 +0000434 DefineBuiltinMacro(Buf, "__int64=long long");
Steve Naroffcfe78212008-02-11 22:29:58 +0000435 DefineBuiltinMacro(Buf, "__declspec(X)=");
Steve Naroff73a07032008-02-07 03:50:06 +0000436 }
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000437 // FIXME: Should emit a #line directive here.
438}
439
440
441/// EnterMainSourceFile - Enter the specified FileID as the main source file,
Nate Begeman886bf132008-01-07 04:01:26 +0000442/// which implicitly adds the builtin defines etc.
Ted Kremenek17861c52007-12-19 22:51:13 +0000443void Preprocessor::EnterMainSourceFile() {
444
445 unsigned MainFileID = SourceMgr.getMainFileID();
446
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000447 // Enter the main file source buffer.
448 EnterSourceFile(MainFileID, 0);
449
Chris Lattnerb45f05c2007-11-15 19:07:47 +0000450 // Tell the header info that the main file was entered. If the file is later
451 // #imported, it won't be re-entered.
452 if (const FileEntry *FE =
453 SourceMgr.getFileEntryForLoc(SourceLocation::getFileLoc(MainFileID, 0)))
454 HeaderInfo.IncrementIncludeCount(FE);
455
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000456 std::vector<char> PrologFile;
457 PrologFile.reserve(4080);
458
459 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
460 InitializePredefinedMacros(*this, PrologFile);
461
462 // Add on the predefines from the driver.
463 PrologFile.insert(PrologFile.end(), Predefines,Predefines+strlen(Predefines));
464
465 // Memory buffer must end with a null byte!
466 PrologFile.push_back(0);
467
468 // Now that we have emitted the predefined macros, #includes, etc into
469 // PrologFile, preprocess it to populate the initial preprocessor state.
470 llvm::MemoryBuffer *SB =
471 llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(),
472 "<predefines>");
473 assert(SB && "Cannot fail to create predefined source buffer");
474 unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB);
475 assert(FileID && "Could not create FileID for predefines?");
476
477 // Start parsing the predefines.
478 EnterSourceFile(FileID, 0);
479}
Chris Lattner4b009652007-07-25 00:24:17 +0000480
Chris Lattner4b009652007-07-25 00:24:17 +0000481
482//===----------------------------------------------------------------------===//
483// Lexer Event Handling.
484//===----------------------------------------------------------------------===//
485
486/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
487/// identifier information for the token and install it into the token.
488IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
489 const char *BufPtr) {
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000490 assert(Identifier.is(tok::identifier) && "Not an identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +0000491 assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
492
493 // Look up this token, see if it is a macro, or if it is a language keyword.
494 IdentifierInfo *II;
495 if (BufPtr && !Identifier.needsCleaning()) {
496 // No cleaning needed, just use the characters from the lexed buffer.
497 II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
498 } else {
499 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
500 llvm::SmallVector<char, 64> IdentifierBuffer;
501 IdentifierBuffer.resize(Identifier.getLength());
502 const char *TmpBuf = &IdentifierBuffer[0];
503 unsigned Size = getSpelling(Identifier, TmpBuf);
504 II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
505 }
506 Identifier.setIdentifierInfo(II);
507 return II;
508}
509
510
511/// HandleIdentifier - This callback is invoked when the lexer reads an
512/// identifier. This callback looks up the identifier in the map and/or
513/// potentially macro expands it or turns it into a named token (like 'for').
514void Preprocessor::HandleIdentifier(Token &Identifier) {
515 assert(Identifier.getIdentifierInfo() &&
516 "Can't handle identifiers without identifier info!");
517
518 IdentifierInfo &II = *Identifier.getIdentifierInfo();
519
520 // If this identifier was poisoned, and if it was not produced from a macro
521 // expansion, emit an error.
522 if (II.isPoisoned() && CurLexer) {
523 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
524 Diag(Identifier, diag::err_pp_used_poisoned_id);
525 else
526 Diag(Identifier, diag::ext_pp_bad_vaargs_use);
527 }
528
529 // If this is a macro to be expanded, do it.
Chris Lattner7a1b0882007-10-07 08:44:20 +0000530 if (MacroInfo *MI = getMacroInfo(&II)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000531 if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
532 if (MI->isEnabled()) {
533 if (!HandleMacroExpandedIdentifier(Identifier, MI))
534 return;
535 } else {
536 // C99 6.10.3.4p2 says that a disabled macro may never again be
537 // expanded, even if it's in a context where it could be expanded in the
538 // future.
539 Identifier.setFlag(Token::DisableExpand);
540 }
541 }
Chris Lattner4b009652007-07-25 00:24:17 +0000542 }
543
544 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
545 // then we act as if it is the actual operator and not the textual
546 // representation of it.
547 if (II.isCPlusPlusOperatorKeyword())
548 Identifier.setIdentifierInfo(0);
549
550 // Change the kind of this identifier to the appropriate token kind, e.g.
551 // turning "for" into a keyword.
552 Identifier.setKind(II.getTokenID());
553
554 // If this is an extension token, diagnose its use.
555 // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99
556 // For now, I'm just commenting it out (while I work on attributes).
557 if (II.isExtensionToken() && Features.C99)
558 Diag(Identifier, diag::ext_token_used);
559}
560