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

via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 18 05:02:20 PST 2023


Author: arrv-sc
Date: 2023-12-18T13:02:17Z
New Revision: 74cf5254d284f0218db29f535e9ccbcc6a59a3a4

URL: https://github.com/llvm/llvm-project/commit/74cf5254d284f0218db29f535e9ccbcc6a59a3a4
DIFF: https://github.com/llvm/llvm-project/commit/74cf5254d284f0218db29f535e9ccbcc6a59a3a4.diff

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

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

Added: 
    

Modified: 
    llvm/include/llvm/Support/YAMLTraits.h
    llvm/unittests/Support/YAMLIOTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h
index 99074105a55698..3b1f4bad57fcf1 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) {
   }
 }
 
+namespace detail {
+
+template <typename T, typename Context>
+std::string doValidate(IO &io, T &Val, Context &Ctx) {
+  return MappingContextTraits<T, Context>::validate(io, Val, Ctx);
+}
+
+template <typename T> std::string doValidate(IO &io, T &Val, EmptyContext &) {
+  return MappingTraits<T>::validate(io, Val);
+}
+
+} // namespace detail
+
 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 = detail::doValidate(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 = detail::doValidate(io, Val, Ctx);
     if (!Err.empty())
       io.setError(Err);
   }

diff  --git a/llvm/unittests/Support/YAMLIOTest.cpp b/llvm/unittests/Support/YAMLIOTest.cpp
index 488746764ae655..401981f3841ee2 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);
   }
+  static std::string validate(IO &io, SimpleMap &sm, MappingContext &Context) {
+    return "";
+  }
 };
 
 template <> struct MappingTraits<NestedMap> {


        


More information about the llvm-commits mailing list