From 82dbb1b2b4f1e70ca453cca60a4ba5b856058fc0 Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Wed, 22 Jul 2020 09:37:51 +0200 Subject: [PATCH] Fix the clang-tidy build after get/isIntegerConstantExpression refactoring. --- .../NarrowingConversionsCheck.cpp | 5 ++--- .../ProBoundsConstantArrayIndexCheck.cpp | 14 ++++++------ .../misc/RedundantExpressionCheck.cpp | 22 +++++++++++++------ .../clang-tidy/modernize/LoopConvertUtils.cpp | 7 +++--- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp index aa860b30fe75..1837ccb6002f 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp @@ -79,9 +79,8 @@ static QualType getUnqualifiedType(const Expr &E) { } static APValue getConstantExprValue(const ASTContext &Ctx, const Expr &E) { - llvm::APSInt IntegerConstant; - if (E.isIntegerConstantExpr(IntegerConstant, Ctx)) - return APValue(IntegerConstant); + if (auto IntegerConstant = E.getIntegerConstantExpr(Ctx)) + return APValue(*IntegerConstant); APValue Constant; if (Ctx.getLangOpts().CPlusPlus && E.isCXX11ConstantExpr(Ctx, &Constant)) return Constant; diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp index dd0bedd742a4..96b0bb0f9b02 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp @@ -65,9 +65,9 @@ void ProBoundsConstantArrayIndexCheck::check( if (IndexExpr->isValueDependent()) return; // We check in the specialization. - llvm::APSInt Index; - if (!IndexExpr->isIntegerConstantExpr(Index, *Result.Context, nullptr, - /*isEvaluated=*/true)) { + Optional Index = + IndexExpr->getIntegerConstantExpr(*Result.Context); + if (!Index) { SourceRange BaseRange; if (const auto *ArraySubscriptE = dyn_cast(Matched)) BaseRange = ArraySubscriptE->getBase()->getSourceRange(); @@ -101,9 +101,9 @@ void ProBoundsConstantArrayIndexCheck::check( if (!StdArrayDecl) return; - if (Index.isSigned() && Index.isNegative()) { + if (Index->isSigned() && Index->isNegative()) { diag(Matched->getExprLoc(), "std::array<> index %0 is negative") - << Index.toString(10); + << Index->toString(10); return; } @@ -118,11 +118,11 @@ void ProBoundsConstantArrayIndexCheck::check( // Get uint64_t values, because different bitwidths would lead to an assertion // in APInt::uge. - if (Index.getZExtValue() >= ArraySize.getZExtValue()) { + if (Index->getZExtValue() >= ArraySize.getZExtValue()) { diag(Matched->getExprLoc(), "std::array<> index %0 is past the end of the array " "(which contains %1 elements)") - << Index.toString(10) << ArraySize.toString(10, false); + << Index->toString(10) << ArraySize.toString(10, false); } } diff --git a/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp b/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp index aef513a527b5..b84e4d525d8c 100644 --- a/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp @@ -500,7 +500,13 @@ static bool retrieveIntegerConstantExpr(const MatchFinder::MatchResult &Result, const Expr *&ConstExpr) { std::string CstId = (Id + "-const").str(); ConstExpr = Result.Nodes.getNodeAs(CstId); - return ConstExpr && ConstExpr->isIntegerConstantExpr(Value, *Result.Context); + if (!ConstExpr) + return false; + Optional R = ConstExpr->getIntegerConstantExpr(*Result.Context); + if (!R) + return false; + Value = *R; + return true; } // Overloaded `retrieveIntegerConstantExpr` for compatibility. @@ -673,7 +679,7 @@ static bool retrieveRelationalIntegerConstantExpr( if (const auto *Arg = OverloadedOperatorExpr->getArg(1)) { if (!Arg->isValueDependent() && - !Arg->isIntegerConstantExpr(Value, *Result.Context)) + !Arg->isIntegerConstantExpr(*Result.Context)) return false; } Symbol = OverloadedOperatorExpr->getArg(0); @@ -1265,21 +1271,23 @@ void RedundantExpressionCheck::check(const MatchFinder::MatchResult &Result) { "left-right-shift-confusion")) { const auto *ShiftingConst = Result.Nodes.getNodeAs("shift-const"); assert(ShiftingConst && "Expr* 'ShiftingConst' is nullptr!"); - APSInt ShiftingValue; + Optional ShiftingValue = + ShiftingConst->getIntegerConstantExpr(*Result.Context); - if (!ShiftingConst->isIntegerConstantExpr(ShiftingValue, *Result.Context)) + if (!ShiftingValue) return; const auto *AndConst = Result.Nodes.getNodeAs("and-const"); assert(AndConst && "Expr* 'AndCont' is nullptr!"); - APSInt AndValue; - if (!AndConst->isIntegerConstantExpr(AndValue, *Result.Context)) + Optional AndValue = + AndConst->getIntegerConstantExpr(*Result.Context); + if (!AndValue) return; // If ShiftingConst is shifted left with more bits than the position of the // leftmost 1 in the bit representation of AndValue, AndConstant is // ineffective. - if (AndValue.getActiveBits() > ShiftingValue) + if (AndValue->getActiveBits() > *ShiftingValue) return; auto Diag = diag(BinaryAndExpr->getOperatorLoc(), diff --git a/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp b/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp index 56d4cceb6002..c20472c8de59 100644 --- a/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp +++ b/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp @@ -438,11 +438,12 @@ static bool arrayMatchesBoundExpr(ASTContext *Context, Context->getAsConstantArrayType(ArrayType); if (!ConstType) return false; - llvm::APSInt ConditionSize; - if (!ConditionExpr->isIntegerConstantExpr(ConditionSize, *Context)) + Optional ConditionSize = + ConditionExpr->getIntegerConstantExpr(*Context); + if (!ConditionSize) return false; llvm::APSInt ArraySize(ConstType->getSize()); - return llvm::APSInt::isSameValue(ConditionSize, ArraySize); + return llvm::APSInt::isSameValue(*ConditionSize, ArraySize); } ForLoopIndexUseVisitor::ForLoopIndexUseVisitor(ASTContext *Context,