Skip to content

[clang] Preserve UDL nodes in RemoveNestedImmediateInvocation #66641

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

Merged
merged 2 commits into from
Oct 4, 2023

Conversation

zyn0217
Copy link
Contributor

@zyn0217 zyn0217 commented Sep 18, 2023

D63960 performs a tree transformation on AST to prevent evaluating constant expressions eagerly by removing recorded immediate consteval invocations from subexpressions. (See https://reviews.llvm.org/D63960#inline-600736 for its motivation.)

However, the UDL node has been replaced with a CallExpr in the default TreeTransform since ca844ab (inadvertently?). This confuses clangd as it relies on the exact AST node type to decide whether or not to present inlay hints for an expression.

With regard to the fix, I think it's enough to return the UDL expression as-is during the transformation: We've bound it to temporary in its construction, and there's no ConstantExpr to visit under a UDL.

Fixes #63898.

@zyn0217 zyn0217 added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Sep 18, 2023
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Sep 18, 2023
@llvmbot
Copy link
Member

llvmbot commented Sep 18, 2023

@llvm/pr-subscribers-clang

Changes

D63960 performs a tree transformation on AST to prevent evaluating constant expressions eagerly by removing recorded immediate consteval invocations from subexpressions. (See https://reviews.llvm.org/D63960#inline-600736 for its motivation.)

However, the UDL node has been replaced with a CallExpr in the default TreeTransform since ca844ab (inadvertently?). This confuses clangd as it relies on the exact AST node type to decide whether or not to present inlay hints for an expression.

With regard to the fix, I think it's enough to return the UDL expression as-is during the transformation: We've bound it to temporary in its construction, and there's no ConstantExpr to visit under a UDL.

Fixes #63898.

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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaExpr.cpp (+4-1)
  • (added) clang/test/AST/ast-dump-udl-consteval.cpp (+17)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 92496b03ecabe54..104da822aae2930 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -18438,7 +18438,10 @@ static void RemoveNestedImmediateInvocation(
       DRSet.erase(cast<DeclRefExpr>(E->getCallee()->IgnoreImplicit()));
       return Base::TransformCXXOperatorCallExpr(E);
     }
-    /// Base::TransformInitializer skip ConstantExpr so we need to visit them
+    /// Base::TransformUserDefinedLiteral doesn't preserve the
+    /// UserDefinedLiteral node.
+    ExprResult TransformUserDefinedLiteral(UserDefinedLiteral *E) { return E; }
+    /// Base::TransformInitializer skips ConstantExpr so we need to visit them
     /// here.
     ExprResult TransformInitializer(Expr *Init, bool NotCopyInit) {
       if (!Init)
diff --git a/clang/test/AST/ast-dump-udl-consteval.cpp b/clang/test/AST/ast-dump-udl-consteval.cpp
new file mode 100644
index 000000000000000..9da53f725172aba
--- /dev/null
+++ b/clang/test/AST/ast-dump-udl-consteval.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -xc++ -std=c++23 -ast-dump %s | FileCheck %s
+
+int inline consteval operator""_u32(unsigned long long val) {
+  return val;
+}
+
+void udl() {
+  (void)(0_u32 + 1_u32);
+}
+
+// CHECK: `-BinaryOperator {{.+}} <col:10, col:18> 'int' '+'
+// CHECK-NEXT: |-ConstantExpr {{.+}} <col:10> 'int'
+// CHECK-NEXT: | |-value: Int 0
+// CHECK-NEXT: | `-UserDefinedLiteral {{.+}} <col:10> 'int'
+// CHECK: `-ConstantExpr {{.+}} <col:18> 'int'
+// CHECK-NEXT:   |-value: Int 1
+// CHECK-NEXT:   `-UserDefinedLiteral {{.+}} <col:18> 'int'

@zyn0217
Copy link
Contributor Author

zyn0217 commented Sep 25, 2023

Ping~ Looking for a review :)

@HighCommander4
Copy link
Collaborator

I wonder if TreeTransform::TransformUserDefinedLiteral() should be doing something like RebuildUserDefinedLiteral().

Anyways, the patch looks reasonable to me but someone more familiar with Sema should review it.

@HighCommander4 HighCommander4 removed their request for review September 25, 2023 08:07
@zyn0217
Copy link
Contributor Author

zyn0217 commented Sep 27, 2023

Invite @shafik and @cor3ntin to have a look at this.

My apologies if I'm churning anyone's review backlog. Honestly, I have no idea who the appropriate reviewer/code owner is. :(

@zyn0217 zyn0217 requested review from cor3ntin and shafik September 27, 2023 04:36
@cor3ntin
Copy link
Contributor

cor3ntin commented Oct 2, 2023

@zyn0217 Can you add a release note indicating that #63898 was fixed? Otherwise it looks good to me.
Sorry for the long delay, feel free to ping me again

@zyn0217
Copy link
Contributor Author

zyn0217 commented Oct 4, 2023

No problem! And here it is.
PTAL, thanks! @cor3ntin

D63960 performs a tree transformation on AST to prevent evaluating
constant expressions eagerly by removing recorded immediate consteval
invocations from subexpressions. (See https://reviews.llvm.org/D63960#inline-600736
for its motivation.)

However, the UDL node has been replaced with a CallExpr in the default
TreeTransform since ca844ab (inadvertently?). This confuses clangd
as it relies on the exact AST node type to decide whether or not to
present inlay hints for an expression.

With regard to the fix, I think it's enough to return the UDL expression
as-is during the transformation: We've bound it to temporary
in its construction, and there's no ConstantExpr to visit under a UDL.

Fixes llvm#63898.
Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@zyn0217 zyn0217 merged commit 077e1b8 into llvm:main Oct 4, 2023
Guzhu-AMD pushed a commit to GPUOpen-Drivers/llvm-project that referenced this pull request Oct 12, 2023
Local branch amd-gfx 1d4c3cc Merged main:503bc5f66111 into amd-gfx:2a4e1350d643
Remote branch main 077e1b8 [clang] Preserve UDL nodes in RemoveNestedImmediateInvocation (llvm#66641)
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.

Clangd puts parameter name hint on UDLs
4 participants