Skip to content

Commit 56abcfa

Browse files
committed
Revert "[analyzer][NFC] Tie CheckerRegistry to CheckerManager, allow CheckerManager to be constructed for non-analysis purposes"
Temporarily reverting this patch because it breaks the modules build.
1 parent 2ad5fc1 commit 56abcfa

File tree

12 files changed

+186
-176
lines changed

12 files changed

+186
-176
lines changed

clang/include/clang/StaticAnalyzer/Core/CheckerManager.h

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@
1414
#define LLVM_CLANG_STATICANALYZER_CORE_CHECKERMANAGER_H
1515

1616
#include "clang/Analysis/ProgramPoint.h"
17-
#include "clang/Basic/Diagnostic.h"
1817
#include "clang/Basic/LangOptions.h"
1918
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
2019
#include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
21-
#include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h"
2220
#include "llvm/ADT/ArrayRef.h"
2321
#include "llvm/ADT/DenseMap.h"
2422
#include "llvm/ADT/SmallVector.h"
@@ -42,6 +40,7 @@ class BugReporter;
4240
class CallEvent;
4341
class CheckerBase;
4442
class CheckerContext;
43+
class CheckerRegistry;
4544
class ExplodedGraph;
4645
class ExplodedNode;
4746
class ExplodedNodeSet;
@@ -122,42 +121,14 @@ enum class ObjCMessageVisitKind {
122121
};
123122

124123
class CheckerManager {
125-
ASTContext *Context;
124+
ASTContext &Context;
126125
const LangOptions LangOpts;
127126
AnalyzerOptions &AOptions;
128127
CheckerNameRef CurrentCheckerName;
129-
DiagnosticsEngine &Diags;
130-
CheckerRegistry Registry;
131128

132129
public:
133-
CheckerManager(
134-
ASTContext &Context, AnalyzerOptions &AOptions,
135-
ArrayRef<std::string> plugins,
136-
ArrayRef<std::function<void(CheckerRegistry &)>> checkerRegistrationFns)
137-
: Context(&Context), LangOpts(Context.getLangOpts()), AOptions(AOptions),
138-
Diags(Context.getDiagnostics()),
139-
Registry(plugins, Context.getDiagnostics(), AOptions,
140-
checkerRegistrationFns) {
141-
Registry.initializeRegistry(*this);
142-
Registry.initializeManager(*this);
143-
finishedCheckerRegistration();
144-
}
145-
146-
/// Constructs a CheckerManager that ignores all non TblGen-generated
147-
/// checkers. Useful for unit testing, unless the checker infrastructure
148-
/// itself is tested.
149130
CheckerManager(ASTContext &Context, AnalyzerOptions &AOptions)
150-
: CheckerManager(Context, AOptions, {}, {}) {}
151-
152-
/// Constructs a CheckerManager without requiring an AST. No checker
153-
/// registration will take place. Only useful for retrieving the
154-
/// CheckerRegistry and print for help flags where the AST is unavalaible.
155-
CheckerManager(AnalyzerOptions &AOptions, const LangOptions &LangOpts,
156-
DiagnosticsEngine &Diags, ArrayRef<std::string> plugins)
157-
: LangOpts(LangOpts), AOptions(AOptions), Diags(Diags),
158-
Registry(plugins, Diags, AOptions) {
159-
Registry.initializeRegistry(*this);
160-
}
131+
: Context(Context), LangOpts(Context.getLangOpts()), AOptions(AOptions) {}
161132

162133
~CheckerManager();
163134

@@ -170,12 +141,7 @@ class CheckerManager {
170141

171142
const LangOptions &getLangOpts() const { return LangOpts; }
172143
AnalyzerOptions &getAnalyzerOptions() const { return AOptions; }
173-
const CheckerRegistry &getCheckerRegistry() const { return Registry; }
174-
DiagnosticsEngine &getDiagnostics() const { return Diags; }
175-
ASTContext &getASTContext() const {
176-
assert(Context);
177-
return *Context;
178-
}
144+
ASTContext &getASTContext() const { return Context; }
179145

180146
/// Emits an error through a DiagnosticsEngine about an invalid user supplied
181147
/// checker option value.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class AnalysisASTConsumer : public ASTConsumer {
5555
std::unique_ptr<AnalysisASTConsumer>
5656
CreateAnalysisConsumer(CompilerInstance &CI);
5757

58-
} // namespace ento
58+
} // end GR namespace
5959

6060
} // end clang namespace
6161

clang/include/clang/StaticAnalyzer/Frontend/AnalyzerHelpFlags.h

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===-- CheckerRegistration.h - Checker Registration Function ---*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CLANG_STATICANALYZER_FRONTEND_CHECKERREGISTRATION_H
10+
#define LLVM_CLANG_STATICANALYZER_FRONTEND_CHECKERREGISTRATION_H
11+
12+
#include "clang/AST/ASTContext.h"
13+
#include "clang/Basic/LLVM.h"
14+
#include <functional>
15+
#include <memory>
16+
#include <string>
17+
18+
namespace clang {
19+
class AnalyzerOptions;
20+
class LangOptions;
21+
class DiagnosticsEngine;
22+
23+
namespace ento {
24+
class CheckerManager;
25+
class CheckerRegistry;
26+
27+
std::unique_ptr<CheckerManager> createCheckerManager(
28+
ASTContext &context,
29+
AnalyzerOptions &opts,
30+
ArrayRef<std::string> plugins,
31+
ArrayRef<std::function<void(CheckerRegistry &)>> checkerRegistrationFns,
32+
DiagnosticsEngine &diags);
33+
34+
} // end ento namespace
35+
36+
} // end namespace clang
37+
38+
#endif

clang/include/clang/StaticAnalyzer/Frontend/CheckerRegistry.h

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_CLANG_STATICANALYZER_CORE_CHECKERREGISTRY_H
1111

1212
#include "clang/Basic/LLVM.h"
13+
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
1314
#include "llvm/ADT/SetVector.h"
1415
#include "llvm/ADT/StringMap.h"
1516
#include "llvm/ADT/StringRef.h"
@@ -73,8 +74,6 @@ class LangOptions;
7374

7475
namespace ento {
7576

76-
class CheckerManager;
77-
7877
/// Manages a set of available checkers for running a static analysis.
7978
/// The checkers are organized into packages by full name, where including
8079
/// a package will recursively include all subpackages and checkers within it.
@@ -84,15 +83,10 @@ class CheckerManager;
8483
class CheckerRegistry {
8584
public:
8685
CheckerRegistry(ArrayRef<std::string> plugins, DiagnosticsEngine &diags,
87-
AnalyzerOptions &AnOpts,
86+
AnalyzerOptions &AnOpts, const LangOptions &LangOpts,
8887
ArrayRef<std::function<void(CheckerRegistry &)>>
8988
checkerRegistrationFns = {});
9089

91-
/// Collects all enabled checkers in the field EnabledCheckers. It preserves
92-
/// the order of insertion, as dependencies have to be enabled before the
93-
/// checkers that depend on them.
94-
void initializeRegistry(const CheckerManager &Mgr);
95-
9690
/// Initialization functions perform any necessary setup for a checker.
9791
/// They should include a call to CheckerManager::registerChecker.
9892
using InitializationFunction = void (*)(CheckerManager &);
@@ -211,20 +205,14 @@ class CheckerRegistry {
211205

212206
using PackageInfoList = llvm::SmallVector<PackageInfo, 0>;
213207

214-
private:
215-
/// Default initialization function for checkers -- since CheckerManager
216-
/// includes this header, we need to make it a template parameter, and since
217-
/// the checker must be a template parameter as well, we can't put this in the
218-
/// cpp file.
219-
template <typename MGR, typename T> static void initializeManager(MGR &mgr) {
220-
mgr.template registerChecker<T>();
208+
template <typename T> static void addToCheckerMgr(CheckerManager &mgr) {
209+
mgr.registerChecker<T>();
221210
}
222211

223-
template <typename T> static bool returnTrue(const LangOptions &) {
212+
static bool returnTrue(const LangOptions &LO) {
224213
return true;
225214
}
226215

227-
public:
228216
/// Adds a checker to the registry. Use this non-templated overload when your
229217
/// checker requires custom initialization.
230218
void addChecker(InitializationFunction Fn, ShouldRegisterFunction sfn,
@@ -233,16 +221,13 @@ class CheckerRegistry {
233221

234222
/// Adds a checker to the registry. Use this templated overload when your
235223
/// checker does not require any custom initialization.
236-
/// This function isn't really needed and probably causes more headaches than
237-
/// the tiny convenience that it provides, but external plugins might use it,
238-
/// and there isn't a strong incentive to remove it.
239224
template <class T>
240225
void addChecker(StringRef FullName, StringRef Desc, StringRef DocsUri,
241226
bool IsHidden = false) {
242227
// Avoid MSVC's Compiler Error C2276:
243228
// http://msdn.microsoft.com/en-us/library/850cstw1(v=VS.80).aspx
244-
addChecker(&initializeManager<CheckerManager, T>, &returnTrue<T>, FullName,
245-
Desc, DocsUri, IsHidden);
229+
addChecker(&CheckerRegistry::addToCheckerMgr<T>,
230+
&CheckerRegistry::returnTrue, FullName, Desc, DocsUri, IsHidden);
246231
}
247232

248233
/// Makes the checker with the full name \p fullName depends on the checker
@@ -278,7 +263,7 @@ class CheckerRegistry {
278263
void addPackageOption(StringRef OptionType, StringRef PackageFullName,
279264
StringRef OptionName, StringRef DefaultValStr,
280265
StringRef Description, StringRef DevelopmentStatus,
281-
bool IsHidden = false);
266+
bool IsHidden = false);
282267

283268
// FIXME: This *really* should be added to the frontend flag descriptions.
284269
/// Initializes a CheckerManager by calling the initialization functions for
@@ -298,6 +283,11 @@ class CheckerRegistry {
298283
void printCheckerOptionList(raw_ostream &Out) const;
299284

300285
private:
286+
/// Collect all enabled checkers. The returned container preserves the order
287+
/// of insertion, as dependencies have to be enabled before the checkers that
288+
/// depend on them.
289+
CheckerInfoSet getEnabledCheckers() const;
290+
301291
/// Return an iterator range of mutable CheckerInfos \p CmdLineArg applies to.
302292
/// For example, it'll return the checkers for the core package, if
303293
/// \p CmdLineArg is "core".
@@ -324,7 +314,7 @@ class CheckerRegistry {
324314

325315
DiagnosticsEngine &Diags;
326316
AnalyzerOptions &AnOpts;
327-
CheckerInfoSet EnabledCheckers;
317+
const LangOptions &LangOpts;
328318
};
329319

330320
} // namespace ento

clang/include/clang/StaticAnalyzer/Frontend/FrontendActions.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,22 @@ class ParseModelFileAction : public ASTFrontendAction {
5151
llvm::StringMap<Stmt *> &Bodies;
5252
};
5353

54-
} // namespace ento
54+
void printCheckerHelp(raw_ostream &OS,
55+
ArrayRef<std::string> plugins,
56+
AnalyzerOptions &opts,
57+
DiagnosticsEngine &diags,
58+
const LangOptions &LangOpts);
59+
void printEnabledCheckerList(raw_ostream &OS, ArrayRef<std::string> plugins,
60+
AnalyzerOptions &opts,
61+
DiagnosticsEngine &diags,
62+
const LangOptions &LangOpts);
63+
void printAnalyzerConfigList(raw_ostream &OS);
64+
void printCheckerConfigList(raw_ostream &OS, ArrayRef<std::string> plugins,
65+
AnalyzerOptions &opts,
66+
DiagnosticsEngine &diags,
67+
const LangOptions &LangOpts);
68+
69+
} // end GR namespace
5570

5671
} // end namespace clang
5772

clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "clang/Frontend/Utils.h"
2424
#include "clang/FrontendTool/Utils.h"
2525
#include "clang/Rewrite/Frontend/FrontendActions.h"
26-
#include "clang/StaticAnalyzer/Frontend/AnalyzerHelpFlags.h"
2726
#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
2827
#include "llvm/Option/OptTable.h"
2928
#include "llvm/Option/Option.h"
@@ -244,24 +243,35 @@ bool ExecuteCompilerInvocation(CompilerInstance *Clang) {
244243
// These should happen AFTER plugins have been loaded!
245244

246245
AnalyzerOptions &AnOpts = *Clang->getAnalyzerOpts();
247-
248246
// Honor -analyzer-checker-help and -analyzer-checker-help-hidden.
249247
if (AnOpts.ShowCheckerHelp || AnOpts.ShowCheckerHelpAlpha ||
250248
AnOpts.ShowCheckerHelpDeveloper) {
251-
ento::printCheckerHelp(llvm::outs(), *Clang);
249+
ento::printCheckerHelp(llvm::outs(),
250+
Clang->getFrontendOpts().Plugins,
251+
AnOpts,
252+
Clang->getDiagnostics(),
253+
Clang->getLangOpts());
252254
return true;
253255
}
254256

255257
// Honor -analyzer-checker-option-help.
256258
if (AnOpts.ShowCheckerOptionList || AnOpts.ShowCheckerOptionAlphaList ||
257259
AnOpts.ShowCheckerOptionDeveloperList) {
258-
ento::printCheckerConfigList(llvm::outs(), *Clang);
260+
ento::printCheckerConfigList(llvm::outs(),
261+
Clang->getFrontendOpts().Plugins,
262+
*Clang->getAnalyzerOpts(),
263+
Clang->getDiagnostics(),
264+
Clang->getLangOpts());
259265
return true;
260266
}
261267

262268
// Honor -analyzer-list-enabled-checkers.
263269
if (AnOpts.ShowEnabledCheckerList) {
264-
ento::printEnabledCheckerList(llvm::outs(), *Clang);
270+
ento::printEnabledCheckerList(llvm::outs(),
271+
Clang->getFrontendOpts().Plugins,
272+
AnOpts,
273+
Clang->getDiagnostics(),
274+
Clang->getLangOpts());
265275
return true;
266276
}
267277

clang/lib/StaticAnalyzer/Core/CheckerManager.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ void CheckerManager::reportInvalidCheckerOptionValue(
6464
const CheckerBase *C, StringRef OptionName,
6565
StringRef ExpectedValueDesc) const {
6666

67-
getDiagnostics().Report(diag::err_analyzer_checker_option_invalid_input)
68-
<< (llvm::Twine() + C->getTagDescription() + ":" + OptionName).str()
69-
<< ExpectedValueDesc;
67+
Context.getDiagnostics()
68+
.Report(diag::err_analyzer_checker_option_invalid_input)
69+
<< (llvm::Twine() + C->getTagDescription() + ":" + OptionName).str()
70+
<< ExpectedValueDesc;
7071
}
7172

7273
//===----------------------------------------------------------------------===//

clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
3434
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
3535
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
36+
#include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h"
3637
#include "clang/Tooling/Core/Replacement.h"
3738
#include "clang/Tooling/Tooling.h"
3839
#include "llvm/ADT/PostOrderIterator.h"
@@ -347,8 +348,8 @@ class AnalysisConsumer : public AnalysisASTConsumer,
347348

348349
void Initialize(ASTContext &Context) override {
349350
Ctx = &Context;
350-
checkerMgr = std::make_unique<CheckerManager>(*Ctx, *Opts, Plugins,
351-
CheckerRegistrationFns);
351+
checkerMgr = createCheckerManager(
352+
*Ctx, *Opts, Plugins, CheckerRegistrationFns, PP.getDiagnostics());
352353

353354
Mgr = std::make_unique<AnalysisManager>(*Ctx, PP, PathConsumers,
354355
CreateStoreMgr, CreateConstraintMgr,

clang/lib/StaticAnalyzer/Frontend/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set(LLVM_LINK_COMPONENTS
66

77
add_clang_library(clangStaticAnalyzerFrontend
88
AnalysisConsumer.cpp
9-
AnalyzerHelpFlags.cpp
9+
CheckerRegistration.cpp
1010
CheckerRegistry.cpp
1111
FrontendActions.cpp
1212
ModelConsumer.cpp

0 commit comments

Comments
 (0)