@@ -79,13 +79,29 @@ struct _yy_buffer
79
79
80
80
static char *old;
81
81
82
+ /*
83
+ * Vars for handling ifdef/elif/endif constructs. preproc_tos is the current
84
+ * nesting depth of such constructs, and stacked_if_value[preproc_tos] is the
85
+ * state for the innermost level. (For convenience, stacked_if_value[0] is
86
+ * initialized as though we are in the active branch of some outermost IF.)
87
+ * The active field is true if the current branch is active (being expanded).
88
+ * The saw_active field is true if we have found any successful branch,
89
+ * so that all subsequent branches of this level should be skipped.
90
+ * The else_branch field is true if we've found an 'else' (so that another
91
+ * 'else' or 'elif' at this level is an error.)
92
+ * For IFs nested within an inactive branch, all branches always have active
93
+ * set to false, but saw_active and else_branch are maintained normally.
94
+ * ifcond is valid only while evaluating an if-condition; it's true if we
95
+ * are doing ifdef, false if ifndef.
96
+ */
82
97
#define MAX_NESTED_IF 128
83
98
static short preproc_tos;
84
- static short ifcond;
99
+ static bool ifcond;
85
100
static struct _if_value
86
101
{
87
- short condition;
88
- short else_branch;
102
+ bool active;
103
+ bool saw_active;
104
+ bool else_branch;
89
105
} stacked_if_value[MAX_NESTED_IF];
90
106
91
107
%}
@@ -1165,11 +1181,26 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
1165
1181
return S_ANYTHING;
1166
1182
}
1167
1183
}
1168
- <C ,xskip >{exec_sql }{ifdef }{space }* { ifcond = true ; BEGIN (xcond); }
1184
+ <C ,xskip >{exec_sql }{ifdef }{space }* {
1185
+ if (preproc_tos >= MAX_NESTED_IF-1 )
1186
+ mmfatal (PARSE_ERROR, " too many nested EXEC SQL IFDEF conditions" );
1187
+ preproc_tos++;
1188
+ stacked_if_value[preproc_tos].active = false ;
1189
+ stacked_if_value[preproc_tos].saw_active = false ;
1190
+ stacked_if_value[preproc_tos].else_branch = false ;
1191
+ ifcond = true ;
1192
+ BEGIN (xcond);
1193
+ }
1169
1194
<C ,xskip >{informix_special }{ifdef }{space }* {
1170
1195
/* are we simulating Informix? */
1171
1196
if (INFORMIX_MODE)
1172
1197
{
1198
+ if (preproc_tos >= MAX_NESTED_IF-1 )
1199
+ mmfatal (PARSE_ERROR, " too many nested EXEC SQL IFDEF conditions" );
1200
+ preproc_tos++;
1201
+ stacked_if_value[preproc_tos].active = false ;
1202
+ stacked_if_value[preproc_tos].saw_active = false ;
1203
+ stacked_if_value[preproc_tos].else_branch = false ;
1173
1204
ifcond = true ;
1174
1205
BEGIN (xcond);
1175
1206
}
@@ -1179,11 +1210,26 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
1179
1210
return S_ANYTHING;
1180
1211
}
1181
1212
}
1182
- <C ,xskip >{exec_sql }{ifndef }{space }* { ifcond = false ; BEGIN (xcond); }
1213
+ <C ,xskip >{exec_sql }{ifndef }{space }* {
1214
+ if (preproc_tos >= MAX_NESTED_IF-1 )
1215
+ mmfatal (PARSE_ERROR, " too many nested EXEC SQL IFDEF conditions" );
1216
+ preproc_tos++;
1217
+ stacked_if_value[preproc_tos].active = false ;
1218
+ stacked_if_value[preproc_tos].saw_active = false ;
1219
+ stacked_if_value[preproc_tos].else_branch = false ;
1220
+ ifcond = false ;
1221
+ BEGIN (xcond);
1222
+ }
1183
1223
<C ,xskip >{informix_special }{ifndef }{space }* {
1184
1224
/* are we simulating Informix? */
1185
1225
if (INFORMIX_MODE)
1186
1226
{
1227
+ if (preproc_tos >= MAX_NESTED_IF-1 )
1228
+ mmfatal (PARSE_ERROR, " too many nested EXEC SQL IFDEF conditions" );
1229
+ preproc_tos++;
1230
+ stacked_if_value[preproc_tos].active = false ;
1231
+ stacked_if_value[preproc_tos].saw_active = false ;
1232
+ stacked_if_value[preproc_tos].else_branch = false ;
1187
1233
ifcond = false ;
1188
1234
BEGIN (xcond);
1189
1235
}
@@ -1193,28 +1239,22 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
1193
1239
return S_ANYTHING;
1194
1240
}
1195
1241
}
1196
- <C ,xskip >{exec_sql }{elif }{space }* { /* pop stack */
1197
- if ( preproc_tos == 0 ) {
1242
+ <C ,xskip >{exec_sql }{elif }{space }* {
1243
+ if (preproc_tos == 0 )
1198
1244
mmfatal (PARSE_ERROR, " missing matching \" EXEC SQL IFDEF\" / \" EXEC SQL IFNDEF\" " );
1199
- }
1200
- else if ( stacked_if_value[preproc_tos].else_branch )
1245
+ if (stacked_if_value[preproc_tos].else_branch )
1201
1246
mmfatal (PARSE_ERROR, " missing \" EXEC SQL ENDIF;\" " );
1202
- else
1203
- preproc_tos--;
1204
-
1205
- ifcond = true ; BEGIN (xcond);
1247
+ ifcond = true ;
1248
+ BEGIN (xcond);
1206
1249
}
1207
1250
<C ,xskip >{informix_special }{elif }{space }* {
1208
1251
/* are we simulating Informix? */
1209
1252
if (INFORMIX_MODE)
1210
1253
{
1211
1254
if (preproc_tos == 0 )
1212
1255
mmfatal (PARSE_ERROR, " missing matching \" EXEC SQL IFDEF\" / \" EXEC SQL IFNDEF\" " );
1213
- else if (stacked_if_value[preproc_tos].else_branch )
1256
+ if (stacked_if_value[preproc_tos].else_branch )
1214
1257
mmfatal (PARSE_ERROR, " missing \" EXEC SQL ENDIF;\" " );
1215
- else
1216
- preproc_tos--;
1217
-
1218
1258
ifcond = true ;
1219
1259
BEGIN (xcond);
1220
1260
}
@@ -1226,16 +1266,19 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
1226
1266
}
1227
1267
1228
1268
<C ,xskip >{exec_sql }{else }{space }* " ;" { /* only exec sql endif pops the stack, so take care of duplicated 'else' */
1229
- if (stacked_if_value[preproc_tos].else_branch )
1269
+ if ( preproc_tos == 0 )
1270
+ mmfatal (PARSE_ERROR, " missing matching \" EXEC SQL IFDEF\" / \" EXEC SQL IFNDEF\" " );
1271
+ else if (stacked_if_value[preproc_tos].else_branch )
1230
1272
mmfatal (PARSE_ERROR, " more than one EXEC SQL ELSE" );
1231
1273
else
1232
1274
{
1233
1275
stacked_if_value[preproc_tos].else_branch = true ;
1234
- stacked_if_value[preproc_tos].condition =
1235
- (stacked_if_value[preproc_tos-1 ].condition &&
1236
- !stacked_if_value[preproc_tos].condition );
1276
+ stacked_if_value[preproc_tos].active =
1277
+ (stacked_if_value[preproc_tos-1 ].active &&
1278
+ !stacked_if_value[preproc_tos].saw_active );
1279
+ stacked_if_value[preproc_tos].saw_active = true ;
1237
1280
1238
- if (stacked_if_value[preproc_tos].condition )
1281
+ if (stacked_if_value[preproc_tos].active )
1239
1282
BEGIN (C);
1240
1283
else
1241
1284
BEGIN (xskip);
@@ -1245,16 +1288,19 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
1245
1288
/* are we simulating Informix? */
1246
1289
if (INFORMIX_MODE)
1247
1290
{
1248
- if (stacked_if_value[preproc_tos].else_branch )
1291
+ if ( preproc_tos == 0 )
1292
+ mmfatal (PARSE_ERROR, " missing matching \" EXEC SQL IFDEF\" / \" EXEC SQL IFNDEF\" " );
1293
+ else if (stacked_if_value[preproc_tos].else_branch )
1249
1294
mmfatal (PARSE_ERROR, " more than one EXEC SQL ELSE" );
1250
1295
else
1251
1296
{
1252
1297
stacked_if_value[preproc_tos].else_branch = true ;
1253
- stacked_if_value[preproc_tos].condition =
1254
- (stacked_if_value[preproc_tos-1 ].condition &&
1255
- !stacked_if_value[preproc_tos].condition );
1298
+ stacked_if_value[preproc_tos].active =
1299
+ (stacked_if_value[preproc_tos-1 ].active &&
1300
+ !stacked_if_value[preproc_tos].saw_active );
1301
+ stacked_if_value[preproc_tos].saw_active = true ;
1256
1302
1257
- if (stacked_if_value[preproc_tos].condition )
1303
+ if (stacked_if_value[preproc_tos].active )
1258
1304
BEGIN (C);
1259
1305
else
1260
1306
BEGIN (xskip);
@@ -1272,7 +1318,7 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
1272
1318
else
1273
1319
preproc_tos--;
1274
1320
1275
- if (stacked_if_value[preproc_tos].condition )
1321
+ if (stacked_if_value[preproc_tos].active )
1276
1322
BEGIN (C);
1277
1323
else
1278
1324
BEGIN (xskip);
@@ -1286,7 +1332,7 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
1286
1332
else
1287
1333
preproc_tos--;
1288
1334
1289
- if (stacked_if_value[preproc_tos].condition )
1335
+ if (stacked_if_value[preproc_tos].active )
1290
1336
BEGIN (C);
1291
1337
else
1292
1338
BEGIN (xskip);
@@ -1301,12 +1347,10 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
1301
1347
<xskip >{other } { /* ignore */ }
1302
1348
1303
1349
<xcond >{identifier }{space }* " ;" {
1304
- if (preproc_tos >= MAX_NESTED_IF-1 )
1305
- mmfatal (PARSE_ERROR, " too many nested EXEC SQL IFDEF conditions" );
1306
- else
1307
1350
{
1308
1351
struct _defines *defptr;
1309
1352
unsigned int i;
1353
+ bool this_active;
1310
1354
1311
1355
/*
1312
1356
* Skip the ";" and trailing whitespace. Note that yytext
@@ -1324,13 +1368,15 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
1324
1368
defptr = defptr->next )
1325
1369
/* skip */ ;
1326
1370
1327
- preproc_tos++;
1328
- stacked_if_value[preproc_tos].else_branch = false ;
1329
- stacked_if_value[preproc_tos].condition =
1330
- (defptr ? ifcond : !ifcond) && stacked_if_value[preproc_tos-1 ].condition ;
1371
+ this_active = (defptr ? ifcond : !ifcond);
1372
+ stacked_if_value[preproc_tos].active =
1373
+ (stacked_if_value[preproc_tos-1 ].active &&
1374
+ !stacked_if_value[preproc_tos].saw_active &&
1375
+ this_active);
1376
+ stacked_if_value[preproc_tos].saw_active |= this_active;
1331
1377
}
1332
1378
1333
- if (stacked_if_value[preproc_tos].condition )
1379
+ if (stacked_if_value[preproc_tos].active )
1334
1380
BEGIN (C);
1335
1381
else
1336
1382
BEGIN (xskip);
@@ -1442,10 +1488,12 @@ lex_init(void)
1442
1488
parenths_open = 0 ;
1443
1489
current_function = NULL ;
1444
1490
1445
- preproc_tos = 0 ;
1446
1491
yylineno = 1 ;
1447
- ifcond = true ;
1448
- stacked_if_value[preproc_tos].condition = ifcond;
1492
+
1493
+ /* initialize state for if/else/endif */
1494
+ preproc_tos = 0 ;
1495
+ stacked_if_value[preproc_tos].active = true ;
1496
+ stacked_if_value[preproc_tos].saw_active = true ;
1449
1497
stacked_if_value[preproc_tos].else_branch = false ;
1450
1498
1451
1499
/* initialize literal buffer to a reasonable but expansible size */
0 commit comments