blob: 77098724b566de98f582ccf5ec21c6cf2519c1c3 [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;
90 if (PeekTok.getKind() == tok::l_paren) {
91 // 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?
103 Result = II->getMacroInfo() != 0;
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 Lattner81278c62006-10-14 19:03:49 +0000108 II->getMacroInfo()->setIsUsed(true);
109
110 // If this is the first use of a target-specific macro, warn about it.
111 if (II->getMacroInfo()->isTargetSpecific()) {
112 // Don't warn on second use.
113 II->getMacroInfo()->setIsTargetSpecific(false);
114 PP.getTargetInfo().DiagnoseNonPortability(PeekTok.getLocation(),
115 diag::port_target_macro_use);
116 }
Chris Lattner86054a92007-04-10 05:26:38 +0000117 } else if (ValueLive) {
Chris Lattner063400e2006-10-14 19:54:15 +0000118 // Use of a target-specific macro for some other target? If so, warn.
119 if (II->isOtherTargetMacro()) {
120 II->setIsOtherTargetMacro(false); // Don't warn on second use.
121 PP.getTargetInfo().DiagnoseNonPortability(PeekTok.getLocation(),
122 diag::port_target_macro_use);
123 }
Chris Lattner81278c62006-10-14 19:03:49 +0000124 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000125
126 // Consume identifier.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000127 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000128
129 // If we are in parens, ensure we have a trailing ).
130 if (InParens) {
131 if (PeekTok.getKind() != tok::r_paren) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000132 PP.Diag(PeekTok, diag::err_pp_missing_rparen);
Chris Lattner22eb9722006-06-18 05:43:12 +0000133 return true;
134 }
135 // Consume the ).
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000136 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000137 }
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000138
139 // Success, remember that we saw defined(X).
140 DT.State = DefinedTracker::DefinedMacro;
141 DT.TheMacro = II;
Chris Lattner22eb9722006-06-18 05:43:12 +0000142 return false;
143 }
144
145 switch (PeekTok.getKind()) {
146 default: // Non-value token.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000147 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
Chris Lattner22eb9722006-06-18 05:43:12 +0000148 return true;
149 case tok::eom:
150 case tok::r_paren:
151 // If there is no expression, report and exit.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000152 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000153 return true;
154 case tok::numeric_constant: {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000155 llvm::SmallString<64> IntegerBuffer;
Steve Naroff451d8f162007-03-12 23:22:38 +0000156 IntegerBuffer.resize(PeekTok.getLength());
157 const char *ThisTokBegin = &IntegerBuffer[0];
158 unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
159 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
160 PeekTok.getLocation(), PP);
Chris Lattner6df79752007-04-04 06:54:19 +0000161 if (Literal.hadError)
Steve Narofff2fb89e2007-03-13 20:29:44 +0000162 return true; // a diagnostic was already reported.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000163
164 if (Literal.isFloatingLiteral()) {
Steve Naroff451d8f162007-03-12 23:22:38 +0000165 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000166 return true;
Steve Naroff451d8f162007-03-12 23:22:38 +0000167 }
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000168 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000169
170 // Parse the integer literal into Result.
171 if (Literal.GetIntegerValue(Result)) {
172 // Overflow parsing integer literal.
Chris Lattner86054a92007-04-10 05:26:38 +0000173 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000174 Result.setIsUnsigned(true);
175 } else {
176 // Set the signedness of the result to match whether there was a U suffix
177 // or not.
178 Result.setIsUnsigned(Literal.isUnsigned);
179
180 // Detect overflow based on whether the value is signed. If signed
181 // and if the value is too large, emit a warning "integer constant is so
182 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
183 // is 64-bits.
184 if (!Literal.isUnsigned && Result.isNegative()) {
Chris Lattner86054a92007-04-10 05:26:38 +0000185 if (ValueLive)PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000186 Result.setIsUnsigned(true);
187 }
188 }
189
190 // Consume the token.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000191 PP.LexNonComment(PeekTok);
192 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000193 }
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000194 case tok::char_constant: { // 'x'
Chris Lattner23b7eb62007-06-15 23:05:46 +0000195 llvm::SmallString<32> CharBuffer;
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000196 CharBuffer.resize(PeekTok.getLength());
197 const char *ThisTokBegin = &CharBuffer[0];
198 unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
199 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
200 PeekTok.getLocation(), PP);
201 if (Literal.hadError())
202 return true; // A diagnostic was already emitted.
203
204 // Character literals are always int or wchar_t, expand to intmax_t.
205 TargetInfo &TI = PP.getTargetInfo();
206 unsigned NumBits;
207 if (Literal.isWide())
208 NumBits = TI.getWCharWidth(PeekTok.getLocation());
209 else
210 NumBits = TI.getCharWidth(PeekTok.getLocation());
211
212 // Set the width.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000213 llvm::APSInt Val(NumBits);
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000214 // Set the value.
215 Val = Literal.getValue();
216 // Set the signedness.
217 Val.setIsUnsigned(!TI.isCharSigned(PeekTok.getLocation()));
218
219 if (Result.getBitWidth() > Val.getBitWidth()) {
220 if (Val.isSigned())
221 Result = Val.sext(Result.getBitWidth());
222 else
223 Result = Val.zext(Result.getBitWidth());
224 Result.setIsUnsigned(Val.isUnsigned());
225 } else {
226 assert(Result.getBitWidth() == Val.getBitWidth() &&
227 "intmax_t smaller than char/wchar_t?");
228 Result = Val;
229 }
230
231 // Consume the token.
232 PP.LexNonComment(PeekTok);
233 return false;
234 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000235 case tok::l_paren:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000236 PP.LexNonComment(PeekTok); // Eat the (.
Chris Lattnercb283342006-06-18 06:48:37 +0000237 // Parse the value and if there are any binary operators involved, parse
238 // them.
Chris Lattner86054a92007-04-10 05:26:38 +0000239 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000240
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000241 // If this is a silly value like (X), which doesn't need parens, check for
242 // !(defined X).
243 if (PeekTok.getKind() == tok::r_paren) {
244 // Just use DT unmodified as our result.
245 } else {
Chris Lattner86054a92007-04-10 05:26:38 +0000246 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
247 return true;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000248
249 if (PeekTok.getKind() != tok::r_paren) {
250 PP.Diag(PeekTok, diag::err_pp_expected_rparen);
251 return true;
252 }
253 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000254 }
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000255 PP.LexNonComment(PeekTok); // Eat the ).
Chris Lattner22eb9722006-06-18 05:43:12 +0000256 return false;
257
258 case tok::plus:
259 // Unary plus doesn't modify the value.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000260 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000261 return EvaluateValue(Result, PeekTok, DT, ValueLive, PP);
Chris Lattner7e61ac52007-04-11 03:42:36 +0000262 case tok::minus: {
263 SourceLocation Loc = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000264 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000265 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000266 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner22eb9722006-06-18 05:43:12 +0000267 Result = -Result;
Chris Lattner7e61ac52007-04-11 03:42:36 +0000268
269 bool Overflow = false;
270 if (Result.isUnsigned())
271 Overflow = !Result.isPositive();
272 else if (Result.isMinSignedValue())
273 Overflow = true; // -MININT is the only thing that overflows.
274
275 // If this operator is live and overflowed, report the issue.
276 if (Overflow && ValueLive)
277 PP.Diag(Loc, diag::warn_pp_expr_overflow);
278
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000279 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000280 return false;
Chris Lattner7e61ac52007-04-11 03:42:36 +0000281 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000282
283 case tok::tilde:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000284 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000285 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000286 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner22eb9722006-06-18 05:43:12 +0000287 Result = ~Result;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000288 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000289 return false;
290
291 case tok::exclaim:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000292 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000293 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000294 Result = !Result;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000295 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
296 Result.setIsUnsigned(false);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000297
298 if (DT.State == DefinedTracker::DefinedMacro)
299 DT.State = DefinedTracker::NotDefinedMacro;
300 else if (DT.State == DefinedTracker::NotDefinedMacro)
301 DT.State = DefinedTracker::DefinedMacro;
Chris Lattner22eb9722006-06-18 05:43:12 +0000302 return false;
303
304 // FIXME: Handle #assert
305 }
306}
307
308
309
310/// getPrecedence - Return the precedence of the specified binary operator
311/// token. This returns:
312/// ~0 - Invalid token.
Chris Lattner9916c5c2006-10-27 05:24:37 +0000313/// 14 - *,/,%
314/// 13 - -,+
315/// 12 - <<,>>
316/// 11 - >=, <=, >, <
317/// 10 - ==, !=
Chris Lattner22eb9722006-06-18 05:43:12 +0000318/// 9 - &
319/// 8 - ^
320/// 7 - |
321/// 6 - &&
322/// 5 - ||
323/// 4 - ?
324/// 3 - :
325/// 0 - eom, )
326static unsigned getPrecedence(tok::TokenKind Kind) {
327 switch (Kind) {
328 default: return ~0U;
329 case tok::percent:
330 case tok::slash:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000331 case tok::star: return 14;
Chris Lattner22eb9722006-06-18 05:43:12 +0000332 case tok::plus:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000333 case tok::minus: return 13;
Chris Lattner22eb9722006-06-18 05:43:12 +0000334 case tok::lessless:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000335 case tok::greatergreater: return 12;
Chris Lattner22eb9722006-06-18 05:43:12 +0000336 case tok::lessequal:
337 case tok::less:
338 case tok::greaterequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000339 case tok::greater: return 11;
Chris Lattner22eb9722006-06-18 05:43:12 +0000340 case tok::exclaimequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000341 case tok::equalequal: return 10;
Chris Lattner22eb9722006-06-18 05:43:12 +0000342 case tok::amp: return 9;
343 case tok::caret: return 8;
344 case tok::pipe: return 7;
345 case tok::ampamp: return 6;
346 case tok::pipepipe: return 5;
347 case tok::question: return 4;
348 case tok::colon: return 3;
349 case tok::comma: return 2;
350 case tok::r_paren: return 0; // Lowest priority, end of expr.
351 case tok::eom: return 0; // Lowest priority, end of macro.
352 }
353}
354
355
356/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
357/// PeekTok, and whose precedence is PeekPrec.
Chris Lattner86054a92007-04-10 05:26:38 +0000358///
359/// If ValueLive is false, then this value is being evaluated in a context where
360/// the result is not used. As such, avoid diagnostics that relate to
361/// evaluation.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000362static bool EvaluateDirectiveSubExpr(llvm::APSInt &LHS, unsigned MinPrec,
Chris Lattner146762e2007-07-20 16:59:19 +0000363 Token &PeekTok, bool ValueLive,
Chris Lattner86054a92007-04-10 05:26:38 +0000364 Preprocessor &PP) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000365 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
366 // If this token isn't valid, report the error.
367 if (PeekPrec == ~0U) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000368 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
Chris Lattner22eb9722006-06-18 05:43:12 +0000369 return true;
370 }
371
372 while (1) {
373 // If this token has a lower precedence than we are allowed to parse, return
374 // it so that higher levels of the recursion can parse it.
375 if (PeekPrec < MinPrec)
376 return false;
377
378 tok::TokenKind Operator = PeekTok.getKind();
Chris Lattner86054a92007-04-10 05:26:38 +0000379
380 // If this is a short-circuiting operator, see if the RHS of the operator is
381 // dead. Note that this cannot just clobber ValueLive. Consider
382 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
383 // this example, the RHS of the && being dead does not make the rest of the
384 // expr dead.
385 bool RHSIsLive;
386 if (Operator == tok::ampamp && LHS == 0)
387 RHSIsLive = false; // RHS of "0 && x" is dead.
388 else if (Operator == tok::pipepipe && LHS != 0)
389 RHSIsLive = false; // RHS of "1 || x" is dead.
390 else if (Operator == tok::question && LHS == 0)
391 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
392 else
393 RHSIsLive = ValueLive;
Chris Lattner22eb9722006-06-18 05:43:12 +0000394
395 // Consume the operator, saving the operator token for error reporting.
Chris Lattner146762e2007-07-20 16:59:19 +0000396 Token OpToken = PeekTok;
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000397 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000398
Chris Lattner23b7eb62007-06-15 23:05:46 +0000399 llvm::APSInt RHS(LHS.getBitWidth());
Chris Lattner22eb9722006-06-18 05:43:12 +0000400 // Parse the RHS of the operator.
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000401 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000402 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000403
404 // Remember the precedence of this operator and get the precedence of the
405 // operator immediately to the right of the RHS.
406 unsigned ThisPrec = PeekPrec;
407 PeekPrec = getPrecedence(PeekTok.getKind());
408
409 // If this token isn't valid, report the error.
410 if (PeekPrec == ~0U) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000411 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
Chris Lattner22eb9722006-06-18 05:43:12 +0000412 return true;
413 }
414
415 bool isRightAssoc = Operator == tok::question;
416
417 // Get the precedence of the operator to the right of the RHS. If it binds
418 // more tightly with RHS than we do, evaluate it completely first.
419 if (ThisPrec < PeekPrec ||
420 (ThisPrec == PeekPrec && isRightAssoc)) {
Chris Lattner86054a92007-04-10 05:26:38 +0000421 if (EvaluateDirectiveSubExpr(RHS, ThisPrec+1, PeekTok, RHSIsLive, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000422 return true;
423 PeekPrec = getPrecedence(PeekTok.getKind());
424 }
425 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
426
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000427 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
428 // either operand is unsigned. Don't do this for x and y in "x ? y : z".
Chris Lattner23b7eb62007-06-15 23:05:46 +0000429 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000430 if (Operator != tok::question) {
Chris Lattner99ca0912007-04-11 04:14:45 +0000431 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
432 // If this just promoted something from signed to unsigned, and if the
433 // value was negative, warn about it.
434 if (ValueLive && Res.isUnsigned()) {
435 if (!LHS.isUnsigned() && LHS.isNegative())
436 PP.Diag(OpToken, diag::warn_pp_convert_lhs_to_positive,
437 LHS.toString(10, true) + " to " + LHS.toString(10, false));
438 if (!RHS.isUnsigned() && RHS.isNegative())
439 PP.Diag(OpToken, diag::warn_pp_convert_rhs_to_positive,
440 RHS.toString(10, true) + " to " + RHS.toString(10, false));
441 }
442 LHS.setIsUnsigned(Res.isUnsigned());
443 RHS.setIsUnsigned(Res.isUnsigned());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000444 }
445
446 // FIXME: All of these should detect and report overflow??
Chris Lattner5a0f1642007-04-10 06:54:33 +0000447 bool Overflow = false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000448 switch (Operator) {
449 default: assert(0 && "Unknown operator token!");
450 case tok::percent:
451 if (RHS == 0) {
Chris Lattner86054a92007-04-10 05:26:38 +0000452 if (ValueLive) PP.Diag(OpToken, diag::err_pp_remainder_by_zero);
Chris Lattner22eb9722006-06-18 05:43:12 +0000453 return true;
454 }
Chris Lattner9cc755d2007-04-10 07:07:11 +0000455 Res = LHS % RHS;
Chris Lattner22eb9722006-06-18 05:43:12 +0000456 break;
457 case tok::slash:
458 if (RHS == 0) {
Chris Lattner86054a92007-04-10 05:26:38 +0000459 if (ValueLive) PP.Diag(OpToken, diag::err_pp_division_by_zero);
Chris Lattner22eb9722006-06-18 05:43:12 +0000460 return true;
461 }
Chris Lattner9cc755d2007-04-10 07:07:11 +0000462 Res = LHS / RHS;
Chris Lattner8ec93552007-04-11 04:04:29 +0000463 if (LHS.isSigned())
464 Overflow = LHS.isMinSignedValue() && RHS.isAllOnesValue(); // MININT/-1
Chris Lattner22eb9722006-06-18 05:43:12 +0000465 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000466 case tok::star:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000467 Res = LHS * RHS;
Chris Lattner8ec93552007-04-11 04:04:29 +0000468 if (LHS != 0 && RHS != 0)
469 Overflow = Res/RHS != LHS || Res/LHS != RHS;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000470 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000471 case tok::lessless: {
472 // Determine whether overflow is about to happen.
473 unsigned ShAmt = RHS.getLimitedValue();
474 if (ShAmt >= LHS.getBitWidth())
475 Overflow = true, ShAmt = LHS.getBitWidth()-1;
476 else if (LHS.isUnsigned())
477 Overflow = ShAmt > LHS.countLeadingZeros();
478 else if (LHS.isPositive())
479 Overflow = ShAmt >= LHS.countLeadingZeros(); // Don't allow sign change.
480 else
481 Overflow = ShAmt >= LHS.countLeadingOnes();
482
Chris Lattner9cc755d2007-04-10 07:07:11 +0000483 Res = LHS << ShAmt;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000484 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000485 }
486 case tok::greatergreater: {
487 // Determine whether overflow is about to happen.
488 unsigned ShAmt = RHS.getLimitedValue();
489 if (ShAmt >= LHS.getBitWidth())
490 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000491 Res = LHS >> ShAmt;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000492 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000493 }
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000494 case tok::plus:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000495 Res = LHS + RHS;
Chris Lattner028c7de2007-04-11 03:34:29 +0000496 if (LHS.isUnsigned())
497 Overflow = Res.ult(LHS);
498 else if (LHS.isPositive() == RHS.isPositive() &&
499 Res.isPositive() != LHS.isPositive())
500 Overflow = true; // Overflow for signed addition.
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000501 break;
502 case tok::minus:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000503 Res = LHS - RHS;
Chris Lattner028c7de2007-04-11 03:34:29 +0000504 if (LHS.isUnsigned())
505 Overflow = Res.ugt(LHS);
506 else if (LHS.isPositive() != RHS.isPositive() &&
507 Res.isPositive() != LHS.isPositive())
508 Overflow = true; // Overflow for signed subtraction.
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000509 break;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000510 case tok::lessequal:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000511 Res = LHS <= RHS;
512 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000513 break;
514 case tok::less:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000515 Res = LHS < RHS;
516 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000517 break;
518 case tok::greaterequal:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000519 Res = LHS >= RHS;
520 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000521 break;
522 case tok::greater:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000523 Res = LHS > RHS;
524 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000525 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000526 case tok::exclaimequal:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000527 Res = LHS != RHS;
528 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000529 break;
530 case tok::equalequal:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000531 Res = LHS == RHS;
532 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000533 break;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000534 case tok::amp:
535 Res = LHS & RHS;
536 break;
537 case tok::caret:
538 Res = LHS ^ RHS;
539 break;
540 case tok::pipe:
541 Res = LHS | RHS;
542 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000543 case tok::ampamp:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000544 Res = (LHS != 0 && RHS != 0);
545 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000546 break;
547 case tok::pipepipe:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000548 Res = (LHS != 0 || RHS != 0);
549 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000550 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000551 case tok::comma:
Chris Lattnere3519cc2006-07-04 18:11:39 +0000552 PP.Diag(OpToken, diag::ext_pp_comma_expr);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000553 Res = RHS; // LHS = LHS,RHS -> RHS.
Chris Lattner22eb9722006-06-18 05:43:12 +0000554 break;
555 case tok::question: {
556 // Parse the : part of the expression.
557 if (PeekTok.getKind() != tok::colon) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000558 PP.Diag(OpToken, diag::err_pp_question_without_colon);
Chris Lattner22eb9722006-06-18 05:43:12 +0000559 return true;
560 }
561 // Consume the :.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000562 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000563
564 // Evaluate the value after the :.
Chris Lattner86054a92007-04-10 05:26:38 +0000565 bool AfterColonLive = ValueLive && LHS == 0;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000566 llvm::APSInt AfterColonVal(LHS.getBitWidth());
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000567 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000568 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
569 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000570
571 // Parse anything after the : RHS that has a higher precedence than ?.
572 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec+1,
Chris Lattner86054a92007-04-10 05:26:38 +0000573 PeekTok, AfterColonLive, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000574 return true;
575
576 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner9cc755d2007-04-10 07:07:11 +0000577 Res = LHS != 0 ? RHS : AfterColonVal;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000578
579 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
580 // either operand is unsigned.
Chris Lattner9cc755d2007-04-10 07:07:11 +0000581 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Chris Lattner22eb9722006-06-18 05:43:12 +0000582
583 // Figure out the precedence of the token after the : part.
584 PeekPrec = getPrecedence(PeekTok.getKind());
585 break;
586 }
587 case tok::colon:
588 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000589 PP.Diag(OpToken, diag::err_pp_colon_without_question);
Chris Lattner22eb9722006-06-18 05:43:12 +0000590 return true;
591 }
Chris Lattner5a0f1642007-04-10 06:54:33 +0000592
593 // If this operator is live and overflowed, report the issue.
594 if (Overflow && ValueLive)
595 PP.Diag(OpToken, diag::warn_pp_expr_overflow);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000596
597 // Put the result back into 'LHS' for our next iteration.
598 LHS = Res;
Chris Lattner22eb9722006-06-18 05:43:12 +0000599 }
600
601 return false;
602}
Chris Lattnere3519cc2006-07-04 18:11:39 +0000603
604/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
605/// may occur after a #if or #elif directive. If the expression is equivalent
606/// to "!defined(X)" return X in IfNDefMacro.
607bool Preprocessor::
608EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
609 // Peek ahead one token.
Chris Lattner146762e2007-07-20 16:59:19 +0000610 Token Tok;
Chris Lattnere3519cc2006-07-04 18:11:39 +0000611 Lex(Tok);
612
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000613 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
614 unsigned BitWidth = getTargetInfo().getIntMaxTWidth(Tok.getLocation());
Chris Lattner23b7eb62007-06-15 23:05:46 +0000615 llvm::APSInt ResVal(BitWidth);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000616 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000617 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000618 // Parse error, skip the rest of the macro line.
619 if (Tok.getKind() != tok::eom)
620 DiscardUntilEndOfDirective();
621 return false;
622 }
623
624 // If we are at the end of the expression after just parsing a value, there
625 // must be no (unparenthesized) binary operators involved, so we can exit
626 // directly.
627 if (Tok.getKind() == tok::eom) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000628 // If the expression we parsed was of the form !defined(macro), return the
629 // macro in IfNDefMacro.
630 if (DT.State == DefinedTracker::NotDefinedMacro)
631 IfNDefMacro = DT.TheMacro;
632
Chris Lattnere3519cc2006-07-04 18:11:39 +0000633 return ResVal != 0;
634 }
635
636 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
637 // operator and the stuff after it.
Chris Lattner86054a92007-04-10 05:26:38 +0000638 if (EvaluateDirectiveSubExpr(ResVal, 1, Tok, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000639 // Parse error, skip the rest of the macro line.
640 if (Tok.getKind() != tok::eom)
641 DiscardUntilEndOfDirective();
642 return false;
643 }
644
645 // If we aren't at the tok::eom token, something bad happened, like an extra
646 // ')' token.
647 if (Tok.getKind() != tok::eom) {
648 Diag(Tok, diag::err_pp_expected_eol);
649 DiscardUntilEndOfDirective();
650 }
651
652 return ResVal != 0;
653}
654