1 //===--- MacroExpansion.cpp - Top level Macro Expansion -------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the top level handling of macro expasion for the
11 // preprocessor.
12 //
13 //===----------------------------------------------------------------------===//
14 
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/Lex/LexDiagnostic.h"
21 #include <cstdio>
22 #include <ctime>
23 using namespace clang;
24 
25 /// setMacroInfo - Specify a macro for this identifier.
26 ///
27 void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
28   if (MI) {
29     Macros[II] = MI;
30     II->setHasMacroDefinition(true);
31   } else if (II->hasMacroDefinition()) {
32     Macros.erase(II);
33     II->setHasMacroDefinition(false);
34   }
35 }
36 
37 /// RegisterBuiltinMacro - Register the specified identifier in the identifier
38 /// table and mark it as a builtin macro to be expanded.
39 static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
40   // Get the identifier.
41   IdentifierInfo *Id = PP.getIdentifierInfo(Name);
42 
43   // Mark it as being a macro that is builtin.
44   MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
45   MI->setIsBuiltinMacro();
46   PP.setMacroInfo(Id, MI);
47   return Id;
48 }
49 
50 
51 /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
52 /// identifier table.
53 void Preprocessor::RegisterBuiltinMacros() {
54   Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
55   Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");
56   Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");
57   Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__");
58   Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
59   Ident_Pragma  = RegisterBuiltinMacro(*this, "_Pragma");
60 
61   // GCC Extensions.
62   Ident__BASE_FILE__     = RegisterBuiltinMacro(*this, "__BASE_FILE__");
63   Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
64   Ident__TIMESTAMP__     = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
65 
66   // Clang Extensions.
67   Ident__has_feature     = RegisterBuiltinMacro(*this, "__has_feature");
68   Ident__has_builtin     = RegisterBuiltinMacro(*this, "__has_builtin");
69 }
70 
71 /// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
72 /// in its expansion, currently expands to that token literally.
73 static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
74                                           const IdentifierInfo *MacroIdent,
75                                           Preprocessor &PP) {
76   IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
77 
78   // If the token isn't an identifier, it's always literally expanded.
79   if (II == 0) return true;
80 
81   // If the identifier is a macro, and if that macro is enabled, it may be
82   // expanded so it's not a trivial expansion.
83   if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
84       // Fast expanding "#define X X" is ok, because X would be disabled.
85       II != MacroIdent)
86     return false;
87 
88   // If this is an object-like macro invocation, it is safe to trivially expand
89   // it.
90   if (MI->isObjectLike()) return true;
91 
92   // If this is a function-like macro invocation, it's safe to trivially expand
93   // as long as the identifier is not a macro argument.
94   for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
95        I != E; ++I)
96     if (*I == II)
97       return false;   // Identifier is a macro argument.
98 
99   return true;
100 }
101 
102 
103 /// isNextPPTokenLParen - Determine whether the next preprocessor token to be
104 /// lexed is a '('.  If so, consume the token and return true, if not, this
105 /// method should have no observable side-effect on the lexed tokens.
106 bool Preprocessor::isNextPPTokenLParen() {
107   // Do some quick tests for rejection cases.
108   unsigned Val;
109   if (CurLexer)
110     Val = CurLexer->isNextPPTokenLParen();
111   else if (CurPTHLexer)
112     Val = CurPTHLexer->isNextPPTokenLParen();
113   else
114     Val = CurTokenLexer->isNextTokenLParen();
115 
116   if (Val == 2) {
117     // We have run off the end.  If it's a source file we don't
118     // examine enclosing ones (C99 5.1.1.2p4).  Otherwise walk up the
119     // macro stack.
120     if (CurPPLexer)
121       return false;
122     for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
123       IncludeStackInfo &Entry = IncludeMacroStack[i-1];
124       if (Entry.TheLexer)
125         Val = Entry.TheLexer->isNextPPTokenLParen();
126       else if (Entry.ThePTHLexer)
127         Val = Entry.ThePTHLexer->isNextPPTokenLParen();
128       else
129         Val = Entry.TheTokenLexer->isNextTokenLParen();
130 
131       if (Val != 2)
132         break;
133 
134       // Ran off the end of a source file?
135       if (Entry.ThePPLexer)
136         return false;
137     }
138   }
139 
140   // Okay, if we know that the token is a '(', lex it and return.  Otherwise we
141   // have found something that isn't a '(' or we found the end of the
142   // translation unit.  In either case, return false.
143   return Val == 1;
144 }
145 
146 /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
147 /// expanded as a macro, handle it and return the next token as 'Identifier'.
148 bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
149                                                  MacroInfo *MI) {
150   if (Callbacks) Callbacks->MacroExpands(Identifier, MI);
151 
152   // If this is a macro exapnsion in the "#if !defined(x)" line for the file,
153   // then the macro could expand to different things in other contexts, we need
154   // to disable the optimization in this case.
155   if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
156 
157   // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
158   if (MI->isBuiltinMacro()) {
159     ExpandBuiltinMacro(Identifier);
160     return false;
161   }
162 
163   /// Args - If this is a function-like macro expansion, this contains,
164   /// for each macro argument, the list of tokens that were provided to the
165   /// invocation.
166   MacroArgs *Args = 0;
167 
168   // Remember where the end of the instantiation occurred.  For an object-like
169   // macro, this is the identifier.  For a function-like macro, this is the ')'.
170   SourceLocation InstantiationEnd = Identifier.getLocation();
171 
172   // If this is a function-like macro, read the arguments.
173   if (MI->isFunctionLike()) {
174     // C99 6.10.3p10: If the preprocessing token immediately after the the macro
175     // name isn't a '(', this macro should not be expanded.
176     if (!isNextPPTokenLParen())
177       return true;
178 
179     // Remember that we are now parsing the arguments to a macro invocation.
180     // Preprocessor directives used inside macro arguments are not portable, and
181     // this enables the warning.
182     InMacroArgs = true;
183     Args = ReadFunctionLikeMacroArgs(Identifier, MI, InstantiationEnd);
184 
185     // Finished parsing args.
186     InMacroArgs = false;
187 
188     // If there was an error parsing the arguments, bail out.
189     if (Args == 0) return false;
190 
191     ++NumFnMacroExpanded;
192   } else {
193     ++NumMacroExpanded;
194   }
195 
196   // Notice that this macro has been used.
197   MI->setIsUsed(true);
198 
199   // If we started lexing a macro, enter the macro expansion body.
200 
201   // If this macro expands to no tokens, don't bother to push it onto the
202   // expansion stack, only to take it right back off.
203   if (MI->getNumTokens() == 0) {
204     // No need for arg info.
205     if (Args) Args->destroy();
206 
207     // Ignore this macro use, just return the next token in the current
208     // buffer.
209     bool HadLeadingSpace = Identifier.hasLeadingSpace();
210     bool IsAtStartOfLine = Identifier.isAtStartOfLine();
211 
212     Lex(Identifier);
213 
214     // If the identifier isn't on some OTHER line, inherit the leading
215     // whitespace/first-on-a-line property of this token.  This handles
216     // stuff like "! XX," -> "! ," and "   XX," -> "    ,", when XX is
217     // empty.
218     if (!Identifier.isAtStartOfLine()) {
219       if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
220       if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
221     }
222     ++NumFastMacroExpanded;
223     return false;
224 
225   } else if (MI->getNumTokens() == 1 &&
226              isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
227                                            *this)) {
228     // Otherwise, if this macro expands into a single trivially-expanded
229     // token: expand it now.  This handles common cases like
230     // "#define VAL 42".
231 
232     // No need for arg info.
233     if (Args) Args->destroy();
234 
235     // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
236     // identifier to the expanded token.
237     bool isAtStartOfLine = Identifier.isAtStartOfLine();
238     bool hasLeadingSpace = Identifier.hasLeadingSpace();
239 
240     // Remember where the token is instantiated.
241     SourceLocation InstantiateLoc = Identifier.getLocation();
242 
243     // Replace the result token.
244     Identifier = MI->getReplacementToken(0);
245 
246     // Restore the StartOfLine/LeadingSpace markers.
247     Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
248     Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
249 
250     // Update the tokens location to include both its instantiation and physical
251     // locations.
252     SourceLocation Loc =
253       SourceMgr.createInstantiationLoc(Identifier.getLocation(), InstantiateLoc,
254                                        InstantiationEnd,Identifier.getLength());
255     Identifier.setLocation(Loc);
256 
257     // If this is #define X X, we must mark the result as unexpandible.
258     if (IdentifierInfo *NewII = Identifier.getIdentifierInfo())
259       if (getMacroInfo(NewII) == MI)
260         Identifier.setFlag(Token::DisableExpand);
261 
262     // Since this is not an identifier token, it can't be macro expanded, so
263     // we're done.
264     ++NumFastMacroExpanded;
265     return false;
266   }
267 
268   // Start expanding the macro.
269   EnterMacro(Identifier, InstantiationEnd, Args);
270 
271   // Now that the macro is at the top of the include stack, ask the
272   // preprocessor to read the next token from it.
273   Lex(Identifier);
274   return false;
275 }
276 
277 /// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next
278 /// token is the '(' of the macro, this method is invoked to read all of the
279 /// actual arguments specified for the macro invocation.  This returns null on
280 /// error.
281 MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
282                                                    MacroInfo *MI,
283                                                    SourceLocation &MacroEnd) {
284   // The number of fixed arguments to parse.
285   unsigned NumFixedArgsLeft = MI->getNumArgs();
286   bool isVariadic = MI->isVariadic();
287 
288   // Outer loop, while there are more arguments, keep reading them.
289   Token Tok;
290 
291   // Read arguments as unexpanded tokens.  This avoids issues, e.g., where
292   // an argument value in a macro could expand to ',' or '(' or ')'.
293   LexUnexpandedToken(Tok);
294   assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
295 
296   // ArgTokens - Build up a list of tokens that make up each argument.  Each
297   // argument is separated by an EOF token.  Use a SmallVector so we can avoid
298   // heap allocations in the common case.
299   llvm::SmallVector<Token, 64> ArgTokens;
300 
301   unsigned NumActuals = 0;
302   while (Tok.isNot(tok::r_paren)) {
303     assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) &&
304            "only expect argument separators here");
305 
306     unsigned ArgTokenStart = ArgTokens.size();
307     SourceLocation ArgStartLoc = Tok.getLocation();
308 
309     // C99 6.10.3p11: Keep track of the number of l_parens we have seen.  Note
310     // that we already consumed the first one.
311     unsigned NumParens = 0;
312 
313     while (1) {
314       // Read arguments as unexpanded tokens.  This avoids issues, e.g., where
315       // an argument value in a macro could expand to ',' or '(' or ')'.
316       LexUnexpandedToken(Tok);
317 
318       if (Tok.is(tok::eof) || Tok.is(tok::eom)) { // "#if f(<eof>" & "#if f(\n"
319         Diag(MacroName, diag::err_unterm_macro_invoc);
320         // Do not lose the EOF/EOM.  Return it to the client.
321         MacroName = Tok;
322         return 0;
323       } else if (Tok.is(tok::r_paren)) {
324         // If we found the ) token, the macro arg list is done.
325         if (NumParens-- == 0) {
326           MacroEnd = Tok.getLocation();
327           break;
328         }
329       } else if (Tok.is(tok::l_paren)) {
330         ++NumParens;
331       } else if (Tok.is(tok::comma) && NumParens == 0) {
332         // Comma ends this argument if there are more fixed arguments expected.
333         // However, if this is a variadic macro, and this is part of the
334         // variadic part, then the comma is just an argument token.
335         if (!isVariadic) break;
336         if (NumFixedArgsLeft > 1)
337           break;
338       } else if (Tok.is(tok::comment) && !KeepMacroComments) {
339         // If this is a comment token in the argument list and we're just in
340         // -C mode (not -CC mode), discard the comment.
341         continue;
342       } else if (Tok.getIdentifierInfo() != 0) {
343         // Reading macro arguments can cause macros that we are currently
344         // expanding from to be popped off the expansion stack.  Doing so causes
345         // them to be reenabled for expansion.  Here we record whether any
346         // identifiers we lex as macro arguments correspond to disabled macros.
347         // If so, we mark the token as noexpand.  This is a subtle aspect of
348         // C99 6.10.3.4p2.
349         if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
350           if (!MI->isEnabled())
351             Tok.setFlag(Token::DisableExpand);
352       }
353       ArgTokens.push_back(Tok);
354     }
355 
356     // If this was an empty argument list foo(), don't add this as an empty
357     // argument.
358     if (ArgTokens.empty() && Tok.getKind() == tok::r_paren)
359       break;
360 
361     // If this is not a variadic macro, and too many args were specified, emit
362     // an error.
363     if (!isVariadic && NumFixedArgsLeft == 0) {
364       if (ArgTokens.size() != ArgTokenStart)
365         ArgStartLoc = ArgTokens[ArgTokenStart].getLocation();
366 
367       // Emit the diagnostic at the macro name in case there is a missing ).
368       // Emitting it at the , could be far away from the macro name.
369       Diag(ArgStartLoc, diag::err_too_many_args_in_macro_invoc);
370       return 0;
371     }
372 
373     // Empty arguments are standard in C99 and supported as an extension in
374     // other modes.
375     if (ArgTokens.size() == ArgTokenStart && !Features.C99)
376       Diag(Tok, diag::ext_empty_fnmacro_arg);
377 
378     // Add a marker EOF token to the end of the token list for this argument.
379     Token EOFTok;
380     EOFTok.startToken();
381     EOFTok.setKind(tok::eof);
382     EOFTok.setLocation(Tok.getLocation());
383     EOFTok.setLength(0);
384     ArgTokens.push_back(EOFTok);
385     ++NumActuals;
386     assert(NumFixedArgsLeft != 0 && "Too many arguments parsed");
387     --NumFixedArgsLeft;
388   }
389 
390   // Okay, we either found the r_paren.  Check to see if we parsed too few
391   // arguments.
392   unsigned MinArgsExpected = MI->getNumArgs();
393 
394   // See MacroArgs instance var for description of this.
395   bool isVarargsElided = false;
396 
397   if (NumActuals < MinArgsExpected) {
398     // There are several cases where too few arguments is ok, handle them now.
399     if (NumActuals == 0 && MinArgsExpected == 1) {
400       // #define A(X)  or  #define A(...)   ---> A()
401 
402       // If there is exactly one argument, and that argument is missing,
403       // then we have an empty "()" argument empty list.  This is fine, even if
404       // the macro expects one argument (the argument is just empty).
405       isVarargsElided = MI->isVariadic();
406     } else if (MI->isVariadic() &&
407                (NumActuals+1 == MinArgsExpected ||  // A(x, ...) -> A(X)
408                 (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A()
409       // Varargs where the named vararg parameter is missing: ok as extension.
410       // #define A(x, ...)
411       // A("blah")
412       Diag(Tok, diag::ext_missing_varargs_arg);
413 
414       // Remember this occurred, allowing us to elide the comma when used for
415       // cases like:
416       //   #define A(x, foo...) blah(a, ## foo)
417       //   #define B(x, ...) blah(a, ## __VA_ARGS__)
418       //   #define C(...) blah(a, ## __VA_ARGS__)
419       //  A(x) B(x) C()
420       isVarargsElided = true;
421     } else {
422       // Otherwise, emit the error.
423       Diag(Tok, diag::err_too_few_args_in_macro_invoc);
424       return 0;
425     }
426 
427     // Add a marker EOF token to the end of the token list for this argument.
428     SourceLocation EndLoc = Tok.getLocation();
429     Tok.startToken();
430     Tok.setKind(tok::eof);
431     Tok.setLocation(EndLoc);
432     Tok.setLength(0);
433     ArgTokens.push_back(Tok);
434 
435     // If we expect two arguments, add both as empty.
436     if (NumActuals == 0 && MinArgsExpected == 2)
437       ArgTokens.push_back(Tok);
438 
439   } else if (NumActuals > MinArgsExpected && !MI->isVariadic()) {
440     // Emit the diagnostic at the macro name in case there is a missing ).
441     // Emitting it at the , could be far away from the macro name.
442     Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
443     return 0;
444   }
445 
446   return MacroArgs::create(MI, ArgTokens.data(), ArgTokens.size(),
447                            isVarargsElided);
448 }
449 
450 /// ComputeDATE_TIME - Compute the current time, enter it into the specified
451 /// scratch buffer, then return DATELoc/TIMELoc locations with the position of
452 /// the identifier tokens inserted.
453 static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
454                              Preprocessor &PP) {
455   time_t TT = time(0);
456   struct tm *TM = localtime(&TT);
457 
458   static const char * const Months[] = {
459     "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
460   };
461 
462   char TmpBuffer[100];
463   sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
464           TM->tm_year+1900);
465 
466   Token TmpTok;
467   TmpTok.startToken();
468   PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok);
469   DATELoc = TmpTok.getLocation();
470 
471   sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
472   PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok);
473   TIMELoc = TmpTok.getLocation();
474 }
475 
476 
477 /// HasFeature - Return true if we recognize and implement the specified feature
478 /// specified by the identifier.
479 static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) {
480   const LangOptions &LangOpts = PP.getLangOptions();
481 
482   switch (II->getLength()) {
483   default: return false;
484   case 6:
485     if (II->isStr("blocks")) return LangOpts.Blocks;
486     return false;
487   case 22:
488     if (II->isStr("attribute_overloadable")) return true;
489     return false;
490   case 25:
491     if (II->isStr("attribute_ext_vector_type")) return true;
492     return false;
493   case 27:
494     if (II->isStr("attribute_analyzer_noreturn")) return true;
495     return false;
496   case 29:
497     if (II->isStr("attribute_ns_returns_retained")) return true;
498     if (II->isStr("attribute_cf_returns_retained")) return true;
499     return false;
500   }
501 }
502 
503 
504 /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
505 /// as a builtin macro, handle it and return the next token as 'Tok'.
506 void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
507   // Figure out which token this is.
508   IdentifierInfo *II = Tok.getIdentifierInfo();
509   assert(II && "Can't be a macro without id info!");
510 
511   // If this is an _Pragma directive, expand it, invoke the pragma handler, then
512   // lex the token after it.
513   if (II == Ident_Pragma)
514     return Handle_Pragma(Tok);
515 
516   ++NumBuiltinMacroExpanded;
517 
518   char TmpBuffer[100];
519 
520   // Set up the return result.
521   Tok.setIdentifierInfo(0);
522   Tok.clearFlag(Token::NeedsCleaning);
523 
524   if (II == Ident__LINE__) {
525     // C99 6.10.8: "__LINE__: The presumed line number (within the current
526     // source file) of the current source line (an integer constant)".  This can
527     // be affected by #line.
528     SourceLocation Loc = Tok.getLocation();
529 
530     // Advance to the location of the first _, this might not be the first byte
531     // of the token if it starts with an escaped newline.
532     Loc = AdvanceToTokenCharacter(Loc, 0);
533 
534     // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
535     // a macro instantiation.  This doesn't matter for object-like macros, but
536     // can matter for a function-like macro that expands to contain __LINE__.
537     // Skip down through instantiation points until we find a file loc for the
538     // end of the instantiation history.
539     Loc = SourceMgr.getInstantiationRange(Loc).second;
540     PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
541 
542     // __LINE__ expands to a simple numeric value.
543     sprintf(TmpBuffer, "%u", PLoc.getLine());
544     Tok.setKind(tok::numeric_constant);
545     CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
546   } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
547     // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
548     // character string literal)". This can be affected by #line.
549     PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
550 
551     // __BASE_FILE__ is a GNU extension that returns the top of the presumed
552     // #include stack instead of the current file.
553     if (II == Ident__BASE_FILE__) {
554       Diag(Tok, diag::ext_pp_base_file);
555       SourceLocation NextLoc = PLoc.getIncludeLoc();
556       while (NextLoc.isValid()) {
557         PLoc = SourceMgr.getPresumedLoc(NextLoc);
558         NextLoc = PLoc.getIncludeLoc();
559       }
560     }
561 
562     // Escape this filename.  Turn '\' -> '\\' '"' -> '\"'
563     std::string FN = PLoc.getFilename();
564     FN = '"' + Lexer::Stringify(FN) + '"';
565     Tok.setKind(tok::string_literal);
566     CreateString(&FN[0], FN.size(), Tok, Tok.getLocation());
567   } else if (II == Ident__DATE__) {
568     if (!DATELoc.isValid())
569       ComputeDATE_TIME(DATELoc, TIMELoc, *this);
570     Tok.setKind(tok::string_literal);
571     Tok.setLength(strlen("\"Mmm dd yyyy\""));
572     Tok.setLocation(SourceMgr.createInstantiationLoc(DATELoc, Tok.getLocation(),
573                                                      Tok.getLocation(),
574                                                      Tok.getLength()));
575   } else if (II == Ident__TIME__) {
576     if (!TIMELoc.isValid())
577       ComputeDATE_TIME(DATELoc, TIMELoc, *this);
578     Tok.setKind(tok::string_literal);
579     Tok.setLength(strlen("\"hh:mm:ss\""));
580     Tok.setLocation(SourceMgr.createInstantiationLoc(TIMELoc, Tok.getLocation(),
581                                                      Tok.getLocation(),
582                                                      Tok.getLength()));
583   } else if (II == Ident__INCLUDE_LEVEL__) {
584     Diag(Tok, diag::ext_pp_include_level);
585 
586     // Compute the presumed include depth of this token.  This can be affected
587     // by GNU line markers.
588     unsigned Depth = 0;
589 
590     PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
591     PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
592     for (; PLoc.isValid(); ++Depth)
593       PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
594 
595     // __INCLUDE_LEVEL__ expands to a simple numeric value.
596     sprintf(TmpBuffer, "%u", Depth);
597     Tok.setKind(tok::numeric_constant);
598     CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
599   } else if (II == Ident__TIMESTAMP__) {
600     // MSVC, ICC, GCC, VisualAge C++ extension.  The generated string should be
601     // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
602     Diag(Tok, diag::ext_pp_timestamp);
603 
604     // Get the file that we are lexing out of.  If we're currently lexing from
605     // a macro, dig into the include stack.
606     const FileEntry *CurFile = 0;
607     PreprocessorLexer *TheLexer = getCurrentFileLexer();
608 
609     if (TheLexer)
610       CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
611 
612     // If this file is older than the file it depends on, emit a diagnostic.
613     const char *Result;
614     if (CurFile) {
615       time_t TT = CurFile->getModificationTime();
616       struct tm *TM = localtime(&TT);
617       Result = asctime(TM);
618     } else {
619       Result = "??? ??? ?? ??:??:?? ????\n";
620     }
621     TmpBuffer[0] = '"';
622     strcpy(TmpBuffer+1, Result);
623     unsigned Len = strlen(TmpBuffer);
624     TmpBuffer[Len] = '"';  // Replace the newline with a quote.
625     Tok.setKind(tok::string_literal);
626     CreateString(TmpBuffer, Len+1, Tok, Tok.getLocation());
627   } else if (II == Ident__COUNTER__) {
628     Diag(Tok, diag::ext_pp_counter);
629 
630     // __COUNTER__ expands to a simple numeric value.
631     sprintf(TmpBuffer, "%u", CounterValue++);
632     Tok.setKind(tok::numeric_constant);
633     CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
634   } else if (II == Ident__has_feature ||
635              II == Ident__has_builtin) {
636     // The argument to these two builtins should be a parenthesized identifier.
637     SourceLocation StartLoc = Tok.getLocation();
638 
639     bool IsValid = false;
640     IdentifierInfo *FeatureII = 0;
641 
642     // Read the '('.
643     Lex(Tok);
644     if (Tok.is(tok::l_paren)) {
645       // Read the identifier
646       Lex(Tok);
647       if (Tok.is(tok::identifier)) {
648         FeatureII = Tok.getIdentifierInfo();
649 
650         // Read the ')'.
651         Lex(Tok);
652         if (Tok.is(tok::r_paren))
653           IsValid = true;
654       }
655     }
656 
657     bool Value = false;
658     if (!IsValid)
659       Diag(StartLoc, diag::err_feature_check_malformed);
660     else if (II == Ident__has_builtin) {
661       // Check for a builtin is trivial.
662       Value = FeatureII->getBuiltinID() != 0;
663     } else {
664       assert(II == Ident__has_feature && "Must be feature check");
665       Value = HasFeature(*this, FeatureII);
666     }
667 
668     sprintf(TmpBuffer, "%d", (int)Value);
669     Tok.setKind(tok::numeric_constant);
670     CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
671   } else {
672     assert(0 && "Unknown identifier!");
673   }
674 }
675