1 //===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===// 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 Preprocessor::EvaluateDirectiveExpression method, 11 // which parses and evaluates integer constant expressions for #if directives. 12 // 13 //===----------------------------------------------------------------------===// 14 // 15 // FIXME: implement testing for #assert's. 16 // 17 //===----------------------------------------------------------------------===// 18 19 #include "clang/Lex/Preprocessor.h" 20 #include "clang/Basic/IdentifierTable.h" 21 #include "clang/Basic/SourceLocation.h" 22 #include "clang/Basic/SourceManager.h" 23 #include "clang/Basic/TargetInfo.h" 24 #include "clang/Basic/TokenKinds.h" 25 #include "clang/Lex/CodeCompletionHandler.h" 26 #include "clang/Lex/LexDiagnostic.h" 27 #include "clang/Lex/LiteralSupport.h" 28 #include "clang/Lex/MacroInfo.h" 29 #include "clang/Lex/PPCallbacks.h" 30 #include "clang/Lex/Token.h" 31 #include "llvm/ADT/APSInt.h" 32 #include "llvm/ADT/SmallString.h" 33 #include "llvm/ADT/StringRef.h" 34 #include "llvm/Support/ErrorHandling.h" 35 #include "llvm/Support/SaveAndRestore.h" 36 #include <cassert> 37 38 using namespace clang; 39 40 namespace { 41 42 /// PPValue - Represents the value of a subexpression of a preprocessor 43 /// conditional and the source range covered by it. 44 class PPValue { 45 SourceRange Range; 46 IdentifierInfo *II; 47 48 public: 49 llvm::APSInt Val; 50 51 // Default ctor - Construct an 'invalid' PPValue. 52 PPValue(unsigned BitWidth) : Val(BitWidth) {} 53 54 // If this value was produced by directly evaluating an identifier, produce 55 // that identifier. 56 IdentifierInfo *getIdentifier() const { return II; } 57 void setIdentifier(IdentifierInfo *II) { this->II = II; } 58 59 unsigned getBitWidth() const { return Val.getBitWidth(); } 60 bool isUnsigned() const { return Val.isUnsigned(); } 61 62 SourceRange getRange() const { return Range; } 63 64 void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); } 65 void setRange(SourceLocation B, SourceLocation E) { 66 Range.setBegin(B); Range.setEnd(E); 67 } 68 void setBegin(SourceLocation L) { Range.setBegin(L); } 69 void setEnd(SourceLocation L) { Range.setEnd(L); } 70 }; 71 72 } // end anonymous namespace 73 74 static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec, 75 Token &PeekTok, bool ValueLive, 76 bool &IncludedUndefinedIds, 77 Preprocessor &PP); 78 79 /// DefinedTracker - This struct is used while parsing expressions to keep track 80 /// of whether !defined(X) has been seen. 81 /// 82 /// With this simple scheme, we handle the basic forms: 83 /// !defined(X) and !defined X 84 /// but we also trivially handle (silly) stuff like: 85 /// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)). 86 struct DefinedTracker { 87 /// Each time a Value is evaluated, it returns information about whether the 88 /// parsed value is of the form defined(X), !defined(X) or is something else. 89 enum TrackerState { 90 DefinedMacro, // defined(X) 91 NotDefinedMacro, // !defined(X) 92 Unknown // Something else. 93 } State; 94 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this 95 /// indicates the macro that was checked. 96 IdentifierInfo *TheMacro; 97 bool IncludedUndefinedIds = false; 98 }; 99 100 /// EvaluateDefined - Process a 'defined(sym)' expression. 101 static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT, 102 bool ValueLive, Preprocessor &PP) { 103 SourceLocation beginLoc(PeekTok.getLocation()); 104 Result.setBegin(beginLoc); 105 106 // Get the next token, don't expand it. 107 PP.LexUnexpandedNonComment(PeekTok); 108 109 // Two options, it can either be a pp-identifier or a (. 110 SourceLocation LParenLoc; 111 if (PeekTok.is(tok::l_paren)) { 112 // Found a paren, remember we saw it and skip it. 113 LParenLoc = PeekTok.getLocation(); 114 PP.LexUnexpandedNonComment(PeekTok); 115 } 116 117 if (PeekTok.is(tok::code_completion)) { 118 if (PP.getCodeCompletionHandler()) 119 PP.getCodeCompletionHandler()->CodeCompleteMacroName(false); 120 PP.setCodeCompletionReached(); 121 PP.LexUnexpandedNonComment(PeekTok); 122 } 123 124 // If we don't have a pp-identifier now, this is an error. 125 if (PP.CheckMacroName(PeekTok, MU_Other)) 126 return true; 127 128 // Otherwise, we got an identifier, is it defined to something? 129 IdentifierInfo *II = PeekTok.getIdentifierInfo(); 130 MacroDefinition Macro = PP.getMacroDefinition(II); 131 Result.Val = !!Macro; 132 Result.Val.setIsUnsigned(false); // Result is signed intmax_t. 133 DT.IncludedUndefinedIds = !Macro; 134 135 // If there is a macro, mark it used. 136 if (Result.Val != 0 && ValueLive) 137 PP.markMacroAsUsed(Macro.getMacroInfo()); 138 139 // Save macro token for callback. 140 Token macroToken(PeekTok); 141 142 // If we are in parens, ensure we have a trailing ). 143 if (LParenLoc.isValid()) { 144 // Consume identifier. 145 Result.setEnd(PeekTok.getLocation()); 146 PP.LexUnexpandedNonComment(PeekTok); 147 148 if (PeekTok.isNot(tok::r_paren)) { 149 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_after) 150 << "'defined'" << tok::r_paren; 151 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren; 152 return true; 153 } 154 // Consume the ). 155 Result.setEnd(PeekTok.getLocation()); 156 PP.LexNonComment(PeekTok); 157 } else { 158 // Consume identifier. 159 Result.setEnd(PeekTok.getLocation()); 160 PP.LexNonComment(PeekTok); 161 } 162 163 // [cpp.cond]p4: 164 // Prior to evaluation, macro invocations in the list of preprocessing 165 // tokens that will become the controlling constant expression are replaced 166 // (except for those macro names modified by the 'defined' unary operator), 167 // just as in normal text. If the token 'defined' is generated as a result 168 // of this replacement process or use of the 'defined' unary operator does 169 // not match one of the two specified forms prior to macro replacement, the 170 // behavior is undefined. 171 // This isn't an idle threat, consider this program: 172 // #define FOO 173 // #define BAR defined(FOO) 174 // #if BAR 175 // ... 176 // #else 177 // ... 178 // #endif 179 // clang and gcc will pick the #if branch while Visual Studio will take the 180 // #else branch. Emit a warning about this undefined behavior. 181 if (beginLoc.isMacroID()) { 182 bool IsFunctionTypeMacro = 183 PP.getSourceManager() 184 .getSLocEntry(PP.getSourceManager().getFileID(beginLoc)) 185 .getExpansion() 186 .isFunctionMacroExpansion(); 187 // For object-type macros, it's easy to replace 188 // #define FOO defined(BAR) 189 // with 190 // #if defined(BAR) 191 // #define FOO 1 192 // #else 193 // #define FOO 0 194 // #endif 195 // and doing so makes sense since compilers handle this differently in 196 // practice (see example further up). But for function-type macros, 197 // there is no good way to write 198 // # define FOO(x) (defined(M_ ## x) && M_ ## x) 199 // in a different way, and compilers seem to agree on how to behave here. 200 // So warn by default on object-type macros, but only warn in -pedantic 201 // mode on function-type macros. 202 if (IsFunctionTypeMacro) 203 PP.Diag(beginLoc, diag::warn_defined_in_function_type_macro); 204 else 205 PP.Diag(beginLoc, diag::warn_defined_in_object_type_macro); 206 } 207 208 // Invoke the 'defined' callback. 209 if (PPCallbacks *Callbacks = PP.getPPCallbacks()) { 210 Callbacks->Defined(macroToken, Macro, 211 SourceRange(beginLoc, PeekTok.getLocation())); 212 } 213 214 // Success, remember that we saw defined(X). 215 DT.State = DefinedTracker::DefinedMacro; 216 DT.TheMacro = II; 217 return false; 218 } 219 220 /// EvaluateValue - Evaluate the token PeekTok (and any others needed) and 221 /// return the computed value in Result. Return true if there was an error 222 /// parsing. This function also returns information about the form of the 223 /// expression in DT. See above for information on what DT means. 224 /// 225 /// If ValueLive is false, then this value is being evaluated in a context where 226 /// the result is not used. As such, avoid diagnostics that relate to 227 /// evaluation. 228 static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT, 229 bool ValueLive, Preprocessor &PP) { 230 DT.State = DefinedTracker::Unknown; 231 232 Result.setIdentifier(nullptr); 233 234 if (PeekTok.is(tok::code_completion)) { 235 if (PP.getCodeCompletionHandler()) 236 PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression(); 237 PP.setCodeCompletionReached(); 238 PP.LexNonComment(PeekTok); 239 } 240 241 switch (PeekTok.getKind()) { 242 default: 243 // If this token's spelling is a pp-identifier, check to see if it is 244 // 'defined' or if it is a macro. Note that we check here because many 245 // keywords are pp-identifiers, so we can't check the kind. 246 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) { 247 // Handle "defined X" and "defined(X)". 248 if (II->isStr("defined")) 249 return EvaluateDefined(Result, PeekTok, DT, ValueLive, PP); 250 251 if (!II->isCPlusPlusOperatorKeyword()) { 252 // If this identifier isn't 'defined' or one of the special 253 // preprocessor keywords and it wasn't macro expanded, it turns 254 // into a simple 0 255 if (ValueLive) 256 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II; 257 Result.Val = 0; 258 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0. 259 Result.setIdentifier(II); 260 Result.setRange(PeekTok.getLocation()); 261 DT.IncludedUndefinedIds = true; 262 PP.LexNonComment(PeekTok); 263 return false; 264 } 265 } 266 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr); 267 return true; 268 case tok::eod: 269 case tok::r_paren: 270 // If there is no expression, report and exit. 271 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr); 272 return true; 273 case tok::numeric_constant: { 274 SmallString<64> IntegerBuffer; 275 bool NumberInvalid = false; 276 StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer, 277 &NumberInvalid); 278 if (NumberInvalid) 279 return true; // a diagnostic was already reported 280 281 NumericLiteralParser Literal(Spelling, PeekTok.getLocation(), PP); 282 if (Literal.hadError) 283 return true; // a diagnostic was already reported. 284 285 if (Literal.isFloatingLiteral() || Literal.isImaginary) { 286 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal); 287 return true; 288 } 289 assert(Literal.isIntegerLiteral() && "Unknown ppnumber"); 290 291 // Complain about, and drop, any ud-suffix. 292 if (Literal.hasUDSuffix()) 293 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1; 294 295 // 'long long' is a C99 or C++11 feature. 296 if (!PP.getLangOpts().C99 && Literal.isLongLong) { 297 if (PP.getLangOpts().CPlusPlus) 298 PP.Diag(PeekTok, 299 PP.getLangOpts().CPlusPlus11 ? 300 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong); 301 else 302 PP.Diag(PeekTok, diag::ext_c99_longlong); 303 } 304 305 // Parse the integer literal into Result. 306 if (Literal.GetIntegerValue(Result.Val)) { 307 // Overflow parsing integer literal. 308 if (ValueLive) 309 PP.Diag(PeekTok, diag::err_integer_literal_too_large) 310 << /* Unsigned */ 1; 311 Result.Val.setIsUnsigned(true); 312 } else { 313 // Set the signedness of the result to match whether there was a U suffix 314 // or not. 315 Result.Val.setIsUnsigned(Literal.isUnsigned); 316 317 // Detect overflow based on whether the value is signed. If signed 318 // and if the value is too large, emit a warning "integer constant is so 319 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t 320 // is 64-bits. 321 if (!Literal.isUnsigned && Result.Val.isNegative()) { 322 // Octal, hexadecimal, and binary literals are implicitly unsigned if 323 // the value does not fit into a signed integer type. 324 if (ValueLive && Literal.getRadix() == 10) 325 PP.Diag(PeekTok, diag::ext_integer_literal_too_large_for_signed); 326 Result.Val.setIsUnsigned(true); 327 } 328 } 329 330 // Consume the token. 331 Result.setRange(PeekTok.getLocation()); 332 PP.LexNonComment(PeekTok); 333 return false; 334 } 335 case tok::char_constant: // 'x' 336 case tok::wide_char_constant: // L'x' 337 case tok::utf8_char_constant: // u8'x' 338 case tok::utf16_char_constant: // u'x' 339 case tok::utf32_char_constant: { // U'x' 340 // Complain about, and drop, any ud-suffix. 341 if (PeekTok.hasUDSuffix()) 342 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0; 343 344 SmallString<32> CharBuffer; 345 bool CharInvalid = false; 346 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid); 347 if (CharInvalid) 348 return true; 349 350 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), 351 PeekTok.getLocation(), PP, PeekTok.getKind()); 352 if (Literal.hadError()) 353 return true; // A diagnostic was already emitted. 354 355 // Character literals are always int or wchar_t, expand to intmax_t. 356 const TargetInfo &TI = PP.getTargetInfo(); 357 unsigned NumBits; 358 if (Literal.isMultiChar()) 359 NumBits = TI.getIntWidth(); 360 else if (Literal.isWide()) 361 NumBits = TI.getWCharWidth(); 362 else if (Literal.isUTF16()) 363 NumBits = TI.getChar16Width(); 364 else if (Literal.isUTF32()) 365 NumBits = TI.getChar32Width(); 366 else // char or char8_t 367 NumBits = TI.getCharWidth(); 368 369 // Set the width. 370 llvm::APSInt Val(NumBits); 371 // Set the value. 372 Val = Literal.getValue(); 373 // Set the signedness. UTF-16 and UTF-32 are always unsigned 374 if (Literal.isWide()) 375 Val.setIsUnsigned(!TargetInfo::isTypeSigned(TI.getWCharType())); 376 else if (!Literal.isUTF16() && !Literal.isUTF32()) 377 Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned); 378 379 if (Result.Val.getBitWidth() > Val.getBitWidth()) { 380 Result.Val = Val.extend(Result.Val.getBitWidth()); 381 } else { 382 assert(Result.Val.getBitWidth() == Val.getBitWidth() && 383 "intmax_t smaller than char/wchar_t?"); 384 Result.Val = Val; 385 } 386 387 // Consume the token. 388 Result.setRange(PeekTok.getLocation()); 389 PP.LexNonComment(PeekTok); 390 return false; 391 } 392 case tok::l_paren: { 393 SourceLocation Start = PeekTok.getLocation(); 394 PP.LexNonComment(PeekTok); // Eat the (. 395 // Parse the value and if there are any binary operators involved, parse 396 // them. 397 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true; 398 399 // If this is a silly value like (X), which doesn't need parens, check for 400 // !(defined X). 401 if (PeekTok.is(tok::r_paren)) { 402 // Just use DT unmodified as our result. 403 } else { 404 // Otherwise, we have something like (x+y), and we consumed '(x'. 405 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, 406 DT.IncludedUndefinedIds, PP)) 407 return true; 408 409 if (PeekTok.isNot(tok::r_paren)) { 410 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen) 411 << Result.getRange(); 412 PP.Diag(Start, diag::note_matching) << tok::l_paren; 413 return true; 414 } 415 DT.State = DefinedTracker::Unknown; 416 } 417 Result.setRange(Start, PeekTok.getLocation()); 418 Result.setIdentifier(nullptr); 419 PP.LexNonComment(PeekTok); // Eat the ). 420 return false; 421 } 422 case tok::plus: { 423 SourceLocation Start = PeekTok.getLocation(); 424 // Unary plus doesn't modify the value. 425 PP.LexNonComment(PeekTok); 426 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true; 427 Result.setBegin(Start); 428 Result.setIdentifier(nullptr); 429 return false; 430 } 431 case tok::minus: { 432 SourceLocation Loc = PeekTok.getLocation(); 433 PP.LexNonComment(PeekTok); 434 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true; 435 Result.setBegin(Loc); 436 Result.setIdentifier(nullptr); 437 438 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand. 439 Result.Val = -Result.Val; 440 441 // -MININT is the only thing that overflows. Unsigned never overflows. 442 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue(); 443 444 // If this operator is live and overflowed, report the issue. 445 if (Overflow && ValueLive) 446 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange(); 447 448 DT.State = DefinedTracker::Unknown; 449 return false; 450 } 451 452 case tok::tilde: { 453 SourceLocation Start = PeekTok.getLocation(); 454 PP.LexNonComment(PeekTok); 455 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true; 456 Result.setBegin(Start); 457 Result.setIdentifier(nullptr); 458 459 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand. 460 Result.Val = ~Result.Val; 461 DT.State = DefinedTracker::Unknown; 462 return false; 463 } 464 465 case tok::exclaim: { 466 SourceLocation Start = PeekTok.getLocation(); 467 PP.LexNonComment(PeekTok); 468 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true; 469 Result.setBegin(Start); 470 Result.Val = !Result.Val; 471 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed. 472 Result.Val.setIsUnsigned(false); 473 Result.setIdentifier(nullptr); 474 475 if (DT.State == DefinedTracker::DefinedMacro) 476 DT.State = DefinedTracker::NotDefinedMacro; 477 else if (DT.State == DefinedTracker::NotDefinedMacro) 478 DT.State = DefinedTracker::DefinedMacro; 479 return false; 480 } 481 case tok::kw_true: 482 case tok::kw_false: 483 Result.Val = PeekTok.getKind() == tok::kw_true; 484 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0. 485 Result.setIdentifier(PeekTok.getIdentifierInfo()); 486 Result.setRange(PeekTok.getLocation()); 487 PP.LexNonComment(PeekTok); 488 return false; 489 490 // FIXME: Handle #assert 491 } 492 } 493 494 /// getPrecedence - Return the precedence of the specified binary operator 495 /// token. This returns: 496 /// ~0 - Invalid token. 497 /// 14 -> 3 - various operators. 498 /// 0 - 'eod' or ')' 499 static unsigned getPrecedence(tok::TokenKind Kind) { 500 switch (Kind) { 501 default: return ~0U; 502 case tok::percent: 503 case tok::slash: 504 case tok::star: return 14; 505 case tok::plus: 506 case tok::minus: return 13; 507 case tok::lessless: 508 case tok::greatergreater: return 12; 509 case tok::lessequal: 510 case tok::less: 511 case tok::greaterequal: 512 case tok::greater: return 11; 513 case tok::exclaimequal: 514 case tok::equalequal: return 10; 515 case tok::amp: return 9; 516 case tok::caret: return 8; 517 case tok::pipe: return 7; 518 case tok::ampamp: return 6; 519 case tok::pipepipe: return 5; 520 case tok::question: return 4; 521 case tok::comma: return 3; 522 case tok::colon: return 2; 523 case tok::r_paren: return 0;// Lowest priority, end of expr. 524 case tok::eod: return 0;// Lowest priority, end of directive. 525 } 526 } 527 528 static void diagnoseUnexpectedOperator(Preprocessor &PP, PPValue &LHS, 529 Token &Tok) { 530 if (Tok.is(tok::l_paren) && LHS.getIdentifier()) 531 PP.Diag(LHS.getRange().getBegin(), diag::err_pp_expr_bad_token_lparen) 532 << LHS.getIdentifier(); 533 else 534 PP.Diag(Tok.getLocation(), diag::err_pp_expr_bad_token_binop) 535 << LHS.getRange(); 536 } 537 538 /// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is 539 /// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS. 540 /// 541 /// If ValueLive is false, then this value is being evaluated in a context where 542 /// the result is not used. As such, avoid diagnostics that relate to 543 /// evaluation, such as division by zero warnings. 544 static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec, 545 Token &PeekTok, bool ValueLive, 546 bool &IncludedUndefinedIds, 547 Preprocessor &PP) { 548 unsigned PeekPrec = getPrecedence(PeekTok.getKind()); 549 // If this token isn't valid, report the error. 550 if (PeekPrec == ~0U) { 551 diagnoseUnexpectedOperator(PP, LHS, PeekTok); 552 return true; 553 } 554 555 while (true) { 556 // If this token has a lower precedence than we are allowed to parse, return 557 // it so that higher levels of the recursion can parse it. 558 if (PeekPrec < MinPrec) 559 return false; 560 561 tok::TokenKind Operator = PeekTok.getKind(); 562 563 // If this is a short-circuiting operator, see if the RHS of the operator is 564 // dead. Note that this cannot just clobber ValueLive. Consider 565 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In 566 // this example, the RHS of the && being dead does not make the rest of the 567 // expr dead. 568 bool RHSIsLive; 569 if (Operator == tok::ampamp && LHS.Val == 0) 570 RHSIsLive = false; // RHS of "0 && x" is dead. 571 else if (Operator == tok::pipepipe && LHS.Val != 0) 572 RHSIsLive = false; // RHS of "1 || x" is dead. 573 else if (Operator == tok::question && LHS.Val == 0) 574 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead. 575 else 576 RHSIsLive = ValueLive; 577 578 // Consume the operator, remembering the operator's location for reporting. 579 SourceLocation OpLoc = PeekTok.getLocation(); 580 PP.LexNonComment(PeekTok); 581 582 PPValue RHS(LHS.getBitWidth()); 583 // Parse the RHS of the operator. 584 DefinedTracker DT; 585 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true; 586 IncludedUndefinedIds = DT.IncludedUndefinedIds; 587 588 // Remember the precedence of this operator and get the precedence of the 589 // operator immediately to the right of the RHS. 590 unsigned ThisPrec = PeekPrec; 591 PeekPrec = getPrecedence(PeekTok.getKind()); 592 593 // If this token isn't valid, report the error. 594 if (PeekPrec == ~0U) { 595 diagnoseUnexpectedOperator(PP, RHS, PeekTok); 596 return true; 597 } 598 599 // Decide whether to include the next binop in this subexpression. For 600 // example, when parsing x+y*z and looking at '*', we want to recursively 601 // handle y*z as a single subexpression. We do this because the precedence 602 // of * is higher than that of +. The only strange case we have to handle 603 // here is for the ?: operator, where the precedence is actually lower than 604 // the LHS of the '?'. The grammar rule is: 605 // 606 // conditional-expression ::= 607 // logical-OR-expression ? expression : conditional-expression 608 // where 'expression' is actually comma-expression. 609 unsigned RHSPrec; 610 if (Operator == tok::question) 611 // The RHS of "?" should be maximally consumed as an expression. 612 RHSPrec = getPrecedence(tok::comma); 613 else // All others should munch while higher precedence. 614 RHSPrec = ThisPrec+1; 615 616 if (PeekPrec >= RHSPrec) { 617 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, 618 IncludedUndefinedIds, PP)) 619 return true; 620 PeekPrec = getPrecedence(PeekTok.getKind()); 621 } 622 assert(PeekPrec <= ThisPrec && "Recursion didn't work!"); 623 624 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if 625 // either operand is unsigned. 626 llvm::APSInt Res(LHS.getBitWidth()); 627 switch (Operator) { 628 case tok::question: // No UAC for x and y in "x ? y : z". 629 case tok::lessless: // Shift amount doesn't UAC with shift value. 630 case tok::greatergreater: // Shift amount doesn't UAC with shift value. 631 case tok::comma: // Comma operands are not subject to UACs. 632 case tok::pipepipe: // Logical || does not do UACs. 633 case tok::ampamp: // Logical && does not do UACs. 634 break; // No UAC 635 default: 636 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned()); 637 // If this just promoted something from signed to unsigned, and if the 638 // value was negative, warn about it. 639 if (ValueLive && Res.isUnsigned()) { 640 if (!LHS.isUnsigned() && LHS.Val.isNegative()) 641 PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 0 642 << LHS.Val.toString(10, true) + " to " + 643 LHS.Val.toString(10, false) 644 << LHS.getRange() << RHS.getRange(); 645 if (!RHS.isUnsigned() && RHS.Val.isNegative()) 646 PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 1 647 << RHS.Val.toString(10, true) + " to " + 648 RHS.Val.toString(10, false) 649 << LHS.getRange() << RHS.getRange(); 650 } 651 LHS.Val.setIsUnsigned(Res.isUnsigned()); 652 RHS.Val.setIsUnsigned(Res.isUnsigned()); 653 } 654 655 bool Overflow = false; 656 switch (Operator) { 657 default: llvm_unreachable("Unknown operator token!"); 658 case tok::percent: 659 if (RHS.Val != 0) 660 Res = LHS.Val % RHS.Val; 661 else if (ValueLive) { 662 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero) 663 << LHS.getRange() << RHS.getRange(); 664 return true; 665 } 666 break; 667 case tok::slash: 668 if (RHS.Val != 0) { 669 if (LHS.Val.isSigned()) 670 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false); 671 else 672 Res = LHS.Val / RHS.Val; 673 } else if (ValueLive) { 674 PP.Diag(OpLoc, diag::err_pp_division_by_zero) 675 << LHS.getRange() << RHS.getRange(); 676 return true; 677 } 678 break; 679 680 case tok::star: 681 if (Res.isSigned()) 682 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false); 683 else 684 Res = LHS.Val * RHS.Val; 685 break; 686 case tok::lessless: { 687 // Determine whether overflow is about to happen. 688 if (LHS.isUnsigned()) 689 Res = LHS.Val.ushl_ov(RHS.Val, Overflow); 690 else 691 Res = llvm::APSInt(LHS.Val.sshl_ov(RHS.Val, Overflow), false); 692 break; 693 } 694 case tok::greatergreater: { 695 // Determine whether overflow is about to happen. 696 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue()); 697 if (ShAmt >= LHS.getBitWidth()) { 698 Overflow = true; 699 ShAmt = LHS.getBitWidth()-1; 700 } 701 Res = LHS.Val >> ShAmt; 702 break; 703 } 704 case tok::plus: 705 if (LHS.isUnsigned()) 706 Res = LHS.Val + RHS.Val; 707 else 708 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false); 709 break; 710 case tok::minus: 711 if (LHS.isUnsigned()) 712 Res = LHS.Val - RHS.Val; 713 else 714 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false); 715 break; 716 case tok::lessequal: 717 Res = LHS.Val <= RHS.Val; 718 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed) 719 break; 720 case tok::less: 721 Res = LHS.Val < RHS.Val; 722 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed) 723 break; 724 case tok::greaterequal: 725 Res = LHS.Val >= RHS.Val; 726 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed) 727 break; 728 case tok::greater: 729 Res = LHS.Val > RHS.Val; 730 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed) 731 break; 732 case tok::exclaimequal: 733 Res = LHS.Val != RHS.Val; 734 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed) 735 break; 736 case tok::equalequal: 737 Res = LHS.Val == RHS.Val; 738 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed) 739 break; 740 case tok::amp: 741 Res = LHS.Val & RHS.Val; 742 break; 743 case tok::caret: 744 Res = LHS.Val ^ RHS.Val; 745 break; 746 case tok::pipe: 747 Res = LHS.Val | RHS.Val; 748 break; 749 case tok::ampamp: 750 Res = (LHS.Val != 0 && RHS.Val != 0); 751 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed) 752 break; 753 case tok::pipepipe: 754 Res = (LHS.Val != 0 || RHS.Val != 0); 755 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed) 756 break; 757 case tok::comma: 758 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99 759 // if not being evaluated. 760 if (!PP.getLangOpts().C99 || ValueLive) 761 PP.Diag(OpLoc, diag::ext_pp_comma_expr) 762 << LHS.getRange() << RHS.getRange(); 763 Res = RHS.Val; // LHS = LHS,RHS -> RHS. 764 break; 765 case tok::question: { 766 // Parse the : part of the expression. 767 if (PeekTok.isNot(tok::colon)) { 768 PP.Diag(PeekTok.getLocation(), diag::err_expected) 769 << tok::colon << LHS.getRange() << RHS.getRange(); 770 PP.Diag(OpLoc, diag::note_matching) << tok::question; 771 return true; 772 } 773 // Consume the :. 774 PP.LexNonComment(PeekTok); 775 776 // Evaluate the value after the :. 777 bool AfterColonLive = ValueLive && LHS.Val == 0; 778 PPValue AfterColonVal(LHS.getBitWidth()); 779 DefinedTracker DT; 780 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP)) 781 return true; 782 783 // Parse anything after the : with the same precedence as ?. We allow 784 // things of equal precedence because ?: is right associative. 785 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec, 786 PeekTok, AfterColonLive, 787 IncludedUndefinedIds, PP)) 788 return true; 789 790 // Now that we have the condition, the LHS and the RHS of the :, evaluate. 791 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val; 792 RHS.setEnd(AfterColonVal.getRange().getEnd()); 793 794 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if 795 // either operand is unsigned. 796 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned()); 797 798 // Figure out the precedence of the token after the : part. 799 PeekPrec = getPrecedence(PeekTok.getKind()); 800 break; 801 } 802 case tok::colon: 803 // Don't allow :'s to float around without being part of ?: exprs. 804 PP.Diag(OpLoc, diag::err_pp_colon_without_question) 805 << LHS.getRange() << RHS.getRange(); 806 return true; 807 } 808 809 // If this operator is live and overflowed, report the issue. 810 if (Overflow && ValueLive) 811 PP.Diag(OpLoc, diag::warn_pp_expr_overflow) 812 << LHS.getRange() << RHS.getRange(); 813 814 // Put the result back into 'LHS' for our next iteration. 815 LHS.Val = Res; 816 LHS.setEnd(RHS.getRange().getEnd()); 817 RHS.setIdentifier(nullptr); 818 } 819 } 820 821 /// EvaluateDirectiveExpression - Evaluate an integer constant expression that 822 /// may occur after a #if or #elif directive. If the expression is equivalent 823 /// to "!defined(X)" return X in IfNDefMacro. 824 Preprocessor::DirectiveEvalResult 825 Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) { 826 SaveAndRestore<bool> PPDir(ParsingIfOrElifDirective, true); 827 // Save the current state of 'DisableMacroExpansion' and reset it to false. If 828 // 'DisableMacroExpansion' is true, then we must be in a macro argument list 829 // in which case a directive is undefined behavior. We want macros to be able 830 // to recursively expand in order to get more gcc-list behavior, so we force 831 // DisableMacroExpansion to false and restore it when we're done parsing the 832 // expression. 833 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion; 834 DisableMacroExpansion = false; 835 836 // Peek ahead one token. 837 Token Tok; 838 LexNonComment(Tok); 839 840 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t. 841 unsigned BitWidth = getTargetInfo().getIntMaxTWidth(); 842 843 PPValue ResVal(BitWidth); 844 DefinedTracker DT; 845 if (EvaluateValue(ResVal, Tok, DT, true, *this)) { 846 // Parse error, skip the rest of the macro line. 847 if (Tok.isNot(tok::eod)) 848 DiscardUntilEndOfDirective(); 849 850 // Restore 'DisableMacroExpansion'. 851 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective; 852 return {false, DT.IncludedUndefinedIds}; 853 } 854 855 // If we are at the end of the expression after just parsing a value, there 856 // must be no (unparenthesized) binary operators involved, so we can exit 857 // directly. 858 if (Tok.is(tok::eod)) { 859 // If the expression we parsed was of the form !defined(macro), return the 860 // macro in IfNDefMacro. 861 if (DT.State == DefinedTracker::NotDefinedMacro) 862 IfNDefMacro = DT.TheMacro; 863 864 // Restore 'DisableMacroExpansion'. 865 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective; 866 return {ResVal.Val != 0, DT.IncludedUndefinedIds}; 867 } 868 869 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the 870 // operator and the stuff after it. 871 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question), 872 Tok, true, DT.IncludedUndefinedIds, *this)) { 873 // Parse error, skip the rest of the macro line. 874 if (Tok.isNot(tok::eod)) 875 DiscardUntilEndOfDirective(); 876 877 // Restore 'DisableMacroExpansion'. 878 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective; 879 return {false, DT.IncludedUndefinedIds}; 880 } 881 882 // If we aren't at the tok::eod token, something bad happened, like an extra 883 // ')' token. 884 if (Tok.isNot(tok::eod)) { 885 Diag(Tok, diag::err_pp_expected_eol); 886 DiscardUntilEndOfDirective(); 887 } 888 889 // Restore 'DisableMacroExpansion'. 890 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective; 891 return {ResVal.Val != 0, DT.IncludedUndefinedIds}; 892 } 893