13

I'm looking for a tool that works on Windows to reformat some C++ code in my codebase. Essentially, I've got some code I wrote a while ago that I'd like to use, but it doesn't match the style I'm using in a more recent project.

What's the best way to reformat C++ code in a standard manner?

Billy3

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552

9 Answers9

10

GNU Indent should get you most of the way there

Ana Betts
  • 73,868
  • 16
  • 141
  • 209
  • Yes, I found that one on Google -- but does it work on Windows? – Billy ONeal Mar 17 '10 at 16:23
  • 1
    Yes. See http://gnuwin32.sourceforge.net/packages/indent.htm – Matthew Flaschen Mar 17 '10 at 16:26
  • @Billy: it works as well on Windows as it does anywhere else. Regardless of platform, unless you want one of the "canned" formats, getting usable output can be challenging (to the point that unless you're dealing with a *lot* of code, formatting by hand is often faster and easier). – Jerry Coffin Mar 17 '10 at 16:33
  • @Herry Coffin: I was asking because the site Paul Betts linked to contained only UNIX source code, and no description as to how to get it running on Windows. – Billy ONeal Mar 17 '10 at 17:27
  • GNU indent does NOT support C++. Section 1.12 of the manual says "While an attempt was made to get indent working for C++, it will not do a good job on any C++ source except the very simplest." – user8908459 Mar 02 '23 at 05:52
7

In Visual Studio: Edit / Advanced / Format Document

The format applied to the document will match the settings in: Tools / Options / Text Editor / C/C++

Visual Studio might not support all the formatting options you want applied to your document, in which case you'll need a separate tool (such as Paul Betts is suggesting) to format the way you want.

Braiam
  • 1
  • 11
  • 47
  • 78
Task
  • 3,668
  • 1
  • 21
  • 32
7

Astyle is one of the more popular tools.

Edit: I don't know why I didn't think about it when I first posted this, but you can also use Vim. The = command will reformat your code according to your indentexpr and cinoptions.

greyfade
  • 24,948
  • 7
  • 64
  • 80
  • We used this a year or two ago to format our entire code base (GNU->ANSI indentation), and it worked flawlessly. – Ben Mar 17 '10 at 18:42
4

I vote for Uncrustify.

Otherwise: Best C++ Code Formatter/Beautifier

Community
  • 1
  • 1
Stefan Dragnev
  • 14,143
  • 6
  • 48
  • 52
3

GC Great Code has been an old standby for me. It's quite configurable.

http://sourceforge.net/projects/gcgreatcode/

Mr Fooz
  • 109,094
  • 6
  • 73
  • 101
2

Visual Studio can. And most IDEs can.

Andrey
  • 59,039
  • 12
  • 119
  • 163
  • Care to explain how? I did spend some time with Visual Studio before asking the question... – Billy ONeal Mar 17 '10 at 16:21
  • -1: Without 3rd party add-ins, Visual Studio cannot. – Sam Harwell Mar 17 '10 at 16:22
  • 2
    @280Z28 let me prove it. In VS: select the whole document, then Edit | Advanced | Format Selection. If you don't know something it doesn't mean that it doesn't exist. – Andrey Mar 17 '10 at 16:25
  • 1
    Actually, it can, to some degree, assist you with formatting of your code. You can run "Format Selection" on a part of or your whole document, and it will re-indent your code. – Dave Van den Eynde Mar 17 '10 at 16:27
  • @Andrey: It will reindent if I do that but it won't change the way brackets are handled or other types of formatting. I'll throw you a +1 -- but this really doesn't do what I'm looking for. – Billy ONeal Mar 17 '10 at 17:29
  • @Andrey, you can't possibly believe that indenting a document is what the OP is asking about. – Sam Harwell Mar 18 '10 at 00:59
2

I have also used AStyle in the past. I used this GUI for it to make it a bit easier (I think--it's been a while): Artistic Style for Windows: http://jimp03.zxq.net/

Nathan McDaniel
  • 971
  • 1
  • 8
  • 7
2

Eclipse/CDT can indent your code very well...

  • Its configurable.
  • You can also share the settings within your team/group/organization.

P.S. I am writing something about that in my Book Post comment if you have some direct questions. :-)

Meera
  • 107
  • 3
0

use AStyle ( inbuilt in codeblocks )

Step 1 : Select your code
Step 2 : Click on Plugins->Source Code Format (AStyle )

Hope it helps.

Abhishek Goel
  • 18,785
  • 11
  • 87
  • 65