@@ -149,8 +149,8 @@ def b(x: str | bytes) -> bytes:
149
149
# this by marshalling object arguments as void**.
150
150
c_object_p : TType [_Pointer [Any ]] = POINTER (c_void_p )
151
151
152
- ### Exception Classes ###
153
152
153
+ ### Exception Classes ###
154
154
155
155
class TranslationUnitLoadError (Exception ):
156
156
"""Represents an error that occurred when loading a TranslationUnit.
@@ -161,7 +161,35 @@ class TranslationUnitLoadError(Exception):
161
161
FIXME: Make libclang expose additional error information in this scenario.
162
162
"""
163
163
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 ))
165
193
166
194
167
195
class TranslationUnitSaveError (Exception ):
@@ -3094,7 +3122,8 @@ def from_source(
3094
3122
)
3095
3123
3096
3124
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." )
3098
3127
3099
3128
return cls (ptr , index = index )
3100
3129
@@ -3118,7 +3147,8 @@ def from_ast_file(cls, filename, index=None):
3118
3147
3119
3148
ptr = conf .lib .clang_createTranslationUnit (index , os .fspath (filename ))
3120
3149
if not ptr :
3121
- raise TranslationUnitLoadError (filename )
3150
+ # FIXME: use clang_createTranslationUnit2 to preserve error code
3151
+ raise TranslationUnitLoadError (None , filename )
3122
3152
3123
3153
return cls (ptr = ptr , index = index )
3124
3154
@@ -3263,9 +3293,11 @@ def reparse(self, unsaved_files=None, options=0):
3263
3293
unsaved_files = []
3264
3294
3265
3295
unsaved_files_array = self .process_unsaved_files (unsaved_files )
3266
- ptr = conf .lib .clang_reparseTranslationUnit (
3296
+ result = conf .lib .clang_reparseTranslationUnit (
3267
3297
self , len (unsaved_files ), unsaved_files_array , options
3268
3298
)
3299
+ if result != 0 :
3300
+ raise TranslationUnitLoadError (result , 'Error reparsing TranslationUnit.' )
3269
3301
3270
3302
def save (self , filename ):
3271
3303
"""Saves the TranslationUnit to a file.
0 commit comments