Skip to content

Commit c0bbf58

Browse files
authored
Merge pull request #158 from ChAoSUnItY/master
Implement preprocessor directive #ifndef
2 parents 16484f2 + 86afaa9 commit c0bbf58

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

src/lexer.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ typedef enum {
7777
T_cppd_elif,
7878
T_cppd_else,
7979
T_cppd_endif,
80-
T_cppd_ifdef
80+
T_cppd_ifdef,
81+
T_cppd_ifndef
8182
} token_t;
8283

8384
char token_str[MAX_TOKEN_LEN];
@@ -206,6 +207,8 @@ token_t lex_token_internal(bool aliasing)
206207
return T_cppd_elif;
207208
if (!strcmp(token_str, "#ifdef"))
208209
return T_cppd_ifdef;
210+
if (!strcmp(token_str, "#ifndef"))
211+
return T_cppd_ifndef;
209212
if (!strcmp(token_str, "#else"))
210213
return T_cppd_else;
211214
if (!strcmp(token_str, "#endif"))

src/parser.c

+17-4
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,9 @@ void cppd_control_flow_skip_lines()
334334
skip_whitespace();
335335
}
336336

337-
void check_def(char *alias)
337+
void check_def(char *alias, bool expected)
338338
{
339-
if (find_alias(alias))
339+
if ((find_alias(alias) != NULL) == expected)
340340
preproc_match = true;
341341
}
342342

@@ -349,7 +349,7 @@ void read_defined_macro()
349349
lex_ident(T_identifier, lookup_alias);
350350
lex_expect(T_close_bracket);
351351

352-
check_def(lookup_alias);
352+
check_def(lookup_alias, true);
353353
}
354354

355355
/* read preprocessor directive at each potential positions: e.g.,
@@ -485,7 +485,20 @@ bool read_preproc_directive()
485485
if (lex_accept_internal(T_cppd_ifdef, false)) {
486486
preproc_match = false;
487487
lex_ident(T_identifier, token);
488-
check_def(token);
488+
check_def(token, true);
489+
490+
if (preproc_match) {
491+
skip_whitespace();
492+
return true;
493+
}
494+
495+
cppd_control_flow_skip_lines();
496+
return true;
497+
}
498+
if (lex_accept_internal(T_cppd_ifndef, false)) {
499+
preproc_match = false;
500+
lex_ident(T_identifier, token);
501+
check_def(token, false);
489502

490503
if (preproc_match) {
491504
skip_whitespace();

tests/driver.sh

+13
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,19 @@ int main()
458458
}
459459
EOF
460460

461+
# #ifndef...#else...#endif
462+
try_ 0 << EOF
463+
#ifndef A
464+
#define A 0
465+
#else
466+
#define A 1
467+
#endif
468+
int main()
469+
{
470+
return A;
471+
}
472+
EOF
473+
461474
# #if defined(...) ... #elif defined(...) ... #else ... #endif
462475
try_ 0 << EOF
463476
#define A 0

0 commit comments

Comments
 (0)