blob: 0ab97c27d9c9d847a91549027eafbabce25885cf [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"
Argyrios Kyrtzidis98cabba2010-12-22 18:51:49 +000019#include "clang/GR/PathSensitive/CheckerVisitor.h"
20#include "clang/GR/PathSensitive/ExplodedGraph.h"
21#include "clang/GR/PathSensitive/SVals.h"
22#include "clang/GR/PathSensitive/CheckerHelpers.h"
23#include "clang/GR/BugReporter/BugReporter.h"
Tom Carec4b5bd82010-07-23 23:04:53 +000024#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;
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000032using namespace GR;
Tom Carec4b5bd82010-07-23 23:04:53 +000033
34namespace {
Tom Carebfc4a952010-10-01 20:52:07 +000035class UnreachableCodeChecker : public Checker {
Tom Carec4b5bd82010-07-23 23:04:53 +000036public:
37 static void *getTag();
Tom Carebc42c532010-08-03 01:55:07 +000038 void VisitEndAnalysis(ExplodedGraph &G,
39 BugReporter &B,
40 GRExprEngine &Eng);
Tom Carec4b5bd82010-07-23 23:04:53 +000041private:
Tom Caref8906792010-08-03 21:24:13 +000042 static inline const Stmt *getUnreachableStmt(const CFGBlock *CB);
Tom Carec4b5bd82010-07-23 23:04:53 +000043 void FindUnreachableEntryPoints(const CFGBlock *CB);
Tom Care7bce3a12010-07-27 23:30:21 +000044 static bool isInvalidPath(const CFGBlock *CB, const ParentMap &PM);
Tom Care505a5062010-08-12 23:01:06 +000045 static inline bool isEmptyCFGBlock(const CFGBlock *CB);
Tom Carec4b5bd82010-07-23 23:04:53 +000046
Tom Caref8906792010-08-03 21:24:13 +000047 llvm::SmallSet<unsigned, DEFAULT_CFGBLOCKS> reachable;
48 llvm::SmallSet<unsigned, DEFAULT_CFGBLOCKS> visited;
Tom Carec4b5bd82010-07-23 23:04:53 +000049};
50}
51
52void *UnreachableCodeChecker::getTag() {
53 static int x = 0;
54 return &x;
55}
56
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000057void GR::RegisterUnreachableCodeChecker(GRExprEngine &Eng) {
Tom Carec4b5bd82010-07-23 23:04:53 +000058 Eng.registerCheck(new UnreachableCodeChecker());
59}
60
61void UnreachableCodeChecker::VisitEndAnalysis(ExplodedGraph &G,
62 BugReporter &B,
Tom Carebc42c532010-08-03 01:55:07 +000063 GRExprEngine &Eng) {
Tom Carec4b5bd82010-07-23 23:04:53 +000064 // Bail out if we didn't cover all paths
Tom Carebc42c532010-08-03 01:55:07 +000065 if (Eng.hasWorkRemaining())
Tom Carec4b5bd82010-07-23 23:04:53 +000066 return;
67
68 CFG *C = 0;
Tom Care7bce3a12010-07-27 23:30:21 +000069 ParentMap *PM = 0;
Tom Carec4b5bd82010-07-23 23:04:53 +000070 // Iterate over ExplodedGraph
Tom Care505a5062010-08-12 23:01:06 +000071 for (ExplodedGraph::node_iterator I = G.nodes_begin(), E = G.nodes_end();
72 I != E; ++I) {
Tom Carec4b5bd82010-07-23 23:04:53 +000073 const ProgramPoint &P = I->getLocation();
Tom Care7bce3a12010-07-27 23:30:21 +000074 const LocationContext *LC = P.getLocationContext();
Tom Carec4b5bd82010-07-23 23:04:53 +000075
76 // Save the CFG if we don't have it already
77 if (!C)
Tom Caref8906792010-08-03 21:24:13 +000078 C = LC->getAnalysisContext()->getUnoptimizedCFG();
Tom Care7bce3a12010-07-27 23:30:21 +000079 if (!PM)
80 PM = &LC->getParentMap();
Tom Carec4b5bd82010-07-23 23:04:53 +000081
82 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
83 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.
89 if (!C || !PM)
Tom Carec4b5bd82010-07-23 23:04:53 +000090 return;
91
Jordy Rose5e04bdd2010-07-27 03:39:53 +000092 ASTContext &Ctx = B.getContext();
93
Tom Carec4b5bd82010-07-23 23:04:53 +000094 // Find CFGBlocks that were not covered by any node
Tom Care4895b9c2010-10-06 23:02:25 +000095 for (CFG::const_iterator I = C->begin(), E = C->end(); I != E; ++I) {
Tom Carec4b5bd82010-07-23 23:04:53 +000096 const CFGBlock *CB = *I;
97 // Check if the block is unreachable
Tom Caref8906792010-08-03 21:24:13 +000098 if (reachable.count(CB->getBlockID()))
Tom Care7bce3a12010-07-27 23:30:21 +000099 continue;
Tom Carec4b5bd82010-07-23 23:04:53 +0000100
Tom Care505a5062010-08-12 23:01:06 +0000101 // Check if the block is empty (an artificial block)
102 if (isEmptyCFGBlock(CB))
103 continue;
104
Tom Care7bce3a12010-07-27 23:30:21 +0000105 // Find the entry points for this block
Tom Care4895b9c2010-10-06 23:02:25 +0000106 if (!visited.count(CB->getBlockID()))
107 FindUnreachableEntryPoints(CB);
Jordy Rose5e04bdd2010-07-27 03:39:53 +0000108
Tom Care7bce3a12010-07-27 23:30:21 +0000109 // This block may have been pruned; check if we still want to report it
Tom Caref8906792010-08-03 21:24:13 +0000110 if (reachable.count(CB->getBlockID()))
Tom Care7bce3a12010-07-27 23:30:21 +0000111 continue;
112
113 // Check for false positives
114 if (CB->size() > 0 && isInvalidPath(CB, *PM))
115 continue;
116
Tom Care7bce3a12010-07-27 23:30:21 +0000117 // Special case for __builtin_unreachable.
118 // FIXME: This should be extended to include other unreachable markers,
119 // such as llvm_unreachable.
120 if (!CB->empty()) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +0000121 CFGElement First = CB->front();
122 if (CFGStmt S = First.getAs<CFGStmt>()) {
123 if (const CallExpr *CE = dyn_cast<CallExpr>(S.getStmt())) {
124 if (CE->isBuiltinCall(Ctx) == Builtin::BI__builtin_unreachable)
125 continue;
126 }
Jordy Rose5e04bdd2010-07-27 03:39:53 +0000127 }
Tom Carec4b5bd82010-07-23 23:04:53 +0000128 }
Tom Care7bce3a12010-07-27 23:30:21 +0000129
Tom Caref8906792010-08-03 21:24:13 +0000130 // We found a block that wasn't covered - find the statement to report
131 SourceRange SR;
132 SourceLocation SL;
133 if (const Stmt *S = getUnreachableStmt(CB)) {
134 SR = S->getSourceRange();
135 SL = S->getLocStart();
136 if (SR.isInvalid() || SL.isInvalid())
137 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"
148 " executed", SL, SR);
Tom Carec4b5bd82010-07-23 23:04:53 +0000149 }
150}
151
152// Recursively finds the entry point(s) for this dead CFGBlock.
153void UnreachableCodeChecker::FindUnreachableEntryPoints(const CFGBlock *CB) {
Tom Caref8906792010-08-03 21:24:13 +0000154 visited.insert(CB->getBlockID());
Tom Carec4b5bd82010-07-23 23:04:53 +0000155
Tom Care4895b9c2010-10-06 23:02:25 +0000156 for (CFGBlock::const_pred_iterator I = CB->pred_begin(), E = CB->pred_end();
157 I != E; ++I) {
Tom Care06009182010-08-05 17:53:44 +0000158 if (!reachable.count((*I)->getBlockID())) {
Tom Care4895b9c2010-10-06 23:02:25 +0000159 // If we find an unreachable predecessor, mark this block as reachable so
160 // we don't report this block
161 reachable.insert(CB->getBlockID());
Tom Care06009182010-08-05 17:53:44 +0000162 if (!visited.count((*I)->getBlockID()))
Tom Care4895b9c2010-10-06 23:02:25 +0000163 // If we haven't previously visited the unreachable predecessor, recurse
Tom Care06009182010-08-05 17:53:44 +0000164 FindUnreachableEntryPoints(*I);
Tom Carec4b5bd82010-07-23 23:04:53 +0000165 }
166 }
Tom Carec4b5bd82010-07-23 23:04:53 +0000167}
168
Tom Caref8906792010-08-03 21:24:13 +0000169// Find the Stmt* in a CFGBlock for reporting a warning
170const Stmt *UnreachableCodeChecker::getUnreachableStmt(const CFGBlock *CB) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +0000171 for (CFGBlock::const_iterator I = CB->begin(), E = CB->end(); I != E; ++I) {
172 if (CFGStmt S = I->getAs<CFGStmt>())
173 return S;
174 }
175 if (const Stmt *S = CB->getTerminator())
Tom Caref8906792010-08-03 21:24:13 +0000176 return S;
Tom Carec4b5bd82010-07-23 23:04:53 +0000177 else
Tom Caref8906792010-08-03 21:24:13 +0000178 return 0;
Tom Carec4b5bd82010-07-23 23:04:53 +0000179}
Tom Care7bce3a12010-07-27 23:30:21 +0000180
Tom Caref8906792010-08-03 21:24:13 +0000181// Determines if the path to this CFGBlock contained an element that infers this
182// block is a false positive. We assume that FindUnreachableEntryPoints has
183// already marked only the entry points to any dead code, so we need only to
184// find the condition that led to this block (the predecessor of this block.)
185// There will never be more than one predecessor.
Tom Care7bce3a12010-07-27 23:30:21 +0000186bool UnreachableCodeChecker::isInvalidPath(const CFGBlock *CB,
187 const ParentMap &PM) {
Tom Careaaca0112010-08-27 22:37:31 +0000188 // We only expect a predecessor size of 0 or 1. If it is >1, then an external
189 // condition has broken our assumption (for example, a sink being placed by
190 // another check). In these cases, we choose not to report.
191 if (CB->pred_size() > 1)
192 return true;
Tom Care7bce3a12010-07-27 23:30:21 +0000193
Tom Caref8906792010-08-03 21:24:13 +0000194 // If there are no predecessors, then this block is trivially unreachable
195 if (CB->pred_size() == 0)
196 return false;
197
198 const CFGBlock *pred = *CB->pred_begin();
199
200 // Get the predecessor block's terminator conditon
201 const Stmt *cond = pred->getTerminatorCondition();
Tom Care505a5062010-08-12 23:01:06 +0000202
203 //assert(cond && "CFGBlock's predecessor has a terminator condition");
204 // The previous assertion is invalid in some cases (eg do/while). Leaving
205 // reporting of these situations on at the moment to help triage these cases.
206 if (!cond)
207 return false;
Tom Caref8906792010-08-03 21:24:13 +0000208
209 // Run each of the checks on the conditions
210 if (containsMacro(cond) || containsEnum(cond)
211 || containsStaticLocal(cond) || containsBuiltinOffsetOf(cond)
212 || containsStmt<SizeOfAlignOfExpr>(cond))
213 return true;
Tom Care7bce3a12010-07-27 23:30:21 +0000214
215 return false;
216}
Tom Care505a5062010-08-12 23:01:06 +0000217
218// Returns true if the given CFGBlock is empty
219bool UnreachableCodeChecker::isEmptyCFGBlock(const CFGBlock *CB) {
220 return CB->getLabel() == 0 // No labels
221 && CB->size() == 0 // No statements
222 && CB->getTerminator() == 0; // No terminator
223}