Skip to content

Commit

Permalink
Add additional notes re: ClangFormat configuration
Browse files Browse the repository at this point in the history
ClangFormat has an extensive and complex configuration system. Although the documentation is excellent and
comprehensive, some additional knowledge was hard earned during the development of the Arduino ClangFormat
configuration.

Where appropriate, some of this information was recorded in comments in the original configuration file, which have
already been transferred to the notes document. However, that original comment format was limiting so some of the
information was not previously recorded there.

The additional information is hereby added to the notes document.
  • Loading branch information
per1234 committed Jul 26, 2022
1 parent 4613cfd commit c5f256f
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion other/clang-format-configuration/notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ClangFormat configuration notes

## `AccessModifierOffset`

This is the additional indent beyond the standard code indentation level.

The Arduino IDE 1.x formatting style indented access modifiers (equivalent to `AccessModifierOffset: 0`), but also added an extra indent for the members. So non-indented access modifiers (`AccessModifierOffset: -2`) is actually closest to the previous formatting style.

## `BasedOnStyle`

This key is irrelevant because we define all configuration keys.
Expand All @@ -10,26 +16,86 @@ This key is irrelevant because we define all configuration keys.

This key is ignored when `BreakBeforeBraces` is set to anything other than `Custom`.

## `BreakBeforeInheritanceComma`

Seems to be a backwards compatibility name for `BreakInheritanceList: BeforeComma`:

https://releases.llvm.org/6.0.0/tools/clang/docs/ClangFormatStyleOptions.html#configurable-format-style-options:~:text=BreakBeforeInheritanceComma

## `BreakConstructorInitializersBeforeComma`

Seems to be a backwards compatibility name for `BreakConstructorInitializers: BeforeComma`:

https://releases.llvm.org/3.8.0/tools/clang/docs/ClangFormatStyleOptions.html#configurable-format-style-options:~:text=BreakConstructorInitializersBeforeComma

## `CommentPragmas`

Setting this to an empty string (e.g., `""`) prevents any comments from being matched.

## `ForEachMacros`

The items specified via this key are added to the base set hardcoded into **ClangFormat**, even if you set it to `ForEachMacros: []`.

## `IncludeCategories`

The items specified via this key are added to the base set hardcoded into **ClangFormat**, even if you set it to `IncludeCategories: []`.

## `IncludeIsMainRegex`

Setting this to an empty string (e.g., `""`) prevents any `#include` directives from being matched.

## `KeepEmptyLinesAtTheStartOfBlocks`

Note that empty lines at the ends of blocks are always removed.

## `MaxEmptyLinesToKeep`

Since the established behavior of the "Auto Format" feature from Arduino IDE 1.x is to allow any number of empty lines, this is set to a very large number to effectively reproduce the same.

## PointerAlignment
Note that empty lines at the ends of blocks are always removed.

## `NamespaceMacros`

This key is omitted from the `clang-format --dump-config` output when it is set to an empty array. Since Arduino's configuration does not have any need to define such macros, it is expected that this key will be absent from the configuration file even though present in the **ClangFormat** documentation.

## `PointerAlignment`

The `DerivePointerAlignment: true` configuration causes whatever pointer alignment style is predominant in the code to be used by **ClangFormat**. In the event no prevailing style can be can't be detected from code, this value is used as a fallback.

## `RawStringFormats`

This key is omitted from the `clang-format --dump-config` output when it is set to an empty array. Since Arduino's configuration does not have any need to define such formats, it is expected that this key will be absent from the configuration file even though present in the **ClangFormat** documentation.

## `StatementMacros`

The items specified via this key are added to the base set hardcoded into **ClangFormat**, even if you set it to `StatementMacros: []`.

## `TabWidth`

During the formatting process, **ClangFormat** always uses spaces initially for indentation and alignment, according to the `IndentWidth` configuration setting value.

If use of tabs for the final indents or alignment is indicated by the `UseTab` configuration setting, those spaces are then converted to tabs according to the value of `TabWidth`. For example, if `TabWidth` is set to `4`, then every occurrence of four spaces within the scope of the `UseTab` setting is converted to a tab.

This means there is a complex interaction between the `IndentWidth` and `TabWidth` values, which can produce unexpected results. For example, if `IndentWidth` is set to `2` and `TabWidth` to 4, every two indent levels is converted to a single tab, leaving two spaces for the final indentation of each odd level.

For this reason, `TabWidth` must always be set to the same value as `IndentWidth`.

## `TypenameMacros`

This key is omitted from the `clang-format --dump-config` output when it is set to an empty array. Since Arduino's configuration does not have any need to define such macros, it is expected that this key will be absent from the configuration file even though present in the **ClangFormat** documentation.

## `UseCRLF`

The `DeriveLineEnding: true` configuration causes whatever line endings are predominant in the code to be used by **ClangFormat**. In the event no prevailing style can be can't be detected from code, this value is used as a fallback.

## `WhitespaceSensitiveMacros`

The items specified via this key are added to the base set hardcoded into **ClangFormat**, even if you set it to `WhitespaceSensitiveMacros: []`.

## "Penalty" keys

These keys (e.g., `PenaltyBreakAssignment`) are not applicable when wrapping is disabled by setting `ColumnLimit` to `0`, as is done in Arduino's configuration. They are all arbitrarily set to `1`.

## Keys specific to irrelevant languages

The following keys are specific to languages other than the Arduino/C++/C targeted by this configuration. For this reason, their values are inconsequential.
Expand All @@ -43,6 +109,7 @@ They should be set to **ClangFormat**'s default values.

### JavaScript

- `InsertTrailingCommas`
- `JavaScriptQuotes`
- `JavaScriptWrapImports`

Expand Down

0 comments on commit c5f256f

Please sign in to comment.