.NAME Definition of macros

.DESCRIPTION
	Macros shall only be #define'd or #undef'd in the global namespace. 

.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 #define or #undef
  if {[string equal $curOperatorName pp_define] || [string equal $curOperatorName pp_undef]} {
	# Check if one of the predecessor nodes is a namespace declaration
	if {[expr [hasNamespaceAncestor $node] == 1]} {
	  ::logiscope::addViolation $node
	  return 0		
	}
  }
return 1
}

proc hasNamespaceAncestor {tree} {
  # Check the current tree node: is it an ancestor declaration?
  set curOperator [::logiscope::treeOperator $tree]
  set curOperatorName [::logiscope::opName $curOperator]
  # Look for a case
  if {[string equal $curOperatorName namespace_def]} {
    return 1
  }
  
  # If the current node is the tree root, don't recurse upwards anymore
  # Since we reached this code without having returned 1, this means we couldn't find a namespace declaration node, and must return 0
  if ([::logiscope::treeEqual $tree [::logiscope::treeRoot $tree]]) {
    return 0
  } else {
    return [hasNamespaceAncestor [::logiscope::treeUp $tree]]
  }
}
