Added special case for INT64_MIN in the codegen

PiperOrigin-RevId: 516236492
pull/13171/head
Joshua Haberman 2 years ago committed by Copybara-Service
parent 3286f948f8
commit bdee30b0a6
  1. 4
      upb/test/test.proto
  2. 17
      upbc/protoc-gen-upb.cc

@ -88,3 +88,7 @@ message ModelWithMaps {
map<string, string> map_ss = 3;
map<int32, int32> map_ii = 4;
}
message ExtremeDefaults {
optional int64 int64_min = 1 [default = -9223372036854775808];
}

@ -45,9 +45,6 @@
#include "absl/strings/substitute.h"
#include "upb/base/descriptor_constants.h"
#include "upb/base/string_view.h"
#include "upb/mem/arena.h"
#include "upb/mini_table/enum_internal.h"
#include "upb/mini_table/extension_internal.h"
#include "upb/reflection/def.hpp"
#include "upb/wire/types.h"
#include "upbc/common.h"
@ -206,7 +203,19 @@ std::string FieldDefault(upb::FieldDefPtr field) {
case kUpb_CType_Int32:
return absl::Substitute("(int32_t)$0", field.default_value().int32_val);
case kUpb_CType_Int64:
return absl::Substitute("(int64_t)$0ll", field.default_value().int64_val);
if (field.default_value().int64_val == INT64_MIN) {
// Special-case to avoid:
// integer literal is too large to be represented in a signed integer
// type, interpreting as unsigned
// [-Werror,-Wimplicitly-unsigned-literal]
// int64_t default_val = (int64_t)-9223372036854775808ll;
//
// More info here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52661
return "INT64_MIN";
} else {
return absl::Substitute("(int64_t)$0ll",
field.default_value().int64_val);
}
case kUpb_CType_UInt32:
return absl::Substitute("(uint32_t)$0u",
field.default_value().uint32_val);

Loading…
Cancel
Save