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