[clang] #101784 part 2: fix error handling of TranslationUnit.reparse (PR #102410)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 7 18:29:47 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: None (TsXor)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/102410.diff
1 Files Affected:
- (modified) clang/bindings/python/clang/cindex.py (+38-1)
``````````diff
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index 2038ef6045c7d..4fcc20169eeb2 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -152,6 +152,41 @@ def b(x: str | bytes) -> bytes:
### Exception Classes ###
+class CXError(Exception):
+ '''Represents C error of type enum CXErrorCode.'''
+
+ # A generic error code, no further details are available.
+ #
+ # Errors of this kind can get their own specific error codes in future
+ # libclang versions.
+ ERROR_FAILURE = 1
+
+ # libclang crashed while performing the requested operation.
+ ERROR_CRASHED = 2
+
+ # The function detected that the arguments violate the function
+ # contract.
+ ERROR_INVALID_ARGUMENTS = 3
+
+ # An AST deserialization error has occurred.
+ ERROR_AST_READ_ERROR = 4
+
+ error_code: int
+
+ def __init__(self, enumeration: int, message: str):
+ assert isinstance(enumeration, int)
+
+ if enumeration < 1 or enumeration > 4:
+ raise Exception(
+ "Encountered undefined CXError "
+ "constant: %d. Please file a bug to have this "
+ "value supported." % enumeration
+ )
+
+ self.error_code = enumeration
+ Exception.__init__(self, "Error %d: %s" % (enumeration, message))
+
+
class TranslationUnitLoadError(Exception):
"""Represents an error that occurred when loading a TranslationUnit.
@@ -3263,9 +3298,11 @@ def reparse(self, unsaved_files=None, options=0):
unsaved_files = []
unsaved_files_array = self.process_unsaved_files(unsaved_files)
- ptr = conf.lib.clang_reparseTranslationUnit(
+ result = conf.lib.clang_reparseTranslationUnit(
self, len(unsaved_files), unsaved_files_array, options
)
+ if result != 0:
+ raise CXError(result, 'Error reparsing TranslationUnit.')
def save(self, filename):
"""Saves the TranslationUnit to a file.
``````````
</details>
https://github.com/llvm/llvm-project/pull/102410
More information about the cfe-commits
mailing list