GenerateImplementations.refscript

This page describes the GenerateImplementations RefactorScript shipped with VisualGDB.

Overview

This script generates implementations for the selected methods or functions.
In order to run this script, first select one or more methods in the Code Explorer -> Outline view.
You can easily tweak it to return the default value (e.g. EINVAL or NULL) based on the return type, or to check arguments for NULL values.

Script


[Main, Description("Generate implementations")]
generator GenerateFunctionBodies(Array functions,
                                 bool generateNullChecks,
                                 string defaultReturnedValue = "")
{
    foreach(func in functions)
    {
>>$func.ReturnType.Literal 
        if (func.IsMethod)
>>$func.ContainingType.ShortName::
>>$func.ShortName(
        foreach(arg in func.Arguments)
        {
>>$arg.Type.Literal $arg.ShortName
            if (arg.HasInitializer)
>> /* = $arg.Initializer */
            if (!arg.IsLast)
>>, 
        }
>)
>{
    if (generateNullChecks)
        GenerateNullChecks(func, false);

    if ((func.ReturnType.Resolved.Name != "void") && (defaultReturnedValue != ""))
>    return $defaultReturnedValue;
>}
>
    }
}


generator GenerateNullChecks(Function func, bool withBraces = false)
{
    set returnType = func.ReturnType;
    foreach(arg in func.Arguments)
    {
        if (arg.Type.Resolved.IsPointerOrReference || arg.Type.Resolved.IsArray)
        {
>    if ($arg.ShortName == NULL)
            if (withBraces)
>    {
            set isError_t = returnType.Referenced.Name == "error_t";
            set isInt = returnType.Referenced.Name == "int";
            if (isError_t || isInt)
>        return EINVAL;
            else if (returnType.Referenced.Name == "void")
>        return;
            else
>        return -1;
            if (withBraces)
>    }
>
        }
    }
}

Sample Input

class SimpleClass
{
public:
    void UnimplementedMethod(int x, void *p);
    double UnimplementedMethod2(double x);
};

Sample Output

void SimpleClass::UnimplementedMethod(int x, void * p)
{
    if (p == NULL)
        return;

}

A detailed reference on RefactorScript syntax and data model is available here.
See this page for a full list of RefactorScripts shipped with VisualGDB.