[clang] [clang] Improve diagnostics with incompatible VLA types (PR #101261)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 28 11:53:47 PDT 2024


================
@@ -1099,37 +1099,49 @@ 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
       TDT.PrintTree = false;
+      SmallString<64> FromTypeStr, ToTypeStr;
+
+      // Get first type text
       TDT.PrintFromType = true;
       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.emplace_back(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.emplace_back(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) {
----------------
AaronBallman wrote:

One thing that makes me a bit uncomfortable is that this presumes that identical type strings mean the type must be a VLA, so I was thinking "we should look at the types to make sure they're actually VLA types" and that got me hunting around and I think we may want to move this logic now (sorry for not noticing this earlier!): https://github.com/llvm/llvm-project/blob/41b55071a13374654a290c01224eb066c38dc87a/clang/lib/AST/ASTDiagnostic.cpp#L402

We can't inspect the `QualType` here in Diagnostic.cpp because of layering violations, but we can inspect it from ASTDiagnostic.cpp because that's at the correct library layer.

https://github.com/llvm/llvm-project/pull/101261


More information about the cfe-commits mailing list