Lua plugin for the GNU Compiler Collection https://colberg.org/gcc-lua
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Peter Colberg eb535dea8c Support GCC 8 7 months ago
..
CHANGES.html Update release notes 3 years ago
CHANGES.mdwn Update release notes 3 years ago
INSTALL.html Update release notes 3 years ago
INSTALL.mdwn Update release notes 3 years ago
Makefile Add documentation in HTML format 4 years ago
README.html Update copyright notice 3 years ago
README.mdwn Add documentation in HTML format 4 years ago
contents.mdwn Update copyright notice 3 years ago
gcc-lua.png Migrate documentation to Pandoc and Markdown 5 years ago
gcc-lua.ps Migrate documentation to Pandoc and Markdown 5 years ago
index.html Support GCC 8 7 months ago
index.mdwn Support GCC 8 7 months ago
pandoc.css Update copyright notice 3 years ago
pandoc.html.in Add documentation in HTML format 4 years ago
reference.html Add mode() for types 3 years ago
reference.mdwn Add mode() for types 3 years ago

README.mdwn

---
title: User's Guide
---

Consider the following C source file `hello.c`:

~~~ {.c}
static const char hello[] = "Hello, World!";
~~~

We retrieve the string constant using a Lua script `hello.lua`:

~~~ {.lua}
local gcc = require("gcc")

-- send assembler output to /dev/null
gcc.set_asm_file_name(gcc.HOST_BIT_BUCKET)

-- invoke Lua function after translation unit has been parsed
gcc.register_callback(gcc.PLUGIN_FINISH_UNIT, function()
-- get global variables in reverse order of declaration
local vars = gcc.get_variables()
for i = #vars, 1, -1 do
-- initial value is a string constant
print(vars[i]:initial():value())
end
end)
~~~

The plugin is loaded into GCC by passing its path to `-fplugin`:

~~~
gcc -S hello.c -fplugin=./gcc/gcclua.so -fplugin-arg-gcclua-script=hello.lua
~~~

For detailed examples, please refer to the source code of the test suite.


Resources
---------

The [Plugins] section of the [GCC Internals] documentation explains writing,
building and loading plugins. The [GENERIC] section describes the format of
abstract syntax trees.

[Plugins]: http://gcc.gnu.org/onlinedocs/gccint/Plugins.html
[GCC Internals]: http://gcc.gnu.org/onlinedocs/gccint/
[GENERIC]: http://gcc.gnu.org/onlinedocs/gccint/GENERIC.html

Relevant GCC source files are `gcc/tree.h`, `gcc/tree.def` and
`gcc/tree-dump.c` for the GENERIC format, and `gcc/cgraph.h` for entry
points to global variable, function and type declarations.

The [GCC Wiki] links to documentation and existing plugins.

[GCC Wiki]: http://gcc.gnu.org/wiki/plugins

Boris Kolpackov has written a series on Parsing C++ with GCC plugins:
[[1]](http://www.codesynthesis.com/~boris/blog/2010/05/03/parsing-cxx-with-gcc-plugin-part-1/),
[[2]](http://www.codesynthesis.com/~boris/blog/2010/05/10/parsing-cxx-with-gcc-plugin-part-2/),
[[3]](http://www.codesynthesis.com/~boris/blog/2010/05/17/parsing-cxx-with-gcc-plugin-part-3/).