tools/amd_build/pyHIPIFY/hipify-python.py
def disable_function(input_string, function, replace_style):
    """ Finds and disables a function in a particular file.

    If type(function) == List
        function - The signature of the function to disable.
            e.g. ["bool", "overlappingIndices", "(const Tensor& t)"]
            disables function -> "bool overlappingIndices(const Tensor& t)"

    If type(function) == String
        function - Disables the function by name only.
            e.g. "overlappingIndices"

    replace_style - The style to use when stubbing functions.
    """
# void (*)(hcrngStateMtgp32 *, int, float *, double, double)
    info = {
        "function_start": -1,
        "function_end": -1,
        "bracket_count": 0
    }

    STARTED = 0
    INSIDE_FUNCTION = 1
    BRACKET_COMPLETE = 2

    STATE = STARTED

    if type(function) == list:
        # Extract components from function signature.
        func_info = {
            "return_type": function[0].strip(),
            "function_name": function[1].strip(),
            "function_args": function[2].strip()
        }

        # Create function string to search for
        function_string = "{0}{1}{2}".format(
            func_info["return_type"],
            func_info["function_name"],
            func_info["function_args"]
        )

        # Find the starting position for the function
        info["function_start"] = input_string.find(function_string)
    else:
        # Automatically detect signature.
        the_match = re.search(r"(((.*) (\*)?)({0})(\([^{{)]*\)))\s*{{".format(
            function.replace("(", "\(").replace(")", "\)")), input_string)
        if the_match is None:
            return input_string

        func_info = {
            "return_type": the_match.group(2).strip(),
            "function_name": the_match.group(5).strip(),
            "function_args": the_match.group(6).strip(),
        }

        # Find the starting position for the function
        info["function_start"] = the_match.start()
        function_string = the_match.group(1)

    # The function can't be found anymore.
    if info["function_start"] == -1:
        return input_string

    # Find function block start.
    pos = info["function_start"] + len(function_string) - 1
    while pos < len(input_string) and STATE != BRACKET_COMPLETE:
        if input_string[pos] == "{":
            if STATE != INSIDE_FUNCTION:
                STATE = INSIDE_FUNCTION
                info["bracket_count"] = 1
            else:
                info["bracket_count"] += 1
        elif input_string[pos] == "}":
            info["bracket_count"] -= 1

            if info["bracket_count"] == 0 and STATE == INSIDE_FUNCTION:
                STATE = BRACKET_COMPLETE
                info["function_end"] = pos

        pos += 1

    # Never found the function end. Corrupted file!
    if STATE != BRACKET_COMPLETE:
        return input_string

    # Preprocess the source by removing the function.
    function_body = input_string[info["function_start"]:info["function_end"] + 1]

    # Remove the entire function body
    if replace_style == disablefuncmode.REMOVE:
        output_string = input_string.replace(function_body, "")

    # Stub the function based off its return type.
    elif replace_style == disablefuncmode.STUB:
        # void return type
        if func_info["return_type"] == "void" or func_info["return_type"] == "static void":
            stub = "{0}{{\n}}".format(function_string)
        # pointer return type
        elif "*" in func_info["return_type"]:
            stub = "{0}{{\nreturn {1};\n}}".format(function_string, "NULL")  # nullptr
        else:
            stub = "{0}{{\n{1} stub_var;\nreturn stub_var;\n}}".format(function_string, func_info["return_type"])

        output_string = input_string.replace(function_body, stub)

    # Add HIP Preprocessors.
    elif replace_style == disablefuncmode.HCC_MACRO:
        output_string = input_string.replace(
            function_body,
            "#if !defined(__HIP_PLATFORM_HCC__)\n{0}\n#endif".format(function_body))

    # Add HIP Preprocessors.
    elif replace_style == disablefuncmode.DEVICE_MACRO:
        output_string = input_string.replace(
            function_body,
            "#if !defined(__HIP_DEVICE_COMPILE__)\n{0}\n#endif".format(function_body))

    # Throw an exception at runtime.
    elif replace_style == disablefuncmode.EXCEPTION:
        stub = "{0}{{\n{1};\n}}".format(
            function_string,
            'throw std::runtime_error("The function {0} is not implemented.")'.format(
                function_string.replace("\n", " ")))
        output_string = input_string.replace(function_body, stub)

    elif replace_style == disablefuncmode.ASSERT:
        stub = "{0}{{\n{1};\n}}".format(
            function_string,
            'assert(0)')
        output_string = input_string.replace(function_body, stub)

    elif replace_style == disablefuncmode.EMPTYBODY:
        stub = "{0}{{\n;\n}}".format(function_string)
        output_string = input_string.replace(function_body, stub)
    return output_string
 Finds and disables a function in a particular file.

    If type(function) == List
        function - The signature of the function to disable.
            e.g. ["bool", "overlappingIndices", "(const Tensor& t)"]
            disables function -> "bool overlappingIndices(const Tensor& t)"

    If type(function) == String
        function - Disables the function by name only.
            e.g. "overlappingIndices"

    replace_style - The style to use when stubbing functions.
    
Finds and disables a function in a particular file.
If type(function) == List
function - The signature of the function to disable.

If type(function) == String
function - Disables the function by name only.

The style to use when stubbing functions.
Finds and disables a function in a particular file . If type ( function ) == List function - The signature of the function to disable . If type ( function ) == String function - Disables the function by name only . The style to use when stubbing functions .
def disable_function(input_string, function, replace_style):
    info = {
        "function_start": -1,
        "function_end": -1,
        "bracket_count": 0
    }
    STARTED = 0
    INSIDE_FUNCTION = 1
    BRACKET_COMPLETE = 2
    STATE = STARTED
    if type(function) == list:
        func_info = {
            "return_type": function[0].strip(),
            "function_name": function[1].strip(),
            "function_args": function[2].strip()
        }
        function_string = "{0}{1}{2}".format(
            func_info["return_type"],
            func_info["function_name"],
            func_info["function_args"]
        )
        info["function_start"] = input_string.find(function_string)
    else:
        the_match = re.search(r"(((.*) (\*)?)({0})(\([^{{)]*\)))\s*{{".format(
            function.replace("(", "\(").replace(")", "\)")), input_string)
        if the_match is None:
            return input_string
        func_info = {
            "return_type": the_match.group(2).strip(),
            "function_name": the_match.group(5).strip(),
            "function_args": the_match.group(6).strip(),
        }
        info["function_start"] = the_match.start()
        function_string = the_match.group(1)
    if info["function_start"] == -1:
        return input_string
    pos = info["function_start"] + len(function_string) - 1
    while pos < len(input_string) and STATE != BRACKET_COMPLETE:
        if input_string[pos] == "{":
            if STATE != INSIDE_FUNCTION:
                STATE = INSIDE_FUNCTION
                info["bracket_count"] = 1
            else:
                info["bracket_count"] += 1
        elif input_string[pos] == "}":
            info["bracket_count"] -= 1
            if info["bracket_count"] == 0 and STATE == INSIDE_FUNCTION:
                STATE = BRACKET_COMPLETE
                info["function_end"] = pos
        pos += 1
    if STATE != BRACKET_COMPLETE:
        return input_string
    function_body = input_string[info["function_start"]:info["function_end"] + 1]
    if replace_style == disablefuncmode.REMOVE:
        output_string = input_string.replace(function_body, "")
    elif replace_style == disablefuncmode.STUB:
        if func_info["return_type"] == "void" or func_info["return_type"] == "static void":
            stub = "{0}{{\n}}".format(function_string)
        elif "*" in func_info["return_type"]:
            stub = "{0}{{\nreturn {1};\n}}".format(function_string, "NULL")  
        else:
            stub = "{0}{{\n{1} stub_var;\nreturn stub_var;\n}}".format(function_string, func_info["return_type"])
        output_string = input_string.replace(function_body, stub)
    elif replace_style == disablefuncmode.HCC_MACRO:
        output_string = input_string.replace(
            function_body,
            "#if !defined(__HIP_PLATFORM_HCC__)\n{0}\n#endif".format(function_body))
    elif replace_style == disablefuncmode.DEVICE_MACRO:
        output_string = input_string.replace(
            function_body,
            "#if !defined(__HIP_DEVICE_COMPILE__)\n{0}\n#endif".format(function_body))
    elif replace_style == disablefuncmode.EXCEPTION:
        stub = "{0}{{\n{1};\n}}".format(
            function_string,
            'throw std::runtime_error("The function {0} is not implemented.")'.format(
                function_string.replace("\n", " ")))
        output_string = input_string.replace(function_body, stub)
    elif replace_style == disablefuncmode.ASSERT:
        stub = "{0}{{\n{1};\n}}".format(
            function_string,
            'assert(0)')
        output_string = input_string.replace(function_body, stub)
    elif replace_style == disablefuncmode.EMPTYBODY:
        stub = "{0}{{\n;\n}}".format(function_string)
        output_string = input_string.replace(function_body, stub)
    return output_string
def disable_function ( input_string , function , replace_style ) : info = { "function_start" : - 1 , "function_end" : - 1 , "bracket_count" : 0 } STARTED = 0 INSIDE_FUNCTION = 1 BRACKET_COMPLETE = 2 STATE = STARTED if type ( function ) == list : func_info = { "return_type" : function [ 0 ] . strip ( ) , "function_name" : function [ 1 ] . strip ( ) , "function_args" : function [ 2 ] . strip ( ) } function_string = "{0}{1}{2}" . format ( func_info [ "return_type" ] , func_info [ "function_name" ] , func_info [ "function_args" ] ) info [ "function_start" ] = input_string . find ( function_string ) else : the_match = re . search ( r"(((.*) (\*)?)({0})(\([^{{)]*\)))\s*{{" . format ( function . replace ( "(" , "\(" ) . replace ( ")" , "\)" ) ) , input_string ) if the_match is None : return input_string func_info = { "return_type" : the_match . group ( 2 ) . strip ( ) , "function_name" : the_match . group ( 5 ) . strip ( ) , "function_args" : the_match . group ( 6 ) . strip ( ) , } info [ "function_start" ] = the_match . start ( ) function_string = the_match . group ( 1 ) if info [ "function_start" ] == - 1 : return input_string pos = info [ "function_start" ] + len ( function_string ) - 1 while pos < len ( input_string ) and STATE != BRACKET_COMPLETE : if input_string [ pos ] == "{" : if STATE != INSIDE_FUNCTION : STATE = INSIDE_FUNCTION info [ "bracket_count" ] = 1 else : info [ "bracket_count" ] += 1 elif input_string [ pos ] == "}" : info [ "bracket_count" ] -= 1 if info [ "bracket_count" ] == 0 and STATE == INSIDE_FUNCTION : STATE = BRACKET_COMPLETE info [ "function_end" ] = pos pos += 1 if STATE != BRACKET_COMPLETE : return input_string function_body = input_string [ info [ "function_start" ] : info [ "function_end" ] + 1 ] if replace_style == disablefuncmode . REMOVE : output_string = input_string . replace ( function_body , "" ) elif replace_style == disablefuncmode . STUB : if func_info [ "return_type" ] == "void" or func_info [ "return_type" ] == "static void" : stub = "{0}{{\n}}" . format ( function_string ) elif "*" in func_info [ "return_type" ] : stub = "{0}{{\nreturn {1};\n}}" . format ( function_string , "NULL" ) else : stub = "{0}{{\n{1} stub_var;\nreturn stub_var;\n}}" . format ( function_string , func_info [ "return_type" ] ) output_string = input_string . replace ( function_body , stub ) elif replace_style == disablefuncmode . HCC_MACRO : output_string = input_string . replace ( function_body , "#if !defined(__HIP_PLATFORM_HCC__)\n{0}\n#endif" . format ( function_body ) ) elif replace_style == disablefuncmode . DEVICE_MACRO : output_string = input_string . replace ( function_body , "#if !defined(__HIP_DEVICE_COMPILE__)\n{0}\n#endif" . format ( function_body ) ) elif replace_style == disablefuncmode . EXCEPTION : stub = "{0}{{\n{1};\n}}" . format ( function_string , 'throw std::runtime_error("The function {0} is not implemented.")' . format ( function_string . replace ( "\n" , " " ) ) ) output_string = input_string . replace ( function_body , stub ) elif replace_style == disablefuncmode . ASSERT : stub = "{0}{{\n{1};\n}}" . format ( function_string , 'assert(0)' ) output_string = input_string . replace ( function_body , stub ) elif replace_style == disablefuncmode . EMPTYBODY : stub = "{0}{{\n;\n}}" . format ( function_string ) output_string = input_string . replace ( function_body , stub ) return output_string
Finds and disables a function in a particular file.
Finds and disables a function in a particular file .
""" Finds and disables a function in a particular file.

    If type(function) == List
        function - The signature of the function to disable.
            e.g. ["bool", "overlappingIndices", "(const Tensor& t)"]
            disables function -> "bool overlappingIndices(const Tensor& t)"

    If type(function) == String
        function - Disables the function by name only.
            e.g. "overlappingIndices"

    replace_style - The style to use when stubbing functions.
    """ # void (*)(hcrngStateMtgp32 *, int, float *, double, double) # Extract components from function signature. # Create function string to search for # Find the starting position for the function # Automatically detect signature. # Find the starting position for the function # The function can't be found anymore. # Find function block start. # Never found the function end. Corrupted file! # Preprocess the source by removing the function. # Remove the entire function body # Stub the function based off its return type. # void return type # pointer return type # nullptr # Add HIP Preprocessors. # Add HIP Preprocessors. # Throw an exception at runtime.
input_string
function
replace_style