[clang] [clang] Improve diagnostics with incompatible VLA types (PR #101261)
Andrew Sukach via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 12 20:29:27 PDT 2024
https://github.com/sookach updated https://github.com/llvm/llvm-project/pull/101261
>From 1b598a5f3b158ec231b96281e4e0edc6fa819389 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 | 21 +++++++++++++++++++
.../test/Sema/incompatible-vla-assignment.cpp | 13 ++++++++++++
2 files changed, 34 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..367378a0d3df25 100644
--- a/clang/lib/AST/ASTDiagnostic.cpp
+++ b/clang/lib/AST/ASTDiagnostic.cpp
@@ -424,6 +424,27 @@ void clang::FormatASTNodeDiagnosticArgument(
Val = TDT.PrintFromType ? TDT.FromType : TDT.ToType;
Modifier = StringRef();
Argument = StringRef();
+
+ if (FromType->isVariablyModifiedType() &&
+ ToType->isVariablyModifiedType() &&
+ 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