@@ -177,16 +177,15 @@ class TranslationUnitLoadError(Exception):
177
177
# An AST deserialization error has occurred.
178
178
ERROR_AST_READ_ERROR = 4
179
179
180
- def __init__ (self , enumeration : int | None , message : str ):
181
- if enumeration is not None :
182
- assert isinstance (enumeration , int )
180
+ def __init__ (self , enumeration : int , message : str ):
181
+ assert isinstance (enumeration , int )
183
182
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
- )
183
+ if enumeration < 1 or enumeration > 4 :
184
+ raise Exception (
185
+ "Encountered undefined CXError "
186
+ "constant: %d. Please file a bug to have this "
187
+ "value supported." % enumeration
188
+ )
190
189
191
190
self .error_code = enumeration
192
191
Exception .__init__ (self , "Error %d: %s" % (enumeration or 0 , message ))
@@ -3111,19 +3110,20 @@ def from_source(
3111
3110
3112
3111
unsaved_array = cls .process_unsaved_files (unsaved_files )
3113
3112
3114
- ptr = conf .lib .clang_parseTranslationUnit (
3113
+ ptr = c_object_p ()
3114
+ errc = conf .lib .clang_parseTranslationUnit2 (
3115
3115
index ,
3116
3116
os .fspath (filename ) if filename is not None else None ,
3117
3117
args_array ,
3118
3118
len (args ),
3119
3119
unsaved_array ,
3120
3120
len (unsaved_files ),
3121
3121
options ,
3122
+ byref (ptr ),
3122
3123
)
3123
3124
3124
- if not ptr :
3125
- # FIXME: use clang_parseTranslationUnit2 to preserve error code
3126
- raise TranslationUnitLoadError (None , "Error parsing translation unit." )
3125
+ if errc != 0 :
3126
+ raise TranslationUnitLoadError (errc , "Error parsing translation unit." )
3127
3127
3128
3128
return cls (ptr , index = index )
3129
3129
@@ -3145,10 +3145,15 @@ def from_ast_file(cls, filename, index=None):
3145
3145
if index is None :
3146
3146
index = Index .create ()
3147
3147
3148
- ptr = conf .lib .clang_createTranslationUnit (index , os .fspath (filename ))
3149
- if not ptr :
3150
- # FIXME: use clang_createTranslationUnit2 to preserve error code
3151
- raise TranslationUnitLoadError (None , filename )
3148
+ ptr = c_object_p ()
3149
+ errc = conf .lib .clang_createTranslationUnit2 (
3150
+ index ,
3151
+ os .fspath (filename ),
3152
+ byref (ptr )
3153
+ )
3154
+
3155
+ if errc != 0 :
3156
+ raise TranslationUnitLoadError (errc , filename )
3152
3157
3153
3158
return cls (ptr = ptr , index = index )
3154
3159
@@ -3293,11 +3298,11 @@ def reparse(self, unsaved_files=None, options=0):
3293
3298
unsaved_files = []
3294
3299
3295
3300
unsaved_files_array = self .process_unsaved_files (unsaved_files )
3296
- result = conf .lib .clang_reparseTranslationUnit (
3301
+ errc = conf .lib .clang_reparseTranslationUnit (
3297
3302
self , len (unsaved_files ), unsaved_files_array , options
3298
3303
)
3299
- if result != 0 :
3300
- raise TranslationUnitLoadError (result , 'Error reparsing TranslationUnit.' )
3304
+ if errc != 0 :
3305
+ raise TranslationUnitLoadError (errc , 'Error reparsing TranslationUnit.' )
3301
3306
3302
3307
def save (self , filename ):
3303
3308
"""Saves the TranslationUnit to a file.
@@ -3751,6 +3756,7 @@ def write_main_file_to_stdout(self):
3751
3756
("clang_codeCompleteGetNumDiagnostics" , [CodeCompletionResults ], c_int ),
3752
3757
("clang_createIndex" , [c_int , c_int ], c_object_p ),
3753
3758
("clang_createTranslationUnit" , [Index , c_interop_string ], c_object_p ),
3759
+ ("clang_createTranslationUnit2" , [Index , c_interop_string , POINTER (c_object_p )], c_int ),
3754
3760
("clang_CXRewriter_create" , [TranslationUnit ], c_object_p ),
3755
3761
("clang_CXRewriter_dispose" , [Rewriter ]),
3756
3762
("clang_CXRewriter_insertTextBefore" , [Rewriter , SourceLocation , c_interop_string ]),
@@ -3945,6 +3951,11 @@ def write_main_file_to_stdout(self):
3945
3951
[Index , c_interop_string , c_void_p , c_int , c_void_p , c_int , c_int ],
3946
3952
c_object_p ,
3947
3953
),
3954
+ (
3955
+ "clang_parseTranslationUnit2" ,
3956
+ [Index , c_interop_string , c_void_p , c_int , c_void_p , c_int , c_int , POINTER (c_object_p )],
3957
+ c_int ,
3958
+ ),
3948
3959
("clang_reparseTranslationUnit" , [TranslationUnit , c_int , c_void_p , c_int ], c_int ),
3949
3960
("clang_saveTranslationUnit" , [TranslationUnit , c_interop_string , c_uint ], c_int ),
3950
3961
(
0 commit comments