1 //===--- MacroExpansion.cpp - Top level Macro Expansion -------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the top level handling of macro expansion for the 11 // preprocessor. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "clang/Lex/Preprocessor.h" 16 #include "clang/Basic/Attributes.h" 17 #include "clang/Basic/FileManager.h" 18 #include "clang/Basic/SourceManager.h" 19 #include "clang/Basic/TargetInfo.h" 20 #include "clang/Lex/CodeCompletionHandler.h" 21 #include "clang/Lex/ExternalPreprocessorSource.h" 22 #include "clang/Lex/LexDiagnostic.h" 23 #include "clang/Lex/MacroArgs.h" 24 #include "clang/Lex/MacroInfo.h" 25 #include "llvm/ADT/STLExtras.h" 26 #include "llvm/ADT/SmallString.h" 27 #include "llvm/ADT/StringSwitch.h" 28 #include "llvm/Config/llvm-config.h" 29 #include "llvm/Support/ErrorHandling.h" 30 #include "llvm/Support/Format.h" 31 #include "llvm/Support/raw_ostream.h" 32 #include <cstdio> 33 #include <ctime> 34 using namespace clang; 35 36 MacroDirective * 37 Preprocessor::getMacroDirectiveHistory(const IdentifierInfo *II) const { 38 assert(II->hadMacroDefinition() && "Identifier has not been not a macro!"); 39 40 macro_iterator Pos = Macros.find(II); 41 assert(Pos != Macros.end() && "Identifier macro info is missing!"); 42 return Pos->second; 43 } 44 45 void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){ 46 assert(MD && "MacroDirective should be non-zero!"); 47 assert(!MD->getPrevious() && "Already attached to a MacroDirective history."); 48 49 MacroDirective *&StoredMD = Macros[II]; 50 MD->setPrevious(StoredMD); 51 StoredMD = MD; 52 // Setup the identifier as having associated macro history. 53 II->setHasMacroDefinition(true); 54 if (!MD->isDefined()) 55 II->setHasMacroDefinition(false); 56 bool isImportedMacro = isa<DefMacroDirective>(MD) && 57 cast<DefMacroDirective>(MD)->isImported(); 58 if (II->isFromAST() && !isImportedMacro) 59 II->setChangedSinceDeserialization(); 60 } 61 62 void Preprocessor::setLoadedMacroDirective(IdentifierInfo *II, 63 MacroDirective *MD) { 64 assert(II && MD); 65 MacroDirective *&StoredMD = Macros[II]; 66 assert(!StoredMD && 67 "the macro history was modified before initializing it from a pch"); 68 StoredMD = MD; 69 // Setup the identifier as having associated macro history. 70 II->setHasMacroDefinition(true); 71 if (!MD->isDefined()) 72 II->setHasMacroDefinition(false); 73 } 74 75 /// RegisterBuiltinMacro - Register the specified identifier in the identifier 76 /// table and mark it as a builtin macro to be expanded. 77 static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){ 78 // Get the identifier. 79 IdentifierInfo *Id = PP.getIdentifierInfo(Name); 80 81 // Mark it as being a macro that is builtin. 82 MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation()); 83 MI->setIsBuiltinMacro(); 84 PP.appendDefMacroDirective(Id, MI); 85 return Id; 86 } 87 88 89 /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the 90 /// identifier table. 91 void Preprocessor::RegisterBuiltinMacros() { 92 Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__"); 93 Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__"); 94 Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__"); 95 Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__"); 96 Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__"); 97 Ident_Pragma = RegisterBuiltinMacro(*this, "_Pragma"); 98 99 // C++ Standing Document Extensions. 100 Ident__has_cpp_attribute = RegisterBuiltinMacro(*this, "__has_cpp_attribute"); 101 102 // GCC Extensions. 103 Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__"); 104 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__"); 105 Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__"); 106 107 // Microsoft Extensions. 108 if (LangOpts.MicrosoftExt) { 109 Ident__identifier = RegisterBuiltinMacro(*this, "__identifier"); 110 Ident__pragma = RegisterBuiltinMacro(*this, "__pragma"); 111 } else { 112 Ident__identifier = nullptr; 113 Ident__pragma = nullptr; 114 } 115 116 // Clang Extensions. 117 Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature"); 118 Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension"); 119 Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin"); 120 Ident__has_attribute = RegisterBuiltinMacro(*this, "__has_attribute"); 121 Ident__has_declspec = RegisterBuiltinMacro(*this, "__has_declspec_attribute"); 122 Ident__has_include = RegisterBuiltinMacro(*this, "__has_include"); 123 Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next"); 124 Ident__has_warning = RegisterBuiltinMacro(*this, "__has_warning"); 125 Ident__is_identifier = RegisterBuiltinMacro(*this, "__is_identifier"); 126 127 // Modules. 128 if (LangOpts.Modules) { 129 Ident__building_module = RegisterBuiltinMacro(*this, "__building_module"); 130 131 // __MODULE__ 132 if (!LangOpts.CurrentModule.empty()) 133 Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__"); 134 else 135 Ident__MODULE__ = nullptr; 136 } else { 137 Ident__building_module = nullptr; 138 Ident__MODULE__ = nullptr; 139 } 140 } 141 142 /// isTrivialSingleTokenExpansion - Return true if MI, which has a single token 143 /// in its expansion, currently expands to that token literally. 144 static bool isTrivialSingleTokenExpansion(const MacroInfo *MI, 145 const IdentifierInfo *MacroIdent, 146 Preprocessor &PP) { 147 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo(); 148 149 // If the token isn't an identifier, it's always literally expanded. 150 if (!II) return true; 151 152 // If the information about this identifier is out of date, update it from 153 // the external source. 154 if (II->isOutOfDate()) 155 PP.getExternalSource()->updateOutOfDateIdentifier(*II); 156 157 // If the identifier is a macro, and if that macro is enabled, it may be 158 // expanded so it's not a trivial expansion. 159 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() && 160 // Fast expanding "#define X X" is ok, because X would be disabled. 161 II != MacroIdent) 162 return false; 163 164 // If this is an object-like macro invocation, it is safe to trivially expand 165 // it. 166 if (MI->isObjectLike()) return true; 167 168 // If this is a function-like macro invocation, it's safe to trivially expand 169 // as long as the identifier is not a macro argument. 170 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); 171 I != E; ++I) 172 if (*I == II) 173 return false; // Identifier is a macro argument. 174 175 return true; 176 } 177 178 179 /// isNextPPTokenLParen - Determine whether the next preprocessor token to be 180 /// lexed is a '('. If so, consume the token and return true, if not, this 181 /// method should have no observable side-effect on the lexed tokens. 182 bool Preprocessor::isNextPPTokenLParen() { 183 // Do some quick tests for rejection cases. 184 unsigned Val; 185 if (CurLexer) 186 Val = CurLexer->isNextPPTokenLParen(); 187 else if (CurPTHLexer) 188 Val = CurPTHLexer->isNextPPTokenLParen(); 189 else 190 Val = CurTokenLexer->isNextTokenLParen(); 191 192 if (Val == 2) { 193 // We have run off the end. If it's a source file we don't 194 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the 195 // macro stack. 196 if (CurPPLexer) 197 return false; 198 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { 199 IncludeStackInfo &Entry = IncludeMacroStack[i-1]; 200 if (Entry.TheLexer) 201 Val = Entry.TheLexer->isNextPPTokenLParen(); 202 else if (Entry.ThePTHLexer) 203 Val = Entry.ThePTHLexer->isNextPPTokenLParen(); 204 else 205 Val = Entry.TheTokenLexer->isNextTokenLParen(); 206 207 if (Val != 2) 208 break; 209 210 // Ran off the end of a source file? 211 if (Entry.ThePPLexer) 212 return false; 213 } 214 } 215 216 // Okay, if we know that the token is a '(', lex it and return. Otherwise we 217 // have found something that isn't a '(' or we found the end of the 218 // translation unit. In either case, return false. 219 return Val == 1; 220 } 221 222 /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be 223 /// expanded as a macro, handle it and return the next token as 'Identifier'. 224 bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier, 225 MacroDirective *MD) { 226 MacroDirective::DefInfo Def = MD->getDefinition(); 227 assert(Def.isValid()); 228 MacroInfo *MI = Def.getMacroInfo(); 229 230 // If this is a macro expansion in the "#if !defined(x)" line for the file, 231 // then the macro could expand to different things in other contexts, we need 232 // to disable the optimization in this case. 233 if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro(); 234 235 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially. 236 if (MI->isBuiltinMacro()) { 237 if (Callbacks) Callbacks->MacroExpands(Identifier, MD, 238 Identifier.getLocation(), 239 /*Args=*/nullptr); 240 ExpandBuiltinMacro(Identifier); 241 return true; 242 } 243 244 /// Args - If this is a function-like macro expansion, this contains, 245 /// for each macro argument, the list of tokens that were provided to the 246 /// invocation. 247 MacroArgs *Args = nullptr; 248 249 // Remember where the end of the expansion occurred. For an object-like 250 // macro, this is the identifier. For a function-like macro, this is the ')'. 251 SourceLocation ExpansionEnd = Identifier.getLocation(); 252 253 // If this is a function-like macro, read the arguments. 254 if (MI->isFunctionLike()) { 255 // Remember that we are now parsing the arguments to a macro invocation. 256 // Preprocessor directives used inside macro arguments are not portable, and 257 // this enables the warning. 258 InMacroArgs = true; 259 Args = ReadFunctionLikeMacroArgs(Identifier, MI, ExpansionEnd); 260 261 // Finished parsing args. 262 InMacroArgs = false; 263 264 // If there was an error parsing the arguments, bail out. 265 if (!Args) return true; 266 267 ++NumFnMacroExpanded; 268 } else { 269 ++NumMacroExpanded; 270 } 271 272 // Notice that this macro has been used. 273 markMacroAsUsed(MI); 274 275 // Remember where the token is expanded. 276 SourceLocation ExpandLoc = Identifier.getLocation(); 277 SourceRange ExpansionRange(ExpandLoc, ExpansionEnd); 278 279 if (Callbacks) { 280 if (InMacroArgs) { 281 // We can have macro expansion inside a conditional directive while 282 // reading the function macro arguments. To ensure, in that case, that 283 // MacroExpands callbacks still happen in source order, queue this 284 // callback to have it happen after the function macro callback. 285 DelayedMacroExpandsCallbacks.push_back( 286 MacroExpandsInfo(Identifier, MD, ExpansionRange)); 287 } else { 288 Callbacks->MacroExpands(Identifier, MD, ExpansionRange, Args); 289 if (!DelayedMacroExpandsCallbacks.empty()) { 290 for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) { 291 MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i]; 292 // FIXME: We lose macro args info with delayed callback. 293 Callbacks->MacroExpands(Info.Tok, Info.MD, Info.Range, 294 /*Args=*/nullptr); 295 } 296 DelayedMacroExpandsCallbacks.clear(); 297 } 298 } 299 } 300 301 // If the macro definition is ambiguous, complain. 302 if (Def.getDirective()->isAmbiguous()) { 303 Diag(Identifier, diag::warn_pp_ambiguous_macro) 304 << Identifier.getIdentifierInfo(); 305 Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen) 306 << Identifier.getIdentifierInfo(); 307 for (MacroDirective::DefInfo PrevDef = Def.getPreviousDefinition(); 308 PrevDef && !PrevDef.isUndefined(); 309 PrevDef = PrevDef.getPreviousDefinition()) { 310 Diag(PrevDef.getMacroInfo()->getDefinitionLoc(), 311 diag::note_pp_ambiguous_macro_other) 312 << Identifier.getIdentifierInfo(); 313 if (!PrevDef.getDirective()->isAmbiguous()) 314 break; 315 } 316 } 317 318 // If we started lexing a macro, enter the macro expansion body. 319 320 // If this macro expands to no tokens, don't bother to push it onto the 321 // expansion stack, only to take it right back off. 322 if (MI->getNumTokens() == 0) { 323 // No need for arg info. 324 if (Args) Args->destroy(*this); 325 326 // Propagate whitespace info as if we had pushed, then popped, 327 // a macro context. 328 Identifier.setFlag(Token::LeadingEmptyMacro); 329 PropagateLineStartLeadingSpaceInfo(Identifier); 330 ++NumFastMacroExpanded; 331 return false; 332 } else if (MI->getNumTokens() == 1 && 333 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(), 334 *this)) { 335 // Otherwise, if this macro expands into a single trivially-expanded 336 // token: expand it now. This handles common cases like 337 // "#define VAL 42". 338 339 // No need for arg info. 340 if (Args) Args->destroy(*this); 341 342 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro 343 // identifier to the expanded token. 344 bool isAtStartOfLine = Identifier.isAtStartOfLine(); 345 bool hasLeadingSpace = Identifier.hasLeadingSpace(); 346 347 // Replace the result token. 348 Identifier = MI->getReplacementToken(0); 349 350 // Restore the StartOfLine/LeadingSpace markers. 351 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine); 352 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace); 353 354 // Update the tokens location to include both its expansion and physical 355 // locations. 356 SourceLocation Loc = 357 SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc, 358 ExpansionEnd,Identifier.getLength()); 359 Identifier.setLocation(Loc); 360 361 // If this is a disabled macro or #define X X, we must mark the result as 362 // unexpandable. 363 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) { 364 if (MacroInfo *NewMI = getMacroInfo(NewII)) 365 if (!NewMI->isEnabled() || NewMI == MI) { 366 Identifier.setFlag(Token::DisableExpand); 367 // Don't warn for "#define X X" like "#define bool bool" from 368 // stdbool.h. 369 if (NewMI != MI || MI->isFunctionLike()) 370 Diag(Identifier, diag::pp_disabled_macro_expansion); 371 } 372 } 373 374 // Since this is not an identifier token, it can't be macro expanded, so 375 // we're done. 376 ++NumFastMacroExpanded; 377 return true; 378 } 379 380 // Start expanding the macro. 381 EnterMacro(Identifier, ExpansionEnd, MI, Args); 382 return false; 383 } 384 385 enum Bracket { 386 Brace, 387 Paren 388 }; 389 390 /// CheckMatchedBrackets - Returns true if the braces and parentheses in the 391 /// token vector are properly nested. 392 static bool CheckMatchedBrackets(const SmallVectorImpl<Token> &Tokens) { 393 SmallVector<Bracket, 8> Brackets; 394 for (SmallVectorImpl<Token>::const_iterator I = Tokens.begin(), 395 E = Tokens.end(); 396 I != E; ++I) { 397 if (I->is(tok::l_paren)) { 398 Brackets.push_back(Paren); 399 } else if (I->is(tok::r_paren)) { 400 if (Brackets.empty() || Brackets.back() == Brace) 401 return false; 402 Brackets.pop_back(); 403 } else if (I->is(tok::l_brace)) { 404 Brackets.push_back(Brace); 405 } else if (I->is(tok::r_brace)) { 406 if (Brackets.empty() || Brackets.back() == Paren) 407 return false; 408 Brackets.pop_back(); 409 } 410 } 411 if (!Brackets.empty()) 412 return false; 413 return true; 414 } 415 416 /// GenerateNewArgTokens - Returns true if OldTokens can be converted to a new 417 /// vector of tokens in NewTokens. The new number of arguments will be placed 418 /// in NumArgs and the ranges which need to surrounded in parentheses will be 419 /// in ParenHints. 420 /// Returns false if the token stream cannot be changed. If this is because 421 /// of an initializer list starting a macro argument, the range of those 422 /// initializer lists will be place in InitLists. 423 static bool GenerateNewArgTokens(Preprocessor &PP, 424 SmallVectorImpl<Token> &OldTokens, 425 SmallVectorImpl<Token> &NewTokens, 426 unsigned &NumArgs, 427 SmallVectorImpl<SourceRange> &ParenHints, 428 SmallVectorImpl<SourceRange> &InitLists) { 429 if (!CheckMatchedBrackets(OldTokens)) 430 return false; 431 432 // Once it is known that the brackets are matched, only a simple count of the 433 // braces is needed. 434 unsigned Braces = 0; 435 436 // First token of a new macro argument. 437 SmallVectorImpl<Token>::iterator ArgStartIterator = OldTokens.begin(); 438 439 // First closing brace in a new macro argument. Used to generate 440 // SourceRanges for InitLists. 441 SmallVectorImpl<Token>::iterator ClosingBrace = OldTokens.end(); 442 NumArgs = 0; 443 Token TempToken; 444 // Set to true when a macro separator token is found inside a braced list. 445 // If true, the fixed argument spans multiple old arguments and ParenHints 446 // will be updated. 447 bool FoundSeparatorToken = false; 448 for (SmallVectorImpl<Token>::iterator I = OldTokens.begin(), 449 E = OldTokens.end(); 450 I != E; ++I) { 451 if (I->is(tok::l_brace)) { 452 ++Braces; 453 } else if (I->is(tok::r_brace)) { 454 --Braces; 455 if (Braces == 0 && ClosingBrace == E && FoundSeparatorToken) 456 ClosingBrace = I; 457 } else if (I->is(tok::eof)) { 458 // EOF token is used to separate macro arguments 459 if (Braces != 0) { 460 // Assume comma separator is actually braced list separator and change 461 // it back to a comma. 462 FoundSeparatorToken = true; 463 I->setKind(tok::comma); 464 I->setLength(1); 465 } else { // Braces == 0 466 // Separator token still separates arguments. 467 ++NumArgs; 468 469 // If the argument starts with a brace, it can't be fixed with 470 // parentheses. A different diagnostic will be given. 471 if (FoundSeparatorToken && ArgStartIterator->is(tok::l_brace)) { 472 InitLists.push_back( 473 SourceRange(ArgStartIterator->getLocation(), 474 PP.getLocForEndOfToken(ClosingBrace->getLocation()))); 475 ClosingBrace = E; 476 } 477 478 // Add left paren 479 if (FoundSeparatorToken) { 480 TempToken.startToken(); 481 TempToken.setKind(tok::l_paren); 482 TempToken.setLocation(ArgStartIterator->getLocation()); 483 TempToken.setLength(0); 484 NewTokens.push_back(TempToken); 485 } 486 487 // Copy over argument tokens 488 NewTokens.insert(NewTokens.end(), ArgStartIterator, I); 489 490 // Add right paren and store the paren locations in ParenHints 491 if (FoundSeparatorToken) { 492 SourceLocation Loc = PP.getLocForEndOfToken((I - 1)->getLocation()); 493 TempToken.startToken(); 494 TempToken.setKind(tok::r_paren); 495 TempToken.setLocation(Loc); 496 TempToken.setLength(0); 497 NewTokens.push_back(TempToken); 498 ParenHints.push_back(SourceRange(ArgStartIterator->getLocation(), 499 Loc)); 500 } 501 502 // Copy separator token 503 NewTokens.push_back(*I); 504 505 // Reset values 506 ArgStartIterator = I + 1; 507 FoundSeparatorToken = false; 508 } 509 } 510 } 511 512 return !ParenHints.empty() && InitLists.empty(); 513 } 514 515 /// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next 516 /// token is the '(' of the macro, this method is invoked to read all of the 517 /// actual arguments specified for the macro invocation. This returns null on 518 /// error. 519 MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName, 520 MacroInfo *MI, 521 SourceLocation &MacroEnd) { 522 // The number of fixed arguments to parse. 523 unsigned NumFixedArgsLeft = MI->getNumArgs(); 524 bool isVariadic = MI->isVariadic(); 525 526 // Outer loop, while there are more arguments, keep reading them. 527 Token Tok; 528 529 // Read arguments as unexpanded tokens. This avoids issues, e.g., where 530 // an argument value in a macro could expand to ',' or '(' or ')'. 531 LexUnexpandedToken(Tok); 532 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?"); 533 534 // ArgTokens - Build up a list of tokens that make up each argument. Each 535 // argument is separated by an EOF token. Use a SmallVector so we can avoid 536 // heap allocations in the common case. 537 SmallVector<Token, 64> ArgTokens; 538 bool ContainsCodeCompletionTok = false; 539 540 SourceLocation TooManyArgsLoc; 541 542 unsigned NumActuals = 0; 543 while (Tok.isNot(tok::r_paren)) { 544 if (ContainsCodeCompletionTok && (Tok.is(tok::eof) || Tok.is(tok::eod))) 545 break; 546 547 assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) && 548 "only expect argument separators here"); 549 550 unsigned ArgTokenStart = ArgTokens.size(); 551 SourceLocation ArgStartLoc = Tok.getLocation(); 552 553 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note 554 // that we already consumed the first one. 555 unsigned NumParens = 0; 556 557 while (1) { 558 // Read arguments as unexpanded tokens. This avoids issues, e.g., where 559 // an argument value in a macro could expand to ',' or '(' or ')'. 560 LexUnexpandedToken(Tok); 561 562 if (Tok.is(tok::eof) || Tok.is(tok::eod)) { // "#if f(<eof>" & "#if f(\n" 563 if (!ContainsCodeCompletionTok) { 564 Diag(MacroName, diag::err_unterm_macro_invoc); 565 Diag(MI->getDefinitionLoc(), diag::note_macro_here) 566 << MacroName.getIdentifierInfo(); 567 // Do not lose the EOF/EOD. Return it to the client. 568 MacroName = Tok; 569 return nullptr; 570 } else { 571 // Do not lose the EOF/EOD. 572 Token *Toks = new Token[1]; 573 Toks[0] = Tok; 574 EnterTokenStream(Toks, 1, true, true); 575 break; 576 } 577 } else if (Tok.is(tok::r_paren)) { 578 // If we found the ) token, the macro arg list is done. 579 if (NumParens-- == 0) { 580 MacroEnd = Tok.getLocation(); 581 break; 582 } 583 } else if (Tok.is(tok::l_paren)) { 584 ++NumParens; 585 } else if (Tok.is(tok::comma) && NumParens == 0 && 586 !(Tok.getFlags() & Token::IgnoredComma)) { 587 // In Microsoft-compatibility mode, single commas from nested macro 588 // expansions should not be considered as argument separators. We test 589 // for this with the IgnoredComma token flag above. 590 591 // Comma ends this argument if there are more fixed arguments expected. 592 // However, if this is a variadic macro, and this is part of the 593 // variadic part, then the comma is just an argument token. 594 if (!isVariadic) break; 595 if (NumFixedArgsLeft > 1) 596 break; 597 } else if (Tok.is(tok::comment) && !KeepMacroComments) { 598 // If this is a comment token in the argument list and we're just in 599 // -C mode (not -CC mode), discard the comment. 600 continue; 601 } else if (Tok.getIdentifierInfo() != nullptr) { 602 // Reading macro arguments can cause macros that we are currently 603 // expanding from to be popped off the expansion stack. Doing so causes 604 // them to be reenabled for expansion. Here we record whether any 605 // identifiers we lex as macro arguments correspond to disabled macros. 606 // If so, we mark the token as noexpand. This is a subtle aspect of 607 // C99 6.10.3.4p2. 608 if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo())) 609 if (!MI->isEnabled()) 610 Tok.setFlag(Token::DisableExpand); 611 } else if (Tok.is(tok::code_completion)) { 612 ContainsCodeCompletionTok = true; 613 if (CodeComplete) 614 CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(), 615 MI, NumActuals); 616 // Don't mark that we reached the code-completion point because the 617 // parser is going to handle the token and there will be another 618 // code-completion callback. 619 } 620 621 ArgTokens.push_back(Tok); 622 } 623 624 // If this was an empty argument list foo(), don't add this as an empty 625 // argument. 626 if (ArgTokens.empty() && Tok.getKind() == tok::r_paren) 627 break; 628 629 // If this is not a variadic macro, and too many args were specified, emit 630 // an error. 631 if (!isVariadic && NumFixedArgsLeft == 0 && TooManyArgsLoc.isInvalid()) { 632 if (ArgTokens.size() != ArgTokenStart) 633 TooManyArgsLoc = ArgTokens[ArgTokenStart].getLocation(); 634 else 635 TooManyArgsLoc = ArgStartLoc; 636 } 637 638 // Empty arguments are standard in C99 and C++0x, and are supported as an 639 // extension in other modes. 640 if (ArgTokens.size() == ArgTokenStart && !LangOpts.C99) 641 Diag(Tok, LangOpts.CPlusPlus11 ? 642 diag::warn_cxx98_compat_empty_fnmacro_arg : 643 diag::ext_empty_fnmacro_arg); 644 645 // Add a marker EOF token to the end of the token list for this argument. 646 Token EOFTok; 647 EOFTok.startToken(); 648 EOFTok.setKind(tok::eof); 649 EOFTok.setLocation(Tok.getLocation()); 650 EOFTok.setLength(0); 651 ArgTokens.push_back(EOFTok); 652 ++NumActuals; 653 if (!ContainsCodeCompletionTok && NumFixedArgsLeft != 0) 654 --NumFixedArgsLeft; 655 } 656 657 // Okay, we either found the r_paren. Check to see if we parsed too few 658 // arguments. 659 unsigned MinArgsExpected = MI->getNumArgs(); 660 661 // If this is not a variadic macro, and too many args were specified, emit 662 // an error. 663 if (!isVariadic && NumActuals > MinArgsExpected && 664 !ContainsCodeCompletionTok) { 665 // Emit the diagnostic at the macro name in case there is a missing ). 666 // Emitting it at the , could be far away from the macro name. 667 Diag(TooManyArgsLoc, diag::err_too_many_args_in_macro_invoc); 668 Diag(MI->getDefinitionLoc(), diag::note_macro_here) 669 << MacroName.getIdentifierInfo(); 670 671 // Commas from braced initializer lists will be treated as argument 672 // separators inside macros. Attempt to correct for this with parentheses. 673 // TODO: See if this can be generalized to angle brackets for templates 674 // inside macro arguments. 675 676 SmallVector<Token, 4> FixedArgTokens; 677 unsigned FixedNumArgs = 0; 678 SmallVector<SourceRange, 4> ParenHints, InitLists; 679 if (!GenerateNewArgTokens(*this, ArgTokens, FixedArgTokens, FixedNumArgs, 680 ParenHints, InitLists)) { 681 if (!InitLists.empty()) { 682 DiagnosticBuilder DB = 683 Diag(MacroName, 684 diag::note_init_list_at_beginning_of_macro_argument); 685 for (const SourceRange &Range : InitLists) 686 DB << Range; 687 } 688 return nullptr; 689 } 690 if (FixedNumArgs != MinArgsExpected) 691 return nullptr; 692 693 DiagnosticBuilder DB = Diag(MacroName, diag::note_suggest_parens_for_macro); 694 for (const SourceRange &ParenLocation : ParenHints) { 695 DB << FixItHint::CreateInsertion(ParenLocation.getBegin(), "("); 696 DB << FixItHint::CreateInsertion(ParenLocation.getEnd(), ")"); 697 } 698 ArgTokens.swap(FixedArgTokens); 699 NumActuals = FixedNumArgs; 700 } 701 702 // See MacroArgs instance var for description of this. 703 bool isVarargsElided = false; 704 705 if (ContainsCodeCompletionTok) { 706 // Recover from not-fully-formed macro invocation during code-completion. 707 Token EOFTok; 708 EOFTok.startToken(); 709 EOFTok.setKind(tok::eof); 710 EOFTok.setLocation(Tok.getLocation()); 711 EOFTok.setLength(0); 712 for (; NumActuals < MinArgsExpected; ++NumActuals) 713 ArgTokens.push_back(EOFTok); 714 } 715 716 if (NumActuals < MinArgsExpected) { 717 // There are several cases where too few arguments is ok, handle them now. 718 if (NumActuals == 0 && MinArgsExpected == 1) { 719 // #define A(X) or #define A(...) ---> A() 720 721 // If there is exactly one argument, and that argument is missing, 722 // then we have an empty "()" argument empty list. This is fine, even if 723 // the macro expects one argument (the argument is just empty). 724 isVarargsElided = MI->isVariadic(); 725 } else if (MI->isVariadic() && 726 (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X) 727 (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A() 728 // Varargs where the named vararg parameter is missing: OK as extension. 729 // #define A(x, ...) 730 // A("blah") 731 // 732 // If the macro contains the comma pasting extension, the diagnostic 733 // is suppressed; we know we'll get another diagnostic later. 734 if (!MI->hasCommaPasting()) { 735 Diag(Tok, diag::ext_missing_varargs_arg); 736 Diag(MI->getDefinitionLoc(), diag::note_macro_here) 737 << MacroName.getIdentifierInfo(); 738 } 739 740 // Remember this occurred, allowing us to elide the comma when used for 741 // cases like: 742 // #define A(x, foo...) blah(a, ## foo) 743 // #define B(x, ...) blah(a, ## __VA_ARGS__) 744 // #define C(...) blah(a, ## __VA_ARGS__) 745 // A(x) B(x) C() 746 isVarargsElided = true; 747 } else if (!ContainsCodeCompletionTok) { 748 // Otherwise, emit the error. 749 Diag(Tok, diag::err_too_few_args_in_macro_invoc); 750 Diag(MI->getDefinitionLoc(), diag::note_macro_here) 751 << MacroName.getIdentifierInfo(); 752 return nullptr; 753 } 754 755 // Add a marker EOF token to the end of the token list for this argument. 756 SourceLocation EndLoc = Tok.getLocation(); 757 Tok.startToken(); 758 Tok.setKind(tok::eof); 759 Tok.setLocation(EndLoc); 760 Tok.setLength(0); 761 ArgTokens.push_back(Tok); 762 763 // If we expect two arguments, add both as empty. 764 if (NumActuals == 0 && MinArgsExpected == 2) 765 ArgTokens.push_back(Tok); 766 767 } else if (NumActuals > MinArgsExpected && !MI->isVariadic() && 768 !ContainsCodeCompletionTok) { 769 // Emit the diagnostic at the macro name in case there is a missing ). 770 // Emitting it at the , could be far away from the macro name. 771 Diag(MacroName, diag::err_too_many_args_in_macro_invoc); 772 Diag(MI->getDefinitionLoc(), diag::note_macro_here) 773 << MacroName.getIdentifierInfo(); 774 return nullptr; 775 } 776 777 return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this); 778 } 779 780 /// \brief Keeps macro expanded tokens for TokenLexers. 781 // 782 /// Works like a stack; a TokenLexer adds the macro expanded tokens that is 783 /// going to lex in the cache and when it finishes the tokens are removed 784 /// from the end of the cache. 785 Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer, 786 ArrayRef<Token> tokens) { 787 assert(tokLexer); 788 if (tokens.empty()) 789 return nullptr; 790 791 size_t newIndex = MacroExpandedTokens.size(); 792 bool cacheNeedsToGrow = tokens.size() > 793 MacroExpandedTokens.capacity()-MacroExpandedTokens.size(); 794 MacroExpandedTokens.append(tokens.begin(), tokens.end()); 795 796 if (cacheNeedsToGrow) { 797 // Go through all the TokenLexers whose 'Tokens' pointer points in the 798 // buffer and update the pointers to the (potential) new buffer array. 799 for (unsigned i = 0, e = MacroExpandingLexersStack.size(); i != e; ++i) { 800 TokenLexer *prevLexer; 801 size_t tokIndex; 802 std::tie(prevLexer, tokIndex) = MacroExpandingLexersStack[i]; 803 prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex; 804 } 805 } 806 807 MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex)); 808 return MacroExpandedTokens.data() + newIndex; 809 } 810 811 void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() { 812 assert(!MacroExpandingLexersStack.empty()); 813 size_t tokIndex = MacroExpandingLexersStack.back().second; 814 assert(tokIndex < MacroExpandedTokens.size()); 815 // Pop the cached macro expanded tokens from the end. 816 MacroExpandedTokens.resize(tokIndex); 817 MacroExpandingLexersStack.pop_back(); 818 } 819 820 /// ComputeDATE_TIME - Compute the current time, enter it into the specified 821 /// scratch buffer, then return DATELoc/TIMELoc locations with the position of 822 /// the identifier tokens inserted. 823 static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc, 824 Preprocessor &PP) { 825 time_t TT = time(nullptr); 826 struct tm *TM = localtime(&TT); 827 828 static const char * const Months[] = { 829 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" 830 }; 831 832 { 833 SmallString<32> TmpBuffer; 834 llvm::raw_svector_ostream TmpStream(TmpBuffer); 835 TmpStream << llvm::format("\"%s %2d %4d\"", Months[TM->tm_mon], 836 TM->tm_mday, TM->tm_year + 1900); 837 Token TmpTok; 838 TmpTok.startToken(); 839 PP.CreateString(TmpStream.str(), TmpTok); 840 DATELoc = TmpTok.getLocation(); 841 } 842 843 { 844 SmallString<32> TmpBuffer; 845 llvm::raw_svector_ostream TmpStream(TmpBuffer); 846 TmpStream << llvm::format("\"%02d:%02d:%02d\"", 847 TM->tm_hour, TM->tm_min, TM->tm_sec); 848 Token TmpTok; 849 TmpTok.startToken(); 850 PP.CreateString(TmpStream.str(), TmpTok); 851 TIMELoc = TmpTok.getLocation(); 852 } 853 } 854 855 856 /// HasFeature - Return true if we recognize and implement the feature 857 /// specified by the identifier as a standard language feature. 858 static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) { 859 const LangOptions &LangOpts = PP.getLangOpts(); 860 StringRef Feature = II->getName(); 861 862 // Normalize the feature name, __foo__ becomes foo. 863 if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4) 864 Feature = Feature.substr(2, Feature.size() - 4); 865 866 return llvm::StringSwitch<bool>(Feature) 867 .Case("address_sanitizer", LangOpts.Sanitize.has(SanitizerKind::Address)) 868 .Case("attribute_analyzer_noreturn", true) 869 .Case("attribute_availability", true) 870 .Case("attribute_availability_with_message", true) 871 .Case("attribute_cf_returns_not_retained", true) 872 .Case("attribute_cf_returns_retained", true) 873 .Case("attribute_deprecated_with_message", true) 874 .Case("attribute_ext_vector_type", true) 875 .Case("attribute_ns_returns_not_retained", true) 876 .Case("attribute_ns_returns_retained", true) 877 .Case("attribute_ns_consumes_self", true) 878 .Case("attribute_ns_consumed", true) 879 .Case("attribute_cf_consumed", true) 880 .Case("attribute_objc_ivar_unused", true) 881 .Case("attribute_objc_method_family", true) 882 .Case("attribute_overloadable", true) 883 .Case("attribute_unavailable_with_message", true) 884 .Case("attribute_unused_on_fields", true) 885 .Case("blocks", LangOpts.Blocks) 886 .Case("c_thread_safety_attributes", true) 887 .Case("cxx_exceptions", LangOpts.CXXExceptions) 888 .Case("cxx_rtti", LangOpts.RTTI) 889 .Case("enumerator_attributes", true) 890 .Case("memory_sanitizer", LangOpts.Sanitize.has(SanitizerKind::Memory)) 891 .Case("thread_sanitizer", LangOpts.Sanitize.has(SanitizerKind::Thread)) 892 .Case("dataflow_sanitizer", LangOpts.Sanitize.has(SanitizerKind::DataFlow)) 893 // Objective-C features 894 .Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE? 895 .Case("objc_arc", LangOpts.ObjCAutoRefCount) 896 .Case("objc_arc_weak", LangOpts.ObjCARCWeak) 897 .Case("objc_default_synthesize_properties", LangOpts.ObjC2) 898 .Case("objc_fixed_enum", LangOpts.ObjC2) 899 .Case("objc_instancetype", LangOpts.ObjC2) 900 .Case("objc_modules", LangOpts.ObjC2 && LangOpts.Modules) 901 .Case("objc_nonfragile_abi", LangOpts.ObjCRuntime.isNonFragile()) 902 .Case("objc_property_explicit_atomic", 903 true) // Does clang support explicit "atomic" keyword? 904 .Case("objc_protocol_qualifier_mangling", true) 905 .Case("objc_weak_class", LangOpts.ObjCRuntime.hasWeakClassImport()) 906 .Case("ownership_holds", true) 907 .Case("ownership_returns", true) 908 .Case("ownership_takes", true) 909 .Case("objc_bool", true) 910 .Case("objc_subscripting", LangOpts.ObjCRuntime.isNonFragile()) 911 .Case("objc_array_literals", LangOpts.ObjC2) 912 .Case("objc_dictionary_literals", LangOpts.ObjC2) 913 .Case("objc_boxed_expressions", LangOpts.ObjC2) 914 .Case("arc_cf_code_audited", true) 915 .Case("objc_bridge_id", LangOpts.ObjC2) 916 // C11 features 917 .Case("c_alignas", LangOpts.C11) 918 .Case("c_alignof", LangOpts.C11) 919 .Case("c_atomic", LangOpts.C11) 920 .Case("c_generic_selections", LangOpts.C11) 921 .Case("c_static_assert", LangOpts.C11) 922 .Case("c_thread_local", 923 LangOpts.C11 && PP.getTargetInfo().isTLSSupported()) 924 // C++11 features 925 .Case("cxx_access_control_sfinae", LangOpts.CPlusPlus11) 926 .Case("cxx_alias_templates", LangOpts.CPlusPlus11) 927 .Case("cxx_alignas", LangOpts.CPlusPlus11) 928 .Case("cxx_alignof", LangOpts.CPlusPlus11) 929 .Case("cxx_atomic", LangOpts.CPlusPlus11) 930 .Case("cxx_attributes", LangOpts.CPlusPlus11) 931 .Case("cxx_auto_type", LangOpts.CPlusPlus11) 932 .Case("cxx_constexpr", LangOpts.CPlusPlus11) 933 .Case("cxx_decltype", LangOpts.CPlusPlus11) 934 .Case("cxx_decltype_incomplete_return_types", LangOpts.CPlusPlus11) 935 .Case("cxx_default_function_template_args", LangOpts.CPlusPlus11) 936 .Case("cxx_defaulted_functions", LangOpts.CPlusPlus11) 937 .Case("cxx_delegating_constructors", LangOpts.CPlusPlus11) 938 .Case("cxx_deleted_functions", LangOpts.CPlusPlus11) 939 .Case("cxx_explicit_conversions", LangOpts.CPlusPlus11) 940 .Case("cxx_generalized_initializers", LangOpts.CPlusPlus11) 941 .Case("cxx_implicit_moves", LangOpts.CPlusPlus11) 942 .Case("cxx_inheriting_constructors", LangOpts.CPlusPlus11) 943 .Case("cxx_inline_namespaces", LangOpts.CPlusPlus11) 944 .Case("cxx_lambdas", LangOpts.CPlusPlus11) 945 .Case("cxx_local_type_template_args", LangOpts.CPlusPlus11) 946 .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus11) 947 .Case("cxx_noexcept", LangOpts.CPlusPlus11) 948 .Case("cxx_nullptr", LangOpts.CPlusPlus11) 949 .Case("cxx_override_control", LangOpts.CPlusPlus11) 950 .Case("cxx_range_for", LangOpts.CPlusPlus11) 951 .Case("cxx_raw_string_literals", LangOpts.CPlusPlus11) 952 .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus11) 953 .Case("cxx_rvalue_references", LangOpts.CPlusPlus11) 954 .Case("cxx_strong_enums", LangOpts.CPlusPlus11) 955 .Case("cxx_static_assert", LangOpts.CPlusPlus11) 956 .Case("cxx_thread_local", 957 LangOpts.CPlusPlus11 && PP.getTargetInfo().isTLSSupported()) 958 .Case("cxx_trailing_return", LangOpts.CPlusPlus11) 959 .Case("cxx_unicode_literals", LangOpts.CPlusPlus11) 960 .Case("cxx_unrestricted_unions", LangOpts.CPlusPlus11) 961 .Case("cxx_user_literals", LangOpts.CPlusPlus11) 962 .Case("cxx_variadic_templates", LangOpts.CPlusPlus11) 963 // C++1y features 964 .Case("cxx_aggregate_nsdmi", LangOpts.CPlusPlus14) 965 .Case("cxx_binary_literals", LangOpts.CPlusPlus14) 966 .Case("cxx_contextual_conversions", LangOpts.CPlusPlus14) 967 .Case("cxx_decltype_auto", LangOpts.CPlusPlus14) 968 .Case("cxx_generic_lambdas", LangOpts.CPlusPlus14) 969 .Case("cxx_init_captures", LangOpts.CPlusPlus14) 970 .Case("cxx_relaxed_constexpr", LangOpts.CPlusPlus14) 971 .Case("cxx_return_type_deduction", LangOpts.CPlusPlus14) 972 .Case("cxx_variable_templates", LangOpts.CPlusPlus14) 973 // C++ TSes 974 //.Case("cxx_runtime_arrays", LangOpts.CPlusPlusTSArrays) 975 //.Case("cxx_concepts", LangOpts.CPlusPlusTSConcepts) 976 // FIXME: Should this be __has_feature or __has_extension? 977 //.Case("raw_invocation_type", LangOpts.CPlusPlus) 978 // Type traits 979 .Case("has_nothrow_assign", LangOpts.CPlusPlus) 980 .Case("has_nothrow_copy", LangOpts.CPlusPlus) 981 .Case("has_nothrow_constructor", LangOpts.CPlusPlus) 982 .Case("has_trivial_assign", LangOpts.CPlusPlus) 983 .Case("has_trivial_copy", LangOpts.CPlusPlus) 984 .Case("has_trivial_constructor", LangOpts.CPlusPlus) 985 .Case("has_trivial_destructor", LangOpts.CPlusPlus) 986 .Case("has_virtual_destructor", LangOpts.CPlusPlus) 987 .Case("is_abstract", LangOpts.CPlusPlus) 988 .Case("is_base_of", LangOpts.CPlusPlus) 989 .Case("is_class", LangOpts.CPlusPlus) 990 .Case("is_constructible", LangOpts.CPlusPlus) 991 .Case("is_convertible_to", LangOpts.CPlusPlus) 992 .Case("is_empty", LangOpts.CPlusPlus) 993 .Case("is_enum", LangOpts.CPlusPlus) 994 .Case("is_final", LangOpts.CPlusPlus) 995 .Case("is_literal", LangOpts.CPlusPlus) 996 .Case("is_standard_layout", LangOpts.CPlusPlus) 997 .Case("is_pod", LangOpts.CPlusPlus) 998 .Case("is_polymorphic", LangOpts.CPlusPlus) 999 .Case("is_sealed", LangOpts.MicrosoftExt) 1000 .Case("is_trivial", LangOpts.CPlusPlus) 1001 .Case("is_trivially_assignable", LangOpts.CPlusPlus) 1002 .Case("is_trivially_constructible", LangOpts.CPlusPlus) 1003 .Case("is_trivially_copyable", LangOpts.CPlusPlus) 1004 .Case("is_union", LangOpts.CPlusPlus) 1005 .Case("modules", LangOpts.Modules) 1006 .Case("tls", PP.getTargetInfo().isTLSSupported()) 1007 .Case("underlying_type", LangOpts.CPlusPlus) 1008 .Default(false); 1009 } 1010 1011 /// HasExtension - Return true if we recognize and implement the feature 1012 /// specified by the identifier, either as an extension or a standard language 1013 /// feature. 1014 static bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) { 1015 if (HasFeature(PP, II)) 1016 return true; 1017 1018 // If the use of an extension results in an error diagnostic, extensions are 1019 // effectively unavailable, so just return false here. 1020 if (PP.getDiagnostics().getExtensionHandlingBehavior() >= 1021 diag::Severity::Error) 1022 return false; 1023 1024 const LangOptions &LangOpts = PP.getLangOpts(); 1025 StringRef Extension = II->getName(); 1026 1027 // Normalize the extension name, __foo__ becomes foo. 1028 if (Extension.startswith("__") && Extension.endswith("__") && 1029 Extension.size() >= 4) 1030 Extension = Extension.substr(2, Extension.size() - 4); 1031 1032 // Because we inherit the feature list from HasFeature, this string switch 1033 // must be less restrictive than HasFeature's. 1034 return llvm::StringSwitch<bool>(Extension) 1035 // C11 features supported by other languages as extensions. 1036 .Case("c_alignas", true) 1037 .Case("c_alignof", true) 1038 .Case("c_atomic", true) 1039 .Case("c_generic_selections", true) 1040 .Case("c_static_assert", true) 1041 .Case("c_thread_local", PP.getTargetInfo().isTLSSupported()) 1042 // C++11 features supported by other languages as extensions. 1043 .Case("cxx_atomic", LangOpts.CPlusPlus) 1044 .Case("cxx_deleted_functions", LangOpts.CPlusPlus) 1045 .Case("cxx_explicit_conversions", LangOpts.CPlusPlus) 1046 .Case("cxx_inline_namespaces", LangOpts.CPlusPlus) 1047 .Case("cxx_local_type_template_args", LangOpts.CPlusPlus) 1048 .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus) 1049 .Case("cxx_override_control", LangOpts.CPlusPlus) 1050 .Case("cxx_range_for", LangOpts.CPlusPlus) 1051 .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus) 1052 .Case("cxx_rvalue_references", LangOpts.CPlusPlus) 1053 // C++1y features supported by other languages as extensions. 1054 .Case("cxx_binary_literals", true) 1055 .Case("cxx_init_captures", LangOpts.CPlusPlus11) 1056 .Case("cxx_variable_templates", LangOpts.CPlusPlus) 1057 .Default(false); 1058 } 1059 1060 /// EvaluateHasIncludeCommon - Process a '__has_include("path")' 1061 /// or '__has_include_next("path")' expression. 1062 /// Returns true if successful. 1063 static bool EvaluateHasIncludeCommon(Token &Tok, 1064 IdentifierInfo *II, Preprocessor &PP, 1065 const DirectoryLookup *LookupFrom, 1066 const FileEntry *LookupFromFile) { 1067 // Save the location of the current token. If a '(' is later found, use 1068 // that location. If not, use the end of this location instead. 1069 SourceLocation LParenLoc = Tok.getLocation(); 1070 1071 // These expressions are only allowed within a preprocessor directive. 1072 if (!PP.isParsingIfOrElifDirective()) { 1073 PP.Diag(LParenLoc, diag::err_pp_directive_required) << II->getName(); 1074 return false; 1075 } 1076 1077 // Get '('. 1078 PP.LexNonComment(Tok); 1079 1080 // Ensure we have a '('. 1081 if (Tok.isNot(tok::l_paren)) { 1082 // No '(', use end of last token. 1083 LParenLoc = PP.getLocForEndOfToken(LParenLoc); 1084 PP.Diag(LParenLoc, diag::err_pp_expected_after) << II << tok::l_paren; 1085 // If the next token looks like a filename or the start of one, 1086 // assume it is and process it as such. 1087 if (!Tok.is(tok::angle_string_literal) && !Tok.is(tok::string_literal) && 1088 !Tok.is(tok::less)) 1089 return false; 1090 } else { 1091 // Save '(' location for possible missing ')' message. 1092 LParenLoc = Tok.getLocation(); 1093 1094 if (PP.getCurrentLexer()) { 1095 // Get the file name. 1096 PP.getCurrentLexer()->LexIncludeFilename(Tok); 1097 } else { 1098 // We're in a macro, so we can't use LexIncludeFilename; just 1099 // grab the next token. 1100 PP.Lex(Tok); 1101 } 1102 } 1103 1104 // Reserve a buffer to get the spelling. 1105 SmallString<128> FilenameBuffer; 1106 StringRef Filename; 1107 SourceLocation EndLoc; 1108 1109 switch (Tok.getKind()) { 1110 case tok::eod: 1111 // If the token kind is EOD, the error has already been diagnosed. 1112 return false; 1113 1114 case tok::angle_string_literal: 1115 case tok::string_literal: { 1116 bool Invalid = false; 1117 Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid); 1118 if (Invalid) 1119 return false; 1120 break; 1121 } 1122 1123 case tok::less: 1124 // This could be a <foo/bar.h> file coming from a macro expansion. In this 1125 // case, glue the tokens together into FilenameBuffer and interpret those. 1126 FilenameBuffer.push_back('<'); 1127 if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) { 1128 // Let the caller know a <eod> was found by changing the Token kind. 1129 Tok.setKind(tok::eod); 1130 return false; // Found <eod> but no ">"? Diagnostic already emitted. 1131 } 1132 Filename = FilenameBuffer.str(); 1133 break; 1134 default: 1135 PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename); 1136 return false; 1137 } 1138 1139 SourceLocation FilenameLoc = Tok.getLocation(); 1140 1141 // Get ')'. 1142 PP.LexNonComment(Tok); 1143 1144 // Ensure we have a trailing ). 1145 if (Tok.isNot(tok::r_paren)) { 1146 PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_expected_after) 1147 << II << tok::r_paren; 1148 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren; 1149 return false; 1150 } 1151 1152 bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename); 1153 // If GetIncludeFilenameSpelling set the start ptr to null, there was an 1154 // error. 1155 if (Filename.empty()) 1156 return false; 1157 1158 // Search include directories. 1159 const DirectoryLookup *CurDir; 1160 const FileEntry *File = 1161 PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, LookupFromFile, 1162 CurDir, nullptr, nullptr, nullptr); 1163 1164 // Get the result value. A result of true means the file exists. 1165 return File != nullptr; 1166 } 1167 1168 /// EvaluateHasInclude - Process a '__has_include("path")' expression. 1169 /// Returns true if successful. 1170 static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II, 1171 Preprocessor &PP) { 1172 return EvaluateHasIncludeCommon(Tok, II, PP, nullptr, nullptr); 1173 } 1174 1175 /// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression. 1176 /// Returns true if successful. 1177 static bool EvaluateHasIncludeNext(Token &Tok, 1178 IdentifierInfo *II, Preprocessor &PP) { 1179 // __has_include_next is like __has_include, except that we start 1180 // searching after the current found directory. If we can't do this, 1181 // issue a diagnostic. 1182 // FIXME: Factor out duplication wiht 1183 // Preprocessor::HandleIncludeNextDirective. 1184 const DirectoryLookup *Lookup = PP.GetCurDirLookup(); 1185 const FileEntry *LookupFromFile = nullptr; 1186 if (PP.isInPrimaryFile()) { 1187 Lookup = nullptr; 1188 PP.Diag(Tok, diag::pp_include_next_in_primary); 1189 } else if (PP.getCurrentSubmodule()) { 1190 // Start looking up in the directory *after* the one in which the current 1191 // file would be found, if any. 1192 assert(PP.getCurrentLexer() && "#include_next directive in macro?"); 1193 LookupFromFile = PP.getCurrentLexer()->getFileEntry(); 1194 Lookup = nullptr; 1195 } else if (!Lookup) { 1196 PP.Diag(Tok, diag::pp_include_next_absolute_path); 1197 } else { 1198 // Start looking up in the next directory. 1199 ++Lookup; 1200 } 1201 1202 return EvaluateHasIncludeCommon(Tok, II, PP, Lookup, LookupFromFile); 1203 } 1204 1205 /// \brief Process __building_module(identifier) expression. 1206 /// \returns true if we are building the named module, false otherwise. 1207 static bool EvaluateBuildingModule(Token &Tok, 1208 IdentifierInfo *II, Preprocessor &PP) { 1209 // Get '('. 1210 PP.LexNonComment(Tok); 1211 1212 // Ensure we have a '('. 1213 if (Tok.isNot(tok::l_paren)) { 1214 PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II 1215 << tok::l_paren; 1216 return false; 1217 } 1218 1219 // Save '(' location for possible missing ')' message. 1220 SourceLocation LParenLoc = Tok.getLocation(); 1221 1222 // Get the module name. 1223 PP.LexNonComment(Tok); 1224 1225 // Ensure that we have an identifier. 1226 if (Tok.isNot(tok::identifier)) { 1227 PP.Diag(Tok.getLocation(), diag::err_expected_id_building_module); 1228 return false; 1229 } 1230 1231 bool Result 1232 = Tok.getIdentifierInfo()->getName() == PP.getLangOpts().CurrentModule; 1233 1234 // Get ')'. 1235 PP.LexNonComment(Tok); 1236 1237 // Ensure we have a trailing ). 1238 if (Tok.isNot(tok::r_paren)) { 1239 PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II 1240 << tok::r_paren; 1241 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren; 1242 return false; 1243 } 1244 1245 return Result; 1246 } 1247 1248 /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded 1249 /// as a builtin macro, handle it and return the next token as 'Tok'. 1250 void Preprocessor::ExpandBuiltinMacro(Token &Tok) { 1251 // Figure out which token this is. 1252 IdentifierInfo *II = Tok.getIdentifierInfo(); 1253 assert(II && "Can't be a macro without id info!"); 1254 1255 // If this is an _Pragma or Microsoft __pragma directive, expand it, 1256 // invoke the pragma handler, then lex the token after it. 1257 if (II == Ident_Pragma) 1258 return Handle_Pragma(Tok); 1259 else if (II == Ident__pragma) // in non-MS mode this is null 1260 return HandleMicrosoft__pragma(Tok); 1261 1262 ++NumBuiltinMacroExpanded; 1263 1264 SmallString<128> TmpBuffer; 1265 llvm::raw_svector_ostream OS(TmpBuffer); 1266 1267 // Set up the return result. 1268 Tok.setIdentifierInfo(nullptr); 1269 Tok.clearFlag(Token::NeedsCleaning); 1270 1271 if (II == Ident__LINE__) { 1272 // C99 6.10.8: "__LINE__: The presumed line number (within the current 1273 // source file) of the current source line (an integer constant)". This can 1274 // be affected by #line. 1275 SourceLocation Loc = Tok.getLocation(); 1276 1277 // Advance to the location of the first _, this might not be the first byte 1278 // of the token if it starts with an escaped newline. 1279 Loc = AdvanceToTokenCharacter(Loc, 0); 1280 1281 // One wrinkle here is that GCC expands __LINE__ to location of the *end* of 1282 // a macro expansion. This doesn't matter for object-like macros, but 1283 // can matter for a function-like macro that expands to contain __LINE__. 1284 // Skip down through expansion points until we find a file loc for the 1285 // end of the expansion history. 1286 Loc = SourceMgr.getExpansionRange(Loc).second; 1287 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc); 1288 1289 // __LINE__ expands to a simple numeric value. 1290 OS << (PLoc.isValid()? PLoc.getLine() : 1); 1291 Tok.setKind(tok::numeric_constant); 1292 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) { 1293 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a 1294 // character string literal)". This can be affected by #line. 1295 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); 1296 1297 // __BASE_FILE__ is a GNU extension that returns the top of the presumed 1298 // #include stack instead of the current file. 1299 if (II == Ident__BASE_FILE__ && PLoc.isValid()) { 1300 SourceLocation NextLoc = PLoc.getIncludeLoc(); 1301 while (NextLoc.isValid()) { 1302 PLoc = SourceMgr.getPresumedLoc(NextLoc); 1303 if (PLoc.isInvalid()) 1304 break; 1305 1306 NextLoc = PLoc.getIncludeLoc(); 1307 } 1308 } 1309 1310 // Escape this filename. Turn '\' -> '\\' '"' -> '\"' 1311 SmallString<128> FN; 1312 if (PLoc.isValid()) { 1313 FN += PLoc.getFilename(); 1314 Lexer::Stringify(FN); 1315 OS << '"' << FN.str() << '"'; 1316 } 1317 Tok.setKind(tok::string_literal); 1318 } else if (II == Ident__DATE__) { 1319 Diag(Tok.getLocation(), diag::warn_pp_date_time); 1320 if (!DATELoc.isValid()) 1321 ComputeDATE_TIME(DATELoc, TIMELoc, *this); 1322 Tok.setKind(tok::string_literal); 1323 Tok.setLength(strlen("\"Mmm dd yyyy\"")); 1324 Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(), 1325 Tok.getLocation(), 1326 Tok.getLength())); 1327 return; 1328 } else if (II == Ident__TIME__) { 1329 Diag(Tok.getLocation(), diag::warn_pp_date_time); 1330 if (!TIMELoc.isValid()) 1331 ComputeDATE_TIME(DATELoc, TIMELoc, *this); 1332 Tok.setKind(tok::string_literal); 1333 Tok.setLength(strlen("\"hh:mm:ss\"")); 1334 Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(), 1335 Tok.getLocation(), 1336 Tok.getLength())); 1337 return; 1338 } else if (II == Ident__INCLUDE_LEVEL__) { 1339 // Compute the presumed include depth of this token. This can be affected 1340 // by GNU line markers. 1341 unsigned Depth = 0; 1342 1343 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); 1344 if (PLoc.isValid()) { 1345 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc()); 1346 for (; PLoc.isValid(); ++Depth) 1347 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc()); 1348 } 1349 1350 // __INCLUDE_LEVEL__ expands to a simple numeric value. 1351 OS << Depth; 1352 Tok.setKind(tok::numeric_constant); 1353 } else if (II == Ident__TIMESTAMP__) { 1354 Diag(Tok.getLocation(), diag::warn_pp_date_time); 1355 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be 1356 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime. 1357 1358 // Get the file that we are lexing out of. If we're currently lexing from 1359 // a macro, dig into the include stack. 1360 const FileEntry *CurFile = nullptr; 1361 PreprocessorLexer *TheLexer = getCurrentFileLexer(); 1362 1363 if (TheLexer) 1364 CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID()); 1365 1366 const char *Result; 1367 if (CurFile) { 1368 time_t TT = CurFile->getModificationTime(); 1369 struct tm *TM = localtime(&TT); 1370 Result = asctime(TM); 1371 } else { 1372 Result = "??? ??? ?? ??:??:?? ????\n"; 1373 } 1374 // Surround the string with " and strip the trailing newline. 1375 OS << '"' << StringRef(Result).drop_back() << '"'; 1376 Tok.setKind(tok::string_literal); 1377 } else if (II == Ident__COUNTER__) { 1378 // __COUNTER__ expands to a simple numeric value. 1379 OS << CounterValue++; 1380 Tok.setKind(tok::numeric_constant); 1381 } else if (II == Ident__has_feature || 1382 II == Ident__has_extension || 1383 II == Ident__has_builtin || 1384 II == Ident__is_identifier || 1385 II == Ident__has_attribute || 1386 II == Ident__has_declspec || 1387 II == Ident__has_cpp_attribute) { 1388 // The argument to these builtins should be a parenthesized identifier. 1389 SourceLocation StartLoc = Tok.getLocation(); 1390 1391 bool IsValid = false; 1392 IdentifierInfo *FeatureII = nullptr; 1393 IdentifierInfo *ScopeII = nullptr; 1394 1395 // Read the '('. 1396 LexUnexpandedToken(Tok); 1397 if (Tok.is(tok::l_paren)) { 1398 // Read the identifier 1399 LexUnexpandedToken(Tok); 1400 if ((FeatureII = Tok.getIdentifierInfo())) { 1401 // If we're checking __has_cpp_attribute, it is possible to receive a 1402 // scope token. Read the "::", if it's available. 1403 LexUnexpandedToken(Tok); 1404 bool IsScopeValid = true; 1405 if (II == Ident__has_cpp_attribute && Tok.is(tok::coloncolon)) { 1406 LexUnexpandedToken(Tok); 1407 // The first thing we read was not the feature, it was the scope. 1408 ScopeII = FeatureII; 1409 if ((FeatureII = Tok.getIdentifierInfo())) 1410 LexUnexpandedToken(Tok); 1411 else 1412 IsScopeValid = false; 1413 } 1414 // Read the closing paren. 1415 if (IsScopeValid && Tok.is(tok::r_paren)) 1416 IsValid = true; 1417 } 1418 // Eat tokens until ')'. 1419 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eod) && 1420 Tok.isNot(tok::eof)) 1421 LexUnexpandedToken(Tok); 1422 } 1423 1424 int Value = 0; 1425 if (!IsValid) 1426 Diag(StartLoc, diag::err_feature_check_malformed); 1427 else if (II == Ident__is_identifier) 1428 Value = FeatureII->getTokenID() == tok::identifier; 1429 else if (II == Ident__has_builtin) { 1430 // Check for a builtin is trivial. 1431 Value = FeatureII->getBuiltinID() != 0; 1432 } else if (II == Ident__has_attribute) 1433 Value = hasAttribute(AttrSyntax::GNU, nullptr, FeatureII, 1434 getTargetInfo().getTriple(), getLangOpts()); 1435 else if (II == Ident__has_cpp_attribute) 1436 Value = hasAttribute(AttrSyntax::CXX, ScopeII, FeatureII, 1437 getTargetInfo().getTriple(), getLangOpts()); 1438 else if (II == Ident__has_declspec) 1439 Value = hasAttribute(AttrSyntax::Declspec, nullptr, FeatureII, 1440 getTargetInfo().getTriple(), getLangOpts()); 1441 else if (II == Ident__has_extension) 1442 Value = HasExtension(*this, FeatureII); 1443 else { 1444 assert(II == Ident__has_feature && "Must be feature check"); 1445 Value = HasFeature(*this, FeatureII); 1446 } 1447 1448 if (!IsValid) 1449 return; 1450 OS << Value; 1451 Tok.setKind(tok::numeric_constant); 1452 } else if (II == Ident__has_include || 1453 II == Ident__has_include_next) { 1454 // The argument to these two builtins should be a parenthesized 1455 // file name string literal using angle brackets (<>) or 1456 // double-quotes (""). 1457 bool Value; 1458 if (II == Ident__has_include) 1459 Value = EvaluateHasInclude(Tok, II, *this); 1460 else 1461 Value = EvaluateHasIncludeNext(Tok, II, *this); 1462 OS << (int)Value; 1463 if (Tok.is(tok::r_paren)) 1464 Tok.setKind(tok::numeric_constant); 1465 } else if (II == Ident__has_warning) { 1466 // The argument should be a parenthesized string literal. 1467 // The argument to these builtins should be a parenthesized identifier. 1468 SourceLocation StartLoc = Tok.getLocation(); 1469 bool IsValid = false; 1470 bool Value = false; 1471 // Read the '('. 1472 LexUnexpandedToken(Tok); 1473 do { 1474 if (Tok.isNot(tok::l_paren)) { 1475 Diag(StartLoc, diag::err_warning_check_malformed); 1476 break; 1477 } 1478 1479 LexUnexpandedToken(Tok); 1480 std::string WarningName; 1481 SourceLocation StrStartLoc = Tok.getLocation(); 1482 if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'", 1483 /*MacroExpansion=*/false)) { 1484 // Eat tokens until ')'. 1485 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eod) && 1486 Tok.isNot(tok::eof)) 1487 LexUnexpandedToken(Tok); 1488 break; 1489 } 1490 1491 // Is the end a ')'? 1492 if (!(IsValid = Tok.is(tok::r_paren))) { 1493 Diag(StartLoc, diag::err_warning_check_malformed); 1494 break; 1495 } 1496 1497 // FIXME: Should we accept "-R..." flags here, or should that be handled 1498 // by a separate __has_remark? 1499 if (WarningName.size() < 3 || WarningName[0] != '-' || 1500 WarningName[1] != 'W') { 1501 Diag(StrStartLoc, diag::warn_has_warning_invalid_option); 1502 break; 1503 } 1504 1505 // Finally, check if the warning flags maps to a diagnostic group. 1506 // We construct a SmallVector here to talk to getDiagnosticIDs(). 1507 // Although we don't use the result, this isn't a hot path, and not 1508 // worth special casing. 1509 SmallVector<diag::kind, 10> Diags; 1510 Value = !getDiagnostics().getDiagnosticIDs()-> 1511 getDiagnosticsInGroup(diag::Flavor::WarningOrError, 1512 WarningName.substr(2), Diags); 1513 } while (false); 1514 1515 if (!IsValid) 1516 return; 1517 OS << (int)Value; 1518 Tok.setKind(tok::numeric_constant); 1519 } else if (II == Ident__building_module) { 1520 // The argument to this builtin should be an identifier. The 1521 // builtin evaluates to 1 when that identifier names the module we are 1522 // currently building. 1523 OS << (int)EvaluateBuildingModule(Tok, II, *this); 1524 Tok.setKind(tok::numeric_constant); 1525 } else if (II == Ident__MODULE__) { 1526 // The current module as an identifier. 1527 OS << getLangOpts().CurrentModule; 1528 IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule); 1529 Tok.setIdentifierInfo(ModuleII); 1530 Tok.setKind(ModuleII->getTokenID()); 1531 } else if (II == Ident__identifier) { 1532 SourceLocation Loc = Tok.getLocation(); 1533 1534 // We're expecting '__identifier' '(' identifier ')'. Try to recover 1535 // if the parens are missing. 1536 LexNonComment(Tok); 1537 if (Tok.isNot(tok::l_paren)) { 1538 // No '(', use end of last token. 1539 Diag(getLocForEndOfToken(Loc), diag::err_pp_expected_after) 1540 << II << tok::l_paren; 1541 // If the next token isn't valid as our argument, we can't recover. 1542 if (!Tok.isAnnotation() && Tok.getIdentifierInfo()) 1543 Tok.setKind(tok::identifier); 1544 return; 1545 } 1546 1547 SourceLocation LParenLoc = Tok.getLocation(); 1548 LexNonComment(Tok); 1549 1550 if (!Tok.isAnnotation() && Tok.getIdentifierInfo()) 1551 Tok.setKind(tok::identifier); 1552 else { 1553 Diag(Tok.getLocation(), diag::err_pp_identifier_arg_not_identifier) 1554 << Tok.getKind(); 1555 // Don't walk past anything that's not a real token. 1556 if (Tok.is(tok::eof) || Tok.is(tok::eod) || Tok.isAnnotation()) 1557 return; 1558 } 1559 1560 // Discard the ')', preserving 'Tok' as our result. 1561 Token RParen; 1562 LexNonComment(RParen); 1563 if (RParen.isNot(tok::r_paren)) { 1564 Diag(getLocForEndOfToken(Tok.getLocation()), diag::err_pp_expected_after) 1565 << Tok.getKind() << tok::r_paren; 1566 Diag(LParenLoc, diag::note_matching) << tok::l_paren; 1567 } 1568 return; 1569 } else { 1570 llvm_unreachable("Unknown identifier!"); 1571 } 1572 CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation()); 1573 } 1574 1575 void Preprocessor::markMacroAsUsed(MacroInfo *MI) { 1576 // If the 'used' status changed, and the macro requires 'unused' warning, 1577 // remove its SourceLocation from the warn-for-unused-macro locations. 1578 if (MI->isWarnIfUnused() && !MI->isUsed()) 1579 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); 1580 MI->setIsUsed(true); 1581 } 1582