[PATCH] D27263: Address of bitfield in anonymous struct doesn't error.

Jacob Young via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 30 09:25:46 PST 2016


jacobly created this revision.
jacobly added a subscriber: cfe-commits.

  struct A {
    struct {
      int B : 1;
    }
  }
  int A::*addr_anon_bitfield() {
      return &A::B;
  }

This code does not error, but instead returns a member pointer to a full int that starts at the beginning of the byte the bitfield starts on.


https://reviews.llvm.org/D27263

Files:
  /home/jacob/Source/llvm/tools/clang/lib/Sema/SemaExpr.cpp
  /home/jacob/Source/llvm/tools/clang/test/Sema/expr-address-of.c
  /home/jacob/Source/llvm/tools/clang/test/SemaCXX/anonymous-struct.cpp
  /home/jacob/Source/llvm/tools/clang/test/SemaCXX/ptrtomember.cpp


Index: /home/jacob/Source/llvm/tools/clang/test/SemaCXX/ptrtomember.cpp
===================================================================
--- /home/jacob/Source/llvm/tools/clang/test/SemaCXX/ptrtomember.cpp
+++ /home/jacob/Source/llvm/tools/clang/test/SemaCXX/ptrtomember.cpp
@@ -13,9 +13,13 @@
 
 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();
Index: /home/jacob/Source/llvm/tools/clang/test/SemaCXX/anonymous-struct.cpp
===================================================================
--- /home/jacob/Source/llvm/tools/clang/test/SemaCXX/anonymous-struct.cpp
+++ /home/jacob/Source/llvm/tools/clang/test/SemaCXX/anonymous-struct.cpp
@@ -21,6 +21,9 @@
   };
   static struct {
   };
+  class {
+    int anon_priv_field; // expected-error {{anonymous struct cannot contain a private data member}}
+  };
 };
 
 template <class T> void foo(T);
Index: /home/jacob/Source/llvm/tools/clang/test/Sema/expr-address-of.c
===================================================================
--- /home/jacob/Source/llvm/tools/clang/test/Sema/expr-address-of.c
+++ /home/jacob/Source/llvm/tools/clang/test/Sema/expr-address-of.c
@@ -102,8 +102,9 @@
   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);
 }
Index: /home/jacob/Source/llvm/tools/clang/lib/Sema/SemaExpr.cpp
===================================================================
--- /home/jacob/Source/llvm/tools/clang/lib/Sema/SemaExpr.cpp
+++ /home/jacob/Source/llvm/tools/clang/lib/Sema/SemaExpr.cpp
@@ -1762,7 +1762,10 @@
       !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, E->getLocStart()))
       recordUseOfEvaluatedWeak(E);
 
-  if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
+  FieldDecl *FD = dyn_cast<FieldDecl>(D);
+  if (IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(D))
+    FD = IFD->getAnonField();
+  if (FD) {
     UnusedPrivateFields.remove(FD);
     // Just in case we're building an illegal pointer-to-member.
     if (FD->isBitField())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27263.79759.patch
Type: text/x-patch
Size: 2458 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161130/97e66ed4/attachment-0001.bin>


More information about the cfe-commits mailing list