Format your code with clang-format

Choose style: LLVM, GNU, Google, Chromium, Microsoft or WebKit.


Format your code with clang-format


We have already published an article that describes details of the code style in the article:

In this article, we are going to know a tool that formats our code according to the style we want, it is: clang-format.

ClangFormat(clang-format) is a toolkit that uses LibFormat that formats your code in a variety of ways, including a standalone tool and editor integrations.


Installation

To install clang-format, you just need the collection of tools from LLVM, here in this article there is the procedure downloading the binary and installing it, but you can also use your system’s package manager to get, examples:

# windows
winget install -e --id LLVM.LLVM

# macOS
brew install clang-format

# Debian, Ubuntu, Mint and derivatives
sudo apt install clang-format

# fedora
sudo dnf install clang-tools-extra

# Arch
sudo pacman -S clang-format-git # AUR

# Gentoo, Funtoo and others
emerge clang

Usage

Suppose you have this code C++: main.cpp:

#include <iostream>
#include <memory>

typedef struct Model {
   std::string hello = "Hello";
} model;

int main(){
   auto m = std::make_unique<model>();
   m->hello = "World";
   std::cout << m->hello << '\n';
}

It is formatted the way I like it, but if I wanted to change it to the GNU standard, I would just run the following command:

clang-format -style=gnu main.cpp

The output transforms my code in this style:

#include <iostream>
#include <memory>

typedef struct Model
{
   std::string hello = "Hello";
} model;

int
main()
{
   auto m = std::make_unique<model> ();
   m->hello = "World";
   std::cout << m->hello << '\n';
}

Very different formatting, right?!

If you want to save to another file:

clang-format -style=GNU main.cpp > output.cpp

There are styles available: llvm, google, chromium, microsoft, webkit and others.

Examples:

clang-format -style=llvm main.cpp
clang-format -style=google main.cpp
clang-format -style=microsoft main.cpp

You can also save your preferred style by dumping it into a file named: .clang-format in the path/directory you are working with. For that run:

Assuming you prefer the gnu style.

clang-format -style=gnu -dump-config > .clang-format

The file will be similar to the content below and note that you can change the settings as you wish:

---
Language: Cpp
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignArrayOfStructures: None
AlignConsecutiveAssignments:
   Enabled: false
   AcrossEmptyLines: false
   AcrossComments: false
   AlignCompound: false
   PadOperators: true
AlignConsecutiveBitFields:
   Enabled: false
   AcrossEmptyLines: false
   AcrossComments: false
   AlignCompound: false
   PadOperators: false
AlignConsecutiveDeclarations:
   Enabled: false
   AcrossEmptyLines: false
   AcrossComments: false
   AlignCompound: false
   PadOperators: false
AlignConsecutiveMacros:
   Enabled: false
   AcrossEmptyLines: false
   AcrossComments: false
   AlignCompound: false
   PadOperators: false
AlignEscapedNewlines: Right
...

The file is much bigger, the ... refers to that!

If you want to format multiple .cpp files use wildcard:

clang-format -i *.cpp

The -i parameter modifies the file.

And to modify multiple files including header files, use this command as an example:

find . -regex '.*\.\(cpp\|hpp\|cc\|c\|h\)' -exec clang-format -style=file -i {} \;

If you want to include it in your Code Editor/IDE look for the name corresponding documentation. In the case of Vim/Neovim use vim-clang-format.

For more information see clang-format --help and the official address of clang-format.


clang llvm cpp clanguage programming


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles