summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2023-02-22 21:28:52 +0100
committerStephan Bergmann <sbergman@redhat.com>2023-02-23 07:04:09 +0000
commitdc96de7c62e1e6f37ec485f3f0f564a00f3d01f4 (patch)
tree2f22076b18079daa2f79021463818de6facd71e4 /compilerplugins
parent6d404f9ccad3ac6a7ffb83876ae7a656bc2d4547 (diff)
Extend loplugin:cppunitassertequals to more argument types
Change-Id: Ic2990ebc2e4a9a36dcd3f90c5f634ca7dd225d52 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/147491 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/cppunitassertequals.cxx15
-rw-r--r--compilerplugins/clang/test/cppunitassertequals.cxx11
2 files changed, 24 insertions, 2 deletions
diff --git a/compilerplugins/clang/cppunitassertequals.cxx b/compilerplugins/clang/cppunitassertequals.cxx
index 56f13b822163..26879fae9f95 100644
--- a/compilerplugins/clang/cppunitassertequals.cxx
+++ b/compilerplugins/clang/cppunitassertequals.cxx
@@ -132,6 +132,19 @@ bool CppunitAssertEquals::VisitCallExpr(const CallExpr* callExpr)
callExpr->getExprLoc())
<< callExpr->getSourceRange();
}
+ if (loplugin::DeclCheck(decl).Function("assertDoubleEquals").
+ Namespace("CppUnit").GlobalNamespace())
+ {
+ // can happen in template test code that both params are compile time constants
+ if (isCompileTimeConstant(callExpr->getArg(0)))
+ return true;
+ if (isCompileTimeConstant(callExpr->getArg(1)))
+ report(
+ DiagnosticsEngine::Warning,
+ "CPPUNIT_ASSERT_DOUBLES_EQUALS parameters look switched, expected value should be first param",
+ callExpr->getExprLoc())
+ << callExpr->getSourceRange();
+ }
return true;
}
@@ -167,7 +180,7 @@ Expr const * stripConstructor(Expr const * expr) {
bool CppunitAssertEquals::isCompileTimeConstant(Expr const * expr)
{
- if (expr->isIntegerConstantExpr(compiler.getASTContext()))
+ if (expr->isCXX11ConstantExpr(compiler.getASTContext()))
return true;
// is string literal ?
expr = expr->IgnoreParenImpCasts();
diff --git a/compilerplugins/clang/test/cppunitassertequals.cxx b/compilerplugins/clang/test/cppunitassertequals.cxx
index 05d814c855c2..3de01eb2b6eb 100644
--- a/compilerplugins/clang/test/cppunitassertequals.cxx
+++ b/compilerplugins/clang/test/cppunitassertequals.cxx
@@ -20,7 +20,8 @@
#define TEST2(x) x
void test(
- bool b1, bool b2, OUString const & s1, OUString const & s2, T t, void * p, std::nullptr_t n)
+ bool b1, bool b2, OUString const & s1, OUString const & s2, T t, void * p, std::nullptr_t n,
+ double d)
{
CppUnit::Asserter::failIf(b1,"");
CPPUNIT_ASSERT(b1 && b2); // expected-error {{rather split into two CPPUNIT_ASSERT [loplugin:cppunitassertequals]}}
@@ -70,6 +71,14 @@ void test(
CPPUNIT_ASSERT_EQUAL_MESSAGE("foo", s1, OUString("xxx")); // expected-error {{CPPUNIT_ASSERT_EQUALS parameters look switched, expected value should be first param [loplugin:cppunitassertequals]}}
CPPUNIT_ASSERT_EQUAL(OUString("xxx"), s1);
CPPUNIT_ASSERT_EQUAL_MESSAGE("foo", OUString("xxx"), s1);
+
+ CPPUNIT_ASSERT_EQUAL(d, 1.0); // expected-error {{CPPUNIT_ASSERT_EQUALS parameters look switched, expected value should be first param [loplugin:cppunitassertequals]}}
+ CPPUNIT_ASSERT_EQUAL(1.0, d);
+
+ CPPUNIT_ASSERT_DOUBLES_EQUAL(d, 1.0, 0.1); // expected-error {{CPPUNIT_ASSERT_DOUBLES_EQUALS parameters look switched, expected value should be first param [loplugin:cppunitassertequals]}}
+ CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("foo", d, 1.0, 0.1); // expected-error {{CPPUNIT_ASSERT_DOUBLES_EQUALS parameters look switched, expected value should be first param [loplugin:cppunitassertequals]}}
+ CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, d, 0.1);
+ CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("foo", 1.0, d, 0.1);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */