Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ typedef enum {
T_cppd_elif,
T_cppd_else,
T_cppd_endif,
T_cppd_ifdef
T_cppd_ifdef,
T_cppd_ifndef
} token_t;

char token_str[MAX_TOKEN_LEN];
Expand Down Expand Up @@ -206,6 +207,8 @@ token_t lex_token_internal(bool aliasing)
return T_cppd_elif;
if (!strcmp(token_str, "#ifdef"))
return T_cppd_ifdef;
if (!strcmp(token_str, "#ifndef"))
return T_cppd_ifndef;
if (!strcmp(token_str, "#else"))
return T_cppd_else;
if (!strcmp(token_str, "#endif"))
Expand Down
21 changes: 17 additions & 4 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,9 @@ void cppd_control_flow_skip_lines()
skip_whitespace();
}

void check_def(char *alias)
void check_def(char *alias, bool expected)
{
if (find_alias(alias))
if ((find_alias(alias) != NULL) == expected)
preproc_match = true;
}

Expand All @@ -349,7 +349,7 @@ void read_defined_macro()
lex_ident(T_identifier, lookup_alias);
lex_expect(T_close_bracket);

check_def(lookup_alias);
check_def(lookup_alias, true);
}

/* read preprocessor directive at each potential positions: e.g.,
Expand Down Expand Up @@ -485,7 +485,20 @@ bool read_preproc_directive()
if (lex_accept_internal(T_cppd_ifdef, false)) {
preproc_match = false;
lex_ident(T_identifier, token);
check_def(token);
check_def(token, true);

if (preproc_match) {
skip_whitespace();
return true;
}

cppd_control_flow_skip_lines();
return true;
}
if (lex_accept_internal(T_cppd_ifndef, false)) {
preproc_match = false;
lex_ident(T_identifier, token);
check_def(token, false);

if (preproc_match) {
skip_whitespace();
Expand Down
13 changes: 13 additions & 0 deletions tests/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,19 @@ int main()
}
EOF

# #ifndef...#else...#endif
try_ 0 << EOF
#ifndef A
#define A 0
#else
#define A 1
#endif
int main()
{
return A;
}
EOF

# #if defined(...) ... #elif defined(...) ... #else ... #endif
try_ 0 << EOF
#define A 0
Expand Down