| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Python bindings for libgdiagnostics, using ctypes |
| 4 |
|
| 5 |
# Contributed by David Malcolm <[email protected]> |
| 6 |
# |
| 7 |
# Copyright (C) 2025 Free Software Foundation, Inc. |
| 8 |
# |
| 9 |
# This file is part of GCC. |
| 10 |
# |
| 11 |
# GCC is free software; you can redistribute it and/or modify |
| 12 |
# it under the terms of the GNU General Public License as published by |
| 13 |
# the Free Software Foundation; either version 3, or (at your option) |
| 14 |
# any later version. |
| 15 |
# |
| 16 |
# GCC is distributed in the hope that it will be useful, |
| 17 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
# GNU General Public License for more details. |
| 20 |
# |
| 21 |
# You should have received a copy of the GNU General Public License |
| 22 |
# along with GCC; see the file COPYING. If not, write to |
| 23 |
# the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
| 24 |
# Boston, MA 02110-1301, USA. |
| 25 |
|
| 26 |
from contextlib import contextmanager |
| 27 |
import ctypes |
| 28 |
|
| 29 |
# Lower-level API: use ctypes and FFI to wrap the C API directly |
| 30 |
|
| 31 |
cdll = ctypes.cdll.LoadLibrary('libgdiagnostics.so') |
| 32 |
|
| 33 |
libc = ctypes.CDLL(None) |
| 34 |
c_stderr = ctypes.c_void_p.in_dll(libc, 'stderr') |
| 35 |
|
| 36 |
# Opaque structs |
| 37 |
|
| 38 |
class c_diagnostic_manager(ctypes.Structure): |
| 39 |
pass |
| 40 |
c_diagnostic_manager_ptr = ctypes.POINTER(c_diagnostic_manager) |
| 41 |
|
| 42 |
class c_diagnostic(ctypes.Structure): |
| 43 |
pass |
| 44 |
c_diagnostic_ptr = ctypes.POINTER(c_diagnostic) |
| 45 |
|
| 46 |
class c_diagnostic_file(ctypes.Structure): |
| 47 |
pass |
| 48 |
c_diagnostic_file_ptr = ctypes.POINTER(c_diagnostic_file) |
| 49 |
|
| 50 |
class c_diagnostic_physical_location(ctypes.Structure): |
| 51 |
pass |
| 52 |
c_diagnostic_physical_location_ptr = ctypes.POINTER(c_diagnostic_physical_location) |
| 53 |
|
| 54 |
# Enums |
| 55 |
|
| 56 |
DIAGNOSTIC_COLORIZE_IF_TTY = 0 |
| 57 |
|
| 58 |
DIAGNOSTIC_LEVEL_ERROR = 0 |
| 59 |
DIAGNOSTIC_LEVEL_WARNING = 1 |
| 60 |
DIAGNOSTIC_LEVEL_NOTE = 2 |
| 61 |
DIAGNOSTIC_LEVEL_SORRY = 3 |
| 62 |
|
| 63 |
# Entrypoints |
| 64 |
|
| 65 |
cdll.diagnostic_manager_new.argtypes = [] |
| 66 |
cdll.diagnostic_manager_new.restype = c_diagnostic_manager_ptr |
| 67 |
|
| 68 |
cdll.diagnostic_manager_release.argtypes = [c_diagnostic_manager_ptr] |
| 69 |
cdll.diagnostic_manager_release.restype = None |
| 70 |
|
| 71 |
cdll.diagnostic_manager_set_tool_name.argtypes = [c_diagnostic_manager_ptr, |
| 72 |
ctypes.c_char_p] |
| 73 |
cdll.diagnostic_manager_set_tool_name.restype = None |
| 74 |
|
| 75 |
cdll.diagnostic_manager_begin_group.argtypes = [c_diagnostic_manager_ptr] |
| 76 |
cdll.diagnostic_manager_begin_group.restype = None |
| 77 |
|
| 78 |
cdll.diagnostic_manager_end_group.argtypes = [c_diagnostic_manager_ptr] |
| 79 |
cdll.diagnostic_manager_end_group.restype = None |
| 80 |
|
| 81 |
cdll.diagnostic_begin.argtypes = [c_diagnostic_manager_ptr, |
| 82 |
ctypes.c_int] |
| 83 |
cdll.diagnostic_begin.restype = c_diagnostic_ptr |
| 84 |
|
| 85 |
cdll.diagnostic_finish.argtypes = [c_diagnostic_ptr, |
| 86 |
ctypes.c_char_p]#, ctypes.c_char_p] # FIXME: should be variadic |
| 87 |
cdll.diagnostic_finish.restype = None |
| 88 |
|
| 89 |
cdll.diagnostic_manager_new_file.argtypes = [c_diagnostic_manager_ptr, |
| 90 |
ctypes.c_char_p, |
| 91 |
ctypes.c_char_p] |
| 92 |
cdll.diagnostic_manager_new_file.restype = c_diagnostic_file_ptr |
| 93 |
|
| 94 |
cdll.diagnostic_manager_new_location_from_file_and_line.argtypes \ |
| 95 |
= [c_diagnostic_manager_ptr, |
| 96 |
c_diagnostic_file_ptr, |
| 97 |
ctypes.c_int] |
| 98 |
cdll.diagnostic_manager_new_location_from_file_and_line.restype \ |
| 99 |
= c_diagnostic_physical_location_ptr |
| 100 |
|
| 101 |
cdll.diagnostic_manager_new_location_from_file_line_column.argtypes \ |
| 102 |
= [c_diagnostic_manager_ptr, |
| 103 |
c_diagnostic_file_ptr, |
| 104 |
ctypes.c_int, |
| 105 |
ctypes.c_int] |
| 106 |
cdll.diagnostic_manager_new_location_from_file_line_column.restype \ |
| 107 |
= c_diagnostic_physical_location_ptr |
| 108 |
|
| 109 |
cdll.diagnostic_manager_new_location_from_range.argtypes\ |
| 110 |
= [c_diagnostic_manager_ptr, |
| 111 |
c_diagnostic_physical_location_ptr, |
| 112 |
c_diagnostic_physical_location_ptr, |
| 113 |
c_diagnostic_physical_location_ptr] |
| 114 |
cdll.diagnostic_manager_new_location_from_range.restype \ |
| 115 |
= c_diagnostic_physical_location_ptr |
| 116 |
|
| 117 |
cdll.diagnostic_set_location.argtypes = [c_diagnostic_ptr, |
| 118 |
c_diagnostic_physical_location_ptr] |
| 119 |
cdll.diagnostic_set_location.restype = None |
| 120 |
|
| 121 |
cdll.diagnostic_add_fix_it_hint_replace.argtypes \ |
| 122 |
= [c_diagnostic_ptr, |
| 123 |
c_diagnostic_physical_location_ptr, |
| 124 |
ctypes.c_char_p] |
| 125 |
cdll.diagnostic_add_fix_it_hint_replace.restype = None |
| 126 |
|
| 127 |
# Helper functions |
| 128 |
|
| 129 |
def _to_utf8(s: str): |
| 130 |
if s is None: |
| 131 |
return None |
| 132 |
return s.encode('utf-8') |
| 133 |
|
| 134 |
# Higher-level API, a more pythonic approach, with classes |
| 135 |
|
| 136 |
class Manager: |
| 137 |
def __init__(self): |
| 138 |
self.c_mgr = cdll.diagnostic_manager_new() |
| 139 |
if 0: |
| 140 |
print('__init__: %r' % self.c_mgr) |
| 141 |
|
| 142 |
def __del__(self): |
| 143 |
if 0: |
| 144 |
print('__del__: %r' % self.c_mgr) |
| 145 |
self.c_mgr = cdll.diagnostic_manager_release(self.c_mgr) |
| 146 |
|
| 147 |
def set_tool_name(self, name: str): |
| 148 |
assert self.c_mgr |
| 149 |
assert str |
| 150 |
cdll.diagnostic_manager_set_tool_name (self.c_mgr, _to_utf8(name)) |
| 151 |
|
| 152 |
def add_text_sink(self): |
| 153 |
assert self.c_mgr |
| 154 |
# FIXME: hardcode the args for now |
| 155 |
cdll.diagnostic_manager_add_text_sink (self.c_mgr, |
| 156 |
c_stderr, |
| 157 |
DIAGNOSTIC_COLORIZE_IF_TTY) |
| 158 |
|
| 159 |
def get_file(self, path: str, sarif_lang: str = None): |
| 160 |
assert self.c_mgr |
| 161 |
assert path |
| 162 |
c_file = cdll.diagnostic_manager_new_file (self.c_mgr, |
| 163 |
_to_utf8(path), |
| 164 |
_to_utf8(sarif_lang)) |
| 165 |
return c_file |
| 166 |
|
| 167 |
def get_file_and_line(self, |
| 168 |
c_file: c_diagnostic_file_ptr, |
| 169 |
line_num: int): |
| 170 |
assert self.c_mgr |
| 171 |
assert c_file |
| 172 |
c_phys_loc = cdll.diagnostic_manager_new_location_from_file_and_line (self.c_mgr, |
| 173 |
c_file, |
| 174 |
line_num) |
| 175 |
return c_phys_loc |
| 176 |
|
| 177 |
def get_file_line_and_column(self, |
| 178 |
c_file: c_diagnostic_file_ptr, |
| 179 |
line_num: int, |
| 180 |
column_num: int): |
| 181 |
assert self.c_mgr |
| 182 |
assert c_file |
| 183 |
c_phys_loc = cdll.diagnostic_manager_new_location_from_file_line_column (self.c_mgr, |
| 184 |
c_file, |
| 185 |
line_num, |
| 186 |
column_num) |
| 187 |
return c_phys_loc |
| 188 |
|
| 189 |
def get_range(self, |
| 190 |
caret: c_diagnostic_physical_location_ptr, |
| 191 |
start: c_diagnostic_physical_location_ptr, |
| 192 |
finish: c_diagnostic_physical_location_ptr): |
| 193 |
assert self.c_mgr |
| 194 |
c_phys_loc = cdll.diagnostic_manager_new_location_from_range (self.c_mgr, |
| 195 |
caret, |
| 196 |
start, |
| 197 |
finish) |
| 198 |
return c_phys_loc |
| 199 |
|
| 200 |
@contextmanager |
| 201 |
def make_group(self): |
| 202 |
assert self.c_mgr |
| 203 |
cdll.diagnostic_manager_begin_group (self.c_mgr) |
| 204 |
try: |
| 205 |
yield |
| 206 |
finally: |
| 207 |
assert self.c_mgr |
| 208 |
cdll.diagnostic_manager_end_group (self.c_mgr) |
| 209 |
|
| 210 |
class Diagnostic: |
| 211 |
def __init__(self, mgr: Manager, level: int): |
| 212 |
self.c_diag = cdll.diagnostic_begin (mgr.c_mgr, level) |
| 213 |
if 0: |
| 214 |
print('__init__: %r' % self.c_diag) |
| 215 |
|
| 216 |
def finish(self, fmt: str, *args): |
| 217 |
assert self.c_diag |
| 218 |
c_args = [] |
| 219 |
for arg in args: |
| 220 |
if type(arg) is str: |
| 221 |
arg = _to_utf8(arg) |
| 222 |
c_args.append(arg) |
| 223 |
cdll.diagnostic_finish (self.c_diag, _to_utf8(fmt), *c_args) |
| 224 |
self.c_diagnostic = None |
| 225 |
|
| 226 |
def set_location(self, |
| 227 |
phys_loc: c_diagnostic_physical_location_ptr): |
| 228 |
assert self.c_diag |
| 229 |
cdll.diagnostic_set_location(self.c_diag, phys_loc) |
| 230 |
|
| 231 |
|
| 232 |
# Chaining-style API |
| 233 |
|
| 234 |
def at(self, |
| 235 |
phys_loc: c_diagnostic_physical_location_ptr): |
| 236 |
self.set_location(phys_loc) |
| 237 |
return self |
| 238 |
|
| 239 |
def with_fix_it_replace(self, |
| 240 |
phys_loc: c_diagnostic_physical_location_ptr, |
| 241 |
replacement: str): |
| 242 |
assert self.c_diag |
| 243 |
assert replacement |
| 244 |
cdll.diagnostic_add_fix_it_hint_replace(self.c_diag, |
| 245 |
phys_loc, |
| 246 |
_to_utf8(replacement)) |
| 247 |
return self |
| 248 |
|
| 249 |
def emit(self, fmt: str, *args): |
| 250 |
self.finish(fmt, *args) |