diff --git a/CHANGES.current b/CHANGES.current index 028adcf74..b52bf0bf1 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,6 +7,12 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ Version 4.1.0 (in progress) =========================== +2022-01-25: olly + #2091 Support most cases of `sizeof` applied to an expression + in constant expressions. Previously there was only support for + `sizeof()` and expressions which syntactically look like a + type (such as `sizeof(foo)`). + 2022-01-25: olly #80 #635 https://sourceforge.net/p/swig/bugs/1139/ Add support for parsing common cases of `<` and `>` comparisons diff --git a/Examples/test-suite/constant_expr_c.i b/Examples/test-suite/constant_expr_c.i index b7431c1f1..aae0b953c 100644 --- a/Examples/test-suite/constant_expr_c.i +++ b/Examples/test-suite/constant_expr_c.i @@ -32,4 +32,25 @@ double d_array[12 % 9]; #define TEST_I 0 #endif +/* sizeof didn't work on an expression before SWIG 4.1.0 except for cases where + * the expression was in parentheses and looked syntactically like a type (so + * sizeof(X) worked because X could be a type syntactically). + */ +const int s1a = sizeof(X); /* worked before 4.1.0 */ +//const int s1b = sizeof X; /* not currently supported */ +const int s2a = sizeof("a string" ); +const int s2b = sizeof "a string"; +const int s3a = sizeof('c'); +const int s3b = sizeof('c'); +const int s4a = sizeof(L"a wstring"); +const int s4b = sizeof L"a wstring"; +const int s5a = sizeof(L'C'); +const int s5b = sizeof L'C'; +const int s6a = sizeof(sizeof(X)); +const int s6b = sizeof sizeof(X); +const int s7a = sizeof(3.14); +const int s7b = sizeof 3.14; +const int s8a = sizeof(2.1e-6); +const int s8b = sizeof 2.1e-6; + %} diff --git a/Source/CParse/parser.y b/Source/CParse/parser.y index 562c51269..458ef1a7b 100644 --- a/Source/CParse/parser.y +++ b/Source/CParse/parser.y @@ -6554,6 +6554,25 @@ exprsimple : exprnum { $$.val = NewStringf("sizeof...(%s)",SwigType_str($6,0)); $$.type = T_ULONG; } + /* We don't support all valid expressions here currently - e.g. + * sizeof( x) doesn't work - but those are unlikely to + * be seen in real code. + * + * Note: sizeof(x) is not handled here, but instead by the rule + * for sizeof() because it matches that syntactically. + */ + | SIZEOF LPAREN exprsimple RPAREN { + $$.val = NewStringf("sizeof(%s)", $3.val); + $$.type = T_ULONG; + } + /* `sizeof expr` without parentheses is valid for an expression, + * but not for a type. This doesn't support `sizeof x` in + * addition to the case not supported above. + */ + | SIZEOF exprsimple { + $$.val = NewStringf("sizeof(%s)", $2.val); + $$.type = T_ULONG; + } | wstring { $$.val = $1; $$.rawval = NewStringf("L\"%s\"", $$.val);