.NAME Use of goto and switch in try catch block

.DESCRIPTION
	Control shall not be transferred into a try or catch block 
using a goto or a switch statement. 

.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 label nodes under try or catch nodes
  variable labels
  set labels {}
  utilities::filterTree $tree [list [namespace current]::checkLabels]
  utilities::filterTree $tree [list [namespace current]::checkGotos]
}

proc checkLabelNames { node } {
	variable labels
	if { [::utilities::isTreeOperator $node label] } {
		set name [::utilities::getName [::logiscope::treeDown $node 0]]
		lappend labels $name
	}
	return 1
}

proc checkLabelCases { node } {
	if { [::utilities::isTreeOperator $node switch] } {
		return 0
	}
	if { [::utilities::isTreeOperator $node case] } {
		::logiscope::addViolation $node
	}
	return 1
}

proc checkLabels { node } {
	if { [::utilities::isTreeOperator $node try_block]} {
		utilities::filterTree $node [list [namespace current]::checkLabelNames]
		utilities::filterTree $node [list [namespace current]::checkLabelCases]
		return 0
	}
	return 1
}

proc checkGotos { node } {
	variable labels
	if { [::utilities::isTreeOperator $node goto] } {
		set name [::utilities::getName [::logiscope::treeDown $node 0]]
		if { [lsearch -exact $labels $name] >= 0 } {
			::logiscope::addViolation $node
		}
	}
	return 1
}
