1 //===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the Preprocessor::EvaluateDirectiveExpression method,
10 // which parses and evaluates integer constant expressions for #if directives.
12 //===----------------------------------------------------------------------===//
14 // FIXME: implement testing for #assert's.
16 //===----------------------------------------------------------------------===//
18 #include "clang/Lex/Preprocessor.h"
19 #include "clang/Basic/IdentifierTable.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "clang/Basic/TargetInfo.h"
23 #include "clang/Basic/TokenKinds.h"
24 #include "clang/Lex/CodeCompletionHandler.h"
25 #include "clang/Lex/LexDiagnostic.h"
26 #include "clang/Lex/LiteralSupport.h"
27 #include "clang/Lex/MacroInfo.h"
28 #include "clang/Lex/PPCallbacks.h"
29 #include "clang/Lex/Token.h"
30 #include "llvm/ADT/APSInt.h"
31 #include "llvm/ADT/SmallString.h"
32 #include "llvm/ADT/StringRef.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/SaveAndRestore.h"
37 using namespace clang;
41 /// PPValue - Represents the value of a subexpression of a preprocessor
42 /// conditional and the source range covered by it.
50 // Default ctor - Construct an 'invalid' PPValue.
51 PPValue(unsigned BitWidth) : Val(BitWidth) {}
53 // If this value was produced by directly evaluating an identifier, produce
55 IdentifierInfo *getIdentifier() const { return II; }
56 void setIdentifier(IdentifierInfo *II) { this->II = II; }
58 unsigned getBitWidth() const { return Val.getBitWidth(); }
59 bool isUnsigned() const { return Val.isUnsigned(); }
61 SourceRange getRange() const { return Range; }
63 void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
64 void setRange(SourceLocation B, SourceLocation E) {
65 Range.setBegin(B); Range.setEnd(E);
67 void setBegin(SourceLocation L) { Range.setBegin(L); }
68 void setEnd(SourceLocation L) { Range.setEnd(L); }
71 } // end anonymous namespace
73 static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
74 Token &PeekTok, bool ValueLive,
75 bool &IncludedUndefinedIds,
78 /// DefinedTracker - This struct is used while parsing expressions to keep track
79 /// of whether !defined(X) has been seen.
81 /// With this simple scheme, we handle the basic forms:
82 /// !defined(X) and !defined X
83 /// but we also trivially handle (silly) stuff like:
84 /// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
85 struct DefinedTracker {
86 /// Each time a Value is evaluated, it returns information about whether the
87 /// parsed value is of the form defined(X), !defined(X) or is something else.
89 DefinedMacro, // defined(X)
90 NotDefinedMacro, // !defined(X)
91 Unknown // Something else.
93 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
94 /// indicates the macro that was checked.
95 IdentifierInfo *TheMacro;
96 bool IncludedUndefinedIds = false;
99 /// EvaluateDefined - Process a 'defined(sym)' expression.
100 static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
101 bool ValueLive, Preprocessor &PP) {
102 SourceLocation beginLoc(PeekTok.getLocation());
103 Result.setBegin(beginLoc);
105 // Get the next token, don't expand it.
106 PP.LexUnexpandedNonComment(PeekTok);
108 // Two options, it can either be a pp-identifier or a (.
109 SourceLocation LParenLoc;
110 if (PeekTok.is(tok::l_paren)) {
111 // Found a paren, remember we saw it and skip it.
112 LParenLoc = PeekTok.getLocation();
113 PP.LexUnexpandedNonComment(PeekTok);
116 if (PeekTok.is(tok::code_completion)) {
117 if (PP.getCodeCompletionHandler())
118 PP.getCodeCompletionHandler()->CodeCompleteMacroName(false);
119 PP.setCodeCompletionReached();
120 PP.LexUnexpandedNonComment(PeekTok);
123 // If we don't have a pp-identifier now, this is an error.
124 if (PP.CheckMacroName(PeekTok, MU_Other))
127 // Otherwise, we got an identifier, is it defined to something?
128 IdentifierInfo *II = PeekTok.getIdentifierInfo();
129 MacroDefinition Macro = PP.getMacroDefinition(II);
130 Result.Val = !!Macro;
131 Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
132 DT.IncludedUndefinedIds = !Macro;
134 // If there is a macro, mark it used.
135 if (Result.Val != 0 && ValueLive)
136 PP.markMacroAsUsed(Macro.getMacroInfo());
138 // Save macro token for callback.
139 Token macroToken(PeekTok);
141 // If we are in parens, ensure we have a trailing ).
142 if (LParenLoc.isValid()) {
143 // Consume identifier.
144 Result.setEnd(PeekTok.getLocation());
145 PP.LexUnexpandedNonComment(PeekTok);
147 if (PeekTok.isNot(tok::r_paren)) {
148 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_after)
149 << "'defined'" << tok::r_paren;
150 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
154 PP.LexNonComment(PeekTok);
155 Result.setEnd(PeekTok.getLocation());
157 // Consume identifier.
158 Result.setEnd(PeekTok.getLocation());
159 PP.LexNonComment(PeekTok);
163 // Prior to evaluation, macro invocations in the list of preprocessing
164 // tokens that will become the controlling constant expression are replaced
165 // (except for those macro names modified by the 'defined' unary operator),
166 // just as in normal text. If the token 'defined' is generated as a result
167 // of this replacement process or use of the 'defined' unary operator does
168 // not match one of the two specified forms prior to macro replacement, the
169 // behavior is undefined.
170 // This isn't an idle threat, consider this program:
172 // #define BAR defined(FOO)
178 // clang and gcc will pick the #if branch while Visual Studio will take the
179 // #else branch. Emit a warning about this undefined behavior.
180 if (beginLoc.isMacroID()) {
181 bool IsFunctionTypeMacro =
182 PP.getSourceManager()
183 .getSLocEntry(PP.getSourceManager().getFileID(beginLoc))
185 .isFunctionMacroExpansion();
186 // For object-type macros, it's easy to replace
187 // #define FOO defined(BAR)
194 // and doing so makes sense since compilers handle this differently in
195 // practice (see example further up). But for function-type macros,
196 // there is no good way to write
197 // # define FOO(x) (defined(M_ ## x) && M_ ## x)
198 // in a different way, and compilers seem to agree on how to behave here.
199 // So warn by default on object-type macros, but only warn in -pedantic
200 // mode on function-type macros.
201 if (IsFunctionTypeMacro)
202 PP.Diag(beginLoc, diag::warn_defined_in_function_type_macro);
204 PP.Diag(beginLoc, diag::warn_defined_in_object_type_macro);
207 // Invoke the 'defined' callback.
208 if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
209 Callbacks->Defined(macroToken, Macro,
210 SourceRange(beginLoc, PeekTok.getLocation()));
213 // Success, remember that we saw defined(X).
214 DT.State = DefinedTracker::DefinedMacro;
219 /// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
220 /// return the computed value in Result. Return true if there was an error
221 /// parsing. This function also returns information about the form of the
222 /// expression in DT. See above for information on what DT means.
224 /// If ValueLive is false, then this value is being evaluated in a context where
225 /// the result is not used. As such, avoid diagnostics that relate to
227 static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
228 bool ValueLive, Preprocessor &PP) {
229 DT.State = DefinedTracker::Unknown;
231 Result.setIdentifier(nullptr);
233 if (PeekTok.is(tok::code_completion)) {
234 if (PP.getCodeCompletionHandler())
235 PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
236 PP.setCodeCompletionReached();
237 PP.LexNonComment(PeekTok);
240 switch (PeekTok.getKind()) {
242 // If this token's spelling is a pp-identifier, check to see if it is
243 // 'defined' or if it is a macro. Note that we check here because many
244 // keywords are pp-identifiers, so we can't check the kind.
245 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
246 // Handle "defined X" and "defined(X)".
247 if (II->isStr("defined"))
248 return EvaluateDefined(Result, PeekTok, DT, ValueLive, PP);
250 if (!II->isCPlusPlusOperatorKeyword()) {
251 // If this identifier isn't 'defined' or one of the special
252 // preprocessor keywords and it wasn't macro expanded, it turns
255 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
257 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
258 Result.setIdentifier(II);
259 Result.setRange(PeekTok.getLocation());
260 DT.IncludedUndefinedIds = true;
261 PP.LexNonComment(PeekTok);
265 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
269 // If there is no expression, report and exit.
270 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
272 case tok::numeric_constant: {
273 SmallString<64> IntegerBuffer;
274 bool NumberInvalid = false;
275 StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
278 return true; // a diagnostic was already reported
280 NumericLiteralParser Literal(Spelling, PeekTok.getLocation(), PP);
281 if (Literal.hadError)
282 return true; // a diagnostic was already reported.
284 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
285 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
288 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
290 // Complain about, and drop, any ud-suffix.
291 if (Literal.hasUDSuffix())
292 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1;
294 // 'long long' is a C99 or C++11 feature.
295 if (!PP.getLangOpts().C99 && Literal.isLongLong) {
296 if (PP.getLangOpts().CPlusPlus)
298 PP.getLangOpts().CPlusPlus11 ?
299 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
301 PP.Diag(PeekTok, diag::ext_c99_longlong);
304 // Parse the integer literal into Result.
305 if (Literal.GetIntegerValue(Result.Val)) {
306 // Overflow parsing integer literal.
308 PP.Diag(PeekTok, diag::err_integer_literal_too_large)
310 Result.Val.setIsUnsigned(true);
312 // Set the signedness of the result to match whether there was a U suffix
314 Result.Val.setIsUnsigned(Literal.isUnsigned);
316 // Detect overflow based on whether the value is signed. If signed
317 // and if the value is too large, emit a warning "integer constant is so
318 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
320 if (!Literal.isUnsigned && Result.Val.isNegative()) {
321 // Octal, hexadecimal, and binary literals are implicitly unsigned if
322 // the value does not fit into a signed integer type.
323 if (ValueLive && Literal.getRadix() == 10)
324 PP.Diag(PeekTok, diag::ext_integer_literal_too_large_for_signed);
325 Result.Val.setIsUnsigned(true);
329 // Consume the token.
330 Result.setRange(PeekTok.getLocation());
331 PP.LexNonComment(PeekTok);
334 case tok::char_constant: // 'x'
335 case tok::wide_char_constant: // L'x'
336 case tok::utf8_char_constant: // u8'x'
337 case tok::utf16_char_constant: // u'x'
338 case tok::utf32_char_constant: { // U'x'
339 // Complain about, and drop, any ud-suffix.
340 if (PeekTok.hasUDSuffix())
341 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
343 SmallString<32> CharBuffer;
344 bool CharInvalid = false;
345 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
349 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
350 PeekTok.getLocation(), PP, PeekTok.getKind());
351 if (Literal.hadError())
352 return true; // A diagnostic was already emitted.
354 // Character literals are always int or wchar_t, expand to intmax_t.
355 const TargetInfo &TI = PP.getTargetInfo();
357 if (Literal.isMultiChar())
358 NumBits = TI.getIntWidth();
359 else if (Literal.isWide())
360 NumBits = TI.getWCharWidth();
361 else if (Literal.isUTF16())
362 NumBits = TI.getChar16Width();
363 else if (Literal.isUTF32())
364 NumBits = TI.getChar32Width();
365 else // char or char8_t
366 NumBits = TI.getCharWidth();
369 llvm::APSInt Val(NumBits);
371 Val = Literal.getValue();
372 // Set the signedness. UTF-16 and UTF-32 are always unsigned
373 if (Literal.isWide())
374 Val.setIsUnsigned(!TargetInfo::isTypeSigned(TI.getWCharType()));
375 else if (!Literal.isUTF16() && !Literal.isUTF32())
376 Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned);
378 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
379 Result.Val = Val.extend(Result.Val.getBitWidth());
381 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
382 "intmax_t smaller than char/wchar_t?");
386 // Consume the token.
387 Result.setRange(PeekTok.getLocation());
388 PP.LexNonComment(PeekTok);
392 SourceLocation Start = PeekTok.getLocation();
393 PP.LexNonComment(PeekTok); // Eat the (.
394 // Parse the value and if there are any binary operators involved, parse
396 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
398 // If this is a silly value like (X), which doesn't need parens, check for
400 if (PeekTok.is(tok::r_paren)) {
401 // Just use DT unmodified as our result.
403 // Otherwise, we have something like (x+y), and we consumed '(x'.
404 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive,
405 DT.IncludedUndefinedIds, PP))
408 if (PeekTok.isNot(tok::r_paren)) {
409 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
410 << Result.getRange();
411 PP.Diag(Start, diag::note_matching) << tok::l_paren;
414 DT.State = DefinedTracker::Unknown;
416 Result.setRange(Start, PeekTok.getLocation());
417 Result.setIdentifier(nullptr);
418 PP.LexNonComment(PeekTok); // Eat the ).
422 SourceLocation Start = PeekTok.getLocation();
423 // Unary plus doesn't modify the value.
424 PP.LexNonComment(PeekTok);
425 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
426 Result.setBegin(Start);
427 Result.setIdentifier(nullptr);
431 SourceLocation Loc = PeekTok.getLocation();
432 PP.LexNonComment(PeekTok);
433 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
434 Result.setBegin(Loc);
435 Result.setIdentifier(nullptr);
437 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
438 Result.Val = -Result.Val;
440 // -MININT is the only thing that overflows. Unsigned never overflows.
441 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
443 // If this operator is live and overflowed, report the issue.
444 if (Overflow && ValueLive)
445 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
447 DT.State = DefinedTracker::Unknown;
452 SourceLocation Start = PeekTok.getLocation();
453 PP.LexNonComment(PeekTok);
454 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
455 Result.setBegin(Start);
456 Result.setIdentifier(nullptr);
458 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
459 Result.Val = ~Result.Val;
460 DT.State = DefinedTracker::Unknown;
465 SourceLocation Start = PeekTok.getLocation();
466 PP.LexNonComment(PeekTok);
467 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
468 Result.setBegin(Start);
469 Result.Val = !Result.Val;
470 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
471 Result.Val.setIsUnsigned(false);
472 Result.setIdentifier(nullptr);
474 if (DT.State == DefinedTracker::DefinedMacro)
475 DT.State = DefinedTracker::NotDefinedMacro;
476 else if (DT.State == DefinedTracker::NotDefinedMacro)
477 DT.State = DefinedTracker::DefinedMacro;
482 Result.Val = PeekTok.getKind() == tok::kw_true;
483 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
484 Result.setIdentifier(PeekTok.getIdentifierInfo());
485 Result.setRange(PeekTok.getLocation());
486 PP.LexNonComment(PeekTok);
489 // FIXME: Handle #assert
493 /// getPrecedence - Return the precedence of the specified binary operator
494 /// token. This returns:
495 /// ~0 - Invalid token.
496 /// 14 -> 3 - various operators.
498 static unsigned getPrecedence(tok::TokenKind Kind) {
503 case tok::star: return 14;
505 case tok::minus: return 13;
507 case tok::greatergreater: return 12;
510 case tok::greaterequal:
511 case tok::greater: return 11;
512 case tok::exclaimequal:
513 case tok::equalequal: return 10;
514 case tok::amp: return 9;
515 case tok::caret: return 8;
516 case tok::pipe: return 7;
517 case tok::ampamp: return 6;
518 case tok::pipepipe: return 5;
519 case tok::question: return 4;
520 case tok::comma: return 3;
521 case tok::colon: return 2;
522 case tok::r_paren: return 0;// Lowest priority, end of expr.
523 case tok::eod: return 0;// Lowest priority, end of directive.
527 static void diagnoseUnexpectedOperator(Preprocessor &PP, PPValue &LHS,
529 if (Tok.is(tok::l_paren) && LHS.getIdentifier())
530 PP.Diag(LHS.getRange().getBegin(), diag::err_pp_expr_bad_token_lparen)
531 << LHS.getIdentifier();
533 PP.Diag(Tok.getLocation(), diag::err_pp_expr_bad_token_binop)
537 /// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
538 /// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
540 /// If ValueLive is false, then this value is being evaluated in a context where
541 /// the result is not used. As such, avoid diagnostics that relate to
542 /// evaluation, such as division by zero warnings.
543 static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
544 Token &PeekTok, bool ValueLive,
545 bool &IncludedUndefinedIds,
547 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
548 // If this token isn't valid, report the error.
549 if (PeekPrec == ~0U) {
550 diagnoseUnexpectedOperator(PP, LHS, PeekTok);
555 // If this token has a lower precedence than we are allowed to parse, return
556 // it so that higher levels of the recursion can parse it.
557 if (PeekPrec < MinPrec)
560 tok::TokenKind Operator = PeekTok.getKind();
562 // If this is a short-circuiting operator, see if the RHS of the operator is
563 // dead. Note that this cannot just clobber ValueLive. Consider
564 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
565 // this example, the RHS of the && being dead does not make the rest of the
568 if (Operator == tok::ampamp && LHS.Val == 0)
569 RHSIsLive = false; // RHS of "0 && x" is dead.
570 else if (Operator == tok::pipepipe && LHS.Val != 0)
571 RHSIsLive = false; // RHS of "1 || x" is dead.
572 else if (Operator == tok::question && LHS.Val == 0)
573 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
575 RHSIsLive = ValueLive;
577 // Consume the operator, remembering the operator's location for reporting.
578 SourceLocation OpLoc = PeekTok.getLocation();
579 PP.LexNonComment(PeekTok);
581 PPValue RHS(LHS.getBitWidth());
582 // Parse the RHS of the operator.
584 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
585 IncludedUndefinedIds = DT.IncludedUndefinedIds;
587 // Remember the precedence of this operator and get the precedence of the
588 // operator immediately to the right of the RHS.
589 unsigned ThisPrec = PeekPrec;
590 PeekPrec = getPrecedence(PeekTok.getKind());
592 // If this token isn't valid, report the error.
593 if (PeekPrec == ~0U) {
594 diagnoseUnexpectedOperator(PP, RHS, PeekTok);
598 // Decide whether to include the next binop in this subexpression. For
599 // example, when parsing x+y*z and looking at '*', we want to recursively
600 // handle y*z as a single subexpression. We do this because the precedence
601 // of * is higher than that of +. The only strange case we have to handle
602 // here is for the ?: operator, where the precedence is actually lower than
603 // the LHS of the '?'. The grammar rule is:
605 // conditional-expression ::=
606 // logical-OR-expression ? expression : conditional-expression
607 // where 'expression' is actually comma-expression.
609 if (Operator == tok::question)
610 // The RHS of "?" should be maximally consumed as an expression.
611 RHSPrec = getPrecedence(tok::comma);
612 else // All others should munch while higher precedence.
613 RHSPrec = ThisPrec+1;
615 if (PeekPrec >= RHSPrec) {
616 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive,
617 IncludedUndefinedIds, PP))
619 PeekPrec = getPrecedence(PeekTok.getKind());
621 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
623 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
624 // either operand is unsigned.
625 llvm::APSInt Res(LHS.getBitWidth());
627 case tok::question: // No UAC for x and y in "x ? y : z".
628 case tok::lessless: // Shift amount doesn't UAC with shift value.
629 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
630 case tok::comma: // Comma operands are not subject to UACs.
631 case tok::pipepipe: // Logical || does not do UACs.
632 case tok::ampamp: // Logical && does not do UACs.
635 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
636 // If this just promoted something from signed to unsigned, and if the
637 // value was negative, warn about it.
638 if (ValueLive && Res.isUnsigned()) {
639 if (!LHS.isUnsigned() && LHS.Val.isNegative())
640 PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 0
641 << LHS.Val.toString(10, true) + " to " +
642 LHS.Val.toString(10, false)
643 << LHS.getRange() << RHS.getRange();
644 if (!RHS.isUnsigned() && RHS.Val.isNegative())
645 PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 1
646 << RHS.Val.toString(10, true) + " to " +
647 RHS.Val.toString(10, false)
648 << LHS.getRange() << RHS.getRange();
650 LHS.Val.setIsUnsigned(Res.isUnsigned());
651 RHS.Val.setIsUnsigned(Res.isUnsigned());
654 bool Overflow = false;
656 default: llvm_unreachable("Unknown operator token!");
659 Res = LHS.Val % RHS.Val;
660 else if (ValueLive) {
661 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
662 << LHS.getRange() << RHS.getRange();
668 if (LHS.Val.isSigned())
669 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
671 Res = LHS.Val / RHS.Val;
672 } else if (ValueLive) {
673 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
674 << LHS.getRange() << RHS.getRange();
681 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
683 Res = LHS.Val * RHS.Val;
685 case tok::lessless: {
686 // Determine whether overflow is about to happen.
687 if (LHS.isUnsigned())
688 Res = LHS.Val.ushl_ov(RHS.Val, Overflow);
690 Res = llvm::APSInt(LHS.Val.sshl_ov(RHS.Val, Overflow), false);
693 case tok::greatergreater: {
694 // Determine whether overflow is about to happen.
695 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
696 if (ShAmt >= LHS.getBitWidth()) {
698 ShAmt = LHS.getBitWidth()-1;
700 Res = LHS.Val >> ShAmt;
704 if (LHS.isUnsigned())
705 Res = LHS.Val + RHS.Val;
707 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
710 if (LHS.isUnsigned())
711 Res = LHS.Val - RHS.Val;
713 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
716 Res = LHS.Val <= RHS.Val;
717 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
720 Res = LHS.Val < RHS.Val;
721 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
723 case tok::greaterequal:
724 Res = LHS.Val >= RHS.Val;
725 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
728 Res = LHS.Val > RHS.Val;
729 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
731 case tok::exclaimequal:
732 Res = LHS.Val != RHS.Val;
733 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
735 case tok::equalequal:
736 Res = LHS.Val == RHS.Val;
737 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
740 Res = LHS.Val & RHS.Val;
743 Res = LHS.Val ^ RHS.Val;
746 Res = LHS.Val | RHS.Val;
749 Res = (LHS.Val != 0 && RHS.Val != 0);
750 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
753 Res = (LHS.Val != 0 || RHS.Val != 0);
754 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
757 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
758 // if not being evaluated.
759 if (!PP.getLangOpts().C99 || ValueLive)
760 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
761 << LHS.getRange() << RHS.getRange();
762 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
764 case tok::question: {
765 // Parse the : part of the expression.
766 if (PeekTok.isNot(tok::colon)) {
767 PP.Diag(PeekTok.getLocation(), diag::err_expected)
768 << tok::colon << LHS.getRange() << RHS.getRange();
769 PP.Diag(OpLoc, diag::note_matching) << tok::question;
773 PP.LexNonComment(PeekTok);
775 // Evaluate the value after the :.
776 bool AfterColonLive = ValueLive && LHS.Val == 0;
777 PPValue AfterColonVal(LHS.getBitWidth());
779 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
782 // Parse anything after the : with the same precedence as ?. We allow
783 // things of equal precedence because ?: is right associative.
784 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
785 PeekTok, AfterColonLive,
786 IncludedUndefinedIds, PP))
789 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
790 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
791 RHS.setEnd(AfterColonVal.getRange().getEnd());
793 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
794 // either operand is unsigned.
795 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
797 // Figure out the precedence of the token after the : part.
798 PeekPrec = getPrecedence(PeekTok.getKind());
802 // Don't allow :'s to float around without being part of ?: exprs.
803 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
804 << LHS.getRange() << RHS.getRange();
808 // If this operator is live and overflowed, report the issue.
809 if (Overflow && ValueLive)
810 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
811 << LHS.getRange() << RHS.getRange();
813 // Put the result back into 'LHS' for our next iteration.
815 LHS.setEnd(RHS.getRange().getEnd());
816 RHS.setIdentifier(nullptr);
820 /// EvaluateDirectiveExpression - Evaluate an integer constant expression that
821 /// may occur after a #if or #elif directive. If the expression is equivalent
822 /// to "!defined(X)" return X in IfNDefMacro.
823 Preprocessor::DirectiveEvalResult
824 Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
825 SaveAndRestore<bool> PPDir(ParsingIfOrElifDirective, true);
826 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
827 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
828 // in which case a directive is undefined behavior. We want macros to be able
829 // to recursively expand in order to get more gcc-list behavior, so we force
830 // DisableMacroExpansion to false and restore it when we're done parsing the
832 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
833 DisableMacroExpansion = false;
835 // Peek ahead one token.
839 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
840 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
842 PPValue ResVal(BitWidth);
844 SourceLocation ExprStartLoc = SourceMgr.getExpansionLoc(Tok.getLocation());
845 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
846 // Parse error, skip the rest of the macro line.
847 SourceRange ConditionRange = ExprStartLoc;
848 if (Tok.isNot(tok::eod))
849 ConditionRange = DiscardUntilEndOfDirective();
851 // Restore 'DisableMacroExpansion'.
852 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
854 // We cannot trust the source range from the value because there was a
855 // parse error. Track the range manually -- the end of the directive is the
856 // end of the condition range.
858 DT.IncludedUndefinedIds,
859 {ExprStartLoc, ConditionRange.getEnd()}};
862 // If we are at the end of the expression after just parsing a value, there
863 // must be no (unparenthesized) binary operators involved, so we can exit
865 if (Tok.is(tok::eod)) {
866 // If the expression we parsed was of the form !defined(macro), return the
867 // macro in IfNDefMacro.
868 if (DT.State == DefinedTracker::NotDefinedMacro)
869 IfNDefMacro = DT.TheMacro;
871 // Restore 'DisableMacroExpansion'.
872 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
873 return {ResVal.Val != 0, DT.IncludedUndefinedIds, ResVal.getRange()};
876 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
877 // operator and the stuff after it.
878 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
879 Tok, true, DT.IncludedUndefinedIds, *this)) {
880 // Parse error, skip the rest of the macro line.
881 if (Tok.isNot(tok::eod))
882 DiscardUntilEndOfDirective();
884 // Restore 'DisableMacroExpansion'.
885 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
886 return {false, DT.IncludedUndefinedIds, ResVal.getRange()};
889 // If we aren't at the tok::eod token, something bad happened, like an extra
891 if (Tok.isNot(tok::eod)) {
892 Diag(Tok, diag::err_pp_expected_eol);
893 DiscardUntilEndOfDirective();
896 // Restore 'DisableMacroExpansion'.
897 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
898 return {ResVal.Val != 0, DT.IncludedUndefinedIds, ResVal.getRange()};