diff options
| -rw-r--r-- | preprocess.c | 20 | ||||
| -rw-r--r-- | tests/078.sh | 38 |
2 files changed, 58 insertions, 0 deletions
diff --git a/preprocess.c b/preprocess.c index 6547028..4ab1a48 100644 --- a/preprocess.c +++ b/preprocess.c @@ -552,6 +552,22 @@ PpToken* process_ifdef_directive(Preprocessor* pp, PpToken* tok) { return NULL; } +PpToken* process_ifndef_directive(Preprocessor* pp, PpToken* tok) { + PpToken* tok2 = skip_whitespace(tok + 1); + if (tok2->kind == PpTokenKind_identifier && string_equals_cstr(&tok2->raw, "ifndef")) { + ++tok2; + tok2 = skip_whitespace(tok2); + if (tok2->kind == PpTokenKind_identifier) { + PpToken* name = tok2; + ++tok2; + pp->skip_pp_tokens = find_pp_macro(pp, &name->raw) != -1; + } + remove_directive_tokens(tok, tok2); + return tok2; + } + return NULL; +} + PpToken* read_include_header_name(PpToken* tok2, String* include_name) { if (tok2->kind == PpTokenKind_string_literal) { *include_name = tok2->raw; @@ -764,6 +780,10 @@ void process_pp_directives(Preprocessor* pp) { tok = next_tok; continue; } + if ((next_tok = process_ifndef_directive(pp, tok)) != NULL) { + tok = next_tok; + continue; + } if (skip_pp_tokens(pp)) { make_token_whitespace(tok); } else if ((next_tok = process_include_directive(pp, tok)) != NULL) { diff --git a/tests/078.sh b/tests/078.sh new file mode 100644 index 0000000..3f890fd --- /dev/null +++ b/tests/078.sh @@ -0,0 +1,38 @@ +set -e + +cat <<'EOF' > expected +__ducc__ is defined. +A is defined. +B is undefined. +EOF + +bash ../../test_diff.sh <<'EOF' +int printf(); + +#define A 123 + +int main() { + +#ifndef __ducc__ + printf("__ducc__ is undefined.\n"); +#else + printf("__ducc__ is defined.\n"); +#endif + +#ifndef A + printf("A is undefined.\n"); +#else + printf("A is defined.\n"); +#endif + +#ifndef B + printf("B is undefined.\n"); +#else + printf("B is defined.\n"); +#endif + +#define B 456 + + return 0; +} +EOF |
