Modernize Optional::{getValueOr,hasValue}
authorFangrui Song <i@maskray.me>
Fri, 15 Jul 2022 08:20:38 +0000 (01:20 -0700)
committerFangrui Song <i@maskray.me>
Fri, 15 Jul 2022 08:20:39 +0000 (01:20 -0700)
clang-tools-extra/clangd/AST.cpp
clang/lib/Analysis/FlowSensitive/DebugSupport.cpp
clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp
lldb/source/Plugins/Process/Linux/IntelPTCollector.cpp
lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
llvm/include/llvm/Support/Casting.h
llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp
mlir/include/mlir/IR/OpImplementation.h
mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp

index 6ceff56..4df043d 100644 (file)
@@ -942,7 +942,7 @@ resolveForwardingParameters(const FunctionDecl *D, unsigned MaxDepth) {
       TailIt = std::copy(Info.Tail.rbegin(), Info.Tail.rend(), TailIt);
       // Prepare next recursion level
       Pack = Info.Pack;
-      CurrentFunction = Info.PackTarget.getValueOr(nullptr);
+      CurrentFunction = Info.PackTarget.value_or(nullptr);
       Depth++;
       // If we are recursing into a previously encountered function: Abort
       if (CurrentFunction) {
index 305d9d3..a2895d0 100644 (file)
@@ -103,8 +103,7 @@ Constraints
 
     auto StatusString = debugString(Result.getStatus());
     auto Solution = Result.getSolution();
-    auto SolutionString =
-        Solution.hasValue() ? "\n" + debugString(Solution.value()) : "";
+    auto SolutionString = Solution ? "\n" + debugString(Solution.value()) : "";
 
     return formatv(
         Template,
index 825b11a..08fac9f 100644 (file)
@@ -504,7 +504,7 @@ void ExprEngine::handleConstructor(const Expr *E,
 
     unsigned Idx = 0;
     if (CE->getType()->isArrayType()) {
-      Idx = getIndexOfElementToConstruct(State, CE, LCtx).getValueOr(0u);
+      Idx = getIndexOfElementToConstruct(State, CE, LCtx).value_or(0u);
       State = setIndexOfElementToConstruct(State, CE, LCtx, Idx + 1);
     }
 
index 2c44683..f899364 100644 (file)
@@ -124,7 +124,7 @@ Error IntelPTCollector::TraceStart(const TraceIntelPTStartRequest &request) {
 
       // We try to use cgroup filtering whenever possible
       Optional<int> cgroup_fd;
-      if (!request.disable_cgroup_filtering.getValueOr(false))
+      if (!request.disable_cgroup_filtering.value_or(false))
         cgroup_fd = GetCGroupFileDescriptor(m_process.GetID());
 
       if (Expected<IntelPTProcessTraceUP> trace =
index cbc24b1..8ee709d 100644 (file)
@@ -3475,10 +3475,9 @@ VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
 
   if (use_type_size_for_value && type_sp->GetType()) {
     DWARFExpression *location = location_list.GetMutableExpressionAtAddress();
-    location->UpdateValue(
-        const_value_form.Unsigned(),
-        type_sp->GetType()->GetByteSize(nullptr).getValueOr(0),
-        die.GetCU()->GetAddressByteSize());
+    location->UpdateValue(const_value_form.Unsigned(),
+                          type_sp->GetType()->GetByteSize(nullptr).value_or(0),
+                          die.GetCU()->GetAddressByteSize());
   }
 
   return std::make_shared<Variable>(
index 5444d77..b6bbff8 100644 (file)
@@ -265,7 +265,7 @@ struct CastIsPossible {
 template <typename To, typename From>
 struct CastIsPossible<To, Optional<From>> {
   static inline bool isPossible(const Optional<From> &f) {
-    assert(f.hasValue() && "CastIsPossible::isPossible called on a nullopt!");
+    assert(f && "CastIsPossible::isPossible called on a nullopt!");
     return isa_impl_wrap<
         To, const From,
         typename simplify_type<const From>::SimpleType>::doit(*f);
index 468c4f4..2d08d5c 100644 (file)
@@ -38,9 +38,7 @@ static std::string computeDataLayout(const Triple &TT) {
 
 static Reloc::Model getEffectiveRelocModel(const Triple &TT,
                                            Optional<Reloc::Model> RM) {
-  if (!RM.hasValue())
-    return Reloc::Static;
-  return *RM;
+  return RM.value_or(Reloc::Static);
 }
 
 LoongArchTargetMachine::LoongArchTargetMachine(
index c4bbac9..bfb8add 100644 (file)
@@ -723,7 +723,7 @@ public:
     }
 
     /// Returns true if this switch has a value yet.
-    bool hasValue() const { return result.hasValue(); }
+    bool hasValue() const { return result.has_value(); }
 
     /// Return the result of the switch.
     LLVM_NODISCARD operator ResultT() {
index 868aff9..a423cd2 100644 (file)
@@ -1878,14 +1878,14 @@ ContractionOpLowering::lowerParallel(vector::ContractionOp op, int64_t lhsIndex,
       diag << "expected either lhsIndex=" << lhsIndex
            << " or rhsIndex=" << rhsIndex << " to be nonnegative";
     });
-  // getValueOr(-1) means that we tolerate a dimension not appearing
+  // value_or(-1) means that we tolerate a dimension not appearing
   // in the result map. That can't happen for actual parallel iterators, but
   // the caller ContractionOpLowering::matchAndRewrite is currently calling
   // lowerParallel also for the case of unit-size reduction dims appearing only
   // on one of LHS or RHS, not both. At the moment, such cases are created by
   // CastAwayContractionLeadingOneDim, so we need to either support that or
   // modify that pattern.
-  int64_t resIndex = getResultIndex(iMap[2], iterIndex).getValueOr(-1);
+  int64_t resIndex = getResultIndex(iMap[2], iterIndex).value_or(-1);
   if (resIndex == -1 && dimSize != 1)
     return rewriter.notifyMatchFailure(op, [&](Diagnostic &diag) {
       diag << "expected the dimension for iterIndex=" << iterIndex
@@ -1932,11 +1932,11 @@ ContractionOpLowering::lowerReduction(vector::ContractionOp op,
   SmallVector<AffineMap, 4> iMap = op.getIndexingMaps();
   Optional<int64_t> lookupLhs = getResultIndex(iMap[0], iterIndex);
   Optional<int64_t> lookupRhs = getResultIndex(iMap[1], iterIndex);
-  if (!lookupLhs.hasValue())
+  if (!lookupLhs.has_value())
     return rewriter.notifyMatchFailure(op, [&](Diagnostic &diag) {
       diag << "expected iterIndex=" << iterIndex << "to map to a LHS dimension";
     });
-  if (!lookupRhs.hasValue())
+  if (!lookupRhs.has_value())
     return rewriter.notifyMatchFailure(op, [&](Diagnostic &diag) {
       diag << "expected iterIndex=" << iterIndex << "to map to a RHS dimension";
     });