clang 10.0.0git

AnalysisManager.h
Go to the documentation of this file.
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/Analysis/PathDiagnostic.h"
19 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
20 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
21 #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
22
23 namespace clang {
24
25 class CodeInjector;
26
27 namespace ento {
28 class CheckerManager;
29
30 class AnalysisManager : public BugReporterData {
31 virtual void anchor();
32 AnalysisDeclContextManager AnaCtxMgr;
33
34 ASTContext &Ctx;
35 const LangOptions &LangOpts;
36 PathDiagnosticConsumers PathConsumers;
37
38 // Configurable components creators.
39 StoreManagerCreator CreateStoreMgr;
40 ConstraintManagerCreator CreateConstraintMgr;
41
42 CheckerManager *CheckerMgr;
43
44 public:
45 AnalyzerOptions &options;
46
47 AnalysisManager(ASTContext &ctx,
48 const PathDiagnosticConsumers &Consumers,
49 StoreManagerCreator storemgr,
50 ConstraintManagerCreator constraintmgr,
51 CheckerManager *checkerMgr, AnalyzerOptions &Options,
52 CodeInjector *injector = nullptr);
53
54 ~AnalysisManager() override;
55
56 void ClearContexts() {
57 AnaCtxMgr.clear();
58 }
59
60 AnalysisDeclContextManager& getAnalysisDeclContextManager() {
61 return AnaCtxMgr;
62 }
63
64 StoreManagerCreator getStoreManagerCreator() {
65 return CreateStoreMgr;
66 }
67
68 AnalyzerOptions& getAnalyzerOptions() override {
69 return options;
70 }
71
72 ConstraintManagerCreator getConstraintManagerCreator() {
73 return CreateConstraintMgr;
74 }
75
76 CheckerManager *getCheckerManager() const { return CheckerMgr; }
77
78 ASTContext &getASTContext() override {
79 return Ctx;
80 }
81
82 SourceManager &getSourceManager() override {
83 return getASTContext().getSourceManager();
84 }
85
86 const LangOptions &getLangOpts() const {
87 return LangOpts;
88 }
89
90 ArrayRef<PathDiagnosticConsumer*> getPathDiagnosticConsumers() override {
91 return PathConsumers;
92 }
93
94 void FlushDiagnostics();
95
96 bool shouldVisualize() const {
97 return options.visualizeExplodedGraphWithGraphViz;
98 }
99
100 bool shouldInlineCall() const {
101 return options.getIPAMode() != IPAK_None;
102 }
103
104 CFG *getCFG(Decl const *D) {
105 return AnaCtxMgr.getContext(D)->getCFG();
106 }
107
108 template <typename T>
109 T *getAnalysis(Decl const *D) {
110 return AnaCtxMgr.getContext(D)->getAnalysis<T>();
111 }
112
113 ParentMap &getParentMap(Decl const *D) {
114 return AnaCtxMgr.getContext(D)->getParentMap();
115 }
116
117 AnalysisDeclContext *getAnalysisDeclContext(const Decl *D) {
118 return AnaCtxMgr.getContext(D);
119 }
120
121 static bool isInCodeFile(SourceLocation SL, const SourceManager &SM) {
122 if (SM.isInMainFile(SL))
123 return true;
124
125 // Support the "unified sources" compilation method (eg. WebKit) that
126 // involves producing non-header files that include other non-header files.
127 // We should be included directly from a UnifiedSource* file
128 // and we shouldn't be a header - which is a very safe defensive check.
129 SourceLocation IL = SM.getIncludeLoc(SM.getFileID(SL));
130 if (!IL.isValid() || !SM.isInMainFile(IL))
131 return false;
132 // Should rather be "file name starts with", but the current .getFilename
133 // includes the full path.
134 if (SM.getFilename(IL).contains("UnifiedSource")) {
135 // It might be great to reuse FrontendOptions::getInputKindForExtension()
136 // but for now it doesn't discriminate between code and header files.
137 return llvm::StringSwitch<bool>(SM.getFilename(SL).rsplit('.').second)
138 .Cases("c", "m", "mm", "C", "cc", "cp", true)
139 .Cases("cpp", "CPP", "c++", "cxx", "cppm", true)
140 .Default(false);
141 }
142
143 return false;
144 }
145
146 bool isInCodeFile(SourceLocation SL) {
147 const SourceManager &SM = getASTContext().getSourceManager();
148 return isInCodeFile(SL, SM);
149 }
150 };
151
152 } // enAnaCtxMgrspace
153
154 } // end clang namespace
155
156 #endif
clang::ento::AnalysisManager::getAnalysisDeclContextManager
AnalysisDeclContextManager & getAnalysisDeclContextManager()
Definition: AnalysisManager.h:60
ento
clang::ento::AnalysisManager::AnalysisManager
AnalysisManager(ASTContext &ctx, const PathDiagnosticConsumers &Consumers, StoreManagerCreator storemgr, ConstraintManagerCreator constraintmgr, CheckerManager *checkerMgr, AnalyzerOptions &Options, CodeInjector *injector=nullptr)
Definition: AnalysisManager.cpp:16
clang::ento::AnalysisManager::FlushDiagnostics
void FlushDiagnostics()
Definition: AnalysisManager.cpp:59
clang::ParentMap
Definition: ParentMap.h:20
clang::ento::AnalysisManager::getCheckerManager
CheckerManager * getCheckerManager() const
Definition: AnalysisManager.h:76
PathDiagnosticConsumers.h
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
clang::ento::AnalysisManager::~AnalysisManager
~AnalysisManager() override
Definition: AnalysisManager.cpp:51
clang::AnalyzerOptions::visualizeExplodedGraphWithGraphViz
unsigned visualizeExplodedGraphWithGraphViz
Definition: AnalyzerOptions.h:249
clang::IPAK_None
Perform only intra-procedural analysis.
Definition: AnalyzerOptions.h:106
clang::ento::CheckerManager
Definition: CheckerManager.h:123
AnalyzerOptions.h
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
clang::AnalysisDeclContext
AnalysisDeclContext contains the context data for the function or method under analysis.
Definition: AnalysisDeclContext.h:69
clang::ento::StoreManagerCreator
std::unique_ptr< StoreManager >(* StoreManagerCreator)(ProgramStateManager &)
Definition: ProgramState.h:43
clang::ento::AnalysisManager::isInCodeFile
static bool isInCodeFile(SourceLocation SL, const SourceManager &SM)
Definition: AnalysisManager.h:121
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:53
clang::ento::AnalysisManager::ClearContexts
void ClearContexts()
Definition: AnalysisManager.h:56
clang::AnalysisDeclContext::getAnalysis
T * getAnalysis()
Return the specified analysis object, lazily running the analysis if necessary.
Definition: AnalysisDeclContext.h:197
clang::ento::AnalysisManager::getPathDiagnosticConsumers
ArrayRef< PathDiagnosticConsumer * > getPathDiagnosticConsumers() override
Definition: AnalysisManager.h:90
clang::ento::AnalysisManager::shouldVisualize
bool shouldVisualize() const
Definition: AnalysisManager.h:96
llvm::ArrayRef
Definition: LLVM.h:31
clang::ento::AnalysisManager::getASTContext
ASTContext & getASTContext() override
Definition: AnalysisManager.h:78
clang::ento::AnalysisManager::getAnalysisDeclContext
AnalysisDeclContext * getAnalysisDeclContext(const Decl *D)
Definition: AnalysisManager.h:117
clang::ento::AnalysisManager
Definition: AnalysisManager.h:30
clang::ento::AnalysisManager::getStoreManagerCreator
StoreManagerCreator getStoreManagerCreator()
Definition: AnalysisManager.h:64
clang::ento::ConstraintManagerCreator
std::unique_ptr< ConstraintManager >(* ConstraintManagerCreator)(ProgramStateManager &, SubEngine *)
Definition: ProgramState.h:41
BugReporter.h
clang::AnalysisDeclContextManager::getContext
AnalysisDeclContext * getContext(const Decl *D)
Definition: AnalysisDeclContext.cpp:295
clang::CFG
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt...
Definition: CFG.h:1226
clang::ento::AnalysisManager::getAnalyzerOptions
AnalyzerOptions & getAnalyzerOptions() override
Definition: AnalysisManager.h:68
clang::ento::AnalysisManager::getConstraintManagerCreator
ConstraintManagerCreator getConstraintManagerCreator()
Definition: AnalysisManager.h:72
PathDiagnostic.h
clang::AnalysisDeclContext::getCFG
CFG * getCFG()
Definition: AnalysisDeclContext.cpp:213
clang::ento::AnalysisManager::getCFG
CFG * getCFG(Decl const *D)
Definition: AnalysisManager.h:104
SM
const SourceManager & SM
Definition: Format.cpp:1685
AnalysisDeclContext.h
clang::AnalyzerOptions::getIPAMode
IPAKind getIPAMode() const
Returns the inter-procedural analysis mode.
Definition: AnalyzerOptions.cpp:84
clang::SourceManager::getFilename
StringRef getFilename(SourceLocation SpellingLoc) const
Return the filename of the file containing a SourceLocation.
Definition: SourceManager.h:1101
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::ento::PathDiagnosticConsumers
std::vector< PathDiagnosticConsumer * > PathDiagnosticConsumers
Definition: PathDiagnosticConsumers.h:29
clang::ento::AnalysisManager::getParentMap
ParentMap & getParentMap(Decl const *D)
Definition: AnalysisManager.h:113
clang::SourceManager::isInMainFile
bool isInMainFile(SourceLocation Loc) const
Returns whether the PresumedLoc for a given SourceLocation is in the main file.
Definition: SourceManager.cpp:1519
clang::ento::AnalysisManager::options
AnalyzerOptions & options
Definition: AnalysisManager.h:45
clang::ento::AnalysisManager::isInCodeFile
bool isInCodeFile(SourceLocation SL)
Definition: AnalysisManager.h:146
clang::AnalysisDeclContextManager
Definition: AnalysisDeclContext.h:439
clang::ento::AnalysisManager::getSourceManager
SourceManager & getSourceManager() override
Definition: AnalysisManager.h:82
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::SourceLocation::isValid
bool isValid() const
Return true if this is a valid SourceLocation object.
Definition: SourceLocation.h:106
clang::CodeInjector
CodeInjector is an interface which is responsible for injecting AST of function definitions that may ...
Definition: CodeInjector.h:35
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:1133
clang::ento::AnalysisManager::getLangOpts
const LangOptions & getLangOpts() const
Definition: AnalysisManager.h:86
clang::SourceManager::getFileID
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
Definition: SourceManager.h:1090
clang::ento::BugReporterData
Definition: BugReporter.h:561
clang::AnalyzerOptions
Stores options for the analyzer from the command line.
Definition: AnalyzerOptions.h:162
clang::ASTContext::getSourceManager
SourceManager & getSourceManager()
Definition: ASTContext.h:679
clang::AnalysisDeclContext::getParentMap
ParentMap & getParentMap()
Definition: AnalysisDeclContext.cpp:279
clang::AnalysisDeclContextManager::clear
void clear()
Discard all previously created AnalysisDeclContexts.
Definition: AnalysisDeclContext.cpp:91
clang::ento::AnalysisManager::getAnalysis
T * getAnalysis(Decl const *D)
Definition: AnalysisManager.h:109
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:596
clang::ento::AnalysisManager::shouldInlineCall
bool shouldInlineCall() const
Definition: AnalysisManager.h:100