diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h index a42c7bb5a9f26..66eafaaab715e 100644 --- a/clang/include/clang/AST/Expr.h +++ b/clang/include/clang/AST/Expr.h @@ -510,15 +510,16 @@ class Expr : public ValueStmt { /// semantically correspond to a bool. bool isKnownToHaveBooleanValue(bool Semantic = true) const; - /// isIntegerConstantExpr - Return the value if this expression is a valid - /// integer constant expression. If not a valid i-c-e, return None and fill - /// in Loc (if specified) with the location of the invalid expression. + /// isIntegerConstantExpr - Return true if this expression is a valid integer + /// constant expression, and, if so, return its value in Result. If not a + /// valid i-c-e, return false and fill in Loc (if specified) with the location + /// of the invalid expression. /// /// Note: This does not perform the implicit conversions required by C++11 /// [expr.const]p5. - Optional getIntegerConstantExpr(const ASTContext &Ctx, - SourceLocation *Loc = nullptr, - bool isEvaluated = true) const; + bool isIntegerConstantExpr(llvm::APSInt &Result, const ASTContext &Ctx, + SourceLocation *Loc = nullptr, + bool isEvaluated = true) const; bool isIntegerConstantExpr(const ASTContext &Ctx, SourceLocation *Loc = nullptr) const; diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index 807028885652d..2ba643f12a82f 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -9471,15 +9471,17 @@ QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, const ConstantArrayType* CAT) -> std::pair { if (VAT) { - Optional TheInt; + llvm::APSInt TheInt; Expr *E = VAT->getSizeExpr(); - if (E && (TheInt = E->getIntegerConstantExpr(*this))) - return std::make_pair(true, *TheInt); - return std::make_pair(false, llvm::APSInt()); + if (E && E->isIntegerConstantExpr(TheInt, *this)) + return std::make_pair(true, TheInt); + else + return std::make_pair(false, TheInt); + } else if (CAT) { + return std::make_pair(true, CAT->getSize()); + } else { + return std::make_pair(false, llvm::APInt()); } - if (CAT) - return std::make_pair(true, CAT->getSize()); - return std::make_pair(false, llvm::APInt()); }; bool HaveLSize, HaveRSize; diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 011dc890496d0..a4dc0ccad1e0f 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -14883,22 +14883,16 @@ bool Expr::isIntegerConstantExpr(const ASTContext &Ctx, return true; } -Optional Expr::getIntegerConstantExpr(const ASTContext &Ctx, - SourceLocation *Loc, - bool isEvaluated) const { +bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, const ASTContext &Ctx, + SourceLocation *Loc, bool isEvaluated) const { assert(!isValueDependent() && "Expression evaluator can't be called on a dependent expression."); - APSInt Value; - - if (Ctx.getLangOpts().CPlusPlus11) { - if (EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc)) - return Value; - return None; - } + if (Ctx.getLangOpts().CPlusPlus11) + return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); if (!isIntegerConstantExpr(Ctx, Loc)) - return None; + return false; // The only possible side-effects here are due to UB discovered in the // evaluation (for instance, INT_MAX + 1). In such a case, we are still @@ -14912,7 +14906,8 @@ Optional Expr::getIntegerConstantExpr(const ASTContext &Ctx, if (!::EvaluateAsInt(this, ExprResult, Ctx, SE_AllowSideEffects, Info)) llvm_unreachable("ICE cannot be evaluated!"); - return ExprResult.Val.getInt(); + Value = ExprResult.Val.getInt(); + return true; } bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const { diff --git a/clang/lib/AST/MicrosoftMangle.cpp b/clang/lib/AST/MicrosoftMangle.cpp index 09579c28061ae..529f301e46964 100644 --- a/clang/lib/AST/MicrosoftMangle.cpp +++ b/clang/lib/AST/MicrosoftMangle.cpp @@ -1372,9 +1372,9 @@ void MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value, void MicrosoftCXXNameMangler::mangleExpression(const Expr *E) { // See if this is a constant expression. - if (Optional Value = - E->getIntegerConstantExpr(Context.getASTContext())) { - mangleIntegerLiteral(*Value, E->getType()->isBooleanType()); + llvm::APSInt Value; + if (E->isIntegerConstantExpr(Value, Context.getASTContext())) { + mangleIntegerLiteral(Value, E->getType()->isBooleanType()); return; } diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 3588e33714d2f..35a93a7889f40 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -4419,9 +4419,11 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, } else { // If this is required to be a constant, constant fold it so that we // know that the generated intrinsic gets a ConstantInt. - ArgValue = llvm::ConstantInt::get( - getLLVMContext(), - *E->getArg(i)->getIntegerConstantExpr(getContext())); + llvm::APSInt Result; + bool IsConst = E->getArg(i)->isIntegerConstantExpr(Result,getContext()); + assert(IsConst && "Constant arg isn't actually constant?"); + (void)IsConst; + ArgValue = llvm::ConstantInt::get(getLLVMContext(), Result); } // If the intrinsic arg type is different from the builtin arg type @@ -5594,14 +5596,13 @@ Value *CodeGenFunction::EmitCommonNeonBuiltinExpr( SmallVectorImpl &Ops, Address PtrOp0, Address PtrOp1, llvm::Triple::ArchType Arch) { // Get the last argument, which specifies the vector type. + llvm::APSInt NeonTypeConst; const Expr *Arg = E->getArg(E->getNumArgs() - 1); - Optional NeonTypeConst = - Arg->getIntegerConstantExpr(getContext()); - if (!NeonTypeConst) + if (!Arg->isIntegerConstantExpr(NeonTypeConst, getContext())) return nullptr; // Determine the type of this overloaded NEON intrinsic. - NeonTypeFlags Type(NeonTypeConst->getZExtValue()); + NeonTypeFlags Type(NeonTypeConst.getZExtValue()); bool Usgn = Type.isUnsigned(); bool Quad = Type.isQuad(); const bool HasLegalHalfType = getTarget().hasLegalHalfType(); @@ -6884,9 +6885,10 @@ Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID, } else { // If this is required to be a constant, constant fold it so that we know // that the generated intrinsic gets a ConstantInt. - Ops.push_back(llvm::ConstantInt::get( - getLLVMContext(), - *E->getArg(i)->getIntegerConstantExpr(getContext()))); + llvm::APSInt Result; + bool IsConst = E->getArg(i)->isIntegerConstantExpr(Result, getContext()); + assert(IsConst && "Constant arg isn't actually constant?"); (void)IsConst; + Ops.push_back(llvm::ConstantInt::get(getLLVMContext(), Result)); } } @@ -7097,9 +7099,9 @@ Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID, // Get the last argument, which specifies the vector type. assert(HasExtraArg); + llvm::APSInt Result; const Expr *Arg = E->getArg(E->getNumArgs()-1); - Optional Result = Arg->getIntegerConstantExpr(getContext()); - if (!Result) + if (!Arg->isIntegerConstantExpr(Result, getContext())) return nullptr; if (BuiltinID == ARM::BI__builtin_arm_vcvtr_f || @@ -7112,7 +7114,7 @@ Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID, Ty = DoubleTy; // Determine whether this is an unsigned conversion or not. - bool usgn = Result->getZExtValue() == 1; + bool usgn = Result.getZExtValue() == 1; unsigned Int = usgn ? Intrinsic::arm_vcvtru : Intrinsic::arm_vcvtr; // Call the appropriate intrinsic. @@ -7121,7 +7123,7 @@ Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID, } // Determine the type of this overloaded NEON intrinsic. - NeonTypeFlags Type = Result->getZExtValue(); + NeonTypeFlags Type(Result.getZExtValue()); bool usgn = Type.isUnsigned(); bool rightShift = false; @@ -7265,7 +7267,11 @@ Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID, template static Integer GetIntegerConstantValue(const Expr *E, ASTContext &Context) { - return E->getIntegerConstantExpr(Context)->getExtValue(); + llvm::APSInt IntVal; + bool IsConst = E->isIntegerConstantExpr(IntVal, Context); + assert(IsConst && "Sema should have checked this was a constant"); + (void)IsConst; + return IntVal.getExtValue(); } static llvm::Value *SignOrZeroExtend(CGBuilderTy &Builder, llvm::Value *V, @@ -7538,13 +7544,13 @@ static Value *EmitAArch64TblBuiltinExpr(CodeGenFunction &CGF, unsigned BuiltinID assert(E->getNumArgs() >= 3); // Get the last argument, which specifies the vector type. + llvm::APSInt Result; const Expr *Arg = E->getArg(E->getNumArgs() - 1); - Optional Result = Arg->getIntegerConstantExpr(CGF.getContext()); - if (!Result) + if (!Arg->isIntegerConstantExpr(Result, CGF.getContext())) return nullptr; // Determine the type of this overloaded NEON intrinsic. - NeonTypeFlags Type = Result->getZExtValue(); + NeonTypeFlags Type(Result.getZExtValue()); llvm::VectorType *Ty = GetNeonType(&CGF, Type); if (!Ty) return nullptr; @@ -8930,9 +8936,11 @@ Value *CodeGenFunction::EmitAArch64BuiltinExpr(unsigned BuiltinID, } else { // If this is required to be a constant, constant fold it so that we know // that the generated intrinsic gets a ConstantInt. - Ops.push_back(llvm::ConstantInt::get( - getLLVMContext(), - *E->getArg(i)->getIntegerConstantExpr(getContext()))); + llvm::APSInt Result; + bool IsConst = E->getArg(i)->isIntegerConstantExpr(Result, getContext()); + assert(IsConst && "Constant arg isn't actually constant?"); + (void)IsConst; + Ops.push_back(llvm::ConstantInt::get(getLLVMContext(), Result)); } } @@ -8947,11 +8955,12 @@ Value *CodeGenFunction::EmitAArch64BuiltinExpr(unsigned BuiltinID, return Result; } + llvm::APSInt Result; const Expr *Arg = E->getArg(E->getNumArgs()-1); NeonTypeFlags Type(0); - if (Optional Result = Arg->getIntegerConstantExpr(getContext())) + if (Arg->isIntegerConstantExpr(Result, getContext())) // Determine the type of this overloaded NEON intrinsic. - Type = NeonTypeFlags(Result->getZExtValue()); + Type = NeonTypeFlags(Result.getZExtValue()); bool usgn = Type.isUnsigned(); bool quad = Type.isQuad(); @@ -11782,8 +11791,10 @@ Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID, // If this is required to be a constant, constant fold it so that we know // that the generated intrinsic gets a ConstantInt. - Ops.push_back(llvm::ConstantInt::get( - getLLVMContext(), *E->getArg(i)->getIntegerConstantExpr(getContext()))); + llvm::APSInt Result; + bool IsConst = E->getArg(i)->isIntegerConstantExpr(Result, getContext()); + assert(IsConst && "Constant arg isn't actually constant?"); (void)IsConst; + Ops.push_back(llvm::ConstantInt::get(getLLVMContext(), Result)); } // These exist so that the builtin that takes an immediate can be bounds @@ -15062,8 +15073,11 @@ Value *CodeGenFunction::EmitSystemZBuiltinExpr(unsigned BuiltinID, llvm::Type *ResultType = ConvertType(E->getType()); Value *X = EmitScalarExpr(E->getArg(0)); // Constant-fold the M4 and M5 mask arguments. - llvm::APSInt M4 = *E->getArg(1)->getIntegerConstantExpr(getContext()); - llvm::APSInt M5 = *E->getArg(2)->getIntegerConstantExpr(getContext()); + llvm::APSInt M4, M5; + bool IsConstM4 = E->getArg(1)->isIntegerConstantExpr(M4, getContext()); + bool IsConstM5 = E->getArg(2)->isIntegerConstantExpr(M5, getContext()); + assert(IsConstM4 && IsConstM5 && "Constant arg isn't actually constant?"); + (void)IsConstM4; (void)IsConstM5; // Check whether this instance can be represented via a LLVM standard // intrinsic. We only support some combinations of M4 and M5. Intrinsic::ID ID = Intrinsic::not_intrinsic; @@ -15118,7 +15132,10 @@ Value *CodeGenFunction::EmitSystemZBuiltinExpr(unsigned BuiltinID, Value *X = EmitScalarExpr(E->getArg(0)); Value *Y = EmitScalarExpr(E->getArg(1)); // Constant-fold the M4 mask argument. - llvm::APSInt M4 = *E->getArg(2)->getIntegerConstantExpr(getContext()); + llvm::APSInt M4; + bool IsConstM4 = E->getArg(2)->isIntegerConstantExpr(M4, getContext()); + assert(IsConstM4 && "Constant arg isn't actually constant?"); + (void)IsConstM4; // Check whether this instance can be represented via a LLVM standard // intrinsic. We only support some values of M4. Intrinsic::ID ID = Intrinsic::not_intrinsic; @@ -15152,7 +15169,10 @@ Value *CodeGenFunction::EmitSystemZBuiltinExpr(unsigned BuiltinID, Value *X = EmitScalarExpr(E->getArg(0)); Value *Y = EmitScalarExpr(E->getArg(1)); // Constant-fold the M4 mask argument. - llvm::APSInt M4 = *E->getArg(2)->getIntegerConstantExpr(getContext()); + llvm::APSInt M4; + bool IsConstM4 = E->getArg(2)->isIntegerConstantExpr(M4, getContext()); + assert(IsConstM4 && "Constant arg isn't actually constant?"); + (void)IsConstM4; // Check whether this instance can be represented via a LLVM standard // intrinsic. We only support some values of M4. Intrinsic::ID ID = Intrinsic::not_intrinsic; @@ -15819,11 +15839,10 @@ CodeGenFunction::EmitNVPTXBuiltinExpr(unsigned BuiltinID, const CallExpr *E) { Address Dst = EmitPointerWithAlignment(E->getArg(0)); Value *Src = EmitScalarExpr(E->getArg(1)); Value *Ldm = EmitScalarExpr(E->getArg(2)); - Optional isColMajorArg = - E->getArg(3)->getIntegerConstantExpr(getContext()); - if (!isColMajorArg) + llvm::APSInt isColMajorArg; + if (!E->getArg(3)->isIntegerConstantExpr(isColMajorArg, getContext())) return nullptr; - bool isColMajor = isColMajorArg->getSExtValue(); + bool isColMajor = isColMajorArg.getSExtValue(); NVPTXMmaLdstInfo II = getNVPTXMmaLdstInfo(BuiltinID); unsigned IID = isColMajor ? II.IID_col : II.IID_row; if (IID == 0) @@ -15864,11 +15883,10 @@ CodeGenFunction::EmitNVPTXBuiltinExpr(unsigned BuiltinID, const CallExpr *E) { Value *Dst = EmitScalarExpr(E->getArg(0)); Address Src = EmitPointerWithAlignment(E->getArg(1)); Value *Ldm = EmitScalarExpr(E->getArg(2)); - Optional isColMajorArg = - E->getArg(3)->getIntegerConstantExpr(getContext()); - if (!isColMajorArg) + llvm::APSInt isColMajorArg; + if (!E->getArg(3)->isIntegerConstantExpr(isColMajorArg, getContext())) return nullptr; - bool isColMajor = isColMajorArg->getSExtValue(); + bool isColMajor = isColMajorArg.getSExtValue(); NVPTXMmaLdstInfo II = getNVPTXMmaLdstInfo(BuiltinID); unsigned IID = isColMajor ? II.IID_col : II.IID_row; if (IID == 0) @@ -15915,20 +15933,16 @@ CodeGenFunction::EmitNVPTXBuiltinExpr(unsigned BuiltinID, const CallExpr *E) { Address SrcA = EmitPointerWithAlignment(E->getArg(1)); Address SrcB = EmitPointerWithAlignment(E->getArg(2)); Address SrcC = EmitPointerWithAlignment(E->getArg(3)); - Optional LayoutArg = - E->getArg(4)->getIntegerConstantExpr(getContext()); - if (!LayoutArg) + llvm::APSInt LayoutArg; + if (!E->getArg(4)->isIntegerConstantExpr(LayoutArg, getContext())) return nullptr; - int Layout = LayoutArg->getSExtValue(); + int Layout = LayoutArg.getSExtValue(); if (Layout < 0 || Layout > 3) return nullptr; llvm::APSInt SatfArg; if (BuiltinID == NVPTX::BI__bmma_m8n8k128_mma_xor_popc_b1) SatfArg = 0; // .b1 does not have satf argument. - else if (Optional OptSatfArg = - E->getArg(5)->getIntegerConstantExpr(getContext())) - SatfArg = *OptSatfArg; - else + else if (!E->getArg(5)->isIntegerConstantExpr(SatfArg, getContext())) return nullptr; bool Satf = SatfArg.getSExtValue(); NVPTXMmaInfo MI = getNVPTXMmaInfo(BuiltinID); @@ -16257,8 +16271,9 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID, case WebAssembly::BI__builtin_wasm_extract_lane_i64x2: case WebAssembly::BI__builtin_wasm_extract_lane_f32x4: case WebAssembly::BI__builtin_wasm_extract_lane_f64x2: { - llvm::APSInt LaneConst = - *E->getArg(1)->getIntegerConstantExpr(getContext()); + llvm::APSInt LaneConst; + if (!E->getArg(1)->isIntegerConstantExpr(LaneConst, getContext())) + llvm_unreachable("Constant arg isn't actually constant?"); Value *Vec = EmitScalarExpr(E->getArg(0)); Value *Lane = llvm::ConstantInt::get(getLLVMContext(), LaneConst); Value *Extract = Builder.CreateExtractElement(Vec, Lane); @@ -16284,8 +16299,9 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID, case WebAssembly::BI__builtin_wasm_replace_lane_i64x2: case WebAssembly::BI__builtin_wasm_replace_lane_f32x4: case WebAssembly::BI__builtin_wasm_replace_lane_f64x2: { - llvm::APSInt LaneConst = - *E->getArg(1)->getIntegerConstantExpr(getContext()); + llvm::APSInt LaneConst; + if (!E->getArg(1)->isIntegerConstantExpr(LaneConst, getContext())) + llvm_unreachable("Constant arg isn't actually constant?"); Value *Vec = EmitScalarExpr(E->getArg(0)); Value *Lane = llvm::ConstantInt::get(getLLVMContext(), LaneConst); Value *Val = EmitScalarExpr(E->getArg(2)); diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index ab29e32929ceb..9e8770573d701 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -3868,17 +3868,15 @@ LValue CodeGenFunction::EmitOMPArraySectionExpr(const OMPArraySectionExpr *E, llvm::APSInt ConstLength; if (Length) { // Idx = LowerBound + Length - 1; - if (Optional CL = Length->getIntegerConstantExpr(C)) { - ConstLength = CL->zextOrTrunc(PointerWidthInBits); + if (Length->isIntegerConstantExpr(ConstLength, C)) { + ConstLength = ConstLength.zextOrTrunc(PointerWidthInBits); Length = nullptr; } auto *LowerBound = E->getLowerBound(); llvm::APSInt ConstLowerBound(PointerWidthInBits, /*isUnsigned=*/false); - if (LowerBound) { - if (Optional LB = LowerBound->getIntegerConstantExpr(C)) { - ConstLowerBound = LB->zextOrTrunc(PointerWidthInBits); - LowerBound = nullptr; - } + if (LowerBound && LowerBound->isIntegerConstantExpr(ConstLowerBound, C)) { + ConstLowerBound = ConstLowerBound.zextOrTrunc(PointerWidthInBits); + LowerBound = nullptr; } if (!Length) --ConstLength; @@ -3915,10 +3913,8 @@ LValue CodeGenFunction::EmitOMPArraySectionExpr(const OMPArraySectionExpr *E, : BaseTy; if (auto *VAT = C.getAsVariableArrayType(ArrayTy)) { Length = VAT->getSizeExpr(); - if (Optional L = Length->getIntegerConstantExpr(C)) { - ConstLength = *L; + if (Length->isIntegerConstantExpr(ConstLength, C)) Length = nullptr; - } } else { auto *CAT = C.getAsConstantArrayType(ArrayTy); ConstLength = CAT->getSize(); diff --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp index f9785e4bea5e2..b354e810974c4 100644 --- a/clang/lib/Sema/SemaAttr.cpp +++ b/clang/lib/Sema/SemaAttr.cpp @@ -300,18 +300,20 @@ void Sema::ActOnPragmaPack(SourceLocation PragmaLoc, PragmaMsStackAction Action, // If specified then alignment must be a "small" power of two. unsigned AlignmentVal = 0; if (Alignment) { - Optional Val; + llvm::APSInt Val; // pack(0) is like pack(), which just works out since that is what // we use 0 for in PackAttr. - if (Alignment->isTypeDependent() || Alignment->isValueDependent() || - !(Val = Alignment->getIntegerConstantExpr(Context)) || - !(*Val == 0 || Val->isPowerOf2()) || Val->getZExtValue() > 16) { + if (Alignment->isTypeDependent() || + Alignment->isValueDependent() || + !Alignment->isIntegerConstantExpr(Val, Context) || + !(Val == 0 || Val.isPowerOf2()) || + Val.getZExtValue() > 16) { Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment); return; // Ignore } - AlignmentVal = (unsigned)Val->getZExtValue(); + AlignmentVal = (unsigned) Val.getZExtValue(); } if (Action == Sema::PSK_Show) { // Show the current alignment, making sure to show the right value diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index c501c706a97bc..efaf36a693061 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -2284,7 +2284,10 @@ bool Sema::CheckARMCoprocessorImmediate(const TargetInfo &TI, if (CoprocArg->isTypeDependent() || CoprocArg->isValueDependent()) return false; - llvm::APSInt CoprocNoAP = *CoprocArg->getIntegerConstantExpr(Context); + llvm::APSInt CoprocNoAP; + bool IsICE = CoprocArg->isIntegerConstantExpr(CoprocNoAP, Context); + (void)IsICE; + assert(IsICE && "Coprocossor immediate is not a constant expression"); int64_t CoprocNo = CoprocNoAP.getExtValue(); assert(CoprocNo >= 0 && "Coprocessor immediate must be non-negative"); @@ -2596,7 +2599,8 @@ bool Sema::CheckBPFBuiltinFunctionCall(unsigned BuiltinID, // The second argument needs to be a constant int Arg = TheCall->getArg(1); - if (!Arg->isIntegerConstantExpr(Context)) { + llvm::APSInt Value; + if (!Arg->isIntegerConstantExpr(Value, Context)) { Diag(Arg->getBeginLoc(), diag::err_preserve_field_info_not_const) << 2 << Arg->getSourceRange(); return true; @@ -3194,10 +3198,11 @@ bool Sema::CheckSystemZBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { if (BuiltinID == SystemZ::BI__builtin_tabort) { Expr *Arg = TheCall->getArg(0); - if (Optional AbortCode = Arg->getIntegerConstantExpr(Context)) - if (AbortCode->getSExtValue() >= 0 && AbortCode->getSExtValue() < 256) - return Diag(Arg->getBeginLoc(), diag::err_systemz_invalid_tabort_code) - << Arg->getSourceRange(); + llvm::APSInt AbortCode(32); + if (Arg->isIntegerConstantExpr(AbortCode, Context) && + AbortCode.getSExtValue() >= 0 && AbortCode.getSExtValue() < 256) + return Diag(Arg->getBeginLoc(), diag::err_systemz_invalid_tabort_code) + << Arg->getSourceRange(); } // For intrinsics which take an immediate value as part of the instruction, @@ -4918,21 +4923,21 @@ ExprResult Sema::BuildAtomicExpr(SourceRange CallRange, SourceRange ExprRange, } if (SubExprs.size() >= 2 && Form != Init) { - if (Optional Result = - SubExprs[1]->getIntegerConstantExpr(Context)) - if (!isValidOrderingForOp(Result->getSExtValue(), Op)) - Diag(SubExprs[1]->getBeginLoc(), - diag::warn_atomic_op_has_invalid_memory_order) - << SubExprs[1]->getSourceRange(); + llvm::APSInt Result(32); + if (SubExprs[1]->isIntegerConstantExpr(Result, Context) && + !isValidOrderingForOp(Result.getSExtValue(), Op)) + Diag(SubExprs[1]->getBeginLoc(), + diag::warn_atomic_op_has_invalid_memory_order) + << SubExprs[1]->getSourceRange(); } if (auto ScopeModel = AtomicExpr::getScopeModel(Op)) { auto *Scope = Args[Args.size() - 1]; - if (Optional Result = - Scope->getIntegerConstantExpr(Context)) { - if (!ScopeModel->isValid(Result->getZExtValue())) - Diag(Scope->getBeginLoc(), diag::err_atomic_op_has_invalid_synch_scope) - << Scope->getSourceRange(); + llvm::APSInt Result(32); + if (Scope->isIntegerConstantExpr(Result, Context) && + !ScopeModel->isValid(Result.getZExtValue())) { + Diag(Scope->getBeginLoc(), diag::err_atomic_op_has_invalid_synch_scope) + << Scope->getSourceRange(); } SubExprs.push_back(Scope); } @@ -5800,7 +5805,8 @@ bool Sema::SemaBuiltinVSX(CallExpr *TheCall) { << TheCall->getSourceRange(); // Check the third argument is a compile time constant - if (!TheCall->getArg(2)->isIntegerConstantExpr(Context)) + llvm::APSInt Value; + if(!TheCall->getArg(2)->isIntegerConstantExpr(Value, Context)) return Diag(TheCall->getBeginLoc(), diag::err_vsx_builtin_nonconstant_argument) << 3 /* argument index */ << TheCall->getDirectCallee() @@ -5895,18 +5901,17 @@ ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { TheCall->getArg(i)->isValueDependent()) continue; - Optional Result; - if (!(Result = TheCall->getArg(i)->getIntegerConstantExpr(Context))) + llvm::APSInt Result(32); + if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) return ExprError(Diag(TheCall->getBeginLoc(), diag::err_shufflevector_nonconstant_argument) << TheCall->getArg(i)->getSourceRange()); // Allow -1 which will be translated to undef in the IR. - if (Result->isSigned() && Result->isAllOnesValue()) + if (Result.isSigned() && Result.isAllOnesValue()) continue; - if (Result->getActiveBits() > 64 || - Result->getZExtValue() >= numElements * 2) + if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) return ExprError(Diag(TheCall->getBeginLoc(), diag::err_shufflevector_argument_too_large) << TheCall->getArg(i)->getSourceRange()); @@ -6153,11 +6158,10 @@ bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum, if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; - Optional R; - if (!(R = Arg->getIntegerConstantExpr(Context))) + if (!Arg->isIntegerConstantExpr(Result, Context)) return Diag(TheCall->getBeginLoc(), diag::err_constant_integer_arg_type) << FDecl->getDeclName() << Arg->getSourceRange(); - Result = *R; + return false; } @@ -10317,15 +10321,14 @@ static IntRange GetExprRange(ASTContext &C, const Expr *E, unsigned MaxWidth, // If the shift amount is a positive constant, drop the width by // that much. - if (Optional shift = - BO->getRHS()->getIntegerConstantExpr(C)) { - if (shift->isNonNegative()) { - unsigned zext = shift->getZExtValue(); - if (zext >= L.Width) - L.Width = (L.NonNegative ? 0 : 1); - else - L.Width -= zext; - } + llvm::APSInt shift; + if (BO->getRHS()->isIntegerConstantExpr(shift, C) && + shift.isNonNegative()) { + unsigned zext = shift.getZExtValue(); + if (zext >= L.Width) + L.Width = (L.NonNegative ? 0 : 1); + else + L.Width -= zext; } return L; @@ -10349,9 +10352,9 @@ static IntRange GetExprRange(ASTContext &C, const Expr *E, unsigned MaxWidth, IntRange L = GetExprRange(C, BO->getLHS(), opWidth, InConstantContext); // If the divisor is constant, use that. - if (Optional divisor = - BO->getRHS()->getIntegerConstantExpr(C)) { - unsigned log2 = divisor->logBase2(); // floor(log_2(divisor)) + llvm::APSInt divisor; + if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) { + unsigned log2 = divisor.logBase2(); // floor(log_2(divisor)) if (log2 >= L.Width) L.Width = (L.NonNegative ? 0 : 1); else @@ -10783,20 +10786,23 @@ static void AnalyzeComparison(Sema &S, BinaryOperator *E) { Expr *RHS = E->getRHS(); if (T->isIntegralType(S.Context)) { - Optional RHSValue = RHS->getIntegerConstantExpr(S.Context); - Optional LHSValue = LHS->getIntegerConstantExpr(S.Context); + llvm::APSInt RHSValue; + llvm::APSInt LHSValue; + + bool IsRHSIntegralLiteral = RHS->isIntegerConstantExpr(RHSValue, S.Context); + bool IsLHSIntegralLiteral = LHS->isIntegerConstantExpr(LHSValue, S.Context); // We don't care about expressions whose result is a constant. - if (RHSValue && LHSValue) + if (IsRHSIntegralLiteral && IsLHSIntegralLiteral) return AnalyzeImpConvsInComparison(S, E); // We only care about expressions where just one side is literal - if ((bool)RHSValue ^ (bool)LHSValue) { + if (IsRHSIntegralLiteral ^ IsLHSIntegralLiteral) { // Is the constant on the RHS or LHS? - const bool RhsConstant = (bool)RHSValue; + const bool RhsConstant = IsRHSIntegralLiteral; Expr *Const = RhsConstant ? RHS : LHS; Expr *Other = RhsConstant ? LHS : RHS; - const llvm::APSInt &Value = RhsConstant ? *RHSValue : *LHSValue; + const llvm::APSInt &Value = RhsConstant ? RHSValue : LHSValue; // Check whether an integer constant comparison results in a value // of 'true' or 'false'. @@ -11754,8 +11760,8 @@ static void CheckImplicitConversion(Sema &S, Expr *E, QualType T, if (SourcePrecision > 0 && TargetPrecision > 0 && SourcePrecision > TargetPrecision) { - if (Optional SourceInt = - E->getIntegerConstantExpr(S.Context)) { + llvm::APSInt SourceInt; + if (E->isIntegerConstantExpr(SourceInt, S.Context)) { // If the source integer is a constant, convert it to the target // floating point type. Issue a warning if the value changes // during the whole conversion. @@ -11763,11 +11769,11 @@ static void CheckImplicitConversion(Sema &S, Expr *E, QualType T, S.Context.getFloatTypeSemantics(QualType(TargetBT, 0))); llvm::APFloat::opStatus ConversionStatus = TargetFloatValue.convertFromAPInt( - *SourceInt, SourceBT->isSignedInteger(), + SourceInt, SourceBT->isSignedInteger(), llvm::APFloat::rmNearestTiesToEven); if (ConversionStatus != llvm::APFloat::opOK) { - std::string PrettySourceValue = SourceInt->toString(10); + std::string PrettySourceValue = SourceInt.toString(10); SmallString<32> PrettyTargetValue; TargetFloatValue.toString(PrettyTargetValue, TargetPrecision); @@ -14118,10 +14124,9 @@ namespace { return; if (Expr *RHS = BinOp->getRHS()) { RHS = RHS->IgnoreParenCasts(); - Optional Value; + llvm::APSInt Value; VarWillBeReased = - (RHS && (Value = RHS->getIntegerConstantExpr(Context)) && - *Value == 0); + (RHS && RHS->isIntegerConstantExpr(Value, Context) && Value == 0); } } } diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index dc0f3d68fde38..f5e375134c293 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -13141,20 +13141,20 @@ void Sema::FinalizeDeclaration(Decl *ThisDecl) { if (!MagicValueExpr) { continue; } - Optional MagicValueInt; - if (!(MagicValueInt = MagicValueExpr->getIntegerConstantExpr(Context))) { + llvm::APSInt MagicValueInt; + if (!MagicValueExpr->isIntegerConstantExpr(MagicValueInt, Context)) { Diag(I->getRange().getBegin(), diag::err_type_tag_for_datatype_not_ice) << LangOpts.CPlusPlus << MagicValueExpr->getSourceRange(); continue; } - if (MagicValueInt->getActiveBits() > 64) { + if (MagicValueInt.getActiveBits() > 64) { Diag(I->getRange().getBegin(), diag::err_type_tag_for_datatype_too_large) << LangOpts.CPlusPlus << MagicValueExpr->getSourceRange(); continue; } - uint64_t MagicValue = MagicValueInt->getZExtValue(); + uint64_t MagicValue = MagicValueInt.getZExtValue(); RegisterTypeTagForDatatype(I->getArgumentKind(), MagicValue, I->getMatchingCType(), diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index ece93cbd6a9bd..1a0594512a606 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -240,9 +240,9 @@ template static bool checkUInt32Argument(Sema &S, const AttrInfo &AI, const Expr *Expr, uint32_t &Val, unsigned Idx = UINT_MAX, bool StrictlyUnsigned = false) { - Optional I = llvm::APSInt(32); + llvm::APSInt I(32); if (Expr->isTypeDependent() || Expr->isValueDependent() || - !(I = Expr->getIntegerConstantExpr(S.Context))) { + !Expr->isIntegerConstantExpr(I, S.Context)) { if (Idx != UINT_MAX) S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type) << &AI << Idx << AANT_ArgumentIntegerConstant @@ -253,19 +253,19 @@ static bool checkUInt32Argument(Sema &S, const AttrInfo &AI, const Expr *Expr, return false; } - if (!I->isIntN(32)) { + if (!I.isIntN(32)) { S.Diag(Expr->getExprLoc(), diag::err_ice_too_large) - << I->toString(10, false) << 32 << /* Unsigned */ 1; + << I.toString(10, false) << 32 << /* Unsigned */ 1; return false; } - if (StrictlyUnsigned && I->isSigned() && I->isNegative()) { + if (StrictlyUnsigned && I.isSigned() && I.isNegative()) { S.Diag(getAttrLoc(AI), diag::err_attribute_requires_positive_integer) << &AI << /*non-negative*/ 1; return false; } - Val = (uint32_t)I->getZExtValue(); + Val = (uint32_t)I.getZExtValue(); return true; } @@ -332,16 +332,16 @@ static bool checkFunctionOrMethodParameterIndex( unsigned NumParams = (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam; - Optional IdxInt; + llvm::APSInt IdxInt; if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || - !(IdxInt = IdxExpr->getIntegerConstantExpr(S.Context))) { + !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type) << &AI << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange(); return false; } - unsigned IdxSource = IdxInt->getLimitedValue(UINT_MAX); + unsigned IdxSource = IdxInt.getLimitedValue(UINT_MAX); if (IdxSource < 1 || (!IV && IdxSource > NumParams)) { S.Diag(getAttrLoc(AI), diag::err_attribute_argument_out_of_bounds) << &AI << AttrArgNum << IdxExpr->getSourceRange(); @@ -1605,8 +1605,8 @@ void Sema::AddAssumeAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E, } if (!E->isValueDependent()) { - Optional I = llvm::APSInt(64); - if (!(I = E->getIntegerConstantExpr(Context))) { + llvm::APSInt I(64); + if (!E->isIntegerConstantExpr(I, Context)) { if (OE) Diag(AttrLoc, diag::err_attribute_argument_n_type) << &TmpAttr << 1 << AANT_ArgumentIntegerConstant @@ -1618,22 +1618,27 @@ void Sema::AddAssumeAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E, return; } - if (!I->isPowerOf2()) { + if (!I.isPowerOf2()) { Diag(AttrLoc, diag::err_alignment_not_power_of_two) << E->getSourceRange(); return; } - if (*I > Sema::MaximumAlignment) + if (I > Sema::MaximumAlignment) Diag(CI.getLoc(), diag::warn_assume_aligned_too_great) << CI.getRange() << Sema::MaximumAlignment; } - if (OE && !OE->isValueDependent() && !OE->isIntegerConstantExpr(Context)) { - Diag(AttrLoc, diag::err_attribute_argument_n_type) - << &TmpAttr << 2 << AANT_ArgumentIntegerConstant - << OE->getSourceRange(); - return; + if (OE) { + if (!OE->isValueDependent()) { + llvm::APSInt I(64); + if (!OE->isIntegerConstantExpr(I, Context)) { + Diag(AttrLoc, diag::err_attribute_argument_n_type) + << &TmpAttr << 2 << AANT_ArgumentIntegerConstant + << OE->getSourceRange(); + return; + } + } } D->addAttr(::new (Context) AssumeAlignedAttr(Context, CI, E, OE)); @@ -2724,36 +2729,36 @@ static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) { unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; if (AL.getNumArgs() > 0) { Expr *E = AL.getArgAsExpr(0); - Optional Idx = llvm::APSInt(32); + llvm::APSInt Idx(32); if (E->isTypeDependent() || E->isValueDependent() || - !(Idx = E->getIntegerConstantExpr(S.Context))) { + !E->isIntegerConstantExpr(Idx, S.Context)) { S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) << AL << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange(); return; } - if (Idx->isSigned() && Idx->isNegative()) { + if (Idx.isSigned() && Idx.isNegative()) { S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero) << E->getSourceRange(); return; } - sentinel = Idx->getZExtValue(); + sentinel = Idx.getZExtValue(); } unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; if (AL.getNumArgs() > 1) { Expr *E = AL.getArgAsExpr(1); - Optional Idx = llvm::APSInt(32); + llvm::APSInt Idx(32); if (E->isTypeDependent() || E->isValueDependent() || - !(Idx = E->getIntegerConstantExpr(S.Context))) { + !E->isIntegerConstantExpr(Idx, S.Context)) { S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) << AL << 2 << AANT_ArgumentIntegerConstant << E->getSourceRange(); return; } - nullPos = Idx->getZExtValue(); + nullPos = Idx.getZExtValue(); - if ((Idx->isSigned() && Idx->isNegative()) || nullPos > 1) { + if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { // FIXME: This error message could be improved, it would be nice // to say what the bounds actually are. S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) @@ -4828,19 +4833,19 @@ static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E, if (E->isValueDependent()) return E; - Optional I = llvm::APSInt(64); - if (!(I = E->getIntegerConstantExpr(S.Context))) { + llvm::APSInt I(64); + if (!E->isIntegerConstantExpr(I, S.Context)) { S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type) << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange(); return nullptr; } // Make sure we can fit it in 32 bits. - if (!I->isIntN(32)) { - S.Diag(E->getExprLoc(), diag::err_ice_too_large) - << I->toString(10, false) << 32 << /* Unsigned */ 1; + if (!I.isIntN(32)) { + S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false) + << 32 << /* Unsigned */ 1; return nullptr; } - if (*I < 0) + if (I < 0) S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative) << &AL << Idx << E->getSourceRange(); @@ -5681,18 +5686,18 @@ static void handleMSP430InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { } Expr *NumParamsExpr = static_cast(AL.getArgAsExpr(0)); - Optional NumParams = llvm::APSInt(32); - if (!(NumParams = NumParamsExpr->getIntegerConstantExpr(S.Context))) { + llvm::APSInt NumParams(32); + if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { S.Diag(AL.getLoc(), diag::err_attribute_argument_type) << AL << AANT_ArgumentIntegerConstant << NumParamsExpr->getSourceRange(); return; } // The argument should be in range 0..63. - unsigned Num = NumParams->getLimitedValue(255); + unsigned Num = NumParams.getLimitedValue(255); if (Num > 63) { S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) - << AL << (int)NumParams->getSExtValue() + << AL << (int)NumParams.getSExtValue() << NumParamsExpr->getSourceRange(); return; } diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index e3aa817c62249..d885920b6c14e 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -2073,29 +2073,29 @@ Sema::BuildCXXNew(SourceRange Range, bool UseGlobal, // per CWG1464. Otherwise, if it's not a constant, we must have an // unparenthesized array type. if (!(*ArraySize)->isValueDependent()) { + llvm::APSInt Value; // We've already performed any required implicit conversion to integer or // unscoped enumeration type. // FIXME: Per CWG1464, we are required to check the value prior to // converting to size_t. This will never find a negative array size in // C++14 onwards, because Value is always unsigned here! - if (Optional Value = - (*ArraySize)->getIntegerConstantExpr(Context)) { - if (Value->isSigned() && Value->isNegative()) { + if ((*ArraySize)->isIntegerConstantExpr(Value, Context)) { + if (Value.isSigned() && Value.isNegative()) { return ExprError(Diag((*ArraySize)->getBeginLoc(), diag::err_typecheck_negative_array_size) << (*ArraySize)->getSourceRange()); } if (!AllocType->isDependentType()) { - unsigned ActiveSizeBits = ConstantArrayType::getNumAddressingBits( - Context, AllocType, *Value); + unsigned ActiveSizeBits = + ConstantArrayType::getNumAddressingBits(Context, AllocType, Value); if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) return ExprError( Diag((*ArraySize)->getBeginLoc(), diag::err_array_too_large) - << Value->toString(10) << (*ArraySize)->getSourceRange()); + << Value.toString(10) << (*ArraySize)->getSourceRange()); } - KnownArraySize = Value->getZExtValue(); + KnownArraySize = Value.getZExtValue(); } else if (TypeIdParens.isValid()) { // Can't have dynamic array size when the type-id is in parentheses. Diag((*ArraySize)->getBeginLoc(), diag::ext_new_paren_array_nonconst) diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp index d1ddf10724172..b27abb54c170f 100644 --- a/clang/lib/Sema/SemaOpenMP.cpp +++ b/clang/lib/Sema/SemaOpenMP.cpp @@ -5989,7 +5989,8 @@ Sema::checkOpenMPDeclareVariantFunction(Sema::DeclGroupPtrTy DG, // Deal with non-constant score and user condition expressions. auto HandleNonConstantScoresAndConditions = [this](Expr *&E, bool IsScore) -> bool { - if (!E || E->isIntegerConstantExpr(Context)) + llvm::APSInt Result; + if (!E || E->isIntegerConstantExpr(Result, Context)) return false; if (IsScore) { @@ -6475,14 +6476,14 @@ bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { // loop. If test-expr is of form b relational-op var and relational-op is // > or >= then incr-expr must cause var to increase on each iteration of // the loop. - Optional Result = - NewStep->getIntegerConstantExpr(SemaRef.Context); + llvm::APSInt Result; + bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); bool IsConstNeg = - Result && Result->isSigned() && (Subtract != Result->isNegative()); + IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); bool IsConstPos = - Result && Result->isSigned() && (Subtract == Result->isNegative()); - bool IsConstZero = Result && !Result->getBoolValue(); + IsConstant && Result.isSigned() && (Subtract == Result.isNegative()); + bool IsConstZero = IsConstant && !Result.getBoolValue(); // != with increment is treated as <; != with decrement is treated as > if (!TestIsLessOp.hasValue()) @@ -7913,9 +7914,9 @@ static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { if (E == nullptr) return false; - if (Optional Result = - E->getIntegerConstantExpr(SemaRef.Context)) - return Signed ? Result->isSignedIntN(Bits) : Result->isIntN(Bits); + llvm::APSInt Result; + if (E->isIntegerConstantExpr(Result, SemaRef.Context)) + return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits); return false; } @@ -8188,7 +8189,9 @@ checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, // Calculate the last iteration number beforehand instead of doing this on // each iteration. Do not do this if the number of iterations may be kfold-ed. - bool IsConstant = LastIteration.get()->isIntegerConstantExpr(SemaRef.Context); + llvm::APSInt Result; + bool IsConstant = + LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context); ExprResult CalcLastIteration; if (!IsConstant) { ExprResult SaveRef = @@ -12579,16 +12582,15 @@ isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind, ValExpr = Value.get(); // The expression must evaluate to a non-negative integer value. - if (Optional Result = - ValExpr->getIntegerConstantExpr(SemaRef.Context)) { - if (Result->isSigned() && - !((!StrictlyPositive && Result->isNonNegative()) || - (StrictlyPositive && Result->isStrictlyPositive()))) { - SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) - << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) - << ValExpr->getSourceRange(); - return false; - } + llvm::APSInt Result; + if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) && + Result.isSigned() && + !((!StrictlyPositive && Result.isNonNegative()) || + (StrictlyPositive && Result.isStrictlyPositive()))) { + SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) + << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) + << ValExpr->getSourceRange(); + return false; } if (!BuildCapture) return true; @@ -13213,9 +13215,9 @@ OMPClause *Sema::ActOnOpenMPScheduleClause( // OpenMP [2.7.1, Restrictions] // chunk_size must be a loop invariant integer expression with a positive // value. - if (Optional Result = - ValExpr->getIntegerConstantExpr(Context)) { - if (Result->isSigned() && !Result->isStrictlyPositive()) { + llvm::APSInt Result; + if (ValExpr->isIntegerConstantExpr(Result, Context)) { + if (Result.isSigned() && !Result.isStrictlyPositive()) { Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) << "schedule" << 1 << ChunkSize->getSourceRange(); return nullptr; @@ -15686,12 +15688,12 @@ OMPClause *Sema::ActOnOpenMPLinearClause( // Warn about zero linear step (it would be probably better specified as // making corresponding variables 'const'). - if (Optional Result = - StepExpr->getIntegerConstantExpr(Context)) { - if (!Result->isNegative() && !Result->isStrictlyPositive()) - Diag(StepLoc, diag::warn_omp_linear_step_zero) - << Vars[0] << (Vars.size() > 1); - } else if (CalcStep.isUsable()) { + llvm::APSInt Result; + bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context); + if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive()) + Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] + << (Vars.size() > 1); + if (!IsConstant && CalcStep.isUsable()) { // Calculate the step beforehand instead of doing this on each iteration. // (This is not used if the number of iterations may be kfold-ed). CalcStepExpr = CalcStep.get(); @@ -18223,9 +18225,9 @@ OMPClause *Sema::ActOnOpenMPDistScheduleClause( // OpenMP [2.7.1, Restrictions] // chunk_size must be a loop invariant integer expression with a positive // value. - if (Optional Result = - ValExpr->getIntegerConstantExpr(Context)) { - if (Result->isSigned() && !Result->isStrictlyPositive()) { + llvm::APSInt Result; + if (ValExpr->isIntegerConstantExpr(Result, Context)) { + if (Result.isSigned() && !Result.isStrictlyPositive()) { Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) << "dist_schedule" << ChunkSize->getSourceRange(); return nullptr; diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index 7c6acf011d574..8635397f48067 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -346,6 +346,7 @@ NarrowingKind StandardConversionSequence::getNarrowingKind( ToType->isRealFloatingType()) { if (IgnoreFloatToIntegralConversion) return NK_Not_Narrowing; + llvm::APSInt IntConstantValue; const Expr *Initializer = IgnoreNarrowingConversion(Ctx, Converted); assert(Initializer && "Unknown conversion expression"); @@ -353,20 +354,19 @@ NarrowingKind StandardConversionSequence::getNarrowingKind( if (Initializer->isValueDependent()) return NK_Dependent_Narrowing; - if (Optional IntConstantValue = - Initializer->getIntegerConstantExpr(Ctx)) { + if (Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { // Convert the integer to the floating type. llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); - Result.convertFromAPInt(*IntConstantValue, IntConstantValue->isSigned(), + Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), llvm::APFloat::rmNearestTiesToEven); // And back. - llvm::APSInt ConvertedValue = *IntConstantValue; + llvm::APSInt ConvertedValue = IntConstantValue; bool ignored; Result.convertToInteger(ConvertedValue, llvm::APFloat::rmTowardZero, &ignored); // If the resulting value is different, this was a narrowing conversion. - if (*IntConstantValue != ConvertedValue) { - ConstantValue = APValue(*IntConstantValue); + if (IntConstantValue != ConvertedValue) { + ConstantValue = APValue(IntConstantValue); ConstantType = Initializer->getType(); return NK_Constant_Narrowing; } @@ -430,18 +430,17 @@ NarrowingKind StandardConversionSequence::getNarrowingKind( (FromWidth == ToWidth && FromSigned != ToSigned) || (FromSigned && !ToSigned)) { // Not all values of FromType can be represented in ToType. + llvm::APSInt InitializerValue; const Expr *Initializer = IgnoreNarrowingConversion(Ctx, Converted); // If it's value-dependent, we can't tell whether it's narrowing. if (Initializer->isValueDependent()) return NK_Dependent_Narrowing; - Optional OptInitializerValue; - if (!(OptInitializerValue = Initializer->getIntegerConstantExpr(Ctx))) { + if (!Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { // Such conversions on variables are always narrowing. return NK_Variable_Narrowing; } - llvm::APSInt &InitializerValue = *OptInitializerValue; bool Narrowing = false; if (FromWidth < ToWidth) { // Negative -> unsigned is narrowing. Otherwise, more bits is never @@ -2184,22 +2183,21 @@ bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { // compatibility. if (From) { if (FieldDecl *MemberDecl = From->getSourceBitField()) { - Optional BitWidth; + llvm::APSInt BitWidth; if (FromType->isIntegralType(Context) && - (BitWidth = - MemberDecl->getBitWidth()->getIntegerConstantExpr(Context))) { - llvm::APSInt ToSize(BitWidth->getBitWidth(), BitWidth->isUnsigned()); + MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { + llvm::APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); ToSize = Context.getTypeSize(ToType); // Are we promoting to an int from a bitfield that fits in an int? - if (*BitWidth < ToSize || - (FromType->isSignedIntegerType() && *BitWidth <= ToSize)) { + if (BitWidth < ToSize || + (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { return To->getKind() == BuiltinType::Int; } // Are we promoting to an unsigned int from an unsigned bitfield // that fits into an unsigned int? - if (FromType->isUnsignedIntegerType() && *BitWidth <= ToSize) { + if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { return To->getKind() == BuiltinType::UInt; } diff --git a/clang/lib/Sema/SemaStmtAttr.cpp b/clang/lib/Sema/SemaStmtAttr.cpp index c7b97ec4d975a..e9d3c755eb232 100644 --- a/clang/lib/Sema/SemaStmtAttr.cpp +++ b/clang/lib/Sema/SemaStmtAttr.cpp @@ -335,15 +335,15 @@ static Attr *handleOpenCLUnrollHint(Sema &S, Stmt *St, const ParsedAttr &A, if (NumArgs == 1) { Expr *E = A.getArgAsExpr(0); - Optional ArgVal; + llvm::APSInt ArgVal(32); - if (!(ArgVal = E->getIntegerConstantExpr(S.Context))) { + if (!E->isIntegerConstantExpr(ArgVal, S.Context)) { S.Diag(A.getLoc(), diag::err_attribute_argument_type) << A << AANT_ArgumentIntegerConstant << E->getSourceRange(); return nullptr; } - int Val = ArgVal->getSExtValue(); + int Val = ArgVal.getSExtValue(); if (Val <= 0) { S.Diag(A.getRange().getBegin(), diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index 13426cbf2db40..b8f7f1a581590 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -2476,8 +2476,8 @@ QualType Sema::BuildVectorType(QualType CurType, Expr *SizeExpr, return Context.getDependentVectorType(CurType, SizeExpr, AttrLoc, VectorType::GenericVector); - Optional VecSize = SizeExpr->getIntegerConstantExpr(Context); - if (!VecSize) { + llvm::APSInt VecSize(32); + if (!SizeExpr->isIntegerConstantExpr(VecSize, Context)) { Diag(AttrLoc, diag::err_attribute_argument_type) << "vector_size" << AANT_ArgumentIntegerConstant << SizeExpr->getSourceRange(); @@ -2489,13 +2489,13 @@ QualType Sema::BuildVectorType(QualType CurType, Expr *SizeExpr, VectorType::GenericVector); // vecSize is specified in bytes - convert to bits. - if (!VecSize->isIntN(61)) { + if (!VecSize.isIntN(61)) { // Bit size will overflow uint64. Diag(AttrLoc, diag::err_attribute_size_too_large) << SizeExpr->getSourceRange() << "vector"; return QualType(); } - uint64_t VectorSizeBits = VecSize->getZExtValue() * 8; + uint64_t VectorSizeBits = VecSize.getZExtValue() * 8; unsigned TypeSize = static_cast(Context.getTypeSize(CurType)); if (VectorSizeBits == 0) { @@ -2540,8 +2540,8 @@ QualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize, } if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) { - Optional vecSize = ArraySize->getIntegerConstantExpr(Context); - if (!vecSize) { + llvm::APSInt vecSize(32); + if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) { Diag(AttrLoc, diag::err_attribute_argument_type) << "ext_vector_type" << AANT_ArgumentIntegerConstant << ArraySize->getSourceRange(); @@ -2555,7 +2555,7 @@ QualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize, } // Unlike gcc's vector_size attribute, the size is specified as the // number of elements, not the number of bytes. - unsigned vectorSize = static_cast(vecSize->getZExtValue()); + unsigned vectorSize = static_cast(vecSize.getZExtValue()); if (vectorSize == 0) { Diag(AttrLoc, diag::err_attribute_zero_size) @@ -6254,15 +6254,13 @@ static bool BuildAddressSpaceIndex(Sema &S, LangAS &ASIdx, const Expr *AddrSpace, SourceLocation AttrLoc) { if (!AddrSpace->isValueDependent()) { - Optional OptAddrSpace = - AddrSpace->getIntegerConstantExpr(S.Context); - if (!OptAddrSpace) { + llvm::APSInt addrSpace(32); + if (!AddrSpace->isIntegerConstantExpr(addrSpace, S.Context)) { S.Diag(AttrLoc, diag::err_attribute_argument_type) << "'address_space'" << AANT_ArgumentIntegerConstant << AddrSpace->getSourceRange(); return false; } - llvm::APSInt &addrSpace = *OptAddrSpace; // Bounds checking. if (addrSpace.isSigned()) { @@ -7714,9 +7712,9 @@ static void HandleNeonVectorTypeAttr(QualType &CurType, const ParsedAttr &Attr, } // The number of elements must be an ICE. Expr *numEltsExpr = static_cast(Attr.getArgAsExpr(0)); - Optional numEltsInt; + llvm::APSInt numEltsInt(32); if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() || - !(numEltsInt = numEltsExpr->getIntegerConstantExpr(S.Context))) { + !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) { S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr << AANT_ArgumentIntegerConstant << numEltsExpr->getSourceRange(); @@ -7732,7 +7730,7 @@ static void HandleNeonVectorTypeAttr(QualType &CurType, const ParsedAttr &Attr, // The total size of the vector must be 64 or 128 bits. unsigned typeSize = static_cast(S.Context.getTypeSize(CurType)); - unsigned numElts = static_cast(numEltsInt->getZExtValue()); + unsigned numElts = static_cast(numEltsInt.getZExtValue()); unsigned vecSize = typeSize * numElts; if (vecSize != 64 && vecSize != 128) { S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType;