Skip to content
Ross Whitfield edited this page Oct 5, 2015 · 22 revisions

This page describes how to use and configure clang-format. clang-format is a command line tool built on top of llvm that enforces whitespace conventions for c/c++ code. The configuration for it (.clang-format) is in the root directory of the repository and is discovered by the tool automatically. clang-format includes support for e.g. the coding styles: LLVM, Google, Chromium and Mozilla. Mantid uses the llvm style, see also the Mantid C++ coding standards page.

Using git to format

There is a tool, git-clang-format, which adds support to git to automatically format code that is added before it is committed. This only changes the formatting of the lines that you have already edited. In general, after you make your changes you can do the following

git add <list of files>
git clang-format
git add <list of files that got formatting changes>
git commit

This will format only the lines that you had edited already, eliminating the possibility of introducing merge conflicts.

Configure your favorite IDE or editor for using clang-format

Most everything can attach to clang-format. The list in clang-format's documentation.

Others have plugins/config from other sources:

  • Sublime (hiding in the page)
  • QTCreator (gist describing howto. Ignore the configuration and just select LLVM and it will be available in the Tools->Beautifier menu) ** To configure a keyboard shortcut go to "Tools->Options->Environment->Keyboard and search for ClangFormat. Add your desired short cut
  • XCode (on github)
  • [Eclipse] (https://github.com/wangzw/cppstyle) (replace Google with LLVM as style)