Skip to content

Commit 931d40d

Browse files
committed
Fix translation of union array members
1 parent 609ae54 commit 931d40d

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ bool IsInMainFile(const clang::Decl *decl) {
130130
return src_mgr.isInMainFile(src_mgr.getExpansionLoc(loc));
131131
}
132132

133+
bool IsUnionArrayMember(const clang::Expr *base) {
134+
if (auto *me =
135+
clang::dyn_cast<clang::MemberExpr>(base->IgnoreParenImpCasts())) {
136+
if (auto *fd = clang::dyn_cast<clang::FieldDecl>(me->getMemberDecl())) {
137+
return fd->getParent()->isUnion() && fd->getType()->isArrayType();
138+
}
139+
}
140+
return false;
141+
}
142+
133143
bool IsUserDefinedDecl(const clang::Decl *decl) {
134144
const auto &ctx = decl->getASTContext();
135145
const auto &src_mgr = ctx.getSourceManager();

‎cpp2rust/converter/converter_lib.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ bool IsComparisonWithNullOp(const clang::BinaryOperator *expr);
4040

4141
bool IsInMainFile(const clang::Decl *decl);
4242

43+
bool IsUnionArrayMember(const clang::Expr *base);
44+
4345
bool IsUserDefinedDecl(const clang::Decl *decl);
4446

4547
bool RefersToUserDefinedDecl(const clang::Expr *expr);

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,8 @@ std::string ConverterRefCount::ConvertPtrType(clang::QualType type) {
378378
bool ConverterRefCount::VisitArraySubscriptExpr(
379379
clang::ArraySubscriptExpr *expr) {
380380
auto *base = expr->getBase();
381-
if (base->IgnoreCasts()->getType()->isPointerType()) {
381+
if (base->IgnoreCasts()->getType()->isPointerType() ||
382+
(isLValue() && IsUnionArrayMember(base))) {
382383
ConvertPointerSubscript(expr);
383384
} else {
384385
if (!base->IgnoreCasts()->getType()->isArrayType()) {

0 commit comments

Comments
 (0)