Support most cases of sizeof applied to an expression

Previously there was only support for `sizeof(<type>)` and expressions
which syntactically look like a type (such as `sizeof(foo)`).

Fixes #2091.
This commit is contained in:
Olly Betts 2022-01-25 23:47:01 +13:00
commit 7ab24e7e86
3 changed files with 46 additions and 0 deletions

View file

@ -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(<type>)` 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

View file

@ -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;
%}

View file

@ -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(<unaryop> 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(<type>) 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);