Add csbegin, dbegin, javabegin for %module

[C#] Support nullable reference types. A generic C# option to the
%module directive allows one to add in any code at the beginning of every
C# file. This can add the #nullable enable preprocessor directive at the beginning
of every C# file in order to enable nullable reference types as follows:

  %module(csbegin="#nullable enable\n") mymodule

Closes #2681

[D, Java] Add the dbegin option to the %module directive for generating code at
the beginning of every D file. Similarly javabegin for Java. This enables one
to add a common comment at the start of each D/Java file.
This commit is contained in:
William S Fulton 2023-11-06 19:12:35 +00:00
parent ec6640d6d2
commit 3cfaae48ef
10 changed files with 160 additions and 6 deletions

View File

@ -7,6 +7,19 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 4.2.0 (in progress)
===========================
2023-11-06: wsfulton
[D, Java] Add the dbegin option to the %module directive for generating code at
the beginning of every D file. Similarly javabegin for Java. This enables one
to add a common comment at the start of each D/Java file.
2023-11-06: wsfulton
[C#] #2681 Support nullable reference types. A generic C# option to the
%module directive allows one to add in any code at the beginning of every
C# file. This can add the #nullable enable preprocessor directive at the beginning
of every C# file in order to enable nullable reference types as follows:
%module(csbegin="#nullable enable\n") mymodule
2023-10-21: wsfulton
[Python] #1783 Don't swallow all exceptions into a NotImplemented return
when wrapping operators which are marked with %pythonmaybecall. Corrects the

View File

@ -525,7 +525,7 @@ The module directive attribute <tt>imclassname</tt> is used to achieve this:
<div class="code">
<pre>
%module (imclassname="name") modulename
%module(imclassname="name") modulename
</pre>
</div>
@ -535,6 +535,38 @@ from <tt>modulename</tt> to <tt>modulenameModule</tt>.
</p>
</li>
<li>
<a name="CSharp_begin"></a>
<p>
The <tt>%module</tt> directive supports the <tt>csbegin</tt> option for adding code to the start of every generated C# file.
This is useful for adding common comments, using statements and/or preprocessor statements into all generated .cs files. For example,
C# 8 nullable reference types can be enabled via a C# preprocessor directive by adding <tt>#nullable enable</tt> into C# files as follows:
</p>
<div class="code">
<pre>
%module(csbegin="#nullable enable\n") mymodule
</pre>
</div>
<p>
It might be easier to use a macro for multiple lines of code, for example:
</p>
<div class="code">
<pre>
%define CSBEGIN_CODE
"
/* Copyright statement */
using System.Text;
#nullable enable
"
%enddef
%module(csbegin=CSBEGIN_CODE) mymodule
</pre>
</div>
<li>
There is no additional 'premature garbage collection prevention parameter' as the marshalling of the <tt>HandleRef</tt> object
takes care of ensuring a reference to the proxy class is held until the unmanaged call completed.
@ -800,9 +832,6 @@ Conditionally applying the typemaps using a macro is easily done. For example de
</pre>
</div>
<p>
</p>
<H3><a name="CSharp_other_type_mappings">23.3.2 Other types</a></H3>

View File

@ -856,7 +856,11 @@
<li><a href="D.html#D_code_injection_typemaps">Code injection typemaps</a>
<li><a href="D.html#D_special_variables">Special variable macros</a>
</ul>
<li><a href="D.html#D_other_code_control">Other D code control features</a>
<ul>
<li><a href="D.html#D_module">D begin</a>
<li><a href="D.html#D_features">D and %feature</a>
</ul>
<li><a href="D.html#D_pragmas">Pragmas</a>
<li><a href="D.html#D_exceptions">D Exceptions</a>
<li><a href="D.html#D_directors">D Directors</a>
@ -1056,6 +1060,7 @@
<li><a href="Java.html#Java_proxycode">Class extension with %proxycode</a>
<li><a href="Java.html#Java_exception_handling">Exception handling with %exception and %javaexception</a>
<li><a href="Java.html#Java_method_access">Method access with %javamethodmodifiers</a>
<li><a href="Java.html#Java_begin">Java begin</a>
</ul>
<li><a href="Java.html#Java_tips_techniques">Tips and techniques</a>
<ul>

View File

@ -22,7 +22,11 @@
<li><a href="#D_code_injection_typemaps">Code injection typemaps</a>
<li><a href="#D_special_variables">Special variable macros</a>
</ul>
<li><a href="#D_other_code_control">Other D code control features</a>
<ul>
<li><a href="#D_module">D begin</a>
<li><a href="#D_features">D and %feature</a>
</ul>
<li><a href="#D_pragmas">Pragmas</a>
<li><a href="#D_exceptions">D Exceptions</a>
<li><a href="#D_directors">D Directors</a>
@ -305,7 +309,29 @@ $importtype(AnotherInterface)
</dl>
<H2><a name="D_features">24.4 D and %feature</a></H2>
<H2><a name="D_other_code_control">24.4 Other D code control features</a></H2>
<H3><a name="D_module">24.4.1 D begin</a></H3>
<p>
It is possible to add a common comment at the start of every generated D file.
The <tt>%module</tt> directive supports the <tt>dbegin</tt> option for this.
The provided text is generated at the very beginning of each generated D file.
As it is generated before the D module statement, is only really useful for adding in
a common comment into all generated D files. For example, copyright text for each file:
</p>
<div class="code">
<pre>
%module(dbegin="/* Common comment. Copyright (C) 2000 Mr Nobody. */\n") nobodymodule
</pre>
</div>
<H3><a name="D_features">24.4.2 D and %feature</a></H3>
<p>The D module defines a number of directives which modify the <a href="Customization.html#Customization_features">SWIG features</a> set globally or for a specific declaration:</p>

View File

@ -106,6 +106,7 @@
<li><a href="#Java_proxycode">Class extension with %proxycode</a>
<li><a href="#Java_exception_handling">Exception handling with %exception and %javaexception</a>
<li><a href="#Java_method_access">Method access with %javamethodmodifiers</a>
<li><a href="#Java_begin">Java begin</a>
</ul>
<li><a href="#Java_tips_techniques">Tips and techniques</a>
<ul>
@ -2226,7 +2227,7 @@ The module directive attribute <tt>jniclassname</tt> is used to achieve this:
<div class="code">
<pre>
%module (jniclassname="name") modulename
%module(jniclassname="name") modulename
</pre>
</div>
@ -5030,6 +5031,25 @@ protected static void protect_me() {
</pre>
</div>
<H3><a name="Java_begin">27.7.6 Java begin</a></H3>
<p>
It is possible to add a common comment at the start of every generated Java file.
The <tt>%module</tt> directive supports the <tt>javabegin</tt> option for this.
The provided text is generated at the very beginning of each generated .java file.
As it is generated before the package statement, is only really useful for adding in
a common comment into all generated .java files. For example, copyright text for each file:
</p>
<div class="code">
<pre>
%module(javabegin="/* Common comment. Copyright (C) 2000 Mr Nobody. */\n") nobodymodule
</pre>
</div>
<H2><a name="Java_tips_techniques">27.8 Tips and techniques</a></H2>

View File

@ -0,0 +1,39 @@
// Tests the csbegin, dbegin and javabegin for %module
%define CSBEGIN_CODE
"
/* Copyright statement */
using System.Text;
// #nullable enable // commented out: only works with very modern versions of C#
"
%enddef
%module(csbegin=CSBEGIN_CODE, dbegin="/* D common comment */", javabegin="/* Java common comment */\n") begin_code
%inline %{
struct ABC {
void abc_method() {}
};
%}
#if defined(SWIGCSHARP)
%extend ABC {
%proxycode %{
public StringBuilder TestBeginProxy(string input) {
return new StringBuilder(input);
}
%}
}
%pragma(csharp) imclasscode=%{
public StringBuilder TestBeginIM(string input) {
return new StringBuilder(input);
}
%}
%pragma(csharp) modulecode=%{
public StringBuilder TestBeginModule(string input) {
return new StringBuilder(input);
}
%}
#endif

View File

@ -131,6 +131,7 @@ CPP_TEST_CASES += \
assign_const \
assign_reference \
autodoc \
begin_code \
bloody_hell \
bools \
catches \

View File

@ -59,6 +59,7 @@ class CSHARP:public Language {
String *variable_name; //Name of a variable being wrapped
String *proxy_class_constants_code;
String *module_class_constants_code;
String *common_begin_code;
String *enum_code;
String *dllimport; // DllImport attribute name
String *namespce; // Optional namespace name
@ -134,6 +135,7 @@ public:
variable_name(NULL),
proxy_class_constants_code(NULL),
module_class_constants_code(NULL),
common_begin_code(NULL),
enum_code(NULL),
dllimport(NULL),
namespce(NULL),
@ -308,6 +310,9 @@ public:
allow_dirprot();
}
allow_allprotected(GetFlag(optionsnode, "allprotected"));
common_begin_code = Getattr(optionsnode, "csbegin");
if (common_begin_code)
Printf(common_begin_code, "\n");
}
/* Initialize all of the output files */
@ -649,6 +654,7 @@ public:
Printf(f, "//\n");
Swig_banner_target_lang(f, "//");
Printf(f, "//------------------------------------------------------------------------------\n\n");
Printv(f, common_begin_code, NIL);
}
/* -----------------------------------------------------------------------------

View File

@ -175,6 +175,9 @@ class D : public Language {
// The full code for the current proxy class, including the epilogue.
String* proxy_class_code;
// Code generated at the begin of every D file
String *common_begin_code;
// Contains a D call to the function wrapping C++ the destructor of the
// current class (if there is a public C++ destructor).
String *destructor_call;
@ -261,6 +264,7 @@ public:
proxy_class_body_code(NULL),
proxy_class_epilogue_code(NULL),
proxy_class_code(NULL),
common_begin_code(NULL),
destructor_call(NULL),
director_dcallbacks_code(NULL),
wrapper_loader_code(NULL),
@ -366,6 +370,10 @@ public:
}
allow_allprotected(GetFlag(optionsnode, "allprotected"));
common_begin_code = Getattr(optionsnode, "dbegin");
if (common_begin_code)
Printf(common_begin_code, "\n");
}
/* Initialize all of the output files */
@ -4696,6 +4704,7 @@ private:
Printf(f, "/* ----------------------------------------------------------------------------\n");
Swig_banner_target_lang(f, " *");
Printf(f, " * ----------------------------------------------------------------------------- */\n\n");
Printv(f, common_begin_code, NIL);
}
/* ---------------------------------------------------------------------------

View File

@ -64,6 +64,7 @@ class JAVA:public Language {
String *variable_name; //Name of a variable being wrapped
String *proxy_class_constants_code;
String *module_class_constants_code;
String *common_begin_code;
String *enum_code;
String *package; // Optional package name
String *jnipackage; // Package name used in the JNI code
@ -139,6 +140,7 @@ public:
variable_name(NULL),
proxy_class_constants_code(NULL),
module_class_constants_code(NULL),
common_begin_code(NULL),
enum_code(NULL),
package(NULL),
jnipackage(NULL),
@ -350,6 +352,9 @@ public:
allow_dirprot();
}
allow_allprotected(GetFlag(optionsnode, "allprotected"));
common_begin_code = Getattr(optionsnode, "javabegin");
if (common_begin_code)
Printf(common_begin_code, "\n");
}
/* Initialize all of the output files */
@ -773,6 +778,7 @@ public:
Printf(f, "/* ----------------------------------------------------------------------------\n");
Swig_banner_target_lang(f, " *");
Printf(f, " * ----------------------------------------------------------------------------- */\n\n");
Printv(f, common_begin_code, NIL);
}
/*-----------------------------------------------------------------------