blob: 7f00e6ee9607a6257cfd3670449d0095acc8e70e [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);
Eli Friedman88710f22011-08-03 00:04:13 +000099 PP.LexUnexpandedNonComment(PeekTok);
Douglas Gregor1fbb4472010-08-24 20:21:13 +0000100 }
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
John Thompsona28cc092009-10-30 13:49:06 +0000118 // If we are in parens, ensure we have a trailing ).
119 if (LParenLoc.isValid()) {
Eli Friedman88710f22011-08-03 00:04:13 +0000120 // Consume identifier.
121 Result.setEnd(PeekTok.getLocation());
122 PP.LexUnexpandedNonComment(PeekTok);
123
John Thompsona28cc092009-10-30 13:49:06 +0000124 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);
Eli Friedman88710f22011-08-03 00:04:13 +0000132 } else {
133 // Consume identifier.
134 Result.setEnd(PeekTok.getLocation());
135 PP.LexNonComment(PeekTok);
John Thompsona28cc092009-10-30 13:49:06 +0000136 }
137
138 // Success, remember that we saw defined(X).
139 DT.State = DefinedTracker::DefinedMacro;
140 DT.TheMacro = II;
141 return false;
142}
143
Reid Spencer5f016e22007-07-11 17:01:13 +0000144/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
145/// return the computed value in Result. Return true if there was an error
146/// parsing. This function also returns information about the form of the
147/// expression in DT. See above for information on what DT means.
148///
149/// If ValueLive is false, then this value is being evaluated in a context where
150/// the result is not used. As such, avoid diagnostics that relate to
151/// evaluation.
Chris Lattner8ed30442008-05-05 06:45:50 +0000152static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
153 bool ValueLive, Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 DT.State = DefinedTracker::Unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Douglas Gregorf29c5232010-08-24 22:20:20 +0000156 if (PeekTok.is(tok::code_completion)) {
157 if (PP.getCodeCompletionHandler())
158 PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
Eli Friedman88710f22011-08-03 00:04:13 +0000159 PP.LexNonComment(PeekTok);
Douglas Gregorf29c5232010-08-24 22:20:20 +0000160 }
161
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 // If this token's spelling is a pp-identifier, check to see if it is
163 // 'defined' or if it is a macro. Note that we check here because many
164 // keywords are pp-identifiers, so we can't check the kind.
165 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
Chris Lattnerf8806622009-12-14 04:26:45 +0000166 // Handle "defined X" and "defined(X)".
167 if (II->isStr("defined"))
John Thompsona28cc092009-10-30 13:49:06 +0000168 return(EvaluateDefined(Result, PeekTok, DT, ValueLive, PP));
Chris Lattnerf8806622009-12-14 04:26:45 +0000169
170 // If this identifier isn't 'defined' or one of the special
171 // preprocessor keywords and it wasn't macro expanded, it turns
172 // into a simple 0, unless it is the C++ keyword "true", in which case it
173 // turns into "1".
174 if (ValueLive)
175 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
176 Result.Val = II->getTokenID() == tok::kw_true;
177 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
178 Result.setRange(PeekTok.getLocation());
179 PP.LexNonComment(PeekTok);
180 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 }
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 switch (PeekTok.getKind()) {
184 default: // Non-value token.
Chris Lattnerd98d9752008-04-13 20:38:43 +0000185 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 return true;
Peter Collingbourne84021552011-02-28 02:37:51 +0000187 case tok::eod:
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 case tok::r_paren:
189 // If there is no expression, report and exit.
190 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
191 return true;
192 case tok::numeric_constant: {
193 llvm::SmallString<64> IntegerBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000194 bool NumberInvalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000195 StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
Douglas Gregor50f6af72010-03-16 05:20:39 +0000196 &NumberInvalid);
197 if (NumberInvalid)
198 return true; // a diagnostic was already reported
199
Benjamin Krameraeb863c2010-02-27 16:29:36 +0000200 NumericLiteralParser Literal(Spelling.begin(), Spelling.end(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 PeekTok.getLocation(), PP);
202 if (Literal.hadError)
203 return true; // a diagnostic was already reported.
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Chris Lattner6e400c22007-08-26 03:29:23 +0000205 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
207 return true;
208 }
209 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
210
Neil Boothb9449512007-08-29 22:00:19 +0000211 // long long is a C99 feature.
212 if (!PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus0x
Neil Booth79859c32007-08-29 22:13:52 +0000213 && Literal.isLongLong)
Neil Boothb9449512007-08-29 22:00:19 +0000214 PP.Diag(PeekTok, diag::ext_longlong);
215
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 // Parse the integer literal into Result.
Chris Lattner8ed30442008-05-05 06:45:50 +0000217 if (Literal.GetIntegerValue(Result.Val)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 // Overflow parsing integer literal.
219 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattner8ed30442008-05-05 06:45:50 +0000220 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 } else {
222 // Set the signedness of the result to match whether there was a U suffix
223 // or not.
Chris Lattner8ed30442008-05-05 06:45:50 +0000224 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 // Detect overflow based on whether the value is signed. If signed
227 // and if the value is too large, emit a warning "integer constant is so
228 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
229 // is 64-bits.
Chris Lattner8ed30442008-05-05 06:45:50 +0000230 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Chris Lattnerb081a352008-07-03 03:47:30 +0000231 // Don't warn for a hex literal: 0x8000..0 shouldn't warn.
232 if (ValueLive && Literal.getRadix() != 16)
Chris Lattner8ed30442008-05-05 06:45:50 +0000233 PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
234 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 }
236 }
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000239 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 PP.LexNonComment(PeekTok);
241 return false;
242 }
Douglas Gregor5cee1192011-07-27 05:40:30 +0000243 case tok::char_constant: // 'x'
244 case tok::wide_char_constant: { // L'x'
245 case tok::utf16_char_constant: // u'x'
246 case tok::utf32_char_constant: // U'x'
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 llvm::SmallString<32> CharBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000248 bool CharInvalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000249 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
Douglas Gregor50f6af72010-03-16 05:20:39 +0000250 if (CharInvalid)
251 return true;
Benjamin Kramerddeea562010-02-27 13:44:12 +0000252
253 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Douglas Gregor5cee1192011-07-27 05:40:30 +0000254 PeekTok.getLocation(), PP, PeekTok.getKind());
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 if (Literal.hadError())
256 return true; // A diagnostic was already emitted.
257
258 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar444be732009-11-13 05:51:54 +0000259 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000260 unsigned NumBits;
261 if (Literal.isMultiChar())
262 NumBits = TI.getIntWidth();
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000263 else if (Literal.isWide())
264 NumBits = TI.getWCharWidth();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000265 else if (Literal.isUTF16())
266 NumBits = TI.getChar16Width();
267 else if (Literal.isUTF32())
268 NumBits = TI.getChar32Width();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000269 else
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000270 NumBits = TI.getCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000271
Reid Spencer5f016e22007-07-11 17:01:13 +0000272 // Set the width.
273 llvm::APSInt Val(NumBits);
274 // Set the value.
275 Val = Literal.getValue();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000276 // Set the signedness. UTF-16 and UTF-32 are always unsigned
277 if (!Literal.isUTF16() && !Literal.isUTF32())
278 Val.setIsUnsigned(!PP.getLangOptions().CharIsSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Chris Lattner8ed30442008-05-05 06:45:50 +0000280 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
281 Result.Val = Val.extend(Result.Val.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000283 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 "intmax_t smaller than char/wchar_t?");
Chris Lattner8ed30442008-05-05 06:45:50 +0000285 Result.Val = Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 }
287
288 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000289 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 PP.LexNonComment(PeekTok);
291 return false;
292 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000293 case tok::l_paren: {
294 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 PP.LexNonComment(PeekTok); // Eat the (.
296 // Parse the value and if there are any binary operators involved, parse
297 // them.
298 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
299
300 // If this is a silly value like (X), which doesn't need parens, check for
301 // !(defined X).
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000302 if (PeekTok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 // Just use DT unmodified as our result.
304 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000305 // Otherwise, we have something like (x+y), and we consumed '(x'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
307 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000309 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000310 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
311 << Result.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000312 PP.Diag(Start, diag::note_matching) << "(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 return true;
314 }
315 DT.State = DefinedTracker::Unknown;
316 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000317 Result.setRange(Start, PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 PP.LexNonComment(PeekTok); // Eat the ).
319 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000320 }
321 case tok::plus: {
322 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 // Unary plus doesn't modify the value.
324 PP.LexNonComment(PeekTok);
Chris Lattner8ed30442008-05-05 06:45:50 +0000325 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
326 Result.setBegin(Start);
327 return false;
328 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 case tok::minus: {
330 SourceLocation Loc = PeekTok.getLocation();
331 PP.LexNonComment(PeekTok);
332 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000333 Result.setBegin(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000336 Result.Val = -Result.Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Chris Lattnerb081a352008-07-03 03:47:30 +0000338 // -MININT is the only thing that overflows. Unsigned never overflows.
339 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 // If this operator is live and overflowed, report the issue.
342 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000343 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 DT.State = DefinedTracker::Unknown;
346 return false;
347 }
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Chris Lattner8ed30442008-05-05 06:45:50 +0000349 case tok::tilde: {
350 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 PP.LexNonComment(PeekTok);
352 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000353 Result.setBegin(Start);
354
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000356 Result.Val = ~Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 DT.State = DefinedTracker::Unknown;
358 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000359 }
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Chris Lattner8ed30442008-05-05 06:45:50 +0000361 case tok::exclaim: {
362 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 PP.LexNonComment(PeekTok);
364 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000365 Result.setBegin(Start);
366 Result.Val = !Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner8ed30442008-05-05 06:45:50 +0000368 Result.Val.setIsUnsigned(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 if (DT.State == DefinedTracker::DefinedMacro)
371 DT.State = DefinedTracker::NotDefinedMacro;
372 else if (DT.State == DefinedTracker::NotDefinedMacro)
373 DT.State = DefinedTracker::DefinedMacro;
374 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000375 }
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Reid Spencer5f016e22007-07-11 17:01:13 +0000377 // FIXME: Handle #assert
378 }
379}
380
381
382
383/// getPrecedence - Return the precedence of the specified binary operator
384/// token. This returns:
385/// ~0 - Invalid token.
Chris Lattner9e66ba62008-05-05 04:10:51 +0000386/// 14 -> 3 - various operators.
Peter Collingbourne84021552011-02-28 02:37:51 +0000387/// 0 - 'eod' or ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000388static unsigned getPrecedence(tok::TokenKind Kind) {
389 switch (Kind) {
390 default: return ~0U;
391 case tok::percent:
392 case tok::slash:
393 case tok::star: return 14;
394 case tok::plus:
395 case tok::minus: return 13;
396 case tok::lessless:
397 case tok::greatergreater: return 12;
398 case tok::lessequal:
399 case tok::less:
400 case tok::greaterequal:
401 case tok::greater: return 11;
402 case tok::exclaimequal:
403 case tok::equalequal: return 10;
404 case tok::amp: return 9;
405 case tok::caret: return 8;
406 case tok::pipe: return 7;
407 case tok::ampamp: return 6;
408 case tok::pipepipe: return 5;
Chris Lattner98ed49f2008-05-05 20:07:41 +0000409 case tok::question: return 4;
410 case tok::comma: return 3;
Chris Lattner91891562008-05-04 18:36:18 +0000411 case tok::colon: return 2;
Peter Collingbourne84021552011-02-28 02:37:51 +0000412 case tok::r_paren: return 0;// Lowest priority, end of expr.
413 case tok::eod: return 0;// Lowest priority, end of directive.
Reid Spencer5f016e22007-07-11 17:01:13 +0000414 }
415}
416
417
418/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner8ed30442008-05-05 06:45:50 +0000419/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Reid Spencer5f016e22007-07-11 17:01:13 +0000420///
421/// If ValueLive is false, then this value is being evaluated in a context where
422/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner8ed30442008-05-05 06:45:50 +0000423/// evaluation, such as division by zero warnings.
424static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +0000425 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +0000426 Preprocessor &PP) {
427 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
428 // If this token isn't valid, report the error.
429 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000430 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
431 << LHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 return true;
433 }
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Reid Spencer5f016e22007-07-11 17:01:13 +0000435 while (1) {
436 // If this token has a lower precedence than we are allowed to parse, return
437 // it so that higher levels of the recursion can parse it.
438 if (PeekPrec < MinPrec)
439 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Reid Spencer5f016e22007-07-11 17:01:13 +0000441 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump1eb44332009-09-09 15:08:12 +0000444 // dead. Note that this cannot just clobber ValueLive. Consider
Reid Spencer5f016e22007-07-11 17:01:13 +0000445 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
446 // this example, the RHS of the && being dead does not make the rest of the
447 // expr dead.
448 bool RHSIsLive;
Chris Lattner8ed30442008-05-05 06:45:50 +0000449 if (Operator == tok::ampamp && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000450 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000451 else if (Operator == tok::pipepipe && LHS.Val != 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000453 else if (Operator == tok::question && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000454 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
455 else
456 RHSIsLive = ValueLive;
457
Chris Lattner8ed30442008-05-05 06:45:50 +0000458 // Consume the operator, remembering the operator's location for reporting.
459 SourceLocation OpLoc = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000460 PP.LexNonComment(PeekTok);
461
Chris Lattner8ed30442008-05-05 06:45:50 +0000462 PPValue RHS(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000463 // Parse the RHS of the operator.
464 DefinedTracker DT;
465 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
466
467 // Remember the precedence of this operator and get the precedence of the
468 // operator immediately to the right of the RHS.
469 unsigned ThisPrec = PeekPrec;
470 PeekPrec = getPrecedence(PeekTok.getKind());
471
472 // If this token isn't valid, report the error.
473 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000474 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
475 << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000476 return true;
477 }
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Chris Lattner98ed49f2008-05-05 20:07:41 +0000479 // Decide whether to include the next binop in this subexpression. For
480 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattner44cbbb02008-05-05 20:09:27 +0000481 // handle y*z as a single subexpression. We do this because the precedence
482 // of * is higher than that of +. The only strange case we have to handle
483 // here is for the ?: operator, where the precedence is actually lower than
484 // the LHS of the '?'. The grammar rule is:
Chris Lattner98ed49f2008-05-05 20:07:41 +0000485 //
486 // conditional-expression ::=
487 // logical-OR-expression ? expression : conditional-expression
488 // where 'expression' is actually comma-expression.
489 unsigned RHSPrec;
490 if (Operator == tok::question)
491 // The RHS of "?" should be maximally consumed as an expression.
492 RHSPrec = getPrecedence(tok::comma);
493 else // All others should munch while higher precedence.
494 RHSPrec = ThisPrec+1;
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Chris Lattner98ed49f2008-05-05 20:07:41 +0000496 if (PeekPrec >= RHSPrec) {
497 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 return true;
499 PeekPrec = getPrecedence(PeekTok.getKind());
500 }
501 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Reid Spencer5f016e22007-07-11 17:01:13 +0000503 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump1eb44332009-09-09 15:08:12 +0000504 // either operand is unsigned.
Reid Spencer5f016e22007-07-11 17:01:13 +0000505 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattner019ef7e2008-05-04 23:46:17 +0000506 switch (Operator) {
507 case tok::question: // No UAC for x and y in "x ? y : z".
508 case tok::lessless: // Shift amount doesn't UAC with shift value.
509 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
510 case tok::comma: // Comma operands are not subject to UACs.
511 case tok::pipepipe: // Logical || does not do UACs.
512 case tok::ampamp: // Logical && does not do UACs.
513 break; // No UAC
514 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000515 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
516 // If this just promoted something from signed to unsigned, and if the
517 // value was negative, warn about it.
518 if (ValueLive && Res.isUnsigned()) {
Chris Lattner8ed30442008-05-05 06:45:50 +0000519 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000520 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
521 << LHS.Val.toString(10, true) + " to " +
522 LHS.Val.toString(10, false)
523 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000524 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000525 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
526 << RHS.Val.toString(10, true) + " to " +
527 RHS.Val.toString(10, false)
528 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000529 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000530 LHS.Val.setIsUnsigned(Res.isUnsigned());
531 RHS.Val.setIsUnsigned(Res.isUnsigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 }
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Reid Spencer5f016e22007-07-11 17:01:13 +0000534 bool Overflow = false;
535 switch (Operator) {
536 default: assert(0 && "Unknown operator token!");
537 case tok::percent:
Chris Lattner8ed30442008-05-05 06:45:50 +0000538 if (RHS.Val != 0)
539 Res = LHS.Val % RHS.Val;
540 else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000541 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
542 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000543 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 break;
546 case tok::slash:
Chris Lattner8ed30442008-05-05 06:45:50 +0000547 if (RHS.Val != 0) {
Chris Lattnerf9e77342010-10-13 23:46:56 +0000548 if (LHS.Val.isSigned())
549 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
550 else
551 Res = LHS.Val / RHS.Val;
Chris Lattner8ed30442008-05-05 06:45:50 +0000552 } else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000553 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
554 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000555 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 case tok::star:
Chris Lattnerf9e77342010-10-13 23:46:56 +0000560 if (Res.isSigned())
561 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
562 else
563 Res = LHS.Val * RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 break;
565 case tok::lessless: {
566 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000567 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Chris Lattnerf9e77342010-10-13 23:46:56 +0000568 if (LHS.isUnsigned()) {
569 Overflow = ShAmt >= LHS.Val.getBitWidth();
570 if (Overflow)
571 ShAmt = LHS.Val.getBitWidth()-1;
572 Res = LHS.Val << ShAmt;
573 } else {
574 Res = llvm::APSInt(LHS.Val.sshl_ov(ShAmt, Overflow), false);
575 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000576 break;
577 }
578 case tok::greatergreater: {
579 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000580 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 if (ShAmt >= LHS.getBitWidth())
582 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner8ed30442008-05-05 06:45:50 +0000583 Res = LHS.Val >> ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 break;
585 }
586 case tok::plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 if (LHS.isUnsigned())
Chris Lattnerf9e77342010-10-13 23:46:56 +0000588 Res = LHS.Val + RHS.Val;
589 else
590 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 break;
592 case tok::minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000593 if (LHS.isUnsigned())
Chris Lattnerf9e77342010-10-13 23:46:56 +0000594 Res = LHS.Val - RHS.Val;
595 else
596 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000597 break;
598 case tok::lessequal:
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::less:
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::greaterequal:
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::greater:
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.8p6, result is always int (signed)
613 break;
614 case tok::exclaimequal:
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::equalequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000619 Res = LHS.Val == RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
621 break;
622 case tok::amp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000623 Res = LHS.Val & RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 break;
625 case tok::caret:
Chris Lattner8ed30442008-05-05 06:45:50 +0000626 Res = LHS.Val ^ RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000627 break;
628 case tok::pipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000629 Res = LHS.Val | RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 break;
631 case tok::ampamp:
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.13p3, result is always int (signed)
634 break;
635 case tok::pipepipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000636 Res = (LHS.Val != 0 || RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
638 break;
639 case tok::comma:
Chris Lattner91891562008-05-04 18:36:18 +0000640 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
641 // if not being evaluated.
642 if (!PP.getLangOptions().C99 || ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000643 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
644 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000645 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump1eb44332009-09-09 15:08:12 +0000646 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000647 case tok::question: {
648 // Parse the : part of the expression.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000649 if (PeekTok.isNot(tok::colon)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000650 PP.Diag(PeekTok.getLocation(), diag::err_expected_colon)
651 << LHS.getRange(), RHS.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000652 PP.Diag(OpLoc, diag::note_matching) << "?";
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 return true;
654 }
655 // Consume the :.
656 PP.LexNonComment(PeekTok);
657
658 // Evaluate the value after the :.
Chris Lattner8ed30442008-05-05 06:45:50 +0000659 bool AfterColonLive = ValueLive && LHS.Val == 0;
660 PPValue AfterColonVal(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 DefinedTracker DT;
662 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
663 return true;
664
Chris Lattner44cbbb02008-05-05 20:09:27 +0000665 // Parse anything after the : with the same precedence as ?. We allow
666 // things of equal precedence because ?: is right associative.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000667 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 PeekTok, AfterColonLive, PP))
669 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner8ed30442008-05-05 06:45:50 +0000672 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
673 RHS.setEnd(AfterColonVal.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000674
675 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
676 // either operand is unsigned.
677 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 // Figure out the precedence of the token after the : part.
680 PeekPrec = getPrecedence(PeekTok.getKind());
681 break;
682 }
683 case tok::colon:
684 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattner204b2fe2008-11-18 21:48:13 +0000685 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
686 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 return true;
688 }
689
690 // If this operator is live and overflowed, report the issue.
691 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000692 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
693 << LHS.getRange() << RHS.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 // Put the result back into 'LHS' for our next iteration.
Chris Lattner8ed30442008-05-05 06:45:50 +0000696 LHS.Val = Res;
697 LHS.setEnd(RHS.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 }
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 return false;
701}
702
703/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
704/// may occur after a #if or #elif directive. If the expression is equivalent
705/// to "!defined(X)" return X in IfNDefMacro.
706bool Preprocessor::
707EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
Chris Lattnera3e008a2009-12-14 05:00:18 +0000708 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
709 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
710 // in which case a directive is undefined behavior. We want macros to be able
711 // to recursively expand in order to get more gcc-list behavior, so we force
712 // DisableMacroExpansion to false and restore it when we're done parsing the
713 // expression.
714 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
715 DisableMacroExpansion = false;
716
Reid Spencer5f016e22007-07-11 17:01:13 +0000717 // Peek ahead one token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000718 Token Tok;
Eli Friedman88710f22011-08-03 00:04:13 +0000719 LexNonComment(Tok);
Douglas Gregor1fbb4472010-08-24 20:21:13 +0000720
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner98be4942008-03-05 18:54:05 +0000722 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Chris Lattner8ed30442008-05-05 06:45:50 +0000724 PPValue ResVal(BitWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 DefinedTracker DT;
726 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
727 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000728 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000729 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000730
731 // Restore 'DisableMacroExpansion'.
732 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 return false;
734 }
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 // If we are at the end of the expression after just parsing a value, there
737 // must be no (unparenthesized) binary operators involved, so we can exit
738 // directly.
Peter Collingbourne84021552011-02-28 02:37:51 +0000739 if (Tok.is(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000740 // If the expression we parsed was of the form !defined(macro), return the
741 // macro in IfNDefMacro.
742 if (DT.State == DefinedTracker::NotDefinedMacro)
743 IfNDefMacro = DT.TheMacro;
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Chris Lattnera3e008a2009-12-14 05:00:18 +0000745 // Restore 'DisableMacroExpansion'.
746 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000747 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000748 }
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
751 // operator and the stuff after it.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000752 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
753 Tok, true, *this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000754 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000755 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000756 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000757
758 // Restore 'DisableMacroExpansion'.
759 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000760 return false;
761 }
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Peter Collingbourne84021552011-02-28 02:37:51 +0000763 // If we aren't at the tok::eod token, something bad happened, like an extra
Reid Spencer5f016e22007-07-11 17:01:13 +0000764 // ')' token.
Peter Collingbourne84021552011-02-28 02:37:51 +0000765 if (Tok.isNot(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000766 Diag(Tok, diag::err_pp_expected_eol);
767 DiscardUntilEndOfDirective();
768 }
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Chris Lattnera3e008a2009-12-14 05:00:18 +0000770 // Restore 'DisableMacroExpansion'.
771 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000772 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000773}
774