use base ("Understand::Codecheck"); use Understand::Flowchart; use strict; sub name { return "0-1-1 A project shall not contain unreachable code"; } sub description { return "0-1-1 (Required) A project shall not contain unreachable code."; } sub detailed_description { return <<"END_DESC"

Rationale
Code is unreachable if there is no syntactic (control flow) path to it. If such code exists, it is unclear if this is intentional or simply that an appropriate path has been accidentally omitted. Compilers may choose not to generate code for these constructs, meaning that, even if the unreachable code is intentional, it may not be present in the final executable code.

Missing statements, often caused by editing activities, are a common source of unreachable code.

Example
int16_t with_unreach ( int16_t para ) 
{ 
   int16_t local;
   local = 0;
   switch ( para ) 
   {
      local = para;   // unreachable – Non-compliant
      case 1: 
      { 
         break; 
      }
      default: 
      { 
         break; 
      }
   } 
   return para; 
   para++;            // unreachable – Non-compliant 
}
END_DESC
}

sub test_language {
	my $language = shift;
	return $language =~ /C\+\+/;
  return 1;
}

sub test_entity {
  my $entity = shift;
  my $result = isValidFlowchartEntity($entity);
  return $result ? 1 : -1;
}

sub test_global {
	return 0;
}

sub define_option{
  my $check = shift;
}

sub check {
  my $check = shift;
  my $file = shift;
  return unless $file->kind->check("file ~dll");
  
  my @funcRefs = $file->filerefs("definein, body declarein","c function ~unknown ~unresolved".
   ",java method ~abstract".
   ",fortran subroutine, fortran function, fortran main program ~unknown ~unresolved".
   ",c# method ~abstract".
   ",vhdl procedure, vhdl function".
   ",ada procedure, ada function, ada task",1);
  
  FUNC:foreach my $funcRef (@funcRefs){
    my $func = $funcRef->ent;
    my @nodes = createFlowchart($func,1,0);
    foreach my $node(@nodes){
      if ($node->get("unreachable")){
        $check->violation($func,$file,$node->get("line"),$node->get("column"),"Unreachable Code"); 
        next FUNC;
      }
    }
  }
}