Just some random thoughts about automated conversion between C and C++ code.

Say, I have the following C code

typedef struct {
    int a;
    int b;
} example_t;

void example_set_a(example_t* example, int val) { example->a = val; }
void example_set_b(example_t* example, int val) { example->b = val; }
int  example_get_sum(example_t* example)        { return example->a + example->b};

The C++ equivalent would be

class cExample {
private:
	int a;
	int b;
public:
	void setA(int val);
	void setB(int val);
	int getSum();	
};

void cExample::setA(int val) { a = val; }
void cExample::setB(int val) { b = val; }
int  cExample::getSum()      { return a + b; }
  • Convert naming convention from snake_case to camelCase. Remote the _t suffix, add the c prefix.
  • Place all members of the struct in the private part of the class.
  • Take all functions that take a pointer to the struct, and place them in the pubic part of the class (removing the pointer to the struct)
    • This process feels rather mechanical, and it should be possible to automatically do the said conversion. The question is how?
      Are there any existing tools for such tasks? This task doesn’t seem very unique to me, so I can imagine such tool to exist.
      So far, I have found some website with an AI powered tool. While it performs the task, I would rather not use AI. I wish
      for a tool that runs on my local machine.

      If no such tool exists, how should I go around creating one. I need to parse the C code, up to the point where I can
      identify the function signature, find where it begins and ends, find where the pointer to the struct is used and remove that part.

      All things that should exist already. Think how syntax highlighting and refactoring tools in an IDE work. The parts to do
      such tasks do exist. But what are these parts? Should I be looking into yacc or is that totally the wrong track?