Author Topic: Automated conversion between C and C++  (Read 1205 times)

0 Members and 1 Guest are viewing this topic.

Offline GromBeestjeTopic starter

  • Frequent Contributor
  • **
  • Posts: 308
  • Country: nl
Automated conversion between C and C++
« on: March 05, 2025, 11:00:47 am »
I am thinking about automated conversion between C and C++ code

Say, I have the following C code
Code: [Select]
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
Code: [Select]
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; }

The desired conversion should consist of: list all the members of the struct, convert naming convention
from snake case to camel case, and put them into the private section of a class, class name converted
accordingly.

Take all the functions that take a pointer to the struct as first parameter, and put them in the public section
of the class. Inside the function, replace all the dereferences of the struct with nothing, such they refer to
the class instance.

I know of sites like https://www.codeconvert.ai/c-to-c++-converter but I don't want to use AI for something
that should be able to be done very mechanically, and I would like to run it locally.

Are there any existing tools that could accomplish this job?
If I were to create something myself, would yacc be worth investigating to parse the input?

Edit: forgot part of the example
« Last Edit: March 05, 2025, 11:02:20 am by GromBeestje »
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 8349
  • Country: fi
    • My home page and email address
Re: Automated conversion between C and C++
« Reply #1 on: March 05, 2025, 05:08:46 pm »
I am thinking about automated conversion between C and C++ code
Why?

No, seriously.  Most C code is compilable as C++ without any modifications.  The main difference between C and C++ is their approach to problem-solving, C++ being an object-oriented programming language.  While you can so simple conversions from structures to classes, anything more complicated requires careful inspection of the C code anyway, with a functional rewrite in C++ more preferable to a translation.

Consider a common example, where a structure describes key-value pairs, say
Code: [Select]
struct keyval {
   int  key;
   const char  *val;
};

int key_compare(const void *kv1, const void *kv2) {
   const int  key1 = ((const struct keyval *)kv1)->key;
   const int  key2 = ((const struct keyval *)kv2)->key;
   return (key1 < key2) ? -1 : (key1 > key2) ? +1 : 0;
}

int val_compare(const void *kv1, const void *kv2) {
   return strcoll(((const struct keyval *)kv1)->val, ((const struct keyval *)kv2)->val);
}
where an array a of n structures can be sorted using qsort(a, n, sizeof a[0], key_compare) or qsort(a, n, sizeof a[0], val_compare).

Now, consider what your translator will generate for this simple code, and compare to how you would implement the same functionality in C++.  If the two are not the same, or at least very very similar, then how useful is your translator?

I've found for myself that translations and library-code reuse is much less useful than being able and willing to rewrite and adapt the code, even across languages.  I often write test cases and implementation examples with all sorts of bells and whistles, so that when I write an initial adaptation, I don't need to work at multiple levels of complexity at once (both as the "user" of the interface, and as the "provider" of the interface); but later, when I have an actual use case, I rip out all the unneeded stuff, and what is left, can often be algorithmically optimized via simplification.  When a result isn't needed or useful in existing code, why compute it at all?

For that reason, I'm very suspicious of code translators and code generators.  I believe that the ability and willingness to rewrite existing code is absolutely necessary –– critical! –– in the long term for every software developer, and that those automated tools will only help avoid developing that skill, and degrade the long-term quality said developers produce.  There is enough crappy software in the world already, in my opinion; let's not add to that, and try to do better instead.
 
The following users thanked this post: cfbsoftware, DiTBho

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17773
  • Country: fr
Re: Automated conversion between C and C++
« Reply #2 on: March 05, 2025, 05:40:56 pm »
Translation is a lost cause.
First you'd need to write a fully-compliant, ideally at least C11 to be reasonable, C analyzer. Then find "patterns" in the AST that your translator would identify and translate into idiomatic C++ (which is what GromBeestje seems to talk about).
All this without error.
And you'd still end up with sub-par C++ compared to carefully designed C++.
 
The following users thanked this post: cfbsoftware, DiTBho

Offline GromBeestjeTopic starter

  • Frequent Contributor
  • **
  • Posts: 308
  • Country: nl
Re: Automated conversion between C and C++
« Reply #3 on: March 06, 2025, 07:45:06 pm »
I'm not aiming for the fancy C++ stuff. I am looking for fairly simple stuff, as I described. Just copy-paste the code inside the functions, replacing the pointer.
Not to get distracted by the details, but, my goal is embedded use, like, a driver to some sensor. Replacing get_temperature(some_sensor_t*)  with SomeSensor::getTemperature(). stuff like that.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf