UnreachableCodeChecker.cpp revision 75df4eeede7b91c22c1d63fafd4dd4142844e3b9
1//==- 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//
13// A similar flow-sensitive only check exists in Analysis/ReachableCode.cpp
14//===----------------------------------------------------------------------===//
15
16#include "ClangSACheckers.h"
17#include "clang/StaticAnalyzer/Core/Checker.h"
18#include "clang/StaticAnalyzer/Core/CheckerManager.h"
19#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
20#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"
24#include "clang/AST/ParentMap.h"
25#include "clang/Basic/Builtins.h"
26#include "clang/Basic/SourceManager.h"
27#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;
34using namespace ento;
35
36namespace {
37class UnreachableCodeChecker : public Checker<check::EndAnalysis> {
38public:
39  void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,
40                        ExprEngine &Eng) const;
41private:
42  typedef llvm::SmallSet<unsigned, DEFAULT_CFGBLOCKS> CFGBlocksSet;
43
44  static inline const Stmt *getUnreachableStmt(const CFGBlock *CB);
45  static void FindUnreachableEntryPoints(const CFGBlock *CB,
46                                         CFGBlocksSet &reachable,
47                                         CFGBlocksSet &visited);
48  static bool isInvalidPath(const CFGBlock *CB, const ParentMap &PM);
49  static inline bool isEmptyCFGBlock(const CFGBlock *CB);
50};
51}
52
53void UnreachableCodeChecker::checkEndAnalysis(ExplodedGraph &G,
54                                              BugReporter &B,
55                                              ExprEngine &Eng) const {
56  CFGBlocksSet reachable, visited;
57
58  if (Eng.hasWorkRemaining())
59    return;
60
61  const Decl *D = 0;
62  CFG *C = 0;
63  ParentMap *PM = 0;
64  const LocationContext *LC = 0;
65  // Iterate over ExplodedGraph
66  for (ExplodedGraph::node_iterator I = G.nodes_begin(), E = G.nodes_end();
67      I != E; ++I) {
68    const ProgramPoint &P = I->getLocation();
69    LC = P.getLocationContext();
70
71    if (!D)
72      D = LC->getAnalysisDeclContext()->getDecl();
73    // Save the CFG if we don't have it already
74    if (!C)
75      C = LC->getAnalysisDeclContext()->getUnoptimizedCFG();
76    if (!PM)
77      PM = &LC->getParentMap();
78
79    if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
80      const CFGBlock *CB = BE->getBlock();
81      reachable.insert(CB->getBlockID());
82    }
83  }
84
85  // Bail out if we didn't get the CFG or the ParentMap.
86  if (!D || !C || !PM)
87    return;
88
89  // Don't do anything for template instantiations.  Proving that code
90  // in a template instantiation is unreachable means proving that it is
91  // unreachable in all instantiations.
92  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
93    if (FD->isTemplateInstantiation())
94      return;
95
96  // Find CFGBlocks that were not covered by any node
97  for (CFG::const_iterator I = C->begin(), E = C->end(); I != E; ++I) {
98    const CFGBlock *CB = *I;
99    // Check if the block is unreachable
100    if (reachable.count(CB->getBlockID()))
101      continue;
102
103    // Check if the block is empty (an artificial block)
104    if (isEmptyCFGBlock(CB))
105      continue;
106
107    // Find the entry points for this block
108    if (!visited.count(CB->getBlockID()))
109      FindUnreachableEntryPoints(CB, reachable, visited);
110
111    // This block may have been pruned; check if we still want to report it
112    if (reachable.count(CB->getBlockID()))
113      continue;
114
115    // Check for false positives
116    if (CB->size() > 0 && isInvalidPath(CB, *PM))
117      continue;
118
119    // Special case for __builtin_unreachable.
120    // FIXME: This should be extended to include other unreachable markers,
121    // such as llvm_unreachable.
122    if (!CB->empty()) {
123      bool foundUnreachable = false;
124      for (CFGBlock::const_iterator ci = CB->begin(), ce = CB->end();
125           ci != ce; ++ci) {
126        if (const CFGStmt *S = (*ci).getAs<CFGStmt>())
127          if (const CallExpr *CE = dyn_cast<CallExpr>(S->getStmt())) {
128            if (CE->isBuiltinCall() == Builtin::BI__builtin_unreachable) {
129              foundUnreachable = true;
130              break;
131            }
132          }
133      }
134      if (foundUnreachable)
135        continue;
136    }
137
138    // We found a block that wasn't covered - find the statement to report
139    SourceRange SR;
140    PathDiagnosticLocation DL;
141    SourceLocation SL;
142    if (const Stmt *S = getUnreachableStmt(CB)) {
143      SR = S->getSourceRange();
144      DL = PathDiagnosticLocation::createBegin(S, B.getSourceManager(), LC);
145      SL = DL.asLocation();
146      if (SR.isInvalid() || !SL.isValid())
147        continue;
148    }
149    else
150      continue;
151
152    // Check if the SourceLocation is in a system header
153    const SourceManager &SM = B.getSourceManager();
154    if (SM.isInSystemHeader(SL) || SM.isInExternCSystemHeader(SL))
155      continue;
156
157    B.EmitBasicReport("Unreachable code", "Dead code", "This statement is never"
158        " executed", DL, SR);
159  }
160}
161
162// Recursively finds the entry point(s) for this dead CFGBlock.
163void UnreachableCodeChecker::FindUnreachableEntryPoints(const CFGBlock *CB,
164                                                        CFGBlocksSet &reachable,
165                                                        CFGBlocksSet &visited) {
166  visited.insert(CB->getBlockID());
167
168  for (CFGBlock::const_pred_iterator I = CB->pred_begin(), E = CB->pred_end();
169      I != E; ++I) {
170    if (!reachable.count((*I)->getBlockID())) {
171      // If we find an unreachable predecessor, mark this block as reachable so
172      // we don't report this block
173      reachable.insert(CB->getBlockID());
174      if (!visited.count((*I)->getBlockID()))
175        // If we haven't previously visited the unreachable predecessor, recurse
176        FindUnreachableEntryPoints(*I, reachable, visited);
177    }
178  }
179}
180
181// Find the Stmt* in a CFGBlock for reporting a warning
182const Stmt *UnreachableCodeChecker::getUnreachableStmt(const CFGBlock *CB) {
183  for (CFGBlock::const_iterator I = CB->begin(), E = CB->end(); I != E; ++I) {
184    if (const CFGStmt *S = I->getAs<CFGStmt>())
185      return S->getStmt();
186  }
187  if (const Stmt *S = CB->getTerminator())
188    return S;
189  else
190    return 0;
191}
192
193// Determines if the path to this CFGBlock contained an element that infers this
194// block is a false positive. We assume that FindUnreachableEntryPoints has
195// already marked only the entry points to any dead code, so we need only to
196// find the condition that led to this block (the predecessor of this block.)
197// There will never be more than one predecessor.
198bool UnreachableCodeChecker::isInvalidPath(const CFGBlock *CB,
199                                           const ParentMap &PM) {
200  // We only expect a predecessor size of 0 or 1. If it is >1, then an external
201  // condition has broken our assumption (for example, a sink being placed by
202  // another check). In these cases, we choose not to report.
203  if (CB->pred_size() > 1)
204    return true;
205
206  // If there are no predecessors, then this block is trivially unreachable
207  if (CB->pred_size() == 0)
208    return false;
209
210  const CFGBlock *pred = *CB->pred_begin();
211
212  // Get the predecessor block's terminator conditon
213  const Stmt *cond = pred->getTerminatorCondition();
214
215  //assert(cond && "CFGBlock's predecessor has a terminator condition");
216  // The previous assertion is invalid in some cases (eg do/while). Leaving
217  // reporting of these situations on at the moment to help triage these cases.
218  if (!cond)
219    return false;
220
221  // Run each of the checks on the conditions
222  if (containsMacro(cond) || containsEnum(cond)
223      || containsStaticLocal(cond) || containsBuiltinOffsetOf(cond)
224      || containsStmt<UnaryExprOrTypeTraitExpr>(cond))
225    return true;
226
227  return false;
228}
229
230// Returns true if the given CFGBlock is empty
231bool UnreachableCodeChecker::isEmptyCFGBlock(const CFGBlock *CB) {
232  return CB->getLabel() == 0       // No labels
233      && CB->size() == 0           // No statements
234      && CB->getTerminator() == 0; // No terminator
235}
236
237void ento::registerUnreachableCodeChecker(CheckerManager &mgr) {
238  mgr.registerChecker<UnreachableCodeChecker>();
239}
240