blob: 3cba06f46034dcba20ad9ab17d295dc0348cdd9a [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Preprocessor::EvaluateDirectiveExpression method,
11// which parses and evaluates integer constant expressions for #if directives.
12//
13//===----------------------------------------------------------------------===//
14//
15// FIXME: implement testing for #assert's.
16//
17//===----------------------------------------------------------------------===//
18
19#include "clang/Lex/Preprocessor.h"
20#include "clang/Lex/MacroInfo.h"
21#include "clang/Lex/LiteralSupport.h"
22#include "clang/Basic/TargetInfo.h"
Chris Lattner500d3292009-01-29 05:15:15 +000023#include "clang/Lex/LexDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "llvm/ADT/APSInt.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
Chris Lattner8ed30442008-05-05 06:45:50 +000027/// PPValue - Represents the value of a subexpression of a preprocessor
28/// conditional and the source range covered by it.
29class PPValue {
30 SourceRange Range;
31public:
32 llvm::APSInt Val;
33
34 // Default ctor - Construct an 'invalid' PPValue.
35 PPValue(unsigned BitWidth) : Val(BitWidth) {}
36
37 unsigned getBitWidth() const { return Val.getBitWidth(); }
38 bool isUnsigned() const { return Val.isUnsigned(); }
39
40 const SourceRange &getRange() const { return Range; }
41
42 void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
43 void setRange(SourceLocation B, SourceLocation E) {
44 Range.setBegin(B); Range.setEnd(E);
45 }
46 void setBegin(SourceLocation L) { Range.setBegin(L); }
47 void setEnd(SourceLocation L) { Range.setEnd(L); }
48};
49
50static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +000051 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +000052 Preprocessor &PP);
53
54/// DefinedTracker - This struct is used while parsing expressions to keep track
55/// of whether !defined(X) has been seen.
56///
57/// With this simple scheme, we handle the basic forms:
58/// !defined(X) and !defined X
59/// but we also trivially handle (silly) stuff like:
60/// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
61struct DefinedTracker {
62 /// Each time a Value is evaluated, it returns information about whether the
63 /// parsed value is of the form defined(X), !defined(X) or is something else.
64 enum TrackerState {
65 DefinedMacro, // defined(X)
66 NotDefinedMacro, // !defined(X)
67 Unknown // Something else.
68 } State;
69 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
70 /// indicates the macro that was checked.
71 IdentifierInfo *TheMacro;
72};
73
Reid Spencer5f016e22007-07-11 17:01:13 +000074/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
75/// return the computed value in Result. Return true if there was an error
76/// parsing. This function also returns information about the form of the
77/// expression in DT. See above for information on what DT means.
78///
79/// If ValueLive is false, then this value is being evaluated in a context where
80/// the result is not used. As such, avoid diagnostics that relate to
81/// evaluation.
Chris Lattner8ed30442008-05-05 06:45:50 +000082static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
83 bool ValueLive, Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +000084 DT.State = DefinedTracker::Unknown;
85
86 // If this token's spelling is a pp-identifier, check to see if it is
87 // 'defined' or if it is a macro. Note that we check here because many
88 // keywords are pp-identifiers, so we can't check the kind.
89 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
90 // If this identifier isn't 'defined' and it wasn't macro expanded, it turns
91 // into a simple 0, unless it is the C++ keyword "true", in which case it
92 // turns into "1".
93 if (II->getPPKeywordID() != tok::pp_defined) {
Chris Lattner4d2d04e2009-01-18 21:18:58 +000094 if (ValueLive)
95 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
Chris Lattner8ed30442008-05-05 06:45:50 +000096 Result.Val = II->getTokenID() == tok::kw_true;
97 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
98 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +000099 PP.LexNonComment(PeekTok);
100 return false;
101 }
102
103 // Handle "defined X" and "defined(X)".
Chris Lattner8ed30442008-05-05 06:45:50 +0000104 Result.setBegin(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000105
106 // Get the next token, don't expand it.
107 PP.LexUnexpandedToken(PeekTok);
108
109 // Two options, it can either be a pp-identifier or a (.
Chris Lattner8ed30442008-05-05 06:45:50 +0000110 SourceLocation LParenLoc;
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000111 if (PeekTok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 // Found a paren, remember we saw it and skip it.
Chris Lattner8ed30442008-05-05 06:45:50 +0000113 LParenLoc = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 PP.LexUnexpandedToken(PeekTok);
115 }
116
117 // If we don't have a pp-identifier now, this is an error.
118 if ((II = PeekTok.getIdentifierInfo()) == 0) {
119 PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier);
120 return true;
121 }
122
123 // Otherwise, we got an identifier, is it defined to something?
Chris Lattner8ed30442008-05-05 06:45:50 +0000124 Result.Val = II->hasMacroDefinition();
125 Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000126
127 // If there is a macro, mark it used.
Chris Lattner8ed30442008-05-05 06:45:50 +0000128 if (Result.Val != 0 && ValueLive) {
Chris Lattnercc1a8752007-10-07 08:44:20 +0000129 MacroInfo *Macro = PP.getMacroInfo(II);
Chris Lattner0edde552007-10-07 08:04:56 +0000130 Macro->setIsUsed(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 }
132
133 // Consume identifier.
Chris Lattner8ed30442008-05-05 06:45:50 +0000134 Result.setEnd(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 PP.LexNonComment(PeekTok);
136
137 // If we are in parens, ensure we have a trailing ).
Chris Lattner8ed30442008-05-05 06:45:50 +0000138 if (LParenLoc.isValid()) {
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000139 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattner8ed30442008-05-05 06:45:50 +0000140 PP.Diag(PeekTok.getLocation(), diag::err_pp_missing_rparen);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000141 PP.Diag(LParenLoc, diag::note_matching) << "(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 return true;
143 }
144 // Consume the ).
Chris Lattner8ed30442008-05-05 06:45:50 +0000145 Result.setEnd(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 PP.LexNonComment(PeekTok);
147 }
148
149 // Success, remember that we saw defined(X).
150 DT.State = DefinedTracker::DefinedMacro;
151 DT.TheMacro = II;
152 return false;
153 }
154
155 switch (PeekTok.getKind()) {
156 default: // Non-value token.
Chris Lattnerd98d9752008-04-13 20:38:43 +0000157 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 return true;
159 case tok::eom:
160 case tok::r_paren:
161 // If there is no expression, report and exit.
162 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
163 return true;
164 case tok::numeric_constant: {
165 llvm::SmallString<64> IntegerBuffer;
166 IntegerBuffer.resize(PeekTok.getLength());
167 const char *ThisTokBegin = &IntegerBuffer[0];
168 unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
169 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
170 PeekTok.getLocation(), PP);
171 if (Literal.hadError)
172 return true; // a diagnostic was already reported.
173
Chris Lattner6e400c22007-08-26 03:29:23 +0000174 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
176 return true;
177 }
178 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
179
Neil Boothb9449512007-08-29 22:00:19 +0000180 // long long is a C99 feature.
181 if (!PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus0x
Neil Booth79859c32007-08-29 22:13:52 +0000182 && Literal.isLongLong)
Neil Boothb9449512007-08-29 22:00:19 +0000183 PP.Diag(PeekTok, diag::ext_longlong);
184
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 // Parse the integer literal into Result.
Chris Lattner8ed30442008-05-05 06:45:50 +0000186 if (Literal.GetIntegerValue(Result.Val)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 // Overflow parsing integer literal.
188 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattner8ed30442008-05-05 06:45:50 +0000189 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 } else {
191 // Set the signedness of the result to match whether there was a U suffix
192 // or not.
Chris Lattner8ed30442008-05-05 06:45:50 +0000193 Result.Val.setIsUnsigned(Literal.isUnsigned);
Reid Spencer5f016e22007-07-11 17:01:13 +0000194
195 // Detect overflow based on whether the value is signed. If signed
196 // and if the value is too large, emit a warning "integer constant is so
197 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
198 // is 64-bits.
Chris Lattner8ed30442008-05-05 06:45:50 +0000199 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Chris Lattnerb081a352008-07-03 03:47:30 +0000200 // Don't warn for a hex literal: 0x8000..0 shouldn't warn.
201 if (ValueLive && Literal.getRadix() != 16)
Chris Lattner8ed30442008-05-05 06:45:50 +0000202 PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
203 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 }
205 }
206
207 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000208 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 PP.LexNonComment(PeekTok);
210 return false;
211 }
212 case tok::char_constant: { // 'x'
213 llvm::SmallString<32> CharBuffer;
214 CharBuffer.resize(PeekTok.getLength());
215 const char *ThisTokBegin = &CharBuffer[0];
216 unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
217 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
218 PeekTok.getLocation(), PP);
219 if (Literal.hadError())
220 return true; // A diagnostic was already emitted.
221
222 // Character literals are always int or wchar_t, expand to intmax_t.
223 TargetInfo &TI = PP.getTargetInfo();
Chris Lattner98be4942008-03-05 18:54:05 +0000224 unsigned NumBits = TI.getCharWidth(Literal.isWide());
Reid Spencer5f016e22007-07-11 17:01:13 +0000225
226 // Set the width.
227 llvm::APSInt Val(NumBits);
228 // Set the value.
229 Val = Literal.getValue();
230 // Set the signedness.
Chris Lattner98be4942008-03-05 18:54:05 +0000231 Val.setIsUnsigned(!TI.isCharSigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000232
Chris Lattner8ed30442008-05-05 06:45:50 +0000233 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
234 Result.Val = Val.extend(Result.Val.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000236 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 "intmax_t smaller than char/wchar_t?");
Chris Lattner8ed30442008-05-05 06:45:50 +0000238 Result.Val = Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 }
240
241 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000242 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 PP.LexNonComment(PeekTok);
244 return false;
245 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000246 case tok::l_paren: {
247 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 PP.LexNonComment(PeekTok); // Eat the (.
249 // Parse the value and if there are any binary operators involved, parse
250 // them.
251 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
252
253 // If this is a silly value like (X), which doesn't need parens, check for
254 // !(defined X).
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000255 if (PeekTok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 // Just use DT unmodified as our result.
257 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000258 // Otherwise, we have something like (x+y), and we consumed '(x'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000259 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
260 return true;
261
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000262 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000263 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
264 << Result.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000265 PP.Diag(Start, diag::note_matching) << "(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 return true;
267 }
268 DT.State = DefinedTracker::Unknown;
269 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000270 Result.setRange(Start, PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 PP.LexNonComment(PeekTok); // Eat the ).
272 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000273 }
274 case tok::plus: {
275 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 // Unary plus doesn't modify the value.
277 PP.LexNonComment(PeekTok);
Chris Lattner8ed30442008-05-05 06:45:50 +0000278 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
279 Result.setBegin(Start);
280 return false;
281 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 case tok::minus: {
283 SourceLocation Loc = PeekTok.getLocation();
284 PP.LexNonComment(PeekTok);
285 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000286 Result.setBegin(Loc);
287
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000289 Result.Val = -Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000290
Chris Lattnerb081a352008-07-03 03:47:30 +0000291 // -MININT is the only thing that overflows. Unsigned never overflows.
292 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Reid Spencer5f016e22007-07-11 17:01:13 +0000293
294 // If this operator is live and overflowed, report the issue.
295 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000296 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000297
298 DT.State = DefinedTracker::Unknown;
299 return false;
300 }
301
Chris Lattner8ed30442008-05-05 06:45:50 +0000302 case tok::tilde: {
303 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 PP.LexNonComment(PeekTok);
305 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000306 Result.setBegin(Start);
307
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000309 Result.Val = ~Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 DT.State = DefinedTracker::Unknown;
311 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000312 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000313
Chris Lattner8ed30442008-05-05 06:45:50 +0000314 case tok::exclaim: {
315 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 PP.LexNonComment(PeekTok);
317 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000318 Result.setBegin(Start);
319 Result.Val = !Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner8ed30442008-05-05 06:45:50 +0000321 Result.Val.setIsUnsigned(false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000322
323 if (DT.State == DefinedTracker::DefinedMacro)
324 DT.State = DefinedTracker::NotDefinedMacro;
325 else if (DT.State == DefinedTracker::NotDefinedMacro)
326 DT.State = DefinedTracker::DefinedMacro;
327 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000328 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000329
330 // FIXME: Handle #assert
331 }
332}
333
334
335
336/// getPrecedence - Return the precedence of the specified binary operator
337/// token. This returns:
338/// ~0 - Invalid token.
Chris Lattner9e66ba62008-05-05 04:10:51 +0000339/// 14 -> 3 - various operators.
340/// 0 - 'eom' or ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000341static unsigned getPrecedence(tok::TokenKind Kind) {
342 switch (Kind) {
343 default: return ~0U;
344 case tok::percent:
345 case tok::slash:
346 case tok::star: return 14;
347 case tok::plus:
348 case tok::minus: return 13;
349 case tok::lessless:
350 case tok::greatergreater: return 12;
351 case tok::lessequal:
352 case tok::less:
353 case tok::greaterequal:
354 case tok::greater: return 11;
355 case tok::exclaimequal:
356 case tok::equalequal: return 10;
357 case tok::amp: return 9;
358 case tok::caret: return 8;
359 case tok::pipe: return 7;
360 case tok::ampamp: return 6;
361 case tok::pipepipe: return 5;
Chris Lattner98ed49f2008-05-05 20:07:41 +0000362 case tok::question: return 4;
363 case tok::comma: return 3;
Chris Lattner91891562008-05-04 18:36:18 +0000364 case tok::colon: return 2;
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 case tok::r_paren: return 0; // Lowest priority, end of expr.
366 case tok::eom: return 0; // Lowest priority, end of macro.
367 }
368}
369
370
371/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner8ed30442008-05-05 06:45:50 +0000372/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Reid Spencer5f016e22007-07-11 17:01:13 +0000373///
374/// If ValueLive is false, then this value is being evaluated in a context where
375/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner8ed30442008-05-05 06:45:50 +0000376/// evaluation, such as division by zero warnings.
377static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +0000378 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +0000379 Preprocessor &PP) {
380 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
381 // If this token isn't valid, report the error.
382 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000383 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
384 << LHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 return true;
386 }
387
388 while (1) {
389 // If this token has a lower precedence than we are allowed to parse, return
390 // it so that higher levels of the recursion can parse it.
391 if (PeekPrec < MinPrec)
392 return false;
393
394 tok::TokenKind Operator = PeekTok.getKind();
395
396 // If this is a short-circuiting operator, see if the RHS of the operator is
397 // dead. Note that this cannot just clobber ValueLive. Consider
398 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
399 // this example, the RHS of the && being dead does not make the rest of the
400 // expr dead.
401 bool RHSIsLive;
Chris Lattner8ed30442008-05-05 06:45:50 +0000402 if (Operator == tok::ampamp && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000403 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000404 else if (Operator == tok::pipepipe && LHS.Val != 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000405 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000406 else if (Operator == tok::question && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000407 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
408 else
409 RHSIsLive = ValueLive;
410
Chris Lattner8ed30442008-05-05 06:45:50 +0000411 // Consume the operator, remembering the operator's location for reporting.
412 SourceLocation OpLoc = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000413 PP.LexNonComment(PeekTok);
414
Chris Lattner8ed30442008-05-05 06:45:50 +0000415 PPValue RHS(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 // Parse the RHS of the operator.
417 DefinedTracker DT;
418 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
419
420 // Remember the precedence of this operator and get the precedence of the
421 // operator immediately to the right of the RHS.
422 unsigned ThisPrec = PeekPrec;
423 PeekPrec = getPrecedence(PeekTok.getKind());
424
425 // If this token isn't valid, report the error.
426 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000427 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
428 << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000429 return true;
430 }
431
Chris Lattner98ed49f2008-05-05 20:07:41 +0000432 // Decide whether to include the next binop in this subexpression. For
433 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattner44cbbb02008-05-05 20:09:27 +0000434 // handle y*z as a single subexpression. We do this because the precedence
435 // of * is higher than that of +. The only strange case we have to handle
436 // here is for the ?: operator, where the precedence is actually lower than
437 // the LHS of the '?'. The grammar rule is:
Chris Lattner98ed49f2008-05-05 20:07:41 +0000438 //
439 // conditional-expression ::=
440 // logical-OR-expression ? expression : conditional-expression
441 // where 'expression' is actually comma-expression.
442 unsigned RHSPrec;
443 if (Operator == tok::question)
444 // The RHS of "?" should be maximally consumed as an expression.
445 RHSPrec = getPrecedence(tok::comma);
446 else // All others should munch while higher precedence.
447 RHSPrec = ThisPrec+1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000448
Chris Lattner98ed49f2008-05-05 20:07:41 +0000449 if (PeekPrec >= RHSPrec) {
450 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 return true;
452 PeekPrec = getPrecedence(PeekTok.getKind());
453 }
454 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
455
456 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Chris Lattner019ef7e2008-05-04 23:46:17 +0000457 // either operand is unsigned.
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattner019ef7e2008-05-04 23:46:17 +0000459 switch (Operator) {
460 case tok::question: // No UAC for x and y in "x ? y : z".
461 case tok::lessless: // Shift amount doesn't UAC with shift value.
462 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
463 case tok::comma: // Comma operands are not subject to UACs.
464 case tok::pipepipe: // Logical || does not do UACs.
465 case tok::ampamp: // Logical && does not do UACs.
466 break; // No UAC
467 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
469 // If this just promoted something from signed to unsigned, and if the
470 // value was negative, warn about it.
471 if (ValueLive && Res.isUnsigned()) {
Chris Lattner8ed30442008-05-05 06:45:50 +0000472 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000473 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
474 << LHS.Val.toString(10, true) + " to " +
475 LHS.Val.toString(10, false)
476 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000477 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000478 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
479 << RHS.Val.toString(10, true) + " to " +
480 RHS.Val.toString(10, false)
481 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000482 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000483 LHS.Val.setIsUnsigned(Res.isUnsigned());
484 RHS.Val.setIsUnsigned(Res.isUnsigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 }
486
487 // FIXME: All of these should detect and report overflow??
488 bool Overflow = false;
489 switch (Operator) {
490 default: assert(0 && "Unknown operator token!");
491 case tok::percent:
Chris Lattner8ed30442008-05-05 06:45:50 +0000492 if (RHS.Val != 0)
493 Res = LHS.Val % RHS.Val;
494 else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000495 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
496 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000497 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000499 break;
500 case tok::slash:
Chris Lattner8ed30442008-05-05 06:45:50 +0000501 if (RHS.Val != 0) {
502 Res = LHS.Val / RHS.Val;
503 if (LHS.Val.isSigned()) // MININT/-1 --> overflow.
504 Overflow = LHS.Val.isMinSignedValue() && RHS.Val.isAllOnesValue();
505 } else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000506 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
507 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000508 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000509 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000510 break;
Chris Lattner3b691152008-05-04 07:15:21 +0000511
Reid Spencer5f016e22007-07-11 17:01:13 +0000512 case tok::star:
Chris Lattner8ed30442008-05-05 06:45:50 +0000513 Res = LHS.Val * RHS.Val;
514 if (LHS.Val != 0 && RHS.Val != 0)
515 Overflow = Res/RHS.Val != LHS.Val || Res/LHS.Val != RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 break;
517 case tok::lessless: {
518 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000519 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
520 if (ShAmt >= LHS.Val.getBitWidth())
521 Overflow = true, ShAmt = LHS.Val.getBitWidth()-1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 else if (LHS.isUnsigned())
Chris Lattner8ed30442008-05-05 06:45:50 +0000523 Overflow = ShAmt > LHS.Val.countLeadingZeros();
524 else if (LHS.Val.isNonNegative()) // Don't allow sign change.
525 Overflow = ShAmt >= LHS.Val.countLeadingZeros();
Reid Spencer5f016e22007-07-11 17:01:13 +0000526 else
Chris Lattner8ed30442008-05-05 06:45:50 +0000527 Overflow = ShAmt >= LHS.Val.countLeadingOnes();
Reid Spencer5f016e22007-07-11 17:01:13 +0000528
Chris Lattner8ed30442008-05-05 06:45:50 +0000529 Res = LHS.Val << ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000530 break;
531 }
532 case tok::greatergreater: {
533 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000534 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Reid Spencer5f016e22007-07-11 17:01:13 +0000535 if (ShAmt >= LHS.getBitWidth())
536 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner8ed30442008-05-05 06:45:50 +0000537 Res = LHS.Val >> ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000538 break;
539 }
540 case tok::plus:
Chris Lattner8ed30442008-05-05 06:45:50 +0000541 Res = LHS.Val + RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 if (LHS.isUnsigned())
Chris Lattner8ed30442008-05-05 06:45:50 +0000543 Overflow = Res.ult(LHS.Val);
544 else if (LHS.Val.isNonNegative() == RHS.Val.isNonNegative() &&
545 Res.isNonNegative() != LHS.Val.isNonNegative())
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 Overflow = true; // Overflow for signed addition.
547 break;
548 case tok::minus:
Chris Lattner8ed30442008-05-05 06:45:50 +0000549 Res = LHS.Val - RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 if (LHS.isUnsigned())
Chris Lattner8ed30442008-05-05 06:45:50 +0000551 Overflow = Res.ugt(LHS.Val);
552 else if (LHS.Val.isNonNegative() != RHS.Val.isNonNegative() &&
553 Res.isNonNegative() != LHS.Val.isNonNegative())
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 Overflow = true; // Overflow for signed subtraction.
555 break;
556 case tok::lessequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000557 Res = LHS.Val <= RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000558 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
559 break;
560 case tok::less:
Chris Lattner8ed30442008-05-05 06:45:50 +0000561 Res = LHS.Val < RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000562 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
563 break;
564 case tok::greaterequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000565 Res = LHS.Val >= RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
567 break;
568 case tok::greater:
Chris Lattner8ed30442008-05-05 06:45:50 +0000569 Res = LHS.Val > RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000570 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
571 break;
572 case tok::exclaimequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000573 Res = LHS.Val != RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
575 break;
576 case tok::equalequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000577 Res = LHS.Val == RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000578 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
579 break;
580 case tok::amp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000581 Res = LHS.Val & RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000582 break;
583 case tok::caret:
Chris Lattner8ed30442008-05-05 06:45:50 +0000584 Res = LHS.Val ^ RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000585 break;
586 case tok::pipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000587 Res = LHS.Val | RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 break;
589 case tok::ampamp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000590 Res = (LHS.Val != 0 && RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
592 break;
593 case tok::pipepipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000594 Res = (LHS.Val != 0 || RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
596 break;
597 case tok::comma:
Chris Lattner91891562008-05-04 18:36:18 +0000598 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
599 // if not being evaluated.
600 if (!PP.getLangOptions().C99 || ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000601 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
602 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000603 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 break;
605 case tok::question: {
606 // Parse the : part of the expression.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000607 if (PeekTok.isNot(tok::colon)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000608 PP.Diag(PeekTok.getLocation(), diag::err_expected_colon)
609 << LHS.getRange(), RHS.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000610 PP.Diag(OpLoc, diag::note_matching) << "?";
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 return true;
612 }
613 // Consume the :.
614 PP.LexNonComment(PeekTok);
615
616 // Evaluate the value after the :.
Chris Lattner8ed30442008-05-05 06:45:50 +0000617 bool AfterColonLive = ValueLive && LHS.Val == 0;
618 PPValue AfterColonVal(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 DefinedTracker DT;
620 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
621 return true;
622
Chris Lattner44cbbb02008-05-05 20:09:27 +0000623 // Parse anything after the : with the same precedence as ?. We allow
624 // things of equal precedence because ?: is right associative.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000625 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 PeekTok, AfterColonLive, PP))
627 return true;
628
629 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner8ed30442008-05-05 06:45:50 +0000630 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
631 RHS.setEnd(AfterColonVal.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000632
633 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
634 // either operand is unsigned.
635 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
636
637 // Figure out the precedence of the token after the : part.
638 PeekPrec = getPrecedence(PeekTok.getKind());
639 break;
640 }
641 case tok::colon:
642 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattner204b2fe2008-11-18 21:48:13 +0000643 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
644 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 return true;
646 }
647
648 // If this operator is live and overflowed, report the issue.
649 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000650 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
651 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000652
653 // Put the result back into 'LHS' for our next iteration.
Chris Lattner8ed30442008-05-05 06:45:50 +0000654 LHS.Val = Res;
655 LHS.setEnd(RHS.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 }
657
658 return false;
659}
660
661/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
662/// may occur after a #if or #elif directive. If the expression is equivalent
663/// to "!defined(X)" return X in IfNDefMacro.
664bool Preprocessor::
665EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
666 // Peek ahead one token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000667 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 Lex(Tok);
669
670 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner98be4942008-03-05 18:54:05 +0000671 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000672
Chris Lattner8ed30442008-05-05 06:45:50 +0000673 PPValue ResVal(BitWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 DefinedTracker DT;
675 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
676 // Parse error, skip the rest of the macro line.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000677 if (Tok.isNot(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000678 DiscardUntilEndOfDirective();
679 return false;
680 }
681
682 // If we are at the end of the expression after just parsing a value, there
683 // must be no (unparenthesized) binary operators involved, so we can exit
684 // directly.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000685 if (Tok.is(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 // If the expression we parsed was of the form !defined(macro), return the
687 // macro in IfNDefMacro.
688 if (DT.State == DefinedTracker::NotDefinedMacro)
689 IfNDefMacro = DT.TheMacro;
690
Chris Lattner8ed30442008-05-05 06:45:50 +0000691 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000692 }
693
694 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
695 // operator and the stuff after it.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000696 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
697 Tok, true, *this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 // Parse error, skip the rest of the macro line.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000699 if (Tok.isNot(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 DiscardUntilEndOfDirective();
701 return false;
702 }
703
704 // If we aren't at the tok::eom token, something bad happened, like an extra
705 // ')' token.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000706 if (Tok.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000707 Diag(Tok, diag::err_pp_expected_eol);
708 DiscardUntilEndOfDirective();
709 }
710
Chris Lattner8ed30442008-05-05 06:45:50 +0000711 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000712}
713