Not ready to embrace the AI but hate writing Doxygen commented function bodies to your company coding standard? Just give the AI a kind of sideways hug and let it generate some Python code that takes a list of C function declarations and generates the function bodies. Here is the prompt I used.
Can you write some python code that will, given a list of C function declarations
in a file named "input.txt", generate corresponding function bodies, outputting
them to a file called "output.txt". I'd like Doxygen function comments, listing
the input parameters and return value (details to be filled in by the developer).
Here are a few example input function declarations:
static uint8_t MyFunc1(TS_SETUP_STRUCT *pSetup);
void MyFunc2(uint8_t param1, int32_t param2);
static TS_CMD_MAP* GetMapEntry(TS_CMD_MAP *pMap, uint8_t idx);
Here is an example desired output:
/**
* @brief -
* @param *pSetup -
* @return void
*/
static uint8_t MyFunc1(TS_SETUP_STRUCT *pSetup)
{
}/*END FUNCTION MyFunc1()
And here is what ChatGPT gave me with just a small manual tweak to put a dash after non-void return types.
import re
def parse_function_signature(signature):
"""Parses a C function signature and extracts return type, name, and parameters."""
match = re.match(r'(static\s+)?([\w\*]+)\s+(\w+)\s*\((.*?)\)\s*;', signature)
if not match:
return None
static, return_type, func_name, params = match.groups()
param_list = []
if params.strip():
param_list = [param.strip() for param in params.split(',')]
return {
"static": static if static else "",
"return_type": return_type,
"func_name": func_name,
"params": param_list
}
def generate_doxygen_comment(func_info):
"""Generates a Doxygen comment for the function."""
comment = "/**\n * @brief - \n"
for param in func_info["params"]:
param_name = param.split()[-1] # Extract parameter name
comment += f" * @param {param_name} - \n"
if func_info['return_type'] == "void":
comment += f" * @return {func_info['return_type']} \n */"
else:
comment += f" * @return {func_info['return_type']} - \n */"
return comment
def generate_function_body(func_info):
"""Generates the function body with Doxygen comments."""
comment = generate_doxygen_comment(func_info)
static_kw = func_info["static"] + " " if func_info["static"] else ""
params_str = ", ".join(func_info["params"])
function_body = f"""
{comment}
{static_kw}{func_info['return_type']} {func_info['func_name']}({params_str})
{{
}}/*END FUNCTION {func_info['func_name']}()*/
"""
return function_body.strip()
def process_file(input_file, output_file):
"""Reads input file, processes function signatures, and writes function bodies to output file."""
with open(input_file, "r") as infile, open(output_file, "w") as outfile:
for line in infile:
func_info = parse_function_signature(line.strip())
if func_info:
function_body = generate_function_body(func_info)
outfile.write(function_body + "\n\n")
if __name__ == "__main__":
process_file("input.txt", "output.txt")
And here's the output you get when running the script against the examples given in the prompt.
/**
* @brief -
* @param *pSetup -
* @return uint8_t -
*/
static uint8_t MyFunc1(TS_SETUP_STRUCT *pSetup)
{
}/*END FUNCTION MyFunc1()*/
/**
* @brief -
* @param param1 -
* @param param2 -
* @return void
*/
void MyFunc2(uint8_t param1, int32_t param2)
{
}/*END FUNCTION MyFunc2()*/
/**
* @brief -
* @param *pMap -
* @param idx -
* @return TS_CMD_MAP* -
*/
static TS_CMD_MAP* GetMapEntry(TS_CMD_MAP *pMap, uint8_t idx)
{
}/*END FUNCTION GetMapEntry()*/
So there you have it. Let the AI make you faster until it makes all of us irrelevant.