From 7a4cb9bac50c8c19ec0d4ab7f186ef086064a549 Mon Sep 17 00:00:00 2001 From: Nathan Ridge Date: Mon, 24 Feb 2025 11:31:46 -0500 Subject: [PATCH] [clang-tidy][NFC] Expose HeuristicResolver::lookupDependentName() and use it in StandaloneEmptyCheck (#128391) The use replaces CXXRecordDecl::lookupDependentName() which HeuristicResolver aims to supersede. --- .../clang-tidy/bugprone/CMakeLists.txt | 1 + .../bugprone/StandaloneEmptyCheck.cpp | 9 +++++---- clang/include/clang/Sema/HeuristicResolver.h | 7 +++++++ clang/lib/Sema/HeuristicResolver.cpp | 18 ++++++++---------- 4 files changed, 21 insertions(+), 14 deletions(-) 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(ND) && llvm::cast(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(ND) && llvm::cast(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 + lookupDependentName(CXXRecordDecl *RD, DeclarationName Name, + llvm::function_ref 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 + lookupDependentName(CXXRecordDecl *RD, DeclarationName Name, + llvm::function_ref 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 - lookupDependentName(CXXRecordDecl *RD, DeclarationName Name, - llvm::function_ref 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 HeuristicResolver::lookupDependentName( + CXXRecordDecl *RD, DeclarationName Name, + llvm::function_ref Filter) { + return HeuristicResolverImpl(Ctx).lookupDependentName(RD, Name, Filter); +} const QualType HeuristicResolver::getPointeeType(QualType T) const { return HeuristicResolverImpl(Ctx).getPointeeType(T); }