THRIFT-4324 field names can conflict with local vars in generated code
Client: Delphi
Patch: Jens Geyer
diff --git a/compiler/cpp/src/thrift/generate/t_delphi_generator.cc b/compiler/cpp/src/thrift/generate/t_delphi_generator.cc
index 854013f..1894fe8 100644
--- a/compiler/cpp/src/thrift/generate/t_delphi_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_delphi_generator.cc
@@ -2029,7 +2029,7 @@
indent_up_impl();
indent_impl(s_service_impl) << "seqid_ := seqid_ + 1;" << endl;
- indent_impl(s_service_impl) << "Init( " << msgvar << ", '" << funname
+ indent_impl(s_service_impl) << "Thrift.Protocol.Init( " << msgvar << ", '" << funname
<< "', " << ((*f_iter)->is_oneway() ? "TMessageType.Oneway"
: "TMessageType.Call")
<< ", seqid_);" << endl;
@@ -2257,7 +2257,7 @@
"TApplicationExceptionUnknownMethod.Create("
"'Invalid method name: ''' + msg.Name + '''');" << endl;
indent_impl(s_service_impl)
- << "Init( msg, msg.Name, TMessageType.Exception, msg.SeqID);"
+ << "Thrift.Protocol.Init( msg, msg.Name, TMessageType.Exception, msg.SeqID);"
<< endl;
indent_impl(s_service_impl) << "oprot.WriteMessageBegin( msg);" << endl;
indent_impl(s_service_impl) << "x.Write(oprot);" << endl;
@@ -2459,7 +2459,7 @@
if(events_) {
indent_impl(s_service_impl) << "if events <> nil then events.PreWrite;" << endl;
}
- indent_impl(s_service_impl) << "Init( msg, '"
+ indent_impl(s_service_impl) << "Thrift.Protocol.Init( msg, '"
<< tfunction->get_name() << "', TMessageType.Exception, seqid);"
<< endl;
indent_impl(s_service_impl) << "oprot.WriteMessageBegin( msg);" << endl;
@@ -2487,7 +2487,7 @@
if (events_) {
indent_impl(s_service_impl) << "if events <> nil then events.PreWrite;" << endl;
}
- indent_impl(s_service_impl) << "Init( msg, '"
+ indent_impl(s_service_impl) << "Thrift.Protocol.Init( msg, '"
<< tfunction->get_name() << "', TMessageType.Reply, seqid); "
<< endl;
indent_impl(s_service_impl) << "oprot.WriteMessageBegin( msg); " << endl;
@@ -2804,7 +2804,7 @@
if (ttype->is_map()) {
obj = tmp("map");
local_vars << " " << obj << " : TThriftMap;" << endl;
- indent_impl(out) << "Init( " << obj << ", "
+ indent_impl(out) << "Thrift.Protocol.Init( " << obj << ", "
<< type_to_enum(((t_map*)ttype)->get_key_type()) << ", "
<< type_to_enum(((t_map*)ttype)->get_val_type()) << ", " << prefix
<< ".Count);" << endl;
@@ -2812,14 +2812,14 @@
} else if (ttype->is_set()) {
obj = tmp("set_");
local_vars << " " << obj << " : TThriftSet;" << endl;
- indent_impl(out) << "Init( " << obj << ", "
+ indent_impl(out) << "Thrift.Protocol.Init( " << obj << ", "
<< type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " << prefix
<< ".Count);" << endl;
indent_impl(out) << "oprot.WriteSetBegin( " << obj << ");" << endl;
} else if (ttype->is_list()) {
obj = tmp("list_");
local_vars << " " << obj << " : TThriftList;" << endl;
- indent_impl(out) << "Init( " << obj << ", "
+ indent_impl(out) << "Thrift.Protocol.Init( " << obj << ", "
<< type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " << prefix
<< ".Count);" << endl;
indent_impl(out) << "oprot.WriteListBegin( " << obj << ");" << endl;
@@ -3548,7 +3548,7 @@
<< ") then begin" << endl;
indent_up_impl();
- generate_deserialize_field(code_block, is_exception, *f_iter, "", local_vars);
+ generate_deserialize_field(code_block, is_exception, *f_iter, "Self.", local_vars);
// required field?
if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
@@ -3642,11 +3642,11 @@
indent_impl(local_vars) << "tracker : IProtocolRecursionTracker;" << endl;
indent_impl(code_block) << "tracker := oprot.NextRecursionLevel;" << endl;
- indent_impl(code_block) << "Init( struc, '" << name << "');" << endl;
+ indent_impl(code_block) << "Thrift.Protocol.Init( struc, '" << name << "');" << endl;
indent_impl(code_block) << "oprot.WriteStructBegin(struc);" << endl;
if (fields.size() > 0) {
- indent_impl(code_block) << "Init( field_);" << endl;
+ indent_impl(code_block) << "Thrift.Protocol.Init( field_);" << endl;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
indent_impl(code_block) << "if (__isset_" << prop_name(*f_iter, is_exception) << ") then"
<< endl;
@@ -3657,7 +3657,7 @@
<< endl;
indent_impl(code_block) << "field_.ID := " << (*f_iter)->get_key() << ";" << endl;
indent_impl(code_block) << "oprot.WriteFieldBegin(field_);" << endl;
- generate_serialize_field(code_block, is_exception, *f_iter, "", local_vars);
+ generate_serialize_field(code_block, is_exception, *f_iter, "Self.", local_vars);
indent_impl(code_block) << "oprot.WriteFieldEnd();" << endl;
indent_down_impl();
}
@@ -3706,11 +3706,11 @@
indent_impl(local_vars) << "tracker : IProtocolRecursionTracker;" << endl;
indent_impl(code_block) << "tracker := oprot.NextRecursionLevel;" << endl;
- indent_impl(code_block) << "Init( struc, '" << name << "');" << endl;
+ indent_impl(code_block) << "Thrift.Protocol.Init( struc, '" << name << "');" << endl;
indent_impl(code_block) << "oprot.WriteStructBegin(struc);" << endl;
if (fields.size() > 0) {
- indent_impl(code_block) << "Init( field_);" << endl;
+ indent_impl(code_block) << "Thrift.Protocol.Init( field_);" << endl;
}
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
@@ -3720,13 +3720,13 @@
bool has_isset = (!is_required);
if (is_required && null_allowed) {
null_allowed = false;
- indent_impl(code_block) << "if (" << fieldname << " = nil)" << endl;
+ indent_impl(code_block) << "if (Self." << fieldname << " = nil)" << endl;
indent_impl(code_block) << "then raise TProtocolExceptionInvalidData.Create("
<< "'required field " << fieldname << " not set');"
<< endl;
}
if (null_allowed) {
- indent_impl(code_block) << "if (" << fieldname << " <> nil)";
+ indent_impl(code_block) << "if (Self." << fieldname << " <> nil)";
if (has_isset) {
code_block << " and __isset_" << fieldname;
}
@@ -3743,7 +3743,7 @@
<< endl;
indent_impl(code_block) << "field_.ID := " << (*f_iter)->get_key() << ";" << endl;
indent_impl(code_block) << "oprot.WriteFieldBegin(field_);" << endl;
- generate_serialize_field(code_block, is_exception, *f_iter, "", local_vars);
+ generate_serialize_field(code_block, is_exception, *f_iter, "Self.", local_vars);
indent_impl(code_block) << "oprot.WriteFieldEnd();" << endl;
if (null_allowed || has_isset) {
indent_down_impl();
@@ -3825,7 +3825,7 @@
bool null_allowed = type_can_be_null((*f_iter)->get_type());
bool is_optional = ((*f_iter)->get_req() != t_field::T_REQUIRED);
if (null_allowed) {
- indent_impl(out) << "if (" << prop_name((*f_iter), is_exception) << " <> nil)";
+ indent_impl(out) << "if (Self." << prop_name((*f_iter), is_exception) << " <> nil)";
if (is_optional) {
out << " and __isset_" << prop_name(*f_iter, is_exception);
}
@@ -3857,14 +3857,14 @@
}
if (ttype->is_xception() || ttype->is_struct()) {
- indent_impl(out) << "if (" << prop_name((*f_iter), is_exception) << " = nil) then " << tmp_sb
- << ".Append('<null>') else " << tmp_sb << ".Append("
+ indent_impl(out) << "if (Self." << prop_name((*f_iter), is_exception) << " = nil) then " << tmp_sb
+ << ".Append('<null>') else " << tmp_sb << ".Append( Self."
<< prop_name((*f_iter), is_exception) << ".ToString());" << endl;
} else if (ttype->is_enum()) {
- indent_impl(out) << tmp_sb << ".Append(Integer(" << prop_name((*f_iter), is_exception)
+ indent_impl(out) << tmp_sb << ".Append(Integer( Self." << prop_name((*f_iter), is_exception)
<< "));" << endl;
} else {
- indent_impl(out) << tmp_sb << ".Append(" << prop_name((*f_iter), is_exception) << ");"
+ indent_impl(out) << tmp_sb << ".Append( Self." << prop_name((*f_iter), is_exception) << ");"
<< endl;
}
diff --git a/lib/delphi/test/codegen/ReservedKeywords.thrift b/lib/delphi/test/codegen/ReservedKeywords.thrift
index 300adf9..8db3ea5 100644
--- a/lib/delphi/test/codegen/ReservedKeywords.thrift
+++ b/lib/delphi/test/codegen/ReservedKeywords.thrift
@@ -52,4 +52,49 @@
}
+struct Struct_lists {
+ 1: list<Struct_simple> init;
+ 2: list<Struct_simple> struc;
+ 3: list<Struct_simple> field;
+ 4: list<Struct_simple> field_;
+ 5: list<Struct_simple> tracker;
+ 6: list<Struct_simple> Self;
+}
+
+struct Struct_structs {
+ 1: Struct_simple init;
+ 2: Struct_simple struc;
+ 3: Struct_simple field;
+ 4: Struct_simple field_;
+ 5: Struct_simple tracker;
+ 6: Struct_simple Self;
+}
+
+struct Struct_simple {
+ 1: bool init;
+ 2: bool struc;
+ 3: bool field;
+ 4: bool field_;
+ 5: bool tracker;
+ 6: bool Self;
+}
+
+struct Struct_strings {
+ 1: string init;
+ 2: string struc;
+ 3: string field;
+ 4: string field_;
+ 5: string tracker;
+ 6: string Self;
+}
+
+struct Struct_binary {
+ 1: binary init;
+ 2: binary struc;
+ 3: binary field;
+ 4: binary field_;
+ 5: binary tracker;
+ 6: binary Self;
+}
+
diff --git a/lib/delphi/test/serializer/TestSerializer.dpr b/lib/delphi/test/serializer/TestSerializer.dpr
index 9e283e5..14be502 100644
--- a/lib/delphi/test/serializer/TestSerializer.dpr
+++ b/lib/delphi/test/serializer/TestSerializer.dpr
@@ -35,6 +35,7 @@
Thrift.Serializer in '..\..\src\Thrift.Serializer.pas',
Thrift.Stream in '..\..\src\Thrift.Stream.pas',
Thrift.TypeRegistry in '..\..\src\Thrift.TypeRegistry.pas',
+ ReservedKeywords,
DebugProtoTest,
TestSerializer.Data;