[Clang] Fix ICE in SemaOpenMP with structured binding (#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. 

Because `a` is neither `VarDecl` nor `FieldDecl`. It's a `BindingDecl`
that's not handled in `SemaOpenMP.cpp`'s `getCanonicalDecl`. It appears
to me that this pattern matching is merely just for a refined return
type of the overrides. It can also be achieved with just using the
virtual `Decl::getCanonicalDecl()` instead. Do the final casting should
be safe for `ValueDecl`s.
This commit is contained in:
Yuxuan Chen 2024-08-19 14:06:10 -07:00 committed by GitHub
parent ff2e619dfc
commit e732d1ce86
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 10 deletions

View File

@ -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;
}

View File

@ -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;
}