From 618a09fc9b5dc50985cdd114be84f0eb16701651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Sun, 11 May 2025 17:36:37 +0200 Subject: [PATCH 1/4] add test --- test/testother.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/testother.cpp b/test/testother.cpp index 492f111e10e..60131084abf 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -306,6 +306,8 @@ class TestOther : public TestFixture { TEST_CASE(knownConditionFloating); TEST_CASE(knownConditionPrefixed); + + TEST_CASE(ternarySameValuePlatformDependent); // #13773 } #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) @@ -13093,6 +13095,19 @@ class TestOther : public TestFixture { "[test.cpp:2:13] -> [test.cpp:3:11]: (style) The comparison 'i > +1' is always false. [knownConditionTrueFalse]\n", errout_str()); } + + void ternarySameValuePlatformDependent() // #13773 + { + check("typedef float _Complex complex_f32;\n" + "typedef struct {\n" + " uint16_t real;\n" + " uint16_t imag;\n" + "} packed_complex;\n" + "void foo(void) {\n" + " b = a ? sizeof(packed_complex) : sizeof(complex_f32);\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + } }; REGISTER_TEST(TestOther) From 9c383ca94f29f1d0fec50eb1823e511d25e3e63f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Sun, 11 May 2025 17:30:53 +0200 Subject: [PATCH 2/4] fix #13773 --- lib/checkother.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 282f3f3e50e..09b94d97baa 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2663,6 +2663,14 @@ isStaticAssert(const Settings &settings, const Token *tok) return false; } +static bool isSizeof(const Token *tok) +{ + if (!Token::simpleMatch(tok, "(")) + return false; + + return Token::simpleMatch(tok->astOperand1(), "sizeof"); +} + void CheckOther::checkDuplicateExpression() { { @@ -2846,7 +2854,8 @@ void CheckOther::checkDuplicateExpression() } else if (tok->astOperand1() && tok->astOperand2() && tok->str() == ":" && tok->astParent() && tok->astParent()->str() == "?") { if (!tok->astOperand1()->values().empty() && !tok->astOperand2()->values().empty() && isEqualKnownValue(tok->astOperand1(), tok->astOperand2()) && !isVariableChanged(tok->astParent(), /*indirect*/ 0, *mSettings) && - isConstStatement(tok->astOperand1(), mSettings->library) && isConstStatement(tok->astOperand2(), mSettings->library)) + isConstStatement(tok->astOperand1(), mSettings->library) && isConstStatement(tok->astOperand2(), mSettings->library) && + !isSizeof(tok->astOperand1()) && !isSizeof(tok->astOperand2())) duplicateValueTernaryError(tok); else if (isSameExpression(true, tok->astOperand1(), tok->astOperand2(), *mSettings, false, true, &errorPath)) duplicateExpressionTernaryError(tok, std::move(errorPath)); From bce3fbe365a01b753f58159da28bcd8c65e6ca12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Mon, 12 May 2025 10:13:42 +0200 Subject: [PATCH 3/4] simplify test + add positive test --- test/testother.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/test/testother.cpp b/test/testother.cpp index 60131084abf..b6fbc1dda51 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -307,7 +307,8 @@ class TestOther : public TestFixture { TEST_CASE(knownConditionFloating); TEST_CASE(knownConditionPrefixed); - TEST_CASE(ternarySameValuePlatformDependent); // #13773 + TEST_CASE(ternarySameValuePlatformDependent1); // #13773 + TEST_CASE(ternarySameValuePlatformDependent2); // #23773 } #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) @@ -13096,18 +13097,21 @@ class TestOther : public TestFixture { errout_str()); } - void ternarySameValuePlatformDependent() // #13773 + void ternarySameValuePlatformDependent1() // #13773 { - check("typedef float _Complex complex_f32;\n" - "typedef struct {\n" - " uint16_t real;\n" - " uint16_t imag;\n" - "} packed_complex;\n" - "void foo(void) {\n" - " b = a ? sizeof(packed_complex) : sizeof(complex_f32);\n" + check("void f(void) {\n" + " b = a ? sizeof(unsigned int) : sizeof(uint32_t);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } + + void ternarySameValuePlatformDependent2() // #13773 + { + check("void f(void) {\n" + " b = a ? sizeof(uint32_t) : sizeof(uint32_t);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2:30]: (style) Same expression in both branches of ternary operator. [duplicateExpressionTernary]\n", errout_str()); + } }; REGISTER_TEST(TestOther) From 682172619ab9b9c2e521fed39dfac7207cea7d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Mon, 12 May 2025 11:11:23 +0200 Subject: [PATCH 4/4] refactor tests --- test/testother.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/test/testother.cpp b/test/testother.cpp index b6fbc1dda51..eb6bb28b060 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -307,8 +307,7 @@ class TestOther : public TestFixture { TEST_CASE(knownConditionFloating); TEST_CASE(knownConditionPrefixed); - TEST_CASE(ternarySameValuePlatformDependent1); // #13773 - TEST_CASE(ternarySameValuePlatformDependent2); // #23773 + TEST_CASE(ternarySameValueSizeof); // #13773 } #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) @@ -13097,16 +13096,13 @@ class TestOther : public TestFixture { errout_str()); } - void ternarySameValuePlatformDependent1() // #13773 + void ternarySameValueSizeof() // #13773 { check("void f(void) {\n" " b = a ? sizeof(unsigned int) : sizeof(uint32_t);\n" "}\n"); ASSERT_EQUALS("", errout_str()); - } - void ternarySameValuePlatformDependent2() // #13773 - { check("void f(void) {\n" " b = a ? sizeof(uint32_t) : sizeof(uint32_t);\n" "}\n");