Skip to content

Commit

Permalink
Keep track of which SLOC exist in a CodeNode
Browse files Browse the repository at this point in the history
Previously, a CodeNode could only recover region information in terms
of an extent (start line and end line) which was not sufficient to
export the information required by the new coverage format.

Signed-off-by: John Pennycook <john.pennycook@intel.com>
  • Loading branch information
Pennycook committed Mar 22, 2024
1 parent 9708283 commit cb126be
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
4 changes: 3 additions & 1 deletion bin/cbicov
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ if __name__ == "__main__":
"id": util.compute_file_hash(filename),
"lines": [],
}
# This initial implementation makes no attempt to compress
# consecutive lines, even though this is permitted.
for lines in exports[p][filename]:
covobject["lines"].append(lines)
covobject["lines"].extend(lines)
covarray.append(covobject)
util._validate_json(covarray, "coverage")
json_string = json.dumps(covarray)
Expand Down
14 changes: 12 additions & 2 deletions codebasin/file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self):
self.line_count = 0
self.start_line = -1
self.end_line = -1
self.lines = []
self.body = []

def empty(self):
Expand All @@ -39,7 +40,7 @@ def empty(self):
return False
return True

def add_line(self, phys_int, sloc_count, source=None):
def add_line(self, phys_int, sloc_count, source=None, lines=None):
"""
Add a line to this line group. Update the extent appropriately,
and if it's a countable line, add it to the line count.
Expand All @@ -54,6 +55,8 @@ def add_line(self, phys_int, sloc_count, source=None):
self.line_count += sloc_count
if source is not None:
self.body.append(source)
if lines is not None:
self.lines.extend(lines)

def reset(self):
"""
Expand All @@ -62,6 +65,7 @@ def reset(self):
self.line_count = 0
self.start_line = -1
self.end_line = -1
self.lines = []
self.body = []

def merge(self, line_group):
Expand All @@ -77,6 +81,7 @@ def merge(self, line_group):
line_group.start_line = self.start_line
self.start_line = min(self.start_line, line_group.start_line)

self.lines.extend(line_group.lines)
self.body.extend(line_group.body)

self.end_line = max(self.end_line, line_group.end_line)
Expand Down Expand Up @@ -125,6 +130,7 @@ def insert_code_node(tree, line_group):
line_group.end_line,
line_group.line_count,
line_group.body,
lines=line_group.lines,
)
tree.insert(new_node)

Expand All @@ -140,6 +146,7 @@ def insert_directive_node(tree, line_group, logical_line):
new_node.start_line = line_group.start_line
new_node.end_line = line_group.end_line
new_node.num_lines = line_group.line_count
new_node.lines = line_group.lines

# Issue a warning for unrecognized directives, but suppress warnings
# for common directives that shouldn't impact correctness.
Expand All @@ -156,7 +163,7 @@ def insert_directive_node(tree, line_group, logical_line):

tree.insert(new_node)

def parse_file(self, *, summarize_only=True, language=None):
def parse_file(self, *, summarize_only=False, language=None):
"""
Parse the file that this parser points at, build a SourceTree
representing this file, and return it.
Expand Down Expand Up @@ -197,6 +204,7 @@ def parse_file(self, *, summarize_only=True, language=None):
phys_int,
logical_line.local_sloc,
logical_line.flushed_line,
lines=logical_line.lines,
)

FileParser.handle_directive(
Expand All @@ -211,12 +219,14 @@ def parse_file(self, *, summarize_only=True, language=None):
groups["code"].add_line(
phys_int,
logical_line.local_sloc,
lines=logical_line.lines,
)
else:
groups["code"].add_line(
phys_int,
logical_line.local_sloc,
logical_line.flushed_line,
lines=logical_line.lines,
)
except StopIteration as it:
_, physical_loc = it.value
Expand Down
22 changes: 19 additions & 3 deletions codebasin/file_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ def __init__(self):
self.current_logical_line = one_space_line()
self.current_physical_start = 1
self.current_physical_end = None
self.lines = []
self.local_sloc = 0
self.category = None
self.flushed_line = None
Expand All @@ -433,12 +434,26 @@ def join(self, other_line):
"""
self.current_logical_line.join(other_line)

# This function isn't actually used any more, but can't be removed yet.
def physical_nonblank(self, n):
"""
Mark nonblank link in this logical like.
"""
self.local_sloc += n

def add_physical_lines(self, lines: list[int]) -> None:
"""
Add the specified physical lines to this logical line.
"""
self.lines.extend(lines)
self.local_sloc += len(lines)

def add_physical_line(self, line: int) -> None:
"""
Add the specified physical line to this logical line.
"""
self.add_physical_lines([line])

def physical_update(self, physical_line_num):
"""
Mark end of new physical line.
Expand All @@ -453,6 +468,7 @@ def physical_reset(self):
"""
self.current_physical_start = self.current_physical_end
local_sloc_copy = self.local_sloc
self.lines = []
self.local_sloc = 0
self.flushed_line = None
return local_sloc_copy
Expand Down Expand Up @@ -507,7 +523,7 @@ def c_file_source(fp, relaxed=False, directives_only=False):
cleaner.logical_newline()

if not current_physical_line.category() == "BLANK":
curr_line.physical_nonblank(1)
curr_line.add_physical_line(physical_line_num)

curr_line.join(current_physical_line)

Expand Down Expand Up @@ -583,7 +599,7 @@ def fortran_file_source(fp, relaxed=False):
)

if not current_physical_line.category() == "BLANK":
curr_line.physical_nonblank(src_c_line.local_sloc)
curr_line.add_physical_lines(src_c_line.lines)

curr_line.join(current_physical_line)

Expand Down Expand Up @@ -677,7 +693,7 @@ def asm_file_source(fp, relaxed=False):
cleaner.process(it.islice(line, 0, end))

if not current_physical_line.category() == "BLANK":
curr_line.physical_nonblank(1)
curr_line.add_physical_line(physical_line_num)

curr_line.join(current_physical_line)

Expand Down
13 changes: 12 additions & 1 deletion codebasin/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,11 +646,22 @@ class CodeNode(Node):
the original source.
"""

def __init__(self, start_line=-1, end_line=-1, num_lines=0, source=None):
def __init__(
self,
start_line=-1,
end_line=-1,
num_lines=0,
source=None,
lines=None,
):
super().__init__()
self.start_line = start_line
self.end_line = end_line
self.num_lines = num_lines
if lines is None:
self.lines = []
else:
self.lines = lines
self.source = source

def to_json(self, assoc):
Expand Down
20 changes: 13 additions & 7 deletions codebasin/walkers/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ class Exporter(TreeWalker):
Build a per-platform list of mappings.
"""

def __init__(self, codebase, *, hash_filenames=True):
def __init__(self, codebase, *, hash_filenames=True, export_regions=True):
super().__init__(None, None)
self.codebase = codebase
self.exports = None
self.hash_filenames = hash_filenames
self.export_regions = export_regions

def walk(self, state):
self.exports = collections.defaultdict(
Expand Down Expand Up @@ -49,12 +50,17 @@ def _export_node(self, _filename, _node, _map):
if isinstance(_node, CodeNode):
association = _map[_node]
for p in frozenset(association):
start_line = _node.start_line
end_line = _node.end_line
num_lines = _node.num_lines
self.exports[p][_filename].append(
(start_line, end_line, num_lines),
)
if self.export_regions:
start_line = _node.start_line
end_line = _node.end_line
num_lines = _node.num_lines
self.exports[p][_filename].append(
(start_line, end_line, num_lines),
)
else:
lines = _node.lines
print(_node.lines)
self.exports[p][_filename].append(lines)

next_filename = _filename
if isinstance(_node, FileNode):
Expand Down

0 comments on commit cb126be

Please sign in to comment.