Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 1 | //==--AnalyzerStatsChecker.cpp - Analyzer visitation statistics --*- C++ -*-==// |
| 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 | // This file reports various statistics about analyzer visitation. |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 12 | #include "ClangSACheckers.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 13 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 14 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 15 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h" |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
Argyrios Kyrtzidis | a7af5ea | 2010-12-22 18:52:56 +0000 | [diff] [blame] | 18 | |
Benjamin Kramer | c35fb7d | 2012-01-28 12:06:22 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceManager.h" |
| 21 | #include "llvm/ADT/SmallPtrSet.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 25 | using namespace ento; |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 26 | |
| 27 | namespace { |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 28 | class AnalyzerStatsChecker : public Checker<check::EndAnalysis> { |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 29 | public: |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 30 | void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const; |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 31 | }; |
| 32 | } |
| 33 | |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 34 | void AnalyzerStatsChecker::checkEndAnalysis(ExplodedGraph &G, |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 35 | BugReporter &B, |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 36 | ExprEngine &Eng) const { |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 37 | const CFG *C = 0; |
| 38 | const Decl *D = 0; |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 39 | const SourceManager &SM = B.getSourceManager(); |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 40 | llvm::SmallPtrSet<const CFGBlock*, 256> reachable; |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 41 | |
Anna Zaks | 64394e2 | 2012-03-22 21:05:57 +0000 | [diff] [blame^] | 42 | // Root node should have the location context of the top most function. |
| 43 | const ExplodedNode *GraphRoot = *G.roots_begin(); |
| 44 | const LocationContext *LC = GraphRoot->getLocation().getLocationContext(); |
| 45 | |
| 46 | // Iterate over the exploded graph. |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 47 | for (ExplodedGraph::node_iterator I = G.nodes_begin(); |
| 48 | I != G.nodes_end(); ++I) { |
| 49 | const ProgramPoint &P = I->getLocation(); |
Anna Zaks | 64394e2 | 2012-03-22 21:05:57 +0000 | [diff] [blame^] | 50 | |
| 51 | // Only check the coverage in the top level function. |
| 52 | if (LC != P.getLocationContext()) |
| 53 | continue; |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 54 | |
Tom Care | 2cb5520 | 2010-09-29 23:48:34 +0000 | [diff] [blame] | 55 | if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) { |
| 56 | const CFGBlock *CB = BE->getBlock(); |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 57 | reachable.insert(CB); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Get the CFG and the Decl of this block |
| 62 | C = LC->getCFG(); |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 63 | D = LC->getAnalysisDeclContext()->getDecl(); |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 64 | |
| 65 | unsigned total = 0, unreachable = 0; |
| 66 | |
| 67 | // Find CFGBlocks that were not covered by any node |
| 68 | for (CFG::const_iterator I = C->begin(); I != C->end(); ++I) { |
| 69 | const CFGBlock *CB = *I; |
| 70 | ++total; |
| 71 | // Check if the block is unreachable |
| 72 | if (!reachable.count(CB)) { |
| 73 | ++unreachable; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // We never 'reach' the entry block, so correct the unreachable count |
| 78 | unreachable--; |
Anna Zaks | 64394e2 | 2012-03-22 21:05:57 +0000 | [diff] [blame^] | 79 | // There is no BlockEntrance corresponding to the exit block as well, so |
| 80 | // assume it is reached as well. |
| 81 | unreachable--; |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 82 | |
| 83 | // Generate the warning string |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 84 | SmallString<128> buf; |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 85 | llvm::raw_svector_ostream output(buf); |
| 86 | PresumedLoc Loc = SM.getPresumedLoc(D->getLocation()); |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 87 | if (Loc.isValid()) { |
| 88 | output << Loc.getFilename() << " : "; |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 89 | |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 90 | if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) { |
| 91 | const NamedDecl *ND = cast<NamedDecl>(D); |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 92 | output << *ND; |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 93 | } |
| 94 | else if (isa<BlockDecl>(D)) { |
| 95 | output << "block(line:" << Loc.getLine() << ":col:" << Loc.getColumn(); |
| 96 | } |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 97 | } |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 98 | |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 99 | output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: " |
Ted Kremenek | 422ab7a | 2011-04-02 02:56:23 +0000 | [diff] [blame] | 100 | << unreachable << " | Exhausted Block: " |
| 101 | << (Eng.wasBlocksExhausted() ? "yes" : "no") |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 102 | << " | Empty WorkList: " |
Tom Care | 0e1cd94 | 2010-09-22 21:07:51 +0000 | [diff] [blame] | 103 | << (Eng.hasEmptyWorkList() ? "yes" : "no"); |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 104 | |
| 105 | B.EmitBasicReport("Analyzer Statistics", "Internal Statistics", output.str(), |
Anna Zaks | 590dd8e | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 106 | PathDiagnosticLocation(D, SM)); |
Tom Care | 2cb5520 | 2010-09-29 23:48:34 +0000 | [diff] [blame] | 107 | |
| 108 | // Emit warning for each block we bailed out on |
Ted Kremenek | 422ab7a | 2011-04-02 02:56:23 +0000 | [diff] [blame] | 109 | typedef CoreEngine::BlocksExhausted::const_iterator ExhaustedIterator; |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 110 | const CoreEngine &CE = Eng.getCoreEngine(); |
Ted Kremenek | 422ab7a | 2011-04-02 02:56:23 +0000 | [diff] [blame] | 111 | for (ExhaustedIterator I = CE.blocks_exhausted_begin(), |
| 112 | E = CE.blocks_exhausted_end(); I != E; ++I) { |
Tom Care | 2cb5520 | 2010-09-29 23:48:34 +0000 | [diff] [blame] | 113 | const BlockEdge &BE = I->first; |
| 114 | const CFGBlock *Exit = BE.getDst(); |
| 115 | const CFGElement &CE = Exit->front(); |
| 116 | if (const CFGStmt *CS = dyn_cast<CFGStmt>(&CE)) |
| 117 | B.EmitBasicReport("Bailout Point", "Internal Statistics", "The analyzer " |
Anna Zaks | 590dd8e | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 118 | "stopped analyzing at this point", |
| 119 | PathDiagnosticLocation::createBegin(CS->getStmt(), SM, LC)); |
Tom Care | 2cb5520 | 2010-09-29 23:48:34 +0000 | [diff] [blame] | 120 | } |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 121 | } |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 122 | |
| 123 | void ento::registerAnalyzerStatsChecker(CheckerManager &mgr) { |
| 124 | mgr.registerChecker<AnalyzerStatsChecker>(); |
| 125 | } |