Working with the preprocessor — gcc-python-plugin 0.16 documentation (original) (raw)

gcc-python-plugin

For languages that support a preprocessor, it’s possible to inject new “built-in” macros into the compilation from a Python script.

The motivation for this is to better support the creation of custom attributes, by creating preprocessor names that can be tested against.

gcc. define_macro(argument)

Defines a preprocessor macro with the given argument, which may be of use for code that needs to test for the presence of your script. The argument can either be a simple name, or a name with a definition:

gcc.define_macro("SOMETHING") # define as the empty string gcc.define_macro("SOMETHING=72")

This function can only be called from within specific event callbacks, since it manipulates the state of the preprocessor for a given source file.

For now, only call it in a handler for the event gcc.PLUGIN_ATTRIBUTES:

import gcc

def attribute_callback_for_claims_mutex(*args): print('attribute_callback_for_claims_mutex called: args: %s' % (args, ))

def attribute_callback_for_releases_mutex(*args): print('attribute_callback_for_releases_mutex called: args: %s' % (args, ))

def register_our_attributes(): gcc.register_attribute('claims_mutex', 1, 1, False, False, False, attribute_callback_for_claims_mutex) gcc.define_macro('WITH_ATTRIBUTE_CLAIMS_MUTEX')

gcc.register_attribute('releases_mutex',
                       1, 1,
                       False, False, False,
                       attribute_callback_for_releases_mutex)
gcc.define_macro('WITH_ATTRIBUTE_RELEASES_MUTEX')

Wire up our callback:

gcc.register_callback(gcc.PLUGIN_ATTRIBUTES, register_our_attributes)