1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 03:02:36 +01:00

YAMLIO: Improve template arg deduction for mapOptional

Summary:
The way c++ template argument deduction works, both arguments are used
to deduce the template type in the three-argument overload of
mapOptional. This is a problem if the types are slightly different, even
if they are implicitly convertible. This is fairly easy to trigger with
integral types, as the default type of most integral constants is int,
which then requires casting the constant to the type of the other
argument.

This patch fixes that by using a separate template type for the default
value, which is then cast to the type of the first argument.  To avoid
this conversion triggerring conversions marged as explicit, we use
static_assert to check that the types are implicitly convertible.

Reviewers: zturner, sammccall

Subscribers: kristina, jdoerfert, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D59142

llvm-svn: 356157
This commit is contained in:
Pavel Labath 2019-03-14 15:23:40 +00:00
parent 557ae7b7e6
commit f7b6caaec0
2 changed files with 11 additions and 8 deletions

View File

@ -863,8 +863,8 @@ public:
mapOptionalWithContext(Key, Val, Ctx);
}
template <typename T>
void mapOptional(const char *Key, T &Val, const T &Default) {
template <typename T, typename DefaultT>
void mapOptional(const char *Key, T &Val, const DefaultT &Default) {
EmptyContext Ctx;
mapOptionalWithContext(Key, Val, Default, Ctx);
}
@ -890,10 +890,13 @@ public:
this->processKey(Key, Val, false, Ctx);
}
template <typename T, typename Context>
void mapOptionalWithContext(const char *Key, T &Val, const T &Default,
template <typename T, typename Context, typename DefaultT>
void mapOptionalWithContext(const char *Key, T &Val, const DefaultT &Default,
Context &Ctx) {
this->processKeyWithDefault(Key, Val, Default, false, Ctx);
static_assert(std::is_convertible<DefaultT, T>::value,
"Default type must be implicitly convertible to value type!");
this->processKeyWithDefault(Key, Val, static_cast<const T &>(Default),
false, Ctx);
}
private:

View File

@ -823,7 +823,7 @@ namespace yaml {
io.mapRequired("f1", c.f1);
io.mapRequired("f2", c.f2);
io.mapRequired("f3", c.f3);
io.mapOptional("f4", c.f4, MyFlags(flagRound));
io.mapOptional("f4", c.f4, flagRound);
}
};
}
@ -1327,8 +1327,8 @@ namespace yaml {
static void mapping(IO &io, TotalSeconds &secs) {
MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
io.mapOptional("hours", keys->hours, (uint32_t)0);
io.mapOptional("minutes", keys->minutes, (uint8_t)0);
io.mapOptional("hours", keys->hours, 0);
io.mapOptional("minutes", keys->minutes, 0);
io.mapRequired("seconds", keys->seconds);
}
};