UnreachableCodeChecker.cpp revision 695fb502825a53ccd178ec1c85c77929d88acb71
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/AST/ParentMap.h" 18#include "clang/Basic/Builtins.h" 19#include "clang/Basic/SourceManager.h" 20#include "clang/StaticAnalyzer/Core/CheckerManager.h" 21#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" 26#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; 33using namespace ento; 34 35namespace { 36class UnreachableCodeChecker : public Checker { 37public: 38 static void *getTag(); 39 void VisitEndAnalysis(ExplodedGraph &G, 40 BugReporter &B, 41 ExprEngine &Eng); 42private: 43 static inline const Stmt *getUnreachableStmt(const CFGBlock *CB); 44 void FindUnreachableEntryPoints(const CFGBlock *CB); 45 static bool isInvalidPath(const CFGBlock *CB, const ParentMap &PM); 46 static inline bool isEmptyCFGBlock(const CFGBlock *CB); 47 48 llvm::SmallSet<unsigned, DEFAULT_CFGBLOCKS> reachable; 49 llvm::SmallSet<unsigned, DEFAULT_CFGBLOCKS> visited; 50}; 51} 52 53void *UnreachableCodeChecker::getTag() { 54 static int x = 0; 55 return &x; 56} 57 58static void RegisterUnreachableCodeChecker(ExprEngine &Eng) { 59 Eng.registerCheck(new UnreachableCodeChecker()); 60} 61 62void ento::registerUnreachableCodeChecker(CheckerManager &mgr) { 63 mgr.addCheckerRegisterFunction(RegisterUnreachableCodeChecker); 64} 65 66void UnreachableCodeChecker::VisitEndAnalysis(ExplodedGraph &G, 67 BugReporter &B, 68 ExprEngine &Eng) { 69 // Bail out if we didn't cover all paths 70 if (Eng.hasWorkRemaining()) 71 return; 72 73 CFG *C = 0; 74 ParentMap *PM = 0; 75 // Iterate over ExplodedGraph 76 for (ExplodedGraph::node_iterator I = G.nodes_begin(), E = G.nodes_end(); 77 I != E; ++I) { 78 const ProgramPoint &P = I->getLocation(); 79 const LocationContext *LC = P.getLocationContext(); 80 81 // Save the CFG if we don't have it already 82 if (!C) 83 C = LC->getAnalysisContext()->getUnoptimizedCFG(); 84 if (!PM) 85 PM = &LC->getParentMap(); 86 87 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) { 88 const CFGBlock *CB = BE->getBlock(); 89 reachable.insert(CB->getBlockID()); 90 } 91 } 92 93 // Bail out if we didn't get the CFG or the ParentMap. 94 if (!C || !PM) 95 return; 96 97 ASTContext &Ctx = B.getContext(); 98 99 // Find CFGBlocks that were not covered by any node 100 for (CFG::const_iterator I = C->begin(), E = C->end(); I != E; ++I) { 101 const CFGBlock *CB = *I; 102 // Check if the block is unreachable 103 if (reachable.count(CB->getBlockID())) 104 continue; 105 106 // Check if the block is empty (an artificial block) 107 if (isEmptyCFGBlock(CB)) 108 continue; 109 110 // Find the entry points for this block 111 if (!visited.count(CB->getBlockID())) 112 FindUnreachableEntryPoints(CB); 113 114 // This block may have been pruned; check if we still want to report it 115 if (reachable.count(CB->getBlockID())) 116 continue; 117 118 // Check for false positives 119 if (CB->size() > 0 && isInvalidPath(CB, *PM)) 120 continue; 121 122 // 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()) { 126 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 } 132 } 133 } 134 135 // 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); 154 } 155} 156 157// Recursively finds the entry point(s) for this dead CFGBlock. 158void UnreachableCodeChecker::FindUnreachableEntryPoints(const CFGBlock *CB) { 159 visited.insert(CB->getBlockID()); 160 161 for (CFGBlock::const_pred_iterator I = CB->pred_begin(), E = CB->pred_end(); 162 I != E; ++I) { 163 if (!reachable.count((*I)->getBlockID())) { 164 // If we find an unreachable predecessor, mark this block as reachable so 165 // we don't report this block 166 reachable.insert(CB->getBlockID()); 167 if (!visited.count((*I)->getBlockID())) 168 // If we haven't previously visited the unreachable predecessor, recurse 169 FindUnreachableEntryPoints(*I); 170 } 171 } 172} 173 174// Find the Stmt* in a CFGBlock for reporting a warning 175const Stmt *UnreachableCodeChecker::getUnreachableStmt(const CFGBlock *CB) { 176 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()) 181 return S; 182 else 183 return 0; 184} 185 186// 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. 191bool UnreachableCodeChecker::isInvalidPath(const CFGBlock *CB, 192 const ParentMap &PM) { 193 // 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; 198 199 // 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(); 207 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; 213 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; 219 220 return false; 221} 222 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} 229