Skip to content

Commit

Permalink
Fix #247: --alias-enum-members doesn't work properly for typedef enums
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-carlborg committed Jul 3, 2023
1 parent 260ff59 commit f722a06
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

* [Issue 235](https://github.com/jacob-carlborg/dstep/issues/235): Passing `--` as an argument doesn't work properly
* [Issue 224](https://github.com/jacob-carlborg/dstep/issues/224): Dub assumes default installation path for LLVM on Windows
* [Issue 247](https://github.com/jacob-carlborg/dstep/issues/247): `--alias-enum-members` doesn't work properly for typedef enums

## Version 1.0.0
### New/Changed Features
Expand Down
15 changes: 11 additions & 4 deletions dstep/translator/Enum.d
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ void translateEnumConstantDecl(

void generateEnumAliases(Output output, Context context, Cursor cursor, string spelling)
{
string subscope = cursorScopeString(context, cursor) ~ ".";
auto subscope = cursorScopeString(context, cursor);

if (subscope.length == 0)
subscope = spelling;

subscope ~= '.';

version (D1)
enum fmt = "alias %2$s %1$s;";
Expand Down Expand Up @@ -256,12 +261,14 @@ void translateEnumDef(Output output, Context context, Cursor cursor)

auto variables = cursor.variablesInParentScope();
auto anonymous = context.shouldBeAnonymous(cursor);
auto spelling = "enum";
auto spelling = "";

if (!anonymous || variables || !cursor.isGlobal)
spelling = "enum " ~ translateIdentifier(context.translateTagSpelling(cursor));
spelling = translateIdentifier(context.translateTagSpelling(cursor));

auto declarationSpelling = spelling.length > 0 ? "enum " ~ spelling : "enum";

output.subscopeStrong(cursor.extent, "%s", spelling) in
output.subscopeStrong(cursor.extent, "%s", declarationSpelling) in
{
auto members = cursor.children
.filter!(cursor => cursor.kind == CXCursorKind.enumConstantDecl);
Expand Down
33 changes: 32 additions & 1 deletion tests/unit/EnumUnitTests.d
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import dstep.translator.Translator;
// Test standard enum.
unittest
{
assertTranslates(
assertTranslates(
q"C
enum Qux
{
Expand Down Expand Up @@ -310,6 +310,37 @@ D", options);

}

// Test aliasing of all enum members for anonymous typedefed enum.
unittest
{
Options options;
options.aliasEnumMembers = true;

assertTranslates(
q"C
typedef enum
{
FOO,
BAR,
BAZ,
} Enum;
C",
q"D
extern (C):
enum Enum
{
FOO = 0,
BAR = 1,
BAZ = 2
}
alias FOO = Enum.FOO;
alias BAR = Enum.BAR;
alias BAZ = Enum.BAZ;
D", options);
}

// Test a named enum inside a struct.
unittest
{
Expand Down

0 comments on commit f722a06

Please sign in to comment.