Skip to content

Commit ab1255e

Browse files
committed
Improve C pragma handling -Wincompatible-pointer-types
1 parent a66c050 commit ab1255e

File tree

1 file changed

+47
-27
lines changed

1 file changed

+47
-27
lines changed

src/pythonscript.c

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,31 @@ DLL_EXPORT void pythonscript_gdstringname_delete(GDExtensionStringNamePtr ptr) {
8181
gdstringname_destructor(ptr);
8282
}
8383

84+
#if __GNUC__ // GCC & Clang
85+
86+
#define PRAGMA_PUSH_ALLOW_INCOMPATIBLE_POINTER_TYPES \
87+
_Pragma("GCC diagnostic ignored \"-Wincompatible-pointer-types\"");
88+
89+
#define PRAGMA_POP \
90+
_Pragma("GCC diagnostic pop");
91+
92+
#else // MSVC
93+
94+
#define PRAGMA_PUSH_ALLOW_INCOMPATIBLE_POINTER_TYPES \
95+
_Pragma("warning( push )"); \
96+
_Pragma("warning( disable : 4047 )");
97+
98+
#define PRAGMA_POP \
99+
_Pragma("warning( pop )");
100+
101+
#endif
102+
84103
#define GD_PRINT_ERROR(msg) { \
85104
{ \
86-
_Pragma("GCC diagnostic ignored \"-Wincompatible-pointer-types\"") \
105+
PRAGMA_PUSH_ALLOW_INCOMPATIBLE_POINTER_TYPES; \
87106
void (*fn)(const char *, const char *, const char *, int32_t, GDExtensionBool); \
88107
fn = pythonscript_gdextension_get_proc_address("print_error"); \
89-
_Pragma("GCC diagnostic pop") \
108+
PRAGMA_POP; \
90109
if (fn) { \
91110
fn(msg, __func__, __FILE__, __LINE__, false); \
92111
} else { \
@@ -97,10 +116,10 @@ DLL_EXPORT void pythonscript_gdstringname_delete(GDExtensionStringNamePtr ptr) {
97116

98117
#define GD_PRINT_WARNING(msg) { \
99118
{ \
100-
_Pragma("GCC diagnostic ignored \"-Wincompatible-pointer-types\"") \
119+
PRAGMA_PUSH_ALLOW_INCOMPATIBLE_POINTER_TYPES; \
101120
void (*fn)(const char *, const char *, const char *, int32_t, GDExtensionBool); \
102121
fn = pythonscript_gdextension_get_proc_address("print_warning"); \
103-
_Pragma("GCC diagnostic pop") \
122+
PRAGMA_POP; \
104123
if (fn) { \
105124
fn(msg, __func__, __FILE__, __LINE__, false); \
106125
} else { \
@@ -129,10 +148,10 @@ static void _initialize_python() {
129148
pythonscript_gdstringname_new(&method_name_as_gdstringname, "get_base_dir");
130149
GDExtensionPtrBuiltInMethod gdstring_get_base_dir;
131150
{
132-
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
151+
PRAGMA_PUSH_ALLOW_INCOMPATIBLE_POINTER_TYPES;
133152
GDExtensionPtrBuiltInMethod (*fn)(GDExtensionVariantType, GDExtensionConstStringNamePtr, GDExtensionInt);
134153
fn = pythonscript_gdextension_get_proc_address("variant_get_ptr_builtin_method");
135-
#pragma GCC diagnostic pop
154+
PRAGMA_POP;
136155
gdstring_get_base_dir = fn(
137156
GDEXTENSION_VARIANT_TYPE_STRING,
138157
&method_name_as_gdstringname,
@@ -148,10 +167,10 @@ static void _initialize_python() {
148167
// 1) Retrieve library path
149168
char gd_library_path[GD_STRING_MAX_SIZE];
150169
{
151-
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
170+
PRAGMA_PUSH_ALLOW_INCOMPATIBLE_POINTER_TYPES;
152171
void (*fn)(GDExtensionClassLibraryPtr, GDExtensionUninitializedStringPtr);
153172
fn = pythonscript_gdextension_get_proc_address("get_library_path");
154-
#pragma GCC diagnostic pop
173+
PRAGMA_POP;
155174
fn(pythonscript_gdextension_library, gd_library_path);
156175
}
157176

@@ -164,10 +183,10 @@ static void _initialize_python() {
164183
// 3) Convert base dir into regular c string
165184
GDExtensionInt basedir_path_size;
166185
{
167-
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
186+
PRAGMA_PUSH_ALLOW_INCOMPATIBLE_POINTER_TYPES;
168187
GDExtensionInt (*fn)(GDExtensionConstStringPtr, char *, GDExtensionInt);
169188
fn = pythonscript_gdextension_get_proc_address("string_to_utf8_chars");
170-
#pragma GCC diagnostic pop
189+
PRAGMA_POP;
171190
basedir_path_size = fn(gd_basedir_path, NULL, 0);
172191
}
173192
// Why not using variable length array here ? Glad you asked Timmy !
@@ -177,21 +196,21 @@ static void _initialize_python() {
177196
// like we're about to do two lines down.
178197
char *basedir_path;
179198
{
180-
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
199+
PRAGMA_PUSH_ALLOW_INCOMPATIBLE_POINTER_TYPES;
181200
void *(*fn)(size_t);
182201
fn = pythonscript_gdextension_get_proc_address("mem_alloc");
183-
#pragma GCC diagnostic pop
202+
PRAGMA_POP;
184203
basedir_path = fn(basedir_path_size + 1);
185204
}
186205
if (basedir_path == NULL) {
187206
GD_PRINT_ERROR("Pythonscript: Initialization error (memory allocation failed)");
188207
goto error;
189208
}
190209
{
191-
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
210+
PRAGMA_PUSH_ALLOW_INCOMPATIBLE_POINTER_TYPES;
192211
GDExtensionInt (*fn)(GDExtensionConstStringPtr, char *, GDExtensionInt);
193212
fn = pythonscript_gdextension_get_proc_address("string_to_utf8_chars");
194-
#pragma GCC diagnostic pop
213+
PRAGMA_POP;
195214
fn(gd_basedir_path, basedir_path, basedir_path_size);
196215
}
197216
basedir_path[basedir_path_size] = '\0';
@@ -206,10 +225,10 @@ static void _initialize_python() {
206225
basedir_path
207226
);
208227
{
209-
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
228+
PRAGMA_PUSH_ALLOW_INCOMPATIBLE_POINTER_TYPES;
210229
void (*fn)(void*);
211230
fn = pythonscript_gdextension_get_proc_address("mem_free");
212-
#pragma GCC diagnostic pop
231+
PRAGMA_POP;
213232
fn(basedir_path);
214233
}
215234
if (PyStatus_Exception(status)) {
@@ -376,10 +395,10 @@ DLL_EXPORT GDExtensionBool pythonscript_init(
376395
// Load GDString/GDStringName contructor/destructor needed for pythonscript_gdstringname_new/delete helpers
377396

378397
{
379-
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
398+
PRAGMA_PUSH_ALLOW_INCOMPATIBLE_POINTER_TYPES;
380399
GDExtensionPtrConstructor (*fn)(GDExtensionVariantType, int32_t);
381400
fn = pythonscript_gdextension_get_proc_address("variant_get_ptr_constructor");
382-
#pragma GCC diagnostic pop
401+
PRAGMA_POP;
383402
gdstring_constructor = fn(GDEXTENSION_VARIANT_TYPE_STRING, 0);
384403
}
385404
if (gdstring_constructor == NULL) {
@@ -388,10 +407,10 @@ DLL_EXPORT GDExtensionBool pythonscript_init(
388407
}
389408

390409
{
391-
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
410+
PRAGMA_PUSH_ALLOW_INCOMPATIBLE_POINTER_TYPES;
392411
GDExtensionPtrDestructor (*fn)(GDExtensionVariantType);
393412
fn = pythonscript_gdextension_get_proc_address("variant_get_ptr_destructor");
394-
#pragma GCC diagnostic pop
413+
PRAGMA_POP;
395414
gdstring_destructor = fn(GDEXTENSION_VARIANT_TYPE_STRING);
396415
}
397416
if (gdstring_destructor == NULL) {
@@ -400,10 +419,10 @@ DLL_EXPORT GDExtensionBool pythonscript_init(
400419
}
401420

402421
{
403-
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
422+
PRAGMA_PUSH_ALLOW_INCOMPATIBLE_POINTER_TYPES;
404423
GDExtensionPtrConstructor (*fn)(GDExtensionVariantType, int32_t);
405424
fn = pythonscript_gdextension_get_proc_address("variant_get_ptr_constructor");
406-
#pragma GCC diagnostic pop
425+
PRAGMA_POP;
407426
gdstringname_from_gdstring_constructor = fn(GDEXTENSION_VARIANT_TYPE_STRING_NAME, 2);
408427
}
409428
if (gdstringname_from_gdstring_constructor == NULL) {
@@ -412,10 +431,10 @@ DLL_EXPORT GDExtensionBool pythonscript_init(
412431
}
413432

414433
{
415-
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
434+
PRAGMA_PUSH_ALLOW_INCOMPATIBLE_POINTER_TYPES;
416435
GDExtensionPtrDestructor (*fn)(GDExtensionVariantType);
417436
fn = pythonscript_gdextension_get_proc_address("variant_get_ptr_destructor");
418-
#pragma GCC diagnostic pop
437+
PRAGMA_POP;
419438
gdstringname_destructor = fn(GDEXTENSION_VARIANT_TYPE_STRING_NAME);
420439
}
421440
if (gdstringname_destructor == NULL) {
@@ -424,9 +443,10 @@ DLL_EXPORT GDExtensionBool pythonscript_init(
424443
}
425444

426445
{
427-
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
446+
PRAGMA_PUSH_ALLOW_INCOMPATIBLE_POINTER_TYPES;
428447
gdstring_new_with_utf8_chars = pythonscript_gdextension_get_proc_address("string_new_with_utf8_chars");
429448
#pragma GCC diagnostic pop
449+
PRAGMA_POP;
430450
}
431451
if (gdstring_new_with_utf8_chars == NULL) {
432452
GD_PRINT_ERROR("Pythonscript: Initialization error (cannot retrieve `string_new_with_utf8_chars` destructor)");
@@ -437,10 +457,10 @@ DLL_EXPORT GDExtensionBool pythonscript_init(
437457
// (i.e. the bindings has been generated against) and the version currently executed.
438458
GDExtensionGodotVersion godot_version;
439459
{
440-
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
460+
PRAGMA_PUSH_ALLOW_INCOMPATIBLE_POINTER_TYPES;
441461
void (*fn)(GDExtensionGodotVersion *);
442462
fn = pythonscript_gdextension_get_proc_address("get_godot_version");
443-
#pragma GCC diagnostic pop
463+
PRAGMA_POP;
444464
fn(&godot_version);
445465
}
446466
if (godot_version.major != GODOT_VERSION_MAJOR) {

0 commit comments

Comments
 (0)