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

Corentin Jabot via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 4 14:05:14 PDT 2022


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 <https://reviews.llvm.org/rG127bf44385424891eb04cff8e52d3f157fc2cb7c>


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 at -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<VarDecl>(Var)) {
-    if (V->getInit())
+    if (V->getInit() && V->getType()->isReferenceType())
       ME = dyn_cast<MemberExpr>(V->getInit()->IgnoreImplicit());
   } else if ((BD = dyn_cast<BindingDecl>(Var))) {
     ME = dyn_cast_or_null<MemberExpr>(BD->getBinding());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131202.450128.patch
Type: text/x-patch
Size: 1196 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220804/0c284762/attachment.bin>


More information about the cfe-commits mailing list