blob: 756ce27a93dcba9a56be6ded9a11a035459bb7fb [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;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000173 bool NumberInvalid = false;
174 llvm::StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
175 &NumberInvalid);
176 if (NumberInvalid)
177 return true; // a diagnostic was already reported
178
Benjamin Krameraeb863c2010-02-27 16:29:36 +0000179 NumericLiteralParser Literal(Spelling.begin(), Spelling.end(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 PeekTok.getLocation(), PP);
181 if (Literal.hadError)
182 return true; // a diagnostic was already reported.
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Chris Lattner6e400c22007-08-26 03:29:23 +0000184 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
186 return true;
187 }
188 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
189
Neil Boothb9449512007-08-29 22:00:19 +0000190 // long long is a C99 feature.
191 if (!PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus0x
Neil Booth79859c32007-08-29 22:13:52 +0000192 && Literal.isLongLong)
Neil Boothb9449512007-08-29 22:00:19 +0000193 PP.Diag(PeekTok, diag::ext_longlong);
194
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 // Parse the integer literal into Result.
Chris Lattner8ed30442008-05-05 06:45:50 +0000196 if (Literal.GetIntegerValue(Result.Val)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 // Overflow parsing integer literal.
198 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattner8ed30442008-05-05 06:45:50 +0000199 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 } else {
201 // Set the signedness of the result to match whether there was a U suffix
202 // or not.
Chris Lattner8ed30442008-05-05 06:45:50 +0000203 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 // Detect overflow based on whether the value is signed. If signed
206 // and if the value is too large, emit a warning "integer constant is so
207 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
208 // is 64-bits.
Chris Lattner8ed30442008-05-05 06:45:50 +0000209 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Chris Lattnerb081a352008-07-03 03:47:30 +0000210 // Don't warn for a hex literal: 0x8000..0 shouldn't warn.
211 if (ValueLive && Literal.getRadix() != 16)
Chris Lattner8ed30442008-05-05 06:45:50 +0000212 PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
213 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 }
215 }
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000218 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 PP.LexNonComment(PeekTok);
220 return false;
221 }
222 case tok::char_constant: { // 'x'
223 llvm::SmallString<32> CharBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000224 bool CharInvalid = false;
225 llvm::StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
226 if (CharInvalid)
227 return true;
Benjamin Kramerddeea562010-02-27 13:44:12 +0000228
229 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 PeekTok.getLocation(), PP);
231 if (Literal.hadError())
232 return true; // A diagnostic was already emitted.
233
234 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar444be732009-11-13 05:51:54 +0000235 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000236 unsigned NumBits;
237 if (Literal.isMultiChar())
238 NumBits = TI.getIntWidth();
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000239 else if (Literal.isWide())
240 NumBits = TI.getWCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000241 else
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000242 NumBits = TI.getCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000243
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 // Set the width.
245 llvm::APSInt Val(NumBits);
246 // Set the value.
247 Val = Literal.getValue();
248 // Set the signedness.
Eli Friedman15b91762009-06-05 07:05:05 +0000249 Val.setIsUnsigned(!PP.getLangOptions().CharIsSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Chris Lattner8ed30442008-05-05 06:45:50 +0000251 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
252 Result.Val = Val.extend(Result.Val.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000254 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 "intmax_t smaller than char/wchar_t?");
Chris Lattner8ed30442008-05-05 06:45:50 +0000256 Result.Val = Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 }
258
259 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000260 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 PP.LexNonComment(PeekTok);
262 return false;
263 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000264 case tok::l_paren: {
265 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 PP.LexNonComment(PeekTok); // Eat the (.
267 // Parse the value and if there are any binary operators involved, parse
268 // them.
269 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
270
271 // If this is a silly value like (X), which doesn't need parens, check for
272 // !(defined X).
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000273 if (PeekTok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 // Just use DT unmodified as our result.
275 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000276 // Otherwise, we have something like (x+y), and we consumed '(x'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
278 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000280 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000281 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
282 << Result.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000283 PP.Diag(Start, diag::note_matching) << "(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 return true;
285 }
286 DT.State = DefinedTracker::Unknown;
287 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000288 Result.setRange(Start, PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 PP.LexNonComment(PeekTok); // Eat the ).
290 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000291 }
292 case tok::plus: {
293 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 // Unary plus doesn't modify the value.
295 PP.LexNonComment(PeekTok);
Chris Lattner8ed30442008-05-05 06:45:50 +0000296 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
297 Result.setBegin(Start);
298 return false;
299 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 case tok::minus: {
301 SourceLocation Loc = PeekTok.getLocation();
302 PP.LexNonComment(PeekTok);
303 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000304 Result.setBegin(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000307 Result.Val = -Result.Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Chris Lattnerb081a352008-07-03 03:47:30 +0000309 // -MININT is the only thing that overflows. Unsigned never overflows.
310 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 // If this operator is live and overflowed, report the issue.
313 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000314 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 DT.State = DefinedTracker::Unknown;
317 return false;
318 }
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Chris Lattner8ed30442008-05-05 06:45:50 +0000320 case tok::tilde: {
321 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 PP.LexNonComment(PeekTok);
323 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000324 Result.setBegin(Start);
325
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000327 Result.Val = ~Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000328 DT.State = DefinedTracker::Unknown;
329 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000330 }
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Chris Lattner8ed30442008-05-05 06:45:50 +0000332 case tok::exclaim: {
333 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 PP.LexNonComment(PeekTok);
335 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000336 Result.setBegin(Start);
337 Result.Val = !Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner8ed30442008-05-05 06:45:50 +0000339 Result.Val.setIsUnsigned(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 if (DT.State == DefinedTracker::DefinedMacro)
342 DT.State = DefinedTracker::NotDefinedMacro;
343 else if (DT.State == DefinedTracker::NotDefinedMacro)
344 DT.State = DefinedTracker::DefinedMacro;
345 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000346 }
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 // FIXME: Handle #assert
349 }
350}
351
352
353
354/// getPrecedence - Return the precedence of the specified binary operator
355/// token. This returns:
356/// ~0 - Invalid token.
Chris Lattner9e66ba62008-05-05 04:10:51 +0000357/// 14 -> 3 - various operators.
358/// 0 - 'eom' or ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000359static unsigned getPrecedence(tok::TokenKind Kind) {
360 switch (Kind) {
361 default: return ~0U;
362 case tok::percent:
363 case tok::slash:
364 case tok::star: return 14;
365 case tok::plus:
366 case tok::minus: return 13;
367 case tok::lessless:
368 case tok::greatergreater: return 12;
369 case tok::lessequal:
370 case tok::less:
371 case tok::greaterequal:
372 case tok::greater: return 11;
373 case tok::exclaimequal:
374 case tok::equalequal: return 10;
375 case tok::amp: return 9;
376 case tok::caret: return 8;
377 case tok::pipe: return 7;
378 case tok::ampamp: return 6;
379 case tok::pipepipe: return 5;
Chris Lattner98ed49f2008-05-05 20:07:41 +0000380 case tok::question: return 4;
381 case tok::comma: return 3;
Chris Lattner91891562008-05-04 18:36:18 +0000382 case tok::colon: return 2;
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 case tok::r_paren: return 0; // Lowest priority, end of expr.
384 case tok::eom: return 0; // Lowest priority, end of macro.
385 }
386}
387
388
389/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner8ed30442008-05-05 06:45:50 +0000390/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Reid Spencer5f016e22007-07-11 17:01:13 +0000391///
392/// If ValueLive is false, then this value is being evaluated in a context where
393/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner8ed30442008-05-05 06:45:50 +0000394/// evaluation, such as division by zero warnings.
395static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +0000396 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 Preprocessor &PP) {
398 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
399 // If this token isn't valid, report the error.
400 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000401 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
402 << LHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000403 return true;
404 }
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 while (1) {
407 // If this token has a lower precedence than we are allowed to parse, return
408 // it so that higher levels of the recursion can parse it.
409 if (PeekPrec < MinPrec)
410 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Reid Spencer5f016e22007-07-11 17:01:13 +0000414 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump1eb44332009-09-09 15:08:12 +0000415 // dead. Note that this cannot just clobber ValueLive. Consider
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
417 // this example, the RHS of the && being dead does not make the rest of the
418 // expr dead.
419 bool RHSIsLive;
Chris Lattner8ed30442008-05-05 06:45:50 +0000420 if (Operator == tok::ampamp && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000421 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000422 else if (Operator == tok::pipepipe && LHS.Val != 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000424 else if (Operator == tok::question && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
426 else
427 RHSIsLive = ValueLive;
428
Chris Lattner8ed30442008-05-05 06:45:50 +0000429 // Consume the operator, remembering the operator's location for reporting.
430 SourceLocation OpLoc = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000431 PP.LexNonComment(PeekTok);
432
Chris Lattner8ed30442008-05-05 06:45:50 +0000433 PPValue RHS(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000434 // Parse the RHS of the operator.
435 DefinedTracker DT;
436 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
437
438 // Remember the precedence of this operator and get the precedence of the
439 // operator immediately to the right of the RHS.
440 unsigned ThisPrec = PeekPrec;
441 PeekPrec = getPrecedence(PeekTok.getKind());
442
443 // If this token isn't valid, report the error.
444 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000445 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
446 << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000447 return true;
448 }
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Chris Lattner98ed49f2008-05-05 20:07:41 +0000450 // Decide whether to include the next binop in this subexpression. For
451 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattner44cbbb02008-05-05 20:09:27 +0000452 // handle y*z as a single subexpression. We do this because the precedence
453 // of * is higher than that of +. The only strange case we have to handle
454 // here is for the ?: operator, where the precedence is actually lower than
455 // the LHS of the '?'. The grammar rule is:
Chris Lattner98ed49f2008-05-05 20:07:41 +0000456 //
457 // conditional-expression ::=
458 // logical-OR-expression ? expression : conditional-expression
459 // where 'expression' is actually comma-expression.
460 unsigned RHSPrec;
461 if (Operator == tok::question)
462 // The RHS of "?" should be maximally consumed as an expression.
463 RHSPrec = getPrecedence(tok::comma);
464 else // All others should munch while higher precedence.
465 RHSPrec = ThisPrec+1;
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Chris Lattner98ed49f2008-05-05 20:07:41 +0000467 if (PeekPrec >= RHSPrec) {
468 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 return true;
470 PeekPrec = getPrecedence(PeekTok.getKind());
471 }
472 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Reid Spencer5f016e22007-07-11 17:01:13 +0000474 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump1eb44332009-09-09 15:08:12 +0000475 // either operand is unsigned.
Reid Spencer5f016e22007-07-11 17:01:13 +0000476 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattner019ef7e2008-05-04 23:46:17 +0000477 switch (Operator) {
478 case tok::question: // No UAC for x and y in "x ? y : z".
479 case tok::lessless: // Shift amount doesn't UAC with shift value.
480 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
481 case tok::comma: // Comma operands are not subject to UACs.
482 case tok::pipepipe: // Logical || does not do UACs.
483 case tok::ampamp: // Logical && does not do UACs.
484 break; // No UAC
485 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000486 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
487 // If this just promoted something from signed to unsigned, and if the
488 // value was negative, warn about it.
489 if (ValueLive && Res.isUnsigned()) {
Chris Lattner8ed30442008-05-05 06:45:50 +0000490 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000491 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
492 << LHS.Val.toString(10, true) + " to " +
493 LHS.Val.toString(10, false)
494 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000495 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000496 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
497 << RHS.Val.toString(10, true) + " to " +
498 RHS.Val.toString(10, false)
499 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000500 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000501 LHS.Val.setIsUnsigned(Res.isUnsigned());
502 RHS.Val.setIsUnsigned(Res.isUnsigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000503 }
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Reid Spencer5f016e22007-07-11 17:01:13 +0000505 // FIXME: All of these should detect and report overflow??
506 bool Overflow = false;
507 switch (Operator) {
508 default: assert(0 && "Unknown operator token!");
509 case tok::percent:
Chris Lattner8ed30442008-05-05 06:45:50 +0000510 if (RHS.Val != 0)
511 Res = LHS.Val % RHS.Val;
512 else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000513 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
514 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000515 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000517 break;
518 case tok::slash:
Chris Lattner8ed30442008-05-05 06:45:50 +0000519 if (RHS.Val != 0) {
520 Res = LHS.Val / RHS.Val;
521 if (LHS.Val.isSigned()) // MININT/-1 --> overflow.
522 Overflow = LHS.Val.isMinSignedValue() && RHS.Val.isAllOnesValue();
523 } else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000524 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
525 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000526 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000527 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000528 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Reid Spencer5f016e22007-07-11 17:01:13 +0000530 case tok::star:
Chris Lattner8ed30442008-05-05 06:45:50 +0000531 Res = LHS.Val * RHS.Val;
Eli Friedman3265a422009-05-16 21:24:10 +0000532 if (Res.isSigned() && LHS.Val != 0 && RHS.Val != 0)
Chris Lattner8ed30442008-05-05 06:45:50 +0000533 Overflow = Res/RHS.Val != LHS.Val || Res/LHS.Val != RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000534 break;
535 case tok::lessless: {
536 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000537 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
538 if (ShAmt >= LHS.Val.getBitWidth())
539 Overflow = true, ShAmt = LHS.Val.getBitWidth()-1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 else if (LHS.isUnsigned())
Eli Friedman3265a422009-05-16 21:24:10 +0000541 Overflow = false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000542 else if (LHS.Val.isNonNegative()) // Don't allow sign change.
543 Overflow = ShAmt >= LHS.Val.countLeadingZeros();
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 else
Chris Lattner8ed30442008-05-05 06:45:50 +0000545 Overflow = ShAmt >= LHS.Val.countLeadingOnes();
Mike Stump1eb44332009-09-09 15:08:12 +0000546
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::greatergreater: {
551 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000552 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Reid Spencer5f016e22007-07-11 17:01:13 +0000553 if (ShAmt >= LHS.getBitWidth())
554 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner8ed30442008-05-05 06:45:50 +0000555 Res = LHS.Val >> ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 break;
557 }
558 case tok::plus:
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 addition.
565 break;
566 case tok::minus:
Chris Lattner8ed30442008-05-05 06:45:50 +0000567 Res = LHS.Val - RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000568 if (LHS.isUnsigned())
Eli Friedman3265a422009-05-16 21:24:10 +0000569 Overflow = false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000570 else if (LHS.Val.isNonNegative() != RHS.Val.isNonNegative() &&
571 Res.isNonNegative() != LHS.Val.isNonNegative())
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 Overflow = true; // Overflow for signed subtraction.
573 break;
574 case tok::lessequal:
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::less:
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::greaterequal:
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.8p6, result is always int (signed)
585 break;
586 case tok::greater:
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.8p6, result is always int (signed)
589 break;
590 case tok::exclaimequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000591 Res = LHS.Val != RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
593 break;
594 case tok::equalequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000595 Res = LHS.Val == RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
597 break;
598 case tok::amp:
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::caret:
Chris Lattner8ed30442008-05-05 06:45:50 +0000602 Res = LHS.Val ^ RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 break;
604 case tok::pipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000605 Res = LHS.Val | RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 break;
607 case tok::ampamp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000608 Res = (LHS.Val != 0 && RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
610 break;
611 case tok::pipepipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000612 Res = (LHS.Val != 0 || RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000613 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
614 break;
615 case tok::comma:
Chris Lattner91891562008-05-04 18:36:18 +0000616 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
617 // if not being evaluated.
618 if (!PP.getLangOptions().C99 || ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000619 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
620 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000621 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump1eb44332009-09-09 15:08:12 +0000622 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 case tok::question: {
624 // Parse the : part of the expression.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000625 if (PeekTok.isNot(tok::colon)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000626 PP.Diag(PeekTok.getLocation(), diag::err_expected_colon)
627 << LHS.getRange(), RHS.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000628 PP.Diag(OpLoc, diag::note_matching) << "?";
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 return true;
630 }
631 // Consume the :.
632 PP.LexNonComment(PeekTok);
633
634 // Evaluate the value after the :.
Chris Lattner8ed30442008-05-05 06:45:50 +0000635 bool AfterColonLive = ValueLive && LHS.Val == 0;
636 PPValue AfterColonVal(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 DefinedTracker DT;
638 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
639 return true;
640
Chris Lattner44cbbb02008-05-05 20:09:27 +0000641 // Parse anything after the : with the same precedence as ?. We allow
642 // things of equal precedence because ?: is right associative.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000643 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 PeekTok, AfterColonLive, PP))
645 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Reid Spencer5f016e22007-07-11 17:01:13 +0000647 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner8ed30442008-05-05 06:45:50 +0000648 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
649 RHS.setEnd(AfterColonVal.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000650
651 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
652 // either operand is unsigned.
653 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 // Figure out the precedence of the token after the : part.
656 PeekPrec = getPrecedence(PeekTok.getKind());
657 break;
658 }
659 case tok::colon:
660 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattner204b2fe2008-11-18 21:48:13 +0000661 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
662 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 return true;
664 }
665
666 // If this operator is live and overflowed, report the issue.
667 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000668 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
669 << LHS.getRange() << RHS.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 // Put the result back into 'LHS' for our next iteration.
Chris Lattner8ed30442008-05-05 06:45:50 +0000672 LHS.Val = Res;
673 LHS.setEnd(RHS.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 }
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 return false;
677}
678
679/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
680/// may occur after a #if or #elif directive. If the expression is equivalent
681/// to "!defined(X)" return X in IfNDefMacro.
682bool Preprocessor::
683EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
Chris Lattnera3e008a2009-12-14 05:00:18 +0000684 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
685 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
686 // in which case a directive is undefined behavior. We want macros to be able
687 // to recursively expand in order to get more gcc-list behavior, so we force
688 // DisableMacroExpansion to false and restore it when we're done parsing the
689 // expression.
690 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
691 DisableMacroExpansion = false;
692
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 // Peek ahead one token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000694 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner98be4942008-03-05 18:54:05 +0000698 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Chris Lattner8ed30442008-05-05 06:45:50 +0000700 PPValue ResVal(BitWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000701 DefinedTracker DT;
702 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
703 // Parse error, skip the rest of the macro line.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000704 if (Tok.isNot(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000706
707 // Restore 'DisableMacroExpansion'.
708 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 return false;
710 }
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 // If we are at the end of the expression after just parsing a value, there
713 // must be no (unparenthesized) binary operators involved, so we can exit
714 // directly.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000715 if (Tok.is(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 // If the expression we parsed was of the form !defined(macro), return the
717 // macro in IfNDefMacro.
718 if (DT.State == DefinedTracker::NotDefinedMacro)
719 IfNDefMacro = DT.TheMacro;
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Chris Lattnera3e008a2009-12-14 05:00:18 +0000721 // Restore 'DisableMacroExpansion'.
722 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000723 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 }
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
727 // operator and the stuff after it.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000728 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
729 Tok, true, *this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 // Parse error, skip the rest of the macro line.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000731 if (Tok.isNot(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000733
734 // Restore 'DisableMacroExpansion'.
735 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 return false;
737 }
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 // If we aren't at the tok::eom token, something bad happened, like an extra
740 // ')' token.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000741 if (Tok.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 Diag(Tok, diag::err_pp_expected_eol);
743 DiscardUntilEndOfDirective();
744 }
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Chris Lattnera3e008a2009-12-14 05:00:18 +0000746 // Restore 'DisableMacroExpansion'.
747 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000748 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000749}
750