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