User: Password:
Subscribe / Log in / New account

An introduction to creating GCC plugins: also GCC MELT or GCC Python

An introduction to creating GCC plugins: also GCC MELT or GCC Python

Posted Sep 29, 2011 20:19 UTC (Thu) by dave_malcolm (subscriber, #15013)
In reply to: An introduction to creating GCC plugins: also GCC MELT or GCC Python by bstarynk
Parent article: An introduction to creating GCC plugins

Thanks for mentioning the gcc Python plugin.

I'm the author of the Python plugin, so naturally I'm fond of it.

The lack of pattern matching within Python's syntax hasn't been a major issue to me in practice. Also, I like to think that it's become significantly more mature than when I first announced it :)

I suspect that MELT will lead to faster-executing results, given that CPython isn't the fastest runtime in the world - though speed hasn't been an issue for me yet, either.

Inspired by this LWN article, I had a go at implementing a spell-checking GCC pass in Python, using my plugin.

You can see the results at:

I believe my version is considerably simpler than doing it in C (about 35 lines of Python code, including comments), and, I hope, easier to read.

The Python implementation of the spell checker is slightly different to the one in the article, in that I'm using gcc.Gimple.walk_tree() on each statement to (recursively) visit all tree nodes it references. I'm also using gcc.warning() and gcc.inform() to integrate with GCC's warnings subsystem, so that the error messages are in GCC's normal format (so that e.g. when I click on them in emacs it takes me to the source line containing the error).

One of the nice things about using Python for this is all of the 3rd-party libraries that are available. In the Python implementation I'm using the "enchant" module that came out of AbiWord's spellchecker, and this makes it easy to offer spelling suggestions. There is a huge collection of Python libraries out there, so if you want to e.g. pause compilation part-way through and serve the internal-representation over HTTP, or dump it all into a database etc, there are lots of possibilities there.

Hope this is helpful

(Log in to post comments)

Copyright © 2017, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds