Code beautifying

Aug 21, 2015 at 8:47am
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;.
Aug 21, 2015 at 8:48am
You could use awk or sed or perl. :)
Aug 21, 2015 at 1:51pm
Aug 21, 2015 at 2:18pm
I already know about AStyle. I don't think it removes tokens.
Aug 22, 2015 at 4:04am
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.
Aug 22, 2015 at 9:46am
you could write a preprocessor for it to do all of those things (seems like a simple perl script actually)
Aug 22, 2015 at 10:34am
nchambers: I think you're underestimating the complexity of the problem.
Aug 22, 2015 at 1:12pm
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.
Aug 22, 2015 at 2:01pm
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
Aug 22, 2015 at 2:10pm
my mistake... I was assuming he wanted to remove redundant parens from just math expressions, and not the entire source
Aug 22, 2015 at 5:00pm
A feasible solution easier than CPPCOMPILER-hard would use libclang to inspect the AST.
Aug 24, 2015 at 3:39pm
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.
Aug 24, 2015 at 4:22pm
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.
Aug 24, 2015 at 5:19pm
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?
Aug 24, 2015 at 5:44pm
I don't want to write code to do it. If someone else has already done it, on the other hand...
Aug 25, 2015 at 2:48pm
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?
Aug 25, 2015 at 4:49pm
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.
Aug 26, 2015 at 12:20am
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.