From 09b5cb685c391c19bbbc6b2f233782c270ae1773 Mon Sep 17 00:00:00 2001 From: Jacob Carlborg Date: Sun, 16 Apr 2023 15:06:50 +0200 Subject: [PATCH] Fix #247: --alias-enum-members doesn't work properly for typedef enums --- changelog.md | 1 + dstep/translator/Enum.d | 15 +++++++++++---- tests/unit/EnumUnitTests.d | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/changelog.md b/changelog.md index 69fb3bed..76864b76 100644 --- a/changelog.md +++ b/changelog.md @@ -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 diff --git a/dstep/translator/Enum.d b/dstep/translator/Enum.d index 4c051199..3407e93a 100644 --- a/dstep/translator/Enum.d +++ b/dstep/translator/Enum.d @@ -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;"; @@ -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); diff --git a/tests/unit/EnumUnitTests.d b/tests/unit/EnumUnitTests.d index 1355f96a..15968b37 100644 --- a/tests/unit/EnumUnitTests.d +++ b/tests/unit/EnumUnitTests.d @@ -11,7 +11,7 @@ import dstep.translator.Translator; // Test standard enum. unittest { - assertTranslates( + assertTranslates( q"C enum Qux { @@ -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 {