blob: a0c8d1a5036f601b12b5df2ced4e239f1c0707f3 [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
76 // into a simple 0.
Chris Lattner2bb8a952006-11-21 22:24:17 +000077 if (II->getPPKeywordID() != tok::pp_defined) {
Chris Lattner22eb9722006-06-18 05:43:12 +000078 Result = 0;
Chris Lattnera9eac7f2007-04-05 05:24:00 +000079 Result.setIsUnsigned(false); // "0" is signed intmax_t 0.
Chris Lattnerbcb416b2006-10-27 05:43:50 +000080 PP.LexNonComment(PeekTok);
Chris Lattnercb283342006-06-18 06:48:37 +000081 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +000082 }
83
84 // Handle "defined X" and "defined(X)".
Chris Lattner22eb9722006-06-18 05:43:12 +000085
Chris Lattnere3519cc2006-07-04 18:11:39 +000086 // Get the next token, don't expand it.
87 PP.LexUnexpandedToken(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +000088
89 // Two options, it can either be a pp-identifier or a (.
90 bool InParens = false;
91 if (PeekTok.getKind() == tok::l_paren) {
92 // Found a paren, remember we saw it and skip it.
93 InParens = true;
Chris Lattnere3519cc2006-07-04 18:11:39 +000094 PP.LexUnexpandedToken(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +000095 }
96
97 // If we don't have a pp-identifier now, this is an error.
98 if ((II = PeekTok.getIdentifierInfo()) == 0) {
Chris Lattnere3519cc2006-07-04 18:11:39 +000099 PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000100 return true;
101 }
102
103 // Otherwise, we got an identifier, is it defined to something?
104 Result = II->getMacroInfo() != 0;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000105 Result.setIsUnsigned(false); // Result is signed intmax_t.
106
Chris Lattnera78a97e2006-07-03 05:42:18 +0000107 // If there is a macro, mark it used.
Chris Lattner86054a92007-04-10 05:26:38 +0000108 if (Result != 0 && ValueLive) {
Chris Lattner81278c62006-10-14 19:03:49 +0000109 II->getMacroInfo()->setIsUsed(true);
110
111 // If this is the first use of a target-specific macro, warn about it.
112 if (II->getMacroInfo()->isTargetSpecific()) {
113 // Don't warn on second use.
114 II->getMacroInfo()->setIsTargetSpecific(false);
115 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) {
132 if (PeekTok.getKind() != 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 Lattner6df79752007-04-04 06:54:19 +0000156 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
165 if (Literal.isFloatingLiteral()) {
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
171 // Parse the integer literal into Result.
172 if (Literal.GetIntegerValue(Result)) {
173 // Overflow parsing integer literal.
Chris Lattner86054a92007-04-10 05:26:38 +0000174 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000175 Result.setIsUnsigned(true);
176 } else {
177 // Set the signedness of the result to match whether there was a U suffix
178 // or not.
179 Result.setIsUnsigned(Literal.isUnsigned);
180
181 // Detect overflow based on whether the value is signed. If signed
182 // and if the value is too large, emit a warning "integer constant is so
183 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
184 // is 64-bits.
185 if (!Literal.isUnsigned && Result.isNegative()) {
Chris Lattner86054a92007-04-10 05:26:38 +0000186 if (ValueLive)PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000187 Result.setIsUnsigned(true);
188 }
189 }
190
191 // Consume the token.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000192 PP.LexNonComment(PeekTok);
193 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000194 }
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000195 case tok::char_constant: { // 'x'
196 SmallString<32> CharBuffer;
197 CharBuffer.resize(PeekTok.getLength());
198 const char *ThisTokBegin = &CharBuffer[0];
199 unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
200 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
201 PeekTok.getLocation(), PP);
202 if (Literal.hadError())
203 return true; // A diagnostic was already emitted.
204
205 // Character literals are always int or wchar_t, expand to intmax_t.
206 TargetInfo &TI = PP.getTargetInfo();
207 unsigned NumBits;
208 if (Literal.isWide())
209 NumBits = TI.getWCharWidth(PeekTok.getLocation());
210 else
211 NumBits = TI.getCharWidth(PeekTok.getLocation());
212
213 // Set the width.
214 APSInt Val(NumBits);
215 // Set the value.
216 Val = Literal.getValue();
217 // Set the signedness.
218 Val.setIsUnsigned(!TI.isCharSigned(PeekTok.getLocation()));
219
220 if (Result.getBitWidth() > Val.getBitWidth()) {
221 if (Val.isSigned())
222 Result = Val.sext(Result.getBitWidth());
223 else
224 Result = Val.zext(Result.getBitWidth());
225 Result.setIsUnsigned(Val.isUnsigned());
226 } else {
227 assert(Result.getBitWidth() == Val.getBitWidth() &&
228 "intmax_t smaller than char/wchar_t?");
229 Result = Val;
230 }
231
232 // Consume the token.
233 PP.LexNonComment(PeekTok);
234 return false;
235 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000236 case tok::l_paren:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000237 PP.LexNonComment(PeekTok); // Eat the (.
Chris Lattnercb283342006-06-18 06:48:37 +0000238 // Parse the value and if there are any binary operators involved, parse
239 // them.
Chris Lattner86054a92007-04-10 05:26:38 +0000240 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000241
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000242 // If this is a silly value like (X), which doesn't need parens, check for
243 // !(defined X).
244 if (PeekTok.getKind() == tok::r_paren) {
245 // Just use DT unmodified as our result.
246 } else {
Chris Lattner86054a92007-04-10 05:26:38 +0000247 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
248 return true;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000249
250 if (PeekTok.getKind() != tok::r_paren) {
251 PP.Diag(PeekTok, diag::err_pp_expected_rparen);
252 return true;
253 }
254 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000255 }
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000256 PP.LexNonComment(PeekTok); // Eat the ).
Chris Lattner22eb9722006-06-18 05:43:12 +0000257 return false;
258
259 case tok::plus:
260 // Unary plus doesn't modify the value.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000261 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000262 return EvaluateValue(Result, PeekTok, DT, ValueLive, PP);
Chris Lattner22eb9722006-06-18 05:43:12 +0000263 case tok::minus:
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 Lattnerb9d90f72006-07-04 18:32:03 +0000268 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000269 return false;
270
271 case tok::tilde:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000272 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000273 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000274 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner22eb9722006-06-18 05:43:12 +0000275 Result = ~Result;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000276 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000277 return false;
278
279 case tok::exclaim:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000280 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000281 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000282 Result = !Result;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000283 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
284 Result.setIsUnsigned(false);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000285
286 if (DT.State == DefinedTracker::DefinedMacro)
287 DT.State = DefinedTracker::NotDefinedMacro;
288 else if (DT.State == DefinedTracker::NotDefinedMacro)
289 DT.State = DefinedTracker::DefinedMacro;
Chris Lattner22eb9722006-06-18 05:43:12 +0000290 return false;
291
292 // FIXME: Handle #assert
293 }
294}
295
296
297
298/// getPrecedence - Return the precedence of the specified binary operator
299/// token. This returns:
300/// ~0 - Invalid token.
Chris Lattner9916c5c2006-10-27 05:24:37 +0000301/// 14 - *,/,%
302/// 13 - -,+
303/// 12 - <<,>>
304/// 11 - >=, <=, >, <
305/// 10 - ==, !=
Chris Lattner22eb9722006-06-18 05:43:12 +0000306/// 9 - &
307/// 8 - ^
308/// 7 - |
309/// 6 - &&
310/// 5 - ||
311/// 4 - ?
312/// 3 - :
313/// 0 - eom, )
314static unsigned getPrecedence(tok::TokenKind Kind) {
315 switch (Kind) {
316 default: return ~0U;
317 case tok::percent:
318 case tok::slash:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000319 case tok::star: return 14;
Chris Lattner22eb9722006-06-18 05:43:12 +0000320 case tok::plus:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000321 case tok::minus: return 13;
Chris Lattner22eb9722006-06-18 05:43:12 +0000322 case tok::lessless:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000323 case tok::greatergreater: return 12;
Chris Lattner22eb9722006-06-18 05:43:12 +0000324 case tok::lessequal:
325 case tok::less:
326 case tok::greaterequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000327 case tok::greater: return 11;
Chris Lattner22eb9722006-06-18 05:43:12 +0000328 case tok::exclaimequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000329 case tok::equalequal: return 10;
Chris Lattner22eb9722006-06-18 05:43:12 +0000330 case tok::amp: return 9;
331 case tok::caret: return 8;
332 case tok::pipe: return 7;
333 case tok::ampamp: return 6;
334 case tok::pipepipe: return 5;
335 case tok::question: return 4;
336 case tok::colon: return 3;
337 case tok::comma: return 2;
338 case tok::r_paren: return 0; // Lowest priority, end of expr.
339 case tok::eom: return 0; // Lowest priority, end of macro.
340 }
341}
342
343
344/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
345/// PeekTok, and whose precedence is PeekPrec.
Chris Lattner86054a92007-04-10 05:26:38 +0000346///
347/// If ValueLive is false, then this value is being evaluated in a context where
348/// the result is not used. As such, avoid diagnostics that relate to
349/// evaluation.
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000350static bool EvaluateDirectiveSubExpr(APSInt &LHS, unsigned MinPrec,
Chris Lattner86054a92007-04-10 05:26:38 +0000351 LexerToken &PeekTok, bool ValueLive,
352 Preprocessor &PP) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000353 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
354 // If this token isn't valid, report the error.
355 if (PeekPrec == ~0U) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000356 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
Chris Lattner22eb9722006-06-18 05:43:12 +0000357 return true;
358 }
359
360 while (1) {
361 // If this token has a lower precedence than we are allowed to parse, return
362 // it so that higher levels of the recursion can parse it.
363 if (PeekPrec < MinPrec)
364 return false;
365
366 tok::TokenKind Operator = PeekTok.getKind();
Chris Lattner86054a92007-04-10 05:26:38 +0000367
368 // If this is a short-circuiting operator, see if the RHS of the operator is
369 // dead. Note that this cannot just clobber ValueLive. Consider
370 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
371 // this example, the RHS of the && being dead does not make the rest of the
372 // expr dead.
373 bool RHSIsLive;
374 if (Operator == tok::ampamp && LHS == 0)
375 RHSIsLive = false; // RHS of "0 && x" is dead.
376 else if (Operator == tok::pipepipe && LHS != 0)
377 RHSIsLive = false; // RHS of "1 || x" is dead.
378 else if (Operator == tok::question && LHS == 0)
379 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
380 else
381 RHSIsLive = ValueLive;
Chris Lattner22eb9722006-06-18 05:43:12 +0000382
383 // Consume the operator, saving the operator token for error reporting.
384 LexerToken OpToken = PeekTok;
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000385 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000386
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000387 APSInt RHS(LHS.getBitWidth());
Chris Lattner22eb9722006-06-18 05:43:12 +0000388 // Parse the RHS of the operator.
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000389 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000390 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000391
392 // Remember the precedence of this operator and get the precedence of the
393 // operator immediately to the right of the RHS.
394 unsigned ThisPrec = PeekPrec;
395 PeekPrec = getPrecedence(PeekTok.getKind());
396
397 // If this token isn't valid, report the error.
398 if (PeekPrec == ~0U) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000399 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
Chris Lattner22eb9722006-06-18 05:43:12 +0000400 return true;
401 }
402
403 bool isRightAssoc = Operator == tok::question;
404
405 // Get the precedence of the operator to the right of the RHS. If it binds
406 // more tightly with RHS than we do, evaluate it completely first.
407 if (ThisPrec < PeekPrec ||
408 (ThisPrec == PeekPrec && isRightAssoc)) {
Chris Lattner86054a92007-04-10 05:26:38 +0000409 if (EvaluateDirectiveSubExpr(RHS, ThisPrec+1, PeekTok, RHSIsLive, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000410 return true;
411 PeekPrec = getPrecedence(PeekTok.getKind());
412 }
413 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
414
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000415 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
416 // either operand is unsigned. Don't do this for x and y in "x ? y : z".
417 if (Operator != tok::question) {
418 if (RHS.isUnsigned()) LHS.setIsUnsigned(true);
419 RHS.setIsUnsigned(LHS.isUnsigned());
420 }
421
422 // FIXME: All of these should detect and report overflow??
Chris Lattner22eb9722006-06-18 05:43:12 +0000423 switch (Operator) {
424 default: assert(0 && "Unknown operator token!");
425 case tok::percent:
426 if (RHS == 0) {
Chris Lattner86054a92007-04-10 05:26:38 +0000427 if (ValueLive) PP.Diag(OpToken, diag::err_pp_remainder_by_zero);
Chris Lattner22eb9722006-06-18 05:43:12 +0000428 return true;
429 }
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000430 LHS %= RHS;
Chris Lattner22eb9722006-06-18 05:43:12 +0000431 break;
432 case tok::slash:
433 if (RHS == 0) {
Chris Lattner86054a92007-04-10 05:26:38 +0000434 if (ValueLive) PP.Diag(OpToken, diag::err_pp_division_by_zero);
Chris Lattner22eb9722006-06-18 05:43:12 +0000435 return true;
436 }
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000437 LHS /= RHS;
Chris Lattner22eb9722006-06-18 05:43:12 +0000438 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000439 case tok::star:
440 LHS *= RHS;
441 break;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000442 case tok::lessless:
443 // FIXME: shift amt overflow?
444 // FIXME: Don't use getZExtValue.
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000445 LHS <<= RHS.getZExtValue();
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000446 break;
447 case tok::greatergreater:
448 // FIXME: signed vs unsigned
449 // FIXME: Don't use getZExtValue.
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000450 LHS >>= RHS.getZExtValue();
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000451 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000452 case tok::plus:
453 LHS += RHS;
454 break;
455 case tok::minus:
456 LHS -= RHS;
457 break;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000458 case tok::lessequal:
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000459 LHS = LHS <= RHS;
460 LHS.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000461 break;
462 case tok::less:
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000463 LHS = LHS < RHS;
464 LHS.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000465 break;
466 case tok::greaterequal:
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000467 LHS = LHS >= RHS;
468 LHS.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000469 break;
470 case tok::greater:
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000471 LHS = LHS > RHS;
472 LHS.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000473 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000474 case tok::exclaimequal:
475 LHS = LHS != RHS;
476 LHS.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
477 break;
478 case tok::equalequal:
479 LHS = LHS == RHS;
480 LHS.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
481 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000482 case tok::amp: LHS &= RHS; break;
483 case tok::caret: LHS ^= RHS; break;
484 case tok::pipe: LHS |= RHS; break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000485 case tok::ampamp:
486 LHS = LHS != 0 && RHS != 0;
487 LHS.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
488 break;
489 case tok::pipepipe:
490 LHS = LHS != 0 || RHS != 0;
491 LHS.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
492 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000493 case tok::comma:
Chris Lattnere3519cc2006-07-04 18:11:39 +0000494 PP.Diag(OpToken, diag::ext_pp_comma_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000495 LHS = RHS; // LHS = LHS,RHS -> RHS.
496 break;
497 case tok::question: {
498 // Parse the : part of the expression.
499 if (PeekTok.getKind() != tok::colon) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000500 PP.Diag(OpToken, diag::err_pp_question_without_colon);
Chris Lattner22eb9722006-06-18 05:43:12 +0000501 return true;
502 }
503 // Consume the :.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000504 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000505
506 // Evaluate the value after the :.
Chris Lattner86054a92007-04-10 05:26:38 +0000507 bool AfterColonLive = ValueLive && LHS == 0;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000508 APSInt AfterColonVal(LHS.getBitWidth());
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000509 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000510 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
511 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000512
513 // Parse anything after the : RHS that has a higher precedence than ?.
514 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec+1,
Chris Lattner86054a92007-04-10 05:26:38 +0000515 PeekTok, AfterColonLive, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000516 return true;
517
518 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000519 LHS = LHS != 0 ? RHS : AfterColonVal;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000520
521 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
522 // either operand is unsigned.
523 LHS.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Chris Lattner22eb9722006-06-18 05:43:12 +0000524
525 // Figure out the precedence of the token after the : part.
526 PeekPrec = getPrecedence(PeekTok.getKind());
527 break;
528 }
529 case tok::colon:
530 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000531 PP.Diag(OpToken, diag::err_pp_colon_without_question);
Chris Lattner22eb9722006-06-18 05:43:12 +0000532 return true;
533 }
534 }
535
536 return false;
537}
Chris Lattnere3519cc2006-07-04 18:11:39 +0000538
539/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
540/// may occur after a #if or #elif directive. If the expression is equivalent
541/// to "!defined(X)" return X in IfNDefMacro.
542bool Preprocessor::
543EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
544 // Peek ahead one token.
545 LexerToken Tok;
546 Lex(Tok);
547
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000548 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
549 unsigned BitWidth = getTargetInfo().getIntMaxTWidth(Tok.getLocation());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000550 APSInt ResVal(BitWidth);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000551 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000552 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000553 // Parse error, skip the rest of the macro line.
554 if (Tok.getKind() != tok::eom)
555 DiscardUntilEndOfDirective();
556 return false;
557 }
558
559 // If we are at the end of the expression after just parsing a value, there
560 // must be no (unparenthesized) binary operators involved, so we can exit
561 // directly.
562 if (Tok.getKind() == tok::eom) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000563 // If the expression we parsed was of the form !defined(macro), return the
564 // macro in IfNDefMacro.
565 if (DT.State == DefinedTracker::NotDefinedMacro)
566 IfNDefMacro = DT.TheMacro;
567
Chris Lattnere3519cc2006-07-04 18:11:39 +0000568 return ResVal != 0;
569 }
570
571 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
572 // operator and the stuff after it.
Chris Lattner86054a92007-04-10 05:26:38 +0000573 if (EvaluateDirectiveSubExpr(ResVal, 1, Tok, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000574 // Parse error, skip the rest of the macro line.
575 if (Tok.getKind() != tok::eom)
576 DiscardUntilEndOfDirective();
577 return false;
578 }
579
580 // If we aren't at the tok::eom token, something bad happened, like an extra
581 // ')' token.
582 if (Tok.getKind() != tok::eom) {
583 Diag(Tok, diag::err_pp_expected_eol);
584 DiscardUntilEndOfDirective();
585 }
586
587 return ResVal != 0;
588}
589