[PATCH] D131202: [Clang] Fix capture of values initialized by bitfields

2022-08-10 Thread Alexander Yermolovich via Phabricator via cfe-commits
ayermolo added a comment.

Thanks for reverting this.
Previous commit broke build of projects using fmt library.
https://github.com/fmtlib/fmt/blame/master/include/fmt/format.h

  fmt/fmt/include/fmt/format.h:1904:9: error: cannot capture a bit-field by 
reference
   if (sign) *it++ = detail::sign(sign);
   ^
   fmt/fmt/include/fmt/format.h:1814:8: note: 'sign' declared here
 auto sign = fspecs.sign;
  ^
   fmt/fmt/include/fmt/core.h:2741:10: note: bit-field is declared here
 sign_t sign : 8;




Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D131202/new/

https://reviews.llvm.org/D131202

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D131202: [Clang] Fix capture of values initialized by bitfields

2022-08-04 Thread Corentin Jabot via Phabricator via cfe-commits
This revision was not accepted when it landed; it landed in state "Needs 
Review".
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGa1a71b7dc97b: [Clang] Fix capture of values initialized by 
bitfields (authored by cor3ntin).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D131202/new/

https://reviews.llvm.org/D131202

Files:
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaExpr.cpp
  clang/test/SemaCXX/cxx1z-decomposition.cpp


Index: clang/test/SemaCXX/cxx1z-decomposition.cpp
===
--- clang/test/SemaCXX/cxx1z-decomposition.cpp
+++ clang/test/SemaCXX/cxx1z-decomposition.cpp
@@ -84,9 +84,9 @@
 
   (void)[outerbit1]{}; // expected-error {{'outerbit1' cannot be captured 
because it does not have automatic storage duration}}
 
-  auto [bit, var] = S2{1, 1}; // expected-note 4{{'bit' declared here}}
+  auto [bit, var] = S2{1, 1}; // expected-note 2{{'bit' declared here}}
 
-  (void)[] { // expected-error {{cannot capture a bit-field by reference}} 
\
+  (void)[] { // expected-error {{non-const reference cannot bind to 
bit-field 'a'}} \
 // expected-warning {{C++20}}
 return bit;
   };
@@ -96,7 +96,7 @@
   };
 
   (void)[&] { return bit + u; } // expected-error {{unnamed variable cannot be 
implicitly captured in a lambda expression}} \
-// expected-error {{cannot capture a bit-field 
by reference}} \
+// expected-error {{non-const reference cannot 
bind to bit-field 'a'}} \
 // expected-warning {{C++20}}
   ();
 }
Index: clang/lib/Sema/SemaExpr.cpp
===
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -18533,32 +18533,8 @@
   } else {
 ByRef = (LSI->ImpCaptureStyle == LambdaScopeInfo::ImpCap_LambdaByref);
   }
-  // C++20 : [expr.prim.lambda.capture]p12
-  // A bit-field or a member of an anonymous union shall
-  // not be captured by reference.
-  MemberExpr *ME = nullptr;
-  BindingDecl *BD = nullptr;
-  if (auto *V = dyn_cast(Var)) {
-if (V->getInit())
-  ME = dyn_cast(V->getInit()->IgnoreImplicit());
-  } else if ((BD = dyn_cast(Var))) {
-ME = dyn_cast_or_null(BD->getBinding());
-  }
-
-  // Capturing a bitfield by reference is not allowed except in OpenMP.
-  if (ByRef && ME &&
-  (isa(Var) || !S.LangOpts.OpenMP ||
-   !S.isOpenMPCapturedDecl(Var))) {
-const auto *FD = dyn_cast_or_null(ME->getMemberDecl());
-if (FD && FD->isBitField()) {
-  if (BuildAndDiagnose) {
-S.Diag(Loc, diag::err_bitfield_capture_by_ref) << Var;
-S.Diag(Var->getLocation(), diag::note_entity_declared_at) << Var;
-S.Diag(FD->getLocation(), diag::note_bitfield_decl) << FD;
-  }
-  Invalid = true;
-}
-  }
+
+  BindingDecl *BD = dyn_cast(Var);
   // FIXME: We should support capturing structured bindings in OpenMP.
   if (!Invalid && BD && S.LangOpts.OpenMP) {
 if (BuildAndDiagnose) {
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -9013,8 +9013,6 @@
 def err_reference_to_local_in_enclosing_context : Error<
   "reference to local %select{variable|binding}1 %0 declared in enclosing "
   "%select{%3|block literal|lambda expression|context}2">;
-def err_bitfield_capture_by_ref : Error<
-  "cannot capture a bit-field by reference">;
 def err_capture_binding_openmp : Error<
   "capturing a structured binding is not yet supported in OpenMP">;
 def ext_capture_binding : ExtWarn<


Index: clang/test/SemaCXX/cxx1z-decomposition.cpp
===
--- clang/test/SemaCXX/cxx1z-decomposition.cpp
+++ clang/test/SemaCXX/cxx1z-decomposition.cpp
@@ -84,9 +84,9 @@
 
   (void)[outerbit1]{}; // expected-error {{'outerbit1' cannot be captured because it does not have automatic storage duration}}
 
-  auto [bit, var] = S2{1, 1}; // expected-note 4{{'bit' declared here}}
+  auto [bit, var] = S2{1, 1}; // expected-note 2{{'bit' declared here}}
 
-  (void)[] { // expected-error {{cannot capture a bit-field by reference}} \
+  (void)[] { // expected-error {{non-const reference cannot bind to bit-field 'a'}} \
 // expected-warning {{C++20}}
 return bit;
   };
@@ -96,7 +96,7 @@
   };
 
   (void)[&] { return bit + u; } // expected-error {{unnamed variable cannot be implicitly captured in a lambda expression}} \
-// expected-error {{cannot capture a bit-field by reference}} \
+// expected-error {{non-const reference cannot bind to bit-field 

[PATCH] D131202: [Clang] Fix capture of values initialized by bitfields

2022-08-04 Thread Corentin Jabot via Phabricator via cfe-commits
cor3ntin updated this revision to Diff 450138.
cor3ntin added a comment.
Herald added a reviewer: jdoerfert.
Herald added a subscriber: sstefan1.

Better approach: revert the whole thing.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D131202/new/

https://reviews.llvm.org/D131202

Files:
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaExpr.cpp
  clang/test/SemaCXX/cxx1z-decomposition.cpp


Index: clang/test/SemaCXX/cxx1z-decomposition.cpp
===
--- clang/test/SemaCXX/cxx1z-decomposition.cpp
+++ clang/test/SemaCXX/cxx1z-decomposition.cpp
@@ -84,9 +84,9 @@
 
   (void)[outerbit1]{}; // expected-error {{'outerbit1' cannot be captured 
because it does not have automatic storage duration}}
 
-  auto [bit, var] = S2{1, 1}; // expected-note 4{{'bit' declared here}}
+  auto [bit, var] = S2{1, 1}; // expected-note 2{{'bit' declared here}}
 
-  (void)[] { // expected-error {{cannot capture a bit-field by reference}} 
\
+  (void)[] { // expected-error {{non-const reference cannot bind to 
bit-field 'a'}} \
 // expected-warning {{C++20}}
 return bit;
   };
@@ -96,7 +96,7 @@
   };
 
   (void)[&] { return bit + u; } // expected-error {{unnamed variable cannot be 
implicitly captured in a lambda expression}} \
-// expected-error {{cannot capture a bit-field 
by reference}} \
+// expected-error {{non-const reference cannot 
bind to bit-field 'a'}} \
 // expected-warning {{C++20}}
   ();
 }
Index: clang/lib/Sema/SemaExpr.cpp
===
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -18533,32 +18533,8 @@
   } else {
 ByRef = (LSI->ImpCaptureStyle == LambdaScopeInfo::ImpCap_LambdaByref);
   }
-  // C++20 : [expr.prim.lambda.capture]p12
-  // A bit-field or a member of an anonymous union shall
-  // not be captured by reference.
-  MemberExpr *ME = nullptr;
-  BindingDecl *BD = nullptr;
-  if (auto *V = dyn_cast(Var)) {
-if (V->getInit())
-  ME = dyn_cast(V->getInit()->IgnoreImplicit());
-  } else if ((BD = dyn_cast(Var))) {
-ME = dyn_cast_or_null(BD->getBinding());
-  }
-
-  // Capturing a bitfield by reference is not allowed except in OpenMP.
-  if (ByRef && ME &&
-  (isa(Var) || !S.LangOpts.OpenMP ||
-   !S.isOpenMPCapturedDecl(Var))) {
-const auto *FD = dyn_cast_or_null(ME->getMemberDecl());
-if (FD && FD->isBitField()) {
-  if (BuildAndDiagnose) {
-S.Diag(Loc, diag::err_bitfield_capture_by_ref) << Var;
-S.Diag(Var->getLocation(), diag::note_entity_declared_at) << Var;
-S.Diag(FD->getLocation(), diag::note_bitfield_decl) << FD;
-  }
-  Invalid = true;
-}
-  }
+
+  BindingDecl *BD = dyn_cast(Var);
   // FIXME: We should support capturing structured bindings in OpenMP.
   if (!Invalid && BD && S.LangOpts.OpenMP) {
 if (BuildAndDiagnose) {
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -9013,8 +9013,6 @@
 def err_reference_to_local_in_enclosing_context : Error<
   "reference to local %select{variable|binding}1 %0 declared in enclosing "
   "%select{%3|block literal|lambda expression|context}2">;
-def err_bitfield_capture_by_ref : Error<
-  "cannot capture a bit-field by reference">;
 def err_capture_binding_openmp : Error<
   "capturing a structured binding is not yet supported in OpenMP">;
 def ext_capture_binding : ExtWarn<


Index: clang/test/SemaCXX/cxx1z-decomposition.cpp
===
--- clang/test/SemaCXX/cxx1z-decomposition.cpp
+++ clang/test/SemaCXX/cxx1z-decomposition.cpp
@@ -84,9 +84,9 @@
 
   (void)[outerbit1]{}; // expected-error {{'outerbit1' cannot be captured because it does not have automatic storage duration}}
 
-  auto [bit, var] = S2{1, 1}; // expected-note 4{{'bit' declared here}}
+  auto [bit, var] = S2{1, 1}; // expected-note 2{{'bit' declared here}}
 
-  (void)[] { // expected-error {{cannot capture a bit-field by reference}} \
+  (void)[] { // expected-error {{non-const reference cannot bind to bit-field 'a'}} \
 // expected-warning {{C++20}}
 return bit;
   };
@@ -96,7 +96,7 @@
   };
 
   (void)[&] { return bit + u; } // expected-error {{unnamed variable cannot be implicitly captured in a lambda expression}} \
-// expected-error {{cannot capture a bit-field by reference}} \
+// expected-error {{non-const reference cannot bind to bit-field 'a'}} \
 // expected-warning {{C++20}}
   ();
 }
Index: clang/lib/Sema/SemaExpr.cpp

[PATCH] D131202: [Clang] Fix capture of values initialized by bitfields

2022-08-04 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added inline comments.



Comment at: clang/test/SemaCXX/lambda-expressions.cpp:680
+  auto l = [&]() {
+  a;   // expected-error{{cannot capture a bit-field by reference}}
+  b;

This still should be accepted -- `a` is an `const int &` that was materialized 
from a temporary and lifetime extended, so it's not a bit-field.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D131202/new/

https://reviews.llvm.org/D131202

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D131202: [Clang] Fix capture of values initialized by bitfields

2022-08-04 Thread Corentin Jabot via Phabricator via cfe-commits
cor3ntin created this revision.
Herald added a project: All.
cor3ntin requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This fixes a regression introduced in 127bf44 



Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131202

Files:
  clang/lib/Sema/SemaExpr.cpp
  clang/test/SemaCXX/lambda-expressions.cpp


Index: clang/test/SemaCXX/lambda-expressions.cpp
===
--- clang/test/SemaCXX/lambda-expressions.cpp
+++ clang/test/SemaCXX/lambda-expressions.cpp
@@ -665,3 +665,21 @@
 // expected-note@-2 2 {{default capture by}}
 }
 };
+
+
+namespace bitfields {
+struct S {
+  int s : 4; // expected-note {{bit-field is declared here}}
+};
+
+void f() {
+  S s;
+  const int & a = s.s; // expected-note{{'a' declared here}}
+  int b = s.s;
+  auto l = [&]() {
+  a;   // expected-error{{cannot capture a bit-field by reference}}
+  b;
+  s.s;
+  };
+}
+}
Index: clang/lib/Sema/SemaExpr.cpp
===
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -18539,7 +18539,7 @@
   MemberExpr *ME = nullptr;
   BindingDecl *BD = nullptr;
   if (auto *V = dyn_cast(Var)) {
-if (V->getInit())
+if (V->getInit() && V->getType()->isReferenceType())
   ME = dyn_cast(V->getInit()->IgnoreImplicit());
   } else if ((BD = dyn_cast(Var))) {
 ME = dyn_cast_or_null(BD->getBinding());


Index: clang/test/SemaCXX/lambda-expressions.cpp
===
--- clang/test/SemaCXX/lambda-expressions.cpp
+++ clang/test/SemaCXX/lambda-expressions.cpp
@@ -665,3 +665,21 @@
 // expected-note@-2 2 {{default capture by}}
 }
 };
+
+
+namespace bitfields {
+struct S {
+  int s : 4; // expected-note {{bit-field is declared here}}
+};
+
+void f() {
+  S s;
+  const int & a = s.s; // expected-note{{'a' declared here}}
+  int b = s.s;
+  auto l = [&]() {
+  a;   // expected-error{{cannot capture a bit-field by reference}}
+  b;
+  s.s;
+  };
+}
+}
Index: clang/lib/Sema/SemaExpr.cpp
===
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -18539,7 +18539,7 @@
   MemberExpr *ME = nullptr;
   BindingDecl *BD = nullptr;
   if (auto *V = dyn_cast(Var)) {
-if (V->getInit())
+if (V->getInit() && V->getType()->isReferenceType())
   ME = dyn_cast(V->getInit()->IgnoreImplicit());
   } else if ((BD = dyn_cast(Var))) {
 ME = dyn_cast_or_null(BD->getBinding());
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits