---
 bindings/python/clang/cindex.py                  |   37 ++++++++++++++++++++++
 bindings/python/tests/cindex/test_diagnostics.py |   34 ++++++++++++++++++--
 2 files changed, 68 insertions(+), 3 deletions(-)
diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py
index 474740a..e385ca2 100644
--- a/bindings/python/clang/cindex.py
+++ b/bindings/python/clang/cindex.py
@@ -262,16 +262,39 @@ class Diagnostic(object):
                 value = _clang_getDiagnosticFixIt(self.diag, key, byref(range))
                 if len(value) == 0:
                     raise IndexError
 
                 return FixIt(range, value)
 
         return FixItIterator(self)
 
+    @property
+    def category_number(self):
+        """The category number for this diagnostic."""
+        return _clang_getDiagnosticCategory(self)
+
+    @property
+    def category_name(self):
+        """The string name of the category for this diagnostic."""
+        return _clang_getDiagnosticCategoryName(self.category_number)
+
+    @property
+    def option(self):
+        """The command-line option that enables this diagnostic."""
+        return _clang_getDiagnosticOption(self, None)
+
+    @property
+    def disable_option(self):
+        """The command-line option that disables this diagnostic."""
+        disable = _CXString()
+        _clang_getDiagnosticOption(self, byref(disable))
+
+        return _CXString_getCString(disable)
+
     def __repr__(self):
         return "<Diagnostic severity %r, location %r, spelling %r>" % (
             self.severity, self.location, self.spelling)
 
     def from_param(self):
       return self.ptr
 
 class FixIt(object):
@@ -1200,16 +1223,30 @@ _clang_getDiagnosticNumFixIts = lib.clang_getDiagnosticNumFixIts
 _clang_getDiagnosticNumFixIts.argtypes = [Diagnostic]
 _clang_getDiagnosticNumFixIts.restype = c_uint
 
 _clang_getDiagnosticFixIt = lib.clang_getDiagnosticFixIt
 _clang_getDiagnosticFixIt.argtypes = [Diagnostic, c_uint, POINTER(SourceRange)]
 _clang_getDiagnosticFixIt.restype = _CXString
 _clang_getDiagnosticFixIt.errcheck = _CXString.from_result
 
+_clang_getDiagnosticCategory = lib.clang_getDiagnosticCategory
+_clang_getDiagnosticCategory.argtypes = [Diagnostic]
+_clang_getDiagnosticCategory.restype = c_uint
+
+_clang_getDiagnosticCategoryName = lib.clang_getDiagnosticCategoryName
+_clang_getDiagnosticCategoryName.argtypes = [c_uint]
+_clang_getDiagnosticCategoryName.restype = _CXString
+_clang_getDiagnosticCategoryName.errcheck = _CXString.from_result
+
+_clang_getDiagnosticOption = lib.clang_getDiagnosticOption
+_clang_getDiagnosticOption.argtypes = [Diagnostic, POINTER(_CXString)]
+_clang_getDiagnosticOption.restype = _CXString
+_clang_getDiagnosticOption.errcheck = _CXString.from_result
+
 ###
 
 class CompletionChunk:
     class Kind:
         def __init__(self, name):
             self.name = name
 
         def __str__(self):
diff --git a/bindings/python/tests/cindex/test_diagnostics.py b/bindings/python/tests/cindex/test_diagnostics.py
index 98f97d3..0022f13 100644
--- a/bindings/python/tests/cindex/test_diagnostics.py
+++ b/bindings/python/tests/cindex/test_diagnostics.py
@@ -1,13 +1,18 @@
 from clang.cindex import *
 
-def tu_from_source(source):
+def tu_from_source(source, all_warnings=False):
+    args = []
+    if all_warnings:
+        args = ['-Wall', '-Wextra']
+
     index = Index.create()
-    tu = index.parse('INPUT.c', unsaved_files = [('INPUT.c', source)])
+    tu = index.parse('INPUT.c', args=args,
+            unsaved_files = [('INPUT.c', source)])
     return tu
 
 # FIXME: We need support for invalid translation units to test better.
 
 def test_diagnostic_warning():
     tu = tu_from_source("""int f0() {}\n""")
     assert len(tu.diagnostics) == 1
     assert tu.diagnostics[0].severity == Diagnostic.Warning
@@ -60,10 +65,33 @@ def test_diagnostic_range():
     assert tu.diagnostics[0].ranges[0].end.line == 1
     assert tu.diagnostics[0].ranges[0].end.column == 27
     try:
       tu.diagnostics[0].ranges[1].start.line
     except IndexError:
       assert True
     else:
       assert False
-      
+
+def test_diagnostic_category():
+    # Ensure that category properties work
+    index = Index.create()
+    tu = tu_from_source("""int f(int i) { return 7; }""", all_warnings=True)
+    assert len(tu.diagnostics) == 1
+    d = tu.diagnostics[0]
+
+    assert d.severity == Diagnostic.Warning
+    assert d.location.line == 1
+    assert d.location.column == 11
+
+    assert d.category_number == 2
+    assert d.category_name == 'Semantic Issue'
+
+def test_diagnostic_option():
+    # Ensure that category option properties work
+    index = Index.create()
+    tu = tu_from_source("""int f(int i) { return 7; }""", all_warnings=True)
+    assert len(tu.diagnostics) == 1
+    d = tu.diagnostics[0]
+
+    assert d.option == '-Wunused-parameter'
+    assert d.disable_option == '-Wno-unused-parameter'
 
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to