Skip to content

Commit

Permalink
Fix Integer Literals for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
iguessthislldo committed Jul 9, 2021
1 parent 90531b1 commit 1655769
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
42 changes: 24 additions & 18 deletions TAO/TAO_IDL/be/be_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,26 @@ TAO_OutStream::print (UTL_IdList *idl)
return *this;
}

template <typename IntType>
void
signed_int_helper (TAO_OutStream &os, IntType value, IntType min, const char *specifier)
{
/*
* It seems that in C/C++ the minus sign and the bare number are parsed
* separately for negative integer literals. This can cause compilers
* to complain when using the minimum value of a signed integer because
* the number without the minus sign is 1 past the max signed value.
*
* https://stackoverflow.com/questions/65007935
*
* Apparently the workaround is to write it as `VALUE_PLUS_ONE - 1`.
*/
const bool min_value = value == min;
if (min_value) ++value;
os.print (specifier, value);
if (min_value) os.print (" - 1");
}

TAO_OutStream&
TAO_OutStream::print (AST_Expression *expr)
{
Expand All @@ -523,30 +543,16 @@ TAO_OutStream::print (AST_Expression *expr)
this->TAO_OutStream::print (ACE_INT32_FORMAT_SPECIFIER_ASCII "%c", ev->u.usval, 'U');
break;
case AST_Expression::EV_long:
this->TAO_OutStream::print (ACE_INT32_FORMAT_SPECIFIER_ASCII, ev->u.lval);
signed_int_helper<ACE_CDR::Long> (
*this, ev->u.lval, ACE_INT32_MIN, ACE_INT32_FORMAT_SPECIFIER_ASCII);
break;
case AST_Expression::EV_ulong:
this->TAO_OutStream::print (ACE_UINT32_FORMAT_SPECIFIER_ASCII "%c", ev->u.ulval, 'U');
break;
case AST_Expression::EV_longlong:
this->TAO_OutStream::print ("ACE_INT64_LITERAL (");
{
ACE_CDR::LongLong value = ev->u.llval;
/*
* It seems that in C/C++ the minus sign and the bare number are parsed
* separately for negative integer literals. This can cause compilers
* to complain when using the minimum value of a signed integer because
* the number without the minus sign is 1 past the max signed value.
*
* https://stackoverflow.com/questions/65007935
*
* Apparently the workaround is to write it as `VALUE_PLUS_ONE - 1`.
*/
const bool min_value = value == ACE_INT64_MIN;
if (min_value) ++value;
TAO_OutStream::print (ACE_INT64_FORMAT_SPECIFIER_ASCII, value);
if (min_value) TAO_OutStream::print (" - 1");
}
signed_int_helper<ACE_CDR::LongLong> (
*this, ev->u.llval, ACE_INT64_MIN, ACE_INT64_FORMAT_SPECIFIER_ASCII);
this->TAO_OutStream::print (")");
break;
case AST_Expression::EV_ulonglong:
Expand Down
4 changes: 2 additions & 2 deletions TAO/TAO_IDL/be/be_union_branch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,9 @@ be_union_branch::gen_default_label_value (TAO_OutStream *os,
switch (bu->udisc_type ())
{
case AST_Expression::EV_short:
case AST_Expression::EV_int8:
*os << dv.u.short_val;
break;
case AST_Expression::EV_ushort:
case AST_Expression::EV_uint8:
*os << dv.u.ushort_val;
break;
case AST_Expression::EV_long:
Expand All @@ -133,6 +131,8 @@ be_union_branch::gen_default_label_value (TAO_OutStream *os,
break;
case AST_Expression::EV_octet:
case AST_Expression::EV_char:
case AST_Expression::EV_int8:
case AST_Expression::EV_uint8:
os->print ("'\\%o'", dv.u.char_val);
break;
case AST_Expression::EV_bool:
Expand Down
4 changes: 2 additions & 2 deletions TAO/TAO_IDL/be/be_visitor_union/discriminant_ci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,9 @@ be_visitor_union_discriminant_ci::visit_predefined_type (
switch (bu->udisc_type ())
{
case AST_Expression::EV_short:
case AST_Expression::EV_int8:
*os << dv.u.short_val;
break;
case AST_Expression::EV_ushort:
case AST_Expression::EV_uint8:
*os << dv.u.ushort_val;
break;
case AST_Expression::EV_long:
Expand All @@ -174,6 +172,8 @@ be_visitor_union_discriminant_ci::visit_predefined_type (
break;
case AST_Expression::EV_char:
case AST_Expression::EV_octet:
case AST_Expression::EV_int8:
case AST_Expression::EV_uint8:
os->print ("'\\%o'", dv.u.char_val);
break;
case AST_Expression::EV_bool:
Expand Down
4 changes: 2 additions & 2 deletions TAO/tests/IDLv4/explicit_ints/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ ACE_TMAIN (int, ACE_TCHAR *[])
expect_equals<CORBA::Int16> (any_failed, "i16_min", i16_min, -32768);
expect_equals<CORBA::Int16> (any_failed, "i16_max", i16_max, 32767);
expect_equals<CORBA::UInt32> (any_failed, "u32_max", u32_max, 4294967295);
expect_equals<CORBA::Int32> (any_failed, "i32_min", i32_min, -2147483648);
expect_equals<CORBA::Int32> (any_failed, "i32_min", i32_min, -2147483647 - 1);
expect_equals<CORBA::Int32> (any_failed, "i32_max", i32_max, 2147483647);
expect_equals<CORBA::UInt64> (any_failed, "u64_max", u64_max, 18446744073709551615ULL);
expect_equals<CORBA::Int64> (any_failed, "i64_min", i64_min, (-9223372036854775807 - 1));
expect_equals<CORBA::Int64> (any_failed, "i64_min", i64_min, -9223372036854775807 - 1);
expect_equals<CORBA::Int64> (any_failed, "i64_max", i64_max, 9223372036854775807);

expect_equals<CORBA::UInt8> (any_failed, "u8_min_overflow", u8_min_overflow, u8_max);
Expand Down

0 comments on commit 1655769

Please sign in to comment.