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