From dc6f5c9b588adfe62449a898ebd06a5a09c05439 Mon Sep 17 00:00:00 2001 From: serge-sans-paille Date: Tue, 30 May 2023 23:15:11 +0200 Subject: [PATCH] [Clang][Sema] Do not try to analyze dependent alignment during -Wcast-align Fix #63007 Differential Revision: https://reviews.llvm.org/D151753 --- clang/docs/ReleaseNotes.rst | 1 + clang/lib/Sema/SemaChecking.cpp | 6 +++++- clang/test/SemaCXX/warn-cast-align.cpp | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index c022e9ffb66049..bc6366b36fd4dd 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -370,6 +370,7 @@ Improvements to Clang's diagnostics - Clang now diagnoses unexpected tokens after a ``#pragma clang|GCC diagnostic push|pop`` directive. (`#13920: `_) +- Clang now does not try to analyze cast validity on variables with dependent alignment (`#63007: `_). Bug Fixes in This Version ------------------------- diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 6969cf147ffbd2..ffda87ac87d7a2 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -16351,8 +16351,12 @@ std::optional(cast(E)->getDecl())) { // FIXME: If VD is captured by copy or is an escaping __block variable, // use the alignment of VD's type. - if (!VD->getType()->isReferenceType()) + if (!VD->getType()->isReferenceType()) { + // Dependent alignment cannot be resolved -> bail out. + if (VD->hasDependentAlignment()) + break; return std::make_pair(Ctx.getDeclAlign(VD), CharUnits::Zero()); + } if (VD->hasInit()) return getBaseAlignmentAndOffsetFromLValue(VD->getInit(), Ctx); } diff --git a/clang/test/SemaCXX/warn-cast-align.cpp b/clang/test/SemaCXX/warn-cast-align.cpp index 1e84ba9cd67af4..855fac4dffc250 100644 --- a/clang/test/SemaCXX/warn-cast-align.cpp +++ b/clang/test/SemaCXX/warn-cast-align.cpp @@ -44,6 +44,11 @@ void test1(void *P) { c = IntPtr(P); } +template void DependentAlign() { + alignas(A) int lut[]{}; + (long *)lut; // expected-warning {{cast from 'int *' to 'long *'}} +} + struct __attribute__((aligned(16))) AlignedS { char m[16]; };