1 //===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Preprocessor interface.
12 //===----------------------------------------------------------------------===//
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.
19 // -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD
24 // "Multiple include guards may be useful for:\n"
26 //===----------------------------------------------------------------------===//
28 #include "clang/Lex/Preprocessor.h"
29 #include "clang/Lex/HeaderSearch.h"
30 #include "clang/Lex/MacroInfo.h"
31 #include "clang/Lex/Pragma.h"
32 #include "clang/Lex/ScratchBuffer.h"
33 #include "clang/Basic/Diagnostic.h"
34 #include "clang/Basic/SourceManager.h"
35 #include "clang/Basic/TargetInfo.h"
36 #include "llvm/ADT/APFloat.h"
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/Support/MemoryBuffer.h"
39 #include "llvm/Support/Streams.h"
40 using namespace clang
;
42 //===----------------------------------------------------------------------===//
44 PreprocessorFactory::~PreprocessorFactory() {}
46 Preprocessor::Preprocessor(Diagnostic
&diags
, const LangOptions
&opts
,
47 TargetInfo
&target
, SourceManager
&SM
,
48 HeaderSearch
&Headers
)
49 : Diags(diags
), Features(opts
), Target(target
), FileMgr(Headers
.getFileMgr()),
50 SourceMgr(SM
), HeaderInfo(Headers
), Identifiers(opts
),
51 CurPPLexer(0), CurDirLookup(0), Callbacks(0) {
52 ScratchBuf
= new ScratchBuffer(SourceMgr
);
55 NumDirectives
= NumDefined
= NumUndefined
= NumPragma
= 0;
56 NumIf
= NumElse
= NumEndif
= 0;
57 NumEnteredSourceFiles
= 0;
58 NumMacroExpanded
= NumFnMacroExpanded
= NumBuiltinMacroExpanded
= 0;
59 NumFastMacroExpanded
= NumTokenPaste
= NumFastTokenPaste
= 0;
60 MaxIncludeStackDepth
= 0;
63 // Default to discarding comments.
65 KeepMacroComments
= false;
67 // Macro expansion is enabled.
68 DisableMacroExpansion
= false;
70 NumCachedTokenLexers
= 0;
74 // "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();
78 // Initialize the pragma handlers.
79 PragmaHandlers
= new PragmaNamespace(0);
80 RegisterBuiltinPragmas();
82 // Initialize builtin macros like __LINE__ and friends.
83 RegisterBuiltinMacros();
86 Preprocessor::~Preprocessor() {
87 assert(BacktrackPositions
.empty() && "EnableBacktrack/Backtrack imbalance!");
89 while (!IncludeMacroStack
.empty()) {
90 delete IncludeMacroStack
.back().TheLexer
;
91 delete IncludeMacroStack
.back().TheTokenLexer
;
92 IncludeMacroStack
.pop_back();
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.
101 I
->first
->setHasMacroDefinition(false);
104 // Free any cached macro expanders.
105 for (unsigned i
= 0, e
= NumCachedTokenLexers
; i
!= e
; ++i
)
106 delete TokenLexerCache
[i
];
108 // Release pragma information.
109 delete PragmaHandlers
;
111 // Delete the scratch buffer info.
117 /// Diag - Forwarding function for diagnostics. This emits a diagnostic at
118 /// the specified Token's location, translating the token's start
119 /// position in the current buffer into a SourcePosition object for rendering.
120 DiagnosticInfo
Preprocessor::Diag(SourceLocation Loc
, unsigned DiagID
) {
121 return Diags
.Report(getFullLoc(Loc
), DiagID
);
124 DiagnosticInfo
Preprocessor::Diag(const Token
&Tok
, unsigned DiagID
) {
125 return Diags
.Report(getFullLoc(Tok
.getLocation()), DiagID
);
128 void Preprocessor::DumpToken(const Token
&Tok
, bool DumpFlags
) const {
129 llvm::cerr
<< tok::getTokenName(Tok
.getKind()) << " '"
130 << getSpelling(Tok
) << "'";
132 if (!DumpFlags
) return;
135 if (Tok
.isAtStartOfLine())
136 llvm::cerr
<< " [StartOfLine]";
137 if (Tok
.hasLeadingSpace())
138 llvm::cerr
<< " [LeadingSpace]";
139 if (Tok
.isExpandDisabled())
140 llvm::cerr
<< " [ExpandDisabled]";
141 if (Tok
.needsCleaning()) {
142 const char *Start
= SourceMgr
.getCharacterData(Tok
.getLocation());
143 llvm::cerr
<< " [UnClean='" << std::string(Start
, Start
+Tok
.getLength())
147 llvm::cerr
<< "\tLoc=<";
148 DumpLocation(Tok
.getLocation());
152 void Preprocessor::DumpLocation(SourceLocation Loc
) const {
153 SourceLocation LogLoc
= SourceMgr
.getLogicalLoc(Loc
);
154 llvm::cerr
<< SourceMgr
.getSourceName(LogLoc
) << ':'
155 << SourceMgr
.getLineNumber(LogLoc
) << ':'
156 << SourceMgr
.getColumnNumber(LogLoc
);
158 SourceLocation PhysLoc
= SourceMgr
.getPhysicalLoc(Loc
);
159 if (PhysLoc
!= LogLoc
) {
160 llvm::cerr
<< " <PhysLoc=";
161 DumpLocation(PhysLoc
);
166 void Preprocessor::DumpMacro(const MacroInfo
&MI
) const {
167 llvm::cerr
<< "MACRO: ";
168 for (unsigned i
= 0, e
= MI
.getNumTokens(); i
!= e
; ++i
) {
169 DumpToken(MI
.getReplacementToken(i
));
175 void Preprocessor::PrintStats() {
176 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";
189 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";
197 //===----------------------------------------------------------------------===//
199 //===----------------------------------------------------------------------===//
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
207 std::string
Preprocessor::getSpelling(const Token
&Tok
) const {
208 assert((int)Tok
.getLength() >= 0 && "Token character range is bogus!");
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());
216 Result
.reserve(Tok
.getLength());
218 // Otherwise, hard case, relex the characters into the string.
219 for (const char *Ptr
= TokStart
, *End
= TokStart
+Tok
.getLength();
222 Result
.push_back(Lexer::getCharAndSizeNoWarn(Ptr
, CharSize
, Features
));
225 assert(Result
.size() != unsigned(Tok
.getLength()) &&
226 "NeedsCleaning flag set on something that didn't need cleaning!");
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.
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.
240 unsigned Preprocessor::getSpelling(const Token
&Tok
,
241 const char *&Buffer
) const {
242 assert((int)Tok
.getLength() >= 0 && "Token character range is bogus!");
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();
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();
254 return strlen(Buffer
);
257 // Otherwise, compute the start of the token in the input lexer buffer.
258 const char *TokStart
= SourceMgr
.getCharacterData(Tok
.getLocation());
260 // If this token contains nothing interesting, return it directly.
261 if (!Tok
.needsCleaning()) {
263 return Tok
.getLength();
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();
270 *OutBuf
++ = Lexer::getCharAndSizeNoWarn(Ptr
, CharSize
, Features
);
273 assert(unsigned(OutBuf
-Buffer
) != Tok
.getLength() &&
274 "NeedsCleaning flag set on something that didn't need cleaning!");
276 return OutBuf
-Buffer
;
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.
283 SourceLocation
Preprocessor::
284 CreateString(const char *Buf
, unsigned Len
, SourceLocation SLoc
) {
286 return ScratchBuf
->getToken(Buf
, Len
, SLoc
);
287 return ScratchBuf
->getToken(Buf
, Len
);
291 /// AdvanceToTokenCharacter - Given a location that specifies the start of a
292 /// token, return a new location that specifies a character within the token.
293 SourceLocation
Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart
,
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
300 if (CharNo
== 0 || TokStart
.isMacroID()) return TokStart
;
302 // Figure out how many physical characters away the specified logical
303 // character is. This needs to take into consideration newlines and
305 const char *TokPtr
= SourceMgr
.getCharacterData(TokStart
);
306 unsigned PhysOffset
= 0;
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
;
314 // If we have a character that may be a trigraph or escaped newline, create a
315 // lexer to parse it correctly.
317 // Create a lexer starting at this token position.
318 Lexer
TheLexer(TokStart
, *this, TokPtr
);
320 // Skip over characters the remaining characters.
321 const char *TokStartPtr
= TokPtr
;
322 for (; CharNo
; --CharNo
)
323 TheLexer
.getAndAdvanceChar(TokPtr
, Tok
);
325 PhysOffset
+= TokPtr
-TokStartPtr
;
328 return TokStart
.getFileLocWithOffset(PhysOffset
);
332 //===----------------------------------------------------------------------===//
333 // Preprocessor Initialization Methods
334 //===----------------------------------------------------------------------===//
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=".
339 static 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
);
346 Buf
.insert(Buf
.end(), Equal
+1, Equal
+strlen(Equal
));
348 // Push "macroname 1".
349 Buf
.insert(Buf
.end(), Macro
, Macro
+strlen(Macro
));
356 /// PickFP - This is used to pick a value based on the FP semantics of the
357 /// specified FP model.
358 template <typename T
>
359 static T
PickFP(const llvm::fltSemantics
*Sem
, T IEEESingleVal
,
360 T IEEEDoubleVal
, T X87DoubleExtendedVal
, T PPCDoubleDoubleVal
) {
361 if (Sem
== &llvm::APFloat::IEEEsingle
)
362 return IEEESingleVal
;
363 if (Sem
== &llvm::APFloat::IEEEdouble
)
364 return IEEEDoubleVal
;
365 if (Sem
== &llvm::APFloat::x87DoubleExtended
)
366 return X87DoubleExtendedVal
;
367 assert(Sem
== &llvm::APFloat::PPCDoubleDouble
);
368 return PPCDoubleDoubleVal
;
371 static void DefineFloatMacros(std::vector
<char> &Buf
, const char *Prefix
,
372 const llvm::fltSemantics
*Sem
) {
373 const char *DenormMin
, *Epsilon
, *Max
, *Min
;
374 DenormMin
= PickFP(Sem
, "1.40129846e-45F", "4.9406564584124654e-324",
375 "3.64519953188247460253e-4951L",
376 "4.94065645841246544176568792868221e-324L");
377 int Digits
= PickFP(Sem
, 6, 15, 18, 31);
378 Epsilon
= PickFP(Sem
, "1.19209290e-7F", "2.2204460492503131e-16",
379 "1.08420217248550443401e-19L",
380 "4.94065645841246544176568792868221e-324L");
381 int HasInifinity
= 1, HasQuietNaN
= 1;
382 int MantissaDigits
= PickFP(Sem
, 24, 53, 64, 106);
383 int Min10Exp
= PickFP(Sem
, -37, -307, -4931, -291);
384 int Max10Exp
= PickFP(Sem
, 38, 308, 4932, 308);
385 int MinExp
= PickFP(Sem
, -125, -1021, -16381, -968);
386 int MaxExp
= PickFP(Sem
, 128, 1024, 16384, 1024);
387 Min
= PickFP(Sem
, "1.17549435e-38F", "2.2250738585072014e-308",
388 "3.36210314311209350626e-4932L",
389 "2.00416836000897277799610805135016e-292L");
390 Max
= PickFP(Sem
, "3.40282347e+38F", "1.7976931348623157e+308",
391 "1.18973149535723176502e+4932L",
392 "1.79769313486231580793728971405301e+308L");
395 sprintf(MacroBuf
, "__%s_DENORM_MIN__=%s", Prefix
, DenormMin
);
396 DefineBuiltinMacro(Buf
, MacroBuf
);
397 sprintf(MacroBuf
, "__%s_DIG__=%d", Prefix
, Digits
);
398 DefineBuiltinMacro(Buf
, MacroBuf
);
399 sprintf(MacroBuf
, "__%s_EPSILON__=%s", Prefix
, Epsilon
);
400 DefineBuiltinMacro(Buf
, MacroBuf
);
401 sprintf(MacroBuf
, "__%s_HAS_INFINITY__=%d", Prefix
, HasInifinity
);
402 DefineBuiltinMacro(Buf
, MacroBuf
);
403 sprintf(MacroBuf
, "__%s_HAS_QUIET_NAN__=%d", Prefix
, HasQuietNaN
);
404 DefineBuiltinMacro(Buf
, MacroBuf
);
405 sprintf(MacroBuf
, "__%s_MANT_DIG__=%d", Prefix
, MantissaDigits
);
406 DefineBuiltinMacro(Buf
, MacroBuf
);
407 sprintf(MacroBuf
, "__%s_MAX_10_EXP__=%d", Prefix
, Max10Exp
);
408 DefineBuiltinMacro(Buf
, MacroBuf
);
409 sprintf(MacroBuf
, "__%s_MAX_EXP__=%d", Prefix
, MaxExp
);
410 DefineBuiltinMacro(Buf
, MacroBuf
);
411 sprintf(MacroBuf
, "__%s_MAX__=%s", Prefix
, Max
);
412 DefineBuiltinMacro(Buf
, MacroBuf
);
413 sprintf(MacroBuf
, "__%s_MIN_10_EXP__=(%d)", Prefix
, Min10Exp
);
414 DefineBuiltinMacro(Buf
, MacroBuf
);
415 sprintf(MacroBuf
, "__%s_MIN_EXP__=(%d)", Prefix
, MinExp
);
416 DefineBuiltinMacro(Buf
, MacroBuf
);
417 sprintf(MacroBuf
, "__%s_MIN__=%s", Prefix
, Min
);
418 DefineBuiltinMacro(Buf
, MacroBuf
);
422 static void InitializePredefinedMacros(Preprocessor
&PP
,
423 std::vector
<char> &Buf
) {
424 // Compiler version introspection macros.
425 DefineBuiltinMacro(Buf
, "__llvm__=1"); // LLVM Backend
426 DefineBuiltinMacro(Buf
, "__clang__=1"); // Clang Frontend
428 // Currently claim to be compatible with GCC 4.2.1-5621.
429 DefineBuiltinMacro(Buf
, "__APPLE_CC__=5621");
430 DefineBuiltinMacro(Buf
, "__GNUC_MINOR__=2");
431 DefineBuiltinMacro(Buf
, "__GNUC_PATCHLEVEL__=1");
432 DefineBuiltinMacro(Buf
, "__GNUC__=4");
433 DefineBuiltinMacro(Buf
, "__GXX_ABI_VERSION=1002");
434 DefineBuiltinMacro(Buf
, "__VERSION__=\"4.2.1 (Apple Computer, Inc. "
435 "build 5621) (dot 3)\"");
438 // Initialize language-specific preprocessor defines.
440 // FIXME: Implement magic like cpp_init_builtins for things like __STDC__
442 // These should all be defined in the preprocessor according to the
443 // current language configuration.
444 DefineBuiltinMacro(Buf
, "__STDC__=1");
445 //DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
446 if (PP
.getLangOptions().C99
&& !PP
.getLangOptions().CPlusPlus
)
447 DefineBuiltinMacro(Buf
, "__STDC_VERSION__=199901L");
448 else if (0) // STDC94 ?
449 DefineBuiltinMacro(Buf
, "__STDC_VERSION__=199409L");
451 DefineBuiltinMacro(Buf
, "__STDC_HOSTED__=1");
452 if (PP
.getLangOptions().ObjC1
) {
453 DefineBuiltinMacro(Buf
, "__OBJC__=1");
455 if (PP
.getLangOptions().getGCMode() == LangOptions::NonGC
) {
456 DefineBuiltinMacro(Buf
, "__weak=");
457 DefineBuiltinMacro(Buf
, "__strong=");
459 DefineBuiltinMacro(Buf
, "__weak=__attribute__((objc_gc(weak)))");
460 DefineBuiltinMacro(Buf
, "__strong=__attribute__((objc_gc(strong)))");
461 DefineBuiltinMacro(Buf
, "__OBJC_GC__=1");
464 if (PP
.getLangOptions().NeXTRuntime
)
465 DefineBuiltinMacro(Buf
, "__NEXT_RUNTIME__=1");
468 // darwin_constant_cfstrings controls this. This is also dependent
469 // on other things like the runtime I believe. This is set even for C code.
470 DefineBuiltinMacro(Buf
, "__CONSTANT_CFSTRINGS__=1");
472 if (PP
.getLangOptions().ObjC2
)
473 DefineBuiltinMacro(Buf
, "OBJC_NEW_PROPERTIES");
475 if (PP
.getLangOptions().PascalStrings
)
476 DefineBuiltinMacro(Buf
, "__PASCAL_STRINGS__");
478 if (PP
.getLangOptions().Blocks
) {
479 DefineBuiltinMacro(Buf
, "__block=__attribute__((__blocks__(byref)))");
480 DefineBuiltinMacro(Buf
, "__BLOCKS__=1");
483 if (PP
.getLangOptions().CPlusPlus
) {
484 DefineBuiltinMacro(Buf
, "__DEPRECATED=1");
485 DefineBuiltinMacro(Buf
, "__EXCEPTIONS=1");
486 DefineBuiltinMacro(Buf
, "__GNUG__=4");
487 DefineBuiltinMacro(Buf
, "__GXX_WEAK__=1");
488 DefineBuiltinMacro(Buf
, "__cplusplus=1");
489 DefineBuiltinMacro(Buf
, "__private_extern__=extern");
492 // Filter out some microsoft extensions when trying to parse in ms-compat
494 if (PP
.getLangOptions().Microsoft
) {
495 DefineBuiltinMacro(Buf
, "__stdcall=");
496 DefineBuiltinMacro(Buf
, "__cdecl=");
497 DefineBuiltinMacro(Buf
, "_cdecl=");
498 DefineBuiltinMacro(Buf
, "__ptr64=");
499 DefineBuiltinMacro(Buf
, "__w64=");
500 DefineBuiltinMacro(Buf
, "__forceinline=");
501 DefineBuiltinMacro(Buf
, "__int8=char");
502 DefineBuiltinMacro(Buf
, "__int16=short");
503 DefineBuiltinMacro(Buf
, "__int32=int");
504 DefineBuiltinMacro(Buf
, "__int64=long long");
505 DefineBuiltinMacro(Buf
, "__declspec(X)=");
509 // Initialize target-specific preprocessor defines.
510 const TargetInfo
&TI
= PP
.getTargetInfo();
512 // Define type sizing macros based on the target properties.
513 assert(TI
.getCharWidth() == 8 && "Only support 8-bit char so far");
514 DefineBuiltinMacro(Buf
, "__CHAR_BIT__=8");
515 DefineBuiltinMacro(Buf
, "__SCHAR_MAX__=127");
517 assert(TI
.getWCharWidth() == 32 && "Only support 32-bit wchar so far");
518 DefineBuiltinMacro(Buf
, "__WCHAR_MAX__=2147483647");
519 DefineBuiltinMacro(Buf
, "__WCHAR_TYPE__=int");
520 DefineBuiltinMacro(Buf
, "__WINT_TYPE__=int");
522 assert(TI
.getShortWidth() == 16 && "Only support 16-bit short so far");
523 DefineBuiltinMacro(Buf
, "__SHRT_MAX__=32767");
525 if (TI
.getIntWidth() == 32)
526 DefineBuiltinMacro(Buf
, "__INT_MAX__=2147483647");
527 else if (TI
.getIntWidth() == 16)
528 DefineBuiltinMacro(Buf
, "__INT_MAX__=32767");
530 assert(0 && "Unknown integer size");
532 if (TI
.getLongLongWidth() == 64)
533 DefineBuiltinMacro(Buf
, "__LONG_LONG_MAX__=9223372036854775807LL");
534 else if (TI
.getLongLongWidth() == 32)
535 DefineBuiltinMacro(Buf
, "__LONG_LONG_MAX__=2147483647L");
537 if (TI
.getLongWidth() == 32)
538 DefineBuiltinMacro(Buf
, "__LONG_MAX__=2147483647L");
539 else if (TI
.getLongWidth() == 64)
540 DefineBuiltinMacro(Buf
, "__LONG_MAX__=9223372036854775807L");
541 else if (TI
.getLongWidth() == 16)
542 DefineBuiltinMacro(Buf
, "__LONG_MAX__=32767L");
544 assert(0 && "Unknown long size");
546 sprintf(MacroBuf
, "__INTMAX_MAX__=%lld",
547 (TI
.getIntMaxType() == TargetInfo::UnsignedLongLong
?
548 (1LL << (TI
.getLongLongWidth() - 1)) :
549 ((1LL << (TI
.getLongLongWidth() - 2)) - 1)));
550 DefineBuiltinMacro(Buf
, MacroBuf
);
552 if (TI
.getIntMaxType() == TargetInfo::UnsignedLongLong
)
553 DefineBuiltinMacro(Buf
, "__INTMAX_TYPE__=unsigned long long int");
554 else if (TI
.getIntMaxType() == TargetInfo::SignedLongLong
)
555 DefineBuiltinMacro(Buf
, "__INTMAX_TYPE__=long long int");
556 else if (TI
.getIntMaxType() == TargetInfo::UnsignedLong
)
557 DefineBuiltinMacro(Buf
, "__INTMAX_TYPE__=unsigned long int");
558 else if (TI
.getIntMaxType() == TargetInfo::SignedLong
)
559 DefineBuiltinMacro(Buf
, "__INTMAX_TYPE__=long int");
560 else if (TI
.getIntMaxType() == TargetInfo::UnsignedInt
)
561 DefineBuiltinMacro(Buf
, "__INTMAX_TYPE__=unsigned int");
563 DefineBuiltinMacro(Buf
, "__INTMAX_TYPE__=int");
565 if (TI
.getUIntMaxType() == TargetInfo::UnsignedLongLong
)
566 DefineBuiltinMacro(Buf
, "__UINTMAX_TYPE__=unsigned long long int");
567 else if (TI
.getUIntMaxType() == TargetInfo::SignedLongLong
)
568 DefineBuiltinMacro(Buf
, "__UINTMAX_TYPE__=long long int");
569 else if (TI
.getUIntMaxType() == TargetInfo::UnsignedLong
)
570 DefineBuiltinMacro(Buf
, "__UINTMAX_TYPE__=unsigned long int");
571 else if (TI
.getUIntMaxType() == TargetInfo::SignedLong
)
572 DefineBuiltinMacro(Buf
, "__UINTMAX_TYPE__=long int");
573 else if (TI
.getUIntMaxType() == TargetInfo::UnsignedInt
)
574 DefineBuiltinMacro(Buf
, "__UINTMAX_TYPE__=unsigned int");
576 DefineBuiltinMacro(Buf
, "__UINTMAX_TYPE__=int");
578 if (TI
.getPtrDiffType(0) == TargetInfo::UnsignedLongLong
)
579 DefineBuiltinMacro(Buf
, "__PTRDIFF_TYPE__=unsigned long long int");
580 else if (TI
.getPtrDiffType(0) == TargetInfo::SignedLongLong
)
581 DefineBuiltinMacro(Buf
, "__PTRDIFF_TYPE__=long long int");
582 else if (TI
.getPtrDiffType(0) == TargetInfo::UnsignedLong
)
583 DefineBuiltinMacro(Buf
, "__PTRDIFF_TYPE__=unsigned long int");
584 else if (TI
.getPtrDiffType(0) == TargetInfo::SignedLong
)
585 DefineBuiltinMacro(Buf
, "__PTRDIFF_TYPE__=long int");
586 else if (TI
.getPtrDiffType(0) == TargetInfo::UnsignedInt
)
587 DefineBuiltinMacro(Buf
, "__PTRDIFF_TYPE__=unsigned int");
589 DefineBuiltinMacro(Buf
, "__PTRDIFF_TYPE__=int");
591 if (TI
.getSizeType() == TargetInfo::UnsignedLongLong
)
592 DefineBuiltinMacro(Buf
, "__SIZE_TYPE__=unsigned long long int");
593 else if (TI
.getSizeType() == TargetInfo::SignedLongLong
)
594 DefineBuiltinMacro(Buf
, "__SIZE_TYPE__=long long int");
595 else if (TI
.getSizeType() == TargetInfo::UnsignedLong
)
596 DefineBuiltinMacro(Buf
, "__SIZE_TYPE__=unsigned long int");
597 else if (TI
.getSizeType() == TargetInfo::SignedLong
)
598 DefineBuiltinMacro(Buf
, "__SIZE_TYPE__=long int");
599 else if (TI
.getSizeType() == TargetInfo::UnsignedInt
)
600 DefineBuiltinMacro(Buf
, "__SIZE_TYPE__=unsigned int");
601 else if (TI
.getSizeType() == TargetInfo::SignedInt
)
602 DefineBuiltinMacro(Buf
, "__SIZE_TYPE__=int");
604 DefineBuiltinMacro(Buf
, "__SIZE_TYPE__=unsigned short");
606 DefineFloatMacros(Buf
, "FLT", &TI
.getFloatFormat());
607 DefineFloatMacros(Buf
, "DBL", &TI
.getDoubleFormat());
608 DefineFloatMacros(Buf
, "LDBL", &TI
.getLongDoubleFormat());
611 // Add __builtin_va_list typedef.
613 const char *VAList
= TI
.getVAListDeclaration();
614 Buf
.insert(Buf
.end(), VAList
, VAList
+strlen(VAList
));
618 if (const char *Prefix
= TI
.getUserLabelPrefix()) {
619 sprintf(MacroBuf
, "__USER_LABEL_PREFIX__=%s", Prefix
);
620 DefineBuiltinMacro(Buf
, MacroBuf
);
623 // Build configuration options. FIXME: these should be controlled by
624 // command line options or something.
625 DefineBuiltinMacro(Buf
, "__DYNAMIC__=1");
626 DefineBuiltinMacro(Buf
, "__FINITE_MATH_ONLY__=0");
627 DefineBuiltinMacro(Buf
, "__NO_INLINE__=1");
628 DefineBuiltinMacro(Buf
, "__PIC__=1");
630 // Macros to control C99 numerics and <float.h>
631 DefineBuiltinMacro(Buf
, "__FLT_EVAL_METHOD__=0");
632 DefineBuiltinMacro(Buf
, "__FLT_RADIX__=2");
633 sprintf(MacroBuf
, "__DECIMAL_DIG__=%d",
634 PickFP(&TI
.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33));
635 DefineBuiltinMacro(Buf
, MacroBuf
);
637 // Get other target #defines.
638 TI
.getTargetDefines(Buf
);
640 // FIXME: Should emit a #line directive here.
644 /// EnterMainSourceFile - Enter the specified FileID as the main source file,
645 /// which implicitly adds the builtin defines etc.
646 void Preprocessor::EnterMainSourceFile() {
648 unsigned MainFileID
= SourceMgr
.getMainFileID();
650 // Enter the main file source buffer.
651 EnterSourceFile(MainFileID
, 0);
653 // Tell the header info that the main file was entered. If the file is later
654 // #imported, it won't be re-entered.
655 if (const FileEntry
*FE
=
656 SourceMgr
.getFileEntryForLoc(SourceLocation::getFileLoc(MainFileID
, 0)))
657 HeaderInfo
.IncrementIncludeCount(FE
);
659 std::vector
<char> PrologFile
;
660 PrologFile
.reserve(4080);
662 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
663 InitializePredefinedMacros(*this, PrologFile
);
665 // Add on the predefines from the driver.
666 PrologFile
.insert(PrologFile
.end(), Predefines
.begin(), Predefines
.end());
668 // Memory buffer must end with a null byte!
669 PrologFile
.push_back(0);
671 // Now that we have emitted the predefined macros, #includes, etc into
672 // PrologFile, preprocess it to populate the initial preprocessor state.
673 llvm::MemoryBuffer
*SB
=
674 llvm::MemoryBuffer::getMemBufferCopy(&PrologFile
.front(),&PrologFile
.back(),
676 assert(SB
&& "Cannot fail to create predefined source buffer");
677 unsigned FileID
= SourceMgr
.createFileIDForMemBuffer(SB
);
678 assert(FileID
&& "Could not create FileID for predefines?");
680 // Start parsing the predefines.
681 EnterSourceFile(FileID
, 0);
685 //===----------------------------------------------------------------------===//
686 // Lexer Event Handling.
687 //===----------------------------------------------------------------------===//
689 /// LookUpIdentifierInfo - Given a tok::identifier token, look up the
690 /// identifier information for the token and install it into the token.
691 IdentifierInfo
*Preprocessor::LookUpIdentifierInfo(Token
&Identifier
,
692 const char *BufPtr
) {
693 assert(Identifier
.is(tok::identifier
) && "Not an identifier!");
694 assert(Identifier
.getIdentifierInfo() == 0 && "Identinfo already exists!");
696 // Look up this token, see if it is a macro, or if it is a language keyword.
698 if (BufPtr
&& !Identifier
.needsCleaning()) {
699 // No cleaning needed, just use the characters from the lexed buffer.
700 II
= getIdentifierInfo(BufPtr
, BufPtr
+Identifier
.getLength());
702 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
703 llvm::SmallVector
<char, 64> IdentifierBuffer
;
704 IdentifierBuffer
.resize(Identifier
.getLength());
705 const char *TmpBuf
= &IdentifierBuffer
[0];
706 unsigned Size
= getSpelling(Identifier
, TmpBuf
);
707 II
= getIdentifierInfo(TmpBuf
, TmpBuf
+Size
);
709 Identifier
.setIdentifierInfo(II
);
714 /// HandleIdentifier - This callback is invoked when the lexer reads an
715 /// identifier. This callback looks up the identifier in the map and/or
716 /// potentially macro expands it or turns it into a named token (like 'for').
717 void Preprocessor::HandleIdentifier(Token
&Identifier
) {
718 assert(Identifier
.getIdentifierInfo() &&
719 "Can't handle identifiers without identifier info!");
721 IdentifierInfo
&II
= *Identifier
.getIdentifierInfo();
723 // If this identifier was poisoned, and if it was not produced from a macro
724 // expansion, emit an error.
725 if (II
.isPoisoned() && CurLexer
) {
726 if (&II
!= Ident__VA_ARGS__
) // We warn about __VA_ARGS__ with poisoning.
727 Diag(Identifier
, diag::err_pp_used_poisoned_id
);
729 Diag(Identifier
, diag::ext_pp_bad_vaargs_use
);
732 // If this is a macro to be expanded, do it.
733 if (MacroInfo
*MI
= getMacroInfo(&II
)) {
734 if (!DisableMacroExpansion
&& !Identifier
.isExpandDisabled()) {
735 if (MI
->isEnabled()) {
736 if (!HandleMacroExpandedIdentifier(Identifier
, MI
))
739 // C99 6.10.3.4p2 says that a disabled macro may never again be
740 // expanded, even if it's in a context where it could be expanded in the
742 Identifier
.setFlag(Token::DisableExpand
);
747 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
748 // then we act as if it is the actual operator and not the textual
749 // representation of it.
750 if (II
.isCPlusPlusOperatorKeyword())
751 Identifier
.setIdentifierInfo(0);
753 // Change the kind of this identifier to the appropriate token kind, e.g.
754 // turning "for" into a keyword.
755 Identifier
.setKind(II
.getTokenID());
757 // If this is an extension token, diagnose its use.
758 // We avoid diagnosing tokens that originate from macro definitions.
759 if (II
.isExtensionToken() && Features
.C99
&& !DisableMacroExpansion
)
760 Diag(Identifier
, diag::ext_token_used
);