blob: 73f3d4ee7a0f7f4418a830ebbf37bc6e0dba0b72 [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"
Douglas Gregor1fbb4472010-08-24 20:21:13 +000022#include "clang/Lex/CodeCompletionHandler.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "clang/Basic/TargetInfo.h"
Chris Lattner500d3292009-01-29 05:15:15 +000024#include "clang/Lex/LexDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025#include "llvm/ADT/APSInt.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
Dan Gohman3c46e8d2010-07-26 21:25:24 +000028namespace {
29
Chris Lattner8ed30442008-05-05 06:45:50 +000030/// PPValue - Represents the value of a subexpression of a preprocessor
31/// conditional and the source range covered by it.
32class PPValue {
33 SourceRange Range;
34public:
35 llvm::APSInt Val;
Mike Stump1eb44332009-09-09 15:08:12 +000036
Chris Lattner8ed30442008-05-05 06:45:50 +000037 // Default ctor - Construct an 'invalid' PPValue.
38 PPValue(unsigned BitWidth) : Val(BitWidth) {}
Mike Stump1eb44332009-09-09 15:08:12 +000039
Chris Lattner8ed30442008-05-05 06:45:50 +000040 unsigned getBitWidth() const { return Val.getBitWidth(); }
41 bool isUnsigned() const { return Val.isUnsigned(); }
Mike Stump1eb44332009-09-09 15:08:12 +000042
Chris Lattner8ed30442008-05-05 06:45:50 +000043 const SourceRange &getRange() const { return Range; }
Mike Stump1eb44332009-09-09 15:08:12 +000044
Chris Lattner8ed30442008-05-05 06:45:50 +000045 void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
46 void setRange(SourceLocation B, SourceLocation E) {
Mike Stump1eb44332009-09-09 15:08:12 +000047 Range.setBegin(B); Range.setEnd(E);
Chris Lattner8ed30442008-05-05 06:45:50 +000048 }
49 void setBegin(SourceLocation L) { Range.setBegin(L); }
50 void setEnd(SourceLocation L) { Range.setEnd(L); }
51};
52
Dan Gohman3c46e8d2010-07-26 21:25:24 +000053}
54
Chris Lattner8ed30442008-05-05 06:45:50 +000055static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +000056 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +000057 Preprocessor &PP);
58
59/// DefinedTracker - This struct is used while parsing expressions to keep track
60/// of whether !defined(X) has been seen.
61///
62/// With this simple scheme, we handle the basic forms:
63/// !defined(X) and !defined X
64/// but we also trivially handle (silly) stuff like:
65/// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
66struct DefinedTracker {
67 /// Each time a Value is evaluated, it returns information about whether the
68 /// parsed value is of the form defined(X), !defined(X) or is something else.
69 enum TrackerState {
70 DefinedMacro, // defined(X)
71 NotDefinedMacro, // !defined(X)
72 Unknown // Something else.
73 } State;
74 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
75 /// indicates the macro that was checked.
76 IdentifierInfo *TheMacro;
77};
78
John Thompsona28cc092009-10-30 13:49:06 +000079/// EvaluateDefined - Process a 'defined(sym)' expression.
Chris Lattnera3e008a2009-12-14 05:00:18 +000080static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
81 bool ValueLive, Preprocessor &PP) {
John Thompsona28cc092009-10-30 13:49:06 +000082 IdentifierInfo *II;
83 Result.setBegin(PeekTok.getLocation());
84
85 // Get the next token, don't expand it.
86 PP.LexUnexpandedToken(PeekTok);
87
88 // Two options, it can either be a pp-identifier or a (.
89 SourceLocation LParenLoc;
90 if (PeekTok.is(tok::l_paren)) {
91 // Found a paren, remember we saw it and skip it.
92 LParenLoc = PeekTok.getLocation();
93 PP.LexUnexpandedToken(PeekTok);
94 }
95
Douglas Gregor1fbb4472010-08-24 20:21:13 +000096 if (PeekTok.is(tok::code_completion)) {
97 if (PP.getCodeCompletionHandler())
98 PP.getCodeCompletionHandler()->CodeCompleteMacroName(false);
99 PP.LexUnexpandedToken(PeekTok);
100 }
101
John Thompsona28cc092009-10-30 13:49:06 +0000102 // If we don't have a pp-identifier now, this is an error.
103 if ((II = PeekTok.getIdentifierInfo()) == 0) {
104 PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier);
105 return true;
106 }
107
108 // Otherwise, we got an identifier, is it defined to something?
109 Result.Val = II->hasMacroDefinition();
110 Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
111
112 // If there is a macro, mark it used.
113 if (Result.Val != 0 && ValueLive) {
114 MacroInfo *Macro = PP.getMacroInfo(II);
115 Macro->setIsUsed(true);
116 }
117
118 // Consume identifier.
119 Result.setEnd(PeekTok.getLocation());
Chris Lattner19943152010-02-26 19:42:53 +0000120 PP.LexUnexpandedToken(PeekTok);
John Thompsona28cc092009-10-30 13:49:06 +0000121
122 // If we are in parens, ensure we have a trailing ).
123 if (LParenLoc.isValid()) {
124 if (PeekTok.isNot(tok::r_paren)) {
125 PP.Diag(PeekTok.getLocation(), diag::err_pp_missing_rparen) << "defined";
126 PP.Diag(LParenLoc, diag::note_matching) << "(";
127 return true;
128 }
129 // Consume the ).
130 Result.setEnd(PeekTok.getLocation());
131 PP.LexNonComment(PeekTok);
132 }
133
134 // Success, remember that we saw defined(X).
135 DT.State = DefinedTracker::DefinedMacro;
136 DT.TheMacro = II;
137 return false;
138}
139
Reid Spencer5f016e22007-07-11 17:01:13 +0000140/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
141/// return the computed value in Result. Return true if there was an error
142/// parsing. This function also returns information about the form of the
143/// expression in DT. See above for information on what DT means.
144///
145/// If ValueLive is false, then this value is being evaluated in a context where
146/// the result is not used. As such, avoid diagnostics that relate to
147/// evaluation.
Chris Lattner8ed30442008-05-05 06:45:50 +0000148static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
149 bool ValueLive, Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 DT.State = DefinedTracker::Unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 // If this token's spelling is a pp-identifier, check to see if it is
153 // 'defined' or if it is a macro. Note that we check here because many
154 // keywords are pp-identifiers, so we can't check the kind.
155 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
Chris Lattnerf8806622009-12-14 04:26:45 +0000156 // Handle "defined X" and "defined(X)".
157 if (II->isStr("defined"))
John Thompsona28cc092009-10-30 13:49:06 +0000158 return(EvaluateDefined(Result, PeekTok, DT, ValueLive, PP));
Chris Lattnerf8806622009-12-14 04:26:45 +0000159
160 // If this identifier isn't 'defined' or one of the special
161 // preprocessor keywords and it wasn't macro expanded, it turns
162 // into a simple 0, unless it is the C++ keyword "true", in which case it
163 // turns into "1".
164 if (ValueLive)
165 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
166 Result.Val = II->getTokenID() == tok::kw_true;
167 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
168 Result.setRange(PeekTok.getLocation());
169 PP.LexNonComment(PeekTok);
170 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 }
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 switch (PeekTok.getKind()) {
174 default: // Non-value token.
Chris Lattnerd98d9752008-04-13 20:38:43 +0000175 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 return true;
177 case tok::eom:
178 case tok::r_paren:
179 // If there is no expression, report and exit.
180 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
181 return true;
182 case tok::numeric_constant: {
183 llvm::SmallString<64> IntegerBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000184 bool NumberInvalid = false;
185 llvm::StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
186 &NumberInvalid);
187 if (NumberInvalid)
188 return true; // a diagnostic was already reported
189
Benjamin Krameraeb863c2010-02-27 16:29:36 +0000190 NumericLiteralParser Literal(Spelling.begin(), Spelling.end(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 PeekTok.getLocation(), PP);
192 if (Literal.hadError)
193 return true; // a diagnostic was already reported.
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Chris Lattner6e400c22007-08-26 03:29:23 +0000195 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
197 return true;
198 }
199 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
200
Neil Boothb9449512007-08-29 22:00:19 +0000201 // long long is a C99 feature.
202 if (!PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus0x
Neil Booth79859c32007-08-29 22:13:52 +0000203 && Literal.isLongLong)
Neil Boothb9449512007-08-29 22:00:19 +0000204 PP.Diag(PeekTok, diag::ext_longlong);
205
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 // Parse the integer literal into Result.
Chris Lattner8ed30442008-05-05 06:45:50 +0000207 if (Literal.GetIntegerValue(Result.Val)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 // Overflow parsing integer literal.
209 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattner8ed30442008-05-05 06:45:50 +0000210 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 } else {
212 // Set the signedness of the result to match whether there was a U suffix
213 // or not.
Chris Lattner8ed30442008-05-05 06:45:50 +0000214 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 // Detect overflow based on whether the value is signed. If signed
217 // and if the value is too large, emit a warning "integer constant is so
218 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
219 // is 64-bits.
Chris Lattner8ed30442008-05-05 06:45:50 +0000220 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Chris Lattnerb081a352008-07-03 03:47:30 +0000221 // Don't warn for a hex literal: 0x8000..0 shouldn't warn.
222 if (ValueLive && Literal.getRadix() != 16)
Chris Lattner8ed30442008-05-05 06:45:50 +0000223 PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
224 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 }
226 }
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000229 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 PP.LexNonComment(PeekTok);
231 return false;
232 }
233 case tok::char_constant: { // 'x'
234 llvm::SmallString<32> CharBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000235 bool CharInvalid = false;
236 llvm::StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
237 if (CharInvalid)
238 return true;
Benjamin Kramerddeea562010-02-27 13:44:12 +0000239
240 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 PeekTok.getLocation(), PP);
242 if (Literal.hadError())
243 return true; // A diagnostic was already emitted.
244
245 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar444be732009-11-13 05:51:54 +0000246 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000247 unsigned NumBits;
248 if (Literal.isMultiChar())
249 NumBits = TI.getIntWidth();
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000250 else if (Literal.isWide())
251 NumBits = TI.getWCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000252 else
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000253 NumBits = TI.getCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000254
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 // Set the width.
256 llvm::APSInt Val(NumBits);
257 // Set the value.
258 Val = Literal.getValue();
259 // Set the signedness.
Eli Friedman15b91762009-06-05 07:05:05 +0000260 Val.setIsUnsigned(!PP.getLangOptions().CharIsSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Chris Lattner8ed30442008-05-05 06:45:50 +0000262 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
263 Result.Val = Val.extend(Result.Val.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000265 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 "intmax_t smaller than char/wchar_t?");
Chris Lattner8ed30442008-05-05 06:45:50 +0000267 Result.Val = Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000268 }
269
270 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000271 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000272 PP.LexNonComment(PeekTok);
273 return false;
274 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000275 case tok::l_paren: {
276 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 PP.LexNonComment(PeekTok); // Eat the (.
278 // Parse the value and if there are any binary operators involved, parse
279 // them.
280 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
281
282 // If this is a silly value like (X), which doesn't need parens, check for
283 // !(defined X).
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000284 if (PeekTok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 // Just use DT unmodified as our result.
286 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000287 // Otherwise, we have something like (x+y), and we consumed '(x'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
289 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000291 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000292 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
293 << Result.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000294 PP.Diag(Start, diag::note_matching) << "(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 return true;
296 }
297 DT.State = DefinedTracker::Unknown;
298 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000299 Result.setRange(Start, PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 PP.LexNonComment(PeekTok); // Eat the ).
301 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000302 }
303 case tok::plus: {
304 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 // Unary plus doesn't modify the value.
306 PP.LexNonComment(PeekTok);
Chris Lattner8ed30442008-05-05 06:45:50 +0000307 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
308 Result.setBegin(Start);
309 return false;
310 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 case tok::minus: {
312 SourceLocation Loc = PeekTok.getLocation();
313 PP.LexNonComment(PeekTok);
314 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000315 Result.setBegin(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000318 Result.Val = -Result.Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Chris Lattnerb081a352008-07-03 03:47:30 +0000320 // -MININT is the only thing that overflows. Unsigned never overflows.
321 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 // If this operator is live and overflowed, report the issue.
324 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000325 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 DT.State = DefinedTracker::Unknown;
328 return false;
329 }
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Chris Lattner8ed30442008-05-05 06:45:50 +0000331 case tok::tilde: {
332 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000333 PP.LexNonComment(PeekTok);
334 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000335 Result.setBegin(Start);
336
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000338 Result.Val = ~Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 DT.State = DefinedTracker::Unknown;
340 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000341 }
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Chris Lattner8ed30442008-05-05 06:45:50 +0000343 case tok::exclaim: {
344 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 PP.LexNonComment(PeekTok);
346 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000347 Result.setBegin(Start);
348 Result.Val = !Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner8ed30442008-05-05 06:45:50 +0000350 Result.Val.setIsUnsigned(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 if (DT.State == DefinedTracker::DefinedMacro)
353 DT.State = DefinedTracker::NotDefinedMacro;
354 else if (DT.State == DefinedTracker::NotDefinedMacro)
355 DT.State = DefinedTracker::DefinedMacro;
356 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000357 }
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 // FIXME: Handle #assert
360 }
361}
362
363
364
365/// getPrecedence - Return the precedence of the specified binary operator
366/// token. This returns:
367/// ~0 - Invalid token.
Chris Lattner9e66ba62008-05-05 04:10:51 +0000368/// 14 -> 3 - various operators.
369/// 0 - 'eom' or ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000370static unsigned getPrecedence(tok::TokenKind Kind) {
371 switch (Kind) {
372 default: return ~0U;
373 case tok::percent:
374 case tok::slash:
375 case tok::star: return 14;
376 case tok::plus:
377 case tok::minus: return 13;
378 case tok::lessless:
379 case tok::greatergreater: return 12;
380 case tok::lessequal:
381 case tok::less:
382 case tok::greaterequal:
383 case tok::greater: return 11;
384 case tok::exclaimequal:
385 case tok::equalequal: return 10;
386 case tok::amp: return 9;
387 case tok::caret: return 8;
388 case tok::pipe: return 7;
389 case tok::ampamp: return 6;
390 case tok::pipepipe: return 5;
Chris Lattner98ed49f2008-05-05 20:07:41 +0000391 case tok::question: return 4;
392 case tok::comma: return 3;
Chris Lattner91891562008-05-04 18:36:18 +0000393 case tok::colon: return 2;
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 case tok::r_paren: return 0; // Lowest priority, end of expr.
395 case tok::eom: return 0; // Lowest priority, end of macro.
396 }
397}
398
399
400/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner8ed30442008-05-05 06:45:50 +0000401/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Reid Spencer5f016e22007-07-11 17:01:13 +0000402///
403/// If ValueLive is false, then this value is being evaluated in a context where
404/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner8ed30442008-05-05 06:45:50 +0000405/// evaluation, such as division by zero warnings.
406static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +0000407 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 Preprocessor &PP) {
409 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
410 // If this token isn't valid, report the error.
411 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000412 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
413 << LHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000414 return true;
415 }
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 while (1) {
418 // If this token has a lower precedence than we are allowed to parse, return
419 // it so that higher levels of the recursion can parse it.
420 if (PeekPrec < MinPrec)
421 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump1eb44332009-09-09 15:08:12 +0000426 // dead. Note that this cannot just clobber ValueLive. Consider
Reid Spencer5f016e22007-07-11 17:01:13 +0000427 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
428 // this example, the RHS of the && being dead does not make the rest of the
429 // expr dead.
430 bool RHSIsLive;
Chris Lattner8ed30442008-05-05 06:45:50 +0000431 if (Operator == tok::ampamp && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000433 else if (Operator == tok::pipepipe && LHS.Val != 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000434 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000435 else if (Operator == tok::question && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000436 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
437 else
438 RHSIsLive = ValueLive;
439
Chris Lattner8ed30442008-05-05 06:45:50 +0000440 // Consume the operator, remembering the operator's location for reporting.
441 SourceLocation OpLoc = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000442 PP.LexNonComment(PeekTok);
443
Chris Lattner8ed30442008-05-05 06:45:50 +0000444 PPValue RHS(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000445 // Parse the RHS of the operator.
446 DefinedTracker DT;
447 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
448
449 // Remember the precedence of this operator and get the precedence of the
450 // operator immediately to the right of the RHS.
451 unsigned ThisPrec = PeekPrec;
452 PeekPrec = getPrecedence(PeekTok.getKind());
453
454 // If this token isn't valid, report the error.
455 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000456 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
457 << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 return true;
459 }
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Chris Lattner98ed49f2008-05-05 20:07:41 +0000461 // Decide whether to include the next binop in this subexpression. For
462 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattner44cbbb02008-05-05 20:09:27 +0000463 // handle y*z as a single subexpression. We do this because the precedence
464 // of * is higher than that of +. The only strange case we have to handle
465 // here is for the ?: operator, where the precedence is actually lower than
466 // the LHS of the '?'. The grammar rule is:
Chris Lattner98ed49f2008-05-05 20:07:41 +0000467 //
468 // conditional-expression ::=
469 // logical-OR-expression ? expression : conditional-expression
470 // where 'expression' is actually comma-expression.
471 unsigned RHSPrec;
472 if (Operator == tok::question)
473 // The RHS of "?" should be maximally consumed as an expression.
474 RHSPrec = getPrecedence(tok::comma);
475 else // All others should munch while higher precedence.
476 RHSPrec = ThisPrec+1;
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Chris Lattner98ed49f2008-05-05 20:07:41 +0000478 if (PeekPrec >= RHSPrec) {
479 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Reid Spencer5f016e22007-07-11 17:01:13 +0000480 return true;
481 PeekPrec = getPrecedence(PeekTok.getKind());
482 }
483 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump1eb44332009-09-09 15:08:12 +0000486 // either operand is unsigned.
Reid Spencer5f016e22007-07-11 17:01:13 +0000487 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattner019ef7e2008-05-04 23:46:17 +0000488 switch (Operator) {
489 case tok::question: // No UAC for x and y in "x ? y : z".
490 case tok::lessless: // Shift amount doesn't UAC with shift value.
491 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
492 case tok::comma: // Comma operands are not subject to UACs.
493 case tok::pipepipe: // Logical || does not do UACs.
494 case tok::ampamp: // Logical && does not do UACs.
495 break; // No UAC
496 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000497 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
498 // If this just promoted something from signed to unsigned, and if the
499 // value was negative, warn about it.
500 if (ValueLive && Res.isUnsigned()) {
Chris Lattner8ed30442008-05-05 06:45:50 +0000501 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000502 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
503 << LHS.Val.toString(10, true) + " to " +
504 LHS.Val.toString(10, false)
505 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000506 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000507 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
508 << RHS.Val.toString(10, true) + " to " +
509 RHS.Val.toString(10, false)
510 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000511 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000512 LHS.Val.setIsUnsigned(Res.isUnsigned());
513 RHS.Val.setIsUnsigned(Res.isUnsigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000514 }
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 // FIXME: All of these should detect and report overflow??
517 bool Overflow = false;
518 switch (Operator) {
519 default: assert(0 && "Unknown operator token!");
520 case tok::percent:
Chris Lattner8ed30442008-05-05 06:45:50 +0000521 if (RHS.Val != 0)
522 Res = LHS.Val % RHS.Val;
523 else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000524 PP.Diag(OpLoc, diag::err_pp_remainder_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;
529 case tok::slash:
Chris Lattner8ed30442008-05-05 06:45:50 +0000530 if (RHS.Val != 0) {
531 Res = LHS.Val / RHS.Val;
532 if (LHS.Val.isSigned()) // MININT/-1 --> overflow.
533 Overflow = LHS.Val.isMinSignedValue() && RHS.Val.isAllOnesValue();
534 } else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000535 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
536 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000537 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000538 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000539 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Reid Spencer5f016e22007-07-11 17:01:13 +0000541 case tok::star:
Chris Lattner8ed30442008-05-05 06:45:50 +0000542 Res = LHS.Val * RHS.Val;
Eli Friedman3265a422009-05-16 21:24:10 +0000543 if (Res.isSigned() && LHS.Val != 0 && RHS.Val != 0)
Chris Lattner8ed30442008-05-05 06:45:50 +0000544 Overflow = Res/RHS.Val != LHS.Val || Res/LHS.Val != RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 break;
546 case tok::lessless: {
547 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000548 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
549 if (ShAmt >= LHS.Val.getBitWidth())
550 Overflow = true, ShAmt = LHS.Val.getBitWidth()-1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000551 else if (LHS.isUnsigned())
Eli Friedman3265a422009-05-16 21:24:10 +0000552 Overflow = false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000553 else if (LHS.Val.isNonNegative()) // Don't allow sign change.
554 Overflow = ShAmt >= LHS.Val.countLeadingZeros();
Reid Spencer5f016e22007-07-11 17:01:13 +0000555 else
Chris Lattner8ed30442008-05-05 06:45:50 +0000556 Overflow = ShAmt >= LHS.Val.countLeadingOnes();
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Chris Lattner8ed30442008-05-05 06:45:50 +0000558 Res = LHS.Val << ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 break;
560 }
561 case tok::greatergreater: {
562 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000563 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 if (ShAmt >= LHS.getBitWidth())
565 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner8ed30442008-05-05 06:45:50 +0000566 Res = LHS.Val >> ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 break;
568 }
569 case tok::plus:
Chris Lattner8ed30442008-05-05 06:45:50 +0000570 Res = LHS.Val + RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 if (LHS.isUnsigned())
Eli Friedman3265a422009-05-16 21:24:10 +0000572 Overflow = false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000573 else if (LHS.Val.isNonNegative() == RHS.Val.isNonNegative() &&
574 Res.isNonNegative() != LHS.Val.isNonNegative())
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 Overflow = true; // Overflow for signed addition.
576 break;
577 case tok::minus:
Chris Lattner8ed30442008-05-05 06:45:50 +0000578 Res = LHS.Val - RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 if (LHS.isUnsigned())
Eli Friedman3265a422009-05-16 21:24:10 +0000580 Overflow = false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000581 else if (LHS.Val.isNonNegative() != RHS.Val.isNonNegative() &&
582 Res.isNonNegative() != LHS.Val.isNonNegative())
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 Overflow = true; // Overflow for signed subtraction.
584 break;
585 case tok::lessequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000586 Res = LHS.Val <= RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
588 break;
589 case tok::less:
Chris Lattner8ed30442008-05-05 06:45:50 +0000590 Res = LHS.Val < RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
592 break;
593 case tok::greaterequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000594 Res = LHS.Val >= RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
596 break;
597 case tok::greater:
Chris Lattner8ed30442008-05-05 06:45:50 +0000598 Res = LHS.Val > RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
600 break;
601 case tok::exclaimequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000602 Res = LHS.Val != RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
604 break;
605 case tok::equalequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000606 Res = LHS.Val == RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
608 break;
609 case tok::amp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000610 Res = LHS.Val & RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 break;
612 case tok::caret:
Chris Lattner8ed30442008-05-05 06:45:50 +0000613 Res = LHS.Val ^ RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 break;
615 case tok::pipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000616 Res = LHS.Val | RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 break;
618 case tok::ampamp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000619 Res = (LHS.Val != 0 && RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
621 break;
622 case tok::pipepipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000623 Res = (LHS.Val != 0 || RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
625 break;
626 case tok::comma:
Chris Lattner91891562008-05-04 18:36:18 +0000627 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
628 // if not being evaluated.
629 if (!PP.getLangOptions().C99 || ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000630 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
631 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000632 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump1eb44332009-09-09 15:08:12 +0000633 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 case tok::question: {
635 // Parse the : part of the expression.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000636 if (PeekTok.isNot(tok::colon)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000637 PP.Diag(PeekTok.getLocation(), diag::err_expected_colon)
638 << LHS.getRange(), RHS.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000639 PP.Diag(OpLoc, diag::note_matching) << "?";
Reid Spencer5f016e22007-07-11 17:01:13 +0000640 return true;
641 }
642 // Consume the :.
643 PP.LexNonComment(PeekTok);
644
645 // Evaluate the value after the :.
Chris Lattner8ed30442008-05-05 06:45:50 +0000646 bool AfterColonLive = ValueLive && LHS.Val == 0;
647 PPValue AfterColonVal(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 DefinedTracker DT;
649 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
650 return true;
651
Chris Lattner44cbbb02008-05-05 20:09:27 +0000652 // Parse anything after the : with the same precedence as ?. We allow
653 // things of equal precedence because ?: is right associative.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000654 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 PeekTok, AfterColonLive, PP))
656 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner8ed30442008-05-05 06:45:50 +0000659 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
660 RHS.setEnd(AfterColonVal.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000661
662 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
663 // either operand is unsigned.
664 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Reid Spencer5f016e22007-07-11 17:01:13 +0000666 // Figure out the precedence of the token after the : part.
667 PeekPrec = getPrecedence(PeekTok.getKind());
668 break;
669 }
670 case tok::colon:
671 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattner204b2fe2008-11-18 21:48:13 +0000672 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
673 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 return true;
675 }
676
677 // If this operator is live and overflowed, report the issue.
678 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000679 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
680 << LHS.getRange() << RHS.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 // Put the result back into 'LHS' for our next iteration.
Chris Lattner8ed30442008-05-05 06:45:50 +0000683 LHS.Val = Res;
684 LHS.setEnd(RHS.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 }
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 return false;
688}
689
690/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
691/// may occur after a #if or #elif directive. If the expression is equivalent
692/// to "!defined(X)" return X in IfNDefMacro.
693bool Preprocessor::
694EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
Chris Lattnera3e008a2009-12-14 05:00:18 +0000695 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
696 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
697 // in which case a directive is undefined behavior. We want macros to be able
698 // to recursively expand in order to get more gcc-list behavior, so we force
699 // DisableMacroExpansion to false and restore it when we're done parsing the
700 // expression.
701 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
702 DisableMacroExpansion = false;
703
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 // Peek ahead one token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000705 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000706 Lex(Tok);
Douglas Gregor1fbb4472010-08-24 20:21:13 +0000707
Reid Spencer5f016e22007-07-11 17:01:13 +0000708 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner98be4942008-03-05 18:54:05 +0000709 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Chris Lattner8ed30442008-05-05 06:45:50 +0000711 PPValue ResVal(BitWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 DefinedTracker DT;
713 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
714 // Parse error, skip the rest of the macro line.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000715 if (Tok.isNot(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000717
718 // Restore 'DisableMacroExpansion'.
719 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 return false;
721 }
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Reid Spencer5f016e22007-07-11 17:01:13 +0000723 // If we are at the end of the expression after just parsing a value, there
724 // must be no (unparenthesized) binary operators involved, so we can exit
725 // directly.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000726 if (Tok.is(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 // If the expression we parsed was of the form !defined(macro), return the
728 // macro in IfNDefMacro.
729 if (DT.State == DefinedTracker::NotDefinedMacro)
730 IfNDefMacro = DT.TheMacro;
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Chris Lattnera3e008a2009-12-14 05:00:18 +0000732 // Restore 'DisableMacroExpansion'.
733 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000734 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000735 }
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Reid Spencer5f016e22007-07-11 17:01:13 +0000737 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
738 // operator and the stuff after it.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000739 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
740 Tok, true, *this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000741 // Parse error, skip the rest of the macro line.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000742 if (Tok.isNot(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000744
745 // Restore 'DisableMacroExpansion'.
746 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000747 return false;
748 }
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 // If we aren't at the tok::eom token, something bad happened, like an extra
751 // ')' token.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000752 if (Tok.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 Diag(Tok, diag::err_pp_expected_eol);
754 DiscardUntilEndOfDirective();
755 }
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Chris Lattnera3e008a2009-12-14 05:00:18 +0000757 // Restore 'DisableMacroExpansion'.
758 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000759 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000760}
761