From 4e5935f6631f0f0cd828559ec29ed931bc0333d3 Mon Sep 17 00:00:00 2001 From: Tilak Chad Date: Sun, 12 Jan 2025 00:03:32 +0545 Subject: [PATCH] [Clang] Fixed a crash when __PRETTY_FUNCTION__ or __FUNCSIG__ (clang-cl) appears in the trailing return type of the lambda --- clang/lib/AST/Expr.cpp | 7 ++++++- clang/test/SemaCXX/crash-GH121274.cpp | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 clang/test/SemaCXX/crash-GH121274.cpp diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp index 5331357b5d1fe..0caff41c8a8cf 100644 --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -774,7 +774,12 @@ std::string PredefinedExpr::ComputeName(PredefinedIdentKind IK, const FunctionDecl *Decl = FD; if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern()) Decl = Pattern; - const FunctionType *AFT = Decl->getType()->getAs(); + + const Type *Ty = Decl->getType().getTypePtrOrNull(); + if (!Ty) + return ""; + + const FunctionType *AFT = Ty->getAs(); const FunctionProtoType *FT = nullptr; if (FD->hasWrittenPrototype()) FT = dyn_cast(AFT); diff --git a/clang/test/SemaCXX/crash-GH121274.cpp b/clang/test/SemaCXX/crash-GH121274.cpp new file mode 100644 index 0000000000000..28677a0949bf9 --- /dev/null +++ b/clang/test/SemaCXX/crash-GH121274.cpp @@ -0,0 +1,15 @@ +// RUN: %clang_cc1 -std=c++11 -verify %s +// expected-no-diagnostics + +// Do not crash when __PRETTY_FUNCTION__ appears in the trailing return type of the lambda +void foo() { + []() -> decltype(static_cast(__PRETTY_FUNCTION__)) { + return nullptr; + }(); + +#ifdef MS + []() -> decltype(static_cast(__FUNCSIG__)) { + return nullptr; + }(); +#endif +}