[clang] [clang] Improve diagnostics with incompatible VLA types (PR #101261)
Andrew Sukach via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 9 08:07:36 PDT 2024
https://github.com/sookach updated https://github.com/llvm/llvm-project/pull/101261
>From 54e4912ebcee4a54b05367f52a0e83b36a792c81 Mon Sep 17 00:00:00 2001
From: Andrew Sukach <andrewsukach at gmail.com>
Date: Tue, 30 Jul 2024 19:31:41 -0400
Subject: [PATCH] [clang] Improve diagnostics with incompatible VLA types
---
clang/lib/AST/ASTDiagnostic.cpp | 20 +++++++++++++++++++
.../test/Sema/incompatible-vla-assignment.cpp | 13 ++++++++++++
2 files changed, 33 insertions(+)
create mode 100644 clang/test/Sema/incompatible-vla-assignment.cpp
diff --git a/clang/lib/AST/ASTDiagnostic.cpp b/clang/lib/AST/ASTDiagnostic.cpp
index 0680ff5e3a3851..9fcae664960054 100644
--- a/clang/lib/AST/ASTDiagnostic.cpp
+++ b/clang/lib/AST/ASTDiagnostic.cpp
@@ -424,6 +424,26 @@ void clang::FormatASTNodeDiagnosticArgument(
Val = TDT.PrintFromType ? TDT.FromType : TDT.ToType;
Modifier = StringRef();
Argument = StringRef();
+
+ if (FromType->isVariableArrayType() && ToType->isVariableArrayType() &&
+ ConvertTypeToDiagnosticString(Context, FromType, PrevArgs,
+ QualTypeVals) ==
+ ConvertTypeToDiagnosticString(Context, ToType, PrevArgs,
+ QualTypeVals)) {
+ assert(Modifier.empty() && Argument.empty() &&
+ "Invalid modifier for QualType argument");
+
+ QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void *>(Val)));
+ OS << ConvertTypeToDiagnosticString(Context, Ty, PrevArgs,
+ QualTypeVals);
+ NeedQuotes = false;
+
+ if (!TDT.PrintFromType)
+ OS << "; VLA types differ despite using the same array size "
+ "expression";
+
+ break;
+ }
// Fall through
[[fallthrough]];
}
diff --git a/clang/test/Sema/incompatible-vla-assignment.cpp b/clang/test/Sema/incompatible-vla-assignment.cpp
new file mode 100644
index 00000000000000..af97ec554d02c8
--- /dev/null
+++ b/clang/test/Sema/incompatible-vla-assignment.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+void func(int n) {
+ int grp[n][n];
+ int (*ptr)[n];
+
+ for (int i = 0; i < n; i++)
+ ptr = &grp[i]; // expected-error {{incompatible pointer types assigning to 'int (*)[n]' from 'int (*)[n]'; VLA types differ despite using the same array size expression}}
+}
+
+void func(int n, int (&array)[n]) {
+ int (&other)[n] = array; // expected-error {{non-const lvalue reference to type 'int[n]' cannot bind to a value of unrelated type 'int[n]'; VLA types differ despite using the same array size expression}}
+}
More information about the cfe-commits
mailing list