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 | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 15 | #include "HTMLDiagnostics.h" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
| 17 | #include "clang/AST/Decl.h" |
| 18 | #include "clang/AST/DeclObjC.h" |
| 19 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/OwningPtr.h" |
| 21 | #include "clang/AST/CFG.h" |
| 22 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 23 | #include "clang/Analysis/PathDiagnostic.h" |
| 24 | #include "clang/Basic/SourceManager.h" |
| 25 | #include "clang/Basic/FileManager.h" |
| 26 | #include "clang/AST/ParentMap.h" |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 27 | #include "clang/AST/TranslationUnit.h" |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 28 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 29 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 30 | #include "clang/Analysis/LocalCheckers.h" |
| 31 | #include "clang/Analysis/PathSensitive/GRTransferFuncs.h" |
| 32 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Streams.h" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 34 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 35 | #include <vector> |
| 36 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 37 | using namespace clang; |
| 38 | |
| 39 | |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | // Basic type definitions. |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
| 44 | namespace { |
| 45 | |
| 46 | class AnalysisManager; |
| 47 | typedef void (*CodeAction)(AnalysisManager& Mgr); |
| 48 | |
| 49 | } // end anonymous namespace |
| 50 | |
| 51 | //===----------------------------------------------------------------------===// |
| 52 | // AnalysisConsumer declaration. |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | |
| 55 | namespace { |
| 56 | |
| 57 | class VISIBILITY_HIDDEN AnalysisConsumer : public ASTConsumer { |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 58 | typedef std::vector<CodeAction> Actions; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 59 | Actions FunctionActions; |
| 60 | Actions ObjCMethodActions; |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 61 | Actions ObjCImplementationActions; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 62 | |
| 63 | public: |
| 64 | const bool Visualize; |
| 65 | const bool TrimGraph; |
| 66 | const LangOptions& LOpts; |
| 67 | Diagnostic &Diags; |
| 68 | ASTContext* Ctx; |
| 69 | Preprocessor* PP; |
| 70 | PreprocessorFactory* PPF; |
| 71 | const std::string HTMLDir; |
| 72 | const std::string FName; |
| 73 | llvm::OwningPtr<PathDiagnosticClient> PD; |
| 74 | bool AnalyzeAll; |
| 75 | |
| 76 | AnalysisConsumer(Diagnostic &diags, Preprocessor* pp, |
| 77 | PreprocessorFactory* ppf, |
| 78 | const LangOptions& lopts, |
| 79 | const std::string& fname, |
| 80 | const std::string& htmldir, |
| 81 | bool visualize, bool trim, bool analyzeAll) |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 82 | : Visualize(visualize), TrimGraph(trim), LOpts(lopts), Diags(diags), |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 83 | Ctx(0), PP(pp), PPF(ppf), |
| 84 | HTMLDir(htmldir), |
| 85 | FName(fname), |
| 86 | AnalyzeAll(analyzeAll) {} |
| 87 | |
| 88 | void addCodeAction(CodeAction action) { |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 89 | FunctionActions.push_back(action); |
| 90 | ObjCMethodActions.push_back(action); |
| 91 | } |
| 92 | |
| 93 | void addObjCImplementationAction(CodeAction action) { |
| 94 | ObjCImplementationActions.push_back(action); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | virtual void Initialize(ASTContext &Context) { |
| 98 | Ctx = &Context; |
| 99 | } |
| 100 | |
| 101 | virtual void HandleTopLevelDecl(Decl *D); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 102 | virtual void HandleTranslationUnit(TranslationUnit &TU); |
| 103 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 104 | void HandleCode(Decl* D, Stmt* Body, Actions actions); |
| 105 | }; |
| 106 | |
| 107 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 108 | class VISIBILITY_HIDDEN AnalysisManager : public BugReporterData { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 109 | Decl* D; |
| 110 | Stmt* Body; |
| 111 | AnalysisConsumer& C; |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 112 | bool DisplayedFunction; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 113 | |
| 114 | llvm::OwningPtr<CFG> cfg; |
| 115 | llvm::OwningPtr<LiveVariables> liveness; |
| 116 | llvm::OwningPtr<ParentMap> PM; |
| 117 | |
| 118 | public: |
| 119 | AnalysisManager(AnalysisConsumer& c, Decl* d, Stmt* b) |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 120 | : D(d), Body(b), C(c), DisplayedFunction(false) {} |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 121 | |
| 122 | |
| 123 | Decl* getCodeDecl() const { return D; } |
| 124 | Stmt* getBody() const { return Body; } |
| 125 | |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 126 | virtual CFG* getCFG() { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 127 | if (!cfg) cfg.reset(CFG::buildCFG(getBody())); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 128 | return cfg.get(); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 131 | virtual ParentMap& getParentMap() { |
Ted Kremenek | e207558 | 2008-07-02 23:16:33 +0000 | [diff] [blame] | 132 | if (!PM) |
| 133 | PM.reset(new ParentMap(getBody())); |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 134 | return *PM.get(); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 137 | virtual ASTContext& getContext() { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 138 | return *C.Ctx; |
| 139 | } |
| 140 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 141 | virtual SourceManager& getSourceManager() { |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 142 | return getContext().getSourceManager(); |
| 143 | } |
| 144 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 145 | virtual Diagnostic& getDiagnostic() { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 146 | return C.Diags; |
| 147 | } |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 148 | |
| 149 | const LangOptions& getLangOptions() const { |
| 150 | return C.LOpts; |
| 151 | } |
| 152 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 153 | virtual PathDiagnosticClient* getPathDiagnosticClient() { |
Ted Kremenek | e207558 | 2008-07-02 23:16:33 +0000 | [diff] [blame] | 154 | if (C.PD.get() == 0 && !C.HTMLDir.empty()) |
| 155 | C.PD.reset(CreateHTMLDiagnosticClient(C.HTMLDir, C.PP, C.PPF)); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 156 | |
Ted Kremenek | e207558 | 2008-07-02 23:16:33 +0000 | [diff] [blame] | 157 | return C.PD.get(); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 158 | } |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 160 | virtual LiveVariables* getLiveVariables() { |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 161 | if (!liveness) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 162 | CFG* c = getCFG(); |
| 163 | if (!c) return 0; |
| 164 | |
| 165 | liveness.reset(new LiveVariables(*c)); |
| 166 | liveness->runOnCFG(*c); |
| 167 | liveness->runOnAllBlocks(*c, 0, true); |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 168 | } |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 169 | |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 170 | return liveness.get(); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 171 | } |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 172 | |
| 173 | bool shouldVisualize() const { |
| 174 | return C.Visualize; |
| 175 | } |
| 176 | |
| 177 | bool shouldTrimGraph() const { |
| 178 | return C.TrimGraph; |
| 179 | } |
| 180 | |
| 181 | void DisplayFunction() { |
| 182 | |
| 183 | if (DisplayedFunction) |
| 184 | return; |
| 185 | |
| 186 | DisplayedFunction = true; |
| 187 | |
| 188 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(getCodeDecl())) { |
| 189 | llvm::cerr << "ANALYZE: " |
| 190 | << getContext().getSourceManager().getSourceName(FD->getLocation()) |
| 191 | << ' ' |
| 192 | << FD->getIdentifier()->getName() |
| 193 | << '\n'; |
| 194 | } |
| 195 | else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(getCodeDecl())) { |
| 196 | llvm::cerr << "ANALYZE (ObjC Method): " |
| 197 | << getContext().getSourceManager().getSourceName(MD->getLocation()) |
| 198 | << " '" |
| 199 | << MD->getSelector().getName() << "'\n"; |
| 200 | } |
| 201 | } |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | } // end anonymous namespace |
| 205 | |
| 206 | namespace llvm { |
| 207 | template <> struct FoldingSetTrait<CodeAction> { |
| 208 | static inline void Profile(CodeAction X, FoldingSetNodeID& ID) { |
| 209 | ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X))); |
| 210 | } |
| 211 | }; |
| 212 | } |
| 213 | |
| 214 | //===----------------------------------------------------------------------===// |
| 215 | // AnalysisConsumer implementation. |
| 216 | //===----------------------------------------------------------------------===// |
| 217 | |
| 218 | void AnalysisConsumer::HandleTopLevelDecl(Decl *D) { |
| 219 | switch (D->getKind()) { |
| 220 | case Decl::Function: { |
| 221 | FunctionDecl* FD = cast<FunctionDecl>(D); |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 222 | |
| 223 | if (FName.size() > 0 && FName != FD->getIdentifier()->getName()) |
| 224 | break; |
| 225 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 226 | Stmt* Body = FD->getBody(); |
| 227 | if (Body) HandleCode(FD, Body, FunctionActions); |
| 228 | break; |
| 229 | } |
| 230 | |
| 231 | case Decl::ObjCMethod: { |
| 232 | ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D); |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 233 | |
| 234 | if (FName.size() > 0 && FName != MD->getSelector().getName()) |
| 235 | return; |
| 236 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 237 | Stmt* Body = MD->getBody(); |
| 238 | if (Body) HandleCode(MD, Body, ObjCMethodActions); |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | default: |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 247 | void AnalysisConsumer::HandleTranslationUnit(TranslationUnit& TU) { |
| 248 | |
| 249 | if (ObjCImplementationActions.empty()) |
| 250 | return; |
| 251 | |
| 252 | for (TranslationUnit::iterator I = TU.begin(), E = TU.end(); I!=E; ++I) { |
| 253 | |
| 254 | if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I)) |
| 255 | HandleCode(ID, 0, ObjCImplementationActions); |
| 256 | } |
| 257 | } |
| 258 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 259 | void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions actions) { |
| 260 | |
| 261 | // Don't run the actions if an error has occured with parsing the file. |
| 262 | if (Diags.hasErrorOccurred()) |
| 263 | return; |
| 264 | |
| 265 | SourceLocation Loc = D->getLocation(); |
| 266 | |
| 267 | // Only run actions on declarations defined in actual source. |
| 268 | if (!Loc.isFileID()) |
| 269 | return; |
| 270 | |
| 271 | // Don't run the actions on declarations in header files unless |
| 272 | // otherwise specified. |
| 273 | if (!AnalyzeAll && !Ctx->getSourceManager().isFromMainFile(Loc)) |
| 274 | return; |
| 275 | |
| 276 | // Create an AnalysisManager that will manage the state for analyzing |
| 277 | // this method/function. |
| 278 | AnalysisManager mgr(*this, D, Body); |
| 279 | |
| 280 | // Dispatch on the actions. |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 281 | for (Actions::iterator I = actions.begin(), |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 282 | E = actions.end(); I != E; ++I) |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 283 | (*I)(mgr); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | //===----------------------------------------------------------------------===// |
| 287 | // Analyses |
| 288 | //===----------------------------------------------------------------------===// |
| 289 | |
| 290 | static void ActionDeadStores(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 291 | if (LiveVariables* L = mgr.getLiveVariables()) { |
| 292 | BugReporter BR(mgr); |
| 293 | CheckDeadStores(*L, BR); |
| 294 | } |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static void ActionUninitVals(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 298 | if (CFG* c = mgr.getCFG()) |
| 299 | CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic()); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 302 | |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 303 | static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf) { |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 304 | |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 305 | |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 306 | llvm::OwningPtr<GRTransferFuncs> TF(tf); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 307 | |
| 308 | // Construct the analysis engine. |
| 309 | LiveVariables* L = mgr.getLiveVariables(); |
| 310 | if (!L) return; |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 311 | |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 312 | // Display progress. |
| 313 | if (!mgr.shouldVisualize()) |
| 314 | mgr.DisplayFunction(); |
| 315 | |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 316 | GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L); |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 317 | Eng.setTransferFunctions(tf); |
| 318 | |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 319 | // Execute the worklist algorithm. |
| 320 | Eng.ExecuteWorkList(); |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 321 | |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 322 | // Display warnings. |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 323 | Eng.EmitWarnings(mgr); |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 324 | |
| 325 | // Visualize the exploded graph. |
| 326 | if (mgr.shouldVisualize()) |
| 327 | Eng.ViewGraph(mgr.shouldTrimGraph()); |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static void ActionRefLeakCheckerAux(AnalysisManager& mgr, bool GCEnabled, |
| 331 | bool StandardWarnings) { |
| 332 | |
| 333 | GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(), |
| 334 | GCEnabled, |
| 335 | StandardWarnings, |
| 336 | mgr.getLangOptions()); |
| 337 | |
| 338 | ActionGRExprEngine(mgr, TF); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | static void ActionRefLeakChecker(AnalysisManager& mgr) { |
| 342 | |
| 343 | switch (mgr.getLangOptions().getGCMode()) { |
| 344 | default: |
| 345 | assert (false && "Invalid GC mode."); |
| 346 | case LangOptions::NonGC: |
| 347 | ActionRefLeakCheckerAux(mgr, false, true); |
| 348 | break; |
| 349 | |
| 350 | case LangOptions::GCOnly: |
| 351 | ActionRefLeakCheckerAux(mgr, true, true); |
| 352 | break; |
| 353 | |
| 354 | case LangOptions::HybridGC: |
| 355 | ActionRefLeakCheckerAux(mgr, false, true); |
| 356 | ActionRefLeakCheckerAux(mgr, true, false); |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 361 | static void ActionSimpleChecks(AnalysisManager& mgr) { |
| 362 | ActionGRExprEngine(mgr, MakeGRSimpleValsTF()); |
| 363 | } |
| 364 | |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 365 | static void ActionLiveness(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 366 | if (LiveVariables* L = mgr.getLiveVariables()) { |
| 367 | mgr.DisplayFunction(); |
| 368 | L->dumpBlockLiveness(mgr.getSourceManager()); |
| 369 | } |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 372 | static void ActionCFGDump(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 373 | if (CFG* c = mgr.getCFG()) { |
| 374 | mgr.DisplayFunction(); |
| 375 | c->dump(); |
| 376 | } |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | static void ActionCFGView(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 380 | if (CFG* c = mgr.getCFG()) { |
| 381 | mgr.DisplayFunction(); |
| 382 | c->viewCFG(); |
| 383 | } |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 386 | static void ActionCheckObjCDealloc(AnalysisManager& mgr) { |
| 387 | BugReporter BR(mgr); |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame^] | 388 | |
| 389 | CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), |
| 390 | mgr.getLangOptions(), BR); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 393 | //===----------------------------------------------------------------------===// |
| 394 | // AnalysisConsumer creation. |
| 395 | //===----------------------------------------------------------------------===// |
| 396 | |
| 397 | ASTConsumer* clang::CreateAnalysisConsumer(Analyses* Beg, Analyses* End, |
| 398 | Diagnostic &diags, Preprocessor* pp, |
| 399 | PreprocessorFactory* ppf, |
| 400 | const LangOptions& lopts, |
| 401 | const std::string& fname, |
| 402 | const std::string& htmldir, |
| 403 | bool visualize, bool trim, |
| 404 | bool analyzeAll) { |
| 405 | |
| 406 | llvm::OwningPtr<AnalysisConsumer> |
| 407 | C(new AnalysisConsumer(diags, pp, ppf, lopts, fname, htmldir, |
| 408 | visualize, trim, analyzeAll)); |
| 409 | |
| 410 | for ( ; Beg != End ; ++Beg) |
| 411 | switch (*Beg) { |
| 412 | case WarnDeadStores: |
| 413 | C->addCodeAction(&ActionDeadStores); |
| 414 | break; |
| 415 | |
| 416 | case WarnUninitVals: |
| 417 | C->addCodeAction(&ActionUninitVals); |
| 418 | break; |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 419 | |
| 420 | case DisplayLiveVariables: |
| 421 | C->addCodeAction(&ActionLiveness); |
| 422 | break; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 423 | |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 424 | case CheckerCFRef: |
| 425 | C->addCodeAction(&ActionRefLeakChecker); |
| 426 | break; |
| 427 | |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 428 | case CheckerSimple: |
| 429 | C->addCodeAction(&ActionSimpleChecks); |
| 430 | break; |
| 431 | |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 432 | case CFGDump: |
| 433 | C->addCodeAction(&ActionCFGDump); |
| 434 | break; |
| 435 | |
| 436 | case CFGView: |
| 437 | C->addCodeAction(&ActionCFGView); |
| 438 | break; |
| 439 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 440 | default: break; |
| 441 | } |
| 442 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 443 | // Checks we always perform: |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame^] | 444 | if (lopts.getGCMode() != LangOptions::GCOnly) |
| 445 | C->addObjCImplementationAction(&ActionCheckObjCDealloc); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 446 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 447 | return C.take(); |
| 448 | } |
| 449 | |