.NAME Try-catch statement

.DESCRIPTION
	Where multiple handlers are provided in a single 
try-catch statement or function-try-block, any ellipsis 
(catch-all) handler shall occur last. 

.COMMENT V7.0

.SEVERITY Required

.COMMENT
Please refer to the MISRA C++:2008 document for further details (www.misra-cpp.com)

.COMMAND tcl

.CODE

::logiscope::include -once common utilities.tcl

proc compute {standardName tree} {
  variable CurrentParameterList
  # Check all ... nodes
  utilities::filterTree $tree [list [namespace current]::check]
}

proc check { node } {
  set curOperator [::logiscope::treeOperator $node]
  set curOperatorName [::logiscope::opName $curOperator]
  # Look for a try
  if {[string equal $curOperatorName try_block]} {
    # Parse each of its "catch" nodes
	set foundCatchAll 0
	::logiscope::forobj curChild [::logiscope::treeDown $node 1] treeChild {
	  # Check that we have a "catch" node indeed
	  if {[string equal [::utilities::getName $curChild] handler]} {
	    # Check if the catch is a "catch(...)"
		# The first child must be a var_parmlist
		set firstChild [::logiscope::treeOperator [::logiscope::treeDown $curChild 0]]
		set firstChildName [::logiscope::opName $firstChild]
		if {[string equal $firstChildName var_parmlist]} {
		  set foundCatchAll 1
		} else {
		  # If the catch is not a "catch (...)", check if there was one defined before
		  # If it is the case, the rule is violated
		  if {[expr $foundCatchAll == 1]} {
		    ::logiscope::addViolation $curChild
		    return 0	
		  }
		}
	  }
	}
  }
  return 1
}
