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

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 27 12:30:48 PDT 2025


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

>From 944bf0da1df11b26b40b5ae061caef21499f43fe Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sun, 27 Apr 2025 09:04:55 -0700
Subject: [PATCH 1/2] [Support] Simplify mapOptionalWithContext (NFC)

We can use "constexpt if" to combine the two variants of functions.
---
 llvm/include/llvm/Support/YAMLTraits.h | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)

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) {

>From b64f69c5361b27dd28e7ce05aef6023eb0406e98 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sun, 27 Apr 2025 12:30:41 -0700
Subject: [PATCH 2/2] Update llvm/include/llvm/Support/YAMLTraits.h

Co-authored-by: Nikita Popov <github at npopov.com>
---
 llvm/include/llvm/Support/YAMLTraits.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h
index 1a6094a92c6fd..913d927b6ee63 100644
--- a/llvm/include/llvm/Support/YAMLTraits.h
+++ b/llvm/include/llvm/Support/YAMLTraits.h
@@ -904,7 +904,7 @@ class IO {
   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()))
+      if (this->canElideEmptySequence() && Val.begin() == Val.end())
         return;
     }
     this->processKey(Key, Val, false, Ctx);



More information about the llvm-commits mailing list