From 4b1a5099240d25d3a59ea4834e20bc7b56b62032 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Thu, 14 Mar 2019 15:23:40 +0000 Subject: [PATCH] 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 --- llvm/include/llvm/Support/YAMLTraits.h | 13 ++++++++----- llvm/unittests/Support/YAMLIOTest.cpp | 6 +++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h index e991f3b3..b80c742 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 - void mapOptional(const char *Key, T &Val, const T &Default) { + template + 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 - void mapOptionalWithContext(const char *Key, T &Val, const T &Default, + template + void mapOptionalWithContext(const char *Key, T &Val, const DefaultT &Default, Context &Ctx) { - this->processKeyWithDefault(Key, Val, Default, false, Ctx); + static_assert(std::is_convertible::value, + "Default type must be implicitly convertible to value type!"); + this->processKeyWithDefault(Key, Val, static_cast(Default), + false, Ctx); } private: diff --git a/llvm/unittests/Support/YAMLIOTest.cpp b/llvm/unittests/Support/YAMLIOTest.cpp index cf279ff..941a765 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 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); } }; -- 2.7.4