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