test_diagnostics.py 3.2 KB

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