Skip to content

[clang-tidy][NFC] Expose HeuristicResolver::lookupDependentName() and use it in StandaloneEmptyCheck #128391

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

Conversation

HighCommander4
Copy link
Collaborator

The use replaces CXXRecordDecl::lookupDependentName() which HeuristicResolver aims to supersede.

… use it in StandaloneEmptyCheck

The use replaces CXXRecordDecl::lookupDependentName() which
HeuristicResolver aims to supersede.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang-tools-extra clang-tidy clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Feb 23, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 23, 2025

@llvm/pr-subscribers-clang-tidy

@llvm/pr-subscribers-clang

Author: Nathan Ridge (HighCommander4)

Changes

The use replaces CXXRecordDecl::lookupDependentName() which HeuristicResolver aims to supersede.


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

4 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt (+1)
  • (modified) clang-tools-extra/clang-tidy/bugprone/StandaloneEmptyCheck.cpp (+5-4)
  • (modified) clang/include/clang/Sema/HeuristicResolver.h (+7)
  • (modified) clang/lib/Sema/HeuristicResolver.cpp (+8-10)
diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
index e8309c68b7fca..cad6b456fc268 100644
--- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
@@ -117,6 +117,7 @@ clang_target_link_libraries(clangTidyBugproneModule
   clangASTMatchers
   clangBasic
   clangLex
+  clangSema
   clangTooling
   clangTransformer
   )
diff --git a/clang-tools-extra/clang-tidy/bugprone/StandaloneEmptyCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/StandaloneEmptyCheck.cpp
index 2bce72cca98c6..a1d7b9931e419 100644
--- a/clang-tools-extra/clang-tidy/bugprone/StandaloneEmptyCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/StandaloneEmptyCheck.cpp
@@ -20,6 +20,7 @@
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Lex/Lexer.h"
+#include "clang/Sema/HeuristicResolver.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/Support/Casting.h"
@@ -125,8 +126,8 @@ void StandaloneEmptyCheck::check(const MatchFinder::MatchResult &Result) {
     DeclarationName Name =
         Context.DeclarationNames.getIdentifier(&Context.Idents.get("clear"));
 
-    auto Candidates = MemberCall->getRecordDecl()->lookupDependentName(
-        Name, [](const NamedDecl *ND) {
+    auto Candidates = HeuristicResolver(Context).lookupDependentName(
+        MemberCall->getRecordDecl(), Name, [](const NamedDecl *ND) {
           return isa<CXXMethodDecl>(ND) &&
                  llvm::cast<CXXMethodDecl>(ND)->getMinRequiredArguments() ==
                      0 &&
@@ -174,8 +175,8 @@ void StandaloneEmptyCheck::check(const MatchFinder::MatchResult &Result) {
     DeclarationName Name =
         Context.DeclarationNames.getIdentifier(&Context.Idents.get("clear"));
 
-    auto Candidates =
-        ArgRecordDecl->lookupDependentName(Name, [](const NamedDecl *ND) {
+    auto Candidates = HeuristicResolver(Context).lookupDependentName(
+        ArgRecordDecl, Name, [](const NamedDecl *ND) {
           return isa<CXXMethodDecl>(ND) &&
                  llvm::cast<CXXMethodDecl>(ND)->getMinRequiredArguments() ==
                      0 &&
diff --git a/clang/include/clang/Sema/HeuristicResolver.h b/clang/include/clang/Sema/HeuristicResolver.h
index 3760003aab89f..f511815b40199 100644
--- a/clang/include/clang/Sema/HeuristicResolver.h
+++ b/clang/include/clang/Sema/HeuristicResolver.h
@@ -69,6 +69,13 @@ class HeuristicResolver {
   QualType
   resolveNestedNameSpecifierToType(const NestedNameSpecifier *NNS) const;
 
+  // Perform an imprecise lookup of a dependent name in `RD`.
+  // This function does not follow strict semantic rules and should be used
+  // only when lookup rules can be relaxed, e.g. indexing.
+  std::vector<const NamedDecl *>
+  lookupDependentName(CXXRecordDecl *RD, DeclarationName Name,
+                      llvm::function_ref<bool(const NamedDecl *ND)> Filter);
+
   // Given the type T of a dependent expression that appears of the LHS of a
   // "->", heuristically find a corresponding pointee type in whose scope we
   // could look up the name appearing on the RHS.
diff --git a/clang/lib/Sema/HeuristicResolver.cpp b/clang/lib/Sema/HeuristicResolver.cpp
index 3af4d001d6c1a..7aecd2a73b539 100644
--- a/clang/lib/Sema/HeuristicResolver.cpp
+++ b/clang/lib/Sema/HeuristicResolver.cpp
@@ -44,6 +44,9 @@ class HeuristicResolverImpl {
       const DependentTemplateSpecializationType *DTST);
   QualType resolveNestedNameSpecifierToType(const NestedNameSpecifier *NNS);
   QualType getPointeeType(QualType T);
+  std::vector<const NamedDecl *>
+  lookupDependentName(CXXRecordDecl *RD, DeclarationName Name,
+                      llvm::function_ref<bool(const NamedDecl *ND)> Filter);
 
 private:
   ASTContext &Ctx;
@@ -83,16 +86,6 @@ class HeuristicResolverImpl {
   // during simplification, and the operation fails if no pointer type is found.
   QualType simplifyType(QualType Type, const Expr *E, bool UnwrapPointer);
 
-  // This is a reimplementation of CXXRecordDecl::lookupDependentName()
-  // so that the implementation can call into other HeuristicResolver helpers.
-  // FIXME: Once HeuristicResolver is upstreamed to the clang libraries
-  // (https://github.com/clangd/clangd/discussions/1662),
-  // CXXRecordDecl::lookupDepenedentName() can be removed, and its call sites
-  // can be modified to benefit from the more comprehensive heuristics offered
-  // by HeuristicResolver instead.
-  std::vector<const NamedDecl *>
-  lookupDependentName(CXXRecordDecl *RD, DeclarationName Name,
-                      llvm::function_ref<bool(const NamedDecl *ND)> Filter);
   bool findOrdinaryMemberInDependentClasses(const CXXBaseSpecifier *Specifier,
                                             CXXBasePath &Path,
                                             DeclarationName Name);
@@ -539,6 +532,11 @@ QualType HeuristicResolver::resolveNestedNameSpecifierToType(
     const NestedNameSpecifier *NNS) const {
   return HeuristicResolverImpl(Ctx).resolveNestedNameSpecifierToType(NNS);
 }
+std::vector<const NamedDecl *> HeuristicResolver::lookupDependentName(
+    CXXRecordDecl *RD, DeclarationName Name,
+    llvm::function_ref<bool(const NamedDecl *ND)> Filter) {
+  return HeuristicResolverImpl(Ctx).lookupDependentName(RD, Name, Filter);
+}
 const QualType HeuristicResolver::getPointeeType(QualType T) const {
   return HeuristicResolverImpl(Ctx).getPointeeType(T);
 }

@HighCommander4
Copy link
Collaborator Author

Note: this is the last remaining use of CXXRecordDecl::lookupDependentName(). I plan to remove it in a follow-up patch.

@HighCommander4
Copy link
Collaborator Author

Note: this is the last remaining use of CXXRecordDecl::lookupDependentName(). I plan to remove it in a follow-up patch.

The follow-up patch is #128392.

Copy link
Contributor

@HerrCai0907 HerrCai0907 left a comment

Choose a reason for hiding this comment

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

LGTM as temporary solution
But clang-tidy actually do not want to reply on Sema. I take a look briefly about HeuristicResolver, it looks like not related to other Sema parts.
Is it possible to move the whole HeuristicResolver out of Sema lib to AST?

@HighCommander4
Copy link
Collaborator Author

HighCommander4 commented Feb 23, 2025

I take a look briefly about HeuristicResolver, it looks like not related to other Sema parts. Is it possible to move the whole HeuristicResolver out of Sema lib to AST?

In future enhancements to HeuristicResolver, I would like to implement optional heuristics that make use of Sema. I envisioned the dependency on Sema being optional, such that clients who do not have one can continue to construct a HeursiticResolver without passing in a Sema.

@HighCommander4 HighCommander4 merged commit 7a4cb9b into main Feb 24, 2025
16 checks passed
@HighCommander4 HighCommander4 deleted the users/HighCommander4/lookupdependentname-clangtidy branch February 24, 2025 16:31
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-tidy clang-tools-extra
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants