Skip to content

Commit

Permalink
Fixed handling of nested anonymous arrays of structs
Browse files Browse the repository at this point in the history
  • Loading branch information
bkryza committed Jun 1, 2024
1 parent c9b3f92 commit d599bb6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/class_diagram/visitor/translation_unit_visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,10 @@ void translation_unit_visitor::process_field(
relationship_hint = relationship_t::kAssociation;
field_type = field_type.getNonReferenceType();
}
else if (field_type->isArrayType()) {
relationship_hint = relationship_t::kAggregation;
field_type = field_type->getAsArrayTypeUnsafe()->getElementType();
}
else if (field_type->isRValueReferenceType()) {
field_type = field_type.getNonReferenceType();
}
Expand Down
10 changes: 8 additions & 2 deletions src/common/clang_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ std::string get_tag_name(const clang::TagDecl &declaration)
std::string to_string(const clang::QualType &type, const clang::ASTContext &ctx,
bool try_canonical)
{
if (type->isArrayType()) {
return fmt::format("{}[]",
to_string(type->getAsArrayTypeUnsafe()->getElementType(), ctx,
try_canonical));
}

clang::PrintingPolicy print_policy(ctx.getLangOpts());
print_policy.SuppressScope = 0;
print_policy.PrintCanonicalTypes = 0;
Expand Down Expand Up @@ -150,8 +156,8 @@ std::string to_string(const clang::QualType &type, const clang::ASTContext &ctx,
result = "(anonymous)";
else if (util::contains(result, "unnamed struct") ||
util::contains(result, "unnamed union")) {
auto declarationTag = type->getAsTagDecl();
if (declarationTag == NULL) {
const auto *declarationTag = type->getAsTagDecl();
if (declarationTag == nullptr) {
result = "(unnamed undeclared)";
}
else {
Expand Down
7 changes: 7 additions & 0 deletions tests/t00037/t00037.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace clanguml {
namespace t00037 {

constexpr auto LENGTH{10ULL};

class ST {
public:
struct {
Expand All @@ -10,6 +12,11 @@ class ST {
double z;
} dimensions;

struct {
int len;
int flags;
} __attribute__((packed)) bars[LENGTH];

private:
struct {
double c{1.0};
Expand Down
2 changes: 2 additions & 0 deletions tests/t00037/test_case.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ TEST_CASE("t00037")
REQUIRE(IsClass(src, "A"));
REQUIRE(IsClass(src, "ST::(units)"));
REQUIRE(IsClass(src, "ST::(dimensions)"));
REQUIRE(IsClass(src, "ST::(bars)"));

REQUIRE(
IsAggregation<Public>(src, "ST", "ST::(dimensions)", "dimensions"));
REQUIRE(IsAggregation<Private>(src, "ST", "ST::(units)", "units"));
REQUIRE(IsAggregation<Public>(src, "ST", "ST::(bars)", "bars"));
});
}

0 comments on commit d599bb6

Please sign in to comment.