Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 1 | //===--- AnalysisConsumer.cpp - ASTConsumer for running Analyses ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // "Meta" ASTConsumer for running different source analyses. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "ASTConsumers.h" |
Ted Kremenek | ad99dbf | 2008-11-03 22:31:48 +0000 | [diff] [blame^] | 15 | #include "clang/Driver/PathDiagnosticClients.h" |
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. |
Zhongxing Xu | 3702af5 | 2008-10-30 05:03:28 +0000 | [diff] [blame] | 303 | for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I) |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 304 | (*I)(mgr); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | //===----------------------------------------------------------------------===// |
| 308 | // Analyses |
| 309 | //===----------------------------------------------------------------------===// |
| 310 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 311 | static void ActionWarnDeadStores(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 312 | if (LiveVariables* L = mgr.getLiveVariables()) { |
| 313 | BugReporter BR(mgr); |
| 314 | CheckDeadStores(*L, BR); |
| 315 | } |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 318 | static void ActionWarnUninitVals(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 319 | if (CFG* c = mgr.getCFG()) |
| 320 | CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic()); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 323 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 324 | static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf, |
| 325 | bool StandardWarnings = true) { |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 326 | |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 327 | |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 328 | llvm::OwningPtr<GRTransferFuncs> TF(tf); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 329 | |
| 330 | // Construct the analysis engine. |
| 331 | LiveVariables* L = mgr.getLiveVariables(); |
| 332 | if (!L) return; |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 333 | |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 334 | // Display progress. |
Ted Kremenek | 45479c8 | 2008-08-28 18:34:41 +0000 | [diff] [blame] | 335 | mgr.DisplayFunction(); |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 336 | |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 337 | GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L, |
| 338 | mgr.getStoreManagerCreator()); |
| 339 | |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 340 | Eng.setTransferFunctions(tf); |
| 341 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 342 | if (StandardWarnings) { |
| 343 | Eng.RegisterInternalChecks(); |
| 344 | RegisterAppleChecks(Eng); |
| 345 | } |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 346 | |
| 347 | // Set the graph auditor. |
| 348 | llvm::OwningPtr<ExplodedNodeImpl::Auditor> Auditor; |
| 349 | if (mgr.shouldVisualizeUbigraph()) { |
| 350 | Auditor.reset(CreateUbiViz()); |
| 351 | ExplodedNodeImpl::SetAuditor(Auditor.get()); |
| 352 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 353 | |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 354 | // Execute the worklist algorithm. |
| 355 | Eng.ExecuteWorkList(); |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 356 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 357 | // Release the auditor (if any) so that it doesn't monitor the graph |
| 358 | // created BugReporter. |
| 359 | ExplodedNodeImpl::SetAuditor(0); |
| 360 | |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 361 | // Display warnings. |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 362 | Eng.EmitWarnings(mgr); |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 363 | |
| 364 | // Visualize the exploded graph. |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 365 | if (mgr.shouldVisualizeGraphviz()) |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 366 | Eng.ViewGraph(mgr.shouldTrimGraph()); |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 369 | static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled, |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 370 | bool StandardWarnings) { |
| 371 | |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 372 | GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(), |
| 373 | GCEnabled, |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 374 | mgr.getLangOptions()); |
| 375 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 376 | ActionGRExprEngine(mgr, TF, StandardWarnings); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 379 | static void ActionCheckerCFRef(AnalysisManager& mgr) { |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 380 | |
| 381 | switch (mgr.getLangOptions().getGCMode()) { |
| 382 | default: |
| 383 | assert (false && "Invalid GC mode."); |
| 384 | case LangOptions::NonGC: |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 385 | ActionCheckerCFRefAux(mgr, false, true); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 386 | break; |
| 387 | |
| 388 | case LangOptions::GCOnly: |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 389 | ActionCheckerCFRefAux(mgr, true, true); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 390 | break; |
| 391 | |
| 392 | case LangOptions::HybridGC: |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 393 | ActionCheckerCFRefAux(mgr, false, true); |
| 394 | ActionCheckerCFRefAux(mgr, true, false); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 395 | break; |
| 396 | } |
| 397 | } |
| 398 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 399 | static void ActionCheckerSimple(AnalysisManager& mgr) { |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 400 | ActionGRExprEngine(mgr, MakeGRSimpleValsTF()); |
| 401 | } |
| 402 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 403 | static void ActionDisplayLiveVariables(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 404 | if (LiveVariables* L = mgr.getLiveVariables()) { |
| 405 | mgr.DisplayFunction(); |
| 406 | L->dumpBlockLiveness(mgr.getSourceManager()); |
| 407 | } |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 410 | static void ActionCFGDump(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 411 | if (CFG* c = mgr.getCFG()) { |
| 412 | mgr.DisplayFunction(); |
| 413 | c->dump(); |
| 414 | } |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | static void ActionCFGView(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 418 | if (CFG* c = mgr.getCFG()) { |
| 419 | mgr.DisplayFunction(); |
| 420 | c->viewCFG(); |
| 421 | } |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 424 | static void ActionWarnObjCDealloc(AnalysisManager& mgr) { |
Ted Kremenek | 4f4e7e4 | 2008-08-04 17:14:10 +0000 | [diff] [blame] | 425 | if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 426 | return; |
| 427 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 428 | BugReporter BR(mgr); |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 429 | |
| 430 | CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), |
| 431 | mgr.getLangOptions(), BR); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 434 | static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) { |
| 435 | BugReporter BR(mgr); |
| 436 | CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR); |
| 437 | } |
| 438 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 439 | static void ActionWarnObjCMethSigs(AnalysisManager& mgr) { |
Ted Kremenek | 0d8019e | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 440 | BugReporter BR(mgr); |
| 441 | |
| 442 | CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), |
| 443 | BR); |
| 444 | } |
| 445 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 446 | //===----------------------------------------------------------------------===// |
| 447 | // AnalysisConsumer creation. |
| 448 | //===----------------------------------------------------------------------===// |
| 449 | |
| 450 | ASTConsumer* clang::CreateAnalysisConsumer(Analyses* Beg, Analyses* End, |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 451 | AnalysisStores SM, |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 452 | Diagnostic &diags, Preprocessor* pp, |
| 453 | PreprocessorFactory* ppf, |
| 454 | const LangOptions& lopts, |
| 455 | const std::string& fname, |
| 456 | const std::string& htmldir, |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 457 | bool VisGraphviz, bool VisUbi, |
| 458 | bool trim, |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 459 | bool analyzeAll) { |
| 460 | |
| 461 | llvm::OwningPtr<AnalysisConsumer> |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 462 | C(new AnalysisConsumer(diags, pp, ppf, lopts, fname, htmldir, SM, |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 463 | VisGraphviz, VisUbi, trim, analyzeAll)); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 464 | |
| 465 | for ( ; Beg != End ; ++Beg) |
| 466 | switch (*Beg) { |
Ted Kremenek | f7f3c20 | 2008-07-15 00:46:02 +0000 | [diff] [blame] | 467 | #define ANALYSIS(NAME, CMD, DESC, SCOPE)\ |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 468 | case NAME:\ |
Ted Kremenek | f7f3c20 | 2008-07-15 00:46:02 +0000 | [diff] [blame] | 469 | C->add ## SCOPE ## Action(&Action ## NAME);\ |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 470 | break; |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 471 | #include "Analyses.def" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 472 | default: break; |
| 473 | } |
| 474 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 475 | return C.take(); |
| 476 | } |
| 477 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 478 | //===----------------------------------------------------------------------===// |
| 479 | // Ubigraph Visualization. FIXME: Move to separate file. |
| 480 | //===----------------------------------------------------------------------===// |
| 481 | |
| 482 | namespace { |
| 483 | |
| 484 | class UbigraphViz : public ExplodedNodeImpl::Auditor { |
| 485 | llvm::OwningPtr<llvm::raw_ostream> Out; |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 486 | llvm::sys::Path Dir, Filename; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 487 | unsigned Cntr; |
| 488 | |
| 489 | typedef llvm::DenseMap<void*,unsigned> VMap; |
| 490 | VMap M; |
| 491 | |
| 492 | public: |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 493 | UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir, |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 494 | llvm::sys::Path& filename); |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 495 | |
| 496 | ~UbigraphViz(); |
| 497 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 498 | virtual void AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst); |
| 499 | }; |
| 500 | |
| 501 | } // end anonymous namespace |
| 502 | |
| 503 | static ExplodedNodeImpl::Auditor* CreateUbiViz() { |
| 504 | std::string ErrMsg; |
| 505 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 506 | llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 507 | if (!ErrMsg.empty()) |
| 508 | return 0; |
| 509 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 510 | llvm::sys::Path Filename = Dir; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 511 | Filename.appendComponent("llvm_ubi"); |
| 512 | Filename.makeUnique(true,&ErrMsg); |
| 513 | |
| 514 | if (!ErrMsg.empty()) |
| 515 | return 0; |
| 516 | |
| 517 | llvm::cerr << "Writing '" << Filename << "'.\n"; |
| 518 | |
| 519 | llvm::OwningPtr<llvm::raw_fd_ostream> Stream; |
| 520 | std::string filename = Filename.toString(); |
| 521 | Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), ErrMsg)); |
| 522 | |
| 523 | if (!ErrMsg.empty()) |
| 524 | return 0; |
| 525 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 526 | return new UbigraphViz(Stream.take(), Dir, Filename); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) { |
Ted Kremenek | 45479c8 | 2008-08-28 18:34:41 +0000 | [diff] [blame] | 530 | |
| 531 | assert (Src != Dst && "Self-edges are not allowed."); |
| 532 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 533 | // Lookup the Src. If it is a new node, it's a root. |
| 534 | VMap::iterator SrcI= M.find(Src); |
| 535 | unsigned SrcID; |
| 536 | |
| 537 | if (SrcI == M.end()) { |
| 538 | M[Src] = SrcID = Cntr++; |
| 539 | *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n"; |
| 540 | } |
| 541 | else |
| 542 | SrcID = SrcI->second; |
| 543 | |
| 544 | // Lookup the Dst. |
| 545 | VMap::iterator DstI= M.find(Dst); |
| 546 | unsigned DstID; |
| 547 | |
| 548 | if (DstI == M.end()) { |
| 549 | M[Dst] = DstID = Cntr++; |
| 550 | *Out << "('vertex', " << DstID << ")\n"; |
| 551 | } |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 552 | else { |
| 553 | // 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] | 554 | DstID = DstI->second; |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 555 | *Out << "('change_vertex_style', " << DstID << ", 1)\n"; |
| 556 | } |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 557 | |
| 558 | // Add the edge. |
Ted Kremenek | d128932 | 2008-08-27 22:46:55 +0000 | [diff] [blame] | 559 | *Out << "('edge', " << SrcID << ", " << DstID |
| 560 | << ", ('arrow','true'), ('oriented', 'true'))\n"; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 563 | UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir, |
| 564 | llvm::sys::Path& filename) |
| 565 | : Out(out), Dir(dir), Filename(filename), Cntr(0) { |
| 566 | |
| 567 | *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n"; |
| 568 | *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66')," |
| 569 | " ('size', '1.5'))\n"; |
| 570 | } |
| 571 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 572 | UbigraphViz::~UbigraphViz() { |
| 573 | Out.reset(0); |
| 574 | llvm::cerr << "Running 'ubiviz' program... "; |
| 575 | std::string ErrMsg; |
| 576 | llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz"); |
| 577 | std::vector<const char*> args; |
| 578 | args.push_back(Ubiviz.c_str()); |
| 579 | args.push_back(Filename.c_str()); |
| 580 | args.push_back(0); |
| 581 | |
| 582 | if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) { |
| 583 | llvm::cerr << "Error viewing graph: " << ErrMsg << "\n"; |
| 584 | } |
| 585 | |
| 586 | // Delete the directory. |
| 587 | Dir.eraseFromDisk(true); |
Daniel Dunbar | 932680e | 2008-08-29 03:45:59 +0000 | [diff] [blame] | 588 | } |