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