Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

GCC plugin that embeds CPython inside the compiler

branch: master
Octocat-spinner-32 docs Add gcc.Location.in_system_header attribute January 24, 2014
Octocat-spinner-32 examples use separate filenames for each function in examples/show-{gimple|ssa… February 22, 2013
Octocat-spinner-32 gcc-c-api Add gcc.Location.in_system_header attribute January 24, 2014
Octocat-spinner-32 gccutils Python 3 fixes to graph-handling (Node.__lt__ and Subgraph.__lt__) April 23, 2013
Octocat-spinner-32 libcpychecker Ignore exception edges in CFG June 25, 2013
Octocat-spinner-32 misc Python3: makeindex.py: fix print-as-function SyntaxError April 08, 2012
Octocat-spinner-32 tests Add gcc.Location.in_system_header attribute January 24, 2014
Octocat-spinner-32 .gitignore add print-gcc-version to .gitignore April 22, 2013
Octocat-spinner-32 .travis.yml travis: add more deps February 11, 2013
Octocat-spinner-32 COPYING Add COPYING and COPYING.LESSER, for the LGPLv3 June 08, 2011
Octocat-spinner-32 LICENSES.txt cpychecker: include historical example of pylibmc bug March 26, 2012
Octocat-spinner-32 Makefile Pass CC when building gcc-c-api and invoking the plugin December 03, 2013
Octocat-spinner-32 README.rst docs: add a "Getting Involved" section March 20, 2012
Octocat-spinner-32 configbuilder.py Work-in-progress support for gcc 4.9 December 02, 2013
Octocat-spinner-32 contributors.rst add David Narvaez to contributors.rst January 10, 2012
Octocat-spinner-32 cpybuilder.py mass renaming of python symbols to use PyGcc_CamelCase February 15, 2013
Octocat-spinner-32 cpychecker.py Add GPLv3 license headers to all source files June 20, 2011
Octocat-spinner-32 demo.c add example of refcounting bugs to "make demo" January 18, 2013
Octocat-spinner-32 gcc-python-attribute.c Work-in-progress support for gcc 4.9 December 02, 2013
Octocat-spinner-32 gcc-python-callbacks.c mass renaming of python symbols to use PyGcc_CamelCase February 15, 2013
Octocat-spinner-32 gcc-python-callgraph.c ensure there is a unique gcc.CallgraphEdge for each underlying edge March 02, 2013
Octocat-spinner-32 gcc-python-cfg.c ensure uniqueness of gcc.Cfg for an underlying gcc_cfg March 03, 2013
Octocat-spinner-32 gcc-python-closure.c mass renaming of python symbols to use PyGcc_CamelCase February 15, 2013
Octocat-spinner-32 gcc-python-closure.h mass renaming of python symbols to use PyGcc_CamelCase February 15, 2013
Octocat-spinner-32 gcc-python-compat.h integrate with GCC's garbage collector (ticket #1) January 13, 2012
Octocat-spinner-32 gcc-python-diagnostics.c gcc-c-api: add diagnostics hooks; use them in the Python layer February 21, 2013
Octocat-spinner-32 gcc-python-docs fix gcc-python-docs March 27, 2012
Octocat-spinner-32 gcc-python-function.c mass renaming of python symbols to use PyGcc_CamelCase February 15, 2013
Octocat-spinner-32 gcc-python-gimple.c Work-in-progress support for gcc 4.9 December 02, 2013
Octocat-spinner-32 gcc-python-location.c implement gcc.Location.__hash__ March 03, 2013
Octocat-spinner-32 gcc-python-option.c work-in-progress porting to gcc 4.8 February 22, 2013
Octocat-spinner-32 gcc-python-parameter.c mass renaming of python symbols to use PyGcc_CamelCase February 15, 2013
Octocat-spinner-32 gcc-python-pass.c Work-in-progress support for gcc 4.9 December 02, 2013
Octocat-spinner-32 gcc-python-plugin.spec convert underscores to dashes in plugin names in specfile April 23, 2013
Octocat-spinner-32 gcc-python-pretty-printer.c Work-in-progress support for gcc 4.9 December 02, 2013
Octocat-spinner-32 gcc-python-rtl.c Further 4.9 porting December 03, 2013
Octocat-spinner-32 gcc-python-tree.c Work-in-progress support for gcc 4.9 December 02, 2013
Octocat-spinner-32 gcc-python-variable.c mass renaming of python symbols to use PyGcc_CamelCase February 15, 2013
Octocat-spinner-32 gcc-python-version.c mass renaming of python symbols to use PyGcc_CamelCase February 15, 2013
Octocat-spinner-32 gcc-python-wrapper.c gcc_register_root_tab expects a NULL-terminated list of ggc_root_tab April 29, 2013
Octocat-spinner-32 gcc-python-wrappers.h implement gcc.PointerType.__repr__ March 03, 2013
Octocat-spinner-32 gcc-python.c Work-in-progress support for gcc 4.9 December 02, 2013
Octocat-spinner-32 gcc-python.h Work-in-progress support for gcc 4.9 December 02, 2013
Octocat-spinner-32 gcc-with-cpychecker allow gcc-with-cpychecker to find libgcc-c-api.so when run from a wor… March 13, 2013
Octocat-spinner-32 gcc-with-python allow the use of $CC to override "gcc" in gcc-with-python and gcc-wit… March 27, 2012
Octocat-spinner-32 generate-callgraph-c.py mass renaming of python symbols to use PyGcc_CamelCase February 15, 2013
Octocat-spinner-32 generate-casts-c.py Further 4.9 porting December 03, 2013
Octocat-spinner-32 generate-cfg-c.py Ignore exception edges in CFG June 25, 2013
Octocat-spinner-32 generate-config-h.py Work-in-progress support for gcc 4.9 December 02, 2013
Octocat-spinner-32 generate-function-c.py mass renaming of python symbols to use PyGcc_CamelCase February 15, 2013
Octocat-spinner-32 generate-gimple-c.py add a "label" attribute to gcc.GimpleLabel March 03, 2013
Octocat-spinner-32 generate-location-c.py Add gcc.Location.in_system_header attribute January 24, 2014
Octocat-spinner-32 generate-option-c.py mass renaming of python symbols to use PyGcc_CamelCase February 15, 2013
Octocat-spinner-32 generate-parameter-c.py mass renaming of python symbols to use PyGcc_CamelCase February 15, 2013
Octocat-spinner-32 generate-pass-c.py mass renaming of python symbols to use PyGcc_CamelCase February 15, 2013
Octocat-spinner-32 generate-passes-svg.py Work-in-progress support for gcc 4.9 December 02, 2013
Octocat-spinner-32 generate-pretty-printer-c.py mass renaming of python symbols to use PyGcc_CamelCase February 15, 2013
Octocat-spinner-32 generate-rtl-c.py mass renaming of python symbols to use PyGcc_CamelCase February 15, 2013
Octocat-spinner-32 generate-tables-of-passes-rst.py Work-in-progress support for gcc 4.9 December 02, 2013
Octocat-spinner-32 generate-tree-c.py Work-in-progress support for gcc 4.9 December 02, 2013
Octocat-spinner-32 generate-variable-c.py mass renaming of python symbols to use PyGcc_CamelCase February 15, 2013
Octocat-spinner-32 gimple-types.txt.in Generate appropriate gcc.Gimple subclasses for each statement; add so… April 25, 2011
Octocat-spinner-32 maketreetypes.py Initial wrapping of the Register Transfer Language July 28, 2011
Octocat-spinner-32 print-gcc-version.c add print-gcc-version and use it in selftests March 06, 2013
Octocat-spinner-32 rtl-types.txt.in Initial wrapping of the Register Transfer Language July 28, 2011
Octocat-spinner-32 run-test-suite.py use -fsigned-char when running selftests April 29, 2013
Octocat-spinner-32 test-builder.py Derive gcc name from $CC, not $GCC March 25, 2012
Octocat-spinner-32 test.c Add GPLv3 license headers to all source files June 20, 2011
Octocat-spinner-32 test.py Add GPLv3 license headers to all source files June 20, 2011
Octocat-spinner-32 testcpybuilder.py Derive gcc name from $CC, not $GCC March 25, 2012
Octocat-spinner-32 testcpychecker.py use -fno-diagnostics-show-caret in testcpychecker.py with gcc >= 4.8 April 04, 2013
Octocat-spinner-32 tree-types.txt.in Try a more flexible approach to autogenerating Tree wrapper subclasses April 20, 2011
Octocat-spinner-32 wrapperbuilder.py mass renaming of python symbols to use PyGcc_CamelCase February 15, 2013
README.rst

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.

Requirements

  • GCC: 4.6 or later (it uses APIs that weren't exposed to plugins in 4.5)

  • Python: tested with 2.7 and 3.2; it may work with earlier versions

  • "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:

    http://pypi.python.org/pypi/six/

Usage

I use:

make

to build the plugin and run the tests

You can also use:

make demo

to demonstrate the new compiler errors.

All of my coding so far has been on Fedora 15 x86_64, using:

gcc-plugin-devel-4.6.0-0.15.fc15.x86_64

and I don't know to what extent it will be compatible with other versions and architectures.

The code also makes some assumptions about the Python version you have installed (grep for "PyRuntime" in the .py files). I've been using:

python-devel-2.7.1-5.fc15.x86_64
python-debug-2.7.1-5.fc15.x86_64
python3-debug-3.2-0.9.rc1.fc15.x86_64
python3-devel-3.2-0.9.rc1.fc15.x86_64

but you may have to hack up the PyRuntime() invocations in the code to get it to build on other machines. Ultimately I want the plugin to be buildable against multiple python versions, so there could be a python27.so, python27-debug.so, python-32mu.so, python-32-dmu.so, etc (c.f. PEP-3149)

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 (http://sphinx.pocoo.org/), 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 <dmalcolm@redhat.com>

Something went wrong with that request. Please try again.