Skip to content

Add .clang-format file

Robert Schmidt requested to merge feature-support-clang-format into develop

I propose to add a clang-format configuration file, notably since it allows to configure a range of lines of a file instead of the whole file only. Documentation of the options: https://clang.llvm.org/docs/ClangFormatStyleOptions.html

Integration into editors

Integration into various editors (vim, emacs, VSCode, CLion) is on the project page: https://clang.llvm.org/docs/ClangFormat.html

There is an eclipse plugin: https://github.com/wangzw/CppStyle

Integration into git

See here for a more detailed explanation. In short: Add this to ~/.gitconfig:

[clangFormat]
	binary = clang-format-12
	style = file

Now stage files for reformatting, then run git clang-format. It will reformat only the code that is staged, and you can the view what with a git diff (thus, the modified lines are not automatically staged).

There is also a pre-commit hook that you can install in file pre-commit-clang. See the file for instructions of how to use.

Differences to current astyle:

  • I believe astyle aligns assignments. The clang-format file does not do this, and would need to have AlignConsecutiveAssignments set to true. Example:
int aaaa = 12;
int b    = 23;
int ccc  = 23;
  • Also, the astyle configuration mandates to break lines after a logical, but the current clang format file does it before. Should we keep the astyle behavior? Option BreakBeforeBinaryOperators
if (thisVariable1 == thatVariable1 ||
        thisVariable2 == thatVariable2 ||
        thisVariable3 == thatVariable3)
    bar();

vs.

if (thisVariable1 == thatVariable1
        || thisVariable2 == thatVariable2
        || thisVariable3 == thatVariable3)
    bar();
Edited by Robert Schmidt

Merge request reports

Loading