https://github.com/sookach updated https://github.com/llvm/llvm-project/pull/101261
>From 1de106f82720ea6c470ce6c974c19f966599b9cc Mon Sep 17 00:00:00 2001 From: Andrew Sukach <andrewsuk...@gmail.com> Date: Tue, 30 Jul 2024 19:31:41 -0400 Subject: [PATCH] [clang] Improve diagnostics with incompatible VLA types --- clang/lib/Basic/Diagnostic.cpp | 45 ++++++++++++------- .../test/Sema/incompatible-vla-assignment.cpp | 9 ++++ 2 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 clang/test/Sema/incompatible-vla-assignment.cpp diff --git a/clang/lib/Basic/Diagnostic.cpp b/clang/lib/Basic/Diagnostic.cpp index 66776daa5e1493..9e1d2f996147a9 100644 --- a/clang/lib/Basic/Diagnostic.cpp +++ b/clang/lib/Basic/Diagnostic.cpp @@ -1099,37 +1099,50 @@ FormatDiagnostic(const char *DiagStr, const char *DiagEnd, const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$'); const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$'); - // Append before text - FormatDiagnostic(Argument, FirstDollar, OutStr); - - // Append first type + // Get first type text TDT.PrintTree = false; TDT.PrintFromType = true; + SmallString<64> FromTypeStr, ToTypeStr; getDiags()->ConvertArgToString(Kind, val, StringRef(Modifier, ModifierLen), StringRef(Argument, ArgumentLen), - FormattedArgs, - OutStr, QualTypeVals); + FormattedArgs, FromTypeStr, QualTypeVals); if (!TDT.TemplateDiffUsed) - FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype, - TDT.FromType)); + FormattedArgs.push_back( + std::make_pair(DiagnosticsEngine::ak_qualtype, TDT.FromType)); - // Append middle text - FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr); - - // Append second type + // Get second type text TDT.PrintFromType = false; getDiags()->ConvertArgToString(Kind, val, StringRef(Modifier, ModifierLen), StringRef(Argument, ArgumentLen), - FormattedArgs, - OutStr, QualTypeVals); + FormattedArgs, ToTypeStr, QualTypeVals); if (!TDT.TemplateDiffUsed) - FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype, - TDT.ToType)); + FormattedArgs.push_back( + std::make_pair(DiagnosticsEngine::ak_qualtype, TDT.ToType)); + + // Append before text + FormatDiagnostic(Argument, FirstDollar, OutStr); + + // Append first type + OutStr.append(FromTypeStr); + + // Append middle text + FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr); + + // Append second type + OutStr.append(ToTypeStr); // Append end text FormatDiagnostic(SecondDollar + 1, Pipe, OutStr); + + if (FromTypeStr == ToTypeStr) { + SmallString<88> IncompatibleVLADiag( + "; consider using a typedef to use the same variable-length array " + "type for both operands"); + OutStr.append(IncompatibleVLADiag); + } + break; } } diff --git a/clang/test/Sema/incompatible-vla-assignment.cpp b/clang/test/Sema/incompatible-vla-assignment.cpp new file mode 100644 index 00000000000000..8be063631b7a8a --- /dev/null +++ b/clang/test/Sema/incompatible-vla-assignment.cpp @@ -0,0 +1,9 @@ +// 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]'; consider using a typedef to use the same variable-length array type for both operands}} +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits