style: Introduce clang-format and enforce consistent C++ formatting#160
Merged
jcfr merged 8 commits intocommontk:mainfrom Aug 25, 2025
Merged
style: Introduce clang-format and enforce consistent C++ formatting#160jcfr merged 8 commits intocommontk:mainfrom
jcfr merged 8 commits intocommontk:mainfrom
Conversation
…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.
Configuration based of Clang v20.1.8
This commit applies formatting to all C++ source files (`.h`, `.cpp`) based on the clang-format configuration introduced in the previous commit.
cbf6bea to
efd14f9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR introduces consistent C++ source formatting and enforces style guidelines through
clang-format.CI:
clang-formatconfiguration (based on Clang v20.1.8) with documented exceptions.Formatting:
//,clang-format off/on) to preserve intentional formatting.clang-formatacross all C++ sources (.h,.cpp).These changes ensure consistent, automated formatting across the codebase, improving readability and making future diffs easier to review.