Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use clang-format to do "indentation only"?

  • I have a large code base with indentation that is very inconsistent.
  • I finally got some buy in from developers for running a code formatter.
  • I would like to start with just fixing the indentation because indentation is a lighter touch than running clang-format with a style.
    • I like correcting indentation only because when you do the indentation only and then run 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):

  • Correct Indentation
  • Applies simple indentation rules to the current selection, or the line containing the cursor, if there is no selection
  • Ctrl+I

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.

Is there a way to use clang-format to do "indentation only"? if yes, how?

For example with space-only, 4-spaces.

like image 625
Trevor Boyd Smith Avatar asked Sep 01 '25 04:09

Trevor Boyd Smith


2 Answers

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

like image 53
Rene Oschmann Avatar answered Sep 02 '25 16:09

Rene Oschmann


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".

like image 31
Joe Davis Avatar answered Sep 02 '25 17:09

Joe Davis