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