[clang] [Clang] Fix looking for immediate calls in default arguments. (PR #80690)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 5 01:03:42 PST 2024
https://github.com/cor3ntin updated https://github.com/llvm/llvm-project/pull/80690
>From 690a251c4d8df2099b213ba63e9836c2752b5ac1 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Mon, 5 Feb 2024 15:42:21 +0100
Subject: [PATCH 1/2] [Clang] Fix looking for immediate calls in default
arguments.
Due to improper use of RecursiveASTVisitor.
Fixes #80630
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/lib/Sema/SemaExpr.cpp | 6 +++---
.../SemaCXX/cxx2a-consteval-default-params.cpp | 15 +++++++++++++++
clang/test/SemaCXX/source_location.cpp | 18 ++++++++++++++++++
4 files changed, 38 insertions(+), 3 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c239c97f7afc91..9ebff537e11079 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -294,6 +294,8 @@ Bug Fixes to C++ Support
instead of only on class, alias, and variable templates, as last updated by
CWG2032. Fixes (#GH#83461)
+- Fix evaluation of some immediate calls in default arguments.
+ Fixes (#GH80630)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 0a449fc1082bd4..9b4c3440448ba8 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -6199,7 +6199,7 @@ struct ImmediateCallVisitor : public RecursiveASTVisitor<ImmediateCallVisitor> {
bool VisitCallExpr(CallExpr *E) {
if (const FunctionDecl *FD = E->getDirectCallee())
HasImmediateCalls |= FD->isImmediateFunction();
- return RecursiveASTVisitor<ImmediateCallVisitor>::VisitStmt(E);
+ return RecursiveASTVisitor<ImmediateCallVisitor>::VisitCallExpr(E);
}
bool VisitCXXConstructExpr(CXXConstructExpr *E) {
@@ -6229,9 +6229,9 @@ struct ImmediateCallVisitor : public RecursiveASTVisitor<ImmediateCallVisitor> {
// Blocks don't support default parameters, and, as for lambdas,
// we don't consider their body a subexpression.
- bool VisitBlockDecl(BlockDecl *B) { return false; }
+ bool VisitBlockDecl(BlockDecl *B) { return true; }
- bool VisitCompoundStmt(CompoundStmt *B) { return false; }
+ bool VisitCompoundStmt(CompoundStmt *B) { return true; }
bool VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
return TraverseStmt(E->getExpr());
diff --git a/clang/test/SemaCXX/cxx2a-consteval-default-params.cpp b/clang/test/SemaCXX/cxx2a-consteval-default-params.cpp
index be8f7cc788589f..e4b13725b2dacd 100644
--- a/clang/test/SemaCXX/cxx2a-consteval-default-params.cpp
+++ b/clang/test/SemaCXX/cxx2a-consteval-default-params.cpp
@@ -82,3 +82,18 @@ namespace GH62224 {
C<> Val; // No error since fwd is defined already.
static_assert(Val.get() == 42);
}
+
+namespace GH80630 {
+
+consteval const char* ce() { return "Hello"; }
+
+auto f2(const char* loc = []( char const* fn )
+ { return fn; } ( ce() ) ) {
+ return loc;
+}
+
+auto g() {
+ return f2();
+}
+
+}
diff --git a/clang/test/SemaCXX/source_location.cpp b/clang/test/SemaCXX/source_location.cpp
index 7414fbce7828d1..b151fc45fdad62 100644
--- a/clang/test/SemaCXX/source_location.cpp
+++ b/clang/test/SemaCXX/source_location.cpp
@@ -832,3 +832,21 @@ void test() {
}
}
+
+namespace GH80630 {
+
+#define GH80630_LAMBDA \
+ []( char const* fn ) { \
+ static constexpr std::source_location loc = std::source_location::current(); \
+ return &loc; \
+ }( std::source_location::current().function() )
+
+auto f( std::source_location const* loc = GH80630_LAMBDA ) {
+ return loc;
+}
+
+auto g() {
+ return f();
+}
+
+}
>From 077db5232f786b1298ae01edc4f8e2b165aef80a Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Sun, 18 Feb 2024 14:45:22 +0100
Subject: [PATCH 2/2] Address Aaron's feedback
---
clang/lib/Sema/SemaExpr.cpp | 8 +-------
1 file changed, 1 insertion(+), 7 deletions(-)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 9b4c3440448ba8..47bb263f56aade 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -6199,7 +6199,7 @@ struct ImmediateCallVisitor : public RecursiveASTVisitor<ImmediateCallVisitor> {
bool VisitCallExpr(CallExpr *E) {
if (const FunctionDecl *FD = E->getDirectCallee())
HasImmediateCalls |= FD->isImmediateFunction();
- return RecursiveASTVisitor<ImmediateCallVisitor>::VisitCallExpr(E);
+ return RecursiveASTVisitor<ImmediateCallVisitor>::VisitStmt(E);
}
bool VisitCXXConstructExpr(CXXConstructExpr *E) {
@@ -6227,12 +6227,6 @@ struct ImmediateCallVisitor : public RecursiveASTVisitor<ImmediateCallVisitor> {
return VisitCXXMethodDecl(E->getCallOperator());
}
- // Blocks don't support default parameters, and, as for lambdas,
- // we don't consider their body a subexpression.
- bool VisitBlockDecl(BlockDecl *B) { return true; }
-
- bool VisitCompoundStmt(CompoundStmt *B) { return true; }
-
bool VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
return TraverseStmt(E->getExpr());
}
More information about the cfe-commits
mailing list