summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam S Fulton <wsf@fultondesigns.co.uk>2011-02-07 07:29:55 +0000
committerWilliam S Fulton <wsf@fultondesigns.co.uk>2011-02-07 07:29:55 +0000
commit63af0a2d7221743f5613540d108a56acdd0fc052 (patch)
treeedaa78b69aa6aa698e364fe9fe035a5a6a36bfed
parent65f377c422e33edfbedb4babd0fc0cab14b1fd03 (diff)
downloadswig-63af0a2d7221743f5613540d108a56acdd0fc052.tar.gz
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
-rw-r--r--CHANGES.current3
-rw-r--r--Examples/test-suite/common.mk1
-rw-r--r--Examples/test-suite/preproc_defined.i36
-rw-r--r--Examples/test-suite/python/preproc_defined_runme.py4
-rw-r--r--Source/Preprocessor/cpp.c14
5 files changed, 54 insertions, 4 deletions
diff --git a/CHANGES.current b/CHANGES.current
index 729a04217..59e3a38f6 100644
--- a/CHANGES.current
+++ b/CHANGES.current
@@ -5,6 +5,9 @@ See the RELEASENOTES file for a summary of changes in each release.
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
[MzScheme] SF #2942899 Add user supplied documentation to help getting started with MzScheme.
diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk
index 734b556f3..ad2a91c7c 100644
--- a/Examples/test-suite/common.mk
+++ b/Examples/test-suite/common.mk
@@ -493,6 +493,7 @@ C_TEST_CASES += \
overload_extendc \
preproc \
preproc_constants_c \
+ preproc_defined \
preproc_line_file \
ret_by_value \
simple_array \
diff --git a/Examples/test-suite/preproc_defined.i b/Examples/test-suite/preproc_defined.i
new file mode 100644
index 000000000..1495626b4
--- /dev/null
+++ b/Examples/test-suite/preproc_defined.i
@@ -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();
+}
+%}
diff --git a/Examples/test-suite/python/preproc_defined_runme.py b/Examples/test-suite/python/preproc_defined_runme.py
new file mode 100644
index 000000000..264a3a36a
--- /dev/null
+++ b/Examples/test-suite/python/preproc_defined_runme.py
@@ -0,0 +1,4 @@
+import preproc_defined
+
+if preproc_defined.call_checking() != 1:
+ raise RuntimeError
diff --git a/Source/Preprocessor/cpp.c b/Source/Preprocessor/cpp.c
index 0e8c3fb84..ad3ef864c 100644
--- a/Source/Preprocessor/cpp.c
+++ b/Source/Preprocessor/cpp.c
@@ -33,6 +33,7 @@ static Hash *included_files = 0;
static List *dependencies = 0;
static Scanner *id_scan = 0;
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_start_line = 0;
static const String * macro_start_file = 0;
@@ -1013,8 +1014,8 @@ static DOH *Preprocessor_replace(DOH *s) {
} else {
/* We found the end of a valid identifier */
Ungetc(c, s);
- /* See if this is the special "defined" macro */
- if (Equal(kpp_defined, id)) {
+ /* See if this is the special "defined" operator */
+ if (Equal(kpp_defined, id) && defined_operator_accepted) {
int lenargs = 0;
DOH *args = 0;
/* See whether or not a parenthesis has been used */
@@ -1042,7 +1043,7 @@ static DOH *Preprocessor_replace(DOH *s) {
}
lenargs = Len(args);
if ((!args) || (!lenargs)) {
- /* This is not a defined() macro. */
+ /* This is not a defined() operator. */
Append(ns, id);
state = 0;
break;
@@ -1554,7 +1555,9 @@ String *Preprocessor_parse(String *s) {
level++;
if (allow) {
int val;
- String *sval = Preprocessor_replace(value);
+ String *sval;
+ defined_operator_accepted = 1;
+ sval = Preprocessor_replace(value);
start_level = level;
Seek(sval, 0, SEEK_SET);
/* Printf(stdout,"Evaluating '%s'\n", sval); */
@@ -1570,6 +1573,7 @@ String *Preprocessor_parse(String *s) {
if (val == 0)
allow = 0;
}
+ defined_operator_accepted = 0;
mask = 1;
}
} else if (Equal(id, kpp_elif)) {
@@ -1577,6 +1581,7 @@ String *Preprocessor_parse(String *s) {
Swig_error(Getfile(s), Getline(id), "Misplaced #elif.\n");
} else {
cond_lines[level - 1] = Getline(id);
+ defined_operator_accepted = 1;
if (allow) {
allow = 0;
mask = 0;
@@ -1599,6 +1604,7 @@ String *Preprocessor_parse(String *s) {
allow = 0;
}
}
+ defined_operator_accepted = 0;
}
} else if (Equal(id, kpp_warning)) {
if (allow) {