Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 1 | //==- UnreachableCodeChecker.cpp - Generalized dead code checker -*- 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 implements a generalized unreachable code checker using a |
| 10 | // path-sensitive analysis. We mark any path visited, and then walk the CFG as a |
| 11 | // post-analysis to determine what was never visited. |
| 12 | // |
Jordy Rose | 5e04bdd | 2010-07-27 03:39:53 +0000 | [diff] [blame] | 13 | // A similar flow-sensitive only check exists in Analysis/ReachableCode.cpp |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Argyrios Kyrtzidis | a0decc9 | 2011-02-15 21:25:03 +0000 | [diff] [blame] | 16 | #include "ClangSACheckers.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 695fb50 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Argyrios Kyrtzidis | 30726c6 | 2011-02-23 07:19:23 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h" |
| 21 | #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" |
| 22 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h" |
| 23 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
Argyrios Kyrtzidis | 30726c6 | 2011-02-23 07:19:23 +0000 | [diff] [blame] | 24 | #include "clang/AST/ParentMap.h" |
| 25 | #include "clang/Basic/Builtins.h" |
| 26 | #include "clang/Basic/SourceManager.h" |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallPtrSet.h" |
| 28 | |
| 29 | // The number of CFGBlock pointers we want to reserve memory for. This is used |
| 30 | // once for each function we analyze. |
| 31 | #define DEFAULT_CFGBLOCKS 256 |
| 32 | |
| 33 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 34 | using namespace ento; |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 35 | |
| 36 | namespace { |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 37 | class UnreachableCodeChecker : public Checker<check::EndAnalysis> { |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 38 | public: |
Argyrios Kyrtzidis | 30726c6 | 2011-02-23 07:19:23 +0000 | [diff] [blame] | 39 | void checkEndAnalysis(ExplodedGraph &G, BugReporter &B, |
| 40 | ExprEngine &Eng) const; |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 41 | private: |
Argyrios Kyrtzidis | 30726c6 | 2011-02-23 07:19:23 +0000 | [diff] [blame] | 42 | typedef llvm::SmallSet<unsigned, DEFAULT_CFGBLOCKS> CFGBlocksSet; |
| 43 | |
Tom Care | f890679 | 2010-08-03 21:24:13 +0000 | [diff] [blame] | 44 | static inline const Stmt *getUnreachableStmt(const CFGBlock *CB); |
Argyrios Kyrtzidis | 30726c6 | 2011-02-23 07:19:23 +0000 | [diff] [blame] | 45 | static void FindUnreachableEntryPoints(const CFGBlock *CB, |
| 46 | CFGBlocksSet &reachable, |
| 47 | CFGBlocksSet &visited); |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 48 | static bool isInvalidPath(const CFGBlock *CB, const ParentMap &PM); |
Tom Care | 505a506 | 2010-08-12 23:01:06 +0000 | [diff] [blame] | 49 | static inline bool isEmptyCFGBlock(const CFGBlock *CB); |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 50 | }; |
| 51 | } |
| 52 | |
Argyrios Kyrtzidis | 30726c6 | 2011-02-23 07:19:23 +0000 | [diff] [blame] | 53 | void UnreachableCodeChecker::checkEndAnalysis(ExplodedGraph &G, |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 54 | BugReporter &B, |
Argyrios Kyrtzidis | 30726c6 | 2011-02-23 07:19:23 +0000 | [diff] [blame] | 55 | ExprEngine &Eng) const { |
| 56 | CFGBlocksSet reachable, visited; |
| 57 | |
Tom Care | bc42c53 | 2010-08-03 01:55:07 +0000 | [diff] [blame] | 58 | if (Eng.hasWorkRemaining()) |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 59 | return; |
| 60 | |
| 61 | CFG *C = 0; |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 62 | ParentMap *PM = 0; |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 63 | // Iterate over ExplodedGraph |
Tom Care | 505a506 | 2010-08-12 23:01:06 +0000 | [diff] [blame] | 64 | for (ExplodedGraph::node_iterator I = G.nodes_begin(), E = G.nodes_end(); |
| 65 | I != E; ++I) { |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 66 | const ProgramPoint &P = I->getLocation(); |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 67 | const LocationContext *LC = P.getLocationContext(); |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 68 | |
| 69 | // Save the CFG if we don't have it already |
| 70 | if (!C) |
Tom Care | f890679 | 2010-08-03 21:24:13 +0000 | [diff] [blame] | 71 | C = LC->getAnalysisContext()->getUnoptimizedCFG(); |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 72 | if (!PM) |
| 73 | PM = &LC->getParentMap(); |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 74 | |
| 75 | if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) { |
| 76 | const CFGBlock *CB = BE->getBlock(); |
Tom Care | f890679 | 2010-08-03 21:24:13 +0000 | [diff] [blame] | 77 | reachable.insert(CB->getBlockID()); |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 81 | // Bail out if we didn't get the CFG or the ParentMap. |
| 82 | if (!C || !PM) |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 83 | return; |
| 84 | |
Jordy Rose | 5e04bdd | 2010-07-27 03:39:53 +0000 | [diff] [blame] | 85 | ASTContext &Ctx = B.getContext(); |
| 86 | |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 87 | // Find CFGBlocks that were not covered by any node |
Tom Care | 4895b9c | 2010-10-06 23:02:25 +0000 | [diff] [blame] | 88 | for (CFG::const_iterator I = C->begin(), E = C->end(); I != E; ++I) { |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 89 | const CFGBlock *CB = *I; |
| 90 | // Check if the block is unreachable |
Tom Care | f890679 | 2010-08-03 21:24:13 +0000 | [diff] [blame] | 91 | if (reachable.count(CB->getBlockID())) |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 92 | continue; |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 93 | |
Tom Care | 505a506 | 2010-08-12 23:01:06 +0000 | [diff] [blame] | 94 | // Check if the block is empty (an artificial block) |
| 95 | if (isEmptyCFGBlock(CB)) |
| 96 | continue; |
| 97 | |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 98 | // Find the entry points for this block |
Tom Care | 4895b9c | 2010-10-06 23:02:25 +0000 | [diff] [blame] | 99 | if (!visited.count(CB->getBlockID())) |
Argyrios Kyrtzidis | 30726c6 | 2011-02-23 07:19:23 +0000 | [diff] [blame] | 100 | FindUnreachableEntryPoints(CB, reachable, visited); |
Jordy Rose | 5e04bdd | 2010-07-27 03:39:53 +0000 | [diff] [blame] | 101 | |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 102 | // This block may have been pruned; check if we still want to report it |
Tom Care | f890679 | 2010-08-03 21:24:13 +0000 | [diff] [blame] | 103 | if (reachable.count(CB->getBlockID())) |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 104 | continue; |
| 105 | |
| 106 | // Check for false positives |
| 107 | if (CB->size() > 0 && isInvalidPath(CB, *PM)) |
| 108 | continue; |
| 109 | |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 110 | // Special case for __builtin_unreachable. |
| 111 | // FIXME: This should be extended to include other unreachable markers, |
| 112 | // such as llvm_unreachable. |
| 113 | if (!CB->empty()) { |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame^] | 114 | bool foundUnreachable = false; |
| 115 | for (CFGBlock::const_iterator ci = CB->begin(), ce = CB->end(); |
| 116 | ci != ce; ++ci) { |
| 117 | if (const CFGStmt *S = (*ci).getAs<CFGStmt>()) |
| 118 | if (const CallExpr *CE = dyn_cast<CallExpr>(S->getStmt())) { |
| 119 | if (CE->isBuiltinCall(Ctx) == Builtin::BI__builtin_unreachable) { |
| 120 | foundUnreachable = true; |
| 121 | break; |
| 122 | } |
| 123 | } |
Jordy Rose | 5e04bdd | 2010-07-27 03:39:53 +0000 | [diff] [blame] | 124 | } |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame^] | 125 | if (foundUnreachable) |
| 126 | continue; |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 127 | } |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 128 | |
Tom Care | f890679 | 2010-08-03 21:24:13 +0000 | [diff] [blame] | 129 | // We found a block that wasn't covered - find the statement to report |
| 130 | SourceRange SR; |
| 131 | SourceLocation SL; |
| 132 | if (const Stmt *S = getUnreachableStmt(CB)) { |
| 133 | SR = S->getSourceRange(); |
| 134 | SL = S->getLocStart(); |
| 135 | if (SR.isInvalid() || SL.isInvalid()) |
| 136 | continue; |
| 137 | } |
| 138 | else |
| 139 | continue; |
| 140 | |
| 141 | // Check if the SourceLocation is in a system header |
| 142 | const SourceManager &SM = B.getSourceManager(); |
| 143 | if (SM.isInSystemHeader(SL) || SM.isInExternCSystemHeader(SL)) |
| 144 | continue; |
| 145 | |
| 146 | B.EmitBasicReport("Unreachable code", "Dead code", "This statement is never" |
| 147 | " executed", SL, SR); |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | // Recursively finds the entry point(s) for this dead CFGBlock. |
Argyrios Kyrtzidis | 30726c6 | 2011-02-23 07:19:23 +0000 | [diff] [blame] | 152 | void UnreachableCodeChecker::FindUnreachableEntryPoints(const CFGBlock *CB, |
| 153 | CFGBlocksSet &reachable, |
| 154 | CFGBlocksSet &visited) { |
Tom Care | f890679 | 2010-08-03 21:24:13 +0000 | [diff] [blame] | 155 | visited.insert(CB->getBlockID()); |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 156 | |
Tom Care | 4895b9c | 2010-10-06 23:02:25 +0000 | [diff] [blame] | 157 | for (CFGBlock::const_pred_iterator I = CB->pred_begin(), E = CB->pred_end(); |
| 158 | I != E; ++I) { |
Tom Care | 0600918 | 2010-08-05 17:53:44 +0000 | [diff] [blame] | 159 | if (!reachable.count((*I)->getBlockID())) { |
Tom Care | 4895b9c | 2010-10-06 23:02:25 +0000 | [diff] [blame] | 160 | // If we find an unreachable predecessor, mark this block as reachable so |
| 161 | // we don't report this block |
| 162 | reachable.insert(CB->getBlockID()); |
Tom Care | 0600918 | 2010-08-05 17:53:44 +0000 | [diff] [blame] | 163 | if (!visited.count((*I)->getBlockID())) |
Tom Care | 4895b9c | 2010-10-06 23:02:25 +0000 | [diff] [blame] | 164 | // If we haven't previously visited the unreachable predecessor, recurse |
Argyrios Kyrtzidis | 30726c6 | 2011-02-23 07:19:23 +0000 | [diff] [blame] | 165 | FindUnreachableEntryPoints(*I, reachable, visited); |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Tom Care | f890679 | 2010-08-03 21:24:13 +0000 | [diff] [blame] | 170 | // Find the Stmt* in a CFGBlock for reporting a warning |
| 171 | const Stmt *UnreachableCodeChecker::getUnreachableStmt(const CFGBlock *CB) { |
Zhongxing Xu | b36cd3e | 2010-09-16 01:25:47 +0000 | [diff] [blame] | 172 | for (CFGBlock::const_iterator I = CB->begin(), E = CB->end(); I != E; ++I) { |
Ted Kremenek | 3c0349e | 2011-03-01 03:15:10 +0000 | [diff] [blame] | 173 | if (const CFGStmt *S = I->getAs<CFGStmt>()) |
| 174 | return S->getStmt(); |
Zhongxing Xu | b36cd3e | 2010-09-16 01:25:47 +0000 | [diff] [blame] | 175 | } |
| 176 | if (const Stmt *S = CB->getTerminator()) |
Tom Care | f890679 | 2010-08-03 21:24:13 +0000 | [diff] [blame] | 177 | return S; |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 178 | else |
Tom Care | f890679 | 2010-08-03 21:24:13 +0000 | [diff] [blame] | 179 | return 0; |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 180 | } |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 181 | |
Tom Care | f890679 | 2010-08-03 21:24:13 +0000 | [diff] [blame] | 182 | // Determines if the path to this CFGBlock contained an element that infers this |
| 183 | // block is a false positive. We assume that FindUnreachableEntryPoints has |
| 184 | // already marked only the entry points to any dead code, so we need only to |
| 185 | // find the condition that led to this block (the predecessor of this block.) |
| 186 | // There will never be more than one predecessor. |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 187 | bool UnreachableCodeChecker::isInvalidPath(const CFGBlock *CB, |
| 188 | const ParentMap &PM) { |
Tom Care | aaca011 | 2010-08-27 22:37:31 +0000 | [diff] [blame] | 189 | // We only expect a predecessor size of 0 or 1. If it is >1, then an external |
| 190 | // condition has broken our assumption (for example, a sink being placed by |
| 191 | // another check). In these cases, we choose not to report. |
| 192 | if (CB->pred_size() > 1) |
| 193 | return true; |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 194 | |
Tom Care | f890679 | 2010-08-03 21:24:13 +0000 | [diff] [blame] | 195 | // If there are no predecessors, then this block is trivially unreachable |
| 196 | if (CB->pred_size() == 0) |
| 197 | return false; |
| 198 | |
| 199 | const CFGBlock *pred = *CB->pred_begin(); |
| 200 | |
| 201 | // Get the predecessor block's terminator conditon |
| 202 | const Stmt *cond = pred->getTerminatorCondition(); |
Tom Care | 505a506 | 2010-08-12 23:01:06 +0000 | [diff] [blame] | 203 | |
| 204 | //assert(cond && "CFGBlock's predecessor has a terminator condition"); |
| 205 | // The previous assertion is invalid in some cases (eg do/while). Leaving |
| 206 | // reporting of these situations on at the moment to help triage these cases. |
| 207 | if (!cond) |
| 208 | return false; |
Tom Care | f890679 | 2010-08-03 21:24:13 +0000 | [diff] [blame] | 209 | |
| 210 | // Run each of the checks on the conditions |
| 211 | if (containsMacro(cond) || containsEnum(cond) |
| 212 | || containsStaticLocal(cond) || containsBuiltinOffsetOf(cond) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 213 | || containsStmt<UnaryExprOrTypeTraitExpr>(cond)) |
Tom Care | f890679 | 2010-08-03 21:24:13 +0000 | [diff] [blame] | 214 | return true; |
Tom Care | 7bce3a1 | 2010-07-27 23:30:21 +0000 | [diff] [blame] | 215 | |
| 216 | return false; |
| 217 | } |
Tom Care | 505a506 | 2010-08-12 23:01:06 +0000 | [diff] [blame] | 218 | |
| 219 | // Returns true if the given CFGBlock is empty |
| 220 | bool UnreachableCodeChecker::isEmptyCFGBlock(const CFGBlock *CB) { |
| 221 | return CB->getLabel() == 0 // No labels |
| 222 | && CB->size() == 0 // No statements |
| 223 | && CB->getTerminator() == 0; // No terminator |
| 224 | } |
Argyrios Kyrtzidis | 30726c6 | 2011-02-23 07:19:23 +0000 | [diff] [blame] | 225 | |
| 226 | void ento::registerUnreachableCodeChecker(CheckerManager &mgr) { |
| 227 | mgr.registerChecker<UnreachableCodeChecker>(); |
| 228 | } |