Skip to content

style: Introduce clang-format and enforce consistent C++ formatting#160

Merged
jcfr merged 8 commits intocommontk:mainfrom
jcfr:style-consistently-indent-cpp-files
Aug 25, 2025
Merged

style: Introduce clang-format and enforce consistent C++ formatting#160
jcfr merged 8 commits intocommontk:mainfrom
jcfr:style-consistently-indent-cpp-files

Conversation

@jcfr
Copy link
Member

@jcfr jcfr commented Aug 25, 2025

This PR introduces consistent C++ source formatting and enforces style guidelines through clang-format.

CI:

  • Add clang-format configuration (based on Clang v20.1.8) with documented exceptions.

Formatting:

  • Normalize whitespace in assignments, declarations, and control statements.
  • Standardize pointer/reference placement, casts, and template argument spacing.
  • Apply miscellaneous whitespace cleanups across C++ files.
  • Add inline comments (//, clang-format off/on) to preserve intentional formatting.
  • Apply clang-format across all C++ sources (.h, .cpp).

These changes ensure consistent, automated formatting across the codebase, improving readability and making future diffs easier to review.

@jcfr jcfr enabled auto-merge (rebase) August 25, 2025 06:10
jcfr added 8 commits August 25, 2025 09:38
…g-format

This commit applies a series of `sed`-based transformations to normalize
spacing around pointers and references throughout the codebase. The changes
ensure consistency in formatting.

Adapted from Slicer/Slicer@83294207db ("STYLE: Normalize pointer/reference formatting in preparation for clang-format", 2025-07-09)

The corresponding changes can be reproduced using the following script:

```
echo "[1/5] Normalize pointer/reference formatting before variable names"
for file in $(find . -name '*.h' -o -name '*.cpp' -o -name '*.cxx' -o -name '*.txx'); do
  sed -i -E \
    -e 's/\b(ctk\w+)\s+([*&]{1,2})\s*([a-zA-Z_])/\1\2 \3/g' \
    -e 's/\b(std::\w+)\s+([*&]{1,2})\s*([a-zA-Z_])/\1\2 \3/g' \
    -e 's/\b(Q\w+)\s+([*&]{1,2})\s*([a-zA-Z_])/\1\2 \3/g' \
    -e 's/\b(char|int|short|unsigned short|float|long|unsigned long|double|bool|void)\s+([*&]{1,2})\s*([a-zA-Z_])/\1\2 \3/g' \
    -e 's/\b(T)\s+([*&]{1,2})\s*([a-zA-Z_])/\1\2 \3/g' \
    -e 's/\b(T::\w+)\s+([*&]{1,2})\s*([a-zA-Z_])/\1\2 \3/g' \
    -e 's/\b(\w+Type)\s+([*&]{1,2})\s*([a-zA-Z_])/\1\2 \3/g' \
  "$file"
done

echo "[2/5] Normalize pointer/reference formatting before punctuation"
for file in $(find . -name '*.h' -o -name '*.cpp' -o -name '*.cxx' -o -name '*.txx'); do
  sed -i -E \
    -e 's/\b(ctk\w+)\s+([*&]{1,2})\s*([\,\>\[\)])/\1\2\3/g' \
    -e 's/\b(std::\w+)\s+([*&]{1,2})\s*([\,\>\[\)])/\1\2\3/g' \
    -e 's/\b(Q\w+)\s+([*&]{1,2})\s*([\,\>\[\)])/\1\2\3/g' \
    -e 's/\b(char|int|short|unsigned short|float|long|unsigned long|double|bool|void)\s+([*&]{1,2})\s*([\,\>\[\)])/\1\2\3/g' \
    -e 's/\b(T)\s+([*&]{1,2})\s*([\,\>\[\)])/\1\2\3/g' \
    -e 's/\b(T::\w+)\s+([*&]{1,2})\s*([\,\>\[\)])/\1\2\3/g' \
    -e 's/\b(\w+Type)\s+([*&]{1,2})\s*([\,\>\[\)])/\1\2\3/g' \
  "$file"
done

echo "[3/5] Remove extra space before comma inside parentheses"
for file in $(find . -name '*.h' -o -name '*.cpp' -o -name '*.cxx' -o -name '*.txx'); do
  sed -i -E \
    -e 's/\(\s*\b(ctk\w+)\s+\,/\(\1\,/g' \
    -e 's/\(\s*\b(std::\w+)\s+\,/\(\1\,/g' \
    -e 's/\(\s*\b(Q\w+)\s+\,/\(\1\,/g' \
    -e 's/\(\s*\b(char|int|short|unsigned short|float|long|unsigned long|double|bool|void)\s+\,/\(\1\,/g' \
    -e 's/\(\s*\b(T)\s+\,/\(\1\,/g' \
    -e 's/\(\s*\b(T::\w+)\s+\,/\(\1\,/g' \
    -e 's/\(\s*\b(\w+Type)\s+\,/\(\1\,/g' \
  "$file"
done

echo "[4/5] Remove extra space after punctuation"
for file in $(find . -name '*.h' -o -name '*.cpp' -o -name '*.cxx' -o -name '*.txx'); do
  sed -i -E \
    -e 's/,\s*(ctk\w+)(\*?\[\])\s*\)/, \1\2\)/g' \
    -e 's/,\s*(std::\w+)(\*?\[\])\s*\)/, \1\2\)/g' \
    -e 's/,\s*(Q\w+)(\*?\[\])\s*\)/, \1\2\)/g' \
    -e 's/,\s*(char|int|short|unsigned short|float|long|unsigned long|double|bool|void)(\*?\[\])\s*\)/, \1\2\)/g' \
    -e 's/,\s*(T)(\*?\[\])\s*\)/, \1\2\)/g' \
    -e 's/,\s*(T::\w+)(\*?\[\])\s*\)/, \1\2\)/g' \
    -e 's/,\s*(\w+Type)(\*?\[\])\s*\)/, \1\2\)/g' \
  "$file"
done

echo "[5/5] Remove extra space between angle bracket and type"
for file in $(find . -name '*.h' -o -name '*.cpp' -o -name '*.cxx' -o -name '*.txx'); do
  sed -i -E \
    -e 's/<\s*(ctk\w+)([\,\>])/<\1\2/g' \
    -e 's/<\s*(std::\w+)([\,\>])/<\1\2/g' \
    -e 's/<\s*(Q\w+)([\,\>])/<\1\2/g' \
    -e 's/<\s*(char|int|short|unsigned short|float|long|unsigned long|double|bool|void)([\,\>])/<\1\2/g' \
    -e 's/<\s*(T)([\,\>])/<\1\2/g' \
    -e 's/<\s*(T::\w+)([\,\>])/<\1\2/g' \
    -e 's/<\s*(\w+Type)([\,\>])/<\1\2/g' \
  "$file"
done
```
This commit removes unnecessary spaces inside angle brackets used in C++ casts
and template declarations to ensure consistent formatting and to align with
clang-format expectations.

The corresponding changes can be reproduced using the script from
Slicer/Slicer@9164d1787e ("STYLE: Normalize spacing in cast expressions and template arguments", 2025-07-09)
This commit standardizes the formatting of control statement to improve
code readability and prepare for clang-format integration.

Adapted from Slicer/Slicer@c696b6c1aa ("STYLE: Normalize control statement formatting", 2025-07-10)

The corresponding changes can be reproduced using the following script:

```
for file in $(find . -name '*.h' -o -name '*.cpp' -o -name '*.cxx' -o -name '*.txx'); do
  sed -i -E \
    -e 's/\b(while|foreach|if|for|catch)\(/\1 \(/g' \
  "$file"
done
This commit improves code readability and prepares for consistent formatting with
`clang-format` by normalizing whitespace around `=` in variable declarations and
defaulted special member functions.

Adapted from Slicer/Slicer@ed73eb8f70 ("STYLE: Normalize whitespace in C++ assignments and declarations", 2025-07-10)

Changes include:

* Enforcing consistent spacing around the `=` operator in:

  * Variable assignments (e.g., `foo=true` → `foo = true`)
  * Default declarations (e.g., `=default;` → `= default;`)
  * Pointer and reference declarations involving common C++ types
* Targeted types include:

  * Built-in types: `int`, `bool`, `double`, etc.
  * Qt types: `QString`, `QWidget*`, etc.

Example transformations:

* `parent=0`           → `parent = 0`
* `=default;`          → `= default;`
* `bool foo=true`      → `bool foo = true`

Script used:

```bash
for file in $(find . -name '*.h' -o -name '*.cpp' -o -name '*.cxx' -o -name '*.txx'); do
  sed -i -E \
    -e 's/\b(parent|parentWidget)\s*=\s*(0|nullptr)/\1 = \2/g' \
    -e 's/\s*\=\s*default\;/ = default\;/g' \
    -e 's/\b(bool|int|double|short|char|const char|QString|std\:\:string)(\*?\&?) (\w+)=(\w+)/\1\2 \3 = \4/g' \
    -e 's/(ctk\w+)(\:\:\w+)?(\*?\&?) (\w+)=(\w+)/\1\2\3 \4 = \5/g' \
    -e 's/(Q\w+)(\:\:\w+)?(\*?\&?) (\w+)=(\w+)/\1\2\3 \4 = \5/g' \
  "$file"
done
```
This commit normalizes whitespace across a variety of common C++ patterns to improve consistency, enhance readability, and prepare the codebase for clang-format enforcement.

The corresponding changes can be reproduced using the script from
Slicer/Slicer@11a1fae3b2 ("STYLE: Apply miscellaneous whitespace formatting improvements", 2025-07-10)
…tting

This commit adds inline `//` comments at the end of multi-line expressions
to prevent `clang-format` from collapsing them into single lines.

It also includes `// clang-format off/on` comments around selected code
blocks to preserve intentional formatting.

These changes preserve intentional formatting and improve readability in
preparation for automated formatting enforcement.
This commit applies formatting to all C++ source files (`.h`, `.cpp`)
based on the clang-format configuration introduced in the previous commit.
@jcfr jcfr force-pushed the style-consistently-indent-cpp-files branch from cbf6bea to efd14f9 Compare August 25, 2025 13:42
@jcfr jcfr merged commit 6e3b300 into commontk:main Aug 25, 2025
7 checks passed
@jcfr jcfr deleted the style-consistently-indent-cpp-files branch August 25, 2025 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant