Generating custom errors and warnings

gcc.warning(location, message, option=None)

Emits a compiler warning at the given gcc.Location, potentially controlled by a gcc.Option.

If no option is supplied (or None is supplied), then the warning is an unconditional one, always issued:

gcc.warning(func.start, 'this is an unconditional warning')
$ ./gcc-with-python script.py input.c
input.c:25:1: warning: this is an unconditional warning [enabled by default]

and will be an error if -Werror is supplied as a command-line argument to GCC:

$ ./gcc-with-python script.py -Werror input.c
input.c:25:1: error: this is an unconditional warning [-Werror]

It’s possible to associate the warning with a command-line option, so that it is controlled by that option.

For example, given this Python code:

gcc.warning(func.start, 'Incorrect formatting', gcc.Option('-Wformat'))

if the given warning is enabled, a warning will be printed to stderr:

$ ./gcc-with-python script.py input.c
input.c:25:1: warning: incorrect formatting [-Wformat]

If the given warning is being treated as an error (through the usage of -Werror), then an error will be printed:

$ ./gcc-with-python script.py -Werror input.c
input.c:25:1: error: incorrect formatting [-Werror=format]
cc1: all warnings being treated as errors
$ ./gcc-with-python script.py -Werror=format input.c
input.c:25:1: error: incorrect formatting [-Werror=format]
cc1: some warnings being treated as errors

If the given warning is disabled, the warning will not be printed:

$ ./gcc-with-python script.py -Wno-format input.c

Note

Due to the way GCC implements some options, it’s not always possible for the plugin to fully disable some warnings. See gcc.Option.is_enabled for more information.

The function returns a boolean, indicating whether or not anything was actually printed.

gcc.error(location, message)

Emits a compiler error at the given gcc.Location.

For example:

gcc.error(func.start, 'something bad was detected')

would lead to this error being printed to stderr:

$ ./gcc-with-python script.py input.c
input.c:25:1: error: something bad was detected
gcc.permerror(loc, str)

This is a wrapper around GCC’s permerror function.

Expects an instance of gcc.Location (not None) and a string

Emit a “permissive” error at that location, intended for things that really ought to be errors, but might be present in legacy code.

In theory it’s suppressable using “-fpermissive” at the GCC command line (which turns it into a warning), but this only seems to be legal for C++ source files.

Returns True if the warning was actually printed, False otherwise

gcc.inform(location, message)

This is a wrapper around GCC’s inform function.

Expects an instance of gcc.Location or gcc.RichLocation, (not None) and a string

Emit an informational message at that location.

For example:

gcc.inform(stmt.loc, 'this is where X was defined')

would lead to this informational message being printed to stderr:

$ ./gcc-with-python script.py input.c
input.c:23:3: note: this is where X was defined