Working with C code

“Hello world”

Here’s a simple “hello world” C program:

Here’s a Python script that locates the function at one pass within the compile and prints various interesting things about it:

We can run the script during the compile like this:

./gcc-with-python script.py test.c

Here’s the expected output:

Notice how the call to printf has already been optimized into a call to __builtin_puts.

Spell-checking string constants within source code

This example add a spell-checker pass to GCC: all string constants are run through the “enchant” spelling-checker:

$ ./gcc-with-python tests/examples/spelling-checker/script.py input.c

The Python code for this is:

Given this sample C source file:

these warnings are emitted on stderr:

Finding global variables

This example adds a pass that warns about uses of global variables:

$ ./gcc-with-python \
    tests/examples/find-global-state/script.py \
    -c \
    tests/examples/find-global-state/input.c

The Python code for this is:

Given this sample C source file:

these warnings are emitted on stderr: