How to parse single file using Python bindings to Clang?

2024/9/8 9:58:39

I am writing a simple tool to help with refactoring the source code of our application. I would like to parse C++ code based on wxWidgets library, which defines GUI and produce XML .ui file to use with Qt. I need to get all function calls and value of arguments.

Currently I am toying with Python bindings to Clang, using the example code below I get the tokens and their kind and location, but the cursor kind is always CursorKind.INVALID_FILE.

import sys
import clang.cindexdef find_typerefs(node):""" Find all references to the type named 'typename'"""for t in node.get_tokens():if not node.location.file != sys.argv[1]:continueif t.kind.value != 0 and t.kind.value != 1 and t.kind.value != 4:print t.spellingprint t.locationprint t.cursor.kindprint t.kindprint "\n"index = clang.cindex.Index.create()
tu = index.parse(sys.argv[1])
print 'Translation unit:', tu.spelling
find_typerefs(tu.cursor)

What is the correct way to determine the cursor kind?

I couldn't find any documentation except few blog posts, but they were outdated or not covering this topic. I was neither unable to work it out from examples that came with Clang .

Answer

For cursor objects, it should be ok to just use cursor.kind. Maybe the problem is that you're walking tokens instead of child cursor objects (Not sure about that). Instead of get_tokens, you can use get_children to walk the AST.

In order to see how the AST looks like, when I want to write an AST walking function, I use this script: https://gist.github.com/2503232. This just shows cursor.kind, and gives sensible outputs, on my system. No CursorKind.INVALID_FILE.

https://en.xdnf.cn/q/72815.html

Related Q&A

How can I profile a Kivy application?

Im building a game using Kivy. Im encountering performance issues so I decided to profile the program.I tried to run it by:python -m cProfile main.pyThe application screen stays black. After several se…

Set up multiple python installations on windows with tox

I am trying to set up tox on windows to run tests against multiple python installations. I have installed each python in folders named, C:\Python\PythonXX_YY, XX is the python version (e.g. 27) and YY…

How can I change the alpha value dynamically in matplotlib python

Im seeking how to change an alpha value dynamically which are already plotted.This is a kind of sample code I want to implement, but I know it is a wrong writing.import matplotlib.pyplot as pltfig = pl…

How to detect write failure in asyncio?

As a simple example, consider the network equivalent of /dev/zero, below. (Or more realistically, just a web server sending a large file.)If a client disconnects early, you get a barrage of log message…

how to get place details from place id in google places api for python

I am using the Google Places API with Python to build a collective intelligence app for food. e.g. what restaurants are around, what ratings they have, what are their timings, etc.I am doing the follow…

pandas.algos._return_false causes PicklingError with dill.dump_session on CentOS

I have a code framework which involves dumping sessions with dill. This used to work just fine, until I started to use pandas. The following code raises a PicklingError on CentOS release 6.5:import pan…

How to send an image directly from flask server to html?

I am new to flask and am trying to make an app such an image is taken by the html and js from the webcam and then it is sent to the server with ajax request. I got this part. Then some processing is do…

Alternatives to nested numpy.where for multiconditional pandas operations?

I have a Pandas DataFrame with conditional column A and numeric column B. A B 1 foo 1.2 2 bar 1.3 3 foo 2.2I also have a Python dictionary that defines ranges of B which denote "success" g…

OpenCV findContours() just returning one external contour

Im trying to isolate letters in a captcha, I managed to filter a captcha and that result in this black and white image:But when I tried to separate the letters with findContours method of OpenCV it jus…

Selenium/ChromeDriver Unknown policy Errors

I am currently using Python (v3.5.1), Selenium (v3.7), and Chromedriver (v2.33).When I run the following command:from selenium import webdriver driver = webdriver.Chrome(C:\Program Files\ChromeWebdrive…