blob: ec20eb14c8e93a3e7b95a931561400d2863b9554 [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 Lattner22eb9722006-06-18 05:43:12 +000016//
17//===----------------------------------------------------------------------===//
18
19#include "clang/Lex/Preprocessor.h"
Chris Lattnera78a97e2006-07-03 05:42:18 +000020#include "clang/Lex/MacroInfo.h"
Steve Naroff451d8f162007-03-12 23:22:38 +000021#include "clang/Lex/LiteralSupport.h"
Chris Lattner81278c62006-10-14 19:03:49 +000022#include "clang/Basic/TargetInfo.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000023#include "clang/Basic/TokenKinds.h"
24#include "clang/Basic/Diagnostic.h"
Chris Lattnera9eac7f2007-04-05 05:24:00 +000025#include "llvm/ADT/APSInt.h"
Steve Naroff451d8f162007-03-12 23:22:38 +000026#include "llvm/ADT/SmallString.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000027using namespace clang;
28
Chris Lattner23b7eb62007-06-15 23:05:46 +000029static bool EvaluateDirectiveSubExpr(llvm::APSInt &LHS, unsigned MinPrec,
Chris Lattner146762e2007-07-20 16:59:19 +000030 Token &PeekTok, bool ValueLive,
Chris Lattner86054a92007-04-10 05:26:38 +000031 Preprocessor &PP);
Chris Lattner22eb9722006-06-18 05:43:12 +000032
Chris Lattnerb9d90f72006-07-04 18:32:03 +000033/// DefinedTracker - This struct is used while parsing expressions to keep track
34/// of whether !defined(X) has been seen.
35///
36/// With this simple scheme, we handle the basic forms:
37/// !defined(X) and !defined X
38/// but we also trivially handle (silly) stuff like:
39/// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
40struct DefinedTracker {
41 /// Each time a Value is evaluated, it returns information about whether the
42 /// parsed value is of the form defined(X), !defined(X) or is something else.
43 enum TrackerState {
44 DefinedMacro, // defined(X)
45 NotDefinedMacro, // !defined(X)
46 Unknown // Something else.
47 } State;
48 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
49 /// indicates the macro that was checked.
50 IdentifierInfo *TheMacro;
51};
52
53
Chris Lattner22eb9722006-06-18 05:43:12 +000054
55/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
56/// return the computed value in Result. Return true if there was an error
Chris Lattnerb9d90f72006-07-04 18:32:03 +000057/// parsing. This function also returns information about the form of the
58/// expression in DT. See above for information on what DT means.
Chris Lattner86054a92007-04-10 05:26:38 +000059///
60/// If ValueLive is false, then this value is being evaluated in a context where
61/// the result is not used. As such, avoid diagnostics that relate to
62/// evaluation.
Chris Lattner146762e2007-07-20 16:59:19 +000063static bool EvaluateValue(llvm::APSInt &Result, Token &PeekTok,
Chris Lattner86054a92007-04-10 05:26:38 +000064 DefinedTracker &DT, bool ValueLive,
65 Preprocessor &PP) {
Chris Lattner22eb9722006-06-18 05:43:12 +000066 Result = 0;
Chris Lattnerb9d90f72006-07-04 18:32:03 +000067 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +000068
69 // If this token's spelling is a pp-identifier, check to see if it is
70 // 'defined' or if it is a macro. Note that we check here because many
71 // keywords are pp-identifiers, so we can't check the kind.
Chris Lattnerb9d90f72006-07-04 18:32:03 +000072 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
Chris Lattner22eb9722006-06-18 05:43:12 +000073 // If this identifier isn't 'defined' and it wasn't macro expanded, it turns
Chris Lattnera7fa1b22007-04-10 06:16:30 +000074 // into a simple 0, unless it is the C++ keyword "true", in which case it
75 // turns into "1".
Chris Lattner2bb8a952006-11-21 22:24:17 +000076 if (II->getPPKeywordID() != tok::pp_defined) {
Chris Lattnera7fa1b22007-04-10 06:16:30 +000077 Result = II->getTokenID() == tok::kw_true;
Chris Lattnera9eac7f2007-04-05 05:24:00 +000078 Result.setIsUnsigned(false); // "0" is signed intmax_t 0.
Chris Lattnerbcb416b2006-10-27 05:43:50 +000079 PP.LexNonComment(PeekTok);
Chris Lattnercb283342006-06-18 06:48:37 +000080 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +000081 }
82
83 // Handle "defined X" and "defined(X)".
Chris Lattner22eb9722006-06-18 05:43:12 +000084
Chris Lattnere3519cc2006-07-04 18:11:39 +000085 // Get the next token, don't expand it.
86 PP.LexUnexpandedToken(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +000087
88 // Two options, it can either be a pp-identifier or a (.
89 bool InParens = false;
Chris Lattner98c1f7c2007-10-09 18:02:16 +000090 if (PeekTok.is(tok::l_paren)) {
Chris Lattner22eb9722006-06-18 05:43:12 +000091 // Found a paren, remember we saw it and skip it.
92 InParens = true;
Chris Lattnere3519cc2006-07-04 18:11:39 +000093 PP.LexUnexpandedToken(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +000094 }
95
96 // If we don't have a pp-identifier now, this is an error.
97 if ((II = PeekTok.getIdentifierInfo()) == 0) {
Chris Lattnere3519cc2006-07-04 18:11:39 +000098 PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +000099 return true;
100 }
101
102 // Otherwise, we got an identifier, is it defined to something?
Chris Lattner259716a2007-10-07 08:04:56 +0000103 Result = II->hasMacroDefinition();
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000104 Result.setIsUnsigned(false); // Result is signed intmax_t.
105
Chris Lattnera78a97e2006-07-03 05:42:18 +0000106 // If there is a macro, mark it used.
Chris Lattner86054a92007-04-10 05:26:38 +0000107 if (Result != 0 && ValueLive) {
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000108 MacroInfo *Macro = PP.getMacroInfo(II);
Chris Lattner259716a2007-10-07 08:04:56 +0000109 Macro->setIsUsed(true);
Chris Lattner81278c62006-10-14 19:03:49 +0000110
111 // If this is the first use of a target-specific macro, warn about it.
Chris Lattner259716a2007-10-07 08:04:56 +0000112 if (Macro->isTargetSpecific()) {
Chris Lattner81278c62006-10-14 19:03:49 +0000113 // Don't warn on second use.
Chris Lattner259716a2007-10-07 08:04:56 +0000114 Macro->setIsTargetSpecific(false);
Chris Lattner81278c62006-10-14 19:03:49 +0000115 PP.getTargetInfo().DiagnoseNonPortability(PeekTok.getLocation(),
116 diag::port_target_macro_use);
117 }
Chris Lattner86054a92007-04-10 05:26:38 +0000118 } else if (ValueLive) {
Chris Lattner063400e2006-10-14 19:54:15 +0000119 // Use of a target-specific macro for some other target? If so, warn.
120 if (II->isOtherTargetMacro()) {
121 II->setIsOtherTargetMacro(false); // Don't warn on second use.
122 PP.getTargetInfo().DiagnoseNonPortability(PeekTok.getLocation(),
123 diag::port_target_macro_use);
124 }
Chris Lattner81278c62006-10-14 19:03:49 +0000125 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000126
127 // Consume identifier.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000128 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000129
130 // If we are in parens, ensure we have a trailing ).
131 if (InParens) {
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000132 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000133 PP.Diag(PeekTok, diag::err_pp_missing_rparen);
Chris Lattner22eb9722006-06-18 05:43:12 +0000134 return true;
135 }
136 // Consume the ).
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000137 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000138 }
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000139
140 // Success, remember that we saw defined(X).
141 DT.State = DefinedTracker::DefinedMacro;
142 DT.TheMacro = II;
Chris Lattner22eb9722006-06-18 05:43:12 +0000143 return false;
144 }
145
146 switch (PeekTok.getKind()) {
147 default: // Non-value token.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000148 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
Chris Lattner22eb9722006-06-18 05:43:12 +0000149 return true;
150 case tok::eom:
151 case tok::r_paren:
152 // If there is no expression, report and exit.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000153 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000154 return true;
155 case tok::numeric_constant: {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000156 llvm::SmallString<64> IntegerBuffer;
Steve Naroff451d8f162007-03-12 23:22:38 +0000157 IntegerBuffer.resize(PeekTok.getLength());
158 const char *ThisTokBegin = &IntegerBuffer[0];
159 unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
160 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
161 PeekTok.getLocation(), PP);
Chris Lattner6df79752007-04-04 06:54:19 +0000162 if (Literal.hadError)
Steve Narofff2fb89e2007-03-13 20:29:44 +0000163 return true; // a diagnostic was already reported.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000164
Chris Lattnered045422007-08-26 03:29:23 +0000165 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Steve Naroff451d8f162007-03-12 23:22:38 +0000166 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000167 return true;
Steve Naroff451d8f162007-03-12 23:22:38 +0000168 }
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000169 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000170
Neil Boothac582c52007-08-29 22:00:19 +0000171 // long long is a C99 feature.
172 if (!PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus0x
Neil Booth4a1ee052007-08-29 22:13:52 +0000173 && Literal.isLongLong)
Neil Boothac582c52007-08-29 22:00:19 +0000174 PP.Diag(PeekTok, diag::ext_longlong);
175
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000176 // Parse the integer literal into Result.
177 if (Literal.GetIntegerValue(Result)) {
178 // Overflow parsing integer literal.
Chris Lattner86054a92007-04-10 05:26:38 +0000179 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000180 Result.setIsUnsigned(true);
181 } else {
182 // Set the signedness of the result to match whether there was a U suffix
183 // or not.
184 Result.setIsUnsigned(Literal.isUnsigned);
185
186 // Detect overflow based on whether the value is signed. If signed
187 // and if the value is too large, emit a warning "integer constant is so
188 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
189 // is 64-bits.
190 if (!Literal.isUnsigned && Result.isNegative()) {
Chris Lattner86054a92007-04-10 05:26:38 +0000191 if (ValueLive)PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000192 Result.setIsUnsigned(true);
193 }
194 }
195
196 // Consume the token.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000197 PP.LexNonComment(PeekTok);
198 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000199 }
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000200 case tok::char_constant: { // 'x'
Chris Lattner23b7eb62007-06-15 23:05:46 +0000201 llvm::SmallString<32> CharBuffer;
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000202 CharBuffer.resize(PeekTok.getLength());
203 const char *ThisTokBegin = &CharBuffer[0];
204 unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
205 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
206 PeekTok.getLocation(), PP);
207 if (Literal.hadError())
208 return true; // A diagnostic was already emitted.
209
210 // Character literals are always int or wchar_t, expand to intmax_t.
211 TargetInfo &TI = PP.getTargetInfo();
212 unsigned NumBits;
213 if (Literal.isWide())
214 NumBits = TI.getWCharWidth(PeekTok.getLocation());
215 else
216 NumBits = TI.getCharWidth(PeekTok.getLocation());
217
218 // Set the width.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000219 llvm::APSInt Val(NumBits);
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000220 // Set the value.
221 Val = Literal.getValue();
222 // Set the signedness.
223 Val.setIsUnsigned(!TI.isCharSigned(PeekTok.getLocation()));
224
225 if (Result.getBitWidth() > Val.getBitWidth()) {
226 if (Val.isSigned())
227 Result = Val.sext(Result.getBitWidth());
228 else
229 Result = Val.zext(Result.getBitWidth());
230 Result.setIsUnsigned(Val.isUnsigned());
231 } else {
232 assert(Result.getBitWidth() == Val.getBitWidth() &&
233 "intmax_t smaller than char/wchar_t?");
234 Result = Val;
235 }
236
237 // Consume the token.
238 PP.LexNonComment(PeekTok);
239 return false;
240 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000241 case tok::l_paren:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000242 PP.LexNonComment(PeekTok); // Eat the (.
Chris Lattnercb283342006-06-18 06:48:37 +0000243 // Parse the value and if there are any binary operators involved, parse
244 // them.
Chris Lattner86054a92007-04-10 05:26:38 +0000245 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000246
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000247 // If this is a silly value like (X), which doesn't need parens, check for
248 // !(defined X).
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000249 if (PeekTok.is(tok::r_paren)) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000250 // Just use DT unmodified as our result.
251 } else {
Chris Lattner86054a92007-04-10 05:26:38 +0000252 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
253 return true;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000254
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000255 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000256 PP.Diag(PeekTok, diag::err_pp_expected_rparen);
257 return true;
258 }
259 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000260 }
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000261 PP.LexNonComment(PeekTok); // Eat the ).
Chris Lattner22eb9722006-06-18 05:43:12 +0000262 return false;
263
264 case tok::plus:
265 // Unary plus doesn't modify the value.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000266 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000267 return EvaluateValue(Result, PeekTok, DT, ValueLive, PP);
Chris Lattner7e61ac52007-04-11 03:42:36 +0000268 case tok::minus: {
269 SourceLocation Loc = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000270 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000271 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000272 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner22eb9722006-06-18 05:43:12 +0000273 Result = -Result;
Chris Lattner7e61ac52007-04-11 03:42:36 +0000274
275 bool Overflow = false;
276 if (Result.isUnsigned())
277 Overflow = !Result.isPositive();
278 else if (Result.isMinSignedValue())
279 Overflow = true; // -MININT is the only thing that overflows.
280
281 // If this operator is live and overflowed, report the issue.
282 if (Overflow && ValueLive)
283 PP.Diag(Loc, diag::warn_pp_expr_overflow);
284
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000285 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000286 return false;
Chris Lattner7e61ac52007-04-11 03:42:36 +0000287 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000288
289 case tok::tilde:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000290 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000291 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000292 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner22eb9722006-06-18 05:43:12 +0000293 Result = ~Result;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000294 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000295 return false;
296
297 case tok::exclaim:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000298 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000299 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000300 Result = !Result;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000301 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
302 Result.setIsUnsigned(false);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000303
304 if (DT.State == DefinedTracker::DefinedMacro)
305 DT.State = DefinedTracker::NotDefinedMacro;
306 else if (DT.State == DefinedTracker::NotDefinedMacro)
307 DT.State = DefinedTracker::DefinedMacro;
Chris Lattner22eb9722006-06-18 05:43:12 +0000308 return false;
309
310 // FIXME: Handle #assert
311 }
312}
313
314
315
316/// getPrecedence - Return the precedence of the specified binary operator
317/// token. This returns:
318/// ~0 - Invalid token.
Chris Lattner9916c5c2006-10-27 05:24:37 +0000319/// 14 - *,/,%
320/// 13 - -,+
321/// 12 - <<,>>
322/// 11 - >=, <=, >, <
323/// 10 - ==, !=
Chris Lattner22eb9722006-06-18 05:43:12 +0000324/// 9 - &
325/// 8 - ^
326/// 7 - |
327/// 6 - &&
328/// 5 - ||
329/// 4 - ?
330/// 3 - :
331/// 0 - eom, )
332static unsigned getPrecedence(tok::TokenKind Kind) {
333 switch (Kind) {
334 default: return ~0U;
335 case tok::percent:
336 case tok::slash:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000337 case tok::star: return 14;
Chris Lattner22eb9722006-06-18 05:43:12 +0000338 case tok::plus:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000339 case tok::minus: return 13;
Chris Lattner22eb9722006-06-18 05:43:12 +0000340 case tok::lessless:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000341 case tok::greatergreater: return 12;
Chris Lattner22eb9722006-06-18 05:43:12 +0000342 case tok::lessequal:
343 case tok::less:
344 case tok::greaterequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000345 case tok::greater: return 11;
Chris Lattner22eb9722006-06-18 05:43:12 +0000346 case tok::exclaimequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000347 case tok::equalequal: return 10;
Chris Lattner22eb9722006-06-18 05:43:12 +0000348 case tok::amp: return 9;
349 case tok::caret: return 8;
350 case tok::pipe: return 7;
351 case tok::ampamp: return 6;
352 case tok::pipepipe: return 5;
353 case tok::question: return 4;
354 case tok::colon: return 3;
355 case tok::comma: return 2;
356 case tok::r_paren: return 0; // Lowest priority, end of expr.
357 case tok::eom: return 0; // Lowest priority, end of macro.
358 }
359}
360
361
362/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
363/// PeekTok, and whose precedence is PeekPrec.
Chris Lattner86054a92007-04-10 05:26:38 +0000364///
365/// If ValueLive is false, then this value is being evaluated in a context where
366/// the result is not used. As such, avoid diagnostics that relate to
367/// evaluation.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000368static bool EvaluateDirectiveSubExpr(llvm::APSInt &LHS, unsigned MinPrec,
Chris Lattner146762e2007-07-20 16:59:19 +0000369 Token &PeekTok, bool ValueLive,
Chris Lattner86054a92007-04-10 05:26:38 +0000370 Preprocessor &PP) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000371 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
372 // If this token isn't valid, report the error.
373 if (PeekPrec == ~0U) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000374 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
Chris Lattner22eb9722006-06-18 05:43:12 +0000375 return true;
376 }
377
378 while (1) {
379 // If this token has a lower precedence than we are allowed to parse, return
380 // it so that higher levels of the recursion can parse it.
381 if (PeekPrec < MinPrec)
382 return false;
383
384 tok::TokenKind Operator = PeekTok.getKind();
Chris Lattner86054a92007-04-10 05:26:38 +0000385
386 // If this is a short-circuiting operator, see if the RHS of the operator is
387 // dead. Note that this cannot just clobber ValueLive. Consider
388 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
389 // this example, the RHS of the && being dead does not make the rest of the
390 // expr dead.
391 bool RHSIsLive;
392 if (Operator == tok::ampamp && LHS == 0)
393 RHSIsLive = false; // RHS of "0 && x" is dead.
394 else if (Operator == tok::pipepipe && LHS != 0)
395 RHSIsLive = false; // RHS of "1 || x" is dead.
396 else if (Operator == tok::question && LHS == 0)
397 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
398 else
399 RHSIsLive = ValueLive;
Chris Lattner22eb9722006-06-18 05:43:12 +0000400
401 // Consume the operator, saving the operator token for error reporting.
Chris Lattner146762e2007-07-20 16:59:19 +0000402 Token OpToken = PeekTok;
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000403 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000404
Chris Lattner23b7eb62007-06-15 23:05:46 +0000405 llvm::APSInt RHS(LHS.getBitWidth());
Chris Lattner22eb9722006-06-18 05:43:12 +0000406 // Parse the RHS of the operator.
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000407 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000408 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000409
410 // Remember the precedence of this operator and get the precedence of the
411 // operator immediately to the right of the RHS.
412 unsigned ThisPrec = PeekPrec;
413 PeekPrec = getPrecedence(PeekTok.getKind());
414
415 // If this token isn't valid, report the error.
416 if (PeekPrec == ~0U) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000417 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
Chris Lattner22eb9722006-06-18 05:43:12 +0000418 return true;
419 }
420
421 bool isRightAssoc = Operator == tok::question;
422
423 // Get the precedence of the operator to the right of the RHS. If it binds
424 // more tightly with RHS than we do, evaluate it completely first.
425 if (ThisPrec < PeekPrec ||
426 (ThisPrec == PeekPrec && isRightAssoc)) {
Chris Lattner86054a92007-04-10 05:26:38 +0000427 if (EvaluateDirectiveSubExpr(RHS, ThisPrec+1, PeekTok, RHSIsLive, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000428 return true;
429 PeekPrec = getPrecedence(PeekTok.getKind());
430 }
431 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
432
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000433 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
434 // either operand is unsigned. Don't do this for x and y in "x ? y : z".
Chris Lattner23b7eb62007-06-15 23:05:46 +0000435 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000436 if (Operator != tok::question) {
Chris Lattner99ca0912007-04-11 04:14:45 +0000437 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
438 // If this just promoted something from signed to unsigned, and if the
439 // value was negative, warn about it.
440 if (ValueLive && Res.isUnsigned()) {
441 if (!LHS.isUnsigned() && LHS.isNegative())
442 PP.Diag(OpToken, diag::warn_pp_convert_lhs_to_positive,
Chris Lattner53e5a3862007-08-23 05:22:10 +0000443 LHS.toStringSigned() + " to " + LHS.toStringUnsigned());
Chris Lattner99ca0912007-04-11 04:14:45 +0000444 if (!RHS.isUnsigned() && RHS.isNegative())
445 PP.Diag(OpToken, diag::warn_pp_convert_rhs_to_positive,
Chris Lattner53e5a3862007-08-23 05:22:10 +0000446 RHS.toStringSigned() + " to " + RHS.toStringUnsigned());
Chris Lattner99ca0912007-04-11 04:14:45 +0000447 }
448 LHS.setIsUnsigned(Res.isUnsigned());
449 RHS.setIsUnsigned(Res.isUnsigned());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000450 }
451
452 // FIXME: All of these should detect and report overflow??
Chris Lattner5a0f1642007-04-10 06:54:33 +0000453 bool Overflow = false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000454 switch (Operator) {
455 default: assert(0 && "Unknown operator token!");
456 case tok::percent:
457 if (RHS == 0) {
Chris Lattner86054a92007-04-10 05:26:38 +0000458 if (ValueLive) PP.Diag(OpToken, diag::err_pp_remainder_by_zero);
Chris Lattner22eb9722006-06-18 05:43:12 +0000459 return true;
460 }
Chris Lattner9cc755d2007-04-10 07:07:11 +0000461 Res = LHS % RHS;
Chris Lattner22eb9722006-06-18 05:43:12 +0000462 break;
463 case tok::slash:
464 if (RHS == 0) {
Chris Lattner86054a92007-04-10 05:26:38 +0000465 if (ValueLive) PP.Diag(OpToken, diag::err_pp_division_by_zero);
Chris Lattner22eb9722006-06-18 05:43:12 +0000466 return true;
467 }
Chris Lattner9cc755d2007-04-10 07:07:11 +0000468 Res = LHS / RHS;
Chris Lattner8ec93552007-04-11 04:04:29 +0000469 if (LHS.isSigned())
470 Overflow = LHS.isMinSignedValue() && RHS.isAllOnesValue(); // MININT/-1
Chris Lattner22eb9722006-06-18 05:43:12 +0000471 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000472 case tok::star:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000473 Res = LHS * RHS;
Chris Lattner8ec93552007-04-11 04:04:29 +0000474 if (LHS != 0 && RHS != 0)
475 Overflow = Res/RHS != LHS || Res/LHS != RHS;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000476 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000477 case tok::lessless: {
478 // Determine whether overflow is about to happen.
Chris Lattner9cf21c52007-09-04 02:45:27 +0000479 unsigned ShAmt = static_cast<unsigned>(RHS.getLimitedValue());
Chris Lattner5a0f1642007-04-10 06:54:33 +0000480 if (ShAmt >= LHS.getBitWidth())
481 Overflow = true, ShAmt = LHS.getBitWidth()-1;
482 else if (LHS.isUnsigned())
483 Overflow = ShAmt > LHS.countLeadingZeros();
484 else if (LHS.isPositive())
485 Overflow = ShAmt >= LHS.countLeadingZeros(); // Don't allow sign change.
486 else
487 Overflow = ShAmt >= LHS.countLeadingOnes();
488
Chris Lattner9cc755d2007-04-10 07:07:11 +0000489 Res = LHS << ShAmt;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000490 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000491 }
492 case tok::greatergreater: {
493 // Determine whether overflow is about to happen.
Chris Lattner9cf21c52007-09-04 02:45:27 +0000494 unsigned ShAmt = static_cast<unsigned>(RHS.getLimitedValue());
Chris Lattner5a0f1642007-04-10 06:54:33 +0000495 if (ShAmt >= LHS.getBitWidth())
496 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000497 Res = LHS >> ShAmt;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000498 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000499 }
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000500 case tok::plus:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000501 Res = LHS + RHS;
Chris Lattner028c7de2007-04-11 03:34:29 +0000502 if (LHS.isUnsigned())
503 Overflow = Res.ult(LHS);
504 else if (LHS.isPositive() == RHS.isPositive() &&
505 Res.isPositive() != LHS.isPositive())
506 Overflow = true; // Overflow for signed addition.
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000507 break;
508 case tok::minus:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000509 Res = LHS - RHS;
Chris Lattner028c7de2007-04-11 03:34:29 +0000510 if (LHS.isUnsigned())
511 Overflow = Res.ugt(LHS);
512 else if (LHS.isPositive() != RHS.isPositive() &&
513 Res.isPositive() != LHS.isPositive())
514 Overflow = true; // Overflow for signed subtraction.
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000515 break;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000516 case tok::lessequal:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000517 Res = LHS <= RHS;
518 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000519 break;
520 case tok::less:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000521 Res = LHS < RHS;
522 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000523 break;
524 case tok::greaterequal:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000525 Res = LHS >= RHS;
526 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000527 break;
528 case tok::greater:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000529 Res = LHS > RHS;
530 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000531 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000532 case tok::exclaimequal:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000533 Res = LHS != RHS;
534 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000535 break;
536 case tok::equalequal:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000537 Res = LHS == RHS;
538 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000539 break;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000540 case tok::amp:
541 Res = LHS & RHS;
542 break;
543 case tok::caret:
544 Res = LHS ^ RHS;
545 break;
546 case tok::pipe:
547 Res = LHS | RHS;
548 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000549 case tok::ampamp:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000550 Res = (LHS != 0 && RHS != 0);
551 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000552 break;
553 case tok::pipepipe:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000554 Res = (LHS != 0 || RHS != 0);
555 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000556 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000557 case tok::comma:
Chris Lattnere3519cc2006-07-04 18:11:39 +0000558 PP.Diag(OpToken, diag::ext_pp_comma_expr);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000559 Res = RHS; // LHS = LHS,RHS -> RHS.
Chris Lattner22eb9722006-06-18 05:43:12 +0000560 break;
561 case tok::question: {
562 // Parse the : part of the expression.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000563 if (PeekTok.isNot(tok::colon)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000564 PP.Diag(OpToken, diag::err_pp_question_without_colon);
Chris Lattner22eb9722006-06-18 05:43:12 +0000565 return true;
566 }
567 // Consume the :.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000568 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000569
570 // Evaluate the value after the :.
Chris Lattner86054a92007-04-10 05:26:38 +0000571 bool AfterColonLive = ValueLive && LHS == 0;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000572 llvm::APSInt AfterColonVal(LHS.getBitWidth());
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000573 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000574 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
575 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000576
577 // Parse anything after the : RHS that has a higher precedence than ?.
578 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec+1,
Chris Lattner86054a92007-04-10 05:26:38 +0000579 PeekTok, AfterColonLive, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000580 return true;
581
582 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner9cc755d2007-04-10 07:07:11 +0000583 Res = LHS != 0 ? RHS : AfterColonVal;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000584
585 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
586 // either operand is unsigned.
Chris Lattner9cc755d2007-04-10 07:07:11 +0000587 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Chris Lattner22eb9722006-06-18 05:43:12 +0000588
589 // Figure out the precedence of the token after the : part.
590 PeekPrec = getPrecedence(PeekTok.getKind());
591 break;
592 }
593 case tok::colon:
594 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000595 PP.Diag(OpToken, diag::err_pp_colon_without_question);
Chris Lattner22eb9722006-06-18 05:43:12 +0000596 return true;
597 }
Chris Lattner5a0f1642007-04-10 06:54:33 +0000598
599 // If this operator is live and overflowed, report the issue.
600 if (Overflow && ValueLive)
601 PP.Diag(OpToken, diag::warn_pp_expr_overflow);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000602
603 // Put the result back into 'LHS' for our next iteration.
604 LHS = Res;
Chris Lattner22eb9722006-06-18 05:43:12 +0000605 }
606
607 return false;
608}
Chris Lattnere3519cc2006-07-04 18:11:39 +0000609
610/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
611/// may occur after a #if or #elif directive. If the expression is equivalent
612/// to "!defined(X)" return X in IfNDefMacro.
613bool Preprocessor::
614EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
615 // Peek ahead one token.
Chris Lattner146762e2007-07-20 16:59:19 +0000616 Token Tok;
Chris Lattnere3519cc2006-07-04 18:11:39 +0000617 Lex(Tok);
618
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000619 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
620 unsigned BitWidth = getTargetInfo().getIntMaxTWidth(Tok.getLocation());
Chris Lattner23b7eb62007-06-15 23:05:46 +0000621 llvm::APSInt ResVal(BitWidth);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000622 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000623 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000624 // Parse error, skip the rest of the macro line.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000625 if (Tok.isNot(tok::eom))
Chris Lattnere3519cc2006-07-04 18:11:39 +0000626 DiscardUntilEndOfDirective();
627 return false;
628 }
629
630 // If we are at the end of the expression after just parsing a value, there
631 // must be no (unparenthesized) binary operators involved, so we can exit
632 // directly.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000633 if (Tok.is(tok::eom)) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000634 // If the expression we parsed was of the form !defined(macro), return the
635 // macro in IfNDefMacro.
636 if (DT.State == DefinedTracker::NotDefinedMacro)
637 IfNDefMacro = DT.TheMacro;
638
Chris Lattnere3519cc2006-07-04 18:11:39 +0000639 return ResVal != 0;
640 }
641
642 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
643 // operator and the stuff after it.
Chris Lattner86054a92007-04-10 05:26:38 +0000644 if (EvaluateDirectiveSubExpr(ResVal, 1, Tok, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000645 // Parse error, skip the rest of the macro line.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000646 if (Tok.isNot(tok::eom))
Chris Lattnere3519cc2006-07-04 18:11:39 +0000647 DiscardUntilEndOfDirective();
648 return false;
649 }
650
651 // If we aren't at the tok::eom token, something bad happened, like an extra
652 // ')' token.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000653 if (Tok.isNot(tok::eom)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000654 Diag(Tok, diag::err_pp_expected_eol);
655 DiscardUntilEndOfDirective();
656 }
657
658 return ResVal != 0;
659}
660