Skip to content

[Clang] Add a value_or helper to UnsignedOrNone #143516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Sirraide
Copy link
Member

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.

@Sirraide Sirraide added the clang Clang issues not falling into any other category label Jun 10, 2025
@llvmbot llvmbot added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Jun 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 10, 2025

@llvm/pr-subscribers-clang

Author: None (Sirraide)

Changes

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 --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());

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but if the in-tree use does not materialize, we should have this on our radar for removal. It might make sense to wait to land this until you're ready to use it in-tree in a PR.

@Sirraide
Copy link
Member Author

LGTM but if the in-tree use does not materialize, we should have this on our radar for removal. It might make sense to wait to land this until you're ready to use it in-tree in a PR.

Yeah, that’s a good idea

@mizvekov
Copy link
Contributor

No strong objections, but I'd prefer to wait as well.

This was one thing I looked into adding when I initially implemented it, but all potential uses turned out better without it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants