summaryrefslogtreecommitdiffstats
path: root/bindings
diff options
context:
space:
mode:
authorSaleem Abdulrasool <compnerd@compnerd.org>2016-08-11 05:31:07 +0000
committerSaleem Abdulrasool <compnerd@compnerd.org>2016-08-11 05:31:07 +0000
commit241fa3c674674391c42e2045a195a96324f58c0a (patch)
tree767ceb915613d2767b7d55adf7c5160520b901da /bindings
parentf7250e52a73ad735e46a4874e9789822931cc1cf (diff)
bindings: expose diagnostic formatting to Python
This makes it easier for tools using the Python libclang bindings to display diagnostics in a manner consistent with clang. Patch by Omar Sandoval! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@278315 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'bindings')
-rw-r--r--bindings/python/clang/cindex.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py
index aeb34f8cb2..cab4944097 100644
--- a/bindings/python/clang/cindex.py
+++ b/bindings/python/clang/cindex.py
@@ -305,6 +305,14 @@ class Diagnostic(object):
Error = 3
Fatal = 4
+ DisplaySourceLocation = 0x01
+ DisplayColumn = 0x02
+ DisplaySourceRanges = 0x04
+ DisplayOption = 0x08
+ DisplayCategoryId = 0x10
+ DisplayCategoryName = 0x20
+ _FormatOptionsMask = 0x3f
+
def __init__(self, ptr):
self.ptr = ptr
@@ -399,10 +407,27 @@ class Diagnostic(object):
return conf.lib.clang_getCString(disable)
+ def format(self, options=None):
+ """
+ Format this diagnostic for display. The options argument takes
+ Diagnostic.Display* flags, which can be combined using bitwise OR. If
+ the options argument is not provided, the default display options will
+ be used.
+ """
+ if options is None:
+ options = conf.lib.clang_defaultDiagnosticDisplayOptions()
+ if options & ~Diagnostic._FormatOptionsMask:
+ raise ValueError('Invalid format options')
+ formatted = conf.lib.clang_formatDiagnostic(self, options)
+ return conf.lib.clang_getCString(formatted)
+
def __repr__(self):
return "<Diagnostic severity %r, location %r, spelling %r>" % (
self.severity, self.location, self.spelling)
+ def __str__(self):
+ return self.format()
+
def from_param(self):
return self.ptr
@@ -3012,6 +3037,10 @@ functionList = [
[Cursor],
bool),
+ ("clang_defaultDiagnosticDisplayOptions",
+ [],
+ c_uint),
+
("clang_defaultSaveOptions",
[TranslationUnit],
c_uint),
@@ -3053,6 +3082,10 @@ functionList = [
[Type, Type],
bool),
+ ("clang_formatDiagnostic",
+ [Diagnostic, c_uint],
+ _CXString),
+
("clang_getArgType",
[Type, c_uint],
Type,