1from clang.cindex import *
2from .util import get_tu
3
4# FIXME: We need support for invalid translation units to test better.
5
6def test_diagnostic_warning():
7    tu = get_tu('int f0() {}\n')
8    assert len(tu.diagnostics) == 1
9    assert tu.diagnostics[0].severity == Diagnostic.Warning
10    assert tu.diagnostics[0].location.line == 1
11    assert tu.diagnostics[0].location.column == 11
12    assert (tu.diagnostics[0].spelling ==
13            'control reaches end of non-void function')
14
15def test_diagnostic_note():
16    # FIXME: We aren't getting notes here for some reason.
17    tu = get_tu('#define A x\nvoid *A = 1;\n')
18    assert len(tu.diagnostics) == 1
19    assert tu.diagnostics[0].severity == Diagnostic.Warning
20    assert tu.diagnostics[0].location.line == 2
21    assert tu.diagnostics[0].location.column == 7
22    assert 'incompatible' in tu.diagnostics[0].spelling
23#    assert tu.diagnostics[1].severity == Diagnostic.Note
24#    assert tu.diagnostics[1].location.line == 1
25#    assert tu.diagnostics[1].location.column == 11
26#    assert tu.diagnostics[1].spelling == 'instantiated from'
27
28def test_diagnostic_fixit():
29    tu = get_tu('struct { int f0; } x = { f0 : 1 };')
30    assert len(tu.diagnostics) == 1
31    assert tu.diagnostics[0].severity == Diagnostic.Warning
32    assert tu.diagnostics[0].location.line == 1
33    assert tu.diagnostics[0].location.column == 26
34    assert tu.diagnostics[0].spelling.startswith('use of GNU old-style')
35    assert len(tu.diagnostics[0].fixits) == 1
36    assert tu.diagnostics[0].fixits[0].range.start.line == 1
37    assert tu.diagnostics[0].fixits[0].range.start.column == 26
38    assert tu.diagnostics[0].fixits[0].range.end.line == 1
39    assert tu.diagnostics[0].fixits[0].range.end.column == 30
40    assert tu.diagnostics[0].fixits[0].value == '.f0 = '
41
42def test_diagnostic_range():
43    tu = get_tu('void f() { int i = "a" + 1; }')
44    assert len(tu.diagnostics) == 1
45    assert tu.diagnostics[0].severity == Diagnostic.Warning
46    assert tu.diagnostics[0].location.line == 1
47    assert tu.diagnostics[0].location.column == 16
48    assert tu.diagnostics[0].spelling.startswith('incompatible pointer to')
49    assert len(tu.diagnostics[0].fixits) == 0
50    assert len(tu.diagnostics[0].ranges) == 1
51    assert tu.diagnostics[0].ranges[0].start.line == 1
52    assert tu.diagnostics[0].ranges[0].start.column == 20
53    assert tu.diagnostics[0].ranges[0].end.line == 1
54    assert tu.diagnostics[0].ranges[0].end.column == 27
55    try:
56      tu.diagnostics[0].ranges[1].start.line
57    except IndexError:
58      assert True
59    else:
60      assert False
61
62def test_diagnostic_category():
63    """Ensure that category properties work."""
64    tu = get_tu('int f(int i) { return 7; }', all_warnings=True)
65    assert len(tu.diagnostics) == 1
66    d = tu.diagnostics[0]
67
68    assert d.severity == Diagnostic.Warning
69    assert d.location.line == 1
70    assert d.location.column == 11
71
72    assert d.category_number == 2
73    assert d.category_name == 'Semantic Issue'
74
75def test_diagnostic_option():
76    """Ensure that category option properties work."""
77    tu = get_tu('int f(int i) { return 7; }', all_warnings=True)
78    assert len(tu.diagnostics) == 1
79    d = tu.diagnostics[0]
80
81    assert d.option == '-Wunused-parameter'
82    assert d.disable_option == '-Wno-unused-parameter'
83