From 1421037ece1f3f4e20ab5e81b6931557218e9562 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Fri, 21 Mar 2014 06:02:36 +0000 Subject: [-Wunreachable-code] add a specialized diagnostic for unreachable increment expressions of loops. llvm-svn: 204430 --- clang/lib/Analysis/ReachableCode.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'clang/lib/Analysis') diff --git a/clang/lib/Analysis/ReachableCode.cpp b/clang/lib/Analysis/ReachableCode.cpp index 4220000cd33..53e03de64ed 100644 --- a/clang/lib/Analysis/ReachableCode.cpp +++ b/clang/lib/Analysis/ReachableCode.cpp @@ -529,6 +529,26 @@ void DeadCodeScan::reportDeadCode(const CFGBlock *B, UK = reachable_code::UK_Return; } + if (UK == reachable_code::UK_Other) { + // Check if the dead code is part of the "loop target" of + // a for/for-range loop. This is the block that contains + // the increment code. + if (const Stmt *LoopTarget = B->getLoopTarget()) { + SourceLocation Loc = LoopTarget->getLocStart(); + SourceRange R1(Loc, Loc), R2; + + if (const ForStmt *FS = dyn_cast(LoopTarget)) { + const Expr *Inc = FS->getInc(); + Loc = Inc->getLocStart(); + R2 = Inc->getSourceRange(); + } + + CB.HandleUnreachable(reachable_code::UK_Loop_Increment, + Loc, SourceRange(Loc, Loc), R2); + return; + } + } + SourceRange R1, R2; SourceLocation Loc = GetUnreachableLoc(S, R1, R2); CB.HandleUnreachable(UK, Loc, R1, R2); -- cgit v1.2.3