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