summaryrefslogtreecommitdiff
path: root/Source
diff options
context:
space:
mode:
authorOlly Betts <olly@survex.com>2023-04-21 07:38:52 +1200
committerOlly Betts <olly@survex.com>2023-04-21 07:40:25 +1200
commitde4e3b454e23af3f26f8c2ea670482bef738e983 (patch)
tree33fd3903a69263b03bd0a907c894044c8797087a /Source
parent3275331886192da4ce2047d52be91526f5608c4f (diff)
downloadswig-de4e3b454e23af3f26f8c2ea670482bef738e983.tar.gz
Fix #ifdef and #ifndef to work inside a %define
Previously they were silently ignored in this context (but #if defined already worked here if you need a workaround which works for older versions). Fixes #2183
Diffstat (limited to 'Source')
-rw-r--r--Source/Preprocessor/cpp.c36
1 files changed, 29 insertions, 7 deletions
diff --git a/Source/Preprocessor/cpp.c b/Source/Preprocessor/cpp.c
index 55058fc13..ae1e2ecb5 100644
--- a/Source/Preprocessor/cpp.c
+++ b/Source/Preprocessor/cpp.c
@@ -1191,14 +1191,36 @@ static DOH *Preprocessor_replace(DOH *s, DOH *line_file) {
} else if (Equal(kpp_hash_if, id) || Equal(kpp_hash_elif, id)) {
expand_defined_operator = 1;
Append(ns, id);
- /*
- } else if (Equal("%#if", id) || Equal("%#ifdef", id)) {
- Swig_warning(998, Getfile(s), Getline(s), "Found: %s preprocessor directive.\n", id);
- Append(ns, id);
} else if (Equal("#ifdef", id) || Equal("#ifndef", id)) {
- Swig_warning(998, Getfile(s), Getline(s), "The %s preprocessor directive does not work in macros, try #if instead.\n", id);
- Append(ns, id);
- */
+ /* Evaluate the defined-ness check and substitute `#if 0` or `#if 1`. */
+ int allow = 0;
+ skip_whitespace(s, 0);
+ c = Getc(s);
+ if (isidchar(c)) {
+ DOH *arg = NewStringEmpty();
+ Putc(c, arg);
+ while (((c = Getc(s)) != EOF)) {
+ if (!isidchar(c)) {
+ Ungetc(c, s);
+ break;
+ }
+ Putc(c, arg);
+ }
+ if (Equal("#ifdef", id)) {
+ if (Getattr(symbols, arg)) allow = 1;
+ } else {
+ if (!Getattr(symbols, arg)) allow = 1;
+ }
+ Delete(arg);
+ } else {
+ Ungetc(c, s);
+ Swig_error(Getfile(s), Getline(s), "Missing identifier for %s.\n", id);
+ }
+ if (allow) {
+ Append(ns, "#if 1");
+ } else {
+ Append(ns, "#if 0");
+ }
} else if ((m = Getattr(symbols, id))) {
/* See if the macro is defined in the preprocessor symbol table */
DOH *args = 0;