.NAME Identifiers typographically unambiguous

.DESCRIPTION
	Different identifiers shall be typographically unambiguous. 

.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} {	
	# Check all declaration nodes
	utilities::filterTree $tree [list [namespace current]::check]
}

# getCodedName
# goal: get value of parameter name with some changes:
#  - no more underscore
#  - 0 replaced by O
#  - 1 replaced by I
#  - l replaced by I
#  - i replaced by I
#  - 5 replaced by S
#  - 2 replaced by Z
#  - h replaced by n
#  - 8 replaced by B
#  - rn replaced by m
proc getCodedName { name } {
	return [string map { _ "" rn m 0 O 1 I l I i I 5 S 2 Z h n 8 B } $name]
}

proc check { node } {
	set returnValue 1
	if { [::utilities::isTreeOperator $node identifier] } {
		set identName [::utilities::getName $node]
		if { ![info exists [namespace current]::fullNames($identName)] } {
			# identifier never seen
			set [namespace current]::fullNames($identName) $node
			# check if a known identifier is like this one when ignoring case
			set lowerIdent [string tolower $identName]
			if { [info exists ::lowerNames($lowerIdent)] } {
				# violation: same identifier with different case exists
				::logiscope::addViolation $node
			} else {
				set ::lowerNames($lowerIdent) $node
				# check if known identifier with same coded value exists
				set codedName [getCodedName $identName]
				if { [info exists ::codedNames($codedName)] } {
					::logiscope::addViolation $node
				} else {
					set ::codedNames($codedName) $node
				}
			}
		}
		# else identifier already seen then nothing to do
		set returnValue 0
	}
	return $returnValue
}
