diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h --- a/clang/include/clang/AST/Expr.h +++ b/clang/include/clang/AST/Expr.h @@ -500,14 +500,13 @@ /// semantically correspond to a bool. bool isKnownToHaveBooleanValue(bool Semantic = true) const; - /// 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. + /// 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. /// /// Note: This does not perform the implicit conversions required by C++11 /// [expr.const]p5. - bool isIntegerConstantExpr(llvm::APSInt &Result, const ASTContext &Ctx, + Optional getIntegerConstantExpr(const ASTContext &Ctx, SourceLocation *Loc = nullptr, bool isEvaluated = true) const; bool isIntegerConstantExpr(const ASTContext &Ctx, diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -9212,17 +9212,15 @@ const ConstantArrayType* CAT) -> std::pair { if (VAT) { - llvm::APSInt TheInt; + Optional TheInt; Expr *E = VAT->getSizeExpr(); - 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 (E && (TheInt = E->getIntegerConstantExpr(*this))) + return std::make_pair(true, *TheInt); + return std::make_pair(false, llvm::APSInt()); } + 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 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -14580,16 +14580,22 @@ return true; } -bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, const ASTContext &Ctx, - SourceLocation *Loc, bool isEvaluated) const { +Optional Expr::getIntegerConstantExpr(const ASTContext &Ctx, + SourceLocation *Loc, + bool isEvaluated) const { assert(!isValueDependent() && "Expression evaluator can't be called on a dependent expression."); - if (Ctx.getLangOpts().CPlusPlus11) - return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); + APSInt Value; + + if (Ctx.getLangOpts().CPlusPlus11) { + if (EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc)) + return Value; + return None; + } if (!isIntegerConstantExpr(Ctx, Loc)) - return false; + return None; // 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 @@ -14603,8 +14609,7 @@ if (!::EvaluateAsInt(this, ExprResult, Ctx, SE_AllowSideEffects, Info)) llvm_unreachable("ICE cannot be evaluated!"); - Value = ExprResult.Val.getInt(); - return true; + return ExprResult.Val.getInt(); } bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const { diff --git a/clang/lib/AST/MicrosoftMangle.cpp b/clang/lib/AST/MicrosoftMangle.cpp --- a/clang/lib/AST/MicrosoftMangle.cpp +++ b/clang/lib/AST/MicrosoftMangle.cpp @@ -1358,9 +1358,9 @@ void MicrosoftCXXNameMangler::mangleExpression(const Expr *E) { // See if this is a constant expression. - llvm::APSInt Value; - if (E->isIntegerConstantExpr(Value, Context.getASTContext())) { - mangleIntegerLiteral(Value, E->getType()->isBooleanType()); + if (Optional Value = + E->getIntegerConstantExpr(Context.getASTContext())) { + mangleIntegerLiteral(*Value, E->getType()->isBooleanType()); return; } diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -4306,11 +4306,9 @@ } else { // If this is required to be a constant, constant fold it so that we // know that the generated intrinsic gets a ConstantInt. - 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); + ArgValue = llvm::ConstantInt::get( + getLLVMContext(), + *E->getArg(i)->getIntegerConstantExpr(getContext())); } // If the intrinsic arg type is different from the builtin arg type @@ -5447,13 +5445,13 @@ 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); - if (!Arg->isIntegerConstantExpr(NeonTypeConst, getContext())) + Optional NeonTypeConst = Arg->getIntegerConstantExpr(getContext()); + if (!NeonTypeConst) 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(); @@ -6672,10 +6670,9 @@ } else { // If this is required to be a constant, constant fold it so that we know // that the generated intrinsic gets a ConstantInt. - 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)); + Ops.push_back(llvm::ConstantInt::get( + getLLVMContext(), + *E->getArg(i)->getIntegerConstantExpr(getContext()))); } } @@ -6875,9 +6872,9 @@ // Get the last argument, which specifies the vector type. assert(HasExtraArg); - llvm::APSInt Result; const Expr *Arg = E->getArg(E->getNumArgs()-1); - if (!Arg->isIntegerConstantExpr(Result, getContext())) + Optional Result = Arg->getIntegerConstantExpr(getContext()); + if (!Result) return nullptr; if (BuiltinID == ARM::BI__builtin_arm_vcvtr_f || @@ -6890,7 +6887,7 @@ 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. @@ -6899,7 +6896,7 @@ } // Determine the type of this overloaded NEON intrinsic. - NeonTypeFlags Type(Result.getZExtValue()); + NeonTypeFlags Type = Result->getZExtValue(); bool usgn = Type.isUnsigned(); bool rightShift = false; @@ -7042,11 +7039,7 @@ template static Integer GetIntegerConstantValue(const Expr *E, ASTContext &Context) { - llvm::APSInt IntVal; - bool IsConst = E->isIntegerConstantExpr(IntVal, Context); - assert(IsConst && "Sema should have checked this was a constant"); - (void)IsConst; - return IntVal.getExtValue(); + return E->getIntegerConstantExpr(Context)->getExtValue(); } static llvm::Value *SignOrZeroExtend(CGBuilderTy &Builder, llvm::Value *V, @@ -7316,13 +7309,13 @@ assert(E->getNumArgs() >= 3); // Get the last argument, which specifies the vector type. - llvm::APSInt Result; const Expr *Arg = E->getArg(E->getNumArgs() - 1); - if (!Arg->isIntegerConstantExpr(Result, CGF.getContext())) + Optional Result = Arg->getIntegerConstantExpr(CGF.getContext()); + if (!Result) 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; @@ -7940,11 +7933,9 @@ } else { // If this is required to be a constant, constant fold it so that we know // that the generated intrinsic gets a ConstantInt. - 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)); + Ops.push_back(llvm::ConstantInt::get( + getLLVMContext(), + *E->getArg(i)->getIntegerConstantExpr(getContext()))); } } @@ -7959,12 +7950,11 @@ return Result; } - llvm::APSInt Result; const Expr *Arg = E->getArg(E->getNumArgs()-1); NeonTypeFlags Type(0); - if (Arg->isIntegerConstantExpr(Result, getContext())) + if (Optional Result = Arg->getIntegerConstantExpr(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(); @@ -10688,10 +10678,8 @@ // If this is required to be a constant, constant fold it so that we know // that the generated intrinsic gets a ConstantInt. - 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)); + Ops.push_back(llvm::ConstantInt::get( + getLLVMContext(), *E->getArg(i)->getIntegerConstantExpr(getContext()))); } // These exist so that the builtin that takes an immediate can be bounds @@ -13798,11 +13786,8 @@ llvm::Type *ResultType = ConvertType(E->getType()); Value *X = EmitScalarExpr(E->getArg(0)); // Constant-fold the M4 and M5 mask arguments. - 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; + llvm::APSInt M4 = *E->getArg(1)->getIntegerConstantExpr(getContext()); + llvm::APSInt M5 = *E->getArg(2)->getIntegerConstantExpr(getContext()); // 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; @@ -13857,10 +13842,7 @@ Value *X = EmitScalarExpr(E->getArg(0)); Value *Y = EmitScalarExpr(E->getArg(1)); // Constant-fold the M4 mask argument. - llvm::APSInt M4; - bool IsConstM4 = E->getArg(2)->isIntegerConstantExpr(M4, getContext()); - assert(IsConstM4 && "Constant arg isn't actually constant?"); - (void)IsConstM4; + llvm::APSInt M4 = *E->getArg(2)->getIntegerConstantExpr(getContext()); // 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; @@ -13894,10 +13876,7 @@ Value *X = EmitScalarExpr(E->getArg(0)); Value *Y = EmitScalarExpr(E->getArg(1)); // Constant-fold the M4 mask argument. - llvm::APSInt M4; - bool IsConstM4 = E->getArg(2)->isIntegerConstantExpr(M4, getContext()); - assert(IsConstM4 && "Constant arg isn't actually constant?"); - (void)IsConstM4; + llvm::APSInt M4 = *E->getArg(2)->getIntegerConstantExpr(getContext()); // 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; @@ -14564,10 +14543,11 @@ Address Dst = EmitPointerWithAlignment(E->getArg(0)); Value *Src = EmitScalarExpr(E->getArg(1)); Value *Ldm = EmitScalarExpr(E->getArg(2)); - llvm::APSInt isColMajorArg; - if (!E->getArg(3)->isIntegerConstantExpr(isColMajorArg, getContext())) + Optional isColMajorArg = + E->getArg(3)->getIntegerConstantExpr(getContext()); + if (!isColMajorArg) 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) @@ -14608,10 +14588,11 @@ Value *Dst = EmitScalarExpr(E->getArg(0)); Address Src = EmitPointerWithAlignment(E->getArg(1)); Value *Ldm = EmitScalarExpr(E->getArg(2)); - llvm::APSInt isColMajorArg; - if (!E->getArg(3)->isIntegerConstantExpr(isColMajorArg, getContext())) + Optional isColMajorArg = + E->getArg(3)->getIntegerConstantExpr(getContext()); + if (!isColMajorArg) 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) @@ -14658,16 +14639,20 @@ Address SrcA = EmitPointerWithAlignment(E->getArg(1)); Address SrcB = EmitPointerWithAlignment(E->getArg(2)); Address SrcC = EmitPointerWithAlignment(E->getArg(3)); - llvm::APSInt LayoutArg; - if (!E->getArg(4)->isIntegerConstantExpr(LayoutArg, getContext())) + Optional LayoutArg = + E->getArg(4)->getIntegerConstantExpr(getContext()); + if (!LayoutArg) 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 (!E->getArg(5)->isIntegerConstantExpr(SatfArg, getContext())) + else if (Optional OptSatfArg = + E->getArg(5)->getIntegerConstantExpr(getContext())) + SatfArg = *OptSatfArg; + else return nullptr; bool Satf = SatfArg.getSExtValue(); NVPTXMmaInfo MI = getNVPTXMmaInfo(BuiltinID); @@ -14828,12 +14813,8 @@ return Builder.CreateCall(Callee, Args); } case WebAssembly::BI__builtin_wasm_memory_init: { - llvm::APSInt SegConst; - if (!E->getArg(0)->isIntegerConstantExpr(SegConst, getContext())) - llvm_unreachable("Constant arg isn't actually constant?"); - llvm::APSInt MemConst; - if (!E->getArg(1)->isIntegerConstantExpr(MemConst, getContext())) - llvm_unreachable("Constant arg isn't actually constant?"); + llvm::APSInt SegConst = *E->getArg(0)->getIntegerConstantExpr(getContext()); + llvm::APSInt MemConst = *E->getArg(1)->getIntegerConstantExpr(getContext()); if (!MemConst.isNullValue()) ErrorUnsupported(E, "non-zero memory index"); Value *Args[] = {llvm::ConstantInt::get(getLLVMContext(), SegConst), @@ -14844,9 +14825,7 @@ return Builder.CreateCall(Callee, Args); } case WebAssembly::BI__builtin_wasm_data_drop: { - llvm::APSInt SegConst; - if (!E->getArg(0)->isIntegerConstantExpr(SegConst, getContext())) - llvm_unreachable("Constant arg isn't actually constant?"); + llvm::APSInt SegConst = *E->getArg(0)->getIntegerConstantExpr(getContext()); Value *Arg = llvm::ConstantInt::get(getLLVMContext(), SegConst); Function *Callee = CGM.getIntrinsic(Intrinsic::wasm_data_drop); return Builder.CreateCall(Callee, {Arg}); @@ -14973,9 +14952,7 @@ 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; - if (!E->getArg(1)->isIntegerConstantExpr(LaneConst, getContext())) - llvm_unreachable("Constant arg isn't actually constant?"); + llvm::APSInt LaneConst = *E->getArg(1)->getIntegerConstantExpr(getContext()); Value *Vec = EmitScalarExpr(E->getArg(0)); Value *Lane = llvm::ConstantInt::get(getLLVMContext(), LaneConst); Value *Extract = Builder.CreateExtractElement(Vec, Lane); @@ -15001,9 +14978,7 @@ 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; - if (!E->getArg(1)->isIntegerConstantExpr(LaneConst, getContext())) - llvm_unreachable("Constant arg isn't actually constant?"); + llvm::APSInt LaneConst = *E->getArg(1)->getIntegerConstantExpr(getContext()); 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 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -3760,15 +3760,17 @@ llvm::APSInt ConstLength; if (Length) { // Idx = LowerBound + Length - 1; - if (Length->isIntegerConstantExpr(ConstLength, C)) { - ConstLength = ConstLength.zextOrTrunc(PointerWidthInBits); + if (Optional CL = Length->getIntegerConstantExpr(C)) { + ConstLength = CL->zextOrTrunc(PointerWidthInBits); Length = nullptr; } auto *LowerBound = E->getLowerBound(); llvm::APSInt ConstLowerBound(PointerWidthInBits, /*isUnsigned=*/false); - if (LowerBound && LowerBound->isIntegerConstantExpr(ConstLowerBound, C)) { - ConstLowerBound = ConstLowerBound.zextOrTrunc(PointerWidthInBits); - LowerBound = nullptr; + if (LowerBound) { + if (Optional LB = LowerBound->getIntegerConstantExpr(C)) { + ConstLowerBound = LB->zextOrTrunc(PointerWidthInBits); + LowerBound = nullptr; + } } if (!Length) --ConstLength; @@ -3805,8 +3807,10 @@ : BaseTy; if (auto *VAT = C.getAsVariableArrayType(ArrayTy)) { Length = VAT->getSizeExpr(); - if (Length->isIntegerConstantExpr(ConstLength, C)) + if (Optional L = Length->getIntegerConstantExpr(C)) { + ConstLength = *L; 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 --- a/clang/lib/Sema/SemaAttr.cpp +++ b/clang/lib/Sema/SemaAttr.cpp @@ -293,20 +293,20 @@ // If specified then alignment must be a "small" power of two. unsigned AlignmentVal = 0; if (Alignment) { - llvm::APSInt Val; + Optional 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() || - !Alignment->isIntegerConstantExpr(Val, Context) || - !(Val == 0 || Val.isPowerOf2()) || - Val.getZExtValue() > 16) { + !(Val = Alignment->getIntegerConstantExpr(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 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -2097,10 +2097,7 @@ if (CoprocArg->isTypeDependent() || CoprocArg->isValueDependent()) return false; - llvm::APSInt CoprocNoAP; - bool IsICE = CoprocArg->isIntegerConstantExpr(CoprocNoAP, Context); - (void)IsICE; - assert(IsICE && "Coprocossor immediate is not a constant expression"); + llvm::APSInt CoprocNoAP = *CoprocArg->getIntegerConstantExpr(Context); int64_t CoprocNo = CoprocNoAP.getExtValue(); assert(CoprocNo >= 0 && "Coprocessor immediate must be non-negative"); @@ -2389,8 +2386,7 @@ } // The second argument needs to be a constant int - llvm::APSInt Value; - if (!TheCall->getArg(1)->isIntegerConstantExpr(Value, Context)) { + if (!TheCall->getArg(1)->isIntegerConstantExpr(Context)) { Diag(Arg->getBeginLoc(), diag::err_preserve_field_info_not_const) << 2 << Arg->getSourceRange(); return true; @@ -2925,11 +2921,10 @@ CallExpr *TheCall) { if (BuiltinID == SystemZ::BI__builtin_tabort) { Expr *Arg = TheCall->getArg(0); - 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(); + 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(); } // For intrinsics which take an immediate value as part of the instruction, @@ -4586,21 +4581,19 @@ } if (SubExprs.size() >= 2 && Form != Init) { - 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 (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(); } if (auto ScopeModel = AtomicExpr::getScopeModel(Op)) { auto *Scope = Args[Args.size() - 1]; - 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(); + if (Optional Result = Scope->getIntegerConstantExpr(Context)) { + if (!ScopeModel->isValid(Result->getZExtValue())) + Diag(Scope->getBeginLoc(), diag::err_atomic_op_has_invalid_synch_scope) + << Scope->getSourceRange(); } SubExprs.push_back(Scope); } @@ -5468,8 +5461,7 @@ << TheCall->getSourceRange(); // Check the third argument is a compile time constant - llvm::APSInt Value; - if(!TheCall->getArg(2)->isIntegerConstantExpr(Value, Context)) + if(!TheCall->getArg(2)->isIntegerConstantExpr(Context)) return Diag(TheCall->getBeginLoc(), diag::err_vsx_builtin_nonconstant_argument) << 3 /* argument index */ << TheCall->getDirectCallee() @@ -5564,17 +5556,17 @@ TheCall->getArg(i)->isValueDependent()) continue; - llvm::APSInt Result(32); - if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) + Optional Result; + if (!(Result = TheCall->getArg(i)->getIntegerConstantExpr(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()); @@ -5821,10 +5813,11 @@ if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; - if (!Arg->isIntegerConstantExpr(Result, Context)) + Optional R; + if (!(R = Arg->getIntegerConstantExpr(Context))) return Diag(TheCall->getBeginLoc(), diag::err_constant_integer_arg_type) << FDecl->getDeclName() << Arg->getSourceRange(); - + Result = *R; return false; } @@ -9978,14 +9971,15 @@ // If the shift amount is a positive constant, drop the width by // that much. - 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; + 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; + } } return L; @@ -10009,9 +10003,9 @@ IntRange L = GetExprRange(C, BO->getLHS(), opWidth, InConstantContext); // If the divisor is constant, use that. - llvm::APSInt divisor; - if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) { - unsigned log2 = divisor.logBase2(); // floor(log_2(divisor)) + if (Optional divisor = + BO->getRHS()->getIntegerConstantExpr(C)) { + unsigned log2 = divisor->logBase2(); // floor(log_2(divisor)) if (log2 >= L.Width) L.Width = (L.NonNegative ? 0 : 1); else @@ -10443,23 +10437,20 @@ Expr *RHS = E->getRHS(); if (T->isIntegralType(S.Context)) { - llvm::APSInt RHSValue; - llvm::APSInt LHSValue; - - bool IsRHSIntegralLiteral = RHS->isIntegerConstantExpr(RHSValue, S.Context); - bool IsLHSIntegralLiteral = LHS->isIntegerConstantExpr(LHSValue, S.Context); + Optional RHSValue = RHS->getIntegerConstantExpr(S.Context); + Optional LHSValue = LHS->getIntegerConstantExpr(S.Context); // We don't care about expressions whose result is a constant. - if (IsRHSIntegralLiteral && IsLHSIntegralLiteral) + if (RHSValue && LHSValue) return AnalyzeImpConvsInComparison(S, E); // We only care about expressions where just one side is literal - if (IsRHSIntegralLiteral ^ IsLHSIntegralLiteral) { + if ((bool)RHSValue ^ (bool)LHSValue) { // Is the constant on the RHS or LHS? - const bool RhsConstant = IsRHSIntegralLiteral; + const bool RhsConstant = (bool)RHSValue; 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'. @@ -11417,8 +11408,8 @@ if (SourcePrecision > 0 && TargetPrecision > 0 && SourcePrecision > TargetPrecision) { - llvm::APSInt SourceInt; - if (E->isIntegerConstantExpr(SourceInt, S.Context)) { + if (Optional SourceInt = + E->getIntegerConstantExpr(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. @@ -11426,11 +11417,11 @@ 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); @@ -13385,9 +13376,10 @@ return; if (Expr *RHS = BinOp->getRHS()) { RHS = RHS->IgnoreParenCasts(); - llvm::APSInt Value; + Optional Value; VarWillBeReased = - (RHS && RHS->isIntegerConstantExpr(Value, Context) && Value == 0); + (RHS && (Value = RHS->getIntegerConstantExpr(Context)) && + *Value == 0); } } } diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -13062,20 +13062,20 @@ if (!MagicValueExpr) { continue; } - llvm::APSInt MagicValueInt; - if (!MagicValueExpr->isIntegerConstantExpr(MagicValueInt, Context)) { + Optional MagicValueInt; + if (!(MagicValueInt = MagicValueExpr->getIntegerConstantExpr( 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 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -240,9 +240,9 @@ static bool checkUInt32Argument(Sema &S, const AttrInfo &AI, const Expr *Expr, uint32_t &Val, unsigned Idx = UINT_MAX, bool StrictlyUnsigned = false) { - llvm::APSInt I(32); + Optional I = llvm::APSInt(32); if (Expr->isTypeDependent() || Expr->isValueDependent() || - !Expr->isIntegerConstantExpr(I, S.Context)) { + !(I = Expr->getIntegerConstantExpr(S.Context))) { if (Idx != UINT_MAX) S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type) << &AI << Idx << AANT_ArgumentIntegerConstant @@ -253,19 +253,19 @@ 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 @@ unsigned NumParams = (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam; - llvm::APSInt IdxInt; + Optional IdxInt; if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || - !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { + !(IdxInt = IdxExpr->getIntegerConstantExpr(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 @@ } if (!E->isValueDependent()) { - llvm::APSInt I(64); - if (!E->isIntegerConstantExpr(I, Context)) { + Optional I = llvm::APSInt(64); + if (!(I = E->getIntegerConstantExpr(Context))) { if (OE) Diag(AttrLoc, diag::err_attribute_argument_n_type) << &TmpAttr << 1 << AANT_ArgumentIntegerConstant @@ -1618,27 +1618,22 @@ 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) { - 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; - } - } + if (OE && !OE->isValueDependent() && !OE->isIntegerConstantExpr(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)); @@ -2714,36 +2709,36 @@ unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; if (AL.getNumArgs() > 0) { Expr *E = AL.getArgAsExpr(0); - llvm::APSInt Idx(32); + Optional Idx = llvm::APSInt(32); if (E->isTypeDependent() || E->isValueDependent() || - !E->isIntegerConstantExpr(Idx, S.Context)) { + !(Idx = E->getIntegerConstantExpr(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); - llvm::APSInt Idx(32); + Optional Idx = llvm::APSInt(32); if (E->isTypeDependent() || E->isValueDependent() || - !E->isIntegerConstantExpr(Idx, S.Context)) { + !(Idx = E->getIntegerConstantExpr(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) @@ -4797,19 +4792,19 @@ if (E->isValueDependent()) return E; - llvm::APSInt I(64); - if (!E->isIntegerConstantExpr(I, S.Context)) { + Optional I = llvm::APSInt(64); + if (!(I = E->getIntegerConstantExpr(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) + 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(); @@ -5639,18 +5634,18 @@ } Expr *NumParamsExpr = static_cast(AL.getArgAsExpr(0)); - llvm::APSInt NumParams(32); - if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { + Optional NumParams = llvm::APSInt(32); + if (!(NumParams = NumParamsExpr->getIntegerConstantExpr(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 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -2083,14 +2083,13 @@ // 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 ((*ArraySize)->isIntegerConstantExpr(Value, Context)) { - if (Value.isSigned() && Value.isNegative()) { + if (Optional Value = (*ArraySize)->getIntegerConstantExpr(Context)) { + if (Value->isSigned() && Value->isNegative()) { return ExprError(Diag((*ArraySize)->getBeginLoc(), diag::err_typecheck_negative_array_size) << (*ArraySize)->getSourceRange()); @@ -2098,14 +2097,14 @@ if (!AllocType->isDependentType()) { unsigned ActiveSizeBits = - ConstantArrayType::getNumAddressingBits(Context, AllocType, Value); + 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 --- a/clang/lib/Sema/SemaOpenMP.cpp +++ b/clang/lib/Sema/SemaOpenMP.cpp @@ -5504,8 +5504,7 @@ // Deal with non-constant score and user condition expressions. auto HandleNonConstantScoresAndConditions = [this](Expr *&E, bool IsScore) -> bool { - llvm::APSInt Result; - if (!E || E->isIntegerConstantExpr(Result, Context)) + if (!E || E->isIntegerConstantExpr(Context)) return false; if (IsScore) { @@ -6009,14 +6008,14 @@ // 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. - llvm::APSInt Result; - bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); + Optional Result = + NewStep->getIntegerConstantExpr(SemaRef.Context); bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); bool IsConstNeg = - IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); + Result && Result->isSigned() && (Subtract != Result->isNegative()); bool IsConstPos = - IsConstant && Result.isSigned() && (Subtract == Result.isNegative()); - bool IsConstZero = IsConstant && !Result.getBoolValue(); + Result && Result->isSigned() && (Subtract == Result->isNegative()); + bool IsConstZero = Result && !Result->getBoolValue(); // != with increment is treated as <; != with decrement is treated as > if (!TestIsLessOp.hasValue()) @@ -7353,9 +7352,8 @@ static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { if (E == nullptr) return false; - llvm::APSInt Result; - if (E->isIntegerConstantExpr(Result, SemaRef.Context)) - return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits); + if (Optional Result = E->getIntegerConstantExpr(SemaRef.Context)) + return Signed ? Result->isSignedIntN(Bits) : Result->isIntN(Bits); return false; } @@ -7628,9 +7626,7 @@ // 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. - llvm::APSInt Result; - bool IsConstant = - LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context); + bool IsConstant = LastIteration.get()->isIntegerConstantExpr(SemaRef.Context); ExprResult CalcLastIteration; if (!IsConstant) { ExprResult SaveRef = @@ -11953,15 +11949,16 @@ ValExpr = Value.get(); // The expression must evaluate to a non-negative integer value. - 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 (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; + } } if (!BuildCapture) return true; @@ -12577,9 +12574,9 @@ // OpenMP [2.7.1, Restrictions] // chunk_size must be a loop invariant integer expression with a positive // value. - llvm::APSInt Result; - if (ValExpr->isIntegerConstantExpr(Result, Context)) { - if (Result.isSigned() && !Result.isStrictlyPositive()) { + if (Optional Result = + ValExpr->getIntegerConstantExpr(Context)) { + if (Result->isSigned() && !Result->isStrictlyPositive()) { Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) << "schedule" << 1 << ChunkSize->getSourceRange(); return nullptr; @@ -14930,12 +14927,12 @@ // Warn about zero linear step (it would be probably better specified as // making corresponding variables 'const'). - 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()) { + 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()) { // 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(); @@ -17414,9 +17411,9 @@ // OpenMP [2.7.1, Restrictions] // chunk_size must be a loop invariant integer expression with a positive // value. - llvm::APSInt Result; - if (ValExpr->isIntegerConstantExpr(Result, Context)) { - if (Result.isSigned() && !Result.isStrictlyPositive()) { + if (Optional Result = + ValExpr->getIntegerConstantExpr(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 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -346,7 +346,6 @@ ToType->isRealFloatingType()) { if (IgnoreFloatToIntegralConversion) return NK_Not_Narrowing; - llvm::APSInt IntConstantValue; const Expr *Initializer = IgnoreNarrowingConversion(Ctx, Converted); assert(Initializer && "Unknown conversion expression"); @@ -354,19 +353,20 @@ if (Initializer->isValueDependent()) return NK_Dependent_Narrowing; - if (Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { + if (Optional IntConstantValue = + Initializer->getIntegerConstantExpr(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,17 +430,18 @@ (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; - if (!Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { + Optional OptInitializerValue; + if (!(OptInitializerValue = Initializer->getIntegerConstantExpr(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 @@ -2175,21 +2176,21 @@ // compatibility. if (From) { if (FieldDecl *MemberDecl = From->getSourceBitField()) { - llvm::APSInt BitWidth; + Optional BitWidth; if (FromType->isIntegralType(Context) && - MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { - llvm::APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); + (BitWidth = MemberDecl->getBitWidth()->getIntegerConstantExpr(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 --- a/clang/lib/Sema/SemaStmtAttr.cpp +++ b/clang/lib/Sema/SemaStmtAttr.cpp @@ -296,15 +296,15 @@ if (NumArgs == 1) { Expr *E = A.getArgAsExpr(0); - llvm::APSInt ArgVal(32); + Optional ArgVal; - if (!E->isIntegerConstantExpr(ArgVal, S.Context)) { + if (!(ArgVal = E->getIntegerConstantExpr(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 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -2417,8 +2417,8 @@ return Context.getDependentVectorType(CurType, SizeExpr, AttrLoc, VectorType::GenericVector); - llvm::APSInt VecSize(32); - if (!SizeExpr->isIntegerConstantExpr(VecSize, Context)) { + Optional VecSize = SizeExpr->getIntegerConstantExpr(Context); + if (!VecSize) { Diag(AttrLoc, diag::err_attribute_argument_type) << "vector_size" << AANT_ArgumentIntegerConstant << SizeExpr->getSourceRange(); @@ -2429,7 +2429,7 @@ return Context.getDependentVectorType(CurType, SizeExpr, AttrLoc, VectorType::GenericVector); - unsigned VectorSize = static_cast(VecSize.getZExtValue() * 8); + unsigned VectorSize = static_cast(VecSize->getZExtValue() * 8); unsigned TypeSize = static_cast(Context.getTypeSize(CurType)); if (VectorSize == 0) { @@ -2474,8 +2474,8 @@ } if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) { - llvm::APSInt vecSize(32); - if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) { + Optional vecSize = ArraySize->getIntegerConstantExpr(Context); + if (!vecSize) { Diag(AttrLoc, diag::err_attribute_argument_type) << "ext_vector_type" << AANT_ArgumentIntegerConstant << ArraySize->getSourceRange(); @@ -2484,7 +2484,7 @@ // 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) @@ -6073,13 +6073,15 @@ const Expr *AddrSpace, SourceLocation AttrLoc) { if (!AddrSpace->isValueDependent()) { - llvm::APSInt addrSpace(32); - if (!AddrSpace->isIntegerConstantExpr(addrSpace, S.Context)) { + Optional OptAddrSpace = + AddrSpace->getIntegerConstantExpr(S.Context); + if (!OptAddrSpace) { 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()) { @@ -7510,9 +7512,9 @@ } // The number of elements must be an ICE. Expr *numEltsExpr = static_cast(Attr.getArgAsExpr(0)); - llvm::APSInt numEltsInt(32); + Optional numEltsInt; if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() || - !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) { + !(numEltsInt = numEltsExpr->getIntegerConstantExpr(S.Context))) { S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr << AANT_ArgumentIntegerConstant << numEltsExpr->getSourceRange(); @@ -7528,7 +7530,7 @@ // 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;