Index: lib/AST/ExprConstant.cpp =================================================================== --- lib/AST/ExprConstant.cpp +++ lib/AST/ExprConstant.cpp @@ -8155,6 +8155,121 @@ case Builtin::BIomp_is_initial_device: // We can decide statically which value the runtime would return if called. return Success(Info.getLangOpts().OpenMPIsDevice ? 0 : 1, E); + case Builtin::BI__builtin_add_overflow: + case Builtin::BI__builtin_sub_overflow: + case Builtin::BI__builtin_mul_overflow: + case Builtin::BI__builtin_sadd_overflow: + case Builtin::BI__builtin_uadd_overflow: + case Builtin::BI__builtin_uaddl_overflow: + case Builtin::BI__builtin_uaddll_overflow: + case Builtin::BI__builtin_usub_overflow: + case Builtin::BI__builtin_usubl_overflow: + case Builtin::BI__builtin_usubll_overflow: + case Builtin::BI__builtin_umul_overflow: + case Builtin::BI__builtin_umull_overflow: + case Builtin::BI__builtin_umulll_overflow: + case Builtin::BI__builtin_saddl_overflow: + case Builtin::BI__builtin_saddll_overflow: + case Builtin::BI__builtin_ssub_overflow: + case Builtin::BI__builtin_ssubl_overflow: + case Builtin::BI__builtin_ssubll_overflow: + case Builtin::BI__builtin_smul_overflow: + case Builtin::BI__builtin_smull_overflow: + case Builtin::BI__builtin_smulll_overflow: { + LValue ResultLValue; + APSInt LHS, RHS; + + QualType ResultType = E->getArg(2)->getType()->getPointeeType(); + if (!EvaluateInteger(E->getArg(0), LHS, Info) || + !EvaluateInteger(E->getArg(1), RHS, Info) || + !EvaluatePointer(E->getArg(2), ResultLValue, Info)) + return false; + + APSInt Result; + bool DidOverflow = false; + + // If the types don't have to match, enlarge all 3 to the largest of them. + if (BuiltinOp == Builtin::BI__builtin_add_overflow || + BuiltinOp == Builtin::BI__builtin_sub_overflow || + BuiltinOp == Builtin::BI__builtin_mul_overflow) { + bool IsSigned = LHS.isSigned() || RHS.isSigned() || + ResultType->isSignedIntegerOrEnumerationType(); + bool AllSigned = LHS.isSigned() && RHS.isSigned() && + ResultType->isSignedIntegerOrEnumerationType(); + uint64_t LHSSize = LHS.getBitWidth(); + uint64_t RHSSize = RHS.getBitWidth(); + uint64_t ResultSize = Info.Ctx.getTypeSize(ResultType); + uint64_t MaxBits = std::max(std::max(LHSSize, RHSSize), ResultSize); + + // Add an additional bit if the signedness isn't uniformly agreed to. We + // could do this ONLY if there is a signed and an unsigned that both have + // MaxBits, but the code to check that is pretty nasty. The issue will be + // caught in the shrink-to-result later anyway. + if (IsSigned && !AllSigned) + ++MaxBits; + + LHS = APSInt(IsSigned ? LHS.sextOrSelf(MaxBits) : LHS.zextOrSelf(MaxBits), + !IsSigned); + RHS = APSInt(IsSigned ? RHS.sextOrSelf(MaxBits) : RHS.zextOrSelf(MaxBits), + !IsSigned); + Result = APSInt(MaxBits, !IsSigned); + } + + // Find largest int. + switch (BuiltinOp) { + default: + llvm_unreachable("Invalid value for BuiltinOp"); + case Builtin::BI__builtin_add_overflow: + case Builtin::BI__builtin_sadd_overflow: + case Builtin::BI__builtin_saddl_overflow: + case Builtin::BI__builtin_saddll_overflow: + case Builtin::BI__builtin_uadd_overflow: + case Builtin::BI__builtin_uaddl_overflow: + case Builtin::BI__builtin_uaddll_overflow: + Result = LHS.isSigned() ? LHS.sadd_ov(RHS, DidOverflow) + : LHS.uadd_ov(RHS, DidOverflow); + break; + case Builtin::BI__builtin_sub_overflow: + case Builtin::BI__builtin_ssub_overflow: + case Builtin::BI__builtin_ssubl_overflow: + case Builtin::BI__builtin_ssubll_overflow: + case Builtin::BI__builtin_usub_overflow: + case Builtin::BI__builtin_usubl_overflow: + case Builtin::BI__builtin_usubll_overflow: + Result = LHS.isSigned() ? LHS.ssub_ov(RHS, DidOverflow) + : LHS.usub_ov(RHS, DidOverflow); + break; + case Builtin::BI__builtin_mul_overflow: + case Builtin::BI__builtin_smul_overflow: + case Builtin::BI__builtin_smull_overflow: + case Builtin::BI__builtin_smulll_overflow: + case Builtin::BI__builtin_umul_overflow: + case Builtin::BI__builtin_umull_overflow: + case Builtin::BI__builtin_umulll_overflow: + Result = LHS.isSigned() ? LHS.smul_ov(RHS, DidOverflow) + : LHS.umul_ov(RHS, DidOverflow); + break; + } + + // In the case where multiple sizes are allowed, truncate and see if + // the values are the same. + if (BuiltinOp == Builtin::BI__builtin_add_overflow || + BuiltinOp == Builtin::BI__builtin_sub_overflow || + BuiltinOp == Builtin::BI__builtin_mul_overflow) { + // It won't GROW, since that isn't possible, so use this to allow + // TruncOrSelf. + APSInt Temp = Result.extOrTrunc(Info.Ctx.getTypeSize(ResultType)); + Temp.setIsSigned(ResultType->isSignedIntegerOrEnumerationType()); + + if (!APSInt::isSameValue(Temp, Result)) + DidOverflow = true; + Result = Temp; + } + + APValue APV{Result}; + handleAssignment(Info, E, ResultLValue, ResultType, APV); + return Success(DidOverflow, E); + } } } Index: lib/Sema/SemaChecking.cpp =================================================================== --- lib/Sema/SemaChecking.cpp +++ lib/Sema/SemaChecking.cpp @@ -197,13 +197,17 @@ // First two arguments should be integers. for (unsigned I = 0; I < 2; ++I) { - Expr *Arg = TheCall->getArg(I); - QualType Ty = Arg->getType(); + ExprResult Arg = TheCall->getArg(I); + QualType Ty = Arg.get()->getType(); if (!Ty->isIntegerType()) { - S.Diag(Arg->getLocStart(), diag::err_overflow_builtin_must_be_int) - << Ty << Arg->getSourceRange(); + S.Diag(Arg.get()->getLocStart(), diag::err_overflow_builtin_must_be_int) + << Ty << Arg.get()->getSourceRange(); return true; } + InitializedEntity Entity = InitializedEntity::InitializeParameter( + S.getASTContext(), Ty, /*consume*/ false); + Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg); + TheCall->setArg(I, Arg.get()); } // Third argument should be a pointer to a non-const integer. Index: test/SemaCXX/builtins-overflow.cpp =================================================================== --- test/SemaCXX/builtins-overflow.cpp +++ test/SemaCXX/builtins-overflow.cpp @@ -0,0 +1,74 @@ +// RUN: %clang_cc1 -fsyntax-only -std=c++17 -verify %s +// expected-no-diagnostics + +#include +#define OVERFLOWS 103 + + +template +constexpr RET add(LHS &&lhs, RHS &&rhs) { + RET sum{}; + return __builtin_add_overflow(lhs, rhs, &sum) ? OVERFLOWS : sum; +} + +static_assert(add(static_cast(120), static_cast(10)) == 130); +static_assert(add(static_cast(120), static_cast(10)) == 130); +static_assert(add(static_cast(120), static_cast(10)) == OVERFLOWS); +static_assert(add(INT_MAX, INT_MAX) == static_cast(INT_MAX) * 2u); +static_assert(add(static_cast(INT_MAX), 1u) == OVERFLOWS); + +static_assert(add(17, 22) == 39); +static_assert(add(INT_MAX - 22, 23) == OVERFLOWS); +static_assert(add(INT_MIN + 22, -23) == OVERFLOWS); + +template +constexpr RET sub(LHS &&lhs, RHS &&rhs) { + RET sum{}; + return __builtin_sub_overflow(lhs, rhs, &sum) ? OVERFLOWS : sum; +} + +static_assert(sub(static_cast(0),static_cast(1)) == OVERFLOWS); +static_assert(sub(static_cast(0),static_cast(1)) == -1); +static_assert(sub(static_cast(0),static_cast(1)) == OVERFLOWS); + +static_assert(sub(17,22) == -5); +static_assert(sub(INT_MAX - 22, -23) == OVERFLOWS); +static_assert(sub(INT_MIN + 22, 23) == OVERFLOWS); + +template +constexpr RET mul(LHS &&lhs, RHS &&rhs) { + RET sum{}; + return __builtin_mul_overflow(lhs, rhs, &sum) ? OVERFLOWS : sum; +} + +static_assert(mul(17,22) == 374); +static_assert(mul(INT_MAX / 22, 23) == OVERFLOWS); +static_assert(mul(INT_MIN / 22, -23) == OVERFLOWS); + +constexpr auto sadd(int lhs, int rhs) { + int sum{}; + return __builtin_sadd_overflow(lhs, rhs, &sum) ? OVERFLOWS : sum; +} + +static_assert(sadd(17,22) == 39); +static_assert(sadd(INT_MAX - 22, 23) == OVERFLOWS); +static_assert(sadd(INT_MIN + 22, -23) == OVERFLOWS); + +constexpr auto ssub(int lhs, int rhs) { + int sum{}; + return __builtin_ssub_overflow(lhs, rhs, &sum) ? OVERFLOWS : sum; +} + +static_assert(ssub(17,22) == -5); +static_assert(ssub(INT_MAX - 22, -23) == OVERFLOWS); +static_assert(ssub(INT_MIN + 22, 23) == OVERFLOWS); + +constexpr auto smul(int lhs, int rhs) { + int sum{}; + return __builtin_smul_overflow(lhs, rhs, &sum) ? OVERFLOWS : sum; +} + +static_assert(smul(17,22) == 374); +static_assert(smul(INT_MAX / 22, 23) == OVERFLOWS); +static_assert(smul(INT_MIN / 22, -23) == OVERFLOWS); +