#This script is designed to run with Understand - CodeCheck #Rule 9-6-4 Named bit-fields with signed integer type shall have a length of more than one bit. use base qw(Understand::Codecheck); use strict; use Codecheck::Libraries::InfoSiftr qw(getObjectBitFieldWidth); use constant ERR1 => 'Violation: signed bit field has length of %1.'; sub register_tr_text { my $check = shift; $check->add_tr_text(ERR1); } sub name { '9-6-4 Named bit-fields with signed integer type shall have a length of more than one bit' } sub description { '9-6-4 (Required) Named bit-fields with signed integer type shall have a length of more than one bit.' } sub detailed_description { <<'END_DESC' The values which may be represented by a bit-field of length one may not meet developer expectations. Anonymous signed bit-fields of any length are allowed. 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; if ($ent->type && $ent->type =~ m/ \b signed \b /x) { if ($bits == 1) { $check->violation($ent, $file, $ref->line, $ref->column, ERR1, $bits); } } } return; }