Skip to content

Commit 69bf6ff

Browse files
committed
Fix internal error spews
Signed-off-by: Yangbo Long <[email protected]>
1 parent fb4be2f commit 69bf6ff

File tree

2 files changed

+82
-43
lines changed

2 files changed

+82
-43
lines changed

src/cpp/fastdds/xtypes/dynamic_types/idl_parser/IdlParser.hpp

+82-42
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ struct action<semicolon>
301301
std::map<std::string, std::string>& state,
302302
std::vector<traits<DynamicData>::ref_type>& /*operands*/)
303303
{
304-
if (!state["type"].empty() && state.count("current_struct_member_name") && !state["current_struct_member_name"].empty())
304+
if (!state["type"].empty() && state.count("current_struct_member_name") &&
305+
!state["current_struct_member_name"].empty())
305306
{
306307
// Add the type and name to the member lists
307308
state["struct_member_types"] += state["type"] + ";";
@@ -780,45 +781,49 @@ load_literal_action(oct_literal, octal, int64_t, TK_INT64, set_int64_value)
780781
load_literal_action(hex_literal, hexa, int64_t, TK_INT64, set_int64_value)
781782
load_literal_action(float_literal, float, long double, TK_FLOAT128, set_float128_value)
782783
load_literal_action(fixed_pt_literal, fixed, long double, TK_FLOAT128, set_float128_value)
783-
load_literal_action(character_literal, char8, char, TK_CHAR8, set_char8_value)
784784
load_literal_action(string_literal, string, std::string, TK_STRING8, set_string_value)
785785

786-
template<>
787-
struct action<wide_character_literal>
788-
{
789-
template<typename Input>
790-
static void apply(
791-
const Input& in,
792-
Context* /*ctx*/,
793-
std::map<std::string, std::string>& state,
794-
std::vector<traits<DynamicData>::ref_type>& operands)
795-
{
796-
std::cout << "wide_character_literal: " << typeid(wide_character_literal).name()
797-
<< " " << in.string() << std::endl;
798-
799-
if (state.count("arithmetic_expr"))
800-
{
801-
state["arithmetic_expr"] += (state["arithmetic_expr"].empty() ? "" : ";") + std::string{"wchar"};
802-
}
803-
804-
wchar_t value = L'\0';
805-
std::string content = in.string().substr(2, in.string().size() - 3);
806-
if (!content.empty()) {
807-
value = static_cast<wchar_t>(content[0]);
808-
}
809-
810-
DynamicTypeBuilderFactory::_ref_type factory {DynamicTypeBuilderFactory::get_instance()};
811-
DynamicType::_ref_type xtype {factory->get_primitive_type(TK_CHAR16)};
812-
DynamicData::_ref_type xdata {DynamicDataFactory::get_instance()->create_data(xtype)};
813-
xdata->set_char16_value(MEMBER_ID_INVALID, value);
814-
815-
if (state.count("arithmetic_expr"))
816-
{
817-
operands.push_back(xdata);
818-
}
819-
}
786+
#define load_character_action(Rule, id, type, type_kind, set_value, offset) \
787+
template<> \
788+
struct action<Rule> \
789+
{ \
790+
template<typename Input> \
791+
static void apply( \
792+
const Input& in, \
793+
Context* /*ctx*/, \
794+
std::map<std::string, std::string>& state, \
795+
std::vector<traits<DynamicData>::ref_type>& operands) \
796+
{ \
797+
std::cout << #Rule << ": " << typeid(Rule).name() << " " << in.string() << std::endl; \
798+
\
799+
if (state.count("arithmetic_expr")) \
800+
{ \
801+
state["arithmetic_expr"] += (state["arithmetic_expr"].empty() ? "" : ";") + std::string{#id}; \
802+
} \
803+
\
804+
const std::string content = in.string().substr(offset, in.string().size() - (offset + 1)); \
805+
if (content.empty() || (content.size() < 1)) \
806+
{ \
807+
EPROSIMA_LOG_ERROR(IDLPARSER, "Invalid character literal: " << content); \
808+
throw std::runtime_error("Invalid character literal: " + content); \
809+
} \
810+
\
811+
type value = static_cast<type>(content[0]); \
812+
\
813+
DynamicTypeBuilderFactory::_ref_type factory {DynamicTypeBuilderFactory::get_instance()}; \
814+
DynamicType::_ref_type xtype = factory->get_primitive_type(type_kind); \
815+
DynamicData::_ref_type xdata {DynamicDataFactory::get_instance()->create_data(xtype)}; \
816+
xdata->set_value(MEMBER_ID_INVALID, value); \
817+
\
818+
if (state.count("arithmetic_expr")) \
819+
{ \
820+
operands.push_back(xdata); \
821+
} \
822+
} \
823+
};
820824

821-
};
825+
load_character_action(character_literal, char8, char, TK_CHAR8, set_char8_value, 1)
826+
load_character_action(wide_character_literal, char16, wchar_t, TK_CHAR16, set_char16_value, 2)
822827

823828
template<>
824829
struct action<wide_string_literal>
@@ -1349,6 +1354,7 @@ struct action<enum_dcl>
13491354

13501355
DynamicType::_ref_type member_type {factory->get_primitive_type(TK_INT32)};
13511356
DynamicData::_ref_type member_data {DynamicDataFactory::get_instance()->create_data(member_type)};
1357+
member_data->set_int32_value(MEMBER_ID_INVALID, (int32_t)i);
13521358

13531359
module.create_constant(tokens[i], member_data, false, true); // Mark it as "from_enum"
13541360
}
@@ -1551,10 +1557,38 @@ struct action<case_label>
15511557
throw std::runtime_error("Finished case label parsing with non-empty operands stack.");
15521558
}
15531559

1554-
int64_t value;
1555-
xdata->get_int64_value(value, MEMBER_ID_INVALID);
1560+
std::string label;
1561+
DynamicType::_ref_type xtype = xdata->type();
1562+
if (TK_INT64 == xtype->get_kind() || TK_INT32 == xtype->get_kind())
1563+
{
1564+
int64_t value = 0;
1565+
xdata->get_int64_value(value, MEMBER_ID_INVALID);
1566+
label = std::to_string(value);
1567+
}
1568+
else if (TK_BOOLEAN == xtype->get_kind())
1569+
{
1570+
bool value = false;
1571+
xdata->get_boolean_value(value, MEMBER_ID_INVALID);
1572+
label = value ? "1" : "0";
1573+
}
1574+
else if (TK_CHAR8 == xtype->get_kind())
1575+
{
1576+
char value = '0';
1577+
xdata->get_char8_value(value, MEMBER_ID_INVALID);
1578+
label = value;
1579+
}
1580+
else if (TK_CHAR16 == xtype->get_kind())
1581+
{
1582+
wchar_t value = L'0';
1583+
xdata->get_char16_value(value, MEMBER_ID_INVALID);
1584+
label = static_cast<char>(value);
1585+
}
1586+
else
1587+
{
1588+
EPROSIMA_LOG_ERROR(IDLPARSER, "Unsupported case label data type: " << xtype->get_kind());
1589+
throw std::runtime_error("Unsupported case label data type: " + xtype->get_kind());
1590+
}
15561591

1557-
std::string label = std::to_string(value);
15581592
if (state["union_labels"].empty() || state["union_labels"].back() == ';')
15591593
{
15601594
state["union_labels"] += label;
@@ -1677,7 +1711,14 @@ struct action<union_def>
16771711
}
16781712
else
16791713
{
1680-
labels[i].push_back(std::stoi(num_str));
1714+
if (std::all_of(num_str.begin(), num_str.end(), ::isdigit))
1715+
{
1716+
labels[i].push_back(std::stoi(num_str));
1717+
}
1718+
else
1719+
{
1720+
labels[i].push_back(static_cast<int32_t>(num_str[0]));
1721+
}
16811722
}
16821723
}
16831724
}
@@ -1693,7 +1734,6 @@ struct action<union_def>
16931734
MemberDescriptor::_ref_type member_descriptor {traits<MemberDescriptor>::make_shared()};
16941735
member_descriptor->name(names[i]);
16951736
member_descriptor->type(member_type);
1696-
member_descriptor->id(i);
16971737

16981738
if (default_label_index == static_cast<int>(i))
16991739
{

test/feature/idl_parser/IdlParserTests.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,6 @@ TEST_F(IdlParserTests, relative_path_include)
10761076
EXPECT_EQ(data1->set_int32_value(0, 2), RETCODE_OK);
10771077
EXPECT_EQ(data1->get_int32_value(test1, 0), RETCODE_OK);
10781078
EXPECT_EQ(test1, 2);
1079-
EXPECT_EQ(data1->set_string_value(0, ""), RETCODE_BAD_PARAMETER);
10801079
}
10811080

10821081
TEST_F(IdlParserTests, unions)

0 commit comments

Comments
 (0)