c#: Checking null for parameters generated code.

Checking null for generated code
ref T8399

Reviewed-by: Felipe Magno de Almeida <felipe@expertisesolutions.com.br>
Differential Revision: https://phab.enlightenment.org/D10970
This commit is contained in:
Bruno da Silva Belo 2020-01-02 22:27:25 +00:00 committed by Felipe Magno de Almeida
parent 0aaad623a0
commit 5c99f08511
5 changed files with 32 additions and 17 deletions

View File

@ -165,7 +165,8 @@ struct pack_event_info_and_call_visitor
if (regular.is_struct())
{
return as_generator(
indent << "IntPtr info = Marshal.AllocHGlobal(Marshal.SizeOf(e.arg));\n"
indent << "Contract.Requires(e != null, nameof(e));\n"
<< indent << "IntPtr info = Marshal.AllocHGlobal(Marshal.SizeOf(e.arg));\n"
<< indent << "CallNativeEventCallback(" + library_name + ", \"_" + evt_c_name + "\", info, " << "(p) => Marshal.FreeHGlobal(p));\n"
).generate(sink, attributes::unused, *context);
}
@ -192,7 +193,8 @@ struct pack_event_info_and_call_visitor
auto str_accept_func = [&](std::string const& conversion)
{
return as_generator(
indent << "IntPtr info = Eina.StringConversion.ManagedStringToNativeUtf8Alloc(" << conversion << ");\n"
indent << "Contract.Requires(e != null, nameof(e));\n"
<< indent << "IntPtr info = Eina.StringConversion.ManagedStringToNativeUtf8Alloc(" << conversion << ");\n"
<< indent << "CallNativeEventCallback(" + library_name + ", \"_" + evt_c_name + "\", info, " << "(p) => Eina.MemoryNative.Free(p));\n"
).generate(sink, attributes::unused, *context);
};
@ -210,7 +212,8 @@ struct pack_event_info_and_call_visitor
auto value_accept_func = [&](std::string const& conversion)
{
return as_generator(
indent << "IntPtr info = Eina.PrimitiveConversion.ManagedToPointerAlloc(" << conversion << ");\n"
indent << "Contract.Requires(e != null, nameof(e));\n"
<< indent << "IntPtr info = Eina.PrimitiveConversion.ManagedToPointerAlloc(" << conversion << ");\n"
<< indent << "CallNativeEventCallback(" + library_name + ", \"_" + evt_c_name + "\", info, " << "(p) => Marshal.FreeHGlobal(p));\n"
).generate(sink, attributes::unused, *context);
};
@ -223,7 +226,9 @@ struct pack_event_info_and_call_visitor
bool operator()(grammar::attributes::klass_name const&) const
{
auto const& indent = current_indentation(*context);
return as_generator(indent << "IntPtr info = e.arg.NativeHandle;\n"
return as_generator(
indent << "Contract.Requires(e != null, nameof(e));\n"
<< indent << "IntPtr info = e.arg.NativeHandle;\n"
<< "CallNativeEventCallback(" << library_name << ", \"_" << evt_c_name << "\", IntPtr.Zero, null);\n"
).generate(sink, attributes::unused, *context);
}
@ -233,7 +238,9 @@ struct pack_event_info_and_call_visitor
if ((type.outer.base_type == "iterator") || (type.outer.base_type == "accessor"))
return true;
return as_generator(indent << "IntPtr info = e.arg.Handle;\n"
return as_generator(
indent << "Contract.Requires(e != null, nameof(e));\n"
<< indent << "IntPtr info = e.arg.Handle;\n"
<< "CallNativeEventCallback(" << library_name << ", \"_" << evt_c_name << "\", IntPtr.Zero, null);\n"
).generate(sink, attributes::unused, *context);
}

View File

@ -305,6 +305,7 @@ struct property_extension_method_definition_generator
if (!as_generator(scope_tab << "public static Efl.BindableProperty<" << type(true) << "> " << managed_name << "<T>(this Efl.BindablePart<T> part, Efl.Csharp.ExtensionTag<"
<< name_helpers::klass_full_concrete_or_interface_name(cls)
<< ", T>magic = null) where T : " << name_helpers::klass_full_concrete_or_interface_name(cls) << " {\n"
<< scope_tab << scope_tab << "Contract.Requires(part != null, nameof(part));\n"
<< scope_tab << scope_tab << "return new Efl.BindableProperty<" << type(true) << ">(part.PartName, \"" << property.name << "\", part.Binder);\n"
<< scope_tab << "}\n\n"
).generate(sink, std::make_tuple(prop_type, prop_type), context))

View File

@ -693,10 +693,10 @@ struct convert_in_variable_generator
}
else if (param.type.c_type == "Eina_Binbuf *" || param.type.c_type == "const Eina_Binbuf *")
{
auto var_name = in_variable_name(param.param_name);
if (!as_generator(
"var " << string << " = " << escape_keyword(param.param_name) << ".Handle;\n"
).generate(sink, var_name, context))
"Contract.Requires(" << string << " != null, nameof(" << string << "));\n"
<< scope_tab(2) << "var " << string << " = " << string << ".Handle;\n"
).generate(sink, std::make_tuple(escape_keyword(param.param_name), escape_keyword(param.param_name), in_variable_name(param.param_name), escape_keyword(param.param_name)), context))
return false;
if (param.type.has_own)
{
@ -710,10 +710,10 @@ struct convert_in_variable_generator
attributes::complex_type_def const* complex = efl::eina::get<attributes::complex_type_def>(&param.type.original_type);
if (!complex || complex->subtypes.size() != 2)
return false;
auto var_name = in_variable_name(param.param_name);
if (!as_generator(
"var " << string << " = " << escape_keyword(param.param_name) << ".Handle;\n"
).generate(sink, var_name, context))
"Contract.Requires(" << string << " != null, nameof(" << string << "));\n"
<< scope_tab(2) << "var " << string << " = " << string << ".Handle;\n"
).generate(sink, std::make_tuple(escape_keyword(param.param_name), escape_keyword(param.param_name), in_variable_name(param.param_name), escape_keyword(param.param_name)), context))
return false;
if (param.type.has_own && !as_generator(
escape_keyword(param.param_name) << ".SetOwn(false);\n"
@ -736,10 +736,12 @@ struct convert_in_variable_generator
attributes::complex_type_def const* complex = efl::eina::get<attributes::complex_type_def>(&param.type.original_type);
if (!complex)
return false;
auto var_name = in_variable_name(param.param_name);
if (!as_generator(
"var " << string << " = " << escape_keyword(param.param_name) << ".Handle;\n"
).generate(sink, var_name, context))
"Contract.Requires(" << string << " != null, nameof(" << string << "));\n"
<< "var " << string << " = " << string << ".Handle;\n"
).generate(sink, std::make_tuple(escape_keyword(param.param_name), escape_keyword(param.param_name),
in_variable_name(param.param_name), escape_keyword(param.param_name))
, context))
return false;
if (param.type.has_own && !as_generator(
escape_keyword(param.param_name) << ".Own = false;\n"
@ -777,8 +779,9 @@ struct convert_in_variable_generator
else if (param.type.c_type == "Eina_Value")
{
return as_generator(
"var " << string << " = " << string << ".GetNative();\n"
).generate(sink, std::make_tuple(in_variable_name(param.param_name), param.param_name), context);
"Contract.Requires(" << string << " != null, nameof(" << string << "));\n"
"var " << string << " = " << string << ".GetNative();\n"
).generate(sink, std::make_tuple(param.param_name, param.param_name, in_variable_name(param.param_name), param.param_name), context);
}
return true;
}

View File

@ -172,7 +172,8 @@ run(options_type const& opts)
"using System.Linq;\n"
"using System.Threading;\n"
"using System.ComponentModel;\n"
"using System.Diagnostics.CodeAnalysis;\n")
"using System.Diagnostics.CodeAnalysis;\n"
"using System.Diagnostics.Contracts;\n")
.generate(iterator, efl::eolian::grammar::attributes::unused, efl::eolian::grammar::context_null()))
{
throw std::runtime_error("Failed to generate file preamble");

View File

@ -17,6 +17,7 @@ using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Diagnostics.Contracts;
namespace EinaTestData
{
@ -95,6 +96,8 @@ public static class BaseData
[ CallerLineNumber ] int line = 0,
[ CallerFilePath ] string file = null,
[ CallerMemberName ] string member = null) {
Contract.Requires(a != null, nameof(a));
Contract.Requires(b != null, nameof(b));
Test.Assert(a.Length == b.Length, "Different lenght", line, file, member);
for (int i = 0; i < a.Length; ++i)
{