Giter Club home page Giter Club logo

traversingclangastwithpython's Introduction

Traversing Clang's AST with python

A simple guide on how to use libclang to travers clang's AST via python

Steps for Linux

Tested On Ubuntu

Install clang

Make sure the clang is installed. Otherwise, run the command below to install clang

$ sudo apt-get install clang

Create a link to libclan.so

Clang's python binding library looks for libclang.so

First, make sure that the libclang.so is available, just check where it is located?

$ whereis libclang.so

If nothing is returned, that means libclang.so is not available.

If libclang is not linked properly, an error will be thrown. Something like this:

clang.cindex.LibclangError: libclang.so: cannot open shared object file: No such file or directory. To provide a path to libclang use Config.set_library_path() or Config.set_library_file().

By default, libclang.so is located in this directory (or somewhere similar):

/usr/lib/x86_64-linux-gnu

find the libclang-\<version>.so.1 in this directory, and link it to the libclang.so by running the following command

$ sudo ln -s libclang-10.so.1 libclang.so

Setting up the clang python library

Go to this URL and copy the clang folder. Past the clang folder to the lib folder of your python interpreter. If you are not sure about where python is installed, just run the following python code snippet to find the location of python interpreter.

import sys
print(sys.executable)

Running sample files

sample-hello.cpp is just a very simple Hello World c++ program. We will create an AST for this file.

Simply run the ASTwithClang.py to see some information of the sample-hello. abstract syntax tree.

Windows Issue

Note, I have tried the steps mentioned above on Windows. I always ran into

TranslationUnitLoadError("Error parsing translation unit.")

And still could not find a way around this issue.

traversingclangastwithpython's People

Contributors

tehranixyz avatar

Stargazers

bessky avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.