• Stars
    star
    197
  • Rank 196,559 (Top 4 %)
  • Language
    Python
  • License
    GNU General Publi...
  • Created over 12 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

GCC plugin that embeds CPython inside the compiler

gcc-python

This is a plugin for GCC, which links against libpython, and (I hope) allows you to invoke arbitrary Python scripts from inside the compiler. The aim is to allow you to write GCC plugins in Python.

The plugin is Free Software, licensed under the GPLv3 (or later).

It's still at the "experimental proof-of-concept stage"; expect crashes and tracebacks (I'm new to insides of GCC, and I may have misunderstood things).

It's already possible to use this to add additional compiler errors/warnings, e.g. domain-specific checks, or static analysis. One of my goals for this is to "teach" GCC about the common mistakes people make when writing extensions for CPython, but it could be used e.g. to teach GCC about GTK's reference-counting semantics, or about locking in the Linux kernel, or about signal-safety in APIs.

Other ideas include visualizations of code structure. Given a gcc.CFG instance, gccutils.render_to_dot(cfg) and gccutils.invoke_dot(cfg) will use graphviz and eog to plot a handy visualization of a control flow graph, showing the source code interleaved with GCC's GIMPLE internal representation.

The documentation can be seen at:

http://gcc-python-plugin.readthedocs.io/en/latest/index.html

Requirements

  • GCC: 4.6 or later (it uses APIs that weren't exposed to plugins in 4.5)
    • tested with 4.8, 4.9, 5, 6, 7, and 8.
  • GCC plugin development package: usually available in distribution packages such as gcc-N-plugin-dev or gcc-plugin-devel.
  • Python: requires 2.7 or 3.2 or later
  • "six": The libcpychecker code uses the "six" Python compatibility library to smooth over Python 2 vs Python 3 differences, both at build-time and run-time

Usage

I use:

make

to build the plugin and run the tests

You can also use:

make demo

to demonstrate the new compiler errors.

Development has been on x86_64 and I don't know to what extent it will be compatible with other architectures.

There isn't an installer yet. In theory you should be able to add these arguments to the gcc invocation:

gcc -fplugin=python.so -fplugin-arg-python-script=PATH_TO_SCRIPT.py OTHER_ARGS

and have it run your script as the plugin starts up.

The plugin automatically adds the absolute path to its own directory to the end of its sys.path, so that it can find support modules, such as gccutils.py and libcpychecker.

The exact API is still in flux; you can currently connect to events by registering callbacks e.g. to be called for each function in the source at different passes.

It exposes GCC's various types as Python objects, within a "gcc" module. You can see the API by running:

import gcc
help(gcc)

from within a script.

Overview of the code

This is currently three projects in one:

gcc-python-*: the plugin for GCC. The entrypoint (init_plugin) is in gcc-python.c.

libcpychecker and cpychecker.py: a Python library (and a driver script), written for the plugin, in which I'm building new compiler warnings to help people find bugs in CPython extension code.

cpybuilder: a handy module for programatically generating C source code for CPython extensions. I use this both to generate parts of the GCC plugin, and also in the selftests for the cpychecker script. (I initially attempted to use Cython for the former, but wrapping the "tree" type hierarchy required more programatic control)

Coding style: Python and GCC each have their own coding style guide for C. I've chosen to follow Python's (PEP-7), as I prefer it (although my code is admittedly a mess in places).

You'll find API documentation within the "docs" directory, written in the reStructuredText format (as is this file, in fact). If you have Sphinx installed, you can regenerate these docs using:

make html

within the docs directory. Sphinx is the python-sphinx package on a Fedora/RHEL box.

More detailed documentation can be seen within docs/getting-involved.rst.

Enjoy! David Malcolm <[email protected]>

More Repositories

1

antipatterns.ko

The world's worst kernel module
C
293
star
2

gcc-newbies-guide

An unofficial guide to contributing to GCC, aimed at newbies
Python
108
star
3

jittest

Experiments with JIT compilation
C++
64
star
4

pygccjit

Python bindings for libgccjit.so
Python
48
star
5

coconut

An experimental method JIT for CPython 3
Python
28
star
6

asmdiff

Tool for comparing assembler files/dumps
Python
17
star
7

python-krbV

C
9
star
8

gcc-refactoring-scripts

HTML
9
star
9

gcc-opt-viewer

Python
8
star
10

pygobject

C
7
star
11

gcc-build

Scripts for rebuilding gcc
C++
7
star
12

jamais-vu

Tool for working with DejaGnu output
C
6
star
13

gcc-analyzer-integration-tests

Python
6
star
14

juliet-harness

Harness for testing GCC static analyzer with Juliet test suite
Python
5
star
15

texi2rst

Python
5
star
16

squeal

"squeal" (formerly "show") is a mashup of SQL and the command-line. It lets you issue SQL-like queries upon files in the filesystem.
Python
4
star
17

sarif-dump

Python module for dumping SARIF logfiles in human-readable form
Python
3
star
18

gcc-9-blogpost

CSS
2
star
19

poopmaster3000

Renders a PDF file useful for tracking activities relating to a newborn
Python
2
star
20

every-gcc

Experimental script to help with building every configuration of GCC
Python
2
star
21

libdiagnostics-sarif-dump

Example of using libdiagnostics, to dump SARIF files
C++
2
star
22

gcc-viewer

A pygtk program for viewing GCC .dot dumps
Python
2
star
23

gcc-global-state

Plans for removal of global state from GCC's internals
Python
1
star
24

gcc-dump-diff

Tool for comparing gcc dumps
Python
1
star
25

2to3c

Experiment with using Coccinelle to port Python extension module C source from Python 2 to Python 2
Python
1
star
26

PyCon-US-2013-Talk

Python
1
star
27

gcc-analyzer-viewer

PyGTK app for viewing the output of GCC's -fdump-analyzer-json
Python
1
star
28

pgo-demo

C
1
star
29

2014-cauldron-jit-talk

Talk about libgccjit.so for GNU Tools Cauldron 2014
Python
1
star
30

test-ctor

Reproducer for issue seen with gcc selftests patchkit
Makefile
1
star