Skip to content

Commit

Permalink
Fix spurious warnings about unused private field
Browse files Browse the repository at this point in the history
If the address of a field is taken as a pointer to member, we should
not warn that the field is not used.

Normaly, yse of fields are done from MemberExpr, but in case of pointer to
member, it is in a DeclRefExpr

Differential Revision: http://reviews.llvm.org/D20054

llvm-svn: 268895
  • Loading branch information
ogoffart committed May 9, 2016
1 parent 5af2c00 commit 63a2083
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 6 additions & 4 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1736,10 +1736,12 @@ Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK,
!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, E->getLocStart()))
recordUseOfEvaluatedWeak(E);

// Just in case we're building an illegal pointer-to-member.
FieldDecl *FD = dyn_cast<FieldDecl>(D);
if (FD && FD->isBitField())
E->setObjectKind(OK_BitField);
if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
UnusedPrivateFields.remove(FD);
// Just in case we're building an illegal pointer-to-member.
if (FD->isBitField())
E->setObjectKind(OK_BitField);
}

return E;
}
Expand Down
2 changes: 2 additions & 0 deletions clang/test/SemaCXX/warn-unused-private-field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class EverythingUsed {
int *use = &by_reference_;
int test[2];
test[as_array_index_] = 42;
int EverythingUsed::*ptr = &EverythingUsed::by_pointer_to_member_;
}

template<class T>
Expand All @@ -142,6 +143,7 @@ class EverythingUsed {
int by_template_function_;
int as_array_index_;
int by_initializer_;
int by_pointer_to_member_;
};

class HasFeatureTest {
Expand Down

0 comments on commit 63a2083

Please sign in to comment.