generate an Info message for inefficient structure packing
Since PC-Lint already has the size and alignment information, it could determine if structures members are not arranged most efficiently.
For example, in a standard 32-bit system, a structure defined as
struct foo
{
uint32t a;
uint8t b;
uint16_t c;
}
is not most efficiently arranged, requiring an additional padding byte between b and c to achieve the necessary alignment for c. A more efficient method would be to define foo as
struct foo
{
uint32t a;
uint16t b;
uint8_t c;
}
I think Lint could determine this and generate some Info message for struct name.
Hi Michael,
Thank you for the suggestion. In this case, you can implement a query to report on this issue e.g.
-astquery(
persistent $size = 100
persistent $name = RecordDecl.getNameAsString
if FieldDecl {
FieldDecl : {
if getParent.isStruct{
echo{ $size = getType.getTypeSizeInBits}
echo{ $name = getParent.getNameAsString}
}
if getType.getTypeSizeInBits > $size {
getParent.getNameAsString == $name
message(8001 "Inefficient structure packing. '" currentnode "' is larger than the previous declaration.")
}
}
})
This query will report on the example provided e.g.
//lint -w1 +e8001
#include <stdint.h>
struct foo
{
uint32_t g;
uint16_t h;
uint8_t i;
};
struct foo1
{
uint32_t a;
uint8_t c;
uint16_t b; //Reports here.
};
Output:
Message 8001: (info -- Inefficient structure packing. 'foo1::b' is larger than the previous declaration.)
This query can be placed in a .lnt file and passed to PC-lint Plus as an argument. The documentation for queries can be found in section 16 of the Reference Manual. Note that this query will not work for the current version PC-lint Plus 2.0 or previous versions. The function needed in this case getTypeSizeInBits will be made available in the upcoming beta release. The beta announcement emails will be sent out soon.
-
Dave commented
Dear PC-lint Plus Support, do you know if it would be possible to somehow extend this query so that, in the case of structure members that are arrays, it only considers the size of the type of the array, rather than the size of the entire array? For example, I would not want the following structure to generate a message. Thank you!
struct foo
{
uint32_t a;
uint32_t b[4]; // Reports here, but I don't want it to.
}; -
Arpad Toth commented
This would be still useful for pclp.
-
Mike Diack commented
Note: By the way the PVS-Studio tool already performs this check about optimising structure alignment.