1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
%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) {
int okay;
fn(11);
okay = (vvv == 1);
fn(9);
okay = okay && (vvv == 0);
return okay; /* should be 1 */
}
%}
%inline %{
int call_checking(void) {
return checking();
}
%}
/*****************************************************************************/
/* Check #if/#elif defined() macro expansions
Also checks #if/#elif defined() works correctly within macros... this is not
standard C, but is now relied on in the SWIG library. */
/*****************************************************************************/
#define AAA
#define BBB
#define CCC
#if defined(AAA)\
&& defined(BBB) \
&& defined(CCC)
%{
void thing(int i) {}
void stuff(int i) {}
struct Defined {
int defined;
};
void bumpf(int i) {}
%}
#else
#endif
%define ANOTHER_MACRO(TYPE)
#if defined(AAA) && defined(BBB) && defined(CCC)
void thing(TYPE) {}
#else
void thing_not(TYPE) {}
#endif
#if defined(AAA) &&\
defined(BBB) \\
&& defined(CCC)
void stuff(TYPE) {}
#else
void stuff_not(TYPE);
#endif
#if defined(0)
void defined_not(TYPE);
#elif defined(AAA) && defined( BBB ) && defined(CCC)
struct Defined {
int defined;
};
#else
void defined_not(TYPE);
#endif
#if !( defined(AAA) &&\
defined(BBB) \\
&& defined(CCC) )
void bumpf_not(TYPE);
#else
void bumpf(TYPE) {}
#endif
%enddef
ANOTHER_MACRO(int)
%{
void another_macro_checking(void) {
struct Defined d;
d.defined = 10;
(void)d;
thing(10);
stuff(10);
bumpf(10);
}
%}
/* Check that unknown preprocessor directives are ignored inside an inactive
* conditional (github issue #394).
*/
#ifdef APPLE_OPENGL
# import <OpenGLES/ES1/gl.h>
#endif
#ifdef AAA
# define B
#else
# wibble wibble
#endif
#if 0
# wobble wobble
#endif
/* Check that #error is ignored inside an inactive conditional. */
#ifdef THIS_IS_NOT_DEFINED
# error should not trigger 1
#endif
#ifdef AAA
# define B
#else
# error should not trigger 2
#endif
#if 0
# error should not trigger 3
#endif
/* Check that #warning is ignored inside an inactive conditional. */
#ifdef THIS_IS_NOT_DEFINED
# warning should not trigger 1
#endif
#ifdef AAA
# define B
#else
# warning should not trigger 2
#endif
#if 0
# warning should not trigger 3
#endif
/* Regression test for https://sourceforge.net/p/swig/bugs/1163/
* ONE(1)(2) should expand to `2` but SWIG was expanding it to `TWO(2)`
* which results in the generated C wrapper failing to compile.
*/
#define ONE(X) TWO
#define TWO(X) X
%constant int a = ONE(1)(2);
#define XXX TWO
%constant int b = XXX(42);
#undef ONE
#undef TWO
/*
* The behaviour of Self-Referential Macros is defined
* https://gcc.gnu.org/onlinedocs/gcc-4.8.5/cpp/Self-Referential-Macros.html
*/
%inline %{
const int y = 0;
%}
#define x (4 + y)
#define y (2 * x)
%constant int z = y;
#undef y
#undef x
/* Regression test for #ifdef and #ifndef not working inside a %define.
* https://github.com/swig/swig/issues/2183
*/
#undef THISMACROISNOTDEFINED /* Make sure! */
#define THISMACROISDEFINED
%define %test()
#ifdef THISMACROISNOTDEFINED
# error #ifdef inside percent-define failed
#endif
#ifndef THISMACROISDEFINED
# error #ifndef inside percent-define failed
#endif
/* Check pre-defined macro too. */
#ifndef SWIG
# error #ifndef inside percent-define failed
#endif
/* These cases already worked, but should still have test coverage. */
#if defined THISMACROISNOTDEFINED
# error #if defined inside percent-define failed
#endif
#if !defined THISMACROISDEFINED
# error #if !defined inside percent-define failed
#endif
/* Check pre-defined macro too. */
#if !defined SWIG
# error #if !defined inside percent-define failed
#endif
%enddef
%test()
|