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/IdentifierTable.h"
21 #include "clang/Basic/SourceLocation.h"
22 #include "clang/Basic/SourceManager.h"
23 #include "clang/Basic/TargetInfo.h"
24 #include "clang/Basic/TokenKinds.h"
25 #include "clang/Lex/CodeCompletionHandler.h"
26 #include "clang/Lex/LexDiagnostic.h"
27 #include "clang/Lex/LiteralSupport.h"
28 #include "clang/Lex/MacroInfo.h"
29 #include "clang/Lex/PPCallbacks.h"
30 #include "clang/Lex/Token.h"
31 #include "llvm/ADT/APSInt.h"
32 #include "llvm/ADT/SmallString.h"
33 #include "llvm/ADT/StringRef.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/SaveAndRestore.h"
38 using namespace clang;
42 /// PPValue - Represents the value of a subexpression of a preprocessor
43 /// conditional and the source range covered by it.
51 // Default ctor - Construct an 'invalid' PPValue.
52 PPValue(unsigned BitWidth) : Val(BitWidth) {}
54 // If this value was produced by directly evaluating an identifier, produce
56 IdentifierInfo *getIdentifier() const { return II; }
57 void setIdentifier(IdentifierInfo *II) { this->II = II; }
59 unsigned getBitWidth() const { return Val.getBitWidth(); }
60 bool isUnsigned() const { return Val.isUnsigned(); }
62 SourceRange getRange() const { return Range; }
64 void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
65 void setRange(SourceLocation B, SourceLocation E) {
66 Range.setBegin(B); Range.setEnd(E);
68 void setBegin(SourceLocation L) { Range.setBegin(L); }
69 void setEnd(SourceLocation L) { Range.setEnd(L); }
72 } // end anonymous namespace
74 static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
75 Token &PeekTok, bool ValueLive,
76 bool &IncludedUndefinedIds,
79 /// DefinedTracker - This struct is used while parsing expressions to keep track
80 /// of whether !defined(X) has been seen.
82 /// With this simple scheme, we handle the basic forms:
83 /// !defined(X) and !defined X
84 /// but we also trivially handle (silly) stuff like:
85 /// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
86 struct DefinedTracker {
87 /// Each time a Value is evaluated, it returns information about whether the
88 /// parsed value is of the form defined(X), !defined(X) or is something else.
90 DefinedMacro, // defined(X)
91 NotDefinedMacro, // !defined(X)
92 Unknown // Something else.
94 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
95 /// indicates the macro that was checked.
96 IdentifierInfo *TheMacro;
97 bool IncludedUndefinedIds = false;
100 /// EvaluateDefined - Process a 'defined(sym)' expression.
101 static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
102 bool ValueLive, Preprocessor &PP) {
103 SourceLocation beginLoc(PeekTok.getLocation());
104 Result.setBegin(beginLoc);
106 // Get the next token, don't expand it.
107 PP.LexUnexpandedNonComment(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.LexUnexpandedNonComment(PeekTok);
117 if (PeekTok.is(tok::code_completion)) {
118 if (PP.getCodeCompletionHandler())
119 PP.getCodeCompletionHandler()->CodeCompleteMacroName(false);
120 PP.setCodeCompletionReached();
121 PP.LexUnexpandedNonComment(PeekTok);
124 // If we don't have a pp-identifier now, this is an error.
125 if (PP.CheckMacroName(PeekTok, MU_Other))
128 // Otherwise, we got an identifier, is it defined to something?
129 IdentifierInfo *II = PeekTok.getIdentifierInfo();
130 MacroDefinition Macro = PP.getMacroDefinition(II);
131 Result.Val = !!Macro;
132 Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
133 DT.IncludedUndefinedIds = !Macro;
135 // If there is a macro, mark it used.
136 if (Result.Val != 0 && ValueLive)
137 PP.markMacroAsUsed(Macro.getMacroInfo());
139 // Save macro token for callback.
140 Token macroToken(PeekTok);
142 // If we are in parens, ensure we have a trailing ).
143 if (LParenLoc.isValid()) {
144 // Consume identifier.
145 Result.setEnd(PeekTok.getLocation());
146 PP.LexUnexpandedNonComment(PeekTok);
148 if (PeekTok.isNot(tok::r_paren)) {
149 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_after)
150 << "'defined'" << tok::r_paren;
151 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
155 Result.setEnd(PeekTok.getLocation());
156 PP.LexNonComment(PeekTok);
158 // Consume identifier.
159 Result.setEnd(PeekTok.getLocation());
160 PP.LexNonComment(PeekTok);
164 // Prior to evaluation, macro invocations in the list of preprocessing
165 // tokens that will become the controlling constant expression are replaced
166 // (except for those macro names modified by the 'defined' unary operator),
167 // just as in normal text. If the token 'defined' is generated as a result
168 // of this replacement process or use of the 'defined' unary operator does
169 // not match one of the two specified forms prior to macro replacement, the
170 // behavior is undefined.
171 // This isn't an idle threat, consider this program:
173 // #define BAR defined(FOO)
179 // clang and gcc will pick the #if branch while Visual Studio will take the
180 // #else branch. Emit a warning about this undefined behavior.
181 if (beginLoc.isMacroID()) {
182 bool IsFunctionTypeMacro =
183 PP.getSourceManager()
184 .getSLocEntry(PP.getSourceManager().getFileID(beginLoc))
186 .isFunctionMacroExpansion();
187 // For object-type macros, it's easy to replace
188 // #define FOO defined(BAR)
195 // and doing so makes sense since compilers handle this differently in
196 // practice (see example further up). But for function-type macros,
197 // there is no good way to write
198 // # define FOO(x) (defined(M_ ## x) && M_ ## x)
199 // in a different way, and compilers seem to agree on how to behave here.
200 // So warn by default on object-type macros, but only warn in -pedantic
201 // mode on function-type macros.
202 if (IsFunctionTypeMacro)
203 PP.Diag(beginLoc, diag::warn_defined_in_function_type_macro);
205 PP.Diag(beginLoc, diag::warn_defined_in_object_type_macro);
208 // Invoke the 'defined' callback.
209 if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
210 Callbacks->Defined(macroToken, Macro,
211 SourceRange(beginLoc, PeekTok.getLocation()));
214 // Success, remember that we saw defined(X).
215 DT.State = DefinedTracker::DefinedMacro;
220 /// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
221 /// return the computed value in Result. Return true if there was an error
222 /// parsing. This function also returns information about the form of the
223 /// expression in DT. See above for information on what DT means.
225 /// If ValueLive is false, then this value is being evaluated in a context where
226 /// the result is not used. As such, avoid diagnostics that relate to
228 static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
229 bool ValueLive, Preprocessor &PP) {
230 DT.State = DefinedTracker::Unknown;
232 Result.setIdentifier(nullptr);
234 if (PeekTok.is(tok::code_completion)) {
235 if (PP.getCodeCompletionHandler())
236 PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
237 PP.setCodeCompletionReached();
238 PP.LexNonComment(PeekTok);
241 switch (PeekTok.getKind()) {
243 // If this token's spelling is a pp-identifier, check to see if it is
244 // 'defined' or if it is a macro. Note that we check here because many
245 // keywords are pp-identifiers, so we can't check the kind.
246 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
247 // Handle "defined X" and "defined(X)".
248 if (II->isStr("defined"))
249 return EvaluateDefined(Result, PeekTok, DT, ValueLive, PP);
251 if (!II->isCPlusPlusOperatorKeyword()) {
252 // If this identifier isn't 'defined' or one of the special
253 // preprocessor keywords and it wasn't macro expanded, it turns
256 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
258 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
259 Result.setIdentifier(II);
260 Result.setRange(PeekTok.getLocation());
261 DT.IncludedUndefinedIds = true;
262 PP.LexNonComment(PeekTok);
266 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
270 // If there is no expression, report and exit.
271 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
273 case tok::numeric_constant: {
274 SmallString<64> IntegerBuffer;
275 bool NumberInvalid = false;
276 StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
279 return true; // a diagnostic was already reported
281 NumericLiteralParser Literal(Spelling, PeekTok.getLocation(), PP);
282 if (Literal.hadError)
283 return true; // a diagnostic was already reported.
285 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
286 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
289 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
291 // Complain about, and drop, any ud-suffix.
292 if (Literal.hasUDSuffix())
293 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1;
295 // 'long long' is a C99 or C++11 feature.
296 if (!PP.getLangOpts().C99 && Literal.isLongLong) {
297 if (PP.getLangOpts().CPlusPlus)
299 PP.getLangOpts().CPlusPlus11 ?
300 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
302 PP.Diag(PeekTok, diag::ext_c99_longlong);
305 // Parse the integer literal into Result.
306 if (Literal.GetIntegerValue(Result.Val)) {
307 // Overflow parsing integer literal.
309 PP.Diag(PeekTok, diag::err_integer_literal_too_large)
311 Result.Val.setIsUnsigned(true);
313 // Set the signedness of the result to match whether there was a U suffix
315 Result.Val.setIsUnsigned(Literal.isUnsigned);
317 // Detect overflow based on whether the value is signed. If signed
318 // and if the value is too large, emit a warning "integer constant is so
319 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
321 if (!Literal.isUnsigned && Result.Val.isNegative()) {
322 // Octal, hexadecimal, and binary literals are implicitly unsigned if
323 // the value does not fit into a signed integer type.
324 if (ValueLive && Literal.getRadix() == 10)
325 PP.Diag(PeekTok, diag::ext_integer_literal_too_large_for_signed);
326 Result.Val.setIsUnsigned(true);
330 // Consume the token.
331 Result.setRange(PeekTok.getLocation());
332 PP.LexNonComment(PeekTok);
335 case tok::char_constant: // 'x'
336 case tok::wide_char_constant: // L'x'
337 case tok::utf8_char_constant: // u8'x'
338 case tok::utf16_char_constant: // u'x'
339 case tok::utf32_char_constant: { // U'x'
340 // Complain about, and drop, any ud-suffix.
341 if (PeekTok.hasUDSuffix())
342 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
344 SmallString<32> CharBuffer;
345 bool CharInvalid = false;
346 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
350 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
351 PeekTok.getLocation(), PP, PeekTok.getKind());
352 if (Literal.hadError())
353 return true; // A diagnostic was already emitted.
355 // Character literals are always int or wchar_t, expand to intmax_t.
356 const TargetInfo &TI = PP.getTargetInfo();
358 if (Literal.isMultiChar())
359 NumBits = TI.getIntWidth();
360 else if (Literal.isWide())
361 NumBits = TI.getWCharWidth();
362 else if (Literal.isUTF16())
363 NumBits = TI.getChar16Width();
364 else if (Literal.isUTF32())
365 NumBits = TI.getChar32Width();
366 else // char or char8_t
367 NumBits = TI.getCharWidth();
370 llvm::APSInt Val(NumBits);
372 Val = Literal.getValue();
373 // Set the signedness. UTF-16 and UTF-32 are always unsigned
374 if (Literal.isWide())
375 Val.setIsUnsigned(!TargetInfo::isTypeSigned(TI.getWCharType()));
376 else if (!Literal.isUTF16() && !Literal.isUTF32())
377 Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned);
379 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
380 Result.Val = Val.extend(Result.Val.getBitWidth());
382 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
383 "intmax_t smaller than char/wchar_t?");
387 // Consume the token.
388 Result.setRange(PeekTok.getLocation());
389 PP.LexNonComment(PeekTok);
393 SourceLocation Start = PeekTok.getLocation();
394 PP.LexNonComment(PeekTok); // Eat the (.
395 // Parse the value and if there are any binary operators involved, parse
397 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
399 // If this is a silly value like (X), which doesn't need parens, check for
401 if (PeekTok.is(tok::r_paren)) {
402 // Just use DT unmodified as our result.
404 // Otherwise, we have something like (x+y), and we consumed '(x'.
405 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive,
406 DT.IncludedUndefinedIds, PP))
409 if (PeekTok.isNot(tok::r_paren)) {
410 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
411 << Result.getRange();
412 PP.Diag(Start, diag::note_matching) << tok::l_paren;
415 DT.State = DefinedTracker::Unknown;
417 Result.setRange(Start, PeekTok.getLocation());
418 Result.setIdentifier(nullptr);
419 PP.LexNonComment(PeekTok); // Eat the ).
423 SourceLocation Start = PeekTok.getLocation();
424 // Unary plus doesn't modify the value.
425 PP.LexNonComment(PeekTok);
426 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
427 Result.setBegin(Start);
428 Result.setIdentifier(nullptr);
432 SourceLocation Loc = PeekTok.getLocation();
433 PP.LexNonComment(PeekTok);
434 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
435 Result.setBegin(Loc);
436 Result.setIdentifier(nullptr);
438 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
439 Result.Val = -Result.Val;
441 // -MININT is the only thing that overflows. Unsigned never overflows.
442 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
444 // If this operator is live and overflowed, report the issue.
445 if (Overflow && ValueLive)
446 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
448 DT.State = DefinedTracker::Unknown;
453 SourceLocation Start = PeekTok.getLocation();
454 PP.LexNonComment(PeekTok);
455 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
456 Result.setBegin(Start);
457 Result.setIdentifier(nullptr);
459 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
460 Result.Val = ~Result.Val;
461 DT.State = DefinedTracker::Unknown;
466 SourceLocation Start = PeekTok.getLocation();
467 PP.LexNonComment(PeekTok);
468 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
469 Result.setBegin(Start);
470 Result.Val = !Result.Val;
471 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
472 Result.Val.setIsUnsigned(false);
473 Result.setIdentifier(nullptr);
475 if (DT.State == DefinedTracker::DefinedMacro)
476 DT.State = DefinedTracker::NotDefinedMacro;
477 else if (DT.State == DefinedTracker::NotDefinedMacro)
478 DT.State = DefinedTracker::DefinedMacro;
483 Result.Val = PeekTok.getKind() == tok::kw_true;
484 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
485 Result.setIdentifier(PeekTok.getIdentifierInfo());
486 Result.setRange(PeekTok.getLocation());
487 PP.LexNonComment(PeekTok);
490 // FIXME: Handle #assert
494 /// getPrecedence - Return the precedence of the specified binary operator
495 /// token. This returns:
496 /// ~0 - Invalid token.
497 /// 14 -> 3 - various operators.
499 static unsigned getPrecedence(tok::TokenKind Kind) {
504 case tok::star: return 14;
506 case tok::minus: return 13;
508 case tok::greatergreater: return 12;
511 case tok::greaterequal:
512 case tok::greater: return 11;
513 case tok::exclaimequal:
514 case tok::equalequal: return 10;
515 case tok::amp: return 9;
516 case tok::caret: return 8;
517 case tok::pipe: return 7;
518 case tok::ampamp: return 6;
519 case tok::pipepipe: return 5;
520 case tok::question: return 4;
521 case tok::comma: return 3;
522 case tok::colon: return 2;
523 case tok::r_paren: return 0;// Lowest priority, end of expr.
524 case tok::eod: return 0;// Lowest priority, end of directive.
528 static void diagnoseUnexpectedOperator(Preprocessor &PP, PPValue &LHS,
530 if (Tok.is(tok::l_paren) && LHS.getIdentifier())
531 PP.Diag(LHS.getRange().getBegin(), diag::err_pp_expr_bad_token_lparen)
532 << LHS.getIdentifier();
534 PP.Diag(Tok.getLocation(), diag::err_pp_expr_bad_token_binop)
538 /// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
539 /// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
541 /// If ValueLive is false, then this value is being evaluated in a context where
542 /// the result is not used. As such, avoid diagnostics that relate to
543 /// evaluation, such as division by zero warnings.
544 static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
545 Token &PeekTok, bool ValueLive,
546 bool &IncludedUndefinedIds,
548 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
549 // If this token isn't valid, report the error.
550 if (PeekPrec == ~0U) {
551 diagnoseUnexpectedOperator(PP, LHS, PeekTok);
556 // If this token has a lower precedence than we are allowed to parse, return
557 // it so that higher levels of the recursion can parse it.
558 if (PeekPrec < MinPrec)
561 tok::TokenKind Operator = PeekTok.getKind();
563 // If this is a short-circuiting operator, see if the RHS of the operator is
564 // dead. Note that this cannot just clobber ValueLive. Consider
565 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
566 // this example, the RHS of the && being dead does not make the rest of the
569 if (Operator == tok::ampamp && LHS.Val == 0)
570 RHSIsLive = false; // RHS of "0 && x" is dead.
571 else if (Operator == tok::pipepipe && LHS.Val != 0)
572 RHSIsLive = false; // RHS of "1 || x" is dead.
573 else if (Operator == tok::question && LHS.Val == 0)
574 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
576 RHSIsLive = ValueLive;
578 // Consume the operator, remembering the operator's location for reporting.
579 SourceLocation OpLoc = PeekTok.getLocation();
580 PP.LexNonComment(PeekTok);
582 PPValue RHS(LHS.getBitWidth());
583 // Parse the RHS of the operator.
585 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
586 IncludedUndefinedIds = DT.IncludedUndefinedIds;
588 // Remember the precedence of this operator and get the precedence of the
589 // operator immediately to the right of the RHS.
590 unsigned ThisPrec = PeekPrec;
591 PeekPrec = getPrecedence(PeekTok.getKind());
593 // If this token isn't valid, report the error.
594 if (PeekPrec == ~0U) {
595 diagnoseUnexpectedOperator(PP, RHS, PeekTok);
599 // Decide whether to include the next binop in this subexpression. For
600 // example, when parsing x+y*z and looking at '*', we want to recursively
601 // handle y*z as a single subexpression. We do this because the precedence
602 // of * is higher than that of +. The only strange case we have to handle
603 // here is for the ?: operator, where the precedence is actually lower than
604 // the LHS of the '?'. The grammar rule is:
606 // conditional-expression ::=
607 // logical-OR-expression ? expression : conditional-expression
608 // where 'expression' is actually comma-expression.
610 if (Operator == tok::question)
611 // The RHS of "?" should be maximally consumed as an expression.
612 RHSPrec = getPrecedence(tok::comma);
613 else // All others should munch while higher precedence.
614 RHSPrec = ThisPrec+1;
616 if (PeekPrec >= RHSPrec) {
617 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive,
618 IncludedUndefinedIds, PP))
620 PeekPrec = getPrecedence(PeekTok.getKind());
622 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
624 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
625 // either operand is unsigned.
626 llvm::APSInt Res(LHS.getBitWidth());
628 case tok::question: // No UAC for x and y in "x ? y : z".
629 case tok::lessless: // Shift amount doesn't UAC with shift value.
630 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
631 case tok::comma: // Comma operands are not subject to UACs.
632 case tok::pipepipe: // Logical || does not do UACs.
633 case tok::ampamp: // Logical && does not do UACs.
636 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
637 // If this just promoted something from signed to unsigned, and if the
638 // value was negative, warn about it.
639 if (ValueLive && Res.isUnsigned()) {
640 if (!LHS.isUnsigned() && LHS.Val.isNegative())
641 PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 0
642 << LHS.Val.toString(10, true) + " to " +
643 LHS.Val.toString(10, false)
644 << LHS.getRange() << RHS.getRange();
645 if (!RHS.isUnsigned() && RHS.Val.isNegative())
646 PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 1
647 << RHS.Val.toString(10, true) + " to " +
648 RHS.Val.toString(10, false)
649 << LHS.getRange() << RHS.getRange();
651 LHS.Val.setIsUnsigned(Res.isUnsigned());
652 RHS.Val.setIsUnsigned(Res.isUnsigned());
655 bool Overflow = false;
657 default: llvm_unreachable("Unknown operator token!");
660 Res = LHS.Val % RHS.Val;
661 else if (ValueLive) {
662 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
663 << LHS.getRange() << RHS.getRange();
669 if (LHS.Val.isSigned())
670 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
672 Res = LHS.Val / RHS.Val;
673 } else if (ValueLive) {
674 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
675 << LHS.getRange() << RHS.getRange();
682 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
684 Res = LHS.Val * RHS.Val;
686 case tok::lessless: {
687 // Determine whether overflow is about to happen.
688 if (LHS.isUnsigned())
689 Res = LHS.Val.ushl_ov(RHS.Val, Overflow);
691 Res = llvm::APSInt(LHS.Val.sshl_ov(RHS.Val, Overflow), false);
694 case tok::greatergreater: {
695 // Determine whether overflow is about to happen.
696 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
697 if (ShAmt >= LHS.getBitWidth()) {
699 ShAmt = LHS.getBitWidth()-1;
701 Res = LHS.Val >> ShAmt;
705 if (LHS.isUnsigned())
706 Res = LHS.Val + RHS.Val;
708 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
711 if (LHS.isUnsigned())
712 Res = LHS.Val - RHS.Val;
714 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
717 Res = LHS.Val <= RHS.Val;
718 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
721 Res = LHS.Val < RHS.Val;
722 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
724 case tok::greaterequal:
725 Res = LHS.Val >= RHS.Val;
726 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
729 Res = LHS.Val > RHS.Val;
730 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
732 case tok::exclaimequal:
733 Res = LHS.Val != RHS.Val;
734 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
736 case tok::equalequal:
737 Res = LHS.Val == RHS.Val;
738 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
741 Res = LHS.Val & RHS.Val;
744 Res = LHS.Val ^ RHS.Val;
747 Res = LHS.Val | RHS.Val;
750 Res = (LHS.Val != 0 && RHS.Val != 0);
751 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
754 Res = (LHS.Val != 0 || RHS.Val != 0);
755 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
758 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
759 // if not being evaluated.
760 if (!PP.getLangOpts().C99 || ValueLive)
761 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
762 << LHS.getRange() << RHS.getRange();
763 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
765 case tok::question: {
766 // Parse the : part of the expression.
767 if (PeekTok.isNot(tok::colon)) {
768 PP.Diag(PeekTok.getLocation(), diag::err_expected)
769 << tok::colon << LHS.getRange() << RHS.getRange();
770 PP.Diag(OpLoc, diag::note_matching) << tok::question;
774 PP.LexNonComment(PeekTok);
776 // Evaluate the value after the :.
777 bool AfterColonLive = ValueLive && LHS.Val == 0;
778 PPValue AfterColonVal(LHS.getBitWidth());
780 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
783 // Parse anything after the : with the same precedence as ?. We allow
784 // things of equal precedence because ?: is right associative.
785 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
786 PeekTok, AfterColonLive,
787 IncludedUndefinedIds, PP))
790 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
791 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
792 RHS.setEnd(AfterColonVal.getRange().getEnd());
794 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
795 // either operand is unsigned.
796 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
798 // Figure out the precedence of the token after the : part.
799 PeekPrec = getPrecedence(PeekTok.getKind());
803 // Don't allow :'s to float around without being part of ?: exprs.
804 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
805 << LHS.getRange() << RHS.getRange();
809 // If this operator is live and overflowed, report the issue.
810 if (Overflow && ValueLive)
811 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
812 << LHS.getRange() << RHS.getRange();
814 // Put the result back into 'LHS' for our next iteration.
816 LHS.setEnd(RHS.getRange().getEnd());
817 RHS.setIdentifier(nullptr);
821 /// EvaluateDirectiveExpression - Evaluate an integer constant expression that
822 /// may occur after a #if or #elif directive. If the expression is equivalent
823 /// to "!defined(X)" return X in IfNDefMacro.
824 Preprocessor::DirectiveEvalResult
825 Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
826 SaveAndRestore<bool> PPDir(ParsingIfOrElifDirective, true);
827 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
828 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
829 // in which case a directive is undefined behavior. We want macros to be able
830 // to recursively expand in order to get more gcc-list behavior, so we force
831 // DisableMacroExpansion to false and restore it when we're done parsing the
833 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
834 DisableMacroExpansion = false;
836 // Peek ahead one token.
840 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
841 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
843 PPValue ResVal(BitWidth);
845 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
846 // Parse error, skip the rest of the macro line.
847 if (Tok.isNot(tok::eod))
848 DiscardUntilEndOfDirective();
850 // Restore 'DisableMacroExpansion'.
851 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
852 return {false, DT.IncludedUndefinedIds};
855 // If we are at the end of the expression after just parsing a value, there
856 // must be no (unparenthesized) binary operators involved, so we can exit
858 if (Tok.is(tok::eod)) {
859 // If the expression we parsed was of the form !defined(macro), return the
860 // macro in IfNDefMacro.
861 if (DT.State == DefinedTracker::NotDefinedMacro)
862 IfNDefMacro = DT.TheMacro;
864 // Restore 'DisableMacroExpansion'.
865 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
866 return {ResVal.Val != 0, DT.IncludedUndefinedIds};
869 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
870 // operator and the stuff after it.
871 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
872 Tok, true, DT.IncludedUndefinedIds, *this)) {
873 // Parse error, skip the rest of the macro line.
874 if (Tok.isNot(tok::eod))
875 DiscardUntilEndOfDirective();
877 // Restore 'DisableMacroExpansion'.
878 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
879 return {false, DT.IncludedUndefinedIds};
882 // If we aren't at the tok::eod token, something bad happened, like an extra
884 if (Tok.isNot(tok::eod)) {
885 Diag(Tok, diag::err_pp_expected_eol);
886 DiscardUntilEndOfDirective();
889 // Restore 'DisableMacroExpansion'.
890 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
891 return {ResVal.Val != 0, DT.IncludedUndefinedIds};