aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/analyzer/state-diagram-5-sarif.py
blob: 484da963ea2363c30fb489601c8d497f2067eea2 (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
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
import xml.etree.ElementTree as ET

from sarif import *

import pytest

@pytest.fixture(scope='function', autouse=True)
def sarif():
    return sarif_from_env()

def test_nested_types_in_xml_state(sarif):
    result = get_result_by_index(sarif, 0)

    assert result['level'] == 'note'

    events = result["codeFlows"][0]["threadFlows"][0]['locations']

    assert events[0]['location']['message']['text'] == 'here'
    state = get_xml_state(events, 0)

    memory_regions = state.find('memory-regions')
    assert memory_regions is not None

    stack = memory_regions.find('stack')
    assert stack is not None

    frame = stack.find('stack-frame')
    assert frame.get('function') == 'test'

    # We have:
    #   baz_arr[1].m_bars[1].m_foos[2].m_ints[1] = 42;

    # Verify that we correctly expand from the analyzer's bit-offset
    # representation to nested elements and fields.

    # "baz_arr":
    baz_arr = frame.find("variable[@name='baz_arr']")
    assert baz_arr.get('type') == 'struct baz[2]'

    # "baz_arr[1]":
    baz_arr_1 = baz_arr.find("element[@index='1']")
    assert baz_arr_1.get('type') == 'struct baz'

    assert baz_arr.find("element[@index='0']") is None

    # "baz_arr[1].m_bars":
    baz_arr_1_m_bars = baz_arr_1.find("field[@name='m_bars']")
    assert baz_arr_1_m_bars.get('type') == 'struct bar[2]'

    # "baz_arr[1].m_bars[1]"
    baz_arr_1_m_bars_1 = baz_arr_1_m_bars.find("element[@index='1']")
    assert baz_arr_1_m_bars_1.get('type') == 'struct bar'

    # "baz_arr[1].m_bars[1].m_foos"
    baz_arr_1_m_bars_1_m_foos = baz_arr_1_m_bars_1.find("field[@name='m_foos']")
    assert baz_arr_1_m_bars_1_m_foos.get('type') == 'struct foo[3]'

    # "baz_arr[1].m_bars[1].m_foos[2]"
    baz_arr_1_m_bars_1_m_foos_2 = baz_arr_1_m_bars_1_m_foos.find("element[@index='2']")
    assert baz_arr_1_m_bars_1_m_foos_2.get('type') == 'struct foo'
    
    # "baz_arr[1].m_bars[1].m_foos[2].m_ints"
    baz_arr_1_m_bars_1_m_foos_2_m_ints = baz_arr_1_m_bars_1_m_foos_2.find('field[@name="m_ints"]')
    assert baz_arr_1_m_bars_1_m_foos_2_m_ints.get('type') == 'int[4]'
    
    # "baz_arr[1].m_bars[1].m_foos[2].m_ints[1]"
    baz_arr_1_m_bars_1_m_foos_2_m_ints_1 = baz_arr_1_m_bars_1_m_foos_2_m_ints.find('element[@index="1"]')
    assert baz_arr_1_m_bars_1_m_foos_2_m_ints_1.get('type') == 'int'

    value = baz_arr_1_m_bars_1_m_foos_2_m_ints_1.find('value-of-region')
    constant = value.find('constant')
    assert constant.get('value') == '42'
    assert constant.get('type') == 'int'