Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 1 | //===--- AnalysisConsumer.cpp - ASTConsumer for running Analyses ----------===// |
| 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 | // "Meta" ASTConsumer for running different source analyses. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "ASTConsumers.h" |
Ted Kremenek | ad99dbf | 2008-11-03 22:31:48 +0000 | [diff] [blame] | 15 | #include "clang/Driver/PathDiagnosticClients.h" |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 16 | #include "clang/Driver/ManagerRegistry.h" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTConsumer.h" |
| 18 | #include "clang/AST/Decl.h" |
| 19 | #include "clang/AST/DeclObjC.h" |
| 20 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/OwningPtr.h" |
| 22 | #include "clang/AST/CFG.h" |
| 23 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 24 | #include "clang/Analysis/PathDiagnostic.h" |
| 25 | #include "clang/Basic/SourceManager.h" |
| 26 | #include "clang/Basic/FileManager.h" |
| 27 | #include "clang/AST/ParentMap.h" |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 28 | #include "clang/AST/TranslationUnit.h" |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 29 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 30 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 31 | #include "clang/Analysis/LocalCheckers.h" |
| 32 | #include "clang/Analysis/PathSensitive/GRTransferFuncs.h" |
| 33 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Zhongxing Xu | ff944a8 | 2008-12-22 01:52:37 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Streams.h" |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 36 | #include "llvm/Support/raw_ostream.h" |
| 37 | #include "llvm/System/Path.h" |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 38 | #include "llvm/System/Program.h" |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 39 | #include <vector> |
| 40 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 41 | using namespace clang; |
| 42 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 43 | static ExplodedNodeImpl::Auditor* CreateUbiViz(); |
Zhongxing Xu | ff944a8 | 2008-12-22 01:52:37 +0000 | [diff] [blame] | 44 | |
| 45 | // Analyzer options. |
| 46 | static llvm::cl::opt<bool> |
| 47 | PurgeDead("analyzer-purge-dead", |
| 48 | llvm::cl::init(true), |
| 49 | llvm::cl::desc("Remove dead symbols, bindings, and constraints before" |
| 50 | " processing a statement.")); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 51 | static llvm::cl::opt<bool> |
| 52 | UseRanges("analyzer-range-constraints", |
Ted Kremenek | f06e9c5 | 2009-02-16 17:26:11 +0000 | [diff] [blame] | 53 | llvm::cl::init(false), |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 54 | llvm::cl::desc("Use the range constraint manager instead of the basic" |
| 55 | " constraint manager")); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 56 | |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | // Basic type definitions. |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 61 | namespace { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 62 | class AnalysisManager; |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 63 | typedef void (*CodeAction)(AnalysisManager& Mgr); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 64 | } // end anonymous namespace |
| 65 | |
| 66 | //===----------------------------------------------------------------------===// |
| 67 | // AnalysisConsumer declaration. |
| 68 | //===----------------------------------------------------------------------===// |
| 69 | |
| 70 | namespace { |
| 71 | |
| 72 | class VISIBILITY_HIDDEN AnalysisConsumer : public ASTConsumer { |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 73 | typedef std::vector<CodeAction> Actions; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 74 | Actions FunctionActions; |
| 75 | Actions ObjCMethodActions; |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 76 | Actions ObjCImplementationActions; |
Ted Kremenek | daac634 | 2008-11-07 02:09:25 +0000 | [diff] [blame] | 77 | Actions TranslationUnitActions; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 78 | |
| 79 | public: |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 80 | const bool VisGraphviz; |
| 81 | const bool VisUbigraph; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 82 | const bool TrimGraph; |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 83 | const LangOptions& LOpts; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 84 | Diagnostic &Diags; |
| 85 | ASTContext* Ctx; |
| 86 | Preprocessor* PP; |
| 87 | PreprocessorFactory* PPF; |
| 88 | const std::string HTMLDir; |
| 89 | const std::string FName; |
| 90 | llvm::OwningPtr<PathDiagnosticClient> PD; |
| 91 | bool AnalyzeAll; |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 92 | AnalysisStores SM; |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 93 | AnalysisDiagClients DC; |
Ted Kremenek | 491918e | 2009-01-23 20:52:26 +0000 | [diff] [blame] | 94 | const bool DisplayProgress; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 95 | |
| 96 | AnalysisConsumer(Diagnostic &diags, Preprocessor* pp, |
| 97 | PreprocessorFactory* ppf, |
| 98 | const LangOptions& lopts, |
| 99 | const std::string& fname, |
| 100 | const std::string& htmldir, |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 101 | AnalysisStores sm, AnalysisDiagClients dc, |
Ted Kremenek | 491918e | 2009-01-23 20:52:26 +0000 | [diff] [blame] | 102 | bool visgraphviz, bool visubi, bool trim, bool analyzeAll, |
| 103 | bool displayProgress) |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 104 | : VisGraphviz(visgraphviz), VisUbigraph(visubi), TrimGraph(trim), |
| 105 | LOpts(lopts), Diags(diags), |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 106 | Ctx(0), PP(pp), PPF(ppf), |
| 107 | HTMLDir(htmldir), |
| 108 | FName(fname), |
Ted Kremenek | 491918e | 2009-01-23 20:52:26 +0000 | [diff] [blame] | 109 | AnalyzeAll(analyzeAll), SM(sm), DC(dc), |
| 110 | DisplayProgress(displayProgress) {} |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 111 | |
| 112 | void addCodeAction(CodeAction action) { |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 113 | FunctionActions.push_back(action); |
| 114 | ObjCMethodActions.push_back(action); |
| 115 | } |
| 116 | |
| 117 | void addObjCImplementationAction(CodeAction action) { |
| 118 | ObjCImplementationActions.push_back(action); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Ted Kremenek | daac634 | 2008-11-07 02:09:25 +0000 | [diff] [blame] | 121 | void addTranslationUnitAction(CodeAction action) { |
| 122 | TranslationUnitActions.push_back(action); |
| 123 | } |
| 124 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 125 | virtual void Initialize(ASTContext &Context) { |
| 126 | Ctx = &Context; |
| 127 | } |
| 128 | |
| 129 | virtual void HandleTopLevelDecl(Decl *D); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 130 | virtual void HandleTranslationUnit(TranslationUnit &TU); |
| 131 | |
Ted Kremenek | 81922f0 | 2009-02-02 20:52:40 +0000 | [diff] [blame] | 132 | void HandleCode(Decl* D, Stmt* Body, Actions& actions); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 136 | class VISIBILITY_HIDDEN AnalysisManager : public BugReporterData { |
Ted Kremenek | f304ddc | 2008-11-05 19:05:06 +0000 | [diff] [blame] | 137 | Decl* D; Stmt* Body; |
| 138 | TranslationUnit* TU; |
| 139 | |
| 140 | enum AnalysisScope { ScopeTU, ScopeDecl } AScope; |
| 141 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 142 | AnalysisConsumer& C; |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 143 | bool DisplayedFunction; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 144 | |
| 145 | llvm::OwningPtr<CFG> cfg; |
| 146 | llvm::OwningPtr<LiveVariables> liveness; |
| 147 | llvm::OwningPtr<ParentMap> PM; |
| 148 | |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 149 | // Configurable components creators. |
| 150 | StoreManagerCreator CreateStoreMgr; |
| 151 | ConstraintManagerCreator CreateConstraintMgr; |
| 152 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 153 | public: |
Ted Kremenek | 491918e | 2009-01-23 20:52:26 +0000 | [diff] [blame] | 154 | AnalysisManager(AnalysisConsumer& c, Decl* d, Stmt* b, bool displayProgress) |
| 155 | : D(d), Body(b), TU(0), AScope(ScopeDecl), C(c), |
| 156 | DisplayedFunction(!displayProgress) { |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 157 | setManagerCreators(); |
| 158 | } |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | 491918e | 2009-01-23 20:52:26 +0000 | [diff] [blame] | 160 | AnalysisManager(AnalysisConsumer& c, TranslationUnit* tu, |
| 161 | bool displayProgress) |
| 162 | : D(0), Body(0), TU(tu), AScope(ScopeTU), C(c), |
| 163 | DisplayedFunction(!displayProgress) { |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 164 | setManagerCreators(); |
| 165 | } |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 166 | |
Ted Kremenek | f304ddc | 2008-11-05 19:05:06 +0000 | [diff] [blame] | 167 | Decl* getCodeDecl() const { |
| 168 | assert (AScope == ScopeDecl); |
| 169 | return D; |
| 170 | } |
| 171 | |
| 172 | Stmt* getBody() const { |
| 173 | assert (AScope == ScopeDecl); |
| 174 | return Body; |
| 175 | } |
| 176 | |
| 177 | TranslationUnit* getTranslationUnit() const { |
| 178 | assert (AScope == ScopeTU); |
| 179 | return TU; |
| 180 | } |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 181 | |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 182 | StoreManagerCreator getStoreManagerCreator() { |
| 183 | return CreateStoreMgr; |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 184 | }; |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 185 | |
| 186 | ConstraintManagerCreator getConstraintManagerCreator() { |
| 187 | return CreateConstraintMgr; |
| 188 | } |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 189 | |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 190 | virtual CFG* getCFG() { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 191 | if (!cfg) cfg.reset(CFG::buildCFG(getBody())); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 192 | return cfg.get(); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 195 | virtual ParentMap& getParentMap() { |
Ted Kremenek | e207558 | 2008-07-02 23:16:33 +0000 | [diff] [blame] | 196 | if (!PM) |
| 197 | PM.reset(new ParentMap(getBody())); |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 198 | return *PM.get(); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 201 | virtual ASTContext& getContext() { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 202 | return *C.Ctx; |
| 203 | } |
| 204 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 205 | virtual SourceManager& getSourceManager() { |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 206 | return getContext().getSourceManager(); |
| 207 | } |
| 208 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 209 | virtual Diagnostic& getDiagnostic() { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 210 | return C.Diags; |
| 211 | } |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 212 | |
| 213 | const LangOptions& getLangOptions() const { |
| 214 | return C.LOpts; |
| 215 | } |
| 216 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 217 | virtual PathDiagnosticClient* getPathDiagnosticClient() { |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 218 | if (C.PD.get() == 0 && !C.HTMLDir.empty()) { |
| 219 | switch (C.DC) { |
| 220 | default: |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 221 | #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE)\ |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 222 | case PD_##NAME: C.PD.reset(CREATEFN(C.HTMLDir, C.PP, C.PPF)); break; |
| 223 | #include "Analyses.def" |
| 224 | } |
| 225 | } |
Ted Kremenek | e207558 | 2008-07-02 23:16:33 +0000 | [diff] [blame] | 226 | return C.PD.get(); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 227 | } |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 228 | |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 229 | virtual LiveVariables* getLiveVariables() { |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 230 | if (!liveness) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 231 | CFG* c = getCFG(); |
| 232 | if (!c) return 0; |
| 233 | |
Ted Kremenek | ca9bab0 | 2008-12-09 00:17:51 +0000 | [diff] [blame] | 234 | liveness.reset(new LiveVariables(getContext(), *c)); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 235 | liveness->runOnCFG(*c); |
| 236 | liveness->runOnAllBlocks(*c, 0, true); |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 237 | } |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 238 | |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 239 | return liveness.get(); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 240 | } |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 241 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 242 | bool shouldVisualizeGraphviz() const { |
| 243 | return C.VisGraphviz; |
| 244 | } |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 245 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 246 | bool shouldVisualizeUbigraph() const { |
| 247 | return C.VisUbigraph; |
| 248 | } |
| 249 | |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 250 | bool shouldVisualize() const { |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 251 | return C.VisGraphviz || C.VisUbigraph; |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | bool shouldTrimGraph() const { |
| 255 | return C.TrimGraph; |
| 256 | } |
| 257 | |
| 258 | void DisplayFunction() { |
| 259 | |
| 260 | if (DisplayedFunction) |
| 261 | return; |
| 262 | |
| 263 | DisplayedFunction = true; |
| 264 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 265 | // FIXME: Is getCodeDecl() always a named decl? |
| 266 | if (isa<FunctionDecl>(getCodeDecl()) || |
| 267 | isa<ObjCMethodDecl>(getCodeDecl())) { |
| 268 | NamedDecl *ND = cast<NamedDecl>(getCodeDecl()); |
| 269 | SourceManager &SM = getContext().getSourceManager(); |
Ted Kremenek | a88fcef | 2008-11-20 16:14:48 +0000 | [diff] [blame] | 270 | llvm::cerr << "ANALYZE: " |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 271 | << SM.getPresumedLoc(ND->getLocation()).getFilename() |
| 272 | << ' ' << ND->getNameAsString() << '\n'; |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 275 | |
| 276 | private: |
| 277 | /// Set configurable analyzer components creators. First check if there are |
| 278 | /// components registered at runtime. Otherwise fall back to builtin |
| 279 | /// components. |
| 280 | void setManagerCreators() { |
| 281 | if (ManagerRegistry::StoreMgrCreator != 0) { |
| 282 | CreateStoreMgr = ManagerRegistry::StoreMgrCreator; |
| 283 | } |
| 284 | else { |
| 285 | switch (C.SM) { |
| 286 | default: |
| 287 | assert(0 && "Unknown store manager."); |
| 288 | #define ANALYSIS_STORE(NAME, CMDFLAG, DESC) \ |
| 289 | case NAME##Model: CreateStoreMgr = Create##NAME##Manager; break; |
| 290 | #include "Analyses.def" |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | if (ManagerRegistry::ConstraintMgrCreator != 0) |
| 295 | CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 296 | else if (UseRanges) |
| 297 | CreateConstraintMgr = CreateRangeConstraintManager; |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 298 | else |
| 299 | CreateConstraintMgr = CreateBasicConstraintManager; |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 300 | |
| 301 | // Some DiagnosticClients should be created all the time instead of |
| 302 | // lazily. Create those now. |
| 303 | switch (C.DC) { |
| 304 | default: break; |
| 305 | #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE)\ |
| 306 | case PD_##NAME: if (AUTOCREATE) getPathDiagnosticClient(); break; |
| 307 | #include "Analyses.def" |
| 308 | } |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 311 | }; |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 312 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 313 | } // end anonymous namespace |
| 314 | |
| 315 | namespace llvm { |
| 316 | template <> struct FoldingSetTrait<CodeAction> { |
| 317 | static inline void Profile(CodeAction X, FoldingSetNodeID& ID) { |
| 318 | ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X))); |
| 319 | } |
| 320 | }; |
| 321 | } |
| 322 | |
| 323 | //===----------------------------------------------------------------------===// |
| 324 | // AnalysisConsumer implementation. |
| 325 | //===----------------------------------------------------------------------===// |
| 326 | |
| 327 | void AnalysisConsumer::HandleTopLevelDecl(Decl *D) { |
| 328 | switch (D->getKind()) { |
| 329 | case Decl::Function: { |
| 330 | FunctionDecl* FD = cast<FunctionDecl>(D); |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 331 | |
| 332 | if (FName.size() > 0 && FName != FD->getIdentifier()->getName()) |
| 333 | break; |
| 334 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 335 | Stmt* Body = FD->getBody(); |
| 336 | if (Body) HandleCode(FD, Body, FunctionActions); |
| 337 | break; |
| 338 | } |
| 339 | |
| 340 | case Decl::ObjCMethod: { |
| 341 | ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D); |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 342 | |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 343 | if (FName.size() > 0 && FName != MD->getSelector().getAsString()) |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 344 | return; |
| 345 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 346 | Stmt* Body = MD->getBody(); |
| 347 | if (Body) HandleCode(MD, Body, ObjCMethodActions); |
| 348 | break; |
| 349 | } |
| 350 | |
| 351 | default: |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 356 | void AnalysisConsumer::HandleTranslationUnit(TranslationUnit& TU) { |
| 357 | |
Ted Kremenek | daac634 | 2008-11-07 02:09:25 +0000 | [diff] [blame] | 358 | if(!TranslationUnitActions.empty()) { |
Ted Kremenek | 491918e | 2009-01-23 20:52:26 +0000 | [diff] [blame] | 359 | AnalysisManager mgr(*this, &TU, DisplayProgress); |
Ted Kremenek | daac634 | 2008-11-07 02:09:25 +0000 | [diff] [blame] | 360 | for (Actions::iterator I = TranslationUnitActions.begin(), |
| 361 | E = TranslationUnitActions.end(); I != E; ++I) |
| 362 | (*I)(mgr); |
| 363 | } |
| 364 | |
Ted Kremenek | 4d53a53 | 2009-02-13 00:51:30 +0000 | [diff] [blame] | 365 | if (!ObjCImplementationActions.empty()) |
| 366 | for (TranslationUnit::iterator I = TU.begin(), E = TU.end(); I!=E; ++I) |
| 367 | if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I)) |
| 368 | HandleCode(ID, 0, ObjCImplementationActions); |
| 369 | |
| 370 | // Delete the PathDiagnosticClient here just in case the AnalysisConsumer |
| 371 | // object doesn't get released. This will cause any side-effects in the |
| 372 | // destructor of the PathDiagnosticClient to get executed. |
| 373 | PD.reset(); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Ted Kremenek | 81922f0 | 2009-02-02 20:52:40 +0000 | [diff] [blame] | 376 | void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions& actions) { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 377 | |
| 378 | // Don't run the actions if an error has occured with parsing the file. |
| 379 | if (Diags.hasErrorOccurred()) |
| 380 | return; |
Ted Kremenek | 81922f0 | 2009-02-02 20:52:40 +0000 | [diff] [blame] | 381 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 382 | // Don't run the actions on declarations in header files unless |
| 383 | // otherwise specified. |
Ted Kremenek | 81922f0 | 2009-02-02 20:52:40 +0000 | [diff] [blame] | 384 | if (!AnalyzeAll && !Ctx->getSourceManager().isFromMainFile(D->getLocation())) |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 385 | return; |
| 386 | |
| 387 | // Create an AnalysisManager that will manage the state for analyzing |
| 388 | // this method/function. |
Ted Kremenek | 491918e | 2009-01-23 20:52:26 +0000 | [diff] [blame] | 389 | AnalysisManager mgr(*this, D, Body, DisplayProgress); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 390 | |
| 391 | // Dispatch on the actions. |
Zhongxing Xu | 3702af5 | 2008-10-30 05:03:28 +0000 | [diff] [blame] | 392 | for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I) |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 393 | (*I)(mgr); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | //===----------------------------------------------------------------------===// |
| 397 | // Analyses |
| 398 | //===----------------------------------------------------------------------===// |
| 399 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 400 | static void ActionWarnDeadStores(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 401 | if (LiveVariables* L = mgr.getLiveVariables()) { |
| 402 | BugReporter BR(mgr); |
| 403 | CheckDeadStores(*L, BR); |
| 404 | } |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 407 | static void ActionWarnUninitVals(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 408 | if (CFG* c = mgr.getCFG()) |
| 409 | CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic()); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 412 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 413 | static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf, |
| 414 | bool StandardWarnings = true) { |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 415 | |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 416 | |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 417 | llvm::OwningPtr<GRTransferFuncs> TF(tf); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 418 | |
Ted Kremenek | 8ffc8a5 | 2008-11-24 20:53:32 +0000 | [diff] [blame] | 419 | // Display progress. |
| 420 | mgr.DisplayFunction(); |
| 421 | |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 422 | // Construct the analysis engine. |
| 423 | LiveVariables* L = mgr.getLiveVariables(); |
| 424 | if (!L) return; |
Ted Kremenek | 8ffc8a5 | 2008-11-24 20:53:32 +0000 | [diff] [blame] | 425 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 426 | GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L, mgr, |
Zhongxing Xu | ff944a8 | 2008-12-22 01:52:37 +0000 | [diff] [blame] | 427 | PurgeDead, |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 428 | mgr.getStoreManagerCreator(), |
| 429 | mgr.getConstraintManagerCreator()); |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 430 | |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 431 | Eng.setTransferFunctions(tf); |
| 432 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 433 | if (StandardWarnings) { |
| 434 | Eng.RegisterInternalChecks(); |
| 435 | RegisterAppleChecks(Eng); |
| 436 | } |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 437 | |
| 438 | // Set the graph auditor. |
| 439 | llvm::OwningPtr<ExplodedNodeImpl::Auditor> Auditor; |
| 440 | if (mgr.shouldVisualizeUbigraph()) { |
| 441 | Auditor.reset(CreateUbiViz()); |
| 442 | ExplodedNodeImpl::SetAuditor(Auditor.get()); |
| 443 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 444 | |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 445 | // Execute the worklist algorithm. |
| 446 | Eng.ExecuteWorkList(); |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 447 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 448 | // Release the auditor (if any) so that it doesn't monitor the graph |
| 449 | // created BugReporter. |
| 450 | ExplodedNodeImpl::SetAuditor(0); |
| 451 | |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 452 | // Display warnings. |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 453 | Eng.getBugReporter().FlushReports(); |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 454 | |
| 455 | // Visualize the exploded graph. |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 456 | if (mgr.shouldVisualizeGraphviz()) |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 457 | Eng.ViewGraph(mgr.shouldTrimGraph()); |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 460 | static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled, |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 461 | bool StandardWarnings) { |
| 462 | |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 463 | GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(), |
| 464 | GCEnabled, |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 465 | mgr.getLangOptions()); |
| 466 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 467 | ActionGRExprEngine(mgr, TF, StandardWarnings); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 470 | static void ActionCheckerCFRef(AnalysisManager& mgr) { |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 471 | |
| 472 | switch (mgr.getLangOptions().getGCMode()) { |
| 473 | default: |
| 474 | assert (false && "Invalid GC mode."); |
| 475 | case LangOptions::NonGC: |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 476 | ActionCheckerCFRefAux(mgr, false, true); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 477 | break; |
| 478 | |
| 479 | case LangOptions::GCOnly: |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 480 | ActionCheckerCFRefAux(mgr, true, true); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 481 | break; |
| 482 | |
| 483 | case LangOptions::HybridGC: |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 484 | ActionCheckerCFRefAux(mgr, false, true); |
| 485 | ActionCheckerCFRefAux(mgr, true, false); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 486 | break; |
| 487 | } |
| 488 | } |
| 489 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 490 | static void ActionCheckerSimple(AnalysisManager& mgr) { |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 491 | ActionGRExprEngine(mgr, MakeGRSimpleValsTF()); |
| 492 | } |
| 493 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 494 | static void ActionDisplayLiveVariables(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 495 | if (LiveVariables* L = mgr.getLiveVariables()) { |
| 496 | mgr.DisplayFunction(); |
| 497 | L->dumpBlockLiveness(mgr.getSourceManager()); |
| 498 | } |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 501 | static void ActionCFGDump(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 502 | if (CFG* c = mgr.getCFG()) { |
| 503 | mgr.DisplayFunction(); |
| 504 | c->dump(); |
| 505 | } |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static void ActionCFGView(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 509 | if (CFG* c = mgr.getCFG()) { |
| 510 | mgr.DisplayFunction(); |
| 511 | c->viewCFG(); |
| 512 | } |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 515 | static void ActionWarnObjCDealloc(AnalysisManager& mgr) { |
Ted Kremenek | 4f4e7e4 | 2008-08-04 17:14:10 +0000 | [diff] [blame] | 516 | if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 517 | return; |
| 518 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 519 | BugReporter BR(mgr); |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 520 | |
| 521 | CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), |
| 522 | mgr.getLangOptions(), BR); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 525 | static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) { |
| 526 | BugReporter BR(mgr); |
| 527 | CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR); |
| 528 | } |
| 529 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 530 | static void ActionWarnObjCMethSigs(AnalysisManager& mgr) { |
Ted Kremenek | 0d8019e | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 531 | BugReporter BR(mgr); |
| 532 | |
| 533 | CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), |
| 534 | BR); |
| 535 | } |
| 536 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 537 | //===----------------------------------------------------------------------===// |
| 538 | // AnalysisConsumer creation. |
| 539 | //===----------------------------------------------------------------------===// |
| 540 | |
| 541 | ASTConsumer* clang::CreateAnalysisConsumer(Analyses* Beg, Analyses* End, |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 542 | AnalysisStores SM, |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 543 | AnalysisDiagClients DC, |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 544 | Diagnostic &diags, Preprocessor* pp, |
| 545 | PreprocessorFactory* ppf, |
| 546 | const LangOptions& lopts, |
| 547 | const std::string& fname, |
| 548 | const std::string& htmldir, |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 549 | bool VisGraphviz, bool VisUbi, |
| 550 | bool trim, |
Ted Kremenek | 491918e | 2009-01-23 20:52:26 +0000 | [diff] [blame] | 551 | bool analyzeAll, |
| 552 | bool displayProgress) { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 553 | |
| 554 | llvm::OwningPtr<AnalysisConsumer> |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 555 | C(new AnalysisConsumer(diags, pp, ppf, lopts, fname, htmldir, SM, DC, |
Ted Kremenek | 491918e | 2009-01-23 20:52:26 +0000 | [diff] [blame] | 556 | VisGraphviz, VisUbi, trim, analyzeAll, |
| 557 | displayProgress)); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 558 | |
| 559 | for ( ; Beg != End ; ++Beg) |
| 560 | switch (*Beg) { |
Ted Kremenek | f7f3c20 | 2008-07-15 00:46:02 +0000 | [diff] [blame] | 561 | #define ANALYSIS(NAME, CMD, DESC, SCOPE)\ |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 562 | case NAME:\ |
Ted Kremenek | f7f3c20 | 2008-07-15 00:46:02 +0000 | [diff] [blame] | 563 | C->add ## SCOPE ## Action(&Action ## NAME);\ |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 564 | break; |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 565 | #include "Analyses.def" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 566 | default: break; |
| 567 | } |
| 568 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 569 | return C.take(); |
| 570 | } |
| 571 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 572 | //===----------------------------------------------------------------------===// |
| 573 | // Ubigraph Visualization. FIXME: Move to separate file. |
| 574 | //===----------------------------------------------------------------------===// |
| 575 | |
| 576 | namespace { |
| 577 | |
| 578 | class UbigraphViz : public ExplodedNodeImpl::Auditor { |
| 579 | llvm::OwningPtr<llvm::raw_ostream> Out; |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 580 | llvm::sys::Path Dir, Filename; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 581 | unsigned Cntr; |
| 582 | |
| 583 | typedef llvm::DenseMap<void*,unsigned> VMap; |
| 584 | VMap M; |
| 585 | |
| 586 | public: |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 587 | UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir, |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 588 | llvm::sys::Path& filename); |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 589 | |
| 590 | ~UbigraphViz(); |
| 591 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 592 | virtual void AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst); |
| 593 | }; |
| 594 | |
| 595 | } // end anonymous namespace |
| 596 | |
| 597 | static ExplodedNodeImpl::Auditor* CreateUbiViz() { |
| 598 | std::string ErrMsg; |
| 599 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 600 | llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 601 | if (!ErrMsg.empty()) |
| 602 | return 0; |
| 603 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 604 | llvm::sys::Path Filename = Dir; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 605 | Filename.appendComponent("llvm_ubi"); |
| 606 | Filename.makeUnique(true,&ErrMsg); |
| 607 | |
| 608 | if (!ErrMsg.empty()) |
| 609 | return 0; |
| 610 | |
| 611 | llvm::cerr << "Writing '" << Filename << "'.\n"; |
| 612 | |
| 613 | llvm::OwningPtr<llvm::raw_fd_ostream> Stream; |
| 614 | std::string filename = Filename.toString(); |
Daniel Dunbar | 26fb272 | 2008-11-13 05:09:21 +0000 | [diff] [blame] | 615 | Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), false, ErrMsg)); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 616 | |
| 617 | if (!ErrMsg.empty()) |
| 618 | return 0; |
| 619 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 620 | return new UbigraphViz(Stream.take(), Dir, Filename); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) { |
Ted Kremenek | 45479c8 | 2008-08-28 18:34:41 +0000 | [diff] [blame] | 624 | |
| 625 | assert (Src != Dst && "Self-edges are not allowed."); |
| 626 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 627 | // Lookup the Src. If it is a new node, it's a root. |
| 628 | VMap::iterator SrcI= M.find(Src); |
| 629 | unsigned SrcID; |
| 630 | |
| 631 | if (SrcI == M.end()) { |
| 632 | M[Src] = SrcID = Cntr++; |
| 633 | *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n"; |
| 634 | } |
| 635 | else |
| 636 | SrcID = SrcI->second; |
| 637 | |
| 638 | // Lookup the Dst. |
| 639 | VMap::iterator DstI= M.find(Dst); |
| 640 | unsigned DstID; |
| 641 | |
| 642 | if (DstI == M.end()) { |
| 643 | M[Dst] = DstID = Cntr++; |
| 644 | *Out << "('vertex', " << DstID << ")\n"; |
| 645 | } |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 646 | else { |
| 647 | // We have hit DstID before. Change its style to reflect a cache hit. |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 648 | DstID = DstI->second; |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 649 | *Out << "('change_vertex_style', " << DstID << ", 1)\n"; |
| 650 | } |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 651 | |
| 652 | // Add the edge. |
Ted Kremenek | d128932 | 2008-08-27 22:46:55 +0000 | [diff] [blame] | 653 | *Out << "('edge', " << SrcID << ", " << DstID |
| 654 | << ", ('arrow','true'), ('oriented', 'true'))\n"; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 657 | UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir, |
| 658 | llvm::sys::Path& filename) |
| 659 | : Out(out), Dir(dir), Filename(filename), Cntr(0) { |
| 660 | |
| 661 | *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n"; |
| 662 | *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66')," |
| 663 | " ('size', '1.5'))\n"; |
| 664 | } |
| 665 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 666 | UbigraphViz::~UbigraphViz() { |
| 667 | Out.reset(0); |
| 668 | llvm::cerr << "Running 'ubiviz' program... "; |
| 669 | std::string ErrMsg; |
| 670 | llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz"); |
| 671 | std::vector<const char*> args; |
| 672 | args.push_back(Ubiviz.c_str()); |
| 673 | args.push_back(Filename.c_str()); |
| 674 | args.push_back(0); |
| 675 | |
| 676 | if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) { |
| 677 | llvm::cerr << "Error viewing graph: " << ErrMsg << "\n"; |
| 678 | } |
| 679 | |
| 680 | // Delete the directory. |
| 681 | Dir.eraseFromDisk(true); |
Daniel Dunbar | 932680e | 2008-08-29 03:45:59 +0000 | [diff] [blame] | 682 | } |