Skip to content

Commit

Permalink
[clang-tidy] Small refactor for ExceptionAnalyzer
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
AMS21 authored and PiotrZSL committed Apr 4, 2023
1 parent 25956d5 commit e588ef8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
Expand Up @@ -537,17 +537,18 @@ 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);

// Cache the result of the analysis. This is done prior to filtering
// 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;
}
Expand Down Expand Up @@ -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);
}

Expand Down
12 changes: 6 additions & 6 deletions clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.h
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e588ef8

Please sign in to comment.