From 3c849d0aefa1101cf38586449c2105a97797c414 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Fri, 15 Jul 2022 01:20:38 -0700 Subject: [PATCH] Modernize Optional::{getValueOr,hasValue} --- clang-tools-extra/clangd/AST.cpp | 2 +- clang/lib/Analysis/FlowSensitive/DebugSupport.cpp | 3 +-- clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp | 2 +- lldb/source/Plugins/Process/Linux/IntelPTCollector.cpp | 2 +- lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp | 7 +++---- llvm/include/llvm/Support/Casting.h | 2 +- llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp | 4 +--- mlir/include/mlir/IR/OpImplementation.h | 2 +- mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp | 8 ++++---- 9 files changed, 14 insertions(+), 18 deletions(-) diff --git a/clang-tools-extra/clangd/AST.cpp b/clang-tools-extra/clangd/AST.cpp index 6ceff56..4df043d 100644 --- a/clang-tools-extra/clangd/AST.cpp +++ b/clang-tools-extra/clangd/AST.cpp @@ -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) { diff --git a/clang/lib/Analysis/FlowSensitive/DebugSupport.cpp b/clang/lib/Analysis/FlowSensitive/DebugSupport.cpp index 305d9d3..a2895d0 100644 --- a/clang/lib/Analysis/FlowSensitive/DebugSupport.cpp +++ b/clang/lib/Analysis/FlowSensitive/DebugSupport.cpp @@ -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, diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp index 825b11a..08fac9f 100644 --- a/clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp +++ b/clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp @@ -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); } diff --git a/lldb/source/Plugins/Process/Linux/IntelPTCollector.cpp b/lldb/source/Plugins/Process/Linux/IntelPTCollector.cpp index 2c44683..f899364 100644 --- a/lldb/source/Plugins/Process/Linux/IntelPTCollector.cpp +++ b/lldb/source/Plugins/Process/Linux/IntelPTCollector.cpp @@ -124,7 +124,7 @@ Error IntelPTCollector::TraceStart(const TraceIntelPTStartRequest &request) { // We try to use cgroup filtering whenever possible Optional 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 trace = diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp index cbc24b1..8ee709d 100644 --- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp +++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp @@ -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( diff --git a/llvm/include/llvm/Support/Casting.h b/llvm/include/llvm/Support/Casting.h index 5444d77..b6bbff8 100644 --- a/llvm/include/llvm/Support/Casting.h +++ b/llvm/include/llvm/Support/Casting.h @@ -265,7 +265,7 @@ struct CastIsPossible { template struct CastIsPossible> { static inline bool isPossible(const Optional &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::SimpleType>::doit(*f); diff --git a/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp b/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp index 468c4f4..2d08d5c 100644 --- a/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp +++ b/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp @@ -38,9 +38,7 @@ static std::string computeDataLayout(const Triple &TT) { static Reloc::Model getEffectiveRelocModel(const Triple &TT, Optional RM) { - if (!RM.hasValue()) - return Reloc::Static; - return *RM; + return RM.value_or(Reloc::Static); } LoongArchTargetMachine::LoongArchTargetMachine( diff --git a/mlir/include/mlir/IR/OpImplementation.h b/mlir/include/mlir/IR/OpImplementation.h index c4bbac9..bfb8add 100644 --- a/mlir/include/mlir/IR/OpImplementation.h +++ b/mlir/include/mlir/IR/OpImplementation.h @@ -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() { diff --git a/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp b/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp index 868aff9..a423cd2 100644 --- a/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp +++ b/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp @@ -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 iMap = op.getIndexingMaps(); Optional lookupLhs = getResultIndex(iMap[0], iterIndex); Optional 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"; }); -- 2.7.4