From 38ab3b876baaa899b92dda9113a4d1d4b56c2e79 Mon Sep 17 00:00:00 2001 From: Charusso Date: Thu, 30 Jan 2020 17:00:49 +0100 Subject: [PATCH] [analyzer] CheckerContext: Make the Preprocessor available Summary: This patch hooks the `Preprocessor` trough `BugReporter` to the `CheckerContext` so the checkers could look for macro definitions. Reviewed By: NoQ Differential Revision: https://reviews.llvm.org/D69731 --- .../clang/StaticAnalyzer/Core/BugReporter/BugReporter.h | 6 +++++- .../StaticAnalyzer/Core/PathSensitive/AnalysisManager.h | 6 +++++- .../StaticAnalyzer/Core/PathSensitive/CheckerContext.h | 2 ++ clang/lib/StaticAnalyzer/Core/AnalysisManager.cpp | 4 ++-- clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp | 6 +++--- clang/unittests/StaticAnalyzer/Reusables.h | 2 +- 6 files changed, 18 insertions(+), 8 deletions(-) diff --git a/clang/include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h b/clang/include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h index 69593e2b6c93..1b39cdf12c80 100644 --- a/clang/include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h +++ b/clang/include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h @@ -17,12 +17,13 @@ #include "clang/Analysis/PathDiagnostic.h" #include "clang/Basic/LLVM.h" #include "clang/Basic/SourceLocation.h" +#include "clang/Lex/Preprocessor.h" #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h" #include "clang/StaticAnalyzer/Core/CheckerManager.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h" #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" #include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h" -#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/FoldingSet.h" @@ -566,6 +567,7 @@ public: virtual ASTContext &getASTContext() = 0; virtual SourceManager &getSourceManager() = 0; virtual AnalyzerOptions &getAnalyzerOptions() = 0; + virtual Preprocessor &getPreprocessor() = 0; }; /// BugReporter is a utility class for generating PathDiagnostics for analysis. @@ -608,6 +610,8 @@ public: const AnalyzerOptions &getAnalyzerOptions() { return D.getAnalyzerOptions(); } + Preprocessor &getPreprocessor() { return D.getPreprocessor(); } + /// Add the given report to the set of reports tracked by BugReporter. /// /// The reports are usually generated by the checkers. Further, they are diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h index d605a6a667f6..c76e9c0326af 100644 --- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h +++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h @@ -16,6 +16,7 @@ #include "clang/Analysis/AnalysisDeclContext.h" #include "clang/Analysis/PathDiagnostic.h" +#include "clang/Lex/Preprocessor.h" #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h" #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h" @@ -32,6 +33,7 @@ class AnalysisManager : public BugReporterData { AnalysisDeclContextManager AnaCtxMgr; ASTContext &Ctx; + Preprocessor &PP; const LangOptions &LangOpts; PathDiagnosticConsumers PathConsumers; @@ -44,7 +46,7 @@ class AnalysisManager : public BugReporterData { public: AnalyzerOptions &options; - AnalysisManager(ASTContext &ctx, + AnalysisManager(ASTContext &ctx, Preprocessor &PP, const PathDiagnosticConsumers &Consumers, StoreManagerCreator storemgr, ConstraintManagerCreator constraintmgr, @@ -61,6 +63,8 @@ public: return AnaCtxMgr; } + Preprocessor &getPreprocessor() override { return PP; } + StoreManagerCreator getStoreManagerCreator() { return CreateStoreMgr; } diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h index 286b5b2b2b07..aee26db95fd1 100644 --- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h +++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h @@ -107,6 +107,8 @@ public: return getBugReporter().getSourceManager(); } + Preprocessor &getPreprocessor() { return getBugReporter().getPreprocessor(); } + SValBuilder &getSValBuilder() { return Eng.getSValBuilder(); } diff --git a/clang/lib/StaticAnalyzer/Core/AnalysisManager.cpp b/clang/lib/StaticAnalyzer/Core/AnalysisManager.cpp index fdd03c75920d..ecfc7106560e 100644 --- a/clang/lib/StaticAnalyzer/Core/AnalysisManager.cpp +++ b/clang/lib/StaticAnalyzer/Core/AnalysisManager.cpp @@ -13,7 +13,7 @@ using namespace ento; void AnalysisManager::anchor() { } -AnalysisManager::AnalysisManager(ASTContext &ASTCtx, +AnalysisManager::AnalysisManager(ASTContext &ASTCtx, Preprocessor &PP, const PathDiagnosticConsumers &PDC, StoreManagerCreator storemgr, ConstraintManagerCreator constraintmgr, @@ -38,7 +38,7 @@ AnalysisManager::AnalysisManager(ASTContext &ASTCtx, Options.ShouldElideConstructors, /*addVirtualBaseBranches=*/true, injector), - Ctx(ASTCtx), LangOpts(ASTCtx.getLangOpts()), + Ctx(ASTCtx), PP(PP), LangOpts(ASTCtx.getLangOpts()), PathConsumers(PDC), CreateStoreMgr(storemgr), CreateConstraintMgr(constraintmgr), CheckerMgr(checkerMgr), options(Options) { diff --git a/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp b/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp index 9f69e53e9e51..28a3ab2ba188 100644 --- a/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp +++ b/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp @@ -193,7 +193,7 @@ class AnalysisConsumer : public AnalysisASTConsumer, public: ASTContext *Ctx; - const Preprocessor &PP; + Preprocessor &PP; const std::string OutDir; AnalyzerOptionsRef Opts; ArrayRef Plugins; @@ -336,8 +336,8 @@ public: checkerMgr = createCheckerManager( *Ctx, *Opts, Plugins, CheckerRegistrationFns, PP.getDiagnostics()); - Mgr = std::make_unique(*Ctx, PathConsumers, CreateStoreMgr, - CreateConstraintMgr, + Mgr = std::make_unique(*Ctx, PP, PathConsumers, + CreateStoreMgr, CreateConstraintMgr, checkerMgr.get(), *Opts, Injector); } diff --git a/clang/unittests/StaticAnalyzer/Reusables.h b/clang/unittests/StaticAnalyzer/Reusables.h index bac2808369c2..1346adbac90a 100644 --- a/clang/unittests/StaticAnalyzer/Reusables.h +++ b/clang/unittests/StaticAnalyzer/Reusables.h @@ -58,7 +58,7 @@ public: ExprEngineConsumer(CompilerInstance &C) : C(C), ChkMgr(C.getASTContext(), *C.getAnalyzerOpts()), CTU(C), Consumers(), - AMgr(C.getASTContext(), Consumers, + AMgr(C.getASTContext(), C.getPreprocessor(), Consumers, CreateRegionStoreManager, CreateRangeConstraintManager, &ChkMgr, *C.getAnalyzerOpts()), VisitedCallees(), FS(),