[clang-tools-extra] dca6f60 - [NFC][clang-tidy]improve performance for misc-unused-using-decls check (#78231)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 16 01:45:07 PST 2024
Author: Congcong Cai
Date: 2024-01-16T17:45:03+08:00
New Revision: dca6f60bcdd4a8e6c5defad454d9c470a27701fe
URL: https://github.com/llvm/llvm-project/commit/dca6f60bcdd4a8e6c5defad454d9c470a27701fe
DIFF: https://github.com/llvm/llvm-project/commit/dca6f60bcdd4a8e6c5defad454d9c470a27701fe.diff
LOG: [NFC][clang-tidy]improve performance for misc-unused-using-decls check (#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.
Added:
Modified:
clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
index 051375263e53c3..59e487bab31195 100644
--- a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
@@ -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);
@@ -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
diff erent
// scopes (such as
diff erent namespaces,
diff erent 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;
}
}
@@ -224,6 +229,7 @@ void UnusedUsingDeclsCheck::onEndOfTranslationUnit() {
}
}
Contexts.clear();
+ UsingTargetDeclsCache.clear();
}
} // namespace clang::tidy::misc
diff --git a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h
index 498b3ffd2678c3..7bdaf12e8aecee 100644
--- a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h
+++ b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h
@@ -49,6 +49,7 @@ class UnusedUsingDeclsCheck : public ClangTidyCheck {
};
std::vector<UsingDeclContext> Contexts;
+ llvm::SmallPtrSet<const Decl *, 32> UsingTargetDeclsCache;
StringRef RawStringHeaderFileExtensions;
FileExtensionsSet HeaderFileExtensions;
More information about the cfe-commits
mailing list