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