views:

5318

answers:

5

There are lots of source-code formatting tools out there. Which ones work best for C++?

I'm interested in command-line tools or other things that can be automatically run when checking code in/out, preferably without needing to launch an editor or IDE.

(If you see the one you like already listed as an answer, vote it up. If it's not there, add it. Thanks.)

+22  A: 

AStyle, hands down.

It can be customized in great detail for C++ and Java (and others too)

Edit: This is a source code formatting tool... are you looking for a way to make it look good on screen, or to format the code?

John Weldon
Nice. I hadn't heard of this tool until now. Works great!
Runcible
neat tool. added to favorites collection.
Johannes Schaub - litb
I'm interested in formatting the code, preferably without needing to run an editor. AStyle is the kind of thing I'm looking for.
Kristopher Johnson
I've used it too - it's pretty good
We have some messy source files full of complicated macros, making it hard to understand how to modify the code or set breakpoints. We de-macroed it with gcc -E, resulting in very long lines of nested for loops, if statements etc. Astyle fixed that to be readable, saving us a lot of trouble. Astyle has a permanent place on my software toolshelf!
DarenW
A: 

gvim with good settings... I can send u mine if you want...

A: 

I write beautiful code to begin with - I've never had use for such a tool.

(J/K of course, but not so far from the truth.)

Mark Ransom
There's a lot to be said for it, though. I used one style guide that expressed certain things in terms of astyle options, but didn't actually run astyle, so that if there was a reason to break the rules you could. Mostly-consistent formatting is *much* easier to work with than everyone doing their own thing according to taste and the weather, and only slightly harder to work with than precisely regularly formatted code.
Steve Jessop
+11  A: 

Look at the project UniversalIndentGUI. You can experiment with several indenters using it: AStyle, Uncrustify, GreatCode, ... and select the best for you. Any of them can be run later from a command line.

Konstantin
+4  A: 

Uncrustify has a lot of configurable options. You'll probably need Universal Indent GUI (in Konstantin's reply) as well to configure it.

Indi
The git commands to get source seem to be broken as of 2010-aug-10.
DarenW