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