#This script is designed to run with Understand - CodeCheck use base ("Understand::Codecheck"); use strict; use constant ERR1 => 'More than %1 characers are significant'; sub register_tr_text() { my $check = shift; $check->add_tr_text(ERR1); } sub name { return "5.1 Identifiers shall not rely on the significance of more than 31 characters";} sub description { return "5.1 (Required) Identifiers (internal and external) shall not rely on the significance of more than 31 characters.";} sub detailed_description { return <<"END_DESC"

The ISO standard requires internal identifiers to be distinct in the first 31 characters to guarantee code portability. This limitation shall not be exceeded, even if the compiler supports it. This rule shall apply across all name spaces. Macro names are also included and the 31 character limit applies before and after substitution.

The ISO standard requires external identifiers to be distinct in the first 6 characters, regardless of case, to guarantee optimal portability. However this limitation is particularly severe and is considered unnecessary. The intent of this rule is to sanction a relaxation of the ISO requirement to a degree commensurate with modern environments and it shall be confirmed that 31 character/ case significance is supported by the implementation.

Note that there is a related issue with using identifier names that differ by only one or a few characters, especially if the identifier names are long. The problem is heightened if the differences are in easily mis-read characters like 1 (one) and l (lower case L), 0 and O, 2 and Z, 5 and S, or n and h. It is recommended to ensure that identifier names are always easily visually distinguishable. Specific guidelines on this issue could be placed in the style guidelines (see section 4.2.2).

END_DESC } sub test_language { my $language = shift; return $language =~ /C\+\+/; #Handles C and C++ } sub test_entity { return 0;} sub test_global { return 1;} sub define_options{ my $check = shift; $check->option->integer("significantChars","Signficant Characters",31); } sub check { my $check = shift; my $significantChars = $check->option->lookup("significantChars"); my @ents = $check->db->ents("~file"); my @longEnts; foreach (@ents){ push @longEnts,$_ if length($_->name) > $significantChars; } my $prev; my $prevReported; foreach my $ent (sort {$a->name cmp $b->name;} @longEnts){ my $beginning = substr($ent->name,0,$significantChars); my $prevBegin = substr($prev->name,0,$significantChars) if $prev; if ($prevBegin eq $beginning && $prev->name ne $ent->name){ reportEnt($ent,$check); reportEnt($prev,$check) unless $prevReported; $prevReported = 1; }else{ $prev = $ent; $prevReported = 0; } } } sub reportEnt{ my ($ent, $check) = @_; my $ref = $ent->ref("define, declare"); $ref = $ent->ref unless $ref; $check->violation($ent,$ref->file,$ref->line,$ref->column,ERR1,$check->option->lookup("significantChars")); }