Best C++ Code Formatter/Beautifier
Asked Answered
P

1

127

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.)

Pharmacognosy answered 8/5, 2009 at 18:6 Comment(0)
R
120

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

This is a source code formatting tool.


clang-format is a powerful command line tool bundled with the clang compiler which handles even the most obscure language constructs in a coherent way.

It can be integrated with Visual Studio, Emacs, Vim (and others) and can format just the selected lines (or with git/svn to format some diff).

It can be configured with a variety of options listed here.

When using config files (named .clang-format) styles can be per directory - the closest such file in parent directories shall be used for a particular file.

Styles can be inherited from a preset (say LLVM or Google) and can later override different options

It is used by Google and others and is production ready.


Also 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.


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

Ramification answered 8/5, 2009 at 18:6 Comment(5)
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!Sublime
Vim tip: if you want to beautify the code in a file you're current editing use:%!astyleMovable
This is a great tool. I run astyle each time I have to work on the same project with both MSVStudio on windows (at work) and emacs on linux (at home). I use the BSD style with emacs and astyle (which is close to the style I use with MSVC editor), and I always have properly indented source code. It even looks beautiful independently of the tab size !Inenarrable
Tried it. It's very easy to get a quick result (doc is good). It's a shame there are not so many options though! Some programmers are very creative with the way they pad their code with spaces, and AStyle does not handle all of that creativity.Len
There is no maximum line width option. This should be a must-have.Buyer

© 2022 - 2024 — McMap. All rights reserved.