summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPavel Labath <pavel@labath.sk>2019-03-14 15:23:40 +0000
committerPavel Labath <pavel@labath.sk>2019-03-14 15:23:40 +0000
commit4b1a5099240d25d3a59ea4834e20bc7b56b62032 (patch)
tree23b74bbecf7015b4f2d670b6307129fb3b418af2
parent6bc3a7703b31a4811124e263a47b91e8b5b8958a (diff)
downloadbcm5719-llvm-4b1a5099240d25d3a59ea4834e20bc7b56b62032.tar.gz
bcm5719-llvm-4b1a5099240d25d3a59ea4834e20bc7b56b62032.zip
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
-rw-r--r--llvm/include/llvm/Support/YAMLTraits.h13
-rw-r--r--llvm/unittests/Support/YAMLIOTest.cpp6
2 files changed, 11 insertions, 8 deletions
diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h
index e991f3b3be1..b80c7426d9b 100644
--- a/llvm/include/llvm/Support/YAMLTraits.h
+++ b/llvm/include/llvm/Support/YAMLTraits.h
@@ -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:
diff --git a/llvm/unittests/Support/YAMLIOTest.cpp b/llvm/unittests/Support/YAMLIOTest.cpp
index cf279ffd4c2..941a765a3c1 100644
--- a/llvm/unittests/Support/YAMLIOTest.cpp
+++ b/llvm/unittests/Support/YAMLIOTest.cpp
@@ -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);
}
};
OpenPOWER on IntegriCloud