AMS21 created this revision.
AMS21 added a reviewer: njames93.
Herald added subscribers: PiotrZSL, carlosgalvezp, manas, ASDenysPetrov, 
dkrupp, donat.nagy, Szelethus, a.sidorin, baloghadamsoftware, xazax.hun.
Herald added a project: All.
AMS21 requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

- 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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D147376

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


Index: clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.h
===================================================================
--- clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.h
+++ clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.h
@@ -21,11 +21,11 @@
 /// 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 @@
 
   bool IgnoreBadAlloc = true;
   llvm::StringSet<> IgnoredExceptions;
-  std::map<const FunctionDecl *, ExceptionInfo> FunctionCache;
+  llvm::DenseMap<const FunctionDecl *, ExceptionInfo> FunctionCache{32u};
 };
 
 } // namespace clang::tidy::utils
Index: clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
===================================================================
--- clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
+++ clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
@@ -537,7 +537,8 @@
   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 @@
     // 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 @@
   return analyzeDispatch(Func);
 }
 
-ExceptionAnalyzer::ExceptionInfo
-ExceptionAnalyzer::analyze(const Stmt *Stmt) {
+ExceptionAnalyzer::ExceptionInfo ExceptionAnalyzer::analyze(const Stmt *Stmt) {
   return analyzeDispatch(Stmt);
 }
 


Index: clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.h
===================================================================
--- clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.h
+++ clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.h
@@ -21,11 +21,11 @@
 /// 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 @@
 
   bool IgnoreBadAlloc = true;
   llvm::StringSet<> IgnoredExceptions;
-  std::map<const FunctionDecl *, ExceptionInfo> FunctionCache;
+  llvm::DenseMap<const FunctionDecl *, ExceptionInfo> FunctionCache{32u};
 };
 
 } // namespace clang::tidy::utils
Index: clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
===================================================================
--- clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
+++ clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
@@ -537,7 +537,8 @@
   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 @@
     // 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 @@
   return analyzeDispatch(Func);
 }
 
-ExceptionAnalyzer::ExceptionInfo
-ExceptionAnalyzer::analyze(const Stmt *Stmt) {
+ExceptionAnalyzer::ExceptionInfo ExceptionAnalyzer::analyze(const Stmt *Stmt) {
   return analyzeDispatch(Stmt);
 }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to