Skip to content

Commit

Permalink
[clang] NFCI: Change returned AnalyzerOptions smart pointer to reference
Browse files Browse the repository at this point in the history
  • Loading branch information
jansvoboda11 committed Sep 5, 2023
1 parent 5746002 commit 8e0c9bb
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 68 deletions.
12 changes: 6 additions & 6 deletions clang-tools-extra/clang-tidy/ClangTidy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 1 addition & 3 deletions clang/include/clang/Frontend/CompilerInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,7 @@ class CompilerInstance : public ModuleLoader {
/// @name Forwarding Methods
/// @{

AnalyzerOptionsRef getAnalyzerOpts() {
return Invocation->getAnalyzerOpts();
}
AnalyzerOptions &getAnalyzerOpts() { return Invocation->getAnalyzerOpts(); }

CodeGenOptions &getCodeGenOpts() {
return Invocation->getCodeGenOpts();
Expand Down
3 changes: 2 additions & 1 deletion clang/include/clang/Frontend/CompilerInvocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ class CompilerInvocationRefBase {
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.
Expand Down
8 changes: 4 additions & 4 deletions clang/lib/CrossTU/CrossTranslationUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down
60 changes: 30 additions & 30 deletions clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class AnalysisConsumer : public AnalysisASTConsumer,
ASTContext *Ctx;
Preprocessor &PP;
const std::string OutDir;
AnalyzerOptionsRef Opts;
AnalyzerOptions &Opts;
ArrayRef<std::string> Plugins;
CodeInjector *Injector;
cross_tu::CrossTranslationUnitContext CTU;
Expand Down Expand Up @@ -121,15 +121,15 @@ class AnalysisConsumer : public AnalysisASTConsumer,
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>(
Expand All @@ -141,27 +141,27 @@ class AnalysisConsumer : public AnalysisASTConsumer,
*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"
Expand All @@ -172,7 +172,7 @@ class AnalysisConsumer : public AnalysisASTConsumer,
// 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) \
Expand All @@ -182,7 +182,7 @@ class AnalysisConsumer : public AnalysisASTConsumer,
}

void DisplayTime(llvm::TimeRecord &Time) {
if (!Opts->AnalyzerDisplayProgress) {
if (!Opts.AnalyzerDisplayProgress) {
return;
}
llvm::errs() << " : " << llvm::format("%1.1f", Time.getWallTime() * 1000)
Expand All @@ -191,7 +191,7 @@ class AnalysisConsumer : public AnalysisASTConsumer,

void DisplayFunction(const Decl *D, AnalysisMode Mode,
ExprEngine::InliningModes IMode) {
if (!Opts->AnalyzerDisplayProgress)
if (!Opts.AnalyzerDisplayProgress)
return;

SourceManager &SM = Mgr->getASTContext().getSourceManager();
Expand Down Expand Up @@ -222,12 +222,12 @@ class AnalysisConsumer : public AnalysisASTConsumer,

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.
Expand Down Expand Up @@ -278,7 +278,7 @@ class AnalysisConsumer : public AnalysisASTConsumer,
}

bool VisitVarDecl(VarDecl *VD) {
if (!Opts->IsNaiveCTUEnabled)
if (!Opts.IsNaiveCTUEnabled)
return true;

if (VD->hasExternalStorage() || VD->isStaticDataMember()) {
Expand All @@ -293,8 +293,8 @@ class AnalysisConsumer : public AnalysisASTConsumer,
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(),
Expand Down Expand Up @@ -356,7 +356,7 @@ class AnalysisConsumer : public AnalysisASTConsumer,
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
Expand Down Expand Up @@ -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;
}

Expand All @@ -589,21 +589,21 @@ 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;
}

// 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;
}
Expand All @@ -623,16 +623,16 @@ 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
// where they came from:
// - 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();
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions clang/lib/StaticAnalyzer/Frontend/AnalyzerHelpFlags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions clang/unittests/CrossTU/CrossTranslationUnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ class CTUAction : public clang::ASTFrontendAction {
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);
}

Expand Down
16 changes: 8 additions & 8 deletions clang/unittests/Frontend/CompilerInvocationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class TestAction : public ASTFrontendAction {
Registry.addChecker<InterestingnessTestChecker>("test.Interestingness",
"Description", "");
});
Compiler.getAnalyzerOpts()->CheckersAndPackages = {
Compiler.getAnalyzerOpts().CheckersAndPackages = {
{"test.Interestingness", true}};
return std::move(AnalysisConsumer);
}
Expand Down

0 comments on commit 8e0c9bb

Please sign in to comment.