[clang] [clang] Improve diagnostics with incompatible VLA types (PR #101261)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 31 10:20:08 PDT 2024
================
@@ -16644,7 +16644,32 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
if (Action == AA_Passing_CFAudited) {
DiagKind = diag::err_arc_typecheck_convert_incompatible_pointer;
} else if (getLangOpts().CPlusPlus) {
- DiagKind = diag::err_typecheck_convert_incompatible_pointer;
+ DiagKind = [this, &SrcType, &DstType] {
+ const VariableArrayType *SrcTypeVLA = nullptr, *DstTypeVLA = nullptr;
+ if (const PointerType *P = SrcType->getAs<PointerType>())
+ SrcTypeVLA = Context.getAsVariableArrayType(P->getPointeeType());
+ if (const PointerType *P = DstType->getAs<PointerType>())
+ DstTypeVLA = Context.getAsVariableArrayType(P->getPointeeType());
+
+ if (SrcTypeVLA == nullptr || DstTypeVLA == nullptr)
+ return diag::err_typecheck_convert_incompatible_pointer;
+
+ DeclRefExpr *SrcSizeExpr = nullptr, *DstSizeExpr = nullptr;
+ if (ImplicitCastExpr *I =
+ dyn_cast<ImplicitCastExpr>(SrcTypeVLA->getSizeExpr()))
+ SrcSizeExpr = dyn_cast<DeclRefExpr>(I->getSubExpr());
+ if (ImplicitCastExpr *I =
+ dyn_cast<ImplicitCastExpr>(DstTypeVLA->getSizeExpr()))
+ DstSizeExpr = dyn_cast<DeclRefExpr>(I->getSubExpr());
----------------
AaronBallman wrote:
For this, I would do:
```
auto GetSizeDeclRef = [](const VariableArrayType *VAT) -> const DeclRefExpr * {
if (const Expr *Size = VAT->getSizeExpr())
return dyn_cast<DeclRefExpr>(Size->IgnoreParenImpCasts())
return nullptr;
};
DeclRefExpr *SrcSizeExpr = GetSizeDeclRef(SrcTypeVLA),
*DestSizeExpr = GetSizeDeclRef(DestTypeVLA);
```
This will ignore more than just implicit cast expressions, but other types of implicit AST nodes (things like parentheses expressions, etc), so it will catch more situations.
https://github.com/llvm/llvm-project/pull/101261
More information about the cfe-commits
mailing list