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