blob: 25816923c80eb57cf237982fcb610cf1e114e26f [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);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000115 PP.markMacroAsUsed(Macro);
John Thompsona28cc092009-10-30 13:49:06 +0000116 }
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
Douglas Gregorf29c5232010-08-24 22:20:20 +0000152 if (PeekTok.is(tok::code_completion)) {
153 if (PP.getCodeCompletionHandler())
154 PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
155 PP.LexUnexpandedToken(PeekTok);
156 }
157
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 // If this token's spelling is a pp-identifier, check to see if it is
159 // 'defined' or if it is a macro. Note that we check here because many
160 // keywords are pp-identifiers, so we can't check the kind.
161 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
Chris Lattnerf8806622009-12-14 04:26:45 +0000162 // Handle "defined X" and "defined(X)".
163 if (II->isStr("defined"))
John Thompsona28cc092009-10-30 13:49:06 +0000164 return(EvaluateDefined(Result, PeekTok, DT, ValueLive, PP));
Chris Lattnerf8806622009-12-14 04:26:45 +0000165
166 // If this identifier isn't 'defined' or one of the special
167 // preprocessor keywords and it wasn't macro expanded, it turns
168 // into a simple 0, unless it is the C++ keyword "true", in which case it
169 // turns into "1".
170 if (ValueLive)
171 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
172 Result.Val = II->getTokenID() == tok::kw_true;
173 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
174 Result.setRange(PeekTok.getLocation());
175 PP.LexNonComment(PeekTok);
176 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 }
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 switch (PeekTok.getKind()) {
180 default: // Non-value token.
Chris Lattnerd98d9752008-04-13 20:38:43 +0000181 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 return true;
Peter Collingbourne84021552011-02-28 02:37:51 +0000183 case tok::eod:
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 case tok::r_paren:
185 // If there is no expression, report and exit.
186 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
187 return true;
188 case tok::numeric_constant: {
189 llvm::SmallString<64> IntegerBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000190 bool NumberInvalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000191 StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
Douglas Gregor50f6af72010-03-16 05:20:39 +0000192 &NumberInvalid);
193 if (NumberInvalid)
194 return true; // a diagnostic was already reported
195
Benjamin Krameraeb863c2010-02-27 16:29:36 +0000196 NumericLiteralParser Literal(Spelling.begin(), Spelling.end(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 PeekTok.getLocation(), PP);
198 if (Literal.hadError)
199 return true; // a diagnostic was already reported.
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Chris Lattner6e400c22007-08-26 03:29:23 +0000201 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
203 return true;
204 }
205 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
206
Neil Boothb9449512007-08-29 22:00:19 +0000207 // long long is a C99 feature.
208 if (!PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus0x
Neil Booth79859c32007-08-29 22:13:52 +0000209 && Literal.isLongLong)
Neil Boothb9449512007-08-29 22:00:19 +0000210 PP.Diag(PeekTok, diag::ext_longlong);
211
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 // Parse the integer literal into Result.
Chris Lattner8ed30442008-05-05 06:45:50 +0000213 if (Literal.GetIntegerValue(Result.Val)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 // Overflow parsing integer literal.
215 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattner8ed30442008-05-05 06:45:50 +0000216 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 } else {
218 // Set the signedness of the result to match whether there was a U suffix
219 // or not.
Chris Lattner8ed30442008-05-05 06:45:50 +0000220 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 // Detect overflow based on whether the value is signed. If signed
223 // and if the value is too large, emit a warning "integer constant is so
224 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
225 // is 64-bits.
Chris Lattner8ed30442008-05-05 06:45:50 +0000226 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Chris Lattnerb081a352008-07-03 03:47:30 +0000227 // Don't warn for a hex literal: 0x8000..0 shouldn't warn.
228 if (ValueLive && Literal.getRadix() != 16)
Chris Lattner8ed30442008-05-05 06:45:50 +0000229 PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
230 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 }
232 }
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000235 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 PP.LexNonComment(PeekTok);
237 return false;
238 }
Douglas Gregor5cee1192011-07-27 05:40:30 +0000239 case tok::char_constant: // 'x'
240 case tok::wide_char_constant: { // L'x'
241 case tok::utf16_char_constant: // u'x'
242 case tok::utf32_char_constant: // U'x'
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 llvm::SmallString<32> CharBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000244 bool CharInvalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000245 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
Douglas Gregor50f6af72010-03-16 05:20:39 +0000246 if (CharInvalid)
247 return true;
Benjamin Kramerddeea562010-02-27 13:44:12 +0000248
249 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Douglas Gregor5cee1192011-07-27 05:40:30 +0000250 PeekTok.getLocation(), PP, PeekTok.getKind());
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 if (Literal.hadError())
252 return true; // A diagnostic was already emitted.
253
254 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar444be732009-11-13 05:51:54 +0000255 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000256 unsigned NumBits;
257 if (Literal.isMultiChar())
258 NumBits = TI.getIntWidth();
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000259 else if (Literal.isWide())
260 NumBits = TI.getWCharWidth();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000261 else if (Literal.isUTF16())
262 NumBits = TI.getChar16Width();
263 else if (Literal.isUTF32())
264 NumBits = TI.getChar32Width();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000265 else
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000266 NumBits = TI.getCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000267
Reid Spencer5f016e22007-07-11 17:01:13 +0000268 // Set the width.
269 llvm::APSInt Val(NumBits);
270 // Set the value.
271 Val = Literal.getValue();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000272 // Set the signedness. UTF-16 and UTF-32 are always unsigned
273 if (!Literal.isUTF16() && !Literal.isUTF32())
274 Val.setIsUnsigned(!PP.getLangOptions().CharIsSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Chris Lattner8ed30442008-05-05 06:45:50 +0000276 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
277 Result.Val = Val.extend(Result.Val.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000279 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 "intmax_t smaller than char/wchar_t?");
Chris Lattner8ed30442008-05-05 06:45:50 +0000281 Result.Val = Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 }
283
284 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000285 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 PP.LexNonComment(PeekTok);
287 return false;
288 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000289 case tok::l_paren: {
290 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 PP.LexNonComment(PeekTok); // Eat the (.
292 // Parse the value and if there are any binary operators involved, parse
293 // them.
294 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
295
296 // If this is a silly value like (X), which doesn't need parens, check for
297 // !(defined X).
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000298 if (PeekTok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 // Just use DT unmodified as our result.
300 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000301 // Otherwise, we have something like (x+y), and we consumed '(x'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
303 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000305 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000306 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
307 << Result.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000308 PP.Diag(Start, diag::note_matching) << "(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 return true;
310 }
311 DT.State = DefinedTracker::Unknown;
312 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000313 Result.setRange(Start, PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 PP.LexNonComment(PeekTok); // Eat the ).
315 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000316 }
317 case tok::plus: {
318 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 // Unary plus doesn't modify the value.
320 PP.LexNonComment(PeekTok);
Chris Lattner8ed30442008-05-05 06:45:50 +0000321 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
322 Result.setBegin(Start);
323 return false;
324 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 case tok::minus: {
326 SourceLocation Loc = PeekTok.getLocation();
327 PP.LexNonComment(PeekTok);
328 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000329 Result.setBegin(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Reid Spencer5f016e22007-07-11 17:01:13 +0000331 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000332 Result.Val = -Result.Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Chris Lattnerb081a352008-07-03 03:47:30 +0000334 // -MININT is the only thing that overflows. Unsigned never overflows.
335 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 // If this operator is live and overflowed, report the issue.
338 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000339 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 DT.State = DefinedTracker::Unknown;
342 return false;
343 }
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Chris Lattner8ed30442008-05-05 06:45:50 +0000345 case tok::tilde: {
346 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 PP.LexNonComment(PeekTok);
348 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000349 Result.setBegin(Start);
350
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000352 Result.Val = ~Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 DT.State = DefinedTracker::Unknown;
354 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000355 }
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Chris Lattner8ed30442008-05-05 06:45:50 +0000357 case tok::exclaim: {
358 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 PP.LexNonComment(PeekTok);
360 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000361 Result.setBegin(Start);
362 Result.Val = !Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner8ed30442008-05-05 06:45:50 +0000364 Result.Val.setIsUnsigned(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Reid Spencer5f016e22007-07-11 17:01:13 +0000366 if (DT.State == DefinedTracker::DefinedMacro)
367 DT.State = DefinedTracker::NotDefinedMacro;
368 else if (DT.State == DefinedTracker::NotDefinedMacro)
369 DT.State = DefinedTracker::DefinedMacro;
370 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000371 }
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 // FIXME: Handle #assert
374 }
375}
376
377
378
379/// getPrecedence - Return the precedence of the specified binary operator
380/// token. This returns:
381/// ~0 - Invalid token.
Chris Lattner9e66ba62008-05-05 04:10:51 +0000382/// 14 -> 3 - various operators.
Peter Collingbourne84021552011-02-28 02:37:51 +0000383/// 0 - 'eod' or ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000384static unsigned getPrecedence(tok::TokenKind Kind) {
385 switch (Kind) {
386 default: return ~0U;
387 case tok::percent:
388 case tok::slash:
389 case tok::star: return 14;
390 case tok::plus:
391 case tok::minus: return 13;
392 case tok::lessless:
393 case tok::greatergreater: return 12;
394 case tok::lessequal:
395 case tok::less:
396 case tok::greaterequal:
397 case tok::greater: return 11;
398 case tok::exclaimequal:
399 case tok::equalequal: return 10;
400 case tok::amp: return 9;
401 case tok::caret: return 8;
402 case tok::pipe: return 7;
403 case tok::ampamp: return 6;
404 case tok::pipepipe: return 5;
Chris Lattner98ed49f2008-05-05 20:07:41 +0000405 case tok::question: return 4;
406 case tok::comma: return 3;
Chris Lattner91891562008-05-04 18:36:18 +0000407 case tok::colon: return 2;
Peter Collingbourne84021552011-02-28 02:37:51 +0000408 case tok::r_paren: return 0;// Lowest priority, end of expr.
409 case tok::eod: return 0;// Lowest priority, end of directive.
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 }
411}
412
413
414/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner8ed30442008-05-05 06:45:50 +0000415/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Reid Spencer5f016e22007-07-11 17:01:13 +0000416///
417/// If ValueLive is false, then this value is being evaluated in a context where
418/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner8ed30442008-05-05 06:45:50 +0000419/// evaluation, such as division by zero warnings.
420static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +0000421 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +0000422 Preprocessor &PP) {
423 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
424 // If this token isn't valid, report the error.
425 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000426 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
427 << LHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000428 return true;
429 }
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Reid Spencer5f016e22007-07-11 17:01:13 +0000431 while (1) {
432 // If this token has a lower precedence than we are allowed to parse, return
433 // it so that higher levels of the recursion can parse it.
434 if (PeekPrec < MinPrec)
435 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Reid Spencer5f016e22007-07-11 17:01:13 +0000437 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Reid Spencer5f016e22007-07-11 17:01:13 +0000439 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump1eb44332009-09-09 15:08:12 +0000440 // dead. Note that this cannot just clobber ValueLive. Consider
Reid Spencer5f016e22007-07-11 17:01:13 +0000441 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
442 // this example, the RHS of the && being dead does not make the rest of the
443 // expr dead.
444 bool RHSIsLive;
Chris Lattner8ed30442008-05-05 06:45:50 +0000445 if (Operator == tok::ampamp && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000446 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000447 else if (Operator == tok::pipepipe && LHS.Val != 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000448 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000449 else if (Operator == tok::question && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000450 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
451 else
452 RHSIsLive = ValueLive;
453
Chris Lattner8ed30442008-05-05 06:45:50 +0000454 // Consume the operator, remembering the operator's location for reporting.
455 SourceLocation OpLoc = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 PP.LexNonComment(PeekTok);
457
Chris Lattner8ed30442008-05-05 06:45:50 +0000458 PPValue RHS(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 // Parse the RHS of the operator.
460 DefinedTracker DT;
461 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
462
463 // Remember the precedence of this operator and get the precedence of the
464 // operator immediately to the right of the RHS.
465 unsigned ThisPrec = PeekPrec;
466 PeekPrec = getPrecedence(PeekTok.getKind());
467
468 // If this token isn't valid, report the error.
469 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000470 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
471 << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000472 return true;
473 }
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Chris Lattner98ed49f2008-05-05 20:07:41 +0000475 // Decide whether to include the next binop in this subexpression. For
476 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattner44cbbb02008-05-05 20:09:27 +0000477 // handle y*z as a single subexpression. We do this because the precedence
478 // of * is higher than that of +. The only strange case we have to handle
479 // here is for the ?: operator, where the precedence is actually lower than
480 // the LHS of the '?'. The grammar rule is:
Chris Lattner98ed49f2008-05-05 20:07:41 +0000481 //
482 // conditional-expression ::=
483 // logical-OR-expression ? expression : conditional-expression
484 // where 'expression' is actually comma-expression.
485 unsigned RHSPrec;
486 if (Operator == tok::question)
487 // The RHS of "?" should be maximally consumed as an expression.
488 RHSPrec = getPrecedence(tok::comma);
489 else // All others should munch while higher precedence.
490 RHSPrec = ThisPrec+1;
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Chris Lattner98ed49f2008-05-05 20:07:41 +0000492 if (PeekPrec >= RHSPrec) {
493 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 return true;
495 PeekPrec = getPrecedence(PeekTok.getKind());
496 }
497 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Reid Spencer5f016e22007-07-11 17:01:13 +0000499 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump1eb44332009-09-09 15:08:12 +0000500 // either operand is unsigned.
Reid Spencer5f016e22007-07-11 17:01:13 +0000501 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattner019ef7e2008-05-04 23:46:17 +0000502 switch (Operator) {
503 case tok::question: // No UAC for x and y in "x ? y : z".
504 case tok::lessless: // Shift amount doesn't UAC with shift value.
505 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
506 case tok::comma: // Comma operands are not subject to UACs.
507 case tok::pipepipe: // Logical || does not do UACs.
508 case tok::ampamp: // Logical && does not do UACs.
509 break; // No UAC
510 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000511 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
512 // If this just promoted something from signed to unsigned, and if the
513 // value was negative, warn about it.
514 if (ValueLive && Res.isUnsigned()) {
Chris Lattner8ed30442008-05-05 06:45:50 +0000515 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000516 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
517 << LHS.Val.toString(10, true) + " to " +
518 LHS.Val.toString(10, false)
519 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000520 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000521 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
522 << RHS.Val.toString(10, true) + " to " +
523 RHS.Val.toString(10, false)
524 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000525 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000526 LHS.Val.setIsUnsigned(Res.isUnsigned());
527 RHS.Val.setIsUnsigned(Res.isUnsigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000528 }
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Reid Spencer5f016e22007-07-11 17:01:13 +0000530 bool Overflow = false;
531 switch (Operator) {
532 default: assert(0 && "Unknown operator token!");
533 case tok::percent:
Chris Lattner8ed30442008-05-05 06:45:50 +0000534 if (RHS.Val != 0)
535 Res = LHS.Val % RHS.Val;
536 else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000537 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
538 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000539 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000541 break;
542 case tok::slash:
Chris Lattner8ed30442008-05-05 06:45:50 +0000543 if (RHS.Val != 0) {
Chris Lattnerf9e77342010-10-13 23:46:56 +0000544 if (LHS.Val.isSigned())
545 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
546 else
547 Res = LHS.Val / RHS.Val;
Chris Lattner8ed30442008-05-05 06:45:50 +0000548 } else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000549 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
550 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000551 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000552 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000553 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Reid Spencer5f016e22007-07-11 17:01:13 +0000555 case tok::star:
Chris Lattnerf9e77342010-10-13 23:46:56 +0000556 if (Res.isSigned())
557 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
558 else
559 Res = LHS.Val * RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000560 break;
561 case tok::lessless: {
562 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000563 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Chris Lattnerf9e77342010-10-13 23:46:56 +0000564 if (LHS.isUnsigned()) {
565 Overflow = ShAmt >= LHS.Val.getBitWidth();
566 if (Overflow)
567 ShAmt = LHS.Val.getBitWidth()-1;
568 Res = LHS.Val << ShAmt;
569 } else {
570 Res = llvm::APSInt(LHS.Val.sshl_ov(ShAmt, Overflow), false);
571 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 break;
573 }
574 case tok::greatergreater: {
575 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000576 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Reid Spencer5f016e22007-07-11 17:01:13 +0000577 if (ShAmt >= LHS.getBitWidth())
578 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner8ed30442008-05-05 06:45:50 +0000579 Res = LHS.Val >> ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000580 break;
581 }
582 case tok::plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 if (LHS.isUnsigned())
Chris Lattnerf9e77342010-10-13 23:46:56 +0000584 Res = LHS.Val + RHS.Val;
585 else
586 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 break;
588 case tok::minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 if (LHS.isUnsigned())
Chris Lattnerf9e77342010-10-13 23:46:56 +0000590 Res = LHS.Val - RHS.Val;
591 else
592 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000593 break;
594 case tok::lessequal:
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.8p6, result is always int (signed)
597 break;
598 case tok::less:
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.8p6, result is always int (signed)
601 break;
602 case tok::greaterequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000603 Res = LHS.Val >= RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
605 break;
606 case tok::greater:
Chris Lattner8ed30442008-05-05 06:45:50 +0000607 Res = LHS.Val > RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000608 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
609 break;
610 case tok::exclaimequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000611 Res = LHS.Val != RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
613 break;
614 case tok::equalequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000615 Res = LHS.Val == RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
617 break;
618 case tok::amp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000619 Res = LHS.Val & RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 break;
621 case tok::caret:
Chris Lattner8ed30442008-05-05 06:45:50 +0000622 Res = LHS.Val ^ RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 break;
624 case tok::pipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000625 Res = LHS.Val | RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 break;
627 case tok::ampamp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000628 Res = (LHS.Val != 0 && RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
630 break;
631 case tok::pipepipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000632 Res = (LHS.Val != 0 || RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
634 break;
635 case tok::comma:
Chris Lattner91891562008-05-04 18:36:18 +0000636 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
637 // if not being evaluated.
638 if (!PP.getLangOptions().C99 || ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000639 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
640 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000641 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump1eb44332009-09-09 15:08:12 +0000642 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000643 case tok::question: {
644 // Parse the : part of the expression.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000645 if (PeekTok.isNot(tok::colon)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000646 PP.Diag(PeekTok.getLocation(), diag::err_expected_colon)
647 << LHS.getRange(), RHS.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000648 PP.Diag(OpLoc, diag::note_matching) << "?";
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 return true;
650 }
651 // Consume the :.
652 PP.LexNonComment(PeekTok);
653
654 // Evaluate the value after the :.
Chris Lattner8ed30442008-05-05 06:45:50 +0000655 bool AfterColonLive = ValueLive && LHS.Val == 0;
656 PPValue AfterColonVal(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000657 DefinedTracker DT;
658 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
659 return true;
660
Chris Lattner44cbbb02008-05-05 20:09:27 +0000661 // Parse anything after the : with the same precedence as ?. We allow
662 // things of equal precedence because ?: is right associative.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000663 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Reid Spencer5f016e22007-07-11 17:01:13 +0000664 PeekTok, AfterColonLive, PP))
665 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner8ed30442008-05-05 06:45:50 +0000668 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
669 RHS.setEnd(AfterColonVal.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000670
671 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
672 // either operand is unsigned.
673 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 // Figure out the precedence of the token after the : part.
676 PeekPrec = getPrecedence(PeekTok.getKind());
677 break;
678 }
679 case tok::colon:
680 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattner204b2fe2008-11-18 21:48:13 +0000681 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
682 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 return true;
684 }
685
686 // If this operator is live and overflowed, report the issue.
687 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000688 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
689 << LHS.getRange() << RHS.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Reid Spencer5f016e22007-07-11 17:01:13 +0000691 // Put the result back into 'LHS' for our next iteration.
Chris Lattner8ed30442008-05-05 06:45:50 +0000692 LHS.Val = Res;
693 LHS.setEnd(RHS.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 }
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 return false;
697}
698
699/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
700/// may occur after a #if or #elif directive. If the expression is equivalent
701/// to "!defined(X)" return X in IfNDefMacro.
702bool Preprocessor::
703EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
Chris Lattnera3e008a2009-12-14 05:00:18 +0000704 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
705 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
706 // in which case a directive is undefined behavior. We want macros to be able
707 // to recursively expand in order to get more gcc-list behavior, so we force
708 // DisableMacroExpansion to false and restore it when we're done parsing the
709 // expression.
710 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
711 DisableMacroExpansion = false;
712
Reid Spencer5f016e22007-07-11 17:01:13 +0000713 // Peek ahead one token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000714 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 Lex(Tok);
Douglas Gregor1fbb4472010-08-24 20:21:13 +0000716
Reid Spencer5f016e22007-07-11 17:01:13 +0000717 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner98be4942008-03-05 18:54:05 +0000718 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Chris Lattner8ed30442008-05-05 06:45:50 +0000720 PPValue ResVal(BitWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 DefinedTracker DT;
722 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
723 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000724 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000726
727 // Restore 'DisableMacroExpansion'.
728 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000729 return false;
730 }
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 // If we are at the end of the expression after just parsing a value, there
733 // must be no (unparenthesized) binary operators involved, so we can exit
734 // directly.
Peter Collingbourne84021552011-02-28 02:37:51 +0000735 if (Tok.is(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 // If the expression we parsed was of the form !defined(macro), return the
737 // macro in IfNDefMacro.
738 if (DT.State == DefinedTracker::NotDefinedMacro)
739 IfNDefMacro = DT.TheMacro;
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Chris Lattnera3e008a2009-12-14 05:00:18 +0000741 // Restore 'DisableMacroExpansion'.
742 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000743 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000744 }
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
747 // operator and the stuff after it.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000748 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
749 Tok, true, *this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000751 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000752 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000753
754 // Restore 'DisableMacroExpansion'.
755 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000756 return false;
757 }
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Peter Collingbourne84021552011-02-28 02:37:51 +0000759 // If we aren't at the tok::eod token, something bad happened, like an extra
Reid Spencer5f016e22007-07-11 17:01:13 +0000760 // ')' token.
Peter Collingbourne84021552011-02-28 02:37:51 +0000761 if (Tok.isNot(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 Diag(Tok, diag::err_pp_expected_eol);
763 DiscardUntilEndOfDirective();
764 }
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Chris Lattnera3e008a2009-12-14 05:00:18 +0000766 // Restore 'DisableMacroExpansion'.
767 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000768 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000769}
770