clang  14.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 
19 #include "clang/Lex/Preprocessor.h"
23 
24 namespace clang {
25 
26 class CodeInjector;
27 
28 namespace ento {
29  class CheckerManager;
30 
32  virtual void anchor();
34 
35  ASTContext &Ctx;
36  Preprocessor &PP;
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:
48 
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 
63  return AnaCtxMgr;
64  }
65 
66  Preprocessor &getPreprocessor() override { return PP; }
67 
69  return CreateStoreMgr;
70  }
71 
73  return options;
74  }
75 
77  return CreateConstraintMgr;
78  }
79 
80  CheckerManager *getCheckerManager() const { return CheckerMgr; }
81 
82  ASTContext &getASTContext() override {
83  return Ctx;
84  }
85 
88  }
89 
90  const LangOptions &getLangOpts() const {
91  return LangOpts;
92  }
93 
95  return PathConsumers;
96  }
97 
98  void FlushDiagnostics();
99 
100  bool shouldVisualize() const {
102  }
103 
104  bool shouldInlineCall() const {
105  return options.getIPAMode() != IPAK_None;
106  }
107 
108  CFG *getCFG(Decl const *D) {
109  return AnaCtxMgr.getContext(D)->getCFG();
110  }
111 
112  template <typename T>
113  T *getAnalysis(Decl const *D) {
114  return AnaCtxMgr.getContext(D)->getAnalysis<T>();
115  }
116 
118  return AnaCtxMgr.getContext(D)->getParentMap();
119  }
120 
122  return AnaCtxMgr.getContext(D);
123  }
124 
125  static bool isInCodeFile(SourceLocation SL, const SourceManager &SM) {
126  if (SM.isInMainFile(SL))
127  return true;
128 
129  // Support the "unified sources" compilation method (eg. WebKit) that
130  // involves producing non-header files that include other non-header files.
131  // We should be included directly from a UnifiedSource* file
132  // and we shouldn't be a header - which is a very safe defensive check.
133  SourceLocation IL = SM.getIncludeLoc(SM.getFileID(SL));
134  if (!IL.isValid() || !SM.isInMainFile(IL))
135  return false;
136  // Should rather be "file name starts with", but the current .getFilename
137  // includes the full path.
138  if (SM.getFilename(IL).contains("UnifiedSource")) {
139  // It might be great to reuse FrontendOptions::getInputKindForExtension()
140  // but for now it doesn't discriminate between code and header files.
141  return llvm::StringSwitch<bool>(SM.getFilename(SL).rsplit('.').second)
142  .Cases("c", "m", "mm", "C", "cc", "cp", true)
143  .Cases("cpp", "CPP", "c++", "cxx", "cppm", true)
144  .Default(false);
145  }
146 
147  return false;
148  }
149 
152  return isInCodeFile(SL, SM);
153  }
154 };
155 
156 } // enAnaCtxMgrspace
157 
158 } // end clang namespace
159 
160 #endif
AnalyzerOptions.h
clang::ento::AnalysisManager::getCheckerManager
CheckerManager * getCheckerManager() const
Definition: AnalysisManager.h:80
clang::ento::BugReporterData
Definition: BugReporter.h:571
clang::ento::AnalysisManager::FlushDiagnostics
void FlushDiagnostics()
Definition: AnalysisManager.cpp:59
AnalysisDeclContext.h
clang::ento::StoreManagerCreator
std::unique_ptr< StoreManager >(* StoreManagerCreator)(ProgramStateManager &)
Definition: ProgramState.h:43
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
PathDiagnosticConsumers.h
clang::ento::AnalysisManager::getCFG
CFG * getCFG(Decl const *D)
Definition: AnalysisManager.h:108
clang::AnalysisDeclContext
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Definition: AnalysisDeclContext.h:72
clang::ento::AnalysisManager::getPreprocessor
Preprocessor & getPreprocessor() override
Definition: AnalysisManager.h:66
clang::CFG
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition: CFG.h:1225
clang::ASTContext::getSourceManager
SourceManager & getSourceManager()
Definition: ASTContext.h:695
clang::ento::AnalysisManager::getAnalysisDeclContext
AnalysisDeclContext * getAnalysisDeclContext(const Decl *D)
Definition: AnalysisManager.h:121
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:626
clang::ento::AnalysisManager::getStoreManagerCreator
StoreManagerCreator getStoreManagerCreator()
Definition: AnalysisManager.h:68
Preprocessor.h
clang::ento::AnalysisManager::getAnalyzerOptions
AnalyzerOptions & getAnalyzerOptions() override
Definition: AnalysisManager.h:72
clang::AnalysisDeclContext::getParentMap
ParentMap & getParentMap()
Definition: AnalysisDeclContext.cpp:279
clang::AnalysisDeclContext::getAnalysis
T * getAnalysis()
Definition: AnalysisDeclContext.h:192
clang::ento::AnalysisManager::isInCodeFile
static bool isInCodeFile(SourceLocation SL, const SourceManager &SM)
Definition: AnalysisManager.h:125
clang::ento::AnalysisManager::getLangOpts
const LangOptions & getLangOpts() const
Definition: AnalysisManager.h:90
BugReporter.h
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:212
clang::ento::AnalysisManager::ClearContexts
void ClearContexts()
Definition: AnalysisManager.h:58
clang::ento::AnalysisManager::getPathDiagnosticConsumers
ArrayRef< PathDiagnosticConsumer * > getPathDiagnosticConsumers() override
Definition: AnalysisManager.h:94
PathDiagnostic.h
clang::ento::AnalysisManager::getParentMap
ParentMap & getParentMap(Decl const *D)
Definition: AnalysisManager.h:117
clang::AnalysisDeclContextManager::getContext
AnalysisDeclContext * getContext(const Decl *D)
Definition: AnalysisDeclContext.cpp:295
clang::ento::AnalysisManager::shouldInlineCall
bool shouldInlineCall() const
Definition: AnalysisManager.h:104
clang::IPAK_None
@ IPAK_None
Perform only intra-procedural analysis.
Definition: AnalyzerOptions.h:107
clang::AnalyzerOptions::getIPAMode
IPAKind getIPAMode() const
Returns the inter-procedural analysis mode.
Definition: AnalyzerOptions.cpp:84
clang::ParentMap
Definition: ParentMap.h:20
clang::ento::AnalysisManager::AnalysisManager
AnalysisManager(ASTContext &ctx, Preprocessor &PP, const PathDiagnosticConsumers &Consumers, StoreManagerCreator storemgr, ConstraintManagerCreator constraintmgr, CheckerManager *checkerMgr, AnalyzerOptions &Options, CodeInjector *injector=nullptr)
Definition: AnalysisManager.cpp:16
clang::ento::AnalysisManager::getConstraintManagerCreator
ConstraintManagerCreator getConstraintManagerCreator()
Definition: AnalysisManager.h:76
clang::ento::AnalysisManager::options
AnalyzerOptions & options
Definition: AnalysisManager.h:47
clang::ento::AnalysisManager::isInCodeFile
bool isInCodeFile(SourceLocation SL)
Definition: AnalysisManager.h:150
clang::ento::AnalysisManager::getASTContext
ASTContext & getASTContext() override
Definition: AnalysisManager.h:82
clang::AnalysisDeclContextManager::clear
void clear()
Discard all previously created AnalysisDeclContexts.
Definition: AnalysisDeclContext.cpp:91
clang::ento::AnalysisManager::getAnalysisDeclContextManager
AnalysisDeclContextManager & getAnalysisDeclContextManager()
Definition: AnalysisManager.h:62
llvm::ArrayRef
Definition: LLVM.h:34
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
clang::AnalyzerOptions
Stores options for the analyzer from the command line.
Definition: AnalyzerOptions.h:163
clang::ento::AnalysisManager
Definition: AnalysisManager.h:31
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:58
clang::ento::CheckerManager
Definition: CheckerManager.h:127
clang::ento::PathDiagnosticConsumers
std::vector< PathDiagnosticConsumer * > PathDiagnosticConsumers
Definition: PathDiagnosticConsumers.h:33
clang
Definition: CalledOnceCheck.h:17
clang::ento::AnalysisManager::getAnalysis
T * getAnalysis(Decl const *D)
Definition: AnalysisManager.h:113
clang::SourceLocation::isValid
bool isValid() const
Return true if this is a valid SourceLocation object.
Definition: SourceLocation.h:112
clang::ento::AnalysisManager::shouldVisualize
bool shouldVisualize() const
Definition: AnalysisManager.h:100
clang::AnalysisDeclContextManager
Definition: AnalysisDeclContext.h:421
clang::ento::AnalysisManager::getSourceManager
SourceManager & getSourceManager() override
Definition: AnalysisManager.h:86
clang::CodeInjector
CodeInjector is an interface which is responsible for injecting AST of function definitions that may ...
Definition: CodeInjector.h:35
clang::AnalysisDeclContext::getCFG
CFG * getCFG()
Definition: AnalysisDeclContext.cpp:213
clang::AnalyzerOptions::visualizeExplodedGraphWithGraphViz
unsigned visualizeExplodedGraphWithGraphViz
Definition: AnalyzerOptions.h:250
clang::ento::ConstraintManagerCreator
std::unique_ptr< ConstraintManager >(* ConstraintManagerCreator)(ProgramStateManager &, ExprEngine *)
Definition: ProgramState.h:41
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:130
SM
#define SM(sm)
Definition: Cuda.cpp:78
clang::ento::AnalysisManager::~AnalysisManager
~AnalysisManager() override
Definition: AnalysisManager.cpp:51