[clang] NFCI: Change returned AnalyzerOptions smart pointer to reference

This commit is contained in:
Jan Svoboda 2023-08-31 14:22:23 -07:00
parent 5746002ebb
commit 8e0c9bb91f
14 changed files with 66 additions and 68 deletions

View File

@ -435,13 +435,13 @@ ClangTidyASTConsumerFactory::createASTConsumer(
Consumers.push_back(Finder->newASTConsumer());
#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
AnalyzerOptionsRef AnalyzerOptions = Compiler.getAnalyzerOpts();
AnalyzerOptions->CheckersAndPackages = getAnalyzerCheckersAndPackages(
AnalyzerOptions &AnalyzerOptions = Compiler.getAnalyzerOpts();
AnalyzerOptions.CheckersAndPackages = getAnalyzerCheckersAndPackages(
Context, Context.canEnableAnalyzerAlphaCheckers());
if (!AnalyzerOptions->CheckersAndPackages.empty()) {
setStaticAnalyzerCheckerOpts(Context.getOptions(), *AnalyzerOptions);
AnalyzerOptions->AnalysisDiagOpt = PD_NONE;
AnalyzerOptions->eagerlyAssumeBinOpBifurcation = true;
if (!AnalyzerOptions.CheckersAndPackages.empty()) {
setStaticAnalyzerCheckerOpts(Context.getOptions(), AnalyzerOptions);
AnalyzerOptions.AnalysisDiagOpt = PD_NONE;
AnalyzerOptions.eagerlyAssumeBinOpBifurcation = true;
std::unique_ptr<ento::AnalysisASTConsumer> AnalysisConsumer =
ento::CreateAnalysisConsumer(Compiler);
AnalysisConsumer->AddDiagnosticConsumer(

View File

@ -252,9 +252,7 @@ public:
/// @name Forwarding Methods
/// @{
AnalyzerOptionsRef getAnalyzerOpts() {
return Invocation->getAnalyzerOpts();
}
AnalyzerOptions &getAnalyzerOpts() { return Invocation->getAnalyzerOpts(); }
CodeGenOptions &getCodeGenOpts() {
return Invocation->getCodeGenOpts();

View File

@ -129,7 +129,8 @@ public:
return *PreprocessorOpts;
}
AnalyzerOptionsRef getAnalyzerOpts() const { return AnalyzerOpts; }
AnalyzerOptions &getAnalyzerOpts() { return *AnalyzerOpts; }
const AnalyzerOptions &getAnalyzerOpts() const { return *AnalyzerOpts; }
};
/// The base class of CompilerInvocation with value semantics.

View File

@ -392,11 +392,11 @@ void CrossTranslationUnitContext::emitCrossTUDiagnostics(const IndexError &IE) {
CrossTranslationUnitContext::ASTUnitStorage::ASTUnitStorage(
CompilerInstance &CI)
: Loader(CI, CI.getAnalyzerOpts()->CTUDir,
CI.getAnalyzerOpts()->CTUInvocationList),
: Loader(CI, CI.getAnalyzerOpts().CTUDir,
CI.getAnalyzerOpts().CTUInvocationList),
LoadGuard(CI.getASTContext().getLangOpts().CPlusPlus
? CI.getAnalyzerOpts()->CTUImportCppThreshold
: CI.getAnalyzerOpts()->CTUImportThreshold) {}
? CI.getAnalyzerOpts().CTUImportCppThreshold
: CI.getAnalyzerOpts().CTUImportThreshold) {}
llvm::Expected<ASTUnit *>
CrossTranslationUnitContext::ASTUnitStorage::getASTUnitForFile(

View File

@ -140,7 +140,7 @@ CompilerInvocationRefBase::CompilerInvocationRefBase(
DiagnosticOpts(new DiagnosticOptions(X.getDiagnosticOpts())),
HeaderSearchOpts(new HeaderSearchOptions(X.getHeaderSearchOpts())),
PreprocessorOpts(new PreprocessorOptions(X.getPreprocessorOpts())),
AnalyzerOpts(new AnalyzerOptions(*X.getAnalyzerOpts())) {}
AnalyzerOpts(new AnalyzerOptions(X.getAnalyzerOpts())) {}
CompilerInvocationRefBase::CompilerInvocationRefBase(
CompilerInvocationRefBase &&X) = default;
@ -4385,7 +4385,7 @@ bool CompilerInvocation::CreateFromArgsImpl(
ParseFileSystemArgs(Res.getFileSystemOpts(), Args, Diags);
ParseMigratorArgs(Res.getMigratorOpts(), Args, Diags);
ParseAnalyzerArgs(*Res.getAnalyzerOpts(), Args, Diags);
ParseAnalyzerArgs(Res.getAnalyzerOpts(), Args, Diags);
ParseDiagnosticArgs(Res.getDiagnosticOpts(), Args, &Diags,
/*DefaultDiagColor=*/false);
ParseFrontendArgs(Res.getFrontendOpts(), Args, Diags, LangOpts.IsHeaderFile);

View File

@ -233,7 +233,7 @@ bool ExecuteCompilerInvocation(CompilerInstance *Clang) {
#if CLANG_ENABLE_STATIC_ANALYZER
// These should happen AFTER plugins have been loaded!
AnalyzerOptions &AnOpts = *Clang->getAnalyzerOpts();
AnalyzerOptions &AnOpts = Clang->getAnalyzerOpts();
// Honor -analyzer-checker-help and -analyzer-checker-help-hidden.
if (AnOpts.ShowCheckerHelp || AnOpts.ShowCheckerHelpAlpha ||

View File

@ -87,7 +87,7 @@ public:
ASTContext *Ctx;
Preprocessor &PP;
const std::string OutDir;
AnalyzerOptionsRef Opts;
AnalyzerOptions &Opts;
ArrayRef<std::string> Plugins;
CodeInjector *Injector;
cross_tu::CrossTranslationUnitContext CTU;
@ -121,15 +121,15 @@ public:
FunctionSummariesTy FunctionSummaries;
AnalysisConsumer(CompilerInstance &CI, const std::string &outdir,
AnalyzerOptionsRef opts, ArrayRef<std::string> plugins,
AnalyzerOptions &opts, ArrayRef<std::string> plugins,
CodeInjector *injector)
: RecVisitorMode(0), RecVisitorBR(nullptr), Ctx(nullptr),
PP(CI.getPreprocessor()), OutDir(outdir), Opts(std::move(opts)),
PP(CI.getPreprocessor()), OutDir(outdir), Opts(opts),
Plugins(plugins), Injector(injector), CTU(CI),
MacroExpansions(CI.getLangOpts()) {
DigestAnalyzerOptions();
if (Opts->AnalyzerDisplayProgress || Opts->PrintStats ||
Opts->ShouldSerializeStats) {
if (Opts.AnalyzerDisplayProgress || Opts.PrintStats ||
Opts.ShouldSerializeStats) {
AnalyzerTimers = std::make_unique<llvm::TimerGroup>(
"analyzer", "Analyzer timers");
SyntaxCheckTimer = std::make_unique<llvm::Timer>(
@ -141,27 +141,27 @@ public:
*AnalyzerTimers);
}
if (Opts->PrintStats || Opts->ShouldSerializeStats) {
if (Opts.PrintStats || Opts.ShouldSerializeStats) {
llvm::EnableStatistics(/* DoPrintOnExit= */ false);
}
if (Opts->ShouldDisplayMacroExpansions)
if (Opts.ShouldDisplayMacroExpansions)
MacroExpansions.registerForPreprocessor(PP);
}
~AnalysisConsumer() override {
if (Opts->PrintStats) {
if (Opts.PrintStats) {
llvm::PrintStatistics();
}
}
void DigestAnalyzerOptions() {
switch (Opts->AnalysisDiagOpt) {
switch (Opts.AnalysisDiagOpt) {
case PD_NONE:
break;
#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN) \
case PD_##NAME: \
CREATEFN(Opts->getDiagOpts(), PathConsumers, OutDir, PP, CTU, \
CREATEFN(Opts.getDiagOpts(), PathConsumers, OutDir, PP, CTU, \
MacroExpansions); \
break;
#include "clang/StaticAnalyzer/Core/Analyses.def"
@ -172,7 +172,7 @@ public:
// Create the analyzer component creators.
CreateStoreMgr = &CreateRegionStoreManager;
switch (Opts->AnalysisConstraintsOpt) {
switch (Opts.AnalysisConstraintsOpt) {
default:
llvm_unreachable("Unknown constraint manager.");
#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \
@ -182,7 +182,7 @@ public:
}
void DisplayTime(llvm::TimeRecord &Time) {
if (!Opts->AnalyzerDisplayProgress) {
if (!Opts.AnalyzerDisplayProgress) {
return;
}
llvm::errs() << " : " << llvm::format("%1.1f", Time.getWallTime() * 1000)
@ -191,7 +191,7 @@ public:
void DisplayFunction(const Decl *D, AnalysisMode Mode,
ExprEngine::InliningModes IMode) {
if (!Opts->AnalyzerDisplayProgress)
if (!Opts.AnalyzerDisplayProgress)
return;
SourceManager &SM = Mgr->getASTContext().getSourceManager();
@ -222,12 +222,12 @@ public:
void Initialize(ASTContext &Context) override {
Ctx = &Context;
checkerMgr = std::make_unique<CheckerManager>(*Ctx, *Opts, PP, Plugins,
checkerMgr = std::make_unique<CheckerManager>(*Ctx, Opts, PP, Plugins,
CheckerRegistrationFns);
Mgr = std::make_unique<AnalysisManager>(*Ctx, PP, PathConsumers,
CreateStoreMgr, CreateConstraintMgr,
checkerMgr.get(), *Opts, Injector);
checkerMgr.get(), Opts, Injector);
}
/// Store the top level decls in the set to be processed later on.
@ -278,7 +278,7 @@ public:
}
bool VisitVarDecl(VarDecl *VD) {
if (!Opts->IsNaiveCTUEnabled)
if (!Opts.IsNaiveCTUEnabled)
return true;
if (VD->hasExternalStorage() || VD->isStaticDataMember()) {
@ -293,8 +293,8 @@ public:
return true;
llvm::Expected<const VarDecl *> CTUDeclOrError =
CTU.getCrossTUDefinition(VD, Opts->CTUDir, Opts->CTUIndexName,
Opts->DisplayCTUProgress);
CTU.getCrossTUDefinition(VD, Opts.CTUDir, Opts.CTUIndexName,
Opts.DisplayCTUProgress);
if (!CTUDeclOrError) {
handleAllErrors(CTUDeclOrError.takeError(),
@ -356,7 +356,7 @@ private:
AnalysisMode getModeForDecl(Decl *D, AnalysisMode Mode);
void runAnalysisOnTranslationUnit(ASTContext &C);
/// Print \p S to stderr if \c Opts->AnalyzerDisplayProgress is set.
/// Print \p S to stderr if \c Opts.AnalyzerDisplayProgress is set.
void reportAnalyzerProgress(StringRef S);
}; // namespace
} // end anonymous namespace
@ -567,12 +567,12 @@ void AnalysisConsumer::runAnalysisOnTranslationUnit(ASTContext &C) {
// name correctly.
// FIXME: The user might have analyzed the requested function in Syntax mode,
// but we are unaware of that.
if (!Opts->AnalyzeSpecificFunction.empty() && NumFunctionsAnalyzed == 0)
reportAnalyzerFunctionMisuse(*Opts, *Ctx);
if (!Opts.AnalyzeSpecificFunction.empty() && NumFunctionsAnalyzed == 0)
reportAnalyzerFunctionMisuse(Opts, *Ctx);
}
void AnalysisConsumer::reportAnalyzerProgress(StringRef S) {
if (Opts->AnalyzerDisplayProgress)
if (Opts.AnalyzerDisplayProgress)
llvm::errs() << S;
}
@ -589,13 +589,13 @@ void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
const auto DiagFlusherScopeExit =
llvm::make_scope_exit([this] { Mgr.reset(); });
if (Opts->ShouldIgnoreBisonGeneratedFiles &&
if (Opts.ShouldIgnoreBisonGeneratedFiles &&
fileContainsString("/* A Bison parser, made by", C)) {
reportAnalyzerProgress("Skipping bison-generated file\n");
return;
}
if (Opts->ShouldIgnoreFlexGeneratedFiles &&
if (Opts.ShouldIgnoreFlexGeneratedFiles &&
fileContainsString("/* A lexical scanner generated by flex", C)) {
reportAnalyzerProgress("Skipping flex-generated file\n");
return;
@ -603,7 +603,7 @@ void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
// Don't analyze if the user explicitly asked for no checks to be performed
// on this file.
if (Opts->DisableAllCheckers) {
if (Opts.DisableAllCheckers) {
reportAnalyzerProgress("All checks are disabled using a supplied option\n");
return;
}
@ -623,8 +623,8 @@ void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
AnalysisConsumer::AnalysisMode
AnalysisConsumer::getModeForDecl(Decl *D, AnalysisMode Mode) {
if (!Opts->AnalyzeSpecificFunction.empty() &&
AnalysisDeclContext::getFunctionName(D) != Opts->AnalyzeSpecificFunction)
if (!Opts.AnalyzeSpecificFunction.empty() &&
AnalysisDeclContext::getFunctionName(D) != Opts.AnalyzeSpecificFunction)
return AM_None;
// Unless -analyze-all is specified, treat decls differently depending on
@ -632,7 +632,7 @@ AnalysisConsumer::getModeForDecl(Decl *D, AnalysisMode Mode) {
// - Main source file: run both path-sensitive and non-path-sensitive checks.
// - Header files: run non-path-sensitive checks only.
// - System headers: don't run any checks.
if (Opts->AnalyzeAll)
if (Opts.AnalyzeAll)
return Mode;
const SourceManager &SM = Ctx->getSourceManager();
@ -757,8 +757,8 @@ ento::CreateAnalysisConsumer(CompilerInstance &CI) {
// Disable the effects of '-Werror' when using the AnalysisConsumer.
CI.getPreprocessor().getDiagnostics().setWarningsAsErrors(false);
AnalyzerOptionsRef analyzerOpts = CI.getAnalyzerOpts();
bool hasModelPath = analyzerOpts->Config.count("model-path") > 0;
AnalyzerOptions &analyzerOpts = CI.getAnalyzerOpts();
bool hasModelPath = analyzerOpts.Config.count("model-path") > 0;
return std::make_unique<AnalysisConsumer>(
CI, CI.getFrontendOpts().OutputFile, analyzerOpts,

View File

@ -30,18 +30,18 @@ void ento::printCheckerHelp(raw_ostream &out, CompilerInstance &CI) {
out << "USAGE: -analyzer-checker <CHECKER or PACKAGE,...>\n\n";
auto CheckerMgr = std::make_unique<CheckerManager>(
*CI.getAnalyzerOpts(), CI.getLangOpts(), CI.getDiagnostics(),
CI.getAnalyzerOpts(), CI.getLangOpts(), CI.getDiagnostics(),
CI.getFrontendOpts().Plugins);
CheckerMgr->getCheckerRegistryData().printCheckerWithDescList(
*CI.getAnalyzerOpts(), out);
CI.getAnalyzerOpts(), out);
}
void ento::printEnabledCheckerList(raw_ostream &out, CompilerInstance &CI) {
out << "OVERVIEW: Clang Static Analyzer Enabled Checkers List\n\n";
auto CheckerMgr = std::make_unique<CheckerManager>(
*CI.getAnalyzerOpts(), CI.getLangOpts(), CI.getDiagnostics(),
CI.getAnalyzerOpts(), CI.getLangOpts(), CI.getDiagnostics(),
CI.getFrontendOpts().Plugins);
CheckerMgr->getCheckerRegistryData().printEnabledCheckerList(out);
@ -50,11 +50,11 @@ void ento::printEnabledCheckerList(raw_ostream &out, CompilerInstance &CI) {
void ento::printCheckerConfigList(raw_ostream &out, CompilerInstance &CI) {
auto CheckerMgr = std::make_unique<CheckerManager>(
*CI.getAnalyzerOpts(), CI.getLangOpts(), CI.getDiagnostics(),
CI.getAnalyzerOpts(), CI.getLangOpts(), CI.getDiagnostics(),
CI.getFrontendOpts().Plugins);
CheckerMgr->getCheckerRegistryData().printCheckerOptionList(
*CI.getAnalyzerOpts(), out);
CI.getAnalyzerOpts(), out);
}
void ento::printAnalyzerConfigList(raw_ostream &out) {

View File

@ -48,8 +48,7 @@ void ModelInjector::onBodySynthesis(const NamedDecl *D) {
SourceManager &SM = CI.getSourceManager();
FileID mainFileID = SM.getMainFileID();
AnalyzerOptionsRef analyzerOpts = CI.getAnalyzerOpts();
llvm::StringRef modelPath = analyzerOpts->ModelPath;
llvm::StringRef modelPath = CI.getAnalyzerOpts().ModelPath;
llvm::SmallString<128> fileName;

View File

@ -125,8 +125,8 @@ public:
protected:
std::unique_ptr<clang::ASTConsumer>
CreateASTConsumer(clang::CompilerInstance &CI, StringRef) override {
CI.getAnalyzerOpts()->CTUImportThreshold = OverrideLimit;
CI.getAnalyzerOpts()->CTUImportCppThreshold = OverrideLimit;
CI.getAnalyzerOpts().CTUImportThreshold = OverrideLimit;
CI.getAnalyzerOpts().CTUImportCppThreshold = OverrideLimit;
return std::make_unique<CTUASTConsumer>(CI, Success);
}

View File

@ -102,23 +102,23 @@ TEST(ContainsN, Two) {
TEST(CompilerInvocationTest, DeepCopyConstructor) {
CompilerInvocation A;
A.getAnalyzerOpts()->Config["Key"] = "Old";
A.getAnalyzerOpts().Config["Key"] = "Old";
CompilerInvocation B(A);
B.getAnalyzerOpts()->Config["Key"] = "New";
B.getAnalyzerOpts().Config["Key"] = "New";
ASSERT_EQ(A.getAnalyzerOpts()->Config["Key"], "Old");
ASSERT_EQ(A.getAnalyzerOpts().Config["Key"], "Old");
}
TEST(CompilerInvocationTest, DeepCopyAssignment) {
CompilerInvocation A;
A.getAnalyzerOpts()->Config["Key"] = "Old";
A.getAnalyzerOpts().Config["Key"] = "Old";
CompilerInvocation B;
B = A;
B.getAnalyzerOpts()->Config["Key"] = "New";
B.getAnalyzerOpts().Config["Key"] = "New";
ASSERT_EQ(A.getAnalyzerOpts()->Config["Key"], "Old");
ASSERT_EQ(A.getAnalyzerOpts().Config["Key"], "Old");
}
// Boolean option with a keypath that defaults to true.
@ -982,8 +982,8 @@ TEST_F(CommandLineTest, RoundTrip) {
Contains(std::make_pair(std::string("XY=AB"), false)));
ASSERT_EQ(Invocation.getPreprocessorOpts().ImplicitPCHInclude, "a.pch");
ASSERT_EQ(Invocation.getAnalyzerOpts()->Config["ctu-import-threshold"], "42");
ASSERT_TRUE(Invocation.getAnalyzerOpts()->UnoptimizedCFG);
ASSERT_EQ(Invocation.getAnalyzerOpts().Config["ctu-import-threshold"], "42");
ASSERT_TRUE(Invocation.getAnalyzerOpts().UnoptimizedCFG);
ASSERT_TRUE(Invocation.getMigratorOpts().NoNSAllocReallocError);

View File

@ -118,7 +118,7 @@ public:
Registry.addChecker<InterestingnessTestChecker>("test.Interestingness",
"Description", "");
});
Compiler.getAnalyzerOpts()->CheckersAndPackages = {
Compiler.getAnalyzerOpts().CheckersAndPackages = {
{"test.Interestingness", true}};
return std::move(AnalysisConsumer);
}

View File

@ -99,7 +99,7 @@ public:
else
AnalysisConsumer->AddDiagnosticConsumer(
new PathDiagConsumer(DiagsOutput));
addChecker<Fns...>(*AnalysisConsumer, *Compiler.getAnalyzerOpts());
addChecker<Fns...>(*AnalysisConsumer, Compiler.getAnalyzerOpts());
return std::move(AnalysisConsumer);
}
};

View File

@ -58,11 +58,11 @@ protected:
public:
ExprEngineConsumer(CompilerInstance &C)
: C(C),
ChkMgr(C.getASTContext(), *C.getAnalyzerOpts(), C.getPreprocessor()),
ChkMgr(C.getASTContext(), C.getAnalyzerOpts(), C.getPreprocessor()),
CTU(C), Consumers(),
AMgr(C.getASTContext(), C.getPreprocessor(), Consumers,
CreateRegionStoreManager, CreateRangeConstraintManager, &ChkMgr,
*C.getAnalyzerOpts()),
C.getAnalyzerOpts()),
VisitedCallees(), FS(),
Eng(CTU, AMgr, &VisitedCallees, &FS, ExprEngine::Inline_Regular) {}
};