Skip to content

Commit 7a4cb9b

Browse files
[clang-tidy][NFC] Expose HeuristicResolver::lookupDependentName() and use it in StandaloneEmptyCheck (#128391)
The use replaces CXXRecordDecl::lookupDependentName() which HeuristicResolver aims to supersede.
1 parent 4defac9 commit 7a4cb9b

File tree

4 files changed

+21
-14
lines changed

4 files changed

+21
-14
lines changed

clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ clang_target_link_libraries(clangTidyBugproneModule
117117
clangASTMatchers
118118
clangBasic
119119
clangLex
120+
clangSema
120121
clangTooling
121122
clangTransformer
122123
)

clang-tools-extra/clang-tidy/bugprone/StandaloneEmptyCheck.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "clang/Basic/Diagnostic.h"
2121
#include "clang/Basic/SourceLocation.h"
2222
#include "clang/Lex/Lexer.h"
23+
#include "clang/Sema/HeuristicResolver.h"
2324
#include "llvm/ADT/STLExtras.h"
2425
#include "llvm/ADT/SmallVector.h"
2526
#include "llvm/Support/Casting.h"
@@ -125,8 +126,8 @@ void StandaloneEmptyCheck::check(const MatchFinder::MatchResult &Result) {
125126
DeclarationName Name =
126127
Context.DeclarationNames.getIdentifier(&Context.Idents.get("clear"));
127128

128-
auto Candidates = MemberCall->getRecordDecl()->lookupDependentName(
129-
Name, [](const NamedDecl *ND) {
129+
auto Candidates = HeuristicResolver(Context).lookupDependentName(
130+
MemberCall->getRecordDecl(), Name, [](const NamedDecl *ND) {
130131
return isa<CXXMethodDecl>(ND) &&
131132
llvm::cast<CXXMethodDecl>(ND)->getMinRequiredArguments() ==
132133
0 &&
@@ -174,8 +175,8 @@ void StandaloneEmptyCheck::check(const MatchFinder::MatchResult &Result) {
174175
DeclarationName Name =
175176
Context.DeclarationNames.getIdentifier(&Context.Idents.get("clear"));
176177

177-
auto Candidates =
178-
ArgRecordDecl->lookupDependentName(Name, [](const NamedDecl *ND) {
178+
auto Candidates = HeuristicResolver(Context).lookupDependentName(
179+
ArgRecordDecl, Name, [](const NamedDecl *ND) {
179180
return isa<CXXMethodDecl>(ND) &&
180181
llvm::cast<CXXMethodDecl>(ND)->getMinRequiredArguments() ==
181182
0 &&

clang/include/clang/Sema/HeuristicResolver.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ class HeuristicResolver {
6969
QualType
7070
resolveNestedNameSpecifierToType(const NestedNameSpecifier *NNS) const;
7171

72+
// Perform an imprecise lookup of a dependent name in `RD`.
73+
// This function does not follow strict semantic rules and should be used
74+
// only when lookup rules can be relaxed, e.g. indexing.
75+
std::vector<const NamedDecl *>
76+
lookupDependentName(CXXRecordDecl *RD, DeclarationName Name,
77+
llvm::function_ref<bool(const NamedDecl *ND)> Filter);
78+
7279
// Given the type T of a dependent expression that appears of the LHS of a
7380
// "->", heuristically find a corresponding pointee type in whose scope we
7481
// could look up the name appearing on the RHS.

clang/lib/Sema/HeuristicResolver.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class HeuristicResolverImpl {
4444
const DependentTemplateSpecializationType *DTST);
4545
QualType resolveNestedNameSpecifierToType(const NestedNameSpecifier *NNS);
4646
QualType getPointeeType(QualType T);
47+
std::vector<const NamedDecl *>
48+
lookupDependentName(CXXRecordDecl *RD, DeclarationName Name,
49+
llvm::function_ref<bool(const NamedDecl *ND)> Filter);
4750

4851
private:
4952
ASTContext &Ctx;
@@ -83,16 +86,6 @@ class HeuristicResolverImpl {
8386
// during simplification, and the operation fails if no pointer type is found.
8487
QualType simplifyType(QualType Type, const Expr *E, bool UnwrapPointer);
8588

86-
// This is a reimplementation of CXXRecordDecl::lookupDependentName()
87-
// so that the implementation can call into other HeuristicResolver helpers.
88-
// FIXME: Once HeuristicResolver is upstreamed to the clang libraries
89-
// (https://github.com/clangd/clangd/discussions/1662),
90-
// CXXRecordDecl::lookupDepenedentName() can be removed, and its call sites
91-
// can be modified to benefit from the more comprehensive heuristics offered
92-
// by HeuristicResolver instead.
93-
std::vector<const NamedDecl *>
94-
lookupDependentName(CXXRecordDecl *RD, DeclarationName Name,
95-
llvm::function_ref<bool(const NamedDecl *ND)> Filter);
9689
bool findOrdinaryMemberInDependentClasses(const CXXBaseSpecifier *Specifier,
9790
CXXBasePath &Path,
9891
DeclarationName Name);
@@ -539,6 +532,11 @@ QualType HeuristicResolver::resolveNestedNameSpecifierToType(
539532
const NestedNameSpecifier *NNS) const {
540533
return HeuristicResolverImpl(Ctx).resolveNestedNameSpecifierToType(NNS);
541534
}
535+
std::vector<const NamedDecl *> HeuristicResolver::lookupDependentName(
536+
CXXRecordDecl *RD, DeclarationName Name,
537+
llvm::function_ref<bool(const NamedDecl *ND)> Filter) {
538+
return HeuristicResolverImpl(Ctx).lookupDependentName(RD, Name, Filter);
539+
}
542540
const QualType HeuristicResolver::getPointeeType(QualType T) const {
543541
return HeuristicResolverImpl(Ctx).getPointeeType(T);
544542
}

0 commit comments

Comments
 (0)