[llvm] [llvm][Support] Add indirection to call correct validate(...) function (PR #71966)

via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 26 22:31:31 PST 2023


https://github.com/arrv-sc updated https://github.com/llvm/llvm-project/pull/71966

>From 6552ce61f43a527e145f861b17ca62cf1477d984 Mon Sep 17 00:00:00 2001
From: Alexander Romanov <alexander.romanov at syntacore.com>
Date: Fri, 10 Nov 2023 20:40:39 +0300
Subject: [PATCH 1/2] [llvm][Support] Add indirection to call correct
 validate(...) function     Previously "yamlize" overload for
 validatedMappingTraits was     unconditionally calling
 "MappingTraits<T>::validate" even if     "MappingContextTraits<T, Context>"
 was passed to it. Therefore     compilation failed when specifying    
 "MappingContextTraits<T,Context>::validate()"

---
 llvm/include/llvm/Support/YAMLTraits.h | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h
index 99074105a556989..86226ef46cf5482 100644
--- a/llvm/include/llvm/Support/YAMLTraits.h
+++ b/llvm/include/llvm/Support/YAMLTraits.h
@@ -1058,6 +1058,19 @@ yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
   }
 }
 
+template <typename T, typename Context,
+          std::enable_if_t<validatedMappingTraits<T, Context>::value, int> = 0>
+std::string callValidate(IO &io, T &Val, Context &Ctx) {
+  return MappingContextTraits<T, Context>::validate(io, Val, Ctx);
+}
+
+template <
+    typename T,
+    std::enable_if_t<validatedMappingTraits<T, EmptyContext>::value, int> = 0>
+std::string callValidate(IO &io, T &Val, EmptyContext = {}) {
+  return MappingTraits<T>::validate(io, Val);
+}
+
 template <typename T, typename Context>
 std::enable_if_t<validatedMappingTraits<T, Context>::value, void>
 yamlize(IO &io, T &Val, bool, Context &Ctx) {
@@ -1066,7 +1079,7 @@ yamlize(IO &io, T &Val, bool, Context &Ctx) {
   else
     io.beginMapping();
   if (io.outputting()) {
-    std::string Err = MappingTraits<T>::validate(io, Val);
+    std::string Err = callValidate(io, Val, Ctx);
     if (!Err.empty()) {
       errs() << Err << "\n";
       assert(Err.empty() && "invalid struct trying to be written as yaml");
@@ -1074,7 +1087,7 @@ yamlize(IO &io, T &Val, bool, Context &Ctx) {
   }
   detail::doMapping(io, Val, Ctx);
   if (!io.outputting()) {
-    std::string Err = MappingTraits<T>::validate(io, Val);
+    std::string Err = callValidate(io, Val, Ctx);
     if (!Err.empty())
       io.setError(Err);
   }

>From 8e70fd2c74489cf52cbc3c12103d0a1da6e9934a Mon Sep 17 00:00:00 2001
From: Alexander Romanov <alexander.romanov at syntacore.com>
Date: Mon, 27 Nov 2023 09:31:10 +0300
Subject: [PATCH 2/2] add test

---
 llvm/unittests/Support/YAMLIOTest.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/llvm/unittests/Support/YAMLIOTest.cpp b/llvm/unittests/Support/YAMLIOTest.cpp
index 488746764ae6550..a061fd76f3c9ef0 100644
--- a/llvm/unittests/Support/YAMLIOTest.cpp
+++ b/llvm/unittests/Support/YAMLIOTest.cpp
@@ -2623,6 +2623,9 @@ template <> struct MappingContextTraits<SimpleMap, MappingContext> {
     ++Context.A;
     io.mapRequired("Context", Context.A);
   }
+  std::string validate(IO &io, SimpleMap &sm, MappingContext &Context) {
+    return "";
+  }
 };
 
 template <> struct MappingTraits<NestedMap> {



More information about the llvm-commits mailing list