serge-sans-paille created this revision. serge-sans-paille added reviewers: dim, Fznamznon. Herald added a project: All. serge-sans-paille requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
Fix #63007 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D151753 Files: clang/lib/Sema/SemaChecking.cpp clang/test/SemaCXX/warn-cast-align.cpp Index: clang/test/SemaCXX/warn-cast-align.cpp =================================================================== --- clang/test/SemaCXX/warn-cast-align.cpp +++ clang/test/SemaCXX/warn-cast-align.cpp @@ -44,6 +44,11 @@ c = IntPtr(P); } +template <class A> void DependentAlign() { + alignas(A) int lut[]{}; + (long *)lut; // expected-warning {{cast from 'int *' to 'long *'}} +} + struct __attribute__((aligned(16))) AlignedS { char m[16]; }; Index: clang/lib/Sema/SemaChecking.cpp =================================================================== --- clang/lib/Sema/SemaChecking.cpp +++ clang/lib/Sema/SemaChecking.cpp @@ -16158,8 +16158,13 @@ if (auto *VD = dyn_cast<VarDecl>(cast<DeclRefExpr>(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 (any_of(VD->specific_attrs<AlignedAttr>(), + [](auto *A) { return A->isAlignmentDependent(); })) + break; return std::make_pair(Ctx.getDeclAlign(VD), CharUnits::Zero()); + } if (VD->hasInit()) return getBaseAlignmentAndOffsetFromLValue(VD->getInit(), Ctx); }
Index: clang/test/SemaCXX/warn-cast-align.cpp =================================================================== --- clang/test/SemaCXX/warn-cast-align.cpp +++ clang/test/SemaCXX/warn-cast-align.cpp @@ -44,6 +44,11 @@ c = IntPtr(P); } +template <class A> void DependentAlign() { + alignas(A) int lut[]{}; + (long *)lut; // expected-warning {{cast from 'int *' to 'long *'}} +} + struct __attribute__((aligned(16))) AlignedS { char m[16]; }; Index: clang/lib/Sema/SemaChecking.cpp =================================================================== --- clang/lib/Sema/SemaChecking.cpp +++ clang/lib/Sema/SemaChecking.cpp @@ -16158,8 +16158,13 @@ if (auto *VD = dyn_cast<VarDecl>(cast<DeclRefExpr>(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 (any_of(VD->specific_attrs<AlignedAttr>(), + [](auto *A) { return A->isAlignmentDependent(); })) + break; return std::make_pair(Ctx.getDeclAlign(VD), CharUnits::Zero()); + } if (VD->hasInit()) return getBaseAlignmentAndOffsetFromLValue(VD->getInit(), Ctx); }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits