refactor: introduce get_location_message_info

This commit is contained in:
Michael Hohn
2021-11-17 16:28:43 -08:00
committed by =Michael Hohn
parent 8036ea5ffc
commit 1f7e78b049
2 changed files with 13 additions and 5 deletions

View File

@@ -26,9 +26,7 @@ for runi in S.indices(sarif_struct, 'runs'):
#
for resi in S.indices(sarif_struct, 'runs', runi, 'results'):
result = S.get(sarif_struct, 'runs', runi, 'results', resi)
message = S.get(result, 'message', 'text')
artifact = S.get(result, 'locations', 0, 'physicalLocation', 'artifactLocation')
region = S.get(result, 'locations', 0, 'physicalLocation', 'region')
message, artifact, region = S.get_location_message_info(result)
l1, c1, l2, c2 = S.lineinfo(region)
filepath = "%s:%d:%d:%d:%d" % (artifact['uri'], l1, c1, l2, c2)
S.msg("%s: %s\n\n" % (filepath, message))

View File

@@ -6,6 +6,16 @@ MIN_PYTHON = (3, 7)
if sys.version_info < MIN_PYTHON:
sys.exit("Python %s.%s or later is required.\n" % MIN_PYTHON)
def get_location_message_info(result):
""" Given one of the results, extract message information.
The `result` typically starts from get(sarif_struct, 'runs', run_index, 'results', res_index)
"""
message = get(result, 'message', 'text')
artifact = get(result, 'locations', 0, 'physicalLocation', 'artifactLocation')
region = get(result, 'locations', 0, 'physicalLocation', 'region')
return (message, artifact, region)
def display_underlined(l1, c1, l2, c2, line, line_num):
""" Display the given line followed by a second line with underscores at the locations.
@@ -26,7 +36,7 @@ def underline_for_result(first_line, first_column, last_line, last_column, line,
"""Provide the underline for a result line.
first_line, first_column, last_line, last_column :
the region from S.lineinfo(region)
the region from lineinfo(region)
line:
the line of source
line_num:
@@ -109,5 +119,5 @@ def msg(message):
def dbg(message):
""" Print message to stderr """
sys.stdout.flush()
sys.stderr.write(message)
sys.stderr.write("warning: %s" % message)
sys.stderr.flush()