Skip to content

Commit 74cf525

Browse files
authored
[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()"
1 parent 482a37b commit 74cf525

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

llvm/include/llvm/Support/YAMLTraits.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,19 @@ yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
10581058
}
10591059
}
10601060

1061+
namespace detail {
1062+
1063+
template <typename T, typename Context>
1064+
std::string doValidate(IO &io, T &Val, Context &Ctx) {
1065+
return MappingContextTraits<T, Context>::validate(io, Val, Ctx);
1066+
}
1067+
1068+
template <typename T> std::string doValidate(IO &io, T &Val, EmptyContext &) {
1069+
return MappingTraits<T>::validate(io, Val);
1070+
}
1071+
1072+
} // namespace detail
1073+
10611074
template <typename T, typename Context>
10621075
std::enable_if_t<validatedMappingTraits<T, Context>::value, void>
10631076
yamlize(IO &io, T &Val, bool, Context &Ctx) {
@@ -1066,15 +1079,15 @@ yamlize(IO &io, T &Val, bool, Context &Ctx) {
10661079
else
10671080
io.beginMapping();
10681081
if (io.outputting()) {
1069-
std::string Err = MappingTraits<T>::validate(io, Val);
1082+
std::string Err = detail::doValidate(io, Val, Ctx);
10701083
if (!Err.empty()) {
10711084
errs() << Err << "\n";
10721085
assert(Err.empty() && "invalid struct trying to be written as yaml");
10731086
}
10741087
}
10751088
detail::doMapping(io, Val, Ctx);
10761089
if (!io.outputting()) {
1077-
std::string Err = MappingTraits<T>::validate(io, Val);
1090+
std::string Err = detail::doValidate(io, Val, Ctx);
10781091
if (!Err.empty())
10791092
io.setError(Err);
10801093
}

llvm/unittests/Support/YAMLIOTest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2623,6 +2623,9 @@ template <> struct MappingContextTraits<SimpleMap, MappingContext> {
26232623
++Context.A;
26242624
io.mapRequired("Context", Context.A);
26252625
}
2626+
static std::string validate(IO &io, SimpleMap &sm, MappingContext &Context) {
2627+
return "";
2628+
}
26262629
};
26272630

26282631
template <> struct MappingTraits<NestedMap> {

0 commit comments

Comments
 (0)