[clang] [Clang] Add a `value_or` helper to `UnsignedOrNone` (PR #143516)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 10 05:15:01 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (Sirraide)

<details>
<summary>Changes</summary>

This adds two `value_or` overloads to `UnsignedOrNone`. I’ve had a use for these in something I’m currently working on and thought it might make sense to add them separately. The overload that takes an `UnsignedOrNone` might seem a bit strange at first, but I think it makes sense given how specific this class already is.

---
Full diff: https://github.com/llvm/llvm-project/pull/143516.diff


1 Files Affected:

- (modified) clang/include/clang/Basic/UnsignedOrNone.h (+8) 


``````````diff
diff --git a/clang/include/clang/Basic/UnsignedOrNone.h b/clang/include/clang/Basic/UnsignedOrNone.h
index 659fd8c6487d2..5081e3c2a9e32 100644
--- a/clang/include/clang/Basic/UnsignedOrNone.h
+++ b/clang/include/clang/Basic/UnsignedOrNone.h
@@ -29,6 +29,14 @@ struct UnsignedOrNone {
   }
   constexpr unsigned toInternalRepresentation() const { return Rep; }
 
+  constexpr unsigned value_or(unsigned val) const {
+    return operator bool() ? **this : val;
+  }
+
+  constexpr UnsignedOrNone value_or(UnsignedOrNone val) const {
+    return operator bool() ? *this : val;
+  }
+
   explicit constexpr operator bool() const { return Rep != 0; }
   unsigned operator*() const {
     assert(operator bool());

``````````

</details>


https://github.com/llvm/llvm-project/pull/143516


More information about the cfe-commits mailing list