blob: 120b2ca034f7698900e3911b7083c3cda09e1b56 [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.
Eli Friedman88710f22011-08-03 00:04:13 +000086 PP.LexUnexpandedNonComment(PeekTok);
John Thompsona28cc092009-10-30 13:49:06 +000087
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();
Eli Friedman88710f22011-08-03 00:04:13 +000093 PP.LexUnexpandedNonComment(PeekTok);
John Thompsona28cc092009-10-30 13:49:06 +000094 }
95
Douglas Gregor1fbb4472010-08-24 20:21:13 +000096 if (PeekTok.is(tok::code_completion)) {
97 if (PP.getCodeCompletionHandler())
98 PP.getCodeCompletionHandler()->CodeCompleteMacroName(false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000099 PP.setCodeCompletionReached();
Eli Friedman88710f22011-08-03 00:04:13 +0000100 PP.LexUnexpandedNonComment(PeekTok);
Douglas Gregor1fbb4472010-08-24 20:21:13 +0000101 }
102
John Thompsona28cc092009-10-30 13:49:06 +0000103 // If we don't have a pp-identifier now, this is an error.
104 if ((II = PeekTok.getIdentifierInfo()) == 0) {
105 PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier);
106 return true;
107 }
108
109 // Otherwise, we got an identifier, is it defined to something?
110 Result.Val = II->hasMacroDefinition();
111 Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
112
113 // If there is a macro, mark it used.
114 if (Result.Val != 0 && ValueLive) {
115 MacroInfo *Macro = PP.getMacroInfo(II);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000116 PP.markMacroAsUsed(Macro);
John Thompsona28cc092009-10-30 13:49:06 +0000117 }
118
John Thompsona28cc092009-10-30 13:49:06 +0000119 // If we are in parens, ensure we have a trailing ).
120 if (LParenLoc.isValid()) {
Eli Friedman88710f22011-08-03 00:04:13 +0000121 // Consume identifier.
122 Result.setEnd(PeekTok.getLocation());
123 PP.LexUnexpandedNonComment(PeekTok);
124
John Thompsona28cc092009-10-30 13:49:06 +0000125 if (PeekTok.isNot(tok::r_paren)) {
126 PP.Diag(PeekTok.getLocation(), diag::err_pp_missing_rparen) << "defined";
127 PP.Diag(LParenLoc, diag::note_matching) << "(";
128 return true;
129 }
130 // Consume the ).
131 Result.setEnd(PeekTok.getLocation());
132 PP.LexNonComment(PeekTok);
Eli Friedman88710f22011-08-03 00:04:13 +0000133 } else {
134 // Consume identifier.
135 Result.setEnd(PeekTok.getLocation());
136 PP.LexNonComment(PeekTok);
John Thompsona28cc092009-10-30 13:49:06 +0000137 }
138
139 // Success, remember that we saw defined(X).
140 DT.State = DefinedTracker::DefinedMacro;
141 DT.TheMacro = II;
142 return false;
143}
144
Reid Spencer5f016e22007-07-11 17:01:13 +0000145/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
146/// return the computed value in Result. Return true if there was an error
147/// parsing. This function also returns information about the form of the
148/// expression in DT. See above for information on what DT means.
149///
150/// If ValueLive is false, then this value is being evaluated in a context where
151/// the result is not used. As such, avoid diagnostics that relate to
152/// evaluation.
Chris Lattner8ed30442008-05-05 06:45:50 +0000153static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
154 bool ValueLive, Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 DT.State = DefinedTracker::Unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Douglas Gregorf29c5232010-08-24 22:20:20 +0000157 if (PeekTok.is(tok::code_completion)) {
158 if (PP.getCodeCompletionHandler())
159 PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000160 PP.setCodeCompletionReached();
Eli Friedman88710f22011-08-03 00:04:13 +0000161 PP.LexNonComment(PeekTok);
Douglas Gregorf29c5232010-08-24 22:20:20 +0000162 }
163
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 // If this token's spelling is a pp-identifier, check to see if it is
165 // 'defined' or if it is a macro. Note that we check here because many
166 // keywords are pp-identifiers, so we can't check the kind.
167 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
Chris Lattnerf8806622009-12-14 04:26:45 +0000168 // Handle "defined X" and "defined(X)".
169 if (II->isStr("defined"))
John Thompsona28cc092009-10-30 13:49:06 +0000170 return(EvaluateDefined(Result, PeekTok, DT, ValueLive, PP));
Chris Lattnerf8806622009-12-14 04:26:45 +0000171
172 // If this identifier isn't 'defined' or one of the special
173 // preprocessor keywords and it wasn't macro expanded, it turns
174 // into a simple 0, unless it is the C++ keyword "true", in which case it
175 // turns into "1".
176 if (ValueLive)
177 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
178 Result.Val = II->getTokenID() == tok::kw_true;
179 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
180 Result.setRange(PeekTok.getLocation());
181 PP.LexNonComment(PeekTok);
182 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 }
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 switch (PeekTok.getKind()) {
186 default: // Non-value token.
Chris Lattnerd98d9752008-04-13 20:38:43 +0000187 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 return true;
Peter Collingbourne84021552011-02-28 02:37:51 +0000189 case tok::eod:
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 case tok::r_paren:
191 // If there is no expression, report and exit.
192 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
193 return true;
194 case tok::numeric_constant: {
195 llvm::SmallString<64> IntegerBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000196 bool NumberInvalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000197 StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
Douglas Gregor50f6af72010-03-16 05:20:39 +0000198 &NumberInvalid);
199 if (NumberInvalid)
200 return true; // a diagnostic was already reported
201
Benjamin Krameraeb863c2010-02-27 16:29:36 +0000202 NumericLiteralParser Literal(Spelling.begin(), Spelling.end(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 PeekTok.getLocation(), PP);
204 if (Literal.hadError)
205 return true; // a diagnostic was already reported.
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Chris Lattner6e400c22007-08-26 03:29:23 +0000207 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
209 return true;
210 }
211 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
212
Neil Boothb9449512007-08-29 22:00:19 +0000213 // long long is a C99 feature.
214 if (!PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus0x
Neil Booth79859c32007-08-29 22:13:52 +0000215 && Literal.isLongLong)
Neil Boothb9449512007-08-29 22:00:19 +0000216 PP.Diag(PeekTok, diag::ext_longlong);
217
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 // Parse the integer literal into Result.
Chris Lattner8ed30442008-05-05 06:45:50 +0000219 if (Literal.GetIntegerValue(Result.Val)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 // Overflow parsing integer literal.
221 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattner8ed30442008-05-05 06:45:50 +0000222 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 } else {
224 // Set the signedness of the result to match whether there was a U suffix
225 // or not.
Chris Lattner8ed30442008-05-05 06:45:50 +0000226 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 // Detect overflow based on whether the value is signed. If signed
229 // and if the value is too large, emit a warning "integer constant is so
230 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
231 // is 64-bits.
Chris Lattner8ed30442008-05-05 06:45:50 +0000232 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Chris Lattnerb081a352008-07-03 03:47:30 +0000233 // Don't warn for a hex literal: 0x8000..0 shouldn't warn.
234 if (ValueLive && Literal.getRadix() != 16)
Chris Lattner8ed30442008-05-05 06:45:50 +0000235 PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
236 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 }
238 }
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000241 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 PP.LexNonComment(PeekTok);
243 return false;
244 }
Douglas Gregor5cee1192011-07-27 05:40:30 +0000245 case tok::char_constant: // 'x'
246 case tok::wide_char_constant: { // L'x'
247 case tok::utf16_char_constant: // u'x'
248 case tok::utf32_char_constant: // U'x'
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 llvm::SmallString<32> CharBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000250 bool CharInvalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000251 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
Douglas Gregor50f6af72010-03-16 05:20:39 +0000252 if (CharInvalid)
253 return true;
Benjamin Kramerddeea562010-02-27 13:44:12 +0000254
255 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Douglas Gregor5cee1192011-07-27 05:40:30 +0000256 PeekTok.getLocation(), PP, PeekTok.getKind());
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 if (Literal.hadError())
258 return true; // A diagnostic was already emitted.
259
260 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar444be732009-11-13 05:51:54 +0000261 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000262 unsigned NumBits;
263 if (Literal.isMultiChar())
264 NumBits = TI.getIntWidth();
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000265 else if (Literal.isWide())
266 NumBits = TI.getWCharWidth();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000267 else if (Literal.isUTF16())
268 NumBits = TI.getChar16Width();
269 else if (Literal.isUTF32())
270 NumBits = TI.getChar32Width();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000271 else
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000272 NumBits = TI.getCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000273
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 // Set the width.
275 llvm::APSInt Val(NumBits);
276 // Set the value.
277 Val = Literal.getValue();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000278 // Set the signedness. UTF-16 and UTF-32 are always unsigned
279 if (!Literal.isUTF16() && !Literal.isUTF32())
280 Val.setIsUnsigned(!PP.getLangOptions().CharIsSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Chris Lattner8ed30442008-05-05 06:45:50 +0000282 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
283 Result.Val = Val.extend(Result.Val.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000285 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 "intmax_t smaller than char/wchar_t?");
Chris Lattner8ed30442008-05-05 06:45:50 +0000287 Result.Val = Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 }
289
290 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000291 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 PP.LexNonComment(PeekTok);
293 return false;
294 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000295 case tok::l_paren: {
296 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 PP.LexNonComment(PeekTok); // Eat the (.
298 // Parse the value and if there are any binary operators involved, parse
299 // them.
300 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
301
302 // If this is a silly value like (X), which doesn't need parens, check for
303 // !(defined X).
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000304 if (PeekTok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 // Just use DT unmodified as our result.
306 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000307 // Otherwise, we have something like (x+y), and we consumed '(x'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
309 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000311 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000312 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
313 << Result.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000314 PP.Diag(Start, diag::note_matching) << "(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 return true;
316 }
317 DT.State = DefinedTracker::Unknown;
318 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000319 Result.setRange(Start, PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 PP.LexNonComment(PeekTok); // Eat the ).
321 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000322 }
323 case tok::plus: {
324 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 // Unary plus doesn't modify the value.
326 PP.LexNonComment(PeekTok);
Chris Lattner8ed30442008-05-05 06:45:50 +0000327 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
328 Result.setBegin(Start);
329 return false;
330 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000331 case tok::minus: {
332 SourceLocation Loc = PeekTok.getLocation();
333 PP.LexNonComment(PeekTok);
334 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000335 Result.setBegin(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000338 Result.Val = -Result.Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Chris Lattnerb081a352008-07-03 03:47:30 +0000340 // -MININT is the only thing that overflows. Unsigned never overflows.
341 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Reid Spencer5f016e22007-07-11 17:01:13 +0000343 // If this operator is live and overflowed, report the issue.
344 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000345 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 DT.State = DefinedTracker::Unknown;
348 return false;
349 }
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Chris Lattner8ed30442008-05-05 06:45:50 +0000351 case tok::tilde: {
352 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 PP.LexNonComment(PeekTok);
354 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000355 Result.setBegin(Start);
356
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000358 Result.Val = ~Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 DT.State = DefinedTracker::Unknown;
360 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000361 }
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Chris Lattner8ed30442008-05-05 06:45:50 +0000363 case tok::exclaim: {
364 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 PP.LexNonComment(PeekTok);
366 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000367 Result.setBegin(Start);
368 Result.Val = !Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner8ed30442008-05-05 06:45:50 +0000370 Result.Val.setIsUnsigned(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 if (DT.State == DefinedTracker::DefinedMacro)
373 DT.State = DefinedTracker::NotDefinedMacro;
374 else if (DT.State == DefinedTracker::NotDefinedMacro)
375 DT.State = DefinedTracker::DefinedMacro;
376 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000377 }
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Reid Spencer5f016e22007-07-11 17:01:13 +0000379 // FIXME: Handle #assert
380 }
381}
382
383
384
385/// getPrecedence - Return the precedence of the specified binary operator
386/// token. This returns:
387/// ~0 - Invalid token.
Chris Lattner9e66ba62008-05-05 04:10:51 +0000388/// 14 -> 3 - various operators.
Peter Collingbourne84021552011-02-28 02:37:51 +0000389/// 0 - 'eod' or ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000390static unsigned getPrecedence(tok::TokenKind Kind) {
391 switch (Kind) {
392 default: return ~0U;
393 case tok::percent:
394 case tok::slash:
395 case tok::star: return 14;
396 case tok::plus:
397 case tok::minus: return 13;
398 case tok::lessless:
399 case tok::greatergreater: return 12;
400 case tok::lessequal:
401 case tok::less:
402 case tok::greaterequal:
403 case tok::greater: return 11;
404 case tok::exclaimequal:
405 case tok::equalequal: return 10;
406 case tok::amp: return 9;
407 case tok::caret: return 8;
408 case tok::pipe: return 7;
409 case tok::ampamp: return 6;
410 case tok::pipepipe: return 5;
Chris Lattner98ed49f2008-05-05 20:07:41 +0000411 case tok::question: return 4;
412 case tok::comma: return 3;
Chris Lattner91891562008-05-04 18:36:18 +0000413 case tok::colon: return 2;
Peter Collingbourne84021552011-02-28 02:37:51 +0000414 case tok::r_paren: return 0;// Lowest priority, end of expr.
415 case tok::eod: return 0;// Lowest priority, end of directive.
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 }
417}
418
419
420/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner8ed30442008-05-05 06:45:50 +0000421/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Reid Spencer5f016e22007-07-11 17:01:13 +0000422///
423/// If ValueLive is false, then this value is being evaluated in a context where
424/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner8ed30442008-05-05 06:45:50 +0000425/// evaluation, such as division by zero warnings.
426static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +0000427 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +0000428 Preprocessor &PP) {
429 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
430 // If this token isn't valid, report the error.
431 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000432 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
433 << LHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000434 return true;
435 }
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Reid Spencer5f016e22007-07-11 17:01:13 +0000437 while (1) {
438 // If this token has a lower precedence than we are allowed to parse, return
439 // it so that higher levels of the recursion can parse it.
440 if (PeekPrec < MinPrec)
441 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Reid Spencer5f016e22007-07-11 17:01:13 +0000445 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump1eb44332009-09-09 15:08:12 +0000446 // dead. Note that this cannot just clobber ValueLive. Consider
Reid Spencer5f016e22007-07-11 17:01:13 +0000447 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
448 // this example, the RHS of the && being dead does not make the rest of the
449 // expr dead.
450 bool RHSIsLive;
Chris Lattner8ed30442008-05-05 06:45:50 +0000451 if (Operator == tok::ampamp && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000453 else if (Operator == tok::pipepipe && LHS.Val != 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000454 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000455 else if (Operator == tok::question && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
457 else
458 RHSIsLive = ValueLive;
459
Chris Lattner8ed30442008-05-05 06:45:50 +0000460 // Consume the operator, remembering the operator's location for reporting.
461 SourceLocation OpLoc = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000462 PP.LexNonComment(PeekTok);
463
Chris Lattner8ed30442008-05-05 06:45:50 +0000464 PPValue RHS(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 // Parse the RHS of the operator.
466 DefinedTracker DT;
467 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
468
469 // Remember the precedence of this operator and get the precedence of the
470 // operator immediately to the right of the RHS.
471 unsigned ThisPrec = PeekPrec;
472 PeekPrec = getPrecedence(PeekTok.getKind());
473
474 // If this token isn't valid, report the error.
475 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000476 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
477 << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000478 return true;
479 }
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Chris Lattner98ed49f2008-05-05 20:07:41 +0000481 // Decide whether to include the next binop in this subexpression. For
482 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattner44cbbb02008-05-05 20:09:27 +0000483 // handle y*z as a single subexpression. We do this because the precedence
484 // of * is higher than that of +. The only strange case we have to handle
485 // here is for the ?: operator, where the precedence is actually lower than
486 // the LHS of the '?'. The grammar rule is:
Chris Lattner98ed49f2008-05-05 20:07:41 +0000487 //
488 // conditional-expression ::=
489 // logical-OR-expression ? expression : conditional-expression
490 // where 'expression' is actually comma-expression.
491 unsigned RHSPrec;
492 if (Operator == tok::question)
493 // The RHS of "?" should be maximally consumed as an expression.
494 RHSPrec = getPrecedence(tok::comma);
495 else // All others should munch while higher precedence.
496 RHSPrec = ThisPrec+1;
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Chris Lattner98ed49f2008-05-05 20:07:41 +0000498 if (PeekPrec >= RHSPrec) {
499 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Reid Spencer5f016e22007-07-11 17:01:13 +0000500 return true;
501 PeekPrec = getPrecedence(PeekTok.getKind());
502 }
503 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Reid Spencer5f016e22007-07-11 17:01:13 +0000505 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump1eb44332009-09-09 15:08:12 +0000506 // either operand is unsigned.
Reid Spencer5f016e22007-07-11 17:01:13 +0000507 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattner019ef7e2008-05-04 23:46:17 +0000508 switch (Operator) {
509 case tok::question: // No UAC for x and y in "x ? y : z".
510 case tok::lessless: // Shift amount doesn't UAC with shift value.
511 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
512 case tok::comma: // Comma operands are not subject to UACs.
513 case tok::pipepipe: // Logical || does not do UACs.
514 case tok::ampamp: // Logical && does not do UACs.
515 break; // No UAC
516 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000517 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
518 // If this just promoted something from signed to unsigned, and if the
519 // value was negative, warn about it.
520 if (ValueLive && Res.isUnsigned()) {
Chris Lattner8ed30442008-05-05 06:45:50 +0000521 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000522 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
523 << LHS.Val.toString(10, true) + " to " +
524 LHS.Val.toString(10, false)
525 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000526 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000527 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
528 << RHS.Val.toString(10, true) + " to " +
529 RHS.Val.toString(10, false)
530 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000532 LHS.Val.setIsUnsigned(Res.isUnsigned());
533 RHS.Val.setIsUnsigned(Res.isUnsigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000534 }
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 bool Overflow = false;
537 switch (Operator) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000538 default: llvm_unreachable("Unknown operator token!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000539 case tok::percent:
Chris Lattner8ed30442008-05-05 06:45:50 +0000540 if (RHS.Val != 0)
541 Res = LHS.Val % RHS.Val;
542 else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000543 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
544 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000545 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 break;
548 case tok::slash:
Chris Lattner8ed30442008-05-05 06:45:50 +0000549 if (RHS.Val != 0) {
Chris Lattnerf9e77342010-10-13 23:46:56 +0000550 if (LHS.Val.isSigned())
551 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
552 else
553 Res = LHS.Val / RHS.Val;
Chris Lattner8ed30442008-05-05 06:45:50 +0000554 } else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000555 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
556 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000557 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000558 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Reid Spencer5f016e22007-07-11 17:01:13 +0000561 case tok::star:
Chris Lattnerf9e77342010-10-13 23:46:56 +0000562 if (Res.isSigned())
563 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
564 else
565 Res = LHS.Val * RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 break;
567 case tok::lessless: {
568 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000569 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Chris Lattnerf9e77342010-10-13 23:46:56 +0000570 if (LHS.isUnsigned()) {
571 Overflow = ShAmt >= LHS.Val.getBitWidth();
572 if (Overflow)
573 ShAmt = LHS.Val.getBitWidth()-1;
574 Res = LHS.Val << ShAmt;
575 } else {
576 Res = llvm::APSInt(LHS.Val.sshl_ov(ShAmt, Overflow), false);
577 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000578 break;
579 }
580 case tok::greatergreater: {
581 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000582 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 if (ShAmt >= LHS.getBitWidth())
584 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner8ed30442008-05-05 06:45:50 +0000585 Res = LHS.Val >> ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 break;
587 }
588 case tok::plus:
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.sadd_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000593 break;
594 case tok::minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 if (LHS.isUnsigned())
Chris Lattnerf9e77342010-10-13 23:46:56 +0000596 Res = LHS.Val - RHS.Val;
597 else
598 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 break;
600 case tok::lessequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000601 Res = LHS.Val <= RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
603 break;
604 case tok::less:
Chris Lattner8ed30442008-05-05 06:45:50 +0000605 Res = LHS.Val < RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
607 break;
608 case tok::greaterequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000609 Res = LHS.Val >= RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
611 break;
612 case tok::greater:
Chris Lattner8ed30442008-05-05 06:45:50 +0000613 Res = LHS.Val > RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
615 break;
616 case tok::exclaimequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000617 Res = LHS.Val != RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
619 break;
620 case tok::equalequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000621 Res = LHS.Val == RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
623 break;
624 case tok::amp:
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::caret:
Chris Lattner8ed30442008-05-05 06:45:50 +0000628 Res = LHS.Val ^ RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 break;
630 case tok::pipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000631 Res = LHS.Val | RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 break;
633 case tok::ampamp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000634 Res = (LHS.Val != 0 && RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
636 break;
637 case tok::pipepipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000638 Res = (LHS.Val != 0 || RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
640 break;
641 case tok::comma:
Chris Lattner91891562008-05-04 18:36:18 +0000642 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
643 // if not being evaluated.
644 if (!PP.getLangOptions().C99 || ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000645 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
646 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000647 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump1eb44332009-09-09 15:08:12 +0000648 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 case tok::question: {
650 // Parse the : part of the expression.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000651 if (PeekTok.isNot(tok::colon)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000652 PP.Diag(PeekTok.getLocation(), diag::err_expected_colon)
653 << LHS.getRange(), RHS.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000654 PP.Diag(OpLoc, diag::note_matching) << "?";
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 return true;
656 }
657 // Consume the :.
658 PP.LexNonComment(PeekTok);
659
660 // Evaluate the value after the :.
Chris Lattner8ed30442008-05-05 06:45:50 +0000661 bool AfterColonLive = ValueLive && LHS.Val == 0;
662 PPValue AfterColonVal(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 DefinedTracker DT;
664 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
665 return true;
666
Chris Lattner44cbbb02008-05-05 20:09:27 +0000667 // Parse anything after the : with the same precedence as ?. We allow
668 // things of equal precedence because ?: is right associative.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000669 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 PeekTok, AfterColonLive, PP))
671 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner8ed30442008-05-05 06:45:50 +0000674 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
675 RHS.setEnd(AfterColonVal.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000676
677 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
678 // either operand is unsigned.
679 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 // Figure out the precedence of the token after the : part.
682 PeekPrec = getPrecedence(PeekTok.getKind());
683 break;
684 }
685 case tok::colon:
686 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattner204b2fe2008-11-18 21:48:13 +0000687 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
688 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 return true;
690 }
691
692 // If this operator is live and overflowed, report the issue.
693 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000694 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
695 << LHS.getRange() << RHS.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 // Put the result back into 'LHS' for our next iteration.
Chris Lattner8ed30442008-05-05 06:45:50 +0000698 LHS.Val = Res;
699 LHS.setEnd(RHS.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 }
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 return false;
703}
704
705/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
706/// may occur after a #if or #elif directive. If the expression is equivalent
707/// to "!defined(X)" return X in IfNDefMacro.
708bool Preprocessor::
709EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
Chris Lattnera3e008a2009-12-14 05:00:18 +0000710 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
711 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
712 // in which case a directive is undefined behavior. We want macros to be able
713 // to recursively expand in order to get more gcc-list behavior, so we force
714 // DisableMacroExpansion to false and restore it when we're done parsing the
715 // expression.
716 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
717 DisableMacroExpansion = false;
718
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 // Peek ahead one token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000720 Token Tok;
Eli Friedman88710f22011-08-03 00:04:13 +0000721 LexNonComment(Tok);
Douglas Gregor1fbb4472010-08-24 20:21:13 +0000722
Reid Spencer5f016e22007-07-11 17:01:13 +0000723 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner98be4942008-03-05 18:54:05 +0000724 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Chris Lattner8ed30442008-05-05 06:45:50 +0000726 PPValue ResVal(BitWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 DefinedTracker DT;
728 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
729 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000730 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000731 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000732
733 // Restore 'DisableMacroExpansion'.
734 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000735 return false;
736 }
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Reid Spencer5f016e22007-07-11 17:01:13 +0000738 // If we are at the end of the expression after just parsing a value, there
739 // must be no (unparenthesized) binary operators involved, so we can exit
740 // directly.
Peter Collingbourne84021552011-02-28 02:37:51 +0000741 if (Tok.is(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 // If the expression we parsed was of the form !defined(macro), return the
743 // macro in IfNDefMacro.
744 if (DT.State == DefinedTracker::NotDefinedMacro)
745 IfNDefMacro = DT.TheMacro;
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Chris Lattnera3e008a2009-12-14 05:00:18 +0000747 // Restore 'DisableMacroExpansion'.
748 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000749 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 }
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Reid Spencer5f016e22007-07-11 17:01:13 +0000752 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
753 // operator and the stuff after it.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000754 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
755 Tok, true, *this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000756 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000757 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000759
760 // Restore 'DisableMacroExpansion'.
761 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 return false;
763 }
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Peter Collingbourne84021552011-02-28 02:37:51 +0000765 // If we aren't at the tok::eod token, something bad happened, like an extra
Reid Spencer5f016e22007-07-11 17:01:13 +0000766 // ')' token.
Peter Collingbourne84021552011-02-28 02:37:51 +0000767 if (Tok.isNot(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000768 Diag(Tok, diag::err_pp_expected_eol);
769 DiscardUntilEndOfDirective();
770 }
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Chris Lattnera3e008a2009-12-14 05:00:18 +0000772 // Restore 'DisableMacroExpansion'.
773 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000774 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000775}
776