Fix crash getting name of a template decl

NamedDecl::getIdentifier can return a nullptr when
DeclarationName::isIdentifier is false, which leads to a null pointer
dereference when TypePrinter::printTemplateId calls ->getName().

NamedDecl::getName does the same thing in the successful case and
returns an empty string in the failure case.

This crash affects the llvm 14 packages on llvm.org.
This commit is contained in:
Tom Eccles 2022-04-22 13:03:28 -04:00 committed by Aaron Ballman
parent e9a902c7f7
commit 225b91e6cb
2 changed files with 17 additions and 2 deletions

View File

@ -1467,8 +1467,7 @@ void TypePrinter::printTemplateId(const TemplateSpecializationType *T,
if (!Policy.SuppressScope)
AppendScope(TD->getDeclContext(), OS, TD->getDeclName());
IdentifierInfo *II = TD->getIdentifier();
OS << II->getName();
OS << TD->getName();
} else {
T->getTemplateName().print(OS, Policy);
}

View File

@ -64,6 +64,22 @@ TEST(TypePrinter, TemplateId) {
[](PrintingPolicy &Policy) { Policy.FullyQualifiedName = true; }));
}
TEST(TypePrinter, TemplateId2) {
std::string Code = R"cpp(
template <template <typename ...> class TemplatedType>
void func(TemplatedType<int> Param);
)cpp";
auto Matcher = parmVarDecl(hasType(qualType().bind("id")));
// Regression test ensuring we do not segfault getting the QualType as a
// string.
ASSERT_TRUE(PrintedTypeMatches(Code, {}, Matcher, "<int>",
[](PrintingPolicy &Policy) {
Policy.FullyQualifiedName = true;
Policy.PrintCanonicalTypes = true;
}));
}
TEST(TypePrinter, ParamsUglified) {
llvm::StringLiteral Code = R"cpp(
template <typename _Tp, template <typename> class __f>