Skip to content

[clang][HeuristicResolver] Apply default argument heuristic in resolveDeclRefExpr as well #132576

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 1 commit into from
Apr 11, 2025

Conversation

HighCommander4
Copy link
Collaborator

@HighCommander4 HighCommander4 commented Mar 23, 2025

This is a follow-up to #131074. After moving the default argument heuristic to simplifyType as per this discussion, I realized that this made it no longer apply to the DependentScopeDeclRefExpr case, because that wasn't using simplifyType.

This patch fixes that, with an added testcase.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang-tools-extra clangd clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 23, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 23, 2025

@llvm/pr-subscribers-clangd
@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clang

Author: Nathan Ridge (HighCommander4)

Changes

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

3 Files Affected:

  • (modified) clang-tools-extra/clangd/unittests/XRefsTests.cpp (+1-1)
  • (modified) clang/lib/Sema/HeuristicResolver.cpp (+3-3)
  • (modified) clang/unittests/Sema/HeuristicResolverTest.cpp (+17)
diff --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp b/clang-tools-extra/clangd/unittests/XRefsTests.cpp
index e12d7691c58fb..693e965e78a96 100644
--- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -1091,7 +1091,7 @@ TEST(LocateSymbol, All) {
       )objc",
       R"cpp(
         struct PointerIntPairInfo {
-          static void *getPointer(void *Value);
+          static void *$decl[[getPointer]](void *Value);
         };
 
         template <typename Info = PointerIntPairInfo> struct PointerIntPair {
diff --git a/clang/lib/Sema/HeuristicResolver.cpp b/clang/lib/Sema/HeuristicResolver.cpp
index d377379c627db..5f1495f5334b9 100644
--- a/clang/lib/Sema/HeuristicResolver.cpp
+++ b/clang/lib/Sema/HeuristicResolver.cpp
@@ -300,9 +300,9 @@ std::vector<const NamedDecl *> HeuristicResolverImpl::resolveMemberExpr(
 
 std::vector<const NamedDecl *>
 HeuristicResolverImpl::resolveDeclRefExpr(const DependentScopeDeclRefExpr *RE) {
-  return resolveDependentMember(
-      resolveNestedNameSpecifierToType(RE->getQualifier()), RE->getDeclName(),
-      StaticFilter);
+  QualType Qualifier = resolveNestedNameSpecifierToType(RE->getQualifier());
+  Qualifier = simplifyType(Qualifier, nullptr, /*UnwrapPointer=*/false);
+  return resolveDependentMember(Qualifier, RE->getDeclName(), StaticFilter);
 }
 
 std::vector<const NamedDecl *>
diff --git a/clang/unittests/Sema/HeuristicResolverTest.cpp b/clang/unittests/Sema/HeuristicResolverTest.cpp
index c7cfe7917c532..b4994c315b2ff 100644
--- a/clang/unittests/Sema/HeuristicResolverTest.cpp
+++ b/clang/unittests/Sema/HeuristicResolverTest.cpp
@@ -429,6 +429,23 @@ TEST(HeuristicResolver, DeclRefExpr_StaticMethod) {
       cxxMethodDecl(hasName("bar")).bind("output"));
 }
 
+TEST(HeuristicResolver, DeclRefExpr_DefaultTemplateArgument) {
+  std::string Code = R"cpp(
+    struct Default {
+      static void foo();
+    };
+    template <typename T = Default>
+    void bar() {
+      T::foo();
+    }
+  )cpp";
+  // Test resolution of "foo" in "T::foo()".
+  expectResolution(
+      Code, &HeuristicResolver::resolveDeclRefExpr,
+      dependentScopeDeclRefExpr(hasDependentName("foo")).bind("input"),
+      cxxMethodDecl(hasName("foo")).bind("output"));
+}
+
 TEST(HeuristicResolver, DeclRefExpr_StaticOverloads) {
   std::string Code = R"cpp(
     template <typename T>

@HighCommander4 HighCommander4 force-pushed the users/HighCommander4/issue-130468 branch from 9aff609 to 16dfb5f Compare March 23, 2025 01:16
@HighCommander4 HighCommander4 force-pushed the users/HighCommander4/issue-1056-static branch from ea94986 to 2c9fc17 Compare March 23, 2025 01:17
@HighCommander4
Copy link
Collaborator Author

(Rebased)

Copy link
Contributor

@zyn0217 zyn0217 left a comment

Choose a reason for hiding this comment

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

Thanks for following up on it, this looks great.

Please flesh out the PR body before you commit, with the reason you said in #132576 (comment).
(Just a copy-paste is good enough)

@HighCommander4
Copy link
Collaborator Author

HighCommander4 commented Mar 28, 2025

(I'm holding off on merging this until #130473 is merged first. There isn't an actual code dependency, it just simplifies the version control workflow to merge things in the order in which I have them stacked locally.)

@HighCommander4 HighCommander4 force-pushed the users/HighCommander4/issue-130468 branch from 16dfb5f to 3884509 Compare April 11, 2025 05:19
Base automatically changed from users/HighCommander4/issue-130468 to main April 11, 2025 06:02
@HighCommander4 HighCommander4 force-pushed the users/HighCommander4/issue-1056-static branch from 787f795 to ac3780d Compare April 11, 2025 06:08
@HighCommander4
Copy link
Collaborator Author

(Rebased)

@HighCommander4 HighCommander4 merged commit fa0498f into main Apr 11, 2025
10 of 11 checks passed
@HighCommander4 HighCommander4 deleted the users/HighCommander4/issue-1056-static branch April 11, 2025 06:35
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
…eDeclRefExpr as well (llvm#132576)

This is a follow-up to llvm#131074.

After moving the default argument heuristic to `simplifyType` in that
patch, the heuristic no longer applied to the 
`DependentScopeDeclRefExpr` case, because that wasn't using
`simplifyType`.

This patch fixes that, with an added testcase.
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 clang-tools-extra clangd
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants