Eli Fulkerson .com HomeProjectsPretty-c++-formatter
 

make pretty - reformat and indent C++ code

Description:

This is a shell + perl script to reformat and re-indent c++ code.

I had been looking for a C++ reformatter that met my exact style needs, and wasn't having much luck. I found one that was close: bcpp - C(++) Beautifier, but it didn't quite match what I needed.

So... now I've wrapped bcpp in a shell script to make it fit my requirements more closely. The 'pretty' script takes any number of files as its argument, pipes them through bcpp and then through a seperate postprocessing script (reindent.pl) and then writes them out over the original filename.

The postprocessing script assumes that you are looking for this type of formatting:

if (x >0)
{
	stuff here;
	stuff here;
}
rather than...
if (x > 0) {
	stuff here;
	stuff here;
}

... or any other heathen notation, and isn't guaranteed not to make any other assumptions based on what my needs were at the time. In particular, you will find that it doesn't do anything with switch/case statements. Handling that would have been more work than the script was worth to me, I'm not a big fan of switch/case.

In cases where it was giving me output that was unexpected, I would fix the C++ to match what the formatting script expected and re-run it until the aberrations were corrected. However, if your requirements are similar to mine you will likely find that the script is already quite good.

Language:

  • Perl, Bash. Additionally uses bcpp, which is a seperate utility.
  • License:

  • The code for this program is available under the GPL.
  • Usage:

    You will first either want to compile the bcpp utility, or edit it out of the 'pretty' script altogether. 'pretty' expects to have a local copy of bcpp in its pretty_bin directory.
    ./pretty file1.cc file2.cc file3.cc file4.cc ...
    

    Example Output:

    Download:

    the whole thing - zipped
    reindent.pl - the perl script that does the re-indenting
    mirror of bcpp.tar.gz