From e588ef8a7a531380836e17242c2c2276559df0b9 Mon Sep 17 00:00:00 2001 From: AMS21 Date: Tue, 4 Apr 2023 06:39:15 +0000 Subject: [PATCH] [clang-tidy] Small refactor for ExceptionAnalyzer - Use llvm::DenseMap<> with pre-allocation instead of std::map<> for FunctionCache - Avoid double lookup for FunctionCache - Use try_emplace instead of insert - Simplify definition of State enum Reviewed By: PiotrZSL Differential Revision: https://reviews.llvm.org/D147376 --- .../clang-tidy/utils/ExceptionAnalyzer.cpp | 10 +++++----- .../clang-tidy/utils/ExceptionAnalyzer.h | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp b/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp index c8d165e83045c..c862303706ccb 100644 --- a/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp +++ b/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp @@ -537,7 +537,8 @@ ExceptionAnalyzer::analyzeImpl(const FunctionDecl *Func) { ExceptionInfo ExceptionList; // Check if the function has already been analyzed and reuse that result. - if (FunctionCache.count(Func) == 0) { + const auto CacheEntry = FunctionCache.find(Func); + if (CacheEntry == FunctionCache.end()) { llvm::SmallSet CallStack; ExceptionList = throwsException(Func, CallStack); @@ -545,9 +546,9 @@ ExceptionAnalyzer::analyzeImpl(const FunctionDecl *Func) { // because it is best to keep as much information as possible. // The results here might be relevant to different analysis passes // with different needs as well. - FunctionCache.insert(std::make_pair(Func, ExceptionList)); + FunctionCache.try_emplace(Func, ExceptionList); } else - ExceptionList = FunctionCache[Func]; + ExceptionList = CacheEntry->getSecond(); return ExceptionList; } @@ -579,8 +580,7 @@ ExceptionAnalyzer::analyze(const FunctionDecl *Func) { return analyzeDispatch(Func); } -ExceptionAnalyzer::ExceptionInfo -ExceptionAnalyzer::analyze(const Stmt *Stmt) { +ExceptionAnalyzer::ExceptionInfo ExceptionAnalyzer::analyze(const Stmt *Stmt) { return analyzeDispatch(Stmt); } diff --git a/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.h b/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.h index fd65284d570ab..a40149ac98d84 100644 --- a/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.h +++ b/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.h @@ -21,11 +21,11 @@ namespace clang::tidy::utils { /// custom exception types. class ExceptionAnalyzer { public: - enum class State : std::int8_t { - Throwing = 0, ///< The function can definitely throw given an AST. - NotThrowing = 1, ///< This function can not throw, given an AST. - Unknown = 2, ///< This can happen for extern functions without available - ///< definition. + enum class State { + Throwing, ///< The function can definitely throw given an AST. + NotThrowing, ///< This function can not throw, given an AST. + Unknown, ///< This can happen for extern functions without available + ///< definition. }; /// Bundle the gathered information about an entity like a function regarding @@ -144,7 +144,7 @@ class ExceptionAnalyzer { bool IgnoreBadAlloc = true; llvm::StringSet<> IgnoredExceptions; - std::map FunctionCache; + llvm::DenseMap FunctionCache{32u}; }; } // namespace clang::tidy::utils