From dc96de7c62e1e6f37ec485f3f0f564a00f3d01f4 Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Wed, 22 Feb 2023 21:28:52 +0100 Subject: 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 --- compilerplugins/clang/cppunitassertequals.cxx | 15 ++++++++++++++- compilerplugins/clang/test/cppunitassertequals.cxx | 11 ++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) (limited to 'compilerplugins') 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: */ -- cgit