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