diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2017-11-10 13:40:12 +0100 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2017-11-11 00:04:25 +0100 |
commit | 5d12237d79f289a1dcf8e07aa03df329e136f078 (patch) | |
tree | 1654bf34307bfc56b8db479e9934efb8a7cb1186 /compilerplugins/clang/plugin.cxx | |
parent | 1fc79c3491906d85ed9972e161112459035b62ed (diff) |
loplugin:unnecessaryoverride: suppress warnings when default args differ
...instead of blacklisting such cases. Reuses the
checkIdenticalDefaultArguments code that was originally in
loplugin:overrideparam (and appears to work reasonably well for the default
arguments that actually happen in practice).
Change-Id: I9cf2db17101beb135b2039a9b7ed335bd2af2c08
Reviewed-on: https://gerrit.libreoffice.org/44594
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'compilerplugins/clang/plugin.cxx')
-rw-r--r-- | compilerplugins/clang/plugin.cxx | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/compilerplugins/clang/plugin.cxx b/compilerplugins/clang/plugin.cxx index f8292ef661f6..6697f94b3ee8 100644 --- a/compilerplugins/clang/plugin.cxx +++ b/compilerplugins/clang/plugin.cxx @@ -75,6 +75,19 @@ void Plugin::registerPlugin( Plugin* (*create)( const InstantiationData& ), cons PluginHandler::registerPlugin( create, optionName, isPPCallback, byDefault ); } +bool Plugin::evaluate(const Expr* expr, APSInt& x) +{ + if (expr->EvaluateAsInt(x, compiler.getASTContext())) + { + return true; + } + if (isa<CXXNullPtrLiteralExpr>(expr)) { + x = 0; + return true; + } + return false; +} + const Stmt* Plugin::getParentStmt( const Stmt* stmt ) { auto parentsRange = compiler.getASTContext().getParents(*stmt); @@ -203,6 +216,62 @@ bool Plugin::containsPreprocessingConditionalInclusion(SourceRange range) return false; } +Plugin::IdenticalDefaultArgumentsResult Plugin::checkIdenticalDefaultArguments( + Expr const * argument1, Expr const * argument2) +{ + if ((argument1 == nullptr) != (argument2 == nullptr)) { + return IdenticalDefaultArgumentsResult::No; + } + if (argument1 == nullptr) { + return IdenticalDefaultArgumentsResult::Yes; + } + if (argument1->isNullPointerConstant(compiler.getASTContext(), Expr::NPC_NeverValueDependent) + && argument2->isNullPointerConstant(compiler.getASTContext(), Expr::NPC_NeverValueDependent)) + { + return IdenticalDefaultArgumentsResult::Yes; + } + APSInt x1, x2; + if (evaluate(argument1, x1) && evaluate(argument2, x2)) + { + return x1 == x2 + ? IdenticalDefaultArgumentsResult::Yes + : IdenticalDefaultArgumentsResult::No; + } +#if CLANG_VERSION >= 30900 + APFloat f1(0.0f), f2(0.0f); + if (argument1->EvaluateAsFloat(f1, compiler.getASTContext()) + && argument2->EvaluateAsFloat(f2, compiler.getASTContext())) + { + return f1.bitwiseIsEqual(f2) + ? IdenticalDefaultArgumentsResult::Yes + : IdenticalDefaultArgumentsResult::No; + } +#endif + if (auto const lit1 = dyn_cast<clang::StringLiteral>( + argument1->IgnoreParenImpCasts())) + { + if (auto const lit2 = dyn_cast<clang::StringLiteral>( + argument2->IgnoreParenImpCasts())) + { + return lit1->getBytes() == lit2->getBytes() + ? IdenticalDefaultArgumentsResult::Yes + : IdenticalDefaultArgumentsResult::No; + } + } + // catch params with defaults like "= OUString()" + if (isa<MaterializeTemporaryExpr>(argument1) + && isa<MaterializeTemporaryExpr>(argument2)) + { + return IdenticalDefaultArgumentsResult::Yes; + } + if (isa<CXXBindTemporaryExpr>(argument1) + && isa<CXXBindTemporaryExpr>(argument2)) + { + return IdenticalDefaultArgumentsResult::Yes; + } + return IdenticalDefaultArgumentsResult::Maybe; +} + RewritePlugin::RewritePlugin( const InstantiationData& data ) : Plugin( data ) , rewriter( data.rewriter ) |