Skip to content

Commit d62192a

Browse files
committed
add tests
1 parent 7a4b6df commit d62192a

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

test/testother.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11240,6 +11240,15 @@ class TestOther : public TestFixture {
1124011240
"};\n"
1124111241
"void f(S<char, 3> s) {}\n");
1124211242
ASSERT_EQUALS("", errout_str());
11243+
11244+
Settings settingsUnix32 = settingsBuilder().platform(Platform::Type::Unix32).build();
11245+
check("struct S {\n" // #13850
11246+
" int i0 : 32;\n"
11247+
" int i1 : 16;\n"
11248+
" unsigned short u16;\n"
11249+
"};\n"
11250+
"void f(S s) {}\n", true, true, true, false, &settingsUnix32);
11251+
ASSERT_EQUALS("", errout_str());
1124311252
}
1124411253

1124511254
void checkComparisonFunctionIsAlwaysTrueOrFalse() {

test/testvalueflow.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ class TestValueFlow : public TestFixture {
175175
mNewTemplate = true;
176176

177177
TEST_CASE(performanceIfCount);
178+
TEST_CASE(bitfields);
178179
}
179180

180181
static bool isNotTokValue(const ValueFlow::Value &val) {
@@ -9063,6 +9064,48 @@ class TestValueFlow : public TestFixture {
90639064
"}\n";
90649065
ASSERT_EQUALS(1U, tokenValues(code, "v .", &s).size());
90659066
}
9067+
9068+
#define testBitfields(structBody, expectedSize) testBitfields_(__FILE__, __LINE__, structBody, expectedSize)
9069+
void testBitfields_(const char *file, int line, const std::string &structBody, std::size_t expectedSize) {
9070+
const std::string code = "struct S { " + structBody + " }; const std::size_t size = sizeof(S);";
9071+
const auto values = tokenValues(code.c_str(), "( S");
9072+
ASSERT_LOC(!values.empty(), file, line);
9073+
ASSERT_EQUALS_LOC(expectedSize, values.back().intvalue, file, line);
9074+
}
9075+
9076+
void bitfields() {
9077+
9078+
// #13653
9079+
testBitfields("unsigned int data_rw: 1;\n"
9080+
"unsigned int page_address: 4;\n"
9081+
"unsigned int register_address: 3;\n",
9082+
4);
9083+
9084+
testBitfields("unsigned char data_rw: 1;\n"
9085+
"unsigned char page_address: 4;\n"
9086+
"unsigned char register_address: 3;\n",
9087+
1);
9088+
9089+
testBitfields("unsigned int a : 1;\n"
9090+
"unsigned int b;\n"
9091+
"unsigned int c : 1;\n",
9092+
12);
9093+
9094+
testBitfields("unsigned int a : 1;\n"
9095+
"unsigned char b;\n"
9096+
"unsigned int c : 1;\n",
9097+
12);
9098+
9099+
testBitfields("unsigned int a : 31;\n"
9100+
"unsigned int b : 2;\n",
9101+
8);
9102+
9103+
// #13850
9104+
testBitfields("int a : 32;\n"
9105+
"int b : 16;\n"
9106+
"unsigned short c;\n",
9107+
8);
9108+
}
90669109
};
90679110

90689111
REGISTER_TEST(TestValueFlow)

0 commit comments

Comments
 (0)