Home | History | Annotate | Download | only in cindex
      1 from clang.cindex import *
      2 
      3 def tu_from_source(source):
      4     index = Index.create()
      5     tu = index.parse('INPUT.c', unsaved_files = [('INPUT.c', source)])
      6     return tu
      7 
      8 # FIXME: We need support for invalid translation units to test better.
      9 
     10 def test_diagnostic_warning():
     11     tu = tu_from_source("""int f0() {}\n""")
     12     assert len(tu.diagnostics) == 1
     13     assert tu.diagnostics[0].severity == Diagnostic.Warning
     14     assert tu.diagnostics[0].location.line == 1
     15     assert tu.diagnostics[0].location.column == 11
     16     assert (tu.diagnostics[0].spelling ==
     17             'control reaches end of non-void function')
     18 
     19 def test_diagnostic_note():
     20     # FIXME: We aren't getting notes here for some reason.
     21     index = Index.create()
     22     tu = tu_from_source("""#define A x\nvoid *A = 1;\n""")
     23     assert len(tu.diagnostics) == 1
     24     assert tu.diagnostics[0].severity == Diagnostic.Warning
     25     assert tu.diagnostics[0].location.line == 2
     26     assert tu.diagnostics[0].location.column == 7
     27     assert 'incompatible' in tu.diagnostics[0].spelling
     28 #    assert tu.diagnostics[1].severity == Diagnostic.Note
     29 #    assert tu.diagnostics[1].location.line == 1
     30 #    assert tu.diagnostics[1].location.column == 11
     31 #    assert tu.diagnostics[1].spelling == 'instantiated from'
     32 
     33 def test_diagnostic_fixit():
     34     index = Index.create()
     35     tu = tu_from_source("""struct { int f0; } x = { f0 : 1 };""")
     36     assert len(tu.diagnostics) == 1
     37     assert tu.diagnostics[0].severity == Diagnostic.Warning
     38     assert tu.diagnostics[0].location.line == 1
     39     assert tu.diagnostics[0].location.column == 31
     40     assert tu.diagnostics[0].spelling.startswith('use of GNU old-style')
     41     assert len(tu.diagnostics[0].fixits) == 1
     42     assert tu.diagnostics[0].fixits[0].range.start.line == 1
     43     assert tu.diagnostics[0].fixits[0].range.start.column == 26
     44     assert tu.diagnostics[0].fixits[0].range.end.line == 1
     45     assert tu.diagnostics[0].fixits[0].range.end.column == 30
     46     assert tu.diagnostics[0].fixits[0].value == '.f0 = '
     47 
     48 def test_diagnostic_range():
     49     index = Index.create()
     50     tu = tu_from_source("""void f() { int i = "a" + 1; }""")
     51     assert len(tu.diagnostics) == 1
     52     assert tu.diagnostics[0].severity == Diagnostic.Warning
     53     assert tu.diagnostics[0].location.line == 1
     54     assert tu.diagnostics[0].location.column == 16
     55     assert tu.diagnostics[0].spelling.startswith('incompatible pointer to')
     56     assert len(tu.diagnostics[0].fixits) == 0
     57     assert len(tu.diagnostics[0].ranges) == 1
     58     assert tu.diagnostics[0].ranges[0].start.line == 1
     59     assert tu.diagnostics[0].ranges[0].start.column == 20
     60     assert tu.diagnostics[0].ranges[0].end.line == 1
     61     assert tu.diagnostics[0].ranges[0].end.column == 27
     62     try:
     63       tu.diagnostics[0].ranges[1].start.line
     64     except IndexError:
     65       assert True
     66     else:
     67       assert False
     68       
     69 
     70