blob: a012d8fe7b8d02d6edb8e9b604e99fb06d651d17 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner80965422006-07-04 18:03:19 +000010// This file implements the Preprocessor::EvaluateDirectiveExpression method,
11// which parses and evaluates integer constant expressions for #if directives.
Chris Lattner22eb9722006-06-18 05:43:12 +000012//
13//===----------------------------------------------------------------------===//
14//
Chris Lattner6df79752007-04-04 06:54:19 +000015// FIXME: implement testing for #assert's.
Chris Lattnera9eac7f2007-04-05 05:24:00 +000016// FIXME: Detect and report overflow in expression (e.g. (1 << 62)*2)
Chris Lattner22eb9722006-06-18 05:43:12 +000017//
18//===----------------------------------------------------------------------===//
19
20#include "clang/Lex/Preprocessor.h"
Chris Lattnera78a97e2006-07-03 05:42:18 +000021#include "clang/Lex/MacroInfo.h"
Steve Naroff451d8f162007-03-12 23:22:38 +000022#include "clang/Lex/LiteralSupport.h"
Chris Lattner81278c62006-10-14 19:03:49 +000023#include "clang/Basic/TargetInfo.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000024#include "clang/Basic/TokenKinds.h"
25#include "clang/Basic/Diagnostic.h"
Chris Lattnera9eac7f2007-04-05 05:24:00 +000026#include "llvm/ADT/APSInt.h"
Steve Naroff451d8f162007-03-12 23:22:38 +000027#include "llvm/ADT/SmallString.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000028using namespace llvm;
29using namespace clang;
30
Chris Lattnera9eac7f2007-04-05 05:24:00 +000031static bool EvaluateDirectiveSubExpr(APSInt &LHS, unsigned MinPrec,
Chris Lattner86054a92007-04-10 05:26:38 +000032 LexerToken &PeekTok, bool ValueLive,
33 Preprocessor &PP);
Chris Lattner22eb9722006-06-18 05:43:12 +000034
Chris Lattnerb9d90f72006-07-04 18:32:03 +000035/// DefinedTracker - This struct is used while parsing expressions to keep track
36/// of whether !defined(X) has been seen.
37///
38/// With this simple scheme, we handle the basic forms:
39/// !defined(X) and !defined X
40/// but we also trivially handle (silly) stuff like:
41/// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
42struct DefinedTracker {
43 /// Each time a Value is evaluated, it returns information about whether the
44 /// parsed value is of the form defined(X), !defined(X) or is something else.
45 enum TrackerState {
46 DefinedMacro, // defined(X)
47 NotDefinedMacro, // !defined(X)
48 Unknown // Something else.
49 } State;
50 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
51 /// indicates the macro that was checked.
52 IdentifierInfo *TheMacro;
53};
54
55
Chris Lattner22eb9722006-06-18 05:43:12 +000056
57/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
58/// return the computed value in Result. Return true if there was an error
Chris Lattnerb9d90f72006-07-04 18:32:03 +000059/// parsing. This function also returns information about the form of the
60/// expression in DT. See above for information on what DT means.
Chris Lattner86054a92007-04-10 05:26:38 +000061///
62/// If ValueLive is false, then this value is being evaluated in a context where
63/// the result is not used. As such, avoid diagnostics that relate to
64/// evaluation.
Chris Lattnera9eac7f2007-04-05 05:24:00 +000065static bool EvaluateValue(APSInt &Result, LexerToken &PeekTok,
Chris Lattner86054a92007-04-10 05:26:38 +000066 DefinedTracker &DT, bool ValueLive,
67 Preprocessor &PP) {
Chris Lattner22eb9722006-06-18 05:43:12 +000068 Result = 0;
Chris Lattnerb9d90f72006-07-04 18:32:03 +000069 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +000070
71 // If this token's spelling is a pp-identifier, check to see if it is
72 // 'defined' or if it is a macro. Note that we check here because many
73 // keywords are pp-identifiers, so we can't check the kind.
Chris Lattnerb9d90f72006-07-04 18:32:03 +000074 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
Chris Lattner22eb9722006-06-18 05:43:12 +000075 // If this identifier isn't 'defined' and it wasn't macro expanded, it turns
Chris Lattnera7fa1b22007-04-10 06:16:30 +000076 // into a simple 0, unless it is the C++ keyword "true", in which case it
77 // turns into "1".
Chris Lattner2bb8a952006-11-21 22:24:17 +000078 if (II->getPPKeywordID() != tok::pp_defined) {
Chris Lattnera7fa1b22007-04-10 06:16:30 +000079 Result = II->getTokenID() == tok::kw_true;
Chris Lattnera9eac7f2007-04-05 05:24:00 +000080 Result.setIsUnsigned(false); // "0" is signed intmax_t 0.
Chris Lattnerbcb416b2006-10-27 05:43:50 +000081 PP.LexNonComment(PeekTok);
Chris Lattnercb283342006-06-18 06:48:37 +000082 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +000083 }
84
85 // Handle "defined X" and "defined(X)".
Chris Lattner22eb9722006-06-18 05:43:12 +000086
Chris Lattnere3519cc2006-07-04 18:11:39 +000087 // Get the next token, don't expand it.
88 PP.LexUnexpandedToken(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +000089
90 // Two options, it can either be a pp-identifier or a (.
91 bool InParens = false;
92 if (PeekTok.getKind() == tok::l_paren) {
93 // Found a paren, remember we saw it and skip it.
94 InParens = true;
Chris Lattnere3519cc2006-07-04 18:11:39 +000095 PP.LexUnexpandedToken(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +000096 }
97
98 // If we don't have a pp-identifier now, this is an error.
99 if ((II = PeekTok.getIdentifierInfo()) == 0) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000100 PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000101 return true;
102 }
103
104 // Otherwise, we got an identifier, is it defined to something?
105 Result = II->getMacroInfo() != 0;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000106 Result.setIsUnsigned(false); // Result is signed intmax_t.
107
Chris Lattnera78a97e2006-07-03 05:42:18 +0000108 // If there is a macro, mark it used.
Chris Lattner86054a92007-04-10 05:26:38 +0000109 if (Result != 0 && ValueLive) {
Chris Lattner81278c62006-10-14 19:03:49 +0000110 II->getMacroInfo()->setIsUsed(true);
111
112 // If this is the first use of a target-specific macro, warn about it.
113 if (II->getMacroInfo()->isTargetSpecific()) {
114 // Don't warn on second use.
115 II->getMacroInfo()->setIsTargetSpecific(false);
116 PP.getTargetInfo().DiagnoseNonPortability(PeekTok.getLocation(),
117 diag::port_target_macro_use);
118 }
Chris Lattner86054a92007-04-10 05:26:38 +0000119 } else if (ValueLive) {
Chris Lattner063400e2006-10-14 19:54:15 +0000120 // Use of a target-specific macro for some other target? If so, warn.
121 if (II->isOtherTargetMacro()) {
122 II->setIsOtherTargetMacro(false); // Don't warn on second use.
123 PP.getTargetInfo().DiagnoseNonPortability(PeekTok.getLocation(),
124 diag::port_target_macro_use);
125 }
Chris Lattner81278c62006-10-14 19:03:49 +0000126 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000127
128 // Consume identifier.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000129 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000130
131 // If we are in parens, ensure we have a trailing ).
132 if (InParens) {
133 if (PeekTok.getKind() != tok::r_paren) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000134 PP.Diag(PeekTok, diag::err_pp_missing_rparen);
Chris Lattner22eb9722006-06-18 05:43:12 +0000135 return true;
136 }
137 // Consume the ).
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000138 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000139 }
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000140
141 // Success, remember that we saw defined(X).
142 DT.State = DefinedTracker::DefinedMacro;
143 DT.TheMacro = II;
Chris Lattner22eb9722006-06-18 05:43:12 +0000144 return false;
145 }
146
147 switch (PeekTok.getKind()) {
148 default: // Non-value token.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000149 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
Chris Lattner22eb9722006-06-18 05:43:12 +0000150 return true;
151 case tok::eom:
152 case tok::r_paren:
153 // If there is no expression, report and exit.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000154 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000155 return true;
156 case tok::numeric_constant: {
Chris Lattner6df79752007-04-04 06:54:19 +0000157 SmallString<64> IntegerBuffer;
Steve Naroff451d8f162007-03-12 23:22:38 +0000158 IntegerBuffer.resize(PeekTok.getLength());
159 const char *ThisTokBegin = &IntegerBuffer[0];
160 unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
161 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
162 PeekTok.getLocation(), PP);
Chris Lattner6df79752007-04-04 06:54:19 +0000163 if (Literal.hadError)
Steve Narofff2fb89e2007-03-13 20:29:44 +0000164 return true; // a diagnostic was already reported.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000165
166 if (Literal.isFloatingLiteral()) {
Steve Naroff451d8f162007-03-12 23:22:38 +0000167 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000168 return true;
Steve Naroff451d8f162007-03-12 23:22:38 +0000169 }
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000170 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000171
172 // Parse the integer literal into Result.
173 if (Literal.GetIntegerValue(Result)) {
174 // Overflow parsing integer literal.
Chris Lattner86054a92007-04-10 05:26:38 +0000175 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000176 Result.setIsUnsigned(true);
177 } else {
178 // Set the signedness of the result to match whether there was a U suffix
179 // or not.
180 Result.setIsUnsigned(Literal.isUnsigned);
181
182 // Detect overflow based on whether the value is signed. If signed
183 // and if the value is too large, emit a warning "integer constant is so
184 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
185 // is 64-bits.
186 if (!Literal.isUnsigned && Result.isNegative()) {
Chris Lattner86054a92007-04-10 05:26:38 +0000187 if (ValueLive)PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000188 Result.setIsUnsigned(true);
189 }
190 }
191
192 // Consume the token.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000193 PP.LexNonComment(PeekTok);
194 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000195 }
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000196 case tok::char_constant: { // 'x'
197 SmallString<32> CharBuffer;
198 CharBuffer.resize(PeekTok.getLength());
199 const char *ThisTokBegin = &CharBuffer[0];
200 unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
201 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
202 PeekTok.getLocation(), PP);
203 if (Literal.hadError())
204 return true; // A diagnostic was already emitted.
205
206 // Character literals are always int or wchar_t, expand to intmax_t.
207 TargetInfo &TI = PP.getTargetInfo();
208 unsigned NumBits;
209 if (Literal.isWide())
210 NumBits = TI.getWCharWidth(PeekTok.getLocation());
211 else
212 NumBits = TI.getCharWidth(PeekTok.getLocation());
213
214 // Set the width.
215 APSInt Val(NumBits);
216 // Set the value.
217 Val = Literal.getValue();
218 // Set the signedness.
219 Val.setIsUnsigned(!TI.isCharSigned(PeekTok.getLocation()));
220
221 if (Result.getBitWidth() > Val.getBitWidth()) {
222 if (Val.isSigned())
223 Result = Val.sext(Result.getBitWidth());
224 else
225 Result = Val.zext(Result.getBitWidth());
226 Result.setIsUnsigned(Val.isUnsigned());
227 } else {
228 assert(Result.getBitWidth() == Val.getBitWidth() &&
229 "intmax_t smaller than char/wchar_t?");
230 Result = Val;
231 }
232
233 // Consume the token.
234 PP.LexNonComment(PeekTok);
235 return false;
236 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000237 case tok::l_paren:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000238 PP.LexNonComment(PeekTok); // Eat the (.
Chris Lattnercb283342006-06-18 06:48:37 +0000239 // Parse the value and if there are any binary operators involved, parse
240 // them.
Chris Lattner86054a92007-04-10 05:26:38 +0000241 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000242
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000243 // If this is a silly value like (X), which doesn't need parens, check for
244 // !(defined X).
245 if (PeekTok.getKind() == tok::r_paren) {
246 // Just use DT unmodified as our result.
247 } else {
Chris Lattner86054a92007-04-10 05:26:38 +0000248 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
249 return true;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000250
251 if (PeekTok.getKind() != tok::r_paren) {
252 PP.Diag(PeekTok, diag::err_pp_expected_rparen);
253 return true;
254 }
255 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000256 }
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000257 PP.LexNonComment(PeekTok); // Eat the ).
Chris Lattner22eb9722006-06-18 05:43:12 +0000258 return false;
259
260 case tok::plus:
261 // Unary plus doesn't modify the value.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000262 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000263 return EvaluateValue(Result, PeekTok, DT, ValueLive, PP);
Chris Lattner7e61ac52007-04-11 03:42:36 +0000264 case tok::minus: {
265 SourceLocation Loc = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000266 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000267 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000268 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner22eb9722006-06-18 05:43:12 +0000269 Result = -Result;
Chris Lattner7e61ac52007-04-11 03:42:36 +0000270
271 bool Overflow = false;
272 if (Result.isUnsigned())
273 Overflow = !Result.isPositive();
274 else if (Result.isMinSignedValue())
275 Overflow = true; // -MININT is the only thing that overflows.
276
277 // If this operator is live and overflowed, report the issue.
278 if (Overflow && ValueLive)
279 PP.Diag(Loc, diag::warn_pp_expr_overflow);
280
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000281 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000282 return false;
Chris Lattner7e61ac52007-04-11 03:42:36 +0000283 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000284
285 case tok::tilde:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000286 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000287 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000288 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner22eb9722006-06-18 05:43:12 +0000289 Result = ~Result;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000290 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000291 return false;
292
293 case tok::exclaim:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000294 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000295 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000296 Result = !Result;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000297 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
298 Result.setIsUnsigned(false);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000299
300 if (DT.State == DefinedTracker::DefinedMacro)
301 DT.State = DefinedTracker::NotDefinedMacro;
302 else if (DT.State == DefinedTracker::NotDefinedMacro)
303 DT.State = DefinedTracker::DefinedMacro;
Chris Lattner22eb9722006-06-18 05:43:12 +0000304 return false;
305
306 // FIXME: Handle #assert
307 }
308}
309
310
311
312/// getPrecedence - Return the precedence of the specified binary operator
313/// token. This returns:
314/// ~0 - Invalid token.
Chris Lattner9916c5c2006-10-27 05:24:37 +0000315/// 14 - *,/,%
316/// 13 - -,+
317/// 12 - <<,>>
318/// 11 - >=, <=, >, <
319/// 10 - ==, !=
Chris Lattner22eb9722006-06-18 05:43:12 +0000320/// 9 - &
321/// 8 - ^
322/// 7 - |
323/// 6 - &&
324/// 5 - ||
325/// 4 - ?
326/// 3 - :
327/// 0 - eom, )
328static unsigned getPrecedence(tok::TokenKind Kind) {
329 switch (Kind) {
330 default: return ~0U;
331 case tok::percent:
332 case tok::slash:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000333 case tok::star: return 14;
Chris Lattner22eb9722006-06-18 05:43:12 +0000334 case tok::plus:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000335 case tok::minus: return 13;
Chris Lattner22eb9722006-06-18 05:43:12 +0000336 case tok::lessless:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000337 case tok::greatergreater: return 12;
Chris Lattner22eb9722006-06-18 05:43:12 +0000338 case tok::lessequal:
339 case tok::less:
340 case tok::greaterequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000341 case tok::greater: return 11;
Chris Lattner22eb9722006-06-18 05:43:12 +0000342 case tok::exclaimequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000343 case tok::equalequal: return 10;
Chris Lattner22eb9722006-06-18 05:43:12 +0000344 case tok::amp: return 9;
345 case tok::caret: return 8;
346 case tok::pipe: return 7;
347 case tok::ampamp: return 6;
348 case tok::pipepipe: return 5;
349 case tok::question: return 4;
350 case tok::colon: return 3;
351 case tok::comma: return 2;
352 case tok::r_paren: return 0; // Lowest priority, end of expr.
353 case tok::eom: return 0; // Lowest priority, end of macro.
354 }
355}
356
357
358/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
359/// PeekTok, and whose precedence is PeekPrec.
Chris Lattner86054a92007-04-10 05:26:38 +0000360///
361/// If ValueLive is false, then this value is being evaluated in a context where
362/// the result is not used. As such, avoid diagnostics that relate to
363/// evaluation.
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000364static bool EvaluateDirectiveSubExpr(APSInt &LHS, unsigned MinPrec,
Chris Lattner86054a92007-04-10 05:26:38 +0000365 LexerToken &PeekTok, bool ValueLive,
366 Preprocessor &PP) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000367 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
368 // If this token isn't valid, report the error.
369 if (PeekPrec == ~0U) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000370 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
Chris Lattner22eb9722006-06-18 05:43:12 +0000371 return true;
372 }
373
374 while (1) {
375 // If this token has a lower precedence than we are allowed to parse, return
376 // it so that higher levels of the recursion can parse it.
377 if (PeekPrec < MinPrec)
378 return false;
379
380 tok::TokenKind Operator = PeekTok.getKind();
Chris Lattner86054a92007-04-10 05:26:38 +0000381
382 // If this is a short-circuiting operator, see if the RHS of the operator is
383 // dead. Note that this cannot just clobber ValueLive. Consider
384 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
385 // this example, the RHS of the && being dead does not make the rest of the
386 // expr dead.
387 bool RHSIsLive;
388 if (Operator == tok::ampamp && LHS == 0)
389 RHSIsLive = false; // RHS of "0 && x" is dead.
390 else if (Operator == tok::pipepipe && LHS != 0)
391 RHSIsLive = false; // RHS of "1 || x" is dead.
392 else if (Operator == tok::question && LHS == 0)
393 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
394 else
395 RHSIsLive = ValueLive;
Chris Lattner22eb9722006-06-18 05:43:12 +0000396
397 // Consume the operator, saving the operator token for error reporting.
398 LexerToken OpToken = PeekTok;
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000399 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000400
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000401 APSInt RHS(LHS.getBitWidth());
Chris Lattner22eb9722006-06-18 05:43:12 +0000402 // Parse the RHS of the operator.
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000403 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000404 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000405
406 // Remember the precedence of this operator and get the precedence of the
407 // operator immediately to the right of the RHS.
408 unsigned ThisPrec = PeekPrec;
409 PeekPrec = getPrecedence(PeekTok.getKind());
410
411 // If this token isn't valid, report the error.
412 if (PeekPrec == ~0U) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000413 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
Chris Lattner22eb9722006-06-18 05:43:12 +0000414 return true;
415 }
416
417 bool isRightAssoc = Operator == tok::question;
418
419 // Get the precedence of the operator to the right of the RHS. If it binds
420 // more tightly with RHS than we do, evaluate it completely first.
421 if (ThisPrec < PeekPrec ||
422 (ThisPrec == PeekPrec && isRightAssoc)) {
Chris Lattner86054a92007-04-10 05:26:38 +0000423 if (EvaluateDirectiveSubExpr(RHS, ThisPrec+1, PeekTok, RHSIsLive, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000424 return true;
425 PeekPrec = getPrecedence(PeekTok.getKind());
426 }
427 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
428
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000429 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
430 // either operand is unsigned. Don't do this for x and y in "x ? y : z".
431 if (Operator != tok::question) {
432 if (RHS.isUnsigned()) LHS.setIsUnsigned(true);
433 RHS.setIsUnsigned(LHS.isUnsigned());
434 }
435
436 // FIXME: All of these should detect and report overflow??
Chris Lattner5a0f1642007-04-10 06:54:33 +0000437 bool Overflow = false;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000438 APSInt Res(LHS.getBitWidth());
Chris Lattner22eb9722006-06-18 05:43:12 +0000439 switch (Operator) {
440 default: assert(0 && "Unknown operator token!");
441 case tok::percent:
442 if (RHS == 0) {
Chris Lattner86054a92007-04-10 05:26:38 +0000443 if (ValueLive) PP.Diag(OpToken, diag::err_pp_remainder_by_zero);
Chris Lattner22eb9722006-06-18 05:43:12 +0000444 return true;
445 }
Chris Lattner9cc755d2007-04-10 07:07:11 +0000446 Res = LHS % RHS;
Chris Lattner22eb9722006-06-18 05:43:12 +0000447 break;
448 case tok::slash:
449 if (RHS == 0) {
Chris Lattner86054a92007-04-10 05:26:38 +0000450 if (ValueLive) PP.Diag(OpToken, diag::err_pp_division_by_zero);
Chris Lattner22eb9722006-06-18 05:43:12 +0000451 return true;
452 }
Chris Lattner9cc755d2007-04-10 07:07:11 +0000453 Res = LHS / RHS;
Chris Lattner22eb9722006-06-18 05:43:12 +0000454 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000455 case tok::star:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000456 Res = LHS * RHS;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000457 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000458 case tok::lessless: {
459 // Determine whether overflow is about to happen.
460 unsigned ShAmt = RHS.getLimitedValue();
461 if (ShAmt >= LHS.getBitWidth())
462 Overflow = true, ShAmt = LHS.getBitWidth()-1;
463 else if (LHS.isUnsigned())
464 Overflow = ShAmt > LHS.countLeadingZeros();
465 else if (LHS.isPositive())
466 Overflow = ShAmt >= LHS.countLeadingZeros(); // Don't allow sign change.
467 else
468 Overflow = ShAmt >= LHS.countLeadingOnes();
469
Chris Lattner9cc755d2007-04-10 07:07:11 +0000470 Res = LHS << ShAmt;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000471 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000472 }
473 case tok::greatergreater: {
474 // Determine whether overflow is about to happen.
475 unsigned ShAmt = RHS.getLimitedValue();
476 if (ShAmt >= LHS.getBitWidth())
477 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000478 Res = LHS >> ShAmt;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000479 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000480 }
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000481 case tok::plus:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000482 Res = LHS + RHS;
Chris Lattner028c7de2007-04-11 03:34:29 +0000483 if (LHS.isUnsigned())
484 Overflow = Res.ult(LHS);
485 else if (LHS.isPositive() == RHS.isPositive() &&
486 Res.isPositive() != LHS.isPositive())
487 Overflow = true; // Overflow for signed addition.
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000488 break;
489 case tok::minus:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000490 Res = LHS - RHS;
Chris Lattner028c7de2007-04-11 03:34:29 +0000491 if (LHS.isUnsigned())
492 Overflow = Res.ugt(LHS);
493 else if (LHS.isPositive() != RHS.isPositive() &&
494 Res.isPositive() != LHS.isPositive())
495 Overflow = true; // Overflow for signed subtraction.
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000496 break;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000497 case tok::lessequal:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000498 Res = LHS <= RHS;
499 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000500 break;
501 case tok::less:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000502 Res = LHS < RHS;
503 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000504 break;
505 case tok::greaterequal:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000506 Res = LHS >= RHS;
507 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000508 break;
509 case tok::greater:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000510 Res = LHS > RHS;
511 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000512 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000513 case tok::exclaimequal:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000514 Res = LHS != RHS;
515 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000516 break;
517 case tok::equalequal:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000518 Res = LHS == RHS;
519 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000520 break;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000521 case tok::amp:
522 Res = LHS & RHS;
523 break;
524 case tok::caret:
525 Res = LHS ^ RHS;
526 break;
527 case tok::pipe:
528 Res = LHS | RHS;
529 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000530 case tok::ampamp:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000531 Res = (LHS != 0 && RHS != 0);
532 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000533 break;
534 case tok::pipepipe:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000535 Res = (LHS != 0 || RHS != 0);
536 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000537 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000538 case tok::comma:
Chris Lattnere3519cc2006-07-04 18:11:39 +0000539 PP.Diag(OpToken, diag::ext_pp_comma_expr);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000540 Res = RHS; // LHS = LHS,RHS -> RHS.
Chris Lattner22eb9722006-06-18 05:43:12 +0000541 break;
542 case tok::question: {
543 // Parse the : part of the expression.
544 if (PeekTok.getKind() != tok::colon) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000545 PP.Diag(OpToken, diag::err_pp_question_without_colon);
Chris Lattner22eb9722006-06-18 05:43:12 +0000546 return true;
547 }
548 // Consume the :.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000549 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000550
551 // Evaluate the value after the :.
Chris Lattner86054a92007-04-10 05:26:38 +0000552 bool AfterColonLive = ValueLive && LHS == 0;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000553 APSInt AfterColonVal(LHS.getBitWidth());
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000554 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000555 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
556 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000557
558 // Parse anything after the : RHS that has a higher precedence than ?.
559 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec+1,
Chris Lattner86054a92007-04-10 05:26:38 +0000560 PeekTok, AfterColonLive, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000561 return true;
562
563 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner9cc755d2007-04-10 07:07:11 +0000564 Res = LHS != 0 ? RHS : AfterColonVal;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000565
566 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
567 // either operand is unsigned.
Chris Lattner9cc755d2007-04-10 07:07:11 +0000568 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Chris Lattner22eb9722006-06-18 05:43:12 +0000569
570 // Figure out the precedence of the token after the : part.
571 PeekPrec = getPrecedence(PeekTok.getKind());
572 break;
573 }
574 case tok::colon:
575 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000576 PP.Diag(OpToken, diag::err_pp_colon_without_question);
Chris Lattner22eb9722006-06-18 05:43:12 +0000577 return true;
578 }
Chris Lattner5a0f1642007-04-10 06:54:33 +0000579
580 // If this operator is live and overflowed, report the issue.
581 if (Overflow && ValueLive)
582 PP.Diag(OpToken, diag::warn_pp_expr_overflow);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000583
584 // Put the result back into 'LHS' for our next iteration.
585 LHS = Res;
Chris Lattner22eb9722006-06-18 05:43:12 +0000586 }
587
588 return false;
589}
Chris Lattnere3519cc2006-07-04 18:11:39 +0000590
591/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
592/// may occur after a #if or #elif directive. If the expression is equivalent
593/// to "!defined(X)" return X in IfNDefMacro.
594bool Preprocessor::
595EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
596 // Peek ahead one token.
597 LexerToken Tok;
598 Lex(Tok);
599
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000600 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
601 unsigned BitWidth = getTargetInfo().getIntMaxTWidth(Tok.getLocation());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000602 APSInt ResVal(BitWidth);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000603 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000604 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000605 // Parse error, skip the rest of the macro line.
606 if (Tok.getKind() != tok::eom)
607 DiscardUntilEndOfDirective();
608 return false;
609 }
610
611 // If we are at the end of the expression after just parsing a value, there
612 // must be no (unparenthesized) binary operators involved, so we can exit
613 // directly.
614 if (Tok.getKind() == tok::eom) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000615 // If the expression we parsed was of the form !defined(macro), return the
616 // macro in IfNDefMacro.
617 if (DT.State == DefinedTracker::NotDefinedMacro)
618 IfNDefMacro = DT.TheMacro;
619
Chris Lattnere3519cc2006-07-04 18:11:39 +0000620 return ResVal != 0;
621 }
622
623 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
624 // operator and the stuff after it.
Chris Lattner86054a92007-04-10 05:26:38 +0000625 if (EvaluateDirectiveSubExpr(ResVal, 1, Tok, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000626 // Parse error, skip the rest of the macro line.
627 if (Tok.getKind() != tok::eom)
628 DiscardUntilEndOfDirective();
629 return false;
630 }
631
632 // If we aren't at the tok::eom token, something bad happened, like an extra
633 // ')' token.
634 if (Tok.getKind() != tok::eom) {
635 Diag(Tok, diag::err_pp_expected_eol);
636 DiscardUntilEndOfDirective();
637 }
638
639 return ResVal != 0;
640}
641