[clang-tools-extra] e588ef8 - [clang-tidy] Small refactor for ExceptionAnalyzer

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 4 00:25:43 PDT 2023


Author: AMS21
Date: 2023-04-04T07:20:26Z
New Revision: e588ef8a7a531380836e17242c2c2276559df0b9

URL: https://github.com/llvm/llvm-project/commit/e588ef8a7a531380836e17242c2c2276559df0b9
DIFF: https://github.com/llvm/llvm-project/commit/e588ef8a7a531380836e17242c2c2276559df0b9.diff

LOG: [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

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
    clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.h

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp b/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
index c8d165e83045..c862303706cc 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<const FunctionDecl *, 32> 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 
diff erent analysis passes
     // with 
diff erent 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 fd65284d570a..a40149ac98d8 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<const FunctionDecl *, ExceptionInfo> FunctionCache;
+  llvm::DenseMap<const FunctionDecl *, ExceptionInfo> FunctionCache{32u};
 };
 
 } // namespace clang::tidy::utils


        


More information about the cfe-commits mailing list