#This script is designed to run with Understand - CodeCheck use base ("Understand::Codecheck"); use strict; use constant ERR1 => 'Function %1 declared at block scope'; sub register_tr_text() { my $check = shift; $check->add_tr_text(ERR1); } sub name { return "3-1-2 Functions shall not be declared at block scope";} sub description { return "3-1-2 (Required) Functions shall not be declared at block scope";} sub detailed_description { return <<"END_DESC"
Rationale
A function declared at block scope will refer to a member of the enclosing namespace, and so the
declaration should be explicitly placed at the namespace level.
Additionally, where a declaration statement could either declare a function or an object, the
compiler will choose to declare the function. To avoid potential developer confusion over the
meaning of a declaration, functions should not be declared at block scope.
class A { }; void b1 ( ) { void f1 (); // Non-compliant - declaring a function in block scope A a (); // Non-compliant - appears to declare an object with no // arguments to constructor, but it too declares a // function 'a' returning type 'A' and taking no // parameters. }END_DESC } sub test_language { my $language = shift; return $language =~ /C\+\+/; #Handles C and C++ } sub test_entity { return 1;} sub test_global { return 0;} sub define_options{} sub check { my $check = shift; my $file = shift; return unless $file->kind->check("file ~unknown ~unresolved"); my @refs = $file->filerefs("declare","function ~member"); foreach my $ref (@refs){ next unless($ref->scope != $file); next unless($ref->ent->parent && $ref->ent->parent->kind eq "Namespace"); $check->violation($ref->ent,$file,$ref->line,$ref->column,ERR1,$ref->ent->name); } }