blob: 9920c4cb7cf85331ee607555a59e889278c57789 [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
Dan Gohman3c46e8d2010-07-26 21:25:24 +000027namespace {
28
Chris Lattner8ed30442008-05-05 06:45:50 +000029/// PPValue - Represents the value of a subexpression of a preprocessor
30/// conditional and the source range covered by it.
31class PPValue {
32 SourceRange Range;
33public:
34 llvm::APSInt Val;
Mike Stump1eb44332009-09-09 15:08:12 +000035
Chris Lattner8ed30442008-05-05 06:45:50 +000036 // Default ctor - Construct an 'invalid' PPValue.
37 PPValue(unsigned BitWidth) : Val(BitWidth) {}
Mike Stump1eb44332009-09-09 15:08:12 +000038
Chris Lattner8ed30442008-05-05 06:45:50 +000039 unsigned getBitWidth() const { return Val.getBitWidth(); }
40 bool isUnsigned() const { return Val.isUnsigned(); }
Mike Stump1eb44332009-09-09 15:08:12 +000041
Chris Lattner8ed30442008-05-05 06:45:50 +000042 const SourceRange &getRange() const { return Range; }
Mike Stump1eb44332009-09-09 15:08:12 +000043
Chris Lattner8ed30442008-05-05 06:45:50 +000044 void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
45 void setRange(SourceLocation B, SourceLocation E) {
Mike Stump1eb44332009-09-09 15:08:12 +000046 Range.setBegin(B); Range.setEnd(E);
Chris Lattner8ed30442008-05-05 06:45:50 +000047 }
48 void setBegin(SourceLocation L) { Range.setBegin(L); }
49 void setEnd(SourceLocation L) { Range.setEnd(L); }
50};
51
Dan Gohman3c46e8d2010-07-26 21:25:24 +000052}
53
Chris Lattner8ed30442008-05-05 06:45:50 +000054static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +000055 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +000056 Preprocessor &PP);
57
58/// DefinedTracker - This struct is used while parsing expressions to keep track
59/// of whether !defined(X) has been seen.
60///
61/// With this simple scheme, we handle the basic forms:
62/// !defined(X) and !defined X
63/// but we also trivially handle (silly) stuff like:
64/// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
65struct DefinedTracker {
66 /// Each time a Value is evaluated, it returns information about whether the
67 /// parsed value is of the form defined(X), !defined(X) or is something else.
68 enum TrackerState {
69 DefinedMacro, // defined(X)
70 NotDefinedMacro, // !defined(X)
71 Unknown // Something else.
72 } State;
73 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
74 /// indicates the macro that was checked.
75 IdentifierInfo *TheMacro;
76};
77
John Thompsona28cc092009-10-30 13:49:06 +000078/// EvaluateDefined - Process a 'defined(sym)' expression.
Chris Lattnera3e008a2009-12-14 05:00:18 +000079static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
80 bool ValueLive, Preprocessor &PP) {
John Thompsona28cc092009-10-30 13:49:06 +000081 IdentifierInfo *II;
82 Result.setBegin(PeekTok.getLocation());
83
84 // Get the next token, don't expand it.
85 PP.LexUnexpandedToken(PeekTok);
86
87 // Two options, it can either be a pp-identifier or a (.
88 SourceLocation LParenLoc;
89 if (PeekTok.is(tok::l_paren)) {
90 // Found a paren, remember we saw it and skip it.
91 LParenLoc = PeekTok.getLocation();
92 PP.LexUnexpandedToken(PeekTok);
93 }
94
95 // If we don't have a pp-identifier now, this is an error.
96 if ((II = PeekTok.getIdentifierInfo()) == 0) {
97 PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier);
98 return true;
99 }
100
101 // Otherwise, we got an identifier, is it defined to something?
102 Result.Val = II->hasMacroDefinition();
103 Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
104
105 // If there is a macro, mark it used.
106 if (Result.Val != 0 && ValueLive) {
107 MacroInfo *Macro = PP.getMacroInfo(II);
108 Macro->setIsUsed(true);
109 }
110
111 // Consume identifier.
112 Result.setEnd(PeekTok.getLocation());
Chris Lattner19943152010-02-26 19:42:53 +0000113 PP.LexUnexpandedToken(PeekTok);
John Thompsona28cc092009-10-30 13:49:06 +0000114
115 // If we are in parens, ensure we have a trailing ).
116 if (LParenLoc.isValid()) {
117 if (PeekTok.isNot(tok::r_paren)) {
118 PP.Diag(PeekTok.getLocation(), diag::err_pp_missing_rparen) << "defined";
119 PP.Diag(LParenLoc, diag::note_matching) << "(";
120 return true;
121 }
122 // Consume the ).
123 Result.setEnd(PeekTok.getLocation());
124 PP.LexNonComment(PeekTok);
125 }
126
127 // Success, remember that we saw defined(X).
128 DT.State = DefinedTracker::DefinedMacro;
129 DT.TheMacro = II;
130 return false;
131}
132
Reid Spencer5f016e22007-07-11 17:01:13 +0000133/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
134/// return the computed value in Result. Return true if there was an error
135/// parsing. This function also returns information about the form of the
136/// expression in DT. See above for information on what DT means.
137///
138/// If ValueLive is false, then this value is being evaluated in a context where
139/// the result is not used. As such, avoid diagnostics that relate to
140/// evaluation.
Chris Lattner8ed30442008-05-05 06:45:50 +0000141static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
142 bool ValueLive, Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 DT.State = DefinedTracker::Unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 // If this token's spelling is a pp-identifier, check to see if it is
146 // 'defined' or if it is a macro. Note that we check here because many
147 // keywords are pp-identifiers, so we can't check the kind.
148 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
Chris Lattnerf8806622009-12-14 04:26:45 +0000149 // Handle "defined X" and "defined(X)".
150 if (II->isStr("defined"))
John Thompsona28cc092009-10-30 13:49:06 +0000151 return(EvaluateDefined(Result, PeekTok, DT, ValueLive, PP));
Chris Lattnerf8806622009-12-14 04:26:45 +0000152
153 // If this identifier isn't 'defined' or one of the special
154 // preprocessor keywords and it wasn't macro expanded, it turns
155 // into a simple 0, unless it is the C++ keyword "true", in which case it
156 // turns into "1".
157 if (ValueLive)
158 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
159 Result.Val = II->getTokenID() == tok::kw_true;
160 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
161 Result.setRange(PeekTok.getLocation());
162 PP.LexNonComment(PeekTok);
163 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 }
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 switch (PeekTok.getKind()) {
167 default: // Non-value token.
Chris Lattnerd98d9752008-04-13 20:38:43 +0000168 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 return true;
170 case tok::eom:
171 case tok::r_paren:
172 // If there is no expression, report and exit.
173 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
174 return true;
175 case tok::numeric_constant: {
176 llvm::SmallString<64> IntegerBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000177 bool NumberInvalid = false;
178 llvm::StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
179 &NumberInvalid);
180 if (NumberInvalid)
181 return true; // a diagnostic was already reported
182
Benjamin Krameraeb863c2010-02-27 16:29:36 +0000183 NumericLiteralParser Literal(Spelling.begin(), Spelling.end(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 PeekTok.getLocation(), PP);
185 if (Literal.hadError)
186 return true; // a diagnostic was already reported.
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Chris Lattner6e400c22007-08-26 03:29:23 +0000188 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
190 return true;
191 }
192 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
193
Neil Boothb9449512007-08-29 22:00:19 +0000194 // long long is a C99 feature.
195 if (!PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus0x
Neil Booth79859c32007-08-29 22:13:52 +0000196 && Literal.isLongLong)
Neil Boothb9449512007-08-29 22:00:19 +0000197 PP.Diag(PeekTok, diag::ext_longlong);
198
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 // Parse the integer literal into Result.
Chris Lattner8ed30442008-05-05 06:45:50 +0000200 if (Literal.GetIntegerValue(Result.Val)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 // Overflow parsing integer literal.
202 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattner8ed30442008-05-05 06:45:50 +0000203 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 } else {
205 // Set the signedness of the result to match whether there was a U suffix
206 // or not.
Chris Lattner8ed30442008-05-05 06:45:50 +0000207 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 // Detect overflow based on whether the value is signed. If signed
210 // and if the value is too large, emit a warning "integer constant is so
211 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
212 // is 64-bits.
Chris Lattner8ed30442008-05-05 06:45:50 +0000213 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Chris Lattnerb081a352008-07-03 03:47:30 +0000214 // Don't warn for a hex literal: 0x8000..0 shouldn't warn.
215 if (ValueLive && Literal.getRadix() != 16)
Chris Lattner8ed30442008-05-05 06:45:50 +0000216 PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
217 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 }
219 }
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000222 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 PP.LexNonComment(PeekTok);
224 return false;
225 }
226 case tok::char_constant: { // 'x'
227 llvm::SmallString<32> CharBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000228 bool CharInvalid = false;
229 llvm::StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
230 if (CharInvalid)
231 return true;
Benjamin Kramerddeea562010-02-27 13:44:12 +0000232
233 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 PeekTok.getLocation(), PP);
235 if (Literal.hadError())
236 return true; // A diagnostic was already emitted.
237
238 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar444be732009-11-13 05:51:54 +0000239 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000240 unsigned NumBits;
241 if (Literal.isMultiChar())
242 NumBits = TI.getIntWidth();
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000243 else if (Literal.isWide())
244 NumBits = TI.getWCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000245 else
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000246 NumBits = TI.getCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000247
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 // Set the width.
249 llvm::APSInt Val(NumBits);
250 // Set the value.
251 Val = Literal.getValue();
252 // Set the signedness.
Eli Friedman15b91762009-06-05 07:05:05 +0000253 Val.setIsUnsigned(!PP.getLangOptions().CharIsSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Chris Lattner8ed30442008-05-05 06:45:50 +0000255 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
256 Result.Val = Val.extend(Result.Val.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000258 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000259 "intmax_t smaller than char/wchar_t?");
Chris Lattner8ed30442008-05-05 06:45:50 +0000260 Result.Val = Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 }
262
263 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000264 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 PP.LexNonComment(PeekTok);
266 return false;
267 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000268 case tok::l_paren: {
269 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 PP.LexNonComment(PeekTok); // Eat the (.
271 // Parse the value and if there are any binary operators involved, parse
272 // them.
273 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
274
275 // If this is a silly value like (X), which doesn't need parens, check for
276 // !(defined X).
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000277 if (PeekTok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 // Just use DT unmodified as our result.
279 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000280 // Otherwise, we have something like (x+y), and we consumed '(x'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
282 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000284 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000285 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
286 << Result.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000287 PP.Diag(Start, diag::note_matching) << "(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 return true;
289 }
290 DT.State = DefinedTracker::Unknown;
291 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000292 Result.setRange(Start, PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 PP.LexNonComment(PeekTok); // Eat the ).
294 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000295 }
296 case tok::plus: {
297 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 // Unary plus doesn't modify the value.
299 PP.LexNonComment(PeekTok);
Chris Lattner8ed30442008-05-05 06:45:50 +0000300 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
301 Result.setBegin(Start);
302 return false;
303 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 case tok::minus: {
305 SourceLocation Loc = PeekTok.getLocation();
306 PP.LexNonComment(PeekTok);
307 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000308 Result.setBegin(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000311 Result.Val = -Result.Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Chris Lattnerb081a352008-07-03 03:47:30 +0000313 // -MININT is the only thing that overflows. Unsigned never overflows.
314 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 // If this operator is live and overflowed, report the issue.
317 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000318 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 DT.State = DefinedTracker::Unknown;
321 return false;
322 }
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Chris Lattner8ed30442008-05-05 06:45:50 +0000324 case tok::tilde: {
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
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000331 Result.Val = ~Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 DT.State = DefinedTracker::Unknown;
333 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000334 }
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Chris Lattner8ed30442008-05-05 06:45:50 +0000336 case tok::exclaim: {
337 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 PP.LexNonComment(PeekTok);
339 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000340 Result.setBegin(Start);
341 Result.Val = !Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner8ed30442008-05-05 06:45:50 +0000343 Result.Val.setIsUnsigned(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 if (DT.State == DefinedTracker::DefinedMacro)
346 DT.State = DefinedTracker::NotDefinedMacro;
347 else if (DT.State == DefinedTracker::NotDefinedMacro)
348 DT.State = DefinedTracker::DefinedMacro;
349 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000350 }
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 // FIXME: Handle #assert
353 }
354}
355
356
357
358/// getPrecedence - Return the precedence of the specified binary operator
359/// token. This returns:
360/// ~0 - Invalid token.
Chris Lattner9e66ba62008-05-05 04:10:51 +0000361/// 14 -> 3 - various operators.
362/// 0 - 'eom' or ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000363static unsigned getPrecedence(tok::TokenKind Kind) {
364 switch (Kind) {
365 default: return ~0U;
366 case tok::percent:
367 case tok::slash:
368 case tok::star: return 14;
369 case tok::plus:
370 case tok::minus: return 13;
371 case tok::lessless:
372 case tok::greatergreater: return 12;
373 case tok::lessequal:
374 case tok::less:
375 case tok::greaterequal:
376 case tok::greater: return 11;
377 case tok::exclaimequal:
378 case tok::equalequal: return 10;
379 case tok::amp: return 9;
380 case tok::caret: return 8;
381 case tok::pipe: return 7;
382 case tok::ampamp: return 6;
383 case tok::pipepipe: return 5;
Chris Lattner98ed49f2008-05-05 20:07:41 +0000384 case tok::question: return 4;
385 case tok::comma: return 3;
Chris Lattner91891562008-05-04 18:36:18 +0000386 case tok::colon: return 2;
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 case tok::r_paren: return 0; // Lowest priority, end of expr.
388 case tok::eom: return 0; // Lowest priority, end of macro.
389 }
390}
391
392
393/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner8ed30442008-05-05 06:45:50 +0000394/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Reid Spencer5f016e22007-07-11 17:01:13 +0000395///
396/// If ValueLive is false, then this value is being evaluated in a context where
397/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner8ed30442008-05-05 06:45:50 +0000398/// evaluation, such as division by zero warnings.
399static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +0000400 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +0000401 Preprocessor &PP) {
402 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
403 // If this token isn't valid, report the error.
404 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000405 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
406 << LHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000407 return true;
408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 while (1) {
411 // If this token has a lower precedence than we are allowed to parse, return
412 // it so that higher levels of the recursion can parse it.
413 if (PeekPrec < MinPrec)
414 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Reid Spencer5f016e22007-07-11 17:01:13 +0000418 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump1eb44332009-09-09 15:08:12 +0000419 // dead. Note that this cannot just clobber ValueLive. Consider
Reid Spencer5f016e22007-07-11 17:01:13 +0000420 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
421 // this example, the RHS of the && being dead does not make the rest of the
422 // expr dead.
423 bool RHSIsLive;
Chris Lattner8ed30442008-05-05 06:45:50 +0000424 if (Operator == tok::ampamp && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000426 else if (Operator == tok::pipepipe && LHS.Val != 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000427 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000428 else if (Operator == tok::question && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000429 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
430 else
431 RHSIsLive = ValueLive;
432
Chris Lattner8ed30442008-05-05 06:45:50 +0000433 // Consume the operator, remembering the operator's location for reporting.
434 SourceLocation OpLoc = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000435 PP.LexNonComment(PeekTok);
436
Chris Lattner8ed30442008-05-05 06:45:50 +0000437 PPValue RHS(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 // Parse the RHS of the operator.
439 DefinedTracker DT;
440 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
441
442 // Remember the precedence of this operator and get the precedence of the
443 // operator immediately to the right of the RHS.
444 unsigned ThisPrec = PeekPrec;
445 PeekPrec = getPrecedence(PeekTok.getKind());
446
447 // If this token isn't valid, report the error.
448 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000449 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
450 << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 return true;
452 }
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Chris Lattner98ed49f2008-05-05 20:07:41 +0000454 // Decide whether to include the next binop in this subexpression. For
455 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattner44cbbb02008-05-05 20:09:27 +0000456 // handle y*z as a single subexpression. We do this because the precedence
457 // of * is higher than that of +. The only strange case we have to handle
458 // here is for the ?: operator, where the precedence is actually lower than
459 // the LHS of the '?'. The grammar rule is:
Chris Lattner98ed49f2008-05-05 20:07:41 +0000460 //
461 // conditional-expression ::=
462 // logical-OR-expression ? expression : conditional-expression
463 // where 'expression' is actually comma-expression.
464 unsigned RHSPrec;
465 if (Operator == tok::question)
466 // The RHS of "?" should be maximally consumed as an expression.
467 RHSPrec = getPrecedence(tok::comma);
468 else // All others should munch while higher precedence.
469 RHSPrec = ThisPrec+1;
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Chris Lattner98ed49f2008-05-05 20:07:41 +0000471 if (PeekPrec >= RHSPrec) {
472 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Reid Spencer5f016e22007-07-11 17:01:13 +0000473 return true;
474 PeekPrec = getPrecedence(PeekTok.getKind());
475 }
476 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Reid Spencer5f016e22007-07-11 17:01:13 +0000478 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump1eb44332009-09-09 15:08:12 +0000479 // either operand is unsigned.
Reid Spencer5f016e22007-07-11 17:01:13 +0000480 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattner019ef7e2008-05-04 23:46:17 +0000481 switch (Operator) {
482 case tok::question: // No UAC for x and y in "x ? y : z".
483 case tok::lessless: // Shift amount doesn't UAC with shift value.
484 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
485 case tok::comma: // Comma operands are not subject to UACs.
486 case tok::pipepipe: // Logical || does not do UACs.
487 case tok::ampamp: // Logical && does not do UACs.
488 break; // No UAC
489 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000490 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
491 // If this just promoted something from signed to unsigned, and if the
492 // value was negative, warn about it.
493 if (ValueLive && Res.isUnsigned()) {
Chris Lattner8ed30442008-05-05 06:45:50 +0000494 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000495 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
496 << LHS.Val.toString(10, true) + " to " +
497 LHS.Val.toString(10, false)
498 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000499 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000500 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
501 << RHS.Val.toString(10, true) + " to " +
502 RHS.Val.toString(10, false)
503 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000504 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000505 LHS.Val.setIsUnsigned(Res.isUnsigned());
506 RHS.Val.setIsUnsigned(Res.isUnsigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000507 }
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Reid Spencer5f016e22007-07-11 17:01:13 +0000509 // FIXME: All of these should detect and report overflow??
510 bool Overflow = false;
511 switch (Operator) {
512 default: assert(0 && "Unknown operator token!");
513 case tok::percent:
Chris Lattner8ed30442008-05-05 06:45:50 +0000514 if (RHS.Val != 0)
515 Res = LHS.Val % RHS.Val;
516 else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000517 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
518 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000519 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000521 break;
522 case tok::slash:
Chris Lattner8ed30442008-05-05 06:45:50 +0000523 if (RHS.Val != 0) {
524 Res = LHS.Val / RHS.Val;
525 if (LHS.Val.isSigned()) // MININT/-1 --> overflow.
526 Overflow = LHS.Val.isMinSignedValue() && RHS.Val.isAllOnesValue();
527 } else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000528 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
529 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000530 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Reid Spencer5f016e22007-07-11 17:01:13 +0000534 case tok::star:
Chris Lattner8ed30442008-05-05 06:45:50 +0000535 Res = LHS.Val * RHS.Val;
Eli Friedman3265a422009-05-16 21:24:10 +0000536 if (Res.isSigned() && LHS.Val != 0 && RHS.Val != 0)
Chris Lattner8ed30442008-05-05 06:45:50 +0000537 Overflow = Res/RHS.Val != LHS.Val || Res/LHS.Val != RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000538 break;
539 case tok::lessless: {
540 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000541 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
542 if (ShAmt >= LHS.Val.getBitWidth())
543 Overflow = true, ShAmt = LHS.Val.getBitWidth()-1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 else if (LHS.isUnsigned())
Eli Friedman3265a422009-05-16 21:24:10 +0000545 Overflow = false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000546 else if (LHS.Val.isNonNegative()) // Don't allow sign change.
547 Overflow = ShAmt >= LHS.Val.countLeadingZeros();
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 else
Chris Lattner8ed30442008-05-05 06:45:50 +0000549 Overflow = ShAmt >= LHS.Val.countLeadingOnes();
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Chris Lattner8ed30442008-05-05 06:45:50 +0000551 Res = LHS.Val << ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000552 break;
553 }
554 case tok::greatergreater: {
555 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000556 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 if (ShAmt >= LHS.getBitWidth())
558 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner8ed30442008-05-05 06:45:50 +0000559 Res = LHS.Val >> ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000560 break;
561 }
562 case tok::plus:
Chris Lattner8ed30442008-05-05 06:45:50 +0000563 Res = LHS.Val + RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 if (LHS.isUnsigned())
Eli Friedman3265a422009-05-16 21:24:10 +0000565 Overflow = false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000566 else if (LHS.Val.isNonNegative() == RHS.Val.isNonNegative() &&
567 Res.isNonNegative() != LHS.Val.isNonNegative())
Reid Spencer5f016e22007-07-11 17:01:13 +0000568 Overflow = true; // Overflow for signed addition.
569 break;
570 case tok::minus:
Chris Lattner8ed30442008-05-05 06:45:50 +0000571 Res = LHS.Val - RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 if (LHS.isUnsigned())
Eli Friedman3265a422009-05-16 21:24:10 +0000573 Overflow = false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000574 else if (LHS.Val.isNonNegative() != RHS.Val.isNonNegative() &&
575 Res.isNonNegative() != LHS.Val.isNonNegative())
Reid Spencer5f016e22007-07-11 17:01:13 +0000576 Overflow = true; // Overflow for signed subtraction.
577 break;
578 case tok::lessequal:
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::less:
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::greaterequal:
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::greater:
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.8p6, result is always int (signed)
593 break;
594 case tok::exclaimequal:
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::equalequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000599 Res = LHS.Val == RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000600 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
601 break;
602 case tok::amp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000603 Res = LHS.Val & RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 break;
605 case tok::caret:
Chris Lattner8ed30442008-05-05 06:45:50 +0000606 Res = LHS.Val ^ RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 break;
608 case tok::pipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000609 Res = LHS.Val | RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 break;
611 case tok::ampamp:
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.13p3, result is always int (signed)
614 break;
615 case tok::pipepipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000616 Res = (LHS.Val != 0 || RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
618 break;
619 case tok::comma:
Chris Lattner91891562008-05-04 18:36:18 +0000620 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
621 // if not being evaluated.
622 if (!PP.getLangOptions().C99 || ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000623 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
624 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000625 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump1eb44332009-09-09 15:08:12 +0000626 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000627 case tok::question: {
628 // Parse the : part of the expression.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000629 if (PeekTok.isNot(tok::colon)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000630 PP.Diag(PeekTok.getLocation(), diag::err_expected_colon)
631 << LHS.getRange(), RHS.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000632 PP.Diag(OpLoc, diag::note_matching) << "?";
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 return true;
634 }
635 // Consume the :.
636 PP.LexNonComment(PeekTok);
637
638 // Evaluate the value after the :.
Chris Lattner8ed30442008-05-05 06:45:50 +0000639 bool AfterColonLive = ValueLive && LHS.Val == 0;
640 PPValue AfterColonVal(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 DefinedTracker DT;
642 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
643 return true;
644
Chris Lattner44cbbb02008-05-05 20:09:27 +0000645 // Parse anything after the : with the same precedence as ?. We allow
646 // things of equal precedence because ?: is right associative.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000647 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 PeekTok, AfterColonLive, PP))
649 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner8ed30442008-05-05 06:45:50 +0000652 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
653 RHS.setEnd(AfterColonVal.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000654
655 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
656 // either operand is unsigned.
657 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Reid Spencer5f016e22007-07-11 17:01:13 +0000659 // Figure out the precedence of the token after the : part.
660 PeekPrec = getPrecedence(PeekTok.getKind());
661 break;
662 }
663 case tok::colon:
664 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattner204b2fe2008-11-18 21:48:13 +0000665 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
666 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 return true;
668 }
669
670 // If this operator is live and overflowed, report the issue.
671 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000672 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
673 << LHS.getRange() << RHS.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 // Put the result back into 'LHS' for our next iteration.
Chris Lattner8ed30442008-05-05 06:45:50 +0000676 LHS.Val = Res;
677 LHS.setEnd(RHS.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000678 }
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 return false;
681}
682
683/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
684/// may occur after a #if or #elif directive. If the expression is equivalent
685/// to "!defined(X)" return X in IfNDefMacro.
686bool Preprocessor::
687EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
Chris Lattnera3e008a2009-12-14 05:00:18 +0000688 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
689 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
690 // in which case a directive is undefined behavior. We want macros to be able
691 // to recursively expand in order to get more gcc-list behavior, so we force
692 // DisableMacroExpansion to false and restore it when we're done parsing the
693 // expression.
694 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
695 DisableMacroExpansion = false;
696
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 // Peek ahead one token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000698 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000699 Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Reid Spencer5f016e22007-07-11 17:01:13 +0000701 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner98be4942008-03-05 18:54:05 +0000702 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Chris Lattner8ed30442008-05-05 06:45:50 +0000704 PPValue ResVal(BitWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 DefinedTracker DT;
706 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
707 // Parse error, skip the rest of the macro line.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000708 if (Tok.isNot(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000710
711 // Restore 'DisableMacroExpansion'.
712 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000713 return false;
714 }
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 // If we are at the end of the expression after just parsing a value, there
717 // must be no (unparenthesized) binary operators involved, so we can exit
718 // directly.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000719 if (Tok.is(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 // If the expression we parsed was of the form !defined(macro), return the
721 // macro in IfNDefMacro.
722 if (DT.State == DefinedTracker::NotDefinedMacro)
723 IfNDefMacro = DT.TheMacro;
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Chris Lattnera3e008a2009-12-14 05:00:18 +0000725 // Restore 'DisableMacroExpansion'.
726 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000727 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 }
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
731 // operator and the stuff after it.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000732 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
733 Tok, true, *this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 // Parse error, skip the rest of the macro line.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000735 if (Tok.isNot(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000737
738 // Restore 'DisableMacroExpansion'.
739 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000740 return false;
741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 // If we aren't at the tok::eom token, something bad happened, like an extra
744 // ')' token.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000745 if (Tok.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 Diag(Tok, diag::err_pp_expected_eol);
747 DiscardUntilEndOfDirective();
748 }
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Chris Lattnera3e008a2009-12-14 05:00:18 +0000750 // Restore 'DisableMacroExpansion'.
751 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000752 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000753}
754