bkryza / clang-uml

Customizable automatic UML diagram generator for C++ based on Clang.
Apache License 2.0
610 stars 44 forks source link

extend type alias to work for anonymous structs/unions #325

Closed fneddy closed 1 week ago

fneddy commented 1 month ago

i found myself in the situation where i want to map fields inside of anonymous struct fields to another type. It seems that the syntax does not support it. this is my usecase:

struct id {
  unsigned foo;
};

struct table {
    int number;
    union {
       unsigned id[];
        void* ptr;   // always points to struct id*;
    };
};

i tried to map the type via this but no option that i tried worked:

type_aliases:
      table::ptr: id
      table::()::ptr: id
      table::(anonymous)::ptr: id
      table::<anonymous>::ptr: id
      ....

also it seems that clang-uml creates an extra type instance of the anonymous union for every instance of struct table .

bkryza commented 1 month ago

@fneddy type_aliases allows to provide aliases for types, not variables. The problem in your case, is that you want to tell clang-uml that ptr field (note that ptr is the name of the field not the type) has a different type than what is declared in the code (i.e. void*).

Currently 2 options could be possible:

which should result in the following diagram:

@startuml
class "id" as C_0013001791621350910261
class C_0013001791621350910261 {
__
+foo : unsigned int
}
class "table" as C_0013605029241056812048
class C_0013605029241056812048 {
__
+ : table::()
+number : int
}
class "table::()" as C_0003376134320007800112
class C_0003376134320007800112 {
__
+id : unsigned int[]
+ptr : id *
}
C_0013605029241056812048 o-- C_0003376134320007800112
C_0003376134320007800112 ..> C_0013001791621350910261
@enduml

I'll think about adding an option to override types of variables/members/function signatures in the future...

fneddy commented 1 month ago

thank you this helps me a lot.

curious side note this seems to work:

type_aliases:
      table::number: id

it aliases the field number to the type id.

bkryza commented 1 month ago

@fneddy strange for me this alias:

type_aliases:
      table::number: id

doesn't change anything in the diagram...

fneddy commented 1 month ago

you are right. that must have been in an overlook of me.