Skip to content

Commit 7eee805

Browse files
committed
Clear statistics snapshots between unit tests
1 parent a00774d commit 7eee805

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

clang/include/clang/StaticAnalyzer/Core/PathSensitive/EntryPointStats.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef CLANG_INCLUDE_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_ENTRYPOINTSTATS_H
1010
#define CLANG_INCLUDE_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_ENTRYPOINTSTATS_H
1111

12+
#include "clang/AST/ASTContext.h"
1213
#include "llvm/ADT/Statistic.h"
1314
#include "llvm/ADT/StringRef.h"
1415

@@ -25,7 +26,7 @@ class EntryPointStat {
2526
public:
2627
llvm::StringLiteral name() const { return Name; }
2728

28-
static void lockRegistry(llvm::StringRef CPPFileName);
29+
static void lockRegistry(llvm::StringRef CPPFileName, ASTContext &Ctx);
2930

3031
static void takeSnapshot(const Decl *EntryPoint);
3132
static void dumpStatsAsCSV(llvm::raw_ostream &OS);

clang/lib/StaticAnalyzer/Core/EntryPointStats.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ template <typename Callback> void enumerateStatVectors(const Callback &Fn) {
5959
Fn(StatsRegistry->MaxStats);
6060
Fn(StatsRegistry->CounterStats);
6161
}
62+
63+
void clearSnapshots(void*) { StatsRegistry->Snapshots.clear(); }
64+
6265
} // namespace
6366

6467
static void checkStatName(const EntryPointStat *M) {
@@ -78,7 +81,7 @@ static void checkStatName(const EntryPointStat *M) {
7881
}
7982
}
8083

81-
void EntryPointStat::lockRegistry(llvm::StringRef CPPFileName) {
84+
void EntryPointStat::lockRegistry(llvm::StringRef CPPFileName, ASTContext &Ctx) {
8285
auto CmpByNames = [](const EntryPointStat *L, const EntryPointStat *R) {
8386
return L->name() < R->name();
8487
};
@@ -89,6 +92,10 @@ void EntryPointStat::lockRegistry(llvm::StringRef CPPFileName) {
8992
StatsRegistry->IsLocked = true;
9093
llvm::raw_string_ostream OS(StatsRegistry->EscapedCPPFileName);
9194
llvm::printEscapedString(CPPFileName, OS);
95+
// Make sure snapshots (that reference function Decl's) do not persist after
96+
// the AST is destroyed. This is especially relevant in the context of unit
97+
// tests that construct and destruct multiple ASTs in the same process.
98+
Ctx.AddDeallocation(clearSnapshots, nullptr);
9299
}
93100

94101
[[maybe_unused]] static bool isRegistered(llvm::StringLiteral Name) {

clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class AnalysisConsumer : public AnalysisASTConsumer,
140140
Injector(std::move(injector)), CTU(CI),
141141
MacroExpansions(CI.getLangOpts()) {
142142

143-
EntryPointStat::lockRegistry(getMainFileName(CI.getInvocation()));
143+
EntryPointStat::lockRegistry(getMainFileName(CI.getInvocation()), CI.getASTContext());
144144
DigestAnalyzerOptions();
145145

146146
if (Opts.AnalyzerDisplayProgress || Opts.PrintStats ||

0 commit comments

Comments
 (0)