#This script is designed to run with Understand - CodeCheck use base qw(Understand::Codecheck); use strict; use Codecheck::Libraries::InfoSiftr qw(resolveTypedefs); use constant ERR1 => 'Violation: union used for "%1".'; use constant ERR2 => 'Violation: union "%1" is invalid.'; sub register_tr_text { my $check = shift; $check->add_tr_text(ERR1); $check->add_tr_text(ERR2); } sub name { '18.4 Unions shall not be used' } sub description { '18.4 (Required) Unions shall not be used.' } sub detailed_description { <<'END_DESC' Rule 18.3 prohibits the reuse of memory areas for unrelated purposes. However, even when memory is being reused for related purposes, there is still a risk that the data may be misinterpreted. Therefore, this rule prohibits the use of unions for any purpose. END_DESC } sub test_language { my $language = shift; return $language eq 'C++'; } sub test_entity { 0 } sub test_global { 1 } sub define_options { } sub check { my $check = shift; my %typelist; foreach my $file ($check->get_files) { my $type; foreach my $ref ($file->filerefs('define, declare', 'union', 0)) { $check->violation($ref->ent, $ref->file, $ref->line, $ref->column, ERR2, $ref->ent->longname); } foreach my $ref ($file->filerefs('define, declare', 'object, typedef', 0)) { if($typelist{$ref->ent->type}){ $type = $typelist{$ref->ent->type}; } else{ $type = resolveTypedefs($ref->ent->type, $check->db); $typelist{$ref->ent->type} = $type; } next unless $type =~ m! \b union \b !x; $check->violation($ref->ent, $ref->file, $ref->line, $ref->column, ERR1, $ref->ent->longname); } } return; }