diff --git a/CHANGES.current b/CHANGES.current index 1f0dd3807..b0488780c 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -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) =========================== +2022-07-25: olly + Support for C++14 binary integer literals in preprocessor expressions. + 2022-07-20: wsfulton [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++. diff --git a/Examples/test-suite/cpp14_binary_integer_literals.i b/Examples/test-suite/cpp14_binary_integer_literals.i index 5e736be64..54e070032 100644 --- a/Examples/test-suite/cpp14_binary_integer_literals.i +++ b/Examples/test-suite/cpp14_binary_integer_literals.i @@ -1,5 +1,9 @@ %module cpp14_binary_integer_literals +#if 0b100 < 4 +# error binary constant in preprocessor expression failed +#endif + %inline %{ int b1 = 0b1; int b2 = 0b10; diff --git a/Source/Preprocessor/expr.c b/Source/Preprocessor/expr.c index 52e467b6c..15bf99657 100644 --- a/Source/Preprocessor/expr.c +++ b/Source/Preprocessor/expr.c @@ -318,7 +318,12 @@ int Preprocessor_expr(DOH *s, int *error) { if ((token == SWIG_TOKEN_INT) || (token == SWIG_TOKEN_UINT) || (token == SWIG_TOKEN_LONG) || (token == SWIG_TOKEN_ULONG)) { /* A number. Reduce EXPR_TOP to an EXPR_VALUE */ char *c = Char(Scanner_text(scan)); - stack[sp].value = (long) strtol(c, 0, 0); + if (c[0] == '0' && (c[1] == 'b' || c[1] == 'B')) { + // strtol() doesn't handle binary constants. + stack[sp].value = (long) strtol(c + 2, 0, 2); + } else { + stack[sp].value = (long) strtol(c, 0, 0); + } stack[sp].svalue = 0; stack[sp].op = EXPR_VALUE; } else if ((token == SWIG_TOKEN_MINUS) || (token == SWIG_TOKEN_PLUS) || (token == SWIG_TOKEN_LNOT) || (token == SWIG_TOKEN_NOT)) {