1f4a2713aSLionel Sambuc //=- ReachableCodePathInsensitive.cpp ---------------------------*- C++ --*-==//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements a flow-sensitive, path-insensitive analysis of
11f4a2713aSLionel Sambuc // determining reachable blocks within a CFG.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc
15f4a2713aSLionel Sambuc #include "clang/Analysis/Analyses/ReachableCode.h"
16f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
17f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
18f4a2713aSLionel Sambuc #include "clang/AST/ExprObjC.h"
19*0a6a1f1dSLionel Sambuc #include "clang/AST/ParentMap.h"
20f4a2713aSLionel Sambuc #include "clang/AST/StmtCXX.h"
21f4a2713aSLionel Sambuc #include "clang/Analysis/AnalysisContext.h"
22f4a2713aSLionel Sambuc #include "clang/Analysis/CFG.h"
23f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
24*0a6a1f1dSLionel Sambuc #include "clang/Lex/Preprocessor.h"
25f4a2713aSLionel Sambuc #include "llvm/ADT/BitVector.h"
26f4a2713aSLionel Sambuc #include "llvm/ADT/SmallVector.h"
27f4a2713aSLionel Sambuc
28f4a2713aSLionel Sambuc using namespace clang;
29f4a2713aSLionel Sambuc
30*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
31*0a6a1f1dSLionel Sambuc // Core Reachability Analysis routines.
32*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
33*0a6a1f1dSLionel Sambuc
isEnumConstant(const Expr * Ex)34*0a6a1f1dSLionel Sambuc static bool isEnumConstant(const Expr *Ex) {
35*0a6a1f1dSLionel Sambuc const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex);
36*0a6a1f1dSLionel Sambuc if (!DR)
37*0a6a1f1dSLionel Sambuc return false;
38*0a6a1f1dSLionel Sambuc return isa<EnumConstantDecl>(DR->getDecl());
39*0a6a1f1dSLionel Sambuc }
40*0a6a1f1dSLionel Sambuc
isTrivialExpression(const Expr * Ex)41*0a6a1f1dSLionel Sambuc static bool isTrivialExpression(const Expr *Ex) {
42*0a6a1f1dSLionel Sambuc Ex = Ex->IgnoreParenCasts();
43*0a6a1f1dSLionel Sambuc return isa<IntegerLiteral>(Ex) || isa<StringLiteral>(Ex) ||
44*0a6a1f1dSLionel Sambuc isa<CXXBoolLiteralExpr>(Ex) || isa<ObjCBoolLiteralExpr>(Ex) ||
45*0a6a1f1dSLionel Sambuc isa<CharacterLiteral>(Ex) ||
46*0a6a1f1dSLionel Sambuc isEnumConstant(Ex);
47*0a6a1f1dSLionel Sambuc }
48*0a6a1f1dSLionel Sambuc
isTrivialDoWhile(const CFGBlock * B,const Stmt * S)49*0a6a1f1dSLionel Sambuc static bool isTrivialDoWhile(const CFGBlock *B, const Stmt *S) {
50*0a6a1f1dSLionel Sambuc // Check if the block ends with a do...while() and see if 'S' is the
51*0a6a1f1dSLionel Sambuc // condition.
52*0a6a1f1dSLionel Sambuc if (const Stmt *Term = B->getTerminator()) {
53*0a6a1f1dSLionel Sambuc if (const DoStmt *DS = dyn_cast<DoStmt>(Term)) {
54*0a6a1f1dSLionel Sambuc const Expr *Cond = DS->getCond()->IgnoreParenCasts();
55*0a6a1f1dSLionel Sambuc return Cond == S && isTrivialExpression(Cond);
56*0a6a1f1dSLionel Sambuc }
57*0a6a1f1dSLionel Sambuc }
58*0a6a1f1dSLionel Sambuc return false;
59*0a6a1f1dSLionel Sambuc }
60*0a6a1f1dSLionel Sambuc
isDeadReturn(const CFGBlock * B,const Stmt * S)61*0a6a1f1dSLionel Sambuc static bool isDeadReturn(const CFGBlock *B, const Stmt *S) {
62*0a6a1f1dSLionel Sambuc // Look to see if the current control flow ends with a 'return', and see if
63*0a6a1f1dSLionel Sambuc // 'S' is a substatement. The 'return' may not be the last element in the
64*0a6a1f1dSLionel Sambuc // block, or may be in a subsequent block because of destructors.
65*0a6a1f1dSLionel Sambuc const CFGBlock *Current = B;
66*0a6a1f1dSLionel Sambuc while (true) {
67*0a6a1f1dSLionel Sambuc for (CFGBlock::const_reverse_iterator I = Current->rbegin(),
68*0a6a1f1dSLionel Sambuc E = Current->rend();
69*0a6a1f1dSLionel Sambuc I != E; ++I) {
70*0a6a1f1dSLionel Sambuc if (Optional<CFGStmt> CS = I->getAs<CFGStmt>()) {
71*0a6a1f1dSLionel Sambuc if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(CS->getStmt())) {
72*0a6a1f1dSLionel Sambuc if (RS == S)
73*0a6a1f1dSLionel Sambuc return true;
74*0a6a1f1dSLionel Sambuc if (const Expr *RE = RS->getRetValue()) {
75*0a6a1f1dSLionel Sambuc RE = RE->IgnoreParenCasts();
76*0a6a1f1dSLionel Sambuc if (RE == S)
77*0a6a1f1dSLionel Sambuc return true;
78*0a6a1f1dSLionel Sambuc ParentMap PM(const_cast<Expr *>(RE));
79*0a6a1f1dSLionel Sambuc // If 'S' is in the ParentMap, it is a subexpression of
80*0a6a1f1dSLionel Sambuc // the return statement.
81*0a6a1f1dSLionel Sambuc return PM.getParent(S);
82*0a6a1f1dSLionel Sambuc }
83*0a6a1f1dSLionel Sambuc }
84*0a6a1f1dSLionel Sambuc break;
85*0a6a1f1dSLionel Sambuc }
86*0a6a1f1dSLionel Sambuc }
87*0a6a1f1dSLionel Sambuc // Note also that we are restricting the search for the return statement
88*0a6a1f1dSLionel Sambuc // to stop at control-flow; only part of a return statement may be dead,
89*0a6a1f1dSLionel Sambuc // without the whole return statement being dead.
90*0a6a1f1dSLionel Sambuc if (Current->getTerminator().isTemporaryDtorsBranch()) {
91*0a6a1f1dSLionel Sambuc // Temporary destructors have a predictable control flow, thus we want to
92*0a6a1f1dSLionel Sambuc // look into the next block for the return statement.
93*0a6a1f1dSLionel Sambuc // We look into the false branch, as we know the true branch only contains
94*0a6a1f1dSLionel Sambuc // the call to the destructor.
95*0a6a1f1dSLionel Sambuc assert(Current->succ_size() == 2);
96*0a6a1f1dSLionel Sambuc Current = *(Current->succ_begin() + 1);
97*0a6a1f1dSLionel Sambuc } else if (!Current->getTerminator() && Current->succ_size() == 1) {
98*0a6a1f1dSLionel Sambuc // If there is only one successor, we're not dealing with outgoing control
99*0a6a1f1dSLionel Sambuc // flow. Thus, look into the next block.
100*0a6a1f1dSLionel Sambuc Current = *Current->succ_begin();
101*0a6a1f1dSLionel Sambuc if (Current->pred_size() > 1) {
102*0a6a1f1dSLionel Sambuc // If there is more than one predecessor, we're dealing with incoming
103*0a6a1f1dSLionel Sambuc // control flow - if the return statement is in that block, it might
104*0a6a1f1dSLionel Sambuc // well be reachable via a different control flow, thus it's not dead.
105*0a6a1f1dSLionel Sambuc return false;
106*0a6a1f1dSLionel Sambuc }
107*0a6a1f1dSLionel Sambuc } else {
108*0a6a1f1dSLionel Sambuc // We hit control flow or a dead end. Stop searching.
109*0a6a1f1dSLionel Sambuc return false;
110*0a6a1f1dSLionel Sambuc }
111*0a6a1f1dSLionel Sambuc }
112*0a6a1f1dSLionel Sambuc llvm_unreachable("Broke out of infinite loop.");
113*0a6a1f1dSLionel Sambuc }
114*0a6a1f1dSLionel Sambuc
getTopMostMacro(SourceLocation Loc,SourceManager & SM)115*0a6a1f1dSLionel Sambuc static SourceLocation getTopMostMacro(SourceLocation Loc, SourceManager &SM) {
116*0a6a1f1dSLionel Sambuc assert(Loc.isMacroID());
117*0a6a1f1dSLionel Sambuc SourceLocation Last;
118*0a6a1f1dSLionel Sambuc while (Loc.isMacroID()) {
119*0a6a1f1dSLionel Sambuc Last = Loc;
120*0a6a1f1dSLionel Sambuc Loc = SM.getImmediateMacroCallerLoc(Loc);
121*0a6a1f1dSLionel Sambuc }
122*0a6a1f1dSLionel Sambuc return Last;
123*0a6a1f1dSLionel Sambuc }
124*0a6a1f1dSLionel Sambuc
125*0a6a1f1dSLionel Sambuc /// Returns true if the statement is expanded from a configuration macro.
isExpandedFromConfigurationMacro(const Stmt * S,Preprocessor & PP,bool IgnoreYES_NO=false)126*0a6a1f1dSLionel Sambuc static bool isExpandedFromConfigurationMacro(const Stmt *S,
127*0a6a1f1dSLionel Sambuc Preprocessor &PP,
128*0a6a1f1dSLionel Sambuc bool IgnoreYES_NO = false) {
129*0a6a1f1dSLionel Sambuc // FIXME: This is not very precise. Here we just check to see if the
130*0a6a1f1dSLionel Sambuc // value comes from a macro, but we can do much better. This is likely
131*0a6a1f1dSLionel Sambuc // to be over conservative. This logic is factored into a separate function
132*0a6a1f1dSLionel Sambuc // so that we can refine it later.
133*0a6a1f1dSLionel Sambuc SourceLocation L = S->getLocStart();
134*0a6a1f1dSLionel Sambuc if (L.isMacroID()) {
135*0a6a1f1dSLionel Sambuc if (IgnoreYES_NO) {
136*0a6a1f1dSLionel Sambuc // The Objective-C constant 'YES' and 'NO'
137*0a6a1f1dSLionel Sambuc // are defined as macros. Do not treat them
138*0a6a1f1dSLionel Sambuc // as configuration values.
139*0a6a1f1dSLionel Sambuc SourceManager &SM = PP.getSourceManager();
140*0a6a1f1dSLionel Sambuc SourceLocation TopL = getTopMostMacro(L, SM);
141*0a6a1f1dSLionel Sambuc StringRef MacroName = PP.getImmediateMacroName(TopL);
142*0a6a1f1dSLionel Sambuc if (MacroName == "YES" || MacroName == "NO")
143*0a6a1f1dSLionel Sambuc return false;
144*0a6a1f1dSLionel Sambuc }
145*0a6a1f1dSLionel Sambuc return true;
146*0a6a1f1dSLionel Sambuc }
147*0a6a1f1dSLionel Sambuc return false;
148*0a6a1f1dSLionel Sambuc }
149*0a6a1f1dSLionel Sambuc
150*0a6a1f1dSLionel Sambuc static bool isConfigurationValue(const ValueDecl *D, Preprocessor &PP);
151*0a6a1f1dSLionel Sambuc
152*0a6a1f1dSLionel Sambuc /// Returns true if the statement represents a configuration value.
153*0a6a1f1dSLionel Sambuc ///
154*0a6a1f1dSLionel Sambuc /// A configuration value is something usually determined at compile-time
155*0a6a1f1dSLionel Sambuc /// to conditionally always execute some branch. Such guards are for
156*0a6a1f1dSLionel Sambuc /// "sometimes unreachable" code. Such code is usually not interesting
157*0a6a1f1dSLionel Sambuc /// to report as unreachable, and may mask truly unreachable code within
158*0a6a1f1dSLionel Sambuc /// those blocks.
isConfigurationValue(const Stmt * S,Preprocessor & PP,SourceRange * SilenceableCondVal=nullptr,bool IncludeIntegers=true,bool WrappedInParens=false)159*0a6a1f1dSLionel Sambuc static bool isConfigurationValue(const Stmt *S,
160*0a6a1f1dSLionel Sambuc Preprocessor &PP,
161*0a6a1f1dSLionel Sambuc SourceRange *SilenceableCondVal = nullptr,
162*0a6a1f1dSLionel Sambuc bool IncludeIntegers = true,
163*0a6a1f1dSLionel Sambuc bool WrappedInParens = false) {
164*0a6a1f1dSLionel Sambuc if (!S)
165*0a6a1f1dSLionel Sambuc return false;
166*0a6a1f1dSLionel Sambuc
167*0a6a1f1dSLionel Sambuc if (const Expr *Ex = dyn_cast<Expr>(S))
168*0a6a1f1dSLionel Sambuc S = Ex->IgnoreCasts();
169*0a6a1f1dSLionel Sambuc
170*0a6a1f1dSLionel Sambuc // Special case looking for the sigil '()' around an integer literal.
171*0a6a1f1dSLionel Sambuc if (const ParenExpr *PE = dyn_cast<ParenExpr>(S))
172*0a6a1f1dSLionel Sambuc if (!PE->getLocStart().isMacroID())
173*0a6a1f1dSLionel Sambuc return isConfigurationValue(PE->getSubExpr(), PP, SilenceableCondVal,
174*0a6a1f1dSLionel Sambuc IncludeIntegers, true);
175*0a6a1f1dSLionel Sambuc
176*0a6a1f1dSLionel Sambuc if (const Expr *Ex = dyn_cast<Expr>(S))
177*0a6a1f1dSLionel Sambuc S = Ex->IgnoreCasts();
178*0a6a1f1dSLionel Sambuc
179*0a6a1f1dSLionel Sambuc bool IgnoreYES_NO = false;
180*0a6a1f1dSLionel Sambuc
181*0a6a1f1dSLionel Sambuc switch (S->getStmtClass()) {
182*0a6a1f1dSLionel Sambuc case Stmt::CallExprClass: {
183*0a6a1f1dSLionel Sambuc const FunctionDecl *Callee =
184*0a6a1f1dSLionel Sambuc dyn_cast_or_null<FunctionDecl>(cast<CallExpr>(S)->getCalleeDecl());
185*0a6a1f1dSLionel Sambuc return Callee ? Callee->isConstexpr() : false;
186*0a6a1f1dSLionel Sambuc }
187*0a6a1f1dSLionel Sambuc case Stmt::DeclRefExprClass:
188*0a6a1f1dSLionel Sambuc return isConfigurationValue(cast<DeclRefExpr>(S)->getDecl(), PP);
189*0a6a1f1dSLionel Sambuc case Stmt::ObjCBoolLiteralExprClass:
190*0a6a1f1dSLionel Sambuc IgnoreYES_NO = true;
191*0a6a1f1dSLionel Sambuc // Fallthrough.
192*0a6a1f1dSLionel Sambuc case Stmt::CXXBoolLiteralExprClass:
193*0a6a1f1dSLionel Sambuc case Stmt::IntegerLiteralClass: {
194*0a6a1f1dSLionel Sambuc const Expr *E = cast<Expr>(S);
195*0a6a1f1dSLionel Sambuc if (IncludeIntegers) {
196*0a6a1f1dSLionel Sambuc if (SilenceableCondVal && !SilenceableCondVal->getBegin().isValid())
197*0a6a1f1dSLionel Sambuc *SilenceableCondVal = E->getSourceRange();
198*0a6a1f1dSLionel Sambuc return WrappedInParens || isExpandedFromConfigurationMacro(E, PP, IgnoreYES_NO);
199*0a6a1f1dSLionel Sambuc }
200*0a6a1f1dSLionel Sambuc return false;
201*0a6a1f1dSLionel Sambuc }
202*0a6a1f1dSLionel Sambuc case Stmt::MemberExprClass:
203*0a6a1f1dSLionel Sambuc return isConfigurationValue(cast<MemberExpr>(S)->getMemberDecl(), PP);
204*0a6a1f1dSLionel Sambuc case Stmt::UnaryExprOrTypeTraitExprClass:
205*0a6a1f1dSLionel Sambuc return true;
206*0a6a1f1dSLionel Sambuc case Stmt::BinaryOperatorClass: {
207*0a6a1f1dSLionel Sambuc const BinaryOperator *B = cast<BinaryOperator>(S);
208*0a6a1f1dSLionel Sambuc // Only include raw integers (not enums) as configuration
209*0a6a1f1dSLionel Sambuc // values if they are used in a logical or comparison operator
210*0a6a1f1dSLionel Sambuc // (not arithmetic).
211*0a6a1f1dSLionel Sambuc IncludeIntegers &= (B->isLogicalOp() || B->isComparisonOp());
212*0a6a1f1dSLionel Sambuc return isConfigurationValue(B->getLHS(), PP, SilenceableCondVal,
213*0a6a1f1dSLionel Sambuc IncludeIntegers) ||
214*0a6a1f1dSLionel Sambuc isConfigurationValue(B->getRHS(), PP, SilenceableCondVal,
215*0a6a1f1dSLionel Sambuc IncludeIntegers);
216*0a6a1f1dSLionel Sambuc }
217*0a6a1f1dSLionel Sambuc case Stmt::UnaryOperatorClass: {
218*0a6a1f1dSLionel Sambuc const UnaryOperator *UO = cast<UnaryOperator>(S);
219*0a6a1f1dSLionel Sambuc if (SilenceableCondVal)
220*0a6a1f1dSLionel Sambuc *SilenceableCondVal = UO->getSourceRange();
221*0a6a1f1dSLionel Sambuc return UO->getOpcode() == UO_LNot &&
222*0a6a1f1dSLionel Sambuc isConfigurationValue(UO->getSubExpr(), PP, SilenceableCondVal,
223*0a6a1f1dSLionel Sambuc IncludeIntegers, WrappedInParens);
224*0a6a1f1dSLionel Sambuc }
225*0a6a1f1dSLionel Sambuc default:
226*0a6a1f1dSLionel Sambuc return false;
227*0a6a1f1dSLionel Sambuc }
228*0a6a1f1dSLionel Sambuc }
229*0a6a1f1dSLionel Sambuc
isConfigurationValue(const ValueDecl * D,Preprocessor & PP)230*0a6a1f1dSLionel Sambuc static bool isConfigurationValue(const ValueDecl *D, Preprocessor &PP) {
231*0a6a1f1dSLionel Sambuc if (const EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D))
232*0a6a1f1dSLionel Sambuc return isConfigurationValue(ED->getInitExpr(), PP);
233*0a6a1f1dSLionel Sambuc if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
234*0a6a1f1dSLionel Sambuc // As a heuristic, treat globals as configuration values. Note
235*0a6a1f1dSLionel Sambuc // that we only will get here if Sema evaluated this
236*0a6a1f1dSLionel Sambuc // condition to a constant expression, which means the global
237*0a6a1f1dSLionel Sambuc // had to be declared in a way to be a truly constant value.
238*0a6a1f1dSLionel Sambuc // We could generalize this to local variables, but it isn't
239*0a6a1f1dSLionel Sambuc // clear if those truly represent configuration values that
240*0a6a1f1dSLionel Sambuc // gate unreachable code.
241*0a6a1f1dSLionel Sambuc if (!VD->hasLocalStorage())
242*0a6a1f1dSLionel Sambuc return true;
243*0a6a1f1dSLionel Sambuc
244*0a6a1f1dSLionel Sambuc // As a heuristic, locals that have been marked 'const' explicitly
245*0a6a1f1dSLionel Sambuc // can be treated as configuration values as well.
246*0a6a1f1dSLionel Sambuc return VD->getType().isLocalConstQualified();
247*0a6a1f1dSLionel Sambuc }
248*0a6a1f1dSLionel Sambuc return false;
249*0a6a1f1dSLionel Sambuc }
250*0a6a1f1dSLionel Sambuc
251*0a6a1f1dSLionel Sambuc /// Returns true if we should always explore all successors of a block.
shouldTreatSuccessorsAsReachable(const CFGBlock * B,Preprocessor & PP)252*0a6a1f1dSLionel Sambuc static bool shouldTreatSuccessorsAsReachable(const CFGBlock *B,
253*0a6a1f1dSLionel Sambuc Preprocessor &PP) {
254*0a6a1f1dSLionel Sambuc if (const Stmt *Term = B->getTerminator()) {
255*0a6a1f1dSLionel Sambuc if (isa<SwitchStmt>(Term))
256*0a6a1f1dSLionel Sambuc return true;
257*0a6a1f1dSLionel Sambuc // Specially handle '||' and '&&'.
258*0a6a1f1dSLionel Sambuc if (isa<BinaryOperator>(Term)) {
259*0a6a1f1dSLionel Sambuc return isConfigurationValue(Term, PP);
260*0a6a1f1dSLionel Sambuc }
261*0a6a1f1dSLionel Sambuc }
262*0a6a1f1dSLionel Sambuc
263*0a6a1f1dSLionel Sambuc const Stmt *Cond = B->getTerminatorCondition(/* stripParens */ false);
264*0a6a1f1dSLionel Sambuc return isConfigurationValue(Cond, PP);
265*0a6a1f1dSLionel Sambuc }
266*0a6a1f1dSLionel Sambuc
scanFromBlock(const CFGBlock * Start,llvm::BitVector & Reachable,Preprocessor * PP,bool IncludeSometimesUnreachableEdges)267*0a6a1f1dSLionel Sambuc static unsigned scanFromBlock(const CFGBlock *Start,
268*0a6a1f1dSLionel Sambuc llvm::BitVector &Reachable,
269*0a6a1f1dSLionel Sambuc Preprocessor *PP,
270*0a6a1f1dSLionel Sambuc bool IncludeSometimesUnreachableEdges) {
271*0a6a1f1dSLionel Sambuc unsigned count = 0;
272*0a6a1f1dSLionel Sambuc
273*0a6a1f1dSLionel Sambuc // Prep work queue
274*0a6a1f1dSLionel Sambuc SmallVector<const CFGBlock*, 32> WL;
275*0a6a1f1dSLionel Sambuc
276*0a6a1f1dSLionel Sambuc // The entry block may have already been marked reachable
277*0a6a1f1dSLionel Sambuc // by the caller.
278*0a6a1f1dSLionel Sambuc if (!Reachable[Start->getBlockID()]) {
279*0a6a1f1dSLionel Sambuc ++count;
280*0a6a1f1dSLionel Sambuc Reachable[Start->getBlockID()] = true;
281*0a6a1f1dSLionel Sambuc }
282*0a6a1f1dSLionel Sambuc
283*0a6a1f1dSLionel Sambuc WL.push_back(Start);
284*0a6a1f1dSLionel Sambuc
285*0a6a1f1dSLionel Sambuc // Find the reachable blocks from 'Start'.
286*0a6a1f1dSLionel Sambuc while (!WL.empty()) {
287*0a6a1f1dSLionel Sambuc const CFGBlock *item = WL.pop_back_val();
288*0a6a1f1dSLionel Sambuc
289*0a6a1f1dSLionel Sambuc // There are cases where we want to treat all successors as reachable.
290*0a6a1f1dSLionel Sambuc // The idea is that some "sometimes unreachable" code is not interesting,
291*0a6a1f1dSLionel Sambuc // and that we should forge ahead and explore those branches anyway.
292*0a6a1f1dSLionel Sambuc // This allows us to potentially uncover some "always unreachable" code
293*0a6a1f1dSLionel Sambuc // within the "sometimes unreachable" code.
294*0a6a1f1dSLionel Sambuc // Look at the successors and mark then reachable.
295*0a6a1f1dSLionel Sambuc Optional<bool> TreatAllSuccessorsAsReachable;
296*0a6a1f1dSLionel Sambuc if (!IncludeSometimesUnreachableEdges)
297*0a6a1f1dSLionel Sambuc TreatAllSuccessorsAsReachable = false;
298*0a6a1f1dSLionel Sambuc
299*0a6a1f1dSLionel Sambuc for (CFGBlock::const_succ_iterator I = item->succ_begin(),
300*0a6a1f1dSLionel Sambuc E = item->succ_end(); I != E; ++I) {
301*0a6a1f1dSLionel Sambuc const CFGBlock *B = *I;
302*0a6a1f1dSLionel Sambuc if (!B) do {
303*0a6a1f1dSLionel Sambuc const CFGBlock *UB = I->getPossiblyUnreachableBlock();
304*0a6a1f1dSLionel Sambuc if (!UB)
305*0a6a1f1dSLionel Sambuc break;
306*0a6a1f1dSLionel Sambuc
307*0a6a1f1dSLionel Sambuc if (!TreatAllSuccessorsAsReachable.hasValue()) {
308*0a6a1f1dSLionel Sambuc assert(PP);
309*0a6a1f1dSLionel Sambuc TreatAllSuccessorsAsReachable =
310*0a6a1f1dSLionel Sambuc shouldTreatSuccessorsAsReachable(item, *PP);
311*0a6a1f1dSLionel Sambuc }
312*0a6a1f1dSLionel Sambuc
313*0a6a1f1dSLionel Sambuc if (TreatAllSuccessorsAsReachable.getValue()) {
314*0a6a1f1dSLionel Sambuc B = UB;
315*0a6a1f1dSLionel Sambuc break;
316*0a6a1f1dSLionel Sambuc }
317*0a6a1f1dSLionel Sambuc }
318*0a6a1f1dSLionel Sambuc while (false);
319*0a6a1f1dSLionel Sambuc
320*0a6a1f1dSLionel Sambuc if (B) {
321*0a6a1f1dSLionel Sambuc unsigned blockID = B->getBlockID();
322*0a6a1f1dSLionel Sambuc if (!Reachable[blockID]) {
323*0a6a1f1dSLionel Sambuc Reachable.set(blockID);
324*0a6a1f1dSLionel Sambuc WL.push_back(B);
325*0a6a1f1dSLionel Sambuc ++count;
326*0a6a1f1dSLionel Sambuc }
327*0a6a1f1dSLionel Sambuc }
328*0a6a1f1dSLionel Sambuc }
329*0a6a1f1dSLionel Sambuc }
330*0a6a1f1dSLionel Sambuc return count;
331*0a6a1f1dSLionel Sambuc }
332*0a6a1f1dSLionel Sambuc
scanMaybeReachableFromBlock(const CFGBlock * Start,Preprocessor & PP,llvm::BitVector & Reachable)333*0a6a1f1dSLionel Sambuc static unsigned scanMaybeReachableFromBlock(const CFGBlock *Start,
334*0a6a1f1dSLionel Sambuc Preprocessor &PP,
335*0a6a1f1dSLionel Sambuc llvm::BitVector &Reachable) {
336*0a6a1f1dSLionel Sambuc return scanFromBlock(Start, Reachable, &PP, true);
337*0a6a1f1dSLionel Sambuc }
338*0a6a1f1dSLionel Sambuc
339*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
340*0a6a1f1dSLionel Sambuc // Dead Code Scanner.
341*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
342*0a6a1f1dSLionel Sambuc
343f4a2713aSLionel Sambuc namespace {
344f4a2713aSLionel Sambuc class DeadCodeScan {
345f4a2713aSLionel Sambuc llvm::BitVector Visited;
346f4a2713aSLionel Sambuc llvm::BitVector &Reachable;
347f4a2713aSLionel Sambuc SmallVector<const CFGBlock *, 10> WorkList;
348*0a6a1f1dSLionel Sambuc Preprocessor &PP;
349f4a2713aSLionel Sambuc
350f4a2713aSLionel Sambuc typedef SmallVector<std::pair<const CFGBlock *, const Stmt *>, 12>
351f4a2713aSLionel Sambuc DeferredLocsTy;
352f4a2713aSLionel Sambuc
353f4a2713aSLionel Sambuc DeferredLocsTy DeferredLocs;
354f4a2713aSLionel Sambuc
355f4a2713aSLionel Sambuc public:
DeadCodeScan(llvm::BitVector & reachable,Preprocessor & PP)356*0a6a1f1dSLionel Sambuc DeadCodeScan(llvm::BitVector &reachable, Preprocessor &PP)
357f4a2713aSLionel Sambuc : Visited(reachable.size()),
358*0a6a1f1dSLionel Sambuc Reachable(reachable),
359*0a6a1f1dSLionel Sambuc PP(PP) {}
360f4a2713aSLionel Sambuc
361f4a2713aSLionel Sambuc void enqueue(const CFGBlock *block);
362f4a2713aSLionel Sambuc unsigned scanBackwards(const CFGBlock *Start,
363f4a2713aSLionel Sambuc clang::reachable_code::Callback &CB);
364f4a2713aSLionel Sambuc
365f4a2713aSLionel Sambuc bool isDeadCodeRoot(const CFGBlock *Block);
366f4a2713aSLionel Sambuc
367f4a2713aSLionel Sambuc const Stmt *findDeadCode(const CFGBlock *Block);
368f4a2713aSLionel Sambuc
369*0a6a1f1dSLionel Sambuc void reportDeadCode(const CFGBlock *B,
370*0a6a1f1dSLionel Sambuc const Stmt *S,
371f4a2713aSLionel Sambuc clang::reachable_code::Callback &CB);
372f4a2713aSLionel Sambuc };
373f4a2713aSLionel Sambuc }
374f4a2713aSLionel Sambuc
enqueue(const CFGBlock * block)375f4a2713aSLionel Sambuc void DeadCodeScan::enqueue(const CFGBlock *block) {
376f4a2713aSLionel Sambuc unsigned blockID = block->getBlockID();
377f4a2713aSLionel Sambuc if (Reachable[blockID] || Visited[blockID])
378f4a2713aSLionel Sambuc return;
379f4a2713aSLionel Sambuc Visited[blockID] = true;
380f4a2713aSLionel Sambuc WorkList.push_back(block);
381f4a2713aSLionel Sambuc }
382f4a2713aSLionel Sambuc
isDeadCodeRoot(const clang::CFGBlock * Block)383f4a2713aSLionel Sambuc bool DeadCodeScan::isDeadCodeRoot(const clang::CFGBlock *Block) {
384f4a2713aSLionel Sambuc bool isDeadRoot = true;
385f4a2713aSLionel Sambuc
386f4a2713aSLionel Sambuc for (CFGBlock::const_pred_iterator I = Block->pred_begin(),
387f4a2713aSLionel Sambuc E = Block->pred_end(); I != E; ++I) {
388f4a2713aSLionel Sambuc if (const CFGBlock *PredBlock = *I) {
389f4a2713aSLionel Sambuc unsigned blockID = PredBlock->getBlockID();
390f4a2713aSLionel Sambuc if (Visited[blockID]) {
391f4a2713aSLionel Sambuc isDeadRoot = false;
392f4a2713aSLionel Sambuc continue;
393f4a2713aSLionel Sambuc }
394f4a2713aSLionel Sambuc if (!Reachable[blockID]) {
395f4a2713aSLionel Sambuc isDeadRoot = false;
396f4a2713aSLionel Sambuc Visited[blockID] = true;
397f4a2713aSLionel Sambuc WorkList.push_back(PredBlock);
398f4a2713aSLionel Sambuc continue;
399f4a2713aSLionel Sambuc }
400f4a2713aSLionel Sambuc }
401f4a2713aSLionel Sambuc }
402f4a2713aSLionel Sambuc
403f4a2713aSLionel Sambuc return isDeadRoot;
404f4a2713aSLionel Sambuc }
405f4a2713aSLionel Sambuc
isValidDeadStmt(const Stmt * S)406f4a2713aSLionel Sambuc static bool isValidDeadStmt(const Stmt *S) {
407f4a2713aSLionel Sambuc if (S->getLocStart().isInvalid())
408f4a2713aSLionel Sambuc return false;
409f4a2713aSLionel Sambuc if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(S))
410f4a2713aSLionel Sambuc return BO->getOpcode() != BO_Comma;
411f4a2713aSLionel Sambuc return true;
412f4a2713aSLionel Sambuc }
413f4a2713aSLionel Sambuc
findDeadCode(const clang::CFGBlock * Block)414f4a2713aSLionel Sambuc const Stmt *DeadCodeScan::findDeadCode(const clang::CFGBlock *Block) {
415f4a2713aSLionel Sambuc for (CFGBlock::const_iterator I = Block->begin(), E = Block->end(); I!=E; ++I)
416f4a2713aSLionel Sambuc if (Optional<CFGStmt> CS = I->getAs<CFGStmt>()) {
417f4a2713aSLionel Sambuc const Stmt *S = CS->getStmt();
418f4a2713aSLionel Sambuc if (isValidDeadStmt(S))
419f4a2713aSLionel Sambuc return S;
420f4a2713aSLionel Sambuc }
421f4a2713aSLionel Sambuc
422f4a2713aSLionel Sambuc if (CFGTerminator T = Block->getTerminator()) {
423*0a6a1f1dSLionel Sambuc if (!T.isTemporaryDtorsBranch()) {
424f4a2713aSLionel Sambuc const Stmt *S = T.getStmt();
425f4a2713aSLionel Sambuc if (isValidDeadStmt(S))
426f4a2713aSLionel Sambuc return S;
427f4a2713aSLionel Sambuc }
428*0a6a1f1dSLionel Sambuc }
429f4a2713aSLionel Sambuc
430*0a6a1f1dSLionel Sambuc return nullptr;
431f4a2713aSLionel Sambuc }
432f4a2713aSLionel Sambuc
SrcCmp(const std::pair<const CFGBlock *,const Stmt * > * p1,const std::pair<const CFGBlock *,const Stmt * > * p2)433f4a2713aSLionel Sambuc static int SrcCmp(const std::pair<const CFGBlock *, const Stmt *> *p1,
434f4a2713aSLionel Sambuc const std::pair<const CFGBlock *, const Stmt *> *p2) {
435f4a2713aSLionel Sambuc if (p1->second->getLocStart() < p2->second->getLocStart())
436f4a2713aSLionel Sambuc return -1;
437f4a2713aSLionel Sambuc if (p2->second->getLocStart() < p1->second->getLocStart())
438f4a2713aSLionel Sambuc return 1;
439f4a2713aSLionel Sambuc return 0;
440f4a2713aSLionel Sambuc }
441f4a2713aSLionel Sambuc
scanBackwards(const clang::CFGBlock * Start,clang::reachable_code::Callback & CB)442f4a2713aSLionel Sambuc unsigned DeadCodeScan::scanBackwards(const clang::CFGBlock *Start,
443f4a2713aSLionel Sambuc clang::reachable_code::Callback &CB) {
444f4a2713aSLionel Sambuc
445f4a2713aSLionel Sambuc unsigned count = 0;
446f4a2713aSLionel Sambuc enqueue(Start);
447f4a2713aSLionel Sambuc
448f4a2713aSLionel Sambuc while (!WorkList.empty()) {
449f4a2713aSLionel Sambuc const CFGBlock *Block = WorkList.pop_back_val();
450f4a2713aSLionel Sambuc
451f4a2713aSLionel Sambuc // It is possible that this block has been marked reachable after
452f4a2713aSLionel Sambuc // it was enqueued.
453f4a2713aSLionel Sambuc if (Reachable[Block->getBlockID()])
454f4a2713aSLionel Sambuc continue;
455f4a2713aSLionel Sambuc
456f4a2713aSLionel Sambuc // Look for any dead code within the block.
457f4a2713aSLionel Sambuc const Stmt *S = findDeadCode(Block);
458f4a2713aSLionel Sambuc
459f4a2713aSLionel Sambuc if (!S) {
460f4a2713aSLionel Sambuc // No dead code. Possibly an empty block. Look at dead predecessors.
461f4a2713aSLionel Sambuc for (CFGBlock::const_pred_iterator I = Block->pred_begin(),
462f4a2713aSLionel Sambuc E = Block->pred_end(); I != E; ++I) {
463f4a2713aSLionel Sambuc if (const CFGBlock *predBlock = *I)
464f4a2713aSLionel Sambuc enqueue(predBlock);
465f4a2713aSLionel Sambuc }
466f4a2713aSLionel Sambuc continue;
467f4a2713aSLionel Sambuc }
468f4a2713aSLionel Sambuc
469f4a2713aSLionel Sambuc // Specially handle macro-expanded code.
470f4a2713aSLionel Sambuc if (S->getLocStart().isMacroID()) {
471*0a6a1f1dSLionel Sambuc count += scanMaybeReachableFromBlock(Block, PP, Reachable);
472f4a2713aSLionel Sambuc continue;
473f4a2713aSLionel Sambuc }
474f4a2713aSLionel Sambuc
475f4a2713aSLionel Sambuc if (isDeadCodeRoot(Block)) {
476*0a6a1f1dSLionel Sambuc reportDeadCode(Block, S, CB);
477*0a6a1f1dSLionel Sambuc count += scanMaybeReachableFromBlock(Block, PP, Reachable);
478f4a2713aSLionel Sambuc }
479f4a2713aSLionel Sambuc else {
480f4a2713aSLionel Sambuc // Record this statement as the possibly best location in a
481f4a2713aSLionel Sambuc // strongly-connected component of dead code for emitting a
482f4a2713aSLionel Sambuc // warning.
483f4a2713aSLionel Sambuc DeferredLocs.push_back(std::make_pair(Block, S));
484f4a2713aSLionel Sambuc }
485f4a2713aSLionel Sambuc }
486f4a2713aSLionel Sambuc
487f4a2713aSLionel Sambuc // If we didn't find a dead root, then report the dead code with the
488f4a2713aSLionel Sambuc // earliest location.
489f4a2713aSLionel Sambuc if (!DeferredLocs.empty()) {
490f4a2713aSLionel Sambuc llvm::array_pod_sort(DeferredLocs.begin(), DeferredLocs.end(), SrcCmp);
491f4a2713aSLionel Sambuc for (DeferredLocsTy::iterator I = DeferredLocs.begin(),
492f4a2713aSLionel Sambuc E = DeferredLocs.end(); I != E; ++I) {
493*0a6a1f1dSLionel Sambuc const CFGBlock *Block = I->first;
494*0a6a1f1dSLionel Sambuc if (Reachable[Block->getBlockID()])
495f4a2713aSLionel Sambuc continue;
496*0a6a1f1dSLionel Sambuc reportDeadCode(Block, I->second, CB);
497*0a6a1f1dSLionel Sambuc count += scanMaybeReachableFromBlock(Block, PP, Reachable);
498f4a2713aSLionel Sambuc }
499f4a2713aSLionel Sambuc }
500f4a2713aSLionel Sambuc
501f4a2713aSLionel Sambuc return count;
502f4a2713aSLionel Sambuc }
503f4a2713aSLionel Sambuc
GetUnreachableLoc(const Stmt * S,SourceRange & R1,SourceRange & R2)504f4a2713aSLionel Sambuc static SourceLocation GetUnreachableLoc(const Stmt *S,
505f4a2713aSLionel Sambuc SourceRange &R1,
506f4a2713aSLionel Sambuc SourceRange &R2) {
507f4a2713aSLionel Sambuc R1 = R2 = SourceRange();
508f4a2713aSLionel Sambuc
509f4a2713aSLionel Sambuc if (const Expr *Ex = dyn_cast<Expr>(S))
510f4a2713aSLionel Sambuc S = Ex->IgnoreParenImpCasts();
511f4a2713aSLionel Sambuc
512f4a2713aSLionel Sambuc switch (S->getStmtClass()) {
513f4a2713aSLionel Sambuc case Expr::BinaryOperatorClass: {
514f4a2713aSLionel Sambuc const BinaryOperator *BO = cast<BinaryOperator>(S);
515f4a2713aSLionel Sambuc return BO->getOperatorLoc();
516f4a2713aSLionel Sambuc }
517f4a2713aSLionel Sambuc case Expr::UnaryOperatorClass: {
518f4a2713aSLionel Sambuc const UnaryOperator *UO = cast<UnaryOperator>(S);
519f4a2713aSLionel Sambuc R1 = UO->getSubExpr()->getSourceRange();
520f4a2713aSLionel Sambuc return UO->getOperatorLoc();
521f4a2713aSLionel Sambuc }
522f4a2713aSLionel Sambuc case Expr::CompoundAssignOperatorClass: {
523f4a2713aSLionel Sambuc const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(S);
524f4a2713aSLionel Sambuc R1 = CAO->getLHS()->getSourceRange();
525f4a2713aSLionel Sambuc R2 = CAO->getRHS()->getSourceRange();
526f4a2713aSLionel Sambuc return CAO->getOperatorLoc();
527f4a2713aSLionel Sambuc }
528f4a2713aSLionel Sambuc case Expr::BinaryConditionalOperatorClass:
529f4a2713aSLionel Sambuc case Expr::ConditionalOperatorClass: {
530f4a2713aSLionel Sambuc const AbstractConditionalOperator *CO =
531f4a2713aSLionel Sambuc cast<AbstractConditionalOperator>(S);
532f4a2713aSLionel Sambuc return CO->getQuestionLoc();
533f4a2713aSLionel Sambuc }
534f4a2713aSLionel Sambuc case Expr::MemberExprClass: {
535f4a2713aSLionel Sambuc const MemberExpr *ME = cast<MemberExpr>(S);
536f4a2713aSLionel Sambuc R1 = ME->getSourceRange();
537f4a2713aSLionel Sambuc return ME->getMemberLoc();
538f4a2713aSLionel Sambuc }
539f4a2713aSLionel Sambuc case Expr::ArraySubscriptExprClass: {
540f4a2713aSLionel Sambuc const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(S);
541f4a2713aSLionel Sambuc R1 = ASE->getLHS()->getSourceRange();
542f4a2713aSLionel Sambuc R2 = ASE->getRHS()->getSourceRange();
543f4a2713aSLionel Sambuc return ASE->getRBracketLoc();
544f4a2713aSLionel Sambuc }
545f4a2713aSLionel Sambuc case Expr::CStyleCastExprClass: {
546f4a2713aSLionel Sambuc const CStyleCastExpr *CSC = cast<CStyleCastExpr>(S);
547f4a2713aSLionel Sambuc R1 = CSC->getSubExpr()->getSourceRange();
548f4a2713aSLionel Sambuc return CSC->getLParenLoc();
549f4a2713aSLionel Sambuc }
550f4a2713aSLionel Sambuc case Expr::CXXFunctionalCastExprClass: {
551f4a2713aSLionel Sambuc const CXXFunctionalCastExpr *CE = cast <CXXFunctionalCastExpr>(S);
552f4a2713aSLionel Sambuc R1 = CE->getSubExpr()->getSourceRange();
553f4a2713aSLionel Sambuc return CE->getLocStart();
554f4a2713aSLionel Sambuc }
555f4a2713aSLionel Sambuc case Stmt::CXXTryStmtClass: {
556f4a2713aSLionel Sambuc return cast<CXXTryStmt>(S)->getHandler(0)->getCatchLoc();
557f4a2713aSLionel Sambuc }
558f4a2713aSLionel Sambuc case Expr::ObjCBridgedCastExprClass: {
559f4a2713aSLionel Sambuc const ObjCBridgedCastExpr *CSC = cast<ObjCBridgedCastExpr>(S);
560f4a2713aSLionel Sambuc R1 = CSC->getSubExpr()->getSourceRange();
561f4a2713aSLionel Sambuc return CSC->getLParenLoc();
562f4a2713aSLionel Sambuc }
563f4a2713aSLionel Sambuc default: ;
564f4a2713aSLionel Sambuc }
565f4a2713aSLionel Sambuc R1 = S->getSourceRange();
566f4a2713aSLionel Sambuc return S->getLocStart();
567f4a2713aSLionel Sambuc }
568f4a2713aSLionel Sambuc
reportDeadCode(const CFGBlock * B,const Stmt * S,clang::reachable_code::Callback & CB)569*0a6a1f1dSLionel Sambuc void DeadCodeScan::reportDeadCode(const CFGBlock *B,
570*0a6a1f1dSLionel Sambuc const Stmt *S,
571f4a2713aSLionel Sambuc clang::reachable_code::Callback &CB) {
572*0a6a1f1dSLionel Sambuc // Classify the unreachable code found, or suppress it in some cases.
573*0a6a1f1dSLionel Sambuc reachable_code::UnreachableKind UK = reachable_code::UK_Other;
574*0a6a1f1dSLionel Sambuc
575*0a6a1f1dSLionel Sambuc if (isa<BreakStmt>(S)) {
576*0a6a1f1dSLionel Sambuc UK = reachable_code::UK_Break;
577*0a6a1f1dSLionel Sambuc }
578*0a6a1f1dSLionel Sambuc else if (isTrivialDoWhile(B, S)) {
579*0a6a1f1dSLionel Sambuc return;
580*0a6a1f1dSLionel Sambuc }
581*0a6a1f1dSLionel Sambuc else if (isDeadReturn(B, S)) {
582*0a6a1f1dSLionel Sambuc UK = reachable_code::UK_Return;
583*0a6a1f1dSLionel Sambuc }
584*0a6a1f1dSLionel Sambuc
585*0a6a1f1dSLionel Sambuc SourceRange SilenceableCondVal;
586*0a6a1f1dSLionel Sambuc
587*0a6a1f1dSLionel Sambuc if (UK == reachable_code::UK_Other) {
588*0a6a1f1dSLionel Sambuc // Check if the dead code is part of the "loop target" of
589*0a6a1f1dSLionel Sambuc // a for/for-range loop. This is the block that contains
590*0a6a1f1dSLionel Sambuc // the increment code.
591*0a6a1f1dSLionel Sambuc if (const Stmt *LoopTarget = B->getLoopTarget()) {
592*0a6a1f1dSLionel Sambuc SourceLocation Loc = LoopTarget->getLocStart();
593*0a6a1f1dSLionel Sambuc SourceRange R1(Loc, Loc), R2;
594*0a6a1f1dSLionel Sambuc
595*0a6a1f1dSLionel Sambuc if (const ForStmt *FS = dyn_cast<ForStmt>(LoopTarget)) {
596*0a6a1f1dSLionel Sambuc const Expr *Inc = FS->getInc();
597*0a6a1f1dSLionel Sambuc Loc = Inc->getLocStart();
598*0a6a1f1dSLionel Sambuc R2 = Inc->getSourceRange();
599*0a6a1f1dSLionel Sambuc }
600*0a6a1f1dSLionel Sambuc
601*0a6a1f1dSLionel Sambuc CB.HandleUnreachable(reachable_code::UK_Loop_Increment,
602*0a6a1f1dSLionel Sambuc Loc, SourceRange(), SourceRange(Loc, Loc), R2);
603*0a6a1f1dSLionel Sambuc return;
604*0a6a1f1dSLionel Sambuc }
605*0a6a1f1dSLionel Sambuc
606*0a6a1f1dSLionel Sambuc // Check if the dead block has a predecessor whose branch has
607*0a6a1f1dSLionel Sambuc // a configuration value that *could* be modified to
608*0a6a1f1dSLionel Sambuc // silence the warning.
609*0a6a1f1dSLionel Sambuc CFGBlock::const_pred_iterator PI = B->pred_begin();
610*0a6a1f1dSLionel Sambuc if (PI != B->pred_end()) {
611*0a6a1f1dSLionel Sambuc if (const CFGBlock *PredBlock = PI->getPossiblyUnreachableBlock()) {
612*0a6a1f1dSLionel Sambuc const Stmt *TermCond =
613*0a6a1f1dSLionel Sambuc PredBlock->getTerminatorCondition(/* strip parens */ false);
614*0a6a1f1dSLionel Sambuc isConfigurationValue(TermCond, PP, &SilenceableCondVal);
615*0a6a1f1dSLionel Sambuc }
616*0a6a1f1dSLionel Sambuc }
617*0a6a1f1dSLionel Sambuc }
618*0a6a1f1dSLionel Sambuc
619f4a2713aSLionel Sambuc SourceRange R1, R2;
620f4a2713aSLionel Sambuc SourceLocation Loc = GetUnreachableLoc(S, R1, R2);
621*0a6a1f1dSLionel Sambuc CB.HandleUnreachable(UK, Loc, SilenceableCondVal, R1, R2);
622f4a2713aSLionel Sambuc }
623f4a2713aSLionel Sambuc
624*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
625*0a6a1f1dSLionel Sambuc // Reachability APIs.
626*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
627*0a6a1f1dSLionel Sambuc
628f4a2713aSLionel Sambuc namespace clang { namespace reachable_code {
629f4a2713aSLionel Sambuc
anchor()630f4a2713aSLionel Sambuc void Callback::anchor() { }
631f4a2713aSLionel Sambuc
ScanReachableFromBlock(const CFGBlock * Start,llvm::BitVector & Reachable)632f4a2713aSLionel Sambuc unsigned ScanReachableFromBlock(const CFGBlock *Start,
633f4a2713aSLionel Sambuc llvm::BitVector &Reachable) {
634*0a6a1f1dSLionel Sambuc return scanFromBlock(Start, Reachable, /* SourceManager* */ nullptr, false);
635f4a2713aSLionel Sambuc }
636f4a2713aSLionel Sambuc
FindUnreachableCode(AnalysisDeclContext & AC,Preprocessor & PP,Callback & CB)637*0a6a1f1dSLionel Sambuc void FindUnreachableCode(AnalysisDeclContext &AC, Preprocessor &PP,
638*0a6a1f1dSLionel Sambuc Callback &CB) {
639f4a2713aSLionel Sambuc
640f4a2713aSLionel Sambuc CFG *cfg = AC.getCFG();
641f4a2713aSLionel Sambuc if (!cfg)
642f4a2713aSLionel Sambuc return;
643f4a2713aSLionel Sambuc
644f4a2713aSLionel Sambuc // Scan for reachable blocks from the entrance of the CFG.
645f4a2713aSLionel Sambuc // If there are no unreachable blocks, we're done.
646f4a2713aSLionel Sambuc llvm::BitVector reachable(cfg->getNumBlockIDs());
647*0a6a1f1dSLionel Sambuc unsigned numReachable =
648*0a6a1f1dSLionel Sambuc scanMaybeReachableFromBlock(&cfg->getEntry(), PP, reachable);
649f4a2713aSLionel Sambuc if (numReachable == cfg->getNumBlockIDs())
650f4a2713aSLionel Sambuc return;
651f4a2713aSLionel Sambuc
652f4a2713aSLionel Sambuc // If there aren't explicit EH edges, we should include the 'try' dispatch
653f4a2713aSLionel Sambuc // blocks as roots.
654f4a2713aSLionel Sambuc if (!AC.getCFGBuildOptions().AddEHEdges) {
655f4a2713aSLionel Sambuc for (CFG::try_block_iterator I = cfg->try_blocks_begin(),
656f4a2713aSLionel Sambuc E = cfg->try_blocks_end() ; I != E; ++I) {
657*0a6a1f1dSLionel Sambuc numReachable += scanMaybeReachableFromBlock(*I, PP, reachable);
658f4a2713aSLionel Sambuc }
659f4a2713aSLionel Sambuc if (numReachable == cfg->getNumBlockIDs())
660f4a2713aSLionel Sambuc return;
661f4a2713aSLionel Sambuc }
662f4a2713aSLionel Sambuc
663f4a2713aSLionel Sambuc // There are some unreachable blocks. We need to find the root blocks that
664f4a2713aSLionel Sambuc // contain code that should be considered unreachable.
665f4a2713aSLionel Sambuc for (CFG::iterator I = cfg->begin(), E = cfg->end(); I != E; ++I) {
666f4a2713aSLionel Sambuc const CFGBlock *block = *I;
667f4a2713aSLionel Sambuc // A block may have been marked reachable during this loop.
668f4a2713aSLionel Sambuc if (reachable[block->getBlockID()])
669f4a2713aSLionel Sambuc continue;
670f4a2713aSLionel Sambuc
671*0a6a1f1dSLionel Sambuc DeadCodeScan DS(reachable, PP);
672f4a2713aSLionel Sambuc numReachable += DS.scanBackwards(block, CB);
673f4a2713aSLionel Sambuc
674f4a2713aSLionel Sambuc if (numReachable == cfg->getNumBlockIDs())
675f4a2713aSLionel Sambuc return;
676f4a2713aSLionel Sambuc }
677f4a2713aSLionel Sambuc }
678f4a2713aSLionel Sambuc
679f4a2713aSLionel Sambuc }} // end namespace clang::reachable_code
680