teach unnecessaryparen loplugin about identifiers

Change-Id: I5710b51e53779c222cec0bf08cd34bda330fec4b
Reviewed-on: https://gerrit.libreoffice.org/39737
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Tested-by: Noel Grandin <noel.grandin@collabora.co.uk>
This commit is contained in:
Noel Grandin
2017-07-06 14:49:15 +02:00
parent 2ed9a2b641
commit 4250b25c6a
195 changed files with 394 additions and 398 deletions

View File

@@ -56,16 +56,50 @@ public:
bool VisitWhileStmt(const WhileStmt *);
bool VisitSwitchStmt(const SwitchStmt *);
bool VisitCallExpr(const CallExpr *);
bool TraverseUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *);
bool TraverseCaseStmt(CaseStmt *);
bool TraverseConditionalOperator(ConditionalOperator *);
private:
void VisitSomeStmt(const Stmt *parent, const Expr* cond, StringRef stmtName);
Expr* insideCaseStmt = nullptr;
Expr* insideConditionalOperator = nullptr;
};
bool UnnecessaryParen::TraverseUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *)
{
// for some reason, the parentheses in an expression like "sizeof(x)" actually show up
// in the AST, so just ignore that part of the AST
return true;
}
bool UnnecessaryParen::TraverseCaseStmt(CaseStmt * caseStmt)
{
auto old = insideCaseStmt;
insideCaseStmt = caseStmt->getLHS()->IgnoreImpCasts();
bool ret = RecursiveASTVisitor::TraverseCaseStmt(caseStmt);
insideCaseStmt = old;
return ret;
}
bool UnnecessaryParen::TraverseConditionalOperator(ConditionalOperator * conditionalOperator)
{
auto old = insideConditionalOperator;
insideConditionalOperator = conditionalOperator->getCond()->IgnoreImpCasts();
bool ret = RecursiveASTVisitor::TraverseConditionalOperator(conditionalOperator);
insideConditionalOperator = old;
return ret;
}
bool UnnecessaryParen::VisitParenExpr(const ParenExpr* parenExpr)
{
if (ignoreLocation(parenExpr))
return true;
if (parenExpr->getLocStart().isMacroID())
return true;
if (insideCaseStmt && parenExpr == insideCaseStmt)
return true;
if (insideConditionalOperator && parenExpr == insideConditionalOperator)
return true;
auto subParenExpr = dyn_cast<ParenExpr>(parenExpr->getSubExpr()->IgnoreImpCasts());
if (subParenExpr) {
@@ -76,6 +110,24 @@ bool UnnecessaryParen::VisitParenExpr(const ParenExpr* parenExpr)
parenExpr->getLocStart())
<< parenExpr->getSourceRange();
}
auto declRefExpr = dyn_cast<DeclRefExpr>(parenExpr->getSubExpr()->IgnoreImpCasts());
if (declRefExpr) {
if (declRefExpr->getLocStart().isMacroID())
return true;
// hack for BAD_CAST macro
SourceManager& SM = compiler.getSourceManager();
const char *p1 = SM.getCharacterData( declRefExpr->getLocStart().getLocWithOffset(-10) );
const char *p2 = SM.getCharacterData( declRefExpr->getLocStart() );
if ( std::string(p1, p2 - p1).find("BAD_CAST") != std::string::npos )
return true;
report(
DiagnosticsEngine::Warning, "unnecessary parentheses around identifier",
parenExpr->getLocStart())
<< parenExpr->getSourceRange();
}
return true;
}