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