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. |
Chris Lattner | a3e008a | 2009-12-14 05:00:18 +0000 | [diff] [blame] | 75 | static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT, |
| 76 | bool ValueLive, Preprocessor &PP) { |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 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()); |
Chris Lattner | 1994315 | 2010-02-26 19:42:53 +0000 | [diff] [blame] | 109 | PP.LexUnexpandedToken(PeekTok); |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 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()) { |
Chris Lattner | f880662 | 2009-12-14 04:26:45 +0000 | [diff] [blame] | 145 | // Handle "defined X" and "defined(X)". |
| 146 | if (II->isStr("defined")) |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 147 | return(EvaluateDefined(Result, PeekTok, DT, ValueLive, PP)); |
Chris Lattner | f880662 | 2009-12-14 04:26:45 +0000 | [diff] [blame] | 148 | |
| 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". |
| 153 | if (ValueLive) |
| 154 | PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II; |
| 155 | Result.Val = II->getTokenID() == tok::kw_true; |
| 156 | Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0. |
| 157 | Result.setRange(PeekTok.getLocation()); |
| 158 | PP.LexNonComment(PeekTok); |
| 159 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 160 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 162 | switch (PeekTok.getKind()) { |
| 163 | default: // Non-value token. |
Chris Lattner | d98d975 | 2008-04-13 20:38:43 +0000 | [diff] [blame] | 164 | PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 165 | return true; |
| 166 | case tok::eom: |
| 167 | case tok::r_paren: |
| 168 | // If there is no expression, report and exit. |
| 169 | PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr); |
| 170 | return true; |
| 171 | case tok::numeric_constant: { |
| 172 | llvm::SmallString<64> IntegerBuffer; |
Benjamin Kramer | aeb863c | 2010-02-27 16:29:36 +0000 | [diff] [blame] | 173 | llvm::StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer); |
| 174 | NumericLiteralParser Literal(Spelling.begin(), Spelling.end(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 175 | PeekTok.getLocation(), PP); |
| 176 | if (Literal.hadError) |
| 177 | return true; // a diagnostic was already reported. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 6e400c2 | 2007-08-26 03:29:23 +0000 | [diff] [blame] | 179 | if (Literal.isFloatingLiteral() || Literal.isImaginary) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 180 | PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal); |
| 181 | return true; |
| 182 | } |
| 183 | assert(Literal.isIntegerLiteral() && "Unknown ppnumber"); |
| 184 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 185 | // long long is a C99 feature. |
| 186 | if (!PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus0x |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 187 | && Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 188 | PP.Diag(PeekTok, diag::ext_longlong); |
| 189 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 190 | // Parse the integer literal into Result. |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 191 | if (Literal.GetIntegerValue(Result.Val)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 192 | // Overflow parsing integer literal. |
| 193 | if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large); |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 194 | Result.Val.setIsUnsigned(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 195 | } else { |
| 196 | // Set the signedness of the result to match whether there was a U suffix |
| 197 | // or not. |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 198 | Result.Val.setIsUnsigned(Literal.isUnsigned); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 200 | // Detect overflow based on whether the value is signed. If signed |
| 201 | // and if the value is too large, emit a warning "integer constant is so |
| 202 | // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t |
| 203 | // is 64-bits. |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 204 | if (!Literal.isUnsigned && Result.Val.isNegative()) { |
Chris Lattner | b081a35 | 2008-07-03 03:47:30 +0000 | [diff] [blame] | 205 | // Don't warn for a hex literal: 0x8000..0 shouldn't warn. |
| 206 | if (ValueLive && Literal.getRadix() != 16) |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 207 | PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed); |
| 208 | Result.Val.setIsUnsigned(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 212 | // Consume the token. |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 213 | Result.setRange(PeekTok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 214 | PP.LexNonComment(PeekTok); |
| 215 | return false; |
| 216 | } |
| 217 | case tok::char_constant: { // 'x' |
| 218 | llvm::SmallString<32> CharBuffer; |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 219 | llvm::StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer); |
| 220 | |
| 221 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 222 | PeekTok.getLocation(), PP); |
| 223 | if (Literal.hadError()) |
| 224 | return true; // A diagnostic was already emitted. |
| 225 | |
| 226 | // Character literals are always int or wchar_t, expand to intmax_t. |
Daniel Dunbar | 444be73 | 2009-11-13 05:51:54 +0000 | [diff] [blame] | 227 | const TargetInfo &TI = PP.getTargetInfo(); |
Eli Friedman | 2a1c363 | 2009-06-01 05:25:02 +0000 | [diff] [blame] | 228 | unsigned NumBits; |
| 229 | if (Literal.isMultiChar()) |
| 230 | NumBits = TI.getIntWidth(); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 231 | else if (Literal.isWide()) |
| 232 | NumBits = TI.getWCharWidth(); |
Eli Friedman | 2a1c363 | 2009-06-01 05:25:02 +0000 | [diff] [blame] | 233 | else |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 234 | NumBits = TI.getCharWidth(); |
Eli Friedman | 2a1c363 | 2009-06-01 05:25:02 +0000 | [diff] [blame] | 235 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 236 | // Set the width. |
| 237 | llvm::APSInt Val(NumBits); |
| 238 | // Set the value. |
| 239 | Val = Literal.getValue(); |
| 240 | // Set the signedness. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 241 | Val.setIsUnsigned(!PP.getLangOptions().CharIsSigned); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 243 | if (Result.Val.getBitWidth() > Val.getBitWidth()) { |
| 244 | Result.Val = Val.extend(Result.Val.getBitWidth()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 245 | } else { |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 246 | assert(Result.Val.getBitWidth() == Val.getBitWidth() && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 247 | "intmax_t smaller than char/wchar_t?"); |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 248 | Result.Val = Val; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | // Consume the token. |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 252 | Result.setRange(PeekTok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 253 | PP.LexNonComment(PeekTok); |
| 254 | return false; |
| 255 | } |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 256 | case tok::l_paren: { |
| 257 | SourceLocation Start = PeekTok.getLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 258 | PP.LexNonComment(PeekTok); // Eat the (. |
| 259 | // Parse the value and if there are any binary operators involved, parse |
| 260 | // them. |
| 261 | if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true; |
| 262 | |
| 263 | // If this is a silly value like (X), which doesn't need parens, check for |
| 264 | // !(defined X). |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 265 | if (PeekTok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 266 | // Just use DT unmodified as our result. |
| 267 | } else { |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 268 | // Otherwise, we have something like (x+y), and we consumed '(x'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 269 | if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP)) |
| 270 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 271 | |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 272 | if (PeekTok.isNot(tok::r_paren)) { |
Chris Lattner | 204b2fe | 2008-11-18 21:48:13 +0000 | [diff] [blame] | 273 | PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen) |
| 274 | << Result.getRange(); |
Chris Lattner | 28eb7e9 | 2008-11-23 23:17:07 +0000 | [diff] [blame] | 275 | PP.Diag(Start, diag::note_matching) << "("; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 276 | return true; |
| 277 | } |
| 278 | DT.State = DefinedTracker::Unknown; |
| 279 | } |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 280 | Result.setRange(Start, PeekTok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 281 | PP.LexNonComment(PeekTok); // Eat the ). |
| 282 | return false; |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 283 | } |
| 284 | case tok::plus: { |
| 285 | SourceLocation Start = PeekTok.getLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 286 | // Unary plus doesn't modify the value. |
| 287 | PP.LexNonComment(PeekTok); |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 288 | if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true; |
| 289 | Result.setBegin(Start); |
| 290 | return false; |
| 291 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 292 | case tok::minus: { |
| 293 | SourceLocation Loc = PeekTok.getLocation(); |
| 294 | PP.LexNonComment(PeekTok); |
| 295 | if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true; |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 296 | Result.setBegin(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 298 | // 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] | 299 | Result.Val = -Result.Val; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | |
Chris Lattner | b081a35 | 2008-07-03 03:47:30 +0000 | [diff] [blame] | 301 | // -MININT is the only thing that overflows. Unsigned never overflows. |
| 302 | bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 304 | // If this operator is live and overflowed, report the issue. |
| 305 | if (Overflow && ValueLive) |
Chris Lattner | 204b2fe | 2008-11-18 21:48:13 +0000 | [diff] [blame] | 306 | PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange(); |
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 | DT.State = DefinedTracker::Unknown; |
| 309 | return false; |
| 310 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 312 | case tok::tilde: { |
| 313 | SourceLocation Start = PeekTok.getLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 314 | PP.LexNonComment(PeekTok); |
| 315 | if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true; |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 316 | Result.setBegin(Start); |
| 317 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 318 | // 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] | 319 | Result.Val = ~Result.Val; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 320 | DT.State = DefinedTracker::Unknown; |
| 321 | return false; |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 322 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 324 | case tok::exclaim: { |
| 325 | SourceLocation Start = PeekTok.getLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 326 | PP.LexNonComment(PeekTok); |
| 327 | if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true; |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 328 | Result.setBegin(Start); |
| 329 | Result.Val = !Result.Val; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 330 | // 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] | 331 | Result.Val.setIsUnsigned(false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 333 | if (DT.State == DefinedTracker::DefinedMacro) |
| 334 | DT.State = DefinedTracker::NotDefinedMacro; |
| 335 | else if (DT.State == DefinedTracker::NotDefinedMacro) |
| 336 | DT.State = DefinedTracker::DefinedMacro; |
| 337 | return false; |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 338 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 339 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | // FIXME: Handle #assert |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | |
| 345 | |
| 346 | /// getPrecedence - Return the precedence of the specified binary operator |
| 347 | /// token. This returns: |
| 348 | /// ~0 - Invalid token. |
Chris Lattner | 9e66ba6 | 2008-05-05 04:10:51 +0000 | [diff] [blame] | 349 | /// 14 -> 3 - various operators. |
| 350 | /// 0 - 'eom' or ')' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 351 | static unsigned getPrecedence(tok::TokenKind Kind) { |
| 352 | switch (Kind) { |
| 353 | default: return ~0U; |
| 354 | case tok::percent: |
| 355 | case tok::slash: |
| 356 | case tok::star: return 14; |
| 357 | case tok::plus: |
| 358 | case tok::minus: return 13; |
| 359 | case tok::lessless: |
| 360 | case tok::greatergreater: return 12; |
| 361 | case tok::lessequal: |
| 362 | case tok::less: |
| 363 | case tok::greaterequal: |
| 364 | case tok::greater: return 11; |
| 365 | case tok::exclaimequal: |
| 366 | case tok::equalequal: return 10; |
| 367 | case tok::amp: return 9; |
| 368 | case tok::caret: return 8; |
| 369 | case tok::pipe: return 7; |
| 370 | case tok::ampamp: return 6; |
| 371 | case tok::pipepipe: return 5; |
Chris Lattner | 98ed49f | 2008-05-05 20:07:41 +0000 | [diff] [blame] | 372 | case tok::question: return 4; |
| 373 | case tok::comma: return 3; |
Chris Lattner | 9189156 | 2008-05-04 18:36:18 +0000 | [diff] [blame] | 374 | case tok::colon: return 2; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 375 | case tok::r_paren: return 0; // Lowest priority, end of expr. |
| 376 | case tok::eom: return 0; // Lowest priority, end of macro. |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | |
| 381 | /// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 382 | /// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 383 | /// |
| 384 | /// If ValueLive is false, then this value is being evaluated in a context where |
| 385 | /// the result is not used. As such, avoid diagnostics that relate to |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 386 | /// evaluation, such as division by zero warnings. |
| 387 | static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec, |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 388 | Token &PeekTok, bool ValueLive, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 389 | Preprocessor &PP) { |
| 390 | unsigned PeekPrec = getPrecedence(PeekTok.getKind()); |
| 391 | // If this token isn't valid, report the error. |
| 392 | if (PeekPrec == ~0U) { |
Chris Lattner | 204b2fe | 2008-11-18 21:48:13 +0000 | [diff] [blame] | 393 | PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop) |
| 394 | << LHS.getRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 395 | return true; |
| 396 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 397 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 398 | while (1) { |
| 399 | // If this token has a lower precedence than we are allowed to parse, return |
| 400 | // it so that higher levels of the recursion can parse it. |
| 401 | if (PeekPrec < MinPrec) |
| 402 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 403 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 404 | tok::TokenKind Operator = PeekTok.getKind(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 406 | // 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] | 407 | // dead. Note that this cannot just clobber ValueLive. Consider |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 408 | // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In |
| 409 | // this example, the RHS of the && being dead does not make the rest of the |
| 410 | // expr dead. |
| 411 | bool RHSIsLive; |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 412 | if (Operator == tok::ampamp && LHS.Val == 0) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 413 | RHSIsLive = false; // RHS of "0 && x" is dead. |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 414 | else if (Operator == tok::pipepipe && LHS.Val != 0) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 415 | RHSIsLive = false; // RHS of "1 || x" is dead. |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 416 | else if (Operator == tok::question && LHS.Val == 0) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 417 | RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead. |
| 418 | else |
| 419 | RHSIsLive = ValueLive; |
| 420 | |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 421 | // Consume the operator, remembering the operator's location for reporting. |
| 422 | SourceLocation OpLoc = PeekTok.getLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 423 | PP.LexNonComment(PeekTok); |
| 424 | |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 425 | PPValue RHS(LHS.getBitWidth()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 426 | // Parse the RHS of the operator. |
| 427 | DefinedTracker DT; |
| 428 | if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true; |
| 429 | |
| 430 | // Remember the precedence of this operator and get the precedence of the |
| 431 | // operator immediately to the right of the RHS. |
| 432 | unsigned ThisPrec = PeekPrec; |
| 433 | PeekPrec = getPrecedence(PeekTok.getKind()); |
| 434 | |
| 435 | // If this token isn't valid, report the error. |
| 436 | if (PeekPrec == ~0U) { |
Chris Lattner | 204b2fe | 2008-11-18 21:48:13 +0000 | [diff] [blame] | 437 | PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop) |
| 438 | << RHS.getRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 439 | return true; |
| 440 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | |
Chris Lattner | 98ed49f | 2008-05-05 20:07:41 +0000 | [diff] [blame] | 442 | // Decide whether to include the next binop in this subexpression. For |
| 443 | // 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] | 444 | // handle y*z as a single subexpression. We do this because the precedence |
| 445 | // of * is higher than that of +. The only strange case we have to handle |
| 446 | // here is for the ?: operator, where the precedence is actually lower than |
| 447 | // the LHS of the '?'. The grammar rule is: |
Chris Lattner | 98ed49f | 2008-05-05 20:07:41 +0000 | [diff] [blame] | 448 | // |
| 449 | // conditional-expression ::= |
| 450 | // logical-OR-expression ? expression : conditional-expression |
| 451 | // where 'expression' is actually comma-expression. |
| 452 | unsigned RHSPrec; |
| 453 | if (Operator == tok::question) |
| 454 | // The RHS of "?" should be maximally consumed as an expression. |
| 455 | RHSPrec = getPrecedence(tok::comma); |
| 456 | else // All others should munch while higher precedence. |
| 457 | RHSPrec = ThisPrec+1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 458 | |
Chris Lattner | 98ed49f | 2008-05-05 20:07:41 +0000 | [diff] [blame] | 459 | if (PeekPrec >= RHSPrec) { |
| 460 | if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 461 | return true; |
| 462 | PeekPrec = getPrecedence(PeekTok.getKind()); |
| 463 | } |
| 464 | assert(PeekPrec <= ThisPrec && "Recursion didn't work!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 465 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 466 | // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | // either operand is unsigned. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 468 | llvm::APSInt Res(LHS.getBitWidth()); |
Chris Lattner | 019ef7e | 2008-05-04 23:46:17 +0000 | [diff] [blame] | 469 | switch (Operator) { |
| 470 | case tok::question: // No UAC for x and y in "x ? y : z". |
| 471 | case tok::lessless: // Shift amount doesn't UAC with shift value. |
| 472 | case tok::greatergreater: // Shift amount doesn't UAC with shift value. |
| 473 | case tok::comma: // Comma operands are not subject to UACs. |
| 474 | case tok::pipepipe: // Logical || does not do UACs. |
| 475 | case tok::ampamp: // Logical && does not do UACs. |
| 476 | break; // No UAC |
| 477 | default: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 478 | Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned()); |
| 479 | // If this just promoted something from signed to unsigned, and if the |
| 480 | // value was negative, warn about it. |
| 481 | if (ValueLive && Res.isUnsigned()) { |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 482 | if (!LHS.isUnsigned() && LHS.Val.isNegative()) |
Chris Lattner | 204b2fe | 2008-11-18 21:48:13 +0000 | [diff] [blame] | 483 | PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive) |
| 484 | << LHS.Val.toString(10, true) + " to " + |
| 485 | LHS.Val.toString(10, false) |
| 486 | << LHS.getRange() << RHS.getRange(); |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 487 | if (!RHS.isUnsigned() && RHS.Val.isNegative()) |
Chris Lattner | 204b2fe | 2008-11-18 21:48:13 +0000 | [diff] [blame] | 488 | PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive) |
| 489 | << RHS.Val.toString(10, true) + " to " + |
| 490 | RHS.Val.toString(10, false) |
| 491 | << LHS.getRange() << RHS.getRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 492 | } |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 493 | LHS.Val.setIsUnsigned(Res.isUnsigned()); |
| 494 | RHS.Val.setIsUnsigned(Res.isUnsigned()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 495 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 497 | // FIXME: All of these should detect and report overflow?? |
| 498 | bool Overflow = false; |
| 499 | switch (Operator) { |
| 500 | default: assert(0 && "Unknown operator token!"); |
| 501 | case tok::percent: |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 502 | if (RHS.Val != 0) |
| 503 | Res = LHS.Val % RHS.Val; |
| 504 | else if (ValueLive) { |
Chris Lattner | 204b2fe | 2008-11-18 21:48:13 +0000 | [diff] [blame] | 505 | PP.Diag(OpLoc, diag::err_pp_remainder_by_zero) |
| 506 | << LHS.getRange() << RHS.getRange(); |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 507 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 508 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 509 | break; |
| 510 | case tok::slash: |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 511 | if (RHS.Val != 0) { |
| 512 | Res = LHS.Val / RHS.Val; |
| 513 | if (LHS.Val.isSigned()) // MININT/-1 --> overflow. |
| 514 | Overflow = LHS.Val.isMinSignedValue() && RHS.Val.isAllOnesValue(); |
| 515 | } else if (ValueLive) { |
Chris Lattner | 204b2fe | 2008-11-18 21:48:13 +0000 | [diff] [blame] | 516 | PP.Diag(OpLoc, diag::err_pp_division_by_zero) |
| 517 | << LHS.getRange() << RHS.getRange(); |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 518 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 519 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 520 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 521 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 522 | case tok::star: |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 523 | Res = LHS.Val * RHS.Val; |
Eli Friedman | 3265a42 | 2009-05-16 21:24:10 +0000 | [diff] [blame] | 524 | if (Res.isSigned() && LHS.Val != 0 && RHS.Val != 0) |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 525 | Overflow = Res/RHS.Val != LHS.Val || Res/LHS.Val != RHS.Val; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 526 | break; |
| 527 | case tok::lessless: { |
| 528 | // Determine whether overflow is about to happen. |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 529 | unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue()); |
| 530 | if (ShAmt >= LHS.Val.getBitWidth()) |
| 531 | Overflow = true, ShAmt = LHS.Val.getBitWidth()-1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 532 | else if (LHS.isUnsigned()) |
Eli Friedman | 3265a42 | 2009-05-16 21:24:10 +0000 | [diff] [blame] | 533 | Overflow = false; |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 534 | else if (LHS.Val.isNonNegative()) // Don't allow sign change. |
| 535 | Overflow = ShAmt >= LHS.Val.countLeadingZeros(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 536 | else |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 537 | Overflow = ShAmt >= LHS.Val.countLeadingOnes(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 538 | |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 539 | Res = LHS.Val << ShAmt; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 540 | break; |
| 541 | } |
| 542 | case tok::greatergreater: { |
| 543 | // Determine whether overflow is about to happen. |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 544 | unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 545 | if (ShAmt >= LHS.getBitWidth()) |
| 546 | Overflow = true, ShAmt = LHS.getBitWidth()-1; |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 547 | Res = LHS.Val >> ShAmt; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 548 | break; |
| 549 | } |
| 550 | case tok::plus: |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 551 | Res = LHS.Val + RHS.Val; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 552 | if (LHS.isUnsigned()) |
Eli Friedman | 3265a42 | 2009-05-16 21:24:10 +0000 | [diff] [blame] | 553 | Overflow = false; |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 554 | else if (LHS.Val.isNonNegative() == RHS.Val.isNonNegative() && |
| 555 | Res.isNonNegative() != LHS.Val.isNonNegative()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 556 | Overflow = true; // Overflow for signed addition. |
| 557 | break; |
| 558 | case tok::minus: |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 559 | Res = LHS.Val - RHS.Val; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 560 | if (LHS.isUnsigned()) |
Eli Friedman | 3265a42 | 2009-05-16 21:24:10 +0000 | [diff] [blame] | 561 | Overflow = false; |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 562 | else if (LHS.Val.isNonNegative() != RHS.Val.isNonNegative() && |
| 563 | Res.isNonNegative() != LHS.Val.isNonNegative()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 564 | Overflow = true; // Overflow for signed subtraction. |
| 565 | break; |
| 566 | case tok::lessequal: |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 567 | Res = LHS.Val <= RHS.Val; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 568 | Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed) |
| 569 | break; |
| 570 | case tok::less: |
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::greaterequal: |
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::greater: |
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::exclaimequal: |
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.9p3, result is always int (signed) |
| 585 | break; |
| 586 | case tok::equalequal: |
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::amp: |
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 | break; |
| 593 | case tok::caret: |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 594 | Res = LHS.Val ^ RHS.Val; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 595 | break; |
| 596 | case tok::pipe: |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 597 | Res = LHS.Val | RHS.Val; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 598 | break; |
| 599 | case tok::ampamp: |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 600 | Res = (LHS.Val != 0 && RHS.Val != 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 601 | Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed) |
| 602 | break; |
| 603 | case tok::pipepipe: |
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.14p3, result is always int (signed) |
| 606 | break; |
| 607 | case tok::comma: |
Chris Lattner | 9189156 | 2008-05-04 18:36:18 +0000 | [diff] [blame] | 608 | // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99 |
| 609 | // if not being evaluated. |
| 610 | if (!PP.getLangOptions().C99 || ValueLive) |
Chris Lattner | 204b2fe | 2008-11-18 21:48:13 +0000 | [diff] [blame] | 611 | PP.Diag(OpLoc, diag::ext_pp_comma_expr) |
| 612 | << LHS.getRange() << RHS.getRange(); |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 613 | Res = RHS.Val; // LHS = LHS,RHS -> RHS. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 614 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 615 | case tok::question: { |
| 616 | // Parse the : part of the expression. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 617 | if (PeekTok.isNot(tok::colon)) { |
Chris Lattner | 204b2fe | 2008-11-18 21:48:13 +0000 | [diff] [blame] | 618 | PP.Diag(PeekTok.getLocation(), diag::err_expected_colon) |
| 619 | << LHS.getRange(), RHS.getRange(); |
Chris Lattner | 28eb7e9 | 2008-11-23 23:17:07 +0000 | [diff] [blame] | 620 | PP.Diag(OpLoc, diag::note_matching) << "?"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 621 | return true; |
| 622 | } |
| 623 | // Consume the :. |
| 624 | PP.LexNonComment(PeekTok); |
| 625 | |
| 626 | // Evaluate the value after the :. |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 627 | bool AfterColonLive = ValueLive && LHS.Val == 0; |
| 628 | PPValue AfterColonVal(LHS.getBitWidth()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 629 | DefinedTracker DT; |
| 630 | if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP)) |
| 631 | return true; |
| 632 | |
Chris Lattner | 44cbbb0 | 2008-05-05 20:09:27 +0000 | [diff] [blame] | 633 | // Parse anything after the : with the same precedence as ?. We allow |
| 634 | // things of equal precedence because ?: is right associative. |
Chris Lattner | 98ed49f | 2008-05-05 20:07:41 +0000 | [diff] [blame] | 635 | if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 636 | PeekTok, AfterColonLive, PP)) |
| 637 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 639 | // 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] | 640 | Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val; |
| 641 | RHS.setEnd(AfterColonVal.getRange().getEnd()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 642 | |
| 643 | // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if |
| 644 | // either operand is unsigned. |
| 645 | Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 646 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 647 | // Figure out the precedence of the token after the : part. |
| 648 | PeekPrec = getPrecedence(PeekTok.getKind()); |
| 649 | break; |
| 650 | } |
| 651 | case tok::colon: |
| 652 | // Don't allow :'s to float around without being part of ?: exprs. |
Chris Lattner | 204b2fe | 2008-11-18 21:48:13 +0000 | [diff] [blame] | 653 | PP.Diag(OpLoc, diag::err_pp_colon_without_question) |
| 654 | << LHS.getRange() << RHS.getRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 655 | return true; |
| 656 | } |
| 657 | |
| 658 | // If this operator is live and overflowed, report the issue. |
| 659 | if (Overflow && ValueLive) |
Chris Lattner | 204b2fe | 2008-11-18 21:48:13 +0000 | [diff] [blame] | 660 | PP.Diag(OpLoc, diag::warn_pp_expr_overflow) |
| 661 | << LHS.getRange() << RHS.getRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 662 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 663 | // Put the result back into 'LHS' for our next iteration. |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 664 | LHS.Val = Res; |
| 665 | LHS.setEnd(RHS.getRange().getEnd()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 666 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 667 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 668 | return false; |
| 669 | } |
| 670 | |
| 671 | /// EvaluateDirectiveExpression - Evaluate an integer constant expression that |
| 672 | /// may occur after a #if or #elif directive. If the expression is equivalent |
| 673 | /// to "!defined(X)" return X in IfNDefMacro. |
| 674 | bool Preprocessor:: |
| 675 | EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) { |
Chris Lattner | a3e008a | 2009-12-14 05:00:18 +0000 | [diff] [blame] | 676 | // Save the current state of 'DisableMacroExpansion' and reset it to false. If |
| 677 | // 'DisableMacroExpansion' is true, then we must be in a macro argument list |
| 678 | // in which case a directive is undefined behavior. We want macros to be able |
| 679 | // to recursively expand in order to get more gcc-list behavior, so we force |
| 680 | // DisableMacroExpansion to false and restore it when we're done parsing the |
| 681 | // expression. |
| 682 | bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion; |
| 683 | DisableMacroExpansion = false; |
| 684 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 685 | // Peek ahead one token. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 686 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 687 | Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 688 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 689 | // 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] | 690 | unsigned BitWidth = getTargetInfo().getIntMaxTWidth(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 691 | |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 692 | PPValue ResVal(BitWidth); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 693 | DefinedTracker DT; |
| 694 | if (EvaluateValue(ResVal, Tok, DT, true, *this)) { |
| 695 | // Parse error, skip the rest of the macro line. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 696 | if (Tok.isNot(tok::eom)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 697 | DiscardUntilEndOfDirective(); |
Chris Lattner | a3e008a | 2009-12-14 05:00:18 +0000 | [diff] [blame] | 698 | |
| 699 | // Restore 'DisableMacroExpansion'. |
| 700 | DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 701 | return false; |
| 702 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 703 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 704 | // If we are at the end of the expression after just parsing a value, there |
| 705 | // must be no (unparenthesized) binary operators involved, so we can exit |
| 706 | // directly. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 707 | if (Tok.is(tok::eom)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 708 | // If the expression we parsed was of the form !defined(macro), return the |
| 709 | // macro in IfNDefMacro. |
| 710 | if (DT.State == DefinedTracker::NotDefinedMacro) |
| 711 | IfNDefMacro = DT.TheMacro; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 712 | |
Chris Lattner | a3e008a | 2009-12-14 05:00:18 +0000 | [diff] [blame] | 713 | // Restore 'DisableMacroExpansion'. |
| 714 | DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective; |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 715 | return ResVal.Val != 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 | // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the |
| 719 | // operator and the stuff after it. |
Chris Lattner | 98ed49f | 2008-05-05 20:07:41 +0000 | [diff] [blame] | 720 | if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question), |
| 721 | Tok, true, *this)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 722 | // Parse error, skip the rest of the macro line. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 723 | if (Tok.isNot(tok::eom)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 724 | DiscardUntilEndOfDirective(); |
Chris Lattner | a3e008a | 2009-12-14 05:00:18 +0000 | [diff] [blame] | 725 | |
| 726 | // Restore 'DisableMacroExpansion'. |
| 727 | DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 728 | return false; |
| 729 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 731 | // If we aren't at the tok::eom token, something bad happened, like an extra |
| 732 | // ')' token. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 733 | if (Tok.isNot(tok::eom)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 734 | Diag(Tok, diag::err_pp_expected_eol); |
| 735 | DiscardUntilEndOfDirective(); |
| 736 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 737 | |
Chris Lattner | a3e008a | 2009-12-14 05:00:18 +0000 | [diff] [blame] | 738 | // Restore 'DisableMacroExpansion'. |
| 739 | DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective; |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 740 | return ResVal.Val != 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 741 | } |
| 742 | |