Don't accept some invalid preprocessor code

Whitespace or non-numeric characters are required after a preprocessor
directive that requires an expression.
This commit is contained in:
William S Fulton 2018-01-15 19:57:10 +00:00
commit 72964a1faf
4 changed files with 48 additions and 1 deletions

View file

@ -7,6 +7,31 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 4.0.0 (in progress)
===========================
2018-01-16: wsfulton
Expressions following a preprocessor directive must now be separated by whitespace
or non-numeric characters. This syntax change makes the SWIG preprocessor work like
the C preprocessor in this area.
For example, the following code used be accepted as valid syntax:
#if1
#define ABC 123
#endif
Now you get an error:
example.h:1: Error: Unknown SWIG preprocessor directive: if1 (if this is a block of
target language code, delimit it with %{ and %})
example.h:3: Error: Extraneous #endif.
The following is the correct syntax:
#if 1
#define ABC 123
#endif
The following of course also works:
#if(1)
#define ABC 123
#endif
2018-01-15: wsfulton
Fix issue #1183. Floating point exception evaluating preprocessor expressions
resulting in division by zero.

View file

@ -24,3 +24,20 @@
#if 0
#elif 8.8
#endif
/* Missing whitespace after preproc directive */
#if123
#endif
#if456e
#endif
#if 0
#warning This should not warn
#elif1
#warning This should also not warn
#endif
#if(1)
#warning Warning okay: #if(1)
#endif

View file

@ -12,3 +12,8 @@ pp_expressions_bad.i:21: Warning 202: Could not evaluate expression '2e3'
pp_expressions_bad.i:21: Warning 202: Error: 'Floating point constant in preprocessor expression'
pp_expressions_bad.i:25: Warning 202: Could not evaluate expression '8.8'
pp_expressions_bad.i:25: Warning 202: Error: 'Floating point constant in preprocessor expression'
pp_expressions_bad.i:29: Error: Unknown SWIG preprocessor directive: if123 (if this is a block of target language code, delimit it with %{ and %})
pp_expressions_bad.i:30: Error: Extraneous #endif.
pp_expressions_bad.i:32: Error: Unknown SWIG preprocessor directive: if456e (if this is a block of target language code, delimit it with %{ and %})
pp_expressions_bad.i:33: Error: Extraneous #endif.
pp_expressions_bad.i:42: Warning 204: CPP #warning, "Warning okay: #if(1)".

View file

@ -1454,7 +1454,7 @@ String *Preprocessor_parse(String *s) {
break;
case 41: /* Build up the name of the preprocessor directive */
if ((isspace(c) || (!isalpha(c)))) {
if ((isspace(c) || (!isidchar(c)))) {
Clear(value);
Clear(comment);
if (c == '\n') {