@@ -58,10 +58,6 @@ typedef enum {
58
58
T_while ,
59
59
T_for ,
60
60
T_do ,
61
- T_define ,
62
- T_undef ,
63
- T_error ,
64
- T_include ,
65
61
T_typedef ,
66
62
T_enum ,
67
63
T_struct ,
@@ -71,7 +67,16 @@ typedef enum {
71
67
T_case ,
72
68
T_break ,
73
69
T_default ,
74
- T_continue
70
+ T_continue ,
71
+ T_preproc_include ,
72
+ T_preproc_define ,
73
+ T_preproc_undef ,
74
+ T_preproc_error ,
75
+ T_preproc_if ,
76
+ T_preproc_elif ,
77
+ T_preproc_else ,
78
+ T_preproc_endif ,
79
+ T_preproc_ifdef
75
80
} token_t ;
76
81
77
82
char token_str [MAX_TOKEN_LEN ];
@@ -189,39 +194,6 @@ char peek_char(int offset)
189
194
return SOURCE [source_idx + offset ];
190
195
}
191
196
192
- void if_elif_skip_lines ()
193
- {
194
- char peek_c ;
195
- int i ;
196
-
197
- do {
198
- skip_whitespace ();
199
- i = 0 ;
200
- do {
201
- token_str [i ++ ] = next_char ;
202
- } while (read_char (0 ) != '\n' );
203
- token_str [i ] = 0 ;
204
- read_char (1 );
205
- peek_c = peek_char (1 );
206
- } while (next_char != '#' || (next_char == '#' && peek_c == 'd' ));
207
- skip_whitespace ();
208
- }
209
-
210
- void ifdef_else_skip_lines ()
211
- {
212
- int i ;
213
-
214
- do {
215
- skip_whitespace ();
216
- i = 0 ;
217
- do {
218
- token_str [i ++ ] = next_char ;
219
- } while (read_char (0 ) != '\n' );
220
- token_str [i ] = 0 ;
221
- } while (strcmp (token_str , "#else" ) && strcmp (token_str , "#endif" ));
222
- skip_whitespace ();
223
- }
224
-
225
197
/* check alias defined or not */
226
198
void chk_def (int defined )
227
199
{
@@ -253,108 +225,31 @@ token_t get_next_token()
253
225
skip_whitespace ();
254
226
255
227
if (!strcmp (token_str , "#include" )) {
256
- do {
257
- token_str [i ++ ] = next_char ;
258
- } while (read_char (0 ) != '\n' );
259
- skip_whitespace ();
260
- return T_include ;
228
+ return T_preproc_include ;
261
229
}
262
230
if (!strcmp (token_str , "#define" )) {
263
- skip_whitespace ();
264
- return T_define ;
231
+ return T_preproc_define ;
265
232
}
266
233
if (!strcmp (token_str , "#undef" )) {
267
- skip_whitespace ();
268
- return T_undef ;
234
+ return T_preproc_undef ;
269
235
}
270
236
if (!strcmp (token_str , "#error" )) {
271
- skip_whitespace ();
272
- return T_error ;
237
+ return T_preproc_error ;
273
238
}
274
239
if (!strcmp (token_str , "#if" )) {
275
- preproc_match = 0 ;
276
- i = 0 ;
277
- do {
278
- token_str [i ++ ] = next_char ;
279
- } while (read_char (0 ) != '\n' );
280
- token_str [i ] = 0 ;
281
-
282
- if (!strncmp (token_str , "defined" , 7 )) {
283
- chk_def (1 );
284
- if (preproc_match ) {
285
- skip_whitespace ();
286
- return get_next_token ();
287
- }
288
-
289
- /* skip lines until #elif or #else or #endif */
290
- if_elif_skip_lines ();
291
- return get_next_token ();
292
- }
240
+ return T_preproc_if ;
293
241
}
294
242
if (!strcmp (token_str , "#elif" )) {
295
- if (preproc_match ) {
296
- do {
297
- skip_whitespace ();
298
- i = 0 ;
299
- do {
300
- token_str [i ++ ] = next_char ;
301
- } while (read_char (0 ) != '\n' );
302
- token_str [i ] = 0 ;
303
- } while (strcmp (token_str , "#endif" ));
304
- skip_whitespace ();
305
- return get_next_token ();
306
- }
307
-
308
- i = 0 ;
309
- do {
310
- token_str [i ++ ] = next_char ;
311
- } while (read_char (0 ) != '\n' );
312
- token_str [i ] = 0 ;
313
-
314
- if (!strncmp (token_str , "defined" , 7 )) {
315
- chk_def (1 );
316
- if (preproc_match ) {
317
- skip_whitespace ();
318
- return get_next_token ();
319
- }
320
- /* skip lines until #elif or #else or #endif */
321
- if_elif_skip_lines ();
322
- return get_next_token ();
323
- }
243
+ return T_preproc_elif ;
324
244
}
325
245
if (!strcmp (token_str , "#ifdef" )) {
326
- preproc_match = 0 ;
327
- i = 0 ;
328
- do {
329
- token_str [i ++ ] = next_char ;
330
- } while (read_char (0 ) != '\n' );
331
- token_str [i ] = 0 ;
332
- chk_def (0 );
333
- if (preproc_match ) {
334
- skip_whitespace ();
335
- return get_next_token ();
336
- }
337
- /* skip lines until #else or #endif */
338
- ifdef_else_skip_lines ();
339
- return get_next_token ();
246
+ return T_preproc_ifdef ;
340
247
}
341
248
if (!strcmp (token_str , "#else" )) {
342
- /* reach here has 2 possible cases:
343
- * 1. reach #ifdef preprocessor directive
344
- * 2. conditional expression in #elif is false
345
- */
346
- if (!preproc_match ) {
347
- skip_whitespace ();
348
- return get_next_token ();
349
- }
350
- /* skip lines until #else or #endif */
351
- ifdef_else_skip_lines ();
352
- return get_next_token ();
249
+ return T_preproc_else ;
353
250
}
354
251
if (!strcmp (token_str , "#endif" )) {
355
- preproc_match = 0 ;
356
- skip_whitespace ();
357
- return get_next_token ();
252
+ return T_preproc_endif ;
358
253
}
359
254
error ("Unknown directive" );
360
255
}
@@ -709,7 +604,12 @@ void skip_macro_body()
709
604
int lex_accept (token_t token )
710
605
{
711
606
if (next_token == token ) {
607
+ /* FIXME: this is a hack, fix aggressive aliasing first */
608
+ if (token == T_preproc_ifdef )
609
+ preproc_aliasing = 0 ;
712
610
next_token = get_next_token ();
611
+ if (token == T_preproc_ifdef )
612
+ preproc_aliasing = 1 ;
713
613
return 1 ;
714
614
}
715
615
return 0 ;
0 commit comments