blob: 6c9212632f197b8a226e4545fad413a8bac22907 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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"
22#include "clang/Basic/TargetInfo.h"
Chris Lattner500d3292009-01-29 05:15:15 +000023#include "clang/Lex/LexDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "llvm/ADT/APSInt.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
Chris Lattner8ed30442008-05-05 06:45:50 +000027/// PPValue - Represents the value of a subexpression of a preprocessor
28/// conditional and the source range covered by it.
29class PPValue {
30 SourceRange Range;
31public:
32 llvm::APSInt Val;
Mike Stump1eb44332009-09-09 15:08:12 +000033
Chris Lattner8ed30442008-05-05 06:45:50 +000034 // Default ctor - Construct an 'invalid' PPValue.
35 PPValue(unsigned BitWidth) : Val(BitWidth) {}
Mike Stump1eb44332009-09-09 15:08:12 +000036
Chris Lattner8ed30442008-05-05 06:45:50 +000037 unsigned getBitWidth() const { return Val.getBitWidth(); }
38 bool isUnsigned() const { return Val.isUnsigned(); }
Mike Stump1eb44332009-09-09 15:08:12 +000039
Chris Lattner8ed30442008-05-05 06:45:50 +000040 const SourceRange &getRange() const { return Range; }
Mike Stump1eb44332009-09-09 15:08:12 +000041
Chris Lattner8ed30442008-05-05 06:45:50 +000042 void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
43 void setRange(SourceLocation B, SourceLocation E) {
Mike Stump1eb44332009-09-09 15:08:12 +000044 Range.setBegin(B); Range.setEnd(E);
Chris Lattner8ed30442008-05-05 06:45:50 +000045 }
46 void setBegin(SourceLocation L) { Range.setBegin(L); }
47 void setEnd(SourceLocation L) { Range.setEnd(L); }
48};
49
50static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +000051 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +000052 Preprocessor &PP);
53
54/// DefinedTracker - This struct is used while parsing expressions to keep track
55/// of whether !defined(X) has been seen.
56///
57/// With this simple scheme, we handle the basic forms:
58/// !defined(X) and !defined X
59/// but we also trivially handle (silly) stuff like:
60/// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
61struct DefinedTracker {
62 /// Each time a Value is evaluated, it returns information about whether the
63 /// parsed value is of the form defined(X), !defined(X) or is something else.
64 enum TrackerState {
65 DefinedMacro, // defined(X)
66 NotDefinedMacro, // !defined(X)
67 Unknown // Something else.
68 } State;
69 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
70 /// indicates the macro that was checked.
71 IdentifierInfo *TheMacro;
72};
73
John Thompsona28cc092009-10-30 13:49:06 +000074/// EvaluateDefined - Process a 'defined(sym)' expression.
Chris Lattnera3e008a2009-12-14 05:00:18 +000075static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
76 bool ValueLive, Preprocessor &PP) {
John Thompsona28cc092009-10-30 13:49:06 +000077 IdentifierInfo *II;
78 Result.setBegin(PeekTok.getLocation());
79
80 // Get the next token, don't expand it.
81 PP.LexUnexpandedToken(PeekTok);
82
83 // Two options, it can either be a pp-identifier or a (.
84 SourceLocation LParenLoc;
85 if (PeekTok.is(tok::l_paren)) {
86 // Found a paren, remember we saw it and skip it.
87 LParenLoc = PeekTok.getLocation();
88 PP.LexUnexpandedToken(PeekTok);
89 }
90
91 // If we don't have a pp-identifier now, this is an error.
92 if ((II = PeekTok.getIdentifierInfo()) == 0) {
93 PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier);
94 return true;
95 }
96
97 // Otherwise, we got an identifier, is it defined to something?
98 Result.Val = II->hasMacroDefinition();
99 Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
100
101 // If there is a macro, mark it used.
102 if (Result.Val != 0 && ValueLive) {
103 MacroInfo *Macro = PP.getMacroInfo(II);
104 Macro->setIsUsed(true);
105 }
106
107 // Consume identifier.
108 Result.setEnd(PeekTok.getLocation());
Chris Lattner19943152010-02-26 19:42:53 +0000109 PP.LexUnexpandedToken(PeekTok);
John Thompsona28cc092009-10-30 13:49:06 +0000110
111 // If we are in parens, ensure we have a trailing ).
112 if (LParenLoc.isValid()) {
113 if (PeekTok.isNot(tok::r_paren)) {
114 PP.Diag(PeekTok.getLocation(), diag::err_pp_missing_rparen) << "defined";
115 PP.Diag(LParenLoc, diag::note_matching) << "(";
116 return true;
117 }
118 // Consume the ).
119 Result.setEnd(PeekTok.getLocation());
120 PP.LexNonComment(PeekTok);
121 }
122
123 // Success, remember that we saw defined(X).
124 DT.State = DefinedTracker::DefinedMacro;
125 DT.TheMacro = II;
126 return false;
127}
128
Reid Spencer5f016e22007-07-11 17:01:13 +0000129/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
130/// return the computed value in Result. Return true if there was an error
131/// parsing. This function also returns information about the form of the
132/// expression in DT. See above for information on what DT means.
133///
134/// If ValueLive is false, then this value is being evaluated in a context where
135/// the result is not used. As such, avoid diagnostics that relate to
136/// evaluation.
Chris Lattner8ed30442008-05-05 06:45:50 +0000137static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
138 bool ValueLive, Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 DT.State = DefinedTracker::Unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 // If this token's spelling is a pp-identifier, check to see if it is
142 // 'defined' or if it is a macro. Note that we check here because many
143 // keywords are pp-identifiers, so we can't check the kind.
144 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
Chris Lattnerf8806622009-12-14 04:26:45 +0000145 // Handle "defined X" and "defined(X)".
146 if (II->isStr("defined"))
John Thompsona28cc092009-10-30 13:49:06 +0000147 return(EvaluateDefined(Result, PeekTok, DT, ValueLive, PP));
Chris Lattnerf8806622009-12-14 04:26:45 +0000148
149 // If this identifier isn't 'defined' or one of the special
150 // preprocessor keywords and it wasn't macro expanded, it turns
151 // into a simple 0, unless it is the C++ keyword "true", in which case it
152 // turns into "1".
153 if (ValueLive)
154 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
155 Result.Val = II->getTokenID() == tok::kw_true;
156 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
157 Result.setRange(PeekTok.getLocation());
158 PP.LexNonComment(PeekTok);
159 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 }
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 switch (PeekTok.getKind()) {
163 default: // Non-value token.
Chris Lattnerd98d9752008-04-13 20:38:43 +0000164 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 return true;
166 case tok::eom:
167 case tok::r_paren:
168 // If there is no expression, report and exit.
169 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
170 return true;
171 case tok::numeric_constant: {
172 llvm::SmallString<64> IntegerBuffer;
173 IntegerBuffer.resize(PeekTok.getLength());
174 const char *ThisTokBegin = &IntegerBuffer[0];
175 unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
Mike Stump1eb44332009-09-09 15:08:12 +0000176 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 PeekTok.getLocation(), PP);
178 if (Literal.hadError)
179 return true; // a diagnostic was already reported.
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Chris Lattner6e400c22007-08-26 03:29:23 +0000181 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
183 return true;
184 }
185 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
186
Neil Boothb9449512007-08-29 22:00:19 +0000187 // long long is a C99 feature.
188 if (!PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus0x
Neil Booth79859c32007-08-29 22:13:52 +0000189 && Literal.isLongLong)
Neil Boothb9449512007-08-29 22:00:19 +0000190 PP.Diag(PeekTok, diag::ext_longlong);
191
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 // Parse the integer literal into Result.
Chris Lattner8ed30442008-05-05 06:45:50 +0000193 if (Literal.GetIntegerValue(Result.Val)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 // Overflow parsing integer literal.
195 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattner8ed30442008-05-05 06:45:50 +0000196 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 } else {
198 // Set the signedness of the result to match whether there was a U suffix
199 // or not.
Chris Lattner8ed30442008-05-05 06:45:50 +0000200 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 // Detect overflow based on whether the value is signed. If signed
203 // and if the value is too large, emit a warning "integer constant is so
204 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
205 // is 64-bits.
Chris Lattner8ed30442008-05-05 06:45:50 +0000206 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Chris Lattnerb081a352008-07-03 03:47:30 +0000207 // Don't warn for a hex literal: 0x8000..0 shouldn't warn.
208 if (ValueLive && Literal.getRadix() != 16)
Chris Lattner8ed30442008-05-05 06:45:50 +0000209 PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
210 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 }
212 }
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000215 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 PP.LexNonComment(PeekTok);
217 return false;
218 }
219 case tok::char_constant: { // 'x'
220 llvm::SmallString<32> CharBuffer;
Benjamin Kramerddeea562010-02-27 13:44:12 +0000221 llvm::StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer);
222
223 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 PeekTok.getLocation(), PP);
225 if (Literal.hadError())
226 return true; // A diagnostic was already emitted.
227
228 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar444be732009-11-13 05:51:54 +0000229 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000230 unsigned NumBits;
231 if (Literal.isMultiChar())
232 NumBits = TI.getIntWidth();
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000233 else if (Literal.isWide())
234 NumBits = TI.getWCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000235 else
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000236 NumBits = TI.getCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000237
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 // Set the width.
239 llvm::APSInt Val(NumBits);
240 // Set the value.
241 Val = Literal.getValue();
242 // Set the signedness.
Eli Friedman15b91762009-06-05 07:05:05 +0000243 Val.setIsUnsigned(!PP.getLangOptions().CharIsSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Chris Lattner8ed30442008-05-05 06:45:50 +0000245 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
246 Result.Val = Val.extend(Result.Val.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000248 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 "intmax_t smaller than char/wchar_t?");
Chris Lattner8ed30442008-05-05 06:45:50 +0000250 Result.Val = Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 }
252
253 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000254 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 PP.LexNonComment(PeekTok);
256 return false;
257 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000258 case tok::l_paren: {
259 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 PP.LexNonComment(PeekTok); // Eat the (.
261 // Parse the value and if there are any binary operators involved, parse
262 // them.
263 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
264
265 // If this is a silly value like (X), which doesn't need parens, check for
266 // !(defined X).
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000267 if (PeekTok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000268 // Just use DT unmodified as our result.
269 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000270 // Otherwise, we have something like (x+y), and we consumed '(x'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
272 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000274 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000275 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
276 << Result.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000277 PP.Diag(Start, diag::note_matching) << "(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 return true;
279 }
280 DT.State = DefinedTracker::Unknown;
281 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000282 Result.setRange(Start, PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 PP.LexNonComment(PeekTok); // Eat the ).
284 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000285 }
286 case tok::plus: {
287 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 // Unary plus doesn't modify the value.
289 PP.LexNonComment(PeekTok);
Chris Lattner8ed30442008-05-05 06:45:50 +0000290 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
291 Result.setBegin(Start);
292 return false;
293 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 case tok::minus: {
295 SourceLocation Loc = PeekTok.getLocation();
296 PP.LexNonComment(PeekTok);
297 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000298 Result.setBegin(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000301 Result.Val = -Result.Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Chris Lattnerb081a352008-07-03 03:47:30 +0000303 // -MININT is the only thing that overflows. Unsigned never overflows.
304 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 // If this operator is live and overflowed, report the issue.
307 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000308 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 DT.State = DefinedTracker::Unknown;
311 return false;
312 }
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Chris Lattner8ed30442008-05-05 06:45:50 +0000314 case tok::tilde: {
315 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 PP.LexNonComment(PeekTok);
317 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000318 Result.setBegin(Start);
319
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000321 Result.Val = ~Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 DT.State = DefinedTracker::Unknown;
323 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000324 }
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Chris Lattner8ed30442008-05-05 06:45:50 +0000326 case tok::exclaim: {
327 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000328 PP.LexNonComment(PeekTok);
329 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000330 Result.setBegin(Start);
331 Result.Val = !Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner8ed30442008-05-05 06:45:50 +0000333 Result.Val.setIsUnsigned(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 if (DT.State == DefinedTracker::DefinedMacro)
336 DT.State = DefinedTracker::NotDefinedMacro;
337 else if (DT.State == DefinedTracker::NotDefinedMacro)
338 DT.State = DefinedTracker::DefinedMacro;
339 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000340 }
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 // FIXME: Handle #assert
343 }
344}
345
346
347
348/// getPrecedence - Return the precedence of the specified binary operator
349/// token. This returns:
350/// ~0 - Invalid token.
Chris Lattner9e66ba62008-05-05 04:10:51 +0000351/// 14 -> 3 - various operators.
352/// 0 - 'eom' or ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000353static unsigned getPrecedence(tok::TokenKind Kind) {
354 switch (Kind) {
355 default: return ~0U;
356 case tok::percent:
357 case tok::slash:
358 case tok::star: return 14;
359 case tok::plus:
360 case tok::minus: return 13;
361 case tok::lessless:
362 case tok::greatergreater: return 12;
363 case tok::lessequal:
364 case tok::less:
365 case tok::greaterequal:
366 case tok::greater: return 11;
367 case tok::exclaimequal:
368 case tok::equalequal: return 10;
369 case tok::amp: return 9;
370 case tok::caret: return 8;
371 case tok::pipe: return 7;
372 case tok::ampamp: return 6;
373 case tok::pipepipe: return 5;
Chris Lattner98ed49f2008-05-05 20:07:41 +0000374 case tok::question: return 4;
375 case tok::comma: return 3;
Chris Lattner91891562008-05-04 18:36:18 +0000376 case tok::colon: return 2;
Reid Spencer5f016e22007-07-11 17:01:13 +0000377 case tok::r_paren: return 0; // Lowest priority, end of expr.
378 case tok::eom: return 0; // Lowest priority, end of macro.
379 }
380}
381
382
383/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner8ed30442008-05-05 06:45:50 +0000384/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Reid Spencer5f016e22007-07-11 17:01:13 +0000385///
386/// If ValueLive is false, then this value is being evaluated in a context where
387/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner8ed30442008-05-05 06:45:50 +0000388/// evaluation, such as division by zero warnings.
389static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +0000390 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 Preprocessor &PP) {
392 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
393 // If this token isn't valid, report the error.
394 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000395 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
396 << LHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 return true;
398 }
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 while (1) {
401 // If this token has a lower precedence than we are allowed to parse, return
402 // it so that higher levels of the recursion can parse it.
403 if (PeekPrec < MinPrec)
404 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump1eb44332009-09-09 15:08:12 +0000409 // dead. Note that this cannot just clobber ValueLive. Consider
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
411 // this example, the RHS of the && being dead does not make the rest of the
412 // expr dead.
413 bool RHSIsLive;
Chris Lattner8ed30442008-05-05 06:45:50 +0000414 if (Operator == tok::ampamp && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000415 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000416 else if (Operator == tok::pipepipe && LHS.Val != 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000418 else if (Operator == tok::question && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000419 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
420 else
421 RHSIsLive = ValueLive;
422
Chris Lattner8ed30442008-05-05 06:45:50 +0000423 // Consume the operator, remembering the operator's location for reporting.
424 SourceLocation OpLoc = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 PP.LexNonComment(PeekTok);
426
Chris Lattner8ed30442008-05-05 06:45:50 +0000427 PPValue RHS(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000428 // Parse the RHS of the operator.
429 DefinedTracker DT;
430 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
431
432 // Remember the precedence of this operator and get the precedence of the
433 // operator immediately to the right of the RHS.
434 unsigned ThisPrec = PeekPrec;
435 PeekPrec = getPrecedence(PeekTok.getKind());
436
437 // If this token isn't valid, report the error.
438 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000439 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
440 << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000441 return true;
442 }
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Chris Lattner98ed49f2008-05-05 20:07:41 +0000444 // Decide whether to include the next binop in this subexpression. For
445 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattner44cbbb02008-05-05 20:09:27 +0000446 // handle y*z as a single subexpression. We do this because the precedence
447 // of * is higher than that of +. The only strange case we have to handle
448 // here is for the ?: operator, where the precedence is actually lower than
449 // the LHS of the '?'. The grammar rule is:
Chris Lattner98ed49f2008-05-05 20:07:41 +0000450 //
451 // conditional-expression ::=
452 // logical-OR-expression ? expression : conditional-expression
453 // where 'expression' is actually comma-expression.
454 unsigned RHSPrec;
455 if (Operator == tok::question)
456 // The RHS of "?" should be maximally consumed as an expression.
457 RHSPrec = getPrecedence(tok::comma);
458 else // All others should munch while higher precedence.
459 RHSPrec = ThisPrec+1;
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Chris Lattner98ed49f2008-05-05 20:07:41 +0000461 if (PeekPrec >= RHSPrec) {
462 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Reid Spencer5f016e22007-07-11 17:01:13 +0000463 return true;
464 PeekPrec = getPrecedence(PeekTok.getKind());
465 }
466 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump1eb44332009-09-09 15:08:12 +0000469 // either operand is unsigned.
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattner019ef7e2008-05-04 23:46:17 +0000471 switch (Operator) {
472 case tok::question: // No UAC for x and y in "x ? y : z".
473 case tok::lessless: // Shift amount doesn't UAC with shift value.
474 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
475 case tok::comma: // Comma operands are not subject to UACs.
476 case tok::pipepipe: // Logical || does not do UACs.
477 case tok::ampamp: // Logical && does not do UACs.
478 break; // No UAC
479 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000480 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
481 // If this just promoted something from signed to unsigned, and if the
482 // value was negative, warn about it.
483 if (ValueLive && Res.isUnsigned()) {
Chris Lattner8ed30442008-05-05 06:45:50 +0000484 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000485 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
486 << LHS.Val.toString(10, true) + " to " +
487 LHS.Val.toString(10, false)
488 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000489 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000490 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
491 << RHS.Val.toString(10, true) + " to " +
492 RHS.Val.toString(10, false)
493 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000495 LHS.Val.setIsUnsigned(Res.isUnsigned());
496 RHS.Val.setIsUnsigned(Res.isUnsigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000497 }
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Reid Spencer5f016e22007-07-11 17:01:13 +0000499 // FIXME: All of these should detect and report overflow??
500 bool Overflow = false;
501 switch (Operator) {
502 default: assert(0 && "Unknown operator token!");
503 case tok::percent:
Chris Lattner8ed30442008-05-05 06:45:50 +0000504 if (RHS.Val != 0)
505 Res = LHS.Val % RHS.Val;
506 else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000507 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
508 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000509 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000510 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000511 break;
512 case tok::slash:
Chris Lattner8ed30442008-05-05 06:45:50 +0000513 if (RHS.Val != 0) {
514 Res = LHS.Val / RHS.Val;
515 if (LHS.Val.isSigned()) // MININT/-1 --> overflow.
516 Overflow = LHS.Val.isMinSignedValue() && RHS.Val.isAllOnesValue();
517 } else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000518 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
519 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000520 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000521 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Reid Spencer5f016e22007-07-11 17:01:13 +0000524 case tok::star:
Chris Lattner8ed30442008-05-05 06:45:50 +0000525 Res = LHS.Val * RHS.Val;
Eli Friedman3265a422009-05-16 21:24:10 +0000526 if (Res.isSigned() && LHS.Val != 0 && RHS.Val != 0)
Chris Lattner8ed30442008-05-05 06:45:50 +0000527 Overflow = Res/RHS.Val != LHS.Val || Res/LHS.Val != RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000528 break;
529 case tok::lessless: {
530 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000531 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
532 if (ShAmt >= LHS.Val.getBitWidth())
533 Overflow = true, ShAmt = LHS.Val.getBitWidth()-1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000534 else if (LHS.isUnsigned())
Eli Friedman3265a422009-05-16 21:24:10 +0000535 Overflow = false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000536 else if (LHS.Val.isNonNegative()) // Don't allow sign change.
537 Overflow = ShAmt >= LHS.Val.countLeadingZeros();
Reid Spencer5f016e22007-07-11 17:01:13 +0000538 else
Chris Lattner8ed30442008-05-05 06:45:50 +0000539 Overflow = ShAmt >= LHS.Val.countLeadingOnes();
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Chris Lattner8ed30442008-05-05 06:45:50 +0000541 Res = LHS.Val << ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 break;
543 }
544 case tok::greatergreater: {
545 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000546 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 if (ShAmt >= LHS.getBitWidth())
548 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner8ed30442008-05-05 06:45:50 +0000549 Res = LHS.Val >> ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 break;
551 }
552 case tok::plus:
Chris Lattner8ed30442008-05-05 06:45:50 +0000553 Res = LHS.Val + RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 if (LHS.isUnsigned())
Eli Friedman3265a422009-05-16 21:24:10 +0000555 Overflow = false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000556 else if (LHS.Val.isNonNegative() == RHS.Val.isNonNegative() &&
557 Res.isNonNegative() != LHS.Val.isNonNegative())
Reid Spencer5f016e22007-07-11 17:01:13 +0000558 Overflow = true; // Overflow for signed addition.
559 break;
560 case tok::minus:
Chris Lattner8ed30442008-05-05 06:45:50 +0000561 Res = LHS.Val - RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000562 if (LHS.isUnsigned())
Eli Friedman3265a422009-05-16 21:24:10 +0000563 Overflow = false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000564 else if (LHS.Val.isNonNegative() != RHS.Val.isNonNegative() &&
565 Res.isNonNegative() != LHS.Val.isNonNegative())
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 Overflow = true; // Overflow for signed subtraction.
567 break;
568 case tok::lessequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000569 Res = LHS.Val <= RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000570 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
571 break;
572 case tok::less:
Chris Lattner8ed30442008-05-05 06:45:50 +0000573 Res = LHS.Val < RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
575 break;
576 case tok::greaterequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000577 Res = LHS.Val >= RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000578 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
579 break;
580 case tok::greater:
Chris Lattner8ed30442008-05-05 06:45:50 +0000581 Res = LHS.Val > RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000582 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
583 break;
584 case tok::exclaimequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000585 Res = LHS.Val != RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
587 break;
588 case tok::equalequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000589 Res = LHS.Val == RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000590 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
591 break;
592 case tok::amp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000593 Res = LHS.Val & RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 break;
595 case tok::caret:
Chris Lattner8ed30442008-05-05 06:45:50 +0000596 Res = LHS.Val ^ RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000597 break;
598 case tok::pipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000599 Res = LHS.Val | RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000600 break;
601 case tok::ampamp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000602 Res = (LHS.Val != 0 && RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
604 break;
605 case tok::pipepipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000606 Res = (LHS.Val != 0 || RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
608 break;
609 case tok::comma:
Chris Lattner91891562008-05-04 18:36:18 +0000610 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
611 // if not being evaluated.
612 if (!PP.getLangOptions().C99 || ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000613 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
614 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000615 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump1eb44332009-09-09 15:08:12 +0000616 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 case tok::question: {
618 // Parse the : part of the expression.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000619 if (PeekTok.isNot(tok::colon)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000620 PP.Diag(PeekTok.getLocation(), diag::err_expected_colon)
621 << LHS.getRange(), RHS.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000622 PP.Diag(OpLoc, diag::note_matching) << "?";
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 return true;
624 }
625 // Consume the :.
626 PP.LexNonComment(PeekTok);
627
628 // Evaluate the value after the :.
Chris Lattner8ed30442008-05-05 06:45:50 +0000629 bool AfterColonLive = ValueLive && LHS.Val == 0;
630 PPValue AfterColonVal(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 DefinedTracker DT;
632 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
633 return true;
634
Chris Lattner44cbbb02008-05-05 20:09:27 +0000635 // Parse anything after the : with the same precedence as ?. We allow
636 // things of equal precedence because ?: is right associative.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000637 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 PeekTok, AfterColonLive, PP))
639 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner8ed30442008-05-05 06:45:50 +0000642 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
643 RHS.setEnd(AfterColonVal.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000644
645 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
646 // either operand is unsigned.
647 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 // Figure out the precedence of the token after the : part.
650 PeekPrec = getPrecedence(PeekTok.getKind());
651 break;
652 }
653 case tok::colon:
654 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattner204b2fe2008-11-18 21:48:13 +0000655 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
656 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000657 return true;
658 }
659
660 // If this operator is live and overflowed, report the issue.
661 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000662 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
663 << LHS.getRange() << RHS.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 // Put the result back into 'LHS' for our next iteration.
Chris Lattner8ed30442008-05-05 06:45:50 +0000666 LHS.Val = Res;
667 LHS.setEnd(RHS.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 }
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 return false;
671}
672
673/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
674/// may occur after a #if or #elif directive. If the expression is equivalent
675/// to "!defined(X)" return X in IfNDefMacro.
676bool Preprocessor::
677EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
Chris Lattnera3e008a2009-12-14 05:00:18 +0000678 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
679 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
680 // in which case a directive is undefined behavior. We want macros to be able
681 // to recursively expand in order to get more gcc-list behavior, so we force
682 // DisableMacroExpansion to false and restore it when we're done parsing the
683 // expression.
684 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
685 DisableMacroExpansion = false;
686
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 // Peek ahead one token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000688 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Reid Spencer5f016e22007-07-11 17:01:13 +0000691 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner98be4942008-03-05 18:54:05 +0000692 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Chris Lattner8ed30442008-05-05 06:45:50 +0000694 PPValue ResVal(BitWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 DefinedTracker DT;
696 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
697 // Parse error, skip the rest of the macro line.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000698 if (Tok.isNot(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000699 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000700
701 // Restore 'DisableMacroExpansion'.
702 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000703 return false;
704 }
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Reid Spencer5f016e22007-07-11 17:01:13 +0000706 // If we are at the end of the expression after just parsing a value, there
707 // must be no (unparenthesized) binary operators involved, so we can exit
708 // directly.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000709 if (Tok.is(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 // If the expression we parsed was of the form !defined(macro), return the
711 // macro in IfNDefMacro.
712 if (DT.State == DefinedTracker::NotDefinedMacro)
713 IfNDefMacro = DT.TheMacro;
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Chris Lattnera3e008a2009-12-14 05:00:18 +0000715 // Restore 'DisableMacroExpansion'.
716 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000717 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 }
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
721 // operator and the stuff after it.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000722 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
723 Tok, true, *this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 // Parse error, skip the rest of the macro line.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000725 if (Tok.isNot(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000727
728 // Restore 'DisableMacroExpansion'.
729 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 return false;
731 }
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 // If we aren't at the tok::eom token, something bad happened, like an extra
734 // ')' token.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000735 if (Tok.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 Diag(Tok, diag::err_pp_expected_eol);
737 DiscardUntilEndOfDirective();
738 }
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Chris Lattnera3e008a2009-12-14 05:00:18 +0000740 // Restore 'DisableMacroExpansion'.
741 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000742 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000743}
744