Skip to content

Commit 3dc1594

Browse files
authored
[analyzer] Clean up slightly the messed up ownership model of the analyzer (#128368)
Well, yes. It's not pretty. At least after this we would have a bit more unique pointers than before. This is for fixing the memory leak diagnosed by: https://lab.llvm.org/buildbot/#/builders/24/builds/5580 And that caused the revert of #127409. After these uptrs that patch can re-land finally.
1 parent e7ad07f commit 3dc1594

15 files changed

+77
-72
lines changed

clang/include/clang/Analysis/AnalysisDeclContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ class AnalysisDeclContextManager {
451451
bool synthesizeBodies = false, bool addStaticInitBranches = false,
452452
bool addCXXNewAllocator = true, bool addRichCXXConstructors = true,
453453
bool markElidedCXXConstructors = true, bool addVirtualBaseBranches = true,
454-
CodeInjector *injector = nullptr);
454+
std::unique_ptr<CodeInjector> injector = nullptr);
455455

456456
AnalysisDeclContext *getContext(const Decl *D);
457457

clang/include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,8 @@ class BugReporterData {
570570
public:
571571
virtual ~BugReporterData() = default;
572572

573-
virtual ArrayRef<PathDiagnosticConsumer*> getPathDiagnosticConsumers() = 0;
573+
virtual ArrayRef<std::unique_ptr<PathDiagnosticConsumer>>
574+
getPathDiagnosticConsumers() = 0;
574575
virtual ASTContext &getASTContext() = 0;
575576
virtual SourceManager &getSourceManager() = 0;
576577
virtual AnalyzerOptions &getAnalyzerOptions() = 0;
@@ -608,7 +609,8 @@ class BugReporter {
608609
/// Generate and flush diagnostics for all bug reports.
609610
void FlushReports();
610611

611-
ArrayRef<PathDiagnosticConsumer*> getPathDiagnosticConsumers() {
612+
ArrayRef<std::unique_ptr<PathDiagnosticConsumer>>
613+
getPathDiagnosticConsumers() {
612614
return D.getPathDiagnosticConsumers();
613615
}
614616

@@ -670,9 +672,10 @@ class BugReporter {
670672
protected:
671673
/// Generate the diagnostics for the given bug report.
672674
virtual std::unique_ptr<DiagnosticForConsumerMapTy>
673-
generateDiagnosticForConsumerMap(BugReport *exampleReport,
674-
ArrayRef<PathDiagnosticConsumer *> consumers,
675-
ArrayRef<BugReport *> bugReports);
675+
generateDiagnosticForConsumerMap(
676+
BugReport *exampleReport,
677+
ArrayRef<std::unique_ptr<PathDiagnosticConsumer>> consumers,
678+
ArrayRef<BugReport *> bugReports);
676679
};
677680

678681
/// GRBugReporter is used for generating path-sensitive reports.
@@ -684,10 +687,11 @@ class PathSensitiveBugReporter final : public BugReporter {
684687
SmallVectorImpl<BugReport *> &bugReports) override;
685688

686689
/// Generate the diagnostics for the given bug report.
687-
std::unique_ptr<DiagnosticForConsumerMapTy>
688-
generateDiagnosticForConsumerMap(BugReport *exampleReport,
689-
ArrayRef<PathDiagnosticConsumer *> consumers,
690-
ArrayRef<BugReport *> bugReports) override;
690+
std::unique_ptr<DiagnosticForConsumerMapTy> generateDiagnosticForConsumerMap(
691+
BugReport *exampleReport,
692+
ArrayRef<std::unique_ptr<PathDiagnosticConsumer>> consumers,
693+
ArrayRef<BugReport *> bugReports) override;
694+
691695
public:
692696
PathSensitiveBugReporter(BugReporterData& d, ExprEngine& eng)
693697
: BugReporter(d), Eng(eng) {}
@@ -706,7 +710,7 @@ class PathSensitiveBugReporter final : public BugReporter {
706710
/// Iterates through the bug reports within a single equivalence class,
707711
/// stops at a first non-invalidated report.
708712
std::unique_ptr<DiagnosticForConsumerMapTy> generatePathDiagnostics(
709-
ArrayRef<PathDiagnosticConsumer *> consumers,
713+
ArrayRef<std::unique_ptr<PathDiagnosticConsumer>> consumers,
710714
ArrayRef<PathSensitiveBugReport *> &bugReports);
711715

712716
void emitReport(std::unique_ptr<BugReport> R) override;

clang/include/clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class CrossTranslationUnitContext;
3030
namespace ento {
3131

3232
class PathDiagnosticConsumer;
33-
typedef std::vector<PathDiagnosticConsumer*> PathDiagnosticConsumers;
33+
using PathDiagnosticConsumers =
34+
std::vector<std::unique_ptr<PathDiagnosticConsumer>>;
3435

3536
#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN) \
3637
void CREATEFN(PathDiagnosticConsumerOptions Diagopts, \

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ class AnalysisManager : public BugReporterData {
4747
AnalyzerOptions &options;
4848

4949
AnalysisManager(ASTContext &ctx, Preprocessor &PP,
50-
const PathDiagnosticConsumers &Consumers,
50+
PathDiagnosticConsumers Consumers,
5151
StoreManagerCreator storemgr,
5252
ConstraintManagerCreator constraintmgr,
5353
CheckerManager *checkerMgr, AnalyzerOptions &Options,
54-
CodeInjector *injector = nullptr);
54+
std::unique_ptr<CodeInjector> injector = nullptr);
5555

5656
~AnalysisManager() override;
5757

@@ -91,7 +91,8 @@ class AnalysisManager : public BugReporterData {
9191
return LangOpts;
9292
}
9393

94-
ArrayRef<PathDiagnosticConsumer*> getPathDiagnosticConsumers() override {
94+
ArrayRef<std::unique_ptr<PathDiagnosticConsumer>>
95+
getPathDiagnosticConsumers() override {
9596
return PathConsumers;
9697
}
9798

clang/include/clang/StaticAnalyzer/Frontend/AnalysisConsumer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class CheckerRegistry;
2929

3030
class AnalysisASTConsumer : public ASTConsumer {
3131
public:
32-
virtual void AddDiagnosticConsumer(PathDiagnosticConsumer *Consumer) = 0;
32+
virtual void
33+
AddDiagnosticConsumer(std::unique_ptr<PathDiagnosticConsumer> Consumer) = 0;
3334

3435
/// This method allows registering statically linked custom checkers that are
3536
/// not a part of the Clang tree. It employs the same mechanism that is used

clang/lib/Analysis/AnalysisDeclContext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ AnalysisDeclContextManager::AnalysisDeclContextManager(
7171
bool addLoopExit, bool addScopes, bool synthesizeBodies,
7272
bool addStaticInitBranch, bool addCXXNewAllocator,
7373
bool addRichCXXConstructors, bool markElidedCXXConstructors,
74-
bool addVirtualBaseBranches, CodeInjector *injector)
75-
: Injector(injector), FunctionBodyFarm(ASTCtx, injector),
74+
bool addVirtualBaseBranches, std::unique_ptr<CodeInjector> injector)
75+
: Injector(std::move(injector)), FunctionBodyFarm(ASTCtx, Injector.get()),
7676
SynthesizeBodies(synthesizeBodies) {
7777
cfgBuildOptions.PruneTriviallyFalseEdges = !useUnoptimizedCFG;
7878
cfgBuildOptions.AddImplicitDtors = addImplicitDtors;

clang/lib/StaticAnalyzer/Core/AnalysisManager.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,28 @@ using namespace ento;
1414
void AnalysisManager::anchor() { }
1515

1616
AnalysisManager::AnalysisManager(ASTContext &ASTCtx, Preprocessor &PP,
17-
const PathDiagnosticConsumers &PDC,
17+
PathDiagnosticConsumers PDC,
1818
StoreManagerCreator storemgr,
1919
ConstraintManagerCreator constraintmgr,
2020
CheckerManager *checkerMgr,
2121
AnalyzerOptions &Options,
22-
CodeInjector *injector)
22+
std::unique_ptr<CodeInjector> injector)
2323
: AnaCtxMgr(
2424
ASTCtx, Options.UnoptimizedCFG,
2525
Options.ShouldIncludeImplicitDtorsInCFG,
26-
/*addInitializers=*/true,
27-
Options.ShouldIncludeTemporaryDtorsInCFG,
26+
/*addInitializers=*/true, Options.ShouldIncludeTemporaryDtorsInCFG,
2827
Options.ShouldIncludeLifetimeInCFG,
2928
// Adding LoopExit elements to the CFG is a requirement for loop
3029
// unrolling.
31-
Options.ShouldIncludeLoopExitInCFG ||
32-
Options.ShouldUnrollLoops,
33-
Options.ShouldIncludeScopesInCFG,
34-
Options.ShouldSynthesizeBodies,
30+
Options.ShouldIncludeLoopExitInCFG || Options.ShouldUnrollLoops,
31+
Options.ShouldIncludeScopesInCFG, Options.ShouldSynthesizeBodies,
3532
Options.ShouldConditionalizeStaticInitializers,
3633
/*addCXXNewAllocator=*/true,
3734
Options.ShouldIncludeRichConstructorsInCFG,
3835
Options.ShouldElideConstructors,
39-
/*addVirtualBaseBranches=*/true,
40-
injector),
36+
/*addVirtualBaseBranches=*/true, std::move(injector)),
4137
Ctx(ASTCtx), PP(PP), LangOpts(ASTCtx.getLangOpts()),
42-
PathConsumers(PDC), CreateStoreMgr(storemgr),
38+
PathConsumers(std::move(PDC)), CreateStoreMgr(storemgr),
4339
CreateConstraintMgr(constraintmgr), CheckerMgr(checkerMgr),
4440
options(Options) {
4541
AnaCtxMgr.getCFGBuildOptions().setAllAlwaysAdd();
@@ -50,14 +46,11 @@ AnalysisManager::AnalysisManager(ASTContext &ASTCtx, Preprocessor &PP,
5046

5147
AnalysisManager::~AnalysisManager() {
5248
FlushDiagnostics();
53-
for (PathDiagnosticConsumer *Consumer : PathConsumers) {
54-
delete Consumer;
55-
}
5649
}
5750

5851
void AnalysisManager::FlushDiagnostics() {
5952
PathDiagnosticConsumer::FilesMade filesMade;
60-
for (PathDiagnosticConsumer *Consumer : PathConsumers) {
53+
for (const auto &Consumer : PathConsumers) {
6154
Consumer->FlushDiagnostics(&filesMade);
6255
}
6356
}

clang/lib/StaticAnalyzer/Core/BugReporter.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2958,7 +2958,7 @@ std::optional<PathDiagnosticBuilder> PathDiagnosticBuilder::findValidReport(
29582958

29592959
std::unique_ptr<DiagnosticForConsumerMapTy>
29602960
PathSensitiveBugReporter::generatePathDiagnostics(
2961-
ArrayRef<PathDiagnosticConsumer *> consumers,
2961+
ArrayRef<std::unique_ptr<PathDiagnosticConsumer>> consumers,
29622962
ArrayRef<PathSensitiveBugReport *> &bugReports) {
29632963
assert(!bugReports.empty());
29642964

@@ -2968,9 +2968,9 @@ PathSensitiveBugReporter::generatePathDiagnostics(
29682968
PathDiagnosticBuilder::findValidReport(bugReports, *this);
29692969

29702970
if (PDB) {
2971-
for (PathDiagnosticConsumer *PC : consumers) {
2972-
if (std::unique_ptr<PathDiagnostic> PD = PDB->generate(PC)) {
2973-
(*Out)[PC] = std::move(PD);
2971+
for (const auto &PC : consumers) {
2972+
if (std::unique_ptr<PathDiagnostic> PD = PDB->generate(PC.get())) {
2973+
(*Out)[PC.get()] = std::move(PD);
29742974
}
29752975
}
29762976
}
@@ -3164,7 +3164,7 @@ void BugReporter::FlushReport(BugReportEquivClass &EQ) {
31643164
return;
31653165
}
31663166

3167-
ArrayRef<PathDiagnosticConsumer*> Consumers = getPathDiagnosticConsumers();
3167+
ArrayRef Consumers = getPathDiagnosticConsumers();
31683168
std::unique_ptr<DiagnosticForConsumerMapTy> Diagnostics =
31693169
generateDiagnosticForConsumerMap(report, Consumers, bugReports);
31703170

@@ -3298,12 +3298,13 @@ findExecutedLines(const SourceManager &SM, const ExplodedNode *N) {
32983298

32993299
std::unique_ptr<DiagnosticForConsumerMapTy>
33003300
BugReporter::generateDiagnosticForConsumerMap(
3301-
BugReport *exampleReport, ArrayRef<PathDiagnosticConsumer *> consumers,
3301+
BugReport *exampleReport,
3302+
ArrayRef<std::unique_ptr<PathDiagnosticConsumer>> consumers,
33023303
ArrayRef<BugReport *> bugReports) {
33033304
auto *basicReport = cast<BasicBugReport>(exampleReport);
33043305
auto Out = std::make_unique<DiagnosticForConsumerMapTy>();
3305-
for (auto *Consumer : consumers)
3306-
(*Out)[Consumer] =
3306+
for (const auto &Consumer : consumers)
3307+
(*Out)[Consumer.get()] =
33073308
generateDiagnosticForBasicReport(basicReport, AnalysisEntryPoint);
33083309
return Out;
33093310
}
@@ -3371,11 +3372,10 @@ static void resetDiagnosticLocationToMainFile(PathDiagnostic &PD) {
33713372
}
33723373
}
33733374

3374-
3375-
33763375
std::unique_ptr<DiagnosticForConsumerMapTy>
33773376
PathSensitiveBugReporter::generateDiagnosticForConsumerMap(
3378-
BugReport *exampleReport, ArrayRef<PathDiagnosticConsumer *> consumers,
3377+
BugReport *exampleReport,
3378+
ArrayRef<std::unique_ptr<PathDiagnosticConsumer>> consumers,
33793379
ArrayRef<BugReport *> bugReports) {
33803380
std::vector<BasicBugReport *> BasicBugReports;
33813381
std::vector<PathSensitiveBugReport *> PathSensitiveBugReports;

clang/lib/StaticAnalyzer/Core/HTMLDiagnostics.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ void ento::createHTMLDiagnosticConsumer(
193193
if (OutputDir.empty())
194194
return;
195195

196-
C.push_back(new HTMLDiagnostics(std::move(DiagOpts), OutputDir, PP, true));
196+
C.emplace_back(std::make_unique<HTMLDiagnostics>(std::move(DiagOpts),
197+
OutputDir, PP, true));
197198
}
198199

199200
void ento::createHTMLSingleFileDiagnosticConsumer(
@@ -208,7 +209,8 @@ void ento::createHTMLSingleFileDiagnosticConsumer(
208209
if (OutputDir.empty())
209210
return;
210211

211-
C.push_back(new HTMLDiagnostics(std::move(DiagOpts), OutputDir, PP, false));
212+
C.emplace_back(std::make_unique<HTMLDiagnostics>(std::move(DiagOpts),
213+
OutputDir, PP, false));
212214
}
213215

214216
void ento::createPlistHTMLDiagnosticConsumer(

clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -541,9 +541,9 @@ void ento::createPlistDiagnosticConsumer(
541541
if (OutputFile.empty())
542542
return;
543543

544-
C.push_back(new PlistDiagnostics(DiagOpts, OutputFile, PP, CTU,
545-
MacroExpansions,
546-
/*supportsMultipleFiles=*/false));
544+
C.push_back(std::make_unique<PlistDiagnostics>(
545+
DiagOpts, OutputFile, PP, CTU, MacroExpansions,
546+
/*supportsMultipleFiles=*/false));
547547
createTextMinimalPathDiagnosticConsumer(std::move(DiagOpts), C, OutputFile,
548548
PP, CTU, MacroExpansions);
549549
}
@@ -558,9 +558,9 @@ void ento::createPlistMultiFileDiagnosticConsumer(
558558
if (OutputFile.empty())
559559
return;
560560

561-
C.push_back(new PlistDiagnostics(DiagOpts, OutputFile, PP, CTU,
562-
MacroExpansions,
563-
/*supportsMultipleFiles=*/true));
561+
C.push_back(std::make_unique<PlistDiagnostics>(
562+
DiagOpts, OutputFile, PP, CTU, MacroExpansions,
563+
/*supportsMultipleFiles=*/true));
564564
createTextMinimalPathDiagnosticConsumer(std::move(DiagOpts), C, OutputFile,
565565
PP, CTU, MacroExpansions);
566566
}

clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/Support/ConvertUTF.h"
2424
#include "llvm/Support/JSON.h"
2525
#include "llvm/Support/Path.h"
26+
#include <memory>
2627

2728
using namespace llvm;
2829
using namespace clang;
@@ -60,8 +61,8 @@ void ento::createSarifDiagnosticConsumer(
6061
if (Output.empty())
6162
return;
6263

63-
C.push_back(
64-
new SarifDiagnostics(Output, PP.getLangOpts(), PP.getSourceManager()));
64+
C.push_back(std::make_unique<SarifDiagnostics>(Output, PP.getLangOpts(),
65+
PP.getSourceManager()));
6566
createTextMinimalPathDiagnosticConsumer(std::move(DiagOpts), C, Output, PP,
6667
CTU, MacroExpansions);
6768
}

clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class AnalysisConsumer : public AnalysisASTConsumer,
9090
const std::string OutDir;
9191
AnalyzerOptions &Opts;
9292
ArrayRef<std::string> Plugins;
93-
CodeInjector *Injector;
93+
std::unique_ptr<CodeInjector> Injector;
9494
cross_tu::CrossTranslationUnitContext CTU;
9595

9696
/// Stores the declarations from the local translation unit.
@@ -123,10 +123,10 @@ class AnalysisConsumer : public AnalysisASTConsumer,
123123

124124
AnalysisConsumer(CompilerInstance &CI, const std::string &outdir,
125125
AnalyzerOptions &opts, ArrayRef<std::string> plugins,
126-
CodeInjector *injector)
126+
std::unique_ptr<CodeInjector> injector)
127127
: RecVisitorMode(0), RecVisitorBR(nullptr), Ctx(nullptr),
128-
PP(CI.getPreprocessor()), OutDir(outdir), Opts(opts),
129-
Plugins(plugins), Injector(injector), CTU(CI),
128+
PP(CI.getPreprocessor()), OutDir(outdir), Opts(opts), Plugins(plugins),
129+
Injector(std::move(injector)), CTU(CI),
130130
MacroExpansions(CI.getLangOpts()) {
131131
DigestAnalyzerOptions();
132132
if (Opts.AnalyzerDisplayProgress || Opts.PrintStats ||
@@ -229,9 +229,9 @@ class AnalysisConsumer : public AnalysisASTConsumer,
229229
checkerMgr = std::make_unique<CheckerManager>(*Ctx, Opts, PP, Plugins,
230230
CheckerRegistrationFns);
231231

232-
Mgr = std::make_unique<AnalysisManager>(*Ctx, PP, PathConsumers,
233-
CreateStoreMgr, CreateConstraintMgr,
234-
checkerMgr.get(), Opts, Injector);
232+
Mgr = std::make_unique<AnalysisManager>(
233+
*Ctx, PP, std::move(PathConsumers), CreateStoreMgr, CreateConstraintMgr,
234+
checkerMgr.get(), Opts, std::move(Injector));
235235
}
236236

237237
/// Store the top level decls in the set to be processed later on.
@@ -342,8 +342,9 @@ class AnalysisConsumer : public AnalysisASTConsumer,
342342
return true;
343343
}
344344

345-
void AddDiagnosticConsumer(PathDiagnosticConsumer *Consumer) override {
346-
PathConsumers.push_back(Consumer);
345+
void AddDiagnosticConsumer(
346+
std::unique_ptr<PathDiagnosticConsumer> Consumer) override {
347+
PathConsumers.push_back(std::move(Consumer));
347348
}
348349

349350
void AddCheckerRegistrationFn(std::function<void(CheckerRegistry&)> Fn) override {
@@ -794,5 +795,5 @@ ento::CreateAnalysisConsumer(CompilerInstance &CI) {
794795
return std::make_unique<AnalysisConsumer>(
795796
CI, CI.getFrontendOpts().OutputFile, analyzerOpts,
796797
CI.getFrontendOpts().Plugins,
797-
hasModelPath ? new ModelInjector(CI) : nullptr);
798+
hasModelPath ? std::make_unique<ModelInjector>(CI) : nullptr);
798799
}

clang/unittests/StaticAnalyzer/BugReportInterestingnessTest.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h"
2020
#include "clang/Tooling/Tooling.h"
2121
#include "gtest/gtest.h"
22+
#include <memory>
2223

2324
using namespace clang;
2425
using namespace ento;
@@ -114,8 +115,9 @@ class TestAction : public ASTFrontendAction {
114115
StringRef File) override {
115116
std::unique_ptr<AnalysisASTConsumer> AnalysisConsumer =
116117
CreateAnalysisConsumer(Compiler);
117-
AnalysisConsumer->AddDiagnosticConsumer(new VerifyPathDiagnosticConsumer(
118-
std::move(ExpectedDiags), Compiler.getSourceManager()));
118+
AnalysisConsumer->AddDiagnosticConsumer(
119+
std::make_unique<VerifyPathDiagnosticConsumer>(
120+
std::move(ExpectedDiags), Compiler.getSourceManager()));
119121
AnalysisConsumer->AddCheckerRegistrationFn([](CheckerRegistry &Registry) {
120122
Registry.addChecker<InterestingnessTestChecker>("test.Interestingness",
121123
"Description", "");

clang/unittests/StaticAnalyzer/CheckerRegistration.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ template <AddCheckerFn... Fns> class TestAction : public ASTFrontendAction {
9595
CreateAnalysisConsumer(Compiler);
9696
if (OnlyEmitWarnings)
9797
AnalysisConsumer->AddDiagnosticConsumer(
98-
new OnlyWarningsDiagConsumer(DiagsOutput));
98+
std::make_unique<OnlyWarningsDiagConsumer>(DiagsOutput));
9999
else
100100
AnalysisConsumer->AddDiagnosticConsumer(
101-
new PathDiagConsumer(DiagsOutput));
101+
std::make_unique<PathDiagConsumer>(DiagsOutput));
102102
addChecker<Fns...>(*AnalysisConsumer, Compiler.getAnalyzerOpts());
103103
return std::move(AnalysisConsumer);
104104
}

0 commit comments

Comments
 (0)