blob: ede129edcb6fe2fc15d0cf3c0dff9b6acbf9c6bc [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;
Benjamin Krameraeb863c2010-02-27 16:29:36 +0000173 llvm::StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer);
174 NumericLiteralParser Literal(Spelling.begin(), Spelling.end(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 PeekTok.getLocation(), PP);
176 if (Literal.hadError)
177 return true; // a diagnostic was already reported.
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Chris Lattner6e400c22007-08-26 03:29:23 +0000179 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
181 return true;
182 }
183 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
184
Neil Boothb9449512007-08-29 22:00:19 +0000185 // long long is a C99 feature.
186 if (!PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus0x
Neil Booth79859c32007-08-29 22:13:52 +0000187 && Literal.isLongLong)
Neil Boothb9449512007-08-29 22:00:19 +0000188 PP.Diag(PeekTok, diag::ext_longlong);
189
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 // Parse the integer literal into Result.
Chris Lattner8ed30442008-05-05 06:45:50 +0000191 if (Literal.GetIntegerValue(Result.Val)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 // Overflow parsing integer literal.
193 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattner8ed30442008-05-05 06:45:50 +0000194 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 } else {
196 // Set the signedness of the result to match whether there was a U suffix
197 // or not.
Chris Lattner8ed30442008-05-05 06:45:50 +0000198 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 // Detect overflow based on whether the value is signed. If signed
201 // and if the value is too large, emit a warning "integer constant is so
202 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
203 // is 64-bits.
Chris Lattner8ed30442008-05-05 06:45:50 +0000204 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Chris Lattnerb081a352008-07-03 03:47:30 +0000205 // Don't warn for a hex literal: 0x8000..0 shouldn't warn.
206 if (ValueLive && Literal.getRadix() != 16)
Chris Lattner8ed30442008-05-05 06:45:50 +0000207 PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
208 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 }
210 }
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000213 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 PP.LexNonComment(PeekTok);
215 return false;
216 }
217 case tok::char_constant: { // 'x'
218 llvm::SmallString<32> CharBuffer;
Benjamin Kramerddeea562010-02-27 13:44:12 +0000219 llvm::StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer);
220
221 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 PeekTok.getLocation(), PP);
223 if (Literal.hadError())
224 return true; // A diagnostic was already emitted.
225
226 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar444be732009-11-13 05:51:54 +0000227 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000228 unsigned NumBits;
229 if (Literal.isMultiChar())
230 NumBits = TI.getIntWidth();
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000231 else if (Literal.isWide())
232 NumBits = TI.getWCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000233 else
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000234 NumBits = TI.getCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000235
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 // Set the width.
237 llvm::APSInt Val(NumBits);
238 // Set the value.
239 Val = Literal.getValue();
240 // Set the signedness.
Eli Friedman15b91762009-06-05 07:05:05 +0000241 Val.setIsUnsigned(!PP.getLangOptions().CharIsSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Chris Lattner8ed30442008-05-05 06:45:50 +0000243 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
244 Result.Val = Val.extend(Result.Val.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000245 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000246 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 "intmax_t smaller than char/wchar_t?");
Chris Lattner8ed30442008-05-05 06:45:50 +0000248 Result.Val = Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 }
250
251 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000252 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 PP.LexNonComment(PeekTok);
254 return false;
255 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000256 case tok::l_paren: {
257 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 PP.LexNonComment(PeekTok); // Eat the (.
259 // Parse the value and if there are any binary operators involved, parse
260 // them.
261 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
262
263 // If this is a silly value like (X), which doesn't need parens, check for
264 // !(defined X).
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000265 if (PeekTok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 // Just use DT unmodified as our result.
267 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000268 // Otherwise, we have something like (x+y), and we consumed '(x'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
270 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000272 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000273 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
274 << Result.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000275 PP.Diag(Start, diag::note_matching) << "(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 return true;
277 }
278 DT.State = DefinedTracker::Unknown;
279 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000280 Result.setRange(Start, PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 PP.LexNonComment(PeekTok); // Eat the ).
282 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000283 }
284 case tok::plus: {
285 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 // Unary plus doesn't modify the value.
287 PP.LexNonComment(PeekTok);
Chris Lattner8ed30442008-05-05 06:45:50 +0000288 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
289 Result.setBegin(Start);
290 return false;
291 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 case tok::minus: {
293 SourceLocation Loc = PeekTok.getLocation();
294 PP.LexNonComment(PeekTok);
295 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000296 Result.setBegin(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000299 Result.Val = -Result.Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Chris Lattnerb081a352008-07-03 03:47:30 +0000301 // -MININT is the only thing that overflows. Unsigned never overflows.
302 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 // If this operator is live and overflowed, report the issue.
305 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000306 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 DT.State = DefinedTracker::Unknown;
309 return false;
310 }
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Chris Lattner8ed30442008-05-05 06:45:50 +0000312 case tok::tilde: {
313 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 PP.LexNonComment(PeekTok);
315 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000316 Result.setBegin(Start);
317
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000319 Result.Val = ~Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 DT.State = DefinedTracker::Unknown;
321 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000322 }
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Chris Lattner8ed30442008-05-05 06:45:50 +0000324 case tok::exclaim: {
325 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 PP.LexNonComment(PeekTok);
327 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000328 Result.setBegin(Start);
329 Result.Val = !Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner8ed30442008-05-05 06:45:50 +0000331 Result.Val.setIsUnsigned(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Reid Spencer5f016e22007-07-11 17:01:13 +0000333 if (DT.State == DefinedTracker::DefinedMacro)
334 DT.State = DefinedTracker::NotDefinedMacro;
335 else if (DT.State == DefinedTracker::NotDefinedMacro)
336 DT.State = DefinedTracker::DefinedMacro;
337 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000338 }
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 // FIXME: Handle #assert
341 }
342}
343
344
345
346/// getPrecedence - Return the precedence of the specified binary operator
347/// token. This returns:
348/// ~0 - Invalid token.
Chris Lattner9e66ba62008-05-05 04:10:51 +0000349/// 14 -> 3 - various operators.
350/// 0 - 'eom' or ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000351static unsigned getPrecedence(tok::TokenKind Kind) {
352 switch (Kind) {
353 default: return ~0U;
354 case tok::percent:
355 case tok::slash:
356 case tok::star: return 14;
357 case tok::plus:
358 case tok::minus: return 13;
359 case tok::lessless:
360 case tok::greatergreater: return 12;
361 case tok::lessequal:
362 case tok::less:
363 case tok::greaterequal:
364 case tok::greater: return 11;
365 case tok::exclaimequal:
366 case tok::equalequal: return 10;
367 case tok::amp: return 9;
368 case tok::caret: return 8;
369 case tok::pipe: return 7;
370 case tok::ampamp: return 6;
371 case tok::pipepipe: return 5;
Chris Lattner98ed49f2008-05-05 20:07:41 +0000372 case tok::question: return 4;
373 case tok::comma: return 3;
Chris Lattner91891562008-05-04 18:36:18 +0000374 case tok::colon: return 2;
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 case tok::r_paren: return 0; // Lowest priority, end of expr.
376 case tok::eom: return 0; // Lowest priority, end of macro.
377 }
378}
379
380
381/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner8ed30442008-05-05 06:45:50 +0000382/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Reid Spencer5f016e22007-07-11 17:01:13 +0000383///
384/// If ValueLive is false, then this value is being evaluated in a context where
385/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner8ed30442008-05-05 06:45:50 +0000386/// evaluation, such as division by zero warnings.
387static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +0000388 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 Preprocessor &PP) {
390 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
391 // If this token isn't valid, report the error.
392 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000393 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
394 << LHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 return true;
396 }
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 while (1) {
399 // If this token has a lower precedence than we are allowed to parse, return
400 // it so that higher levels of the recursion can parse it.
401 if (PeekPrec < MinPrec)
402 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Reid Spencer5f016e22007-07-11 17:01:13 +0000404 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump1eb44332009-09-09 15:08:12 +0000407 // dead. Note that this cannot just clobber ValueLive. Consider
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
409 // this example, the RHS of the && being dead does not make the rest of the
410 // expr dead.
411 bool RHSIsLive;
Chris Lattner8ed30442008-05-05 06:45:50 +0000412 if (Operator == tok::ampamp && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000413 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000414 else if (Operator == tok::pipepipe && LHS.Val != 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000415 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000416 else if (Operator == tok::question && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
418 else
419 RHSIsLive = ValueLive;
420
Chris Lattner8ed30442008-05-05 06:45:50 +0000421 // Consume the operator, remembering the operator's location for reporting.
422 SourceLocation OpLoc = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 PP.LexNonComment(PeekTok);
424
Chris Lattner8ed30442008-05-05 06:45:50 +0000425 PPValue RHS(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000426 // Parse the RHS of the operator.
427 DefinedTracker DT;
428 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
429
430 // Remember the precedence of this operator and get the precedence of the
431 // operator immediately to the right of the RHS.
432 unsigned ThisPrec = PeekPrec;
433 PeekPrec = getPrecedence(PeekTok.getKind());
434
435 // If this token isn't valid, report the error.
436 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000437 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
438 << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000439 return true;
440 }
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Chris Lattner98ed49f2008-05-05 20:07:41 +0000442 // Decide whether to include the next binop in this subexpression. For
443 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattner44cbbb02008-05-05 20:09:27 +0000444 // handle y*z as a single subexpression. We do this because the precedence
445 // of * is higher than that of +. The only strange case we have to handle
446 // here is for the ?: operator, where the precedence is actually lower than
447 // the LHS of the '?'. The grammar rule is:
Chris Lattner98ed49f2008-05-05 20:07:41 +0000448 //
449 // conditional-expression ::=
450 // logical-OR-expression ? expression : conditional-expression
451 // where 'expression' is actually comma-expression.
452 unsigned RHSPrec;
453 if (Operator == tok::question)
454 // The RHS of "?" should be maximally consumed as an expression.
455 RHSPrec = getPrecedence(tok::comma);
456 else // All others should munch while higher precedence.
457 RHSPrec = ThisPrec+1;
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Chris Lattner98ed49f2008-05-05 20:07:41 +0000459 if (PeekPrec >= RHSPrec) {
460 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Reid Spencer5f016e22007-07-11 17:01:13 +0000461 return true;
462 PeekPrec = getPrecedence(PeekTok.getKind());
463 }
464 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Reid Spencer5f016e22007-07-11 17:01:13 +0000466 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump1eb44332009-09-09 15:08:12 +0000467 // either operand is unsigned.
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattner019ef7e2008-05-04 23:46:17 +0000469 switch (Operator) {
470 case tok::question: // No UAC for x and y in "x ? y : z".
471 case tok::lessless: // Shift amount doesn't UAC with shift value.
472 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
473 case tok::comma: // Comma operands are not subject to UACs.
474 case tok::pipepipe: // Logical || does not do UACs.
475 case tok::ampamp: // Logical && does not do UACs.
476 break; // No UAC
477 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000478 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
479 // If this just promoted something from signed to unsigned, and if the
480 // value was negative, warn about it.
481 if (ValueLive && Res.isUnsigned()) {
Chris Lattner8ed30442008-05-05 06:45:50 +0000482 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000483 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
484 << LHS.Val.toString(10, true) + " to " +
485 LHS.Val.toString(10, false)
486 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000487 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000488 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
489 << RHS.Val.toString(10, true) + " to " +
490 RHS.Val.toString(10, false)
491 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000492 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000493 LHS.Val.setIsUnsigned(Res.isUnsigned());
494 RHS.Val.setIsUnsigned(Res.isUnsigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000495 }
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Reid Spencer5f016e22007-07-11 17:01:13 +0000497 // FIXME: All of these should detect and report overflow??
498 bool Overflow = false;
499 switch (Operator) {
500 default: assert(0 && "Unknown operator token!");
501 case tok::percent:
Chris Lattner8ed30442008-05-05 06:45:50 +0000502 if (RHS.Val != 0)
503 Res = LHS.Val % RHS.Val;
504 else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000505 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
506 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000507 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000508 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000509 break;
510 case tok::slash:
Chris Lattner8ed30442008-05-05 06:45:50 +0000511 if (RHS.Val != 0) {
512 Res = LHS.Val / RHS.Val;
513 if (LHS.Val.isSigned()) // MININT/-1 --> overflow.
514 Overflow = LHS.Val.isMinSignedValue() && RHS.Val.isAllOnesValue();
515 } else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000516 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
517 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000518 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 case tok::star:
Chris Lattner8ed30442008-05-05 06:45:50 +0000523 Res = LHS.Val * RHS.Val;
Eli Friedman3265a422009-05-16 21:24:10 +0000524 if (Res.isSigned() && LHS.Val != 0 && RHS.Val != 0)
Chris Lattner8ed30442008-05-05 06:45:50 +0000525 Overflow = Res/RHS.Val != LHS.Val || Res/LHS.Val != RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000526 break;
527 case tok::lessless: {
528 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000529 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
530 if (ShAmt >= LHS.Val.getBitWidth())
531 Overflow = true, ShAmt = LHS.Val.getBitWidth()-1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 else if (LHS.isUnsigned())
Eli Friedman3265a422009-05-16 21:24:10 +0000533 Overflow = false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000534 else if (LHS.Val.isNonNegative()) // Don't allow sign change.
535 Overflow = ShAmt >= LHS.Val.countLeadingZeros();
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 else
Chris Lattner8ed30442008-05-05 06:45:50 +0000537 Overflow = ShAmt >= LHS.Val.countLeadingOnes();
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Chris Lattner8ed30442008-05-05 06:45:50 +0000539 Res = LHS.Val << ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 break;
541 }
542 case tok::greatergreater: {
543 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000544 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 if (ShAmt >= LHS.getBitWidth())
546 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner8ed30442008-05-05 06:45:50 +0000547 Res = LHS.Val >> ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 break;
549 }
550 case tok::plus:
Chris Lattner8ed30442008-05-05 06:45:50 +0000551 Res = LHS.Val + RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000552 if (LHS.isUnsigned())
Eli Friedman3265a422009-05-16 21:24:10 +0000553 Overflow = false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000554 else if (LHS.Val.isNonNegative() == RHS.Val.isNonNegative() &&
555 Res.isNonNegative() != LHS.Val.isNonNegative())
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 Overflow = true; // Overflow for signed addition.
557 break;
558 case tok::minus:
Chris Lattner8ed30442008-05-05 06:45:50 +0000559 Res = LHS.Val - RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000560 if (LHS.isUnsigned())
Eli Friedman3265a422009-05-16 21:24:10 +0000561 Overflow = false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000562 else if (LHS.Val.isNonNegative() != RHS.Val.isNonNegative() &&
563 Res.isNonNegative() != LHS.Val.isNonNegative())
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 Overflow = true; // Overflow for signed subtraction.
565 break;
566 case tok::lessequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000567 Res = LHS.Val <= RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000568 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
569 break;
570 case tok::less:
Chris Lattner8ed30442008-05-05 06:45:50 +0000571 Res = LHS.Val < RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
573 break;
574 case tok::greaterequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000575 Res = LHS.Val >= RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000576 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
577 break;
578 case tok::greater:
Chris Lattner8ed30442008-05-05 06:45:50 +0000579 Res = LHS.Val > RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000580 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
581 break;
582 case tok::exclaimequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000583 Res = LHS.Val != RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
585 break;
586 case tok::equalequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000587 Res = LHS.Val == RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
589 break;
590 case tok::amp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000591 Res = LHS.Val & RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 break;
593 case tok::caret:
Chris Lattner8ed30442008-05-05 06:45:50 +0000594 Res = LHS.Val ^ RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 break;
596 case tok::pipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000597 Res = LHS.Val | RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 break;
599 case tok::ampamp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000600 Res = (LHS.Val != 0 && RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000601 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
602 break;
603 case tok::pipepipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000604 Res = (LHS.Val != 0 || RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
606 break;
607 case tok::comma:
Chris Lattner91891562008-05-04 18:36:18 +0000608 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
609 // if not being evaluated.
610 if (!PP.getLangOptions().C99 || ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000611 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
612 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000613 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump1eb44332009-09-09 15:08:12 +0000614 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 case tok::question: {
616 // Parse the : part of the expression.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000617 if (PeekTok.isNot(tok::colon)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000618 PP.Diag(PeekTok.getLocation(), diag::err_expected_colon)
619 << LHS.getRange(), RHS.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000620 PP.Diag(OpLoc, diag::note_matching) << "?";
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 return true;
622 }
623 // Consume the :.
624 PP.LexNonComment(PeekTok);
625
626 // Evaluate the value after the :.
Chris Lattner8ed30442008-05-05 06:45:50 +0000627 bool AfterColonLive = ValueLive && LHS.Val == 0;
628 PPValue AfterColonVal(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 DefinedTracker DT;
630 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
631 return true;
632
Chris Lattner44cbbb02008-05-05 20:09:27 +0000633 // Parse anything after the : with the same precedence as ?. We allow
634 // things of equal precedence because ?: is right associative.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000635 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 PeekTok, AfterColonLive, PP))
637 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner8ed30442008-05-05 06:45:50 +0000640 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
641 RHS.setEnd(AfterColonVal.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000642
643 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
644 // either operand is unsigned.
645 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Reid Spencer5f016e22007-07-11 17:01:13 +0000647 // Figure out the precedence of the token after the : part.
648 PeekPrec = getPrecedence(PeekTok.getKind());
649 break;
650 }
651 case tok::colon:
652 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattner204b2fe2008-11-18 21:48:13 +0000653 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
654 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 return true;
656 }
657
658 // If this operator is live and overflowed, report the issue.
659 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000660 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
661 << LHS.getRange() << RHS.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 // Put the result back into 'LHS' for our next iteration.
Chris Lattner8ed30442008-05-05 06:45:50 +0000664 LHS.Val = Res;
665 LHS.setEnd(RHS.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000666 }
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 return false;
669}
670
671/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
672/// may occur after a #if or #elif directive. If the expression is equivalent
673/// to "!defined(X)" return X in IfNDefMacro.
674bool Preprocessor::
675EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
Chris Lattnera3e008a2009-12-14 05:00:18 +0000676 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
677 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
678 // in which case a directive is undefined behavior. We want macros to be able
679 // to recursively expand in order to get more gcc-list behavior, so we force
680 // DisableMacroExpansion to false and restore it when we're done parsing the
681 // expression.
682 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
683 DisableMacroExpansion = false;
684
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 // Peek ahead one token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000686 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner98be4942008-03-05 18:54:05 +0000690 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Chris Lattner8ed30442008-05-05 06:45:50 +0000692 PPValue ResVal(BitWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 DefinedTracker DT;
694 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
695 // Parse error, skip the rest of the macro line.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000696 if (Tok.isNot(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000698
699 // Restore 'DisableMacroExpansion'.
700 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000701 return false;
702 }
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 // If we are at the end of the expression after just parsing a value, there
705 // must be no (unparenthesized) binary operators involved, so we can exit
706 // directly.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000707 if (Tok.is(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000708 // If the expression we parsed was of the form !defined(macro), return the
709 // macro in IfNDefMacro.
710 if (DT.State == DefinedTracker::NotDefinedMacro)
711 IfNDefMacro = DT.TheMacro;
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Chris Lattnera3e008a2009-12-14 05:00:18 +0000713 // Restore 'DisableMacroExpansion'.
714 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000715 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 }
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
719 // operator and the stuff after it.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000720 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
721 Tok, true, *this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000722 // Parse error, skip the rest of the macro line.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000723 if (Tok.isNot(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000725
726 // Restore 'DisableMacroExpansion'.
727 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 return false;
729 }
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Reid Spencer5f016e22007-07-11 17:01:13 +0000731 // If we aren't at the tok::eom token, something bad happened, like an extra
732 // ')' token.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000733 if (Tok.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 Diag(Tok, diag::err_pp_expected_eol);
735 DiscardUntilEndOfDirective();
736 }
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Chris Lattnera3e008a2009-12-14 05:00:18 +0000738 // Restore 'DisableMacroExpansion'.
739 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000740 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000741}
742