1 //== AnalysisManager.h - Path sensitive analysis data manager ------*- C++ -*-//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the AnalysisManager class that manages the data and policy
11 // for path sensitive analysis.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_ANALYSISMANAGER_H
16 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_ANALYSISMANAGER_H
17 
18 #include "clang/Analysis/AnalysisDeclContext.h"
19 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
20 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
21 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
22 #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
23 
24 namespace clang {
25 
26 class CodeInjector;
27 
28 namespace ento {
29   class CheckerManager;
30 
31 class AnalysisManager : public BugReporterData {
32   virtual void anchor();
33   AnalysisDeclContextManager AnaCtxMgr;
34 
35   ASTContext &Ctx;
36   DiagnosticsEngine &Diags;
37   const LangOptions &LangOpts;
38   PathDiagnosticConsumers PathConsumers;
39 
40   // Configurable components creators.
41   StoreManagerCreator CreateStoreMgr;
42   ConstraintManagerCreator CreateConstraintMgr;
43 
44   CheckerManager *CheckerMgr;
45 
46 public:
47   AnalyzerOptions &options;
48 
49   AnalysisManager(ASTContext &ctx, DiagnosticsEngine &diags,
50                   const PathDiagnosticConsumers &Consumers,
51                   StoreManagerCreator storemgr,
52                   ConstraintManagerCreator constraintmgr,
53                   CheckerManager *checkerMgr, AnalyzerOptions &Options,
54                   CodeInjector *injector = nullptr);
55 
56   ~AnalysisManager() override;
57 
ClearContexts()58   void ClearContexts() {
59     AnaCtxMgr.clear();
60   }
61 
getAnalysisDeclContextManager()62   AnalysisDeclContextManager& getAnalysisDeclContextManager() {
63     return AnaCtxMgr;
64   }
65 
getStoreManagerCreator()66   StoreManagerCreator getStoreManagerCreator() {
67     return CreateStoreMgr;
68   }
69 
getAnalyzerOptions()70   AnalyzerOptions& getAnalyzerOptions() override {
71     return options;
72   }
73 
getConstraintManagerCreator()74   ConstraintManagerCreator getConstraintManagerCreator() {
75     return CreateConstraintMgr;
76   }
77 
getCheckerManager()78   CheckerManager *getCheckerManager() const { return CheckerMgr; }
79 
getASTContext()80   ASTContext &getASTContext() override {
81     return Ctx;
82   }
83 
getSourceManager()84   SourceManager &getSourceManager() override {
85     return getASTContext().getSourceManager();
86   }
87 
getDiagnostic()88   DiagnosticsEngine &getDiagnostic() override {
89     return Diags;
90   }
91 
getLangOpts()92   const LangOptions &getLangOpts() const {
93     return LangOpts;
94   }
95 
getPathDiagnosticConsumers()96   ArrayRef<PathDiagnosticConsumer*> getPathDiagnosticConsumers() override {
97     return PathConsumers;
98   }
99 
100   void FlushDiagnostics();
101 
shouldVisualize()102   bool shouldVisualize() const {
103     return options.visualizeExplodedGraphWithGraphViz;
104   }
105 
shouldInlineCall()106   bool shouldInlineCall() const {
107     return options.getIPAMode() != IPAK_None;
108   }
109 
getCFG(Decl const * D)110   CFG *getCFG(Decl const *D) {
111     return AnaCtxMgr.getContext(D)->getCFG();
112   }
113 
114   template <typename T>
getAnalysis(Decl const * D)115   T *getAnalysis(Decl const *D) {
116     return AnaCtxMgr.getContext(D)->getAnalysis<T>();
117   }
118 
getParentMap(Decl const * D)119   ParentMap &getParentMap(Decl const *D) {
120     return AnaCtxMgr.getContext(D)->getParentMap();
121   }
122 
getAnalysisDeclContext(const Decl * D)123   AnalysisDeclContext *getAnalysisDeclContext(const Decl *D) {
124     return AnaCtxMgr.getContext(D);
125   }
126 
isInCodeFile(SourceLocation SL,const SourceManager & SM)127   static bool isInCodeFile(SourceLocation SL, const SourceManager &SM) {
128     if (SM.isInMainFile(SL))
129       return true;
130 
131     // Support the "unified sources" compilation method (eg. WebKit) that
132     // involves producing non-header files that include other non-header files.
133     // We should be included directly from a UnifiedSource* file
134     // and we shouldn't be a header - which is a very safe defensive check.
135     SourceLocation IL = SM.getIncludeLoc(SM.getFileID(SL));
136     if (!IL.isValid() || !SM.isInMainFile(IL))
137       return false;
138     // Should rather be "file name starts with", but the current .getFilename
139     // includes the full path.
140     if (SM.getFilename(IL).contains("UnifiedSource")) {
141       // It might be great to reuse FrontendOptions::getInputKindForExtension()
142       // but for now it doesn't discriminate between code and header files.
143       return llvm::StringSwitch<bool>(SM.getFilename(SL).rsplit('.').second)
144           .Cases("c", "m", "mm", "C", "cc", "cp", true)
145           .Cases("cpp", "CPP", "c++", "cxx", "cppm", true)
146           .Default(false);
147     }
148 
149     return false;
150   }
151 
isInCodeFile(SourceLocation SL)152   bool isInCodeFile(SourceLocation SL) {
153     const SourceManager &SM = getASTContext().getSourceManager();
154     return isInCodeFile(SL, SM);
155   }
156 };
157 
158 } // enAnaCtxMgrspace
159 
160 } // end clang namespace
161 
162 #endif
163