Fix the clang-tidy build after get/isIntegerConstantExpression
authorHaojian Wu <hokein.wu@gmail.com>
Wed, 22 Jul 2020 07:37:51 +0000 (09:37 +0200)
committerHaojian Wu <hokein.wu@gmail.com>
Wed, 22 Jul 2020 07:38:56 +0000 (09:38 +0200)
refactoring.

clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp
clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp
clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp

index aa860b30fe75976a51f76c5f3db66e62abc93019..1837ccb6002f956efbdf905bca876b222f895b46 100644 (file)
@@ -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;
index dd0bedd742a40b401219bf207380bc7ad3b4169c..96b0bb0f9b02d92ffafab784aa6dd0ec30dd6ac0 100644 (file)
@@ -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<llvm::APSInt> Index =
+      IndexExpr->getIntegerConstantExpr(*Result.Context);
+  if (!Index) {
     SourceRange BaseRange;
     if (const auto *ArraySubscriptE = dyn_cast<ArraySubscriptExpr>(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);
   }
 }
 
index aef513a527b545a7b0967a0ffdb5641e8b9657ba..b84e4d525d8cf24f7934799e576ab0b9b2c3b0e4 100644 (file)
@@ -500,7 +500,13 @@ static bool retrieveIntegerConstantExpr(const MatchFinder::MatchResult &Result,
                                         const Expr *&ConstExpr) {
   std::string CstId = (Id + "-const").str();
   ConstExpr = Result.Nodes.getNodeAs<Expr>(CstId);
-  return ConstExpr && ConstExpr->isIntegerConstantExpr(Value, *Result.Context);
+  if (!ConstExpr)
+    return false;
+  Optional<llvm::APSInt> 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<Expr>("shift-const");
     assert(ShiftingConst && "Expr* 'ShiftingConst' is nullptr!");
-    APSInt ShiftingValue;
+    Optional<llvm::APSInt> ShiftingValue =
+        ShiftingConst->getIntegerConstantExpr(*Result.Context);
 
-    if (!ShiftingConst->isIntegerConstantExpr(ShiftingValue, *Result.Context))
+    if (!ShiftingValue)
       return;
 
     const auto *AndConst = Result.Nodes.getNodeAs<Expr>("and-const");
     assert(AndConst && "Expr* 'AndCont' is nullptr!");
-    APSInt AndValue;
-    if (!AndConst->isIntegerConstantExpr(AndValue, *Result.Context))
+    Optional<llvm::APSInt> 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(),
index 56d4cceb6002a08ac626e9d795412679b4d754a8..c20472c8de593133ff5fa4b8dd88fb695e803aa6 100644 (file)
@@ -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<llvm::APSInt> 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,