#This script is designed to run with Understand - CodeCheck #Rule 6.4 Bit fields shall only be defined to be of type unsigned int or signed int. use base qw(Understand::Codecheck); use strict; use Codecheck::Libraries::InfoSiftr qw(resolveTypedefs getObjectBitFieldWidth); use constant ERR1 => 'Violation: bit field must be of type either "signed int" or "unsigned int".'; sub register_tr_text { my $check = shift; $check->add_tr_text(ERR1); } sub name { '6.4 Bit fields shall only be defined to be of type unsigned int or signed int.' } sub description { '6.4 Bit fields shall only be defined to be of type unsigned int or signed int.' } sub detailed_description { <<'END_DESC' Using int is implementation defined because bit fields of type int can be either signed or unsigned. The use of enum, short or char types for bit fields is not allowed because the behaviour is undefined. END_DESC } sub test_language { my $language = shift; return $language eq 'C++'; } sub test_entity { 1 } sub test_global { 0 } sub define_options { } sub check { my $check = shift; my $file = shift; return unless $file->kind->check('file'); my $lexer = $file->lexer(0); foreach my $ref ($file->filerefs('define', 'object', 0)) { my $ent = $ref->ent; # narrow down to bit fields (unsigned int var : 3) my $bits = getObjectBitFieldWidth($ent, $lexer); next unless defined $bits; my $type = resolveTypedefs($ent->type, $check->db) if $ent->type; if (!$ent->type || $type !~ m/ \b ( signed | unsigned ) \s+ int \b /x) { $check->violation($ent, $file, $ref->line, $ref->column, ERR1); } } return; }