Fix #1940536, overactive preprocessor which was expanding defined(...) outside of #if and #elif preprocessor directives.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12441 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2011-02-07 07:29:55 +00:00
commit 63af0a2d72
5 changed files with 54 additions and 4 deletions

View file

@ -5,6 +5,9 @@ See the RELEASENOTES file for a summary of changes in each release.
Version 2.0.2 (in progress) Version 2.0.2 (in progress)
=========================== ===========================
2011-02-07: wsfulton
Fix #1940536, overactive preprocessor which was expanding defined(...) outside of #if and #elif
preprocessor directives.
2011-02-05: wsfulton 2011-02-05: wsfulton
[MzScheme] SF #2942899 Add user supplied documentation to help getting started with MzScheme. [MzScheme] SF #2942899 Add user supplied documentation to help getting started with MzScheme.

View file

@ -493,6 +493,7 @@ C_TEST_CASES += \
overload_extendc \ overload_extendc \
preproc \ preproc \
preproc_constants_c \ preproc_constants_c \
preproc_defined \
preproc_line_file \ preproc_line_file \
ret_by_value \ ret_by_value \
simple_array \ simple_array \

View file

@ -0,0 +1,36 @@
%module preproc_defined
// Check 'defined' passes through the preprocessor without being processed like '#if defined(ABC)' would be (SF bug #1940536)
%define DEFINED_MACRO
%{
int defined(int b) {
return b > 10;
}
int vvv = -1;
void fn(int val) {
if (defined(val))
vvv = 1;
else
vvv = 0;
}
%}
%enddef
DEFINED_MACRO
%{
int checking(void) {
fn(11);
int okay = (vvv == 1);
fn(9);
okay = okay && (vvv == 0);
return okay; /* should be 1 */
}
%}
%inline %{
int call_checking(void) {
return checking();
}
%}

View file

@ -0,0 +1,4 @@
import preproc_defined
if preproc_defined.call_checking() != 1:
raise RuntimeError

View file

@ -33,6 +33,7 @@ static Hash *included_files = 0;
static List *dependencies = 0; static List *dependencies = 0;
static Scanner *id_scan = 0; static Scanner *id_scan = 0;
static int error_as_warning = 0; /* Understand the cpp #error directive as a special #warning */ static int error_as_warning = 0; /* Understand the cpp #error directive as a special #warning */
static int defined_operator_accepted = 0;
static int macro_level = 0; static int macro_level = 0;
static int macro_start_line = 0; static int macro_start_line = 0;
static const String * macro_start_file = 0; static const String * macro_start_file = 0;
@ -1013,8 +1014,8 @@ static DOH *Preprocessor_replace(DOH *s) {
} else { } else {
/* We found the end of a valid identifier */ /* We found the end of a valid identifier */
Ungetc(c, s); Ungetc(c, s);
/* See if this is the special "defined" macro */ /* See if this is the special "defined" operator */
if (Equal(kpp_defined, id)) { if (Equal(kpp_defined, id) && defined_operator_accepted) {
int lenargs = 0; int lenargs = 0;
DOH *args = 0; DOH *args = 0;
/* See whether or not a parenthesis has been used */ /* See whether or not a parenthesis has been used */
@ -1042,7 +1043,7 @@ static DOH *Preprocessor_replace(DOH *s) {
} }
lenargs = Len(args); lenargs = Len(args);
if ((!args) || (!lenargs)) { if ((!args) || (!lenargs)) {
/* This is not a defined() macro. */ /* This is not a defined() operator. */
Append(ns, id); Append(ns, id);
state = 0; state = 0;
break; break;
@ -1554,7 +1555,9 @@ String *Preprocessor_parse(String *s) {
level++; level++;
if (allow) { if (allow) {
int val; int val;
String *sval = Preprocessor_replace(value); String *sval;
defined_operator_accepted = 1;
sval = Preprocessor_replace(value);
start_level = level; start_level = level;
Seek(sval, 0, SEEK_SET); Seek(sval, 0, SEEK_SET);
/* Printf(stdout,"Evaluating '%s'\n", sval); */ /* Printf(stdout,"Evaluating '%s'\n", sval); */
@ -1570,6 +1573,7 @@ String *Preprocessor_parse(String *s) {
if (val == 0) if (val == 0)
allow = 0; allow = 0;
} }
defined_operator_accepted = 0;
mask = 1; mask = 1;
} }
} else if (Equal(id, kpp_elif)) { } else if (Equal(id, kpp_elif)) {
@ -1577,6 +1581,7 @@ String *Preprocessor_parse(String *s) {
Swig_error(Getfile(s), Getline(id), "Misplaced #elif.\n"); Swig_error(Getfile(s), Getline(id), "Misplaced #elif.\n");
} else { } else {
cond_lines[level - 1] = Getline(id); cond_lines[level - 1] = Getline(id);
defined_operator_accepted = 1;
if (allow) { if (allow) {
allow = 0; allow = 0;
mask = 0; mask = 0;
@ -1599,6 +1604,7 @@ String *Preprocessor_parse(String *s) {
allow = 0; allow = 0;
} }
} }
defined_operator_accepted = 0;
} }
} else if (Equal(id, kpp_warning)) { } else if (Equal(id, kpp_warning)) {
if (allow) { if (allow) {