Code beautifying

Does anyone know of a tool that removes redundant tokens from C++ code?
E.g. change (*this). to this->, (1+2)+3 to 1+2+3, if (a) { b; } to if (a) b;.
You could use awk or sed or perl. :)
I already know about AStyle. I don't think it removes tokens.
Ah, well, I know it does if (a) { b; } to if (a) b;.

The first is a pretty easy awk/regex find and replace.

The one in the middle requires the parser to understand the expression -- and I don't think you're going to find anything to do that.
you could write a preprocessor for it to do all of those things (seems like a simple perl script actually)
nchambers: I think you're underestimating the complexity of the problem.
not the first or last. the middle maybe a bit, but I don't think it would be that hard to parse out a math expression and simplify/optimize the expression (ie (1+2)+3 -> 1+2+3 -> 6). I'm actually doing something similar in python at the moment.
Err, no. Middle is hard. That was only the example. Other exampes: 

list<int> data((istream_iterator<int>(dataFile)), istream_iterator<int>0); 
Second set of braces is not redundand, as removing it will change program meaning.
But there it is redundand: void print((ostream_iterator<int>(data, ", ")));

Braces around a && (b && c) might or might not change the meaning depending on operator&& overloads.

Is that set of braces redundand or not:
1
2
3
4
5
6
7
8
9
int func() 
{
    foo x;
    { //←——
        bar y;
        //...
    } //←——
    //...
}
+x might or might not change program behavior. Only realistic way to implement that is to check if removing particular token changes program AST. You would need IDE deeply integrated with compiler like Eclipse/Java
my mistake... I was assuming he wanted to remove redundant parens from just math expressions, and not the entire source
A feasible solution easier than CPPCOMPILER-hard would use libclang to inspect the AST.
The best solution I've found is to take a chill pill and not worry about it. The danger of removing a token (or 700) that changes the meaning of the program is far too high. Do you really want to have this conversation:

Boss: Helios!! What the hell happened to the code, it's broken
You: I ran a program to beautify it. Trust me, it's better now.
Boss: Better?!?!? Since when is broken "better"????
You: Umm...

If a particular style really offends you. change it with an editor by hand, after examining each case individually.
This is just to improve the output of a code generator. To make the generator simpler, braces and parentheses are added generously. This ensures correct programs, but makes eyeball inspection more difficult.
Obviously, manual simplification is pointless in generated code.
If manually fixing it is pointless, why is automatically fixing it not pointless? If you're willing to fix it in the first place, why not adjust the generator?
I don't want to write code to do it. If someone else has already done it, on the other hand...
But if it's automatically generated code then why care about the parens and braces? Why look at the generated code at all (except perhaps in a debugger)? Isn't this sort of like beautifying the assembly that the C++ compiler can generate?
Someone might be interested in learning what the generated code does, for some reason. Just because it's generated doesn't mean it has to look ugly.
closed account (E0p9LyTq)
Just me:

1. Use AStyle to format source code to a format that makes it easier for me to read.

2. Save the formatted source

3. Add to version control

4. Consider manually removing what I consider "extra" tokens.

I more often than not end up skipping steps 3 and 4, and just go grab an "adult" beverage.
Topic archived. No new replies allowed.