Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
atilaneves committed May 23, 2024
1 parent 47bb595 commit f3dc7fe
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ targetPath "bin"
targetName "d++"

dependency "libclang" version="~>0.3.1"
dependency "sumtype" version="~>0.7.1"
dependency "sumtype" version="~>1.2.0"

versions "SumTypeNoDefaultCtor"

Expand Down
4 changes: 4 additions & 0 deletions source/dpp/clang/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,7 @@ bool hasAnonymousSpelling(in string spelling) @safe pure nothrow {
import std.algorithm : canFind;
return spelling.canFind("(anonymous") || spelling.canFind("(unnamed");
}

bool isSortaAnonymous(in from!"clang".Cursor cursor) @safe pure nothrow {
return cursor.spelling == "" || cursor.isAnonymous;
}
3 changes: 2 additions & 1 deletion source/dpp/runtime/context.d
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ struct Context {

/// return the spelling if it exists, or our made-up nickname for it if not
string spellingOrNickname(in Cursor cursor) @safe pure {
if (cursor.spelling == "" || cursor.isAnonymous)
import dpp.clang: isSortaAnonymous;
if (cursor.isSortaAnonymous)
return nickName(cursor);

return spelling(cursor.spelling);
Expand Down
6 changes: 3 additions & 3 deletions source/dpp/translation/translation.d
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ private bool skipTopLevel(in from!"clang".Cursor cursor,
@safe
{
import dpp.translation.aggregate: isAggregateC;
import dpp.clang: isSortaAnonymous;
import clang: Cursor;
import std.algorithm: startsWith, canFind;

if(context.isFromIgnoredPath(cursor))
return true;

// We want to ignore anonymous structs and unions but not enums. See #54
if((cursor.spelling == "" || cursor.isAnonymous) && cursor.kind == Cursor.Kind.EnumDecl)
if((cursor.isSortaAnonymous) && cursor.kind == Cursor.Kind.EnumDecl)
return false;

// don't bother translating top-level anonymous aggregates
const isAnon = cursor.spelling == "" || cursor.isAnonymous;
if(isAggregateC(cursor) && isAnon)
if(isAggregateC(cursor) && cursor.isSortaAnonymous)
return true;

if(context.options.ignoreMacros && cursor.kind == Cursor.Kind.MacroDefinition)
Expand Down
6 changes: 4 additions & 2 deletions source/dpp/translation/typedef_.d
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ string[] translateNonFunction(in from!"clang".Cursor cursor,
private bool isTopLevelAnonymous(in from!"clang".Cursor[] children)
@safe nothrow
{
import dpp.clang: isSortaAnonymous;
import clang: Cursor;

return
children.length == 1 && // so we can inspect it
(children[0].spelling == "" || children[0].isAnonymous) && // anonymous
children[0].isSortaAnonymous && // anonymous
children[0].lexicalParent.kind == Cursor.Kind.TranslationUnit && // top-level
children[0].kind != Cursor.Kind.ParmDecl // a lot more should be here
;
Expand All @@ -91,6 +92,7 @@ private string[] translateRegular(in from!"clang".Cursor cursor,
import dpp.translation.type: translate, removeDppDecorators;
import dpp.translation.aggregate: isAggregateC;
import dpp.translation.dlang: maybeRename;
import dpp.clang: isSortaAnonymous;
import clang: Type;
import std.typecons: No;

Expand All @@ -102,7 +104,7 @@ private string[] translateRegular(in from!"clang".Cursor cursor,
const isAnonymousEnum =
children.length == 1 &&
isAggregateC(children[0]) &&
children[0].spelling == "" &&
children[0].isSortaAnonymous &&
children[0].type.kind == Type.Kind.Enum
;

Expand Down

0 comments on commit f3dc7fe

Please sign in to comment.