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