blob: 709e316b8036d9ad075c7278848cf50387a8f590 [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".
Chris Lattner09546d42009-04-24 07:15:22 +000093 if (!II->isStr("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();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000224 unsigned NumBits;
225 if (Literal.isMultiChar())
226 NumBits = TI.getIntWidth();
227 else
228 NumBits = TI.getCharWidth(Literal.isWide());
229
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 // Set the width.
231 llvm::APSInt Val(NumBits);
232 // Set the value.
233 Val = Literal.getValue();
234 // Set the signedness.
Chris Lattner98be4942008-03-05 18:54:05 +0000235 Val.setIsUnsigned(!TI.isCharSigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000236
Chris Lattner8ed30442008-05-05 06:45:50 +0000237 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
238 Result.Val = Val.extend(Result.Val.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000240 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 "intmax_t smaller than char/wchar_t?");
Chris Lattner8ed30442008-05-05 06:45:50 +0000242 Result.Val = Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 }
244
245 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000246 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 PP.LexNonComment(PeekTok);
248 return false;
249 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000250 case tok::l_paren: {
251 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 PP.LexNonComment(PeekTok); // Eat the (.
253 // Parse the value and if there are any binary operators involved, parse
254 // them.
255 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
256
257 // If this is a silly value like (X), which doesn't need parens, check for
258 // !(defined X).
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000259 if (PeekTok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 // Just use DT unmodified as our result.
261 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000262 // Otherwise, we have something like (x+y), and we consumed '(x'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
264 return true;
265
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000266 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000267 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
268 << Result.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000269 PP.Diag(Start, diag::note_matching) << "(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 return true;
271 }
272 DT.State = DefinedTracker::Unknown;
273 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000274 Result.setRange(Start, PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 PP.LexNonComment(PeekTok); // Eat the ).
276 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000277 }
278 case tok::plus: {
279 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 // Unary plus doesn't modify the value.
281 PP.LexNonComment(PeekTok);
Chris Lattner8ed30442008-05-05 06:45:50 +0000282 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
283 Result.setBegin(Start);
284 return false;
285 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 case tok::minus: {
287 SourceLocation Loc = PeekTok.getLocation();
288 PP.LexNonComment(PeekTok);
289 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000290 Result.setBegin(Loc);
291
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000293 Result.Val = -Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000294
Chris Lattnerb081a352008-07-03 03:47:30 +0000295 // -MININT is the only thing that overflows. Unsigned never overflows.
296 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Reid Spencer5f016e22007-07-11 17:01:13 +0000297
298 // If this operator is live and overflowed, report the issue.
299 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000300 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000301
302 DT.State = DefinedTracker::Unknown;
303 return false;
304 }
305
Chris Lattner8ed30442008-05-05 06:45:50 +0000306 case tok::tilde: {
307 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 PP.LexNonComment(PeekTok);
309 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000310 Result.setBegin(Start);
311
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000313 Result.Val = ~Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 DT.State = DefinedTracker::Unknown;
315 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000316 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000317
Chris Lattner8ed30442008-05-05 06:45:50 +0000318 case tok::exclaim: {
319 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 PP.LexNonComment(PeekTok);
321 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000322 Result.setBegin(Start);
323 Result.Val = !Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner8ed30442008-05-05 06:45:50 +0000325 Result.Val.setIsUnsigned(false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000326
327 if (DT.State == DefinedTracker::DefinedMacro)
328 DT.State = DefinedTracker::NotDefinedMacro;
329 else if (DT.State == DefinedTracker::NotDefinedMacro)
330 DT.State = DefinedTracker::DefinedMacro;
331 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000332 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000333
334 // FIXME: Handle #assert
335 }
336}
337
338
339
340/// getPrecedence - Return the precedence of the specified binary operator
341/// token. This returns:
342/// ~0 - Invalid token.
Chris Lattner9e66ba62008-05-05 04:10:51 +0000343/// 14 -> 3 - various operators.
344/// 0 - 'eom' or ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000345static unsigned getPrecedence(tok::TokenKind Kind) {
346 switch (Kind) {
347 default: return ~0U;
348 case tok::percent:
349 case tok::slash:
350 case tok::star: return 14;
351 case tok::plus:
352 case tok::minus: return 13;
353 case tok::lessless:
354 case tok::greatergreater: return 12;
355 case tok::lessequal:
356 case tok::less:
357 case tok::greaterequal:
358 case tok::greater: return 11;
359 case tok::exclaimequal:
360 case tok::equalequal: return 10;
361 case tok::amp: return 9;
362 case tok::caret: return 8;
363 case tok::pipe: return 7;
364 case tok::ampamp: return 6;
365 case tok::pipepipe: return 5;
Chris Lattner98ed49f2008-05-05 20:07:41 +0000366 case tok::question: return 4;
367 case tok::comma: return 3;
Chris Lattner91891562008-05-04 18:36:18 +0000368 case tok::colon: return 2;
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 case tok::r_paren: return 0; // Lowest priority, end of expr.
370 case tok::eom: return 0; // Lowest priority, end of macro.
371 }
372}
373
374
375/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner8ed30442008-05-05 06:45:50 +0000376/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Reid Spencer5f016e22007-07-11 17:01:13 +0000377///
378/// If ValueLive is false, then this value is being evaluated in a context where
379/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner8ed30442008-05-05 06:45:50 +0000380/// evaluation, such as division by zero warnings.
381static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +0000382 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 Preprocessor &PP) {
384 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
385 // If this token isn't valid, report the error.
386 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000387 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
388 << LHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 return true;
390 }
391
392 while (1) {
393 // If this token has a lower precedence than we are allowed to parse, return
394 // it so that higher levels of the recursion can parse it.
395 if (PeekPrec < MinPrec)
396 return false;
397
398 tok::TokenKind Operator = PeekTok.getKind();
399
400 // If this is a short-circuiting operator, see if the RHS of the operator is
401 // dead. Note that this cannot just clobber ValueLive. Consider
402 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
403 // this example, the RHS of the && being dead does not make the rest of the
404 // expr dead.
405 bool RHSIsLive;
Chris Lattner8ed30442008-05-05 06:45:50 +0000406 if (Operator == tok::ampamp && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000407 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000408 else if (Operator == tok::pipepipe && LHS.Val != 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000409 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000410 else if (Operator == tok::question && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000411 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
412 else
413 RHSIsLive = ValueLive;
414
Chris Lattner8ed30442008-05-05 06:45:50 +0000415 // Consume the operator, remembering the operator's location for reporting.
416 SourceLocation OpLoc = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 PP.LexNonComment(PeekTok);
418
Chris Lattner8ed30442008-05-05 06:45:50 +0000419 PPValue RHS(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000420 // Parse the RHS of the operator.
421 DefinedTracker DT;
422 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
423
424 // Remember the precedence of this operator and get the precedence of the
425 // operator immediately to the right of the RHS.
426 unsigned ThisPrec = PeekPrec;
427 PeekPrec = getPrecedence(PeekTok.getKind());
428
429 // If this token isn't valid, report the error.
430 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000431 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
432 << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000433 return true;
434 }
435
Chris Lattner98ed49f2008-05-05 20:07:41 +0000436 // Decide whether to include the next binop in this subexpression. For
437 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattner44cbbb02008-05-05 20:09:27 +0000438 // handle y*z as a single subexpression. We do this because the precedence
439 // of * is higher than that of +. The only strange case we have to handle
440 // here is for the ?: operator, where the precedence is actually lower than
441 // the LHS of the '?'. The grammar rule is:
Chris Lattner98ed49f2008-05-05 20:07:41 +0000442 //
443 // conditional-expression ::=
444 // logical-OR-expression ? expression : conditional-expression
445 // where 'expression' is actually comma-expression.
446 unsigned RHSPrec;
447 if (Operator == tok::question)
448 // The RHS of "?" should be maximally consumed as an expression.
449 RHSPrec = getPrecedence(tok::comma);
450 else // All others should munch while higher precedence.
451 RHSPrec = ThisPrec+1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000452
Chris Lattner98ed49f2008-05-05 20:07:41 +0000453 if (PeekPrec >= RHSPrec) {
454 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Reid Spencer5f016e22007-07-11 17:01:13 +0000455 return true;
456 PeekPrec = getPrecedence(PeekTok.getKind());
457 }
458 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
459
460 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Chris Lattner019ef7e2008-05-04 23:46:17 +0000461 // either operand is unsigned.
Reid Spencer5f016e22007-07-11 17:01:13 +0000462 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattner019ef7e2008-05-04 23:46:17 +0000463 switch (Operator) {
464 case tok::question: // No UAC for x and y in "x ? y : z".
465 case tok::lessless: // Shift amount doesn't UAC with shift value.
466 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
467 case tok::comma: // Comma operands are not subject to UACs.
468 case tok::pipepipe: // Logical || does not do UACs.
469 case tok::ampamp: // Logical && does not do UACs.
470 break; // No UAC
471 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000472 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
473 // If this just promoted something from signed to unsigned, and if the
474 // value was negative, warn about it.
475 if (ValueLive && Res.isUnsigned()) {
Chris Lattner8ed30442008-05-05 06:45:50 +0000476 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000477 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
478 << LHS.Val.toString(10, true) + " to " +
479 LHS.Val.toString(10, false)
480 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000481 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000482 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
483 << RHS.Val.toString(10, true) + " to " +
484 RHS.Val.toString(10, false)
485 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000486 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000487 LHS.Val.setIsUnsigned(Res.isUnsigned());
488 RHS.Val.setIsUnsigned(Res.isUnsigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000489 }
490
491 // FIXME: All of these should detect and report overflow??
492 bool Overflow = false;
493 switch (Operator) {
494 default: assert(0 && "Unknown operator token!");
495 case tok::percent:
Chris Lattner8ed30442008-05-05 06:45:50 +0000496 if (RHS.Val != 0)
497 Res = LHS.Val % RHS.Val;
498 else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000499 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
500 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000501 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000502 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000503 break;
504 case tok::slash:
Chris Lattner8ed30442008-05-05 06:45:50 +0000505 if (RHS.Val != 0) {
506 Res = LHS.Val / RHS.Val;
507 if (LHS.Val.isSigned()) // MININT/-1 --> overflow.
508 Overflow = LHS.Val.isMinSignedValue() && RHS.Val.isAllOnesValue();
509 } else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000510 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
511 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000512 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000513 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000514 break;
Chris Lattner3b691152008-05-04 07:15:21 +0000515
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 case tok::star:
Chris Lattner8ed30442008-05-05 06:45:50 +0000517 Res = LHS.Val * RHS.Val;
Eli Friedman3265a422009-05-16 21:24:10 +0000518 if (Res.isSigned() && LHS.Val != 0 && RHS.Val != 0)
Chris Lattner8ed30442008-05-05 06:45:50 +0000519 Overflow = Res/RHS.Val != LHS.Val || Res/LHS.Val != RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 break;
521 case tok::lessless: {
522 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000523 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
524 if (ShAmt >= LHS.Val.getBitWidth())
525 Overflow = true, ShAmt = LHS.Val.getBitWidth()-1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000526 else if (LHS.isUnsigned())
Eli Friedman3265a422009-05-16 21:24:10 +0000527 Overflow = false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000528 else if (LHS.Val.isNonNegative()) // Don't allow sign change.
529 Overflow = ShAmt >= LHS.Val.countLeadingZeros();
Reid Spencer5f016e22007-07-11 17:01:13 +0000530 else
Chris Lattner8ed30442008-05-05 06:45:50 +0000531 Overflow = ShAmt >= LHS.Val.countLeadingOnes();
Reid Spencer5f016e22007-07-11 17:01:13 +0000532
Chris Lattner8ed30442008-05-05 06:45:50 +0000533 Res = LHS.Val << ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000534 break;
535 }
536 case tok::greatergreater: {
537 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000538 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Reid Spencer5f016e22007-07-11 17:01:13 +0000539 if (ShAmt >= LHS.getBitWidth())
540 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner8ed30442008-05-05 06:45:50 +0000541 Res = LHS.Val >> ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 break;
543 }
544 case tok::plus:
Chris Lattner8ed30442008-05-05 06:45:50 +0000545 Res = LHS.Val + RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 if (LHS.isUnsigned())
Eli Friedman3265a422009-05-16 21:24:10 +0000547 Overflow = false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000548 else if (LHS.Val.isNonNegative() == RHS.Val.isNonNegative() &&
549 Res.isNonNegative() != LHS.Val.isNonNegative())
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 Overflow = true; // Overflow for signed addition.
551 break;
552 case tok::minus:
Chris Lattner8ed30442008-05-05 06:45:50 +0000553 Res = LHS.Val - RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 if (LHS.isUnsigned())
Eli Friedman3265a422009-05-16 21:24:10 +0000555 Overflow = false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000556 else if (LHS.Val.isNonNegative() != RHS.Val.isNonNegative() &&
557 Res.isNonNegative() != LHS.Val.isNonNegative())
Reid Spencer5f016e22007-07-11 17:01:13 +0000558 Overflow = true; // Overflow for signed subtraction.
559 break;
560 case tok::lessequal:
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::less:
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::greaterequal:
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::greater:
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.8p6, result is always int (signed)
575 break;
576 case tok::exclaimequal:
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::equalequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000581 Res = LHS.Val == RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000582 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
583 break;
584 case tok::amp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000585 Res = LHS.Val & RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 break;
587 case tok::caret:
Chris Lattner8ed30442008-05-05 06:45:50 +0000588 Res = LHS.Val ^ RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 break;
590 case tok::pipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000591 Res = LHS.Val | RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 break;
593 case tok::ampamp:
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.13p3, result is always int (signed)
596 break;
597 case tok::pipepipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000598 Res = (LHS.Val != 0 || RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
600 break;
601 case tok::comma:
Chris Lattner91891562008-05-04 18:36:18 +0000602 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
603 // if not being evaluated.
604 if (!PP.getLangOptions().C99 || ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000605 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
606 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000607 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Reid Spencer5f016e22007-07-11 17:01:13 +0000608 break;
609 case tok::question: {
610 // Parse the : part of the expression.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000611 if (PeekTok.isNot(tok::colon)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000612 PP.Diag(PeekTok.getLocation(), diag::err_expected_colon)
613 << LHS.getRange(), RHS.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000614 PP.Diag(OpLoc, diag::note_matching) << "?";
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 return true;
616 }
617 // Consume the :.
618 PP.LexNonComment(PeekTok);
619
620 // Evaluate the value after the :.
Chris Lattner8ed30442008-05-05 06:45:50 +0000621 bool AfterColonLive = ValueLive && LHS.Val == 0;
622 PPValue AfterColonVal(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 DefinedTracker DT;
624 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
625 return true;
626
Chris Lattner44cbbb02008-05-05 20:09:27 +0000627 // Parse anything after the : with the same precedence as ?. We allow
628 // things of equal precedence because ?: is right associative.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000629 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 PeekTok, AfterColonLive, PP))
631 return true;
632
633 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner8ed30442008-05-05 06:45:50 +0000634 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
635 RHS.setEnd(AfterColonVal.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000636
637 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
638 // either operand is unsigned.
639 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
640
641 // Figure out the precedence of the token after the : part.
642 PeekPrec = getPrecedence(PeekTok.getKind());
643 break;
644 }
645 case tok::colon:
646 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattner204b2fe2008-11-18 21:48:13 +0000647 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
648 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 return true;
650 }
651
652 // If this operator is live and overflowed, report the issue.
653 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000654 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
655 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000656
657 // Put the result back into 'LHS' for our next iteration.
Chris Lattner8ed30442008-05-05 06:45:50 +0000658 LHS.Val = Res;
659 LHS.setEnd(RHS.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 }
661
662 return false;
663}
664
665/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
666/// may occur after a #if or #elif directive. If the expression is equivalent
667/// to "!defined(X)" return X in IfNDefMacro.
668bool Preprocessor::
669EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
670 // Peek ahead one token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000671 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 Lex(Tok);
673
674 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner98be4942008-03-05 18:54:05 +0000675 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000676
Chris Lattner8ed30442008-05-05 06:45:50 +0000677 PPValue ResVal(BitWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000678 DefinedTracker DT;
679 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
680 // Parse error, skip the rest of the macro line.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000681 if (Tok.isNot(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 DiscardUntilEndOfDirective();
683 return false;
684 }
685
686 // If we are at the end of the expression after just parsing a value, there
687 // must be no (unparenthesized) binary operators involved, so we can exit
688 // directly.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000689 if (Tok.is(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 // If the expression we parsed was of the form !defined(macro), return the
691 // macro in IfNDefMacro.
692 if (DT.State == DefinedTracker::NotDefinedMacro)
693 IfNDefMacro = DT.TheMacro;
694
Chris Lattner8ed30442008-05-05 06:45:50 +0000695 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 }
697
698 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
699 // operator and the stuff after it.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000700 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
701 Tok, true, *this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 // Parse error, skip the rest of the macro line.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000703 if (Tok.isNot(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 DiscardUntilEndOfDirective();
705 return false;
706 }
707
708 // If we aren't at the tok::eom token, something bad happened, like an extra
709 // ')' token.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000710 if (Tok.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000711 Diag(Tok, diag::err_pp_expected_eol);
712 DiscardUntilEndOfDirective();
713 }
714
Chris Lattner8ed30442008-05-05 06:45:50 +0000715 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000716}
717