Change prefix in macros

# move_macros

# Change prefix in macros

Description:

This transformation allows to modify the prefix of macros.

The motivation for this transformation is to help move whole libraries or source sets from one naming conventioin to another.

Please use this transformation as a boilerplate for your own customized version.

For example, the following file:

    #define BOOST_SOME_MACRO 1
    // ...
    #ifdef BOOST_SOME_MACRO
    // ...
    #endif

will be transformed into:

    #define BOOM_SOME_MACRO 1
    // ...
    #ifdef BOOM_SOME_MACRO
    // ...
    #endif

Note: This transformation actually does not check whether the given identifier is indeed a macro name and the prefix replacement is performed systematically on all identifiers that match.

Note: The transformation is performed in place, which means that the source files are modified.

Show source code for this transformation

Hide source code

    # change the prefix in all macros

    set oldPrefix "BOOST"
    set newPrefix "BOOM"

    set oldPrefixLength [string length $oldPrefix]

    foreach fileName [getSourceFileNames] {
        set tokens [getTokens $fileName 1 0 -1 -1 {}]
        set newFile [open $fileName "w"]
        foreach token $tokens {
            set tokenValue [lindex $token 0]
            set tokenType [lindex $token 3]
            if {$tokenType == "identifier" && \
                    [string compare -length $oldPrefixLength $oldPrefix $tokenValue] == 0} {
                set tokenValue [string replace $tokenValue 0 [expr $oldPrefixLength - 1] $newPrefix]
            }
            puts -nonewline $newFile $tokenValue
        }
        close $newFile
    }

Transformation index