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/Lex/Preprocessor.h" 20 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h" 21 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.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 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: 47 AnalyzerOptions &options; 48 49 AnalysisManager(ASTContext &ctx, Preprocessor &PP, 50 PathDiagnosticConsumers Consumers, 51 StoreManagerCreator storemgr, 52 ConstraintManagerCreator constraintmgr, 53 CheckerManager *checkerMgr, AnalyzerOptions &Options, 54 std::unique_ptr<CodeInjector> injector = nullptr); 55 56 ~AnalysisManager() override; 57 ClearContexts()58 void ClearContexts() { 59 AnaCtxMgr.clear(); 60 } 61 getAnalysisDeclContextManager()62 AnalysisDeclContextManager& getAnalysisDeclContextManager() { 63 return AnaCtxMgr; 64 } 65 getPreprocessor()66 Preprocessor &getPreprocessor() override { return PP; } 67 getStoreManagerCreator()68 StoreManagerCreator getStoreManagerCreator() { 69 return CreateStoreMgr; 70 } 71 getAnalyzerOptions()72 AnalyzerOptions& getAnalyzerOptions() override { 73 return options; 74 } 75 getConstraintManagerCreator()76 ConstraintManagerCreator getConstraintManagerCreator() { 77 return CreateConstraintMgr; 78 } 79 getCheckerManager()80 CheckerManager *getCheckerManager() const { return CheckerMgr; } 81 getASTContext()82 ASTContext &getASTContext() override { 83 return Ctx; 84 } 85 getSourceManager()86 SourceManager &getSourceManager() override { 87 return getASTContext().getSourceManager(); 88 } 89 getLangOpts()90 const LangOptions &getLangOpts() const { 91 return LangOpts; 92 } 93 94 ArrayRef<std::unique_ptr<PathDiagnosticConsumer>> getPathDiagnosticConsumers()95 getPathDiagnosticConsumers() override { 96 return PathConsumers; 97 } 98 99 void FlushDiagnostics(); 100 shouldVisualize()101 bool shouldVisualize() const { 102 return options.visualizeExplodedGraphWithGraphViz; 103 } 104 shouldInlineCall()105 bool shouldInlineCall() const { 106 return options.getIPAMode() != IPAK_None; 107 } 108 getCFG(Decl const * D)109 CFG *getCFG(Decl const *D) { 110 return AnaCtxMgr.getContext(D)->getCFG(); 111 } 112 113 template <typename T> getAnalysis(Decl const * D)114 T *getAnalysis(Decl const *D) { 115 return AnaCtxMgr.getContext(D)->getAnalysis<T>(); 116 } 117 getParentMap(Decl const * D)118 ParentMap &getParentMap(Decl const *D) { 119 return AnaCtxMgr.getContext(D)->getParentMap(); 120 } 121 getAnalysisDeclContext(const Decl * D)122 AnalysisDeclContext *getAnalysisDeclContext(const Decl *D) { 123 return AnaCtxMgr.getContext(D); 124 } 125 isInCodeFile(SourceLocation SL,const SourceManager & SM)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 isInCodeFile(SourceLocation SL)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