From 9457aa6452b0a79187e3fea3a9b3c759353cfaa4 Mon Sep 17 00:00:00 2001 From: Matt Kulukundis Date: Sun, 20 Feb 2022 13:43:04 -0500 Subject: [PATCH] force explicit conversions to desired types in generated code --- upb/upb.h | 18 ++++++++++++++++++ upbc/protoc-gen-upb.cc | 12 ++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/upb/upb.h b/upb/upb.h index 12aecb46cb..5b1824ae9b 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -90,6 +90,24 @@ UPB_INLINE bool upb_StringView_IsEqual(upb_StringView a, upb_StringView b) { return a.size == b.size && memcmp(a.data, b.data, a.size) == 0; } +/** upb_*Int* conversion routines ********************************************/ + +UPB_INLINE int32_t upb_Int32_FromI(int v) { + return (int32_t)v; +} + +UPB_INLINE int64_t upb_Int64_FromLL(long long v) { + return (int64_t)v; +} + +UPB_INLINE uint32_t upb_UInt32_FromU(unsigned v) { + return (uint32_t)v; +} + +UPB_INLINE uint64_t upb_UInt64_FromULL(unsigned long long v) { + return (uint64_t)v; +} + #define UPB_STRINGVIEW_INIT(ptr, len) \ { ptr, len } diff --git a/upbc/protoc-gen-upb.cc b/upbc/protoc-gen-upb.cc index da19286766..6cacda1650 100644 --- a/upbc/protoc-gen-upb.cc +++ b/upbc/protoc-gen-upb.cc @@ -323,13 +323,17 @@ std::string FieldDefault(const protobuf::FieldDescriptor* field) { return absl::Substitute("upb_StringView_FromString(\"$0\")", absl::CEscape(field->default_value_string())); case protobuf::FieldDescriptor::CPPTYPE_INT32: - return absl::StrCat(field->default_value_int32()); + return absl::Substitute("upb_Int32_FromI($0)", + field->default_value_int32()); case protobuf::FieldDescriptor::CPPTYPE_INT64: - return absl::StrCat(field->default_value_int64()); + return absl::Substitute("upb_Int64_FromLL($0ll)", + field->default_value_int64()); case protobuf::FieldDescriptor::CPPTYPE_UINT32: - return absl::StrCat(field->default_value_uint32()); + return absl::Substitute("upb_UInt32_FromU($0u)", + field->default_value_uint32()); case protobuf::FieldDescriptor::CPPTYPE_UINT64: - return absl::StrCat(field->default_value_uint64()); + return absl::Substitute("upb_UInt64_FromULL($0ull)", + field->default_value_uint64()); case protobuf::FieldDescriptor::CPPTYPE_FLOAT: return absl::StrCat(field->default_value_float()); case protobuf::FieldDescriptor::CPPTYPE_DOUBLE: