blob: a40b5a3e8378ec37fb013fa3af07af21fda4d17b [file] [log] [blame]
Tom Carec4b5bd82010-07-23 23:04:53 +00001//==- 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 Rose5e04bdd2010-07-27 03:39:53 +000013// A similar flow-sensitive only check exists in Analysis/ReachableCode.cpp
Tom Carec4b5bd82010-07-23 23:04:53 +000014//===----------------------------------------------------------------------===//
15
Argyrios Kyrtzidisa0decc92011-02-15 21:25:03 +000016#include "ClangSACheckers.h"
Argyrios Kyrtzidis30726c62011-02-23 07:19:23 +000017#include "clang/AST/ParentMap.h"
18#include "clang/Basic/Builtins.h"
19#include "clang/Basic/SourceManager.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
21#include "clang/StaticAnalyzer/Core/Checker.h"
22#include "clang/StaticAnalyzer/Core/CheckerManager.h"
23#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
24#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
25#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
26#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
Benjamin Kramer4a5f7242012-04-01 19:30:51 +000027#include "llvm/ADT/SmallSet.h"
Tom Carec4b5bd82010-07-23 23:04:53 +000028
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
33using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000034using namespace ento;
Tom Carec4b5bd82010-07-23 23:04:53 +000035
36namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000037class UnreachableCodeChecker : public Checker<check::EndAnalysis> {
Tom Carec4b5bd82010-07-23 23:04:53 +000038public:
Argyrios Kyrtzidis30726c62011-02-23 07:19:23 +000039 void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,
40 ExprEngine &Eng) const;
Tom Carec4b5bd82010-07-23 23:04:53 +000041private:
Argyrios Kyrtzidis30726c62011-02-23 07:19:23 +000042 typedef llvm::SmallSet<unsigned, DEFAULT_CFGBLOCKS> CFGBlocksSet;
43
Tom Caref8906792010-08-03 21:24:13 +000044 static inline const Stmt *getUnreachableStmt(const CFGBlock *CB);
Argyrios Kyrtzidis30726c62011-02-23 07:19:23 +000045 static void FindUnreachableEntryPoints(const CFGBlock *CB,
46 CFGBlocksSet &reachable,
47 CFGBlocksSet &visited);
Tom Care7bce3a12010-07-27 23:30:21 +000048 static bool isInvalidPath(const CFGBlock *CB, const ParentMap &PM);
Tom Care505a5062010-08-12 23:01:06 +000049 static inline bool isEmptyCFGBlock(const CFGBlock *CB);
Tom Carec4b5bd82010-07-23 23:04:53 +000050};
51}
52
Argyrios Kyrtzidis30726c62011-02-23 07:19:23 +000053void UnreachableCodeChecker::checkEndAnalysis(ExplodedGraph &G,
Tom Carec4b5bd82010-07-23 23:04:53 +000054 BugReporter &B,
Argyrios Kyrtzidis30726c62011-02-23 07:19:23 +000055 ExprEngine &Eng) const {
56 CFGBlocksSet reachable, visited;
Ted Kremenek75df4ee2011-12-01 00:59:17 +000057
Tom Carebc42c532010-08-03 01:55:07 +000058 if (Eng.hasWorkRemaining())
Tom Carec4b5bd82010-07-23 23:04:53 +000059 return;
60
Ted Kremenek75df4ee2011-12-01 00:59:17 +000061 const Decl *D = 0;
Tom Carec4b5bd82010-07-23 23:04:53 +000062 CFG *C = 0;
Tom Care7bce3a12010-07-27 23:30:21 +000063 ParentMap *PM = 0;
Anna Zaks590dd8e2011-09-20 21:38:35 +000064 const LocationContext *LC = 0;
Tom Carec4b5bd82010-07-23 23:04:53 +000065 // Iterate over ExplodedGraph
Tom Care505a5062010-08-12 23:01:06 +000066 for (ExplodedGraph::node_iterator I = G.nodes_begin(), E = G.nodes_end();
67 I != E; ++I) {
Tom Carec4b5bd82010-07-23 23:04:53 +000068 const ProgramPoint &P = I->getLocation();
Anna Zaks590dd8e2011-09-20 21:38:35 +000069 LC = P.getLocationContext();
Jordan Rose51718e32013-08-19 17:03:12 +000070 if (!LC->inTopFrame())
71 continue;
Tom Carec4b5bd82010-07-23 23:04:53 +000072
Ted Kremenek75df4ee2011-12-01 00:59:17 +000073 if (!D)
74 D = LC->getAnalysisDeclContext()->getDecl();
Jordan Rose51718e32013-08-19 17:03:12 +000075
Tom Carec4b5bd82010-07-23 23:04:53 +000076 // Save the CFG if we don't have it already
77 if (!C)
Ted Kremenek1d26f482011-10-24 01:32:45 +000078 C = LC->getAnalysisDeclContext()->getUnoptimizedCFG();
Tom Care7bce3a12010-07-27 23:30:21 +000079 if (!PM)
80 PM = &LC->getParentMap();
Tom Carec4b5bd82010-07-23 23:04:53 +000081
David Blaikie7a95de62013-02-21 22:23:56 +000082 if (Optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) {
Tom Carec4b5bd82010-07-23 23:04:53 +000083 const CFGBlock *CB = BE->getBlock();
Tom Caref8906792010-08-03 21:24:13 +000084 reachable.insert(CB->getBlockID());
Tom Carec4b5bd82010-07-23 23:04:53 +000085 }
86 }
87
Tom Care7bce3a12010-07-27 23:30:21 +000088 // Bail out if we didn't get the CFG or the ParentMap.
Ted Kremenek75df4ee2011-12-01 00:59:17 +000089 if (!D || !C || !PM)
Tom Carec4b5bd82010-07-23 23:04:53 +000090 return;
Ted Kremenek75df4ee2011-12-01 00:59:17 +000091
92 // Don't do anything for template instantiations. Proving that code
93 // in a template instantiation is unreachable means proving that it is
94 // unreachable in all instantiations.
95 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
96 if (FD->isTemplateInstantiation())
97 return;
Tom Carec4b5bd82010-07-23 23:04:53 +000098
99 // Find CFGBlocks that were not covered by any node
Tom Care4895b9c2010-10-06 23:02:25 +0000100 for (CFG::const_iterator I = C->begin(), E = C->end(); I != E; ++I) {
Tom Carec4b5bd82010-07-23 23:04:53 +0000101 const CFGBlock *CB = *I;
102 // Check if the block is unreachable
Tom Caref8906792010-08-03 21:24:13 +0000103 if (reachable.count(CB->getBlockID()))
Tom Care7bce3a12010-07-27 23:30:21 +0000104 continue;
Tom Carec4b5bd82010-07-23 23:04:53 +0000105
Tom Care505a5062010-08-12 23:01:06 +0000106 // Check if the block is empty (an artificial block)
107 if (isEmptyCFGBlock(CB))
108 continue;
109
Tom Care7bce3a12010-07-27 23:30:21 +0000110 // Find the entry points for this block
Tom Care4895b9c2010-10-06 23:02:25 +0000111 if (!visited.count(CB->getBlockID()))
Argyrios Kyrtzidis30726c62011-02-23 07:19:23 +0000112 FindUnreachableEntryPoints(CB, reachable, visited);
Jordy Rose5e04bdd2010-07-27 03:39:53 +0000113
Tom Care7bce3a12010-07-27 23:30:21 +0000114 // This block may have been pruned; check if we still want to report it
Tom Caref8906792010-08-03 21:24:13 +0000115 if (reachable.count(CB->getBlockID()))
Tom Care7bce3a12010-07-27 23:30:21 +0000116 continue;
117
118 // Check for false positives
119 if (CB->size() > 0 && isInvalidPath(CB, *PM))
120 continue;
121
Ted Kremenek28cd22d2012-02-29 06:05:28 +0000122 // It is good practice to always have a "default" label in a "switch", even
123 // if we should never get there. It can be used to detect errors, for
124 // instance. Unreachable code directly under a "default" label is therefore
125 // likely to be a false positive.
126 if (const Stmt *label = CB->getLabel())
127 if (label->getStmtClass() == Stmt::DefaultStmtClass)
128 continue;
129
Tom Care7bce3a12010-07-27 23:30:21 +0000130 // Special case for __builtin_unreachable.
131 // FIXME: This should be extended to include other unreachable markers,
132 // such as llvm_unreachable.
133 if (!CB->empty()) {
Ted Kremenek88299892011-07-28 23:07:59 +0000134 bool foundUnreachable = false;
135 for (CFGBlock::const_iterator ci = CB->begin(), ce = CB->end();
136 ci != ce; ++ci) {
David Blaikieb0780542013-02-23 00:29:34 +0000137 if (Optional<CFGStmt> S = (*ci).getAs<CFGStmt>())
138 if (const CallExpr *CE = dyn_cast<CallExpr>(S->getStmt())) {
Richard Smith180f4792011-11-10 06:34:14 +0000139 if (CE->isBuiltinCall() == Builtin::BI__builtin_unreachable) {
Ted Kremenek88299892011-07-28 23:07:59 +0000140 foundUnreachable = true;
141 break;
142 }
143 }
Jordy Rose5e04bdd2010-07-27 03:39:53 +0000144 }
Ted Kremenek88299892011-07-28 23:07:59 +0000145 if (foundUnreachable)
146 continue;
Tom Carec4b5bd82010-07-23 23:04:53 +0000147 }
Tom Care7bce3a12010-07-27 23:30:21 +0000148
Tom Caref8906792010-08-03 21:24:13 +0000149 // We found a block that wasn't covered - find the statement to report
150 SourceRange SR;
Anna Zaks590dd8e2011-09-20 21:38:35 +0000151 PathDiagnosticLocation DL;
Tom Caref8906792010-08-03 21:24:13 +0000152 SourceLocation SL;
153 if (const Stmt *S = getUnreachableStmt(CB)) {
154 SR = S->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000155 DL = PathDiagnosticLocation::createBegin(S, B.getSourceManager(), LC);
156 SL = DL.asLocation();
157 if (SR.isInvalid() || !SL.isValid())
Tom Caref8906792010-08-03 21:24:13 +0000158 continue;
159 }
160 else
161 continue;
162
163 // Check if the SourceLocation is in a system header
164 const SourceManager &SM = B.getSourceManager();
165 if (SM.isInSystemHeader(SL) || SM.isInExternCSystemHeader(SL))
166 continue;
167
Ted Kremenek07189522012-04-04 18:11:35 +0000168 B.EmitBasicReport(D, "Unreachable code", "Dead code",
169 "This statement is never executed", DL, SR);
Tom Carec4b5bd82010-07-23 23:04:53 +0000170 }
171}
172
173// Recursively finds the entry point(s) for this dead CFGBlock.
Argyrios Kyrtzidis30726c62011-02-23 07:19:23 +0000174void UnreachableCodeChecker::FindUnreachableEntryPoints(const CFGBlock *CB,
175 CFGBlocksSet &reachable,
176 CFGBlocksSet &visited) {
Tom Caref8906792010-08-03 21:24:13 +0000177 visited.insert(CB->getBlockID());
Tom Carec4b5bd82010-07-23 23:04:53 +0000178
Tom Care4895b9c2010-10-06 23:02:25 +0000179 for (CFGBlock::const_pred_iterator I = CB->pred_begin(), E = CB->pred_end();
180 I != E; ++I) {
Tom Care06009182010-08-05 17:53:44 +0000181 if (!reachable.count((*I)->getBlockID())) {
Tom Care4895b9c2010-10-06 23:02:25 +0000182 // If we find an unreachable predecessor, mark this block as reachable so
183 // we don't report this block
184 reachable.insert(CB->getBlockID());
Tom Care06009182010-08-05 17:53:44 +0000185 if (!visited.count((*I)->getBlockID()))
Tom Care4895b9c2010-10-06 23:02:25 +0000186 // If we haven't previously visited the unreachable predecessor, recurse
Argyrios Kyrtzidis30726c62011-02-23 07:19:23 +0000187 FindUnreachableEntryPoints(*I, reachable, visited);
Tom Carec4b5bd82010-07-23 23:04:53 +0000188 }
189 }
Tom Carec4b5bd82010-07-23 23:04:53 +0000190}
191
Tom Caref8906792010-08-03 21:24:13 +0000192// Find the Stmt* in a CFGBlock for reporting a warning
193const Stmt *UnreachableCodeChecker::getUnreachableStmt(const CFGBlock *CB) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +0000194 for (CFGBlock::const_iterator I = CB->begin(), E = CB->end(); I != E; ++I) {
David Blaikieb0780542013-02-23 00:29:34 +0000195 if (Optional<CFGStmt> S = I->getAs<CFGStmt>())
196 return S->getStmt();
Zhongxing Xub36cd3e2010-09-16 01:25:47 +0000197 }
198 if (const Stmt *S = CB->getTerminator())
Tom Caref8906792010-08-03 21:24:13 +0000199 return S;
Tom Carec4b5bd82010-07-23 23:04:53 +0000200 else
Tom Caref8906792010-08-03 21:24:13 +0000201 return 0;
Tom Carec4b5bd82010-07-23 23:04:53 +0000202}
Tom Care7bce3a12010-07-27 23:30:21 +0000203
Tom Caref8906792010-08-03 21:24:13 +0000204// Determines if the path to this CFGBlock contained an element that infers this
205// block is a false positive. We assume that FindUnreachableEntryPoints has
206// already marked only the entry points to any dead code, so we need only to
207// find the condition that led to this block (the predecessor of this block.)
208// There will never be more than one predecessor.
Tom Care7bce3a12010-07-27 23:30:21 +0000209bool UnreachableCodeChecker::isInvalidPath(const CFGBlock *CB,
210 const ParentMap &PM) {
Tom Careaaca0112010-08-27 22:37:31 +0000211 // We only expect a predecessor size of 0 or 1. If it is >1, then an external
212 // condition has broken our assumption (for example, a sink being placed by
213 // another check). In these cases, we choose not to report.
214 if (CB->pred_size() > 1)
215 return true;
Tom Care7bce3a12010-07-27 23:30:21 +0000216
Tom Caref8906792010-08-03 21:24:13 +0000217 // If there are no predecessors, then this block is trivially unreachable
218 if (CB->pred_size() == 0)
219 return false;
220
221 const CFGBlock *pred = *CB->pred_begin();
222
223 // Get the predecessor block's terminator conditon
224 const Stmt *cond = pred->getTerminatorCondition();
Tom Care505a5062010-08-12 23:01:06 +0000225
226 //assert(cond && "CFGBlock's predecessor has a terminator condition");
227 // The previous assertion is invalid in some cases (eg do/while). Leaving
228 // reporting of these situations on at the moment to help triage these cases.
229 if (!cond)
230 return false;
Tom Caref8906792010-08-03 21:24:13 +0000231
232 // Run each of the checks on the conditions
233 if (containsMacro(cond) || containsEnum(cond)
234 || containsStaticLocal(cond) || containsBuiltinOffsetOf(cond)
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000235 || containsStmt<UnaryExprOrTypeTraitExpr>(cond))
Tom Caref8906792010-08-03 21:24:13 +0000236 return true;
Tom Care7bce3a12010-07-27 23:30:21 +0000237
238 return false;
239}
Tom Care505a5062010-08-12 23:01:06 +0000240
241// Returns true if the given CFGBlock is empty
242bool UnreachableCodeChecker::isEmptyCFGBlock(const CFGBlock *CB) {
243 return CB->getLabel() == 0 // No labels
244 && CB->size() == 0 // No statements
245 && CB->getTerminator() == 0; // No terminator
246}
Argyrios Kyrtzidis30726c62011-02-23 07:19:23 +0000247
248void ento::registerUnreachableCodeChecker(CheckerManager &mgr) {
249 mgr.registerChecker<UnreachableCodeChecker>();
250}