clang  9.0.0svn
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 
22 
23 namespace clang {
24 
25 class CodeInjector;
26 
27 namespace ento {
28  class CheckerManager;
29 
31  virtual void anchor();
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 
45 public:
47 
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 
62  return AnaCtxMgr;
63  }
64 
66  return CreateStoreMgr;
67  }
68 
70  return options;
71  }
72 
74  return CreateConstraintMgr;
75  }
76 
77  CheckerManager *getCheckerManager() const { return CheckerMgr; }
78 
79  ASTContext &getASTContext() override {
80  return Ctx;
81  }
82 
85  }
86 
88  return Diags;
89  }
90 
91  const LangOptions &getLangOpts() const {
92  return LangOpts;
93  }
94 
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 
119  return AnaCtxMgr.getContext(D)->getParentMap();
120  }
121 
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 
153  return isInCodeFile(SL, SM);
154  }
155 };
156 
157 } // enAnaCtxMgrspace
158 
159 } // end clang namespace
160 
161 #endif
AnalysisDeclContextManager & getAnalysisDeclContextManager()
CheckerManager * getCheckerManager() const
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
unsigned visualizeExplodedGraphWithGraphViz
Perform only intra-procedural analysis.
AnalysisManager(ASTContext &ctx, DiagnosticsEngine &diags, const PathDiagnosticConsumers &Consumers, StoreManagerCreator storemgr, ConstraintManagerCreator constraintmgr, CheckerManager *checkerMgr, AnalyzerOptions &Options, CodeInjector *injector=nullptr)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
AnalysisDeclContext contains the context data for the function or method under analysis.
std::unique_ptr< StoreManager >(* StoreManagerCreator)(ProgramStateManager &)
Definition: ProgramState.h:44
static bool isInCodeFile(SourceLocation SL, const SourceManager &SM)
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:49
T * getAnalysis()
Return the specified analysis object, lazily running the analysis if necessary.
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:148
ArrayRef< PathDiagnosticConsumer * > getPathDiagnosticConsumers() override
ASTContext & getASTContext() override
AnalysisDeclContext * getAnalysisDeclContext(const Decl *D)
StoreManagerCreator getStoreManagerCreator()
std::unique_ptr< ConstraintManager >(* ConstraintManagerCreator)(ProgramStateManager &, SubEngine *)
Definition: ProgramState.h:42
AnalysisDeclContext * getContext(const Decl *D)
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt...
Definition: CFG.h:1002
AnalyzerOptions & getAnalyzerOptions() override
ConstraintManagerCreator getConstraintManagerCreator()
CFG * getCFG(Decl const *D)
const SourceManager & SM
Definition: Format.cpp:1489
IPAKind getIPAMode() const
Returns the inter-procedural analysis mode.
StringRef getFilename(SourceLocation SpellingLoc) const
Return the filename of the file containing a SourceLocation.
Encodes a location in the source.
std::vector< PathDiagnosticConsumer * > PathDiagnosticConsumers
ParentMap & getParentMap(Decl const *D)
bool isInMainFile(SourceLocation Loc) const
Returns whether the PresumedLoc for a given SourceLocation is in the main file.
bool isInCodeFile(SourceLocation SL)
SourceManager & getSourceManager() override
Dataflow Directional Tag Classes.
bool isValid() const
Return true if this is a valid SourceLocation object.
CodeInjector is an interface which is responsible for injecting AST of function definitions that may ...
Definition: CodeInjector.h:35
SourceLocation getIncludeLoc(FileID FID) const
Returns the include location if FID is a #include&#39;d file otherwise it returns an invalid location...
const LangOptions & getLangOpts() const
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
Stores options for the analyzer from the command line.
SourceManager & getSourceManager()
Definition: ASTContext.h:661
DiagnosticsEngine & getDiagnostic() override
void clear()
Discard all previously created AnalysisDeclContexts.
T * getAnalysis(Decl const *D)
This class handles loading and caching of source files into memory.