Skip to content

Commit c168a55

Browse files
committed
preserve error code in TU parsing APIs
1 parent 4858eef commit c168a55

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

clang/bindings/python/clang/cindex.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,15 @@ class TranslationUnitLoadError(Exception):
177177
# An AST deserialization error has occurred.
178178
ERROR_AST_READ_ERROR = 4
179179

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)
183182

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+
)
190189

191190
self.error_code = enumeration
192191
Exception.__init__(self, "Error %d: %s" % (enumeration or 0, message))
@@ -3111,19 +3110,20 @@ def from_source(
31113110

31123111
unsaved_array = cls.process_unsaved_files(unsaved_files)
31133112

3114-
ptr = conf.lib.clang_parseTranslationUnit(
3113+
ptr = c_object_p()
3114+
errc = conf.lib.clang_parseTranslationUnit2(
31153115
index,
31163116
os.fspath(filename) if filename is not None else None,
31173117
args_array,
31183118
len(args),
31193119
unsaved_array,
31203120
len(unsaved_files),
31213121
options,
3122+
byref(ptr),
31223123
)
31233124

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.")
31273127

31283128
return cls(ptr, index=index)
31293129

@@ -3145,10 +3145,15 @@ def from_ast_file(cls, filename, index=None):
31453145
if index is None:
31463146
index = Index.create()
31473147

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)
31523157

31533158
return cls(ptr=ptr, index=index)
31543159

@@ -3293,11 +3298,11 @@ def reparse(self, unsaved_files=None, options=0):
32933298
unsaved_files = []
32943299

32953300
unsaved_files_array = self.process_unsaved_files(unsaved_files)
3296-
result = conf.lib.clang_reparseTranslationUnit(
3301+
errc = conf.lib.clang_reparseTranslationUnit(
32973302
self, len(unsaved_files), unsaved_files_array, options
32983303
)
3299-
if result != 0:
3300-
raise TranslationUnitLoadError(result, 'Error reparsing TranslationUnit.')
3304+
if errc != 0:
3305+
raise TranslationUnitLoadError(errc, 'Error reparsing TranslationUnit.')
33013306

33023307
def save(self, filename):
33033308
"""Saves the TranslationUnit to a file.
@@ -3751,6 +3756,7 @@ def write_main_file_to_stdout(self):
37513756
("clang_codeCompleteGetNumDiagnostics", [CodeCompletionResults], c_int),
37523757
("clang_createIndex", [c_int, c_int], c_object_p),
37533758
("clang_createTranslationUnit", [Index, c_interop_string], c_object_p),
3759+
("clang_createTranslationUnit2", [Index, c_interop_string, POINTER(c_object_p)], c_int),
37543760
("clang_CXRewriter_create", [TranslationUnit], c_object_p),
37553761
("clang_CXRewriter_dispose", [Rewriter]),
37563762
("clang_CXRewriter_insertTextBefore", [Rewriter, SourceLocation, c_interop_string]),
@@ -3945,6 +3951,11 @@ def write_main_file_to_stdout(self):
39453951
[Index, c_interop_string, c_void_p, c_int, c_void_p, c_int, c_int],
39463952
c_object_p,
39473953
),
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+
),
39483959
("clang_reparseTranslationUnit", [TranslationUnit, c_int, c_void_p, c_int], c_int),
39493960
("clang_saveTranslationUnit", [TranslationUnit, c_interop_string, c_uint], c_int),
39503961
(

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ 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`
62+
- `TranslationUnitLoadError` now contains an error code in `error_code`
6363
attribute. Also, `TranslationUnit.reparse` will throw `TranslationUnitLoadError`
6464
when operation fails.
6565

0 commit comments

Comments
 (0)