Skip to content

Commit d080f78

Browse files
authored
[Clang] Fixed a crash when __PRETTY_FUNCTION__ or __FUNCSIG__ (clang-cl) appears in the trailing return type of the lambda (#122611)
The (function) type of the lambda function is null while parsing trailing return type. The type is filled-in when the lambda body is entered. So, resolving `__PRETTY_FUNCTION__` before the lambda body is entered causes the crash. Fixes #121274.
1 parent 6422546 commit d080f78

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,7 @@ Bug Fixes to C++ Support
926926
(`LWG3929 <https://wg21.link/LWG3929>`__.) (#GH121278)
927927
- Clang now identifies unexpanded parameter packs within the type constraint on a non-type template parameter. (#GH88866)
928928
- Fixed an issue while resolving type of expression indexing into a pack of values of non-dependent type (#GH121242)
929+
- Fixed a crash when __PRETTY_FUNCTION__ or __FUNCSIG__ (clang-cl) appears in the trailing return type of the lambda (#GH121274)
929930

930931
Bug Fixes to AST Handling
931932
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/AST/Expr.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,15 @@ std::string PredefinedExpr::ComputeName(PredefinedIdentKind IK,
774774
const FunctionDecl *Decl = FD;
775775
if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
776776
Decl = Pattern;
777-
const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
777+
778+
// Bail out if the type of the function has not been set yet.
779+
// This can notably happen in the trailing return type of a lambda
780+
// expression.
781+
const Type *Ty = Decl->getType().getTypePtrOrNull();
782+
if (!Ty)
783+
return "";
784+
785+
const FunctionType *AFT = Ty->getAs<FunctionType>();
778786
const FunctionProtoType *FT = nullptr;
779787
if (FD->hasWrittenPrototype())
780788
FT = dyn_cast<FunctionProtoType>(AFT);

clang/test/SemaCXX/crash-GH121274.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %clang_cc1 -std=c++11 -verify %s
2+
// expected-no-diagnostics
3+
4+
// Do not crash when __PRETTY_FUNCTION__ appears in the trailing return type of the lambda
5+
void foo() {
6+
[]() -> decltype(static_cast<const char*>(__PRETTY_FUNCTION__)) {
7+
return nullptr;
8+
}();
9+
10+
#ifdef MS
11+
[]() -> decltype(static_cast<const char*>(__FUNCSIG__)) {
12+
return nullptr;
13+
}();
14+
#endif
15+
}

0 commit comments

Comments
 (0)