Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source 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/Lex/MacroInfo.h" |
| 21 | #include "clang/Lex/LiteralSupport.h" |
| 22 | #include "clang/Basic/TargetInfo.h" |
| 23 | #include "clang/Basic/TokenKinds.h" |
| 24 | #include "clang/Basic/Diagnostic.h" |
| 25 | #include "llvm/ADT/APSInt.h" |
| 26 | #include "llvm/ADT/SmallString.h" |
| 27 | using namespace clang; |
| 28 | |
| 29 | static bool EvaluateDirectiveSubExpr(llvm::APSInt &LHS, unsigned MinPrec, |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 30 | Token &PeekTok, bool ValueLive, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | Preprocessor &PP); |
| 32 | |
| 33 | /// DefinedTracker - This struct is used while parsing expressions to keep track |
| 34 | /// of whether !defined(X) has been seen. |
| 35 | /// |
| 36 | /// With this simple scheme, we handle the basic forms: |
| 37 | /// !defined(X) and !defined X |
| 38 | /// but we also trivially handle (silly) stuff like: |
| 39 | /// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)). |
| 40 | struct DefinedTracker { |
| 41 | /// Each time a Value is evaluated, it returns information about whether the |
| 42 | /// parsed value is of the form defined(X), !defined(X) or is something else. |
| 43 | enum TrackerState { |
| 44 | DefinedMacro, // defined(X) |
| 45 | NotDefinedMacro, // !defined(X) |
| 46 | Unknown // Something else. |
| 47 | } State; |
| 48 | /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this |
| 49 | /// indicates the macro that was checked. |
| 50 | IdentifierInfo *TheMacro; |
| 51 | }; |
| 52 | |
| 53 | |
| 54 | |
| 55 | /// EvaluateValue - Evaluate the token PeekTok (and any others needed) and |
| 56 | /// return the computed value in Result. Return true if there was an error |
| 57 | /// parsing. This function also returns information about the form of the |
| 58 | /// expression in DT. See above for information on what DT means. |
| 59 | /// |
| 60 | /// If ValueLive is false, then this value is being evaluated in a context where |
| 61 | /// the result is not used. As such, avoid diagnostics that relate to |
| 62 | /// evaluation. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 63 | static bool EvaluateValue(llvm::APSInt &Result, Token &PeekTok, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 64 | DefinedTracker &DT, bool ValueLive, |
| 65 | Preprocessor &PP) { |
| 66 | Result = 0; |
| 67 | DT.State = DefinedTracker::Unknown; |
| 68 | |
| 69 | // If this token's spelling is a pp-identifier, check to see if it is |
| 70 | // 'defined' or if it is a macro. Note that we check here because many |
| 71 | // keywords are pp-identifiers, so we can't check the kind. |
| 72 | if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) { |
| 73 | // If this identifier isn't 'defined' and it wasn't macro expanded, it turns |
| 74 | // into a simple 0, unless it is the C++ keyword "true", in which case it |
| 75 | // turns into "1". |
| 76 | if (II->getPPKeywordID() != tok::pp_defined) { |
| 77 | Result = II->getTokenID() == tok::kw_true; |
| 78 | Result.setIsUnsigned(false); // "0" is signed intmax_t 0. |
| 79 | PP.LexNonComment(PeekTok); |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | // Handle "defined X" and "defined(X)". |
| 84 | |
| 85 | // Get the next token, don't expand it. |
| 86 | PP.LexUnexpandedToken(PeekTok); |
| 87 | |
| 88 | // Two options, it can either be a pp-identifier or a (. |
| 89 | bool InParens = false; |
| 90 | if (PeekTok.getKind() == tok::l_paren) { |
| 91 | // Found a paren, remember we saw it and skip it. |
| 92 | InParens = true; |
| 93 | PP.LexUnexpandedToken(PeekTok); |
| 94 | } |
| 95 | |
| 96 | // If we don't have a pp-identifier now, this is an error. |
| 97 | if ((II = PeekTok.getIdentifierInfo()) == 0) { |
| 98 | PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier); |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | // Otherwise, we got an identifier, is it defined to something? |
| 103 | Result = II->getMacroInfo() != 0; |
| 104 | Result.setIsUnsigned(false); // Result is signed intmax_t. |
| 105 | |
| 106 | // If there is a macro, mark it used. |
| 107 | if (Result != 0 && ValueLive) { |
| 108 | II->getMacroInfo()->setIsUsed(true); |
| 109 | |
| 110 | // If this is the first use of a target-specific macro, warn about it. |
| 111 | if (II->getMacroInfo()->isTargetSpecific()) { |
| 112 | // Don't warn on second use. |
| 113 | II->getMacroInfo()->setIsTargetSpecific(false); |
| 114 | PP.getTargetInfo().DiagnoseNonPortability(PeekTok.getLocation(), |
| 115 | diag::port_target_macro_use); |
| 116 | } |
| 117 | } else if (ValueLive) { |
| 118 | // Use of a target-specific macro for some other target? If so, warn. |
| 119 | if (II->isOtherTargetMacro()) { |
| 120 | II->setIsOtherTargetMacro(false); // Don't warn on second use. |
| 121 | PP.getTargetInfo().DiagnoseNonPortability(PeekTok.getLocation(), |
| 122 | diag::port_target_macro_use); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // Consume identifier. |
| 127 | PP.LexNonComment(PeekTok); |
| 128 | |
| 129 | // If we are in parens, ensure we have a trailing ). |
| 130 | if (InParens) { |
| 131 | if (PeekTok.getKind() != tok::r_paren) { |
| 132 | PP.Diag(PeekTok, diag::err_pp_missing_rparen); |
| 133 | return true; |
| 134 | } |
| 135 | // Consume the ). |
| 136 | PP.LexNonComment(PeekTok); |
| 137 | } |
| 138 | |
| 139 | // Success, remember that we saw defined(X). |
| 140 | DT.State = DefinedTracker::DefinedMacro; |
| 141 | DT.TheMacro = II; |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | switch (PeekTok.getKind()) { |
| 146 | default: // Non-value token. |
| 147 | PP.Diag(PeekTok, diag::err_pp_expr_bad_token); |
| 148 | return true; |
| 149 | case tok::eom: |
| 150 | case tok::r_paren: |
| 151 | // If there is no expression, report and exit. |
| 152 | PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr); |
| 153 | return true; |
| 154 | case tok::numeric_constant: { |
| 155 | llvm::SmallString<64> IntegerBuffer; |
| 156 | IntegerBuffer.resize(PeekTok.getLength()); |
| 157 | const char *ThisTokBegin = &IntegerBuffer[0]; |
| 158 | unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin); |
| 159 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 160 | PeekTok.getLocation(), PP); |
| 161 | if (Literal.hadError) |
| 162 | return true; // a diagnostic was already reported. |
| 163 | |
| 164 | if (Literal.isFloatingLiteral()) { |
| 165 | PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal); |
| 166 | return true; |
| 167 | } |
| 168 | assert(Literal.isIntegerLiteral() && "Unknown ppnumber"); |
| 169 | |
| 170 | // Parse the integer literal into Result. |
| 171 | if (Literal.GetIntegerValue(Result)) { |
| 172 | // Overflow parsing integer literal. |
| 173 | if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large); |
| 174 | Result.setIsUnsigned(true); |
| 175 | } else { |
| 176 | // Set the signedness of the result to match whether there was a U suffix |
| 177 | // or not. |
| 178 | Result.setIsUnsigned(Literal.isUnsigned); |
| 179 | |
| 180 | // Detect overflow based on whether the value is signed. If signed |
| 181 | // and if the value is too large, emit a warning "integer constant is so |
| 182 | // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t |
| 183 | // is 64-bits. |
| 184 | if (!Literal.isUnsigned && Result.isNegative()) { |
| 185 | if (ValueLive)PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed); |
| 186 | Result.setIsUnsigned(true); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // Consume the token. |
| 191 | PP.LexNonComment(PeekTok); |
| 192 | return false; |
| 193 | } |
| 194 | case tok::char_constant: { // 'x' |
| 195 | llvm::SmallString<32> CharBuffer; |
| 196 | CharBuffer.resize(PeekTok.getLength()); |
| 197 | const char *ThisTokBegin = &CharBuffer[0]; |
| 198 | unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin); |
| 199 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 200 | PeekTok.getLocation(), PP); |
| 201 | if (Literal.hadError()) |
| 202 | return true; // A diagnostic was already emitted. |
| 203 | |
| 204 | // Character literals are always int or wchar_t, expand to intmax_t. |
| 205 | TargetInfo &TI = PP.getTargetInfo(); |
| 206 | unsigned NumBits; |
| 207 | if (Literal.isWide()) |
| 208 | NumBits = TI.getWCharWidth(PeekTok.getLocation()); |
| 209 | else |
| 210 | NumBits = TI.getCharWidth(PeekTok.getLocation()); |
| 211 | |
| 212 | // Set the width. |
| 213 | llvm::APSInt Val(NumBits); |
| 214 | // Set the value. |
| 215 | Val = Literal.getValue(); |
| 216 | // Set the signedness. |
| 217 | Val.setIsUnsigned(!TI.isCharSigned(PeekTok.getLocation())); |
| 218 | |
| 219 | if (Result.getBitWidth() > Val.getBitWidth()) { |
| 220 | if (Val.isSigned()) |
| 221 | Result = Val.sext(Result.getBitWidth()); |
| 222 | else |
| 223 | Result = Val.zext(Result.getBitWidth()); |
| 224 | Result.setIsUnsigned(Val.isUnsigned()); |
| 225 | } else { |
| 226 | assert(Result.getBitWidth() == Val.getBitWidth() && |
| 227 | "intmax_t smaller than char/wchar_t?"); |
| 228 | Result = Val; |
| 229 | } |
| 230 | |
| 231 | // Consume the token. |
| 232 | PP.LexNonComment(PeekTok); |
| 233 | return false; |
| 234 | } |
| 235 | case tok::l_paren: |
| 236 | PP.LexNonComment(PeekTok); // Eat the (. |
| 237 | // Parse the value and if there are any binary operators involved, parse |
| 238 | // them. |
| 239 | if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true; |
| 240 | |
| 241 | // If this is a silly value like (X), which doesn't need parens, check for |
| 242 | // !(defined X). |
| 243 | if (PeekTok.getKind() == tok::r_paren) { |
| 244 | // Just use DT unmodified as our result. |
| 245 | } else { |
| 246 | if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP)) |
| 247 | return true; |
| 248 | |
| 249 | if (PeekTok.getKind() != tok::r_paren) { |
| 250 | PP.Diag(PeekTok, diag::err_pp_expected_rparen); |
| 251 | return true; |
| 252 | } |
| 253 | DT.State = DefinedTracker::Unknown; |
| 254 | } |
| 255 | PP.LexNonComment(PeekTok); // Eat the ). |
| 256 | return false; |
| 257 | |
| 258 | case tok::plus: |
| 259 | // Unary plus doesn't modify the value. |
| 260 | PP.LexNonComment(PeekTok); |
| 261 | return EvaluateValue(Result, PeekTok, DT, ValueLive, PP); |
| 262 | case tok::minus: { |
| 263 | SourceLocation Loc = PeekTok.getLocation(); |
| 264 | PP.LexNonComment(PeekTok); |
| 265 | if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true; |
| 266 | // C99 6.5.3.3p3: The sign of the result matches the sign of the operand. |
| 267 | Result = -Result; |
| 268 | |
| 269 | bool Overflow = false; |
| 270 | if (Result.isUnsigned()) |
| 271 | Overflow = !Result.isPositive(); |
| 272 | else if (Result.isMinSignedValue()) |
| 273 | Overflow = true; // -MININT is the only thing that overflows. |
| 274 | |
| 275 | // If this operator is live and overflowed, report the issue. |
| 276 | if (Overflow && ValueLive) |
| 277 | PP.Diag(Loc, diag::warn_pp_expr_overflow); |
| 278 | |
| 279 | DT.State = DefinedTracker::Unknown; |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | case tok::tilde: |
| 284 | PP.LexNonComment(PeekTok); |
| 285 | if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true; |
| 286 | // C99 6.5.3.3p4: The sign of the result matches the sign of the operand. |
| 287 | Result = ~Result; |
| 288 | DT.State = DefinedTracker::Unknown; |
| 289 | return false; |
| 290 | |
| 291 | case tok::exclaim: |
| 292 | PP.LexNonComment(PeekTok); |
| 293 | if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true; |
| 294 | Result = !Result; |
| 295 | // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed. |
| 296 | Result.setIsUnsigned(false); |
| 297 | |
| 298 | if (DT.State == DefinedTracker::DefinedMacro) |
| 299 | DT.State = DefinedTracker::NotDefinedMacro; |
| 300 | else if (DT.State == DefinedTracker::NotDefinedMacro) |
| 301 | DT.State = DefinedTracker::DefinedMacro; |
| 302 | return false; |
| 303 | |
| 304 | // FIXME: Handle #assert |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | |
| 309 | |
| 310 | /// getPrecedence - Return the precedence of the specified binary operator |
| 311 | /// token. This returns: |
| 312 | /// ~0 - Invalid token. |
| 313 | /// 14 - *,/,% |
| 314 | /// 13 - -,+ |
| 315 | /// 12 - <<,>> |
| 316 | /// 11 - >=, <=, >, < |
| 317 | /// 10 - ==, != |
| 318 | /// 9 - & |
| 319 | /// 8 - ^ |
| 320 | /// 7 - | |
| 321 | /// 6 - && |
| 322 | /// 5 - || |
| 323 | /// 4 - ? |
| 324 | /// 3 - : |
| 325 | /// 0 - eom, ) |
| 326 | static unsigned getPrecedence(tok::TokenKind Kind) { |
| 327 | switch (Kind) { |
| 328 | default: return ~0U; |
| 329 | case tok::percent: |
| 330 | case tok::slash: |
| 331 | case tok::star: return 14; |
| 332 | case tok::plus: |
| 333 | case tok::minus: return 13; |
| 334 | case tok::lessless: |
| 335 | case tok::greatergreater: return 12; |
| 336 | case tok::lessequal: |
| 337 | case tok::less: |
| 338 | case tok::greaterequal: |
| 339 | case tok::greater: return 11; |
| 340 | case tok::exclaimequal: |
| 341 | case tok::equalequal: return 10; |
| 342 | case tok::amp: return 9; |
| 343 | case tok::caret: return 8; |
| 344 | case tok::pipe: return 7; |
| 345 | case tok::ampamp: return 6; |
| 346 | case tok::pipepipe: return 5; |
| 347 | case tok::question: return 4; |
| 348 | case tok::colon: return 3; |
| 349 | case tok::comma: return 2; |
| 350 | case tok::r_paren: return 0; // Lowest priority, end of expr. |
| 351 | case tok::eom: return 0; // Lowest priority, end of macro. |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | |
| 356 | /// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is |
| 357 | /// PeekTok, and whose precedence is PeekPrec. |
| 358 | /// |
| 359 | /// If ValueLive is false, then this value is being evaluated in a context where |
| 360 | /// the result is not used. As such, avoid diagnostics that relate to |
| 361 | /// evaluation. |
| 362 | static bool EvaluateDirectiveSubExpr(llvm::APSInt &LHS, unsigned MinPrec, |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 363 | Token &PeekTok, bool ValueLive, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 364 | Preprocessor &PP) { |
| 365 | unsigned PeekPrec = getPrecedence(PeekTok.getKind()); |
| 366 | // If this token isn't valid, report the error. |
| 367 | if (PeekPrec == ~0U) { |
| 368 | PP.Diag(PeekTok, diag::err_pp_expr_bad_token); |
| 369 | return true; |
| 370 | } |
| 371 | |
| 372 | while (1) { |
| 373 | // If this token has a lower precedence than we are allowed to parse, return |
| 374 | // it so that higher levels of the recursion can parse it. |
| 375 | if (PeekPrec < MinPrec) |
| 376 | return false; |
| 377 | |
| 378 | tok::TokenKind Operator = PeekTok.getKind(); |
| 379 | |
| 380 | // If this is a short-circuiting operator, see if the RHS of the operator is |
| 381 | // dead. Note that this cannot just clobber ValueLive. Consider |
| 382 | // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In |
| 383 | // this example, the RHS of the && being dead does not make the rest of the |
| 384 | // expr dead. |
| 385 | bool RHSIsLive; |
| 386 | if (Operator == tok::ampamp && LHS == 0) |
| 387 | RHSIsLive = false; // RHS of "0 && x" is dead. |
| 388 | else if (Operator == tok::pipepipe && LHS != 0) |
| 389 | RHSIsLive = false; // RHS of "1 || x" is dead. |
| 390 | else if (Operator == tok::question && LHS == 0) |
| 391 | RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead. |
| 392 | else |
| 393 | RHSIsLive = ValueLive; |
| 394 | |
| 395 | // Consume the operator, saving the operator token for error reporting. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 396 | Token OpToken = PeekTok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 397 | PP.LexNonComment(PeekTok); |
| 398 | |
| 399 | llvm::APSInt RHS(LHS.getBitWidth()); |
| 400 | // Parse the RHS of the operator. |
| 401 | DefinedTracker DT; |
| 402 | if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true; |
| 403 | |
| 404 | // Remember the precedence of this operator and get the precedence of the |
| 405 | // operator immediately to the right of the RHS. |
| 406 | unsigned ThisPrec = PeekPrec; |
| 407 | PeekPrec = getPrecedence(PeekTok.getKind()); |
| 408 | |
| 409 | // If this token isn't valid, report the error. |
| 410 | if (PeekPrec == ~0U) { |
| 411 | PP.Diag(PeekTok, diag::err_pp_expr_bad_token); |
| 412 | return true; |
| 413 | } |
| 414 | |
| 415 | bool isRightAssoc = Operator == tok::question; |
| 416 | |
| 417 | // Get the precedence of the operator to the right of the RHS. If it binds |
| 418 | // more tightly with RHS than we do, evaluate it completely first. |
| 419 | if (ThisPrec < PeekPrec || |
| 420 | (ThisPrec == PeekPrec && isRightAssoc)) { |
| 421 | if (EvaluateDirectiveSubExpr(RHS, ThisPrec+1, PeekTok, RHSIsLive, PP)) |
| 422 | return true; |
| 423 | PeekPrec = getPrecedence(PeekTok.getKind()); |
| 424 | } |
| 425 | assert(PeekPrec <= ThisPrec && "Recursion didn't work!"); |
| 426 | |
| 427 | // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if |
| 428 | // either operand is unsigned. Don't do this for x and y in "x ? y : z". |
| 429 | llvm::APSInt Res(LHS.getBitWidth()); |
| 430 | if (Operator != tok::question) { |
| 431 | Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned()); |
| 432 | // If this just promoted something from signed to unsigned, and if the |
| 433 | // value was negative, warn about it. |
| 434 | if (ValueLive && Res.isUnsigned()) { |
| 435 | if (!LHS.isUnsigned() && LHS.isNegative()) |
| 436 | PP.Diag(OpToken, diag::warn_pp_convert_lhs_to_positive, |
| 437 | LHS.toString(10, true) + " to " + LHS.toString(10, false)); |
| 438 | if (!RHS.isUnsigned() && RHS.isNegative()) |
| 439 | PP.Diag(OpToken, diag::warn_pp_convert_rhs_to_positive, |
| 440 | RHS.toString(10, true) + " to " + RHS.toString(10, false)); |
| 441 | } |
| 442 | LHS.setIsUnsigned(Res.isUnsigned()); |
| 443 | RHS.setIsUnsigned(Res.isUnsigned()); |
| 444 | } |
| 445 | |
| 446 | // FIXME: All of these should detect and report overflow?? |
| 447 | bool Overflow = false; |
| 448 | switch (Operator) { |
| 449 | default: assert(0 && "Unknown operator token!"); |
| 450 | case tok::percent: |
| 451 | if (RHS == 0) { |
| 452 | if (ValueLive) PP.Diag(OpToken, diag::err_pp_remainder_by_zero); |
| 453 | return true; |
| 454 | } |
| 455 | Res = LHS % RHS; |
| 456 | break; |
| 457 | case tok::slash: |
| 458 | if (RHS == 0) { |
| 459 | if (ValueLive) PP.Diag(OpToken, diag::err_pp_division_by_zero); |
| 460 | return true; |
| 461 | } |
| 462 | Res = LHS / RHS; |
| 463 | if (LHS.isSigned()) |
| 464 | Overflow = LHS.isMinSignedValue() && RHS.isAllOnesValue(); // MININT/-1 |
| 465 | break; |
| 466 | case tok::star: |
| 467 | Res = LHS * RHS; |
| 468 | if (LHS != 0 && RHS != 0) |
| 469 | Overflow = Res/RHS != LHS || Res/LHS != RHS; |
| 470 | break; |
| 471 | case tok::lessless: { |
| 472 | // Determine whether overflow is about to happen. |
| 473 | unsigned ShAmt = RHS.getLimitedValue(); |
| 474 | if (ShAmt >= LHS.getBitWidth()) |
| 475 | Overflow = true, ShAmt = LHS.getBitWidth()-1; |
| 476 | else if (LHS.isUnsigned()) |
| 477 | Overflow = ShAmt > LHS.countLeadingZeros(); |
| 478 | else if (LHS.isPositive()) |
| 479 | Overflow = ShAmt >= LHS.countLeadingZeros(); // Don't allow sign change. |
| 480 | else |
| 481 | Overflow = ShAmt >= LHS.countLeadingOnes(); |
| 482 | |
| 483 | Res = LHS << ShAmt; |
| 484 | break; |
| 485 | } |
| 486 | case tok::greatergreater: { |
| 487 | // Determine whether overflow is about to happen. |
| 488 | unsigned ShAmt = RHS.getLimitedValue(); |
| 489 | if (ShAmt >= LHS.getBitWidth()) |
| 490 | Overflow = true, ShAmt = LHS.getBitWidth()-1; |
| 491 | Res = LHS >> ShAmt; |
| 492 | break; |
| 493 | } |
| 494 | case tok::plus: |
| 495 | Res = LHS + RHS; |
| 496 | if (LHS.isUnsigned()) |
| 497 | Overflow = Res.ult(LHS); |
| 498 | else if (LHS.isPositive() == RHS.isPositive() && |
| 499 | Res.isPositive() != LHS.isPositive()) |
| 500 | Overflow = true; // Overflow for signed addition. |
| 501 | break; |
| 502 | case tok::minus: |
| 503 | Res = LHS - RHS; |
| 504 | if (LHS.isUnsigned()) |
| 505 | Overflow = Res.ugt(LHS); |
| 506 | else if (LHS.isPositive() != RHS.isPositive() && |
| 507 | Res.isPositive() != LHS.isPositive()) |
| 508 | Overflow = true; // Overflow for signed subtraction. |
| 509 | break; |
| 510 | case tok::lessequal: |
| 511 | Res = LHS <= RHS; |
| 512 | Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed) |
| 513 | break; |
| 514 | case tok::less: |
| 515 | Res = LHS < RHS; |
| 516 | Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed) |
| 517 | break; |
| 518 | case tok::greaterequal: |
| 519 | Res = LHS >= RHS; |
| 520 | Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed) |
| 521 | break; |
| 522 | case tok::greater: |
| 523 | Res = LHS > RHS; |
| 524 | Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed) |
| 525 | break; |
| 526 | case tok::exclaimequal: |
| 527 | Res = LHS != RHS; |
| 528 | Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed) |
| 529 | break; |
| 530 | case tok::equalequal: |
| 531 | Res = LHS == RHS; |
| 532 | Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed) |
| 533 | break; |
| 534 | case tok::amp: |
| 535 | Res = LHS & RHS; |
| 536 | break; |
| 537 | case tok::caret: |
| 538 | Res = LHS ^ RHS; |
| 539 | break; |
| 540 | case tok::pipe: |
| 541 | Res = LHS | RHS; |
| 542 | break; |
| 543 | case tok::ampamp: |
| 544 | Res = (LHS != 0 && RHS != 0); |
| 545 | Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed) |
| 546 | break; |
| 547 | case tok::pipepipe: |
| 548 | Res = (LHS != 0 || RHS != 0); |
| 549 | Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed) |
| 550 | break; |
| 551 | case tok::comma: |
| 552 | PP.Diag(OpToken, diag::ext_pp_comma_expr); |
| 553 | Res = RHS; // LHS = LHS,RHS -> RHS. |
| 554 | break; |
| 555 | case tok::question: { |
| 556 | // Parse the : part of the expression. |
| 557 | if (PeekTok.getKind() != tok::colon) { |
| 558 | PP.Diag(OpToken, diag::err_pp_question_without_colon); |
| 559 | return true; |
| 560 | } |
| 561 | // Consume the :. |
| 562 | PP.LexNonComment(PeekTok); |
| 563 | |
| 564 | // Evaluate the value after the :. |
| 565 | bool AfterColonLive = ValueLive && LHS == 0; |
| 566 | llvm::APSInt AfterColonVal(LHS.getBitWidth()); |
| 567 | DefinedTracker DT; |
| 568 | if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP)) |
| 569 | return true; |
| 570 | |
| 571 | // Parse anything after the : RHS that has a higher precedence than ?. |
| 572 | if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec+1, |
| 573 | PeekTok, AfterColonLive, PP)) |
| 574 | return true; |
| 575 | |
| 576 | // Now that we have the condition, the LHS and the RHS of the :, evaluate. |
| 577 | Res = LHS != 0 ? RHS : AfterColonVal; |
| 578 | |
| 579 | // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if |
| 580 | // either operand is unsigned. |
| 581 | Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned()); |
| 582 | |
| 583 | // Figure out the precedence of the token after the : part. |
| 584 | PeekPrec = getPrecedence(PeekTok.getKind()); |
| 585 | break; |
| 586 | } |
| 587 | case tok::colon: |
| 588 | // Don't allow :'s to float around without being part of ?: exprs. |
| 589 | PP.Diag(OpToken, diag::err_pp_colon_without_question); |
| 590 | return true; |
| 591 | } |
| 592 | |
| 593 | // If this operator is live and overflowed, report the issue. |
| 594 | if (Overflow && ValueLive) |
| 595 | PP.Diag(OpToken, diag::warn_pp_expr_overflow); |
| 596 | |
| 597 | // Put the result back into 'LHS' for our next iteration. |
| 598 | LHS = Res; |
| 599 | } |
| 600 | |
| 601 | return false; |
| 602 | } |
| 603 | |
| 604 | /// EvaluateDirectiveExpression - Evaluate an integer constant expression that |
| 605 | /// may occur after a #if or #elif directive. If the expression is equivalent |
| 606 | /// to "!defined(X)" return X in IfNDefMacro. |
| 607 | bool Preprocessor:: |
| 608 | EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) { |
| 609 | // Peek ahead one token. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 610 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 611 | Lex(Tok); |
| 612 | |
| 613 | // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t. |
| 614 | unsigned BitWidth = getTargetInfo().getIntMaxTWidth(Tok.getLocation()); |
| 615 | llvm::APSInt ResVal(BitWidth); |
| 616 | DefinedTracker DT; |
| 617 | if (EvaluateValue(ResVal, Tok, DT, true, *this)) { |
| 618 | // Parse error, skip the rest of the macro line. |
| 619 | if (Tok.getKind() != tok::eom) |
| 620 | DiscardUntilEndOfDirective(); |
| 621 | return false; |
| 622 | } |
| 623 | |
| 624 | // If we are at the end of the expression after just parsing a value, there |
| 625 | // must be no (unparenthesized) binary operators involved, so we can exit |
| 626 | // directly. |
| 627 | if (Tok.getKind() == tok::eom) { |
| 628 | // If the expression we parsed was of the form !defined(macro), return the |
| 629 | // macro in IfNDefMacro. |
| 630 | if (DT.State == DefinedTracker::NotDefinedMacro) |
| 631 | IfNDefMacro = DT.TheMacro; |
| 632 | |
| 633 | return ResVal != 0; |
| 634 | } |
| 635 | |
| 636 | // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the |
| 637 | // operator and the stuff after it. |
| 638 | if (EvaluateDirectiveSubExpr(ResVal, 1, Tok, true, *this)) { |
| 639 | // Parse error, skip the rest of the macro line. |
| 640 | if (Tok.getKind() != tok::eom) |
| 641 | DiscardUntilEndOfDirective(); |
| 642 | return false; |
| 643 | } |
| 644 | |
| 645 | // If we aren't at the tok::eom token, something bad happened, like an extra |
| 646 | // ')' token. |
| 647 | if (Tok.getKind() != tok::eom) { |
| 648 | Diag(Tok, diag::err_pp_expected_eol); |
| 649 | DiscardUntilEndOfDirective(); |
| 650 | } |
| 651 | |
| 652 | return ResVal != 0; |
| 653 | } |
| 654 | |