teach unnecessaryparen plugin about other kinds of statements

i.e. do / while / switch

Change-Id: Id0985015cc425557f9984734701d56466f8a6088
Reviewed-on: https://gerrit.libreoffice.org/39601
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
This commit is contained in:
Noel Grandin
2017-07-05 16:30:06 +02:00
parent a1ead1a028
commit 13341ffa49
10 changed files with 59 additions and 21 deletions

View File

@@ -14,7 +14,7 @@ int main()
int x = 1;
x = ((2)); // expected-error {{parentheses around parentheses [loplugin:unnecessaryparen]}}
if ((foo(1))) foo(2); // expected-error {{parentheses immediately inside if [loplugin:unnecessaryparen]}}
if ((foo(1))) foo(2); // expected-error {{parentheses immediately inside if statement [loplugin:unnecessaryparen]}}
};
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */

View File

@@ -38,11 +38,22 @@ public:
return;
if (loplugin::hasPathnamePrefix(fn, WORKDIR "/YaccTarget/idlc/source/parser.cxx"))
return;
// TODO yuck, comma operator at work
if (loplugin::hasPathnamePrefix(fn, SRCDIR "/writerfilter/source/rtftok/rtftokenizer.cxx"))
return;
if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/filter/html/htmltab.cxx"))
return;
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
}
bool VisitParenExpr(const ParenExpr *);
bool VisitIfStmt(const IfStmt *);
bool VisitDoStmt(const DoStmt *);
bool VisitWhileStmt(const WhileStmt *);
bool VisitSwitchStmt(const SwitchStmt *);
private:
void VisitSomeStmt(const Stmt *parent, const Expr* cond, StringRef stmtName);
};
bool UnnecessaryParen::VisitParenExpr(const ParenExpr* parenExpr)
@@ -66,22 +77,49 @@ bool UnnecessaryParen::VisitParenExpr(const ParenExpr* parenExpr)
bool UnnecessaryParen::VisitIfStmt(const IfStmt* ifStmt)
{
if (ignoreLocation(ifStmt))
return true;
VisitSomeStmt(ifStmt, ifStmt->getCond(), "if");
return true;
}
if (auto parenExpr = dyn_cast<ParenExpr>(ifStmt->getCond()->IgnoreImpCasts())) {
bool UnnecessaryParen::VisitDoStmt(const DoStmt* doStmt)
{
VisitSomeStmt(doStmt, doStmt->getCond(), "do");
return true;
}
bool UnnecessaryParen::VisitWhileStmt(const WhileStmt* whileStmt)
{
VisitSomeStmt(whileStmt, whileStmt->getCond(), "while");
return true;
}
bool UnnecessaryParen::VisitSwitchStmt(const SwitchStmt* switchStmt)
{
VisitSomeStmt(switchStmt, switchStmt->getCond(), "switch");
return true;
}
void UnnecessaryParen::VisitSomeStmt(const Stmt *parent, const Expr* cond, StringRef stmtName)
{
if (ignoreLocation(parent))
return;
if (parent->getLocStart().isMacroID())
return;
auto parenExpr = dyn_cast<ParenExpr>(cond->IgnoreImpCasts());
if (parenExpr) {
if (parenExpr->getLocStart().isMacroID())
return true;
return;
// assignments need extra parentheses or they generate a compiler warning
auto binaryOp = dyn_cast<BinaryOperator>(parenExpr->getSubExpr());
if (binaryOp && binaryOp->getOpcode() == BO_Assign)
return true;
return;
report(
DiagnosticsEngine::Warning, "parentheses immediately inside if",
ifStmt->getLocStart())
<< ifStmt->getSourceRange();
DiagnosticsEngine::Warning, "parentheses immediately inside %0 statement",
parenExpr->getLocStart())
<< stmtName
<< parenExpr->getSourceRange();
}
return true;
}
loplugin::Plugin::Registration< UnnecessaryParen > X("unnecessaryparen", true);