.NAME Static storage duration objects

.DESCRIPTION
	The identifier name of a non-member object or function 
with static storage duration should not be reused. 

.COMMENT V7.0

.SEVERITY Advisory

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

.REQUIRED cpp_semantic

.COMMAND tcl

.CODE

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

proc compute {standardName tree} {

	variable staticVarsIds
	variable staticVarsNames
	if {[utilities::isLastFile]} {
		checkStaticVariables
		checkRegularVariables
		checkScopeNames
  }	
}

proc checkStaticVariables {} {
	variable staticVarsIds
	variable staticVarsNames
	set staticVarsIds ""
	set staticVarsNames ""
	foreach curScope [::Scopes::getAllScopes] {
		foreach curVar [::Semantic::getAttribute $curScope variables] {
			set varName [::Semantic::getAttribute $curVar name]
			set varType [::Semantic::getAttribute $curVar type]
			set varAccess [::Semantic::getAttribute $curVar qualifier]
			if {[expr [lsearch $varAccess static] != -1]} {
				if {[expr [lsearch $staticVarsNames $varName] == -1]} {
					lappend staticVarsIds $curVar
					lappend staticVarsNames $varName
					::Semantic::setAttribute $curVar defined 1
				} else {
					::Semantic::setAttribute $curVar defined 0
					::Semantic::violation $curVar "MISRA_2_10_5" "MISRA_2_10_5 - The identifier name of a non-member object or function with static storage duration should not be reused."
				}
			}
		}
	}
}

proc checkRegularVariables {} {
	variable staticVarsIds
	variable staticVarsNames
	foreach curScope [::Scopes::getAllScopes] {
		foreach curVar [::Semantic::getAttribute $curScope variables] {
			set varName [::Semantic::getAttribute $curVar name]
			set varAccess [::Semantic::getAttribute $curVar qualifier]
			if {[lsearch $varAccess static] == -1} {
				set found [lsearch $staticVarsNames $varName]
				if {$found != -1} {
					::Semantic::violation $curVar "MISRA_2_10_5" "MISRA_2_10_5 - The identifier name of a non-member object or function with static storage duration should not be reused."

				}
			}
		}
	}
}

proc checkScopeNames {} {
	variable staticVarsIds
	variable staticVarsNames
	foreach curScope [::Scopes::getAllScopes] {
		set scopeName [::Semantic::getAttribute $curScope name]
		set found [lsearch $staticVarsNames $scopeName]
		if {$found != -1} {
			::Semantic::violation $curScope "MISRA_2_10_5" "MISRA_2_10_5 - The identifier name of a non-member object or function with static storage duration should not be reused."
		}
	}
}