Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame^] | 1 | //===--- MacroExpansion.cpp - Top level Macro Expansion -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the top level handling of macro expasion for the |
| 11 | // preprocessor. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Lex/Preprocessor.h" |
| 16 | #include "MacroArgs.h" |
| 17 | #include "clang/Lex/MacroInfo.h" |
| 18 | #include "clang/Basic/SourceManager.h" |
| 19 | #include "clang/Basic/FileManager.h" |
| 20 | #include "clang/Basic/TargetInfo.h" |
| 21 | #include "clang/Lex/LexDiagnostic.h" |
| 22 | #include "clang/Lex/CodeCompletionHandler.h" |
| 23 | #include "clang/Lex/ExternalPreprocessorSource.h" |
| 24 | #include "clang/Lex/LiteralSupport.h" |
| 25 | #include "llvm/ADT/StringSwitch.h" |
| 26 | #include "llvm/ADT/STLExtras.h" |
| 27 | #include "llvm/Config/llvm-config.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
| 29 | #include "llvm/Support/ErrorHandling.h" |
| 30 | #include <cstdio> |
| 31 | #include <ctime> |
| 32 | using namespace clang; |
| 33 | |
| 34 | MacroInfo *Preprocessor::getInfoForMacro(IdentifierInfo *II) const { |
| 35 | assert(II->hasMacroDefinition() && "Identifier is not a macro!"); |
| 36 | |
| 37 | macro_iterator Pos = Macros.find(II); |
| 38 | if (Pos == Macros.end()) { |
| 39 | // Load this macro from the external source. |
| 40 | getExternalSource()->LoadMacroDefinition(II); |
| 41 | Pos = Macros.find(II); |
| 42 | } |
| 43 | assert(Pos != Macros.end() && "Identifier macro info is missing!"); |
| 44 | assert(Pos->second->getUndefLoc().isInvalid() && "Macro is undefined!"); |
| 45 | return Pos->second; |
| 46 | } |
| 47 | |
| 48 | /// setMacroInfo - Specify a macro for this identifier. |
| 49 | /// |
| 50 | void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI, |
| 51 | bool LoadedFromAST) { |
| 52 | assert(MI && "MacroInfo should be non-zero!"); |
| 53 | MI->setPreviousDefinition(Macros[II]); |
| 54 | Macros[II] = MI; |
| 55 | II->setHasMacroDefinition(true); |
| 56 | if (II->isFromAST() && !LoadedFromAST) |
| 57 | II->setChangedSinceDeserialization(); |
| 58 | } |
| 59 | |
| 60 | /// \brief Undefine a macro for this identifier. |
| 61 | void Preprocessor::clearMacroInfo(IdentifierInfo *II) { |
| 62 | assert(II->hasMacroDefinition() && "Macro is not defined!"); |
| 63 | assert(Macros[II]->getUndefLoc().isValid() && "Macro is still defined!"); |
| 64 | II->setHasMacroDefinition(false); |
| 65 | if (II->isFromAST()) |
| 66 | II->setChangedSinceDeserialization(); |
| 67 | } |
| 68 | |
| 69 | /// RegisterBuiltinMacro - Register the specified identifier in the identifier |
| 70 | /// table and mark it as a builtin macro to be expanded. |
| 71 | static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){ |
| 72 | // Get the identifier. |
| 73 | IdentifierInfo *Id = PP.getIdentifierInfo(Name); |
| 74 | |
| 75 | // Mark it as being a macro that is builtin. |
| 76 | MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation()); |
| 77 | MI->setIsBuiltinMacro(); |
| 78 | PP.setMacroInfo(Id, MI); |
| 79 | return Id; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the |
| 84 | /// identifier table. |
| 85 | void Preprocessor::RegisterBuiltinMacros() { |
| 86 | Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__"); |
| 87 | Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__"); |
| 88 | Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__"); |
| 89 | Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__"); |
| 90 | Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__"); |
| 91 | Ident_Pragma = RegisterBuiltinMacro(*this, "_Pragma"); |
| 92 | |
| 93 | // GCC Extensions. |
| 94 | Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__"); |
| 95 | Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__"); |
| 96 | Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__"); |
| 97 | |
| 98 | // Clang Extensions. |
| 99 | Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature"); |
| 100 | Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension"); |
| 101 | Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin"); |
| 102 | Ident__has_attribute = RegisterBuiltinMacro(*this, "__has_attribute"); |
| 103 | Ident__has_include = RegisterBuiltinMacro(*this, "__has_include"); |
| 104 | Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next"); |
| 105 | Ident__has_warning = RegisterBuiltinMacro(*this, "__has_warning"); |
| 106 | |
| 107 | // Microsoft Extensions. |
| 108 | if (LangOpts.MicrosoftExt) |
| 109 | Ident__pragma = RegisterBuiltinMacro(*this, "__pragma"); |
| 110 | else |
| 111 | Ident__pragma = 0; |
| 112 | } |
| 113 | |
| 114 | /// isTrivialSingleTokenExpansion - Return true if MI, which has a single token |
| 115 | /// in its expansion, currently expands to that token literally. |
| 116 | static bool isTrivialSingleTokenExpansion(const MacroInfo *MI, |
| 117 | const IdentifierInfo *MacroIdent, |
| 118 | Preprocessor &PP) { |
| 119 | IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo(); |
| 120 | |
| 121 | // If the token isn't an identifier, it's always literally expanded. |
| 122 | if (II == 0) return true; |
| 123 | |
| 124 | // If the information about this identifier is out of date, update it from |
| 125 | // the external source. |
| 126 | if (II->isOutOfDate()) |
| 127 | PP.getExternalSource()->updateOutOfDateIdentifier(*II); |
| 128 | |
| 129 | // If the identifier is a macro, and if that macro is enabled, it may be |
| 130 | // expanded so it's not a trivial expansion. |
| 131 | if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() && |
| 132 | // Fast expanding "#define X X" is ok, because X would be disabled. |
| 133 | II != MacroIdent) |
| 134 | return false; |
| 135 | |
| 136 | // If this is an object-like macro invocation, it is safe to trivially expand |
| 137 | // it. |
| 138 | if (MI->isObjectLike()) return true; |
| 139 | |
| 140 | // If this is a function-like macro invocation, it's safe to trivially expand |
| 141 | // as long as the identifier is not a macro argument. |
| 142 | for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); |
| 143 | I != E; ++I) |
| 144 | if (*I == II) |
| 145 | return false; // Identifier is a macro argument. |
| 146 | |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | /// isNextPPTokenLParen - Determine whether the next preprocessor token to be |
| 152 | /// lexed is a '('. If so, consume the token and return true, if not, this |
| 153 | /// method should have no observable side-effect on the lexed tokens. |
| 154 | bool Preprocessor::isNextPPTokenLParen() { |
| 155 | // Do some quick tests for rejection cases. |
| 156 | unsigned Val; |
| 157 | if (CurLexer) |
| 158 | Val = CurLexer->isNextPPTokenLParen(); |
| 159 | else if (CurPTHLexer) |
| 160 | Val = CurPTHLexer->isNextPPTokenLParen(); |
| 161 | else |
| 162 | Val = CurTokenLexer->isNextTokenLParen(); |
| 163 | |
| 164 | if (Val == 2) { |
| 165 | // We have run off the end. If it's a source file we don't |
| 166 | // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the |
| 167 | // macro stack. |
| 168 | if (CurPPLexer) |
| 169 | return false; |
| 170 | for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { |
| 171 | IncludeStackInfo &Entry = IncludeMacroStack[i-1]; |
| 172 | if (Entry.TheLexer) |
| 173 | Val = Entry.TheLexer->isNextPPTokenLParen(); |
| 174 | else if (Entry.ThePTHLexer) |
| 175 | Val = Entry.ThePTHLexer->isNextPPTokenLParen(); |
| 176 | else |
| 177 | Val = Entry.TheTokenLexer->isNextTokenLParen(); |
| 178 | |
| 179 | if (Val != 2) |
| 180 | break; |
| 181 | |
| 182 | // Ran off the end of a source file? |
| 183 | if (Entry.ThePPLexer) |
| 184 | return false; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Okay, if we know that the token is a '(', lex it and return. Otherwise we |
| 189 | // have found something that isn't a '(' or we found the end of the |
| 190 | // translation unit. In either case, return false. |
| 191 | return Val == 1; |
| 192 | } |
| 193 | |
| 194 | /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be |
| 195 | /// expanded as a macro, handle it and return the next token as 'Identifier'. |
| 196 | bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier, |
| 197 | MacroInfo *MI) { |
| 198 | // If this is a macro expansion in the "#if !defined(x)" line for the file, |
| 199 | // then the macro could expand to different things in other contexts, we need |
| 200 | // to disable the optimization in this case. |
| 201 | if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro(); |
| 202 | |
| 203 | // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially. |
| 204 | if (MI->isBuiltinMacro()) { |
| 205 | if (Callbacks) Callbacks->MacroExpands(Identifier, MI, |
| 206 | Identifier.getLocation()); |
| 207 | ExpandBuiltinMacro(Identifier); |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | /// Args - If this is a function-like macro expansion, this contains, |
| 212 | /// for each macro argument, the list of tokens that were provided to the |
| 213 | /// invocation. |
| 214 | MacroArgs *Args = 0; |
| 215 | |
| 216 | // Remember where the end of the expansion occurred. For an object-like |
| 217 | // macro, this is the identifier. For a function-like macro, this is the ')'. |
| 218 | SourceLocation ExpansionEnd = Identifier.getLocation(); |
| 219 | |
| 220 | // If this is a function-like macro, read the arguments. |
| 221 | if (MI->isFunctionLike()) { |
| 222 | // C99 6.10.3p10: If the preprocessing token immediately after the macro |
| 223 | // name isn't a '(', this macro should not be expanded. |
| 224 | if (!isNextPPTokenLParen()) |
| 225 | return true; |
| 226 | |
| 227 | // Remember that we are now parsing the arguments to a macro invocation. |
| 228 | // Preprocessor directives used inside macro arguments are not portable, and |
| 229 | // this enables the warning. |
| 230 | InMacroArgs = true; |
| 231 | Args = ReadFunctionLikeMacroArgs(Identifier, MI, ExpansionEnd); |
| 232 | |
| 233 | // Finished parsing args. |
| 234 | InMacroArgs = false; |
| 235 | |
| 236 | // If there was an error parsing the arguments, bail out. |
| 237 | if (Args == 0) return false; |
| 238 | |
| 239 | ++NumFnMacroExpanded; |
| 240 | } else { |
| 241 | ++NumMacroExpanded; |
| 242 | } |
| 243 | |
| 244 | // Notice that this macro has been used. |
| 245 | markMacroAsUsed(MI); |
| 246 | |
| 247 | // Remember where the token is expanded. |
| 248 | SourceLocation ExpandLoc = Identifier.getLocation(); |
| 249 | SourceRange ExpansionRange(ExpandLoc, ExpansionEnd); |
| 250 | |
| 251 | if (Callbacks) { |
| 252 | if (InMacroArgs) { |
| 253 | // We can have macro expansion inside a conditional directive while |
| 254 | // reading the function macro arguments. To ensure, in that case, that |
| 255 | // MacroExpands callbacks still happen in source order, queue this |
| 256 | // callback to have it happen after the function macro callback. |
| 257 | DelayedMacroExpandsCallbacks.push_back( |
| 258 | MacroExpandsInfo(Identifier, MI, ExpansionRange)); |
| 259 | } else { |
| 260 | Callbacks->MacroExpands(Identifier, MI, ExpansionRange); |
| 261 | if (!DelayedMacroExpandsCallbacks.empty()) { |
| 262 | for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) { |
| 263 | MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i]; |
| 264 | Callbacks->MacroExpands(Info.Tok, Info.MI, Info.Range); |
| 265 | } |
| 266 | DelayedMacroExpandsCallbacks.clear(); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // If we started lexing a macro, enter the macro expansion body. |
| 272 | |
| 273 | // If this macro expands to no tokens, don't bother to push it onto the |
| 274 | // expansion stack, only to take it right back off. |
| 275 | if (MI->getNumTokens() == 0) { |
| 276 | // No need for arg info. |
| 277 | if (Args) Args->destroy(*this); |
| 278 | |
| 279 | // Ignore this macro use, just return the next token in the current |
| 280 | // buffer. |
| 281 | bool HadLeadingSpace = Identifier.hasLeadingSpace(); |
| 282 | bool IsAtStartOfLine = Identifier.isAtStartOfLine(); |
| 283 | |
| 284 | Lex(Identifier); |
| 285 | |
| 286 | // If the identifier isn't on some OTHER line, inherit the leading |
| 287 | // whitespace/first-on-a-line property of this token. This handles |
| 288 | // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is |
| 289 | // empty. |
| 290 | if (!Identifier.isAtStartOfLine()) { |
| 291 | if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine); |
| 292 | if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace); |
| 293 | } |
| 294 | Identifier.setFlag(Token::LeadingEmptyMacro); |
| 295 | ++NumFastMacroExpanded; |
| 296 | return false; |
| 297 | |
| 298 | } else if (MI->getNumTokens() == 1 && |
| 299 | isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(), |
| 300 | *this)) { |
| 301 | // Otherwise, if this macro expands into a single trivially-expanded |
| 302 | // token: expand it now. This handles common cases like |
| 303 | // "#define VAL 42". |
| 304 | |
| 305 | // No need for arg info. |
| 306 | if (Args) Args->destroy(*this); |
| 307 | |
| 308 | // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro |
| 309 | // identifier to the expanded token. |
| 310 | bool isAtStartOfLine = Identifier.isAtStartOfLine(); |
| 311 | bool hasLeadingSpace = Identifier.hasLeadingSpace(); |
| 312 | |
| 313 | // Replace the result token. |
| 314 | Identifier = MI->getReplacementToken(0); |
| 315 | |
| 316 | // Restore the StartOfLine/LeadingSpace markers. |
| 317 | Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine); |
| 318 | Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace); |
| 319 | |
| 320 | // Update the tokens location to include both its expansion and physical |
| 321 | // locations. |
| 322 | SourceLocation Loc = |
| 323 | SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc, |
| 324 | ExpansionEnd,Identifier.getLength()); |
| 325 | Identifier.setLocation(Loc); |
| 326 | |
| 327 | // If this is a disabled macro or #define X X, we must mark the result as |
| 328 | // unexpandable. |
| 329 | if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) { |
| 330 | if (MacroInfo *NewMI = getMacroInfo(NewII)) |
| 331 | if (!NewMI->isEnabled() || NewMI == MI) { |
| 332 | Identifier.setFlag(Token::DisableExpand); |
| 333 | Diag(Identifier, diag::pp_disabled_macro_expansion); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // Since this is not an identifier token, it can't be macro expanded, so |
| 338 | // we're done. |
| 339 | ++NumFastMacroExpanded; |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | // Start expanding the macro. |
| 344 | EnterMacro(Identifier, ExpansionEnd, MI, Args); |
| 345 | |
| 346 | // Now that the macro is at the top of the include stack, ask the |
| 347 | // preprocessor to read the next token from it. |
| 348 | Lex(Identifier); |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | /// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next |
| 353 | /// token is the '(' of the macro, this method is invoked to read all of the |
| 354 | /// actual arguments specified for the macro invocation. This returns null on |
| 355 | /// error. |
| 356 | MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName, |
| 357 | MacroInfo *MI, |
| 358 | SourceLocation &MacroEnd) { |
| 359 | // The number of fixed arguments to parse. |
| 360 | unsigned NumFixedArgsLeft = MI->getNumArgs(); |
| 361 | bool isVariadic = MI->isVariadic(); |
| 362 | |
| 363 | // Outer loop, while there are more arguments, keep reading them. |
| 364 | Token Tok; |
| 365 | |
| 366 | // Read arguments as unexpanded tokens. This avoids issues, e.g., where |
| 367 | // an argument value in a macro could expand to ',' or '(' or ')'. |
| 368 | LexUnexpandedToken(Tok); |
| 369 | assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?"); |
| 370 | |
| 371 | // ArgTokens - Build up a list of tokens that make up each argument. Each |
| 372 | // argument is separated by an EOF token. Use a SmallVector so we can avoid |
| 373 | // heap allocations in the common case. |
| 374 | SmallVector<Token, 64> ArgTokens; |
| 375 | |
| 376 | unsigned NumActuals = 0; |
| 377 | while (Tok.isNot(tok::r_paren)) { |
| 378 | assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) && |
| 379 | "only expect argument separators here"); |
| 380 | |
| 381 | unsigned ArgTokenStart = ArgTokens.size(); |
| 382 | SourceLocation ArgStartLoc = Tok.getLocation(); |
| 383 | |
| 384 | // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note |
| 385 | // that we already consumed the first one. |
| 386 | unsigned NumParens = 0; |
| 387 | |
| 388 | while (1) { |
| 389 | // Read arguments as unexpanded tokens. This avoids issues, e.g., where |
| 390 | // an argument value in a macro could expand to ',' or '(' or ')'. |
| 391 | LexUnexpandedToken(Tok); |
| 392 | |
| 393 | if (Tok.is(tok::eof) || Tok.is(tok::eod)) { // "#if f(<eof>" & "#if f(\n" |
| 394 | Diag(MacroName, diag::err_unterm_macro_invoc); |
| 395 | // Do not lose the EOF/EOD. Return it to the client. |
| 396 | MacroName = Tok; |
| 397 | return 0; |
| 398 | } else if (Tok.is(tok::r_paren)) { |
| 399 | // If we found the ) token, the macro arg list is done. |
| 400 | if (NumParens-- == 0) { |
| 401 | MacroEnd = Tok.getLocation(); |
| 402 | break; |
| 403 | } |
| 404 | } else if (Tok.is(tok::l_paren)) { |
| 405 | ++NumParens; |
| 406 | // In Microsoft-compatibility mode, commas from nested macro expan- |
| 407 | // sions should not be considered as argument separators. We test |
| 408 | // for this with the IgnoredComma token flag. |
| 409 | } else if (Tok.is(tok::comma) |
| 410 | && !(Tok.getFlags() & Token::IgnoredComma) && NumParens == 0) { |
| 411 | // Comma ends this argument if there are more fixed arguments expected. |
| 412 | // However, if this is a variadic macro, and this is part of the |
| 413 | // variadic part, then the comma is just an argument token. |
| 414 | if (!isVariadic) break; |
| 415 | if (NumFixedArgsLeft > 1) |
| 416 | break; |
| 417 | } else if (Tok.is(tok::comment) && !KeepMacroComments) { |
| 418 | // If this is a comment token in the argument list and we're just in |
| 419 | // -C mode (not -CC mode), discard the comment. |
| 420 | continue; |
| 421 | } else if (Tok.getIdentifierInfo() != 0) { |
| 422 | // Reading macro arguments can cause macros that we are currently |
| 423 | // expanding from to be popped off the expansion stack. Doing so causes |
| 424 | // them to be reenabled for expansion. Here we record whether any |
| 425 | // identifiers we lex as macro arguments correspond to disabled macros. |
| 426 | // If so, we mark the token as noexpand. This is a subtle aspect of |
| 427 | // C99 6.10.3.4p2. |
| 428 | if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo())) |
| 429 | if (!MI->isEnabled()) |
| 430 | Tok.setFlag(Token::DisableExpand); |
| 431 | } else if (Tok.is(tok::code_completion)) { |
| 432 | if (CodeComplete) |
| 433 | CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(), |
| 434 | MI, NumActuals); |
| 435 | // Don't mark that we reached the code-completion point because the |
| 436 | // parser is going to handle the token and there will be another |
| 437 | // code-completion callback. |
| 438 | } |
| 439 | |
| 440 | ArgTokens.push_back(Tok); |
| 441 | } |
| 442 | |
| 443 | // If this was an empty argument list foo(), don't add this as an empty |
| 444 | // argument. |
| 445 | if (ArgTokens.empty() && Tok.getKind() == tok::r_paren) |
| 446 | break; |
| 447 | |
| 448 | // If this is not a variadic macro, and too many args were specified, emit |
| 449 | // an error. |
| 450 | if (!isVariadic && NumFixedArgsLeft == 0) { |
| 451 | if (ArgTokens.size() != ArgTokenStart) |
| 452 | ArgStartLoc = ArgTokens[ArgTokenStart].getLocation(); |
| 453 | |
| 454 | // Emit the diagnostic at the macro name in case there is a missing ). |
| 455 | // Emitting it at the , could be far away from the macro name. |
| 456 | Diag(ArgStartLoc, diag::err_too_many_args_in_macro_invoc); |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | // Empty arguments are standard in C99 and C++0x, and are supported as an extension in |
| 461 | // other modes. |
| 462 | if (ArgTokens.size() == ArgTokenStart && !LangOpts.C99) |
| 463 | Diag(Tok, LangOpts.CPlusPlus0x ? |
| 464 | diag::warn_cxx98_compat_empty_fnmacro_arg : |
| 465 | diag::ext_empty_fnmacro_arg); |
| 466 | |
| 467 | // Add a marker EOF token to the end of the token list for this argument. |
| 468 | Token EOFTok; |
| 469 | EOFTok.startToken(); |
| 470 | EOFTok.setKind(tok::eof); |
| 471 | EOFTok.setLocation(Tok.getLocation()); |
| 472 | EOFTok.setLength(0); |
| 473 | ArgTokens.push_back(EOFTok); |
| 474 | ++NumActuals; |
| 475 | assert(NumFixedArgsLeft != 0 && "Too many arguments parsed"); |
| 476 | --NumFixedArgsLeft; |
| 477 | } |
| 478 | |
| 479 | // Okay, we either found the r_paren. Check to see if we parsed too few |
| 480 | // arguments. |
| 481 | unsigned MinArgsExpected = MI->getNumArgs(); |
| 482 | |
| 483 | // See MacroArgs instance var for description of this. |
| 484 | bool isVarargsElided = false; |
| 485 | |
| 486 | if (NumActuals < MinArgsExpected) { |
| 487 | // There are several cases where too few arguments is ok, handle them now. |
| 488 | if (NumActuals == 0 && MinArgsExpected == 1) { |
| 489 | // #define A(X) or #define A(...) ---> A() |
| 490 | |
| 491 | // If there is exactly one argument, and that argument is missing, |
| 492 | // then we have an empty "()" argument empty list. This is fine, even if |
| 493 | // the macro expects one argument (the argument is just empty). |
| 494 | isVarargsElided = MI->isVariadic(); |
| 495 | } else if (MI->isVariadic() && |
| 496 | (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X) |
| 497 | (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A() |
| 498 | // Varargs where the named vararg parameter is missing: OK as extension. |
| 499 | // #define A(x, ...) |
| 500 | // A("blah") |
| 501 | Diag(Tok, diag::ext_missing_varargs_arg); |
| 502 | Diag(MI->getDefinitionLoc(), diag::note_macro_here) |
| 503 | << MacroName.getIdentifierInfo(); |
| 504 | |
| 505 | // Remember this occurred, allowing us to elide the comma when used for |
| 506 | // cases like: |
| 507 | // #define A(x, foo...) blah(a, ## foo) |
| 508 | // #define B(x, ...) blah(a, ## __VA_ARGS__) |
| 509 | // #define C(...) blah(a, ## __VA_ARGS__) |
| 510 | // A(x) B(x) C() |
| 511 | isVarargsElided = true; |
| 512 | } else { |
| 513 | // Otherwise, emit the error. |
| 514 | Diag(Tok, diag::err_too_few_args_in_macro_invoc); |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | // Add a marker EOF token to the end of the token list for this argument. |
| 519 | SourceLocation EndLoc = Tok.getLocation(); |
| 520 | Tok.startToken(); |
| 521 | Tok.setKind(tok::eof); |
| 522 | Tok.setLocation(EndLoc); |
| 523 | Tok.setLength(0); |
| 524 | ArgTokens.push_back(Tok); |
| 525 | |
| 526 | // If we expect two arguments, add both as empty. |
| 527 | if (NumActuals == 0 && MinArgsExpected == 2) |
| 528 | ArgTokens.push_back(Tok); |
| 529 | |
| 530 | } else if (NumActuals > MinArgsExpected && !MI->isVariadic()) { |
| 531 | // Emit the diagnostic at the macro name in case there is a missing ). |
| 532 | // Emitting it at the , could be far away from the macro name. |
| 533 | Diag(MacroName, diag::err_too_many_args_in_macro_invoc); |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this); |
| 538 | } |
| 539 | |
| 540 | /// \brief Keeps macro expanded tokens for TokenLexers. |
| 541 | // |
| 542 | /// Works like a stack; a TokenLexer adds the macro expanded tokens that is |
| 543 | /// going to lex in the cache and when it finishes the tokens are removed |
| 544 | /// from the end of the cache. |
| 545 | Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer, |
| 546 | ArrayRef<Token> tokens) { |
| 547 | assert(tokLexer); |
| 548 | if (tokens.empty()) |
| 549 | return 0; |
| 550 | |
| 551 | size_t newIndex = MacroExpandedTokens.size(); |
| 552 | bool cacheNeedsToGrow = tokens.size() > |
| 553 | MacroExpandedTokens.capacity()-MacroExpandedTokens.size(); |
| 554 | MacroExpandedTokens.append(tokens.begin(), tokens.end()); |
| 555 | |
| 556 | if (cacheNeedsToGrow) { |
| 557 | // Go through all the TokenLexers whose 'Tokens' pointer points in the |
| 558 | // buffer and update the pointers to the (potential) new buffer array. |
| 559 | for (unsigned i = 0, e = MacroExpandingLexersStack.size(); i != e; ++i) { |
| 560 | TokenLexer *prevLexer; |
| 561 | size_t tokIndex; |
| 562 | llvm::tie(prevLexer, tokIndex) = MacroExpandingLexersStack[i]; |
| 563 | prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex)); |
| 568 | return MacroExpandedTokens.data() + newIndex; |
| 569 | } |
| 570 | |
| 571 | void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() { |
| 572 | assert(!MacroExpandingLexersStack.empty()); |
| 573 | size_t tokIndex = MacroExpandingLexersStack.back().second; |
| 574 | assert(tokIndex < MacroExpandedTokens.size()); |
| 575 | // Pop the cached macro expanded tokens from the end. |
| 576 | MacroExpandedTokens.resize(tokIndex); |
| 577 | MacroExpandingLexersStack.pop_back(); |
| 578 | } |
| 579 | |
| 580 | /// ComputeDATE_TIME - Compute the current time, enter it into the specified |
| 581 | /// scratch buffer, then return DATELoc/TIMELoc locations with the position of |
| 582 | /// the identifier tokens inserted. |
| 583 | static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc, |
| 584 | Preprocessor &PP) { |
| 585 | time_t TT = time(0); |
| 586 | struct tm *TM = localtime(&TT); |
| 587 | |
| 588 | static const char * const Months[] = { |
| 589 | "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" |
| 590 | }; |
| 591 | |
| 592 | char TmpBuffer[32]; |
| 593 | #ifdef LLVM_ON_WIN32 |
| 594 | sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday, |
| 595 | TM->tm_year+1900); |
| 596 | #else |
| 597 | snprintf(TmpBuffer, sizeof(TmpBuffer), "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday, |
| 598 | TM->tm_year+1900); |
| 599 | #endif |
| 600 | |
| 601 | Token TmpTok; |
| 602 | TmpTok.startToken(); |
| 603 | PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok); |
| 604 | DATELoc = TmpTok.getLocation(); |
| 605 | |
| 606 | #ifdef LLVM_ON_WIN32 |
| 607 | sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec); |
| 608 | #else |
| 609 | snprintf(TmpBuffer, sizeof(TmpBuffer), "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec); |
| 610 | #endif |
| 611 | PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok); |
| 612 | TIMELoc = TmpTok.getLocation(); |
| 613 | } |
| 614 | |
| 615 | |
| 616 | /// HasFeature - Return true if we recognize and implement the feature |
| 617 | /// specified by the identifier as a standard language feature. |
| 618 | static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) { |
| 619 | const LangOptions &LangOpts = PP.getLangOpts(); |
| 620 | StringRef Feature = II->getName(); |
| 621 | |
| 622 | // Normalize the feature name, __foo__ becomes foo. |
| 623 | if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4) |
| 624 | Feature = Feature.substr(2, Feature.size() - 4); |
| 625 | |
| 626 | return llvm::StringSwitch<bool>(Feature) |
| 627 | .Case("address_sanitizer", LangOpts.AddressSanitizer) |
| 628 | .Case("attribute_analyzer_noreturn", true) |
| 629 | .Case("attribute_availability", true) |
| 630 | .Case("attribute_availability_with_message", true) |
| 631 | .Case("attribute_cf_returns_not_retained", true) |
| 632 | .Case("attribute_cf_returns_retained", true) |
| 633 | .Case("attribute_deprecated_with_message", true) |
| 634 | .Case("attribute_ext_vector_type", true) |
| 635 | .Case("attribute_ns_returns_not_retained", true) |
| 636 | .Case("attribute_ns_returns_retained", true) |
| 637 | .Case("attribute_ns_consumes_self", true) |
| 638 | .Case("attribute_ns_consumed", true) |
| 639 | .Case("attribute_cf_consumed", true) |
| 640 | .Case("attribute_objc_ivar_unused", true) |
| 641 | .Case("attribute_objc_method_family", true) |
| 642 | .Case("attribute_overloadable", true) |
| 643 | .Case("attribute_unavailable_with_message", true) |
| 644 | .Case("attribute_unused_on_fields", true) |
| 645 | .Case("blocks", LangOpts.Blocks) |
| 646 | .Case("cxx_exceptions", LangOpts.Exceptions) |
| 647 | .Case("cxx_rtti", LangOpts.RTTI) |
| 648 | .Case("enumerator_attributes", true) |
| 649 | // Objective-C features |
| 650 | .Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE? |
| 651 | .Case("objc_arc", LangOpts.ObjCAutoRefCount) |
| 652 | .Case("objc_arc_weak", LangOpts.ObjCARCWeak) |
| 653 | .Case("objc_default_synthesize_properties", LangOpts.ObjC2) |
| 654 | .Case("objc_fixed_enum", LangOpts.ObjC2) |
| 655 | .Case("objc_instancetype", LangOpts.ObjC2) |
| 656 | .Case("objc_modules", LangOpts.ObjC2 && LangOpts.Modules) |
| 657 | .Case("objc_nonfragile_abi", LangOpts.ObjCRuntime.isNonFragile()) |
| 658 | .Case("objc_weak_class", LangOpts.ObjCRuntime.hasWeakClassImport()) |
| 659 | .Case("ownership_holds", true) |
| 660 | .Case("ownership_returns", true) |
| 661 | .Case("ownership_takes", true) |
| 662 | .Case("objc_bool", true) |
| 663 | .Case("objc_subscripting", LangOpts.ObjCRuntime.isNonFragile()) |
| 664 | .Case("objc_array_literals", LangOpts.ObjC2) |
| 665 | .Case("objc_dictionary_literals", LangOpts.ObjC2) |
| 666 | .Case("objc_boxed_expressions", LangOpts.ObjC2) |
| 667 | .Case("arc_cf_code_audited", true) |
| 668 | // C11 features |
| 669 | .Case("c_alignas", LangOpts.C11) |
| 670 | .Case("c_atomic", LangOpts.C11) |
| 671 | .Case("c_generic_selections", LangOpts.C11) |
| 672 | .Case("c_static_assert", LangOpts.C11) |
| 673 | // C++11 features |
| 674 | .Case("cxx_access_control_sfinae", LangOpts.CPlusPlus0x) |
| 675 | .Case("cxx_alias_templates", LangOpts.CPlusPlus0x) |
| 676 | .Case("cxx_alignas", LangOpts.CPlusPlus0x) |
| 677 | .Case("cxx_atomic", LangOpts.CPlusPlus0x) |
| 678 | .Case("cxx_attributes", LangOpts.CPlusPlus0x) |
| 679 | .Case("cxx_auto_type", LangOpts.CPlusPlus0x) |
| 680 | .Case("cxx_constexpr", LangOpts.CPlusPlus0x) |
| 681 | .Case("cxx_decltype", LangOpts.CPlusPlus0x) |
| 682 | .Case("cxx_decltype_incomplete_return_types", LangOpts.CPlusPlus0x) |
| 683 | .Case("cxx_default_function_template_args", LangOpts.CPlusPlus0x) |
| 684 | .Case("cxx_defaulted_functions", LangOpts.CPlusPlus0x) |
| 685 | .Case("cxx_delegating_constructors", LangOpts.CPlusPlus0x) |
| 686 | .Case("cxx_deleted_functions", LangOpts.CPlusPlus0x) |
| 687 | .Case("cxx_explicit_conversions", LangOpts.CPlusPlus0x) |
| 688 | .Case("cxx_generalized_initializers", LangOpts.CPlusPlus0x) |
| 689 | .Case("cxx_implicit_moves", LangOpts.CPlusPlus0x) |
| 690 | //.Case("cxx_inheriting_constructors", false) |
| 691 | .Case("cxx_inline_namespaces", LangOpts.CPlusPlus0x) |
| 692 | .Case("cxx_lambdas", LangOpts.CPlusPlus0x) |
| 693 | .Case("cxx_local_type_template_args", LangOpts.CPlusPlus0x) |
| 694 | .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus0x) |
| 695 | .Case("cxx_noexcept", LangOpts.CPlusPlus0x) |
| 696 | .Case("cxx_nullptr", LangOpts.CPlusPlus0x) |
| 697 | .Case("cxx_override_control", LangOpts.CPlusPlus0x) |
| 698 | .Case("cxx_range_for", LangOpts.CPlusPlus0x) |
| 699 | .Case("cxx_raw_string_literals", LangOpts.CPlusPlus0x) |
| 700 | .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus0x) |
| 701 | .Case("cxx_rvalue_references", LangOpts.CPlusPlus0x) |
| 702 | .Case("cxx_strong_enums", LangOpts.CPlusPlus0x) |
| 703 | .Case("cxx_static_assert", LangOpts.CPlusPlus0x) |
| 704 | .Case("cxx_trailing_return", LangOpts.CPlusPlus0x) |
| 705 | .Case("cxx_unicode_literals", LangOpts.CPlusPlus0x) |
| 706 | .Case("cxx_unrestricted_unions", LangOpts.CPlusPlus0x) |
| 707 | .Case("cxx_user_literals", LangOpts.CPlusPlus0x) |
| 708 | .Case("cxx_variadic_templates", LangOpts.CPlusPlus0x) |
| 709 | // Type traits |
| 710 | .Case("has_nothrow_assign", LangOpts.CPlusPlus) |
| 711 | .Case("has_nothrow_copy", LangOpts.CPlusPlus) |
| 712 | .Case("has_nothrow_constructor", LangOpts.CPlusPlus) |
| 713 | .Case("has_trivial_assign", LangOpts.CPlusPlus) |
| 714 | .Case("has_trivial_copy", LangOpts.CPlusPlus) |
| 715 | .Case("has_trivial_constructor", LangOpts.CPlusPlus) |
| 716 | .Case("has_trivial_destructor", LangOpts.CPlusPlus) |
| 717 | .Case("has_virtual_destructor", LangOpts.CPlusPlus) |
| 718 | .Case("is_abstract", LangOpts.CPlusPlus) |
| 719 | .Case("is_base_of", LangOpts.CPlusPlus) |
| 720 | .Case("is_class", LangOpts.CPlusPlus) |
| 721 | .Case("is_convertible_to", LangOpts.CPlusPlus) |
| 722 | // __is_empty is available only if the horrible |
| 723 | // "struct __is_empty" parsing hack hasn't been needed in this |
| 724 | // translation unit. If it has, __is_empty reverts to a normal |
| 725 | // identifier and __has_feature(is_empty) evaluates false. |
| 726 | .Case("is_empty", LangOpts.CPlusPlus) |
| 727 | .Case("is_enum", LangOpts.CPlusPlus) |
| 728 | .Case("is_final", LangOpts.CPlusPlus) |
| 729 | .Case("is_literal", LangOpts.CPlusPlus) |
| 730 | .Case("is_standard_layout", LangOpts.CPlusPlus) |
| 731 | .Case("is_pod", LangOpts.CPlusPlus) |
| 732 | .Case("is_polymorphic", LangOpts.CPlusPlus) |
| 733 | .Case("is_trivial", LangOpts.CPlusPlus) |
| 734 | .Case("is_trivially_assignable", LangOpts.CPlusPlus) |
| 735 | .Case("is_trivially_constructible", LangOpts.CPlusPlus) |
| 736 | .Case("is_trivially_copyable", LangOpts.CPlusPlus) |
| 737 | .Case("is_union", LangOpts.CPlusPlus) |
| 738 | .Case("modules", LangOpts.Modules) |
| 739 | .Case("tls", PP.getTargetInfo().isTLSSupported()) |
| 740 | .Case("underlying_type", LangOpts.CPlusPlus) |
| 741 | .Default(false); |
| 742 | } |
| 743 | |
| 744 | /// HasExtension - Return true if we recognize and implement the feature |
| 745 | /// specified by the identifier, either as an extension or a standard language |
| 746 | /// feature. |
| 747 | static bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) { |
| 748 | if (HasFeature(PP, II)) |
| 749 | return true; |
| 750 | |
| 751 | // If the use of an extension results in an error diagnostic, extensions are |
| 752 | // effectively unavailable, so just return false here. |
| 753 | if (PP.getDiagnostics().getExtensionHandlingBehavior() == |
| 754 | DiagnosticsEngine::Ext_Error) |
| 755 | return false; |
| 756 | |
| 757 | const LangOptions &LangOpts = PP.getLangOpts(); |
| 758 | StringRef Extension = II->getName(); |
| 759 | |
| 760 | // Normalize the extension name, __foo__ becomes foo. |
| 761 | if (Extension.startswith("__") && Extension.endswith("__") && |
| 762 | Extension.size() >= 4) |
| 763 | Extension = Extension.substr(2, Extension.size() - 4); |
| 764 | |
| 765 | // Because we inherit the feature list from HasFeature, this string switch |
| 766 | // must be less restrictive than HasFeature's. |
| 767 | return llvm::StringSwitch<bool>(Extension) |
| 768 | // C11 features supported by other languages as extensions. |
| 769 | .Case("c_alignas", true) |
| 770 | .Case("c_atomic", true) |
| 771 | .Case("c_generic_selections", true) |
| 772 | .Case("c_static_assert", true) |
| 773 | // C++0x features supported by other languages as extensions. |
| 774 | .Case("cxx_atomic", LangOpts.CPlusPlus) |
| 775 | .Case("cxx_deleted_functions", LangOpts.CPlusPlus) |
| 776 | .Case("cxx_explicit_conversions", LangOpts.CPlusPlus) |
| 777 | .Case("cxx_inline_namespaces", LangOpts.CPlusPlus) |
| 778 | .Case("cxx_local_type_template_args", LangOpts.CPlusPlus) |
| 779 | .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus) |
| 780 | .Case("cxx_override_control", LangOpts.CPlusPlus) |
| 781 | .Case("cxx_range_for", LangOpts.CPlusPlus) |
| 782 | .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus) |
| 783 | .Case("cxx_rvalue_references", LangOpts.CPlusPlus) |
| 784 | .Default(false); |
| 785 | } |
| 786 | |
| 787 | /// HasAttribute - Return true if we recognize and implement the attribute |
| 788 | /// specified by the given identifier. |
| 789 | static bool HasAttribute(const IdentifierInfo *II) { |
| 790 | StringRef Name = II->getName(); |
| 791 | // Normalize the attribute name, __foo__ becomes foo. |
| 792 | if (Name.startswith("__") && Name.endswith("__") && Name.size() >= 4) |
| 793 | Name = Name.substr(2, Name.size() - 4); |
| 794 | |
| 795 | // FIXME: Do we need to handle namespaces here? |
| 796 | return llvm::StringSwitch<bool>(Name) |
| 797 | #include "clang/Lex/AttrSpellings.inc" |
| 798 | .Default(false); |
| 799 | } |
| 800 | |
| 801 | /// EvaluateHasIncludeCommon - Process a '__has_include("path")' |
| 802 | /// or '__has_include_next("path")' expression. |
| 803 | /// Returns true if successful. |
| 804 | static bool EvaluateHasIncludeCommon(Token &Tok, |
| 805 | IdentifierInfo *II, Preprocessor &PP, |
| 806 | const DirectoryLookup *LookupFrom) { |
| 807 | SourceLocation LParenLoc; |
| 808 | |
| 809 | // Get '('. |
| 810 | PP.LexNonComment(Tok); |
| 811 | |
| 812 | // Ensure we have a '('. |
| 813 | if (Tok.isNot(tok::l_paren)) { |
| 814 | PP.Diag(Tok.getLocation(), diag::err_pp_missing_lparen) << II->getName(); |
| 815 | return false; |
| 816 | } |
| 817 | |
| 818 | // Save '(' location for possible missing ')' message. |
| 819 | LParenLoc = Tok.getLocation(); |
| 820 | |
| 821 | // Get the file name. |
| 822 | PP.getCurrentLexer()->LexIncludeFilename(Tok); |
| 823 | |
| 824 | // Reserve a buffer to get the spelling. |
| 825 | SmallString<128> FilenameBuffer; |
| 826 | StringRef Filename; |
| 827 | SourceLocation EndLoc; |
| 828 | |
| 829 | switch (Tok.getKind()) { |
| 830 | case tok::eod: |
| 831 | // If the token kind is EOD, the error has already been diagnosed. |
| 832 | return false; |
| 833 | |
| 834 | case tok::angle_string_literal: |
| 835 | case tok::string_literal: { |
| 836 | bool Invalid = false; |
| 837 | Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid); |
| 838 | if (Invalid) |
| 839 | return false; |
| 840 | break; |
| 841 | } |
| 842 | |
| 843 | case tok::less: |
| 844 | // This could be a <foo/bar.h> file coming from a macro expansion. In this |
| 845 | // case, glue the tokens together into FilenameBuffer and interpret those. |
| 846 | FilenameBuffer.push_back('<'); |
| 847 | if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) |
| 848 | return false; // Found <eod> but no ">"? Diagnostic already emitted. |
| 849 | Filename = FilenameBuffer.str(); |
| 850 | break; |
| 851 | default: |
| 852 | PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename); |
| 853 | return false; |
| 854 | } |
| 855 | |
| 856 | // Get ')'. |
| 857 | PP.LexNonComment(Tok); |
| 858 | |
| 859 | // Ensure we have a trailing ). |
| 860 | if (Tok.isNot(tok::r_paren)) { |
| 861 | PP.Diag(Tok.getLocation(), diag::err_pp_missing_rparen) << II->getName(); |
| 862 | PP.Diag(LParenLoc, diag::note_matching) << "("; |
| 863 | return false; |
| 864 | } |
| 865 | |
| 866 | bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename); |
| 867 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 868 | // error. |
| 869 | if (Filename.empty()) |
| 870 | return false; |
| 871 | |
| 872 | // Search include directories. |
| 873 | const DirectoryLookup *CurDir; |
| 874 | const FileEntry *File = |
| 875 | PP.LookupFile(Filename, isAngled, LookupFrom, CurDir, NULL, NULL, NULL); |
| 876 | |
| 877 | // Get the result value. A result of true means the file exists. |
| 878 | return File != 0; |
| 879 | } |
| 880 | |
| 881 | /// EvaluateHasInclude - Process a '__has_include("path")' expression. |
| 882 | /// Returns true if successful. |
| 883 | static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II, |
| 884 | Preprocessor &PP) { |
| 885 | return EvaluateHasIncludeCommon(Tok, II, PP, NULL); |
| 886 | } |
| 887 | |
| 888 | /// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression. |
| 889 | /// Returns true if successful. |
| 890 | static bool EvaluateHasIncludeNext(Token &Tok, |
| 891 | IdentifierInfo *II, Preprocessor &PP) { |
| 892 | // __has_include_next is like __has_include, except that we start |
| 893 | // searching after the current found directory. If we can't do this, |
| 894 | // issue a diagnostic. |
| 895 | const DirectoryLookup *Lookup = PP.GetCurDirLookup(); |
| 896 | if (PP.isInPrimaryFile()) { |
| 897 | Lookup = 0; |
| 898 | PP.Diag(Tok, diag::pp_include_next_in_primary); |
| 899 | } else if (Lookup == 0) { |
| 900 | PP.Diag(Tok, diag::pp_include_next_absolute_path); |
| 901 | } else { |
| 902 | // Start looking up in the next directory. |
| 903 | ++Lookup; |
| 904 | } |
| 905 | |
| 906 | return EvaluateHasIncludeCommon(Tok, II, PP, Lookup); |
| 907 | } |
| 908 | |
| 909 | /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded |
| 910 | /// as a builtin macro, handle it and return the next token as 'Tok'. |
| 911 | void Preprocessor::ExpandBuiltinMacro(Token &Tok) { |
| 912 | // Figure out which token this is. |
| 913 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 914 | assert(II && "Can't be a macro without id info!"); |
| 915 | |
| 916 | // If this is an _Pragma or Microsoft __pragma directive, expand it, |
| 917 | // invoke the pragma handler, then lex the token after it. |
| 918 | if (II == Ident_Pragma) |
| 919 | return Handle_Pragma(Tok); |
| 920 | else if (II == Ident__pragma) // in non-MS mode this is null |
| 921 | return HandleMicrosoft__pragma(Tok); |
| 922 | |
| 923 | ++NumBuiltinMacroExpanded; |
| 924 | |
| 925 | SmallString<128> TmpBuffer; |
| 926 | llvm::raw_svector_ostream OS(TmpBuffer); |
| 927 | |
| 928 | // Set up the return result. |
| 929 | Tok.setIdentifierInfo(0); |
| 930 | Tok.clearFlag(Token::NeedsCleaning); |
| 931 | |
| 932 | if (II == Ident__LINE__) { |
| 933 | // C99 6.10.8: "__LINE__: The presumed line number (within the current |
| 934 | // source file) of the current source line (an integer constant)". This can |
| 935 | // be affected by #line. |
| 936 | SourceLocation Loc = Tok.getLocation(); |
| 937 | |
| 938 | // Advance to the location of the first _, this might not be the first byte |
| 939 | // of the token if it starts with an escaped newline. |
| 940 | Loc = AdvanceToTokenCharacter(Loc, 0); |
| 941 | |
| 942 | // One wrinkle here is that GCC expands __LINE__ to location of the *end* of |
| 943 | // a macro expansion. This doesn't matter for object-like macros, but |
| 944 | // can matter for a function-like macro that expands to contain __LINE__. |
| 945 | // Skip down through expansion points until we find a file loc for the |
| 946 | // end of the expansion history. |
| 947 | Loc = SourceMgr.getExpansionRange(Loc).second; |
| 948 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc); |
| 949 | |
| 950 | // __LINE__ expands to a simple numeric value. |
| 951 | OS << (PLoc.isValid()? PLoc.getLine() : 1); |
| 952 | Tok.setKind(tok::numeric_constant); |
| 953 | } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) { |
| 954 | // C99 6.10.8: "__FILE__: The presumed name of the current source file (a |
| 955 | // character string literal)". This can be affected by #line. |
| 956 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); |
| 957 | |
| 958 | // __BASE_FILE__ is a GNU extension that returns the top of the presumed |
| 959 | // #include stack instead of the current file. |
| 960 | if (II == Ident__BASE_FILE__ && PLoc.isValid()) { |
| 961 | SourceLocation NextLoc = PLoc.getIncludeLoc(); |
| 962 | while (NextLoc.isValid()) { |
| 963 | PLoc = SourceMgr.getPresumedLoc(NextLoc); |
| 964 | if (PLoc.isInvalid()) |
| 965 | break; |
| 966 | |
| 967 | NextLoc = PLoc.getIncludeLoc(); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | // Escape this filename. Turn '\' -> '\\' '"' -> '\"' |
| 972 | SmallString<128> FN; |
| 973 | if (PLoc.isValid()) { |
| 974 | FN += PLoc.getFilename(); |
| 975 | Lexer::Stringify(FN); |
| 976 | OS << '"' << FN.str() << '"'; |
| 977 | } |
| 978 | Tok.setKind(tok::string_literal); |
| 979 | } else if (II == Ident__DATE__) { |
| 980 | if (!DATELoc.isValid()) |
| 981 | ComputeDATE_TIME(DATELoc, TIMELoc, *this); |
| 982 | Tok.setKind(tok::string_literal); |
| 983 | Tok.setLength(strlen("\"Mmm dd yyyy\"")); |
| 984 | Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(), |
| 985 | Tok.getLocation(), |
| 986 | Tok.getLength())); |
| 987 | return; |
| 988 | } else if (II == Ident__TIME__) { |
| 989 | if (!TIMELoc.isValid()) |
| 990 | ComputeDATE_TIME(DATELoc, TIMELoc, *this); |
| 991 | Tok.setKind(tok::string_literal); |
| 992 | Tok.setLength(strlen("\"hh:mm:ss\"")); |
| 993 | Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(), |
| 994 | Tok.getLocation(), |
| 995 | Tok.getLength())); |
| 996 | return; |
| 997 | } else if (II == Ident__INCLUDE_LEVEL__) { |
| 998 | // Compute the presumed include depth of this token. This can be affected |
| 999 | // by GNU line markers. |
| 1000 | unsigned Depth = 0; |
| 1001 | |
| 1002 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); |
| 1003 | if (PLoc.isValid()) { |
| 1004 | PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc()); |
| 1005 | for (; PLoc.isValid(); ++Depth) |
| 1006 | PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc()); |
| 1007 | } |
| 1008 | |
| 1009 | // __INCLUDE_LEVEL__ expands to a simple numeric value. |
| 1010 | OS << Depth; |
| 1011 | Tok.setKind(tok::numeric_constant); |
| 1012 | } else if (II == Ident__TIMESTAMP__) { |
| 1013 | // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be |
| 1014 | // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime. |
| 1015 | |
| 1016 | // Get the file that we are lexing out of. If we're currently lexing from |
| 1017 | // a macro, dig into the include stack. |
| 1018 | const FileEntry *CurFile = 0; |
| 1019 | PreprocessorLexer *TheLexer = getCurrentFileLexer(); |
| 1020 | |
| 1021 | if (TheLexer) |
| 1022 | CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID()); |
| 1023 | |
| 1024 | const char *Result; |
| 1025 | if (CurFile) { |
| 1026 | time_t TT = CurFile->getModificationTime(); |
| 1027 | struct tm *TM = localtime(&TT); |
| 1028 | Result = asctime(TM); |
| 1029 | } else { |
| 1030 | Result = "??? ??? ?? ??:??:?? ????\n"; |
| 1031 | } |
| 1032 | // Surround the string with " and strip the trailing newline. |
| 1033 | OS << '"' << StringRef(Result, strlen(Result)-1) << '"'; |
| 1034 | Tok.setKind(tok::string_literal); |
| 1035 | } else if (II == Ident__COUNTER__) { |
| 1036 | // __COUNTER__ expands to a simple numeric value. |
| 1037 | OS << CounterValue++; |
| 1038 | Tok.setKind(tok::numeric_constant); |
| 1039 | } else if (II == Ident__has_feature || |
| 1040 | II == Ident__has_extension || |
| 1041 | II == Ident__has_builtin || |
| 1042 | II == Ident__has_attribute) { |
| 1043 | // The argument to these builtins should be a parenthesized identifier. |
| 1044 | SourceLocation StartLoc = Tok.getLocation(); |
| 1045 | |
| 1046 | bool IsValid = false; |
| 1047 | IdentifierInfo *FeatureII = 0; |
| 1048 | |
| 1049 | // Read the '('. |
| 1050 | Lex(Tok); |
| 1051 | if (Tok.is(tok::l_paren)) { |
| 1052 | // Read the identifier |
| 1053 | Lex(Tok); |
| 1054 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_const)) { |
| 1055 | FeatureII = Tok.getIdentifierInfo(); |
| 1056 | |
| 1057 | // Read the ')'. |
| 1058 | Lex(Tok); |
| 1059 | if (Tok.is(tok::r_paren)) |
| 1060 | IsValid = true; |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | bool Value = false; |
| 1065 | if (!IsValid) |
| 1066 | Diag(StartLoc, diag::err_feature_check_malformed); |
| 1067 | else if (II == Ident__has_builtin) { |
| 1068 | // Check for a builtin is trivial. |
| 1069 | Value = FeatureII->getBuiltinID() != 0; |
| 1070 | } else if (II == Ident__has_attribute) |
| 1071 | Value = HasAttribute(FeatureII); |
| 1072 | else if (II == Ident__has_extension) |
| 1073 | Value = HasExtension(*this, FeatureII); |
| 1074 | else { |
| 1075 | assert(II == Ident__has_feature && "Must be feature check"); |
| 1076 | Value = HasFeature(*this, FeatureII); |
| 1077 | } |
| 1078 | |
| 1079 | OS << (int)Value; |
| 1080 | if (IsValid) |
| 1081 | Tok.setKind(tok::numeric_constant); |
| 1082 | } else if (II == Ident__has_include || |
| 1083 | II == Ident__has_include_next) { |
| 1084 | // The argument to these two builtins should be a parenthesized |
| 1085 | // file name string literal using angle brackets (<>) or |
| 1086 | // double-quotes (""). |
| 1087 | bool Value; |
| 1088 | if (II == Ident__has_include) |
| 1089 | Value = EvaluateHasInclude(Tok, II, *this); |
| 1090 | else |
| 1091 | Value = EvaluateHasIncludeNext(Tok, II, *this); |
| 1092 | OS << (int)Value; |
| 1093 | Tok.setKind(tok::numeric_constant); |
| 1094 | } else if (II == Ident__has_warning) { |
| 1095 | // The argument should be a parenthesized string literal. |
| 1096 | // The argument to these builtins should be a parenthesized identifier. |
| 1097 | SourceLocation StartLoc = Tok.getLocation(); |
| 1098 | bool IsValid = false; |
| 1099 | bool Value = false; |
| 1100 | // Read the '('. |
| 1101 | Lex(Tok); |
| 1102 | do { |
| 1103 | if (Tok.is(tok::l_paren)) { |
| 1104 | // Read the string. |
| 1105 | Lex(Tok); |
| 1106 | |
| 1107 | // We need at least one string literal. |
| 1108 | if (!Tok.is(tok::string_literal)) { |
| 1109 | StartLoc = Tok.getLocation(); |
| 1110 | IsValid = false; |
| 1111 | // Eat tokens until ')'. |
| 1112 | do Lex(Tok); while (!(Tok.is(tok::r_paren) || Tok.is(tok::eod))); |
| 1113 | break; |
| 1114 | } |
| 1115 | |
| 1116 | // String concatenation allows multiple strings, which can even come |
| 1117 | // from macro expansion. |
| 1118 | SmallVector<Token, 4> StrToks; |
| 1119 | while (Tok.is(tok::string_literal)) { |
| 1120 | // Complain about, and drop, any ud-suffix. |
| 1121 | if (Tok.hasUDSuffix()) |
| 1122 | Diag(Tok, diag::err_invalid_string_udl); |
| 1123 | StrToks.push_back(Tok); |
| 1124 | LexUnexpandedToken(Tok); |
| 1125 | } |
| 1126 | |
| 1127 | // Is the end a ')'? |
| 1128 | if (!(IsValid = Tok.is(tok::r_paren))) |
| 1129 | break; |
| 1130 | |
| 1131 | // Concatenate and parse the strings. |
| 1132 | StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this); |
| 1133 | assert(Literal.isAscii() && "Didn't allow wide strings in"); |
| 1134 | if (Literal.hadError) |
| 1135 | break; |
| 1136 | if (Literal.Pascal) { |
| 1137 | Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
| 1138 | break; |
| 1139 | } |
| 1140 | |
| 1141 | StringRef WarningName(Literal.GetString()); |
| 1142 | |
| 1143 | if (WarningName.size() < 3 || WarningName[0] != '-' || |
| 1144 | WarningName[1] != 'W') { |
| 1145 | Diag(StrToks[0].getLocation(), diag::warn_has_warning_invalid_option); |
| 1146 | break; |
| 1147 | } |
| 1148 | |
| 1149 | // Finally, check if the warning flags maps to a diagnostic group. |
| 1150 | // We construct a SmallVector here to talk to getDiagnosticIDs(). |
| 1151 | // Although we don't use the result, this isn't a hot path, and not |
| 1152 | // worth special casing. |
| 1153 | llvm::SmallVector<diag::kind, 10> Diags; |
| 1154 | Value = !getDiagnostics().getDiagnosticIDs()-> |
| 1155 | getDiagnosticsInGroup(WarningName.substr(2), Diags); |
| 1156 | } |
| 1157 | } while (false); |
| 1158 | |
| 1159 | if (!IsValid) |
| 1160 | Diag(StartLoc, diag::err_warning_check_malformed); |
| 1161 | |
| 1162 | OS << (int)Value; |
| 1163 | Tok.setKind(tok::numeric_constant); |
| 1164 | } else { |
| 1165 | llvm_unreachable("Unknown identifier!"); |
| 1166 | } |
| 1167 | CreateString(OS.str().data(), OS.str().size(), Tok, |
| 1168 | Tok.getLocation(), Tok.getLocation()); |
| 1169 | } |
| 1170 | |
| 1171 | void Preprocessor::markMacroAsUsed(MacroInfo *MI) { |
| 1172 | // If the 'used' status changed, and the macro requires 'unused' warning, |
| 1173 | // remove its SourceLocation from the warn-for-unused-macro locations. |
| 1174 | if (MI->isWarnIfUnused() && !MI->isUsed()) |
| 1175 | WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); |
| 1176 | MI->setIsUsed(true); |
| 1177 | } |