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/Basic/TargetInfo.h"
21 #include "clang/Lex/CodeCompletionHandler.h"
22 #include "clang/Lex/LexDiagnostic.h"
23 #include "clang/Lex/LiteralSupport.h"
24 #include "clang/Lex/MacroInfo.h"
25 #include "llvm/ADT/APSInt.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/SaveAndRestore.h"
28 using namespace clang;
32 /// PPValue - Represents the value of a subexpression of a preprocessor
33 /// conditional and the source range covered by it.
39 // Default ctor - Construct an 'invalid' PPValue.
40 PPValue(unsigned BitWidth) : Val(BitWidth) {}
42 unsigned getBitWidth() const { return Val.getBitWidth(); }
43 bool isUnsigned() const { return Val.isUnsigned(); }
45 const SourceRange &getRange() const { return Range; }
47 void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
48 void setRange(SourceLocation B, SourceLocation E) {
49 Range.setBegin(B); Range.setEnd(E);
51 void setBegin(SourceLocation L) { Range.setBegin(L); }
52 void setEnd(SourceLocation L) { Range.setEnd(L); }
57 static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
58 Token &PeekTok, bool ValueLive,
61 /// DefinedTracker - This struct is used while parsing expressions to keep track
62 /// of whether !defined(X) has been seen.
64 /// With this simple scheme, we handle the basic forms:
65 /// !defined(X) and !defined X
66 /// but we also trivially handle (silly) stuff like:
67 /// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
68 struct DefinedTracker {
69 /// Each time a Value is evaluated, it returns information about whether the
70 /// parsed value is of the form defined(X), !defined(X) or is something else.
72 DefinedMacro, // defined(X)
73 NotDefinedMacro, // !defined(X)
74 Unknown // Something else.
76 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
77 /// indicates the macro that was checked.
78 IdentifierInfo *TheMacro;
81 /// EvaluateDefined - Process a 'defined(sym)' expression.
82 static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
83 bool ValueLive, Preprocessor &PP) {
84 SourceLocation beginLoc(PeekTok.getLocation());
85 Result.setBegin(beginLoc);
87 // Get the next token, don't expand it.
88 PP.LexUnexpandedNonComment(PeekTok);
90 // Two options, it can either be a pp-identifier or a (.
91 SourceLocation LParenLoc;
92 if (PeekTok.is(tok::l_paren)) {
93 // Found a paren, remember we saw it and skip it.
94 LParenLoc = PeekTok.getLocation();
95 PP.LexUnexpandedNonComment(PeekTok);
98 if (PeekTok.is(tok::code_completion)) {
99 if (PP.getCodeCompletionHandler())
100 PP.getCodeCompletionHandler()->CodeCompleteMacroName(false);
101 PP.setCodeCompletionReached();
102 PP.LexUnexpandedNonComment(PeekTok);
105 // If we don't have a pp-identifier now, this is an error.
106 if (PP.CheckMacroName(PeekTok, MU_Other))
109 // Otherwise, we got an identifier, is it defined to something?
110 IdentifierInfo *II = PeekTok.getIdentifierInfo();
111 MacroDefinition Macro = PP.getMacroDefinition(II);
112 Result.Val = !!Macro;
113 Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
115 // If there is a macro, mark it used.
116 if (Result.Val != 0 && ValueLive)
117 PP.markMacroAsUsed(Macro.getMacroInfo());
119 // Save macro token for callback.
120 Token macroToken(PeekTok);
122 // If we are in parens, ensure we have a trailing ).
123 if (LParenLoc.isValid()) {
124 // Consume identifier.
125 Result.setEnd(PeekTok.getLocation());
126 PP.LexUnexpandedNonComment(PeekTok);
128 if (PeekTok.isNot(tok::r_paren)) {
129 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_after)
130 << "'defined'" << tok::r_paren;
131 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
135 Result.setEnd(PeekTok.getLocation());
136 PP.LexNonComment(PeekTok);
138 // Consume identifier.
139 Result.setEnd(PeekTok.getLocation());
140 PP.LexNonComment(PeekTok);
143 // Invoke the 'defined' callback.
144 if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
145 Callbacks->Defined(macroToken, Macro,
146 SourceRange(beginLoc, PeekTok.getLocation()));
149 // Success, remember that we saw defined(X).
150 DT.State = DefinedTracker::DefinedMacro;
155 /// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
156 /// return the computed value in Result. Return true if there was an error
157 /// parsing. This function also returns information about the form of the
158 /// expression in DT. See above for information on what DT means.
160 /// If ValueLive is false, then this value is being evaluated in a context where
161 /// the result is not used. As such, avoid diagnostics that relate to
163 static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
164 bool ValueLive, Preprocessor &PP) {
165 DT.State = DefinedTracker::Unknown;
167 if (PeekTok.is(tok::code_completion)) {
168 if (PP.getCodeCompletionHandler())
169 PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
170 PP.setCodeCompletionReached();
171 PP.LexNonComment(PeekTok);
174 // If this token's spelling is a pp-identifier, check to see if it is
175 // 'defined' or if it is a macro. Note that we check here because many
176 // keywords are pp-identifiers, so we can't check the kind.
177 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
178 // Handle "defined X" and "defined(X)".
179 if (II->isStr("defined"))
180 return(EvaluateDefined(Result, PeekTok, DT, ValueLive, PP));
182 // If this identifier isn't 'defined' or one of the special
183 // preprocessor keywords and it wasn't macro expanded, it turns
184 // into a simple 0, unless it is the C++ keyword "true", in which case it
187 II->getTokenID() != tok::kw_true &&
188 II->getTokenID() != tok::kw_false)
189 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
190 Result.Val = II->getTokenID() == tok::kw_true;
191 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
192 Result.setRange(PeekTok.getLocation());
193 PP.LexNonComment(PeekTok);
197 switch (PeekTok.getKind()) {
198 default: // Non-value token.
199 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
203 // If there is no expression, report and exit.
204 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
206 case tok::numeric_constant: {
207 SmallString<64> IntegerBuffer;
208 bool NumberInvalid = false;
209 StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
212 return true; // a diagnostic was already reported
214 NumericLiteralParser Literal(Spelling, PeekTok.getLocation(), PP);
215 if (Literal.hadError)
216 return true; // a diagnostic was already reported.
218 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
219 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
222 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
224 // Complain about, and drop, any ud-suffix.
225 if (Literal.hasUDSuffix())
226 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1;
228 // 'long long' is a C99 or C++11 feature.
229 if (!PP.getLangOpts().C99 && Literal.isLongLong) {
230 if (PP.getLangOpts().CPlusPlus)
232 PP.getLangOpts().CPlusPlus11 ?
233 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
235 PP.Diag(PeekTok, diag::ext_c99_longlong);
238 // Parse the integer literal into Result.
239 if (Literal.GetIntegerValue(Result.Val)) {
240 // Overflow parsing integer literal.
242 PP.Diag(PeekTok, diag::err_integer_literal_too_large)
244 Result.Val.setIsUnsigned(true);
246 // Set the signedness of the result to match whether there was a U suffix
248 Result.Val.setIsUnsigned(Literal.isUnsigned);
250 // Detect overflow based on whether the value is signed. If signed
251 // and if the value is too large, emit a warning "integer constant is so
252 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
254 if (!Literal.isUnsigned && Result.Val.isNegative()) {
255 // Octal, hexadecimal, and binary literals are implicitly unsigned if
256 // the value does not fit into a signed integer type.
257 if (ValueLive && Literal.getRadix() == 10)
258 PP.Diag(PeekTok, diag::ext_integer_literal_too_large_for_signed);
259 Result.Val.setIsUnsigned(true);
263 // Consume the token.
264 Result.setRange(PeekTok.getLocation());
265 PP.LexNonComment(PeekTok);
268 case tok::char_constant: // 'x'
269 case tok::wide_char_constant: // L'x'
270 case tok::utf8_char_constant: // u8'x'
271 case tok::utf16_char_constant: // u'x'
272 case tok::utf32_char_constant: { // U'x'
273 // Complain about, and drop, any ud-suffix.
274 if (PeekTok.hasUDSuffix())
275 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
277 SmallString<32> CharBuffer;
278 bool CharInvalid = false;
279 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
283 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
284 PeekTok.getLocation(), PP, PeekTok.getKind());
285 if (Literal.hadError())
286 return true; // A diagnostic was already emitted.
288 // Character literals are always int or wchar_t, expand to intmax_t.
289 const TargetInfo &TI = PP.getTargetInfo();
291 if (Literal.isMultiChar())
292 NumBits = TI.getIntWidth();
293 else if (Literal.isWide())
294 NumBits = TI.getWCharWidth();
295 else if (Literal.isUTF16())
296 NumBits = TI.getChar16Width();
297 else if (Literal.isUTF32())
298 NumBits = TI.getChar32Width();
300 NumBits = TI.getCharWidth();
303 llvm::APSInt Val(NumBits);
305 Val = Literal.getValue();
306 // Set the signedness. UTF-16 and UTF-32 are always unsigned
307 if (Literal.isWide())
308 Val.setIsUnsigned(!TargetInfo::isTypeSigned(TI.getWCharType()));
309 else if (!Literal.isUTF16() && !Literal.isUTF32())
310 Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned);
312 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
313 Result.Val = Val.extend(Result.Val.getBitWidth());
315 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
316 "intmax_t smaller than char/wchar_t?");
320 // Consume the token.
321 Result.setRange(PeekTok.getLocation());
322 PP.LexNonComment(PeekTok);
326 SourceLocation Start = PeekTok.getLocation();
327 PP.LexNonComment(PeekTok); // Eat the (.
328 // Parse the value and if there are any binary operators involved, parse
330 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
332 // If this is a silly value like (X), which doesn't need parens, check for
334 if (PeekTok.is(tok::r_paren)) {
335 // Just use DT unmodified as our result.
337 // Otherwise, we have something like (x+y), and we consumed '(x'.
338 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
341 if (PeekTok.isNot(tok::r_paren)) {
342 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
343 << Result.getRange();
344 PP.Diag(Start, diag::note_matching) << tok::l_paren;
347 DT.State = DefinedTracker::Unknown;
349 Result.setRange(Start, PeekTok.getLocation());
350 PP.LexNonComment(PeekTok); // Eat the ).
354 SourceLocation Start = PeekTok.getLocation();
355 // Unary plus doesn't modify the value.
356 PP.LexNonComment(PeekTok);
357 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
358 Result.setBegin(Start);
362 SourceLocation Loc = PeekTok.getLocation();
363 PP.LexNonComment(PeekTok);
364 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
365 Result.setBegin(Loc);
367 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
368 Result.Val = -Result.Val;
370 // -MININT is the only thing that overflows. Unsigned never overflows.
371 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
373 // If this operator is live and overflowed, report the issue.
374 if (Overflow && ValueLive)
375 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
377 DT.State = DefinedTracker::Unknown;
382 SourceLocation Start = PeekTok.getLocation();
383 PP.LexNonComment(PeekTok);
384 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
385 Result.setBegin(Start);
387 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
388 Result.Val = ~Result.Val;
389 DT.State = DefinedTracker::Unknown;
394 SourceLocation Start = PeekTok.getLocation();
395 PP.LexNonComment(PeekTok);
396 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
397 Result.setBegin(Start);
398 Result.Val = !Result.Val;
399 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
400 Result.Val.setIsUnsigned(false);
402 if (DT.State == DefinedTracker::DefinedMacro)
403 DT.State = DefinedTracker::NotDefinedMacro;
404 else if (DT.State == DefinedTracker::NotDefinedMacro)
405 DT.State = DefinedTracker::DefinedMacro;
409 // FIXME: Handle #assert
415 /// getPrecedence - Return the precedence of the specified binary operator
416 /// token. This returns:
417 /// ~0 - Invalid token.
418 /// 14 -> 3 - various operators.
420 static unsigned getPrecedence(tok::TokenKind Kind) {
425 case tok::star: return 14;
427 case tok::minus: return 13;
429 case tok::greatergreater: return 12;
432 case tok::greaterequal:
433 case tok::greater: return 11;
434 case tok::exclaimequal:
435 case tok::equalequal: return 10;
436 case tok::amp: return 9;
437 case tok::caret: return 8;
438 case tok::pipe: return 7;
439 case tok::ampamp: return 6;
440 case tok::pipepipe: return 5;
441 case tok::question: return 4;
442 case tok::comma: return 3;
443 case tok::colon: return 2;
444 case tok::r_paren: return 0;// Lowest priority, end of expr.
445 case tok::eod: return 0;// Lowest priority, end of directive.
450 /// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
451 /// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
453 /// If ValueLive is false, then this value is being evaluated in a context where
454 /// the result is not used. As such, avoid diagnostics that relate to
455 /// evaluation, such as division by zero warnings.
456 static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
457 Token &PeekTok, bool ValueLive,
459 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
460 // If this token isn't valid, report the error.
461 if (PeekPrec == ~0U) {
462 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
468 // If this token has a lower precedence than we are allowed to parse, return
469 // it so that higher levels of the recursion can parse it.
470 if (PeekPrec < MinPrec)
473 tok::TokenKind Operator = PeekTok.getKind();
475 // If this is a short-circuiting operator, see if the RHS of the operator is
476 // dead. Note that this cannot just clobber ValueLive. Consider
477 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
478 // this example, the RHS of the && being dead does not make the rest of the
481 if (Operator == tok::ampamp && LHS.Val == 0)
482 RHSIsLive = false; // RHS of "0 && x" is dead.
483 else if (Operator == tok::pipepipe && LHS.Val != 0)
484 RHSIsLive = false; // RHS of "1 || x" is dead.
485 else if (Operator == tok::question && LHS.Val == 0)
486 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
488 RHSIsLive = ValueLive;
490 // Consume the operator, remembering the operator's location for reporting.
491 SourceLocation OpLoc = PeekTok.getLocation();
492 PP.LexNonComment(PeekTok);
494 PPValue RHS(LHS.getBitWidth());
495 // Parse the RHS of the operator.
497 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
499 // Remember the precedence of this operator and get the precedence of the
500 // operator immediately to the right of the RHS.
501 unsigned ThisPrec = PeekPrec;
502 PeekPrec = getPrecedence(PeekTok.getKind());
504 // If this token isn't valid, report the error.
505 if (PeekPrec == ~0U) {
506 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
511 // Decide whether to include the next binop in this subexpression. For
512 // example, when parsing x+y*z and looking at '*', we want to recursively
513 // handle y*z as a single subexpression. We do this because the precedence
514 // of * is higher than that of +. The only strange case we have to handle
515 // here is for the ?: operator, where the precedence is actually lower than
516 // the LHS of the '?'. The grammar rule is:
518 // conditional-expression ::=
519 // logical-OR-expression ? expression : conditional-expression
520 // where 'expression' is actually comma-expression.
522 if (Operator == tok::question)
523 // The RHS of "?" should be maximally consumed as an expression.
524 RHSPrec = getPrecedence(tok::comma);
525 else // All others should munch while higher precedence.
526 RHSPrec = ThisPrec+1;
528 if (PeekPrec >= RHSPrec) {
529 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
531 PeekPrec = getPrecedence(PeekTok.getKind());
533 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
535 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
536 // either operand is unsigned.
537 llvm::APSInt Res(LHS.getBitWidth());
539 case tok::question: // No UAC for x and y in "x ? y : z".
540 case tok::lessless: // Shift amount doesn't UAC with shift value.
541 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
542 case tok::comma: // Comma operands are not subject to UACs.
543 case tok::pipepipe: // Logical || does not do UACs.
544 case tok::ampamp: // Logical && does not do UACs.
547 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
548 // If this just promoted something from signed to unsigned, and if the
549 // value was negative, warn about it.
550 if (ValueLive && Res.isUnsigned()) {
551 if (!LHS.isUnsigned() && LHS.Val.isNegative())
552 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
553 << LHS.Val.toString(10, true) + " to " +
554 LHS.Val.toString(10, false)
555 << LHS.getRange() << RHS.getRange();
556 if (!RHS.isUnsigned() && RHS.Val.isNegative())
557 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
558 << RHS.Val.toString(10, true) + " to " +
559 RHS.Val.toString(10, false)
560 << LHS.getRange() << RHS.getRange();
562 LHS.Val.setIsUnsigned(Res.isUnsigned());
563 RHS.Val.setIsUnsigned(Res.isUnsigned());
566 bool Overflow = false;
568 default: llvm_unreachable("Unknown operator token!");
571 Res = LHS.Val % RHS.Val;
572 else if (ValueLive) {
573 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
574 << LHS.getRange() << RHS.getRange();
580 if (LHS.Val.isSigned())
581 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
583 Res = LHS.Val / RHS.Val;
584 } else if (ValueLive) {
585 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
586 << LHS.getRange() << RHS.getRange();
593 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
595 Res = LHS.Val * RHS.Val;
597 case tok::lessless: {
598 // Determine whether overflow is about to happen.
599 if (LHS.isUnsigned())
600 Res = LHS.Val.ushl_ov(RHS.Val, Overflow);
602 Res = llvm::APSInt(LHS.Val.sshl_ov(RHS.Val, Overflow), false);
605 case tok::greatergreater: {
606 // Determine whether overflow is about to happen.
607 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
608 if (ShAmt >= LHS.getBitWidth())
609 Overflow = true, ShAmt = LHS.getBitWidth()-1;
610 Res = LHS.Val >> ShAmt;
614 if (LHS.isUnsigned())
615 Res = LHS.Val + RHS.Val;
617 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
620 if (LHS.isUnsigned())
621 Res = LHS.Val - RHS.Val;
623 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
626 Res = LHS.Val <= RHS.Val;
627 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
630 Res = LHS.Val < RHS.Val;
631 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
633 case tok::greaterequal:
634 Res = LHS.Val >= RHS.Val;
635 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
638 Res = LHS.Val > RHS.Val;
639 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
641 case tok::exclaimequal:
642 Res = LHS.Val != RHS.Val;
643 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
645 case tok::equalequal:
646 Res = LHS.Val == RHS.Val;
647 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
650 Res = LHS.Val & RHS.Val;
653 Res = LHS.Val ^ RHS.Val;
656 Res = LHS.Val | RHS.Val;
659 Res = (LHS.Val != 0 && RHS.Val != 0);
660 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
663 Res = (LHS.Val != 0 || RHS.Val != 0);
664 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
667 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
668 // if not being evaluated.
669 if (!PP.getLangOpts().C99 || ValueLive)
670 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
671 << LHS.getRange() << RHS.getRange();
672 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
674 case tok::question: {
675 // Parse the : part of the expression.
676 if (PeekTok.isNot(tok::colon)) {
677 PP.Diag(PeekTok.getLocation(), diag::err_expected)
678 << tok::colon << LHS.getRange() << RHS.getRange();
679 PP.Diag(OpLoc, diag::note_matching) << tok::question;
683 PP.LexNonComment(PeekTok);
685 // Evaluate the value after the :.
686 bool AfterColonLive = ValueLive && LHS.Val == 0;
687 PPValue AfterColonVal(LHS.getBitWidth());
689 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
692 // Parse anything after the : with the same precedence as ?. We allow
693 // things of equal precedence because ?: is right associative.
694 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
695 PeekTok, AfterColonLive, PP))
698 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
699 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
700 RHS.setEnd(AfterColonVal.getRange().getEnd());
702 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
703 // either operand is unsigned.
704 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
706 // Figure out the precedence of the token after the : part.
707 PeekPrec = getPrecedence(PeekTok.getKind());
711 // Don't allow :'s to float around without being part of ?: exprs.
712 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
713 << LHS.getRange() << RHS.getRange();
717 // If this operator is live and overflowed, report the issue.
718 if (Overflow && ValueLive)
719 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
720 << LHS.getRange() << RHS.getRange();
722 // Put the result back into 'LHS' for our next iteration.
724 LHS.setEnd(RHS.getRange().getEnd());
728 /// EvaluateDirectiveExpression - Evaluate an integer constant expression that
729 /// may occur after a #if or #elif directive. If the expression is equivalent
730 /// to "!defined(X)" return X in IfNDefMacro.
731 bool Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
732 SaveAndRestore<bool> PPDir(ParsingIfOrElifDirective, true);
733 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
734 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
735 // in which case a directive is undefined behavior. We want macros to be able
736 // to recursively expand in order to get more gcc-list behavior, so we force
737 // DisableMacroExpansion to false and restore it when we're done parsing the
739 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
740 DisableMacroExpansion = false;
742 // Peek ahead one token.
746 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
747 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
749 PPValue ResVal(BitWidth);
751 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
752 // Parse error, skip the rest of the macro line.
753 if (Tok.isNot(tok::eod))
754 DiscardUntilEndOfDirective();
756 // Restore 'DisableMacroExpansion'.
757 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
761 // If we are at the end of the expression after just parsing a value, there
762 // must be no (unparenthesized) binary operators involved, so we can exit
764 if (Tok.is(tok::eod)) {
765 // If the expression we parsed was of the form !defined(macro), return the
766 // macro in IfNDefMacro.
767 if (DT.State == DefinedTracker::NotDefinedMacro)
768 IfNDefMacro = DT.TheMacro;
770 // Restore 'DisableMacroExpansion'.
771 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
772 return ResVal.Val != 0;
775 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
776 // operator and the stuff after it.
777 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
779 // Parse error, skip the rest of the macro line.
780 if (Tok.isNot(tok::eod))
781 DiscardUntilEndOfDirective();
783 // Restore 'DisableMacroExpansion'.
784 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
788 // If we aren't at the tok::eod token, something bad happened, like an extra
790 if (Tok.isNot(tok::eod)) {
791 Diag(Tok, diag::err_pp_expected_eol);
792 DiscardUntilEndOfDirective();
795 // Restore 'DisableMacroExpansion'.
796 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
797 return ResVal.Val != 0;