2 from clang
.cindex
import Config
4 if "CLANG_LIBRARY_PATH" in os
.environ
:
5 Config
.set_library_path(os
.environ
["CLANG_LIBRARY_PATH"])
7 from clang
.cindex
import *
8 from .util
import get_tu
13 # FIXME: We need support for invalid translation units to test better.
16 class TestDiagnostics(unittest
.TestCase
):
17 def test_diagnostic_warning(self
):
18 tu
= get_tu("int f0() {}\n")
19 self
.assertEqual(len(tu
.diagnostics
), 1)
20 self
.assertEqual(tu
.diagnostics
[0].severity
, Diagnostic
.Warning)
21 self
.assertEqual(tu
.diagnostics
[0].location
.line
, 1)
22 self
.assertEqual(tu
.diagnostics
[0].location
.column
, 11)
24 tu
.diagnostics
[0].spelling
, "non-void function does not return a value"
27 def test_diagnostic_note(self
):
28 # FIXME: We aren't getting notes here for some reason.
29 tu
= get_tu("#define A x\nvoid *A = 1;\n")
30 self
.assertEqual(len(tu
.diagnostics
), 1)
31 self
.assertEqual(tu
.diagnostics
[0].severity
, Diagnostic
.Error
)
32 self
.assertEqual(tu
.diagnostics
[0].location
.line
, 2)
33 self
.assertEqual(tu
.diagnostics
[0].location
.column
, 7)
34 self
.assertIn("incompatible", tu
.diagnostics
[0].spelling
)
36 # self.assertEqual(tu.diagnostics[1].severity, Diagnostic.Note)
37 # self.assertEqual(tu.diagnostics[1].location.line, 1)
38 # self.assertEqual(tu.diagnostics[1].location.column, 11)
39 # self.assertEqual(tu.diagnostics[1].spelling, 'instantiated from')
41 def test_diagnostic_fixit(self
):
42 tu
= get_tu("struct { int f0; } x = { f0 : 1 };")
43 self
.assertEqual(len(tu
.diagnostics
), 1)
44 self
.assertEqual(tu
.diagnostics
[0].severity
, Diagnostic
.Warning)
45 self
.assertEqual(tu
.diagnostics
[0].location
.line
, 1)
46 self
.assertEqual(tu
.diagnostics
[0].location
.column
, 26)
47 self
.assertRegex(tu
.diagnostics
[0].spelling
, "use of GNU old-style.*")
48 self
.assertEqual(len(tu
.diagnostics
[0].fixits
), 1)
49 self
.assertEqual(tu
.diagnostics
[0].fixits
[0].range.start
.line
, 1)
50 self
.assertEqual(tu
.diagnostics
[0].fixits
[0].range.start
.column
, 26)
51 self
.assertEqual(tu
.diagnostics
[0].fixits
[0].range.end
.line
, 1)
52 self
.assertEqual(tu
.diagnostics
[0].fixits
[0].range.end
.column
, 30)
53 self
.assertEqual(tu
.diagnostics
[0].fixits
[0].value
, ".f0 = ")
55 def test_diagnostic_range(self
):
56 tu
= get_tu('void f() { int i = "a"; }')
57 self
.assertEqual(len(tu
.diagnostics
), 1)
58 self
.assertEqual(tu
.diagnostics
[0].severity
, Diagnostic
.Error
)
59 self
.assertEqual(tu
.diagnostics
[0].location
.line
, 1)
60 self
.assertEqual(tu
.diagnostics
[0].location
.column
, 16)
61 self
.assertRegex(tu
.diagnostics
[0].spelling
, "incompatible pointer to.*")
62 self
.assertEqual(len(tu
.diagnostics
[0].fixits
), 0)
63 self
.assertEqual(len(tu
.diagnostics
[0].ranges
), 1)
64 self
.assertEqual(tu
.diagnostics
[0].ranges
[0].start
.line
, 1)
65 self
.assertEqual(tu
.diagnostics
[0].ranges
[0].start
.column
, 20)
66 self
.assertEqual(tu
.diagnostics
[0].ranges
[0].end
.line
, 1)
67 self
.assertEqual(tu
.diagnostics
[0].ranges
[0].end
.column
, 23)
68 with self
.assertRaises(IndexError):
69 tu
.diagnostics
[0].ranges
[1].start
.line
71 def test_diagnostic_category(self
):
72 """Ensure that category properties work."""
73 tu
= get_tu("int f(int i) { return 7; }", all_warnings
=True)
74 self
.assertEqual(len(tu
.diagnostics
), 1)
77 self
.assertEqual(d
.severity
, Diagnostic
.Warning)
78 self
.assertEqual(d
.location
.line
, 1)
79 self
.assertEqual(d
.location
.column
, 11)
81 self
.assertEqual(d
.category_number
, 2)
82 self
.assertEqual(d
.category_name
, "Semantic Issue")
84 def test_diagnostic_option(self
):
85 """Ensure that category option properties work."""
86 tu
= get_tu("int f(int i) { return 7; }", all_warnings
=True)
87 self
.assertEqual(len(tu
.diagnostics
), 1)
90 self
.assertEqual(d
.option
, "-Wunused-parameter")
91 self
.assertEqual(d
.disable_option
, "-Wno-unused-parameter")
93 def test_diagnostic_children(self
):
94 tu
= get_tu("void f(int x) {} void g() { f(); }")
95 self
.assertEqual(len(tu
.diagnostics
), 1)
99 self
.assertEqual(len(children
), 1)
100 self
.assertEqual(children
[0].severity
, Diagnostic
.Note
)
101 self
.assertRegex(children
[0].spelling
, ".*declared here")
102 self
.assertEqual(children
[0].location
.line
, 1)
103 self
.assertEqual(children
[0].location
.column
, 6)
105 def test_diagnostic_string_repr(self
):
106 tu
= get_tu("struct MissingSemicolon{}")
107 self
.assertEqual(len(tu
.diagnostics
), 1)
108 d
= tu
.diagnostics
[0]
112 "<Diagnostic severity 3, location <SourceLocation file 't.c', line 1, column 26>, spelling \"expected ';' after struct\">",