blob: 370244b8f6bd0f28be8b46064ad6ce4d52f513d6 [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 Lattner4b009652007-07-25 00:24:17 +000039using namespace clang;
40
41//===----------------------------------------------------------------------===//
42
Ted Kremenek5ab36b02008-04-17 21:23:07 +000043PreprocessorFactory::~PreprocessorFactory() {}
44
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner5b54ed92008-03-09 02:26:03 +000050 CurLexer(0), CurDirLookup(0), CurTokenLexer(0), Callbacks(0) {
Chris Lattner4b009652007-07-25 00:24:17 +000051 ScratchBuf = new ScratchBuffer(SourceMgr);
52
53 // 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 Lattner5b54ed92008-03-09 02:26:03 +000069 NumCachedTokenLexers = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000070
71 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
72 // This gets unpoisoned where it is allowed.
73 (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
74
Chris Lattnerd1f21e12007-10-09 22:10:18 +000075 Predefines = 0;
76
Chris Lattner4b009652007-07-25 00:24:17 +000077 // Initialize the pragma handlers.
78 PragmaHandlers = new PragmaNamespace(0);
79 RegisterBuiltinPragmas();
80
81 // Initialize builtin macros like __LINE__ and friends.
82 RegisterBuiltinMacros();
83}
84
85Preprocessor::~Preprocessor() {
86 // Free any active lexers.
87 delete CurLexer;
88
89 while (!IncludeMacroStack.empty()) {
90 delete IncludeMacroStack.back().TheLexer;
Chris Lattner5b54ed92008-03-09 02:26:03 +000091 delete IncludeMacroStack.back().TheTokenLexer;
Chris Lattner4b009652007-07-25 00:24:17 +000092 IncludeMacroStack.pop_back();
93 }
Chris Lattner7a1b0882007-10-07 08:44:20 +000094
95 // Free any macro definitions.
96 for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
97 Macros.begin(), E = Macros.end(); I != E; ++I) {
98 // Free the macro definition.
99 delete I->second;
100 I->second = 0;
101 I->first->setHasMacroDefinition(false);
102 }
Chris Lattner4b009652007-07-25 00:24:17 +0000103
104 // Free any cached macro expanders.
Chris Lattner5b54ed92008-03-09 02:26:03 +0000105 for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i)
106 delete TokenLexerCache[i];
Chris Lattner4b009652007-07-25 00:24:17 +0000107
108 // Release pragma information.
109 delete PragmaHandlers;
110
111 // Delete the scratch buffer info.
112 delete ScratchBuf;
Chris Lattner65829812008-03-14 06:07:05 +0000113
114 delete Callbacks;
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000115
116 delete [] Predefines;
Chris Lattner4b009652007-07-25 00:24:17 +0000117}
118
Chris Lattner4b009652007-07-25 00:24:17 +0000119/// Diag - Forwarding function for diagnostics. This emits a diagnostic at
120/// the specified Token's location, translating the token's start
121/// position in the current buffer into a SourcePosition object for rendering.
122void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000123 Diags.Report(getFullLoc(Loc), DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000124}
125
126void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
127 const std::string &Msg) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000128 Diags.Report(getFullLoc(Loc), DiagID, &Msg, 1);
Chris Lattner4b009652007-07-25 00:24:17 +0000129}
130
131void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000132 llvm::cerr << tok::getTokenName(Tok.getKind()) << " '"
133 << getSpelling(Tok) << "'";
Chris Lattner4b009652007-07-25 00:24:17 +0000134
135 if (!DumpFlags) return;
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000136
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000137 llvm::cerr << "\t";
Chris Lattner4b009652007-07-25 00:24:17 +0000138 if (Tok.isAtStartOfLine())
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000139 llvm::cerr << " [StartOfLine]";
Chris Lattner4b009652007-07-25 00:24:17 +0000140 if (Tok.hasLeadingSpace())
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000141 llvm::cerr << " [LeadingSpace]";
Chris Lattner4b009652007-07-25 00:24:17 +0000142 if (Tok.isExpandDisabled())
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000143 llvm::cerr << " [ExpandDisabled]";
Chris Lattner4b009652007-07-25 00:24:17 +0000144 if (Tok.needsCleaning()) {
145 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000146 llvm::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
147 << "']";
Chris Lattner4b009652007-07-25 00:24:17 +0000148 }
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000149
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000150 llvm::cerr << "\tLoc=<";
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000151 DumpLocation(Tok.getLocation());
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000152 llvm::cerr << ">";
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000153}
154
155void Preprocessor::DumpLocation(SourceLocation Loc) const {
156 SourceLocation LogLoc = SourceMgr.getLogicalLoc(Loc);
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000157 llvm::cerr << SourceMgr.getSourceName(LogLoc) << ':'
158 << SourceMgr.getLineNumber(LogLoc) << ':'
159 << SourceMgr.getLineNumber(LogLoc);
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000160
161 SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(Loc);
162 if (PhysLoc != LogLoc) {
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000163 llvm::cerr << " <PhysLoc=";
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000164 DumpLocation(PhysLoc);
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000165 llvm::cerr << ">";
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000166 }
Chris Lattner4b009652007-07-25 00:24:17 +0000167}
168
169void Preprocessor::DumpMacro(const MacroInfo &MI) const {
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000170 llvm::cerr << "MACRO: ";
Chris Lattner4b009652007-07-25 00:24:17 +0000171 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
172 DumpToken(MI.getReplacementToken(i));
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000173 llvm::cerr << " ";
Chris Lattner4b009652007-07-25 00:24:17 +0000174 }
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000175 llvm::cerr << "\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000176}
177
178void Preprocessor::PrintStats() {
Ted Kremenekce4c64e2008-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 Lattner4b009652007-07-25 00:24:17 +0000191
Ted Kremenekce4c64e2008-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 Lattner4b009652007-07-25 00:24:17 +0000198}
199
200//===----------------------------------------------------------------------===//
201// 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.
210std::string Preprocessor::getSpelling(const Token &Tok) const {
211 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
212
213 // If this token contains nothing interesting, return it directly.
214 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
215 if (!Tok.needsCleaning())
216 return std::string(TokStart, TokStart+Tok.getLength());
217
218 std::string Result;
219 Result.reserve(Tok.getLength());
220
221 // Otherwise, hard case, relex the characters into the string.
222 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.
237///
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.
243unsigned Preprocessor::getSpelling(const Token &Tok,
244 const char *&Buffer) const {
245 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
246
247 // 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();
251
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);
258 }
259
260 // Otherwise, compute the start of the token in the input lexer buffer.
261 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
262
263 // If this token contains nothing interesting, return it directly.
264 if (!Tok.needsCleaning()) {
265 Buffer = TokStart;
266 return Tok.getLength();
267 }
268 // Otherwise, hard case, relex the characters into the string.
269 char *OutBuf = const_cast<char*>(Buffer);
270 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
282
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
294/// 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) {
298 // 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;
304
305 // Figure out how many physical characters away the specified logical
306 // character is. This needs to take into consideration newlines and
307 // trigraphs.
308 const char *TokPtr = SourceMgr.getCharacterData(TokStart);
309 unsigned PhysOffset = 0;
310
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))
315 ++TokPtr, --CharNo, ++PhysOffset;
316
317 // If we have a character that may be a trigraph or escaped newline, create a
318 // lexer to parse it correctly.
319 if (CharNo != 0) {
320 // Create a lexer starting at this token position.
321 Lexer TheLexer(TokStart, *this, TokPtr);
322 Token Tok;
323 // Skip over characters the remaining characters.
324 const char *TokStartPtr = TokPtr;
325 for (; CharNo; --CharNo)
326 TheLexer.getAndAdvanceChar(TokPtr, Tok);
327
328 PhysOffset += TokPtr-TokStartPtr;
329 }
330
331 return TokStart.getFileLocWithOffset(PhysOffset);
332}
333
334
Chris Lattnerd1f21e12007-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 Naroffae84af82007-10-31 18:42:27 +0000391
Chris Lattner77cec472007-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 Lattnerd1f21e12007-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 Naroffb5a086e2007-11-10 18:06:36 +0000404 DefineBuiltinMacro(Buf, "__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=1050");
Chris Lattnerd1f21e12007-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 Naroff73a07032008-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 Naroffbe880ec2008-02-07 23:24:32 +0000432 DefineBuiltinMacro(Buf, "__w64=");
Steve Naroff73a07032008-02-07 03:50:06 +0000433 DefineBuiltinMacro(Buf, "__forceinline=");
Steve Narofff9bba132008-02-07 15:26:07 +0000434 DefineBuiltinMacro(Buf, "__int8=char");
435 DefineBuiltinMacro(Buf, "__int16=short");
436 DefineBuiltinMacro(Buf, "__int32=int");
Chris Lattnerd1a552b2008-02-10 21:12:45 +0000437 DefineBuiltinMacro(Buf, "__int64=long long");
Steve Naroffcfe78212008-02-11 22:29:58 +0000438 DefineBuiltinMacro(Buf, "__declspec(X)=");
Steve Naroff73a07032008-02-07 03:50:06 +0000439 }
Chris Lattnerd1f21e12007-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 Begeman886bf132008-01-07 04:01:26 +0000445/// which implicitly adds the builtin defines etc.
Ted Kremenek17861c52007-12-19 22:51:13 +0000446void Preprocessor::EnterMainSourceFile() {
447
448 unsigned MainFileID = SourceMgr.getMainFileID();
449
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000450 // Enter the main file source buffer.
451 EnterSourceFile(MainFileID, 0);
452
Chris Lattnerb45f05c2007-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 Lattnerd1f21e12007-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 Lattner4b009652007-07-25 00:24:17 +0000483
Chris Lattner4b009652007-07-25 00:24:17 +0000484
485//===----------------------------------------------------------------------===//
486// Lexer Event Handling.
487//===----------------------------------------------------------------------===//
488
489/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
490/// identifier information for the token and install it into the token.
491IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
492 const char *BufPtr) {
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000493 assert(Identifier.is(tok::identifier) && "Not an identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +0000494 assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
495
496 // Look up this token, see if it is a macro, or if it is a language keyword.
497 IdentifierInfo *II;
498 if (BufPtr && !Identifier.needsCleaning()) {
499 // No cleaning needed, just use the characters from the lexed buffer.
500 II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
501 } else {
502 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
503 llvm::SmallVector<char, 64> IdentifierBuffer;
504 IdentifierBuffer.resize(Identifier.getLength());
505 const char *TmpBuf = &IdentifierBuffer[0];
506 unsigned Size = getSpelling(Identifier, TmpBuf);
507 II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
508 }
509 Identifier.setIdentifierInfo(II);
510 return II;
511}
512
513
514/// HandleIdentifier - This callback is invoked when the lexer reads an
515/// identifier. This callback looks up the identifier in the map and/or
516/// potentially macro expands it or turns it into a named token (like 'for').
517void Preprocessor::HandleIdentifier(Token &Identifier) {
518 assert(Identifier.getIdentifierInfo() &&
519 "Can't handle identifiers without identifier info!");
520
521 IdentifierInfo &II = *Identifier.getIdentifierInfo();
522
523 // If this identifier was poisoned, and if it was not produced from a macro
524 // expansion, emit an error.
525 if (II.isPoisoned() && CurLexer) {
526 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
527 Diag(Identifier, diag::err_pp_used_poisoned_id);
528 else
529 Diag(Identifier, diag::ext_pp_bad_vaargs_use);
530 }
531
532 // If this is a macro to be expanded, do it.
Chris Lattner7a1b0882007-10-07 08:44:20 +0000533 if (MacroInfo *MI = getMacroInfo(&II)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000534 if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
535 if (MI->isEnabled()) {
536 if (!HandleMacroExpandedIdentifier(Identifier, MI))
537 return;
538 } else {
539 // C99 6.10.3.4p2 says that a disabled macro may never again be
540 // expanded, even if it's in a context where it could be expanded in the
541 // future.
542 Identifier.setFlag(Token::DisableExpand);
543 }
544 }
Chris Lattner4b009652007-07-25 00:24:17 +0000545 }
546
547 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
548 // then we act as if it is the actual operator and not the textual
549 // representation of it.
550 if (II.isCPlusPlusOperatorKeyword())
551 Identifier.setIdentifierInfo(0);
552
553 // Change the kind of this identifier to the appropriate token kind, e.g.
554 // turning "for" into a keyword.
555 Identifier.setKind(II.getTokenID());
556
557 // If this is an extension token, diagnose its use.
558 // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99
559 // For now, I'm just commenting it out (while I work on attributes).
560 if (II.isExtensionToken() && Features.C99)
561 Diag(Identifier, diag::ext_token_used);
562}
563