CLion 2019.1 Help

ClangFormat as Alternative Formatter

ClangFormat is a popular code formatting tool that helps maintain common code style across team members and IDEs. It provides an option to store formatting settings in special YAML files named .clang-format or _clang-format. In CLion, you can use ClangFormat as an alternative to the built-in code formatter.

.clang-format configuration files

.clang-format files with formatting conventions should be placed in the project tree. Each of them applies to code files at the same level and in the subdirectories. Having multiple .clang-format files for a single project lets you, if needed, use different styles for different code folders.

ClangFormat offers many style options, which you can mix to create the set of rules most suitable for your team. It also provides several built-in styles for C/C++: LLVM, Google, WebKit, Chromium, and Mozilla. These predefined styles can be used as is or as a baseline for your custom set of style definitions.

To generate a .clang-format file with, for example, LLVM code style, run the following command (make sure to have ClangTools already installed): clang-format -style=llvm -dump-config > .clang-format

ClangFormat in CLion

If you have one or several .clang-format files in your project, CLion will detect them upon the project opening, prompt you to use ClangFormat, and notify about the changes in code style settings:

clangformat notifications

Invoke ClangFormat manually

You can invoke ClangFormat even if there is no .clang-format file in your project. In this case, the LLVM style will be used as default.

  • Click Enable ClangFormat for Project from the code formatting switcher located in the status bar area:

    enable clangformat from the code style switcher

  • Alternatively, select the Enable ClangFormat support checkbox in Settings/Preferences | Editor | Code Style:

    enable clangformat in the code style settings dialog

    Note that currently, this is an IDE-level setting: it affects all the projects you are working with in CLion, even if you select Project in the Scheme switcher.

After ClangFormat is enabled, the appropriate reformatting is triggered every time you:

  • type in the editor (which includes using auto-completion, code generation, refactorings, and quick-fixes);

  • call Code | Reformat Code (Ctrl+Alt+L) - this action also lets you reformat a selection of code instead of the whole file;

  • commit changes and select Reformat Code as the Before commit action:

    reformat code before commit

Review ClangFormat settings for a file

  • Open the file in the editor and click View ClangFormat options for ... from the toolbar switcher. CLion will show the list of settings taken from the corresponding .clang-format file. This list is currently read-only: to edit the settings, you need to change them right in the .clang-format file.

Edit .clang-format files

  • Open the desired file in the editor and click Edit '.clang-format' for... from the toolbar switcher:

    switch to clang-format file

Last modified: 24 July 2019