Add support for C99 _Bool

SWIG now treats _Bool as an alias for the bool keyword when in C mode.
This commit is contained in:
Olly Betts 2024-09-25 06:23:48 +12:00
parent 1c5b689a1a
commit 6567c5d186
7 changed files with 58 additions and 0 deletions

View File

@ -7,6 +7,10 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 4.3.0 (in progress) Version 4.3.0 (in progress)
=========================== ===========================
2024-09-25: olly
Add support for C99 _Bool. SWIG now treats _Bool as an alias for
the bool keyword when in C mode.
2024-09-24: olly 2024-09-24: olly
#2884 SWIG no longer accepts the invalid definition of a function #2884 SWIG no longer accepts the invalid definition of a function
as the final part of a declaration, e.g. as the final part of a declaration, e.g.

View File

@ -0,0 +1,23 @@
%module cbooltest
%{
#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
/* C99 _Bool should be supported by the compiler. */
# include <stdbool.h>
#else
/* C99 _Bool and stdbool.h may not be supported, so define a hacky version so
* we can still test SWIG's support. */
# define _Bool unsigned char
# define bool _Bool
# define true 1
# define false 1
#endif
%}
%inline %{
_Bool do_and(_Bool a, _Bool b) { return a && b; }
_Bool do_or(_Bool a, _Bool b) { return a || b; }
%}
%constant _Bool TRUE_CONSTANT = true;
%constant _Bool FALSE_CONSTANT = false;

View File

@ -0,0 +1,5 @@
%module xxx
struct A {
// C99 _Bool should not be a keyword in C++ so this should not give an error.
bool _Bool;
};

View File

@ -0,0 +1,8 @@
<?php
require "tests.php";
check::equal(cbooltest::do_or(true, false), true, "do_or() test failed");
check::equal(cbooltest::do_and(true, false), false, "do_and() test failed");
check::equal(cbooltest::TRUE_CONSTANT, true, "TRUE_CONSTANT test failed");
check::equal(cbooltest::FALSE_CONSTANT, false, "FALSE_CONSTANT test failed");

View File

@ -0,0 +1,13 @@
import cbooltest
if not cbooltest.do_or(True, False):
raise RuntimeError("bad _Bool support")
if cbooltest.do_and(True, False):
raise RuntimeError("bad _Bool support")
if not cbooltest.TRUE_CONSTANT:
raise RuntimeError("bad _Bool support")
if cbooltest.FALSE_CONSTANT:
raise RuntimeError("bad _Bool support")

View File

@ -966,6 +966,11 @@ num_common: {
if (strcmp(yytext, "class") == 0) { if (strcmp(yytext, "class") == 0) {
Swig_warning(WARN_PARSE_CLASS_KEYWORD, cparse_file, cparse_line, "class keyword used, but not in C++ mode.\n"); Swig_warning(WARN_PARSE_CLASS_KEYWORD, cparse_file, cparse_line, "class keyword used, but not in C++ mode.\n");
} }
if (strcmp(yytext, "_Bool") == 0) {
/* C99 boolean type. */
yylval.type = NewSwigType(T_BOOL);
return (TYPE_BOOL);
}
if (strcmp(yytext, "_Complex") == 0) { if (strcmp(yytext, "_Complex") == 0) {
yylval.type = NewSwigType(T_COMPLEX); yylval.type = NewSwigType(T_COMPLEX);
return (TYPE_COMPLEX); return (TYPE_COMPLEX);