[PATCH] Expose diagnostic formatting to Python bindings

Omar Sandoval via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 4 21:55:44 PDT 2015


On Fri, Aug 07, 2015 at 11:35:26PM -0700, Omar Sandoval wrote:
> This makes it easier for tools using the Python libclang bindings to
> display diagnostics in a manner consistent with clang.

Ping, any comments on this?

Thanks.

> ---
>  bindings/python/clang/cindex.py | 33 +++++++++++++++++++++++++++++++++
>  1 file changed, 33 insertions(+)
> 
> diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py
> index f5caca8572cb..f46084b140aa 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
>  
> @@ -382,10 +390,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.DisplayFoo 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()
> +        elif 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
>  
> @@ -2889,6 +2914,10 @@ functionList = [
>     [Cursor],
>     bool),
>  
> +  ("clang_defaultDiagnosticDisplayOptions",
> +   [],
> +   c_uint),
> +
>    ("clang_defaultSaveOptions",
>     [TranslationUnit],
>     c_uint),
> @@ -2930,6 +2959,10 @@ functionList = [
>     [Type, Type],
>     bool),
>  
> +  ("clang_formatDiagnostic",
> +   [Diagnostic, c_uint],
> +   _CXString),
> +
>    ("clang_getArgType",
>     [Type, c_uint],
>     Type,
> -- 
> 2.5.0
> 

-- 
Omar


More information about the cfe-commits mailing list