clang 8.0.0

AnalysisManager.h
Go to the documentation of this file.
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
58 void ClearContexts() {
59 AnaCtxMgr.clear();
60 }
61
62 AnalysisDeclContextManager& getAnalysisDeclContextManager() {
63 return AnaCtxMgr;
64 }
65
66 StoreManagerCreator getStoreManagerCreator() {
67 return CreateStoreMgr;
68 }
69
70 AnalyzerOptions& getAnalyzerOptions() override {
71 return options;
72 }
73
74 ConstraintManagerCreator getConstraintManagerCreator() {
75 return CreateConstraintMgr;
76 }
77
78 CheckerManager *getCheckerManager() const { return CheckerMgr; }
79
80 ASTContext &getASTContext() override {
81 return Ctx;
82 }
83
84 SourceManager &getSourceManager() override {
85 return getASTContext().getSourceManager();
86 }
87
88 DiagnosticsEngine &getDiagnostic() override {
89 return Diags;
90 }
91
92 const LangOptions &getLangOpts() const {
93 return LangOpts;
94 }
95
96 ArrayRef<PathDiagnosticConsumer*> getPathDiagnosticConsumers() override {
97 return PathConsumers;
98 }
99
100 void FlushDiagnostics();
101
102 bool shouldVisualize() const {
103 return options.visualizeExplodedGraphWithGraphViz;
104 }
105
106 bool shouldInlineCall() const {
107 return options.getIPAMode() != IPAK_None;
108 }
109
110 CFG *getCFG(Decl const *D) {
111 return AnaCtxMgr.getContext(D)->getCFG();
112 }
113
114 template <typename T>
115 T *getAnalysis(Decl const *D) {
116 return AnaCtxMgr.getContext(D)->getAnalysis<T>();
117 }
118
119 ParentMap &getParentMap(Decl const *D) {
120 return AnaCtxMgr.getContext(D)->getParentMap();
121 }
122
123 AnalysisDeclContext *getAnalysisDeclContext(const Decl *D) {
124 return AnaCtxMgr.getContext(D);
125 }
126
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
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
clang::ento::AnalysisManager::getAnalysisDeclContextManager
AnalysisDeclContextManager & getAnalysisDeclContextManager()
Definition: AnalysisManager.h:62
ento
clang::ento::AnalysisManager::FlushDiagnostics
void FlushDiagnostics()
Definition: AnalysisManager.cpp:55
clang::ParentMap
Definition: ParentMap.h:21
clang::ento::AnalysisManager::getCheckerManager
CheckerManager * getCheckerManager() const
Definition: AnalysisManager.h:78
PathDiagnosticConsumers.h
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:87
clang::ento::AnalysisManager::~AnalysisManager
~AnalysisManager() override
Definition: AnalysisManager.cpp:47
clang::AnalyzerOptions::visualizeExplodedGraphWithGraphViz
unsigned visualizeExplodedGraphWithGraphViz
Definition: AnalyzerOptions.h:211
clang::IPAK_None
Perform only intra-procedural analysis.
Definition: AnalyzerOptions.h:107
clang::ento::CheckerManager
Definition: CheckerManager.h:117
AnalyzerOptions.h
clang::ento::AnalysisManager::AnalysisManager
AnalysisManager(ASTContext &ctx, DiagnosticsEngine &diags, const PathDiagnosticConsumers &Consumers, StoreManagerCreator storemgr, ConstraintManagerCreator constraintmgr, CheckerManager *checkerMgr, AnalyzerOptions &Options, CodeInjector *injector=nullptr)
Definition: AnalysisManager.cpp:17
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:155
clang::AnalysisDeclContext
AnalysisDeclContext contains the context data for the function or method under analysis.
Definition: AnalysisDeclContext.h:70
clang::ento::StoreManagerCreator
std::unique_ptr< StoreManager >(* StoreManagerCreator)(ProgramStateManager &)
Definition: ProgramState.h:45
clang::ento::AnalysisManager::isInCodeFile
static bool isInCodeFile(SourceLocation SL, const SourceManager &SM)
Definition: AnalysisManager.h:127
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:50
clang::ento::AnalysisManager::ClearContexts
void ClearContexts()
Definition: AnalysisManager.h:58
clang::AnalysisDeclContext::getAnalysis
T * getAnalysis()
Return the specified analysis object, lazily running the analysis if necessary.
Definition: AnalysisDeclContext.h:199
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:149
clang::ento::AnalysisManager::getPathDiagnosticConsumers
ArrayRef< PathDiagnosticConsumer * > getPathDiagnosticConsumers() override
Definition: AnalysisManager.h:96
clang::ento::AnalysisManager::shouldVisualize
bool shouldVisualize() const
Definition: AnalysisManager.h:102
llvm::ArrayRef
Definition: LLVM.h:32
clang::ento::AnalysisManager::getASTContext
ASTContext & getASTContext() override
Definition: AnalysisManager.h:80
clang::ento::AnalysisManager::getAnalysisDeclContext
AnalysisDeclContext * getAnalysisDeclContext(const Decl *D)
Definition: AnalysisManager.h:123
clang::ento::AnalysisManager
Definition: AnalysisManager.h:31
clang::ento::AnalysisManager::getStoreManagerCreator
StoreManagerCreator getStoreManagerCreator()
Definition: AnalysisManager.h:66
clang::ento::ConstraintManagerCreator
std::unique_ptr< ConstraintManager >(* ConstraintManagerCreator)(ProgramStateManager &, SubEngine *)
Definition: ProgramState.h:43
BugReporter.h
clang::AnalysisDeclContextManager::getContext
AnalysisDeclContext * getContext(const Decl *D)
Definition: AnalysisDeclContext.cpp:294
clang::CFG
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt...
Definition: CFG.h:1003
clang::ento::AnalysisManager::getAnalyzerOptions
AnalyzerOptions & getAnalyzerOptions() override
Definition: AnalysisManager.h:70
clang::ento::AnalysisManager::getConstraintManagerCreator
ConstraintManagerCreator getConstraintManagerCreator()
Definition: AnalysisManager.h:74
PathDiagnostic.h
clang::AnalysisDeclContext::getCFG
CFG * getCFG()
Definition: AnalysisDeclContext.cpp:212
clang::ento::AnalysisManager::getCFG
CFG * getCFG(Decl const *D)
Definition: AnalysisManager.h:110
SM
const SourceManager & SM
Definition: Format.cpp:1490
AnalysisDeclContext.h
clang::AnalyzerOptions::getIPAMode
IPAKind getIPAMode() const
Returns the inter-procedural analysis mode.
Definition: AnalyzerOptions.cpp:72
clang::SourceManager::getFilename
StringRef getFilename(SourceLocation SpellingLoc) const
Return the filename of the file containing a SourceLocation.
Definition: SourceManager.h:1059
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::ento::PathDiagnosticConsumers
std::vector< PathDiagnosticConsumer * > PathDiagnosticConsumers
Definition: PathDiagnosticConsumers.h:27
clang::ento::AnalysisManager::getParentMap
ParentMap & getParentMap(Decl const *D)
Definition: AnalysisManager.h:119
clang::SourceManager::isInMainFile
bool isInMainFile(SourceLocation Loc) const
Returns whether the PresumedLoc for a given SourceLocation is in the main file.
Definition: SourceManager.cpp:1485
clang::ento::AnalysisManager::options
AnalyzerOptions & options
Definition: AnalysisManager.h:47
clang::ento::AnalysisManager::isInCodeFile
bool isInCodeFile(SourceLocation SL)
Definition: AnalysisManager.h:152
clang::AnalysisDeclContextManager
Definition: AnalysisDeclContext.h:430
clang::ento::AnalysisManager::getSourceManager
SourceManager & getSourceManager() override
Definition: AnalysisManager.h:84
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::SourceLocation::isValid
bool isValid() const
Return true if this is a valid SourceLocation object.
Definition: SourceLocation.h:108
clang::CodeInjector
CodeInjector is an interface which is responsible for injecting AST of function definitions that may ...
Definition: CodeInjector.h:36
clang::SourceManager::getIncludeLoc
SourceLocation getIncludeLoc(FileID FID) const
Returns the include location if FID is a #include&#39;d file otherwise it returns an invalid location...
Definition: SourceManager.h:1091
clang::ento::AnalysisManager::getLangOpts
const LangOptions & getLangOpts() const
Definition: AnalysisManager.h:92
clang::SourceManager::getFileID
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
Definition: SourceManager.h:1048
clang::ento::BugReporterData
Definition: BugReporter.h:396
clang::AnalyzerOptions
Stores options for the analyzer from the command line.
Definition: AnalyzerOptions.h:163
clang::ASTContext::getSourceManager
SourceManager & getSourceManager()
Definition: ASTContext.h:662
clang::ento::AnalysisManager::getDiagnostic
DiagnosticsEngine & getDiagnostic() override
Definition: AnalysisManager.h:88
clang::AnalysisDeclContext::getParentMap
ParentMap & getParentMap()
Definition: AnalysisDeclContext.cpp:278
clang::AnalysisDeclContextManager::clear
void clear()
Discard all previously created AnalysisDeclContexts.
Definition: AnalysisDeclContext.cpp:90
clang::ento::AnalysisManager::getAnalysis
T * getAnalysis(Decl const *D)
Definition: AnalysisManager.h:115
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:582
clang::ento::AnalysisManager::shouldInlineCall
bool shouldInlineCall() const
Definition: AnalysisManager.h:106