-
Notifications
You must be signed in to change notification settings - Fork 12.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Clang] Fixed a crash when __PRETTY_FUNCTION__ or __FUNCSIG__ (clang-cl) appears in the trailing return type of the lambda #122611
Conversation
…cl) appears in the trailing return type of the lambda
@llvm/pr-subscribers-clang Author: TilakChad (TilakChad) ChangesThe (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 This resolves the crash: #121274. Full diff: https://github.com/llvm/llvm-project/pull/122611.diff 2 Files Affected:
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 5331357b5d1fef..0caff41c8a8cf6 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<FunctionType>();
+
+ const Type *Ty = Decl->getType().getTypePtrOrNull();
+ if (!Ty)
+ return "";
+
+ const FunctionType *AFT = Ty->getAs<FunctionType>();
const FunctionProtoType *FT = nullptr;
if (FD->hasWrittenPrototype())
FT = dyn_cast<FunctionProtoType>(AFT);
diff --git a/clang/test/SemaCXX/crash-GH121274.cpp b/clang/test/SemaCXX/crash-GH121274.cpp
new file mode 100644
index 00000000000000..28677a0949bf9e
--- /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<const char*>(__PRETTY_FUNCTION__)) {
+ return nullptr;
+ }();
+
+#ifdef MS
+ []() -> decltype(static_cast<const char*>(__FUNCSIG__)) {
+ return nullptr;
+ }();
+#endif
+}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this PR.
The approach looks sensible to me
Can you add a release note in clang/docs/ReleaseNotes.rst
? Thanks
I've updated the release notes. Thanks for reminding and for the review. 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (but i reworded the comment slightly)
cea9b5d
to
16eaeca
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Do you need me to merge that for you?
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.