Skip to content

Add .clang-format configuration file - #65049

Merged
edwinzrodriguez merged 1 commit into
ceph:mainfrom
edwinzrodriguez:ceph-clang-format
Oct 24, 2025
Merged

edwinzrodriguez merged 1 commit into
ceph:mainfrom
edwinzrodriguez:ceph-clang-format

Conversation

@edwinzrodriguez

@edwinzrodriguez edwinzrodriguez commented Aug 14, 2025 •

Copy link
Copy Markdown
Contributor

Pull Request: Add Clang-Format Configuration for Consistent Coding Standards

This pull request introduces a .clang-format configuration file to establish a consistent coding
standard across the project. The configuration is based on the settings from this Gist and serves as a
starting point for discussion on adopting a unified formatting approach.

Purpose

The goal is to initiate a conversation about the benefits of a standardized coding style and
how best to implement it. The proposed configuration includes several settings that may spark
discussion, including:

  1. Include Sorting and Grouping: The configuration enables include sorting (SortIncludes: true). This organizes include statements but may be debated for its necessity or impact on readability.
  2. Bin Packing: With BinPackParameters: true, function parameters exceeding the line length are packed into fewer lines for compactness. However, this may lead to more line changes when parameters are modified. Alternatives include splitting each parameter to its own line for clarity.
  3. Short Declarations: The configuration allows short declarations (e.g., simple variables or functions) on a single line. We should discuss whether this improves readability or if stricter rules are preferred.
  4. Pointer Alignment: The placement of * and & (e.g., next to the type, variable, or in the middle) is configured. This is a stylistic choice that may require team consensus.

Using git-clang-format

To integrate the .clang-format configuration with your workflow, you can use git-clang-format to apply formatting changes to only the modified lines in your Git commits, ensuring minimal disruption to the codebase. Here's how to use it:

  1. Install Clang-Format: Ensure clang-format is installed on your system. You can download it as part of LLVM or install it via a package manager (e.g., apt install clang-format on Ubuntu or brew install clang-format on macOS).
  2. Run git-clang-format:
    • To format only the changes in your current working directory (staged or unstaged), run:
      git clang-format
      This applies the .clang-format configuration to the modified lines in your working tree and stages the formatting changes.
    • To format changes in a specific commit (e.g., the most recent commit), run:
      git clang-format HEAD^ HEAD
      This formats the changes introduced in the specified commit range.
    • To format changes in your entire branch compared to a base branch (e.g., main), run:
      git clang-format main
  3. Review Changes: After running git-clang-format, review the formatted changes using git diff or git status. The tool modifies only the lines you’ve changed, preserving the rest of the codebase.
  4. Commit Changes: Once satisfied, commit the formatting changes along with your code changes:
    git commit -m "Apply clang-format to modified lines"

Note: Ensure the .clang-format file is in the repository’s root directory or specify its path with the --style=file:<path> option if it’s located elsewhere.

Next Steps

Please review the configuration and share feedback on the proposed settings. Additionally, discuss how git-clang-format can be integrated into our workflow to maintain consistent formatting. This is an opportunity to align on a coding standard that balances readability, maintainability, and team preferences.

Fixes: https://tracker.ceph.com/issues/72587

Contribution Guidelines

  • To sign and title your commits, please refer to Submitting Patches to Ceph.

  • If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.

  • When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands

@adamemerson adamemerson self-assigned this Aug 15, 2025
@ronen-fr
ronen-fr self-requested a review August 20, 2025 19:58
@edwinzrodriguez

Copy link
Copy Markdown
Contributor Author

jenkins test make check
jenkins test make check arm64

@edwinzrodriguez

Copy link
Copy Markdown
Contributor Author

jenkins test make check

@edwinzrodriguez

Copy link
Copy Markdown
Contributor Author

jenkins test make check arm64

@edwinzrodriguez

Copy link
Copy Markdown
Contributor Author

jenkins test make check

Comment thread .clang-format Outdated
BasedOnStyle: Google
IndentWidth: 2
TabWidth: 8
UseTab: Always

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these settings codify mixing tabs/spaces? I really hope that perhaps we can transition tospace-only indentation which is what I usually see folks doing already.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would much prefer avoiding the mixed tab/space style, but that is still in the CodingStyle and that would be a much larger change.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for spaces over tabs, mostly because tabs look wrong during github review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any opinion on the codingStyle which calls for mixed tabs and spaces? For that matter, just about every file has an Emacs/vim header declaring mixed usage.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mixed style is causing issues:

  • indentation seems wrong on some tools (e.g. PR review on GitHub, or beyondcompare)
  • some editors have troubles supporting it;
    ... and - it serves no good purpose. I'm all for spaces-only.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For changing the header of every file in the project, a single PR is probably the best way to go about it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'll affect around 2,000 files. Can Github handle that? And while it would be a simple mechanical change, there would need to be approvals from every group the change touches - is that possible?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You won't need approvals from everybody. If nobody else wants to review that I will just do a scan you aren't touching anything else in the files and then merge.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closer to 4000 files. I probably could break it up better, I didn't realize there was a 3000 file per change limit on what github will display

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, that was quick :) Fails in 'Check for Incompatible Licenses'

@edwinzrodriguez

Copy link
Copy Markdown
Contributor Author

jenkins test make check arm64

@edwinzrodriguez

Copy link
Copy Markdown
Contributor Author

jenkins test api

@adamemerson adamemerson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the configuration file is good so editors using clang-format pick up our style guidelines and not the LLVM defaults.

Comment thread .clang-format Resolved
Comment thread .clang-format
# should always precede "private:" or "public:" or "protected:"
AccessModifierOffset: -2
IndentCaseLabels: false
EmptyLineBeforeAccessModifier: Always

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would vote for the more permissive 'Leave'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the empty line helpful to visually identify interesting blocks. Without it, the declarations tend to get muddled (to my eye)

Comment thread .clang-format
ContinuationIndentWidth: 4

# The intended impact here is to allow up to 96 characters, but break at 80 if
# 96 is exceeded.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

96?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works with the penalty values to allow some lines > 80 if it makes sense

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather keep us at 80 if we can, we've been doing that for a while in much of our code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little wiggle room is handy here, otherwise this 82 char line:

  mutable snapid_t cached_seq; // max seq over self and all past+present parents.

is reformatted across 2 lines

  mutable snapid_t
      cached_seq; // max seq over self and all past+present parents.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a case where I'd just put the comment on the line above, but since we don't enforce hard-80 anywhere, wiggle room looks good to me.

Comment thread .clang-format

# Turn off comment reflow entirely
# TODO: This was done to prevent undoing user-defined formatting of multi-line
# comments.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and because clang-format is known (?) to be inconsistent in re-flowing from one run to another

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup

Comment thread .clang-format Outdated
# If false, a function declaration’s or function definition’s parameters
# will either all be on the same line or will have one line each.
BinPackArguments: true
BinPackParameters: true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a clear opinion here (been switching back and forth). I think we should hear more opinions on this one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like keeping them all separate or all together. So that if you add one, you don't need to modify any adjacent lines

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also makes review, merging, etc. easier, I think.

Comment thread .clang-format Outdated
# Constructor()
# : initializer1(),
# initializer2()
BreakConstructorInitializers: BeforeColon

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really prefer 'BeforeComma', as that allows adding/removing lines without having to fix the line before the point of change.
... and it looks better.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about AfterColon? That way initializers can be modified without affecting the decl line.
AfterColon (in configuration: AfterColon) Break constructor initializers after the colon and commas.
Constructor() :
initializer1(),
initializer2()

Comment thread .clang-format Outdated
BreakConstructorInitializers: BeforeColon

# Pad the braced list with spaces inside the braces.
Cpp11BracedListStyle: false

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd vote for 'true'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

Comment thread .clang-format Outdated

# No way to specify C vs C++ style comment for namespace closure, so just
# leave them alone.
FixNamespaceComments: false

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our code is mostly C++. I strongly suggest 'true'.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

Comment thread .clang-format Outdated
BitFieldColonSpacing: Both

# Do not break up strings into multi-line strings. This becomes a problem when
# multi-line strings with no spaces are broken, which upsets the Tup code checker.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having this set to 'false' kills one of clang-format useful and time saving features.
How bad is the problem with that Tup?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've run into issues when breaking up strings, not just with tup. I'll set it to true and hope for the best :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually use Tup anywhere?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, its a tool I used to use.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it wasn't just tup that had problems. As I recall there was a static analysis tool I liked that had issues

Comment thread .clang-format
# initializer list is short enough to fit on one line after the wrap, do not break
# into individual lines.
# Note NextLineOnly requires clang-format >= 15
PackConstructorInitializers: NextLineOnly

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to myself: delve deeper

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This avoids altering adjacent lines when an initializer is added or removed.

Comment thread .clang-format
# TODO: This was done to prevent undoing good formatting of doxygen @brief @param
# etc multi-line comments (similar to this comment). Investigate comment
# pragmas to leave these alone?
ReflowComments: false

@ronen-fr ronen-fr Sep 14, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and because clang-format is known (?) to be inconsistent in re-flowing from one run to another

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, clang-format can do some ugly things here.

@ronen-fr

Copy link
Copy Markdown
Contributor

A great step forward.
I have some comments/disagreements above. Most - just a matter of prefs. Some - important.

@edwinzrodriguez

Copy link
Copy Markdown
Contributor Author

The bulk of the changes are in:
#65741
#65742
#65743
Though the first tw still run into the issue with incompatible licenses check

@edwinzrodriguez

Copy link
Copy Markdown
Contributor Author

jenkins test make check arm64

@edwinzrodriguez

Copy link
Copy Markdown
Contributor Author

jenkins test dashboard cephadm

@edwinzrodriguez

Copy link
Copy Markdown
Contributor Author

jenkins test dashboard cephadm

1 similar comment
@edwinzrodriguez

Copy link
Copy Markdown
Contributor Author

jenkins test dashboard cephadm

@edwinzrodriguez

Copy link
Copy Markdown
Contributor Author

jenkins test make check arm64

gregsfortytwo added a commit that referenced this pull request Oct 8, 2025
Update header file tab settings, based on the discussions in this PR
and #65049

Reviewed-by: Ilya Dryomov <[email protected]>
Reviewed-by: Anthony D Atri <[email protected]>
Reviewed-by: Adam Emerson <[email protected]>
Reviewed-by: Greg Farnum <[email protected]>
gregsfortytwo added a commit that referenced this pull request Oct 8, 2025
Update cc file tab settings, based on the discussions in this PR
and #65049

Reviewed-by: Ilya Dryomov <[email protected]>
Reviewed-by: Anthony D Atri <[email protected]>
Reviewed-by: Greg Farnum <[email protected]>
gregsfortytwo added a commit that referenced this pull request Oct 8, 2025
…inder

Update misc file tab settings, based on the discussions in this PR
and #65049

Reviewed-by: Ilya Dryomov <[email protected]>
Reviewed-by: Anthony D Atri <[email protected]>
Reviewed-by: John Mulligan <[email protected]>
Reviewed-by: Greg Farnum <[email protected]>
@edwinzrodriguez

Copy link
Copy Markdown
Contributor Author

What needs to be done to finish this up?

@edwinzrodriguez

Copy link
Copy Markdown
Contributor Author

jenkins test make check arm64

1 similar comment
@edwinzrodriguez

Copy link
Copy Markdown
Contributor Author

jenkins test make check arm64

@edwinzrodriguez
edwinzrodriguez merged commit ea5b17a into ceph:main Oct 24, 2025
@github-actions

Copy link
Copy Markdown

This is an automated message by src/script/redmine-upkeep.py.

I have resolved the following tracker ticket due to the merge of this PR:

No backports are pending for the ticket. If this is incorrect, please update the tracker
ticket and reset to Pending Backport state.

Update Log: https://github.com/ceph/ceph/actions/runs/18780675111

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants