From 93fd8845b36417ae62dc856fdaecda96d7dcdc9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Sun, 12 Jan 2025 20:21:26 +0100 Subject: [PATCH] fixed #13363 - apply default signedness to char only (#7155) --- lib/symboldatabase.cpp | 4 ++-- test/testio.cpp | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 51b9f625c93..8008d40c4d1 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -7185,7 +7185,7 @@ static const Token* parsedecl(const Token* type, else if (enum_type->isUnsigned()) valuetype->sign = ValueType::Sign::UNSIGNED; else - valuetype->sign = defaultSignedness; + valuetype->sign = defaultSignedness; // TODO: this is implementation-dependent might be separate from char const ValueType::Type t = ValueType::typeFromString(enum_type->str(), enum_type->isLong()); if (t != ValueType::Type::UNKNOWN_TYPE) valuetype->type = t; @@ -7586,7 +7586,7 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to else if (tok->previous()->isSigned()) valuetype.sign = ValueType::Sign::SIGNED; else if (valuetype.isIntegral() && valuetype.type != ValueType::UNKNOWN_INT) - valuetype.sign = mDefaultSignedness; + valuetype.sign = (valuetype.type == ValueType::Type::CHAR) ? mDefaultSignedness : ValueType::Sign::SIGNED; setValueType(tok, valuetype); } diff --git a/test/testio.cpp b/test/testio.cpp index a314396ed71..7314e3eec04 100644 --- a/test/testio.cpp +++ b/test/testio.cpp @@ -77,6 +77,8 @@ class TestIO : public TestFixture { TEST_CASE(testPrintfParenthesis); // #8489 TEST_CASE(testStdDistance); // #10304 TEST_CASE(testParameterPack); // #11289 + + TEST_CASE(testDefaultSignInt); // #13363 } struct CheckOptions @@ -85,6 +87,7 @@ class TestIO : public TestFixture { bool inconclusive = false; bool portability = false; Platform::Type platform = Platform::Type::Unspecified; + char defaultSign = '\0'; bool onlyFormatStr = false; bool cpp = true; }; @@ -96,6 +99,7 @@ class TestIO : public TestFixture { settings1.severity.setEnabled(Severity::portability, options.portability); settings1.certainty.setEnabled(Certainty::inconclusive, options.inconclusive); PLATFORM(settings1.platform, options.platform); + settings1.platform.defaultSign = options.defaultSign; // Tokenize.. SimpleTokenizer tokenizer(settings1, *this); @@ -4933,6 +4937,22 @@ class TestIO : public TestFixture { "}\n"); ASSERT_EQUALS("", errout_str()); } + + // TODO: we need to run big tests with a platform that has unsigned chars + void testDefaultSignInt() { // #13363 + // Platform::defaultSign should only affect char + const char code[] = + "void f() {\n" + " double d = 1\n;" + " printf(\"%i\", int(d));\n" + "}\n"; + check(code); + ASSERT_EQUALS("", errout_str()); + check(code, dinit(CheckOptions, $.defaultSign = 's')); + ASSERT_EQUALS("", errout_str()); + check(code, dinit(CheckOptions, $.defaultSign = 'u')); + ASSERT_EQUALS("", errout_str()); + } }; REGISTER_TEST(TestIO)