rellic icon indicating copy to clipboard operation
rellic copied to clipboard

Eliminate redundant casts when dealing with pointers

Open frabert opened this issue 4 years ago • 3 comments

The following code

struct foo {
  int x;
};

int bar(struct foo *a) {
  return a->x;
}

int main(void) {
  struct foo *x = 0;
  bar(0);
  if(x) {
    bar(x);
  }
}

roundtrips to

struct struct_foo {
    unsigned int x;
};
unsigned int bar(struct struct_foo *a);
unsigned int main();
unsigned int bar(struct struct_foo *a) {
    struct struct_foo *a_var0;
    a_var0 = a;
    return a_var0->x;
}
unsigned int main() {
    struct struct_foo *x;
    unsigned int var1;
    unsigned int val2;
    unsigned int val3;
    var1 = 0U;
    x = (void *)0U;
    val2 = bar((void *)0U);
    if (x != (struct struct_foo *)(void *)0U) {
        val3 = bar(x);
    }
    return var1;
}

which contains a lot of unneeded casts to and from void*, especially when dealing with NULLs. It would be better if Rellic could detect when these casts were not needed and delete them.

frabert avatar Oct 19 '21 13:10 frabert

One thing that comes to mind is that we could feasibly treat all code as c++, but in an extern "C" block. Then character constants such as 'a' would be char and not ints, and we'd benefit from implicit upcasts, and we'd also have access to bool types and nullptr. Down the line, I think we'd eventually see ourselves taking on some mixed bag of objective-c++ ast nodes, so maybe diving in now makes sense.

pgoodman avatar Oct 19 '21 16:10 pgoodman

I would strongly advise against going the C++ or Obj-C route since that exposes us to dealing with a lot more of C++ semantics and clang's AST. The benefits are not worth the risk of feature creep IMO.

surovic avatar Oct 19 '21 17:10 surovic

We want to deal with those features eventually!! Like, if we had a call to obj_msgSend then we want to lift it to the proper AST node type.

pgoodman avatar Oct 19 '21 18:10 pgoodman