1 //===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Preprocessor::EvaluateDirectiveExpression method,
11 // which parses and evaluates integer constant expressions for #if directives.
13 //===----------------------------------------------------------------------===//
15 // FIXME: implement testing for #assert's.
17 //===----------------------------------------------------------------------===//
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/Lex/LexDiagnostic.h"
24 #include "llvm/ADT/APSInt.h"
25 using namespace clang;
27 /// PPValue - Represents the value of a subexpression of a preprocessor
28 /// conditional and the source range covered by it.
34 // Default ctor - Construct an 'invalid' PPValue.
35 PPValue(unsigned BitWidth) : Val(BitWidth) {}
37 unsigned getBitWidth() const { return Val.getBitWidth(); }
38 bool isUnsigned() const { return Val.isUnsigned(); }
40 const SourceRange &getRange() const { return Range; }
42 void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
43 void setRange(SourceLocation B, SourceLocation E) {
44 Range.setBegin(B); Range.setEnd(E);
46 void setBegin(SourceLocation L) { Range.setBegin(L); }
47 void setEnd(SourceLocation L) { Range.setEnd(L); }
50 static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
51 Token &PeekTok, bool ValueLive,
54 /// DefinedTracker - This struct is used while parsing expressions to keep track
55 /// of whether !defined(X) has been seen.
57 /// With this simple scheme, we handle the basic forms:
58 /// !defined(X) and !defined X
59 /// but we also trivially handle (silly) stuff like:
60 /// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
61 struct DefinedTracker {
62 /// Each time a Value is evaluated, it returns information about whether the
63 /// parsed value is of the form defined(X), !defined(X) or is something else.
65 DefinedMacro, // defined(X)
66 NotDefinedMacro, // !defined(X)
67 Unknown // Something else.
69 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
70 /// indicates the macro that was checked.
71 IdentifierInfo *TheMacro;
74 /// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
75 /// return the computed value in Result. Return true if there was an error
76 /// parsing. This function also returns information about the form of the
77 /// expression in DT. See above for information on what DT means.
79 /// If ValueLive is false, then this value is being evaluated in a context where
80 /// the result is not used. As such, avoid diagnostics that relate to
82 static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
83 bool ValueLive, Preprocessor &PP) {
84 DT.State = DefinedTracker::Unknown;
86 // If this token's spelling is a pp-identifier, check to see if it is
87 // 'defined' or if it is a macro. Note that we check here because many
88 // keywords are pp-identifiers, so we can't check the kind.
89 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
90 // If this identifier isn't 'defined' and it wasn't macro expanded, it turns
91 // into a simple 0, unless it is the C++ keyword "true", in which case it
93 if (!II->isStr("defined")) {
95 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
96 Result.Val = II->getTokenID() == tok::kw_true;
97 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
98 Result.setRange(PeekTok.getLocation());
99 PP.LexNonComment(PeekTok);
103 // Handle "defined X" and "defined(X)".
104 Result.setBegin(PeekTok.getLocation());
106 // Get the next token, don't expand it.
107 PP.LexUnexpandedToken(PeekTok);
109 // Two options, it can either be a pp-identifier or a (.
110 SourceLocation LParenLoc;
111 if (PeekTok.is(tok::l_paren)) {
112 // Found a paren, remember we saw it and skip it.
113 LParenLoc = PeekTok.getLocation();
114 PP.LexUnexpandedToken(PeekTok);
117 // If we don't have a pp-identifier now, this is an error.
118 if ((II = PeekTok.getIdentifierInfo()) == 0) {
119 PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier);
123 // Otherwise, we got an identifier, is it defined to something?
124 Result.Val = II->hasMacroDefinition();
125 Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
127 // If there is a macro, mark it used.
128 if (Result.Val != 0 && ValueLive) {
129 MacroInfo *Macro = PP.getMacroInfo(II);
130 Macro->setIsUsed(true);
133 // Consume identifier.
134 Result.setEnd(PeekTok.getLocation());
135 PP.LexNonComment(PeekTok);
137 // If we are in parens, ensure we have a trailing ).
138 if (LParenLoc.isValid()) {
139 if (PeekTok.isNot(tok::r_paren)) {
140 PP.Diag(PeekTok.getLocation(), diag::err_pp_missing_rparen);
141 PP.Diag(LParenLoc, diag::note_matching) << "(";
145 Result.setEnd(PeekTok.getLocation());
146 PP.LexNonComment(PeekTok);
149 // Success, remember that we saw defined(X).
150 DT.State = DefinedTracker::DefinedMacro;
155 switch (PeekTok.getKind()) {
156 default: // Non-value token.
157 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
161 // If there is no expression, report and exit.
162 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
164 case tok::numeric_constant: {
165 llvm::SmallString<64> IntegerBuffer;
166 IntegerBuffer.resize(PeekTok.getLength());
167 const char *ThisTokBegin = &IntegerBuffer[0];
168 unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
169 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
170 PeekTok.getLocation(), PP);
171 if (Literal.hadError)
172 return true; // a diagnostic was already reported.
174 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
175 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
178 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
180 // long long is a C99 feature.
181 if (!PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus0x
182 && Literal.isLongLong)
183 PP.Diag(PeekTok, diag::ext_longlong);
185 // Parse the integer literal into Result.
186 if (Literal.GetIntegerValue(Result.Val)) {
187 // Overflow parsing integer literal.
188 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
189 Result.Val.setIsUnsigned(true);
191 // Set the signedness of the result to match whether there was a U suffix
193 Result.Val.setIsUnsigned(Literal.isUnsigned);
195 // Detect overflow based on whether the value is signed. If signed
196 // and if the value is too large, emit a warning "integer constant is so
197 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
199 if (!Literal.isUnsigned && Result.Val.isNegative()) {
200 // Don't warn for a hex literal: 0x8000..0 shouldn't warn.
201 if (ValueLive && Literal.getRadix() != 16)
202 PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
203 Result.Val.setIsUnsigned(true);
207 // Consume the token.
208 Result.setRange(PeekTok.getLocation());
209 PP.LexNonComment(PeekTok);
212 case tok::char_constant: { // 'x'
213 llvm::SmallString<32> CharBuffer;
214 CharBuffer.resize(PeekTok.getLength());
215 const char *ThisTokBegin = &CharBuffer[0];
216 unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
217 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
218 PeekTok.getLocation(), PP);
219 if (Literal.hadError())
220 return true; // A diagnostic was already emitted.
222 // Character literals are always int or wchar_t, expand to intmax_t.
223 TargetInfo &TI = PP.getTargetInfo();
225 if (Literal.isMultiChar())
226 NumBits = TI.getIntWidth();
227 else if (Literal.isWide())
228 NumBits = TI.getWCharWidth();
230 NumBits = TI.getCharWidth();
233 llvm::APSInt Val(NumBits);
235 Val = Literal.getValue();
236 // Set the signedness.
237 Val.setIsUnsigned(!PP.getLangOptions().CharIsSigned);
239 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
240 Result.Val = Val.extend(Result.Val.getBitWidth());
242 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
243 "intmax_t smaller than char/wchar_t?");
247 // Consume the token.
248 Result.setRange(PeekTok.getLocation());
249 PP.LexNonComment(PeekTok);
253 SourceLocation Start = PeekTok.getLocation();
254 PP.LexNonComment(PeekTok); // Eat the (.
255 // Parse the value and if there are any binary operators involved, parse
257 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
259 // If this is a silly value like (X), which doesn't need parens, check for
261 if (PeekTok.is(tok::r_paren)) {
262 // Just use DT unmodified as our result.
264 // Otherwise, we have something like (x+y), and we consumed '(x'.
265 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
268 if (PeekTok.isNot(tok::r_paren)) {
269 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
270 << Result.getRange();
271 PP.Diag(Start, diag::note_matching) << "(";
274 DT.State = DefinedTracker::Unknown;
276 Result.setRange(Start, PeekTok.getLocation());
277 PP.LexNonComment(PeekTok); // Eat the ).
281 SourceLocation Start = PeekTok.getLocation();
282 // Unary plus doesn't modify the value.
283 PP.LexNonComment(PeekTok);
284 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
285 Result.setBegin(Start);
289 SourceLocation Loc = PeekTok.getLocation();
290 PP.LexNonComment(PeekTok);
291 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
292 Result.setBegin(Loc);
294 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
295 Result.Val = -Result.Val;
297 // -MININT is the only thing that overflows. Unsigned never overflows.
298 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
300 // If this operator is live and overflowed, report the issue.
301 if (Overflow && ValueLive)
302 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
304 DT.State = DefinedTracker::Unknown;
309 SourceLocation Start = PeekTok.getLocation();
310 PP.LexNonComment(PeekTok);
311 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
312 Result.setBegin(Start);
314 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
315 Result.Val = ~Result.Val;
316 DT.State = DefinedTracker::Unknown;
321 SourceLocation Start = PeekTok.getLocation();
322 PP.LexNonComment(PeekTok);
323 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
324 Result.setBegin(Start);
325 Result.Val = !Result.Val;
326 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
327 Result.Val.setIsUnsigned(false);
329 if (DT.State == DefinedTracker::DefinedMacro)
330 DT.State = DefinedTracker::NotDefinedMacro;
331 else if (DT.State == DefinedTracker::NotDefinedMacro)
332 DT.State = DefinedTracker::DefinedMacro;
336 // FIXME: Handle #assert
342 /// getPrecedence - Return the precedence of the specified binary operator
343 /// token. This returns:
344 /// ~0 - Invalid token.
345 /// 14 -> 3 - various operators.
347 static unsigned getPrecedence(tok::TokenKind Kind) {
352 case tok::star: return 14;
354 case tok::minus: return 13;
356 case tok::greatergreater: return 12;
359 case tok::greaterequal:
360 case tok::greater: return 11;
361 case tok::exclaimequal:
362 case tok::equalequal: return 10;
363 case tok::amp: return 9;
364 case tok::caret: return 8;
365 case tok::pipe: return 7;
366 case tok::ampamp: return 6;
367 case tok::pipepipe: return 5;
368 case tok::question: return 4;
369 case tok::comma: return 3;
370 case tok::colon: return 2;
371 case tok::r_paren: return 0; // Lowest priority, end of expr.
372 case tok::eom: return 0; // Lowest priority, end of macro.
377 /// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
378 /// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
380 /// If ValueLive is false, then this value is being evaluated in a context where
381 /// the result is not used. As such, avoid diagnostics that relate to
382 /// evaluation, such as division by zero warnings.
383 static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
384 Token &PeekTok, bool ValueLive,
386 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
387 // If this token isn't valid, report the error.
388 if (PeekPrec == ~0U) {
389 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
395 // If this token has a lower precedence than we are allowed to parse, return
396 // it so that higher levels of the recursion can parse it.
397 if (PeekPrec < MinPrec)
400 tok::TokenKind Operator = PeekTok.getKind();
402 // If this is a short-circuiting operator, see if the RHS of the operator is
403 // dead. Note that this cannot just clobber ValueLive. Consider
404 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
405 // this example, the RHS of the && being dead does not make the rest of the
408 if (Operator == tok::ampamp && LHS.Val == 0)
409 RHSIsLive = false; // RHS of "0 && x" is dead.
410 else if (Operator == tok::pipepipe && LHS.Val != 0)
411 RHSIsLive = false; // RHS of "1 || x" is dead.
412 else if (Operator == tok::question && LHS.Val == 0)
413 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
415 RHSIsLive = ValueLive;
417 // Consume the operator, remembering the operator's location for reporting.
418 SourceLocation OpLoc = PeekTok.getLocation();
419 PP.LexNonComment(PeekTok);
421 PPValue RHS(LHS.getBitWidth());
422 // Parse the RHS of the operator.
424 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
426 // Remember the precedence of this operator and get the precedence of the
427 // operator immediately to the right of the RHS.
428 unsigned ThisPrec = PeekPrec;
429 PeekPrec = getPrecedence(PeekTok.getKind());
431 // If this token isn't valid, report the error.
432 if (PeekPrec == ~0U) {
433 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
438 // Decide whether to include the next binop in this subexpression. For
439 // example, when parsing x+y*z and looking at '*', we want to recursively
440 // handle y*z as a single subexpression. We do this because the precedence
441 // of * is higher than that of +. The only strange case we have to handle
442 // here is for the ?: operator, where the precedence is actually lower than
443 // the LHS of the '?'. The grammar rule is:
445 // conditional-expression ::=
446 // logical-OR-expression ? expression : conditional-expression
447 // where 'expression' is actually comma-expression.
449 if (Operator == tok::question)
450 // The RHS of "?" should be maximally consumed as an expression.
451 RHSPrec = getPrecedence(tok::comma);
452 else // All others should munch while higher precedence.
453 RHSPrec = ThisPrec+1;
455 if (PeekPrec >= RHSPrec) {
456 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
458 PeekPrec = getPrecedence(PeekTok.getKind());
460 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
462 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
463 // either operand is unsigned.
464 llvm::APSInt Res(LHS.getBitWidth());
466 case tok::question: // No UAC for x and y in "x ? y : z".
467 case tok::lessless: // Shift amount doesn't UAC with shift value.
468 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
469 case tok::comma: // Comma operands are not subject to UACs.
470 case tok::pipepipe: // Logical || does not do UACs.
471 case tok::ampamp: // Logical && does not do UACs.
474 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
475 // If this just promoted something from signed to unsigned, and if the
476 // value was negative, warn about it.
477 if (ValueLive && Res.isUnsigned()) {
478 if (!LHS.isUnsigned() && LHS.Val.isNegative())
479 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
480 << LHS.Val.toString(10, true) + " to " +
481 LHS.Val.toString(10, false)
482 << LHS.getRange() << RHS.getRange();
483 if (!RHS.isUnsigned() && RHS.Val.isNegative())
484 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
485 << RHS.Val.toString(10, true) + " to " +
486 RHS.Val.toString(10, false)
487 << LHS.getRange() << RHS.getRange();
489 LHS.Val.setIsUnsigned(Res.isUnsigned());
490 RHS.Val.setIsUnsigned(Res.isUnsigned());
493 // FIXME: All of these should detect and report overflow??
494 bool Overflow = false;
496 default: assert(0 && "Unknown operator token!");
499 Res = LHS.Val % RHS.Val;
500 else if (ValueLive) {
501 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
502 << LHS.getRange() << RHS.getRange();
508 Res = LHS.Val / RHS.Val;
509 if (LHS.Val.isSigned()) // MININT/-1 --> overflow.
510 Overflow = LHS.Val.isMinSignedValue() && RHS.Val.isAllOnesValue();
511 } else if (ValueLive) {
512 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
513 << LHS.getRange() << RHS.getRange();
519 Res = LHS.Val * RHS.Val;
520 if (Res.isSigned() && LHS.Val != 0 && RHS.Val != 0)
521 Overflow = Res/RHS.Val != LHS.Val || Res/LHS.Val != RHS.Val;
523 case tok::lessless: {
524 // Determine whether overflow is about to happen.
525 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
526 if (ShAmt >= LHS.Val.getBitWidth())
527 Overflow = true, ShAmt = LHS.Val.getBitWidth()-1;
528 else if (LHS.isUnsigned())
530 else if (LHS.Val.isNonNegative()) // Don't allow sign change.
531 Overflow = ShAmt >= LHS.Val.countLeadingZeros();
533 Overflow = ShAmt >= LHS.Val.countLeadingOnes();
535 Res = LHS.Val << ShAmt;
538 case tok::greatergreater: {
539 // Determine whether overflow is about to happen.
540 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
541 if (ShAmt >= LHS.getBitWidth())
542 Overflow = true, ShAmt = LHS.getBitWidth()-1;
543 Res = LHS.Val >> ShAmt;
547 Res = LHS.Val + RHS.Val;
548 if (LHS.isUnsigned())
550 else if (LHS.Val.isNonNegative() == RHS.Val.isNonNegative() &&
551 Res.isNonNegative() != LHS.Val.isNonNegative())
552 Overflow = true; // Overflow for signed addition.
555 Res = LHS.Val - RHS.Val;
556 if (LHS.isUnsigned())
558 else if (LHS.Val.isNonNegative() != RHS.Val.isNonNegative() &&
559 Res.isNonNegative() != LHS.Val.isNonNegative())
560 Overflow = true; // Overflow for signed subtraction.
563 Res = LHS.Val <= RHS.Val;
564 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
567 Res = LHS.Val < RHS.Val;
568 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
570 case tok::greaterequal:
571 Res = LHS.Val >= RHS.Val;
572 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
575 Res = LHS.Val > RHS.Val;
576 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
578 case tok::exclaimequal:
579 Res = LHS.Val != RHS.Val;
580 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
582 case tok::equalequal:
583 Res = LHS.Val == RHS.Val;
584 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
587 Res = LHS.Val & RHS.Val;
590 Res = LHS.Val ^ RHS.Val;
593 Res = LHS.Val | RHS.Val;
596 Res = (LHS.Val != 0 && RHS.Val != 0);
597 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
600 Res = (LHS.Val != 0 || RHS.Val != 0);
601 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
604 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
605 // if not being evaluated.
606 if (!PP.getLangOptions().C99 || ValueLive)
607 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
608 << LHS.getRange() << RHS.getRange();
609 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
611 case tok::question: {
612 // Parse the : part of the expression.
613 if (PeekTok.isNot(tok::colon)) {
614 PP.Diag(PeekTok.getLocation(), diag::err_expected_colon)
615 << LHS.getRange(), RHS.getRange();
616 PP.Diag(OpLoc, diag::note_matching) << "?";
620 PP.LexNonComment(PeekTok);
622 // Evaluate the value after the :.
623 bool AfterColonLive = ValueLive && LHS.Val == 0;
624 PPValue AfterColonVal(LHS.getBitWidth());
626 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
629 // Parse anything after the : with the same precedence as ?. We allow
630 // things of equal precedence because ?: is right associative.
631 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
632 PeekTok, AfterColonLive, PP))
635 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
636 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
637 RHS.setEnd(AfterColonVal.getRange().getEnd());
639 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
640 // either operand is unsigned.
641 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
643 // Figure out the precedence of the token after the : part.
644 PeekPrec = getPrecedence(PeekTok.getKind());
648 // Don't allow :'s to float around without being part of ?: exprs.
649 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
650 << LHS.getRange() << RHS.getRange();
654 // If this operator is live and overflowed, report the issue.
655 if (Overflow && ValueLive)
656 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
657 << LHS.getRange() << RHS.getRange();
659 // Put the result back into 'LHS' for our next iteration.
661 LHS.setEnd(RHS.getRange().getEnd());
667 /// EvaluateDirectiveExpression - Evaluate an integer constant expression that
668 /// may occur after a #if or #elif directive. If the expression is equivalent
669 /// to "!defined(X)" return X in IfNDefMacro.
671 EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
672 // Peek ahead one token.
676 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
677 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
679 PPValue ResVal(BitWidth);
681 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
682 // Parse error, skip the rest of the macro line.
683 if (Tok.isNot(tok::eom))
684 DiscardUntilEndOfDirective();
688 // If we are at the end of the expression after just parsing a value, there
689 // must be no (unparenthesized) binary operators involved, so we can exit
691 if (Tok.is(tok::eom)) {
692 // If the expression we parsed was of the form !defined(macro), return the
693 // macro in IfNDefMacro.
694 if (DT.State == DefinedTracker::NotDefinedMacro)
695 IfNDefMacro = DT.TheMacro;
697 return ResVal.Val != 0;
700 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
701 // operator and the stuff after it.
702 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
704 // Parse error, skip the rest of the macro line.
705 if (Tok.isNot(tok::eom))
706 DiscardUntilEndOfDirective();
710 // If we aren't at the tok::eom token, something bad happened, like an extra
712 if (Tok.isNot(tok::eom)) {
713 Diag(Tok, diag::err_pp_expected_eol);
714 DiscardUntilEndOfDirective();
717 return ResVal.Val != 0;