Skip to content

Commit

Permalink
[Sema] Packed member warning: Use the typedef name for anonymous stru…
Browse files Browse the repository at this point in the history
…ctures

This commit improves the packed member warning by showing the name of the
anonymous structure/union when it was defined within a typedef declaration.

rdar://28498901

Differential Revision: https://reviews.llvm.org/D25106

llvm-svn: 283304
  • Loading branch information
hyp committed Oct 5, 2016
1 parent 602e625 commit 014181e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
7 changes: 6 additions & 1 deletion clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11372,8 +11372,13 @@ void Sema::AddPotentialMisalignedMembers(Expr *E, RecordDecl *RD, ValueDecl *MD,

void Sema::DiagnoseMisalignedMembers() {
for (MisalignedMember &m : MisalignedMembers) {
const NamedDecl *ND = m.RD;
if (ND->getName().empty()) {
if (const TypedefNameDecl *TD = m.RD->getTypedefNameForAnonDecl())
ND = TD;
}
Diag(m.E->getLocStart(), diag::warn_taking_address_of_packed_member)
<< m.MD << m.RD << m.E->getSourceRange();
<< m.MD << ND << m.E->getSourceRange();
}
MisalignedMembers.clear();
}
Expand Down
35 changes: 35 additions & 0 deletions clang/test/Sema/address-packed.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,38 @@ struct AlignedTo2Bis* g7(struct AlignedTo2 *s)
{
return (struct AlignedTo2Bis*)&s->x; // no-warning
}

typedef struct {
char c;
int x;
} __attribute__((packed)) TypedefStructArguable;

typedef union {
char c;
int x;
} __attribute((packed)) TypedefUnionArguable;

typedef TypedefStructArguable TypedefStructArguableTheSecond;

int *typedef1(TypedefStructArguable *s) {
return &s->x; // expected-warning {{packed member 'x' of class or structure 'TypedefStructArguable'}}
}

int *typedef2(TypedefStructArguableTheSecond *s) {
return &s->x; // expected-warning {{packed member 'x' of class or structure 'TypedefStructArguable'}}
}

int *typedef3(TypedefUnionArguable *s) {
return &s->x; // expected-warning {{packed member 'x' of class or structure 'TypedefUnionArguable'}}
}

struct S6 {
union {
char c;
int x;
} __attribute__((packed));
};

int *anonymousInnerUnion(struct S6 *s) {
return &s->x; // expected-warning {{packed member 'x' of class or structure 'S6::(anonymous)'}}
}

0 comments on commit 014181e

Please sign in to comment.