c++pretty-printautoformatting

Auto formatting a 100mb+ C++ file


I have a 100mb auto generated C++ file with broken formatting that I want to autoformat (indentation primarily).

All editors I have tried either crash or do not allow autoformatting more than a few megabytes of text.

By auto formatting I mean indentation.

Beyond writing some custom code to do this how can I auto format 100mb C/c++ files?

I have tried: VSCode Visual Studio Notepad++

Why do I need this?

We need to search the code for meta data (it is decompiled from assembly) and it makes it a lot easier to read if its indented properly.

We don't know ahead of time which parts of the code we need.. until we need it. It needs to be a single file to make it simpler to search for names/functions without constantly searching 1000s of individual files (if we did it per function).

The original C code would be millions of lines long which is why its so large


Solution

  • My first three recommendations are text editors, but I don't know if they'll work for you: 1) JEdit has a CLangBeauty plugin for reformatting source. 2) I assume Netbeans has a similar plugin, and can handle large files.  3) Then, there is QtCreator, which is available in a Windows version.  If you let it install Qt, it will be a large install, but I think you can do a custom install and just get the IDE; but it is very good at large files and can reformat them. Just choose the Open Source Edition at the download.

    Here's a page with a list of text editors than can open files gigabytes in size: "How to open Gigabyte-sized text files on Windows".  I've used glogg to view huge text files successfully; although it's features are minimalistic.

    In order to reformat "C" and "C++" code, there is a handy utility "UniversalIndentGUI" that can call a number of code beautifiers/formatters.  It was lasted updated in 2012, though.  One of the command-line tools it can call is "Uncrustify".

    Uncrustify is a command-line utility for reformatting code, and should be more memory efficient than the editor-based methods, as will be command-line code formatters in general. It is probably the most powerful at customizing the format it puts things in than the others.  It has recently been updated as of this writing. clang-format will probably also do it too.  These are command line utilities, so it's best to run them from a command prompt, call them in a pipe, or from an external GUI like UniversalIndentGUI; at least on Windows.  Uncrustify has a LOT of configuration options you can make.  I was also able to contact the author, and get him to add a feature for code formatting that I felt to be important.  Uncrustify should do it if none of the others will do.