aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang16/bindings/python/examples/cindex
diff options
context:
space:
mode:
authorthegeorg <thegeorg@yandex-team.com>2024-03-13 13:58:24 +0300
committerthegeorg <thegeorg@yandex-team.com>2024-03-13 14:11:53 +0300
commit11a895b7e15d1c5a1f52706396b82e3f9db953cb (patch)
treefabc6d883b0f946151f61ae7865cee9f529a1fdd /contrib/libs/clang16/bindings/python/examples/cindex
parent9685917341315774aad5733b1793b1e533a88bbb (diff)
downloadydb-11a895b7e15d1c5a1f52706396b82e3f9db953cb.tar.gz
Export clang-format16 via ydblib project
6e6be3a95868fde888d801b7590af4044049563f
Diffstat (limited to 'contrib/libs/clang16/bindings/python/examples/cindex')
-rw-r--r--contrib/libs/clang16/bindings/python/examples/cindex/cindex-dump.py86
-rw-r--r--contrib/libs/clang16/bindings/python/examples/cindex/cindex-dump/ya.make21
-rw-r--r--contrib/libs/clang16/bindings/python/examples/cindex/cindex-includes.py57
-rw-r--r--contrib/libs/clang16/bindings/python/examples/cindex/cindex-includes/ya.make21
4 files changed, 185 insertions, 0 deletions
diff --git a/contrib/libs/clang16/bindings/python/examples/cindex/cindex-dump.py b/contrib/libs/clang16/bindings/python/examples/cindex/cindex-dump.py
new file mode 100644
index 0000000000..46073b285c
--- /dev/null
+++ b/contrib/libs/clang16/bindings/python/examples/cindex/cindex-dump.py
@@ -0,0 +1,86 @@
+#!/usr/bin/env python
+
+#===- cindex-dump.py - cindex/Python Source Dump -------------*- python -*--===#
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+#===------------------------------------------------------------------------===#
+
+"""
+A simple command line tool for dumping a source file using the Clang Index
+Library.
+"""
+
+def get_diag_info(diag):
+ return { 'severity' : diag.severity,
+ 'location' : diag.location,
+ 'spelling' : diag.spelling,
+ 'ranges' : diag.ranges,
+ 'fixits' : diag.fixits }
+
+def get_cursor_id(cursor, cursor_list = []):
+ if not opts.showIDs:
+ return None
+
+ if cursor is None:
+ return None
+
+ # FIXME: This is really slow. It would be nice if the index API exposed
+ # something that let us hash cursors.
+ for i,c in enumerate(cursor_list):
+ if cursor == c:
+ return i
+ cursor_list.append(cursor)
+ return len(cursor_list) - 1
+
+def get_info(node, depth=0):
+ if opts.maxDepth is not None and depth >= opts.maxDepth:
+ children = None
+ else:
+ children = [get_info(c, depth+1)
+ for c in node.get_children()]
+ return { 'id' : get_cursor_id(node),
+ 'kind' : node.kind,
+ 'usr' : node.get_usr(),
+ 'spelling' : node.spelling,
+ 'location' : node.location,
+ 'extent.start' : node.extent.start,
+ 'extent.end' : node.extent.end,
+ 'is_definition' : node.is_definition(),
+ 'definition id' : get_cursor_id(node.get_definition()),
+ 'children' : children }
+
+def main():
+ from clang.cindex import Index
+ from pprint import pprint
+
+ from optparse import OptionParser, OptionGroup
+
+ global opts
+
+ parser = OptionParser("usage: %prog [options] {filename} [clang-args*]")
+ parser.add_option("", "--show-ids", dest="showIDs",
+ help="Compute cursor IDs (very slow)",
+ action="store_true", default=False)
+ parser.add_option("", "--max-depth", dest="maxDepth",
+ help="Limit cursor expansion to depth N",
+ metavar="N", type=int, default=None)
+ parser.disable_interspersed_args()
+ (opts, args) = parser.parse_args()
+
+ if len(args) == 0:
+ parser.error('invalid number arguments')
+
+ index = Index.create()
+ tu = index.parse(None, args)
+ if not tu:
+ parser.error("unable to load input")
+
+ pprint(('diags', [get_diag_info(d) for d in tu.diagnostics]))
+ pprint(('nodes', get_info(tu.cursor)))
+
+if __name__ == '__main__':
+ main()
+
diff --git a/contrib/libs/clang16/bindings/python/examples/cindex/cindex-dump/ya.make b/contrib/libs/clang16/bindings/python/examples/cindex/cindex-dump/ya.make
new file mode 100644
index 0000000000..3c4ca13798
--- /dev/null
+++ b/contrib/libs/clang16/bindings/python/examples/cindex/cindex-dump/ya.make
@@ -0,0 +1,21 @@
+# Generated by devtools/yamaker.
+
+PY3_PROGRAM()
+
+WITHOUT_LICENSE_TEXTS()
+
+LICENSE(NCSA)
+
+PEERDIR(
+ contrib/libs/clang16/bindings/python
+)
+
+NO_LINT()
+
+SRCDIR(contrib/libs/clang16/bindings/python/examples)
+
+PY_SRCS(
+ MAIN cindex/cindex-dump.py
+)
+
+END()
diff --git a/contrib/libs/clang16/bindings/python/examples/cindex/cindex-includes.py b/contrib/libs/clang16/bindings/python/examples/cindex/cindex-includes.py
new file mode 100644
index 0000000000..ec1fbc0c3e
--- /dev/null
+++ b/contrib/libs/clang16/bindings/python/examples/cindex/cindex-includes.py
@@ -0,0 +1,57 @@
+#!/usr/bin/env python
+
+#===- cindex-includes.py - cindex/Python Inclusion Graph -----*- python -*--===#
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+#===------------------------------------------------------------------------===#
+
+"""
+A simple command line tool for dumping a Graphviz description (dot) that
+describes include dependencies.
+"""
+
+def main():
+ import sys
+ from clang.cindex import Index
+
+ from optparse import OptionParser, OptionGroup
+
+ parser = OptionParser("usage: %prog [options] {filename} [clang-args*]")
+ parser.disable_interspersed_args()
+ (opts, args) = parser.parse_args()
+ if len(args) == 0:
+ parser.error('invalid number arguments')
+
+ # FIXME: Add an output file option
+ out = sys.stdout
+
+ index = Index.create()
+ tu = index.parse(None, args)
+ if not tu:
+ parser.error("unable to load input")
+
+ # A helper function for generating the node name.
+ def name(f):
+ if f:
+ return "\"" + f.name + "\""
+
+ # Generate the include graph
+ out.write("digraph G {\n")
+ for i in tu.get_includes():
+ line = " ";
+ if i.is_input_file:
+ # Always write the input file as a node just in case it doesn't
+ # actually include anything. This would generate a 1 node graph.
+ line += name(i.include)
+ else:
+ line += '%s->%s' % (name(i.source), name(i.include))
+ line += "\n";
+ out.write(line)
+ out.write("}\n")
+
+if __name__ == '__main__':
+ main()
+
diff --git a/contrib/libs/clang16/bindings/python/examples/cindex/cindex-includes/ya.make b/contrib/libs/clang16/bindings/python/examples/cindex/cindex-includes/ya.make
new file mode 100644
index 0000000000..11ba4ae13f
--- /dev/null
+++ b/contrib/libs/clang16/bindings/python/examples/cindex/cindex-includes/ya.make
@@ -0,0 +1,21 @@
+# Generated by devtools/yamaker.
+
+PY3_PROGRAM()
+
+WITHOUT_LICENSE_TEXTS()
+
+LICENSE(NCSA)
+
+PEERDIR(
+ contrib/libs/clang16/bindings/python
+)
+
+NO_LINT()
+
+SRCDIR(contrib/libs/clang16/bindings/python/examples)
+
+PY_SRCS(
+ MAIN cindex/cindex-includes.py
+)
+
+END()