blob: ecd7a54ac91ef00c0921babc46b3a51911b066a9 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner80965422006-07-04 18:03:19 +000010// This file implements the Preprocessor::EvaluateDirectiveExpression method,
11// which parses and evaluates integer constant expressions for #if directives.
Chris Lattner22eb9722006-06-18 05:43:12 +000012//
13//===----------------------------------------------------------------------===//
14//
Chris Lattner6df79752007-04-04 06:54:19 +000015// FIXME: implement testing for #assert's.
Chris Lattnera9eac7f2007-04-05 05:24:00 +000016// FIXME: Detect and report overflow in expression (e.g. (1 << 62)*2)
Chris Lattner22eb9722006-06-18 05:43:12 +000017//
18//===----------------------------------------------------------------------===//
19
20#include "clang/Lex/Preprocessor.h"
Chris Lattnera78a97e2006-07-03 05:42:18 +000021#include "clang/Lex/MacroInfo.h"
Steve Naroff451d8f162007-03-12 23:22:38 +000022#include "clang/Lex/LiteralSupport.h"
Chris Lattner81278c62006-10-14 19:03:49 +000023#include "clang/Basic/TargetInfo.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000024#include "clang/Basic/TokenKinds.h"
25#include "clang/Basic/Diagnostic.h"
Chris Lattnera9eac7f2007-04-05 05:24:00 +000026#include "llvm/ADT/APSInt.h"
Steve Naroff451d8f162007-03-12 23:22:38 +000027#include "llvm/ADT/SmallString.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000028using namespace llvm;
29using namespace clang;
30
Chris Lattnera9eac7f2007-04-05 05:24:00 +000031static bool EvaluateDirectiveSubExpr(APSInt &LHS, unsigned MinPrec,
Chris Lattner86054a92007-04-10 05:26:38 +000032 LexerToken &PeekTok, bool ValueLive,
33 Preprocessor &PP);
Chris Lattner22eb9722006-06-18 05:43:12 +000034
Chris Lattnerb9d90f72006-07-04 18:32:03 +000035/// DefinedTracker - This struct is used while parsing expressions to keep track
36/// of whether !defined(X) has been seen.
37///
38/// With this simple scheme, we handle the basic forms:
39/// !defined(X) and !defined X
40/// but we also trivially handle (silly) stuff like:
41/// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
42struct DefinedTracker {
43 /// Each time a Value is evaluated, it returns information about whether the
44 /// parsed value is of the form defined(X), !defined(X) or is something else.
45 enum TrackerState {
46 DefinedMacro, // defined(X)
47 NotDefinedMacro, // !defined(X)
48 Unknown // Something else.
49 } State;
50 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
51 /// indicates the macro that was checked.
52 IdentifierInfo *TheMacro;
53};
54
55
Chris Lattner22eb9722006-06-18 05:43:12 +000056
57/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
58/// return the computed value in Result. Return true if there was an error
Chris Lattnerb9d90f72006-07-04 18:32:03 +000059/// parsing. This function also returns information about the form of the
60/// expression in DT. See above for information on what DT means.
Chris Lattner86054a92007-04-10 05:26:38 +000061///
62/// If ValueLive is false, then this value is being evaluated in a context where
63/// the result is not used. As such, avoid diagnostics that relate to
64/// evaluation.
Chris Lattnera9eac7f2007-04-05 05:24:00 +000065static bool EvaluateValue(APSInt &Result, LexerToken &PeekTok,
Chris Lattner86054a92007-04-10 05:26:38 +000066 DefinedTracker &DT, bool ValueLive,
67 Preprocessor &PP) {
Chris Lattner22eb9722006-06-18 05:43:12 +000068 Result = 0;
Chris Lattnerb9d90f72006-07-04 18:32:03 +000069 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +000070
71 // If this token's spelling is a pp-identifier, check to see if it is
72 // 'defined' or if it is a macro. Note that we check here because many
73 // keywords are pp-identifiers, so we can't check the kind.
Chris Lattnerb9d90f72006-07-04 18:32:03 +000074 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
Chris Lattner22eb9722006-06-18 05:43:12 +000075 // If this identifier isn't 'defined' and it wasn't macro expanded, it turns
Chris Lattnera7fa1b22007-04-10 06:16:30 +000076 // into a simple 0, unless it is the C++ keyword "true", in which case it
77 // turns into "1".
Chris Lattner2bb8a952006-11-21 22:24:17 +000078 if (II->getPPKeywordID() != tok::pp_defined) {
Chris Lattnera7fa1b22007-04-10 06:16:30 +000079 Result = II->getTokenID() == tok::kw_true;
Chris Lattnera9eac7f2007-04-05 05:24:00 +000080 Result.setIsUnsigned(false); // "0" is signed intmax_t 0.
Chris Lattnerbcb416b2006-10-27 05:43:50 +000081 PP.LexNonComment(PeekTok);
Chris Lattnercb283342006-06-18 06:48:37 +000082 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +000083 }
84
85 // Handle "defined X" and "defined(X)".
Chris Lattner22eb9722006-06-18 05:43:12 +000086
Chris Lattnere3519cc2006-07-04 18:11:39 +000087 // Get the next token, don't expand it.
88 PP.LexUnexpandedToken(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +000089
90 // Two options, it can either be a pp-identifier or a (.
91 bool InParens = false;
92 if (PeekTok.getKind() == tok::l_paren) {
93 // Found a paren, remember we saw it and skip it.
94 InParens = true;
Chris Lattnere3519cc2006-07-04 18:11:39 +000095 PP.LexUnexpandedToken(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +000096 }
97
98 // If we don't have a pp-identifier now, this is an error.
99 if ((II = PeekTok.getIdentifierInfo()) == 0) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000100 PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000101 return true;
102 }
103
104 // Otherwise, we got an identifier, is it defined to something?
105 Result = II->getMacroInfo() != 0;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000106 Result.setIsUnsigned(false); // Result is signed intmax_t.
107
Chris Lattnera78a97e2006-07-03 05:42:18 +0000108 // If there is a macro, mark it used.
Chris Lattner86054a92007-04-10 05:26:38 +0000109 if (Result != 0 && ValueLive) {
Chris Lattner81278c62006-10-14 19:03:49 +0000110 II->getMacroInfo()->setIsUsed(true);
111
112 // If this is the first use of a target-specific macro, warn about it.
113 if (II->getMacroInfo()->isTargetSpecific()) {
114 // Don't warn on second use.
115 II->getMacroInfo()->setIsTargetSpecific(false);
116 PP.getTargetInfo().DiagnoseNonPortability(PeekTok.getLocation(),
117 diag::port_target_macro_use);
118 }
Chris Lattner86054a92007-04-10 05:26:38 +0000119 } else if (ValueLive) {
Chris Lattner063400e2006-10-14 19:54:15 +0000120 // Use of a target-specific macro for some other target? If so, warn.
121 if (II->isOtherTargetMacro()) {
122 II->setIsOtherTargetMacro(false); // Don't warn on second use.
123 PP.getTargetInfo().DiagnoseNonPortability(PeekTok.getLocation(),
124 diag::port_target_macro_use);
125 }
Chris Lattner81278c62006-10-14 19:03:49 +0000126 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000127
128 // Consume identifier.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000129 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000130
131 // If we are in parens, ensure we have a trailing ).
132 if (InParens) {
133 if (PeekTok.getKind() != tok::r_paren) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000134 PP.Diag(PeekTok, diag::err_pp_missing_rparen);
Chris Lattner22eb9722006-06-18 05:43:12 +0000135 return true;
136 }
137 // Consume the ).
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000138 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000139 }
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000140
141 // Success, remember that we saw defined(X).
142 DT.State = DefinedTracker::DefinedMacro;
143 DT.TheMacro = II;
Chris Lattner22eb9722006-06-18 05:43:12 +0000144 return false;
145 }
146
147 switch (PeekTok.getKind()) {
148 default: // Non-value token.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000149 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
Chris Lattner22eb9722006-06-18 05:43:12 +0000150 return true;
151 case tok::eom:
152 case tok::r_paren:
153 // If there is no expression, report and exit.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000154 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000155 return true;
156 case tok::numeric_constant: {
Chris Lattner6df79752007-04-04 06:54:19 +0000157 SmallString<64> IntegerBuffer;
Steve Naroff451d8f162007-03-12 23:22:38 +0000158 IntegerBuffer.resize(PeekTok.getLength());
159 const char *ThisTokBegin = &IntegerBuffer[0];
160 unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
161 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
162 PeekTok.getLocation(), PP);
Chris Lattner6df79752007-04-04 06:54:19 +0000163 if (Literal.hadError)
Steve Narofff2fb89e2007-03-13 20:29:44 +0000164 return true; // a diagnostic was already reported.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000165
166 if (Literal.isFloatingLiteral()) {
Steve Naroff451d8f162007-03-12 23:22:38 +0000167 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000168 return true;
Steve Naroff451d8f162007-03-12 23:22:38 +0000169 }
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000170 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000171
172 // Parse the integer literal into Result.
173 if (Literal.GetIntegerValue(Result)) {
174 // Overflow parsing integer literal.
Chris Lattner86054a92007-04-10 05:26:38 +0000175 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000176 Result.setIsUnsigned(true);
177 } else {
178 // Set the signedness of the result to match whether there was a U suffix
179 // or not.
180 Result.setIsUnsigned(Literal.isUnsigned);
181
182 // Detect overflow based on whether the value is signed. If signed
183 // and if the value is too large, emit a warning "integer constant is so
184 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
185 // is 64-bits.
186 if (!Literal.isUnsigned && Result.isNegative()) {
Chris Lattner86054a92007-04-10 05:26:38 +0000187 if (ValueLive)PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000188 Result.setIsUnsigned(true);
189 }
190 }
191
192 // Consume the token.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000193 PP.LexNonComment(PeekTok);
194 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000195 }
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000196 case tok::char_constant: { // 'x'
197 SmallString<32> CharBuffer;
198 CharBuffer.resize(PeekTok.getLength());
199 const char *ThisTokBegin = &CharBuffer[0];
200 unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
201 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
202 PeekTok.getLocation(), PP);
203 if (Literal.hadError())
204 return true; // A diagnostic was already emitted.
205
206 // Character literals are always int or wchar_t, expand to intmax_t.
207 TargetInfo &TI = PP.getTargetInfo();
208 unsigned NumBits;
209 if (Literal.isWide())
210 NumBits = TI.getWCharWidth(PeekTok.getLocation());
211 else
212 NumBits = TI.getCharWidth(PeekTok.getLocation());
213
214 // Set the width.
215 APSInt Val(NumBits);
216 // Set the value.
217 Val = Literal.getValue();
218 // Set the signedness.
219 Val.setIsUnsigned(!TI.isCharSigned(PeekTok.getLocation()));
220
221 if (Result.getBitWidth() > Val.getBitWidth()) {
222 if (Val.isSigned())
223 Result = Val.sext(Result.getBitWidth());
224 else
225 Result = Val.zext(Result.getBitWidth());
226 Result.setIsUnsigned(Val.isUnsigned());
227 } else {
228 assert(Result.getBitWidth() == Val.getBitWidth() &&
229 "intmax_t smaller than char/wchar_t?");
230 Result = Val;
231 }
232
233 // Consume the token.
234 PP.LexNonComment(PeekTok);
235 return false;
236 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000237 case tok::l_paren:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000238 PP.LexNonComment(PeekTok); // Eat the (.
Chris Lattnercb283342006-06-18 06:48:37 +0000239 // Parse the value and if there are any binary operators involved, parse
240 // them.
Chris Lattner86054a92007-04-10 05:26:38 +0000241 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000242
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000243 // If this is a silly value like (X), which doesn't need parens, check for
244 // !(defined X).
245 if (PeekTok.getKind() == tok::r_paren) {
246 // Just use DT unmodified as our result.
247 } else {
Chris Lattner86054a92007-04-10 05:26:38 +0000248 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
249 return true;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000250
251 if (PeekTok.getKind() != tok::r_paren) {
252 PP.Diag(PeekTok, diag::err_pp_expected_rparen);
253 return true;
254 }
255 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000256 }
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000257 PP.LexNonComment(PeekTok); // Eat the ).
Chris Lattner22eb9722006-06-18 05:43:12 +0000258 return false;
259
260 case tok::plus:
261 // Unary plus doesn't modify the value.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000262 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000263 return EvaluateValue(Result, PeekTok, DT, ValueLive, PP);
Chris Lattner22eb9722006-06-18 05:43:12 +0000264 case tok::minus:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000265 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000266 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000267 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner22eb9722006-06-18 05:43:12 +0000268 Result = -Result;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000269 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000270 return false;
271
272 case tok::tilde:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000273 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000274 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000275 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner22eb9722006-06-18 05:43:12 +0000276 Result = ~Result;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000277 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000278 return false;
279
280 case tok::exclaim:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000281 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000282 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000283 Result = !Result;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000284 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
285 Result.setIsUnsigned(false);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000286
287 if (DT.State == DefinedTracker::DefinedMacro)
288 DT.State = DefinedTracker::NotDefinedMacro;
289 else if (DT.State == DefinedTracker::NotDefinedMacro)
290 DT.State = DefinedTracker::DefinedMacro;
Chris Lattner22eb9722006-06-18 05:43:12 +0000291 return false;
292
293 // FIXME: Handle #assert
294 }
295}
296
297
298
299/// getPrecedence - Return the precedence of the specified binary operator
300/// token. This returns:
301/// ~0 - Invalid token.
Chris Lattner9916c5c2006-10-27 05:24:37 +0000302/// 14 - *,/,%
303/// 13 - -,+
304/// 12 - <<,>>
305/// 11 - >=, <=, >, <
306/// 10 - ==, !=
Chris Lattner22eb9722006-06-18 05:43:12 +0000307/// 9 - &
308/// 8 - ^
309/// 7 - |
310/// 6 - &&
311/// 5 - ||
312/// 4 - ?
313/// 3 - :
314/// 0 - eom, )
315static unsigned getPrecedence(tok::TokenKind Kind) {
316 switch (Kind) {
317 default: return ~0U;
318 case tok::percent:
319 case tok::slash:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000320 case tok::star: return 14;
Chris Lattner22eb9722006-06-18 05:43:12 +0000321 case tok::plus:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000322 case tok::minus: return 13;
Chris Lattner22eb9722006-06-18 05:43:12 +0000323 case tok::lessless:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000324 case tok::greatergreater: return 12;
Chris Lattner22eb9722006-06-18 05:43:12 +0000325 case tok::lessequal:
326 case tok::less:
327 case tok::greaterequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000328 case tok::greater: return 11;
Chris Lattner22eb9722006-06-18 05:43:12 +0000329 case tok::exclaimequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000330 case tok::equalequal: return 10;
Chris Lattner22eb9722006-06-18 05:43:12 +0000331 case tok::amp: return 9;
332 case tok::caret: return 8;
333 case tok::pipe: return 7;
334 case tok::ampamp: return 6;
335 case tok::pipepipe: return 5;
336 case tok::question: return 4;
337 case tok::colon: return 3;
338 case tok::comma: return 2;
339 case tok::r_paren: return 0; // Lowest priority, end of expr.
340 case tok::eom: return 0; // Lowest priority, end of macro.
341 }
342}
343
344
345/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
346/// PeekTok, and whose precedence is PeekPrec.
Chris Lattner86054a92007-04-10 05:26:38 +0000347///
348/// If ValueLive is false, then this value is being evaluated in a context where
349/// the result is not used. As such, avoid diagnostics that relate to
350/// evaluation.
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000351static bool EvaluateDirectiveSubExpr(APSInt &LHS, unsigned MinPrec,
Chris Lattner86054a92007-04-10 05:26:38 +0000352 LexerToken &PeekTok, bool ValueLive,
353 Preprocessor &PP) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000354 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
355 // If this token isn't valid, report the error.
356 if (PeekPrec == ~0U) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000357 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
Chris Lattner22eb9722006-06-18 05:43:12 +0000358 return true;
359 }
360
361 while (1) {
362 // If this token has a lower precedence than we are allowed to parse, return
363 // it so that higher levels of the recursion can parse it.
364 if (PeekPrec < MinPrec)
365 return false;
366
367 tok::TokenKind Operator = PeekTok.getKind();
Chris Lattner86054a92007-04-10 05:26:38 +0000368
369 // If this is a short-circuiting operator, see if the RHS of the operator is
370 // dead. Note that this cannot just clobber ValueLive. Consider
371 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
372 // this example, the RHS of the && being dead does not make the rest of the
373 // expr dead.
374 bool RHSIsLive;
375 if (Operator == tok::ampamp && LHS == 0)
376 RHSIsLive = false; // RHS of "0 && x" is dead.
377 else if (Operator == tok::pipepipe && LHS != 0)
378 RHSIsLive = false; // RHS of "1 || x" is dead.
379 else if (Operator == tok::question && LHS == 0)
380 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
381 else
382 RHSIsLive = ValueLive;
Chris Lattner22eb9722006-06-18 05:43:12 +0000383
384 // Consume the operator, saving the operator token for error reporting.
385 LexerToken OpToken = PeekTok;
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000386 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000387
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000388 APSInt RHS(LHS.getBitWidth());
Chris Lattner22eb9722006-06-18 05:43:12 +0000389 // Parse the RHS of the operator.
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000390 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000391 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000392
393 // Remember the precedence of this operator and get the precedence of the
394 // operator immediately to the right of the RHS.
395 unsigned ThisPrec = PeekPrec;
396 PeekPrec = getPrecedence(PeekTok.getKind());
397
398 // If this token isn't valid, report the error.
399 if (PeekPrec == ~0U) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000400 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
Chris Lattner22eb9722006-06-18 05:43:12 +0000401 return true;
402 }
403
404 bool isRightAssoc = Operator == tok::question;
405
406 // Get the precedence of the operator to the right of the RHS. If it binds
407 // more tightly with RHS than we do, evaluate it completely first.
408 if (ThisPrec < PeekPrec ||
409 (ThisPrec == PeekPrec && isRightAssoc)) {
Chris Lattner86054a92007-04-10 05:26:38 +0000410 if (EvaluateDirectiveSubExpr(RHS, ThisPrec+1, PeekTok, RHSIsLive, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000411 return true;
412 PeekPrec = getPrecedence(PeekTok.getKind());
413 }
414 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
415
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000416 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
417 // either operand is unsigned. Don't do this for x and y in "x ? y : z".
418 if (Operator != tok::question) {
419 if (RHS.isUnsigned()) LHS.setIsUnsigned(true);
420 RHS.setIsUnsigned(LHS.isUnsigned());
421 }
422
423 // FIXME: All of these should detect and report overflow??
Chris Lattner5a0f1642007-04-10 06:54:33 +0000424 bool Overflow = false;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000425 APSInt Res(LHS.getBitWidth());
Chris Lattner22eb9722006-06-18 05:43:12 +0000426 switch (Operator) {
427 default: assert(0 && "Unknown operator token!");
428 case tok::percent:
429 if (RHS == 0) {
Chris Lattner86054a92007-04-10 05:26:38 +0000430 if (ValueLive) PP.Diag(OpToken, diag::err_pp_remainder_by_zero);
Chris Lattner22eb9722006-06-18 05:43:12 +0000431 return true;
432 }
Chris Lattner9cc755d2007-04-10 07:07:11 +0000433 Res = LHS % RHS;
Chris Lattner22eb9722006-06-18 05:43:12 +0000434 break;
435 case tok::slash:
436 if (RHS == 0) {
Chris Lattner86054a92007-04-10 05:26:38 +0000437 if (ValueLive) PP.Diag(OpToken, diag::err_pp_division_by_zero);
Chris Lattner22eb9722006-06-18 05:43:12 +0000438 return true;
439 }
Chris Lattner9cc755d2007-04-10 07:07:11 +0000440 Res = LHS / RHS;
Chris Lattner22eb9722006-06-18 05:43:12 +0000441 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000442 case tok::star:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000443 Res = LHS * RHS;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000444 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000445 case tok::lessless: {
446 // Determine whether overflow is about to happen.
447 unsigned ShAmt = RHS.getLimitedValue();
448 if (ShAmt >= LHS.getBitWidth())
449 Overflow = true, ShAmt = LHS.getBitWidth()-1;
450 else if (LHS.isUnsigned())
451 Overflow = ShAmt > LHS.countLeadingZeros();
452 else if (LHS.isPositive())
453 Overflow = ShAmt >= LHS.countLeadingZeros(); // Don't allow sign change.
454 else
455 Overflow = ShAmt >= LHS.countLeadingOnes();
456
Chris Lattner9cc755d2007-04-10 07:07:11 +0000457 Res = LHS << ShAmt;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000458 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000459 }
460 case tok::greatergreater: {
461 // Determine whether overflow is about to happen.
462 unsigned ShAmt = RHS.getLimitedValue();
463 if (ShAmt >= LHS.getBitWidth())
464 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000465 Res = LHS >> ShAmt;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000466 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000467 }
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000468 case tok::plus:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000469 Res = LHS + RHS;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000470 break;
471 case tok::minus:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000472 Res = LHS - RHS;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000473 break;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000474 case tok::lessequal:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000475 Res = LHS <= RHS;
476 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000477 break;
478 case tok::less:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000479 Res = LHS < RHS;
480 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000481 break;
482 case tok::greaterequal:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000483 Res = LHS >= RHS;
484 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000485 break;
486 case tok::greater:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000487 Res = LHS > RHS;
488 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000489 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000490 case tok::exclaimequal:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000491 Res = LHS != RHS;
492 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000493 break;
494 case tok::equalequal:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000495 Res = LHS == RHS;
496 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000497 break;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000498 case tok::amp:
499 Res = LHS & RHS;
500 break;
501 case tok::caret:
502 Res = LHS ^ RHS;
503 break;
504 case tok::pipe:
505 Res = LHS | RHS;
506 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000507 case tok::ampamp:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000508 Res = (LHS != 0 && RHS != 0);
509 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000510 break;
511 case tok::pipepipe:
Chris Lattner9cc755d2007-04-10 07:07:11 +0000512 Res = (LHS != 0 || RHS != 0);
513 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000514 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000515 case tok::comma:
Chris Lattnere3519cc2006-07-04 18:11:39 +0000516 PP.Diag(OpToken, diag::ext_pp_comma_expr);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000517 Res = RHS; // LHS = LHS,RHS -> RHS.
Chris Lattner22eb9722006-06-18 05:43:12 +0000518 break;
519 case tok::question: {
520 // Parse the : part of the expression.
521 if (PeekTok.getKind() != tok::colon) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000522 PP.Diag(OpToken, diag::err_pp_question_without_colon);
Chris Lattner22eb9722006-06-18 05:43:12 +0000523 return true;
524 }
525 // Consume the :.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000526 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000527
528 // Evaluate the value after the :.
Chris Lattner86054a92007-04-10 05:26:38 +0000529 bool AfterColonLive = ValueLive && LHS == 0;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000530 APSInt AfterColonVal(LHS.getBitWidth());
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000531 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000532 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
533 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000534
535 // Parse anything after the : RHS that has a higher precedence than ?.
536 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec+1,
Chris Lattner86054a92007-04-10 05:26:38 +0000537 PeekTok, AfterColonLive, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000538 return true;
539
540 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner9cc755d2007-04-10 07:07:11 +0000541 Res = LHS != 0 ? RHS : AfterColonVal;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000542
543 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
544 // either operand is unsigned.
Chris Lattner9cc755d2007-04-10 07:07:11 +0000545 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Chris Lattner22eb9722006-06-18 05:43:12 +0000546
547 // Figure out the precedence of the token after the : part.
548 PeekPrec = getPrecedence(PeekTok.getKind());
549 break;
550 }
551 case tok::colon:
552 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000553 PP.Diag(OpToken, diag::err_pp_colon_without_question);
Chris Lattner22eb9722006-06-18 05:43:12 +0000554 return true;
555 }
Chris Lattner5a0f1642007-04-10 06:54:33 +0000556
557 // If this operator is live and overflowed, report the issue.
558 if (Overflow && ValueLive)
559 PP.Diag(OpToken, diag::warn_pp_expr_overflow);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000560
561 // Put the result back into 'LHS' for our next iteration.
562 LHS = Res;
Chris Lattner22eb9722006-06-18 05:43:12 +0000563 }
564
565 return false;
566}
Chris Lattnere3519cc2006-07-04 18:11:39 +0000567
568/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
569/// may occur after a #if or #elif directive. If the expression is equivalent
570/// to "!defined(X)" return X in IfNDefMacro.
571bool Preprocessor::
572EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
573 // Peek ahead one token.
574 LexerToken Tok;
575 Lex(Tok);
576
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000577 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
578 unsigned BitWidth = getTargetInfo().getIntMaxTWidth(Tok.getLocation());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000579 APSInt ResVal(BitWidth);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000580 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000581 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000582 // Parse error, skip the rest of the macro line.
583 if (Tok.getKind() != tok::eom)
584 DiscardUntilEndOfDirective();
585 return false;
586 }
587
588 // If we are at the end of the expression after just parsing a value, there
589 // must be no (unparenthesized) binary operators involved, so we can exit
590 // directly.
591 if (Tok.getKind() == tok::eom) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000592 // If the expression we parsed was of the form !defined(macro), return the
593 // macro in IfNDefMacro.
594 if (DT.State == DefinedTracker::NotDefinedMacro)
595 IfNDefMacro = DT.TheMacro;
596
Chris Lattnere3519cc2006-07-04 18:11:39 +0000597 return ResVal != 0;
598 }
599
600 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
601 // operator and the stuff after it.
Chris Lattner86054a92007-04-10 05:26:38 +0000602 if (EvaluateDirectiveSubExpr(ResVal, 1, Tok, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000603 // Parse error, skip the rest of the macro line.
604 if (Tok.getKind() != tok::eom)
605 DiscardUntilEndOfDirective();
606 return false;
607 }
608
609 // If we aren't at the tok::eom token, something bad happened, like an extra
610 // ')' token.
611 if (Tok.getKind() != tok::eom) {
612 Diag(Tok, diag::err_pp_expected_eol);
613 DiscardUntilEndOfDirective();
614 }
615
616 return ResVal != 0;
617}
618