Skip to content

Commit a4bd260

Browse files
authored
Fix #14457 (ValueType: char expression without signedness) (danmar#8174)
1 parent a438097 commit a4bd260

File tree

5 files changed

+20
-27
lines changed

5 files changed

+20
-27
lines changed

lib/symboldatabase.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,7 +1915,10 @@ void SymbolDatabase::setArrayDimensionsUsingValueFlow()
19151915
}
19161916

19171917
if (bits > 0 && bits <= 62) {
1918-
if (dimension.tok->valueType()->sign == ValueType::Sign::UNSIGNED)
1918+
auto sign = dimension.tok->valueType()->sign;
1919+
if (sign == ValueType::Sign::UNKNOWN_SIGN && dimension.tok->valueType()->type == ValueType::Type::CHAR)
1920+
sign = mDefaultSignedness;
1921+
if (sign == ValueType::Sign::UNSIGNED)
19191922
dimension.num = 1LL << bits;
19201923
else
19211924
dimension.num = 1LL << (bits - 1);
@@ -7635,9 +7638,7 @@ static const Token* parsedecl(const Token* type,
76357638

76367639
// Set signedness for integral types..
76377640
if (valuetype->isIntegral() && valuetype->sign == ValueType::Sign::UNKNOWN_SIGN) {
7638-
if (valuetype->type == ValueType::Type::CHAR)
7639-
valuetype->sign = defaultSignedness;
7640-
else if (valuetype->type >= ValueType::Type::SHORT)
7641+
if (valuetype->type >= ValueType::Type::SHORT)
76417642
valuetype->sign = ValueType::Sign::SIGNED;
76427643
}
76437644

@@ -8755,7 +8756,7 @@ ValueType::MatchResult ValueType::matchParameter(const ValueType *call, const Va
87558756
return ValueType::MatchResult::UNKNOWN;
87568757
}
87578758

8758-
if (call->isIntegral() && func->isIntegral() && call->sign != ValueType::Sign::UNKNOWN_SIGN && func->sign != ValueType::Sign::UNKNOWN_SIGN && call->sign != func->sign)
8759+
if (call->isIntegral() && func->isIntegral() && call->sign != func->sign)
87598760
return ValueType::MatchResult::FALLBACK1;
87608761

87618762
if (func->reference != Reference::None && (func->constness > call->constness || func->volatileness > call->volatileness))

test/testclangimport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1328,7 +1328,7 @@ class TestClangImport : public TestFixture {
13281328
const Token *tok = Token::findsimplematch(tokenizer.tokens(), "\"hello\"");
13291329
ASSERT(!!tok);
13301330
ASSERT(!!tok->valueType());
1331-
ASSERT_EQUALS("const signed char *", tok->valueType()->str());
1331+
ASSERT_EQUALS("const char *", tok->valueType()->str());
13321332
}
13331333

13341334
void stdinLoc() {

test/testcondition.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4601,11 +4601,7 @@ class TestCondition : public TestFixture {
46014601
" if (o[1] == '\\0') {}\n"
46024602
" }\n"
46034603
"}\n");
4604-
if (std::numeric_limits<char>::is_signed) {
4605-
ASSERT_EQUALS("[test.cpp:6:18]: (style) Condition 'o[1]=='\\0'' is always false [knownConditionTrueFalse]\n", errout_str());
4606-
} else {
4607-
ASSERT_EQUALS("[test.cpp:4:25] -> [test.cpp:6:18]: (style) Condition 'o[1]=='\\0'' is always false [knownConditionTrueFalse]\n", errout_str());
4608-
}
4604+
ASSERT_EQUALS("[test.cpp:6:18]: (style) Condition 'o[1]=='\\0'' is always false [knownConditionTrueFalse]\n", errout_str());
46094605

46104606
check("void f(int x) {\n" // #11449
46114607
" int i = x;\n"
@@ -5354,11 +5350,7 @@ class TestCondition : public TestFixture {
53545350
" buffer.back() == '\\n' ||\n"
53555351
" buffer.back() == '\\0') {}\n"
53565352
"}\n");
5357-
if (std::numeric_limits<char>::is_signed) {
5358-
ASSERT_EQUALS("[test.cpp:5:22]: (style) Condition 'buffer.back()=='\\0'' is always false [knownConditionTrueFalse]\n", errout_str());
5359-
} else {
5360-
ASSERT_EQUALS("[test.cpp:3:22] -> [test.cpp:5:22]: (style) Condition 'buffer.back()=='\\0'' is always false [knownConditionTrueFalse]\n", errout_str());
5361-
}
5353+
ASSERT_EQUALS("[test.cpp:5:22]: (style) Condition 'buffer.back()=='\\0'' is always false [knownConditionTrueFalse]\n", errout_str());
53625354

53635355
// #9353
53645356
check("struct X { std::string s; };\n"

test/testother.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2341,7 +2341,7 @@ class TestOther : public TestFixture {
23412341
checkInvalidPointerCast("void test(float* data) {\n"
23422342
" f.write((char*)data,sizeof(float));\n"
23432343
"}", dinit(CheckInvalidPointerCastOptions, $.inconclusive = true)); // #3639
2344-
ASSERT_EQUALS("[test.cpp:2:13]: (portability, inconclusive) Casting from float * to signed char * is not portable due to different binary data representations on different platforms. [invalidPointerCast]\n", errout_str());
2344+
ASSERT_EQUALS("[test.cpp:2:13]: (portability, inconclusive) Casting from float * to char * is not portable due to different binary data representations on different platforms. [invalidPointerCast]\n", errout_str());
23452345

23462346

23472347
checkInvalidPointerCast("long long* test(float* f) {\n"

test/testsymboldatabase.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ class TestSymbolDatabase : public TestFixture {
586586
TEST_CASE(valueType2);
587587
TEST_CASE(valueType3);
588588
TEST_CASE(valueTypeThis);
589+
TEST_CASE(valueTypeChar);
589590

590591
TEST_CASE(variadic1); // #7453
591592
TEST_CASE(variadic2); // #7649
@@ -7796,20 +7797,12 @@ class TestSymbolDatabase : public TestFixture {
77967797
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v7 ) ) ;");
77977798
ASSERT(f);
77987799
ASSERT(f->function());
7799-
if (std::numeric_limits<char>::is_signed) {
7800-
ASSERT_EQUALS(10, f->function()->tokenDef->linenr());
7801-
} else {
7802-
ASSERT_EQUALS(5, f->function()->tokenDef->linenr());
7803-
}
7800+
ASSERT_EQUALS(10, f->function()->tokenDef->linenr());
78047801

78057802
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v8 ) ) ;");
78067803
ASSERT(f);
78077804
ASSERT(f->function());
7808-
if (std::numeric_limits<char>::is_signed) {
7809-
ASSERT_EQUALS(5, f->function()->tokenDef->linenr());
7810-
} else {
7811-
ASSERT_EQUALS(11, f->function()->tokenDef->linenr());
7812-
}
7805+
ASSERT_EQUALS(11, f->function()->tokenDef->linenr());
78137806

78147807
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v9 ) ) ;");
78157808
ASSERT(f);
@@ -10145,6 +10138,13 @@ class TestSymbolDatabase : public TestFixture {
1014510138
ASSERT_EQUALS("const C *", typeOf("class C { void foo() const; }; void C::foo() const { *this = 0; }", "this"));
1014610139
}
1014710140

10141+
void valueTypeChar() {
10142+
Settings s = settings2;
10143+
s.platform.defaultSign = 's';
10144+
ASSERT_EQUALS("char", typeOf("char c; c = 'x';", "c =", true, &s));
10145+
ASSERT_EQUALS("char", typeOf("char buf[10]; buf[0] = 'x';", "[ 0 ]", true, &s));
10146+
}
10147+
1014810148
void variadic1() { // #7453
1014910149
{
1015010150
GET_SYMBOL_DB("CBase* create(const char *c1, ...);\n"

0 commit comments

Comments
 (0)