Skip to content

Commit 4858eef

Browse files
committed
fix error handling of TranslationUnit.reparse
1 parent 98e4413 commit 4858eef

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

clang/bindings/python/clang/cindex.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ def b(x: str | bytes) -> bytes:
149149
# this by marshalling object arguments as void**.
150150
c_object_p: TType[_Pointer[Any]] = POINTER(c_void_p)
151151

152-
### Exception Classes ###
153152

153+
### Exception Classes ###
154154

155155
class TranslationUnitLoadError(Exception):
156156
"""Represents an error that occurred when loading a TranslationUnit.
@@ -161,7 +161,35 @@ class TranslationUnitLoadError(Exception):
161161
FIXME: Make libclang expose additional error information in this scenario.
162162
"""
163163

164-
pass
164+
# A generic error code, no further details are available.
165+
#
166+
# Errors of this kind can get their own specific error codes in future
167+
# libclang versions.
168+
ERROR_FAILURE = 1
169+
170+
# libclang crashed while performing the requested operation.
171+
ERROR_CRASHED = 2
172+
173+
# The function detected that the arguments violate the function
174+
# contract.
175+
ERROR_INVALID_ARGUMENTS = 3
176+
177+
# An AST deserialization error has occurred.
178+
ERROR_AST_READ_ERROR = 4
179+
180+
def __init__(self, enumeration: int | None, message: str):
181+
if enumeration is not None:
182+
assert isinstance(enumeration, int)
183+
184+
if enumeration < 1 or enumeration > 4:
185+
raise Exception(
186+
"Encountered undefined CXError "
187+
"constant: %d. Please file a bug to have this "
188+
"value supported." % enumeration
189+
)
190+
191+
self.error_code = enumeration
192+
Exception.__init__(self, "Error %d: %s" % (enumeration or 0, message))
165193

166194

167195
class TranslationUnitSaveError(Exception):
@@ -3094,7 +3122,8 @@ def from_source(
30943122
)
30953123

30963124
if not ptr:
3097-
raise TranslationUnitLoadError("Error parsing translation unit.")
3125+
# FIXME: use clang_parseTranslationUnit2 to preserve error code
3126+
raise TranslationUnitLoadError(None, "Error parsing translation unit.")
30983127

30993128
return cls(ptr, index=index)
31003129

@@ -3118,7 +3147,8 @@ def from_ast_file(cls, filename, index=None):
31183147

31193148
ptr = conf.lib.clang_createTranslationUnit(index, os.fspath(filename))
31203149
if not ptr:
3121-
raise TranslationUnitLoadError(filename)
3150+
# FIXME: use clang_createTranslationUnit2 to preserve error code
3151+
raise TranslationUnitLoadError(None, filename)
31223152

31233153
return cls(ptr=ptr, index=index)
31243154

@@ -3263,9 +3293,11 @@ def reparse(self, unsaved_files=None, options=0):
32633293
unsaved_files = []
32643294

32653295
unsaved_files_array = self.process_unsaved_files(unsaved_files)
3266-
ptr = conf.lib.clang_reparseTranslationUnit(
3296+
result = conf.lib.clang_reparseTranslationUnit(
32673297
self, len(unsaved_files), unsaved_files_array, options
32683298
)
3299+
if result != 0:
3300+
raise TranslationUnitLoadError(result, 'Error reparsing TranslationUnit.')
32693301

32703302
def save(self, filename):
32713303
"""Saves the TranslationUnit to a file.

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ Clang Python Bindings Potentially Breaking Changes
5959
- Calling a property on the `CompletionChunk` or `CompletionString` class
6060
statically now leads to an error, instead of returning a `CachedProperty` object
6161
that is used internally. Properties are only available on instances.
62+
- `TranslationUnitLoadError` now contains an optional error code in `error_code`
63+
attribute. Also, `TranslationUnit.reparse` will throw `TranslationUnitLoadError`
64+
when operation fails.
6265

6366
What's New in Clang |release|?
6467
==============================

0 commit comments

Comments
 (0)