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