clang API Documentation

UnreachableCodeChecker.cpp
Go to the documentation of this file.
00001 //==- UnreachableCodeChecker.cpp - Generalized dead code checker -*- C++ -*-==//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 // This file implements a generalized unreachable code checker using a
00010 // path-sensitive analysis. We mark any path visited, and then walk the CFG as a
00011 // post-analysis to determine what was never visited.
00012 //
00013 // A similar flow-sensitive only check exists in Analysis/ReachableCode.cpp
00014 //===----------------------------------------------------------------------===//
00015 
00016 #include "ClangSACheckers.h"
00017 #include "clang/AST/ParentMap.h"
00018 #include "clang/Basic/Builtins.h"
00019 #include "clang/Basic/SourceManager.h"
00020 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
00021 #include "clang/StaticAnalyzer/Core/Checker.h"
00022 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
00023 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
00024 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
00025 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
00026 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
00027 #include "llvm/ADT/SmallSet.h"
00028 
00029 // The number of CFGBlock pointers we want to reserve memory for. This is used
00030 // once for each function we analyze.
00031 #define DEFAULT_CFGBLOCKS 256
00032 
00033 using namespace clang;
00034 using namespace ento;
00035 
00036 namespace {
00037 class UnreachableCodeChecker : public Checker<check::EndAnalysis> {
00038 public:
00039   void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,
00040                         ExprEngine &Eng) const;
00041 private:
00042   typedef llvm::SmallSet<unsigned, DEFAULT_CFGBLOCKS> CFGBlocksSet;
00043 
00044   static inline const Stmt *getUnreachableStmt(const CFGBlock *CB);
00045   static void FindUnreachableEntryPoints(const CFGBlock *CB,
00046                                          CFGBlocksSet &reachable,
00047                                          CFGBlocksSet &visited);
00048   static bool isInvalidPath(const CFGBlock *CB, const ParentMap &PM);
00049   static inline bool isEmptyCFGBlock(const CFGBlock *CB);
00050 };
00051 }
00052 
00053 void UnreachableCodeChecker::checkEndAnalysis(ExplodedGraph &G,
00054                                               BugReporter &B,
00055                                               ExprEngine &Eng) const {
00056   CFGBlocksSet reachable, visited;
00057   
00058   if (Eng.hasWorkRemaining())
00059     return;
00060 
00061   const Decl *D = nullptr;
00062   CFG *C = nullptr;
00063   ParentMap *PM = nullptr;
00064   const LocationContext *LC = nullptr;
00065   // Iterate over ExplodedGraph
00066   for (ExplodedGraph::node_iterator I = G.nodes_begin(), E = G.nodes_end();
00067       I != E; ++I) {
00068     const ProgramPoint &P = I->getLocation();
00069     LC = P.getLocationContext();
00070     if (!LC->inTopFrame())
00071       continue;
00072 
00073     if (!D)
00074       D = LC->getAnalysisDeclContext()->getDecl();
00075 
00076     // Save the CFG if we don't have it already
00077     if (!C)
00078       C = LC->getAnalysisDeclContext()->getUnoptimizedCFG();
00079     if (!PM)
00080       PM = &LC->getParentMap();
00081 
00082     if (Optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) {
00083       const CFGBlock *CB = BE->getBlock();
00084       reachable.insert(CB->getBlockID());
00085     }
00086   }
00087 
00088   // Bail out if we didn't get the CFG or the ParentMap.
00089   if (!D || !C || !PM)
00090     return;
00091   
00092   // Don't do anything for template instantiations.  Proving that code
00093   // in a template instantiation is unreachable means proving that it is
00094   // unreachable in all instantiations.
00095   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
00096     if (FD->isTemplateInstantiation())
00097       return;
00098 
00099   // Find CFGBlocks that were not covered by any node
00100   for (CFG::const_iterator I = C->begin(), E = C->end(); I != E; ++I) {
00101     const CFGBlock *CB = *I;
00102     // Check if the block is unreachable
00103     if (reachable.count(CB->getBlockID()))
00104       continue;
00105 
00106     // Check if the block is empty (an artificial block)
00107     if (isEmptyCFGBlock(CB))
00108       continue;
00109 
00110     // Find the entry points for this block
00111     if (!visited.count(CB->getBlockID()))
00112       FindUnreachableEntryPoints(CB, reachable, visited);
00113 
00114     // This block may have been pruned; check if we still want to report it
00115     if (reachable.count(CB->getBlockID()))
00116       continue;
00117 
00118     // Check for false positives
00119     if (CB->size() > 0 && isInvalidPath(CB, *PM))
00120       continue;
00121 
00122     // It is good practice to always have a "default" label in a "switch", even
00123     // if we should never get there. It can be used to detect errors, for
00124     // instance. Unreachable code directly under a "default" label is therefore
00125     // likely to be a false positive.
00126     if (const Stmt *label = CB->getLabel())
00127       if (label->getStmtClass() == Stmt::DefaultStmtClass)
00128         continue;
00129 
00130     // Special case for __builtin_unreachable.
00131     // FIXME: This should be extended to include other unreachable markers,
00132     // such as llvm_unreachable.
00133     if (!CB->empty()) {
00134       bool foundUnreachable = false;
00135       for (CFGBlock::const_iterator ci = CB->begin(), ce = CB->end();
00136            ci != ce; ++ci) {
00137         if (Optional<CFGStmt> S = (*ci).getAs<CFGStmt>())
00138           if (const CallExpr *CE = dyn_cast<CallExpr>(S->getStmt())) {
00139             if (CE->getBuiltinCallee() == Builtin::BI__builtin_unreachable) {
00140               foundUnreachable = true;
00141               break;
00142             }
00143           }
00144       }
00145       if (foundUnreachable)
00146         continue;
00147     }
00148 
00149     // We found a block that wasn't covered - find the statement to report
00150     SourceRange SR;
00151     PathDiagnosticLocation DL;
00152     SourceLocation SL;
00153     if (const Stmt *S = getUnreachableStmt(CB)) {
00154       SR = S->getSourceRange();
00155       DL = PathDiagnosticLocation::createBegin(S, B.getSourceManager(), LC);
00156       SL = DL.asLocation();
00157       if (SR.isInvalid() || !SL.isValid())
00158         continue;
00159     }
00160     else
00161       continue;
00162 
00163     // Check if the SourceLocation is in a system header
00164     const SourceManager &SM = B.getSourceManager();
00165     if (SM.isInSystemHeader(SL) || SM.isInExternCSystemHeader(SL))
00166       continue;
00167 
00168     B.EmitBasicReport(D, this, "Unreachable code", "Dead code",
00169                       "This statement is never executed", DL, SR);
00170   }
00171 }
00172 
00173 // Recursively finds the entry point(s) for this dead CFGBlock.
00174 void UnreachableCodeChecker::FindUnreachableEntryPoints(const CFGBlock *CB,
00175                                                         CFGBlocksSet &reachable,
00176                                                         CFGBlocksSet &visited) {
00177   visited.insert(CB->getBlockID());
00178 
00179   for (CFGBlock::const_pred_iterator I = CB->pred_begin(), E = CB->pred_end();
00180       I != E; ++I) {
00181     if (!*I)
00182       continue;
00183 
00184     if (!reachable.count((*I)->getBlockID())) {
00185       // If we find an unreachable predecessor, mark this block as reachable so
00186       // we don't report this block
00187       reachable.insert(CB->getBlockID());
00188       if (!visited.count((*I)->getBlockID()))
00189         // If we haven't previously visited the unreachable predecessor, recurse
00190         FindUnreachableEntryPoints(*I, reachable, visited);
00191     }
00192   }
00193 }
00194 
00195 // Find the Stmt* in a CFGBlock for reporting a warning
00196 const Stmt *UnreachableCodeChecker::getUnreachableStmt(const CFGBlock *CB) {
00197   for (CFGBlock::const_iterator I = CB->begin(), E = CB->end(); I != E; ++I) {
00198     if (Optional<CFGStmt> S = I->getAs<CFGStmt>())
00199       return S->getStmt();
00200   }
00201   if (const Stmt *S = CB->getTerminator())
00202     return S;
00203   else
00204     return nullptr;
00205 }
00206 
00207 // Determines if the path to this CFGBlock contained an element that infers this
00208 // block is a false positive. We assume that FindUnreachableEntryPoints has
00209 // already marked only the entry points to any dead code, so we need only to
00210 // find the condition that led to this block (the predecessor of this block.)
00211 // There will never be more than one predecessor.
00212 bool UnreachableCodeChecker::isInvalidPath(const CFGBlock *CB,
00213                                            const ParentMap &PM) {
00214   // We only expect a predecessor size of 0 or 1. If it is >1, then an external
00215   // condition has broken our assumption (for example, a sink being placed by
00216   // another check). In these cases, we choose not to report.
00217   if (CB->pred_size() > 1)
00218     return true;
00219 
00220   // If there are no predecessors, then this block is trivially unreachable
00221   if (CB->pred_size() == 0)
00222     return false;
00223 
00224   const CFGBlock *pred = *CB->pred_begin();
00225   if (!pred)
00226     return false;
00227 
00228   // Get the predecessor block's terminator conditon
00229   const Stmt *cond = pred->getTerminatorCondition();
00230 
00231   //assert(cond && "CFGBlock's predecessor has a terminator condition");
00232   // The previous assertion is invalid in some cases (eg do/while). Leaving
00233   // reporting of these situations on at the moment to help triage these cases.
00234   if (!cond)
00235     return false;
00236 
00237   // Run each of the checks on the conditions
00238   if (containsMacro(cond) || containsEnum(cond)
00239       || containsStaticLocal(cond) || containsBuiltinOffsetOf(cond)
00240       || containsStmt<UnaryExprOrTypeTraitExpr>(cond))
00241     return true;
00242 
00243   return false;
00244 }
00245 
00246 // Returns true if the given CFGBlock is empty
00247 bool UnreachableCodeChecker::isEmptyCFGBlock(const CFGBlock *CB) {
00248   return CB->getLabel() == nullptr // No labels
00249       && CB->size() == 0           // No statements
00250       && !CB->getTerminator();     // No terminator
00251 }
00252 
00253 void ento::registerUnreachableCodeChecker(CheckerManager &mgr) {
00254   mgr.registerChecker<UnreachableCodeChecker>();
00255 }