blob: 3038e29c0efc927eb8100491a9d2c8cecd826fb6 [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"
Tom Care7bce3a12010-07-27 23:30:21 +000017#include "clang/AST/ParentMap.h"
18#include "clang/Basic/Builtins.h"
Tom Caref8906792010-08-03 21:24:13 +000019#include "clang/Basic/SourceManager.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000020#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000021#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerVisitor.h"
22#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
23#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
24#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
25#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
Tom Carec4b5bd82010-07-23 23:04:53 +000026#include "llvm/ADT/SmallPtrSet.h"
27
28// The number of CFGBlock pointers we want to reserve memory for. This is used
29// once for each function we analyze.
30#define DEFAULT_CFGBLOCKS 256
31
32using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000033using namespace ento;
Tom Carec4b5bd82010-07-23 23:04:53 +000034
35namespace {
Tom Carebfc4a952010-10-01 20:52:07 +000036class UnreachableCodeChecker : public Checker {
Tom Carec4b5bd82010-07-23 23:04:53 +000037public:
38 static void *getTag();
Tom Carebc42c532010-08-03 01:55:07 +000039 void VisitEndAnalysis(ExplodedGraph &G,
40 BugReporter &B,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000041 ExprEngine &Eng);
Tom Carec4b5bd82010-07-23 23:04:53 +000042private:
Tom Caref8906792010-08-03 21:24:13 +000043 static inline const Stmt *getUnreachableStmt(const CFGBlock *CB);
Tom Carec4b5bd82010-07-23 23:04:53 +000044 void FindUnreachableEntryPoints(const CFGBlock *CB);
Tom Care7bce3a12010-07-27 23:30:21 +000045 static bool isInvalidPath(const CFGBlock *CB, const ParentMap &PM);
Tom Care505a5062010-08-12 23:01:06 +000046 static inline bool isEmptyCFGBlock(const CFGBlock *CB);
Tom Carec4b5bd82010-07-23 23:04:53 +000047
Tom Caref8906792010-08-03 21:24:13 +000048 llvm::SmallSet<unsigned, DEFAULT_CFGBLOCKS> reachable;
49 llvm::SmallSet<unsigned, DEFAULT_CFGBLOCKS> visited;
Tom Carec4b5bd82010-07-23 23:04:53 +000050};
51}
52
53void *UnreachableCodeChecker::getTag() {
54 static int x = 0;
55 return &x;
56}
57
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000058static void RegisterUnreachableCodeChecker(ExprEngine &Eng) {
Tom Carec4b5bd82010-07-23 23:04:53 +000059 Eng.registerCheck(new UnreachableCodeChecker());
60}
61
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000062void ento::registerUnreachableCodeChecker(CheckerManager &mgr) {
63 mgr.addCheckerRegisterFunction(RegisterUnreachableCodeChecker);
64}
65
Tom Carec4b5bd82010-07-23 23:04:53 +000066void UnreachableCodeChecker::VisitEndAnalysis(ExplodedGraph &G,
67 BugReporter &B,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000068 ExprEngine &Eng) {
Tom Carec4b5bd82010-07-23 23:04:53 +000069 // Bail out if we didn't cover all paths
Tom Carebc42c532010-08-03 01:55:07 +000070 if (Eng.hasWorkRemaining())
Tom Carec4b5bd82010-07-23 23:04:53 +000071 return;
72
73 CFG *C = 0;
Tom Care7bce3a12010-07-27 23:30:21 +000074 ParentMap *PM = 0;
Tom Carec4b5bd82010-07-23 23:04:53 +000075 // Iterate over ExplodedGraph
Tom Care505a5062010-08-12 23:01:06 +000076 for (ExplodedGraph::node_iterator I = G.nodes_begin(), E = G.nodes_end();
77 I != E; ++I) {
Tom Carec4b5bd82010-07-23 23:04:53 +000078 const ProgramPoint &P = I->getLocation();
Tom Care7bce3a12010-07-27 23:30:21 +000079 const LocationContext *LC = P.getLocationContext();
Tom Carec4b5bd82010-07-23 23:04:53 +000080
81 // Save the CFG if we don't have it already
82 if (!C)
Tom Caref8906792010-08-03 21:24:13 +000083 C = LC->getAnalysisContext()->getUnoptimizedCFG();
Tom Care7bce3a12010-07-27 23:30:21 +000084 if (!PM)
85 PM = &LC->getParentMap();
Tom Carec4b5bd82010-07-23 23:04:53 +000086
87 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
88 const CFGBlock *CB = BE->getBlock();
Tom Caref8906792010-08-03 21:24:13 +000089 reachable.insert(CB->getBlockID());
Tom Carec4b5bd82010-07-23 23:04:53 +000090 }
91 }
92
Tom Care7bce3a12010-07-27 23:30:21 +000093 // Bail out if we didn't get the CFG or the ParentMap.
94 if (!C || !PM)
Tom Carec4b5bd82010-07-23 23:04:53 +000095 return;
96
Jordy Rose5e04bdd2010-07-27 03:39:53 +000097 ASTContext &Ctx = B.getContext();
98
Tom Carec4b5bd82010-07-23 23:04:53 +000099 // 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()))
112 FindUnreachableEntryPoints(CB);
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
Tom Care7bce3a12010-07-27 23:30:21 +0000122 // Special case for __builtin_unreachable.
123 // FIXME: This should be extended to include other unreachable markers,
124 // such as llvm_unreachable.
125 if (!CB->empty()) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +0000126 CFGElement First = CB->front();
127 if (CFGStmt S = First.getAs<CFGStmt>()) {
128 if (const CallExpr *CE = dyn_cast<CallExpr>(S.getStmt())) {
129 if (CE->isBuiltinCall(Ctx) == Builtin::BI__builtin_unreachable)
130 continue;
131 }
Jordy Rose5e04bdd2010-07-27 03:39:53 +0000132 }
Tom Carec4b5bd82010-07-23 23:04:53 +0000133 }
Tom Care7bce3a12010-07-27 23:30:21 +0000134
Tom Caref8906792010-08-03 21:24:13 +0000135 // We found a block that wasn't covered - find the statement to report
136 SourceRange SR;
137 SourceLocation SL;
138 if (const Stmt *S = getUnreachableStmt(CB)) {
139 SR = S->getSourceRange();
140 SL = S->getLocStart();
141 if (SR.isInvalid() || SL.isInvalid())
142 continue;
143 }
144 else
145 continue;
146
147 // Check if the SourceLocation is in a system header
148 const SourceManager &SM = B.getSourceManager();
149 if (SM.isInSystemHeader(SL) || SM.isInExternCSystemHeader(SL))
150 continue;
151
152 B.EmitBasicReport("Unreachable code", "Dead code", "This statement is never"
153 " executed", SL, SR);
Tom Carec4b5bd82010-07-23 23:04:53 +0000154 }
155}
156
157// Recursively finds the entry point(s) for this dead CFGBlock.
158void UnreachableCodeChecker::FindUnreachableEntryPoints(const CFGBlock *CB) {
Tom Caref8906792010-08-03 21:24:13 +0000159 visited.insert(CB->getBlockID());
Tom Carec4b5bd82010-07-23 23:04:53 +0000160
Tom Care4895b9c2010-10-06 23:02:25 +0000161 for (CFGBlock::const_pred_iterator I = CB->pred_begin(), E = CB->pred_end();
162 I != E; ++I) {
Tom Care06009182010-08-05 17:53:44 +0000163 if (!reachable.count((*I)->getBlockID())) {
Tom Care4895b9c2010-10-06 23:02:25 +0000164 // If we find an unreachable predecessor, mark this block as reachable so
165 // we don't report this block
166 reachable.insert(CB->getBlockID());
Tom Care06009182010-08-05 17:53:44 +0000167 if (!visited.count((*I)->getBlockID()))
Tom Care4895b9c2010-10-06 23:02:25 +0000168 // If we haven't previously visited the unreachable predecessor, recurse
Tom Care06009182010-08-05 17:53:44 +0000169 FindUnreachableEntryPoints(*I);
Tom Carec4b5bd82010-07-23 23:04:53 +0000170 }
171 }
Tom Carec4b5bd82010-07-23 23:04:53 +0000172}
173
Tom Caref8906792010-08-03 21:24:13 +0000174// Find the Stmt* in a CFGBlock for reporting a warning
175const Stmt *UnreachableCodeChecker::getUnreachableStmt(const CFGBlock *CB) {
Zhongxing Xub36cd3e2010-09-16 01:25:47 +0000176 for (CFGBlock::const_iterator I = CB->begin(), E = CB->end(); I != E; ++I) {
177 if (CFGStmt S = I->getAs<CFGStmt>())
178 return S;
179 }
180 if (const Stmt *S = CB->getTerminator())
Tom Caref8906792010-08-03 21:24:13 +0000181 return S;
Tom Carec4b5bd82010-07-23 23:04:53 +0000182 else
Tom Caref8906792010-08-03 21:24:13 +0000183 return 0;
Tom Carec4b5bd82010-07-23 23:04:53 +0000184}
Tom Care7bce3a12010-07-27 23:30:21 +0000185
Tom Caref8906792010-08-03 21:24:13 +0000186// Determines if the path to this CFGBlock contained an element that infers this
187// block is a false positive. We assume that FindUnreachableEntryPoints has
188// already marked only the entry points to any dead code, so we need only to
189// find the condition that led to this block (the predecessor of this block.)
190// There will never be more than one predecessor.
Tom Care7bce3a12010-07-27 23:30:21 +0000191bool UnreachableCodeChecker::isInvalidPath(const CFGBlock *CB,
192 const ParentMap &PM) {
Tom Careaaca0112010-08-27 22:37:31 +0000193 // We only expect a predecessor size of 0 or 1. If it is >1, then an external
194 // condition has broken our assumption (for example, a sink being placed by
195 // another check). In these cases, we choose not to report.
196 if (CB->pred_size() > 1)
197 return true;
Tom Care7bce3a12010-07-27 23:30:21 +0000198
Tom Caref8906792010-08-03 21:24:13 +0000199 // If there are no predecessors, then this block is trivially unreachable
200 if (CB->pred_size() == 0)
201 return false;
202
203 const CFGBlock *pred = *CB->pred_begin();
204
205 // Get the predecessor block's terminator conditon
206 const Stmt *cond = pred->getTerminatorCondition();
Tom Care505a5062010-08-12 23:01:06 +0000207
208 //assert(cond && "CFGBlock's predecessor has a terminator condition");
209 // The previous assertion is invalid in some cases (eg do/while). Leaving
210 // reporting of these situations on at the moment to help triage these cases.
211 if (!cond)
212 return false;
Tom Caref8906792010-08-03 21:24:13 +0000213
214 // Run each of the checks on the conditions
215 if (containsMacro(cond) || containsEnum(cond)
216 || containsStaticLocal(cond) || containsBuiltinOffsetOf(cond)
217 || containsStmt<SizeOfAlignOfExpr>(cond))
218 return true;
Tom Care7bce3a12010-07-27 23:30:21 +0000219
220 return false;
221}
Tom Care505a5062010-08-12 23:01:06 +0000222
223// Returns true if the given CFGBlock is empty
224bool UnreachableCodeChecker::isEmptyCFGBlock(const CFGBlock *CB) {
225 return CB->getLabel() == 0 // No labels
226 && CB->size() == 0 // No statements
227 && CB->getTerminator() == 0; // No terminator
228}