17330f729Sjoerg //===--- AnalysisConsumer.cpp - ASTConsumer for running Analyses ----------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // "Meta" ASTConsumer for running different source analyses.
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg
137330f729Sjoerg #include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h"
147330f729Sjoerg #include "ModelInjector.h"
157330f729Sjoerg #include "clang/AST/Decl.h"
167330f729Sjoerg #include "clang/AST/DeclCXX.h"
177330f729Sjoerg #include "clang/AST/DeclObjC.h"
187330f729Sjoerg #include "clang/AST/RecursiveASTVisitor.h"
197330f729Sjoerg #include "clang/Analysis/Analyses/LiveVariables.h"
207330f729Sjoerg #include "clang/Analysis/CFG.h"
217330f729Sjoerg #include "clang/Analysis/CallGraph.h"
227330f729Sjoerg #include "clang/Analysis/CodeInjector.h"
23*e038c9c4Sjoerg #include "clang/Analysis/MacroExpansionContext.h"
24*e038c9c4Sjoerg #include "clang/Analysis/PathDiagnostic.h"
257330f729Sjoerg #include "clang/Basic/SourceManager.h"
267330f729Sjoerg #include "clang/CrossTU/CrossTranslationUnit.h"
277330f729Sjoerg #include "clang/Frontend/CompilerInstance.h"
287330f729Sjoerg #include "clang/Lex/Preprocessor.h"
29*e038c9c4Sjoerg #include "clang/Rewrite/Core/Rewriter.h"
307330f729Sjoerg #include "clang/StaticAnalyzer/Checkers/LocalCheckers.h"
317330f729Sjoerg #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
327330f729Sjoerg #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
337330f729Sjoerg #include "clang/StaticAnalyzer/Core/CheckerManager.h"
347330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
357330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
367330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
377330f729Sjoerg #include "llvm/ADT/PostOrderIterator.h"
387330f729Sjoerg #include "llvm/ADT/Statistic.h"
397330f729Sjoerg #include "llvm/Support/FileSystem.h"
407330f729Sjoerg #include "llvm/Support/Path.h"
417330f729Sjoerg #include "llvm/Support/Program.h"
427330f729Sjoerg #include "llvm/Support/Timer.h"
437330f729Sjoerg #include "llvm/Support/raw_ostream.h"
447330f729Sjoerg #include <memory>
457330f729Sjoerg #include <queue>
467330f729Sjoerg #include <utility>
477330f729Sjoerg
487330f729Sjoerg using namespace clang;
497330f729Sjoerg using namespace ento;
507330f729Sjoerg
517330f729Sjoerg #define DEBUG_TYPE "AnalysisConsumer"
527330f729Sjoerg
537330f729Sjoerg STATISTIC(NumFunctionTopLevel, "The # of functions at top level.");
547330f729Sjoerg STATISTIC(NumFunctionsAnalyzed,
557330f729Sjoerg "The # of functions and blocks analyzed (as top level "
567330f729Sjoerg "with inlining turned on).");
577330f729Sjoerg STATISTIC(NumBlocksInAnalyzedFunctions,
587330f729Sjoerg "The # of basic blocks in the analyzed functions.");
597330f729Sjoerg STATISTIC(NumVisitedBlocksInAnalyzedFunctions,
607330f729Sjoerg "The # of visited basic blocks in the analyzed functions.");
617330f729Sjoerg STATISTIC(PercentReachableBlocks, "The % of reachable basic blocks.");
627330f729Sjoerg STATISTIC(MaxCFGSize, "The maximum number of basic blocks in a function.");
637330f729Sjoerg
647330f729Sjoerg //===----------------------------------------------------------------------===//
657330f729Sjoerg // AnalysisConsumer declaration.
667330f729Sjoerg //===----------------------------------------------------------------------===//
677330f729Sjoerg
687330f729Sjoerg namespace {
697330f729Sjoerg
707330f729Sjoerg class AnalysisConsumer : public AnalysisASTConsumer,
717330f729Sjoerg public RecursiveASTVisitor<AnalysisConsumer> {
727330f729Sjoerg enum {
737330f729Sjoerg AM_None = 0,
747330f729Sjoerg AM_Syntax = 0x1,
757330f729Sjoerg AM_Path = 0x2
767330f729Sjoerg };
777330f729Sjoerg typedef unsigned AnalysisMode;
787330f729Sjoerg
797330f729Sjoerg /// Mode of the analyzes while recursively visiting Decls.
807330f729Sjoerg AnalysisMode RecVisitorMode;
817330f729Sjoerg /// Bug Reporter to use while recursively visiting Decls.
827330f729Sjoerg BugReporter *RecVisitorBR;
837330f729Sjoerg
847330f729Sjoerg std::vector<std::function<void(CheckerRegistry &)>> CheckerRegistrationFns;
857330f729Sjoerg
867330f729Sjoerg public:
877330f729Sjoerg ASTContext *Ctx;
88*e038c9c4Sjoerg Preprocessor &PP;
897330f729Sjoerg const std::string OutDir;
907330f729Sjoerg AnalyzerOptionsRef Opts;
917330f729Sjoerg ArrayRef<std::string> Plugins;
927330f729Sjoerg CodeInjector *Injector;
937330f729Sjoerg cross_tu::CrossTranslationUnitContext CTU;
947330f729Sjoerg
957330f729Sjoerg /// Stores the declarations from the local translation unit.
967330f729Sjoerg /// Note, we pre-compute the local declarations at parse time as an
977330f729Sjoerg /// optimization to make sure we do not deserialize everything from disk.
987330f729Sjoerg /// The local declaration to all declarations ratio might be very small when
997330f729Sjoerg /// working with a PCH file.
1007330f729Sjoerg SetOfDecls LocalTUDecls;
1017330f729Sjoerg
102*e038c9c4Sjoerg MacroExpansionContext MacroExpansions;
103*e038c9c4Sjoerg
1047330f729Sjoerg // Set of PathDiagnosticConsumers. Owned by AnalysisManager.
1057330f729Sjoerg PathDiagnosticConsumers PathConsumers;
1067330f729Sjoerg
1077330f729Sjoerg StoreManagerCreator CreateStoreMgr;
1087330f729Sjoerg ConstraintManagerCreator CreateConstraintMgr;
1097330f729Sjoerg
1107330f729Sjoerg std::unique_ptr<CheckerManager> checkerMgr;
1117330f729Sjoerg std::unique_ptr<AnalysisManager> Mgr;
1127330f729Sjoerg
1137330f729Sjoerg /// Time the analyzes time of each translation unit.
1147330f729Sjoerg std::unique_ptr<llvm::TimerGroup> AnalyzerTimers;
1157330f729Sjoerg std::unique_ptr<llvm::Timer> SyntaxCheckTimer;
1167330f729Sjoerg std::unique_ptr<llvm::Timer> ExprEngineTimer;
1177330f729Sjoerg std::unique_ptr<llvm::Timer> BugReporterTimer;
1187330f729Sjoerg
1197330f729Sjoerg /// The information about analyzed functions shared throughout the
1207330f729Sjoerg /// translation unit.
1217330f729Sjoerg FunctionSummariesTy FunctionSummaries;
1227330f729Sjoerg
AnalysisConsumer(CompilerInstance & CI,const std::string & outdir,AnalyzerOptionsRef opts,ArrayRef<std::string> plugins,CodeInjector * injector)1237330f729Sjoerg AnalysisConsumer(CompilerInstance &CI, const std::string &outdir,
1247330f729Sjoerg AnalyzerOptionsRef opts, ArrayRef<std::string> plugins,
1257330f729Sjoerg CodeInjector *injector)
1267330f729Sjoerg : RecVisitorMode(0), RecVisitorBR(nullptr), Ctx(nullptr),
1277330f729Sjoerg PP(CI.getPreprocessor()), OutDir(outdir), Opts(std::move(opts)),
128*e038c9c4Sjoerg Plugins(plugins), Injector(injector), CTU(CI),
129*e038c9c4Sjoerg MacroExpansions(CI.getLangOpts()) {
1307330f729Sjoerg DigestAnalyzerOptions();
1317330f729Sjoerg if (Opts->PrintStats || Opts->ShouldSerializeStats) {
1327330f729Sjoerg AnalyzerTimers = std::make_unique<llvm::TimerGroup>(
1337330f729Sjoerg "analyzer", "Analyzer timers");
1347330f729Sjoerg SyntaxCheckTimer = std::make_unique<llvm::Timer>(
1357330f729Sjoerg "syntaxchecks", "Syntax-based analysis time", *AnalyzerTimers);
1367330f729Sjoerg ExprEngineTimer = std::make_unique<llvm::Timer>(
1377330f729Sjoerg "exprengine", "Path exploration time", *AnalyzerTimers);
1387330f729Sjoerg BugReporterTimer = std::make_unique<llvm::Timer>(
1397330f729Sjoerg "bugreporter", "Path-sensitive report post-processing time",
1407330f729Sjoerg *AnalyzerTimers);
1417330f729Sjoerg llvm::EnableStatistics(/* PrintOnExit= */ false);
1427330f729Sjoerg }
143*e038c9c4Sjoerg
144*e038c9c4Sjoerg if (Opts->ShouldDisplayMacroExpansions)
145*e038c9c4Sjoerg MacroExpansions.registerForPreprocessor(PP);
1467330f729Sjoerg }
1477330f729Sjoerg
~AnalysisConsumer()1487330f729Sjoerg ~AnalysisConsumer() override {
1497330f729Sjoerg if (Opts->PrintStats) {
1507330f729Sjoerg llvm::PrintStatistics();
1517330f729Sjoerg }
1527330f729Sjoerg }
1537330f729Sjoerg
DigestAnalyzerOptions()1547330f729Sjoerg void DigestAnalyzerOptions() {
1557330f729Sjoerg switch (Opts->AnalysisDiagOpt) {
156*e038c9c4Sjoerg case PD_NONE:
157*e038c9c4Sjoerg break;
1587330f729Sjoerg #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN) \
1597330f729Sjoerg case PD_##NAME: \
160*e038c9c4Sjoerg CREATEFN(Opts->getDiagOpts(), PathConsumers, OutDir, PP, CTU, \
161*e038c9c4Sjoerg MacroExpansions); \
1627330f729Sjoerg break;
1637330f729Sjoerg #include "clang/StaticAnalyzer/Core/Analyses.def"
164*e038c9c4Sjoerg default:
165*e038c9c4Sjoerg llvm_unreachable("Unknown analyzer output type!");
1667330f729Sjoerg }
1677330f729Sjoerg
1687330f729Sjoerg // Create the analyzer component creators.
1697330f729Sjoerg switch (Opts->AnalysisStoreOpt) {
1707330f729Sjoerg default:
1717330f729Sjoerg llvm_unreachable("Unknown store manager.");
1727330f729Sjoerg #define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \
1737330f729Sjoerg case NAME##Model: CreateStoreMgr = CREATEFN; break;
1747330f729Sjoerg #include "clang/StaticAnalyzer/Core/Analyses.def"
1757330f729Sjoerg }
1767330f729Sjoerg
1777330f729Sjoerg switch (Opts->AnalysisConstraintsOpt) {
1787330f729Sjoerg default:
1797330f729Sjoerg llvm_unreachable("Unknown constraint manager.");
1807330f729Sjoerg #define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \
1817330f729Sjoerg case NAME##Model: CreateConstraintMgr = CREATEFN; break;
1827330f729Sjoerg #include "clang/StaticAnalyzer/Core/Analyses.def"
1837330f729Sjoerg }
1847330f729Sjoerg }
1857330f729Sjoerg
DisplayFunction(const Decl * D,AnalysisMode Mode,ExprEngine::InliningModes IMode)1867330f729Sjoerg void DisplayFunction(const Decl *D, AnalysisMode Mode,
1877330f729Sjoerg ExprEngine::InliningModes IMode) {
1887330f729Sjoerg if (!Opts->AnalyzerDisplayProgress)
1897330f729Sjoerg return;
1907330f729Sjoerg
1917330f729Sjoerg SourceManager &SM = Mgr->getASTContext().getSourceManager();
1927330f729Sjoerg PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
1937330f729Sjoerg if (Loc.isValid()) {
1947330f729Sjoerg llvm::errs() << "ANALYZE";
1957330f729Sjoerg
1967330f729Sjoerg if (Mode == AM_Syntax)
1977330f729Sjoerg llvm::errs() << " (Syntax)";
1987330f729Sjoerg else if (Mode == AM_Path) {
1997330f729Sjoerg llvm::errs() << " (Path, ";
2007330f729Sjoerg switch (IMode) {
2017330f729Sjoerg case ExprEngine::Inline_Minimal:
2027330f729Sjoerg llvm::errs() << " Inline_Minimal";
2037330f729Sjoerg break;
2047330f729Sjoerg case ExprEngine::Inline_Regular:
2057330f729Sjoerg llvm::errs() << " Inline_Regular";
2067330f729Sjoerg break;
2077330f729Sjoerg }
2087330f729Sjoerg llvm::errs() << ")";
209*e038c9c4Sjoerg } else
2107330f729Sjoerg assert(Mode == (AM_Syntax | AM_Path) && "Unexpected mode!");
2117330f729Sjoerg
212*e038c9c4Sjoerg llvm::errs() << ": " << Loc.getFilename() << ' ' << getFunctionName(D)
213*e038c9c4Sjoerg << '\n';
2147330f729Sjoerg }
2157330f729Sjoerg }
2167330f729Sjoerg
Initialize(ASTContext & Context)2177330f729Sjoerg void Initialize(ASTContext &Context) override {
2187330f729Sjoerg Ctx = &Context;
219*e038c9c4Sjoerg checkerMgr = std::make_unique<CheckerManager>(*Ctx, *Opts, PP, Plugins,
220*e038c9c4Sjoerg CheckerRegistrationFns);
2217330f729Sjoerg
222*e038c9c4Sjoerg Mgr = std::make_unique<AnalysisManager>(*Ctx, PP, PathConsumers,
223*e038c9c4Sjoerg CreateStoreMgr, CreateConstraintMgr,
2247330f729Sjoerg checkerMgr.get(), *Opts, Injector);
2257330f729Sjoerg }
2267330f729Sjoerg
2277330f729Sjoerg /// Store the top level decls in the set to be processed later on.
2287330f729Sjoerg /// (Doing this pre-processing avoids deserialization of data from PCH.)
2297330f729Sjoerg bool HandleTopLevelDecl(DeclGroupRef D) override;
2307330f729Sjoerg void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override;
2317330f729Sjoerg
2327330f729Sjoerg void HandleTranslationUnit(ASTContext &C) override;
2337330f729Sjoerg
2347330f729Sjoerg /// Determine which inlining mode should be used when this function is
2357330f729Sjoerg /// analyzed. This allows to redefine the default inlining policies when
2367330f729Sjoerg /// analyzing a given function.
2377330f729Sjoerg ExprEngine::InliningModes
2387330f729Sjoerg getInliningModeForFunction(const Decl *D, const SetOfConstDecls &Visited);
2397330f729Sjoerg
2407330f729Sjoerg /// Build the call graph for all the top level decls of this TU and
2417330f729Sjoerg /// use it to define the order in which the functions should be visited.
2427330f729Sjoerg void HandleDeclsCallGraph(const unsigned LocalTUDeclsSize);
2437330f729Sjoerg
2447330f729Sjoerg /// Run analyzes(syntax or path sensitive) on the given function.
2457330f729Sjoerg /// \param Mode - determines if we are requesting syntax only or path
2467330f729Sjoerg /// sensitive only analysis.
2477330f729Sjoerg /// \param VisitedCallees - The output parameter, which is populated with the
2487330f729Sjoerg /// set of functions which should be considered analyzed after analyzing the
2497330f729Sjoerg /// given root function.
2507330f729Sjoerg void HandleCode(Decl *D, AnalysisMode Mode,
2517330f729Sjoerg ExprEngine::InliningModes IMode = ExprEngine::Inline_Minimal,
2527330f729Sjoerg SetOfConstDecls *VisitedCallees = nullptr);
2537330f729Sjoerg
2547330f729Sjoerg void RunPathSensitiveChecks(Decl *D,
2557330f729Sjoerg ExprEngine::InliningModes IMode,
2567330f729Sjoerg SetOfConstDecls *VisitedCallees);
2577330f729Sjoerg
2587330f729Sjoerg /// Visitors for the RecursiveASTVisitor.
shouldWalkTypesOfTypeLocs() const2597330f729Sjoerg bool shouldWalkTypesOfTypeLocs() const { return false; }
2607330f729Sjoerg
2617330f729Sjoerg /// Handle callbacks for arbitrary Decls.
VisitDecl(Decl * D)2627330f729Sjoerg bool VisitDecl(Decl *D) {
2637330f729Sjoerg AnalysisMode Mode = getModeForDecl(D, RecVisitorMode);
2647330f729Sjoerg if (Mode & AM_Syntax) {
2657330f729Sjoerg if (SyntaxCheckTimer)
2667330f729Sjoerg SyntaxCheckTimer->startTimer();
2677330f729Sjoerg checkerMgr->runCheckersOnASTDecl(D, *Mgr, *RecVisitorBR);
2687330f729Sjoerg if (SyntaxCheckTimer)
2697330f729Sjoerg SyntaxCheckTimer->stopTimer();
2707330f729Sjoerg }
2717330f729Sjoerg return true;
2727330f729Sjoerg }
2737330f729Sjoerg
VisitVarDecl(VarDecl * VD)2747330f729Sjoerg bool VisitVarDecl(VarDecl *VD) {
2757330f729Sjoerg if (!Opts->IsNaiveCTUEnabled)
2767330f729Sjoerg return true;
2777330f729Sjoerg
2787330f729Sjoerg if (VD->hasExternalStorage() || VD->isStaticDataMember()) {
2797330f729Sjoerg if (!cross_tu::containsConst(VD, *Ctx))
2807330f729Sjoerg return true;
2817330f729Sjoerg } else {
2827330f729Sjoerg // Cannot be initialized in another TU.
2837330f729Sjoerg return true;
2847330f729Sjoerg }
2857330f729Sjoerg
2867330f729Sjoerg if (VD->getAnyInitializer())
2877330f729Sjoerg return true;
2887330f729Sjoerg
2897330f729Sjoerg llvm::Expected<const VarDecl *> CTUDeclOrError =
2907330f729Sjoerg CTU.getCrossTUDefinition(VD, Opts->CTUDir, Opts->CTUIndexName,
2917330f729Sjoerg Opts->DisplayCTUProgress);
2927330f729Sjoerg
2937330f729Sjoerg if (!CTUDeclOrError) {
2947330f729Sjoerg handleAllErrors(CTUDeclOrError.takeError(),
2957330f729Sjoerg [&](const cross_tu::IndexError &IE) {
2967330f729Sjoerg CTU.emitCrossTUDiagnostics(IE);
2977330f729Sjoerg });
2987330f729Sjoerg }
2997330f729Sjoerg
3007330f729Sjoerg return true;
3017330f729Sjoerg }
3027330f729Sjoerg
VisitFunctionDecl(FunctionDecl * FD)3037330f729Sjoerg bool VisitFunctionDecl(FunctionDecl *FD) {
3047330f729Sjoerg IdentifierInfo *II = FD->getIdentifier();
3057330f729Sjoerg if (II && II->getName().startswith("__inline"))
3067330f729Sjoerg return true;
3077330f729Sjoerg
3087330f729Sjoerg // We skip function template definitions, as their semantics is
3097330f729Sjoerg // only determined when they are instantiated.
3107330f729Sjoerg if (FD->isThisDeclarationADefinition() &&
3117330f729Sjoerg !FD->isDependentContext()) {
3127330f729Sjoerg assert(RecVisitorMode == AM_Syntax || Mgr->shouldInlineCall() == false);
3137330f729Sjoerg HandleCode(FD, RecVisitorMode);
3147330f729Sjoerg }
3157330f729Sjoerg return true;
3167330f729Sjoerg }
3177330f729Sjoerg
VisitObjCMethodDecl(ObjCMethodDecl * MD)3187330f729Sjoerg bool VisitObjCMethodDecl(ObjCMethodDecl *MD) {
3197330f729Sjoerg if (MD->isThisDeclarationADefinition()) {
3207330f729Sjoerg assert(RecVisitorMode == AM_Syntax || Mgr->shouldInlineCall() == false);
3217330f729Sjoerg HandleCode(MD, RecVisitorMode);
3227330f729Sjoerg }
3237330f729Sjoerg return true;
3247330f729Sjoerg }
3257330f729Sjoerg
VisitBlockDecl(BlockDecl * BD)3267330f729Sjoerg bool VisitBlockDecl(BlockDecl *BD) {
3277330f729Sjoerg if (BD->hasBody()) {
3287330f729Sjoerg assert(RecVisitorMode == AM_Syntax || Mgr->shouldInlineCall() == false);
3297330f729Sjoerg // Since we skip function template definitions, we should skip blocks
3307330f729Sjoerg // declared in those functions as well.
3317330f729Sjoerg if (!BD->isDependentContext()) {
3327330f729Sjoerg HandleCode(BD, RecVisitorMode);
3337330f729Sjoerg }
3347330f729Sjoerg }
3357330f729Sjoerg return true;
3367330f729Sjoerg }
3377330f729Sjoerg
AddDiagnosticConsumer(PathDiagnosticConsumer * Consumer)3387330f729Sjoerg void AddDiagnosticConsumer(PathDiagnosticConsumer *Consumer) override {
3397330f729Sjoerg PathConsumers.push_back(Consumer);
3407330f729Sjoerg }
3417330f729Sjoerg
AddCheckerRegistrationFn(std::function<void (CheckerRegistry &)> Fn)3427330f729Sjoerg void AddCheckerRegistrationFn(std::function<void(CheckerRegistry&)> Fn) override {
3437330f729Sjoerg CheckerRegistrationFns.push_back(std::move(Fn));
3447330f729Sjoerg }
3457330f729Sjoerg
3467330f729Sjoerg private:
3477330f729Sjoerg void storeTopLevelDecls(DeclGroupRef DG);
3487330f729Sjoerg std::string getFunctionName(const Decl *D);
3497330f729Sjoerg
3507330f729Sjoerg /// Check if we should skip (not analyze) the given function.
3517330f729Sjoerg AnalysisMode getModeForDecl(Decl *D, AnalysisMode Mode);
3527330f729Sjoerg void runAnalysisOnTranslationUnit(ASTContext &C);
3537330f729Sjoerg
3547330f729Sjoerg /// Print \p S to stderr if \c Opts->AnalyzerDisplayProgress is set.
3557330f729Sjoerg void reportAnalyzerProgress(StringRef S);
356*e038c9c4Sjoerg }; // namespace
3577330f729Sjoerg } // end anonymous namespace
3587330f729Sjoerg
3597330f729Sjoerg
3607330f729Sjoerg //===----------------------------------------------------------------------===//
3617330f729Sjoerg // AnalysisConsumer implementation.
3627330f729Sjoerg //===----------------------------------------------------------------------===//
HandleTopLevelDecl(DeclGroupRef DG)3637330f729Sjoerg bool AnalysisConsumer::HandleTopLevelDecl(DeclGroupRef DG) {
3647330f729Sjoerg storeTopLevelDecls(DG);
3657330f729Sjoerg return true;
3667330f729Sjoerg }
3677330f729Sjoerg
HandleTopLevelDeclInObjCContainer(DeclGroupRef DG)3687330f729Sjoerg void AnalysisConsumer::HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) {
3697330f729Sjoerg storeTopLevelDecls(DG);
3707330f729Sjoerg }
3717330f729Sjoerg
storeTopLevelDecls(DeclGroupRef DG)3727330f729Sjoerg void AnalysisConsumer::storeTopLevelDecls(DeclGroupRef DG) {
3737330f729Sjoerg for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) {
3747330f729Sjoerg
3757330f729Sjoerg // Skip ObjCMethodDecl, wait for the objc container to avoid
3767330f729Sjoerg // analyzing twice.
3777330f729Sjoerg if (isa<ObjCMethodDecl>(*I))
3787330f729Sjoerg continue;
3797330f729Sjoerg
3807330f729Sjoerg LocalTUDecls.push_back(*I);
3817330f729Sjoerg }
3827330f729Sjoerg }
3837330f729Sjoerg
shouldSkipFunction(const Decl * D,const SetOfConstDecls & Visited,const SetOfConstDecls & VisitedAsTopLevel)3847330f729Sjoerg static bool shouldSkipFunction(const Decl *D,
3857330f729Sjoerg const SetOfConstDecls &Visited,
3867330f729Sjoerg const SetOfConstDecls &VisitedAsTopLevel) {
3877330f729Sjoerg if (VisitedAsTopLevel.count(D))
3887330f729Sjoerg return true;
3897330f729Sjoerg
390*e038c9c4Sjoerg // Skip analysis of inheriting constructors as top-level functions. These
391*e038c9c4Sjoerg // constructors don't even have a body written down in the code, so even if
392*e038c9c4Sjoerg // we find a bug, we won't be able to display it.
393*e038c9c4Sjoerg if (const auto *CD = dyn_cast<CXXConstructorDecl>(D))
394*e038c9c4Sjoerg if (CD->isInheritingConstructor())
395*e038c9c4Sjoerg return true;
396*e038c9c4Sjoerg
3977330f729Sjoerg // We want to re-analyse the functions as top level in the following cases:
3987330f729Sjoerg // - The 'init' methods should be reanalyzed because
3997330f729Sjoerg // ObjCNonNilReturnValueChecker assumes that '[super init]' never returns
4007330f729Sjoerg // 'nil' and unless we analyze the 'init' functions as top level, we will
4017330f729Sjoerg // not catch errors within defensive code.
4027330f729Sjoerg // - We want to reanalyze all ObjC methods as top level to report Retain
4037330f729Sjoerg // Count naming convention errors more aggressively.
4047330f729Sjoerg if (isa<ObjCMethodDecl>(D))
4057330f729Sjoerg return false;
4067330f729Sjoerg // We also want to reanalyze all C++ copy and move assignment operators to
4077330f729Sjoerg // separately check the two cases where 'this' aliases with the parameter and
4087330f729Sjoerg // where it may not. (cplusplus.SelfAssignmentChecker)
4097330f729Sjoerg if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
4107330f729Sjoerg if (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())
4117330f729Sjoerg return false;
4127330f729Sjoerg }
4137330f729Sjoerg
4147330f729Sjoerg // Otherwise, if we visited the function before, do not reanalyze it.
4157330f729Sjoerg return Visited.count(D);
4167330f729Sjoerg }
4177330f729Sjoerg
4187330f729Sjoerg ExprEngine::InliningModes
getInliningModeForFunction(const Decl * D,const SetOfConstDecls & Visited)4197330f729Sjoerg AnalysisConsumer::getInliningModeForFunction(const Decl *D,
4207330f729Sjoerg const SetOfConstDecls &Visited) {
4217330f729Sjoerg // We want to reanalyze all ObjC methods as top level to report Retain
4227330f729Sjoerg // Count naming convention errors more aggressively. But we should tune down
4237330f729Sjoerg // inlining when reanalyzing an already inlined function.
4247330f729Sjoerg if (Visited.count(D) && isa<ObjCMethodDecl>(D)) {
4257330f729Sjoerg const ObjCMethodDecl *ObjCM = cast<ObjCMethodDecl>(D);
4267330f729Sjoerg if (ObjCM->getMethodFamily() != OMF_init)
4277330f729Sjoerg return ExprEngine::Inline_Minimal;
4287330f729Sjoerg }
4297330f729Sjoerg
4307330f729Sjoerg return ExprEngine::Inline_Regular;
4317330f729Sjoerg }
4327330f729Sjoerg
HandleDeclsCallGraph(const unsigned LocalTUDeclsSize)4337330f729Sjoerg void AnalysisConsumer::HandleDeclsCallGraph(const unsigned LocalTUDeclsSize) {
4347330f729Sjoerg // Build the Call Graph by adding all the top level declarations to the graph.
4357330f729Sjoerg // Note: CallGraph can trigger deserialization of more items from a pch
4367330f729Sjoerg // (though HandleInterestingDecl); triggering additions to LocalTUDecls.
4377330f729Sjoerg // We rely on random access to add the initially processed Decls to CG.
4387330f729Sjoerg CallGraph CG;
4397330f729Sjoerg for (unsigned i = 0 ; i < LocalTUDeclsSize ; ++i) {
4407330f729Sjoerg CG.addToCallGraph(LocalTUDecls[i]);
4417330f729Sjoerg }
4427330f729Sjoerg
4437330f729Sjoerg // Walk over all of the call graph nodes in topological order, so that we
4447330f729Sjoerg // analyze parents before the children. Skip the functions inlined into
4457330f729Sjoerg // the previously processed functions. Use external Visited set to identify
4467330f729Sjoerg // inlined functions. The topological order allows the "do not reanalyze
4477330f729Sjoerg // previously inlined function" performance heuristic to be triggered more
4487330f729Sjoerg // often.
4497330f729Sjoerg SetOfConstDecls Visited;
4507330f729Sjoerg SetOfConstDecls VisitedAsTopLevel;
4517330f729Sjoerg llvm::ReversePostOrderTraversal<clang::CallGraph*> RPOT(&CG);
4527330f729Sjoerg for (llvm::ReversePostOrderTraversal<clang::CallGraph*>::rpo_iterator
4537330f729Sjoerg I = RPOT.begin(), E = RPOT.end(); I != E; ++I) {
4547330f729Sjoerg NumFunctionTopLevel++;
4557330f729Sjoerg
4567330f729Sjoerg CallGraphNode *N = *I;
4577330f729Sjoerg Decl *D = N->getDecl();
4587330f729Sjoerg
4597330f729Sjoerg // Skip the abstract root node.
4607330f729Sjoerg if (!D)
4617330f729Sjoerg continue;
4627330f729Sjoerg
4637330f729Sjoerg // Skip the functions which have been processed already or previously
4647330f729Sjoerg // inlined.
4657330f729Sjoerg if (shouldSkipFunction(D, Visited, VisitedAsTopLevel))
4667330f729Sjoerg continue;
4677330f729Sjoerg
4687330f729Sjoerg // Analyze the function.
4697330f729Sjoerg SetOfConstDecls VisitedCallees;
4707330f729Sjoerg
4717330f729Sjoerg HandleCode(D, AM_Path, getInliningModeForFunction(D, Visited),
4727330f729Sjoerg (Mgr->options.InliningMode == All ? nullptr : &VisitedCallees));
4737330f729Sjoerg
4747330f729Sjoerg // Add the visited callees to the global visited set.
4757330f729Sjoerg for (const Decl *Callee : VisitedCallees)
4767330f729Sjoerg // Decls from CallGraph are already canonical. But Decls coming from
4777330f729Sjoerg // CallExprs may be not. We should canonicalize them manually.
4787330f729Sjoerg Visited.insert(isa<ObjCMethodDecl>(Callee) ? Callee
4797330f729Sjoerg : Callee->getCanonicalDecl());
4807330f729Sjoerg VisitedAsTopLevel.insert(D);
4817330f729Sjoerg }
4827330f729Sjoerg }
4837330f729Sjoerg
isBisonFile(ASTContext & C)4847330f729Sjoerg static bool isBisonFile(ASTContext &C) {
4857330f729Sjoerg const SourceManager &SM = C.getSourceManager();
4867330f729Sjoerg FileID FID = SM.getMainFileID();
487*e038c9c4Sjoerg StringRef Buffer = SM.getBufferOrFake(FID).getBuffer();
4887330f729Sjoerg if (Buffer.startswith("/* A Bison parser, made by"))
4897330f729Sjoerg return true;
4907330f729Sjoerg return false;
4917330f729Sjoerg }
4927330f729Sjoerg
runAnalysisOnTranslationUnit(ASTContext & C)4937330f729Sjoerg void AnalysisConsumer::runAnalysisOnTranslationUnit(ASTContext &C) {
4947330f729Sjoerg BugReporter BR(*Mgr);
4957330f729Sjoerg TranslationUnitDecl *TU = C.getTranslationUnitDecl();
4967330f729Sjoerg if (SyntaxCheckTimer)
4977330f729Sjoerg SyntaxCheckTimer->startTimer();
4987330f729Sjoerg checkerMgr->runCheckersOnASTDecl(TU, *Mgr, BR);
4997330f729Sjoerg if (SyntaxCheckTimer)
5007330f729Sjoerg SyntaxCheckTimer->stopTimer();
5017330f729Sjoerg
5027330f729Sjoerg // Run the AST-only checks using the order in which functions are defined.
5037330f729Sjoerg // If inlining is not turned on, use the simplest function order for path
5047330f729Sjoerg // sensitive analyzes as well.
5057330f729Sjoerg RecVisitorMode = AM_Syntax;
5067330f729Sjoerg if (!Mgr->shouldInlineCall())
5077330f729Sjoerg RecVisitorMode |= AM_Path;
5087330f729Sjoerg RecVisitorBR = &BR;
5097330f729Sjoerg
5107330f729Sjoerg // Process all the top level declarations.
5117330f729Sjoerg //
5127330f729Sjoerg // Note: TraverseDecl may modify LocalTUDecls, but only by appending more
5137330f729Sjoerg // entries. Thus we don't use an iterator, but rely on LocalTUDecls
5147330f729Sjoerg // random access. By doing so, we automatically compensate for iterators
5157330f729Sjoerg // possibly being invalidated, although this is a bit slower.
5167330f729Sjoerg const unsigned LocalTUDeclsSize = LocalTUDecls.size();
5177330f729Sjoerg for (unsigned i = 0 ; i < LocalTUDeclsSize ; ++i) {
5187330f729Sjoerg TraverseDecl(LocalTUDecls[i]);
5197330f729Sjoerg }
5207330f729Sjoerg
5217330f729Sjoerg if (Mgr->shouldInlineCall())
5227330f729Sjoerg HandleDeclsCallGraph(LocalTUDeclsSize);
5237330f729Sjoerg
5247330f729Sjoerg // After all decls handled, run checkers on the entire TranslationUnit.
5257330f729Sjoerg checkerMgr->runCheckersOnEndOfTranslationUnit(TU, *Mgr, BR);
5267330f729Sjoerg
5277330f729Sjoerg BR.FlushReports();
5287330f729Sjoerg RecVisitorBR = nullptr;
5297330f729Sjoerg }
5307330f729Sjoerg
reportAnalyzerProgress(StringRef S)5317330f729Sjoerg void AnalysisConsumer::reportAnalyzerProgress(StringRef S) {
5327330f729Sjoerg if (Opts->AnalyzerDisplayProgress)
5337330f729Sjoerg llvm::errs() << S;
5347330f729Sjoerg }
5357330f729Sjoerg
HandleTranslationUnit(ASTContext & C)5367330f729Sjoerg void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
5377330f729Sjoerg
5387330f729Sjoerg // Don't run the actions if an error has occurred with parsing the file.
5397330f729Sjoerg DiagnosticsEngine &Diags = PP.getDiagnostics();
5407330f729Sjoerg if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred())
5417330f729Sjoerg return;
5427330f729Sjoerg
5437330f729Sjoerg if (isBisonFile(C)) {
5447330f729Sjoerg reportAnalyzerProgress("Skipping bison-generated file\n");
5457330f729Sjoerg } else if (Opts->DisableAllCheckers) {
5467330f729Sjoerg
5477330f729Sjoerg // Don't analyze if the user explicitly asked for no checks to be performed
5487330f729Sjoerg // on this file.
5497330f729Sjoerg reportAnalyzerProgress("All checks are disabled using a supplied option\n");
5507330f729Sjoerg } else {
5517330f729Sjoerg // Otherwise, just run the analysis.
5527330f729Sjoerg runAnalysisOnTranslationUnit(C);
5537330f729Sjoerg }
5547330f729Sjoerg
5557330f729Sjoerg // Count how many basic blocks we have not covered.
5567330f729Sjoerg NumBlocksInAnalyzedFunctions = FunctionSummaries.getTotalNumBasicBlocks();
5577330f729Sjoerg NumVisitedBlocksInAnalyzedFunctions =
5587330f729Sjoerg FunctionSummaries.getTotalNumVisitedBasicBlocks();
5597330f729Sjoerg if (NumBlocksInAnalyzedFunctions > 0)
5607330f729Sjoerg PercentReachableBlocks =
5617330f729Sjoerg (FunctionSummaries.getTotalNumVisitedBasicBlocks() * 100) /
5627330f729Sjoerg NumBlocksInAnalyzedFunctions;
5637330f729Sjoerg
5647330f729Sjoerg // Explicitly destroy the PathDiagnosticConsumer. This will flush its output.
5657330f729Sjoerg // FIXME: This should be replaced with something that doesn't rely on
5667330f729Sjoerg // side-effects in PathDiagnosticConsumer's destructor. This is required when
5677330f729Sjoerg // used with option -disable-free.
5687330f729Sjoerg Mgr.reset();
5697330f729Sjoerg }
5707330f729Sjoerg
getFunctionName(const Decl * D)5717330f729Sjoerg std::string AnalysisConsumer::getFunctionName(const Decl *D) {
5727330f729Sjoerg std::string Str;
5737330f729Sjoerg llvm::raw_string_ostream OS(Str);
5747330f729Sjoerg
5757330f729Sjoerg if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5767330f729Sjoerg OS << FD->getQualifiedNameAsString();
5777330f729Sjoerg
5787330f729Sjoerg // In C++, there are overloads.
5797330f729Sjoerg if (Ctx->getLangOpts().CPlusPlus) {
5807330f729Sjoerg OS << '(';
5817330f729Sjoerg for (const auto &P : FD->parameters()) {
5827330f729Sjoerg if (P != *FD->param_begin())
5837330f729Sjoerg OS << ", ";
5847330f729Sjoerg OS << P->getType().getAsString();
5857330f729Sjoerg }
5867330f729Sjoerg OS << ')';
5877330f729Sjoerg }
5887330f729Sjoerg
5897330f729Sjoerg } else if (isa<BlockDecl>(D)) {
5907330f729Sjoerg PresumedLoc Loc = Ctx->getSourceManager().getPresumedLoc(D->getLocation());
5917330f729Sjoerg
5927330f729Sjoerg if (Loc.isValid()) {
5937330f729Sjoerg OS << "block (line: " << Loc.getLine() << ", col: " << Loc.getColumn()
5947330f729Sjoerg << ')';
5957330f729Sjoerg }
5967330f729Sjoerg
5977330f729Sjoerg } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
5987330f729Sjoerg
5997330f729Sjoerg // FIXME: copy-pasted from CGDebugInfo.cpp.
6007330f729Sjoerg OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
6017330f729Sjoerg const DeclContext *DC = OMD->getDeclContext();
6027330f729Sjoerg if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) {
6037330f729Sjoerg OS << OID->getName();
6047330f729Sjoerg } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) {
6057330f729Sjoerg OS << OID->getName();
6067330f729Sjoerg } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
6077330f729Sjoerg if (OC->IsClassExtension()) {
6087330f729Sjoerg OS << OC->getClassInterface()->getName();
6097330f729Sjoerg } else {
6107330f729Sjoerg OS << OC->getIdentifier()->getNameStart() << '('
6117330f729Sjoerg << OC->getIdentifier()->getNameStart() << ')';
6127330f729Sjoerg }
6137330f729Sjoerg } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) {
6147330f729Sjoerg OS << OCD->getClassInterface()->getName() << '('
6157330f729Sjoerg << OCD->getName() << ')';
6167330f729Sjoerg }
6177330f729Sjoerg OS << ' ' << OMD->getSelector().getAsString() << ']';
6187330f729Sjoerg
6197330f729Sjoerg }
6207330f729Sjoerg
6217330f729Sjoerg return OS.str();
6227330f729Sjoerg }
6237330f729Sjoerg
6247330f729Sjoerg AnalysisConsumer::AnalysisMode
getModeForDecl(Decl * D,AnalysisMode Mode)6257330f729Sjoerg AnalysisConsumer::getModeForDecl(Decl *D, AnalysisMode Mode) {
6267330f729Sjoerg if (!Opts->AnalyzeSpecificFunction.empty() &&
6277330f729Sjoerg getFunctionName(D) != Opts->AnalyzeSpecificFunction)
6287330f729Sjoerg return AM_None;
6297330f729Sjoerg
6307330f729Sjoerg // Unless -analyze-all is specified, treat decls differently depending on
6317330f729Sjoerg // where they came from:
6327330f729Sjoerg // - Main source file: run both path-sensitive and non-path-sensitive checks.
6337330f729Sjoerg // - Header files: run non-path-sensitive checks only.
6347330f729Sjoerg // - System headers: don't run any checks.
6357330f729Sjoerg SourceManager &SM = Ctx->getSourceManager();
6367330f729Sjoerg const Stmt *Body = D->getBody();
6377330f729Sjoerg SourceLocation SL = Body ? Body->getBeginLoc() : D->getLocation();
6387330f729Sjoerg SL = SM.getExpansionLoc(SL);
6397330f729Sjoerg
6407330f729Sjoerg if (!Opts->AnalyzeAll && !Mgr->isInCodeFile(SL)) {
6417330f729Sjoerg if (SL.isInvalid() || SM.isInSystemHeader(SL))
6427330f729Sjoerg return AM_None;
6437330f729Sjoerg return Mode & ~AM_Path;
6447330f729Sjoerg }
6457330f729Sjoerg
6467330f729Sjoerg return Mode;
6477330f729Sjoerg }
6487330f729Sjoerg
HandleCode(Decl * D,AnalysisMode Mode,ExprEngine::InliningModes IMode,SetOfConstDecls * VisitedCallees)6497330f729Sjoerg void AnalysisConsumer::HandleCode(Decl *D, AnalysisMode Mode,
6507330f729Sjoerg ExprEngine::InliningModes IMode,
6517330f729Sjoerg SetOfConstDecls *VisitedCallees) {
6527330f729Sjoerg if (!D->hasBody())
6537330f729Sjoerg return;
6547330f729Sjoerg Mode = getModeForDecl(D, Mode);
6557330f729Sjoerg if (Mode == AM_None)
6567330f729Sjoerg return;
6577330f729Sjoerg
6587330f729Sjoerg // Clear the AnalysisManager of old AnalysisDeclContexts.
6597330f729Sjoerg Mgr->ClearContexts();
6607330f729Sjoerg // Ignore autosynthesized code.
6617330f729Sjoerg if (Mgr->getAnalysisDeclContext(D)->isBodyAutosynthesized())
6627330f729Sjoerg return;
6637330f729Sjoerg
6647330f729Sjoerg DisplayFunction(D, Mode, IMode);
6657330f729Sjoerg CFG *DeclCFG = Mgr->getCFG(D);
6667330f729Sjoerg if (DeclCFG)
6677330f729Sjoerg MaxCFGSize.updateMax(DeclCFG->size());
6687330f729Sjoerg
6697330f729Sjoerg BugReporter BR(*Mgr);
6707330f729Sjoerg
6717330f729Sjoerg if (Mode & AM_Syntax) {
6727330f729Sjoerg if (SyntaxCheckTimer)
6737330f729Sjoerg SyntaxCheckTimer->startTimer();
6747330f729Sjoerg checkerMgr->runCheckersOnASTBody(D, *Mgr, BR);
6757330f729Sjoerg if (SyntaxCheckTimer)
6767330f729Sjoerg SyntaxCheckTimer->stopTimer();
6777330f729Sjoerg }
6787330f729Sjoerg
6797330f729Sjoerg BR.FlushReports();
6807330f729Sjoerg
6817330f729Sjoerg if ((Mode & AM_Path) && checkerMgr->hasPathSensitiveCheckers()) {
6827330f729Sjoerg RunPathSensitiveChecks(D, IMode, VisitedCallees);
6837330f729Sjoerg if (IMode != ExprEngine::Inline_Minimal)
6847330f729Sjoerg NumFunctionsAnalyzed++;
6857330f729Sjoerg }
6867330f729Sjoerg }
6877330f729Sjoerg
6887330f729Sjoerg //===----------------------------------------------------------------------===//
6897330f729Sjoerg // Path-sensitive checking.
6907330f729Sjoerg //===----------------------------------------------------------------------===//
6917330f729Sjoerg
RunPathSensitiveChecks(Decl * D,ExprEngine::InliningModes IMode,SetOfConstDecls * VisitedCallees)6927330f729Sjoerg void AnalysisConsumer::RunPathSensitiveChecks(Decl *D,
6937330f729Sjoerg ExprEngine::InliningModes IMode,
6947330f729Sjoerg SetOfConstDecls *VisitedCallees) {
6957330f729Sjoerg // Construct the analysis engine. First check if the CFG is valid.
6967330f729Sjoerg // FIXME: Inter-procedural analysis will need to handle invalid CFGs.
6977330f729Sjoerg if (!Mgr->getCFG(D))
6987330f729Sjoerg return;
6997330f729Sjoerg
7007330f729Sjoerg // See if the LiveVariables analysis scales.
7017330f729Sjoerg if (!Mgr->getAnalysisDeclContext(D)->getAnalysis<RelaxedLiveVariables>())
7027330f729Sjoerg return;
7037330f729Sjoerg
7047330f729Sjoerg ExprEngine Eng(CTU, *Mgr, VisitedCallees, &FunctionSummaries, IMode);
7057330f729Sjoerg
7067330f729Sjoerg // Execute the worklist algorithm.
7077330f729Sjoerg if (ExprEngineTimer)
7087330f729Sjoerg ExprEngineTimer->startTimer();
7097330f729Sjoerg Eng.ExecuteWorkList(Mgr->getAnalysisDeclContextManager().getStackFrame(D),
7107330f729Sjoerg Mgr->options.MaxNodesPerTopLevelFunction);
7117330f729Sjoerg if (ExprEngineTimer)
7127330f729Sjoerg ExprEngineTimer->stopTimer();
7137330f729Sjoerg
7147330f729Sjoerg if (!Mgr->options.DumpExplodedGraphTo.empty())
7157330f729Sjoerg Eng.DumpGraph(Mgr->options.TrimGraph, Mgr->options.DumpExplodedGraphTo);
7167330f729Sjoerg
7177330f729Sjoerg // Visualize the exploded graph.
7187330f729Sjoerg if (Mgr->options.visualizeExplodedGraphWithGraphViz)
7197330f729Sjoerg Eng.ViewGraph(Mgr->options.TrimGraph);
7207330f729Sjoerg
7217330f729Sjoerg // Display warnings.
7227330f729Sjoerg if (BugReporterTimer)
7237330f729Sjoerg BugReporterTimer->startTimer();
7247330f729Sjoerg Eng.getBugReporter().FlushReports();
7257330f729Sjoerg if (BugReporterTimer)
7267330f729Sjoerg BugReporterTimer->stopTimer();
7277330f729Sjoerg }
7287330f729Sjoerg
7297330f729Sjoerg //===----------------------------------------------------------------------===//
7307330f729Sjoerg // AnalysisConsumer creation.
7317330f729Sjoerg //===----------------------------------------------------------------------===//
7327330f729Sjoerg
7337330f729Sjoerg std::unique_ptr<AnalysisASTConsumer>
CreateAnalysisConsumer(CompilerInstance & CI)7347330f729Sjoerg ento::CreateAnalysisConsumer(CompilerInstance &CI) {
7357330f729Sjoerg // Disable the effects of '-Werror' when using the AnalysisConsumer.
7367330f729Sjoerg CI.getPreprocessor().getDiagnostics().setWarningsAsErrors(false);
7377330f729Sjoerg
7387330f729Sjoerg AnalyzerOptionsRef analyzerOpts = CI.getAnalyzerOpts();
7397330f729Sjoerg bool hasModelPath = analyzerOpts->Config.count("model-path") > 0;
7407330f729Sjoerg
7417330f729Sjoerg return std::make_unique<AnalysisConsumer>(
7427330f729Sjoerg CI, CI.getFrontendOpts().OutputFile, analyzerOpts,
7437330f729Sjoerg CI.getFrontendOpts().Plugins,
7447330f729Sjoerg hasModelPath ? new ModelInjector(CI) : nullptr);
7457330f729Sjoerg }
746