Skip to content

Commit

Permalink
Merge pull request #346 from atilaneves/macros
Browse files Browse the repository at this point in the history
Fix Python macro translation
  • Loading branch information
atilaneves committed Jun 5, 2024
2 parents 739d665 + 3a01aa9 commit 9c2b175
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ tmp
.ninja_*
*.ninja
compile_commands.json
dub_describe.json
dub_describe.json
*.lst
11 changes: 6 additions & 5 deletions source/dpp/translation/macro_.d
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,16 @@ string[] translateMacro(in from!"clang".Cursor cursor,
];
}
}

return [
`#ifdef ` ~ spelling,
`# undef ` ~ spelling,
`#endif`,
`static if(!is(typeof(` ~ spelling ~ `))) {`,
] ~ ret ~ [
`}`,
`#define ` ~ spelling ~ ` ` ~ translation.dcode,
];
] ~
ret ~
[
`#define ` ~ spelling ~ ` ` ~ translation.dcode,
];
}

// Define a template function with the same name as the macro
Expand Down
16 changes: 14 additions & 2 deletions tests/it/issues.d
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,12 @@ version(linux) // linux specific header in the test
}
}

// the original fix for this issue had to do with ordering, but it
// also ended up guarding a `static if` that checked if a macro
// already existed before trying to define it again. Unfortunately,
// too many static ifs caused the compiler to have ordering problems,
// but removing that caused this test to fail. The "fix" is to ask for
// scoped enums so that it doesn't try to define `BAR` twice.
@Tags("issue")
@("79")
unittest {
Expand All @@ -947,7 +953,11 @@ unittest {
#define BAR 33
// before the BAR macro, BAR is 42. After, it's 33.
`,
D(""), // no need for .dpp source code
D(
q{
// the macro is what works here
static assert(BAR == 33);
}),
);
writeFile("2nd.h",
`
Expand All @@ -956,7 +966,9 @@ unittest {
// The bug had to do with ordering.
enum TheEnum { BAR = 42 };
`);
run("-c", inSandboxPath("app.dpp"), "--keep-pre-cpp-files");

runPreprocessOnly("app.dpp", "--scoped-enums");
shouldCompile("app.d");
}
}

Expand Down

0 comments on commit 9c2b175

Please sign in to comment.