diff --git a/clang/include/clang/AST/PrettyPrinter.h b/clang/include/clang/AST/PrettyPrinter.h --- a/clang/include/clang/AST/PrettyPrinter.h +++ b/clang/include/clang/AST/PrettyPrinter.h @@ -75,7 +75,7 @@ PrintCanonicalTypes(false), PrintInjectedClassNameWithArguments(true), UsePreferredNames(true), AlwaysIncludeTypeForTemplateArgument(false), CleanUglifiedParameters(false), EntireContentsOfLargeArray(true), - UseEnumerators(true) {} + UseEnumerators(true), AlwaysIncludeTypeForNonTypeTemplateArgument(false) {} /// Adjust this printing policy for cases where it's known that we're /// printing C++ code (for instance, if AST dumping reaches a C++-only @@ -295,6 +295,42 @@ /// enumerator name or via cast of an integer. unsigned UseEnumerators : 1; + /// Whether to always print full type names of non-type template arguments. + /// + /// \code + /// template< typename T, auto ... dims > + /// struct NDArray {}; + + /// struct Dimension + /// { + /// using value_type = unsigned short; + + /// value_type size{ value_type( 0 ) }; + /// }; + + /// template < typename ConcreteDim > + /// struct DimensionImpl : Dimension {}; + + /// struct Width : DimensionImpl< Width > {}; + /// struct Height : DimensionImpl< Height > {}; + /// struct Channels : DimensionImpl< Channels > {}; + + /// inline constexpr Width W; + /// inline constexpr Height H; + /// inline constexpr Channels C; + + /// template< auto ... Dims > + /// consteval auto makeArray() noexcept + /// { + /// return NDArray< float, Dims ... >{}; + /// } + + /// [[ maybe_unused ]] auto x { makeArray< H, W, C >() }; + /// \endcode + /// + /// decltype(x) will be printed as "NDArray{Dimension{0}}}, Width{DimensionImpl{Dimension{0}}}, Channels{DimensionImpl{Dimension{0}}}>" if enabled and as "NDArray" if disabled + unsigned AlwaysIncludeTypeForNonTypeTemplateArgument : 1; + /// Callbacks to use to allow the behavior of printing to be customized. const PrintingCallbacks *Callbacks = nullptr; }; diff --git a/clang/lib/AST/APValue.cpp b/clang/lib/AST/APValue.cpp --- a/clang/lib/AST/APValue.cpp +++ b/clang/lib/AST/APValue.cpp @@ -892,6 +892,8 @@ assert(BI != CD->bases_end()); if (!First) Out << ", "; + if (Policy.AlwaysIncludeTypeForNonTypeTemplateArgument) + BI->getType().getUnqualifiedType().print(Out, Policy); getStructBase(I).printPretty(Out, Policy, BI->getType(), Ctx); First = false; } diff --git a/clang/lib/AST/TemplateBase.cpp b/clang/lib/AST/TemplateBase.cpp --- a/clang/lib/AST/TemplateBase.cpp +++ b/clang/lib/AST/TemplateBase.cpp @@ -432,10 +432,10 @@ } case Declaration: { - // FIXME: Include the type if it's not obvious from the context. NamedDecl *ND = getAsDecl(); if (getParamTypeForDecl()->isRecordType()) { if (auto *TPO = dyn_cast(ND)) { + TPO->getType().getUnqualifiedType().print(Out, Policy); TPO->printAsInit(Out, Policy); break; } diff --git a/clang/test/CodeGenCXX/debug-info-template.cpp b/clang/test/CodeGenCXX/debug-info-template.cpp --- a/clang/test/CodeGenCXX/debug-info-template.cpp +++ b/clang/test/CodeGenCXX/debug-info-template.cpp @@ -165,7 +165,7 @@ static constexpr const ClassTemplateArg &Arg = A; }; -// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "ClassTemplateArgTemplate<{1, 2.000000e+00}>", {{.*}}, templateParams: ![[CLASS_TEMP_ARGS:[0-9]*]], +// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "ClassTemplateArgTemplate", {{.*}}, templateParams: ![[CLASS_TEMP_ARGS:[0-9]*]], // CHECK: ![[CLASS_TEMP_ARG_CONST_REF_TYPE:[0-9]*]] = !DIDerivedType(tag: DW_TAG_reference_type, baseType: ![[CLASS_TEMP_ARG_CONST_TYPE:[0-9]*]], // CHECK: ![[CLASS_TEMP_ARG_CONST_TYPE]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[CLASS_TEMP_ARG_TYPE:[0-9]*]]) // CHECK: ![[CLASS_TEMP_ARG_TYPE]] = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "ClassTemplateArg", @@ -221,20 +221,20 @@ template void f1() { } template void f1(); -// CHECK: !DISubprogram(name: "f1", +// CHECK: !DISubprogram(name: "f1", // CHECK-SAME: templateParams: ![[RAW_FUNC_QUAL_ARGS:[0-9]*]], // CHECK: ![[RAW_FUNC_QUAL_ARGS]] = !{![[RAW_FUNC_QUAL_T1:[0-9]*]], ![[RAW_FUNC_QUAL_T2:[0-9]*]], ![[RAW_FUNC_QUAL_T3:[0-9]*]], ![[RAW_FUNC_QUAL_T4:[0-9]*]]} -// CHECK: ![[RAW_FUNC_QUAL_T1]] = !DITemplateTypeParameter(name: "T1", type: ![[RAW_FUNC_QUAL_VOL:[0-9]*]]) +// CHECK: ![[RAW_FUNC_QUAL_T1]] = !DITemplateTypeParameter(name: "T1", type: ![[RAW_FUNC_QUAL_VOL:[0-9]*]]) // CHECK: ![[RAW_FUNC_QUAL_VOL]] = !DIDerivedType(tag: DW_TAG_volatile_type, baseType: ![[RAW_FUNC_QUAL_TYPE:[0-9]*]]) // CHECK: ![[RAW_FUNC_QUAL_TYPE]] = !DISubroutineType(types: ![[RAW_FUNC_QUAL_LIST:[0-9]*]] // CHECK: ![[RAW_FUNC_QUAL_LIST]] = !{![[RAW_FUNC_QUAL_STRUCT:[0-9]*]]} // CHECK: ![[RAW_FUNC_QUAL_STRUCT]] = !DICompositeType(tag: DW_TAG_structure_type, name: "t1" -// CHECK: ![[RAW_FUNC_QUAL_T2]] = !DITemplateTypeParameter(name: "T2", type: ![[RAW_FUNC_QUAL_CNST:[0-9]*]]) +// CHECK: ![[RAW_FUNC_QUAL_T2]] = !DITemplateTypeParameter(name: "T2", type: ![[RAW_FUNC_QUAL_CNST:[0-9]*]]) // CHECK: ![[RAW_FUNC_QUAL_CNST]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[RAW_FUNC_QUAL_TYPE:[0-9]*]]) -// CHECK: ![[RAW_FUNC_QUAL_T3]] = !DITemplateTypeParameter(name: "T3", type: ![[RAW_FUNC_QUAL_REF:[0-9]*]]) +// CHECK: ![[RAW_FUNC_QUAL_T3]] = !DITemplateTypeParameter(name: "T3", type: ![[RAW_FUNC_QUAL_REF:[0-9]*]]) // CHECK: ![[RAW_FUNC_QUAL_REF]] = !DISubroutineType(flags: DIFlagLValueReference, types: ![[RAW_FUNC_QUAL_LIST]]) -// CHECK: ![[RAW_FUNC_QUAL_T4]] = !DITemplateTypeParameter(name: "T4", type: ![[RAW_FUNC_QUAL_REF_REF:[0-9]*]]) +// CHECK: ![[RAW_FUNC_QUAL_T4]] = !DITemplateTypeParameter(name: "T4", type: ![[RAW_FUNC_QUAL_REF_REF:[0-9]*]]) // CHECK: ![[RAW_FUNC_QUAL_REF_REF]] = !DISubroutineType(flags: DIFlagRValueReference, types: ![[RAW_FUNC_QUAL_LIST]]) } // namespace RawFuncQual diff --git a/clang/test/SemaCXX/cxx2a-nttp-printing.cpp b/clang/test/SemaCXX/cxx2a-nttp-printing.cpp --- a/clang/test/SemaCXX/cxx2a-nttp-printing.cpp +++ b/clang/test/SemaCXX/cxx2a-nttp-printing.cpp @@ -16,13 +16,13 @@ " is too long"> a; Foo(a); // expected-error {{no matching function}} - decltype(a)::display(); // expected-error {{no member named 'display' in 'ASCII<{"this nontype template argument is [...]"}>'}} + decltype(a)::display(); // expected-error {{no member named 'display' in 'ASCII{"this nontype template argument is [...]"}>'}} } void test_non_ascii() { ASCII<"wait a s\033cond"> a; Bar(a); // expected-error {{no matching function}} - decltype(a)::display(); // expected-error {{no member named 'display' in 'ASCII<{{119, 97, 105, 116, 32, 97, 32, 115, 27, 99, ...}}>'}} + decltype(a)::display(); // expected-error {{no member named 'display' in 'ASCII{{119, 97, 105, 116, 32, 97, 32, 115, 27, 99, ...}}>'}} } // The dialects (C++20 and above) that accept string literals as non-type @@ -30,5 +30,5 @@ void test_trigraph() { ASCII<"what??!"> a; // expected-warning {{trigraph ignored}} Meow(a); // expected-error {{no matching function}} - decltype(a)::display(); // expected-error {{no member named 'display' in 'ASCII<{"what??!"}>'}} + decltype(a)::display(); // expected-error {{no member named 'display' in 'ASCII{"what??!"}>'}} } diff --git a/clang/test/SemaTemplate/temp_arg_nontype_cxx20.cpp b/clang/test/SemaTemplate/temp_arg_nontype_cxx20.cpp --- a/clang/test/SemaTemplate/temp_arg_nontype_cxx20.cpp +++ b/clang/test/SemaTemplate/temp_arg_nontype_cxx20.cpp @@ -185,7 +185,7 @@ namespace Diags { struct A { int n, m; }; template struct X { static_assert(a.n == a.m); }; // expected-error {{static assertion failed due to requirement 'Diags::A{1, 2}.n == Diags::A{1, 2}.m'}} - template struct X; // expected-note {{in instantiation of template class 'Diags::X<{1, 2}>' requested here}} + template struct X; // expected-note {{in instantiation of template class 'Diags::X' requested here}} } namespace CTADPartialOrder { diff --git a/clang/test/SemaTemplate/temp_arg_string_printing.cpp b/clang/test/SemaTemplate/temp_arg_string_printing.cpp --- a/clang/test/SemaTemplate/temp_arg_string_printing.cpp +++ b/clang/test/SemaTemplate/temp_arg_string_printing.cpp @@ -19,111 +19,111 @@ template class ASCII {}; void not_string() { - // CHECK{LITERAL}: ASCII<{{9, -1, 42}}> + // CHECK{LITERAL}: ASCII{{9, -1, 42}}> new ASCII<(int[]){9, -1, 42}>; - // CHECK{LITERAL}: ASCII<{{3.140000e+00, 0.000000e+00, 4.200000e+01}}> + // CHECK{LITERAL}: ASCII{{3.140000e+00, 0.000000e+00, 4.200000e+01}}> new ASCII<(double[]){3.14, 0., 42.}>; } void narrow() { - // CHECK{LITERAL}: ASCII<{""}> + // CHECK{LITERAL}: ASCII{""}> new ASCII<"">; - // CHECK{LITERAL}: ASCII<{"the quick brown fox jumps"}> + // CHECK{LITERAL}: ASCII{"the quick brown fox jumps"}> new ASCII<"the quick brown fox jumps">; - // CHECK{LITERAL}: ASCII<{"OVER THE LAZY DOG 0123456789"}> + // CHECK{LITERAL}: ASCII{"OVER THE LAZY DOG 0123456789"}> new ASCII<"OVER THE LAZY DOG 0123456789">; - // CHECK{LITERAL}: ASCII<{"\\`~!@#$%^&*()_+-={}[]|\'\";:,.<>?/"}> + // CHECK{LITERAL}: ASCII{"\\`~!@#$%^&*()_+-={}[]|\'\";:,.<>?/"}> new ASCII?/)">; - // CHECK{LITERAL}: ASCII<{{101, 115, 99, 97, 112, 101, 0, 0}}> + // CHECK{LITERAL}: ASCII{{101, 115, 99, 97, 112, 101, 0, 0}}> new ASCII<"escape\0">; - // CHECK{LITERAL}: ASCII<{"escape\r\n"}> + // CHECK{LITERAL}: ASCII{"escape\r\n"}> new ASCII<"escape\r\n">; - // CHECK{LITERAL}: ASCII<{"escape\\\t\f\v"}> + // CHECK{LITERAL}: ASCII{"escape\\\t\f\v"}> new ASCII<"escape\\\t\f\v">; - // CHECK{LITERAL}: ASCII<{"escape\a\bc"}> + // CHECK{LITERAL}: ASCII{"escape\a\bc"}> new ASCII<"escape\a\b\c">; - // CHECK{LITERAL}: ASCII<{{110, 111, 116, 17, 0}}> + // CHECK{LITERAL}: ASCII{{110, 111, 116, 17, 0}}> new ASCII<"not\x11">; - // CHECK{LITERAL}: ASCII<{{18, 20, 127, 16, 1, 32, 97, 98, 99, 0}}> + // CHECK{LITERAL}: ASCII{{18, 20, 127, 16, 1, 32, 97, 98, 99, 0}}> new ASCII<"\x12\x14\x7f\x10\x01 abc">; - // CHECK{LITERAL}: ASCII<{{18, 20, 127, 16, 1, 32, 97, 98, 99, 100, 0}}> + // CHECK{LITERAL}: ASCII{{18, 20, 127, 16, 1, 32, 97, 98, 99, 100, 0}}> new ASCII<"\x12\x14\x7f\x10\x01 abcd">; - // CHECK{LITERAL}: ASCII<{"print more characters as string"}> + // CHECK{LITERAL}: ASCII{"print more characters as string"}> new ASCII<"print more characters as string">; - // CHECK{LITERAL}: ASCII<{"print more characters as string, no uplimit"}> + // CHECK{LITERAL}: ASCII{"print more characters as string, no uplimit"}> new ASCII<"print more characters as string, no uplimit">; } void wide() { - // CHECK{LITERAL}: ASCII<{L""}> + // CHECK{LITERAL}: ASCII{L""}> new ASCII; - // CHECK{LITERAL}: ASCII<{L"the quick brown fox jumps"}> + // CHECK{LITERAL}: ASCII{L"the quick brown fox jumps"}> new ASCII; - // CHECK{LITERAL}: ASCII<{L"OVER THE LAZY DOG 0123456789"}> + // CHECK{LITERAL}: ASCII{L"OVER THE LAZY DOG 0123456789"}> new ASCII; - // CHECK{LITERAL}: ASCII<{L"\\`~!@#$%^&*()_+-={}[]|\'\";:,.<>?/"}> + // CHECK{LITERAL}: ASCII{L"\\`~!@#$%^&*()_+-={}[]|\'\";:,.<>?/"}> new ASCII?/)">; - // CHECK{LITERAL}: ASCII<{{101, 115, 99, 97, 112, 101, 0, 0}}> + // CHECK{LITERAL}: ASCII{{101, 115, 99, 97, 112, 101, 0, 0}}> new ASCII; - // CHECK{LITERAL}: ASCII<{L"escape\r\n"}> + // CHECK{LITERAL}: ASCII{L"escape\r\n"}> new ASCII; - // CHECK{LITERAL}: ASCII<{L"escape\\\t\f\v"}> + // CHECK{LITERAL}: ASCII{L"escape\\\t\f\v"}> new ASCII; - // CHECK{LITERAL}: ASCII<{L"escape\a\bc"}> + // CHECK{LITERAL}: ASCII{L"escape\a\bc"}> new ASCII; - // CHECK{LITERAL}: ASCII<{{110, 111, 116, 17, 0}}> + // CHECK{LITERAL}: ASCII{{110, 111, 116, 17, 0}}> new ASCII; - // CHECK{LITERAL}: ASCII<{{18, 20, 255, 22909, 136, 32, 97, 98, 99, 0}}> + // CHECK{LITERAL}: ASCII{{18, 20, 255, 22909, 136, 32, 97, 98, 99, 0}}> new ASCII; - // CHECK{LITERAL}: ASCII<{{18, 20, 255, 22909, 136, 32, 97, 98, 99, 100, 0}}> + // CHECK{LITERAL}: ASCII{{18, 20, 255, 22909, 136, 32, 97, 98, 99, 100, 0}}> new ASCII; - // CHECK{LITERAL}: ASCII<{L"print more characters as string"}> + // CHECK{LITERAL}: ASCII{L"print more characters as string"}> new ASCII; - // CHECK{LITERAL}: ASCII<{L"print more characters as string, no uplimit"}> + // CHECK{LITERAL}: ASCII{L"print more characters as string, no uplimit"}> new ASCII; } void utf8() { - // CHECK{LITERAL}: ASCII<{u8""}> + // CHECK{LITERAL}: ASCII{u8""}> new ASCII; - // CHECK{LITERAL}: ASCII<{u8"\\`~!@#$%^&*()_+-={}[]|\'\";:,.<>?/"}> + // CHECK{LITERAL}: ASCII{u8"\\`~!@#$%^&*()_+-={}[]|\'\";:,.<>?/"}> new ASCII?/)">; - // CHECK{LITERAL}: ASCII<{{101, 115, 99, 97, 112, 101, 0, 0}}> + // CHECK{LITERAL}: ASCII{{101, 115, 99, 97, 112, 101, 0, 0}}> new ASCII; - // CHECK{LITERAL}: ASCII<{u8"escape\r\n"}> + // CHECK{LITERAL}: ASCII{u8"escape\r\n"}> new ASCII; - // CHECK{LITERAL}: ASCII<{{229, 165, 189, 239, 191, 189, 0}}> + // CHECK{LITERAL}: ASCII{{229, 165, 189, 239, 191, 189, 0}}> new ASCII; - // CHECK{LITERAL}: ASCII<{u8"print more characters as string, no uplimit"}> + // CHECK{LITERAL}: ASCII{u8"print more characters as string, no uplimit"}> new ASCII; } void utf16() { - // CHECK{LITERAL}: ASCII<{u""}> + // CHECK{LITERAL}: ASCII{u""}> new ASCII; - // CHECK{LITERAL}: ASCII<{u"\\`~!@#$%^&*()_+-={}[]|\'\";:,.<>?/"}> + // CHECK{LITERAL}: ASCII{u"\\`~!@#$%^&*()_+-={}[]|\'\";:,.<>?/"}> new ASCII?/)">; - // CHECK{LITERAL}: ASCII<{{101, 115, 99, 97, 112, 101, 0, 0}}> + // CHECK{LITERAL}: ASCII{{101, 115, 99, 97, 112, 101, 0, 0}}> new ASCII; - // CHECK{LITERAL}: ASCII<{u"escape\r\n"}> + // CHECK{LITERAL}: ASCII{u"escape\r\n"}> new ASCII; - // CHECK{LITERAL}: ASCII<{{22909, 65533, 0}}> + // CHECK{LITERAL}: ASCII{{22909, 65533, 0}}> new ASCII; - // CHECK{LITERAL}: ASCII<{u"print more characters as string, no uplimit"}> + // CHECK{LITERAL}: ASCII{u"print more characters as string, no uplimit"}> new ASCII; } void utf32() { - // CHECK{LITERAL}: ASCII<{U""}> + // CHECK{LITERAL}: ASCII{U""}> new ASCII; - // CHECK{LITERAL}: ASCII<{U"\\`~!@#$%^&*()_+-={}[]|\'\";:,.<>?/"}> + // CHECK{LITERAL}: ASCII{U"\\`~!@#$%^&*()_+-={}[]|\'\";:,.<>?/"}> new ASCII?/)">; - // CHECK{LITERAL}: ASCII<{{101, 115, 99, 97, 112, 101, 0, 0}}> + // CHECK{LITERAL}: ASCII{{101, 115, 99, 97, 112, 101, 0, 0}}> new ASCII; - // CHECK{LITERAL}: ASCII<{U"escape\r\n"}> + // CHECK{LITERAL}: ASCII{U"escape\r\n"}> new ASCII; - // CHECK{LITERAL}: ASCII<{{22909, 131358, 0}}> + // CHECK{LITERAL}: ASCII{{22909, 131358, 0}}> new ASCII; - // CHECK{LITERAL}: ASCII<{U"print more characters as string, no uplimit"}> + // CHECK{LITERAL}: ASCII{U"print more characters as string, no uplimit"}> new ASCII; } diff --git a/clang/unittests/AST/TypePrinterTest.cpp b/clang/unittests/AST/TypePrinterTest.cpp --- a/clang/unittests/AST/TypePrinterTest.cpp +++ b/clang/unittests/AST/TypePrinterTest.cpp @@ -48,7 +48,7 @@ std::string Code = R"cpp( namespace N { template struct Type {}; - + template void Foo(const Type &Param); } @@ -115,15 +115,65 @@ ASSERT_TRUE(PrintedTypeMatches( Code, {"-std=c++20"}, Matcher, - R"(ASCII<{"this nontype template argument is [...]"}> &&)", + R"(ASCII{"this nontype template argument is [...]"}> &&)", [](PrintingPolicy &Policy) { Policy.EntireContentsOfLargeArray = false; })); ASSERT_TRUE(PrintedTypeMatches( Code, {"-std=c++20"}, Matcher, - R"(ASCII<{"this nontype template argument is too long to print"}> &&)", + R"(ASCII{"this nontype template argument is too long to print"}> &&)", [](PrintingPolicy &Policy) { Policy.EntireContentsOfLargeArray = true; })); } + +TEST(TypePrinter, TemplateIdWithComplexFullTypeNTTP) { + constexpr char Code[] = R"cpp( + template< typename T, auto ... dims > + struct NDArray {}; + + struct Dimension + { + using value_type = unsigned short; + + value_type size{ value_type( 0 ) }; + }; + + template < typename ConcreteDim > + struct DimensionImpl : Dimension {}; + + struct Width : DimensionImpl< Width > {}; + struct Height : DimensionImpl< Height > {}; + struct Channels : DimensionImpl< Channels > {}; + + inline constexpr Width W; + inline constexpr Height H; + inline constexpr Channels C; + + template< auto ... Dims > + consteval auto makeArray() noexcept + { + return NDArray< float, Dims ... >{}; + } + + [[ maybe_unused ]] auto x { makeArray< H, W, C >() }; + + )cpp"; + auto Matcher = varDecl( + allOf(hasAttr(attr::Kind::Unused), hasType(qualType().bind("id")))); + + ASSERT_TRUE(PrintedTypeMatches( + Code, {"-std=c++20"}, Matcher, + R"(NDArray)", + [](PrintingPolicy &Policy) { + Policy.AlwaysIncludeTypeForNonTypeTemplateArgument = false; + })); + + ASSERT_TRUE(PrintedTypeMatches( + Code, {"-std=c++20"}, Matcher, + R"(NDArray{Dimension{0}}}, Width{DimensionImpl{Dimension{0}}}, Channels{DimensionImpl{Dimension{0}}}>)", + [](PrintingPolicy &Policy) { + Policy.AlwaysIncludeTypeForNonTypeTemplateArgument = true; + })); +}