Skip to content
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] Fix looking for immediate calls in default arguments. #80690

Merged
merged 3 commits into from
Mar 5, 2024

Conversation

cor3ntin
Copy link
Contributor

@cor3ntin cor3ntin commented Feb 5, 2024

Due to improper use of RecursiveASTVisitor.

Fixes #80630

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Feb 5, 2024
@llvmbot
Copy link
Member

llvmbot commented Feb 5, 2024

@llvm/pr-subscribers-clang

Author: cor3ntin (cor3ntin)

Changes

Due to improper use of RecursiveASTVisitor.

Fixes #80630


Full diff: https://github.com/llvm/llvm-project/pull/80690.diff

4 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+2)
  • (modified) clang/lib/Sema/SemaExpr.cpp (+3-3)
  • (modified) clang/test/SemaCXX/cxx2a-consteval-default-params.cpp (+15)
  • (modified) clang/test/SemaCXX/source_location.cpp (+18)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3596109bf044f..fc9acd3f20792 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -201,6 +201,8 @@ Bug Fixes to C++ Support
   parameter where we did an incorrect specialization of the initialization of
   the default parameter.
   Fixes (`#68490 <https://github.com/llvm/llvm-project/issues/68490>`_)
+- Fix evaluation of some immediate calls in default arguments.
+  Fixes (`#80630 <https://github.com/llvm/llvm-project/issues/80630>`_)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index d15278bce5a6b..ab212447f7368 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -6198,7 +6198,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);
   }
 
   // SourceLocExpr are not immediate invocations
@@ -6222,9 +6222,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 be8f7cc788589..e4b13725b2dac 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 7414fbce7828d..b151fc45fdad6 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();
+}
+
+}

@cor3ntin cor3ntin requested a review from AaronBallman February 5, 2024 14:52
Comment on lines 6223 to 6227
// 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; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can just remove these entirely; the default implementation already returns true.

@@ -6198,7 +6198,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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a noop, right? If so, I'd drop the changes here but make an NFC change for this and the one in VisitSourceLocExpr() at the same time.

@cor3ntin cor3ntin force-pushed the corentin/fix_immediate_arg branch from a9e1885 to c0fdb22 Compare February 18, 2024 13:45
Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@cor3ntin cor3ntin force-pushed the corentin/fix_immediate_arg branch from 6eec644 to 077db52 Compare March 4, 2024 13:29
@zyn0217
Copy link
Contributor

zyn0217 commented Mar 5, 2024

Does this fix #67134 as well?

@cor3ntin
Copy link
Contributor Author

cor3ntin commented Mar 5, 2024

Does this fix #67134 as well?

I don't think so, no

@cor3ntin cor3ntin merged commit d773c00 into llvm:main Mar 5, 2024
4 of 5 checks passed
@cor3ntin cor3ntin deleted the corentin/fix_immediate_arg branch March 5, 2024 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Clang] nontrivial use of std::source_location::current() in a default argument fails
4 participants