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