1c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care//==- UnreachableCodeChecker.cpp - Generalized dead code checker -*- C++ -*-==// 2c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care// 3c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care// The LLVM Compiler Infrastructure 4c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care// 5c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care// This file is distributed under the University of Illinois Open Source 6c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care// License. See LICENSE.TXT for details. 7c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care// 8c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care//===----------------------------------------------------------------------===// 9c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care// This file implements a generalized unreachable code checker using a 10c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care// path-sensitive analysis. We mark any path visited, and then walk the CFG as a 11c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care// post-analysis to determine what was never visited. 12c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care// 135e04bdde8e74d991feffe9cf95d731f7e473dbb7Jordy Rose// A similar flow-sensitive only check exists in Analysis/ReachableCode.cpp 14c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care//===----------------------------------------------------------------------===// 15c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care 16a0decc9a2481f938e1675b4f7bbd58761a882a36Argyrios Kyrtzidis#include "ClangSACheckers.h" 1755fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/ParentMap.h" 1855fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Basic/Builtins.h" 1955fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Basic/SourceManager.h" 2055fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" 21ec8605f1d7ec846dbf51047bfd5c56d32d1ff91cArgyrios Kyrtzidis#include "clang/StaticAnalyzer/Core/Checker.h" 22695fb502825a53ccd178ec1c85c77929d88acb71Argyrios Kyrtzidis#include "clang/StaticAnalyzer/Core/CheckerManager.h" 2330726c6baee1417307236e854f1474fdb3cedb98Argyrios Kyrtzidis#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 2455fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h" 259b663716449b618ba0390b1dbebc54fa8e971124Ted Kremenek#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h" 269b663716449b618ba0390b1dbebc54fa8e971124Ted Kremenek#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" 274a5f724538cbc275370c9504e8169ce92503256cBenjamin Kramer#include "llvm/ADT/SmallSet.h" 28c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care 29c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care// The number of CFGBlock pointers we want to reserve memory for. This is used 30c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care// once for each function we analyze. 31c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care#define DEFAULT_CFGBLOCKS 256 32c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care 33c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Careusing namespace clang; 349ef6537a894c33003359b1f9b9676e9178e028b7Ted Kremenekusing namespace ento; 35c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care 36c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Carenamespace { 37ec8605f1d7ec846dbf51047bfd5c56d32d1ff91cArgyrios Kyrtzidisclass UnreachableCodeChecker : public Checker<check::EndAnalysis> { 38c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Carepublic: 3930726c6baee1417307236e854f1474fdb3cedb98Argyrios Kyrtzidis void checkEndAnalysis(ExplodedGraph &G, BugReporter &B, 4030726c6baee1417307236e854f1474fdb3cedb98Argyrios Kyrtzidis ExprEngine &Eng) const; 41c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Careprivate: 4230726c6baee1417307236e854f1474fdb3cedb98Argyrios Kyrtzidis typedef llvm::SmallSet<unsigned, DEFAULT_CFGBLOCKS> CFGBlocksSet; 4330726c6baee1417307236e854f1474fdb3cedb98Argyrios Kyrtzidis 44f8906794f439643b688c2857f5543c9e2499f476Tom Care static inline const Stmt *getUnreachableStmt(const CFGBlock *CB); 4530726c6baee1417307236e854f1474fdb3cedb98Argyrios Kyrtzidis static void FindUnreachableEntryPoints(const CFGBlock *CB, 4630726c6baee1417307236e854f1474fdb3cedb98Argyrios Kyrtzidis CFGBlocksSet &reachable, 4730726c6baee1417307236e854f1474fdb3cedb98Argyrios Kyrtzidis CFGBlocksSet &visited); 487bce3a122296eba0e74f401c188e55c71935132fTom Care static bool isInvalidPath(const CFGBlock *CB, const ParentMap &PM); 49505a5065af704c63c2c747458e7d997dd31ae35bTom Care static inline bool isEmptyCFGBlock(const CFGBlock *CB); 50c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care}; 51c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care} 52c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care 5330726c6baee1417307236e854f1474fdb3cedb98Argyrios Kyrtzidisvoid UnreachableCodeChecker::checkEndAnalysis(ExplodedGraph &G, 54c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care BugReporter &B, 5530726c6baee1417307236e854f1474fdb3cedb98Argyrios Kyrtzidis ExprEngine &Eng) const { 5630726c6baee1417307236e854f1474fdb3cedb98Argyrios Kyrtzidis CFGBlocksSet reachable, visited; 5775df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek 58bc42c533e7d3d946704a49e242939dd232f33072Tom Care if (Eng.hasWorkRemaining()) 59c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care return; 60c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care 616bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines const Decl *D = nullptr; 626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines CFG *C = nullptr; 636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines ParentMap *PM = nullptr; 646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines const LocationContext *LC = nullptr; 65c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care // Iterate over ExplodedGraph 66505a5065af704c63c2c747458e7d997dd31ae35bTom Care for (ExplodedGraph::node_iterator I = G.nodes_begin(), E = G.nodes_end(); 67505a5065af704c63c2c747458e7d997dd31ae35bTom Care I != E; ++I) { 68c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care const ProgramPoint &P = I->getLocation(); 69590dd8e0959d8df5621827768987c4792b74fc06Anna Zaks LC = P.getLocationContext(); 7051718e3555404192040a5fad715367bc4cef22fbJordan Rose if (!LC->inTopFrame()) 7151718e3555404192040a5fad715367bc4cef22fbJordan Rose continue; 72c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care 7375df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek if (!D) 7475df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek D = LC->getAnalysisDeclContext()->getDecl(); 7551718e3555404192040a5fad715367bc4cef22fbJordan Rose 76c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care // Save the CFG if we don't have it already 77c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care if (!C) 781d26f48dc2eea1c07431ca1519d7034a21b9bcffTed Kremenek C = LC->getAnalysisDeclContext()->getUnoptimizedCFG(); 797bce3a122296eba0e74f401c188e55c71935132fTom Care if (!PM) 807bce3a122296eba0e74f401c188e55c71935132fTom Care PM = &LC->getParentMap(); 81c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care 827a95de68c093991047ed8d339479ccad51b88663David Blaikie if (Optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) { 83c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care const CFGBlock *CB = BE->getBlock(); 84f8906794f439643b688c2857f5543c9e2499f476Tom Care reachable.insert(CB->getBlockID()); 85c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care } 86c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care } 87c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care 887bce3a122296eba0e74f401c188e55c71935132fTom Care // Bail out if we didn't get the CFG or the ParentMap. 8975df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek if (!D || !C || !PM) 90c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care return; 9175df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek 9275df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek // Don't do anything for template instantiations. Proving that code 9375df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek // in a template instantiation is unreachable means proving that it is 9475df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek // unreachable in all instantiations. 9575df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) 9675df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek if (FD->isTemplateInstantiation()) 9775df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek return; 98c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care 99c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care // Find CFGBlocks that were not covered by any node 1004895b9cf34b26b20e674a88fa8104489e1d06812Tom Care for (CFG::const_iterator I = C->begin(), E = C->end(); I != E; ++I) { 101c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care const CFGBlock *CB = *I; 102c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care // Check if the block is unreachable 103f8906794f439643b688c2857f5543c9e2499f476Tom Care if (reachable.count(CB->getBlockID())) 1047bce3a122296eba0e74f401c188e55c71935132fTom Care continue; 1057bce3a122296eba0e74f401c188e55c71935132fTom Care 106505a5065af704c63c2c747458e7d997dd31ae35bTom Care // Check if the block is empty (an artificial block) 107505a5065af704c63c2c747458e7d997dd31ae35bTom Care if (isEmptyCFGBlock(CB)) 108505a5065af704c63c2c747458e7d997dd31ae35bTom Care continue; 109505a5065af704c63c2c747458e7d997dd31ae35bTom Care 1107bce3a122296eba0e74f401c188e55c71935132fTom Care // Find the entry points for this block 1114895b9cf34b26b20e674a88fa8104489e1d06812Tom Care if (!visited.count(CB->getBlockID())) 11230726c6baee1417307236e854f1474fdb3cedb98Argyrios Kyrtzidis FindUnreachableEntryPoints(CB, reachable, visited); 1137bce3a122296eba0e74f401c188e55c71935132fTom Care 1147bce3a122296eba0e74f401c188e55c71935132fTom Care // This block may have been pruned; check if we still want to report it 115f8906794f439643b688c2857f5543c9e2499f476Tom Care if (reachable.count(CB->getBlockID())) 1167bce3a122296eba0e74f401c188e55c71935132fTom Care continue; 1177bce3a122296eba0e74f401c188e55c71935132fTom Care 1187bce3a122296eba0e74f401c188e55c71935132fTom Care // Check for false positives 1197bce3a122296eba0e74f401c188e55c71935132fTom Care if (CB->size() > 0 && isInvalidPath(CB, *PM)) 1207bce3a122296eba0e74f401c188e55c71935132fTom Care continue; 1217bce3a122296eba0e74f401c188e55c71935132fTom Care 12228cd22d7c2d2458575ce9cc19dfe63c6321010ceTed Kremenek // It is good practice to always have a "default" label in a "switch", even 12328cd22d7c2d2458575ce9cc19dfe63c6321010ceTed Kremenek // if we should never get there. It can be used to detect errors, for 12428cd22d7c2d2458575ce9cc19dfe63c6321010ceTed Kremenek // instance. Unreachable code directly under a "default" label is therefore 12528cd22d7c2d2458575ce9cc19dfe63c6321010ceTed Kremenek // likely to be a false positive. 12628cd22d7c2d2458575ce9cc19dfe63c6321010ceTed Kremenek if (const Stmt *label = CB->getLabel()) 12728cd22d7c2d2458575ce9cc19dfe63c6321010ceTed Kremenek if (label->getStmtClass() == Stmt::DefaultStmtClass) 12828cd22d7c2d2458575ce9cc19dfe63c6321010ceTed Kremenek continue; 12928cd22d7c2d2458575ce9cc19dfe63c6321010ceTed Kremenek 1307bce3a122296eba0e74f401c188e55c71935132fTom Care // Special case for __builtin_unreachable. 1317bce3a122296eba0e74f401c188e55c71935132fTom Care // FIXME: This should be extended to include other unreachable markers, 1327bce3a122296eba0e74f401c188e55c71935132fTom Care // such as llvm_unreachable. 1337bce3a122296eba0e74f401c188e55c71935132fTom Care if (!CB->empty()) { 134882998923889a2fcce9b49696506c499e22cf38fTed Kremenek bool foundUnreachable = false; 135882998923889a2fcce9b49696506c499e22cf38fTed Kremenek for (CFGBlock::const_iterator ci = CB->begin(), ce = CB->end(); 136882998923889a2fcce9b49696506c499e22cf38fTed Kremenek ci != ce; ++ci) { 137b07805485c603be3d8011f72611465324c9e664bDavid Blaikie if (Optional<CFGStmt> S = (*ci).getAs<CFGStmt>()) 138b07805485c603be3d8011f72611465324c9e664bDavid Blaikie if (const CallExpr *CE = dyn_cast<CallExpr>(S->getStmt())) { 139651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (CE->getBuiltinCallee() == Builtin::BI__builtin_unreachable) { 140882998923889a2fcce9b49696506c499e22cf38fTed Kremenek foundUnreachable = true; 141882998923889a2fcce9b49696506c499e22cf38fTed Kremenek break; 142882998923889a2fcce9b49696506c499e22cf38fTed Kremenek } 143882998923889a2fcce9b49696506c499e22cf38fTed Kremenek } 1445e04bdde8e74d991feffe9cf95d731f7e473dbb7Jordy Rose } 145882998923889a2fcce9b49696506c499e22cf38fTed Kremenek if (foundUnreachable) 146882998923889a2fcce9b49696506c499e22cf38fTed Kremenek continue; 147c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care } 1487bce3a122296eba0e74f401c188e55c71935132fTom Care 149f8906794f439643b688c2857f5543c9e2499f476Tom Care // We found a block that wasn't covered - find the statement to report 150f8906794f439643b688c2857f5543c9e2499f476Tom Care SourceRange SR; 151590dd8e0959d8df5621827768987c4792b74fc06Anna Zaks PathDiagnosticLocation DL; 152f8906794f439643b688c2857f5543c9e2499f476Tom Care SourceLocation SL; 153f8906794f439643b688c2857f5543c9e2499f476Tom Care if (const Stmt *S = getUnreachableStmt(CB)) { 154f8906794f439643b688c2857f5543c9e2499f476Tom Care SR = S->getSourceRange(); 155590dd8e0959d8df5621827768987c4792b74fc06Anna Zaks DL = PathDiagnosticLocation::createBegin(S, B.getSourceManager(), LC); 156590dd8e0959d8df5621827768987c4792b74fc06Anna Zaks SL = DL.asLocation(); 157590dd8e0959d8df5621827768987c4792b74fc06Anna Zaks if (SR.isInvalid() || !SL.isValid()) 158f8906794f439643b688c2857f5543c9e2499f476Tom Care continue; 159f8906794f439643b688c2857f5543c9e2499f476Tom Care } 160f8906794f439643b688c2857f5543c9e2499f476Tom Care else 161f8906794f439643b688c2857f5543c9e2499f476Tom Care continue; 162f8906794f439643b688c2857f5543c9e2499f476Tom Care 163f8906794f439643b688c2857f5543c9e2499f476Tom Care // Check if the SourceLocation is in a system header 164f8906794f439643b688c2857f5543c9e2499f476Tom Care const SourceManager &SM = B.getSourceManager(); 165f8906794f439643b688c2857f5543c9e2499f476Tom Care if (SM.isInSystemHeader(SL) || SM.isInExternCSystemHeader(SL)) 166f8906794f439643b688c2857f5543c9e2499f476Tom Care continue; 167f8906794f439643b688c2857f5543c9e2499f476Tom Care 168651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines B.EmitBasicReport(D, this, "Unreachable code", "Dead code", 16907189521a15d9c088216b943649cb9fe231cbb57Ted Kremenek "This statement is never executed", DL, SR); 170c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care } 171c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care} 172c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care 173c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care// Recursively finds the entry point(s) for this dead CFGBlock. 17430726c6baee1417307236e854f1474fdb3cedb98Argyrios Kyrtzidisvoid UnreachableCodeChecker::FindUnreachableEntryPoints(const CFGBlock *CB, 17530726c6baee1417307236e854f1474fdb3cedb98Argyrios Kyrtzidis CFGBlocksSet &reachable, 17630726c6baee1417307236e854f1474fdb3cedb98Argyrios Kyrtzidis CFGBlocksSet &visited) { 177f8906794f439643b688c2857f5543c9e2499f476Tom Care visited.insert(CB->getBlockID()); 178c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care 1794895b9cf34b26b20e674a88fa8104489e1d06812Tom Care for (CFGBlock::const_pred_iterator I = CB->pred_begin(), E = CB->pred_end(); 1804895b9cf34b26b20e674a88fa8104489e1d06812Tom Care I != E; ++I) { 181651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (!*I) 182651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines continue; 183651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 1840600918d1418c2eac2c96491637946206009c4deTom Care if (!reachable.count((*I)->getBlockID())) { 1854895b9cf34b26b20e674a88fa8104489e1d06812Tom Care // If we find an unreachable predecessor, mark this block as reachable so 1864895b9cf34b26b20e674a88fa8104489e1d06812Tom Care // we don't report this block 1874895b9cf34b26b20e674a88fa8104489e1d06812Tom Care reachable.insert(CB->getBlockID()); 1880600918d1418c2eac2c96491637946206009c4deTom Care if (!visited.count((*I)->getBlockID())) 1894895b9cf34b26b20e674a88fa8104489e1d06812Tom Care // If we haven't previously visited the unreachable predecessor, recurse 19030726c6baee1417307236e854f1474fdb3cedb98Argyrios Kyrtzidis FindUnreachableEntryPoints(*I, reachable, visited); 191c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care } 192c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care } 193c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care} 194c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care 195f8906794f439643b688c2857f5543c9e2499f476Tom Care// Find the Stmt* in a CFGBlock for reporting a warning 196f8906794f439643b688c2857f5543c9e2499f476Tom Careconst Stmt *UnreachableCodeChecker::getUnreachableStmt(const CFGBlock *CB) { 197b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu for (CFGBlock::const_iterator I = CB->begin(), E = CB->end(); I != E; ++I) { 198b07805485c603be3d8011f72611465324c9e664bDavid Blaikie if (Optional<CFGStmt> S = I->getAs<CFGStmt>()) 199b07805485c603be3d8011f72611465324c9e664bDavid Blaikie return S->getStmt(); 200b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu } 201b36cd3e1757fb4fcd9509f35558c847b04bef35fZhongxing Xu if (const Stmt *S = CB->getTerminator()) 202f8906794f439643b688c2857f5543c9e2499f476Tom Care return S; 203c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care else 2046bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return nullptr; 205c4b5bd89e1ef611c7a31b767763030acc45274c8Tom Care} 2067bce3a122296eba0e74f401c188e55c71935132fTom Care 207f8906794f439643b688c2857f5543c9e2499f476Tom Care// Determines if the path to this CFGBlock contained an element that infers this 208f8906794f439643b688c2857f5543c9e2499f476Tom Care// block is a false positive. We assume that FindUnreachableEntryPoints has 209f8906794f439643b688c2857f5543c9e2499f476Tom Care// already marked only the entry points to any dead code, so we need only to 210f8906794f439643b688c2857f5543c9e2499f476Tom Care// find the condition that led to this block (the predecessor of this block.) 211f8906794f439643b688c2857f5543c9e2499f476Tom Care// There will never be more than one predecessor. 2127bce3a122296eba0e74f401c188e55c71935132fTom Carebool UnreachableCodeChecker::isInvalidPath(const CFGBlock *CB, 2137bce3a122296eba0e74f401c188e55c71935132fTom Care const ParentMap &PM) { 214aaca011f78ce4d1ce08d4320da1326d4ae31609aTom Care // We only expect a predecessor size of 0 or 1. If it is >1, then an external 215aaca011f78ce4d1ce08d4320da1326d4ae31609aTom Care // condition has broken our assumption (for example, a sink being placed by 216aaca011f78ce4d1ce08d4320da1326d4ae31609aTom Care // another check). In these cases, we choose not to report. 217aaca011f78ce4d1ce08d4320da1326d4ae31609aTom Care if (CB->pred_size() > 1) 218aaca011f78ce4d1ce08d4320da1326d4ae31609aTom Care return true; 2197bce3a122296eba0e74f401c188e55c71935132fTom Care 220f8906794f439643b688c2857f5543c9e2499f476Tom Care // If there are no predecessors, then this block is trivially unreachable 221f8906794f439643b688c2857f5543c9e2499f476Tom Care if (CB->pred_size() == 0) 222f8906794f439643b688c2857f5543c9e2499f476Tom Care return false; 223f8906794f439643b688c2857f5543c9e2499f476Tom Care 224f8906794f439643b688c2857f5543c9e2499f476Tom Care const CFGBlock *pred = *CB->pred_begin(); 225651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (!pred) 226651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines return false; 227f8906794f439643b688c2857f5543c9e2499f476Tom Care 228f8906794f439643b688c2857f5543c9e2499f476Tom Care // Get the predecessor block's terminator conditon 229f8906794f439643b688c2857f5543c9e2499f476Tom Care const Stmt *cond = pred->getTerminatorCondition(); 230505a5065af704c63c2c747458e7d997dd31ae35bTom Care 231505a5065af704c63c2c747458e7d997dd31ae35bTom Care //assert(cond && "CFGBlock's predecessor has a terminator condition"); 232505a5065af704c63c2c747458e7d997dd31ae35bTom Care // The previous assertion is invalid in some cases (eg do/while). Leaving 233505a5065af704c63c2c747458e7d997dd31ae35bTom Care // reporting of these situations on at the moment to help triage these cases. 234505a5065af704c63c2c747458e7d997dd31ae35bTom Care if (!cond) 235505a5065af704c63c2c747458e7d997dd31ae35bTom Care return false; 236f8906794f439643b688c2857f5543c9e2499f476Tom Care 237f8906794f439643b688c2857f5543c9e2499f476Tom Care // Run each of the checks on the conditions 238f8906794f439643b688c2857f5543c9e2499f476Tom Care if (containsMacro(cond) || containsEnum(cond) 239f8906794f439643b688c2857f5543c9e2499f476Tom Care || containsStaticLocal(cond) || containsBuiltinOffsetOf(cond) 240f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne || containsStmt<UnaryExprOrTypeTraitExpr>(cond)) 241f8906794f439643b688c2857f5543c9e2499f476Tom Care return true; 2427bce3a122296eba0e74f401c188e55c71935132fTom Care 2437bce3a122296eba0e74f401c188e55c71935132fTom Care return false; 2447bce3a122296eba0e74f401c188e55c71935132fTom Care} 245505a5065af704c63c2c747458e7d997dd31ae35bTom Care 246505a5065af704c63c2c747458e7d997dd31ae35bTom Care// Returns true if the given CFGBlock is empty 247505a5065af704c63c2c747458e7d997dd31ae35bTom Carebool UnreachableCodeChecker::isEmptyCFGBlock(const CFGBlock *CB) { 2486bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return CB->getLabel() == nullptr // No labels 249505a5065af704c63c2c747458e7d997dd31ae35bTom Care && CB->size() == 0 // No statements 250651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines && !CB->getTerminator(); // No terminator 251505a5065af704c63c2c747458e7d997dd31ae35bTom Care} 25230726c6baee1417307236e854f1474fdb3cedb98Argyrios Kyrtzidis 25330726c6baee1417307236e854f1474fdb3cedb98Argyrios Kyrtzidisvoid ento::registerUnreachableCodeChecker(CheckerManager &mgr) { 25430726c6baee1417307236e854f1474fdb3cedb98Argyrios Kyrtzidis mgr.registerChecker<UnreachableCodeChecker>(); 25530726c6baee1417307236e854f1474fdb3cedb98Argyrios Kyrtzidis} 256