blob: fa517e7fa8114e9edd050d6c9f3f499568fb86a1 [file] [log] [blame]
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001//===--- 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//
James Dennett32740042013-12-02 17:39:27 +000010// This file implements the top level handling of macro expansion for the
Joao Matosc0d4c1b2012-08-31 21:34:27 +000011// preprocessor.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/Preprocessor.h"
Aaron Ballman2fbf9942014-03-31 13:14:44 +000016#include "clang/Basic/Attributes.h"
Joao Matosc0d4c1b2012-08-31 21:34:27 +000017#include "clang/Basic/FileManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Basic/SourceManager.h"
Joao Matosc0d4c1b2012-08-31 21:34:27 +000019#include "clang/Basic/TargetInfo.h"
Joao Matosc0d4c1b2012-08-31 21:34:27 +000020#include "clang/Lex/CodeCompletionHandler.h"
21#include "clang/Lex/ExternalPreprocessorSource.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Lex/LexDiagnostic.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000023#include "clang/Lex/MacroArgs.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/Lex/MacroInfo.h"
25#include "llvm/ADT/STLExtras.h"
Andy Gibbs58905d22012-11-17 19:15:38 +000026#include "llvm/ADT/SmallString.h"
Joao Matosc0d4c1b2012-08-31 21:34:27 +000027#include "llvm/ADT/StringSwitch.h"
Joao Matosc0d4c1b2012-08-31 21:34:27 +000028#include "llvm/Config/llvm-config.h"
Joao Matosc0d4c1b2012-08-31 21:34:27 +000029#include "llvm/Support/ErrorHandling.h"
Dmitri Gribenkoae07f722012-09-24 20:56:28 +000030#include "llvm/Support/Format.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000031#include "llvm/Support/raw_ostream.h"
Joao Matosc0d4c1b2012-08-31 21:34:27 +000032#include <cstdio>
33#include <ctime>
34using namespace clang;
35
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +000036MacroDirective *
37Preprocessor::getMacroDirectiveHistory(const IdentifierInfo *II) const {
Alexander Kornienko1d26c022012-09-25 17:18:14 +000038 assert(II->hadMacroDefinition() && "Identifier has not been not a macro!");
Joao Matosc0d4c1b2012-08-31 21:34:27 +000039
40 macro_iterator Pos = Macros.find(II);
Joao Matosc0d4c1b2012-08-31 21:34:27 +000041 assert(Pos != Macros.end() && "Identifier macro info is missing!");
Joao Matosc0d4c1b2012-08-31 21:34:27 +000042 return Pos->second;
43}
44
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +000045void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +000046 assert(MD && "MacroDirective should be non-zero!");
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +000047 assert(!MD->getPrevious() && "Already attached to a MacroDirective history.");
Douglas Gregor5a4649b2012-10-11 00:46:49 +000048
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +000049 MacroDirective *&StoredMD = Macros[II];
50 MD->setPrevious(StoredMD);
51 StoredMD = MD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +000052 II->setHasMacroDefinition(MD->isDefined());
53 bool isImportedMacro = isa<DefMacroDirective>(MD) &&
54 cast<DefMacroDirective>(MD)->isImported();
55 if (II->isFromAST() && !isImportedMacro)
Joao Matosc0d4c1b2012-08-31 21:34:27 +000056 II->setChangedSinceDeserialization();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +000057}
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +000058
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +000059void Preprocessor::setLoadedMacroDirective(IdentifierInfo *II,
60 MacroDirective *MD) {
61 assert(II && MD);
62 MacroDirective *&StoredMD = Macros[II];
63 assert(!StoredMD &&
64 "the macro history was modified before initializing it from a pch");
65 StoredMD = MD;
66 // Setup the identifier as having associated macro history.
67 II->setHasMacroDefinition(true);
68 if (!MD->isDefined())
69 II->setHasMacroDefinition(false);
Joao Matosc0d4c1b2012-08-31 21:34:27 +000070}
71
Joao Matosc0d4c1b2012-08-31 21:34:27 +000072/// RegisterBuiltinMacro - Register the specified identifier in the identifier
73/// table and mark it as a builtin macro to be expanded.
74static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
75 // Get the identifier.
76 IdentifierInfo *Id = PP.getIdentifierInfo(Name);
77
78 // Mark it as being a macro that is builtin.
79 MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
80 MI->setIsBuiltinMacro();
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +000081 PP.appendDefMacroDirective(Id, MI);
Joao Matosc0d4c1b2012-08-31 21:34:27 +000082 return Id;
83}
84
85
86/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
87/// identifier table.
88void Preprocessor::RegisterBuiltinMacros() {
89 Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
90 Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");
91 Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");
92 Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__");
93 Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
94 Ident_Pragma = RegisterBuiltinMacro(*this, "_Pragma");
95
96 // GCC Extensions.
97 Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__");
98 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
99 Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
100
Richard Smithae385082014-03-15 00:06:08 +0000101 // Microsoft Extensions.
102 if (LangOpts.MicrosoftExt) {
103 Ident__identifier = RegisterBuiltinMacro(*this, "__identifier");
104 Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");
105 } else {
106 Ident__identifier = 0;
107 Ident__pragma = 0;
108 }
109
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000110 // Clang Extensions.
111 Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature");
112 Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension");
113 Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin");
114 Ident__has_attribute = RegisterBuiltinMacro(*this, "__has_attribute");
115 Ident__has_include = RegisterBuiltinMacro(*this, "__has_include");
116 Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");
117 Ident__has_warning = RegisterBuiltinMacro(*this, "__has_warning");
Yunzhong Gaoef309f42014-04-11 20:55:19 +0000118 Ident__is_identifier = RegisterBuiltinMacro(*this, "__is_identifier");
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000119
Douglas Gregorc83de302012-09-25 15:44:52 +0000120 // Modules.
121 if (LangOpts.Modules) {
122 Ident__building_module = RegisterBuiltinMacro(*this, "__building_module");
123
124 // __MODULE__
125 if (!LangOpts.CurrentModule.empty())
126 Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__");
127 else
128 Ident__MODULE__ = 0;
129 } else {
130 Ident__building_module = 0;
131 Ident__MODULE__ = 0;
132 }
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000133}
134
135/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
136/// in its expansion, currently expands to that token literally.
137static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
138 const IdentifierInfo *MacroIdent,
139 Preprocessor &PP) {
140 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
141
142 // If the token isn't an identifier, it's always literally expanded.
143 if (II == 0) return true;
144
145 // If the information about this identifier is out of date, update it from
146 // the external source.
147 if (II->isOutOfDate())
148 PP.getExternalSource()->updateOutOfDateIdentifier(*II);
149
150 // If the identifier is a macro, and if that macro is enabled, it may be
151 // expanded so it's not a trivial expansion.
152 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
153 // Fast expanding "#define X X" is ok, because X would be disabled.
154 II != MacroIdent)
155 return false;
156
157 // If this is an object-like macro invocation, it is safe to trivially expand
158 // it.
159 if (MI->isObjectLike()) return true;
160
161 // If this is a function-like macro invocation, it's safe to trivially expand
162 // as long as the identifier is not a macro argument.
163 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
164 I != E; ++I)
165 if (*I == II)
166 return false; // Identifier is a macro argument.
167
168 return true;
169}
170
171
172/// isNextPPTokenLParen - Determine whether the next preprocessor token to be
173/// lexed is a '('. If so, consume the token and return true, if not, this
174/// method should have no observable side-effect on the lexed tokens.
175bool Preprocessor::isNextPPTokenLParen() {
176 // Do some quick tests for rejection cases.
177 unsigned Val;
178 if (CurLexer)
179 Val = CurLexer->isNextPPTokenLParen();
180 else if (CurPTHLexer)
181 Val = CurPTHLexer->isNextPPTokenLParen();
182 else
183 Val = CurTokenLexer->isNextTokenLParen();
184
185 if (Val == 2) {
186 // We have run off the end. If it's a source file we don't
187 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
188 // macro stack.
189 if (CurPPLexer)
190 return false;
191 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
192 IncludeStackInfo &Entry = IncludeMacroStack[i-1];
193 if (Entry.TheLexer)
194 Val = Entry.TheLexer->isNextPPTokenLParen();
195 else if (Entry.ThePTHLexer)
196 Val = Entry.ThePTHLexer->isNextPPTokenLParen();
197 else
198 Val = Entry.TheTokenLexer->isNextTokenLParen();
199
200 if (Val != 2)
201 break;
202
203 // Ran off the end of a source file?
204 if (Entry.ThePPLexer)
205 return false;
206 }
207 }
208
209 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
210 // have found something that isn't a '(' or we found the end of the
211 // translation unit. In either case, return false.
212 return Val == 1;
213}
214
215/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
216/// expanded as a macro, handle it and return the next token as 'Identifier'.
217bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000218 MacroDirective *MD) {
Argyrios Kyrtzidis09796b92013-03-27 01:25:19 +0000219 MacroDirective::DefInfo Def = MD->getDefinition();
220 assert(Def.isValid());
221 MacroInfo *MI = Def.getMacroInfo();
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000222
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000223 // If this is a macro expansion in the "#if !defined(x)" line for the file,
224 // then the macro could expand to different things in other contexts, we need
225 // to disable the optimization in this case.
226 if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
227
228 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
229 if (MI->isBuiltinMacro()) {
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000230 if (Callbacks) Callbacks->MacroExpands(Identifier, MD,
Argyrios Kyrtzidis37e48ff2013-05-03 22:31:32 +0000231 Identifier.getLocation(),/*Args=*/0);
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000232 ExpandBuiltinMacro(Identifier);
Eli Friedman0834a4b2013-09-19 00:41:32 +0000233 return true;
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000234 }
235
236 /// Args - If this is a function-like macro expansion, this contains,
237 /// for each macro argument, the list of tokens that were provided to the
238 /// invocation.
239 MacroArgs *Args = 0;
240
241 // Remember where the end of the expansion occurred. For an object-like
242 // macro, this is the identifier. For a function-like macro, this is the ')'.
243 SourceLocation ExpansionEnd = Identifier.getLocation();
244
245 // If this is a function-like macro, read the arguments.
246 if (MI->isFunctionLike()) {
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000247 // Remember that we are now parsing the arguments to a macro invocation.
248 // Preprocessor directives used inside macro arguments are not portable, and
249 // this enables the warning.
250 InMacroArgs = true;
251 Args = ReadFunctionLikeMacroArgs(Identifier, MI, ExpansionEnd);
252
253 // Finished parsing args.
254 InMacroArgs = false;
255
256 // If there was an error parsing the arguments, bail out.
Eli Friedman0834a4b2013-09-19 00:41:32 +0000257 if (Args == 0) return true;
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000258
259 ++NumFnMacroExpanded;
260 } else {
261 ++NumMacroExpanded;
262 }
263
264 // Notice that this macro has been used.
265 markMacroAsUsed(MI);
266
267 // Remember where the token is expanded.
268 SourceLocation ExpandLoc = Identifier.getLocation();
269 SourceRange ExpansionRange(ExpandLoc, ExpansionEnd);
270
271 if (Callbacks) {
272 if (InMacroArgs) {
273 // We can have macro expansion inside a conditional directive while
274 // reading the function macro arguments. To ensure, in that case, that
275 // MacroExpands callbacks still happen in source order, queue this
276 // callback to have it happen after the function macro callback.
277 DelayedMacroExpandsCallbacks.push_back(
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000278 MacroExpandsInfo(Identifier, MD, ExpansionRange));
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000279 } else {
Argyrios Kyrtzidis37e48ff2013-05-03 22:31:32 +0000280 Callbacks->MacroExpands(Identifier, MD, ExpansionRange, Args);
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000281 if (!DelayedMacroExpandsCallbacks.empty()) {
282 for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) {
283 MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i];
Argyrios Kyrtzidis37e48ff2013-05-03 22:31:32 +0000284 // FIXME: We lose macro args info with delayed callback.
285 Callbacks->MacroExpands(Info.Tok, Info.MD, Info.Range, /*Args=*/0);
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000286 }
287 DelayedMacroExpandsCallbacks.clear();
288 }
289 }
290 }
Douglas Gregor5968b1b2012-10-11 21:07:39 +0000291
292 // If the macro definition is ambiguous, complain.
Argyrios Kyrtzidis09796b92013-03-27 01:25:19 +0000293 if (Def.getDirective()->isAmbiguous()) {
Douglas Gregor5968b1b2012-10-11 21:07:39 +0000294 Diag(Identifier, diag::warn_pp_ambiguous_macro)
295 << Identifier.getIdentifierInfo();
296 Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen)
297 << Identifier.getIdentifierInfo();
Argyrios Kyrtzidis09796b92013-03-27 01:25:19 +0000298 for (MacroDirective::DefInfo PrevDef = Def.getPreviousDefinition();
299 PrevDef && !PrevDef.isUndefined();
300 PrevDef = PrevDef.getPreviousDefinition()) {
Richard Smith49f906a2014-03-01 00:08:04 +0000301 Diag(PrevDef.getMacroInfo()->getDefinitionLoc(),
302 diag::note_pp_ambiguous_macro_other)
303 << Identifier.getIdentifierInfo();
304 if (!PrevDef.getDirective()->isAmbiguous())
305 break;
Douglas Gregor5968b1b2012-10-11 21:07:39 +0000306 }
307 }
308
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000309 // If we started lexing a macro, enter the macro expansion body.
310
311 // If this macro expands to no tokens, don't bother to push it onto the
312 // expansion stack, only to take it right back off.
313 if (MI->getNumTokens() == 0) {
314 // No need for arg info.
315 if (Args) Args->destroy(*this);
316
Eli Friedman0834a4b2013-09-19 00:41:32 +0000317 // Propagate whitespace info as if we had pushed, then popped,
318 // a macro context.
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000319 Identifier.setFlag(Token::LeadingEmptyMacro);
Eli Friedman0834a4b2013-09-19 00:41:32 +0000320 PropagateLineStartLeadingSpaceInfo(Identifier);
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000321 ++NumFastMacroExpanded;
322 return false;
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000323 } else if (MI->getNumTokens() == 1 &&
324 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
325 *this)) {
326 // Otherwise, if this macro expands into a single trivially-expanded
327 // token: expand it now. This handles common cases like
328 // "#define VAL 42".
329
330 // No need for arg info.
331 if (Args) Args->destroy(*this);
332
333 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
334 // identifier to the expanded token.
335 bool isAtStartOfLine = Identifier.isAtStartOfLine();
336 bool hasLeadingSpace = Identifier.hasLeadingSpace();
337
338 // Replace the result token.
339 Identifier = MI->getReplacementToken(0);
340
341 // Restore the StartOfLine/LeadingSpace markers.
342 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
343 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
344
345 // Update the tokens location to include both its expansion and physical
346 // locations.
347 SourceLocation Loc =
348 SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc,
349 ExpansionEnd,Identifier.getLength());
350 Identifier.setLocation(Loc);
351
352 // If this is a disabled macro or #define X X, we must mark the result as
353 // unexpandable.
354 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) {
355 if (MacroInfo *NewMI = getMacroInfo(NewII))
356 if (!NewMI->isEnabled() || NewMI == MI) {
357 Identifier.setFlag(Token::DisableExpand);
Douglas Gregor1a347f72013-01-30 23:10:17 +0000358 // Don't warn for "#define X X" like "#define bool bool" from
359 // stdbool.h.
360 if (NewMI != MI || MI->isFunctionLike())
361 Diag(Identifier, diag::pp_disabled_macro_expansion);
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000362 }
363 }
364
365 // Since this is not an identifier token, it can't be macro expanded, so
366 // we're done.
367 ++NumFastMacroExpanded;
Eli Friedman0834a4b2013-09-19 00:41:32 +0000368 return true;
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000369 }
370
371 // Start expanding the macro.
372 EnterMacro(Identifier, ExpansionEnd, MI, Args);
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000373 return false;
374}
375
Richard Trieu79b45382013-07-23 18:01:49 +0000376enum Bracket {
377 Brace,
378 Paren
379};
380
381/// CheckMatchedBrackets - Returns true if the braces and parentheses in the
382/// token vector are properly nested.
383static bool CheckMatchedBrackets(const SmallVectorImpl<Token> &Tokens) {
384 SmallVector<Bracket, 8> Brackets;
385 for (SmallVectorImpl<Token>::const_iterator I = Tokens.begin(),
386 E = Tokens.end();
387 I != E; ++I) {
388 if (I->is(tok::l_paren)) {
389 Brackets.push_back(Paren);
390 } else if (I->is(tok::r_paren)) {
391 if (Brackets.empty() || Brackets.back() == Brace)
392 return false;
393 Brackets.pop_back();
394 } else if (I->is(tok::l_brace)) {
395 Brackets.push_back(Brace);
396 } else if (I->is(tok::r_brace)) {
397 if (Brackets.empty() || Brackets.back() == Paren)
398 return false;
399 Brackets.pop_back();
400 }
401 }
402 if (!Brackets.empty())
403 return false;
404 return true;
405}
406
407/// GenerateNewArgTokens - Returns true if OldTokens can be converted to a new
408/// vector of tokens in NewTokens. The new number of arguments will be placed
409/// in NumArgs and the ranges which need to surrounded in parentheses will be
410/// in ParenHints.
411/// Returns false if the token stream cannot be changed. If this is because
412/// of an initializer list starting a macro argument, the range of those
413/// initializer lists will be place in InitLists.
414static bool GenerateNewArgTokens(Preprocessor &PP,
415 SmallVectorImpl<Token> &OldTokens,
416 SmallVectorImpl<Token> &NewTokens,
417 unsigned &NumArgs,
418 SmallVectorImpl<SourceRange> &ParenHints,
419 SmallVectorImpl<SourceRange> &InitLists) {
420 if (!CheckMatchedBrackets(OldTokens))
421 return false;
422
423 // Once it is known that the brackets are matched, only a simple count of the
424 // braces is needed.
425 unsigned Braces = 0;
426
427 // First token of a new macro argument.
428 SmallVectorImpl<Token>::iterator ArgStartIterator = OldTokens.begin();
429
430 // First closing brace in a new macro argument. Used to generate
431 // SourceRanges for InitLists.
432 SmallVectorImpl<Token>::iterator ClosingBrace = OldTokens.end();
433 NumArgs = 0;
434 Token TempToken;
435 // Set to true when a macro separator token is found inside a braced list.
436 // If true, the fixed argument spans multiple old arguments and ParenHints
437 // will be updated.
438 bool FoundSeparatorToken = false;
439 for (SmallVectorImpl<Token>::iterator I = OldTokens.begin(),
440 E = OldTokens.end();
441 I != E; ++I) {
442 if (I->is(tok::l_brace)) {
443 ++Braces;
444 } else if (I->is(tok::r_brace)) {
445 --Braces;
446 if (Braces == 0 && ClosingBrace == E && FoundSeparatorToken)
447 ClosingBrace = I;
448 } else if (I->is(tok::eof)) {
449 // EOF token is used to separate macro arguments
450 if (Braces != 0) {
451 // Assume comma separator is actually braced list separator and change
452 // it back to a comma.
453 FoundSeparatorToken = true;
454 I->setKind(tok::comma);
455 I->setLength(1);
456 } else { // Braces == 0
457 // Separator token still separates arguments.
458 ++NumArgs;
459
460 // If the argument starts with a brace, it can't be fixed with
461 // parentheses. A different diagnostic will be given.
462 if (FoundSeparatorToken && ArgStartIterator->is(tok::l_brace)) {
463 InitLists.push_back(
464 SourceRange(ArgStartIterator->getLocation(),
465 PP.getLocForEndOfToken(ClosingBrace->getLocation())));
466 ClosingBrace = E;
467 }
468
469 // Add left paren
470 if (FoundSeparatorToken) {
471 TempToken.startToken();
472 TempToken.setKind(tok::l_paren);
473 TempToken.setLocation(ArgStartIterator->getLocation());
474 TempToken.setLength(0);
475 NewTokens.push_back(TempToken);
476 }
477
478 // Copy over argument tokens
479 NewTokens.insert(NewTokens.end(), ArgStartIterator, I);
480
481 // Add right paren and store the paren locations in ParenHints
482 if (FoundSeparatorToken) {
483 SourceLocation Loc = PP.getLocForEndOfToken((I - 1)->getLocation());
484 TempToken.startToken();
485 TempToken.setKind(tok::r_paren);
486 TempToken.setLocation(Loc);
487 TempToken.setLength(0);
488 NewTokens.push_back(TempToken);
489 ParenHints.push_back(SourceRange(ArgStartIterator->getLocation(),
490 Loc));
491 }
492
493 // Copy separator token
494 NewTokens.push_back(*I);
495
496 // Reset values
497 ArgStartIterator = I + 1;
498 FoundSeparatorToken = false;
499 }
500 }
501 }
502
503 return !ParenHints.empty() && InitLists.empty();
504}
505
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000506/// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next
507/// token is the '(' of the macro, this method is invoked to read all of the
508/// actual arguments specified for the macro invocation. This returns null on
509/// error.
510MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
511 MacroInfo *MI,
512 SourceLocation &MacroEnd) {
513 // The number of fixed arguments to parse.
514 unsigned NumFixedArgsLeft = MI->getNumArgs();
515 bool isVariadic = MI->isVariadic();
516
517 // Outer loop, while there are more arguments, keep reading them.
518 Token Tok;
519
520 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
521 // an argument value in a macro could expand to ',' or '(' or ')'.
522 LexUnexpandedToken(Tok);
523 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
524
525 // ArgTokens - Build up a list of tokens that make up each argument. Each
526 // argument is separated by an EOF token. Use a SmallVector so we can avoid
527 // heap allocations in the common case.
528 SmallVector<Token, 64> ArgTokens;
Argyrios Kyrtzidisc1d9a672012-12-21 01:17:20 +0000529 bool ContainsCodeCompletionTok = false;
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000530
Richard Trieu79b45382013-07-23 18:01:49 +0000531 SourceLocation TooManyArgsLoc;
532
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000533 unsigned NumActuals = 0;
534 while (Tok.isNot(tok::r_paren)) {
Argyrios Kyrtzidisc1d9a672012-12-21 01:17:20 +0000535 if (ContainsCodeCompletionTok && (Tok.is(tok::eof) || Tok.is(tok::eod)))
536 break;
537
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000538 assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) &&
539 "only expect argument separators here");
540
541 unsigned ArgTokenStart = ArgTokens.size();
542 SourceLocation ArgStartLoc = Tok.getLocation();
543
544 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
545 // that we already consumed the first one.
546 unsigned NumParens = 0;
547
548 while (1) {
549 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
550 // an argument value in a macro could expand to ',' or '(' or ')'.
551 LexUnexpandedToken(Tok);
552
553 if (Tok.is(tok::eof) || Tok.is(tok::eod)) { // "#if f(<eof>" & "#if f(\n"
Argyrios Kyrtzidisc1d9a672012-12-21 01:17:20 +0000554 if (!ContainsCodeCompletionTok) {
555 Diag(MacroName, diag::err_unterm_macro_invoc);
556 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
557 << MacroName.getIdentifierInfo();
558 // Do not lose the EOF/EOD. Return it to the client.
559 MacroName = Tok;
560 return 0;
561 } else {
Argyrios Kyrtzidis9fd15712012-12-22 04:48:10 +0000562 // Do not lose the EOF/EOD.
563 Token *Toks = new Token[1];
564 Toks[0] = Tok;
565 EnterTokenStream(Toks, 1, true, true);
Argyrios Kyrtzidisc1d9a672012-12-21 01:17:20 +0000566 break;
567 }
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000568 } else if (Tok.is(tok::r_paren)) {
569 // If we found the ) token, the macro arg list is done.
570 if (NumParens-- == 0) {
571 MacroEnd = Tok.getLocation();
572 break;
573 }
574 } else if (Tok.is(tok::l_paren)) {
575 ++NumParens;
Reid Kleckner596b85c2013-06-26 17:16:08 +0000576 } else if (Tok.is(tok::comma) && NumParens == 0 &&
577 !(Tok.getFlags() & Token::IgnoredComma)) {
578 // In Microsoft-compatibility mode, single commas from nested macro
579 // expansions should not be considered as argument separators. We test
580 // for this with the IgnoredComma token flag above.
581
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000582 // Comma ends this argument if there are more fixed arguments expected.
583 // However, if this is a variadic macro, and this is part of the
584 // variadic part, then the comma is just an argument token.
585 if (!isVariadic) break;
586 if (NumFixedArgsLeft > 1)
587 break;
588 } else if (Tok.is(tok::comment) && !KeepMacroComments) {
589 // If this is a comment token in the argument list and we're just in
590 // -C mode (not -CC mode), discard the comment.
591 continue;
592 } else if (Tok.getIdentifierInfo() != 0) {
593 // Reading macro arguments can cause macros that we are currently
594 // expanding from to be popped off the expansion stack. Doing so causes
595 // them to be reenabled for expansion. Here we record whether any
596 // identifiers we lex as macro arguments correspond to disabled macros.
597 // If so, we mark the token as noexpand. This is a subtle aspect of
598 // C99 6.10.3.4p2.
599 if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
600 if (!MI->isEnabled())
601 Tok.setFlag(Token::DisableExpand);
602 } else if (Tok.is(tok::code_completion)) {
Argyrios Kyrtzidisc1d9a672012-12-21 01:17:20 +0000603 ContainsCodeCompletionTok = true;
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000604 if (CodeComplete)
605 CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(),
606 MI, NumActuals);
607 // Don't mark that we reached the code-completion point because the
608 // parser is going to handle the token and there will be another
609 // code-completion callback.
610 }
611
612 ArgTokens.push_back(Tok);
613 }
614
615 // If this was an empty argument list foo(), don't add this as an empty
616 // argument.
617 if (ArgTokens.empty() && Tok.getKind() == tok::r_paren)
618 break;
619
620 // If this is not a variadic macro, and too many args were specified, emit
621 // an error.
Richard Trieu79b45382013-07-23 18:01:49 +0000622 if (!isVariadic && NumFixedArgsLeft == 0 && TooManyArgsLoc.isInvalid()) {
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000623 if (ArgTokens.size() != ArgTokenStart)
Richard Trieu79b45382013-07-23 18:01:49 +0000624 TooManyArgsLoc = ArgTokens[ArgTokenStart].getLocation();
625 else
626 TooManyArgsLoc = ArgStartLoc;
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000627 }
628
Richard Trieu79b45382013-07-23 18:01:49 +0000629 // Empty arguments are standard in C99 and C++0x, and are supported as an
630 // extension in other modes.
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000631 if (ArgTokens.size() == ArgTokenStart && !LangOpts.C99)
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000632 Diag(Tok, LangOpts.CPlusPlus11 ?
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000633 diag::warn_cxx98_compat_empty_fnmacro_arg :
634 diag::ext_empty_fnmacro_arg);
635
636 // Add a marker EOF token to the end of the token list for this argument.
637 Token EOFTok;
638 EOFTok.startToken();
639 EOFTok.setKind(tok::eof);
640 EOFTok.setLocation(Tok.getLocation());
641 EOFTok.setLength(0);
642 ArgTokens.push_back(EOFTok);
643 ++NumActuals;
Richard Trieu79b45382013-07-23 18:01:49 +0000644 if (!ContainsCodeCompletionTok && NumFixedArgsLeft != 0)
Argyrios Kyrtzidisfb703802013-02-22 22:28:58 +0000645 --NumFixedArgsLeft;
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000646 }
647
648 // Okay, we either found the r_paren. Check to see if we parsed too few
649 // arguments.
650 unsigned MinArgsExpected = MI->getNumArgs();
651
Richard Trieu79b45382013-07-23 18:01:49 +0000652 // If this is not a variadic macro, and too many args were specified, emit
653 // an error.
654 if (!isVariadic && NumActuals > MinArgsExpected &&
655 !ContainsCodeCompletionTok) {
656 // Emit the diagnostic at the macro name in case there is a missing ).
657 // Emitting it at the , could be far away from the macro name.
658 Diag(TooManyArgsLoc, diag::err_too_many_args_in_macro_invoc);
659 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
660 << MacroName.getIdentifierInfo();
661
662 // Commas from braced initializer lists will be treated as argument
663 // separators inside macros. Attempt to correct for this with parentheses.
664 // TODO: See if this can be generalized to angle brackets for templates
665 // inside macro arguments.
666
Bob Wilson57217352013-07-27 21:59:57 +0000667 SmallVector<Token, 4> FixedArgTokens;
Richard Trieu79b45382013-07-23 18:01:49 +0000668 unsigned FixedNumArgs = 0;
669 SmallVector<SourceRange, 4> ParenHints, InitLists;
670 if (!GenerateNewArgTokens(*this, ArgTokens, FixedArgTokens, FixedNumArgs,
671 ParenHints, InitLists)) {
672 if (!InitLists.empty()) {
673 DiagnosticBuilder DB =
674 Diag(MacroName,
675 diag::note_init_list_at_beginning_of_macro_argument);
676 for (SmallVector<SourceRange, 4>::iterator
677 Range = InitLists.begin(), RangeEnd = InitLists.end();
678 Range != RangeEnd; ++Range) {
679 if (DB.hasMaxRanges())
680 break;
681 DB << *Range;
682 }
683 }
684 return 0;
685 }
686 if (FixedNumArgs != MinArgsExpected)
687 return 0;
688
689 DiagnosticBuilder DB = Diag(MacroName, diag::note_suggest_parens_for_macro);
690 for (SmallVector<SourceRange, 4>::iterator
691 ParenLocation = ParenHints.begin(), ParenEnd = ParenHints.end();
692 ParenLocation != ParenEnd; ++ParenLocation) {
693 if (DB.hasMaxFixItHints())
694 break;
695 DB << FixItHint::CreateInsertion(ParenLocation->getBegin(), "(");
696 if (DB.hasMaxFixItHints())
697 break;
698 DB << FixItHint::CreateInsertion(ParenLocation->getEnd(), ")");
699 }
700 ArgTokens.swap(FixedArgTokens);
701 NumActuals = FixedNumArgs;
702 }
703
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000704 // See MacroArgs instance var for description of this.
705 bool isVarargsElided = false;
706
Argyrios Kyrtzidisd4635d42012-12-21 01:51:12 +0000707 if (ContainsCodeCompletionTok) {
708 // Recover from not-fully-formed macro invocation during code-completion.
709 Token EOFTok;
710 EOFTok.startToken();
711 EOFTok.setKind(tok::eof);
712 EOFTok.setLocation(Tok.getLocation());
713 EOFTok.setLength(0);
714 for (; NumActuals < MinArgsExpected; ++NumActuals)
715 ArgTokens.push_back(EOFTok);
716 }
717
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000718 if (NumActuals < MinArgsExpected) {
719 // There are several cases where too few arguments is ok, handle them now.
720 if (NumActuals == 0 && MinArgsExpected == 1) {
721 // #define A(X) or #define A(...) ---> A()
722
723 // If there is exactly one argument, and that argument is missing,
724 // then we have an empty "()" argument empty list. This is fine, even if
725 // the macro expects one argument (the argument is just empty).
726 isVarargsElided = MI->isVariadic();
727 } else if (MI->isVariadic() &&
728 (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X)
729 (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A()
730 // Varargs where the named vararg parameter is missing: OK as extension.
731 // #define A(x, ...)
732 // A("blah")
Eli Friedman14d3c792012-11-14 02:18:46 +0000733 //
734 // If the macro contains the comma pasting extension, the diagnostic
735 // is suppressed; we know we'll get another diagnostic later.
736 if (!MI->hasCommaPasting()) {
737 Diag(Tok, diag::ext_missing_varargs_arg);
738 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
739 << MacroName.getIdentifierInfo();
740 }
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000741
742 // Remember this occurred, allowing us to elide the comma when used for
743 // cases like:
744 // #define A(x, foo...) blah(a, ## foo)
745 // #define B(x, ...) blah(a, ## __VA_ARGS__)
746 // #define C(...) blah(a, ## __VA_ARGS__)
747 // A(x) B(x) C()
748 isVarargsElided = true;
Argyrios Kyrtzidisc1d9a672012-12-21 01:17:20 +0000749 } else if (!ContainsCodeCompletionTok) {
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000750 // Otherwise, emit the error.
751 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
Argyrios Kyrtzidis164fdb62012-12-14 18:53:47 +0000752 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
753 << MacroName.getIdentifierInfo();
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000754 return 0;
755 }
756
757 // Add a marker EOF token to the end of the token list for this argument.
758 SourceLocation EndLoc = Tok.getLocation();
759 Tok.startToken();
760 Tok.setKind(tok::eof);
761 Tok.setLocation(EndLoc);
762 Tok.setLength(0);
763 ArgTokens.push_back(Tok);
764
765 // If we expect two arguments, add both as empty.
766 if (NumActuals == 0 && MinArgsExpected == 2)
767 ArgTokens.push_back(Tok);
768
Argyrios Kyrtzidisc1d9a672012-12-21 01:17:20 +0000769 } else if (NumActuals > MinArgsExpected && !MI->isVariadic() &&
770 !ContainsCodeCompletionTok) {
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000771 // Emit the diagnostic at the macro name in case there is a missing ).
772 // Emitting it at the , could be far away from the macro name.
773 Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
Argyrios Kyrtzidis164fdb62012-12-14 18:53:47 +0000774 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
775 << MacroName.getIdentifierInfo();
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000776 return 0;
777 }
778
779 return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this);
780}
781
782/// \brief Keeps macro expanded tokens for TokenLexers.
783//
784/// Works like a stack; a TokenLexer adds the macro expanded tokens that is
785/// going to lex in the cache and when it finishes the tokens are removed
786/// from the end of the cache.
787Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer,
788 ArrayRef<Token> tokens) {
789 assert(tokLexer);
790 if (tokens.empty())
791 return 0;
792
793 size_t newIndex = MacroExpandedTokens.size();
794 bool cacheNeedsToGrow = tokens.size() >
795 MacroExpandedTokens.capacity()-MacroExpandedTokens.size();
796 MacroExpandedTokens.append(tokens.begin(), tokens.end());
797
798 if (cacheNeedsToGrow) {
799 // Go through all the TokenLexers whose 'Tokens' pointer points in the
800 // buffer and update the pointers to the (potential) new buffer array.
801 for (unsigned i = 0, e = MacroExpandingLexersStack.size(); i != e; ++i) {
802 TokenLexer *prevLexer;
803 size_t tokIndex;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000804 std::tie(prevLexer, tokIndex) = MacroExpandingLexersStack[i];
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000805 prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex;
806 }
807 }
808
809 MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex));
810 return MacroExpandedTokens.data() + newIndex;
811}
812
813void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() {
814 assert(!MacroExpandingLexersStack.empty());
815 size_t tokIndex = MacroExpandingLexersStack.back().second;
816 assert(tokIndex < MacroExpandedTokens.size());
817 // Pop the cached macro expanded tokens from the end.
818 MacroExpandedTokens.resize(tokIndex);
819 MacroExpandingLexersStack.pop_back();
820}
821
822/// ComputeDATE_TIME - Compute the current time, enter it into the specified
823/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
824/// the identifier tokens inserted.
825static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
826 Preprocessor &PP) {
827 time_t TT = time(0);
828 struct tm *TM = localtime(&TT);
829
830 static const char * const Months[] = {
831 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
832 };
833
Dmitri Gribenkoae07f722012-09-24 20:56:28 +0000834 {
835 SmallString<32> TmpBuffer;
836 llvm::raw_svector_ostream TmpStream(TmpBuffer);
837 TmpStream << llvm::format("\"%s %2d %4d\"", Months[TM->tm_mon],
838 TM->tm_mday, TM->tm_year + 1900);
Dmitri Gribenkoae07f722012-09-24 20:56:28 +0000839 Token TmpTok;
840 TmpTok.startToken();
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000841 PP.CreateString(TmpStream.str(), TmpTok);
Dmitri Gribenkoae07f722012-09-24 20:56:28 +0000842 DATELoc = TmpTok.getLocation();
843 }
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000844
Dmitri Gribenkoae07f722012-09-24 20:56:28 +0000845 {
846 SmallString<32> TmpBuffer;
847 llvm::raw_svector_ostream TmpStream(TmpBuffer);
848 TmpStream << llvm::format("\"%02d:%02d:%02d\"",
849 TM->tm_hour, TM->tm_min, TM->tm_sec);
Dmitri Gribenkoae07f722012-09-24 20:56:28 +0000850 Token TmpTok;
851 TmpTok.startToken();
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000852 PP.CreateString(TmpStream.str(), TmpTok);
Dmitri Gribenkoae07f722012-09-24 20:56:28 +0000853 TIMELoc = TmpTok.getLocation();
854 }
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000855}
856
857
858/// HasFeature - Return true if we recognize and implement the feature
859/// specified by the identifier as a standard language feature.
860static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) {
861 const LangOptions &LangOpts = PP.getLangOpts();
862 StringRef Feature = II->getName();
863
864 // Normalize the feature name, __foo__ becomes foo.
865 if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4)
866 Feature = Feature.substr(2, Feature.size() - 4);
867
868 return llvm::StringSwitch<bool>(Feature)
Will Dietzf54319c2013-01-18 11:30:38 +0000869 .Case("address_sanitizer", LangOpts.Sanitize.Address)
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000870 .Case("attribute_analyzer_noreturn", true)
871 .Case("attribute_availability", true)
872 .Case("attribute_availability_with_message", true)
873 .Case("attribute_cf_returns_not_retained", true)
874 .Case("attribute_cf_returns_retained", true)
875 .Case("attribute_deprecated_with_message", true)
876 .Case("attribute_ext_vector_type", true)
877 .Case("attribute_ns_returns_not_retained", true)
878 .Case("attribute_ns_returns_retained", true)
879 .Case("attribute_ns_consumes_self", true)
880 .Case("attribute_ns_consumed", true)
881 .Case("attribute_cf_consumed", true)
882 .Case("attribute_objc_ivar_unused", true)
883 .Case("attribute_objc_method_family", true)
884 .Case("attribute_overloadable", true)
885 .Case("attribute_unavailable_with_message", true)
886 .Case("attribute_unused_on_fields", true)
887 .Case("blocks", LangOpts.Blocks)
David Blaikie021221d2013-07-29 18:24:03 +0000888 .Case("c_thread_safety_attributes", true)
Justin Bogner4e3a01f2014-04-16 02:56:48 +0000889 .Case("cxx_exceptions", LangOpts.CXXExceptions)
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000890 .Case("cxx_rtti", LangOpts.RTTI)
891 .Case("enumerator_attributes", true)
Will Dietzf54319c2013-01-18 11:30:38 +0000892 .Case("memory_sanitizer", LangOpts.Sanitize.Memory)
893 .Case("thread_sanitizer", LangOpts.Sanitize.Thread)
Peter Collingbournec3772752013-08-07 22:47:34 +0000894 .Case("dataflow_sanitizer", LangOpts.Sanitize.DataFlow)
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000895 // Objective-C features
896 .Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE?
897 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
898 .Case("objc_arc_weak", LangOpts.ObjCARCWeak)
899 .Case("objc_default_synthesize_properties", LangOpts.ObjC2)
900 .Case("objc_fixed_enum", LangOpts.ObjC2)
901 .Case("objc_instancetype", LangOpts.ObjC2)
902 .Case("objc_modules", LangOpts.ObjC2 && LangOpts.Modules)
903 .Case("objc_nonfragile_abi", LangOpts.ObjCRuntime.isNonFragile())
Ted Kremenekdae8f9f2013-01-04 19:04:44 +0000904 .Case("objc_property_explicit_atomic", true) // Does clang support explicit "atomic" keyword?
Ted Kremenekb0cba4c2013-10-14 23:48:27 +0000905 .Case("objc_protocol_qualifier_mangling", true)
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000906 .Case("objc_weak_class", LangOpts.ObjCRuntime.hasWeakClassImport())
907 .Case("ownership_holds", true)
908 .Case("ownership_returns", true)
909 .Case("ownership_takes", true)
910 .Case("objc_bool", true)
911 .Case("objc_subscripting", LangOpts.ObjCRuntime.isNonFragile())
912 .Case("objc_array_literals", LangOpts.ObjC2)
913 .Case("objc_dictionary_literals", LangOpts.ObjC2)
914 .Case("objc_boxed_expressions", LangOpts.ObjC2)
915 .Case("arc_cf_code_audited", true)
916 // C11 features
917 .Case("c_alignas", LangOpts.C11)
918 .Case("c_atomic", LangOpts.C11)
919 .Case("c_generic_selections", LangOpts.C11)
920 .Case("c_static_assert", LangOpts.C11)
Richard Smith6d540142014-05-09 21:08:59 +0000921 .Case("c_thread_local",
Douglas Gregora7130bf2013-05-02 05:28:32 +0000922 LangOpts.C11 && PP.getTargetInfo().isTLSSupported())
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000923 // C++11 features
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000924 .Case("cxx_access_control_sfinae", LangOpts.CPlusPlus11)
925 .Case("cxx_alias_templates", LangOpts.CPlusPlus11)
926 .Case("cxx_alignas", LangOpts.CPlusPlus11)
927 .Case("cxx_atomic", LangOpts.CPlusPlus11)
928 .Case("cxx_attributes", LangOpts.CPlusPlus11)
929 .Case("cxx_auto_type", LangOpts.CPlusPlus11)
930 .Case("cxx_constexpr", LangOpts.CPlusPlus11)
931 .Case("cxx_decltype", LangOpts.CPlusPlus11)
932 .Case("cxx_decltype_incomplete_return_types", LangOpts.CPlusPlus11)
933 .Case("cxx_default_function_template_args", LangOpts.CPlusPlus11)
934 .Case("cxx_defaulted_functions", LangOpts.CPlusPlus11)
935 .Case("cxx_delegating_constructors", LangOpts.CPlusPlus11)
936 .Case("cxx_deleted_functions", LangOpts.CPlusPlus11)
937 .Case("cxx_explicit_conversions", LangOpts.CPlusPlus11)
938 .Case("cxx_generalized_initializers", LangOpts.CPlusPlus11)
939 .Case("cxx_implicit_moves", LangOpts.CPlusPlus11)
Richard Smith25b555a2013-04-19 17:00:31 +0000940 .Case("cxx_inheriting_constructors", LangOpts.CPlusPlus11)
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000941 .Case("cxx_inline_namespaces", LangOpts.CPlusPlus11)
942 .Case("cxx_lambdas", LangOpts.CPlusPlus11)
943 .Case("cxx_local_type_template_args", LangOpts.CPlusPlus11)
944 .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus11)
945 .Case("cxx_noexcept", LangOpts.CPlusPlus11)
946 .Case("cxx_nullptr", LangOpts.CPlusPlus11)
947 .Case("cxx_override_control", LangOpts.CPlusPlus11)
948 .Case("cxx_range_for", LangOpts.CPlusPlus11)
949 .Case("cxx_raw_string_literals", LangOpts.CPlusPlus11)
950 .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus11)
951 .Case("cxx_rvalue_references", LangOpts.CPlusPlus11)
952 .Case("cxx_strong_enums", LangOpts.CPlusPlus11)
953 .Case("cxx_static_assert", LangOpts.CPlusPlus11)
Richard Smithb438e622013-09-28 04:37:56 +0000954 .Case("cxx_thread_local",
Douglas Gregora7130bf2013-05-02 05:28:32 +0000955 LangOpts.CPlusPlus11 && PP.getTargetInfo().isTLSSupported())
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000956 .Case("cxx_trailing_return", LangOpts.CPlusPlus11)
957 .Case("cxx_unicode_literals", LangOpts.CPlusPlus11)
958 .Case("cxx_unrestricted_unions", LangOpts.CPlusPlus11)
959 .Case("cxx_user_literals", LangOpts.CPlusPlus11)
960 .Case("cxx_variadic_templates", LangOpts.CPlusPlus11)
Richard Smith0a715422013-05-07 19:32:56 +0000961 // C++1y features
Richard Smith4fb09722013-07-24 17:51:13 +0000962 .Case("cxx_aggregate_nsdmi", LangOpts.CPlusPlus1y)
Richard Smith0a715422013-05-07 19:32:56 +0000963 .Case("cxx_binary_literals", LangOpts.CPlusPlus1y)
Richard Smithc0f7b812013-07-24 17:41:31 +0000964 .Case("cxx_contextual_conversions", LangOpts.CPlusPlus1y)
Richard Smith6d540142014-05-09 21:08:59 +0000965 .Case("cxx_decltype_auto", LangOpts.CPlusPlus1y)
966 .Case("cxx_generic_lambdas", LangOpts.CPlusPlus1y)
Richard Smithb438e622013-09-28 04:37:56 +0000967 .Case("cxx_init_captures", LangOpts.CPlusPlus1y)
Richard Smithc0f7b812013-07-24 17:41:31 +0000968 .Case("cxx_relaxed_constexpr", LangOpts.CPlusPlus1y)
Richard Smith9155be12013-05-12 03:09:35 +0000969 .Case("cxx_return_type_deduction", LangOpts.CPlusPlus1y)
Richard Smithdca0c7a2013-09-27 20:19:41 +0000970 .Case("cxx_variable_templates", LangOpts.CPlusPlus1y)
Richard Smith6d540142014-05-09 21:08:59 +0000971 // C++ TSes
972 //.Case("cxx_runtime_arrays", LangOpts.CPlusPlusTSArrays)
973 //.Case("cxx_concepts", LangOpts.CPlusPlusTSConcepts)
974 // FIXME: Should this be __has_feature or __has_extension?
975 //.Case("raw_invocation_type", LangOpts.CPlusPlus)
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000976 // Type traits
977 .Case("has_nothrow_assign", LangOpts.CPlusPlus)
978 .Case("has_nothrow_copy", LangOpts.CPlusPlus)
979 .Case("has_nothrow_constructor", LangOpts.CPlusPlus)
980 .Case("has_trivial_assign", LangOpts.CPlusPlus)
981 .Case("has_trivial_copy", LangOpts.CPlusPlus)
982 .Case("has_trivial_constructor", LangOpts.CPlusPlus)
983 .Case("has_trivial_destructor", LangOpts.CPlusPlus)
984 .Case("has_virtual_destructor", LangOpts.CPlusPlus)
985 .Case("is_abstract", LangOpts.CPlusPlus)
986 .Case("is_base_of", LangOpts.CPlusPlus)
987 .Case("is_class", LangOpts.CPlusPlus)
Marshall Clowd28acc02014-03-18 14:13:10 +0000988 .Case("is_constructible", LangOpts.CPlusPlus)
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000989 .Case("is_convertible_to", LangOpts.CPlusPlus)
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000990 .Case("is_empty", LangOpts.CPlusPlus)
991 .Case("is_enum", LangOpts.CPlusPlus)
992 .Case("is_final", LangOpts.CPlusPlus)
993 .Case("is_literal", LangOpts.CPlusPlus)
994 .Case("is_standard_layout", LangOpts.CPlusPlus)
995 .Case("is_pod", LangOpts.CPlusPlus)
996 .Case("is_polymorphic", LangOpts.CPlusPlus)
David Majnemera5433082013-10-18 00:33:31 +0000997 .Case("is_sealed", LangOpts.MicrosoftExt)
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000998 .Case("is_trivial", LangOpts.CPlusPlus)
999 .Case("is_trivially_assignable", LangOpts.CPlusPlus)
1000 .Case("is_trivially_constructible", LangOpts.CPlusPlus)
1001 .Case("is_trivially_copyable", LangOpts.CPlusPlus)
1002 .Case("is_union", LangOpts.CPlusPlus)
1003 .Case("modules", LangOpts.Modules)
1004 .Case("tls", PP.getTargetInfo().isTLSSupported())
1005 .Case("underlying_type", LangOpts.CPlusPlus)
1006 .Default(false);
1007}
1008
1009/// HasExtension - Return true if we recognize and implement the feature
1010/// specified by the identifier, either as an extension or a standard language
1011/// feature.
1012static bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) {
1013 if (HasFeature(PP, II))
1014 return true;
1015
1016 // If the use of an extension results in an error diagnostic, extensions are
1017 // effectively unavailable, so just return false here.
1018 if (PP.getDiagnostics().getExtensionHandlingBehavior() ==
1019 DiagnosticsEngine::Ext_Error)
1020 return false;
1021
1022 const LangOptions &LangOpts = PP.getLangOpts();
1023 StringRef Extension = II->getName();
1024
1025 // Normalize the extension name, __foo__ becomes foo.
1026 if (Extension.startswith("__") && Extension.endswith("__") &&
1027 Extension.size() >= 4)
1028 Extension = Extension.substr(2, Extension.size() - 4);
1029
1030 // Because we inherit the feature list from HasFeature, this string switch
1031 // must be less restrictive than HasFeature's.
1032 return llvm::StringSwitch<bool>(Extension)
1033 // C11 features supported by other languages as extensions.
1034 .Case("c_alignas", true)
1035 .Case("c_atomic", true)
1036 .Case("c_generic_selections", true)
1037 .Case("c_static_assert", true)
Ed Schouten401aeba2013-09-14 16:17:20 +00001038 .Case("c_thread_local", PP.getTargetInfo().isTLSSupported())
Richard Smith0a715422013-05-07 19:32:56 +00001039 // C++11 features supported by other languages as extensions.
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001040 .Case("cxx_atomic", LangOpts.CPlusPlus)
1041 .Case("cxx_deleted_functions", LangOpts.CPlusPlus)
1042 .Case("cxx_explicit_conversions", LangOpts.CPlusPlus)
1043 .Case("cxx_inline_namespaces", LangOpts.CPlusPlus)
1044 .Case("cxx_local_type_template_args", LangOpts.CPlusPlus)
1045 .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus)
1046 .Case("cxx_override_control", LangOpts.CPlusPlus)
1047 .Case("cxx_range_for", LangOpts.CPlusPlus)
1048 .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus)
1049 .Case("cxx_rvalue_references", LangOpts.CPlusPlus)
Richard Smith0a715422013-05-07 19:32:56 +00001050 // C++1y features supported by other languages as extensions.
1051 .Case("cxx_binary_literals", true)
Richard Smithb438e622013-09-28 04:37:56 +00001052 .Case("cxx_init_captures", LangOpts.CPlusPlus11)
Alp Tokera8bb9c92014-01-15 04:11:24 +00001053 .Case("cxx_variable_templates", LangOpts.CPlusPlus)
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001054 .Default(false);
1055}
1056
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001057/// EvaluateHasIncludeCommon - Process a '__has_include("path")'
1058/// or '__has_include_next("path")' expression.
1059/// Returns true if successful.
1060static bool EvaluateHasIncludeCommon(Token &Tok,
1061 IdentifierInfo *II, Preprocessor &PP,
1062 const DirectoryLookup *LookupFrom) {
Richard Trieuda031982012-10-22 20:28:48 +00001063 // Save the location of the current token. If a '(' is later found, use
Aaron Ballman5cb24112013-01-15 21:59:46 +00001064 // that location. If not, use the end of this location instead.
Richard Trieuda031982012-10-22 20:28:48 +00001065 SourceLocation LParenLoc = Tok.getLocation();
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001066
Aaron Ballman6ce00002013-01-16 19:32:21 +00001067 // These expressions are only allowed within a preprocessor directive.
1068 if (!PP.isParsingIfOrElifDirective()) {
1069 PP.Diag(LParenLoc, diag::err_pp_directive_required) << II->getName();
1070 return false;
1071 }
1072
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001073 // Get '('.
1074 PP.LexNonComment(Tok);
1075
1076 // Ensure we have a '('.
1077 if (Tok.isNot(tok::l_paren)) {
Richard Trieuda031982012-10-22 20:28:48 +00001078 // No '(', use end of last token.
1079 LParenLoc = PP.getLocForEndOfToken(LParenLoc);
Alp Toker751d6352013-12-30 01:59:29 +00001080 PP.Diag(LParenLoc, diag::err_pp_expected_after) << II << tok::l_paren;
Richard Trieuda031982012-10-22 20:28:48 +00001081 // If the next token looks like a filename or the start of one,
1082 // assume it is and process it as such.
1083 if (!Tok.is(tok::angle_string_literal) && !Tok.is(tok::string_literal) &&
1084 !Tok.is(tok::less))
1085 return false;
1086 } else {
1087 // Save '(' location for possible missing ')' message.
1088 LParenLoc = Tok.getLocation();
1089
Eli Friedmanec94b612013-01-09 02:20:00 +00001090 if (PP.getCurrentLexer()) {
1091 // Get the file name.
1092 PP.getCurrentLexer()->LexIncludeFilename(Tok);
1093 } else {
1094 // We're in a macro, so we can't use LexIncludeFilename; just
1095 // grab the next token.
1096 PP.Lex(Tok);
1097 }
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001098 }
1099
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001100 // Reserve a buffer to get the spelling.
1101 SmallString<128> FilenameBuffer;
1102 StringRef Filename;
1103 SourceLocation EndLoc;
1104
1105 switch (Tok.getKind()) {
1106 case tok::eod:
1107 // If the token kind is EOD, the error has already been diagnosed.
1108 return false;
1109
1110 case tok::angle_string_literal:
1111 case tok::string_literal: {
1112 bool Invalid = false;
1113 Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
1114 if (Invalid)
1115 return false;
1116 break;
1117 }
1118
1119 case tok::less:
1120 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1121 // case, glue the tokens together into FilenameBuffer and interpret those.
1122 FilenameBuffer.push_back('<');
Richard Trieuda031982012-10-22 20:28:48 +00001123 if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) {
1124 // Let the caller know a <eod> was found by changing the Token kind.
1125 Tok.setKind(tok::eod);
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001126 return false; // Found <eod> but no ">"? Diagnostic already emitted.
Richard Trieuda031982012-10-22 20:28:48 +00001127 }
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001128 Filename = FilenameBuffer.str();
1129 break;
1130 default:
1131 PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
1132 return false;
1133 }
1134
Richard Trieuda031982012-10-22 20:28:48 +00001135 SourceLocation FilenameLoc = Tok.getLocation();
1136
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001137 // Get ')'.
1138 PP.LexNonComment(Tok);
1139
1140 // Ensure we have a trailing ).
1141 if (Tok.isNot(tok::r_paren)) {
Alp Toker751d6352013-12-30 01:59:29 +00001142 PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_expected_after)
1143 << II << tok::r_paren;
Alp Tokerec543272013-12-24 09:48:30 +00001144 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001145 return false;
1146 }
1147
1148 bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
1149 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1150 // error.
1151 if (Filename.empty())
1152 return false;
1153
1154 // Search include directories.
1155 const DirectoryLookup *CurDir;
1156 const FileEntry *File =
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001157 PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, CurDir, NULL,
1158 NULL, NULL);
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001159
1160 // Get the result value. A result of true means the file exists.
1161 return File != 0;
1162}
1163
1164/// EvaluateHasInclude - Process a '__has_include("path")' expression.
1165/// Returns true if successful.
1166static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II,
1167 Preprocessor &PP) {
1168 return EvaluateHasIncludeCommon(Tok, II, PP, NULL);
1169}
1170
1171/// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.
1172/// Returns true if successful.
1173static bool EvaluateHasIncludeNext(Token &Tok,
1174 IdentifierInfo *II, Preprocessor &PP) {
1175 // __has_include_next is like __has_include, except that we start
1176 // searching after the current found directory. If we can't do this,
1177 // issue a diagnostic.
1178 const DirectoryLookup *Lookup = PP.GetCurDirLookup();
1179 if (PP.isInPrimaryFile()) {
1180 Lookup = 0;
1181 PP.Diag(Tok, diag::pp_include_next_in_primary);
1182 } else if (Lookup == 0) {
1183 PP.Diag(Tok, diag::pp_include_next_absolute_path);
1184 } else {
1185 // Start looking up in the next directory.
1186 ++Lookup;
1187 }
1188
1189 return EvaluateHasIncludeCommon(Tok, II, PP, Lookup);
1190}
1191
Douglas Gregorc83de302012-09-25 15:44:52 +00001192/// \brief Process __building_module(identifier) expression.
1193/// \returns true if we are building the named module, false otherwise.
1194static bool EvaluateBuildingModule(Token &Tok,
1195 IdentifierInfo *II, Preprocessor &PP) {
1196 // Get '('.
1197 PP.LexNonComment(Tok);
1198
1199 // Ensure we have a '('.
1200 if (Tok.isNot(tok::l_paren)) {
Alp Toker751d6352013-12-30 01:59:29 +00001201 PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II
1202 << tok::l_paren;
Douglas Gregorc83de302012-09-25 15:44:52 +00001203 return false;
1204 }
1205
1206 // Save '(' location for possible missing ')' message.
1207 SourceLocation LParenLoc = Tok.getLocation();
1208
1209 // Get the module name.
1210 PP.LexNonComment(Tok);
1211
1212 // Ensure that we have an identifier.
1213 if (Tok.isNot(tok::identifier)) {
1214 PP.Diag(Tok.getLocation(), diag::err_expected_id_building_module);
1215 return false;
1216 }
1217
1218 bool Result
1219 = Tok.getIdentifierInfo()->getName() == PP.getLangOpts().CurrentModule;
1220
1221 // Get ')'.
1222 PP.LexNonComment(Tok);
1223
1224 // Ensure we have a trailing ).
1225 if (Tok.isNot(tok::r_paren)) {
Alp Toker751d6352013-12-30 01:59:29 +00001226 PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II
1227 << tok::r_paren;
Alp Tokerec543272013-12-24 09:48:30 +00001228 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
Douglas Gregorc83de302012-09-25 15:44:52 +00001229 return false;
1230 }
1231
1232 return Result;
1233}
1234
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001235/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1236/// as a builtin macro, handle it and return the next token as 'Tok'.
1237void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1238 // Figure out which token this is.
1239 IdentifierInfo *II = Tok.getIdentifierInfo();
1240 assert(II && "Can't be a macro without id info!");
1241
1242 // If this is an _Pragma or Microsoft __pragma directive, expand it,
1243 // invoke the pragma handler, then lex the token after it.
1244 if (II == Ident_Pragma)
1245 return Handle_Pragma(Tok);
1246 else if (II == Ident__pragma) // in non-MS mode this is null
1247 return HandleMicrosoft__pragma(Tok);
1248
1249 ++NumBuiltinMacroExpanded;
1250
1251 SmallString<128> TmpBuffer;
1252 llvm::raw_svector_ostream OS(TmpBuffer);
1253
1254 // Set up the return result.
1255 Tok.setIdentifierInfo(0);
1256 Tok.clearFlag(Token::NeedsCleaning);
1257
1258 if (II == Ident__LINE__) {
1259 // C99 6.10.8: "__LINE__: The presumed line number (within the current
1260 // source file) of the current source line (an integer constant)". This can
1261 // be affected by #line.
1262 SourceLocation Loc = Tok.getLocation();
1263
1264 // Advance to the location of the first _, this might not be the first byte
1265 // of the token if it starts with an escaped newline.
1266 Loc = AdvanceToTokenCharacter(Loc, 0);
1267
1268 // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
1269 // a macro expansion. This doesn't matter for object-like macros, but
1270 // can matter for a function-like macro that expands to contain __LINE__.
1271 // Skip down through expansion points until we find a file loc for the
1272 // end of the expansion history.
1273 Loc = SourceMgr.getExpansionRange(Loc).second;
1274 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
1275
1276 // __LINE__ expands to a simple numeric value.
1277 OS << (PLoc.isValid()? PLoc.getLine() : 1);
1278 Tok.setKind(tok::numeric_constant);
1279 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
1280 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
1281 // character string literal)". This can be affected by #line.
1282 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1283
1284 // __BASE_FILE__ is a GNU extension that returns the top of the presumed
1285 // #include stack instead of the current file.
1286 if (II == Ident__BASE_FILE__ && PLoc.isValid()) {
1287 SourceLocation NextLoc = PLoc.getIncludeLoc();
1288 while (NextLoc.isValid()) {
1289 PLoc = SourceMgr.getPresumedLoc(NextLoc);
1290 if (PLoc.isInvalid())
1291 break;
1292
1293 NextLoc = PLoc.getIncludeLoc();
1294 }
1295 }
1296
1297 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
1298 SmallString<128> FN;
1299 if (PLoc.isValid()) {
1300 FN += PLoc.getFilename();
1301 Lexer::Stringify(FN);
1302 OS << '"' << FN.str() << '"';
1303 }
1304 Tok.setKind(tok::string_literal);
1305 } else if (II == Ident__DATE__) {
1306 if (!DATELoc.isValid())
1307 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1308 Tok.setKind(tok::string_literal);
1309 Tok.setLength(strlen("\"Mmm dd yyyy\""));
1310 Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),
1311 Tok.getLocation(),
1312 Tok.getLength()));
1313 return;
1314 } else if (II == Ident__TIME__) {
1315 if (!TIMELoc.isValid())
1316 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1317 Tok.setKind(tok::string_literal);
1318 Tok.setLength(strlen("\"hh:mm:ss\""));
1319 Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),
1320 Tok.getLocation(),
1321 Tok.getLength()));
1322 return;
1323 } else if (II == Ident__INCLUDE_LEVEL__) {
1324 // Compute the presumed include depth of this token. This can be affected
1325 // by GNU line markers.
1326 unsigned Depth = 0;
1327
1328 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1329 if (PLoc.isValid()) {
1330 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1331 for (; PLoc.isValid(); ++Depth)
1332 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1333 }
1334
1335 // __INCLUDE_LEVEL__ expands to a simple numeric value.
1336 OS << Depth;
1337 Tok.setKind(tok::numeric_constant);
1338 } else if (II == Ident__TIMESTAMP__) {
1339 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
1340 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1341
1342 // Get the file that we are lexing out of. If we're currently lexing from
1343 // a macro, dig into the include stack.
1344 const FileEntry *CurFile = 0;
1345 PreprocessorLexer *TheLexer = getCurrentFileLexer();
1346
1347 if (TheLexer)
1348 CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
1349
1350 const char *Result;
1351 if (CurFile) {
1352 time_t TT = CurFile->getModificationTime();
1353 struct tm *TM = localtime(&TT);
1354 Result = asctime(TM);
1355 } else {
1356 Result = "??? ??? ?? ??:??:?? ????\n";
1357 }
1358 // Surround the string with " and strip the trailing newline.
1359 OS << '"' << StringRef(Result, strlen(Result)-1) << '"';
1360 Tok.setKind(tok::string_literal);
1361 } else if (II == Ident__COUNTER__) {
1362 // __COUNTER__ expands to a simple numeric value.
1363 OS << CounterValue++;
1364 Tok.setKind(tok::numeric_constant);
1365 } else if (II == Ident__has_feature ||
1366 II == Ident__has_extension ||
1367 II == Ident__has_builtin ||
Yunzhong Gaoef309f42014-04-11 20:55:19 +00001368 II == Ident__is_identifier ||
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001369 II == Ident__has_attribute) {
1370 // The argument to these builtins should be a parenthesized identifier.
1371 SourceLocation StartLoc = Tok.getLocation();
1372
1373 bool IsValid = false;
1374 IdentifierInfo *FeatureII = 0;
1375
1376 // Read the '('.
Andy Gibbsd41d0942012-11-17 19:18:27 +00001377 LexUnexpandedToken(Tok);
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001378 if (Tok.is(tok::l_paren)) {
1379 // Read the identifier
Andy Gibbsd41d0942012-11-17 19:18:27 +00001380 LexUnexpandedToken(Tok);
Richard Smithbaf29122013-07-09 00:57:56 +00001381 if ((FeatureII = Tok.getIdentifierInfo())) {
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001382 // Read the ')'.
Andy Gibbsd41d0942012-11-17 19:18:27 +00001383 LexUnexpandedToken(Tok);
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001384 if (Tok.is(tok::r_paren))
1385 IsValid = true;
1386 }
1387 }
1388
1389 bool Value = false;
1390 if (!IsValid)
1391 Diag(StartLoc, diag::err_feature_check_malformed);
Yunzhong Gaoef309f42014-04-11 20:55:19 +00001392 else if (II == Ident__is_identifier)
1393 Value = FeatureII->getTokenID() == tok::identifier;
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001394 else if (II == Ident__has_builtin) {
1395 // Check for a builtin is trivial.
1396 Value = FeatureII->getBuiltinID() != 0;
1397 } else if (II == Ident__has_attribute)
Aaron Ballman759c71d2014-03-31 15:26:40 +00001398 Value = hasAttribute(AttrSyntax::Generic, nullptr, FeatureII,
Aaron Ballman2fbf9942014-03-31 13:14:44 +00001399 getTargetInfo().getTriple(), getLangOpts());
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001400 else if (II == Ident__has_extension)
1401 Value = HasExtension(*this, FeatureII);
1402 else {
1403 assert(II == Ident__has_feature && "Must be feature check");
1404 Value = HasFeature(*this, FeatureII);
1405 }
1406
1407 OS << (int)Value;
1408 if (IsValid)
1409 Tok.setKind(tok::numeric_constant);
1410 } else if (II == Ident__has_include ||
1411 II == Ident__has_include_next) {
1412 // The argument to these two builtins should be a parenthesized
1413 // file name string literal using angle brackets (<>) or
1414 // double-quotes ("").
1415 bool Value;
1416 if (II == Ident__has_include)
1417 Value = EvaluateHasInclude(Tok, II, *this);
1418 else
1419 Value = EvaluateHasIncludeNext(Tok, II, *this);
1420 OS << (int)Value;
Richard Trieuda031982012-10-22 20:28:48 +00001421 if (Tok.is(tok::r_paren))
1422 Tok.setKind(tok::numeric_constant);
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001423 } else if (II == Ident__has_warning) {
1424 // The argument should be a parenthesized string literal.
1425 // The argument to these builtins should be a parenthesized identifier.
1426 SourceLocation StartLoc = Tok.getLocation();
1427 bool IsValid = false;
1428 bool Value = false;
1429 // Read the '('.
Andy Gibbs58905d22012-11-17 19:15:38 +00001430 LexUnexpandedToken(Tok);
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001431 do {
Andy Gibbs58905d22012-11-17 19:15:38 +00001432 if (Tok.isNot(tok::l_paren)) {
1433 Diag(StartLoc, diag::err_warning_check_malformed);
1434 break;
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001435 }
Andy Gibbs58905d22012-11-17 19:15:38 +00001436
1437 LexUnexpandedToken(Tok);
1438 std::string WarningName;
1439 SourceLocation StrStartLoc = Tok.getLocation();
Andy Gibbsa8df57a2012-11-17 19:16:52 +00001440 if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'",
1441 /*MacroExpansion=*/false)) {
Andy Gibbs58905d22012-11-17 19:15:38 +00001442 // Eat tokens until ')'.
Andy Gibbsb5b30c42012-11-17 22:17:28 +00001443 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eod) &&
1444 Tok.isNot(tok::eof))
Andy Gibbs58905d22012-11-17 19:15:38 +00001445 LexUnexpandedToken(Tok);
1446 break;
1447 }
1448
1449 // Is the end a ')'?
1450 if (!(IsValid = Tok.is(tok::r_paren))) {
1451 Diag(StartLoc, diag::err_warning_check_malformed);
1452 break;
1453 }
1454
1455 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1456 WarningName[1] != 'W') {
1457 Diag(StrStartLoc, diag::warn_has_warning_invalid_option);
1458 break;
1459 }
1460
1461 // Finally, check if the warning flags maps to a diagnostic group.
1462 // We construct a SmallVector here to talk to getDiagnosticIDs().
1463 // Although we don't use the result, this isn't a hot path, and not
1464 // worth special casing.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001465 SmallVector<diag::kind, 10> Diags;
Andy Gibbs58905d22012-11-17 19:15:38 +00001466 Value = !getDiagnostics().getDiagnosticIDs()->
1467 getDiagnosticsInGroup(WarningName.substr(2), Diags);
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001468 } while (false);
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001469
1470 OS << (int)Value;
Andy Gibbsf591982b2012-11-17 19:14:53 +00001471 if (IsValid)
1472 Tok.setKind(tok::numeric_constant);
Douglas Gregorc83de302012-09-25 15:44:52 +00001473 } else if (II == Ident__building_module) {
1474 // The argument to this builtin should be an identifier. The
1475 // builtin evaluates to 1 when that identifier names the module we are
1476 // currently building.
1477 OS << (int)EvaluateBuildingModule(Tok, II, *this);
1478 Tok.setKind(tok::numeric_constant);
1479 } else if (II == Ident__MODULE__) {
1480 // The current module as an identifier.
1481 OS << getLangOpts().CurrentModule;
1482 IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule);
1483 Tok.setIdentifierInfo(ModuleII);
1484 Tok.setKind(ModuleII->getTokenID());
Richard Smithae385082014-03-15 00:06:08 +00001485 } else if (II == Ident__identifier) {
1486 SourceLocation Loc = Tok.getLocation();
1487
1488 // We're expecting '__identifier' '(' identifier ')'. Try to recover
1489 // if the parens are missing.
1490 LexNonComment(Tok);
1491 if (Tok.isNot(tok::l_paren)) {
1492 // No '(', use end of last token.
1493 Diag(getLocForEndOfToken(Loc), diag::err_pp_expected_after)
1494 << II << tok::l_paren;
1495 // If the next token isn't valid as our argument, we can't recover.
1496 if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
1497 Tok.setKind(tok::identifier);
1498 return;
1499 }
1500
1501 SourceLocation LParenLoc = Tok.getLocation();
1502 LexNonComment(Tok);
1503
1504 if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
1505 Tok.setKind(tok::identifier);
1506 else {
1507 Diag(Tok.getLocation(), diag::err_pp_identifier_arg_not_identifier)
1508 << Tok.getKind();
1509 // Don't walk past anything that's not a real token.
1510 if (Tok.is(tok::eof) || Tok.is(tok::eod) || Tok.isAnnotation())
1511 return;
1512 }
1513
1514 // Discard the ')', preserving 'Tok' as our result.
1515 Token RParen;
1516 LexNonComment(RParen);
1517 if (RParen.isNot(tok::r_paren)) {
1518 Diag(getLocForEndOfToken(Tok.getLocation()), diag::err_pp_expected_after)
1519 << Tok.getKind() << tok::r_paren;
1520 Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1521 }
1522 return;
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001523 } else {
1524 llvm_unreachable("Unknown identifier!");
1525 }
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +00001526 CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation());
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001527}
1528
1529void Preprocessor::markMacroAsUsed(MacroInfo *MI) {
1530 // If the 'used' status changed, and the macro requires 'unused' warning,
1531 // remove its SourceLocation from the warn-for-unused-macro locations.
1532 if (MI->isWarnIfUnused() && !MI->isUsed())
1533 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
1534 MI->setIsUsed(true);
1535}