Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions clang/lib/AST/TypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions clang/test/SemaCXX/decltype-diagnostic-print.cpp
Original file line number Diff line number Diff line change
@@ -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{})
}