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(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 +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 test1; + // expected-error@6 {{Static assert to check type printing}} + // expected-note@16 {{in instantiation of template class 'TestAssert' requested here}} + // CHECK-NOT: decltype(MySimpleType{}) + + TestAssert test2; + // expected-error@6 {{Static assert to check type printing}} + // expected-note@22 {{in instantiation of template class 'TestAssert' requested here}} + // CHECK-NOT: decltype(MyOtherType{}) +} \ No newline at end of file