PPMacroExpansion.cpp revision 3e4c6c4c79a03f5cb0c4671d7c282d623c6dc35e
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/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"
27#include <cstdio>
28#include <ctime>
29using namespace clang;
30
31MacroInfo *Preprocessor::getInfoForMacro(IdentifierInfo *II) const {
32  assert(II->hasMacroDefinition() && "Identifier is not a macro!");
33
34  llvm::DenseMap<IdentifierInfo*, MacroInfo*>::const_iterator Pos
35    = Macros.find(II);
36  if (Pos == Macros.end()) {
37    // Load this macro from the external source.
38    getExternalSource()->LoadMacroDefinition(II);
39    Pos = Macros.find(II);
40  }
41  assert(Pos != Macros.end() && "Identifier macro info is missing!");
42  return Pos->second;
43}
44
45/// setMacroInfo - Specify a macro for this identifier.
46///
47void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
48  if (MI) {
49    Macros[II] = MI;
50    II->setHasMacroDefinition(true);
51  } else if (II->hasMacroDefinition()) {
52    Macros.erase(II);
53    II->setHasMacroDefinition(false);
54  }
55}
56
57/// RegisterBuiltinMacro - Register the specified identifier in the identifier
58/// table and mark it as a builtin macro to be expanded.
59static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
60  // Get the identifier.
61  IdentifierInfo *Id = PP.getIdentifierInfo(Name);
62
63  // Mark it as being a macro that is builtin.
64  MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
65  MI->setIsBuiltinMacro();
66  PP.setMacroInfo(Id, MI);
67  return Id;
68}
69
70
71/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
72/// identifier table.
73void 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");
80
81  // GCC Extensions.
82  Ident__BASE_FILE__     = RegisterBuiltinMacro(*this, "__BASE_FILE__");
83  Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
84  Ident__TIMESTAMP__     = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
85
86  // Clang Extensions.
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");
92
93  // Microsoft Extensions.
94  if (Features.Microsoft)
95    Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");
96  else
97    Ident__pragma = 0;
98}
99
100/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
101/// in its expansion, currently expands to that token literally.
102static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
103                                          const IdentifierInfo *MacroIdent,
104                                          Preprocessor &PP) {
105  IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
106
107  // If the token isn't an identifier, it's always literally expanded.
108  if (II == 0) return true;
109
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.
114      II != MacroIdent)
115    return false;
116
117  // If this is an object-like macro invocation, it is safe to trivially expand
118  // it.
119  if (MI->isObjectLike()) return true;
120
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();
124       I != E; ++I)
125    if (*I == II)
126      return false;   // Identifier is a macro argument.
127
128  return true;
129}
130
131
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.
135bool Preprocessor::isNextPPTokenLParen() {
136  // Do some quick tests for rejection cases.
137  unsigned Val;
138  if (CurLexer)
139    Val = CurLexer->isNextPPTokenLParen();
140  else if (CurPTHLexer)
141    Val = CurPTHLexer->isNextPPTokenLParen();
142  else
143    Val = CurTokenLexer->isNextTokenLParen();
144
145  if (Val == 2) {
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
148    // macro stack.
149    if (CurPPLexer)
150      return false;
151    for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
152      IncludeStackInfo &Entry = IncludeMacroStack[i-1];
153      if (Entry.TheLexer)
154        Val = Entry.TheLexer->isNextPPTokenLParen();
155      else if (Entry.ThePTHLexer)
156        Val = Entry.ThePTHLexer->isNextPPTokenLParen();
157      else
158        Val = Entry.TheTokenLexer->isNextTokenLParen();
159
160      if (Val != 2)
161        break;
162
163      // Ran off the end of a source file?
164      if (Entry.ThePPLexer)
165        return false;
166    }
167  }
168
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.
172  return Val == 1;
173}
174
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'.
177bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
178                                                 MacroInfo *MI) {
179  // If this is a macro expansion in the "#if !defined(x)" line for the file,
180  // then the macro could expand to different things in other contexts, we need
181  // to disable the optimization in this case.
182  if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
183
184  // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
185  if (MI->isBuiltinMacro()) {
186    if (Callbacks) Callbacks->MacroExpands(Identifier, MI);
187    ExpandBuiltinMacro(Identifier);
188    return false;
189  }
190
191  /// Args - If this is a function-like macro expansion, this contains,
192  /// for each macro argument, the list of tokens that were provided to the
193  /// invocation.
194  MacroArgs *Args = 0;
195
196  // Remember where the end of the instantiation occurred.  For an object-like
197  // macro, this is the identifier.  For a function-like macro, this is the ')'.
198  SourceLocation InstantiationEnd = Identifier.getLocation();
199
200  // If this is a function-like macro, read the arguments.
201  if (MI->isFunctionLike()) {
202    // C99 6.10.3p10: If the preprocessing token immediately after the the macro
203    // name isn't a '(', this macro should not be expanded.
204    if (!isNextPPTokenLParen())
205      return true;
206
207    // Remember that we are now parsing the arguments to a macro invocation.
208    // Preprocessor directives used inside macro arguments are not portable, and
209    // this enables the warning.
210    InMacroArgs = true;
211    Args = ReadFunctionLikeMacroArgs(Identifier, MI, InstantiationEnd);
212
213    // Finished parsing args.
214    InMacroArgs = false;
215
216    // If there was an error parsing the arguments, bail out.
217    if (Args == 0) return false;
218
219    ++NumFnMacroExpanded;
220  } else {
221    ++NumMacroExpanded;
222  }
223
224  // Notice that this macro has been used.
225  markMacroAsUsed(MI);
226
227  if (Callbacks) Callbacks->MacroExpands(Identifier, MI);
228
229  // If we started lexing a macro, enter the macro expansion body.
230
231  // Remember where the token is instantiated.
232  SourceLocation InstantiateLoc = Identifier.getLocation();
233
234  // If this macro expands to no tokens, don't bother to push it onto the
235  // expansion stack, only to take it right back off.
236  if (MI->getNumTokens() == 0) {
237    // No need for arg info.
238    if (Args) Args->destroy(*this);
239
240    // Ignore this macro use, just return the next token in the current
241    // buffer.
242    bool HadLeadingSpace = Identifier.hasLeadingSpace();
243    bool IsAtStartOfLine = Identifier.isAtStartOfLine();
244
245    Lex(Identifier);
246
247    // If the identifier isn't on some OTHER line, inherit the leading
248    // whitespace/first-on-a-line property of this token.  This handles
249    // stuff like "! XX," -> "! ," and "   XX," -> "    ,", when XX is
250    // empty.
251    if (!Identifier.isAtStartOfLine()) {
252      if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
253      if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
254    }
255    Identifier.setFlag(Token::LeadingEmptyMacro);
256    LastEmptyMacroInstantiationLoc = InstantiateLoc;
257    ++NumFastMacroExpanded;
258    return false;
259
260  } else if (MI->getNumTokens() == 1 &&
261             isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
262                                           *this)) {
263    // Otherwise, if this macro expands into a single trivially-expanded
264    // token: expand it now.  This handles common cases like
265    // "#define VAL 42".
266
267    // No need for arg info.
268    if (Args) Args->destroy(*this);
269
270    // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
271    // identifier to the expanded token.
272    bool isAtStartOfLine = Identifier.isAtStartOfLine();
273    bool hasLeadingSpace = Identifier.hasLeadingSpace();
274
275    // Replace the result token.
276    Identifier = MI->getReplacementToken(0);
277
278    // Restore the StartOfLine/LeadingSpace markers.
279    Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
280    Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
281
282    // Update the tokens location to include both its instantiation and physical
283    // locations.
284    SourceLocation Loc =
285      SourceMgr.createInstantiationLoc(Identifier.getLocation(), InstantiateLoc,
286                                       InstantiationEnd,Identifier.getLength());
287    Identifier.setLocation(Loc);
288
289    // If this is a disabled macro or #define X X, we must mark the result as
290    // unexpandable.
291    if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) {
292      if (MacroInfo *NewMI = getMacroInfo(NewII))
293        if (!NewMI->isEnabled() || NewMI == MI)
294          Identifier.setFlag(Token::DisableExpand);
295    }
296
297    // Since this is not an identifier token, it can't be macro expanded, so
298    // we're done.
299    ++NumFastMacroExpanded;
300    return false;
301  }
302
303  // Start expanding the macro.
304  EnterMacro(Identifier, InstantiationEnd, Args);
305
306  // Now that the macro is at the top of the include stack, ask the
307  // preprocessor to read the next token from it.
308  Lex(Identifier);
309  return false;
310}
311
312/// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next
313/// token is the '(' of the macro, this method is invoked to read all of the
314/// actual arguments specified for the macro invocation.  This returns null on
315/// error.
316MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
317                                                   MacroInfo *MI,
318                                                   SourceLocation &MacroEnd) {
319  // The number of fixed arguments to parse.
320  unsigned NumFixedArgsLeft = MI->getNumArgs();
321  bool isVariadic = MI->isVariadic();
322
323  // Outer loop, while there are more arguments, keep reading them.
324  Token Tok;
325
326  // Read arguments as unexpanded tokens.  This avoids issues, e.g., where
327  // an argument value in a macro could expand to ',' or '(' or ')'.
328  LexUnexpandedToken(Tok);
329  assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
330
331  // ArgTokens - Build up a list of tokens that make up each argument.  Each
332  // argument is separated by an EOF token.  Use a SmallVector so we can avoid
333  // heap allocations in the common case.
334  llvm::SmallVector<Token, 64> ArgTokens;
335
336  unsigned NumActuals = 0;
337  while (Tok.isNot(tok::r_paren)) {
338    assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) &&
339           "only expect argument separators here");
340
341    unsigned ArgTokenStart = ArgTokens.size();
342    SourceLocation ArgStartLoc = Tok.getLocation();
343
344    // C99 6.10.3p11: Keep track of the number of l_parens we have seen.  Note
345    // that we already consumed the first one.
346    unsigned NumParens = 0;
347
348    while (1) {
349      // Read arguments as unexpanded tokens.  This avoids issues, e.g., where
350      // an argument value in a macro could expand to ',' or '(' or ')'.
351      LexUnexpandedToken(Tok);
352
353      if (Tok.is(tok::code_completion)) {
354        if (CodeComplete)
355          CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(),
356                                                  MI, NumActuals);
357        LexUnexpandedToken(Tok);
358      }
359
360      if (Tok.is(tok::eof) || Tok.is(tok::eod)) { // "#if f(<eof>" & "#if f(\n"
361        Diag(MacroName, diag::err_unterm_macro_invoc);
362        // Do not lose the EOF/EOD.  Return it to the client.
363        MacroName = Tok;
364        return 0;
365      } else if (Tok.is(tok::r_paren)) {
366        // If we found the ) token, the macro arg list is done.
367        if (NumParens-- == 0) {
368          MacroEnd = Tok.getLocation();
369          break;
370        }
371      } else if (Tok.is(tok::l_paren)) {
372        ++NumParens;
373      } else if (Tok.is(tok::comma) && NumParens == 0) {
374        // Comma ends this argument if there are more fixed arguments expected.
375        // However, if this is a variadic macro, and this is part of the
376        // variadic part, then the comma is just an argument token.
377        if (!isVariadic) break;
378        if (NumFixedArgsLeft > 1)
379          break;
380      } else if (Tok.is(tok::comment) && !KeepMacroComments) {
381        // If this is a comment token in the argument list and we're just in
382        // -C mode (not -CC mode), discard the comment.
383        continue;
384      } else if (Tok.getIdentifierInfo() != 0) {
385        // Reading macro arguments can cause macros that we are currently
386        // expanding from to be popped off the expansion stack.  Doing so causes
387        // them to be reenabled for expansion.  Here we record whether any
388        // identifiers we lex as macro arguments correspond to disabled macros.
389        // If so, we mark the token as noexpand.  This is a subtle aspect of
390        // C99 6.10.3.4p2.
391        if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
392          if (!MI->isEnabled())
393            Tok.setFlag(Token::DisableExpand);
394      }
395      ArgTokens.push_back(Tok);
396    }
397
398    // If this was an empty argument list foo(), don't add this as an empty
399    // argument.
400    if (ArgTokens.empty() && Tok.getKind() == tok::r_paren)
401      break;
402
403    // If this is not a variadic macro, and too many args were specified, emit
404    // an error.
405    if (!isVariadic && NumFixedArgsLeft == 0) {
406      if (ArgTokens.size() != ArgTokenStart)
407        ArgStartLoc = ArgTokens[ArgTokenStart].getLocation();
408
409      // Emit the diagnostic at the macro name in case there is a missing ).
410      // Emitting it at the , could be far away from the macro name.
411      Diag(ArgStartLoc, diag::err_too_many_args_in_macro_invoc);
412      return 0;
413    }
414
415    // Empty arguments are standard in C99 and C++0x, and are supported as an extension in
416    // other modes.
417    if (ArgTokens.size() == ArgTokenStart && !Features.C99 && !Features.CPlusPlus0x)
418      Diag(Tok, diag::ext_empty_fnmacro_arg);
419
420    // Add a marker EOF token to the end of the token list for this argument.
421    Token EOFTok;
422    EOFTok.startToken();
423    EOFTok.setKind(tok::eof);
424    EOFTok.setLocation(Tok.getLocation());
425    EOFTok.setLength(0);
426    ArgTokens.push_back(EOFTok);
427    ++NumActuals;
428    assert(NumFixedArgsLeft != 0 && "Too many arguments parsed");
429    --NumFixedArgsLeft;
430  }
431
432  // Okay, we either found the r_paren.  Check to see if we parsed too few
433  // arguments.
434  unsigned MinArgsExpected = MI->getNumArgs();
435
436  // See MacroArgs instance var for description of this.
437  bool isVarargsElided = false;
438
439  if (NumActuals < MinArgsExpected) {
440    // There are several cases where too few arguments is ok, handle them now.
441    if (NumActuals == 0 && MinArgsExpected == 1) {
442      // #define A(X)  or  #define A(...)   ---> A()
443
444      // If there is exactly one argument, and that argument is missing,
445      // then we have an empty "()" argument empty list.  This is fine, even if
446      // the macro expects one argument (the argument is just empty).
447      isVarargsElided = MI->isVariadic();
448    } else if (MI->isVariadic() &&
449               (NumActuals+1 == MinArgsExpected ||  // A(x, ...) -> A(X)
450                (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A()
451      // Varargs where the named vararg parameter is missing: ok as extension.
452      // #define A(x, ...)
453      // A("blah")
454      Diag(Tok, diag::ext_missing_varargs_arg);
455
456      // Remember this occurred, allowing us to elide the comma when used for
457      // cases like:
458      //   #define A(x, foo...) blah(a, ## foo)
459      //   #define B(x, ...) blah(a, ## __VA_ARGS__)
460      //   #define C(...) blah(a, ## __VA_ARGS__)
461      //  A(x) B(x) C()
462      isVarargsElided = true;
463    } else {
464      // Otherwise, emit the error.
465      Diag(Tok, diag::err_too_few_args_in_macro_invoc);
466      return 0;
467    }
468
469    // Add a marker EOF token to the end of the token list for this argument.
470    SourceLocation EndLoc = Tok.getLocation();
471    Tok.startToken();
472    Tok.setKind(tok::eof);
473    Tok.setLocation(EndLoc);
474    Tok.setLength(0);
475    ArgTokens.push_back(Tok);
476
477    // If we expect two arguments, add both as empty.
478    if (NumActuals == 0 && MinArgsExpected == 2)
479      ArgTokens.push_back(Tok);
480
481  } else if (NumActuals > MinArgsExpected && !MI->isVariadic()) {
482    // Emit the diagnostic at the macro name in case there is a missing ).
483    // Emitting it at the , could be far away from the macro name.
484    Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
485    return 0;
486  }
487
488  return MacroArgs::create(MI, ArgTokens.data(), ArgTokens.size(),
489                           isVarargsElided, *this);
490}
491
492/// ComputeDATE_TIME - Compute the current time, enter it into the specified
493/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
494/// the identifier tokens inserted.
495static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
496                             Preprocessor &PP) {
497  time_t TT = time(0);
498  struct tm *TM = localtime(&TT);
499
500  static const char * const Months[] = {
501    "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
502  };
503
504  char TmpBuffer[32];
505#ifdef LLVM_ON_WIN32
506  sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
507          TM->tm_year+1900);
508#else
509  snprintf(TmpBuffer, sizeof(TmpBuffer), "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
510          TM->tm_year+1900);
511#endif
512
513  Token TmpTok;
514  TmpTok.startToken();
515  PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok);
516  DATELoc = TmpTok.getLocation();
517
518#ifdef LLVM_ON_WIN32
519  sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
520#else
521  snprintf(TmpBuffer, sizeof(TmpBuffer), "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
522#endif
523  PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok);
524  TIMELoc = TmpTok.getLocation();
525}
526
527
528/// HasFeature - Return true if we recognize and implement the specified feature
529/// specified by the identifier.
530static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) {
531  const LangOptions &LangOpts = PP.getLangOptions();
532
533  return llvm::StringSwitch<bool>(II->getName())
534           .Case("attribute_analyzer_noreturn", true)
535           .Case("attribute_availability", true)
536           .Case("attribute_cf_returns_not_retained", true)
537           .Case("attribute_cf_returns_retained", true)
538           .Case("attribute_deprecated_with_message", true)
539           .Case("attribute_ext_vector_type", true)
540           .Case("attribute_ns_returns_not_retained", true)
541           .Case("attribute_ns_returns_retained", true)
542           .Case("attribute_ns_consumes_self", true)
543           .Case("attribute_ns_consumed", true)
544           .Case("attribute_cf_consumed", true)
545           .Case("attribute_objc_ivar_unused", true)
546           .Case("attribute_objc_method_family", true)
547           .Case("attribute_overloadable", true)
548           .Case("attribute_unavailable_with_message", true)
549           .Case("blocks", LangOpts.Blocks)
550           .Case("cxx_exceptions", LangOpts.Exceptions)
551           .Case("cxx_rtti", LangOpts.RTTI)
552           .Case("enumerator_attributes", true)
553           .Case("generic_selections", true)
554           .Case("objc_nonfragile_abi", LangOpts.ObjCNonFragileABI)
555           .Case("objc_weak_class", LangOpts.ObjCNonFragileABI)
556           .Case("ownership_holds", true)
557           .Case("ownership_returns", true)
558           .Case("ownership_takes", true)
559           // C++0x features
560           .Case("cxx_alias_templates", LangOpts.CPlusPlus0x)
561           .Case("cxx_attributes", LangOpts.CPlusPlus0x)
562           .Case("cxx_auto_type", LangOpts.CPlusPlus0x)
563           .Case("cxx_decltype", LangOpts.CPlusPlus0x)
564           .Case("cxx_default_function_template_args", LangOpts.CPlusPlus0x)
565           .Case("cxx_delegating_constructors", LangOpts.CPlusPlus0x)
566           .Case("cxx_deleted_functions", LangOpts.CPlusPlus0x)
567           .Case("cxx_inline_namespaces", LangOpts.CPlusPlus0x)
568         //.Case("cxx_lambdas", false)
569           .Case("cxx_noexcept", LangOpts.CPlusPlus0x)
570         //.Case("cxx_nullptr", false)
571           .Case("cxx_override_control", LangOpts.CPlusPlus0x)
572           .Case("cxx_range_for", LangOpts.CPlusPlus0x)
573           .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus0x)
574           .Case("cxx_rvalue_references", LangOpts.CPlusPlus0x)
575           .Case("cxx_strong_enums", LangOpts.CPlusPlus0x)
576           .Case("cxx_static_assert", LangOpts.CPlusPlus0x)
577           .Case("cxx_trailing_return", LangOpts.CPlusPlus0x)
578           .Case("cxx_variadic_templates", LangOpts.CPlusPlus0x)
579           // Type traits
580           .Case("has_nothrow_assign", LangOpts.CPlusPlus)
581           .Case("has_nothrow_copy", LangOpts.CPlusPlus)
582           .Case("has_nothrow_constructor", LangOpts.CPlusPlus)
583           .Case("has_trivial_assign", LangOpts.CPlusPlus)
584           .Case("has_trivial_copy", LangOpts.CPlusPlus)
585           .Case("has_trivial_constructor", LangOpts.CPlusPlus)
586           .Case("has_trivial_destructor", LangOpts.CPlusPlus)
587           .Case("has_virtual_destructor", LangOpts.CPlusPlus)
588           .Case("is_abstract", LangOpts.CPlusPlus)
589           .Case("is_base_of", LangOpts.CPlusPlus)
590           .Case("is_class", LangOpts.CPlusPlus)
591           .Case("is_convertible_to", LangOpts.CPlusPlus)
592           .Case("is_empty", LangOpts.CPlusPlus)
593           .Case("is_enum", LangOpts.CPlusPlus)
594           .Case("is_literal", LangOpts.CPlusPlus)
595           .Case("is_pod", LangOpts.CPlusPlus)
596           .Case("is_polymorphic", LangOpts.CPlusPlus)
597           .Case("is_trivial", LangOpts.CPlusPlus)
598           .Case("is_union", LangOpts.CPlusPlus)
599           .Case("tls", PP.getTargetInfo().isTLSSupported())
600           .Default(false);
601}
602
603/// HasAttribute -  Return true if we recognize and implement the attribute
604/// specified by the given identifier.
605static bool HasAttribute(const IdentifierInfo *II) {
606    return llvm::StringSwitch<bool>(II->getName())
607#include "clang/Lex/AttrSpellings.inc"
608        .Default(false);
609}
610
611/// EvaluateHasIncludeCommon - Process a '__has_include("path")'
612/// or '__has_include_next("path")' expression.
613/// Returns true if successful.
614static bool EvaluateHasIncludeCommon(Token &Tok,
615                                     IdentifierInfo *II, Preprocessor &PP,
616                                     const DirectoryLookup *LookupFrom) {
617  SourceLocation LParenLoc;
618
619  // Get '('.
620  PP.LexNonComment(Tok);
621
622  // Ensure we have a '('.
623  if (Tok.isNot(tok::l_paren)) {
624    PP.Diag(Tok.getLocation(), diag::err_pp_missing_lparen) << II->getName();
625    return false;
626  }
627
628  // Save '(' location for possible missing ')' message.
629  LParenLoc = Tok.getLocation();
630
631  // Get the file name.
632  PP.getCurrentLexer()->LexIncludeFilename(Tok);
633
634  // Reserve a buffer to get the spelling.
635  llvm::SmallString<128> FilenameBuffer;
636  llvm::StringRef Filename;
637  SourceLocation EndLoc;
638
639  switch (Tok.getKind()) {
640  case tok::eod:
641    // If the token kind is EOD, the error has already been diagnosed.
642    return false;
643
644  case tok::angle_string_literal:
645  case tok::string_literal: {
646    bool Invalid = false;
647    Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
648    if (Invalid)
649      return false;
650    break;
651  }
652
653  case tok::less:
654    // This could be a <foo/bar.h> file coming from a macro expansion.  In this
655    // case, glue the tokens together into FilenameBuffer and interpret those.
656    FilenameBuffer.push_back('<');
657    if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc))
658      return false;   // Found <eod> but no ">"?  Diagnostic already emitted.
659    Filename = FilenameBuffer.str();
660    break;
661  default:
662    PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
663    return false;
664  }
665
666  bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
667  // If GetIncludeFilenameSpelling set the start ptr to null, there was an
668  // error.
669  if (Filename.empty())
670    return false;
671
672  // Search include directories.
673  const DirectoryLookup *CurDir;
674  const FileEntry *File =
675      PP.LookupFile(Filename, isAngled, LookupFrom, CurDir, NULL, NULL);
676
677  // Get the result value.  Result = true means the file exists.
678  bool Result = File != 0;
679
680  // Get ')'.
681  PP.LexNonComment(Tok);
682
683  // Ensure we have a trailing ).
684  if (Tok.isNot(tok::r_paren)) {
685    PP.Diag(Tok.getLocation(), diag::err_pp_missing_rparen) << II->getName();
686    PP.Diag(LParenLoc, diag::note_matching) << "(";
687    return false;
688  }
689
690  return Result;
691}
692
693/// EvaluateHasInclude - Process a '__has_include("path")' expression.
694/// Returns true if successful.
695static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II,
696                               Preprocessor &PP) {
697  return EvaluateHasIncludeCommon(Tok, II, PP, NULL);
698}
699
700/// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.
701/// Returns true if successful.
702static bool EvaluateHasIncludeNext(Token &Tok,
703                                   IdentifierInfo *II, Preprocessor &PP) {
704  // __has_include_next is like __has_include, except that we start
705  // searching after the current found directory.  If we can't do this,
706  // issue a diagnostic.
707  const DirectoryLookup *Lookup = PP.GetCurDirLookup();
708  if (PP.isInPrimaryFile()) {
709    Lookup = 0;
710    PP.Diag(Tok, diag::pp_include_next_in_primary);
711  } else if (Lookup == 0) {
712    PP.Diag(Tok, diag::pp_include_next_absolute_path);
713  } else {
714    // Start looking up in the next directory.
715    ++Lookup;
716  }
717
718  return EvaluateHasIncludeCommon(Tok, II, PP, Lookup);
719}
720
721/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
722/// as a builtin macro, handle it and return the next token as 'Tok'.
723void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
724  // Figure out which token this is.
725  IdentifierInfo *II = Tok.getIdentifierInfo();
726  assert(II && "Can't be a macro without id info!");
727
728  // If this is an _Pragma or Microsoft __pragma directive, expand it,
729  // invoke the pragma handler, then lex the token after it.
730  if (II == Ident_Pragma)
731    return Handle_Pragma(Tok);
732  else if (II == Ident__pragma) // in non-MS mode this is null
733    return HandleMicrosoft__pragma(Tok);
734
735  ++NumBuiltinMacroExpanded;
736
737  llvm::SmallString<128> TmpBuffer;
738  llvm::raw_svector_ostream OS(TmpBuffer);
739
740  // Set up the return result.
741  Tok.setIdentifierInfo(0);
742  Tok.clearFlag(Token::NeedsCleaning);
743
744  if (II == Ident__LINE__) {
745    // C99 6.10.8: "__LINE__: The presumed line number (within the current
746    // source file) of the current source line (an integer constant)".  This can
747    // be affected by #line.
748    SourceLocation Loc = Tok.getLocation();
749
750    // Advance to the location of the first _, this might not be the first byte
751    // of the token if it starts with an escaped newline.
752    Loc = AdvanceToTokenCharacter(Loc, 0);
753
754    // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
755    // a macro instantiation.  This doesn't matter for object-like macros, but
756    // can matter for a function-like macro that expands to contain __LINE__.
757    // Skip down through instantiation points until we find a file loc for the
758    // end of the instantiation history.
759    Loc = SourceMgr.getInstantiationRange(Loc).second;
760    PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
761
762    // __LINE__ expands to a simple numeric value.
763    OS << (PLoc.isValid()? PLoc.getLine() : 1);
764    Tok.setKind(tok::numeric_constant);
765  } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
766    // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
767    // character string literal)". This can be affected by #line.
768    PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
769
770    // __BASE_FILE__ is a GNU extension that returns the top of the presumed
771    // #include stack instead of the current file.
772    if (II == Ident__BASE_FILE__ && PLoc.isValid()) {
773      SourceLocation NextLoc = PLoc.getIncludeLoc();
774      while (NextLoc.isValid()) {
775        PLoc = SourceMgr.getPresumedLoc(NextLoc);
776        if (PLoc.isInvalid())
777          break;
778
779        NextLoc = PLoc.getIncludeLoc();
780      }
781    }
782
783    // Escape this filename.  Turn '\' -> '\\' '"' -> '\"'
784    llvm::SmallString<128> FN;
785    if (PLoc.isValid()) {
786      FN += PLoc.getFilename();
787      Lexer::Stringify(FN);
788      OS << '"' << FN.str() << '"';
789    }
790    Tok.setKind(tok::string_literal);
791  } else if (II == Ident__DATE__) {
792    if (!DATELoc.isValid())
793      ComputeDATE_TIME(DATELoc, TIMELoc, *this);
794    Tok.setKind(tok::string_literal);
795    Tok.setLength(strlen("\"Mmm dd yyyy\""));
796    Tok.setLocation(SourceMgr.createInstantiationLoc(DATELoc, Tok.getLocation(),
797                                                     Tok.getLocation(),
798                                                     Tok.getLength()));
799    return;
800  } else if (II == Ident__TIME__) {
801    if (!TIMELoc.isValid())
802      ComputeDATE_TIME(DATELoc, TIMELoc, *this);
803    Tok.setKind(tok::string_literal);
804    Tok.setLength(strlen("\"hh:mm:ss\""));
805    Tok.setLocation(SourceMgr.createInstantiationLoc(TIMELoc, Tok.getLocation(),
806                                                     Tok.getLocation(),
807                                                     Tok.getLength()));
808    return;
809  } else if (II == Ident__INCLUDE_LEVEL__) {
810    // Compute the presumed include depth of this token.  This can be affected
811    // by GNU line markers.
812    unsigned Depth = 0;
813
814    PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
815    if (PLoc.isValid()) {
816      PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
817      for (; PLoc.isValid(); ++Depth)
818        PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
819    }
820
821    // __INCLUDE_LEVEL__ expands to a simple numeric value.
822    OS << Depth;
823    Tok.setKind(tok::numeric_constant);
824  } else if (II == Ident__TIMESTAMP__) {
825    // MSVC, ICC, GCC, VisualAge C++ extension.  The generated string should be
826    // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
827
828    // Get the file that we are lexing out of.  If we're currently lexing from
829    // a macro, dig into the include stack.
830    const FileEntry *CurFile = 0;
831    PreprocessorLexer *TheLexer = getCurrentFileLexer();
832
833    if (TheLexer)
834      CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
835
836    const char *Result;
837    if (CurFile) {
838      time_t TT = CurFile->getModificationTime();
839      struct tm *TM = localtime(&TT);
840      Result = asctime(TM);
841    } else {
842      Result = "??? ??? ?? ??:??:?? ????\n";
843    }
844    // Surround the string with " and strip the trailing newline.
845    OS << '"' << llvm::StringRef(Result, strlen(Result)-1) << '"';
846    Tok.setKind(tok::string_literal);
847  } else if (II == Ident__COUNTER__) {
848    // __COUNTER__ expands to a simple numeric value.
849    OS << CounterValue++;
850    Tok.setKind(tok::numeric_constant);
851  } else if (II == Ident__has_feature ||
852             II == Ident__has_builtin ||
853             II == Ident__has_attribute) {
854    // The argument to these two builtins should be a parenthesized identifier.
855    SourceLocation StartLoc = Tok.getLocation();
856
857    bool IsValid = false;
858    IdentifierInfo *FeatureII = 0;
859
860    // Read the '('.
861    Lex(Tok);
862    if (Tok.is(tok::l_paren)) {
863      // Read the identifier
864      Lex(Tok);
865      if (Tok.is(tok::identifier)) {
866        FeatureII = Tok.getIdentifierInfo();
867
868        // Read the ')'.
869        Lex(Tok);
870        if (Tok.is(tok::r_paren))
871          IsValid = true;
872      }
873    }
874
875    bool Value = false;
876    if (!IsValid)
877      Diag(StartLoc, diag::err_feature_check_malformed);
878    else if (II == Ident__has_builtin) {
879      // Check for a builtin is trivial.
880      Value = FeatureII->getBuiltinID() != 0;
881    } else if (II == Ident__has_attribute)
882      Value = HasAttribute(FeatureII);
883    else {
884      assert(II == Ident__has_feature && "Must be feature check");
885      Value = HasFeature(*this, FeatureII);
886    }
887
888    OS << (int)Value;
889    Tok.setKind(tok::numeric_constant);
890  } else if (II == Ident__has_include ||
891             II == Ident__has_include_next) {
892    // The argument to these two builtins should be a parenthesized
893    // file name string literal using angle brackets (<>) or
894    // double-quotes ("").
895    bool Value;
896    if (II == Ident__has_include)
897      Value = EvaluateHasInclude(Tok, II, *this);
898    else
899      Value = EvaluateHasIncludeNext(Tok, II, *this);
900    OS << (int)Value;
901    Tok.setKind(tok::numeric_constant);
902  } else {
903    assert(0 && "Unknown identifier!");
904  }
905  CreateString(OS.str().data(), OS.str().size(), Tok, Tok.getLocation());
906}
907
908void Preprocessor::markMacroAsUsed(MacroInfo *MI) {
909  // If the 'used' status changed, and the macro requires 'unused' warning,
910  // remove its SourceLocation from the warn-for-unused-macro locations.
911  if (MI->isWarnIfUnused() && !MI->isUsed())
912    WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
913  MI->setIsUsed(true);
914}
915