diff options
author | William S Fulton <wsf@fultondesigns.co.uk> | 2019-03-30 12:18:40 +0000 |
---|---|---|
committer | William S Fulton <wsf@fultondesigns.co.uk> | 2019-04-08 19:20:45 +0100 |
commit | 06462acdf9b7738a19dce5579d90b1c91b3f9cfb (patch) | |
tree | 7b7d073067c66a7447af9704623256a64974ff24 /Source | |
parent | 33f8c36813f731d54f6220b089248311d3134279 (diff) | |
download | swig-06462acdf9b7738a19dce5579d90b1c91b3f9cfb.tar.gz |
Fix C# CA1063 warning by implementing the recommended Dispose methods.
Previously just the Dispose() method was generated.
Now the Dispose() and Dispose(bool disposing) methods are generated.
Changes are required if custom "csfinalize", "csdestruct" or "csdestruct_derived"
typemaps are being used. Details in #421 on Github. SWIG will error out if one of
the "csfinalize, "csdestruct" or "csdestruct_derived" typemaps are found. Example
error message:
foo.h:60: Error: A deprecated csfinalize typemap was found for Foo, please remove
it and replace all csdestruct, csdestruct_derived and csfinalize typemaps by the
csdispose, csdispose_derived, csdisposing and csdisposing_derived typemaps.
Closes #421
Diffstat (limited to 'Source')
-rw-r--r-- | Source/Modules/csharp.cxx | 74 |
1 files changed, 55 insertions, 19 deletions
diff --git a/Source/Modules/csharp.cxx b/Source/Modules/csharp.cxx index bd00ffaf3..ff73c3075 100644 --- a/Source/Modules/csharp.cxx +++ b/Source/Modules/csharp.cxx @@ -1873,38 +1873,56 @@ public: typemapLookup(n, "csbody", typemap_lookup_type, WARN_CSHARP_TYPEMAP_CSBODY_UNDEF), // main body of class NIL); - // C++ destructor is wrapped by the Dispose method - // Note that the method name is specified in a typemap attribute called methodname + // C++ destructor is wrapped by the Finalize and Dispose methods + + const char *tmap_method = derived ? "csdestruct_derived" : "csdestruct"; + const String *tm = typemapExists(n, tmap_method, typemap_lookup_type); + if (tm) { + Swig_error(Getfile(tm), Getline(tm), + "A deprecated %s typemap was found for %s, please remove it and replace all csdestruct, csdestruct_derived and csfinalize typemaps by the csdispose, csdispose_derived, csdisposing and csdisposing_derived typemaps.\n", + tmap_method, proxy_class_name); + } + tmap_method = "csfinalize"; + tm = typemapExists(n, tmap_method, typemap_lookup_type); + if (tm) { + Swig_error(Getfile(tm), Getline(tm), + "A deprecated %s typemap was found for %s, please remove it and replace all csdestruct, csdestruct_derived and csfinalize typemaps by the csdispose, csdispose_derived, csdisposing and csdisposing_derived typemaps.\n", + tmap_method, proxy_class_name); + } + + tmap_method = derived ? "csdisposing_derived" : "csdisposing"; String *destruct = NewString(""); - const String *tm = NULL; attributes = NewHash(); - String *destruct_methodname = NULL; - String *destruct_methodmodifiers = NULL; + const String *destruct_methodname = NULL; + const String *destruct_methodmodifiers = NULL; + const String *destruct_parameters = NULL; if (derived) { - tm = typemapLookup(n, "csdestruct_derived", typemap_lookup_type, WARN_NONE, attributes); - destruct_methodname = Getattr(attributes, "tmap:csdestruct_derived:methodname"); - destruct_methodmodifiers = Getattr(attributes, "tmap:csdestruct_derived:methodmodifiers"); + tm = typemapLookup(n, "csdisposing_derived", typemap_lookup_type, WARN_NONE, attributes); + destruct_methodname = Getattr(attributes, "tmap:csdisposing_derived:methodname"); + destruct_methodmodifiers = Getattr(attributes, "tmap:csdisposing_derived:methodmodifiers"); + destruct_parameters = Getattr(attributes, "tmap:csdisposing_derived:parameters"); } else { - tm = typemapLookup(n, "csdestruct", typemap_lookup_type, WARN_NONE, attributes); - destruct_methodname = Getattr(attributes, "tmap:csdestruct:methodname"); - destruct_methodmodifiers = Getattr(attributes, "tmap:csdestruct:methodmodifiers"); + tm = typemapLookup(n, "csdisposing", typemap_lookup_type, WARN_NONE, attributes); + destruct_methodname = Getattr(attributes, "tmap:csdisposing:methodname"); + destruct_methodmodifiers = Getattr(attributes, "tmap:csdisposing:methodmodifiers"); + destruct_parameters = Getattr(attributes, "tmap:csdisposing:parameters"); } if (tm && *Char(tm)) { if (!destruct_methodname) { - Swig_error(Getfile(n), Getline(n), "No methodname attribute defined in csdestruct%s typemap for %s\n", (derived ? "_derived" : ""), proxy_class_name); + Swig_error(Getfile(n), Getline(n), "No methodname attribute defined in %s typemap for %s\n", tmap_method, proxy_class_name); } if (!destruct_methodmodifiers) { Swig_error(Getfile(n), Getline(n), - "No methodmodifiers attribute defined in csdestruct%s typemap for %s.\n", (derived ? "_derived" : ""), proxy_class_name); + "No methodmodifiers attribute defined in %s typemap for %s.\n", tmap_method, proxy_class_name); } + if (!destruct_parameters) + destruct_parameters = empty_string; } // Emit the Finalize and Dispose methods if (tm) { - // Finalize method - if (*Char(destructor_call)) { - Printv(proxy_class_def, typemapLookup(n, "csfinalize", typemap_lookup_type, WARN_NONE), NIL); - } - // Dispose method + // Finalize and Dispose methods + Printv(proxy_class_def, typemapLookup(n, derived ? "csdispose_derived" : "csdispose", typemap_lookup_type, WARN_NONE), NIL); + // Dispose(bool disposing) method Printv(destruct, tm, NIL); if (*Char(destructor_call)) Replaceall(destruct, "$imcall", destructor_call); @@ -1917,7 +1935,7 @@ public: Printv(proxy_class_def, methodmods, NIL); else Printv(proxy_class_def, destruct_methodmodifiers, " ", derived ? "override" : "virtual", NIL); - Printv(proxy_class_def, " void ", destruct_methodname, "() ", destruct, "\n", NIL); + Printv(proxy_class_def, " void ", destruct_methodname, "(", destruct_parameters, ") ", destruct, "\n", NIL); } } if (*Char(interface_upcasts)) @@ -3553,6 +3571,24 @@ public: } /* ----------------------------------------------------------------------------- + * typemapExists() + * n - for input only and must contain info for Getfile(n) and Getline(n) to work + * tmap_method - typemap method name + * type - typemap type to lookup + * returns found typemap or NULL if not found + * ----------------------------------------------------------------------------- */ + + const String *typemapExists(Node *n, const_String_or_char_ptr tmap_method, SwigType *type) { + Node *node = NewHash(); + Setattr(node, "type", type); + Setfile(node, Getfile(n)); + Setline(node, Getline(n)); + const String *tm = Swig_typemap_lookup(tmap_method, node, "", 0); + Delete(node); + return tm; + } + + /* ----------------------------------------------------------------------------- * canThrow() * Determine whether the code in the typemap can throw a C# exception. * If so, note it for later when excodeSubstitute() is called. |