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