Best C++ Code Formatter/Beautifier

Kristopher Johnson picture Kristopher Johnson · May 8, 2009 · Viewed 256.5k times · Source

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

Answer

John Weldon picture John Weldon · May 8, 2009

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.