[clang] 4c670b2 - [libclang/python] Factor out unsaved files processing (#101308)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 31 05:19:17 PDT 2024
Author: Jannick Kremer
Date: 2024-07-31T16:19:13+04:00
New Revision: 4c670b266a10d613a58b71cc9c3d3a21cb6dc36b
URL: https://github.com/llvm/llvm-project/commit/4c670b266a10d613a58b71cc9c3d3a21cb6dc36b
DIFF: https://github.com/llvm/llvm-project/commit/4c670b266a10d613a58b71cc9c3d3a21cb6dc36b.diff
LOG: [libclang/python] Factor out unsaved files processing (#101308)
Factor out the processing of unsaved files into its own function as
suggested by @Endilll
[here](https://github.com/llvm/llvm-project/pull/78114/files#r1697730196)
Added:
Modified:
clang/bindings/python/clang/cindex.py
Removed:
################################################################################
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index 72509a8a54f28..f669c279aab6d 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -3011,6 +3011,20 @@ class TranslationUnit(ClangObject):
# into the set of code completions returned from this translation unit.
PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION = 128
+ @staticmethod
+ def process_unsaved_files(unsaved_files) -> Array[_CXUnsavedFile] | None:
+ unsaved_array = None
+ if len(unsaved_files):
+ unsaved_array = (_CXUnsavedFile * len(unsaved_files))()
+ for i, (name, contents) in enumerate(unsaved_files):
+ if hasattr(contents, "read"):
+ contents = contents.read()
+ binary_contents = b(contents)
+ unsaved_array[i].name = b(os.fspath(name))
+ unsaved_array[i].contents = binary_contents
+ unsaved_array[i].length = len(binary_contents)
+ return unsaved_array
+
@classmethod
def from_source(
cls, filename, args=None, unsaved_files=None, options=0, index=None
@@ -3067,16 +3081,7 @@ def from_source(
if len(args) > 0:
args_array = (c_char_p * len(args))(*[b(x) for x in args])
- unsaved_array = None
- if len(unsaved_files) > 0:
- unsaved_array = (_CXUnsavedFile * len(unsaved_files))()
- for i, (name, contents) in enumerate(unsaved_files):
- if hasattr(contents, "read"):
- contents = contents.read()
- contents = b(contents)
- unsaved_array[i].name = b(os.fspath(name))
- unsaved_array[i].contents = contents
- unsaved_array[i].length = len(contents)
+ unsaved_array = cls.process_unsaved_files(unsaved_files)
ptr = conf.lib.clang_parseTranslationUnit(
index,
@@ -3257,16 +3262,7 @@ def reparse(self, unsaved_files=None, options=0):
if unsaved_files is None:
unsaved_files = []
- unsaved_files_array = 0
- if len(unsaved_files):
- unsaved_files_array = (_CXUnsavedFile * len(unsaved_files))()
- for i, (name, contents) in enumerate(unsaved_files):
- if hasattr(contents, "read"):
- contents = contents.read()
- contents = b(contents)
- unsaved_files_array[i].name = b(os.fspath(name))
- unsaved_files_array[i].contents = contents
- unsaved_files_array[i].length = len(contents)
+ unsaved_files_array = self.process_unsaved_files(unsaved_files)
ptr = conf.lib.clang_reparseTranslationUnit(
self, len(unsaved_files), unsaved_files_array, options
)
@@ -3329,16 +3325,7 @@ def codeComplete(
if unsaved_files is None:
unsaved_files = []
- unsaved_files_array = 0
- if len(unsaved_files):
- unsaved_files_array = (_CXUnsavedFile * len(unsaved_files))()
- for i, (name, contents) in enumerate(unsaved_files):
- if hasattr(contents, "read"):
- contents = contents.read()
- contents = b(contents)
- unsaved_files_array[i].name = b(os.fspath(name))
- unsaved_files_array[i].contents = contents
- unsaved_files_array[i].length = len(contents)
+ unsaved_files_array = self.process_unsaved_files(unsaved_files)
ptr = conf.lib.clang_codeCompleteAt(
self,
os.fspath(path),
More information about the cfe-commits
mailing list