blob: cd96f8cf34cb7baa92c9ad0d79d36d43e14663e3 [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)
Douglas Gregora7130bf2013-05-02 05:28:32 +0000921 .Case("c_thread_local",
922 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 Smithb438e622013-09-28 04:37:56 +0000965 //.Case("cxx_generic_lambdas", LangOpts.CPlusPlus1y)
966 .Case("cxx_init_captures", LangOpts.CPlusPlus1y)
Richard Smithc0f7b812013-07-24 17:41:31 +0000967 .Case("cxx_relaxed_constexpr", LangOpts.CPlusPlus1y)
Richard Smith9155be12013-05-12 03:09:35 +0000968 .Case("cxx_return_type_deduction", LangOpts.CPlusPlus1y)
Richard Smithb438e622013-09-28 04:37:56 +0000969 //.Case("cxx_runtime_arrays", LangOpts.CPlusPlus1y)
Richard Smithdca0c7a2013-09-27 20:19:41 +0000970 .Case("cxx_variable_templates", LangOpts.CPlusPlus1y)
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000971 // Type traits
972 .Case("has_nothrow_assign", LangOpts.CPlusPlus)
973 .Case("has_nothrow_copy", LangOpts.CPlusPlus)
974 .Case("has_nothrow_constructor", LangOpts.CPlusPlus)
975 .Case("has_trivial_assign", LangOpts.CPlusPlus)
976 .Case("has_trivial_copy", LangOpts.CPlusPlus)
977 .Case("has_trivial_constructor", LangOpts.CPlusPlus)
978 .Case("has_trivial_destructor", LangOpts.CPlusPlus)
979 .Case("has_virtual_destructor", LangOpts.CPlusPlus)
980 .Case("is_abstract", LangOpts.CPlusPlus)
981 .Case("is_base_of", LangOpts.CPlusPlus)
982 .Case("is_class", LangOpts.CPlusPlus)
Marshall Clowd28acc02014-03-18 14:13:10 +0000983 .Case("is_constructible", LangOpts.CPlusPlus)
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000984 .Case("is_convertible_to", LangOpts.CPlusPlus)
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000985 .Case("is_empty", LangOpts.CPlusPlus)
986 .Case("is_enum", LangOpts.CPlusPlus)
987 .Case("is_final", LangOpts.CPlusPlus)
988 .Case("is_literal", LangOpts.CPlusPlus)
989 .Case("is_standard_layout", LangOpts.CPlusPlus)
990 .Case("is_pod", LangOpts.CPlusPlus)
991 .Case("is_polymorphic", LangOpts.CPlusPlus)
David Majnemera5433082013-10-18 00:33:31 +0000992 .Case("is_sealed", LangOpts.MicrosoftExt)
Joao Matosc0d4c1b2012-08-31 21:34:27 +0000993 .Case("is_trivial", LangOpts.CPlusPlus)
994 .Case("is_trivially_assignable", LangOpts.CPlusPlus)
995 .Case("is_trivially_constructible", LangOpts.CPlusPlus)
996 .Case("is_trivially_copyable", LangOpts.CPlusPlus)
997 .Case("is_union", LangOpts.CPlusPlus)
998 .Case("modules", LangOpts.Modules)
999 .Case("tls", PP.getTargetInfo().isTLSSupported())
1000 .Case("underlying_type", LangOpts.CPlusPlus)
1001 .Default(false);
1002}
1003
1004/// HasExtension - Return true if we recognize and implement the feature
1005/// specified by the identifier, either as an extension or a standard language
1006/// feature.
1007static bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) {
1008 if (HasFeature(PP, II))
1009 return true;
1010
1011 // If the use of an extension results in an error diagnostic, extensions are
1012 // effectively unavailable, so just return false here.
1013 if (PP.getDiagnostics().getExtensionHandlingBehavior() ==
1014 DiagnosticsEngine::Ext_Error)
1015 return false;
1016
1017 const LangOptions &LangOpts = PP.getLangOpts();
1018 StringRef Extension = II->getName();
1019
1020 // Normalize the extension name, __foo__ becomes foo.
1021 if (Extension.startswith("__") && Extension.endswith("__") &&
1022 Extension.size() >= 4)
1023 Extension = Extension.substr(2, Extension.size() - 4);
1024
1025 // Because we inherit the feature list from HasFeature, this string switch
1026 // must be less restrictive than HasFeature's.
1027 return llvm::StringSwitch<bool>(Extension)
1028 // C11 features supported by other languages as extensions.
1029 .Case("c_alignas", true)
1030 .Case("c_atomic", true)
1031 .Case("c_generic_selections", true)
1032 .Case("c_static_assert", true)
Ed Schouten401aeba2013-09-14 16:17:20 +00001033 .Case("c_thread_local", PP.getTargetInfo().isTLSSupported())
Richard Smith0a715422013-05-07 19:32:56 +00001034 // C++11 features supported by other languages as extensions.
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001035 .Case("cxx_atomic", LangOpts.CPlusPlus)
1036 .Case("cxx_deleted_functions", LangOpts.CPlusPlus)
1037 .Case("cxx_explicit_conversions", LangOpts.CPlusPlus)
1038 .Case("cxx_inline_namespaces", LangOpts.CPlusPlus)
1039 .Case("cxx_local_type_template_args", LangOpts.CPlusPlus)
1040 .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus)
1041 .Case("cxx_override_control", LangOpts.CPlusPlus)
1042 .Case("cxx_range_for", LangOpts.CPlusPlus)
1043 .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus)
1044 .Case("cxx_rvalue_references", LangOpts.CPlusPlus)
Richard Smith0a715422013-05-07 19:32:56 +00001045 // C++1y features supported by other languages as extensions.
1046 .Case("cxx_binary_literals", true)
Richard Smithb438e622013-09-28 04:37:56 +00001047 .Case("cxx_init_captures", LangOpts.CPlusPlus11)
Alp Tokera8bb9c92014-01-15 04:11:24 +00001048 .Case("cxx_variable_templates", LangOpts.CPlusPlus)
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001049 .Default(false);
1050}
1051
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001052/// EvaluateHasIncludeCommon - Process a '__has_include("path")'
1053/// or '__has_include_next("path")' expression.
1054/// Returns true if successful.
1055static bool EvaluateHasIncludeCommon(Token &Tok,
1056 IdentifierInfo *II, Preprocessor &PP,
1057 const DirectoryLookup *LookupFrom) {
Richard Trieuda031982012-10-22 20:28:48 +00001058 // Save the location of the current token. If a '(' is later found, use
Aaron Ballman5cb24112013-01-15 21:59:46 +00001059 // that location. If not, use the end of this location instead.
Richard Trieuda031982012-10-22 20:28:48 +00001060 SourceLocation LParenLoc = Tok.getLocation();
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001061
Aaron Ballman6ce00002013-01-16 19:32:21 +00001062 // These expressions are only allowed within a preprocessor directive.
1063 if (!PP.isParsingIfOrElifDirective()) {
1064 PP.Diag(LParenLoc, diag::err_pp_directive_required) << II->getName();
1065 return false;
1066 }
1067
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001068 // Get '('.
1069 PP.LexNonComment(Tok);
1070
1071 // Ensure we have a '('.
1072 if (Tok.isNot(tok::l_paren)) {
Richard Trieuda031982012-10-22 20:28:48 +00001073 // No '(', use end of last token.
1074 LParenLoc = PP.getLocForEndOfToken(LParenLoc);
Alp Toker751d6352013-12-30 01:59:29 +00001075 PP.Diag(LParenLoc, diag::err_pp_expected_after) << II << tok::l_paren;
Richard Trieuda031982012-10-22 20:28:48 +00001076 // If the next token looks like a filename or the start of one,
1077 // assume it is and process it as such.
1078 if (!Tok.is(tok::angle_string_literal) && !Tok.is(tok::string_literal) &&
1079 !Tok.is(tok::less))
1080 return false;
1081 } else {
1082 // Save '(' location for possible missing ')' message.
1083 LParenLoc = Tok.getLocation();
1084
Eli Friedmanec94b612013-01-09 02:20:00 +00001085 if (PP.getCurrentLexer()) {
1086 // Get the file name.
1087 PP.getCurrentLexer()->LexIncludeFilename(Tok);
1088 } else {
1089 // We're in a macro, so we can't use LexIncludeFilename; just
1090 // grab the next token.
1091 PP.Lex(Tok);
1092 }
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001093 }
1094
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001095 // Reserve a buffer to get the spelling.
1096 SmallString<128> FilenameBuffer;
1097 StringRef Filename;
1098 SourceLocation EndLoc;
1099
1100 switch (Tok.getKind()) {
1101 case tok::eod:
1102 // If the token kind is EOD, the error has already been diagnosed.
1103 return false;
1104
1105 case tok::angle_string_literal:
1106 case tok::string_literal: {
1107 bool Invalid = false;
1108 Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
1109 if (Invalid)
1110 return false;
1111 break;
1112 }
1113
1114 case tok::less:
1115 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1116 // case, glue the tokens together into FilenameBuffer and interpret those.
1117 FilenameBuffer.push_back('<');
Richard Trieuda031982012-10-22 20:28:48 +00001118 if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) {
1119 // Let the caller know a <eod> was found by changing the Token kind.
1120 Tok.setKind(tok::eod);
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001121 return false; // Found <eod> but no ">"? Diagnostic already emitted.
Richard Trieuda031982012-10-22 20:28:48 +00001122 }
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001123 Filename = FilenameBuffer.str();
1124 break;
1125 default:
1126 PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
1127 return false;
1128 }
1129
Richard Trieuda031982012-10-22 20:28:48 +00001130 SourceLocation FilenameLoc = Tok.getLocation();
1131
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001132 // Get ')'.
1133 PP.LexNonComment(Tok);
1134
1135 // Ensure we have a trailing ).
1136 if (Tok.isNot(tok::r_paren)) {
Alp Toker751d6352013-12-30 01:59:29 +00001137 PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_expected_after)
1138 << II << tok::r_paren;
Alp Tokerec543272013-12-24 09:48:30 +00001139 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001140 return false;
1141 }
1142
1143 bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
1144 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1145 // error.
1146 if (Filename.empty())
1147 return false;
1148
1149 // Search include directories.
1150 const DirectoryLookup *CurDir;
1151 const FileEntry *File =
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001152 PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, CurDir, NULL,
1153 NULL, NULL);
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001154
1155 // Get the result value. A result of true means the file exists.
1156 return File != 0;
1157}
1158
1159/// EvaluateHasInclude - Process a '__has_include("path")' expression.
1160/// Returns true if successful.
1161static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II,
1162 Preprocessor &PP) {
1163 return EvaluateHasIncludeCommon(Tok, II, PP, NULL);
1164}
1165
1166/// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.
1167/// Returns true if successful.
1168static bool EvaluateHasIncludeNext(Token &Tok,
1169 IdentifierInfo *II, Preprocessor &PP) {
1170 // __has_include_next is like __has_include, except that we start
1171 // searching after the current found directory. If we can't do this,
1172 // issue a diagnostic.
1173 const DirectoryLookup *Lookup = PP.GetCurDirLookup();
1174 if (PP.isInPrimaryFile()) {
1175 Lookup = 0;
1176 PP.Diag(Tok, diag::pp_include_next_in_primary);
1177 } else if (Lookup == 0) {
1178 PP.Diag(Tok, diag::pp_include_next_absolute_path);
1179 } else {
1180 // Start looking up in the next directory.
1181 ++Lookup;
1182 }
1183
1184 return EvaluateHasIncludeCommon(Tok, II, PP, Lookup);
1185}
1186
Douglas Gregorc83de302012-09-25 15:44:52 +00001187/// \brief Process __building_module(identifier) expression.
1188/// \returns true if we are building the named module, false otherwise.
1189static bool EvaluateBuildingModule(Token &Tok,
1190 IdentifierInfo *II, Preprocessor &PP) {
1191 // Get '('.
1192 PP.LexNonComment(Tok);
1193
1194 // Ensure we have a '('.
1195 if (Tok.isNot(tok::l_paren)) {
Alp Toker751d6352013-12-30 01:59:29 +00001196 PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II
1197 << tok::l_paren;
Douglas Gregorc83de302012-09-25 15:44:52 +00001198 return false;
1199 }
1200
1201 // Save '(' location for possible missing ')' message.
1202 SourceLocation LParenLoc = Tok.getLocation();
1203
1204 // Get the module name.
1205 PP.LexNonComment(Tok);
1206
1207 // Ensure that we have an identifier.
1208 if (Tok.isNot(tok::identifier)) {
1209 PP.Diag(Tok.getLocation(), diag::err_expected_id_building_module);
1210 return false;
1211 }
1212
1213 bool Result
1214 = Tok.getIdentifierInfo()->getName() == PP.getLangOpts().CurrentModule;
1215
1216 // Get ')'.
1217 PP.LexNonComment(Tok);
1218
1219 // Ensure we have a trailing ).
1220 if (Tok.isNot(tok::r_paren)) {
Alp Toker751d6352013-12-30 01:59:29 +00001221 PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II
1222 << tok::r_paren;
Alp Tokerec543272013-12-24 09:48:30 +00001223 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
Douglas Gregorc83de302012-09-25 15:44:52 +00001224 return false;
1225 }
1226
1227 return Result;
1228}
1229
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001230/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1231/// as a builtin macro, handle it and return the next token as 'Tok'.
1232void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1233 // Figure out which token this is.
1234 IdentifierInfo *II = Tok.getIdentifierInfo();
1235 assert(II && "Can't be a macro without id info!");
1236
1237 // If this is an _Pragma or Microsoft __pragma directive, expand it,
1238 // invoke the pragma handler, then lex the token after it.
1239 if (II == Ident_Pragma)
1240 return Handle_Pragma(Tok);
1241 else if (II == Ident__pragma) // in non-MS mode this is null
1242 return HandleMicrosoft__pragma(Tok);
1243
1244 ++NumBuiltinMacroExpanded;
1245
1246 SmallString<128> TmpBuffer;
1247 llvm::raw_svector_ostream OS(TmpBuffer);
1248
1249 // Set up the return result.
1250 Tok.setIdentifierInfo(0);
1251 Tok.clearFlag(Token::NeedsCleaning);
1252
1253 if (II == Ident__LINE__) {
1254 // C99 6.10.8: "__LINE__: The presumed line number (within the current
1255 // source file) of the current source line (an integer constant)". This can
1256 // be affected by #line.
1257 SourceLocation Loc = Tok.getLocation();
1258
1259 // Advance to the location of the first _, this might not be the first byte
1260 // of the token if it starts with an escaped newline.
1261 Loc = AdvanceToTokenCharacter(Loc, 0);
1262
1263 // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
1264 // a macro expansion. This doesn't matter for object-like macros, but
1265 // can matter for a function-like macro that expands to contain __LINE__.
1266 // Skip down through expansion points until we find a file loc for the
1267 // end of the expansion history.
1268 Loc = SourceMgr.getExpansionRange(Loc).second;
1269 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
1270
1271 // __LINE__ expands to a simple numeric value.
1272 OS << (PLoc.isValid()? PLoc.getLine() : 1);
1273 Tok.setKind(tok::numeric_constant);
1274 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
1275 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
1276 // character string literal)". This can be affected by #line.
1277 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1278
1279 // __BASE_FILE__ is a GNU extension that returns the top of the presumed
1280 // #include stack instead of the current file.
1281 if (II == Ident__BASE_FILE__ && PLoc.isValid()) {
1282 SourceLocation NextLoc = PLoc.getIncludeLoc();
1283 while (NextLoc.isValid()) {
1284 PLoc = SourceMgr.getPresumedLoc(NextLoc);
1285 if (PLoc.isInvalid())
1286 break;
1287
1288 NextLoc = PLoc.getIncludeLoc();
1289 }
1290 }
1291
1292 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
1293 SmallString<128> FN;
1294 if (PLoc.isValid()) {
1295 FN += PLoc.getFilename();
1296 Lexer::Stringify(FN);
1297 OS << '"' << FN.str() << '"';
1298 }
1299 Tok.setKind(tok::string_literal);
1300 } else if (II == Ident__DATE__) {
1301 if (!DATELoc.isValid())
1302 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1303 Tok.setKind(tok::string_literal);
1304 Tok.setLength(strlen("\"Mmm dd yyyy\""));
1305 Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),
1306 Tok.getLocation(),
1307 Tok.getLength()));
1308 return;
1309 } else if (II == Ident__TIME__) {
1310 if (!TIMELoc.isValid())
1311 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1312 Tok.setKind(tok::string_literal);
1313 Tok.setLength(strlen("\"hh:mm:ss\""));
1314 Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),
1315 Tok.getLocation(),
1316 Tok.getLength()));
1317 return;
1318 } else if (II == Ident__INCLUDE_LEVEL__) {
1319 // Compute the presumed include depth of this token. This can be affected
1320 // by GNU line markers.
1321 unsigned Depth = 0;
1322
1323 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1324 if (PLoc.isValid()) {
1325 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1326 for (; PLoc.isValid(); ++Depth)
1327 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1328 }
1329
1330 // __INCLUDE_LEVEL__ expands to a simple numeric value.
1331 OS << Depth;
1332 Tok.setKind(tok::numeric_constant);
1333 } else if (II == Ident__TIMESTAMP__) {
1334 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
1335 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1336
1337 // Get the file that we are lexing out of. If we're currently lexing from
1338 // a macro, dig into the include stack.
1339 const FileEntry *CurFile = 0;
1340 PreprocessorLexer *TheLexer = getCurrentFileLexer();
1341
1342 if (TheLexer)
1343 CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
1344
1345 const char *Result;
1346 if (CurFile) {
1347 time_t TT = CurFile->getModificationTime();
1348 struct tm *TM = localtime(&TT);
1349 Result = asctime(TM);
1350 } else {
1351 Result = "??? ??? ?? ??:??:?? ????\n";
1352 }
1353 // Surround the string with " and strip the trailing newline.
1354 OS << '"' << StringRef(Result, strlen(Result)-1) << '"';
1355 Tok.setKind(tok::string_literal);
1356 } else if (II == Ident__COUNTER__) {
1357 // __COUNTER__ expands to a simple numeric value.
1358 OS << CounterValue++;
1359 Tok.setKind(tok::numeric_constant);
1360 } else if (II == Ident__has_feature ||
1361 II == Ident__has_extension ||
1362 II == Ident__has_builtin ||
Yunzhong Gaoef309f42014-04-11 20:55:19 +00001363 II == Ident__is_identifier ||
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001364 II == Ident__has_attribute) {
1365 // The argument to these builtins should be a parenthesized identifier.
1366 SourceLocation StartLoc = Tok.getLocation();
1367
1368 bool IsValid = false;
1369 IdentifierInfo *FeatureII = 0;
1370
1371 // Read the '('.
Andy Gibbsd41d0942012-11-17 19:18:27 +00001372 LexUnexpandedToken(Tok);
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001373 if (Tok.is(tok::l_paren)) {
1374 // Read the identifier
Andy Gibbsd41d0942012-11-17 19:18:27 +00001375 LexUnexpandedToken(Tok);
Richard Smithbaf29122013-07-09 00:57:56 +00001376 if ((FeatureII = Tok.getIdentifierInfo())) {
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001377 // Read the ')'.
Andy Gibbsd41d0942012-11-17 19:18:27 +00001378 LexUnexpandedToken(Tok);
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001379 if (Tok.is(tok::r_paren))
1380 IsValid = true;
1381 }
1382 }
1383
1384 bool Value = false;
1385 if (!IsValid)
1386 Diag(StartLoc, diag::err_feature_check_malformed);
Yunzhong Gaoef309f42014-04-11 20:55:19 +00001387 else if (II == Ident__is_identifier)
1388 Value = FeatureII->getTokenID() == tok::identifier;
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001389 else if (II == Ident__has_builtin) {
1390 // Check for a builtin is trivial.
1391 Value = FeatureII->getBuiltinID() != 0;
1392 } else if (II == Ident__has_attribute)
Aaron Ballman759c71d2014-03-31 15:26:40 +00001393 Value = hasAttribute(AttrSyntax::Generic, nullptr, FeatureII,
Aaron Ballman2fbf9942014-03-31 13:14:44 +00001394 getTargetInfo().getTriple(), getLangOpts());
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001395 else if (II == Ident__has_extension)
1396 Value = HasExtension(*this, FeatureII);
1397 else {
1398 assert(II == Ident__has_feature && "Must be feature check");
1399 Value = HasFeature(*this, FeatureII);
1400 }
1401
1402 OS << (int)Value;
1403 if (IsValid)
1404 Tok.setKind(tok::numeric_constant);
1405 } else if (II == Ident__has_include ||
1406 II == Ident__has_include_next) {
1407 // The argument to these two builtins should be a parenthesized
1408 // file name string literal using angle brackets (<>) or
1409 // double-quotes ("").
1410 bool Value;
1411 if (II == Ident__has_include)
1412 Value = EvaluateHasInclude(Tok, II, *this);
1413 else
1414 Value = EvaluateHasIncludeNext(Tok, II, *this);
1415 OS << (int)Value;
Richard Trieuda031982012-10-22 20:28:48 +00001416 if (Tok.is(tok::r_paren))
1417 Tok.setKind(tok::numeric_constant);
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001418 } else if (II == Ident__has_warning) {
1419 // The argument should be a parenthesized string literal.
1420 // The argument to these builtins should be a parenthesized identifier.
1421 SourceLocation StartLoc = Tok.getLocation();
1422 bool IsValid = false;
1423 bool Value = false;
1424 // Read the '('.
Andy Gibbs58905d22012-11-17 19:15:38 +00001425 LexUnexpandedToken(Tok);
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001426 do {
Andy Gibbs58905d22012-11-17 19:15:38 +00001427 if (Tok.isNot(tok::l_paren)) {
1428 Diag(StartLoc, diag::err_warning_check_malformed);
1429 break;
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001430 }
Andy Gibbs58905d22012-11-17 19:15:38 +00001431
1432 LexUnexpandedToken(Tok);
1433 std::string WarningName;
1434 SourceLocation StrStartLoc = Tok.getLocation();
Andy Gibbsa8df57a2012-11-17 19:16:52 +00001435 if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'",
1436 /*MacroExpansion=*/false)) {
Andy Gibbs58905d22012-11-17 19:15:38 +00001437 // Eat tokens until ')'.
Andy Gibbsb5b30c42012-11-17 22:17:28 +00001438 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eod) &&
1439 Tok.isNot(tok::eof))
Andy Gibbs58905d22012-11-17 19:15:38 +00001440 LexUnexpandedToken(Tok);
1441 break;
1442 }
1443
1444 // Is the end a ')'?
1445 if (!(IsValid = Tok.is(tok::r_paren))) {
1446 Diag(StartLoc, diag::err_warning_check_malformed);
1447 break;
1448 }
1449
1450 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1451 WarningName[1] != 'W') {
1452 Diag(StrStartLoc, diag::warn_has_warning_invalid_option);
1453 break;
1454 }
1455
1456 // Finally, check if the warning flags maps to a diagnostic group.
1457 // We construct a SmallVector here to talk to getDiagnosticIDs().
1458 // Although we don't use the result, this isn't a hot path, and not
1459 // worth special casing.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001460 SmallVector<diag::kind, 10> Diags;
Andy Gibbs58905d22012-11-17 19:15:38 +00001461 Value = !getDiagnostics().getDiagnosticIDs()->
1462 getDiagnosticsInGroup(WarningName.substr(2), Diags);
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001463 } while (false);
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001464
1465 OS << (int)Value;
Andy Gibbsf591982b2012-11-17 19:14:53 +00001466 if (IsValid)
1467 Tok.setKind(tok::numeric_constant);
Douglas Gregorc83de302012-09-25 15:44:52 +00001468 } else if (II == Ident__building_module) {
1469 // The argument to this builtin should be an identifier. The
1470 // builtin evaluates to 1 when that identifier names the module we are
1471 // currently building.
1472 OS << (int)EvaluateBuildingModule(Tok, II, *this);
1473 Tok.setKind(tok::numeric_constant);
1474 } else if (II == Ident__MODULE__) {
1475 // The current module as an identifier.
1476 OS << getLangOpts().CurrentModule;
1477 IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule);
1478 Tok.setIdentifierInfo(ModuleII);
1479 Tok.setKind(ModuleII->getTokenID());
Richard Smithae385082014-03-15 00:06:08 +00001480 } else if (II == Ident__identifier) {
1481 SourceLocation Loc = Tok.getLocation();
1482
1483 // We're expecting '__identifier' '(' identifier ')'. Try to recover
1484 // if the parens are missing.
1485 LexNonComment(Tok);
1486 if (Tok.isNot(tok::l_paren)) {
1487 // No '(', use end of last token.
1488 Diag(getLocForEndOfToken(Loc), diag::err_pp_expected_after)
1489 << II << tok::l_paren;
1490 // If the next token isn't valid as our argument, we can't recover.
1491 if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
1492 Tok.setKind(tok::identifier);
1493 return;
1494 }
1495
1496 SourceLocation LParenLoc = Tok.getLocation();
1497 LexNonComment(Tok);
1498
1499 if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
1500 Tok.setKind(tok::identifier);
1501 else {
1502 Diag(Tok.getLocation(), diag::err_pp_identifier_arg_not_identifier)
1503 << Tok.getKind();
1504 // Don't walk past anything that's not a real token.
1505 if (Tok.is(tok::eof) || Tok.is(tok::eod) || Tok.isAnnotation())
1506 return;
1507 }
1508
1509 // Discard the ')', preserving 'Tok' as our result.
1510 Token RParen;
1511 LexNonComment(RParen);
1512 if (RParen.isNot(tok::r_paren)) {
1513 Diag(getLocForEndOfToken(Tok.getLocation()), diag::err_pp_expected_after)
1514 << Tok.getKind() << tok::r_paren;
1515 Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1516 }
1517 return;
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001518 } else {
1519 llvm_unreachable("Unknown identifier!");
1520 }
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +00001521 CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation());
Joao Matosc0d4c1b2012-08-31 21:34:27 +00001522}
1523
1524void Preprocessor::markMacroAsUsed(MacroInfo *MI) {
1525 // If the 'used' status changed, and the macro requires 'unused' warning,
1526 // remove its SourceLocation from the warn-for-unused-macro locations.
1527 if (MI->isWarnIfUnused() && !MI->isUsed())
1528 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
1529 MI->setIsUsed(true);
1530}