[clang] [Clang] Fix ICE in SemaOpenMP with structured binding (PR #104822)

Yuxuan Chen via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 19 10:57:19 PDT 2024


https://github.com/yuxuanchen1997 created https://github.com/llvm/llvm-project/pull/104822

Fixes https://github.com/llvm/llvm-project/issues/104810.

Clang currently crashes on the following program:
```
struct S {
  int i;
};

auto [a] = S{1};

void foo() {
    a;
}
```
when `-fopenmp` is enabled. 

Remove the bad pattern matching and just use the virtual overrides for `Decl::getCanonicalDecl()` instead. 

>From 122b5086a1f0ca4f97ad46b660d4932f686f6eef Mon Sep 17 00:00:00 2001
From: Yuxuan Chen <ych at meta.com>
Date: Mon, 19 Aug 2024 10:54:36 -0700
Subject: [PATCH] [Clang] Fix ICE in SemaOpenMP with structured binding

---
 clang/lib/Sema/SemaOpenMP.cpp      | 12 ++----------
 clang/test/SemaOpenMP/gh104810.cpp | 12 ++++++++++++
 2 files changed, 14 insertions(+), 10 deletions(-)
 create mode 100644 clang/test/SemaOpenMP/gh104810.cpp

diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index c1442b8a58eda4..74c646f64b42f2 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -1256,16 +1256,8 @@ static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
   if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
     if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
       D = ME->getMemberDecl();
-  const auto *VD = dyn_cast<VarDecl>(D);
-  const auto *FD = dyn_cast<FieldDecl>(D);
-  if (VD != nullptr) {
-    VD = VD->getCanonicalDecl();
-    D = VD;
-  } else {
-    assert(FD);
-    FD = FD->getCanonicalDecl();
-    D = FD;
-  }
+
+  D = cast<ValueDecl>(D->getCanonicalDecl());
   return D;
 }
 
diff --git a/clang/test/SemaOpenMP/gh104810.cpp b/clang/test/SemaOpenMP/gh104810.cpp
new file mode 100644
index 00000000000000..92640893197760
--- /dev/null
+++ b/clang/test/SemaOpenMP/gh104810.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fopenmp -fsyntax-only %s
+
+// expected-no-diagnostics
+struct S {
+  int i;
+};
+
+auto [a] = S{1};
+
+void foo() {
+    a;
+}



More information about the cfe-commits mailing list