From adf3cf074e05e633e907f72215d56720c01fd2db Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Tue, 7 Nov 2017 23:27:54 +0100 Subject: Fix loplugin:flatten's skipping of if/then/else/if chains (but which finds no new hits) Change-Id: I5d5f351402797b662a08ec8dca301bd174e22a50 Reviewed-on: https://gerrit.libreoffice.org/44433 Tested-by: Jenkins Reviewed-by: Stephan Bergmann --- compilerplugins/clang/flatten.cxx | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'compilerplugins/clang/flatten.cxx') diff --git a/compilerplugins/clang/flatten.cxx b/compilerplugins/clang/flatten.cxx index 7d546ef5b448..7dc2bf019738 100644 --- a/compilerplugins/clang/flatten.cxx +++ b/compilerplugins/clang/flatten.cxx @@ -47,6 +47,7 @@ private: std::stack mLastStmtInParentStack; std::vector> mvModifiedRanges; + Stmt const * mElseBranch = nullptr; }; static Stmt const * containsSingleThrowExpr(Stmt const * stmt) @@ -84,10 +85,20 @@ bool Flatten::TraverseCXXCatchStmt(CXXCatchStmt* ) bool Flatten::TraverseIfStmt(IfStmt * ifStmt) { - // ignore if we are part of an if/then/else/if chain - if (ifStmt->getElse() && isa(ifStmt->getElse())) - return true; - return RecursiveASTVisitor::TraverseIfStmt(ifStmt); + if (!WalkUpFromIfStmt(ifStmt)) { + return false; + } + auto const saved = mElseBranch; + mElseBranch = ifStmt->getElse(); + auto ret = true; + for (auto const sub: ifStmt->children()) { + if (!TraverseStmt(sub)) { + ret = false; + break; + } + } + mElseBranch = saved; + return ret; } bool Flatten::TraverseCompoundStmt(CompoundStmt * compoundStmt) @@ -110,6 +121,10 @@ bool Flatten::VisitIfStmt(IfStmt const * ifStmt) if (!ifStmt->getElse()) return true; + // ignore if we are part of an if/then/else/if chain + if (ifStmt == mElseBranch || isa(ifStmt->getElse())) + return true; + auto const thenThrowExpr = containsSingleThrowExpr(ifStmt->getThen()); auto const elseThrowExpr = containsSingleThrowExpr(ifStmt->getElse()); // If neither contains a throw, nothing to do; if both contain throws, no -- cgit