blob: 48ec52af8cbf4590a3d933862f81ab9d506598bd [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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//
10// This file implements the Preprocessor::EvaluateDirectiveExpression method,
11// which parses and evaluates integer constant expressions for #if directives.
12//
13//===----------------------------------------------------------------------===//
14//
15// FIXME: implement testing for #assert's.
16//
17//===----------------------------------------------------------------------===//
18
19#include "clang/Lex/Preprocessor.h"
20#include "clang/Lex/MacroInfo.h"
21#include "clang/Lex/LiteralSupport.h"
22#include "clang/Basic/TargetInfo.h"
23#include "clang/Basic/TokenKinds.h"
24#include "clang/Basic/Diagnostic.h"
25#include "llvm/ADT/APSInt.h"
26#include "llvm/ADT/SmallString.h"
27using namespace clang;
28
29static bool EvaluateDirectiveSubExpr(llvm::APSInt &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +000030 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +000031 Preprocessor &PP);
32
33/// 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
54
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
57/// parsing. This function also returns information about the form of the
58/// expression in DT. See above for information on what DT means.
59///
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 Lattnerd2177732007-07-20 16:59:19 +000063static bool EvaluateValue(llvm::APSInt &Result, Token &PeekTok,
Reid Spencer5f016e22007-07-11 17:01:13 +000064 DefinedTracker &DT, bool ValueLive,
65 Preprocessor &PP) {
66 Result = 0;
67 DT.State = DefinedTracker::Unknown;
68
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.
72 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
73 // If this identifier isn't 'defined' and it wasn't macro expanded, it turns
74 // into a simple 0, unless it is the C++ keyword "true", in which case it
75 // turns into "1".
76 if (II->getPPKeywordID() != tok::pp_defined) {
77 Result = II->getTokenID() == tok::kw_true;
78 Result.setIsUnsigned(false); // "0" is signed intmax_t 0.
79 PP.LexNonComment(PeekTok);
80 return false;
81 }
82
83 // Handle "defined X" and "defined(X)".
84
85 // Get the next token, don't expand it.
86 PP.LexUnexpandedToken(PeekTok);
87
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;
93 PP.LexUnexpandedToken(PeekTok);
94 }
95
96 // If we don't have a pp-identifier now, this is an error.
97 if ((II = PeekTok.getIdentifierInfo()) == 0) {
98 PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier);
99 return true;
100 }
101
102 // Otherwise, we got an identifier, is it defined to something?
103 Result = II->getMacroInfo() != 0;
104 Result.setIsUnsigned(false); // Result is signed intmax_t.
105
106 // If there is a macro, mark it used.
107 if (Result != 0 && ValueLive) {
108 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 }
117 } else if (ValueLive) {
118 // 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 }
124 }
125
126 // Consume identifier.
127 PP.LexNonComment(PeekTok);
128
129 // If we are in parens, ensure we have a trailing ).
130 if (InParens) {
131 if (PeekTok.getKind() != tok::r_paren) {
132 PP.Diag(PeekTok, diag::err_pp_missing_rparen);
133 return true;
134 }
135 // Consume the ).
136 PP.LexNonComment(PeekTok);
137 }
138
139 // Success, remember that we saw defined(X).
140 DT.State = DefinedTracker::DefinedMacro;
141 DT.TheMacro = II;
142 return false;
143 }
144
145 switch (PeekTok.getKind()) {
146 default: // Non-value token.
147 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
148 return true;
149 case tok::eom:
150 case tok::r_paren:
151 // If there is no expression, report and exit.
152 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
153 return true;
154 case tok::numeric_constant: {
155 llvm::SmallString<64> IntegerBuffer;
156 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);
161 if (Literal.hadError)
162 return true; // a diagnostic was already reported.
163
Chris Lattner6e400c22007-08-26 03:29:23 +0000164 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
166 return true;
167 }
168 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
169
Neil Boothb9449512007-08-29 22:00:19 +0000170 // long long is a C99 feature.
171 if (!PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus0x
Neil Booth79859c32007-08-29 22:13:52 +0000172 && Literal.isLongLong)
Neil Boothb9449512007-08-29 22:00:19 +0000173 PP.Diag(PeekTok, diag::ext_longlong);
174
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 // Parse the integer literal into Result.
176 if (Literal.GetIntegerValue(Result)) {
177 // Overflow parsing integer literal.
178 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
179 Result.setIsUnsigned(true);
180 } else {
181 // Set the signedness of the result to match whether there was a U suffix
182 // or not.
183 Result.setIsUnsigned(Literal.isUnsigned);
184
185 // Detect overflow based on whether the value is signed. If signed
186 // and if the value is too large, emit a warning "integer constant is so
187 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
188 // is 64-bits.
189 if (!Literal.isUnsigned && Result.isNegative()) {
190 if (ValueLive)PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
191 Result.setIsUnsigned(true);
192 }
193 }
194
195 // Consume the token.
196 PP.LexNonComment(PeekTok);
197 return false;
198 }
199 case tok::char_constant: { // 'x'
200 llvm::SmallString<32> CharBuffer;
201 CharBuffer.resize(PeekTok.getLength());
202 const char *ThisTokBegin = &CharBuffer[0];
203 unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
204 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
205 PeekTok.getLocation(), PP);
206 if (Literal.hadError())
207 return true; // A diagnostic was already emitted.
208
209 // Character literals are always int or wchar_t, expand to intmax_t.
210 TargetInfo &TI = PP.getTargetInfo();
211 unsigned NumBits;
212 if (Literal.isWide())
213 NumBits = TI.getWCharWidth(PeekTok.getLocation());
214 else
215 NumBits = TI.getCharWidth(PeekTok.getLocation());
216
217 // Set the width.
218 llvm::APSInt Val(NumBits);
219 // Set the value.
220 Val = Literal.getValue();
221 // Set the signedness.
222 Val.setIsUnsigned(!TI.isCharSigned(PeekTok.getLocation()));
223
224 if (Result.getBitWidth() > Val.getBitWidth()) {
225 if (Val.isSigned())
226 Result = Val.sext(Result.getBitWidth());
227 else
228 Result = Val.zext(Result.getBitWidth());
229 Result.setIsUnsigned(Val.isUnsigned());
230 } else {
231 assert(Result.getBitWidth() == Val.getBitWidth() &&
232 "intmax_t smaller than char/wchar_t?");
233 Result = Val;
234 }
235
236 // Consume the token.
237 PP.LexNonComment(PeekTok);
238 return false;
239 }
240 case tok::l_paren:
241 PP.LexNonComment(PeekTok); // Eat the (.
242 // Parse the value and if there are any binary operators involved, parse
243 // them.
244 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
245
246 // If this is a silly value like (X), which doesn't need parens, check for
247 // !(defined X).
248 if (PeekTok.getKind() == tok::r_paren) {
249 // Just use DT unmodified as our result.
250 } else {
251 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
252 return true;
253
254 if (PeekTok.getKind() != tok::r_paren) {
255 PP.Diag(PeekTok, diag::err_pp_expected_rparen);
256 return true;
257 }
258 DT.State = DefinedTracker::Unknown;
259 }
260 PP.LexNonComment(PeekTok); // Eat the ).
261 return false;
262
263 case tok::plus:
264 // Unary plus doesn't modify the value.
265 PP.LexNonComment(PeekTok);
266 return EvaluateValue(Result, PeekTok, DT, ValueLive, PP);
267 case tok::minus: {
268 SourceLocation Loc = PeekTok.getLocation();
269 PP.LexNonComment(PeekTok);
270 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
271 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
272 Result = -Result;
273
274 bool Overflow = false;
275 if (Result.isUnsigned())
276 Overflow = !Result.isPositive();
277 else if (Result.isMinSignedValue())
278 Overflow = true; // -MININT is the only thing that overflows.
279
280 // If this operator is live and overflowed, report the issue.
281 if (Overflow && ValueLive)
282 PP.Diag(Loc, diag::warn_pp_expr_overflow);
283
284 DT.State = DefinedTracker::Unknown;
285 return false;
286 }
287
288 case tok::tilde:
289 PP.LexNonComment(PeekTok);
290 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
291 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
292 Result = ~Result;
293 DT.State = DefinedTracker::Unknown;
294 return false;
295
296 case tok::exclaim:
297 PP.LexNonComment(PeekTok);
298 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
299 Result = !Result;
300 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
301 Result.setIsUnsigned(false);
302
303 if (DT.State == DefinedTracker::DefinedMacro)
304 DT.State = DefinedTracker::NotDefinedMacro;
305 else if (DT.State == DefinedTracker::NotDefinedMacro)
306 DT.State = DefinedTracker::DefinedMacro;
307 return false;
308
309 // FIXME: Handle #assert
310 }
311}
312
313
314
315/// getPrecedence - Return the precedence of the specified binary operator
316/// token. This returns:
317/// ~0 - Invalid token.
318/// 14 - *,/,%
319/// 13 - -,+
320/// 12 - <<,>>
321/// 11 - >=, <=, >, <
322/// 10 - ==, !=
323/// 9 - &
324/// 8 - ^
325/// 7 - |
326/// 6 - &&
327/// 5 - ||
328/// 4 - ?
329/// 3 - :
330/// 0 - eom, )
331static unsigned getPrecedence(tok::TokenKind Kind) {
332 switch (Kind) {
333 default: return ~0U;
334 case tok::percent:
335 case tok::slash:
336 case tok::star: return 14;
337 case tok::plus:
338 case tok::minus: return 13;
339 case tok::lessless:
340 case tok::greatergreater: return 12;
341 case tok::lessequal:
342 case tok::less:
343 case tok::greaterequal:
344 case tok::greater: return 11;
345 case tok::exclaimequal:
346 case tok::equalequal: return 10;
347 case tok::amp: return 9;
348 case tok::caret: return 8;
349 case tok::pipe: return 7;
350 case tok::ampamp: return 6;
351 case tok::pipepipe: return 5;
352 case tok::question: return 4;
353 case tok::colon: return 3;
354 case tok::comma: return 2;
355 case tok::r_paren: return 0; // Lowest priority, end of expr.
356 case tok::eom: return 0; // Lowest priority, end of macro.
357 }
358}
359
360
361/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
362/// PeekTok, and whose precedence is PeekPrec.
363///
364/// If ValueLive is false, then this value is being evaluated in a context where
365/// the result is not used. As such, avoid diagnostics that relate to
366/// evaluation.
367static bool EvaluateDirectiveSubExpr(llvm::APSInt &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +0000368 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 Preprocessor &PP) {
370 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
371 // If this token isn't valid, report the error.
372 if (PeekPrec == ~0U) {
373 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
374 return true;
375 }
376
377 while (1) {
378 // If this token has a lower precedence than we are allowed to parse, return
379 // it so that higher levels of the recursion can parse it.
380 if (PeekPrec < MinPrec)
381 return false;
382
383 tok::TokenKind Operator = PeekTok.getKind();
384
385 // If this is a short-circuiting operator, see if the RHS of the operator is
386 // dead. Note that this cannot just clobber ValueLive. Consider
387 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
388 // this example, the RHS of the && being dead does not make the rest of the
389 // expr dead.
390 bool RHSIsLive;
391 if (Operator == tok::ampamp && LHS == 0)
392 RHSIsLive = false; // RHS of "0 && x" is dead.
393 else if (Operator == tok::pipepipe && LHS != 0)
394 RHSIsLive = false; // RHS of "1 || x" is dead.
395 else if (Operator == tok::question && LHS == 0)
396 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
397 else
398 RHSIsLive = ValueLive;
399
400 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000401 Token OpToken = PeekTok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 PP.LexNonComment(PeekTok);
403
404 llvm::APSInt RHS(LHS.getBitWidth());
405 // Parse the RHS of the operator.
406 DefinedTracker DT;
407 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
408
409 // Remember the precedence of this operator and get the precedence of the
410 // operator immediately to the right of the RHS.
411 unsigned ThisPrec = PeekPrec;
412 PeekPrec = getPrecedence(PeekTok.getKind());
413
414 // If this token isn't valid, report the error.
415 if (PeekPrec == ~0U) {
416 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
417 return true;
418 }
419
420 bool isRightAssoc = Operator == tok::question;
421
422 // Get the precedence of the operator to the right of the RHS. If it binds
423 // more tightly with RHS than we do, evaluate it completely first.
424 if (ThisPrec < PeekPrec ||
425 (ThisPrec == PeekPrec && isRightAssoc)) {
426 if (EvaluateDirectiveSubExpr(RHS, ThisPrec+1, PeekTok, RHSIsLive, PP))
427 return true;
428 PeekPrec = getPrecedence(PeekTok.getKind());
429 }
430 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
431
432 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
433 // either operand is unsigned. Don't do this for x and y in "x ? y : z".
434 llvm::APSInt Res(LHS.getBitWidth());
435 if (Operator != tok::question) {
436 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
437 // If this just promoted something from signed to unsigned, and if the
438 // value was negative, warn about it.
439 if (ValueLive && Res.isUnsigned()) {
440 if (!LHS.isUnsigned() && LHS.isNegative())
441 PP.Diag(OpToken, diag::warn_pp_convert_lhs_to_positive,
Chris Lattnerb2024b22007-08-23 05:22:10 +0000442 LHS.toStringSigned() + " to " + LHS.toStringUnsigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 if (!RHS.isUnsigned() && RHS.isNegative())
444 PP.Diag(OpToken, diag::warn_pp_convert_rhs_to_positive,
Chris Lattnerb2024b22007-08-23 05:22:10 +0000445 RHS.toStringSigned() + " to " + RHS.toStringUnsigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000446 }
447 LHS.setIsUnsigned(Res.isUnsigned());
448 RHS.setIsUnsigned(Res.isUnsigned());
449 }
450
451 // FIXME: All of these should detect and report overflow??
452 bool Overflow = false;
453 switch (Operator) {
454 default: assert(0 && "Unknown operator token!");
455 case tok::percent:
456 if (RHS == 0) {
457 if (ValueLive) PP.Diag(OpToken, diag::err_pp_remainder_by_zero);
458 return true;
459 }
460 Res = LHS % RHS;
461 break;
462 case tok::slash:
463 if (RHS == 0) {
464 if (ValueLive) PP.Diag(OpToken, diag::err_pp_division_by_zero);
465 return true;
466 }
467 Res = LHS / RHS;
468 if (LHS.isSigned())
469 Overflow = LHS.isMinSignedValue() && RHS.isAllOnesValue(); // MININT/-1
470 break;
471 case tok::star:
472 Res = LHS * RHS;
473 if (LHS != 0 && RHS != 0)
474 Overflow = Res/RHS != LHS || Res/LHS != RHS;
475 break;
476 case tok::lessless: {
477 // Determine whether overflow is about to happen.
478 unsigned ShAmt = RHS.getLimitedValue();
479 if (ShAmt >= LHS.getBitWidth())
480 Overflow = true, ShAmt = LHS.getBitWidth()-1;
481 else if (LHS.isUnsigned())
482 Overflow = ShAmt > LHS.countLeadingZeros();
483 else if (LHS.isPositive())
484 Overflow = ShAmt >= LHS.countLeadingZeros(); // Don't allow sign change.
485 else
486 Overflow = ShAmt >= LHS.countLeadingOnes();
487
488 Res = LHS << ShAmt;
489 break;
490 }
491 case tok::greatergreater: {
492 // Determine whether overflow is about to happen.
493 unsigned ShAmt = RHS.getLimitedValue();
494 if (ShAmt >= LHS.getBitWidth())
495 Overflow = true, ShAmt = LHS.getBitWidth()-1;
496 Res = LHS >> ShAmt;
497 break;
498 }
499 case tok::plus:
500 Res = LHS + RHS;
501 if (LHS.isUnsigned())
502 Overflow = Res.ult(LHS);
503 else if (LHS.isPositive() == RHS.isPositive() &&
504 Res.isPositive() != LHS.isPositive())
505 Overflow = true; // Overflow for signed addition.
506 break;
507 case tok::minus:
508 Res = LHS - RHS;
509 if (LHS.isUnsigned())
510 Overflow = Res.ugt(LHS);
511 else if (LHS.isPositive() != RHS.isPositive() &&
512 Res.isPositive() != LHS.isPositive())
513 Overflow = true; // Overflow for signed subtraction.
514 break;
515 case tok::lessequal:
516 Res = LHS <= RHS;
517 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
518 break;
519 case tok::less:
520 Res = LHS < RHS;
521 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
522 break;
523 case tok::greaterequal:
524 Res = LHS >= RHS;
525 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
526 break;
527 case tok::greater:
528 Res = LHS > RHS;
529 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
530 break;
531 case tok::exclaimequal:
532 Res = LHS != RHS;
533 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
534 break;
535 case tok::equalequal:
536 Res = LHS == RHS;
537 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
538 break;
539 case tok::amp:
540 Res = LHS & RHS;
541 break;
542 case tok::caret:
543 Res = LHS ^ RHS;
544 break;
545 case tok::pipe:
546 Res = LHS | RHS;
547 break;
548 case tok::ampamp:
549 Res = (LHS != 0 && RHS != 0);
550 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
551 break;
552 case tok::pipepipe:
553 Res = (LHS != 0 || RHS != 0);
554 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
555 break;
556 case tok::comma:
557 PP.Diag(OpToken, diag::ext_pp_comma_expr);
558 Res = RHS; // LHS = LHS,RHS -> RHS.
559 break;
560 case tok::question: {
561 // Parse the : part of the expression.
562 if (PeekTok.getKind() != tok::colon) {
563 PP.Diag(OpToken, diag::err_pp_question_without_colon);
564 return true;
565 }
566 // Consume the :.
567 PP.LexNonComment(PeekTok);
568
569 // Evaluate the value after the :.
570 bool AfterColonLive = ValueLive && LHS == 0;
571 llvm::APSInt AfterColonVal(LHS.getBitWidth());
572 DefinedTracker DT;
573 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
574 return true;
575
576 // Parse anything after the : RHS that has a higher precedence than ?.
577 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec+1,
578 PeekTok, AfterColonLive, PP))
579 return true;
580
581 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
582 Res = LHS != 0 ? RHS : AfterColonVal;
583
584 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
585 // either operand is unsigned.
586 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
587
588 // Figure out the precedence of the token after the : part.
589 PeekPrec = getPrecedence(PeekTok.getKind());
590 break;
591 }
592 case tok::colon:
593 // Don't allow :'s to float around without being part of ?: exprs.
594 PP.Diag(OpToken, diag::err_pp_colon_without_question);
595 return true;
596 }
597
598 // If this operator is live and overflowed, report the issue.
599 if (Overflow && ValueLive)
600 PP.Diag(OpToken, diag::warn_pp_expr_overflow);
601
602 // Put the result back into 'LHS' for our next iteration.
603 LHS = Res;
604 }
605
606 return false;
607}
608
609/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
610/// may occur after a #if or #elif directive. If the expression is equivalent
611/// to "!defined(X)" return X in IfNDefMacro.
612bool Preprocessor::
613EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
614 // Peek ahead one token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000615 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 Lex(Tok);
617
618 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
619 unsigned BitWidth = getTargetInfo().getIntMaxTWidth(Tok.getLocation());
620 llvm::APSInt ResVal(BitWidth);
621 DefinedTracker DT;
622 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
623 // Parse error, skip the rest of the macro line.
624 if (Tok.getKind() != tok::eom)
625 DiscardUntilEndOfDirective();
626 return false;
627 }
628
629 // If we are at the end of the expression after just parsing a value, there
630 // must be no (unparenthesized) binary operators involved, so we can exit
631 // directly.
632 if (Tok.getKind() == tok::eom) {
633 // If the expression we parsed was of the form !defined(macro), return the
634 // macro in IfNDefMacro.
635 if (DT.State == DefinedTracker::NotDefinedMacro)
636 IfNDefMacro = DT.TheMacro;
637
638 return ResVal != 0;
639 }
640
641 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
642 // operator and the stuff after it.
643 if (EvaluateDirectiveSubExpr(ResVal, 1, Tok, true, *this)) {
644 // Parse error, skip the rest of the macro line.
645 if (Tok.getKind() != tok::eom)
646 DiscardUntilEndOfDirective();
647 return false;
648 }
649
650 // If we aren't at the tok::eom token, something bad happened, like an extra
651 // ')' token.
652 if (Tok.getKind() != tok::eom) {
653 Diag(Tok, diag::err_pp_expected_eol);
654 DiscardUntilEndOfDirective();
655 }
656
657 return ResVal != 0;
658}
659