blob: 97cc44f0cbdbaecde8cb8607f15cdac485ebbcc1 [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 Kyrtzidisec8605f2011-03-01 01:16:21 +000017#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000018#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidis30726c62011-02-23 07:19:23 +000019#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000020#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 Kyrtzidis30726c62011-02-23 07:19:23 +000024#include "clang/AST/ParentMap.h"
25#include "clang/Basic/Builtins.h"
26#include "clang/Basic/SourceManager.h"
Tom Carec4b5bd82010-07-23 23:04:53 +000027#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
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;
57
Tom Carebc42c532010-08-03 01:55:07 +000058 if (Eng.hasWorkRemaining())
Tom Carec4b5bd82010-07-23 23:04:53 +000059 return;
60
61 CFG *C = 0;
Tom Care7bce3a12010-07-27 23:30:21 +000062 ParentMap *PM = 0;
Anna Zaks590dd8e2011-09-20 21:38:35 +000063 const LocationContext *LC = 0;
Tom Carec4b5bd82010-07-23 23:04:53 +000064 // Iterate over ExplodedGraph
Tom Care505a5062010-08-12 23:01:06 +000065 for (ExplodedGraph::node_iterator I = G.nodes_begin(), E = G.nodes_end();
66 I != E; ++I) {
Tom Carec4b5bd82010-07-23 23:04:53 +000067 const ProgramPoint &P = I->getLocation();
Anna Zaks590dd8e2011-09-20 21:38:35 +000068 LC = P.getLocationContext();
Tom Carec4b5bd82010-07-23 23:04:53 +000069
70 // Save the CFG if we don't have it already
71 if (!C)
Ted Kremenek1d26f482011-10-24 01:32:45 +000072 C = LC->getAnalysisDeclContext()->getUnoptimizedCFG();
Tom Care7bce3a12010-07-27 23:30:21 +000073 if (!PM)
74 PM = &LC->getParentMap();
Tom Carec4b5bd82010-07-23 23:04:53 +000075
76 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
77 const CFGBlock *CB = BE->getBlock();
Tom Caref8906792010-08-03 21:24:13 +000078 reachable.insert(CB->getBlockID());
Tom Carec4b5bd82010-07-23 23:04:53 +000079 }
80 }
81
Tom Care7bce3a12010-07-27 23:30:21 +000082 // Bail out if we didn't get the CFG or the ParentMap.
83 if (!C || !PM)
Tom Carec4b5bd82010-07-23 23:04:53 +000084 return;
85
86 // Find CFGBlocks that were not covered by any node
Tom Care4895b9c2010-10-06 23:02:25 +000087 for (CFG::const_iterator I = C->begin(), E = C->end(); I != E; ++I) {
Tom Carec4b5bd82010-07-23 23:04:53 +000088 const CFGBlock *CB = *I;
89 // Check if the block is unreachable
Tom Caref8906792010-08-03 21:24:13 +000090 if (reachable.count(CB->getBlockID()))
Tom Care7bce3a12010-07-27 23:30:21 +000091 continue;
Tom Carec4b5bd82010-07-23 23:04:53 +000092
Tom Care505a5062010-08-12 23:01:06 +000093 // Check if the block is empty (an artificial block)
94 if (isEmptyCFGBlock(CB))
95 continue;
96
Tom Care7bce3a12010-07-27 23:30:21 +000097 // Find the entry points for this block
Tom Care4895b9c2010-10-06 23:02:25 +000098 if (!visited.count(CB->getBlockID()))
Argyrios Kyrtzidis30726c62011-02-23 07:19:23 +000099 FindUnreachableEntryPoints(CB, reachable, visited);
Jordy Rose5e04bdd2010-07-27 03:39:53 +0000100
Tom Care7bce3a12010-07-27 23:30:21 +0000101 // This block may have been pruned; check if we still want to report it
Tom Caref8906792010-08-03 21:24:13 +0000102 if (reachable.count(CB->getBlockID()))
Tom Care7bce3a12010-07-27 23:30:21 +0000103 continue;
104
105 // Check for false positives
106 if (CB->size() > 0 && isInvalidPath(CB, *PM))
107 continue;
108
Tom Care7bce3a12010-07-27 23:30:21 +0000109 // Special case for __builtin_unreachable.
110 // FIXME: This should be extended to include other unreachable markers,
111 // such as llvm_unreachable.
112 if (!CB->empty()) {
Ted Kremenek88299892011-07-28 23:07:59 +0000113 bool foundUnreachable = false;
114 for (CFGBlock::const_iterator ci = CB->begin(), ce = CB->end();
115 ci != ce; ++ci) {
116 if (const CFGStmt *S = (*ci).getAs<CFGStmt>())
117 if (const CallExpr *CE = dyn_cast<CallExpr>(S->getStmt())) {
Richard Smith180f4792011-11-10 06:34:14 +0000118 if (CE->isBuiltinCall() == Builtin::BI__builtin_unreachable) {
Ted Kremenek88299892011-07-28 23:07:59 +0000119 foundUnreachable = true;
120 break;
121 }
122 }
Jordy Rose5e04bdd2010-07-27 03:39:53 +0000123 }
Ted Kremenek88299892011-07-28 23:07:59 +0000124 if (foundUnreachable)
125 continue;
Tom Carec4b5bd82010-07-23 23:04:53 +0000126 }
Tom Care7bce3a12010-07-27 23:30:21 +0000127
Tom Caref8906792010-08-03 21:24:13 +0000128 // We found a block that wasn't covered - find the statement to report
129 SourceRange SR;
Anna Zaks590dd8e2011-09-20 21:38:35 +0000130 PathDiagnosticLocation DL;
Tom Caref8906792010-08-03 21:24:13 +0000131 SourceLocation SL;
132 if (const Stmt *S = getUnreachableStmt(CB)) {
133 SR = S->getSourceRange();
Anna Zaks590dd8e2011-09-20 21:38:35 +0000134 DL = PathDiagnosticLocation::createBegin(S, B.getSourceManager(), LC);
135 SL = DL.asLocation();
136 if (SR.isInvalid() || !SL.isValid())
Tom Caref8906792010-08-03 21:24:13 +0000137 continue;
138 }
139 else
140 continue;
141
142 // Check if the SourceLocation is in a system header
143 const SourceManager &SM = B.getSourceManager();
144 if (SM.isInSystemHeader(SL) || SM.isInExternCSystemHeader(SL))
145 continue;
146
147 B.EmitBasicReport("Unreachable code", "Dead code", "This statement is never"
Anna Zaks590dd8e2011-09-20 21:38:35 +0000148 " executed", DL, SR);
Tom Carec4b5bd82010-07-23 23:04:53 +0000149 }
150}
151
152// Recursively finds the entry point(s) for this dead CFGBlock.
Argyrios Kyrtzidis30726c62011-02-23 07:19:23 +0000153void UnreachableCodeChecker::FindUnreachableEntryPoints(const CFGBlock *CB,
154 CFGBlocksSet &reachable,
155 CFGBlocksSet &visited) {
Tom Caref8906792010-08-03 21:24:13 +0000156 visited.insert(CB->getBlockID());
Tom Carec4b5bd82010-07-23 23:04:53 +0000157
Tom Care4895b9c2010-10-06 23:02:25 +0000158 for (CFGBlock::const_pred_iterator I = CB->pred_begin(), E = CB->pred_end();
159 I != E; ++I) {
Tom Care06009182010-08-05 17:53:44 +0000160 if (!reachable.count((*I)->getBlockID())) {
Tom Care4895b9c2010-10-06 23:02:25 +0000161 // If we find an unreachable predecessor, mark this block as reachable so
162 // we don't report this block
163 reachable.insert(CB->getBlockID());
Tom Care06009182010-08-05 17:53:44 +0000164 if (!visited.count((*I)->getBlockID()))
Tom Care4895b9c2010-10-06 23:02:25 +0000165 // If we haven't previously visited the unreachable predecessor, recurse
Argyrios Kyrtzidis30726c62011-02-23 07:19:23 +0000166 FindUnreachableEntryPoints(*I, reachable, visited);
Tom Carec4b5bd82010-07-23 23:04:53 +0000167 }
168 }
Tom Carec4b5bd82010-07-23 23:04:53 +0000169}
170
Tom Caref8906792010-08-03 21:24:13 +0000171// Find the Stmt* in a CFGBlock for reporting a warning
172const Stmt *UnreachableCodeChecker::getUnreachableStmt(const CFGBlock *CB) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +0000173 for (CFGBlock::const_iterator I = CB->begin(), E = CB->end(); I != E; ++I) {
Ted Kremenek3c0349e2011-03-01 03:15:10 +0000174 if (const CFGStmt *S = I->getAs<CFGStmt>())
175 return S->getStmt();
Zhongxing Xub36cd3e2010-09-16 01:25:47 +0000176 }
177 if (const Stmt *S = CB->getTerminator())
Tom Caref8906792010-08-03 21:24:13 +0000178 return S;
Tom Carec4b5bd82010-07-23 23:04:53 +0000179 else
Tom Caref8906792010-08-03 21:24:13 +0000180 return 0;
Tom Carec4b5bd82010-07-23 23:04:53 +0000181}
Tom Care7bce3a12010-07-27 23:30:21 +0000182
Tom Caref8906792010-08-03 21:24:13 +0000183// Determines if the path to this CFGBlock contained an element that infers this
184// block is a false positive. We assume that FindUnreachableEntryPoints has
185// already marked only the entry points to any dead code, so we need only to
186// find the condition that led to this block (the predecessor of this block.)
187// There will never be more than one predecessor.
Tom Care7bce3a12010-07-27 23:30:21 +0000188bool UnreachableCodeChecker::isInvalidPath(const CFGBlock *CB,
189 const ParentMap &PM) {
Tom Careaaca0112010-08-27 22:37:31 +0000190 // We only expect a predecessor size of 0 or 1. If it is >1, then an external
191 // condition has broken our assumption (for example, a sink being placed by
192 // another check). In these cases, we choose not to report.
193 if (CB->pred_size() > 1)
194 return true;
Tom Care7bce3a12010-07-27 23:30:21 +0000195
Tom Caref8906792010-08-03 21:24:13 +0000196 // If there are no predecessors, then this block is trivially unreachable
197 if (CB->pred_size() == 0)
198 return false;
199
200 const CFGBlock *pred = *CB->pred_begin();
201
202 // Get the predecessor block's terminator conditon
203 const Stmt *cond = pred->getTerminatorCondition();
Tom Care505a5062010-08-12 23:01:06 +0000204
205 //assert(cond && "CFGBlock's predecessor has a terminator condition");
206 // The previous assertion is invalid in some cases (eg do/while). Leaving
207 // reporting of these situations on at the moment to help triage these cases.
208 if (!cond)
209 return false;
Tom Caref8906792010-08-03 21:24:13 +0000210
211 // Run each of the checks on the conditions
212 if (containsMacro(cond) || containsEnum(cond)
213 || containsStaticLocal(cond) || containsBuiltinOffsetOf(cond)
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000214 || containsStmt<UnaryExprOrTypeTraitExpr>(cond))
Tom Caref8906792010-08-03 21:24:13 +0000215 return true;
Tom Care7bce3a12010-07-27 23:30:21 +0000216
217 return false;
218}
Tom Care505a5062010-08-12 23:01:06 +0000219
220// Returns true if the given CFGBlock is empty
221bool UnreachableCodeChecker::isEmptyCFGBlock(const CFGBlock *CB) {
222 return CB->getLabel() == 0 // No labels
223 && CB->size() == 0 // No statements
224 && CB->getTerminator() == 0; // No terminator
225}
Argyrios Kyrtzidis30726c62011-02-23 07:19:23 +0000226
227void ento::registerUnreachableCodeChecker(CheckerManager &mgr) {
228 mgr.registerChecker<UnreachableCodeChecker>();
229}