git diff --ignore-space-at-eol --ignore-space-change --ignore-all-space
you get zero diff lines.I want to avoid a situation where the formatted code is somehow worse and so people avoid any future attempts at making our codebase better via something like clang-format. In our case at least we can agree on spaces-only, tabs are 4-spaces. So improving only the indentation can only be a good thing.
Eclipse has a feature to "correct indentation" (via Menu --> Source --> Correct Indentation):
Eclipse's "Correct Indentation" does just the indentation but it is not a shell command and i want/need a shell command so that I can run the command on all the source code files.
For example with space-only, 4-spaces.
Clang-format always works with a default format. You can just customize it. If you don't specify a style the clang-format default is chosen. [1],[2]
Unfortunately, you cannot necessarily fix only indentation.
In the comments to your question KamilCuk suggested to use indent
probably referring to https://www.gnu.org/software/indent/
I thought about configuring a custom style which does only indentation but, while going over the style options there are unfortunately some which might alter the code-base, depending on how it looks, like AllowShortIfStatementsOnASingleLine
This disallows the co-existence of
if (a)
return ;
else {
return;
}
if (b) return ;
else {
return;
}
So, it might be possible that you find a certain configuration which works for your code-base, but this would be highly specific and brittle.
[1]
The configuration file can consist of several sections each having different Language: parameter denoting the programming language this section of the configuration is targeted at. See the description of the Language option below for the list of supported languages. The first section may have no language set, it will set the default style options for all lanugages. Configuration sections for specific language will override options set in the default section.
https://clang.llvm.org/docs/ClangFormatStyleOptions.html#configuring-style-with-clang-format
[2]
This section lists the supported style options. Value type is specified for each option. For enumeration types possible values are specified both as a C++ enumeration member (with a prefix, e.g. LS_Auto), and as a value usable in the configuration (without a prefix: Auto).
BasedOnStyle (string) The style used for all options not specifically set in the configuration.
https://clang.llvm.org/docs/ClangFormatStyleOptions.html#configurable-format-style-options
I don't have a Shell script for you that can do this on all source files, however, I use VSCode that allows me to specify the clang format fallback style in the settings.json so that every time I save my files, it applys the same formatting to each one consistently. Here is an example of my settings.json C_Cpp.clang_format_fallbackStyle that applys an indent width of 4.
"C_Cpp.clang_format_fallbackStyle": " {BasedOnStyle: Google, AllowShortCaseLabelsOnASingleLine: true, AlignConsecutiveDeclarations: true, AllowShortFunctionsOnASingleLine: All, AlignTrailingComments: true, Language: Cpp, AlwaysBreakAfterReturnType: None, PenaltyReturnTypeOnItsOwnLine: 9999, PointerAlignment: Left, SortIncludes: true, IndentWidth: 4, ColumnLimit: 0, BreakBeforeBraces: Allman, SpacesBeforeTrailingComments: 5, AlignAfterOpenBracket: true, AlignConsecutiveAssignments: true, AlignConsecutiveMacros : true}",
https://clang.llvm.org/docs/ClangFormatStyleOptions.html This documentation will explain the different parameters and values for this option. For your questions specifically, I would look at "IndentWidth" and "UseTab".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With