From: Richard Smith Date: Thu, 13 Apr 2017 21:49:46 +0000 (+0000) Subject: Diagnose attempt to take address of bitfield members in anonymous structs. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=18974a9561e3bc3bddc03430a7ecbf226c9121ef;p=clang Diagnose attempt to take address of bitfield members in anonymous structs. Patch by Jacob Young! Differential Revision: https://reviews.llvm.org/D27263 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@300264 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 73baf1a660..bb174521c7 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -1772,7 +1772,10 @@ Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, E->getLocStart())) recordUseOfEvaluatedWeak(E); - if (FieldDecl *FD = dyn_cast(D)) { + FieldDecl *FD = dyn_cast(D); + if (IndirectFieldDecl *IFD = dyn_cast(D)) + FD = IFD->getAnonField(); + if (FD) { UnusedPrivateFields.remove(FD); // Just in case we're building an illegal pointer-to-member. if (FD->isBitField()) diff --git a/test/Sema/expr-address-of.c b/test/Sema/expr-address-of.c index 32bd0dfdd5..480871afad 100644 --- a/test/Sema/expr-address-of.c +++ b/test/Sema/expr-address-of.c @@ -102,8 +102,9 @@ char* f7() { register struct {char* x;} t1 = {"Hello"}; char* dummy1 = &(t1.x[0]); - struct {int a : 10;} t2; + struct {int a : 10; struct{int b : 10;};} t2; int* dummy2 = &(t2.a); // expected-error {{address of bit-field requested}} + int* dummy3 = &(t2.b); // expected-error {{address of bit-field requested}} void* t3 = &(*(void*)0); } diff --git a/test/SemaCXX/ptrtomember.cpp b/test/SemaCXX/ptrtomember.cpp index aee535e559..d4a4507d02 100644 --- a/test/SemaCXX/ptrtomember.cpp +++ b/test/SemaCXX/ptrtomember.cpp @@ -13,9 +13,13 @@ int foo(int S::* ps, S *s) struct S2 { int bitfield : 1; + struct { + int anon_bitfield : 1; + }; }; int S2::*pf = &S2::bitfield; // expected-error {{address of bit-field requested}} +int S2::*anon_pf = &S2::anon_bitfield; // expected-error {{address of bit-field requested}} struct S3 { void m();