Skip to content

Commit

Permalink
[NFC][clang-tidy]improve performance for misc-unused-using-decls check (
Browse files Browse the repository at this point in the history
#78231)

`UnusedUsingDeclsCheck::removeFromFoundDecls` will be called with high
frequency. At current time it will check every `Context`.
This patch adds a cache to reduce algorithm complexity.
  • Loading branch information
HerrCai0907 committed Jan 16, 2024
1 parent a690e86 commit dca6f60
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
14 changes: 10 additions & 4 deletions clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,10 @@ void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
/*SkipTrailingWhitespaceAndNewLine=*/true));
for (const auto *UsingShadow : Using->shadows()) {
const auto *TargetDecl = UsingShadow->getTargetDecl()->getCanonicalDecl();
if (shouldCheckDecl(TargetDecl))
if (shouldCheckDecl(TargetDecl)) {
Context.UsingTargetDecls.insert(TargetDecl);
UsingTargetDeclsCache.insert(TargetDecl);
}
}
if (!Context.UsingTargetDecls.empty())
Contexts.push_back(Context);
Expand Down Expand Up @@ -201,13 +203,16 @@ void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
void UnusedUsingDeclsCheck::removeFromFoundDecls(const Decl *D) {
if (!D)
return;
const Decl *CanonicalDecl = D->getCanonicalDecl();
if (!UsingTargetDeclsCache.contains(CanonicalDecl))
return;
// FIXME: Currently, we don't handle the using-decls being used in different
// scopes (such as different namespaces, different functions). Instead of
// giving an incorrect message, we mark all of them as used.
//
// FIXME: Use a more efficient way to find a matching context.
for (auto &Context : Contexts) {
if (Context.UsingTargetDecls.contains(D->getCanonicalDecl()))
if (Context.IsUsed)
continue;
if (Context.UsingTargetDecls.contains(CanonicalDecl))
Context.IsUsed = true;
}
}
Expand All @@ -224,6 +229,7 @@ void UnusedUsingDeclsCheck::onEndOfTranslationUnit() {
}
}
Contexts.clear();
UsingTargetDeclsCache.clear();
}

} // namespace clang::tidy::misc
1 change: 1 addition & 0 deletions clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class UnusedUsingDeclsCheck : public ClangTidyCheck {
};

std::vector<UsingDeclContext> Contexts;
llvm::SmallPtrSet<const Decl *, 32> UsingTargetDeclsCache;

StringRef RawStringHeaderFileExtensions;
FileExtensionsSet HeaderFileExtensions;
Expand Down

0 comments on commit dca6f60

Please sign in to comment.