Skip to content

Commit

Permalink
Remove redundant version condition with ifndef
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorpel committed Jul 19, 2023
1 parent 850a448 commit 5e3cf61
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
14 changes: 14 additions & 0 deletions source/ctod/cpreproc.d
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ bool ctodTryPreprocessor(ref CtodCtx ctx, ref Node node) {
nameNode.prepend("(");
nameNode.append(") {");
} else if (auto c = node.firstChildType(Sym.aux_preproc_ifdef_token2)) {
// Recognize `#ifndef X (#define X Y) #endif`, treat as just `#define X Y`
if (node.children.length == 4) {
Node b = node.children[2];
if (b.typeEnum == Sym.preproc_def) {
if (auto cc = b.childField(Field.name)) {
if (cc.source == nameNode.source) {
translateNode(ctx, b);
node.replace(b.output);
return true;
}
}
}
}

// "#ifndef"
c.replace("version");
nameNode.prepend("(");
Expand Down
12 changes: 10 additions & 2 deletions source/ctod/test.d
Original file line number Diff line number Diff line change
Expand Up @@ -564,11 +564,19 @@ public import small;
#define NO_HEADER_GUARD 3
#endif
", "
version (NO_HEADER_GUARD) {} else {
enum NO_HEADER_GUARD = 3;
}
");

test("
#ifndef CONDITION
#define NOT_THE_IFNDEF_CONDITION 99
#endif
", "
version (CONDITION) {} else {
enum NOT_THE_IFNDEF_CONDITION = 99;
}
");

test("
#define TEST
Expand Down

0 comments on commit 5e3cf61

Please sign in to comment.