UCIS.UCDB

UCIS/UCDB.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# ==================================================================================================================== #
#               _____ ____    _        _     _   _  ____ ___ ____                                                      #
#   _ __  _   _| ____|  _ \  / \      / \   | | | |/ ___|_ _/ ___|                                                     #
#  | '_ \| | | |  _| | | | |/ _ \    / _ \  | | | | |    | |\___ \                                                     #
#  | |_) | |_| | |___| |_| / ___ \  / ___ \ | |_| | |___ | | ___) |                                                    #
#  | .__/ \__, |_____|____/_/   \_\/_/   \_(_)___/ \____|___|____/                                                     #
#  |_|    |___/                                                                                                        #
# ==================================================================================================================== #
# Authors:                                                                                                             #
#   Artur Porebski (Aldec Inc.)                                                                                        #
#   Michal Pacula  (Aldec Inc.)                                                                                        #
#                                                                                                                      #
# License:                                                                                                             #
# ==================================================================================================================== #
# Copyright 2021-2022 Electronic Design Automation Abstraction (EDA²)                                                  #
#                                                                                                                      #
# Licensed under the Apache License, Version 2.0 (the "License");                                                      #
# you may not use this file except in compliance with the License.                                                     #
# You may obtain a copy of the License at                                                                              #
#                                                                                                                      #
#          http://www.apache.org/licenses/LICENSE-2.0                                                                  #
#                                                                                                                      #
# Unless required by applicable law or agreed to in writing, software                                                  #
# distributed under the License is distributed on an "AS IS" BASIS,                                                    #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.                                             #
# See the License for the specific language governing permissions and                                                  #
# limitations under the License.                                                                                       #
#                                                                                                                      #
# SPDX-License-Identifier: Apache-2.0                                                                                  #
# ==================================================================================================================== #
#
"""Data model of the UCDB format."""
from collections import namedtuple, defaultdict
from itertools import groupby
from operator import attrgetter
from pathlib import Path
from typing import List, Tuple, Dict, DefaultDict

from lxml import etree
from pyTooling.Decorators import export

from pyEDAA.UCIS.Cobertura import Class, Coverage, Package


UCDB_EXCLUDE_PRAGMA = 0x00000020
UCDB_EXCLUDE_FILE = 0x00000040
UCDB_EXCLUDE_INST = 0x00000080
UCDB_EXCLUDE_AUTO = 0x00000100

UCDB_EXCLUDED = (
	UCDB_EXCLUDE_FILE | UCDB_EXCLUDE_PRAGMA | UCDB_EXCLUDE_INST | UCDB_EXCLUDE_AUTO
)

StatementData = namedtuple(
	"StatementData",
	["file", "line", "index", "instance", "hits"],
)


@export
class UcdbParserException(Exception):
	"""Base-class for other UCDB Parser exceptions"""


@export
class InternalErrorOccurred(UcdbParserException):
	"""Raised when internal error occurred"""


@export
class Parser:
	_mergeInstances: bool
	_tree: etree._ElementTree
	_nsmap: Dict
	_coverage: Coverage
	statementsCount: int
	statementsCovered: int

	def __init__(self, ucdbFile: Path, mergeInstances: bool):
		self._mergeInstances = mergeInstances

		with ucdbFile.open("r") as filename:
			self._tree = etree.parse(filename)

		self._nsmap = {
			k: v for (k, v) in self._tree.getroot().nsmap.items() if k is not None
		}

		self._coverage = Coverage()

		self.statementsCount = 0
		self.statementsCovered = 0

	def getCoberturaModel(self) -> Coverage:
		self._parseStatementCoverage()

		return self._coverage

	def _groupByIndex(self, statements: List[StatementData]) -> List[StatementData]:
		groupedStmts = []

		sortedStmts = sorted(statements, key=attrgetter("index"))
		for index, stmts in groupby(sortedStmts, key=attrgetter("index")):
			hit = any((stmt.hits for stmt in stmts))

			groupedStmts.append(
				StatementData(
					file=statements[0].file,
					line=statements[0].line,
					index=index,
					instance="",
					hits=hit,
				)
			)

		return groupedStmts

	def _parseStatementCoverage(self) -> None:
		scopes = self._tree.xpath(
			"/ux:ucdb/ux:scope[.//ux:bin[@type='STMTBIN']]", namespaces=self._nsmap
		)

		if not isinstance(scopes, list):
			raise InternalErrorOccurred(f"Unexpected type: '{scopes.__class__.__name__}'.")

		nodes: List[etree._Element] = []

		for scopeNode in scopes:
			if not isinstance(scopeNode, etree._Element):
				raise InternalErrorOccurred(f"Unexpected type: '{scopeNode.__class__.__name__}'.")

			typeName = scopeNode.get("type")

			if typeName is None:
				raise InternalErrorOccurred("Unexpected 'None' value.")

			if typeName.startswith("DU_"):
				continue

			statementBins = scopeNode.xpath(".//ux:bin[@type='STMTBIN']", namespaces=self._nsmap)

			if not isinstance(statementBins, list):
				raise InternalErrorOccurred(f"Unexpected type: '{statementBins.__class__.__name__}'.")

			for statementBin in statementBins:
				if not isinstance(statementBin, etree._Element):
					raise InternalErrorOccurred(f"Unexpected type: '{statementBin.__class__.__name__}'.")

				nodes.append(statementBin)

		statements: DefaultDict[str, DefaultDict[int, List]] = defaultdict(lambda: defaultdict(list))

		for node in nodes:
			workdir, stmtData = self._parseStatementNode(node)
			self._coverage.addSource(workdir)

			flags = node.get("flags")

			if flags is None:
				raise InternalErrorOccurred("Unexpected 'None' value.")

			if int(flags, 16) & UCDB_EXCLUDED:
				_ = statements[stmtData.file]
				continue

			statements[stmtData.file][stmtData.line].append(stmtData)

		for file, lines in statements.items():
			package = Package(file)
			coberturaClass = Class(file, file)
			package.addClass(coberturaClass)
			self._coverage.addPackage(package)

			for line, lineStmts in lines.items():
				if self._mergeInstances:
					lineStmts = self._groupByIndex(lineStmts)

				self.statementsCount += len(lineStmts)

				covered = len(list(filter(attrgetter("hits"), lineStmts)))
				hit = int(covered == len(lineStmts))

				self.statementsCovered += covered

				coberturaClass.addStatement(line, hit)

	def _parseStatementNode(self, node) -> Tuple[str, StatementData]:
		srcNode = node.find("./ux:src", namespaces=self._nsmap)
		workdir = srcNode.get("workdir")

		instancePath = ".".join(
			(scope.get("name") for scope in node.iterancestors("{*}scope"))
		)

		stmtIndex = int(
			node.find("./ux:attr[@key='#SINDEX#']", namespaces=self._nsmap).text
		)

		count = int(node.find("./ux:count", namespaces=self._nsmap).text)

		stmtData = StatementData(
			file=srcNode.get("file"),
			line=int(srcNode.get("line")),
			index=stmtIndex,
			instance=instancePath,
			hits=count,
		)

		return workdir, stmtData