PPMacroExpansion.cpp revision 263508
1190293Srwatson//===--- MacroExpansion.cpp - Top level Macro Expansion -------------------===// 2190293Srwatson// 3190293Srwatson// The LLVM Compiler Infrastructure 4190293Srwatson// 5190293Srwatson// This file is distributed under the University of Illinois Open Source 6190293Srwatson// License. See LICENSE.TXT for details. 7190293Srwatson// 8190293Srwatson//===----------------------------------------------------------------------===// 9190293Srwatson// 10190293Srwatson// This file implements the top level handling of macro expasion for the 11190293Srwatson// preprocessor. 12190293Srwatson// 13190293Srwatson//===----------------------------------------------------------------------===// 14190293Srwatson 15190293Srwatson#include "clang/Lex/Preprocessor.h" 16190293Srwatson#include "clang/Lex/MacroArgs.h" 17190293Srwatson#include "clang/Basic/FileManager.h" 18190293Srwatson#include "clang/Basic/SourceManager.h" 19190293Srwatson#include "clang/Basic/TargetInfo.h" 20190293Srwatson#include "clang/Lex/CodeCompletionHandler.h" 21190293Srwatson#include "clang/Lex/ExternalPreprocessorSource.h" 22190293Srwatson#include "clang/Lex/LexDiagnostic.h" 23190293Srwatson#include "clang/Lex/MacroInfo.h" 24190293Srwatson#include "llvm/ADT/STLExtras.h" 25190293Srwatson#include "llvm/ADT/SmallString.h" 26190293Srwatson#include "llvm/ADT/StringSwitch.h" 27190293Srwatson#include "llvm/Config/llvm-config.h" 28190293Srwatson#include "llvm/Support/ErrorHandling.h" 29190293Srwatson#include "llvm/Support/Format.h" 30190293Srwatson#include "llvm/Support/raw_ostream.h" 31190293Srwatson#include <cstdio> 32190293Srwatson#include <ctime> 33190293Srwatsonusing namespace clang; 34190293Srwatson 35190293SrwatsonMacroDirective * 36190293SrwatsonPreprocessor::getMacroDirectiveHistory(const IdentifierInfo *II) const { 37190293Srwatson assert(II->hadMacroDefinition() && "Identifier has not been not a macro!"); 38190293Srwatson 39190293Srwatson macro_iterator Pos = Macros.find(II); 40190293Srwatson assert(Pos != Macros.end() && "Identifier macro info is missing!"); 41190293Srwatson return Pos->second; 42190293Srwatson} 43190293Srwatson 44190293Srwatsonvoid Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){ 45190293Srwatson assert(MD && "MacroDirective should be non-zero!"); 46190293Srwatson assert(!MD->getPrevious() && "Already attached to a MacroDirective history."); 47190380Srwatson 48190380Srwatson MacroDirective *&StoredMD = Macros[II]; 49190293Srwatson MD->setPrevious(StoredMD); 50190380Srwatson StoredMD = MD; 51190380Srwatson II->setHasMacroDefinition(MD->isDefined()); 52190380Srwatson bool isImportedMacro = isa<DefMacroDirective>(MD) && 53190380Srwatson cast<DefMacroDirective>(MD)->isImported(); 54190293Srwatson if (II->isFromAST() && !isImportedMacro) 55190293Srwatson II->setChangedSinceDeserialization(); 56190293Srwatson} 57190293Srwatson 58190293Srwatsonvoid Preprocessor::setLoadedMacroDirective(IdentifierInfo *II, 59190293Srwatson MacroDirective *MD) { 60190293Srwatson assert(II && MD); 61190293Srwatson MacroDirective *&StoredMD = Macros[II]; 62190293Srwatson assert(!StoredMD && 63190293Srwatson "the macro history was modified before initializing it from a pch"); 64190293Srwatson StoredMD = MD; 65190293Srwatson // Setup the identifier as having associated macro history. 66190293Srwatson II->setHasMacroDefinition(true); 67190293Srwatson if (!MD->isDefined()) 68190293Srwatson II->setHasMacroDefinition(false); 69190293Srwatson} 70190293Srwatson 71190293Srwatson/// RegisterBuiltinMacro - Register the specified identifier in the identifier 72190293Srwatson/// table and mark it as a builtin macro to be expanded. 73190380Srwatsonstatic IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){ 74191776Srwatson // Get the identifier. 75190380Srwatson IdentifierInfo *Id = PP.getIdentifierInfo(Name); 76190293Srwatson 77190293Srwatson // Mark it as being a macro that is builtin. 78190293Srwatson MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation()); 79190293Srwatson MI->setIsBuiltinMacro(); 80190293Srwatson PP.appendDefMacroDirective(Id, MI); 81190293Srwatson return Id; 82190293Srwatson} 83190293Srwatson 84190293Srwatson 85190293Srwatson/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the 86190293Srwatson/// identifier table. 87190293Srwatsonvoid Preprocessor::RegisterBuiltinMacros() { 88190293Srwatson Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__"); 89190293Srwatson Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__"); 90190293Srwatson Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__"); 91190293Srwatson Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__"); 92190293Srwatson Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__"); 93190293Srwatson Ident_Pragma = RegisterBuiltinMacro(*this, "_Pragma"); 94190293Srwatson 95190293Srwatson // GCC Extensions. 96190293Srwatson Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__"); 97190293Srwatson Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__"); 98190293Srwatson Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__"); 99190293Srwatson 100190293Srwatson // Clang Extensions. 101190293Srwatson Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature"); 102190293Srwatson Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension"); 103190293Srwatson Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin"); 104190293Srwatson Ident__has_attribute = RegisterBuiltinMacro(*this, "__has_attribute"); 105190293Srwatson Ident__has_include = RegisterBuiltinMacro(*this, "__has_include"); 106190293Srwatson Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next"); 107190293Srwatson Ident__has_warning = RegisterBuiltinMacro(*this, "__has_warning"); 108190293Srwatson 109190293Srwatson // Modules. 110190293Srwatson if (LangOpts.Modules) { 111190293Srwatson Ident__building_module = RegisterBuiltinMacro(*this, "__building_module"); 112190293Srwatson 113190293Srwatson // __MODULE__ 114190293Srwatson if (!LangOpts.CurrentModule.empty()) 115190293Srwatson Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__"); 116190293Srwatson else 117190380Srwatson Ident__MODULE__ = 0; 118190380Srwatson } else { 119190380Srwatson Ident__building_module = 0; 120190380Srwatson Ident__MODULE__ = 0; 121190380Srwatson } 122190380Srwatson 123190380Srwatson // Microsoft Extensions. 124190293Srwatson if (LangOpts.MicrosoftExt) 125190380Srwatson Ident__pragma = RegisterBuiltinMacro(*this, "__pragma"); 126190380Srwatson else 127190380Srwatson Ident__pragma = 0; 128190380Srwatson} 129190380Srwatson 130190380Srwatson/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token 131190293Srwatson/// in its expansion, currently expands to that token literally. 132190380Srwatsonstatic bool isTrivialSingleTokenExpansion(const MacroInfo *MI, 133190380Srwatson const IdentifierInfo *MacroIdent, 134190380Srwatson Preprocessor &PP) { 135190380Srwatson IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo(); 136190380Srwatson 137190380Srwatson // If the token isn't an identifier, it's always literally expanded. 138190380Srwatson if (II == 0) return true; 139190380Srwatson 140190293Srwatson // If the information about this identifier is out of date, update it from 141190293Srwatson // the external source. 142190293Srwatson if (II->isOutOfDate()) 143190293Srwatson PP.getExternalSource()->updateOutOfDateIdentifier(*II); 144190293Srwatson 145190293Srwatson // If the identifier is a macro, and if that macro is enabled, it may be 146190293Srwatson // expanded so it's not a trivial expansion. 147190293Srwatson if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() && 148190293Srwatson // Fast expanding "#define X X" is ok, because X would be disabled. 149190293Srwatson II != MacroIdent) 150190293Srwatson return false; 151190293Srwatson 152190293Srwatson // If this is an object-like macro invocation, it is safe to trivially expand 153190293Srwatson // it. 154190293Srwatson if (MI->isObjectLike()) return true; 155190293Srwatson 156190380Srwatson // If this is a function-like macro invocation, it's safe to trivially expand 157190380Srwatson // as long as the identifier is not a macro argument. 158190380Srwatson for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); 159190380Srwatson I != E; ++I) 160190380Srwatson if (*I == II) 161190380Srwatson return false; // Identifier is a macro argument. 162190380Srwatson 163190380Srwatson return true; 164190380Srwatson} 165190380Srwatson 166190380Srwatson 167190380Srwatson/// isNextPPTokenLParen - Determine whether the next preprocessor token to be 168190380Srwatson/// lexed is a '('. If so, consume the token and return true, if not, this 169190380Srwatson/// method should have no observable side-effect on the lexed tokens. 170190380Srwatsonbool Preprocessor::isNextPPTokenLParen() { 171190380Srwatson // Do some quick tests for rejection cases. 172190293Srwatson unsigned Val; 173190293Srwatson if (CurLexer) 174190293Srwatson Val = CurLexer->isNextPPTokenLParen(); 175190293Srwatson else if (CurPTHLexer) 176190293Srwatson Val = CurPTHLexer->isNextPPTokenLParen(); 177190293Srwatson else 178190293Srwatson Val = CurTokenLexer->isNextTokenLParen(); 179190293Srwatson 180190293Srwatson if (Val == 2) { 181190293Srwatson // We have run off the end. If it's a source file we don't 182190293Srwatson // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the 183190293Srwatson // macro stack. 184190293Srwatson if (CurPPLexer) 185190293Srwatson return false; 186190293Srwatson for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { 187190293Srwatson IncludeStackInfo &Entry = IncludeMacroStack[i-1]; 188190293Srwatson if (Entry.TheLexer) 189190293Srwatson Val = Entry.TheLexer->isNextPPTokenLParen(); 190190293Srwatson else if (Entry.ThePTHLexer) 191190293Srwatson Val = Entry.ThePTHLexer->isNextPPTokenLParen(); 192190293Srwatson else 193190293Srwatson Val = Entry.TheTokenLexer->isNextTokenLParen(); 194190293Srwatson 195190293Srwatson if (Val != 2) 196190293Srwatson break; 197190293Srwatson 198190293Srwatson // Ran off the end of a source file? 199190293Srwatson if (Entry.ThePPLexer) 200190293Srwatson return false; 201190293Srwatson } 202190293Srwatson } 203190293Srwatson 204190293Srwatson // Okay, if we know that the token is a '(', lex it and return. Otherwise we 205190293Srwatson // have found something that isn't a '(' or we found the end of the 206190293Srwatson // translation unit. In either case, return false. 207190380Srwatson return Val == 1; 208190380Srwatson} 209190380Srwatson 210190380Srwatson/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be 211190380Srwatson/// expanded as a macro, handle it and return the next token as 'Identifier'. 212190380Srwatsonbool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier, 213190380Srwatson MacroDirective *MD) { 214190380Srwatson MacroDirective::DefInfo Def = MD->getDefinition(); 215190380Srwatson assert(Def.isValid()); 216190380Srwatson MacroInfo *MI = Def.getMacroInfo(); 217190380Srwatson 218190380Srwatson // If this is a macro expansion in the "#if !defined(x)" line for the file, 219190380Srwatson // then the macro could expand to different things in other contexts, we need 220190380Srwatson // to disable the optimization in this case. 221190380Srwatson if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro(); 222190380Srwatson 223190380Srwatson // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially. 224190380Srwatson if (MI->isBuiltinMacro()) { 225190380Srwatson if (Callbacks) Callbacks->MacroExpands(Identifier, MD, 226190380Srwatson Identifier.getLocation(),/*Args=*/0); 227190380Srwatson ExpandBuiltinMacro(Identifier); 228190380Srwatson return true; 229190380Srwatson } 230190380Srwatson 231190380Srwatson /// Args - If this is a function-like macro expansion, this contains, 232190380Srwatson /// for each macro argument, the list of tokens that were provided to the 233190380Srwatson /// invocation. 234190380Srwatson MacroArgs *Args = 0; 235190380Srwatson 236190380Srwatson // Remember where the end of the expansion occurred. For an object-like 237190380Srwatson // macro, this is the identifier. For a function-like macro, this is the ')'. 238190380Srwatson SourceLocation ExpansionEnd = Identifier.getLocation(); 239190380Srwatson 240190380Srwatson // If this is a function-like macro, read the arguments. 241190380Srwatson if (MI->isFunctionLike()) { 242190380Srwatson // Remember that we are now parsing the arguments to a macro invocation. 243190380Srwatson // Preprocessor directives used inside macro arguments are not portable, and 244190380Srwatson // this enables the warning. 245190380Srwatson InMacroArgs = true; 246190293Srwatson Args = ReadFunctionLikeMacroArgs(Identifier, MI, ExpansionEnd); 247190293Srwatson 248190380Srwatson // Finished parsing args. 249190380Srwatson InMacroArgs = false; 250190380Srwatson 251190293Srwatson // If there was an error parsing the arguments, bail out. 252190380Srwatson if (Args == 0) return true; 253190380Srwatson 254190380Srwatson ++NumFnMacroExpanded; 255190380Srwatson } else { 256190380Srwatson ++NumMacroExpanded; 257190380Srwatson } 258190380Srwatson 259190380Srwatson // Notice that this macro has been used. 260190380Srwatson markMacroAsUsed(MI); 261190380Srwatson 262190380Srwatson // Remember where the token is expanded. 263190380Srwatson SourceLocation ExpandLoc = Identifier.getLocation(); 264190380Srwatson SourceRange ExpansionRange(ExpandLoc, ExpansionEnd); 265190380Srwatson 266190380Srwatson if (Callbacks) { 267190380Srwatson if (InMacroArgs) { 268190380Srwatson // We can have macro expansion inside a conditional directive while 269190380Srwatson // reading the function macro arguments. To ensure, in that case, that 270190380Srwatson // MacroExpands callbacks still happen in source order, queue this 271190380Srwatson // callback to have it happen after the function macro callback. 272190380Srwatson DelayedMacroExpandsCallbacks.push_back( 273190380Srwatson MacroExpandsInfo(Identifier, MD, ExpansionRange)); 274190380Srwatson } else { 275190380Srwatson Callbacks->MacroExpands(Identifier, MD, ExpansionRange, Args); 276190380Srwatson if (!DelayedMacroExpandsCallbacks.empty()) { 277190380Srwatson for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) { 278190380Srwatson MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i]; 279190380Srwatson // FIXME: We lose macro args info with delayed callback. 280190380Srwatson Callbacks->MacroExpands(Info.Tok, Info.MD, Info.Range, /*Args=*/0); 281190380Srwatson } 282190380Srwatson DelayedMacroExpandsCallbacks.clear(); 283190380Srwatson } 284190380Srwatson } 285190380Srwatson } 286190380Srwatson 287190380Srwatson // If the macro definition is ambiguous, complain. 288190380Srwatson if (Def.getDirective()->isAmbiguous()) { 289190380Srwatson Diag(Identifier, diag::warn_pp_ambiguous_macro) 290190380Srwatson << Identifier.getIdentifierInfo(); 291190380Srwatson Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen) 292190380Srwatson << Identifier.getIdentifierInfo(); 293190380Srwatson for (MacroDirective::DefInfo PrevDef = Def.getPreviousDefinition(); 294190380Srwatson PrevDef && !PrevDef.isUndefined(); 295190380Srwatson PrevDef = PrevDef.getPreviousDefinition()) { 296190380Srwatson if (PrevDef.getDirective()->isAmbiguous()) { 297190380Srwatson Diag(PrevDef.getMacroInfo()->getDefinitionLoc(), 298190380Srwatson diag::note_pp_ambiguous_macro_other) 299190380Srwatson << Identifier.getIdentifierInfo(); 300190380Srwatson } 301190380Srwatson } 302190380Srwatson } 303190293Srwatson 304190293Srwatson // If we started lexing a macro, enter the macro expansion body. 305190293Srwatson 306190293Srwatson // If this macro expands to no tokens, don't bother to push it onto the 307190293Srwatson // expansion stack, only to take it right back off. 308190293Srwatson if (MI->getNumTokens() == 0) { 309190293Srwatson // No need for arg info. 310190293Srwatson if (Args) Args->destroy(*this); 311190293Srwatson 312190293Srwatson // Propagate whitespace info as if we had pushed, then popped, 313190293Srwatson // a macro context. 314190293Srwatson Identifier.setFlag(Token::LeadingEmptyMacro); 315190293Srwatson PropagateLineStartLeadingSpaceInfo(Identifier); 316190293Srwatson ++NumFastMacroExpanded; 317190380Srwatson return false; 318190293Srwatson } else if (MI->getNumTokens() == 1 && 319190380Srwatson isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(), 320190380Srwatson *this)) { 321190380Srwatson // Otherwise, if this macro expands into a single trivially-expanded 322190380Srwatson // token: expand it now. This handles common cases like 323190380Srwatson // "#define VAL 42". 324190380Srwatson 325190380Srwatson // No need for arg info. 326190380Srwatson if (Args) Args->destroy(*this); 327190380Srwatson 328190380Srwatson // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro 329190380Srwatson // identifier to the expanded token. 330190380Srwatson bool isAtStartOfLine = Identifier.isAtStartOfLine(); 331190380Srwatson bool hasLeadingSpace = Identifier.hasLeadingSpace(); 332190380Srwatson 333190380Srwatson // Replace the result token. 334190380Srwatson Identifier = MI->getReplacementToken(0); 335190380Srwatson 336190380Srwatson // Restore the StartOfLine/LeadingSpace markers. 337190380Srwatson Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine); 338190380Srwatson Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace); 339190380Srwatson 340190380Srwatson // Update the tokens location to include both its expansion and physical 341190380Srwatson // locations. 342190380Srwatson SourceLocation Loc = 343190380Srwatson SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc, 344190380Srwatson ExpansionEnd,Identifier.getLength()); 345190380Srwatson Identifier.setLocation(Loc); 346190380Srwatson 347190380Srwatson // If this is a disabled macro or #define X X, we must mark the result as 348190380Srwatson // unexpandable. 349190380Srwatson if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) { 350190380Srwatson if (MacroInfo *NewMI = getMacroInfo(NewII)) 351190380Srwatson if (!NewMI->isEnabled() || NewMI == MI) { 352190380Srwatson Identifier.setFlag(Token::DisableExpand); 353190380Srwatson // Don't warn for "#define X X" like "#define bool bool" from 354190380Srwatson // stdbool.h. 355190380Srwatson if (NewMI != MI || MI->isFunctionLike()) 356190380Srwatson Diag(Identifier, diag::pp_disabled_macro_expansion); 357190380Srwatson } 358190380Srwatson } 359190380Srwatson 360190380Srwatson // Since this is not an identifier token, it can't be macro expanded, so 361190380Srwatson // we're done. 362190380Srwatson ++NumFastMacroExpanded; 363190380Srwatson return true; 364190380Srwatson } 365190380Srwatson 366190380Srwatson // Start expanding the macro. 367190380Srwatson EnterMacro(Identifier, ExpansionEnd, MI, Args); 368190380Srwatson return false; 369190380Srwatson} 370190380Srwatson 371190380Srwatsonenum Bracket { 372190380Srwatson Brace, 373190380Srwatson Paren 374190380Srwatson}; 375190380Srwatson 376190293Srwatson/// CheckMatchedBrackets - Returns true if the braces and parentheses in the 377190293Srwatson/// token vector are properly nested. 378190380Srwatsonstatic bool CheckMatchedBrackets(const SmallVectorImpl<Token> &Tokens) { 379190380Srwatson SmallVector<Bracket, 8> Brackets; 380190380Srwatson for (SmallVectorImpl<Token>::const_iterator I = Tokens.begin(), 381190293Srwatson E = Tokens.end(); 382190293Srwatson I != E; ++I) { 383190380Srwatson if (I->is(tok::l_paren)) { 384190293Srwatson Brackets.push_back(Paren); 385190380Srwatson } else if (I->is(tok::r_paren)) { 386190293Srwatson if (Brackets.empty() || Brackets.back() == Brace) 387190293Srwatson return false; 388190293Srwatson Brackets.pop_back(); 389190380Srwatson } else if (I->is(tok::l_brace)) { 390190380Srwatson Brackets.push_back(Brace); 391190380Srwatson } else if (I->is(tok::r_brace)) { 392190293Srwatson if (Brackets.empty() || Brackets.back() == Paren) 393190293Srwatson return false; 394190380Srwatson Brackets.pop_back(); 395190293Srwatson } 396190380Srwatson } 397190293Srwatson if (!Brackets.empty()) 398190293Srwatson return false; 399190293Srwatson return true; 400190293Srwatson} 401190293Srwatson 402190380Srwatson/// GenerateNewArgTokens - Returns true if OldTokens can be converted to a new 403190293Srwatson/// vector of tokens in NewTokens. The new number of arguments will be placed 404190293Srwatson/// in NumArgs and the ranges which need to surrounded in parentheses will be 405190380Srwatson/// in ParenHints. 406190380Srwatson/// Returns false if the token stream cannot be changed. If this is because 407190380Srwatson/// of an initializer list starting a macro argument, the range of those 408190293Srwatson/// initializer lists will be place in InitLists. 409190293Srwatsonstatic bool GenerateNewArgTokens(Preprocessor &PP, 410190380Srwatson SmallVectorImpl<Token> &OldTokens, 411190293Srwatson SmallVectorImpl<Token> &NewTokens, 412190380Srwatson unsigned &NumArgs, 413190293Srwatson SmallVectorImpl<SourceRange> &ParenHints, 414190293Srwatson SmallVectorImpl<SourceRange> &InitLists) { 415190380Srwatson if (!CheckMatchedBrackets(OldTokens)) 416190380Srwatson return false; 417190380Srwatson 418190293Srwatson // Once it is known that the brackets are matched, only a simple count of the 419190293Srwatson // braces is needed. 420190380Srwatson unsigned Braces = 0; 421190293Srwatson 422190380Srwatson // First token of a new macro argument. 423190293Srwatson SmallVectorImpl<Token>::iterator ArgStartIterator = OldTokens.begin(); 424190293Srwatson 425190293Srwatson // First closing brace in a new macro argument. Used to generate 426190293Srwatson // SourceRanges for InitLists. 427190293Srwatson SmallVectorImpl<Token>::iterator ClosingBrace = OldTokens.end(); 428190293Srwatson NumArgs = 0; 429190293Srwatson Token TempToken; 430190293Srwatson // Set to true when a macro separator token is found inside a braced list. 431190293Srwatson // If true, the fixed argument spans multiple old arguments and ParenHints 432190293Srwatson // will be updated. 433190293Srwatson bool FoundSeparatorToken = false; 434190293Srwatson for (SmallVectorImpl<Token>::iterator I = OldTokens.begin(), 435190293Srwatson E = OldTokens.end(); 436190293Srwatson I != E; ++I) { 437190380Srwatson if (I->is(tok::l_brace)) { 438190293Srwatson ++Braces; 439190380Srwatson } else if (I->is(tok::r_brace)) { 440190380Srwatson --Braces; 441190380Srwatson if (Braces == 0 && ClosingBrace == E && FoundSeparatorToken) 442190380Srwatson ClosingBrace = I; 443190380Srwatson } else if (I->is(tok::eof)) { 444190380Srwatson // EOF token is used to separate macro arguments 445190380Srwatson if (Braces != 0) { 446190380Srwatson // Assume comma separator is actually braced list separator and change 447190380Srwatson // it back to a comma. 448190380Srwatson FoundSeparatorToken = true; 449190380Srwatson I->setKind(tok::comma); 450190380Srwatson I->setLength(1); 451190380Srwatson } else { // Braces == 0 452190380Srwatson // Separator token still separates arguments. 453190380Srwatson ++NumArgs; 454190380Srwatson 455190380Srwatson // If the argument starts with a brace, it can't be fixed with 456190380Srwatson // parentheses. A different diagnostic will be given. 457190293Srwatson if (FoundSeparatorToken && ArgStartIterator->is(tok::l_brace)) { 458190293Srwatson InitLists.push_back( 459190293Srwatson SourceRange(ArgStartIterator->getLocation(), 460190293Srwatson PP.getLocForEndOfToken(ClosingBrace->getLocation()))); 461190293Srwatson ClosingBrace = E; 462190293Srwatson } 463190293Srwatson 464190380Srwatson // Add left paren 465190380Srwatson if (FoundSeparatorToken) { 466190380Srwatson TempToken.startToken(); 467190380Srwatson TempToken.setKind(tok::l_paren); 468190380Srwatson TempToken.setLocation(ArgStartIterator->getLocation()); 469190380Srwatson TempToken.setLength(0); 470190380Srwatson NewTokens.push_back(TempToken); 471190380Srwatson } 472190380Srwatson 473190380Srwatson // Copy over argument tokens 474190380Srwatson NewTokens.insert(NewTokens.end(), ArgStartIterator, I); 475190380Srwatson 476190380Srwatson // Add right paren and store the paren locations in ParenHints 477190380Srwatson if (FoundSeparatorToken) { 478190380Srwatson SourceLocation Loc = PP.getLocForEndOfToken((I - 1)->getLocation()); 479190380Srwatson TempToken.startToken(); 480190380Srwatson TempToken.setKind(tok::r_paren); 481190380Srwatson TempToken.setLocation(Loc); 482190293Srwatson TempToken.setLength(0); 483190293Srwatson NewTokens.push_back(TempToken); 484190293Srwatson ParenHints.push_back(SourceRange(ArgStartIterator->getLocation(), 485190293Srwatson Loc)); 486190293Srwatson } 487190293Srwatson 488190293Srwatson // Copy separator token 489190293Srwatson NewTokens.push_back(*I); 490190293Srwatson 491190293Srwatson // Reset values 492190293Srwatson ArgStartIterator = I + 1; 493190293Srwatson FoundSeparatorToken = false; 494190293Srwatson } 495190293Srwatson } 496190293Srwatson } 497190293Srwatson 498190293Srwatson return !ParenHints.empty() && InitLists.empty(); 499190293Srwatson} 500190293Srwatson 501190293Srwatson/// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next 502190293Srwatson/// token is the '(' of the macro, this method is invoked to read all of the 503190293Srwatson/// actual arguments specified for the macro invocation. This returns null on 504190293Srwatson/// error. 505190293SrwatsonMacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName, 506190293Srwatson MacroInfo *MI, 507204063Spjd SourceLocation &MacroEnd) { 508190293Srwatson // The number of fixed arguments to parse. 509190293Srwatson unsigned NumFixedArgsLeft = MI->getNumArgs(); 510190293Srwatson bool isVariadic = MI->isVariadic(); 511190293Srwatson 512190293Srwatson // Outer loop, while there are more arguments, keep reading them. 513190293Srwatson Token Tok; 514190293Srwatson 515190293Srwatson // Read arguments as unexpanded tokens. This avoids issues, e.g., where 516190293Srwatson // an argument value in a macro could expand to ',' or '(' or ')'. 517190293Srwatson LexUnexpandedToken(Tok); 518190293Srwatson assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?"); 519190293Srwatson 520190293Srwatson // ArgTokens - Build up a list of tokens that make up each argument. Each 521190293Srwatson // argument is separated by an EOF token. Use a SmallVector so we can avoid 522190293Srwatson // heap allocations in the common case. 523190293Srwatson SmallVector<Token, 64> ArgTokens; 524190293Srwatson bool ContainsCodeCompletionTok = false; 525190293Srwatson 526190293Srwatson SourceLocation TooManyArgsLoc; 527190293Srwatson 528190293Srwatson unsigned NumActuals = 0; 529190293Srwatson while (Tok.isNot(tok::r_paren)) { 530190293Srwatson if (ContainsCodeCompletionTok && (Tok.is(tok::eof) || Tok.is(tok::eod))) 531190293Srwatson break; 532190293Srwatson 533190293Srwatson assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) && 534190293Srwatson "only expect argument separators here"); 535190293Srwatson 536190293Srwatson unsigned ArgTokenStart = ArgTokens.size(); 537190293Srwatson SourceLocation ArgStartLoc = Tok.getLocation(); 538190293Srwatson 539190293Srwatson // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note 540190293Srwatson // that we already consumed the first one. 541190293Srwatson unsigned NumParens = 0; 542221542Srmacklem 543 while (1) { 544 // Read arguments as unexpanded tokens. This avoids issues, e.g., where 545 // an argument value in a macro could expand to ',' or '(' or ')'. 546 LexUnexpandedToken(Tok); 547 548 if (Tok.is(tok::eof) || Tok.is(tok::eod)) { // "#if f(<eof>" & "#if f(\n" 549 if (!ContainsCodeCompletionTok) { 550 Diag(MacroName, diag::err_unterm_macro_invoc); 551 Diag(MI->getDefinitionLoc(), diag::note_macro_here) 552 << MacroName.getIdentifierInfo(); 553 // Do not lose the EOF/EOD. Return it to the client. 554 MacroName = Tok; 555 return 0; 556 } else { 557 // Do not lose the EOF/EOD. 558 Token *Toks = new Token[1]; 559 Toks[0] = Tok; 560 EnterTokenStream(Toks, 1, true, true); 561 break; 562 } 563 } else if (Tok.is(tok::r_paren)) { 564 // If we found the ) token, the macro arg list is done. 565 if (NumParens-- == 0) { 566 MacroEnd = Tok.getLocation(); 567 break; 568 } 569 } else if (Tok.is(tok::l_paren)) { 570 ++NumParens; 571 } else if (Tok.is(tok::comma) && NumParens == 0 && 572 !(Tok.getFlags() & Token::IgnoredComma)) { 573 // In Microsoft-compatibility mode, single commas from nested macro 574 // expansions should not be considered as argument separators. We test 575 // for this with the IgnoredComma token flag above. 576 577 // Comma ends this argument if there are more fixed arguments expected. 578 // However, if this is a variadic macro, and this is part of the 579 // variadic part, then the comma is just an argument token. 580 if (!isVariadic) break; 581 if (NumFixedArgsLeft > 1) 582 break; 583 } else if (Tok.is(tok::comment) && !KeepMacroComments) { 584 // If this is a comment token in the argument list and we're just in 585 // -C mode (not -CC mode), discard the comment. 586 continue; 587 } else if (Tok.getIdentifierInfo() != 0) { 588 // Reading macro arguments can cause macros that we are currently 589 // expanding from to be popped off the expansion stack. Doing so causes 590 // them to be reenabled for expansion. Here we record whether any 591 // identifiers we lex as macro arguments correspond to disabled macros. 592 // If so, we mark the token as noexpand. This is a subtle aspect of 593 // C99 6.10.3.4p2. 594 if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo())) 595 if (!MI->isEnabled()) 596 Tok.setFlag(Token::DisableExpand); 597 } else if (Tok.is(tok::code_completion)) { 598 ContainsCodeCompletionTok = true; 599 if (CodeComplete) 600 CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(), 601 MI, NumActuals); 602 // Don't mark that we reached the code-completion point because the 603 // parser is going to handle the token and there will be another 604 // code-completion callback. 605 } 606 607 ArgTokens.push_back(Tok); 608 } 609 610 // If this was an empty argument list foo(), don't add this as an empty 611 // argument. 612 if (ArgTokens.empty() && Tok.getKind() == tok::r_paren) 613 break; 614 615 // If this is not a variadic macro, and too many args were specified, emit 616 // an error. 617 if (!isVariadic && NumFixedArgsLeft == 0 && TooManyArgsLoc.isInvalid()) { 618 if (ArgTokens.size() != ArgTokenStart) 619 TooManyArgsLoc = ArgTokens[ArgTokenStart].getLocation(); 620 else 621 TooManyArgsLoc = ArgStartLoc; 622 } 623 624 // Empty arguments are standard in C99 and C++0x, and are supported as an 625 // extension in other modes. 626 if (ArgTokens.size() == ArgTokenStart && !LangOpts.C99) 627 Diag(Tok, LangOpts.CPlusPlus11 ? 628 diag::warn_cxx98_compat_empty_fnmacro_arg : 629 diag::ext_empty_fnmacro_arg); 630 631 // Add a marker EOF token to the end of the token list for this argument. 632 Token EOFTok; 633 EOFTok.startToken(); 634 EOFTok.setKind(tok::eof); 635 EOFTok.setLocation(Tok.getLocation()); 636 EOFTok.setLength(0); 637 ArgTokens.push_back(EOFTok); 638 ++NumActuals; 639 if (!ContainsCodeCompletionTok && NumFixedArgsLeft != 0) 640 --NumFixedArgsLeft; 641 } 642 643 // Okay, we either found the r_paren. Check to see if we parsed too few 644 // arguments. 645 unsigned MinArgsExpected = MI->getNumArgs(); 646 647 // If this is not a variadic macro, and too many args were specified, emit 648 // an error. 649 if (!isVariadic && NumActuals > MinArgsExpected && 650 !ContainsCodeCompletionTok) { 651 // Emit the diagnostic at the macro name in case there is a missing ). 652 // Emitting it at the , could be far away from the macro name. 653 Diag(TooManyArgsLoc, diag::err_too_many_args_in_macro_invoc); 654 Diag(MI->getDefinitionLoc(), diag::note_macro_here) 655 << MacroName.getIdentifierInfo(); 656 657 // Commas from braced initializer lists will be treated as argument 658 // separators inside macros. Attempt to correct for this with parentheses. 659 // TODO: See if this can be generalized to angle brackets for templates 660 // inside macro arguments. 661 662 SmallVector<Token, 4> FixedArgTokens; 663 unsigned FixedNumArgs = 0; 664 SmallVector<SourceRange, 4> ParenHints, InitLists; 665 if (!GenerateNewArgTokens(*this, ArgTokens, FixedArgTokens, FixedNumArgs, 666 ParenHints, InitLists)) { 667 if (!InitLists.empty()) { 668 DiagnosticBuilder DB = 669 Diag(MacroName, 670 diag::note_init_list_at_beginning_of_macro_argument); 671 for (SmallVector<SourceRange, 4>::iterator 672 Range = InitLists.begin(), RangeEnd = InitLists.end(); 673 Range != RangeEnd; ++Range) { 674 if (DB.hasMaxRanges()) 675 break; 676 DB << *Range; 677 } 678 } 679 return 0; 680 } 681 if (FixedNumArgs != MinArgsExpected) 682 return 0; 683 684 DiagnosticBuilder DB = Diag(MacroName, diag::note_suggest_parens_for_macro); 685 for (SmallVector<SourceRange, 4>::iterator 686 ParenLocation = ParenHints.begin(), ParenEnd = ParenHints.end(); 687 ParenLocation != ParenEnd; ++ParenLocation) { 688 if (DB.hasMaxFixItHints()) 689 break; 690 DB << FixItHint::CreateInsertion(ParenLocation->getBegin(), "("); 691 if (DB.hasMaxFixItHints()) 692 break; 693 DB << FixItHint::CreateInsertion(ParenLocation->getEnd(), ")"); 694 } 695 ArgTokens.swap(FixedArgTokens); 696 NumActuals = FixedNumArgs; 697 } 698 699 // See MacroArgs instance var for description of this. 700 bool isVarargsElided = false; 701 702 if (ContainsCodeCompletionTok) { 703 // Recover from not-fully-formed macro invocation during code-completion. 704 Token EOFTok; 705 EOFTok.startToken(); 706 EOFTok.setKind(tok::eof); 707 EOFTok.setLocation(Tok.getLocation()); 708 EOFTok.setLength(0); 709 for (; NumActuals < MinArgsExpected; ++NumActuals) 710 ArgTokens.push_back(EOFTok); 711 } 712 713 if (NumActuals < MinArgsExpected) { 714 // There are several cases where too few arguments is ok, handle them now. 715 if (NumActuals == 0 && MinArgsExpected == 1) { 716 // #define A(X) or #define A(...) ---> A() 717 718 // If there is exactly one argument, and that argument is missing, 719 // then we have an empty "()" argument empty list. This is fine, even if 720 // the macro expects one argument (the argument is just empty). 721 isVarargsElided = MI->isVariadic(); 722 } else if (MI->isVariadic() && 723 (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X) 724 (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A() 725 // Varargs where the named vararg parameter is missing: OK as extension. 726 // #define A(x, ...) 727 // A("blah") 728 // 729 // If the macro contains the comma pasting extension, the diagnostic 730 // is suppressed; we know we'll get another diagnostic later. 731 if (!MI->hasCommaPasting()) { 732 Diag(Tok, diag::ext_missing_varargs_arg); 733 Diag(MI->getDefinitionLoc(), diag::note_macro_here) 734 << MacroName.getIdentifierInfo(); 735 } 736 737 // Remember this occurred, allowing us to elide the comma when used for 738 // cases like: 739 // #define A(x, foo...) blah(a, ## foo) 740 // #define B(x, ...) blah(a, ## __VA_ARGS__) 741 // #define C(...) blah(a, ## __VA_ARGS__) 742 // A(x) B(x) C() 743 isVarargsElided = true; 744 } else if (!ContainsCodeCompletionTok) { 745 // Otherwise, emit the error. 746 Diag(Tok, diag::err_too_few_args_in_macro_invoc); 747 Diag(MI->getDefinitionLoc(), diag::note_macro_here) 748 << MacroName.getIdentifierInfo(); 749 return 0; 750 } 751 752 // Add a marker EOF token to the end of the token list for this argument. 753 SourceLocation EndLoc = Tok.getLocation(); 754 Tok.startToken(); 755 Tok.setKind(tok::eof); 756 Tok.setLocation(EndLoc); 757 Tok.setLength(0); 758 ArgTokens.push_back(Tok); 759 760 // If we expect two arguments, add both as empty. 761 if (NumActuals == 0 && MinArgsExpected == 2) 762 ArgTokens.push_back(Tok); 763 764 } else if (NumActuals > MinArgsExpected && !MI->isVariadic() && 765 !ContainsCodeCompletionTok) { 766 // Emit the diagnostic at the macro name in case there is a missing ). 767 // Emitting it at the , could be far away from the macro name. 768 Diag(MacroName, diag::err_too_many_args_in_macro_invoc); 769 Diag(MI->getDefinitionLoc(), diag::note_macro_here) 770 << MacroName.getIdentifierInfo(); 771 return 0; 772 } 773 774 return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this); 775} 776 777/// \brief Keeps macro expanded tokens for TokenLexers. 778// 779/// Works like a stack; a TokenLexer adds the macro expanded tokens that is 780/// going to lex in the cache and when it finishes the tokens are removed 781/// from the end of the cache. 782Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer, 783 ArrayRef<Token> tokens) { 784 assert(tokLexer); 785 if (tokens.empty()) 786 return 0; 787 788 size_t newIndex = MacroExpandedTokens.size(); 789 bool cacheNeedsToGrow = tokens.size() > 790 MacroExpandedTokens.capacity()-MacroExpandedTokens.size(); 791 MacroExpandedTokens.append(tokens.begin(), tokens.end()); 792 793 if (cacheNeedsToGrow) { 794 // Go through all the TokenLexers whose 'Tokens' pointer points in the 795 // buffer and update the pointers to the (potential) new buffer array. 796 for (unsigned i = 0, e = MacroExpandingLexersStack.size(); i != e; ++i) { 797 TokenLexer *prevLexer; 798 size_t tokIndex; 799 llvm::tie(prevLexer, tokIndex) = MacroExpandingLexersStack[i]; 800 prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex; 801 } 802 } 803 804 MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex)); 805 return MacroExpandedTokens.data() + newIndex; 806} 807 808void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() { 809 assert(!MacroExpandingLexersStack.empty()); 810 size_t tokIndex = MacroExpandingLexersStack.back().second; 811 assert(tokIndex < MacroExpandedTokens.size()); 812 // Pop the cached macro expanded tokens from the end. 813 MacroExpandedTokens.resize(tokIndex); 814 MacroExpandingLexersStack.pop_back(); 815} 816 817/// ComputeDATE_TIME - Compute the current time, enter it into the specified 818/// scratch buffer, then return DATELoc/TIMELoc locations with the position of 819/// the identifier tokens inserted. 820static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc, 821 Preprocessor &PP) { 822 time_t TT = time(0); 823 struct tm *TM = localtime(&TT); 824 825 static const char * const Months[] = { 826 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" 827 }; 828 829 { 830 SmallString<32> TmpBuffer; 831 llvm::raw_svector_ostream TmpStream(TmpBuffer); 832 TmpStream << llvm::format("\"%s %2d %4d\"", Months[TM->tm_mon], 833 TM->tm_mday, TM->tm_year + 1900); 834 Token TmpTok; 835 TmpTok.startToken(); 836 PP.CreateString(TmpStream.str(), TmpTok); 837 DATELoc = TmpTok.getLocation(); 838 } 839 840 { 841 SmallString<32> TmpBuffer; 842 llvm::raw_svector_ostream TmpStream(TmpBuffer); 843 TmpStream << llvm::format("\"%02d:%02d:%02d\"", 844 TM->tm_hour, TM->tm_min, TM->tm_sec); 845 Token TmpTok; 846 TmpTok.startToken(); 847 PP.CreateString(TmpStream.str(), TmpTok); 848 TIMELoc = TmpTok.getLocation(); 849 } 850} 851 852 853/// HasFeature - Return true if we recognize and implement the feature 854/// specified by the identifier as a standard language feature. 855static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) { 856 const LangOptions &LangOpts = PP.getLangOpts(); 857 StringRef Feature = II->getName(); 858 859 // Normalize the feature name, __foo__ becomes foo. 860 if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4) 861 Feature = Feature.substr(2, Feature.size() - 4); 862 863 return llvm::StringSwitch<bool>(Feature) 864 .Case("address_sanitizer", LangOpts.Sanitize.Address) 865 .Case("attribute_analyzer_noreturn", true) 866 .Case("attribute_availability", true) 867 .Case("attribute_availability_with_message", true) 868 .Case("attribute_cf_returns_not_retained", true) 869 .Case("attribute_cf_returns_retained", true) 870 .Case("attribute_deprecated_with_message", true) 871 .Case("attribute_ext_vector_type", true) 872 .Case("attribute_ns_returns_not_retained", true) 873 .Case("attribute_ns_returns_retained", true) 874 .Case("attribute_ns_consumes_self", true) 875 .Case("attribute_ns_consumed", true) 876 .Case("attribute_cf_consumed", true) 877 .Case("attribute_objc_ivar_unused", true) 878 .Case("attribute_objc_method_family", true) 879 .Case("attribute_overloadable", true) 880 .Case("attribute_unavailable_with_message", true) 881 .Case("attribute_unused_on_fields", true) 882 .Case("blocks", LangOpts.Blocks) 883 .Case("c_thread_safety_attributes", true) 884 .Case("cxx_exceptions", LangOpts.Exceptions) 885 .Case("cxx_rtti", LangOpts.RTTI) 886 .Case("enumerator_attributes", true) 887 .Case("memory_sanitizer", LangOpts.Sanitize.Memory) 888 .Case("thread_sanitizer", LangOpts.Sanitize.Thread) 889 .Case("dataflow_sanitizer", LangOpts.Sanitize.DataFlow) 890 // Objective-C features 891 .Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE? 892 .Case("objc_arc", LangOpts.ObjCAutoRefCount) 893 .Case("objc_arc_weak", LangOpts.ObjCARCWeak) 894 .Case("objc_default_synthesize_properties", LangOpts.ObjC2) 895 .Case("objc_fixed_enum", LangOpts.ObjC2) 896 .Case("objc_instancetype", LangOpts.ObjC2) 897 .Case("objc_modules", LangOpts.ObjC2 && LangOpts.Modules) 898 .Case("objc_nonfragile_abi", LangOpts.ObjCRuntime.isNonFragile()) 899 .Case("objc_property_explicit_atomic", true) // Does clang support explicit "atomic" keyword? 900 .Case("objc_protocol_qualifier_mangling", true) 901 .Case("objc_weak_class", LangOpts.ObjCRuntime.hasWeakClassImport()) 902 .Case("ownership_holds", true) 903 .Case("ownership_returns", true) 904 .Case("ownership_takes", true) 905 .Case("objc_bool", true) 906 .Case("objc_subscripting", LangOpts.ObjCRuntime.isNonFragile()) 907 .Case("objc_array_literals", LangOpts.ObjC2) 908 .Case("objc_dictionary_literals", LangOpts.ObjC2) 909 .Case("objc_boxed_expressions", LangOpts.ObjC2) 910 .Case("arc_cf_code_audited", true) 911 // C11 features 912 .Case("c_alignas", LangOpts.C11) 913 .Case("c_atomic", LangOpts.C11) 914 .Case("c_generic_selections", LangOpts.C11) 915 .Case("c_static_assert", LangOpts.C11) 916 .Case("c_thread_local", 917 LangOpts.C11 && PP.getTargetInfo().isTLSSupported()) 918 // C++11 features 919 .Case("cxx_access_control_sfinae", LangOpts.CPlusPlus11) 920 .Case("cxx_alias_templates", LangOpts.CPlusPlus11) 921 .Case("cxx_alignas", LangOpts.CPlusPlus11) 922 .Case("cxx_atomic", LangOpts.CPlusPlus11) 923 .Case("cxx_attributes", LangOpts.CPlusPlus11) 924 .Case("cxx_auto_type", LangOpts.CPlusPlus11) 925 .Case("cxx_constexpr", LangOpts.CPlusPlus11) 926 .Case("cxx_decltype", LangOpts.CPlusPlus11) 927 .Case("cxx_decltype_incomplete_return_types", LangOpts.CPlusPlus11) 928 .Case("cxx_default_function_template_args", LangOpts.CPlusPlus11) 929 .Case("cxx_defaulted_functions", LangOpts.CPlusPlus11) 930 .Case("cxx_delegating_constructors", LangOpts.CPlusPlus11) 931 .Case("cxx_deleted_functions", LangOpts.CPlusPlus11) 932 .Case("cxx_explicit_conversions", LangOpts.CPlusPlus11) 933 .Case("cxx_generalized_initializers", LangOpts.CPlusPlus11) 934 .Case("cxx_implicit_moves", LangOpts.CPlusPlus11) 935 .Case("cxx_inheriting_constructors", LangOpts.CPlusPlus11) 936 .Case("cxx_inline_namespaces", LangOpts.CPlusPlus11) 937 .Case("cxx_lambdas", LangOpts.CPlusPlus11) 938 .Case("cxx_local_type_template_args", LangOpts.CPlusPlus11) 939 .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus11) 940 .Case("cxx_noexcept", LangOpts.CPlusPlus11) 941 .Case("cxx_nullptr", LangOpts.CPlusPlus11) 942 .Case("cxx_override_control", LangOpts.CPlusPlus11) 943 .Case("cxx_range_for", LangOpts.CPlusPlus11) 944 .Case("cxx_raw_string_literals", LangOpts.CPlusPlus11) 945 .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus11) 946 .Case("cxx_rvalue_references", LangOpts.CPlusPlus11) 947 .Case("cxx_strong_enums", LangOpts.CPlusPlus11) 948 .Case("cxx_static_assert", LangOpts.CPlusPlus11) 949 .Case("cxx_thread_local", 950 LangOpts.CPlusPlus11 && PP.getTargetInfo().isTLSSupported()) 951 .Case("cxx_trailing_return", LangOpts.CPlusPlus11) 952 .Case("cxx_unicode_literals", LangOpts.CPlusPlus11) 953 .Case("cxx_unrestricted_unions", LangOpts.CPlusPlus11) 954 .Case("cxx_user_literals", LangOpts.CPlusPlus11) 955 .Case("cxx_variadic_templates", LangOpts.CPlusPlus11) 956 // C++1y features 957 .Case("cxx_aggregate_nsdmi", LangOpts.CPlusPlus1y) 958 .Case("cxx_binary_literals", LangOpts.CPlusPlus1y) 959 .Case("cxx_contextual_conversions", LangOpts.CPlusPlus1y) 960 //.Case("cxx_generic_lambdas", LangOpts.CPlusPlus1y) 961 .Case("cxx_init_captures", LangOpts.CPlusPlus1y) 962 .Case("cxx_relaxed_constexpr", LangOpts.CPlusPlus1y) 963 .Case("cxx_return_type_deduction", LangOpts.CPlusPlus1y) 964 //.Case("cxx_runtime_arrays", LangOpts.CPlusPlus1y) 965 .Case("cxx_variable_templates", LangOpts.CPlusPlus1y) 966 // Type traits 967 .Case("has_nothrow_assign", LangOpts.CPlusPlus) 968 .Case("has_nothrow_copy", LangOpts.CPlusPlus) 969 .Case("has_nothrow_constructor", LangOpts.CPlusPlus) 970 .Case("has_trivial_assign", LangOpts.CPlusPlus) 971 .Case("has_trivial_copy", LangOpts.CPlusPlus) 972 .Case("has_trivial_constructor", LangOpts.CPlusPlus) 973 .Case("has_trivial_destructor", LangOpts.CPlusPlus) 974 .Case("has_virtual_destructor", LangOpts.CPlusPlus) 975 .Case("is_abstract", LangOpts.CPlusPlus) 976 .Case("is_base_of", LangOpts.CPlusPlus) 977 .Case("is_class", LangOpts.CPlusPlus) 978 .Case("is_convertible_to", LangOpts.CPlusPlus) 979 .Case("is_empty", LangOpts.CPlusPlus) 980 .Case("is_enum", LangOpts.CPlusPlus) 981 .Case("is_final", LangOpts.CPlusPlus) 982 .Case("is_literal", LangOpts.CPlusPlus) 983 .Case("is_standard_layout", LangOpts.CPlusPlus) 984 .Case("is_pod", LangOpts.CPlusPlus) 985 .Case("is_polymorphic", LangOpts.CPlusPlus) 986 .Case("is_sealed", LangOpts.MicrosoftExt) 987 .Case("is_trivial", LangOpts.CPlusPlus) 988 .Case("is_trivially_assignable", LangOpts.CPlusPlus) 989 .Case("is_trivially_constructible", LangOpts.CPlusPlus) 990 .Case("is_trivially_copyable", LangOpts.CPlusPlus) 991 .Case("is_union", LangOpts.CPlusPlus) 992 .Case("modules", LangOpts.Modules) 993 .Case("tls", PP.getTargetInfo().isTLSSupported()) 994 .Case("underlying_type", LangOpts.CPlusPlus) 995 .Default(false); 996} 997 998/// HasExtension - Return true if we recognize and implement the feature 999/// specified by the identifier, either as an extension or a standard language 1000/// feature. 1001static bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) { 1002 if (HasFeature(PP, II)) 1003 return true; 1004 1005 // If the use of an extension results in an error diagnostic, extensions are 1006 // effectively unavailable, so just return false here. 1007 if (PP.getDiagnostics().getExtensionHandlingBehavior() == 1008 DiagnosticsEngine::Ext_Error) 1009 return false; 1010 1011 const LangOptions &LangOpts = PP.getLangOpts(); 1012 StringRef Extension = II->getName(); 1013 1014 // Normalize the extension name, __foo__ becomes foo. 1015 if (Extension.startswith("__") && Extension.endswith("__") && 1016 Extension.size() >= 4) 1017 Extension = Extension.substr(2, Extension.size() - 4); 1018 1019 // Because we inherit the feature list from HasFeature, this string switch 1020 // must be less restrictive than HasFeature's. 1021 return llvm::StringSwitch<bool>(Extension) 1022 // C11 features supported by other languages as extensions. 1023 .Case("c_alignas", true) 1024 .Case("c_atomic", true) 1025 .Case("c_generic_selections", true) 1026 .Case("c_static_assert", true) 1027 .Case("c_thread_local", PP.getTargetInfo().isTLSSupported()) 1028 // C++11 features supported by other languages as extensions. 1029 .Case("cxx_atomic", LangOpts.CPlusPlus) 1030 .Case("cxx_deleted_functions", LangOpts.CPlusPlus) 1031 .Case("cxx_explicit_conversions", LangOpts.CPlusPlus) 1032 .Case("cxx_inline_namespaces", LangOpts.CPlusPlus) 1033 .Case("cxx_local_type_template_args", LangOpts.CPlusPlus) 1034 .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus) 1035 .Case("cxx_override_control", LangOpts.CPlusPlus) 1036 .Case("cxx_range_for", LangOpts.CPlusPlus) 1037 .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus) 1038 .Case("cxx_rvalue_references", LangOpts.CPlusPlus) 1039 // C++1y features supported by other languages as extensions. 1040 .Case("cxx_binary_literals", true) 1041 .Case("cxx_init_captures", LangOpts.CPlusPlus11) 1042 .Case("cxx_variable_templates", true) 1043 .Default(false); 1044} 1045 1046/// HasAttribute - Return true if we recognize and implement the attribute 1047/// specified by the given identifier. 1048static bool HasAttribute(const IdentifierInfo *II) { 1049 StringRef Name = II->getName(); 1050 // Normalize the attribute name, __foo__ becomes foo. 1051 if (Name.startswith("__") && Name.endswith("__") && Name.size() >= 4) 1052 Name = Name.substr(2, Name.size() - 4); 1053 1054 // FIXME: Do we need to handle namespaces here? 1055 return llvm::StringSwitch<bool>(Name) 1056#include "clang/Lex/AttrSpellings.inc" 1057 .Default(false); 1058} 1059 1060/// EvaluateHasIncludeCommon - Process a '__has_include("path")' 1061/// or '__has_include_next("path")' expression. 1062/// Returns true if successful. 1063static bool EvaluateHasIncludeCommon(Token &Tok, 1064 IdentifierInfo *II, Preprocessor &PP, 1065 const DirectoryLookup *LookupFrom) { 1066 // Save the location of the current token. If a '(' is later found, use 1067 // that location. If not, use the end of this location instead. 1068 SourceLocation LParenLoc = Tok.getLocation(); 1069 1070 // These expressions are only allowed within a preprocessor directive. 1071 if (!PP.isParsingIfOrElifDirective()) { 1072 PP.Diag(LParenLoc, diag::err_pp_directive_required) << II->getName(); 1073 return false; 1074 } 1075 1076 // Get '('. 1077 PP.LexNonComment(Tok); 1078 1079 // Ensure we have a '('. 1080 if (Tok.isNot(tok::l_paren)) { 1081 // No '(', use end of last token. 1082 LParenLoc = PP.getLocForEndOfToken(LParenLoc); 1083 PP.Diag(LParenLoc, diag::err_pp_missing_lparen) << II->getName(); 1084 // If the next token looks like a filename or the start of one, 1085 // assume it is and process it as such. 1086 if (!Tok.is(tok::angle_string_literal) && !Tok.is(tok::string_literal) && 1087 !Tok.is(tok::less)) 1088 return false; 1089 } else { 1090 // Save '(' location for possible missing ')' message. 1091 LParenLoc = Tok.getLocation(); 1092 1093 if (PP.getCurrentLexer()) { 1094 // Get the file name. 1095 PP.getCurrentLexer()->LexIncludeFilename(Tok); 1096 } else { 1097 // We're in a macro, so we can't use LexIncludeFilename; just 1098 // grab the next token. 1099 PP.Lex(Tok); 1100 } 1101 } 1102 1103 // Reserve a buffer to get the spelling. 1104 SmallString<128> FilenameBuffer; 1105 StringRef Filename; 1106 SourceLocation EndLoc; 1107 1108 switch (Tok.getKind()) { 1109 case tok::eod: 1110 // If the token kind is EOD, the error has already been diagnosed. 1111 return false; 1112 1113 case tok::angle_string_literal: 1114 case tok::string_literal: { 1115 bool Invalid = false; 1116 Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid); 1117 if (Invalid) 1118 return false; 1119 break; 1120 } 1121 1122 case tok::less: 1123 // This could be a <foo/bar.h> file coming from a macro expansion. In this 1124 // case, glue the tokens together into FilenameBuffer and interpret those. 1125 FilenameBuffer.push_back('<'); 1126 if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) { 1127 // Let the caller know a <eod> was found by changing the Token kind. 1128 Tok.setKind(tok::eod); 1129 return false; // Found <eod> but no ">"? Diagnostic already emitted. 1130 } 1131 Filename = FilenameBuffer.str(); 1132 break; 1133 default: 1134 PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename); 1135 return false; 1136 } 1137 1138 SourceLocation FilenameLoc = Tok.getLocation(); 1139 1140 // Get ')'. 1141 PP.LexNonComment(Tok); 1142 1143 // Ensure we have a trailing ). 1144 if (Tok.isNot(tok::r_paren)) { 1145 PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_missing_rparen) 1146 << II->getName(); 1147 PP.Diag(LParenLoc, diag::note_matching) << "("; 1148 return false; 1149 } 1150 1151 bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename); 1152 // If GetIncludeFilenameSpelling set the start ptr to null, there was an 1153 // error. 1154 if (Filename.empty()) 1155 return false; 1156 1157 // Search include directories. 1158 const DirectoryLookup *CurDir; 1159 const FileEntry *File = 1160 PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, CurDir, NULL, 1161 NULL, NULL); 1162 1163 // Get the result value. A result of true means the file exists. 1164 return File != 0; 1165} 1166 1167/// EvaluateHasInclude - Process a '__has_include("path")' expression. 1168/// Returns true if successful. 1169static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II, 1170 Preprocessor &PP) { 1171 return EvaluateHasIncludeCommon(Tok, II, PP, NULL); 1172} 1173 1174/// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression. 1175/// Returns true if successful. 1176static bool EvaluateHasIncludeNext(Token &Tok, 1177 IdentifierInfo *II, Preprocessor &PP) { 1178 // __has_include_next is like __has_include, except that we start 1179 // searching after the current found directory. If we can't do this, 1180 // issue a diagnostic. 1181 const DirectoryLookup *Lookup = PP.GetCurDirLookup(); 1182 if (PP.isInPrimaryFile()) { 1183 Lookup = 0; 1184 PP.Diag(Tok, diag::pp_include_next_in_primary); 1185 } else if (Lookup == 0) { 1186 PP.Diag(Tok, diag::pp_include_next_absolute_path); 1187 } else { 1188 // Start looking up in the next directory. 1189 ++Lookup; 1190 } 1191 1192 return EvaluateHasIncludeCommon(Tok, II, PP, Lookup); 1193} 1194 1195/// \brief Process __building_module(identifier) expression. 1196/// \returns true if we are building the named module, false otherwise. 1197static bool EvaluateBuildingModule(Token &Tok, 1198 IdentifierInfo *II, Preprocessor &PP) { 1199 // Get '('. 1200 PP.LexNonComment(Tok); 1201 1202 // Ensure we have a '('. 1203 if (Tok.isNot(tok::l_paren)) { 1204 PP.Diag(Tok.getLocation(), diag::err_pp_missing_lparen) << II->getName(); 1205 return false; 1206 } 1207 1208 // Save '(' location for possible missing ')' message. 1209 SourceLocation LParenLoc = Tok.getLocation(); 1210 1211 // Get the module name. 1212 PP.LexNonComment(Tok); 1213 1214 // Ensure that we have an identifier. 1215 if (Tok.isNot(tok::identifier)) { 1216 PP.Diag(Tok.getLocation(), diag::err_expected_id_building_module); 1217 return false; 1218 } 1219 1220 bool Result 1221 = Tok.getIdentifierInfo()->getName() == PP.getLangOpts().CurrentModule; 1222 1223 // Get ')'. 1224 PP.LexNonComment(Tok); 1225 1226 // Ensure we have a trailing ). 1227 if (Tok.isNot(tok::r_paren)) { 1228 PP.Diag(Tok.getLocation(), diag::err_pp_missing_rparen) << II->getName(); 1229 PP.Diag(LParenLoc, diag::note_matching) << "("; 1230 return false; 1231 } 1232 1233 return Result; 1234} 1235 1236/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded 1237/// as a builtin macro, handle it and return the next token as 'Tok'. 1238void Preprocessor::ExpandBuiltinMacro(Token &Tok) { 1239 // Figure out which token this is. 1240 IdentifierInfo *II = Tok.getIdentifierInfo(); 1241 assert(II && "Can't be a macro without id info!"); 1242 1243 // If this is an _Pragma or Microsoft __pragma directive, expand it, 1244 // invoke the pragma handler, then lex the token after it. 1245 if (II == Ident_Pragma) 1246 return Handle_Pragma(Tok); 1247 else if (II == Ident__pragma) // in non-MS mode this is null 1248 return HandleMicrosoft__pragma(Tok); 1249 1250 ++NumBuiltinMacroExpanded; 1251 1252 SmallString<128> TmpBuffer; 1253 llvm::raw_svector_ostream OS(TmpBuffer); 1254 1255 // Set up the return result. 1256 Tok.setIdentifierInfo(0); 1257 Tok.clearFlag(Token::NeedsCleaning); 1258 1259 if (II == Ident__LINE__) { 1260 // C99 6.10.8: "__LINE__: The presumed line number (within the current 1261 // source file) of the current source line (an integer constant)". This can 1262 // be affected by #line. 1263 SourceLocation Loc = Tok.getLocation(); 1264 1265 // Advance to the location of the first _, this might not be the first byte 1266 // of the token if it starts with an escaped newline. 1267 Loc = AdvanceToTokenCharacter(Loc, 0); 1268 1269 // One wrinkle here is that GCC expands __LINE__ to location of the *end* of 1270 // a macro expansion. This doesn't matter for object-like macros, but 1271 // can matter for a function-like macro that expands to contain __LINE__. 1272 // Skip down through expansion points until we find a file loc for the 1273 // end of the expansion history. 1274 Loc = SourceMgr.getExpansionRange(Loc).second; 1275 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc); 1276 1277 // __LINE__ expands to a simple numeric value. 1278 OS << (PLoc.isValid()? PLoc.getLine() : 1); 1279 Tok.setKind(tok::numeric_constant); 1280 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) { 1281 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a 1282 // character string literal)". This can be affected by #line. 1283 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); 1284 1285 // __BASE_FILE__ is a GNU extension that returns the top of the presumed 1286 // #include stack instead of the current file. 1287 if (II == Ident__BASE_FILE__ && PLoc.isValid()) { 1288 SourceLocation NextLoc = PLoc.getIncludeLoc(); 1289 while (NextLoc.isValid()) { 1290 PLoc = SourceMgr.getPresumedLoc(NextLoc); 1291 if (PLoc.isInvalid()) 1292 break; 1293 1294 NextLoc = PLoc.getIncludeLoc(); 1295 } 1296 } 1297 1298 // Escape this filename. Turn '\' -> '\\' '"' -> '\"' 1299 SmallString<128> FN; 1300 if (PLoc.isValid()) { 1301 FN += PLoc.getFilename(); 1302 Lexer::Stringify(FN); 1303 OS << '"' << FN.str() << '"'; 1304 } 1305 Tok.setKind(tok::string_literal); 1306 } else if (II == Ident__DATE__) { 1307 if (!DATELoc.isValid()) 1308 ComputeDATE_TIME(DATELoc, TIMELoc, *this); 1309 Tok.setKind(tok::string_literal); 1310 Tok.setLength(strlen("\"Mmm dd yyyy\"")); 1311 Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(), 1312 Tok.getLocation(), 1313 Tok.getLength())); 1314 return; 1315 } else if (II == Ident__TIME__) { 1316 if (!TIMELoc.isValid()) 1317 ComputeDATE_TIME(DATELoc, TIMELoc, *this); 1318 Tok.setKind(tok::string_literal); 1319 Tok.setLength(strlen("\"hh:mm:ss\"")); 1320 Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(), 1321 Tok.getLocation(), 1322 Tok.getLength())); 1323 return; 1324 } else if (II == Ident__INCLUDE_LEVEL__) { 1325 // Compute the presumed include depth of this token. This can be affected 1326 // by GNU line markers. 1327 unsigned Depth = 0; 1328 1329 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); 1330 if (PLoc.isValid()) { 1331 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc()); 1332 for (; PLoc.isValid(); ++Depth) 1333 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc()); 1334 } 1335 1336 // __INCLUDE_LEVEL__ expands to a simple numeric value. 1337 OS << Depth; 1338 Tok.setKind(tok::numeric_constant); 1339 } else if (II == Ident__TIMESTAMP__) { 1340 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be 1341 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime. 1342 1343 // Get the file that we are lexing out of. If we're currently lexing from 1344 // a macro, dig into the include stack. 1345 const FileEntry *CurFile = 0; 1346 PreprocessorLexer *TheLexer = getCurrentFileLexer(); 1347 1348 if (TheLexer) 1349 CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID()); 1350 1351 const char *Result; 1352 if (CurFile) { 1353 time_t TT = CurFile->getModificationTime(); 1354 struct tm *TM = localtime(&TT); 1355 Result = asctime(TM); 1356 } else { 1357 Result = "??? ??? ?? ??:??:?? ????\n"; 1358 } 1359 // Surround the string with " and strip the trailing newline. 1360 OS << '"' << StringRef(Result, strlen(Result)-1) << '"'; 1361 Tok.setKind(tok::string_literal); 1362 } else if (II == Ident__COUNTER__) { 1363 // __COUNTER__ expands to a simple numeric value. 1364 OS << CounterValue++; 1365 Tok.setKind(tok::numeric_constant); 1366 } else if (II == Ident__has_feature || 1367 II == Ident__has_extension || 1368 II == Ident__has_builtin || 1369 II == Ident__has_attribute) { 1370 // The argument to these builtins should be a parenthesized identifier. 1371 SourceLocation StartLoc = Tok.getLocation(); 1372 1373 bool IsValid = false; 1374 IdentifierInfo *FeatureII = 0; 1375 1376 // Read the '('. 1377 LexUnexpandedToken(Tok); 1378 if (Tok.is(tok::l_paren)) { 1379 // Read the identifier 1380 LexUnexpandedToken(Tok); 1381 if ((FeatureII = Tok.getIdentifierInfo())) { 1382 // Read the ')'. 1383 LexUnexpandedToken(Tok); 1384 if (Tok.is(tok::r_paren)) 1385 IsValid = true; 1386 } 1387 } 1388 1389 bool Value = false; 1390 if (!IsValid) 1391 Diag(StartLoc, diag::err_feature_check_malformed); 1392 else if (II == Ident__has_builtin) { 1393 // Check for a builtin is trivial. 1394 Value = FeatureII->getBuiltinID() != 0; 1395 } else if (II == Ident__has_attribute) 1396 Value = HasAttribute(FeatureII); 1397 else if (II == Ident__has_extension) 1398 Value = HasExtension(*this, FeatureII); 1399 else { 1400 assert(II == Ident__has_feature && "Must be feature check"); 1401 Value = HasFeature(*this, FeatureII); 1402 } 1403 1404 OS << (int)Value; 1405 if (IsValid) 1406 Tok.setKind(tok::numeric_constant); 1407 } else if (II == Ident__has_include || 1408 II == Ident__has_include_next) { 1409 // The argument to these two builtins should be a parenthesized 1410 // file name string literal using angle brackets (<>) or 1411 // double-quotes (""). 1412 bool Value; 1413 if (II == Ident__has_include) 1414 Value = EvaluateHasInclude(Tok, II, *this); 1415 else 1416 Value = EvaluateHasIncludeNext(Tok, II, *this); 1417 OS << (int)Value; 1418 if (Tok.is(tok::r_paren)) 1419 Tok.setKind(tok::numeric_constant); 1420 } else if (II == Ident__has_warning) { 1421 // The argument should be a parenthesized string literal. 1422 // The argument to these builtins should be a parenthesized identifier. 1423 SourceLocation StartLoc = Tok.getLocation(); 1424 bool IsValid = false; 1425 bool Value = false; 1426 // Read the '('. 1427 LexUnexpandedToken(Tok); 1428 do { 1429 if (Tok.isNot(tok::l_paren)) { 1430 Diag(StartLoc, diag::err_warning_check_malformed); 1431 break; 1432 } 1433 1434 LexUnexpandedToken(Tok); 1435 std::string WarningName; 1436 SourceLocation StrStartLoc = Tok.getLocation(); 1437 if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'", 1438 /*MacroExpansion=*/false)) { 1439 // Eat tokens until ')'. 1440 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eod) && 1441 Tok.isNot(tok::eof)) 1442 LexUnexpandedToken(Tok); 1443 break; 1444 } 1445 1446 // Is the end a ')'? 1447 if (!(IsValid = Tok.is(tok::r_paren))) { 1448 Diag(StartLoc, diag::err_warning_check_malformed); 1449 break; 1450 } 1451 1452 if (WarningName.size() < 3 || WarningName[0] != '-' || 1453 WarningName[1] != 'W') { 1454 Diag(StrStartLoc, diag::warn_has_warning_invalid_option); 1455 break; 1456 } 1457 1458 // Finally, check if the warning flags maps to a diagnostic group. 1459 // We construct a SmallVector here to talk to getDiagnosticIDs(). 1460 // Although we don't use the result, this isn't a hot path, and not 1461 // worth special casing. 1462 SmallVector<diag::kind, 10> Diags; 1463 Value = !getDiagnostics().getDiagnosticIDs()-> 1464 getDiagnosticsInGroup(WarningName.substr(2), Diags); 1465 } while (false); 1466 1467 OS << (int)Value; 1468 if (IsValid) 1469 Tok.setKind(tok::numeric_constant); 1470 } else if (II == Ident__building_module) { 1471 // The argument to this builtin should be an identifier. The 1472 // builtin evaluates to 1 when that identifier names the module we are 1473 // currently building. 1474 OS << (int)EvaluateBuildingModule(Tok, II, *this); 1475 Tok.setKind(tok::numeric_constant); 1476 } else if (II == Ident__MODULE__) { 1477 // The current module as an identifier. 1478 OS << getLangOpts().CurrentModule; 1479 IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule); 1480 Tok.setIdentifierInfo(ModuleII); 1481 Tok.setKind(ModuleII->getTokenID()); 1482 } else { 1483 llvm_unreachable("Unknown identifier!"); 1484 } 1485 CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation()); 1486} 1487 1488void Preprocessor::markMacroAsUsed(MacroInfo *MI) { 1489 // If the 'used' status changed, and the macro requires 'unused' warning, 1490 // remove its SourceLocation from the warn-for-unused-macro locations. 1491 if (MI->isWarnIfUnused() && !MI->isUsed()) 1492 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); 1493 MI->setIsUsed(true); 1494} 1495