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