aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/lib/sarif.py
blob: 384de2fb0154aae6c2b1b133a07a5c99e5ba8b97 (plain)
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
import json
import os
import xml.etree.ElementTree as ET

def sarif_from_env():
    # return parsed JSON content a SARIF_PATH file
    json_filename = os.environ['SARIF_PATH']
    json_filename += '.sarif'
    print('json_filename: %r' % json_filename)
    with open(json_filename) as f:
        json_data = f.read()
    return json.loads(json_data)

def get_location_artifact_uri(location):
    return location['physicalLocation']['artifactLocation']['uri']

def get_location_physical_region(location):
    return location['physicalLocation']['region']

def get_location_snippet_text(location):
    return location['physicalLocation']['contextRegion']['snippet']['text']

def get_location_relationships(location):
    return location['relationships']

def get_result_by_index(sarif, idx):
    runs = sarif['runs']
    run = runs[0]
    results = run['results']
    return results[idx]
    
def get_xml_state(events, event_idx):
    xml_src = events[event_idx]['properties']['gcc/diagnostic_event/xml_state']
    if 0:
        print(xml_src)
    xml = ET.fromstring(xml_src)
    assert xml.tag == 'state-diagram'
    return xml