https://github.com/adityak2006 updated 
https://github.com/llvm/llvm-project/pull/167224

>From f24e5b8d0c9c9a3cea91d1671bf956e0a9285176 Mon Sep 17 00:00:00 2001
From: Aditya <[email protected]>
Date: Sun, 9 Nov 2025 14:49:24 +0000
Subject: [PATCH] [Clang][Diagnostics] Simplify 'decltype(T{})' to 'T' in
 diagnostics

Clang's type printer would verbosely print `decltype(T{})` in diagnostics,
even though this is equivalent to just `T`. This made error messages
unnecessarily long and harder to read.

This commit modifies `TypePrinter::printDecltypeBefore` to detect this
specific pattern. It checks if the underlying expression of a
`DecltypeType` is a default `CXXConstructExpr` (i.e., `T{}`) and, if so,
prints the constructor's type (`T`) directly instead of the full
`decltype`.

Fixes #96638.
---
 clang/lib/AST/TypePrinter.cpp                 | 13 ++++++++++
 .../SemaCXX/decltype-diagnostic-print.cpp     | 25 +++++++++++++++++++
 2 files changed, 38 insertions(+)
 create mode 100644 clang/test/SemaCXX/decltype-diagnostic-print.cpp

diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp
index c18b2eafc722c..510923229ea11 100644
--- a/clang/lib/AST/TypePrinter.cpp
+++ b/clang/lib/AST/TypePrinter.cpp
@@ -18,6 +18,7 @@
 #include "clang/AST/DeclObjC.h"
 #include "clang/AST/DeclTemplate.h"
 #include "clang/AST/Expr.h"
+#include "clang/AST/ExprCXX.h"
 #include "clang/AST/NestedNameSpecifier.h"
 #include "clang/AST/PrettyPrinter.h"
 #include "clang/AST/TemplateBase.h"
@@ -1321,6 +1322,18 @@ void TypePrinter::printTypeOfBefore(const TypeOfType *T, 
raw_ostream &OS) {
 void TypePrinter::printTypeOfAfter(const TypeOfType *T, raw_ostream &OS) {}
 
 void TypePrinter::printDecltypeBefore(const DecltypeType *T, raw_ostream &OS) {
+  // Check for the 'decltype(T{})' pattern and simplify it to 'T'.
+  if (const Expr *E = T->getUnderlyingExpr()) {
+    E = E->IgnoreParenImpCasts();
+    if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) {
+      // Check if it's a default constructor (T{}) and not a list init (T{...})
+      if (CE->getNumArgs() == 0 && !CE->isListInitialization()) {
+        CE->getType().print(OS, Policy);
+        spaceBeforePlaceHolder(OS);
+        return; // Skip the default 'decltype(...)' printing
+      }
+    }
+  }
   OS << "decltype(";
   if (const Expr *E = T->getUnderlyingExpr()) {
     PrintingPolicy ExprPolicy = Policy;
diff --git a/clang/test/SemaCXX/decltype-diagnostic-print.cpp 
b/clang/test/SemaCXX/decltype-diagnostic-print.cpp
new file mode 100644
index 0000000000000..c4b075ef4ea33
--- /dev/null
+++ b/clang/test/SemaCXX/decltype-diagnostic-print.cpp
@@ -0,0 +1,25 @@
+// clang/test/SemaCXX/decltype-diagnostic-print.cpp
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
+
+template <typename T>
+struct TestAssert {
+  // This static_assert will fail, forcing the compiler to print the name
+  // of the template instantiation in its diagnostic "note".
+  static_assert(sizeof(T) == 0, "Static assert to check type printing");
+};
+
+struct MySimpleType {};
+struct MyOtherType {};
+
+void test() {
+  // This will fail the static_assert.
+  TestAssert<decltype(MySimpleType{})> test1;
+  // expected-error@6 {{Static assert to check type printing}}
+  // expected-note@16 {{in instantiation of template class 
'TestAssert<MySimpleType>' requested here}}
+  // CHECK-NOT: decltype(MySimpleType{})
+
+  TestAssert<decltype(MyOtherType{})> test2;
+  // expected-error@6 {{Static assert to check type printing}}
+  // expected-note@22 {{in instantiation of template class 
'TestAssert<MyOtherType>' requested here}}
+  // CHECK-NOT: decltype(MyOtherType{})
+}
\ No newline at end of file

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to