1 //===--- MacroExpansion.cpp - Top level Macro Expansion -------------------===//
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 top level handling of macro expasion for the
13 //===----------------------------------------------------------------------===//
15 #include "clang/Lex/Preprocessor.h"
16 #include "MacroArgs.h"
17 #include "clang/Lex/MacroInfo.h"
18 #include "clang/Basic/SourceManager.h"
19 #include "clang/Basic/FileManager.h"
20 #include "clang/Basic/TargetInfo.h"
21 #include "clang/Lex/LexDiagnostic.h"
22 #include "clang/Lex/CodeCompletionHandler.h"
23 #include "clang/Lex/ExternalPreprocessorSource.h"
24 #include "llvm/ADT/StringSwitch.h"
25 #include "llvm/Config/config.h"
26 #include "llvm/Support/raw_ostream.h"
29 using namespace clang
;
31 MacroInfo
*Preprocessor::getInfoForMacro(IdentifierInfo
*II
) const {
32 assert(II
->hasMacroDefinition() && "Identifier is not a macro!");
34 llvm::DenseMap
<IdentifierInfo
*, MacroInfo
*>::const_iterator Pos
36 if (Pos
== Macros
.end()) {
37 // Load this macro from the external source.
38 getExternalSource()->LoadMacroDefinition(II
);
39 Pos
= Macros
.find(II
);
41 assert(Pos
!= Macros
.end() && "Identifier macro info is missing!");
45 /// setMacroInfo - Specify a macro for this identifier.
47 void Preprocessor::setMacroInfo(IdentifierInfo
*II
, MacroInfo
*MI
) {
50 II
->setHasMacroDefinition(true);
51 } else if (II
->hasMacroDefinition()) {
53 II
->setHasMacroDefinition(false);
57 /// RegisterBuiltinMacro - Register the specified identifier in the identifier
58 /// table and mark it as a builtin macro to be expanded.
59 static IdentifierInfo
*RegisterBuiltinMacro(Preprocessor
&PP
, const char *Name
){
60 // Get the identifier.
61 IdentifierInfo
*Id
= PP
.getIdentifierInfo(Name
);
63 // Mark it as being a macro that is builtin.
64 MacroInfo
*MI
= PP
.AllocateMacroInfo(SourceLocation());
65 MI
->setIsBuiltinMacro();
66 PP
.setMacroInfo(Id
, MI
);
71 /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
73 void Preprocessor::RegisterBuiltinMacros() {
74 Ident__LINE__
= RegisterBuiltinMacro(*this, "__LINE__");
75 Ident__FILE__
= RegisterBuiltinMacro(*this, "__FILE__");
76 Ident__DATE__
= RegisterBuiltinMacro(*this, "__DATE__");
77 Ident__TIME__
= RegisterBuiltinMacro(*this, "__TIME__");
78 Ident__COUNTER__
= RegisterBuiltinMacro(*this, "__COUNTER__");
79 Ident_Pragma
= RegisterBuiltinMacro(*this, "_Pragma");
82 Ident__BASE_FILE__
= RegisterBuiltinMacro(*this, "__BASE_FILE__");
83 Ident__INCLUDE_LEVEL__
= RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
84 Ident__TIMESTAMP__
= RegisterBuiltinMacro(*this, "__TIMESTAMP__");
87 Ident__has_feature
= RegisterBuiltinMacro(*this, "__has_feature");
88 Ident__has_builtin
= RegisterBuiltinMacro(*this, "__has_builtin");
89 Ident__has_attribute
= RegisterBuiltinMacro(*this, "__has_attribute");
90 Ident__has_include
= RegisterBuiltinMacro(*this, "__has_include");
91 Ident__has_include_next
= RegisterBuiltinMacro(*this, "__has_include_next");
93 // Microsoft Extensions.
94 if (Features
.Microsoft
)
95 Ident__pragma
= RegisterBuiltinMacro(*this, "__pragma");
100 /// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
101 /// in its expansion, currently expands to that token literally.
102 static bool isTrivialSingleTokenExpansion(const MacroInfo
*MI
,
103 const IdentifierInfo
*MacroIdent
,
105 IdentifierInfo
*II
= MI
->getReplacementToken(0).getIdentifierInfo();
107 // If the token isn't an identifier, it's always literally expanded.
108 if (II
== 0) return true;
110 // If the identifier is a macro, and if that macro is enabled, it may be
111 // expanded so it's not a trivial expansion.
112 if (II
->hasMacroDefinition() && PP
.getMacroInfo(II
)->isEnabled() &&
113 // Fast expanding "#define X X" is ok, because X would be disabled.
117 // If this is an object-like macro invocation, it is safe to trivially expand
119 if (MI
->isObjectLike()) return true;
121 // If this is a function-like macro invocation, it's safe to trivially expand
122 // as long as the identifier is not a macro argument.
123 for (MacroInfo::arg_iterator I
= MI
->arg_begin(), E
= MI
->arg_end();
126 return false; // Identifier is a macro argument.
132 /// isNextPPTokenLParen - Determine whether the next preprocessor token to be
133 /// lexed is a '('. If so, consume the token and return true, if not, this
134 /// method should have no observable side-effect on the lexed tokens.
135 bool Preprocessor::isNextPPTokenLParen() {
136 // Do some quick tests for rejection cases.
139 Val
= CurLexer
->isNextPPTokenLParen();
140 else if (CurPTHLexer
)
141 Val
= CurPTHLexer
->isNextPPTokenLParen();
143 Val
= CurTokenLexer
->isNextTokenLParen();
146 // We have run off the end. If it's a source file we don't
147 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
151 for (unsigned i
= IncludeMacroStack
.size(); i
!= 0; --i
) {
152 IncludeStackInfo
&Entry
= IncludeMacroStack
[i
-1];
154 Val
= Entry
.TheLexer
->isNextPPTokenLParen();
155 else if (Entry
.ThePTHLexer
)
156 Val
= Entry
.ThePTHLexer
->isNextPPTokenLParen();
158 Val
= Entry
.TheTokenLexer
->isNextTokenLParen();
163 // Ran off the end of a source file?
164 if (Entry
.ThePPLexer
)
169 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
170 // have found something that isn't a '(' or we found the end of the
171 // translation unit. In either case, return false.
175 /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
176 /// expanded as a macro, handle it and return the next token as 'Identifier'.
177 bool Preprocessor::HandleMacroExpandedIdentifier(Token
&Identifier
,
179 MacroExpansionFlag
= true;
180 if (Callbacks
) Callbacks
->MacroExpands(Identifier
, MI
);
182 // If this is a macro expansion in the "#if !defined(x)" line for the file,
183 // then the macro could expand to different things in other contexts, we need
184 // to disable the optimization in this case.
185 if (CurPPLexer
) CurPPLexer
->MIOpt
.ExpandedMacro();
187 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
188 if (MI
->isBuiltinMacro()) {
189 ExpandBuiltinMacro(Identifier
);
193 /// Args - If this is a function-like macro expansion, this contains,
194 /// for each macro argument, the list of tokens that were provided to the
198 // Remember where the end of the instantiation occurred. For an object-like
199 // macro, this is the identifier. For a function-like macro, this is the ')'.
200 SourceLocation InstantiationEnd
= Identifier
.getLocation();
202 // If this is a function-like macro, read the arguments.
203 if (MI
->isFunctionLike()) {
204 // C99 6.10.3p10: If the preprocessing token immediately after the the macro
205 // name isn't a '(', this macro should not be expanded.
206 if (!isNextPPTokenLParen())
209 // Remember that we are now parsing the arguments to a macro invocation.
210 // Preprocessor directives used inside macro arguments are not portable, and
211 // this enables the warning.
213 Args
= ReadFunctionLikeMacroArgs(Identifier
, MI
, InstantiationEnd
);
215 // Finished parsing args.
218 // If there was an error parsing the arguments, bail out.
219 if (Args
== 0) return false;
221 ++NumFnMacroExpanded
;
226 // Notice that this macro has been used.
229 // If we started lexing a macro, enter the macro expansion body.
231 // If this macro expands to no tokens, don't bother to push it onto the
232 // expansion stack, only to take it right back off.
233 if (MI
->getNumTokens() == 0) {
234 // No need for arg info.
235 if (Args
) Args
->destroy(*this);
237 // Ignore this macro use, just return the next token in the current
239 bool HadLeadingSpace
= Identifier
.hasLeadingSpace();
240 bool IsAtStartOfLine
= Identifier
.isAtStartOfLine();
244 // If the identifier isn't on some OTHER line, inherit the leading
245 // whitespace/first-on-a-line property of this token. This handles
246 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
248 if (!Identifier
.isAtStartOfLine()) {
249 if (IsAtStartOfLine
) Identifier
.setFlag(Token::StartOfLine
);
250 if (HadLeadingSpace
) Identifier
.setFlag(Token::LeadingSpace
);
252 ++NumFastMacroExpanded
;
255 } else if (MI
->getNumTokens() == 1 &&
256 isTrivialSingleTokenExpansion(MI
, Identifier
.getIdentifierInfo(),
258 // Otherwise, if this macro expands into a single trivially-expanded
259 // token: expand it now. This handles common cases like
262 // No need for arg info.
263 if (Args
) Args
->destroy(*this);
265 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
266 // identifier to the expanded token.
267 bool isAtStartOfLine
= Identifier
.isAtStartOfLine();
268 bool hasLeadingSpace
= Identifier
.hasLeadingSpace();
270 // Remember where the token is instantiated.
271 SourceLocation InstantiateLoc
= Identifier
.getLocation();
273 // Replace the result token.
274 Identifier
= MI
->getReplacementToken(0);
276 // Restore the StartOfLine/LeadingSpace markers.
277 Identifier
.setFlagValue(Token::StartOfLine
, isAtStartOfLine
);
278 Identifier
.setFlagValue(Token::LeadingSpace
, hasLeadingSpace
);
280 // Update the tokens location to include both its instantiation and physical
283 SourceMgr
.createInstantiationLoc(Identifier
.getLocation(), InstantiateLoc
,
284 InstantiationEnd
,Identifier
.getLength());
285 Identifier
.setLocation(Loc
);
287 // If this is a disabled macro or #define X X, we must mark the result as
289 if (IdentifierInfo
*NewII
= Identifier
.getIdentifierInfo()) {
290 if (MacroInfo
*NewMI
= getMacroInfo(NewII
))
291 if (!NewMI
->isEnabled() || NewMI
== MI
)
292 Identifier
.setFlag(Token::DisableExpand
);
295 // Since this is not an identifier token, it can't be macro expanded, so
297 ++NumFastMacroExpanded
;
301 // Start expanding the macro.
302 EnterMacro(Identifier
, InstantiationEnd
, Args
);
304 // Now that the macro is at the top of the include stack, ask the
305 // preprocessor to read the next token from it.
310 /// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next
311 /// token is the '(' of the macro, this method is invoked to read all of the
312 /// actual arguments specified for the macro invocation. This returns null on
314 MacroArgs
*Preprocessor::ReadFunctionLikeMacroArgs(Token
&MacroName
,
316 SourceLocation
&MacroEnd
) {
317 // The number of fixed arguments to parse.
318 unsigned NumFixedArgsLeft
= MI
->getNumArgs();
319 bool isVariadic
= MI
->isVariadic();
321 // Outer loop, while there are more arguments, keep reading them.
324 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
325 // an argument value in a macro could expand to ',' or '(' or ')'.
326 LexUnexpandedToken(Tok
);
327 assert(Tok
.is(tok::l_paren
) && "Error computing l-paren-ness?");
329 // ArgTokens - Build up a list of tokens that make up each argument. Each
330 // argument is separated by an EOF token. Use a SmallVector so we can avoid
331 // heap allocations in the common case.
332 llvm::SmallVector
<Token
, 64> ArgTokens
;
334 unsigned NumActuals
= 0;
335 while (Tok
.isNot(tok::r_paren
)) {
336 assert((Tok
.is(tok::l_paren
) || Tok
.is(tok::comma
)) &&
337 "only expect argument separators here");
339 unsigned ArgTokenStart
= ArgTokens
.size();
340 SourceLocation ArgStartLoc
= Tok
.getLocation();
342 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
343 // that we already consumed the first one.
344 unsigned NumParens
= 0;
347 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
348 // an argument value in a macro could expand to ',' or '(' or ')'.
349 LexUnexpandedToken(Tok
);
351 if (Tok
.is(tok::code_completion
)) {
353 CodeComplete
->CodeCompleteMacroArgument(MacroName
.getIdentifierInfo(),
355 LexUnexpandedToken(Tok
);
358 if (Tok
.is(tok::eof
) || Tok
.is(tok::eom
)) { // "#if f(<eof>" & "#if f(\n"
359 Diag(MacroName
, diag::err_unterm_macro_invoc
);
360 // Do not lose the EOF/EOM. Return it to the client.
363 } else if (Tok
.is(tok::r_paren
)) {
364 // If we found the ) token, the macro arg list is done.
365 if (NumParens
-- == 0) {
366 MacroEnd
= Tok
.getLocation();
369 } else if (Tok
.is(tok::l_paren
)) {
371 } else if (Tok
.is(tok::comma
) && NumParens
== 0) {
372 // Comma ends this argument if there are more fixed arguments expected.
373 // However, if this is a variadic macro, and this is part of the
374 // variadic part, then the comma is just an argument token.
375 if (!isVariadic
) break;
376 if (NumFixedArgsLeft
> 1)
378 } else if (Tok
.is(tok::comment
) && !KeepMacroComments
) {
379 // If this is a comment token in the argument list and we're just in
380 // -C mode (not -CC mode), discard the comment.
382 } else if (Tok
.getIdentifierInfo() != 0) {
383 // Reading macro arguments can cause macros that we are currently
384 // expanding from to be popped off the expansion stack. Doing so causes
385 // them to be reenabled for expansion. Here we record whether any
386 // identifiers we lex as macro arguments correspond to disabled macros.
387 // If so, we mark the token as noexpand. This is a subtle aspect of
389 if (MacroInfo
*MI
= getMacroInfo(Tok
.getIdentifierInfo()))
390 if (!MI
->isEnabled())
391 Tok
.setFlag(Token::DisableExpand
);
393 ArgTokens
.push_back(Tok
);
396 // If this was an empty argument list foo(), don't add this as an empty
398 if (ArgTokens
.empty() && Tok
.getKind() == tok::r_paren
)
401 // If this is not a variadic macro, and too many args were specified, emit
403 if (!isVariadic
&& NumFixedArgsLeft
== 0) {
404 if (ArgTokens
.size() != ArgTokenStart
)
405 ArgStartLoc
= ArgTokens
[ArgTokenStart
].getLocation();
407 // Emit the diagnostic at the macro name in case there is a missing ).
408 // Emitting it at the , could be far away from the macro name.
409 Diag(ArgStartLoc
, diag::err_too_many_args_in_macro_invoc
);
413 // Empty arguments are standard in C99 and supported as an extension in
415 if (ArgTokens
.size() == ArgTokenStart
&& !Features
.C99
)
416 Diag(Tok
, diag::ext_empty_fnmacro_arg
);
418 // Add a marker EOF token to the end of the token list for this argument.
421 EOFTok
.setKind(tok::eof
);
422 EOFTok
.setLocation(Tok
.getLocation());
424 ArgTokens
.push_back(EOFTok
);
426 assert(NumFixedArgsLeft
!= 0 && "Too many arguments parsed");
430 // Okay, we either found the r_paren. Check to see if we parsed too few
432 unsigned MinArgsExpected
= MI
->getNumArgs();
434 // See MacroArgs instance var for description of this.
435 bool isVarargsElided
= false;
437 if (NumActuals
< MinArgsExpected
) {
438 // There are several cases where too few arguments is ok, handle them now.
439 if (NumActuals
== 0 && MinArgsExpected
== 1) {
440 // #define A(X) or #define A(...) ---> A()
442 // If there is exactly one argument, and that argument is missing,
443 // then we have an empty "()" argument empty list. This is fine, even if
444 // the macro expects one argument (the argument is just empty).
445 isVarargsElided
= MI
->isVariadic();
446 } else if (MI
->isVariadic() &&
447 (NumActuals
+1 == MinArgsExpected
|| // A(x, ...) -> A(X)
448 (NumActuals
== 0 && MinArgsExpected
== 2))) {// A(x,...) -> A()
449 // Varargs where the named vararg parameter is missing: ok as extension.
452 Diag(Tok
, diag::ext_missing_varargs_arg
);
454 // Remember this occurred, allowing us to elide the comma when used for
456 // #define A(x, foo...) blah(a, ## foo)
457 // #define B(x, ...) blah(a, ## __VA_ARGS__)
458 // #define C(...) blah(a, ## __VA_ARGS__)
460 isVarargsElided
= true;
462 // Otherwise, emit the error.
463 Diag(Tok
, diag::err_too_few_args_in_macro_invoc
);
467 // Add a marker EOF token to the end of the token list for this argument.
468 SourceLocation EndLoc
= Tok
.getLocation();
470 Tok
.setKind(tok::eof
);
471 Tok
.setLocation(EndLoc
);
473 ArgTokens
.push_back(Tok
);
475 // If we expect two arguments, add both as empty.
476 if (NumActuals
== 0 && MinArgsExpected
== 2)
477 ArgTokens
.push_back(Tok
);
479 } else if (NumActuals
> MinArgsExpected
&& !MI
->isVariadic()) {
480 // Emit the diagnostic at the macro name in case there is a missing ).
481 // Emitting it at the , could be far away from the macro name.
482 Diag(MacroName
, diag::err_too_many_args_in_macro_invoc
);
486 return MacroArgs::create(MI
, ArgTokens
.data(), ArgTokens
.size(),
487 isVarargsElided
, *this);
490 /// ComputeDATE_TIME - Compute the current time, enter it into the specified
491 /// scratch buffer, then return DATELoc/TIMELoc locations with the position of
492 /// the identifier tokens inserted.
493 static void ComputeDATE_TIME(SourceLocation
&DATELoc
, SourceLocation
&TIMELoc
,
496 struct tm
*TM
= localtime(&TT
);
498 static const char * const Months
[] = {
499 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
504 sprintf(TmpBuffer
, "\"%s %2d %4d\"", Months
[TM
->tm_mon
], TM
->tm_mday
,
507 snprintf(TmpBuffer
, sizeof(TmpBuffer
), "\"%s %2d %4d\"", Months
[TM
->tm_mon
], TM
->tm_mday
,
513 PP
.CreateString(TmpBuffer
, strlen(TmpBuffer
), TmpTok
);
514 DATELoc
= TmpTok
.getLocation();
517 sprintf(TmpBuffer
, "\"%02d:%02d:%02d\"", TM
->tm_hour
, TM
->tm_min
, TM
->tm_sec
);
519 snprintf(TmpBuffer
, sizeof(TmpBuffer
), "\"%02d:%02d:%02d\"", TM
->tm_hour
, TM
->tm_min
, TM
->tm_sec
);
521 PP
.CreateString(TmpBuffer
, strlen(TmpBuffer
), TmpTok
);
522 TIMELoc
= TmpTok
.getLocation();
526 /// HasFeature - Return true if we recognize and implement the specified feature
527 /// specified by the identifier.
528 static bool HasFeature(const Preprocessor
&PP
, const IdentifierInfo
*II
) {
529 const LangOptions
&LangOpts
= PP
.getLangOptions();
531 return llvm::StringSwitch
<bool>(II
->getName())
532 .Case("attribute_analyzer_noreturn", true)
533 .Case("attribute_cf_returns_not_retained", true)
534 .Case("attribute_cf_returns_retained", true)
535 .Case("attribute_deprecated_with_message", true)
536 .Case("attribute_ext_vector_type", true)
537 .Case("attribute_ns_returns_not_retained", true)
538 .Case("attribute_ns_returns_retained", true)
539 .Case("attribute_objc_ivar_unused", true)
540 .Case("attribute_overloadable", true)
541 .Case("attribute_unavailable_with_message", true)
542 .Case("blocks", LangOpts
.Blocks
)
543 .Case("cxx_attributes", LangOpts
.CPlusPlus0x
)
544 .Case("cxx_auto_type", LangOpts
.CPlusPlus0x
)
545 .Case("cxx_decltype", LangOpts
.CPlusPlus0x
)
546 .Case("cxx_deleted_functions", true) // Accepted as an extension.
547 .Case("cxx_exceptions", LangOpts
.Exceptions
)
548 .Case("cxx_rtti", LangOpts
.RTTI
)
549 .Case("cxx_strong_enums", LangOpts
.CPlusPlus0x
)
550 .Case("cxx_static_assert", LangOpts
.CPlusPlus0x
)
551 .Case("cxx_trailing_return", LangOpts
.CPlusPlus0x
)
552 .Case("enumerator_attributes", true)
553 .Case("objc_nonfragile_abi", LangOpts
.ObjCNonFragileABI
)
554 .Case("objc_weak_class", LangOpts
.ObjCNonFragileABI
)
555 .Case("ownership_holds", true)
556 .Case("ownership_returns", true)
557 .Case("ownership_takes", true)
558 .Case("cxx_inline_namespaces", true)
559 //.Case("cxx_concepts", false)
560 //.Case("cxx_lambdas", false)
561 //.Case("cxx_nullptr", false)
562 //.Case("cxx_rvalue_references", false)
563 //.Case("cxx_variadic_templates", false)
564 .Case("tls", PP
.getTargetInfo().isTLSSupported())
568 /// HasAttribute - Return true if we recognize and implement the attribute
569 /// specified by the given identifier.
570 static bool HasAttribute(const IdentifierInfo
*II
) {
571 return llvm::StringSwitch
<bool>(II
->getName())
572 #include "clang/Lex/AttrSpellings.inc"
576 /// EvaluateHasIncludeCommon - Process a '__has_include("path")'
577 /// or '__has_include_next("path")' expression.
578 /// Returns true if successful.
579 static bool EvaluateHasIncludeCommon(bool &Result
, Token
&Tok
,
580 IdentifierInfo
*II
, Preprocessor
&PP
,
581 const DirectoryLookup
*LookupFrom
) {
582 SourceLocation LParenLoc
;
585 PP
.LexNonComment(Tok
);
587 // Ensure we have a '('.
588 if (Tok
.isNot(tok::l_paren
)) {
589 PP
.Diag(Tok
.getLocation(), diag::err_pp_missing_lparen
) << II
->getName();
593 // Save '(' location for possible missing ')' message.
594 LParenLoc
= Tok
.getLocation();
596 // Get the file name.
597 PP
.getCurrentLexer()->LexIncludeFilename(Tok
);
599 // Reserve a buffer to get the spelling.
600 llvm::SmallString
<128> FilenameBuffer
;
601 llvm::StringRef Filename
;
602 SourceLocation EndLoc
;
604 switch (Tok
.getKind()) {
606 // If the token kind is EOM, the error has already been diagnosed.
609 case tok::angle_string_literal
:
610 case tok::string_literal
: {
611 bool Invalid
= false;
612 Filename
= PP
.getSpelling(Tok
, FilenameBuffer
, &Invalid
);
619 // This could be a <foo/bar.h> file coming from a macro expansion. In this
620 // case, glue the tokens together into FilenameBuffer and interpret those.
621 FilenameBuffer
.push_back('<');
622 if (PP
.ConcatenateIncludeName(FilenameBuffer
, EndLoc
))
623 return false; // Found <eom> but no ">"? Diagnostic already emitted.
624 Filename
= FilenameBuffer
.str();
627 PP
.Diag(Tok
.getLocation(), diag::err_pp_expects_filename
);
631 bool isAngled
= PP
.GetIncludeFilenameSpelling(Tok
.getLocation(), Filename
);
632 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
634 if (Filename
.empty())
637 // Search include directories.
638 const DirectoryLookup
*CurDir
;
639 const FileEntry
*File
= PP
.LookupFile(Filename
, isAngled
, LookupFrom
, CurDir
);
641 // Get the result value. Result = true means the file exists.
645 PP
.LexNonComment(Tok
);
647 // Ensure we have a trailing ).
648 if (Tok
.isNot(tok::r_paren
)) {
649 PP
.Diag(Tok
.getLocation(), diag::err_pp_missing_rparen
) << II
->getName();
650 PP
.Diag(LParenLoc
, diag::note_matching
) << "(";
657 /// EvaluateHasInclude - Process a '__has_include("path")' expression.
658 /// Returns true if successful.
659 static bool EvaluateHasInclude(bool &Result
, Token
&Tok
, IdentifierInfo
*II
,
661 return(EvaluateHasIncludeCommon(Result
, Tok
, II
, PP
, NULL
));
664 /// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.
665 /// Returns true if successful.
666 static bool EvaluateHasIncludeNext(bool &Result
, Token
&Tok
,
667 IdentifierInfo
*II
, Preprocessor
&PP
) {
668 // __has_include_next is like __has_include, except that we start
669 // searching after the current found directory. If we can't do this,
670 // issue a diagnostic.
671 const DirectoryLookup
*Lookup
= PP
.GetCurDirLookup();
672 if (PP
.isInPrimaryFile()) {
674 PP
.Diag(Tok
, diag::pp_include_next_in_primary
);
675 } else if (Lookup
== 0) {
676 PP
.Diag(Tok
, diag::pp_include_next_absolute_path
);
678 // Start looking up in the next directory.
682 return(EvaluateHasIncludeCommon(Result
, Tok
, II
, PP
, Lookup
));
685 /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
686 /// as a builtin macro, handle it and return the next token as 'Tok'.
687 void Preprocessor::ExpandBuiltinMacro(Token
&Tok
) {
688 // Figure out which token this is.
689 IdentifierInfo
*II
= Tok
.getIdentifierInfo();
690 assert(II
&& "Can't be a macro without id info!");
692 // If this is an _Pragma or Microsoft __pragma directive, expand it,
693 // invoke the pragma handler, then lex the token after it.
694 if (II
== Ident_Pragma
)
695 return Handle_Pragma(Tok
);
696 else if (II
== Ident__pragma
) // in non-MS mode this is null
697 return HandleMicrosoft__pragma(Tok
);
699 ++NumBuiltinMacroExpanded
;
701 llvm::SmallString
<128> TmpBuffer
;
702 llvm::raw_svector_ostream
OS(TmpBuffer
);
704 // Set up the return result.
705 Tok
.setIdentifierInfo(0);
706 Tok
.clearFlag(Token::NeedsCleaning
);
708 if (II
== Ident__LINE__
) {
709 // C99 6.10.8: "__LINE__: The presumed line number (within the current
710 // source file) of the current source line (an integer constant)". This can
711 // be affected by #line.
712 SourceLocation Loc
= Tok
.getLocation();
714 // Advance to the location of the first _, this might not be the first byte
715 // of the token if it starts with an escaped newline.
716 Loc
= AdvanceToTokenCharacter(Loc
, 0);
718 // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
719 // a macro instantiation. This doesn't matter for object-like macros, but
720 // can matter for a function-like macro that expands to contain __LINE__.
721 // Skip down through instantiation points until we find a file loc for the
722 // end of the instantiation history.
723 Loc
= SourceMgr
.getInstantiationRange(Loc
).second
;
724 PresumedLoc PLoc
= SourceMgr
.getPresumedLoc(Loc
);
726 // __LINE__ expands to a simple numeric value.
727 OS
<< (PLoc
.isValid()? PLoc
.getLine() : 1);
728 Tok
.setKind(tok::numeric_constant
);
729 } else if (II
== Ident__FILE__
|| II
== Ident__BASE_FILE__
) {
730 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
731 // character string literal)". This can be affected by #line.
732 PresumedLoc PLoc
= SourceMgr
.getPresumedLoc(Tok
.getLocation());
734 // __BASE_FILE__ is a GNU extension that returns the top of the presumed
735 // #include stack instead of the current file.
736 if (II
== Ident__BASE_FILE__
&& PLoc
.isValid()) {
737 SourceLocation NextLoc
= PLoc
.getIncludeLoc();
738 while (NextLoc
.isValid()) {
739 PLoc
= SourceMgr
.getPresumedLoc(NextLoc
);
740 if (PLoc
.isInvalid())
743 NextLoc
= PLoc
.getIncludeLoc();
747 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
748 llvm::SmallString
<128> FN
;
749 if (PLoc
.isValid()) {
750 FN
+= PLoc
.getFilename();
751 Lexer::Stringify(FN
);
752 OS
<< '"' << FN
.str() << '"';
754 Tok
.setKind(tok::string_literal
);
755 } else if (II
== Ident__DATE__
) {
756 if (!DATELoc
.isValid())
757 ComputeDATE_TIME(DATELoc
, TIMELoc
, *this);
758 Tok
.setKind(tok::string_literal
);
759 Tok
.setLength(strlen("\"Mmm dd yyyy\""));
760 Tok
.setLocation(SourceMgr
.createInstantiationLoc(DATELoc
, Tok
.getLocation(),
764 } else if (II
== Ident__TIME__
) {
765 if (!TIMELoc
.isValid())
766 ComputeDATE_TIME(DATELoc
, TIMELoc
, *this);
767 Tok
.setKind(tok::string_literal
);
768 Tok
.setLength(strlen("\"hh:mm:ss\""));
769 Tok
.setLocation(SourceMgr
.createInstantiationLoc(TIMELoc
, Tok
.getLocation(),
773 } else if (II
== Ident__INCLUDE_LEVEL__
) {
774 // Compute the presumed include depth of this token. This can be affected
775 // by GNU line markers.
778 PresumedLoc PLoc
= SourceMgr
.getPresumedLoc(Tok
.getLocation());
779 if (PLoc
.isValid()) {
780 PLoc
= SourceMgr
.getPresumedLoc(PLoc
.getIncludeLoc());
781 for (; PLoc
.isValid(); ++Depth
)
782 PLoc
= SourceMgr
.getPresumedLoc(PLoc
.getIncludeLoc());
785 // __INCLUDE_LEVEL__ expands to a simple numeric value.
787 Tok
.setKind(tok::numeric_constant
);
788 } else if (II
== Ident__TIMESTAMP__
) {
789 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
790 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
792 // Get the file that we are lexing out of. If we're currently lexing from
793 // a macro, dig into the include stack.
794 const FileEntry
*CurFile
= 0;
795 PreprocessorLexer
*TheLexer
= getCurrentFileLexer();
798 CurFile
= SourceMgr
.getFileEntryForID(TheLexer
->getFileID());
802 time_t TT
= CurFile
->getModificationTime();
803 struct tm
*TM
= localtime(&TT
);
804 Result
= asctime(TM
);
806 Result
= "??? ??? ?? ??:??:?? ????\n";
808 // Surround the string with " and strip the trailing newline.
809 OS
<< '"' << llvm::StringRef(Result
, strlen(Result
)-1) << '"';
810 Tok
.setKind(tok::string_literal
);
811 } else if (II
== Ident__COUNTER__
) {
812 // __COUNTER__ expands to a simple numeric value.
813 OS
<< CounterValue
++;
814 Tok
.setKind(tok::numeric_constant
);
815 } else if (II
== Ident__has_feature
||
816 II
== Ident__has_builtin
||
817 II
== Ident__has_attribute
) {
818 // The argument to these two builtins should be a parenthesized identifier.
819 SourceLocation StartLoc
= Tok
.getLocation();
821 bool IsValid
= false;
822 IdentifierInfo
*FeatureII
= 0;
826 if (Tok
.is(tok::l_paren
)) {
827 // Read the identifier
829 if (Tok
.is(tok::identifier
)) {
830 FeatureII
= Tok
.getIdentifierInfo();
834 if (Tok
.is(tok::r_paren
))
841 Diag(StartLoc
, diag::err_feature_check_malformed
);
842 else if (II
== Ident__has_builtin
) {
843 // Check for a builtin is trivial.
844 Value
= FeatureII
->getBuiltinID() != 0;
845 } else if (II
== Ident__has_attribute
)
846 Value
= HasAttribute(FeatureII
);
848 assert(II
== Ident__has_feature
&& "Must be feature check");
849 Value
= HasFeature(*this, FeatureII
);
853 Tok
.setKind(tok::numeric_constant
);
854 } else if (II
== Ident__has_include
||
855 II
== Ident__has_include_next
) {
856 // The argument to these two builtins should be a parenthesized
857 // file name string literal using angle brackets (<>) or
858 // double-quotes ("").
861 if (II
== Ident__has_include
)
862 IsValid
= EvaluateHasInclude(Value
, Tok
, II
, *this);
864 IsValid
= EvaluateHasIncludeNext(Value
, Tok
, II
, *this);
866 Tok
.setKind(tok::numeric_constant
);
868 assert(0 && "Unknown identifier!");
870 CreateString(OS
.str().data(), OS
.str().size(), Tok
, Tok
.getLocation());