Fix operator precedence in preprocessor expressions

This commit is contained in:
Olly Betts 2022-07-26 12:35:58 +12:00
parent 5a4baece4f
commit de5ce08a7d
4 changed files with 48 additions and 10 deletions

View File

@ -7,6 +7,9 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 4.1.0 (in progress) Version 4.1.0 (in progress)
=========================== ===========================
2022-07-26: olly
Fix incorrect operator precedence in preprocessor expressions.
2022-07-20: wsfulton 2022-07-20: wsfulton
[C#, Java] Ensure the order of interfaces generated in proxy interfaces for the [C#, Java] Ensure the order of interfaces generated in proxy interfaces for the
%interface family of macros is the same as that parsed from the bases in C++. %interface family of macros is the same as that parsed from the bases in C++.

View File

@ -746,6 +746,7 @@ C_TEST_CASES += \
preproc \ preproc \
preproc_constants_c \ preproc_constants_c \
preproc_defined \ preproc_defined \
preproc_expr \
preproc_gcc_output \ preproc_gcc_output \
preproc_include \ preproc_include \
preproc_line_file \ preproc_line_file \

View File

@ -0,0 +1,34 @@
%module preproc_expr
// Check expressions which suffered from incorrect operator precedence prior
// to SWIG 4.1.0.
%inline %{
// `!` should have higher precedence than binary `+`.
#if !0 + 1
#else
# error Bad preprocessor expression operator precedence
#endif
// `!=` should have higher precedence than bitwise and.
#if 1 & 2 != 0
#else
# error Bad preprocessor expression operator precedence
#endif
// `==` should have higher precedence than bitwise or.
#if (2 | 1 == 3) != 2
# error Bad preprocessor expression operator precedence
#endif
// `!=` should have higher precedence than bitwise xor.
#if 1 ^ 2 != 4
# error Bad preprocessor expression operator precedence
#endif
// `<` should have higher precedence than '=='.
#if 2 == 2 < 2
# error Bad preprocessor expression operator precedence
#endif
%}

View File

@ -54,6 +54,7 @@ static const char *errmsg = 0; /* Parsing error */
/* Initialize the precedence table for various operators. Low values have higher precedence */ /* Initialize the precedence table for various operators. Low values have higher precedence */
static void init_precedence(void) { static void init_precedence(void) {
prec[SWIG_TOKEN_NOT] = 10; prec[SWIG_TOKEN_NOT] = 10;
prec[SWIG_TOKEN_LNOT] = 10;
prec[OP_UMINUS] = 10; prec[OP_UMINUS] = 10;
prec[OP_UPLUS] = 10; prec[OP_UPLUS] = 10;
prec[SWIG_TOKEN_STAR] = 20; prec[SWIG_TOKEN_STAR] = 20;
@ -63,16 +64,15 @@ static void init_precedence(void) {
prec[SWIG_TOKEN_MINUS] = 30; prec[SWIG_TOKEN_MINUS] = 30;
prec[SWIG_TOKEN_LSHIFT] = 40; prec[SWIG_TOKEN_LSHIFT] = 40;
prec[SWIG_TOKEN_RSHIFT] = 40; prec[SWIG_TOKEN_RSHIFT] = 40;
prec[SWIG_TOKEN_AND] = 50; prec[SWIG_TOKEN_LESSTHAN] = 50;
prec[SWIG_TOKEN_XOR] = 60; prec[SWIG_TOKEN_GREATERTHAN] = 50;
prec[SWIG_TOKEN_OR] = 70; prec[SWIG_TOKEN_LTEQUAL] = 50;
prec[SWIG_TOKEN_EQUALTO] = 80; prec[SWIG_TOKEN_GTEQUAL] = 50;
prec[SWIG_TOKEN_NOTEQUAL] = 80; prec[SWIG_TOKEN_EQUALTO] = 60;
prec[SWIG_TOKEN_LESSTHAN] = 80; prec[SWIG_TOKEN_NOTEQUAL] = 60;
prec[SWIG_TOKEN_GREATERTHAN] = 80; prec[SWIG_TOKEN_AND] = 70;
prec[SWIG_TOKEN_LTEQUAL] = 80; prec[SWIG_TOKEN_XOR] = 80;
prec[SWIG_TOKEN_GTEQUAL] = 80; prec[SWIG_TOKEN_OR] = 90;
prec[SWIG_TOKEN_LNOT] = 90;
prec[SWIG_TOKEN_LAND] = 100; prec[SWIG_TOKEN_LAND] = 100;
prec[SWIG_TOKEN_LOR] = 110; prec[SWIG_TOKEN_LOR] = 110;
expr_init = 1; expr_init = 1;