blob: 68542197147cf039b87bf6ecad459fd5afa86dd6 [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
Tom Care7bce3a12010-07-27 23:30:21 +000016#include "clang/AST/ParentMap.h"
17#include "clang/Basic/Builtins.h"
Tom Caref8906792010-08-03 21:24:13 +000018#include "clang/Basic/SourceManager.h"
Tom Carec4b5bd82010-07-23 23:04:53 +000019#include "clang/Checker/PathSensitive/CheckerVisitor.h"
20#include "clang/Checker/PathSensitive/ExplodedGraph.h"
21#include "clang/Checker/PathSensitive/SVals.h"
Tom Care7bce3a12010-07-27 23:30:21 +000022#include "clang/Checker/PathSensitive/CheckerHelpers.h"
Tom Carec4b5bd82010-07-23 23:04:53 +000023#include "clang/Checker/BugReporter/BugReporter.h"
24#include "GRExprEngineExperimentalChecks.h"
Tom Carec4b5bd82010-07-23 23:04:53 +000025#include "llvm/ADT/SmallPtrSet.h"
26
27// The number of CFGBlock pointers we want to reserve memory for. This is used
28// once for each function we analyze.
29#define DEFAULT_CFGBLOCKS 256
30
31using namespace clang;
32
33namespace {
Tom Carebfc4a952010-10-01 20:52:07 +000034class UnreachableCodeChecker : public Checker {
Tom Carec4b5bd82010-07-23 23:04:53 +000035public:
36 static void *getTag();
Tom Carebc42c532010-08-03 01:55:07 +000037 void VisitEndAnalysis(ExplodedGraph &G,
38 BugReporter &B,
39 GRExprEngine &Eng);
Tom Carec4b5bd82010-07-23 23:04:53 +000040private:
Tom Caref8906792010-08-03 21:24:13 +000041 static inline const Stmt *getUnreachableStmt(const CFGBlock *CB);
Tom Carec4b5bd82010-07-23 23:04:53 +000042 void FindUnreachableEntryPoints(const CFGBlock *CB);
Tom Care7bce3a12010-07-27 23:30:21 +000043 static bool isInvalidPath(const CFGBlock *CB, const ParentMap &PM);
Tom Care505a5062010-08-12 23:01:06 +000044 static inline bool isEmptyCFGBlock(const CFGBlock *CB);
Tom Carec4b5bd82010-07-23 23:04:53 +000045
Tom Caref8906792010-08-03 21:24:13 +000046 llvm::SmallSet<unsigned, DEFAULT_CFGBLOCKS> reachable;
47 llvm::SmallSet<unsigned, DEFAULT_CFGBLOCKS> visited;
Tom Carec4b5bd82010-07-23 23:04:53 +000048};
49}
50
51void *UnreachableCodeChecker::getTag() {
52 static int x = 0;
53 return &x;
54}
55
56void clang::RegisterUnreachableCodeChecker(GRExprEngine &Eng) {
57 Eng.registerCheck(new UnreachableCodeChecker());
58}
59
60void UnreachableCodeChecker::VisitEndAnalysis(ExplodedGraph &G,
61 BugReporter &B,
Tom Carebc42c532010-08-03 01:55:07 +000062 GRExprEngine &Eng) {
Tom Carec4b5bd82010-07-23 23:04:53 +000063 // Bail out if we didn't cover all paths
Tom Carebc42c532010-08-03 01:55:07 +000064 if (Eng.hasWorkRemaining())
Tom Carec4b5bd82010-07-23 23:04:53 +000065 return;
66
67 CFG *C = 0;
Tom Care7bce3a12010-07-27 23:30:21 +000068 ParentMap *PM = 0;
Tom Carec4b5bd82010-07-23 23:04:53 +000069 // Iterate over ExplodedGraph
Tom Care505a5062010-08-12 23:01:06 +000070 for (ExplodedGraph::node_iterator I = G.nodes_begin(), E = G.nodes_end();
71 I != E; ++I) {
Tom Carec4b5bd82010-07-23 23:04:53 +000072 const ProgramPoint &P = I->getLocation();
Tom Care7bce3a12010-07-27 23:30:21 +000073 const LocationContext *LC = P.getLocationContext();
Tom Carec4b5bd82010-07-23 23:04:53 +000074
75 // Save the CFG if we don't have it already
76 if (!C)
Tom Caref8906792010-08-03 21:24:13 +000077 C = LC->getAnalysisContext()->getUnoptimizedCFG();
Tom Care7bce3a12010-07-27 23:30:21 +000078 if (!PM)
79 PM = &LC->getParentMap();
Tom Carec4b5bd82010-07-23 23:04:53 +000080
81 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
82 const CFGBlock *CB = BE->getBlock();
Tom Caref8906792010-08-03 21:24:13 +000083 reachable.insert(CB->getBlockID());
Tom Carec4b5bd82010-07-23 23:04:53 +000084 }
85 }
86
Tom Care7bce3a12010-07-27 23:30:21 +000087 // Bail out if we didn't get the CFG or the ParentMap.
88 if (!C || !PM)
Tom Carec4b5bd82010-07-23 23:04:53 +000089 return;
90
Jordy Rose5e04bdd2010-07-27 03:39:53 +000091 ASTContext &Ctx = B.getContext();
92
Tom Carec4b5bd82010-07-23 23:04:53 +000093 // Find CFGBlocks that were not covered by any node
Tom Care4895b9c2010-10-06 23:02:25 +000094 for (CFG::const_iterator I = C->begin(), E = C->end(); I != E; ++I) {
Tom Carec4b5bd82010-07-23 23:04:53 +000095 const CFGBlock *CB = *I;
96 // Check if the block is unreachable
Tom Caref8906792010-08-03 21:24:13 +000097 if (reachable.count(CB->getBlockID()))
Tom Care7bce3a12010-07-27 23:30:21 +000098 continue;
Tom Carec4b5bd82010-07-23 23:04:53 +000099
Tom Care505a5062010-08-12 23:01:06 +0000100 // Check if the block is empty (an artificial block)
101 if (isEmptyCFGBlock(CB))
102 continue;
103
Tom Care7bce3a12010-07-27 23:30:21 +0000104 // Find the entry points for this block
Tom Care4895b9c2010-10-06 23:02:25 +0000105 if (!visited.count(CB->getBlockID()))
106 FindUnreachableEntryPoints(CB);
Jordy Rose5e04bdd2010-07-27 03:39:53 +0000107
Tom Care7bce3a12010-07-27 23:30:21 +0000108 // This block may have been pruned; check if we still want to report it
Tom Caref8906792010-08-03 21:24:13 +0000109 if (reachable.count(CB->getBlockID()))
Tom Care7bce3a12010-07-27 23:30:21 +0000110 continue;
111
112 // Check for false positives
113 if (CB->size() > 0 && isInvalidPath(CB, *PM))
114 continue;
115
Tom Care7bce3a12010-07-27 23:30:21 +0000116 // Special case for __builtin_unreachable.
117 // FIXME: This should be extended to include other unreachable markers,
118 // such as llvm_unreachable.
119 if (!CB->empty()) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +0000120 CFGElement First = CB->front();
121 if (CFGStmt S = First.getAs<CFGStmt>()) {
122 if (const CallExpr *CE = dyn_cast<CallExpr>(S.getStmt())) {
123 if (CE->isBuiltinCall(Ctx) == Builtin::BI__builtin_unreachable)
124 continue;
125 }
Jordy Rose5e04bdd2010-07-27 03:39:53 +0000126 }
Tom Carec4b5bd82010-07-23 23:04:53 +0000127 }
Tom Care7bce3a12010-07-27 23:30:21 +0000128
Tom Caref8906792010-08-03 21:24:13 +0000129 // 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 Carec4b5bd82010-07-23 23:04:53 +0000148 }
149}
150
151// Recursively finds the entry point(s) for this dead CFGBlock.
152void UnreachableCodeChecker::FindUnreachableEntryPoints(const CFGBlock *CB) {
Tom Caref8906792010-08-03 21:24:13 +0000153 visited.insert(CB->getBlockID());
Tom Carec4b5bd82010-07-23 23:04:53 +0000154
Tom Care4895b9c2010-10-06 23:02:25 +0000155 for (CFGBlock::const_pred_iterator I = CB->pred_begin(), E = CB->pred_end();
156 I != E; ++I) {
Tom Care06009182010-08-05 17:53:44 +0000157 if (!reachable.count((*I)->getBlockID())) {
Tom Care4895b9c2010-10-06 23:02:25 +0000158 // If we find an unreachable predecessor, mark this block as reachable so
159 // we don't report this block
160 reachable.insert(CB->getBlockID());
Tom Care06009182010-08-05 17:53:44 +0000161 if (!visited.count((*I)->getBlockID()))
Tom Care4895b9c2010-10-06 23:02:25 +0000162 // If we haven't previously visited the unreachable predecessor, recurse
Tom Care06009182010-08-05 17:53:44 +0000163 FindUnreachableEntryPoints(*I);
Tom Carec4b5bd82010-07-23 23:04:53 +0000164 }
165 }
Tom Carec4b5bd82010-07-23 23:04:53 +0000166}
167
Tom Caref8906792010-08-03 21:24:13 +0000168// Find the Stmt* in a CFGBlock for reporting a warning
169const Stmt *UnreachableCodeChecker::getUnreachableStmt(const CFGBlock *CB) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +0000170 for (CFGBlock::const_iterator I = CB->begin(), E = CB->end(); I != E; ++I) {
171 if (CFGStmt S = I->getAs<CFGStmt>())
172 return S;
173 }
174 if (const Stmt *S = CB->getTerminator())
Tom Caref8906792010-08-03 21:24:13 +0000175 return S;
Tom Carec4b5bd82010-07-23 23:04:53 +0000176 else
Tom Caref8906792010-08-03 21:24:13 +0000177 return 0;
Tom Carec4b5bd82010-07-23 23:04:53 +0000178}
Tom Care7bce3a12010-07-27 23:30:21 +0000179
Tom Caref8906792010-08-03 21:24:13 +0000180// Determines if the path to this CFGBlock contained an element that infers this
181// block is a false positive. We assume that FindUnreachableEntryPoints has
182// already marked only the entry points to any dead code, so we need only to
183// find the condition that led to this block (the predecessor of this block.)
184// There will never be more than one predecessor.
Tom Care7bce3a12010-07-27 23:30:21 +0000185bool UnreachableCodeChecker::isInvalidPath(const CFGBlock *CB,
186 const ParentMap &PM) {
Tom Careaaca0112010-08-27 22:37:31 +0000187 // We only expect a predecessor size of 0 or 1. If it is >1, then an external
188 // condition has broken our assumption (for example, a sink being placed by
189 // another check). In these cases, we choose not to report.
190 if (CB->pred_size() > 1)
191 return true;
Tom Care7bce3a12010-07-27 23:30:21 +0000192
Tom Caref8906792010-08-03 21:24:13 +0000193 // If there are no predecessors, then this block is trivially unreachable
194 if (CB->pred_size() == 0)
195 return false;
196
197 const CFGBlock *pred = *CB->pred_begin();
198
199 // Get the predecessor block's terminator conditon
200 const Stmt *cond = pred->getTerminatorCondition();
Tom Care505a5062010-08-12 23:01:06 +0000201
202 //assert(cond && "CFGBlock's predecessor has a terminator condition");
203 // The previous assertion is invalid in some cases (eg do/while). Leaving
204 // reporting of these situations on at the moment to help triage these cases.
205 if (!cond)
206 return false;
Tom Caref8906792010-08-03 21:24:13 +0000207
208 // Run each of the checks on the conditions
209 if (containsMacro(cond) || containsEnum(cond)
210 || containsStaticLocal(cond) || containsBuiltinOffsetOf(cond)
211 || containsStmt<SizeOfAlignOfExpr>(cond))
212 return true;
Tom Care7bce3a12010-07-27 23:30:21 +0000213
214 return false;
215}
Tom Care505a5062010-08-12 23:01:06 +0000216
217// Returns true if the given CFGBlock is empty
218bool UnreachableCodeChecker::isEmptyCFGBlock(const CFGBlock *CB) {
219 return CB->getLabel() == 0 // No labels
220 && CB->size() == 0 // No statements
221 && CB->getTerminator() == 0; // No terminator
222}