Clang format and code beautifier for Qt Creator
Different developers use different styles with different IDE (i.e some people use space, some use tab) which may cause lots of changes in your git repository. One solution to this is to use some standard code formatting and beautifier. clang is a great tool for this purpose and can be easily integrated with git hooks and IDE.
First, install it by:
1 |
sudo apt-get install clang-format |
now, if you have a file like this:
after running, the following:
1 |
clang-format -style=WebKit -i main.cpp |
now you can use it like:
Available styles are LLVM, Google, Chromium, Mozilla, WebKit.
You can config clang for Qt Creator:
go to Tools>Option then:
Apply the formatting to the entire project
Apply the following script:
1 |
find foo/bar/ -iname *.h -o -iname *.cpp | xargs clang-format -i |
alternatively:
1 |
find . -regex '.*\.\(cpp\|hpp\|cc\|cxx\)' -exec clang-format -style=file -i {} \; |
On Windows machines:
1 2 3 4 5 6 |
Get-ChildItem -Path . -Directory -Recurse | foreach { cd $_.FullName echo $_.FullName &clang-format -i -style=file *.cpp *.hpp *.h } |
Turing off clang formatting
You can turn off the formatting by the following:
1 2 3 |
// clang-format off void unformatted_code // clang-format on |