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