use base ("Understand::Codecheck"); use Understand::Flowchart; use strict; sub name { return "14.1 There shall be no unreachable code"; } sub description { return "14.1 (Required) There shall be no unreachable code."; } sub detailed_description { return "This rule refers to code which cannot under any circumstances be reached, and which can be identified as such at compile time. Code that can be reached but may never be executed is excluded from the rule (e.g. defensive programming code).
A portion of code is unreachable if there is no control flow path from the relevant entry point to that code. For example, unlabelled code following an unconditional control transfer is unreachable:
switch (event) 
{ 
   case E_wakeup: 
      do_wakeup(); 
      break;       /* unconditional control transfer   */ 
      do_more();   /* Not compliant - unreachable code */ 
      /* ... */ 
   default: 
      /* ... */ 
      break; 
}

A whole function will be unreachable if there is no means by which it can be called. Code that is excluded by pre-processor directives is not present following pre-processing, and is therefore not subject to this rule."; } 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; } } } }