compilerplugins/clang/cppunitassertequals.cxx      |   52 +++++++++++++++++++++
 compilerplugins/clang/test/cppunitassertequals.cxx |    7 ++
 2 files changed, 58 insertions(+), 1 deletion(-)

New commits:
commit 569977cfe1c3be4f66665306372d0253924ba6c0
Author:     Stephan Bergmann <[email protected]>
AuthorDate: Tue Feb 28 08:06:00 2023 +0100
Commit:     Stephan Bergmann <[email protected]>
CommitDate: Tue Feb 28 08:29:10 2023 +0000

    Extend loplugin:cppunitassertequals to CPPUNIT_ASSERT_LESS etc.
    
    (Just in case, even though this doesn't find any actual issues in the code 
for
    now.)
    
    Change-Id: I80b8b0a647e89fdb6a4f0f4363fa1c3df8e5ddeb
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/147942
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <[email protected]>

diff --git a/compilerplugins/clang/cppunitassertequals.cxx 
b/compilerplugins/clang/cppunitassertequals.cxx
index 26879fae9f95..df50f57537bc 100644
--- a/compilerplugins/clang/cppunitassertequals.cxx
+++ b/compilerplugins/clang/cppunitassertequals.cxx
@@ -145,6 +145,58 @@ bool CppunitAssertEquals::VisitCallExpr(const CallExpr* 
callExpr)
                 callExpr->getExprLoc())
                 << callExpr->getSourceRange();
     }
+    if (loplugin::DeclCheck(decl).Function("assertLess").
+             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_LESS parameters look switched, expected value 
should be first param",
+                callExpr->getExprLoc())
+                << callExpr->getSourceRange();
+    }
+    if (loplugin::DeclCheck(decl).Function("assertLessEqual").
+             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_LESSEQUAL parameters look switched, expected 
value should be first param",
+                callExpr->getExprLoc())
+                << callExpr->getSourceRange();
+    }
+    if (loplugin::DeclCheck(decl).Function("assertGreater").
+             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_GREATER parameters look switched, expected 
value should be first param",
+                callExpr->getExprLoc())
+                << callExpr->getSourceRange();
+    }
+    if (loplugin::DeclCheck(decl).Function("assertGreaterEqual").
+             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_GREATEREQUAL parameters look switched, 
expected value should be first param",
+                callExpr->getExprLoc())
+                << callExpr->getSourceRange();
+    }
     return true;
 }
 
diff --git a/compilerplugins/clang/test/cppunitassertequals.cxx 
b/compilerplugins/clang/test/cppunitassertequals.cxx
index 3de01eb2b6eb..ea68a77e9628 100644
--- a/compilerplugins/clang/test/cppunitassertequals.cxx
+++ b/compilerplugins/clang/test/cppunitassertequals.cxx
@@ -21,7 +21,7 @@
 
 void test(
     bool b1, bool b2, OUString const & s1, OUString const & s2, T t, void * p, 
std::nullptr_t n,
-    double d)
+    double d, int i)
 {
     CppUnit::Asserter::failIf(b1,"");
     CPPUNIT_ASSERT(b1 && b2); // expected-error {{rather split into two 
CPPUNIT_ASSERT [loplugin:cppunitassertequals]}}
@@ -79,6 +79,11 @@ void test(
     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);
+
+    CPPUNIT_ASSERT_LESS(i, 1); // expected-error {{CPPUNIT_ASSERT_LESS 
parameters look switched, expected value should be first param 
[loplugin:cppunitassertequals]}}
+    CPPUNIT_ASSERT_LESSEQUAL(i, 1); // expected-error 
{{CPPUNIT_ASSERT_LESSEQUAL parameters look switched, expected value should be 
first param [loplugin:cppunitassertequals]}}
+    CPPUNIT_ASSERT_GREATER(i, 1); // expected-error {{CPPUNIT_ASSERT_GREATER 
parameters look switched, expected value should be first param 
[loplugin:cppunitassertequals]}}
+    CPPUNIT_ASSERT_GREATEREQUAL(i, 1); // expected-error 
{{CPPUNIT_ASSERT_GREATEREQUAL parameters look switched, expected value should 
be first param [loplugin:cppunitassertequals]}}
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s 
cinkeys+=0=break: */

Reply via email to