[clang] [libclang/python] Expose Rewriter to the libclang python binding. (PR #71341)
https://github.com/jimmy-zx created https://github.com/llvm/llvm-project/pull/71341 Exposes `CXRewriter` API in https://github.com/llvm/llvm-project/commit/69e5abb57b70570cf04671a93246e5e624023650. >From 887751e365ca72515679f61d0734b1631ac38335 Mon Sep 17 00:00:00 2001 From: Jimmy Z <51149050+jimmy...@users.noreply.github.com> Date: Mon, 6 Nov 2023 00:25:39 + Subject: [PATCH] [libclang/python] Expose Rewriter to the libclang python binding. --- clang/bindings/python/clang/cindex.py | 62 .../python/tests/cindex/test_rewrite.py | 74 +++ 2 files changed, 136 insertions(+) create mode 100644 clang/bindings/python/tests/cindex/test_rewrite.py diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index 6a16f3a9ef6e957..e51d558ab73fbce 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -3531,6 +3531,61 @@ def cursor(self): return cursor +class Rewriter(ClangObject): +""" +The Rewriter is a wrapper class around clang::Rewriter + +It enables rewriting buffers. +""" + +@staticmethod +def create(tu): +""" +Creates a new Rewriter +Parameters: +tu -- The translation unit for the target AST. +""" +return Rewriter(conf.lib.clang_CXRewriter_create(tu)) + +def __init__(self, ptr): +ClangObject.__init__(self, ptr) + +def __del__(self): +conf.lib.clang_CXRewriter_dispose(self) + +def insertTextBefore(self, loc, insert): +""" +Insert the specified string at the specified location in the original buffer. +""" +conf.lib.clang_CXRewriter_insertTextBefore(self, loc, insert) + +def replaceText(self, toBeReplaced, replacement): +""" +This method replaces a range of characters in the input buffer with a new string. +""" +conf.lib.clang_CXRewriter_replaceText(self, toBeReplaced, replacement) + +def removeText(self, toBeRemoved): +""" +Remove the specified text region. +""" +conf.lib.clang_CXRewriter_removeText(self, toBeRemoved) + +def overwriteChangedFiles(self): +""" +Save all changed files to disk. +""" +conf.lib.clang_CXRewriter_overwriteChangedFiles(self) + +def writeMainFileToStdOut(self): +""" +Writes the main file to stdout. +""" +conf.lib.clang_CXRewriter_writeMainFileToStdOut(self) + + + + # Now comes the plumbing to hook up the C library. # Register callback types in common container. @@ -3596,6 +3651,13 @@ def cursor(self): ("clang_codeCompleteGetNumDiagnostics", [CodeCompletionResults], c_int), ("clang_createIndex", [c_int, c_int], c_object_p), ("clang_createTranslationUnit", [Index, c_interop_string], c_object_p), +("clang_CXRewriter_create", [TranslationUnit], c_object_p), +("clang_CXRewriter_dispose", [Rewriter]), +("clang_CXRewriter_insertTextBefore", [Rewriter, SourceLocation, c_interop_string]), +("clang_CXRewriter_overwriteChangedFiles", [Rewriter], c_int), +("clang_CXRewriter_removeText", [Rewriter, SourceRange]), +("clang_CXRewriter_replaceText", [Rewriter, SourceRange, c_interop_string]), +("clang_CXRewriter_writeMainFileToStdOut", [Rewriter]), ("clang_CXXConstructor_isConvertingConstructor", [Cursor], bool), ("clang_CXXConstructor_isCopyConstructor", [Cursor], bool), ("clang_CXXConstructor_isDefaultConstructor", [Cursor], bool), diff --git a/clang/bindings/python/tests/cindex/test_rewrite.py b/clang/bindings/python/tests/cindex/test_rewrite.py new file mode 100644 index 000..eb697f72a923030 --- /dev/null +++ b/clang/bindings/python/tests/cindex/test_rewrite.py @@ -0,0 +1,74 @@ +import sys +import io +import unittest +import tempfile + +from clang.cindex import Rewriter, TranslationUnit, Config, File, SourceLocation, SourceRange + +class TestRewrite(unittest.TestCase): +code = ''' +int test1; + +void test2(void); + +int f(int c) { +return c; +} +''' + +@classmethod +def setUpClass(cls): +Config.set_compatibility_check(False) + +def setUp(self): +self.tmp = tempfile.NamedTemporaryFile(suffix='.cpp', buffering=0) +self.tmp.write(TestRewrite.code.encode('utf-8')) +self.tmp.flush() +self.tu = TranslationUnit.from_source(self.tmp.name) +self.rew = Rewriter.create(self.tu) +self.file = File.from_name(self.tu, self.tmp.name) + +def tearDown(self): +self.tmp.close() + + +def test_insert(self): +snip = '#include \n' + +beginning = SourceLocation.from_offset(self.tu, self.file, 0) +self.rew.insertTextBefore(beginning, snip) +self.rew.overwriteChangedFiles() + +with open(self.tmp.name, 'r', encoding='utf-8') as f: +self.assertEqual(f.read(), snip + TestRewrite.code) + + +
[clang] [libclang/python] Expose Rewriter to the libclang python binding. (PR #71341)
https://github.com/jimmy-zx updated https://github.com/llvm/llvm-project/pull/71341 >From 887751e365ca72515679f61d0734b1631ac38335 Mon Sep 17 00:00:00 2001 From: Jimmy Z <51149050+jimmy...@users.noreply.github.com> Date: Mon, 6 Nov 2023 00:25:39 + Subject: [PATCH 1/2] [libclang/python] Expose Rewriter to the libclang python binding. --- clang/bindings/python/clang/cindex.py | 62 .../python/tests/cindex/test_rewrite.py | 74 +++ 2 files changed, 136 insertions(+) create mode 100644 clang/bindings/python/tests/cindex/test_rewrite.py diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index 6a16f3a9ef6e957..e51d558ab73fbce 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -3531,6 +3531,61 @@ def cursor(self): return cursor +class Rewriter(ClangObject): +""" +The Rewriter is a wrapper class around clang::Rewriter + +It enables rewriting buffers. +""" + +@staticmethod +def create(tu): +""" +Creates a new Rewriter +Parameters: +tu -- The translation unit for the target AST. +""" +return Rewriter(conf.lib.clang_CXRewriter_create(tu)) + +def __init__(self, ptr): +ClangObject.__init__(self, ptr) + +def __del__(self): +conf.lib.clang_CXRewriter_dispose(self) + +def insertTextBefore(self, loc, insert): +""" +Insert the specified string at the specified location in the original buffer. +""" +conf.lib.clang_CXRewriter_insertTextBefore(self, loc, insert) + +def replaceText(self, toBeReplaced, replacement): +""" +This method replaces a range of characters in the input buffer with a new string. +""" +conf.lib.clang_CXRewriter_replaceText(self, toBeReplaced, replacement) + +def removeText(self, toBeRemoved): +""" +Remove the specified text region. +""" +conf.lib.clang_CXRewriter_removeText(self, toBeRemoved) + +def overwriteChangedFiles(self): +""" +Save all changed files to disk. +""" +conf.lib.clang_CXRewriter_overwriteChangedFiles(self) + +def writeMainFileToStdOut(self): +""" +Writes the main file to stdout. +""" +conf.lib.clang_CXRewriter_writeMainFileToStdOut(self) + + + + # Now comes the plumbing to hook up the C library. # Register callback types in common container. @@ -3596,6 +3651,13 @@ def cursor(self): ("clang_codeCompleteGetNumDiagnostics", [CodeCompletionResults], c_int), ("clang_createIndex", [c_int, c_int], c_object_p), ("clang_createTranslationUnit", [Index, c_interop_string], c_object_p), +("clang_CXRewriter_create", [TranslationUnit], c_object_p), +("clang_CXRewriter_dispose", [Rewriter]), +("clang_CXRewriter_insertTextBefore", [Rewriter, SourceLocation, c_interop_string]), +("clang_CXRewriter_overwriteChangedFiles", [Rewriter], c_int), +("clang_CXRewriter_removeText", [Rewriter, SourceRange]), +("clang_CXRewriter_replaceText", [Rewriter, SourceRange, c_interop_string]), +("clang_CXRewriter_writeMainFileToStdOut", [Rewriter]), ("clang_CXXConstructor_isConvertingConstructor", [Cursor], bool), ("clang_CXXConstructor_isCopyConstructor", [Cursor], bool), ("clang_CXXConstructor_isDefaultConstructor", [Cursor], bool), diff --git a/clang/bindings/python/tests/cindex/test_rewrite.py b/clang/bindings/python/tests/cindex/test_rewrite.py new file mode 100644 index 000..eb697f72a923030 --- /dev/null +++ b/clang/bindings/python/tests/cindex/test_rewrite.py @@ -0,0 +1,74 @@ +import sys +import io +import unittest +import tempfile + +from clang.cindex import Rewriter, TranslationUnit, Config, File, SourceLocation, SourceRange + +class TestRewrite(unittest.TestCase): +code = ''' +int test1; + +void test2(void); + +int f(int c) { +return c; +} +''' + +@classmethod +def setUpClass(cls): +Config.set_compatibility_check(False) + +def setUp(self): +self.tmp = tempfile.NamedTemporaryFile(suffix='.cpp', buffering=0) +self.tmp.write(TestRewrite.code.encode('utf-8')) +self.tmp.flush() +self.tu = TranslationUnit.from_source(self.tmp.name) +self.rew = Rewriter.create(self.tu) +self.file = File.from_name(self.tu, self.tmp.name) + +def tearDown(self): +self.tmp.close() + + +def test_insert(self): +snip = '#include \n' + +beginning = SourceLocation.from_offset(self.tu, self.file, 0) +self.rew.insertTextBefore(beginning, snip) +self.rew.overwriteChangedFiles() + +with open(self.tmp.name, 'r', encoding='utf-8') as f: +self.assertEqual(f.read(), snip + TestRewrite.code) + + +def test_replace(self): +pattern = 'test2' +replacement = 'func' + +offset = TestRewri
[clang] [libclang/python] Expose Rewriter to the libclang python binding. (PR #71341)
https://github.com/jimmy-zx edited https://github.com/llvm/llvm-project/pull/71341 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [libclang/python] Expose Rewriter to the libclang python binding. (PR #71341)
https://github.com/jimmy-zx updated https://github.com/llvm/llvm-project/pull/71341 >From 887751e365ca72515679f61d0734b1631ac38335 Mon Sep 17 00:00:00 2001 From: Jimmy Z <51149050+jimmy...@users.noreply.github.com> Date: Mon, 6 Nov 2023 00:25:39 + Subject: [PATCH 1/3] [libclang/python] Expose Rewriter to the libclang python binding. --- clang/bindings/python/clang/cindex.py | 62 .../python/tests/cindex/test_rewrite.py | 74 +++ 2 files changed, 136 insertions(+) create mode 100644 clang/bindings/python/tests/cindex/test_rewrite.py diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index 6a16f3a9ef6e957..e51d558ab73fbce 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -3531,6 +3531,61 @@ def cursor(self): return cursor +class Rewriter(ClangObject): +""" +The Rewriter is a wrapper class around clang::Rewriter + +It enables rewriting buffers. +""" + +@staticmethod +def create(tu): +""" +Creates a new Rewriter +Parameters: +tu -- The translation unit for the target AST. +""" +return Rewriter(conf.lib.clang_CXRewriter_create(tu)) + +def __init__(self, ptr): +ClangObject.__init__(self, ptr) + +def __del__(self): +conf.lib.clang_CXRewriter_dispose(self) + +def insertTextBefore(self, loc, insert): +""" +Insert the specified string at the specified location in the original buffer. +""" +conf.lib.clang_CXRewriter_insertTextBefore(self, loc, insert) + +def replaceText(self, toBeReplaced, replacement): +""" +This method replaces a range of characters in the input buffer with a new string. +""" +conf.lib.clang_CXRewriter_replaceText(self, toBeReplaced, replacement) + +def removeText(self, toBeRemoved): +""" +Remove the specified text region. +""" +conf.lib.clang_CXRewriter_removeText(self, toBeRemoved) + +def overwriteChangedFiles(self): +""" +Save all changed files to disk. +""" +conf.lib.clang_CXRewriter_overwriteChangedFiles(self) + +def writeMainFileToStdOut(self): +""" +Writes the main file to stdout. +""" +conf.lib.clang_CXRewriter_writeMainFileToStdOut(self) + + + + # Now comes the plumbing to hook up the C library. # Register callback types in common container. @@ -3596,6 +3651,13 @@ def cursor(self): ("clang_codeCompleteGetNumDiagnostics", [CodeCompletionResults], c_int), ("clang_createIndex", [c_int, c_int], c_object_p), ("clang_createTranslationUnit", [Index, c_interop_string], c_object_p), +("clang_CXRewriter_create", [TranslationUnit], c_object_p), +("clang_CXRewriter_dispose", [Rewriter]), +("clang_CXRewriter_insertTextBefore", [Rewriter, SourceLocation, c_interop_string]), +("clang_CXRewriter_overwriteChangedFiles", [Rewriter], c_int), +("clang_CXRewriter_removeText", [Rewriter, SourceRange]), +("clang_CXRewriter_replaceText", [Rewriter, SourceRange, c_interop_string]), +("clang_CXRewriter_writeMainFileToStdOut", [Rewriter]), ("clang_CXXConstructor_isConvertingConstructor", [Cursor], bool), ("clang_CXXConstructor_isCopyConstructor", [Cursor], bool), ("clang_CXXConstructor_isDefaultConstructor", [Cursor], bool), diff --git a/clang/bindings/python/tests/cindex/test_rewrite.py b/clang/bindings/python/tests/cindex/test_rewrite.py new file mode 100644 index 000..eb697f72a923030 --- /dev/null +++ b/clang/bindings/python/tests/cindex/test_rewrite.py @@ -0,0 +1,74 @@ +import sys +import io +import unittest +import tempfile + +from clang.cindex import Rewriter, TranslationUnit, Config, File, SourceLocation, SourceRange + +class TestRewrite(unittest.TestCase): +code = ''' +int test1; + +void test2(void); + +int f(int c) { +return c; +} +''' + +@classmethod +def setUpClass(cls): +Config.set_compatibility_check(False) + +def setUp(self): +self.tmp = tempfile.NamedTemporaryFile(suffix='.cpp', buffering=0) +self.tmp.write(TestRewrite.code.encode('utf-8')) +self.tmp.flush() +self.tu = TranslationUnit.from_source(self.tmp.name) +self.rew = Rewriter.create(self.tu) +self.file = File.from_name(self.tu, self.tmp.name) + +def tearDown(self): +self.tmp.close() + + +def test_insert(self): +snip = '#include \n' + +beginning = SourceLocation.from_offset(self.tu, self.file, 0) +self.rew.insertTextBefore(beginning, snip) +self.rew.overwriteChangedFiles() + +with open(self.tmp.name, 'r', encoding='utf-8') as f: +self.assertEqual(f.read(), snip + TestRewrite.code) + + +def test_replace(self): +pattern = 'test2' +replacement = 'func' + +offset = TestRewri
[clang] [libclang/python] Expose Rewriter to the libclang python binding. (PR #71341)
https://github.com/jimmy-zx updated https://github.com/llvm/llvm-project/pull/71341 >From 887751e365ca72515679f61d0734b1631ac38335 Mon Sep 17 00:00:00 2001 From: Jimmy Z <51149050+jimmy...@users.noreply.github.com> Date: Mon, 6 Nov 2023 00:25:39 + Subject: [PATCH 1/4] [libclang/python] Expose Rewriter to the libclang python binding. --- clang/bindings/python/clang/cindex.py | 62 .../python/tests/cindex/test_rewrite.py | 74 +++ 2 files changed, 136 insertions(+) create mode 100644 clang/bindings/python/tests/cindex/test_rewrite.py diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index 6a16f3a9ef6e957..e51d558ab73fbce 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -3531,6 +3531,61 @@ def cursor(self): return cursor +class Rewriter(ClangObject): +""" +The Rewriter is a wrapper class around clang::Rewriter + +It enables rewriting buffers. +""" + +@staticmethod +def create(tu): +""" +Creates a new Rewriter +Parameters: +tu -- The translation unit for the target AST. +""" +return Rewriter(conf.lib.clang_CXRewriter_create(tu)) + +def __init__(self, ptr): +ClangObject.__init__(self, ptr) + +def __del__(self): +conf.lib.clang_CXRewriter_dispose(self) + +def insertTextBefore(self, loc, insert): +""" +Insert the specified string at the specified location in the original buffer. +""" +conf.lib.clang_CXRewriter_insertTextBefore(self, loc, insert) + +def replaceText(self, toBeReplaced, replacement): +""" +This method replaces a range of characters in the input buffer with a new string. +""" +conf.lib.clang_CXRewriter_replaceText(self, toBeReplaced, replacement) + +def removeText(self, toBeRemoved): +""" +Remove the specified text region. +""" +conf.lib.clang_CXRewriter_removeText(self, toBeRemoved) + +def overwriteChangedFiles(self): +""" +Save all changed files to disk. +""" +conf.lib.clang_CXRewriter_overwriteChangedFiles(self) + +def writeMainFileToStdOut(self): +""" +Writes the main file to stdout. +""" +conf.lib.clang_CXRewriter_writeMainFileToStdOut(self) + + + + # Now comes the plumbing to hook up the C library. # Register callback types in common container. @@ -3596,6 +3651,13 @@ def cursor(self): ("clang_codeCompleteGetNumDiagnostics", [CodeCompletionResults], c_int), ("clang_createIndex", [c_int, c_int], c_object_p), ("clang_createTranslationUnit", [Index, c_interop_string], c_object_p), +("clang_CXRewriter_create", [TranslationUnit], c_object_p), +("clang_CXRewriter_dispose", [Rewriter]), +("clang_CXRewriter_insertTextBefore", [Rewriter, SourceLocation, c_interop_string]), +("clang_CXRewriter_overwriteChangedFiles", [Rewriter], c_int), +("clang_CXRewriter_removeText", [Rewriter, SourceRange]), +("clang_CXRewriter_replaceText", [Rewriter, SourceRange, c_interop_string]), +("clang_CXRewriter_writeMainFileToStdOut", [Rewriter]), ("clang_CXXConstructor_isConvertingConstructor", [Cursor], bool), ("clang_CXXConstructor_isCopyConstructor", [Cursor], bool), ("clang_CXXConstructor_isDefaultConstructor", [Cursor], bool), diff --git a/clang/bindings/python/tests/cindex/test_rewrite.py b/clang/bindings/python/tests/cindex/test_rewrite.py new file mode 100644 index 000..eb697f72a923030 --- /dev/null +++ b/clang/bindings/python/tests/cindex/test_rewrite.py @@ -0,0 +1,74 @@ +import sys +import io +import unittest +import tempfile + +from clang.cindex import Rewriter, TranslationUnit, Config, File, SourceLocation, SourceRange + +class TestRewrite(unittest.TestCase): +code = ''' +int test1; + +void test2(void); + +int f(int c) { +return c; +} +''' + +@classmethod +def setUpClass(cls): +Config.set_compatibility_check(False) + +def setUp(self): +self.tmp = tempfile.NamedTemporaryFile(suffix='.cpp', buffering=0) +self.tmp.write(TestRewrite.code.encode('utf-8')) +self.tmp.flush() +self.tu = TranslationUnit.from_source(self.tmp.name) +self.rew = Rewriter.create(self.tu) +self.file = File.from_name(self.tu, self.tmp.name) + +def tearDown(self): +self.tmp.close() + + +def test_insert(self): +snip = '#include \n' + +beginning = SourceLocation.from_offset(self.tu, self.file, 0) +self.rew.insertTextBefore(beginning, snip) +self.rew.overwriteChangedFiles() + +with open(self.tmp.name, 'r', encoding='utf-8') as f: +self.assertEqual(f.read(), snip + TestRewrite.code) + + +def test_replace(self): +pattern = 'test2' +replacement = 'func' + +offset = TestRewri
[clang] [libclang/python] Expose Rewriter to the libclang python binding. (PR #71341)
https://github.com/jimmy-zx updated https://github.com/llvm/llvm-project/pull/71341 >From 887751e365ca72515679f61d0734b1631ac38335 Mon Sep 17 00:00:00 2001 From: Jimmy Z <51149050+jimmy...@users.noreply.github.com> Date: Mon, 6 Nov 2023 00:25:39 + Subject: [PATCH 1/5] [libclang/python] Expose Rewriter to the libclang python binding. --- clang/bindings/python/clang/cindex.py | 62 .../python/tests/cindex/test_rewrite.py | 74 +++ 2 files changed, 136 insertions(+) create mode 100644 clang/bindings/python/tests/cindex/test_rewrite.py diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index 6a16f3a9ef6e957..e51d558ab73fbce 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -3531,6 +3531,61 @@ def cursor(self): return cursor +class Rewriter(ClangObject): +""" +The Rewriter is a wrapper class around clang::Rewriter + +It enables rewriting buffers. +""" + +@staticmethod +def create(tu): +""" +Creates a new Rewriter +Parameters: +tu -- The translation unit for the target AST. +""" +return Rewriter(conf.lib.clang_CXRewriter_create(tu)) + +def __init__(self, ptr): +ClangObject.__init__(self, ptr) + +def __del__(self): +conf.lib.clang_CXRewriter_dispose(self) + +def insertTextBefore(self, loc, insert): +""" +Insert the specified string at the specified location in the original buffer. +""" +conf.lib.clang_CXRewriter_insertTextBefore(self, loc, insert) + +def replaceText(self, toBeReplaced, replacement): +""" +This method replaces a range of characters in the input buffer with a new string. +""" +conf.lib.clang_CXRewriter_replaceText(self, toBeReplaced, replacement) + +def removeText(self, toBeRemoved): +""" +Remove the specified text region. +""" +conf.lib.clang_CXRewriter_removeText(self, toBeRemoved) + +def overwriteChangedFiles(self): +""" +Save all changed files to disk. +""" +conf.lib.clang_CXRewriter_overwriteChangedFiles(self) + +def writeMainFileToStdOut(self): +""" +Writes the main file to stdout. +""" +conf.lib.clang_CXRewriter_writeMainFileToStdOut(self) + + + + # Now comes the plumbing to hook up the C library. # Register callback types in common container. @@ -3596,6 +3651,13 @@ def cursor(self): ("clang_codeCompleteGetNumDiagnostics", [CodeCompletionResults], c_int), ("clang_createIndex", [c_int, c_int], c_object_p), ("clang_createTranslationUnit", [Index, c_interop_string], c_object_p), +("clang_CXRewriter_create", [TranslationUnit], c_object_p), +("clang_CXRewriter_dispose", [Rewriter]), +("clang_CXRewriter_insertTextBefore", [Rewriter, SourceLocation, c_interop_string]), +("clang_CXRewriter_overwriteChangedFiles", [Rewriter], c_int), +("clang_CXRewriter_removeText", [Rewriter, SourceRange]), +("clang_CXRewriter_replaceText", [Rewriter, SourceRange, c_interop_string]), +("clang_CXRewriter_writeMainFileToStdOut", [Rewriter]), ("clang_CXXConstructor_isConvertingConstructor", [Cursor], bool), ("clang_CXXConstructor_isCopyConstructor", [Cursor], bool), ("clang_CXXConstructor_isDefaultConstructor", [Cursor], bool), diff --git a/clang/bindings/python/tests/cindex/test_rewrite.py b/clang/bindings/python/tests/cindex/test_rewrite.py new file mode 100644 index 000..eb697f72a923030 --- /dev/null +++ b/clang/bindings/python/tests/cindex/test_rewrite.py @@ -0,0 +1,74 @@ +import sys +import io +import unittest +import tempfile + +from clang.cindex import Rewriter, TranslationUnit, Config, File, SourceLocation, SourceRange + +class TestRewrite(unittest.TestCase): +code = ''' +int test1; + +void test2(void); + +int f(int c) { +return c; +} +''' + +@classmethod +def setUpClass(cls): +Config.set_compatibility_check(False) + +def setUp(self): +self.tmp = tempfile.NamedTemporaryFile(suffix='.cpp', buffering=0) +self.tmp.write(TestRewrite.code.encode('utf-8')) +self.tmp.flush() +self.tu = TranslationUnit.from_source(self.tmp.name) +self.rew = Rewriter.create(self.tu) +self.file = File.from_name(self.tu, self.tmp.name) + +def tearDown(self): +self.tmp.close() + + +def test_insert(self): +snip = '#include \n' + +beginning = SourceLocation.from_offset(self.tu, self.file, 0) +self.rew.insertTextBefore(beginning, snip) +self.rew.overwriteChangedFiles() + +with open(self.tmp.name, 'r', encoding='utf-8') as f: +self.assertEqual(f.read(), snip + TestRewrite.code) + + +def test_replace(self): +pattern = 'test2' +replacement = 'func' + +offset = TestRewri
[clang] [libclang/python] Expose Rewriter to the libclang python binding. (PR #71341)
https://github.com/jimmy-zx closed https://github.com/llvm/llvm-project/pull/71341 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [libclang/python] Add cursorkinds 272-306 to the python binding (PR #77268)
https://github.com/jimmy-zx created https://github.com/llvm/llvm-project/pull/77268 Added cursorkinds 272-306 to the python binding as in `index.h`. Note: seems like `CursorKind.OMP_SCOPE_DIRECTIVE` is not in any of the group. I have changed the test cases to reflect that but I am not sure if this is a bug or intended behavior. >From 32b62205d18bf96651db4f695075891b493e6b7e Mon Sep 17 00:00:00 2001 From: Jimmy Z <51149050+jimmy...@users.noreply.github.com> Date: Mon, 8 Jan 2024 04:11:47 + Subject: [PATCH] [libclang] Add cursorkinds 272-306 --- clang/bindings/python/clang/cindex.py | 105 ++ .../python/tests/cindex/test_cursor_kind.py | 1 + 2 files changed, 106 insertions(+) diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index d780ee353a133c..663373baec7856 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -1343,6 +1343,111 @@ def __repr__(self): # OpenMP teams distribute directive. CursorKind.OMP_TEAMS_DISTRIBUTE_DIRECTIVE = CursorKind(271) +# OpenMP teams distribute simd directive. +CursorKind.OMP_TEAMS_DISTRIBUTE_SIMD_DIRECTIVE = CursorKind(272) + +# OpenMP teams distribute parallel for simd directive. +CursorKind.OMP_TEAMS_DISTRIBUTE_PARALLEL_FOR_SIMD_DIRECTIVE = CursorKind(273) + +# OpenMP teams distribute parallel for directive. +CursorKind.OMP_TEAMS_DISTRIBUTE_PARALLEL_FOR_DIRECTIVE = CursorKind(274) + +# OpenMP target teams directive. +CursorKind.OMP_TARGET_TEAMS_DIRECTIVE = CursorKind(275) + +# OpenMP target teams distribute directive. +CursorKind.OMP_TARGET_TEAMS_DISTRIBUTE_DIRECTIVE = CursorKind(276) + +# OpenMP target teams distribute parallel for directive. +CursorKind.OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_FOR_DIRECTIVE = CursorKind(277) + +# OpenMP target teams distribute parallel for simd directive. +CursorKind.OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_FOR_SIMD_DIRECTIVE = CursorKind(278) + +# OpenMP target teams distribute simd directive. +CursorKind.OMP_TARGET_TEAMS_DISTRIBUTE_SIMD_DIRECTIVE = CursorKind(279) + +# C++2a std::bit_cast expression. +CursorKind.BUILTIN_BIT_CAST_EXPR = CursorKind(280) + +# OpenMP master taskloop directive. +CursorKind.OMP_MASTER_TASK_LOOP_DIRECTIVE = CursorKind(281) + +# OpenMP parallel master taskloop directive. +CursorKind.OMP_PARALLEL_MASTER_TASK_LOOP_DIRECTIVE = CursorKind(282) + +# OpenMP master taskloop simd directive. +CursorKind.OMP_MASTER_TASK_LOOP_SIMD_DIRECTIVE = CursorKind(283) + +# OpenMP parallel master taskloop simd directive. +CursorKind.OMP_PARALLEL_MASTER_TASK_LOOP_SIMD_DIRECTIVE = CursorKind(284) + +# OpenMP parallel master directive. +CursorKind.OMP_PARALLEL_MASTER_DIRECTIVE = CursorKind(285) + +# OpenMP depobj directive. +CursorKind.OMP_DEPOBJ_DIRECTIVE = CursorKind(286) + +# OpenMP scan directive. +CursorKind.OMP_SCAN_DIRECTIVE = CursorKind(287) + +# OpenMP tile directive. +CursorKind.OMP_TILE_DIRECTIVE = CursorKind(288) + +# OpenMP canonical loop. +CursorKind.OMP_CANONICAL_LOOP = CursorKind(289) + +# OpenMP interop directive. +CursorKind.OMP_INTEROP_DIRECTIVE = CursorKind(290) + +# OpenMP dispatch directive. +CursorKind.OMP_DISPATCH_DIRECTIVE = CursorKind(291) + +# OpenMP masked directive. +CursorKind.OMP_MASKED_DIRECTIVE = CursorKind(292) + +# OpenMP unroll directive. +CursorKind.OMP_UNROLL_DIRECTIVE = CursorKind(293) + +# OpenMP metadirective directive. +CursorKind.OMP_META_DIRECTIVE = CursorKind(294) + +# OpenMP loop directive. +CursorKind.OMP_GENERIC_LOOP_DIRECTIVE = CursorKind(295) + +# OpenMP teams loop directive. +CursorKind.OMP_TEAMS_GENERIC_LOOP_DIRECTIVE = CursorKind(296) + +# OpenMP target teams loop directive. +CursorKind.OMP_TARGET_TEAMS_GENERIC_LOOP_DIRECTIVE = CursorKind(297) + +# OpenMP parallel loop directive. +CursorKind.OMP_PARALLEL_GENERIC_LOOP_DIRECTIVE = CursorKind(298) + +# OpenMP target parallel loop directive. +CursorKind.OMP_TARGET_PARALLEL_GENERIC_LOOP_DIRECTIVE = CursorKind(299) + +# OpenMP parallel masked directive. +CursorKind.OMP_PARALLEL_MASKED_DIRECTIVE = CursorKind(300) + +# OpenMP masked taskloop directive. +CursorKind.OMP_MASKED_TASK_LOOP_DIRECTIVE = CursorKind(301) + +# OpenMP masked taskloop simd directive. +CursorKind.OMP_MASKED_TASK_LOOP_SIMD_DIRECTIVE = CursorKind(302) + +# OpenMP parallel masked taskloop directive. +CursorKind.OMP_PARALLEL_MASKED_TASK_LOOP_DIRECTIVE = CursorKind(303) + +# OpenMP parallel masked taskloop simd directive. +CursorKind.OMP_PARALLEL_MASKED_TASK_LOOP_SIMD_DIRECTIVE = CursorKind(304) + +# OpenMP error directive. +CursorKind.OMP_ERROR_DIRECTIVE = CursorKind(305) + +# OpenMP scope directive. +CursorKind.OMP_SCOPE_DIRECTIVE = CursorKind(306) + ### # Other Kinds diff --git a/clang/bindings/python/tests/cindex/test_cursor_kind.py b/clang/bindings/python/tests/cindex/test_cursor_kind.py index 87199dba06ed25..d21e362fa73aed 100644 --- a/clang/bindings/python/tests/cindex/test_cursor_kind.py +++ b/clang/bindings/python/tests/cindex/test_cursor
[clang] [libclang/python] Expose Rewriter to the python binding (PR #77269)
https://github.com/jimmy-zx created https://github.com/llvm/llvm-project/pull/77269 Exposes `CXRewriter` API to the python binding as in https://github.com/llvm/llvm-project/commit/69e5abb57b70570cf04671a93246e5e624023650. >From a5379ca876d9531d48b37b9ad9c864db98c7dcd6 Mon Sep 17 00:00:00 2001 From: Jimmy Z <51149050+jimmy...@users.noreply.github.com> Date: Mon, 8 Jan 2024 04:36:27 + Subject: [PATCH] [libclang/python] Expose Rewriter to the python binding --- clang/bindings/python/clang/cindex.py | 62 +++ .../python/tests/cindex/test_rewrite.py | 75 +++ 2 files changed, 137 insertions(+) create mode 100644 clang/bindings/python/tests/cindex/test_rewrite.py diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index d780ee353a133c..ced449180d98fc 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -3531,6 +3531,61 @@ def cursor(self): return cursor +class Rewriter(ClangObject): +""" +The Rewriter is a wrapper class around clang::Rewriter + +It enables rewriting buffers. +""" + +@staticmethod +def create(tu): +""" +Creates a new Rewriter +Parameters: +tu -- The translation unit for the target AST. +""" +return Rewriter(conf.lib.clang_CXRewriter_create(tu)) + +def __init__(self, ptr): +ClangObject.__init__(self, ptr) + +def __del__(self): +conf.lib.clang_CXRewriter_dispose(self) + +def insertTextBefore(self, loc, insert): +""" +Insert the specified string at the specified location in the original buffer. +""" +conf.lib.clang_CXRewriter_insertTextBefore(self, loc, insert) + +def replaceText(self, toBeReplaced, replacement): +""" +This method replaces a range of characters in the input buffer with a new string. +""" +conf.lib.clang_CXRewriter_replaceText(self, toBeReplaced, replacement) + +def removeText(self, toBeRemoved): +""" +Remove the specified text region. +""" +conf.lib.clang_CXRewriter_removeText(self, toBeRemoved) + +def overwriteChangedFiles(self): +""" +Save all changed files to disk. + +Returns 1 if any files were not saved successfully, returns 0 otherwise. +""" +return conf.lib.clang_CXRewriter_overwriteChangedFiles(self) + +def writeMainFileToStdOut(self): +""" +Writes the main file to stdout. +""" +conf.lib.clang_CXRewriter_writeMainFileToStdOut(self) + + # Now comes the plumbing to hook up the C library. # Register callback types in common container. @@ -3596,6 +3651,13 @@ def cursor(self): ("clang_codeCompleteGetNumDiagnostics", [CodeCompletionResults], c_int), ("clang_createIndex", [c_int, c_int], c_object_p), ("clang_createTranslationUnit", [Index, c_interop_string], c_object_p), +("clang_CXRewriter_create", [TranslationUnit], c_object_p), +("clang_CXRewriter_dispose", [Rewriter]), +("clang_CXRewriter_insertTextBefore", [Rewriter, SourceLocation, c_interop_string]), +("clang_CXRewriter_overwriteChangedFiles", [Rewriter], c_int), +("clang_CXRewriter_removeText", [Rewriter, SourceRange]), +("clang_CXRewriter_replaceText", [Rewriter, SourceRange, c_interop_string]), +("clang_CXRewriter_writeMainFileToStdOut", [Rewriter]), ("clang_CXXConstructor_isConvertingConstructor", [Cursor], bool), ("clang_CXXConstructor_isCopyConstructor", [Cursor], bool), ("clang_CXXConstructor_isDefaultConstructor", [Cursor], bool), diff --git a/clang/bindings/python/tests/cindex/test_rewrite.py b/clang/bindings/python/tests/cindex/test_rewrite.py new file mode 100644 index 00..2c5f904ce50bdc --- /dev/null +++ b/clang/bindings/python/tests/cindex/test_rewrite.py @@ -0,0 +1,75 @@ +import sys +import io +import unittest +import tempfile + +from clang.cindex import ( +Rewriter, +TranslationUnit, +Config, +File, +SourceLocation, +SourceRange, +) + + +class TestRewrite(unittest.TestCase): +code = """ +int test1; + +void test2(void); + +int f(int c) { +return c; +} +""" + +def setUp(self): +self.tmp = tempfile.NamedTemporaryFile(suffix=".cpp", buffering=0) +self.tmp.write(TestRewrite.code.encode("utf-8")) +self.tmp.flush() +self.tu = TranslationUnit.from_source(self.tmp.name) +self.rew = Rewriter.create(self.tu) +self.file = File.from_name(self.tu, self.tmp.name) + +def tearDown(self): +self.tmp.close() + +def test_insert(self): +snip = "#include \n" + +beginning = SourceLocation.from_offset(self.tu, self.file, 0) +self.rew.insertTextBefore(beginning, snip) +self.rew.overwriteChangedFiles() + +with open(self.tmp.name, "r", encoding="utf-8") as f: +self.assertEqual(f.r
[clang] [libclang/python] Expose Rewriter to the python binding (PR #77269)
jimmy-zx wrote: Pinging @AaronBallman and @jyknight for review. (sorry if I chose the wrong people) https://github.com/llvm/llvm-project/pull/77269 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [libclang/python] Expose Rewriter to the python binding (PR #77269)
jimmy-zx wrote: Ping. Can one of you press the merge button? https://github.com/llvm/llvm-project/pull/77269 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [libclang/python] Expose Rewriter to the python binding (PR #77269)
https://github.com/jimmy-zx updated https://github.com/llvm/llvm-project/pull/77269 >From a5379ca876d9531d48b37b9ad9c864db98c7dcd6 Mon Sep 17 00:00:00 2001 From: Jimmy Z <51149050+jimmy...@users.noreply.github.com> Date: Mon, 8 Jan 2024 04:36:27 + Subject: [PATCH 1/2] [libclang/python] Expose Rewriter to the python binding --- clang/bindings/python/clang/cindex.py | 62 +++ .../python/tests/cindex/test_rewrite.py | 75 +++ 2 files changed, 137 insertions(+) create mode 100644 clang/bindings/python/tests/cindex/test_rewrite.py diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index d780ee353a133cb..ced449180d98fca 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -3531,6 +3531,61 @@ def cursor(self): return cursor +class Rewriter(ClangObject): +""" +The Rewriter is a wrapper class around clang::Rewriter + +It enables rewriting buffers. +""" + +@staticmethod +def create(tu): +""" +Creates a new Rewriter +Parameters: +tu -- The translation unit for the target AST. +""" +return Rewriter(conf.lib.clang_CXRewriter_create(tu)) + +def __init__(self, ptr): +ClangObject.__init__(self, ptr) + +def __del__(self): +conf.lib.clang_CXRewriter_dispose(self) + +def insertTextBefore(self, loc, insert): +""" +Insert the specified string at the specified location in the original buffer. +""" +conf.lib.clang_CXRewriter_insertTextBefore(self, loc, insert) + +def replaceText(self, toBeReplaced, replacement): +""" +This method replaces a range of characters in the input buffer with a new string. +""" +conf.lib.clang_CXRewriter_replaceText(self, toBeReplaced, replacement) + +def removeText(self, toBeRemoved): +""" +Remove the specified text region. +""" +conf.lib.clang_CXRewriter_removeText(self, toBeRemoved) + +def overwriteChangedFiles(self): +""" +Save all changed files to disk. + +Returns 1 if any files were not saved successfully, returns 0 otherwise. +""" +return conf.lib.clang_CXRewriter_overwriteChangedFiles(self) + +def writeMainFileToStdOut(self): +""" +Writes the main file to stdout. +""" +conf.lib.clang_CXRewriter_writeMainFileToStdOut(self) + + # Now comes the plumbing to hook up the C library. # Register callback types in common container. @@ -3596,6 +3651,13 @@ def cursor(self): ("clang_codeCompleteGetNumDiagnostics", [CodeCompletionResults], c_int), ("clang_createIndex", [c_int, c_int], c_object_p), ("clang_createTranslationUnit", [Index, c_interop_string], c_object_p), +("clang_CXRewriter_create", [TranslationUnit], c_object_p), +("clang_CXRewriter_dispose", [Rewriter]), +("clang_CXRewriter_insertTextBefore", [Rewriter, SourceLocation, c_interop_string]), +("clang_CXRewriter_overwriteChangedFiles", [Rewriter], c_int), +("clang_CXRewriter_removeText", [Rewriter, SourceRange]), +("clang_CXRewriter_replaceText", [Rewriter, SourceRange, c_interop_string]), +("clang_CXRewriter_writeMainFileToStdOut", [Rewriter]), ("clang_CXXConstructor_isConvertingConstructor", [Cursor], bool), ("clang_CXXConstructor_isCopyConstructor", [Cursor], bool), ("clang_CXXConstructor_isDefaultConstructor", [Cursor], bool), diff --git a/clang/bindings/python/tests/cindex/test_rewrite.py b/clang/bindings/python/tests/cindex/test_rewrite.py new file mode 100644 index 000..2c5f904ce50bdc7 --- /dev/null +++ b/clang/bindings/python/tests/cindex/test_rewrite.py @@ -0,0 +1,75 @@ +import sys +import io +import unittest +import tempfile + +from clang.cindex import ( +Rewriter, +TranslationUnit, +Config, +File, +SourceLocation, +SourceRange, +) + + +class TestRewrite(unittest.TestCase): +code = """ +int test1; + +void test2(void); + +int f(int c) { +return c; +} +""" + +def setUp(self): +self.tmp = tempfile.NamedTemporaryFile(suffix=".cpp", buffering=0) +self.tmp.write(TestRewrite.code.encode("utf-8")) +self.tmp.flush() +self.tu = TranslationUnit.from_source(self.tmp.name) +self.rew = Rewriter.create(self.tu) +self.file = File.from_name(self.tu, self.tmp.name) + +def tearDown(self): +self.tmp.close() + +def test_insert(self): +snip = "#include \n" + +beginning = SourceLocation.from_offset(self.tu, self.file, 0) +self.rew.insertTextBefore(beginning, snip) +self.rew.overwriteChangedFiles() + +with open(self.tmp.name, "r", encoding="utf-8") as f: +self.assertEqual(f.read(), snip + TestRewrite.code) + +def test_replace(self): +pattern = "test2" +replacement = "func" + +of
[clang] [libclang/python] Expose Rewriter to the python binding (PR #77269)
https://github.com/jimmy-zx updated https://github.com/llvm/llvm-project/pull/77269 >From a5379ca876d9531d48b37b9ad9c864db98c7dcd6 Mon Sep 17 00:00:00 2001 From: Jimmy Z <51149050+jimmy...@users.noreply.github.com> Date: Mon, 8 Jan 2024 04:36:27 + Subject: [PATCH 1/3] [libclang/python] Expose Rewriter to the python binding --- clang/bindings/python/clang/cindex.py | 62 +++ .../python/tests/cindex/test_rewrite.py | 75 +++ 2 files changed, 137 insertions(+) create mode 100644 clang/bindings/python/tests/cindex/test_rewrite.py diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index d780ee353a133c..ced449180d98fc 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -3531,6 +3531,61 @@ def cursor(self): return cursor +class Rewriter(ClangObject): +""" +The Rewriter is a wrapper class around clang::Rewriter + +It enables rewriting buffers. +""" + +@staticmethod +def create(tu): +""" +Creates a new Rewriter +Parameters: +tu -- The translation unit for the target AST. +""" +return Rewriter(conf.lib.clang_CXRewriter_create(tu)) + +def __init__(self, ptr): +ClangObject.__init__(self, ptr) + +def __del__(self): +conf.lib.clang_CXRewriter_dispose(self) + +def insertTextBefore(self, loc, insert): +""" +Insert the specified string at the specified location in the original buffer. +""" +conf.lib.clang_CXRewriter_insertTextBefore(self, loc, insert) + +def replaceText(self, toBeReplaced, replacement): +""" +This method replaces a range of characters in the input buffer with a new string. +""" +conf.lib.clang_CXRewriter_replaceText(self, toBeReplaced, replacement) + +def removeText(self, toBeRemoved): +""" +Remove the specified text region. +""" +conf.lib.clang_CXRewriter_removeText(self, toBeRemoved) + +def overwriteChangedFiles(self): +""" +Save all changed files to disk. + +Returns 1 if any files were not saved successfully, returns 0 otherwise. +""" +return conf.lib.clang_CXRewriter_overwriteChangedFiles(self) + +def writeMainFileToStdOut(self): +""" +Writes the main file to stdout. +""" +conf.lib.clang_CXRewriter_writeMainFileToStdOut(self) + + # Now comes the plumbing to hook up the C library. # Register callback types in common container. @@ -3596,6 +3651,13 @@ def cursor(self): ("clang_codeCompleteGetNumDiagnostics", [CodeCompletionResults], c_int), ("clang_createIndex", [c_int, c_int], c_object_p), ("clang_createTranslationUnit", [Index, c_interop_string], c_object_p), +("clang_CXRewriter_create", [TranslationUnit], c_object_p), +("clang_CXRewriter_dispose", [Rewriter]), +("clang_CXRewriter_insertTextBefore", [Rewriter, SourceLocation, c_interop_string]), +("clang_CXRewriter_overwriteChangedFiles", [Rewriter], c_int), +("clang_CXRewriter_removeText", [Rewriter, SourceRange]), +("clang_CXRewriter_replaceText", [Rewriter, SourceRange, c_interop_string]), +("clang_CXRewriter_writeMainFileToStdOut", [Rewriter]), ("clang_CXXConstructor_isConvertingConstructor", [Cursor], bool), ("clang_CXXConstructor_isCopyConstructor", [Cursor], bool), ("clang_CXXConstructor_isDefaultConstructor", [Cursor], bool), diff --git a/clang/bindings/python/tests/cindex/test_rewrite.py b/clang/bindings/python/tests/cindex/test_rewrite.py new file mode 100644 index 00..2c5f904ce50bdc --- /dev/null +++ b/clang/bindings/python/tests/cindex/test_rewrite.py @@ -0,0 +1,75 @@ +import sys +import io +import unittest +import tempfile + +from clang.cindex import ( +Rewriter, +TranslationUnit, +Config, +File, +SourceLocation, +SourceRange, +) + + +class TestRewrite(unittest.TestCase): +code = """ +int test1; + +void test2(void); + +int f(int c) { +return c; +} +""" + +def setUp(self): +self.tmp = tempfile.NamedTemporaryFile(suffix=".cpp", buffering=0) +self.tmp.write(TestRewrite.code.encode("utf-8")) +self.tmp.flush() +self.tu = TranslationUnit.from_source(self.tmp.name) +self.rew = Rewriter.create(self.tu) +self.file = File.from_name(self.tu, self.tmp.name) + +def tearDown(self): +self.tmp.close() + +def test_insert(self): +snip = "#include \n" + +beginning = SourceLocation.from_offset(self.tu, self.file, 0) +self.rew.insertTextBefore(beginning, snip) +self.rew.overwriteChangedFiles() + +with open(self.tmp.name, "r", encoding="utf-8") as f: +self.assertEqual(f.read(), snip + TestRewrite.code) + +def test_replace(self): +pattern = "test2" +replacement = "func" + +offset
[clang] [libclang/python] Expose Rewriter to the python binding (PR #77269)
jimmy-zx wrote: > Looks good overall. It's good that you wrote your own tests, but it would > also be nice to mirror tests in `clang/unittests/libclang/LibclangTest.cpp` > which test the same API. This way we can identify issues in binding layer > itself (when C++ tests pass, but Python tests don't, or vise versa,) I have reviewed the tests for libclang and it appears that there are already tests for the rewriter, which are more extensive than the ones I wrote. Therefore, I have decided to mirror the tests from libclang in the Python binding. Please let me know if this approach is appropriate. https://github.com/llvm/llvm-project/pull/77269 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [libclang/python] Expose Rewriter to the python binding (PR #77269)
jimmy-zx wrote: Both tests are just invoking the functionalities of the rewriter on a sample code. I think additional tests for libclang does not provide extra coverage (as it is just the front-end). Thus I mirrored the tests back to the Python binding in 7a4ebf9c8c9ffa29d7f7f863d9a19bfe53432348. https://github.com/llvm/llvm-project/pull/77269 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [libclang/python] Expose Rewriter to the python binding (PR #77269)
https://github.com/jimmy-zx edited https://github.com/llvm/llvm-project/pull/77269 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [libclang/python] Expose Rewriter to the python binding (PR #77269)
jimmy-zx wrote: Just updated the description a bit and I believe it's all set for merging. https://github.com/llvm/llvm-project/pull/77269 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [libclang/python] Expose Rewriter to the python binding (PR #77269)
https://github.com/jimmy-zx updated https://github.com/llvm/llvm-project/pull/77269 >From a5379ca876d9531d48b37b9ad9c864db98c7dcd6 Mon Sep 17 00:00:00 2001 From: Jimmy Z <51149050+jimmy...@users.noreply.github.com> Date: Mon, 8 Jan 2024 04:36:27 + Subject: [PATCH 1/4] [libclang/python] Expose Rewriter to the python binding --- clang/bindings/python/clang/cindex.py | 62 +++ .../python/tests/cindex/test_rewrite.py | 75 +++ 2 files changed, 137 insertions(+) create mode 100644 clang/bindings/python/tests/cindex/test_rewrite.py diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index d780ee353a133cb..ced449180d98fca 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -3531,6 +3531,61 @@ def cursor(self): return cursor +class Rewriter(ClangObject): +""" +The Rewriter is a wrapper class around clang::Rewriter + +It enables rewriting buffers. +""" + +@staticmethod +def create(tu): +""" +Creates a new Rewriter +Parameters: +tu -- The translation unit for the target AST. +""" +return Rewriter(conf.lib.clang_CXRewriter_create(tu)) + +def __init__(self, ptr): +ClangObject.__init__(self, ptr) + +def __del__(self): +conf.lib.clang_CXRewriter_dispose(self) + +def insertTextBefore(self, loc, insert): +""" +Insert the specified string at the specified location in the original buffer. +""" +conf.lib.clang_CXRewriter_insertTextBefore(self, loc, insert) + +def replaceText(self, toBeReplaced, replacement): +""" +This method replaces a range of characters in the input buffer with a new string. +""" +conf.lib.clang_CXRewriter_replaceText(self, toBeReplaced, replacement) + +def removeText(self, toBeRemoved): +""" +Remove the specified text region. +""" +conf.lib.clang_CXRewriter_removeText(self, toBeRemoved) + +def overwriteChangedFiles(self): +""" +Save all changed files to disk. + +Returns 1 if any files were not saved successfully, returns 0 otherwise. +""" +return conf.lib.clang_CXRewriter_overwriteChangedFiles(self) + +def writeMainFileToStdOut(self): +""" +Writes the main file to stdout. +""" +conf.lib.clang_CXRewriter_writeMainFileToStdOut(self) + + # Now comes the plumbing to hook up the C library. # Register callback types in common container. @@ -3596,6 +3651,13 @@ def cursor(self): ("clang_codeCompleteGetNumDiagnostics", [CodeCompletionResults], c_int), ("clang_createIndex", [c_int, c_int], c_object_p), ("clang_createTranslationUnit", [Index, c_interop_string], c_object_p), +("clang_CXRewriter_create", [TranslationUnit], c_object_p), +("clang_CXRewriter_dispose", [Rewriter]), +("clang_CXRewriter_insertTextBefore", [Rewriter, SourceLocation, c_interop_string]), +("clang_CXRewriter_overwriteChangedFiles", [Rewriter], c_int), +("clang_CXRewriter_removeText", [Rewriter, SourceRange]), +("clang_CXRewriter_replaceText", [Rewriter, SourceRange, c_interop_string]), +("clang_CXRewriter_writeMainFileToStdOut", [Rewriter]), ("clang_CXXConstructor_isConvertingConstructor", [Cursor], bool), ("clang_CXXConstructor_isCopyConstructor", [Cursor], bool), ("clang_CXXConstructor_isDefaultConstructor", [Cursor], bool), diff --git a/clang/bindings/python/tests/cindex/test_rewrite.py b/clang/bindings/python/tests/cindex/test_rewrite.py new file mode 100644 index 000..2c5f904ce50bdc7 --- /dev/null +++ b/clang/bindings/python/tests/cindex/test_rewrite.py @@ -0,0 +1,75 @@ +import sys +import io +import unittest +import tempfile + +from clang.cindex import ( +Rewriter, +TranslationUnit, +Config, +File, +SourceLocation, +SourceRange, +) + + +class TestRewrite(unittest.TestCase): +code = """ +int test1; + +void test2(void); + +int f(int c) { +return c; +} +""" + +def setUp(self): +self.tmp = tempfile.NamedTemporaryFile(suffix=".cpp", buffering=0) +self.tmp.write(TestRewrite.code.encode("utf-8")) +self.tmp.flush() +self.tu = TranslationUnit.from_source(self.tmp.name) +self.rew = Rewriter.create(self.tu) +self.file = File.from_name(self.tu, self.tmp.name) + +def tearDown(self): +self.tmp.close() + +def test_insert(self): +snip = "#include \n" + +beginning = SourceLocation.from_offset(self.tu, self.file, 0) +self.rew.insertTextBefore(beginning, snip) +self.rew.overwriteChangedFiles() + +with open(self.tmp.name, "r", encoding="utf-8") as f: +self.assertEqual(f.read(), snip + TestRewrite.code) + +def test_replace(self): +pattern = "test2" +replacement = "func" + +of
[clang] [libclang/python] Expose Rewriter to the python binding (PR #77269)
jimmy-zx wrote: @linux4life798 Thanks for pointing out! I've updated the naming to conform snake_case. https://github.com/llvm/llvm-project/pull/77269 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [libclang/python] Expose Rewriter to the python binding (PR #77269)
https://github.com/jimmy-zx updated https://github.com/llvm/llvm-project/pull/77269 >From a5379ca876d9531d48b37b9ad9c864db98c7dcd6 Mon Sep 17 00:00:00 2001 From: Jimmy Z <51149050+jimmy...@users.noreply.github.com> Date: Mon, 8 Jan 2024 04:36:27 + Subject: [PATCH 1/5] [libclang/python] Expose Rewriter to the python binding --- clang/bindings/python/clang/cindex.py | 62 +++ .../python/tests/cindex/test_rewrite.py | 75 +++ 2 files changed, 137 insertions(+) create mode 100644 clang/bindings/python/tests/cindex/test_rewrite.py diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index d780ee353a133cb..ced449180d98fca 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -3531,6 +3531,61 @@ def cursor(self): return cursor +class Rewriter(ClangObject): +""" +The Rewriter is a wrapper class around clang::Rewriter + +It enables rewriting buffers. +""" + +@staticmethod +def create(tu): +""" +Creates a new Rewriter +Parameters: +tu -- The translation unit for the target AST. +""" +return Rewriter(conf.lib.clang_CXRewriter_create(tu)) + +def __init__(self, ptr): +ClangObject.__init__(self, ptr) + +def __del__(self): +conf.lib.clang_CXRewriter_dispose(self) + +def insertTextBefore(self, loc, insert): +""" +Insert the specified string at the specified location in the original buffer. +""" +conf.lib.clang_CXRewriter_insertTextBefore(self, loc, insert) + +def replaceText(self, toBeReplaced, replacement): +""" +This method replaces a range of characters in the input buffer with a new string. +""" +conf.lib.clang_CXRewriter_replaceText(self, toBeReplaced, replacement) + +def removeText(self, toBeRemoved): +""" +Remove the specified text region. +""" +conf.lib.clang_CXRewriter_removeText(self, toBeRemoved) + +def overwriteChangedFiles(self): +""" +Save all changed files to disk. + +Returns 1 if any files were not saved successfully, returns 0 otherwise. +""" +return conf.lib.clang_CXRewriter_overwriteChangedFiles(self) + +def writeMainFileToStdOut(self): +""" +Writes the main file to stdout. +""" +conf.lib.clang_CXRewriter_writeMainFileToStdOut(self) + + # Now comes the plumbing to hook up the C library. # Register callback types in common container. @@ -3596,6 +3651,13 @@ def cursor(self): ("clang_codeCompleteGetNumDiagnostics", [CodeCompletionResults], c_int), ("clang_createIndex", [c_int, c_int], c_object_p), ("clang_createTranslationUnit", [Index, c_interop_string], c_object_p), +("clang_CXRewriter_create", [TranslationUnit], c_object_p), +("clang_CXRewriter_dispose", [Rewriter]), +("clang_CXRewriter_insertTextBefore", [Rewriter, SourceLocation, c_interop_string]), +("clang_CXRewriter_overwriteChangedFiles", [Rewriter], c_int), +("clang_CXRewriter_removeText", [Rewriter, SourceRange]), +("clang_CXRewriter_replaceText", [Rewriter, SourceRange, c_interop_string]), +("clang_CXRewriter_writeMainFileToStdOut", [Rewriter]), ("clang_CXXConstructor_isConvertingConstructor", [Cursor], bool), ("clang_CXXConstructor_isCopyConstructor", [Cursor], bool), ("clang_CXXConstructor_isDefaultConstructor", [Cursor], bool), diff --git a/clang/bindings/python/tests/cindex/test_rewrite.py b/clang/bindings/python/tests/cindex/test_rewrite.py new file mode 100644 index 000..2c5f904ce50bdc7 --- /dev/null +++ b/clang/bindings/python/tests/cindex/test_rewrite.py @@ -0,0 +1,75 @@ +import sys +import io +import unittest +import tempfile + +from clang.cindex import ( +Rewriter, +TranslationUnit, +Config, +File, +SourceLocation, +SourceRange, +) + + +class TestRewrite(unittest.TestCase): +code = """ +int test1; + +void test2(void); + +int f(int c) { +return c; +} +""" + +def setUp(self): +self.tmp = tempfile.NamedTemporaryFile(suffix=".cpp", buffering=0) +self.tmp.write(TestRewrite.code.encode("utf-8")) +self.tmp.flush() +self.tu = TranslationUnit.from_source(self.tmp.name) +self.rew = Rewriter.create(self.tu) +self.file = File.from_name(self.tu, self.tmp.name) + +def tearDown(self): +self.tmp.close() + +def test_insert(self): +snip = "#include \n" + +beginning = SourceLocation.from_offset(self.tu, self.file, 0) +self.rew.insertTextBefore(beginning, snip) +self.rew.overwriteChangedFiles() + +with open(self.tmp.name, "r", encoding="utf-8") as f: +self.assertEqual(f.read(), snip + TestRewrite.code) + +def test_replace(self): +pattern = "test2" +replacement = "func" + +of
[clang] [libclang/python] Expose Rewriter to the python binding (PR #77269)
jimmy-zx wrote: Added flush before calling `writeMainFileToStdOut`. Personally I didn't find this function particularly useful: I was hoping to perform rewriting without creating new files, but seems like capturing stdout from a shared library requires some complex operation (`dup2` stuff). `Rewriter::getRewrittenText()` might be a more interesting interface to interact with, which returns the rewritten content as a string. (This is defined in clang but not exposed by libclang yet). https://github.com/llvm/llvm-project/blob/179ba129f50aefe6e670800aec7091d958aa6f90/clang/lib/Rewrite/Rewriter.cpp#L180 https://github.com/llvm/llvm-project/pull/77269 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits