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