[llvm] [Support] Simplify mapOptionalWithContext (NFC) (PR #137529)

via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 27 10:01:51 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Kazu Hirata (kazutakahirata)

<details>
<summary>Changes</summary>

We can use "constexpt if" to combine the two variants of functions.


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


1 Files Affected:

- (modified) llvm/include/llvm/Support/YAMLTraits.h (+6-11) 


``````````diff
diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h
index e707a445012b5..1a6094a92c6fd 100644
--- a/llvm/include/llvm/Support/YAMLTraits.h
+++ b/llvm/include/llvm/Support/YAMLTraits.h
@@ -901,11 +901,12 @@ class IO {
   }
 
   template <typename T, typename Context>
-  std::enable_if_t<has_SequenceTraits<T>::value, void>
-  mapOptionalWithContext(const char *Key, T &Val, Context &Ctx) {
-    // omit key/value instead of outputting empty sequence
-    if (this->canElideEmptySequence() && !(Val.begin() != Val.end()))
-      return;
+  void mapOptionalWithContext(const char *Key, T &Val, Context &Ctx) {
+    if constexpr (has_SequenceTraits<T>::value) {
+      // omit key/value instead of outputting empty sequence
+      if (this->canElideEmptySequence() && !(Val.begin() != Val.end()))
+        return;
+    }
     this->processKey(Key, Val, false, Ctx);
   }
 
@@ -916,12 +917,6 @@ class IO {
                                 /*Required=*/false, Ctx);
   }
 
-  template <typename T, typename Context>
-  std::enable_if_t<!has_SequenceTraits<T>::value, void>
-  mapOptionalWithContext(const char *Key, T &Val, Context &Ctx) {
-    this->processKey(Key, Val, false, Ctx);
-  }
-
   template <typename T, typename Context, typename DefaultT>
   void mapOptionalWithContext(const char *Key, T &Val, const DefaultT &Default,
                               Context &Ctx) {

``````````

</details>


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


More information about the llvm-commits mailing list