Kaydet (Commit) 06c281c2 authored tarafından Noel Grandin's avatar Noel Grandin

loplugin:unusedindex fix false+ in nested loops

Change-Id: I31acbf104e49a4d1f077817a68d0b116fd2e0a30
üst ca6d205e
...@@ -18,6 +18,22 @@ void func1() ...@@ -18,6 +18,22 @@ void func1()
n += 1; n += 1;
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
n += i; n += i;
for (int i = 0; i < 10; ++i) // expected-error {{loop variable not used [loplugin:unusedindex]}}
{
for (int j = 0; j < 10; ++j)
{
n += j;
}
}
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
{
n += j;
n += i;
}
}
} }
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
...@@ -38,7 +38,7 @@ public: ...@@ -38,7 +38,7 @@ public:
bool VisitDeclRefExpr(DeclRefExpr const* stmt); bool VisitDeclRefExpr(DeclRefExpr const* stmt);
private: private:
VarDecl const* mLoopVarDecl = nullptr; std::vector<VarDecl const*> mLoopVarDecls;
std::unordered_set<VarDecl const*> mFoundSet; std::unordered_set<VarDecl const*> mFoundSet;
}; };
...@@ -46,27 +46,28 @@ bool UnusedIndex::TraverseForStmt(ForStmt* stmt) ...@@ -46,27 +46,28 @@ bool UnusedIndex::TraverseForStmt(ForStmt* stmt)
{ {
if (ignoreLocation(stmt)) if (ignoreLocation(stmt))
return true; return true;
auto savedCopy = mLoopVarDecl;
mLoopVarDecl = nullptr; VarDecl const* loopVarDecl = nullptr;
if (stmt->getInit()) if (stmt->getInit())
{ {
auto declStmt = dyn_cast<DeclStmt>(stmt->getInit()); auto declStmt = dyn_cast<DeclStmt>(stmt->getInit());
if (declStmt && declStmt->isSingleDecl()) if (declStmt && declStmt->isSingleDecl())
{ {
auto varDecl = dyn_cast<VarDecl>(declStmt->getSingleDecl()); loopVarDecl = dyn_cast<VarDecl>(declStmt->getSingleDecl());
if (varDecl)
mLoopVarDecl = varDecl;
} }
} }
if (loopVarDecl)
mLoopVarDecls.push_back(loopVarDecl);
// deliberately ignore the other parts of the for stmt, except for the body // deliberately ignore the other parts of the for stmt, except for the body
auto ret = RecursiveASTVisitor::TraverseStmt(stmt->getBody()); auto ret = RecursiveASTVisitor::TraverseStmt(stmt->getBody());
if (mLoopVarDecl && mFoundSet.erase(mLoopVarDecl) == 0) if (loopVarDecl && mFoundSet.erase(loopVarDecl) == 0)
report(DiagnosticsEngine::Warning, "loop variable not used", mLoopVarDecl->getLocStart()) report(DiagnosticsEngine::Warning, "loop variable not used", loopVarDecl->getLocStart())
<< mLoopVarDecl->getSourceRange(); << loopVarDecl->getSourceRange();
mLoopVarDecl = savedCopy;
if (loopVarDecl)
mLoopVarDecls.pop_back();
return ret; return ret;
} }
...@@ -75,7 +76,7 @@ bool UnusedIndex::VisitDeclRefExpr(DeclRefExpr const* stmt) ...@@ -75,7 +76,7 @@ bool UnusedIndex::VisitDeclRefExpr(DeclRefExpr const* stmt)
auto varDecl = dyn_cast<VarDecl>(stmt->getDecl()); auto varDecl = dyn_cast<VarDecl>(stmt->getDecl());
if (!varDecl) if (!varDecl)
return true; return true;
if (mLoopVarDecl && mLoopVarDecl == varDecl) if (std::find(mLoopVarDecls.begin(), mLoopVarDecls.end(), varDecl) != mLoopVarDecls.end())
mFoundSet.insert(varDecl); mFoundSet.insert(varDecl);
return true; return true;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment