Using clang to reformat code.


Example input source code:
int main() {
  int a,b,c;
  a = 1;
  b = a + 3;
  c = b * 4;
  printf("Result = %d\n", c);

Desired output source code:
int main() {
  int a;
  a = (1 + 3) * 4;
  printf("Result = %d\n", a);

I.e. Is there a way to automatically take multi-line basic
calculations and turn them into a single line of more complex
I know these sorts of optimizations are done during compiling, but I
would like the actual source code to be simplified and made easier to

Example use. The "input source code" is some auto-generated code. I
wish to process that code to make it easier to read, by producing the
desired "output source code".

Kind Regards


I don’t know of any such tool that does this sort of thing already, though it is theoretically possible (refer to the Clang Tooling infrastructure, and possibly the AST Matchers). Getting the right balance of readability improvements in such a tool wouldn’t necessarily be trivial though (just making larger expressions isn’t always the best idea, of course - named variables can often improve readability)