Skip to content

Commit c92437c

Browse files
committed
add tests
1 parent 2bd0440 commit c92437c

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

test/testvalueflow.cpp

Lines changed: 37 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,42 @@ 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+
}
90669103
};
90679104

90689105
REGISTER_TEST(TestValueFlow)

0 commit comments

Comments
 (0)