Skip to content

Add clang-format file#4238

Open
netmindz wants to merge 2 commits intowled:mainfrom
netmindz:clang-file
Open

Add clang-format file#4238
netmindz wants to merge 2 commits intowled:mainfrom
netmindz:clang-file

Conversation

@netmindz
Copy link
Member

@netmindz netmindz commented Oct 31, 2024

Rather than just describing what code style we look for, it would be good to provide a clang-format file

To get us started I have created one based on a style detection tool and one of the current files

In order to verify if the file is right, we can run clang-format -i wled00/*.cpp and if we have the right values then all the changes should match our expectations

We might not choose to mass reformat all existing code, but it serves as useful test to check that new code being written will indeed conform to our style

I very much welcome input from others, especially those with knowledge of how to write a clang-format file

Summary by CodeRabbit

  • Chores
    • Added a new code formatting configuration to standardize code style across the project.
    • Updated file ignore settings to allow the new formatting configuration file to be tracked.

@netmindz netmindz changed the base branch from 0_15 to main December 16, 2024 13:29
@w00000dy
Copy link
Contributor

w00000dy commented Feb 2, 2025

I think that's a very good idea.
The question is, do we want a .clang-format file that is as close as possible to the current WLED code, or do we want a .clang-format file that also fixes some of the unattractive things in the WLED code?

@netmindz
Copy link
Member Author

netmindz commented Feb 2, 2025

That's where I've stumbled a bit to be honest @w00000dy
If you use on of the automated tools to help build a clang file against current code then you get slightly different rules depending on which file you use as your reference

Copy link
Contributor

@w00000dy w00000dy left a comment

Choose a reason for hiding this comment

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

Since you wanted some feedback, I had a look at it. I've written what I think about a few options. The following options are suggestions for additions to the .clang-format file from my side:

  • InsertNewlineAtEOF: true
  • AllowShortBlocksOnASingleLine: true

AllowShortBlocksOnASingleLine allows this:

while (true) {}

and

while (true) { continue; }

---
BasedOnStyle: Google
AlignAfterOpenBracket: DontAlign
AlignConsecutiveDeclarations: Consecutive
Copy link
Contributor

Choose a reason for hiding this comment

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

This formats this:

float fmod_t(float num, float denom) {
  int tquot = num / denom;
  float res = num - tquot * denom;
  return res;
}

to this:

float fmod_t(float num, float denom) {
  int   tquot = num / denom;
  float res = num - tquot * denom;
  return res;
}

I prefer the first version.

AlignTrailingComments: false
AllowAllArgumentsOnNextLine: false
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: None
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you want to set this to None?

Now this formats this:

int add(int a, int b) { return a + b; }

to this:

int add(int a, int b) {
  return a + b;
}

AlignAfterOpenBracket: DontAlign
AlignConsecutiveDeclarations: Consecutive
AlignEscapedNewlines: DontAlign
AlignOperands: DontAlign
Copy link
Contributor

Choose a reason for hiding this comment

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

In the rare case that this happens, I think aligning would improve readability here.

BasedOnStyle: Google
AlignAfterOpenBracket: DontAlign
AlignConsecutiveDeclarations: Consecutive
AlignEscapedNewlines: DontAlign
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this was set because of your tool. I see no reason why we should set this to DontAlign since aligning would improve readability.

@@ -0,0 +1,23 @@
---
BasedOnStyle: Google
AlignAfterOpenBracket: DontAlign
Copy link
Contributor

Choose a reason for hiding this comment

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

I think in the rare case where we exceed the ColumnLimit, aligning would improve readability.

AllowShortIfStatementsOnASingleLine: Always
AlwaysBreakBeforeMultilineStrings: false
BreakBeforeTernaryOperators: false
BreakConstructorInitializersBeforeComma: true
Copy link
Contributor

Choose a reason for hiding this comment

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

The BreakConstructorInitializersBeforeComma should be replaced with the BreakConstructorInitializers option.

https://reviews.llvm.org/D32479

ColumnLimit: 240
ContinuationIndentWidth: 2
IndentPPDirectives: BeforeHash
KeepEmptyLinesAtTheStartOfBlocks: true
Copy link
Contributor

Choose a reason for hiding this comment

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

ContinuationIndentWidth: 2
IndentPPDirectives: BeforeHash
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 2
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should change this so that the code doesn't take up so many lines?

IndentPPDirectives: BeforeHash
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 2
ReflowComments: false
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should at least set this to IndentOnly so that misaligned comments are formatted.

KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 2
ReflowComments: false
SortIncludes: Never
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not?

@netmindz
Copy link
Member Author

netmindz commented Feb 3, 2025

Thank you @w00000dy for your comments, this is my first time using clang and I'm not a C++ developer I am very grateful for input from those with experience.

I'll have a look through your individual comments when I get time to do so

@github-actions
Copy link

github-actions bot commented Jun 4, 2025

Hey! This pull request has been open for quite some time without any new comments now. It will be closed automatically in a week if no further activity occurs.
Thank you for contributing to WLED! ❤️

@github-actions github-actions bot added the stale This issue will be closed soon because of prolonged inactivity label Jun 4, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 4, 2025

Walkthrough

A new .clang-format file was added to define code formatting rules, and the .gitignore file was updated to stop ignoring .clang-format. No changes were made to any code or exported entities.

Changes

Files Change Summary
.clang-format Added a code formatting configuration file with custom rules based on Google style.
.gitignore Removed .clang-format from ignored files, allowing it to be tracked in version control.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (3)
.clang-format (3)

3-3: Reconsider disabling bracket alignment
AlignAfterOpenBracket: DontAlign prevents aligning parameters, which can hurt readability in multi-line calls. Consider enabling alignment for consistency.


7-7: Revisit trailing comments alignment
AlignTrailingComments: false will leave trailing comments unaligned. Consider IndentOnly to maintain comment indentation consistency.


14-14: ⚠️ Potential issue

Deprecated option used for constructor initializers
BreakConstructorInitializersBeforeComma is deprecated. Replace with the modern BreakConstructorInitializers setting:

-BreakConstructorInitializersBeforeComma: true
+BreakConstructorInitializers: BeforeComma
🧹 Nitpick comments (2)
.clang-format (2)

8-8: AllowAllArgumentsOnNextLine consideration
AllowAllArgumentsOnNextLine: false forbids moving all function arguments to the next line, which may reduce readability for long signatures. Consider enabling it.


13-13: Ternary operator breaks
BreakBeforeTernaryOperators: false compacts ternary expressions but can hinder readability for complex conditions. Consider setting it to true.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 18e0ec9 and 16c91b6.

📒 Files selected for processing (2)
  • .clang-format (1 hunks)
  • .gitignore (0 hunks)
💤 Files with no reviewable changes (1)
  • .gitignore
🔇 Additional comments (14)
.clang-format (14)

1-2: Baseline Style Confirmation
BasedOnStyle: Google and the YAML header are correctly set to anchor on the Google style.


4-4: Approve consecutive declarations alignment
AlignConsecutiveDeclarations: Consecutive will line up declarations nicely, improving readability.


5-5: Escaped newlines alignment
AlignEscapedNewlines: DontAlign is sensible to avoid unpredictable indent shifts with escaped line breaks.


6-6: Operand alignment disabled
AlignOperands: DontAlign matches Google style and avoids uneven spacing in expressions.


9-11: Short constructs formatting
AllowShortCaseLabelsOnASingleLine: true, AllowShortFunctionsOnASingleLine: None, and AllowShortIfStatementsOnASingleLine: Always align with Google style and look appropriate.


12-12: Multiline strings behavior
AlwaysBreakBeforeMultilineStrings: false preserves the original formatting of long strings. This is reasonable if you want to keep manual line breaks.


15-15: String literal breaks
BreakStringLiterals: false prevents splitting string literals. This is fine if preserving literal integrity is a priority.


16-16: Column limit sanity check
ColumnLimit: 240 is much wider than typical guidelines (80–100). Confirm this is intentional for display configurations.


17-17: Continuation indent width
ContinuationIndentWidth: 2 follows Google style and ensures consistent indentation for wrapped lines.


18-18: Preprocessor directive indent
IndentPPDirectives: BeforeHash is less common than AfterHash. Verify that macro and conditional directive formatting meets expectations.


20-20: Empty lines retention
MaxEmptyLinesToKeep: 2 prevents excessive blank lines and is a good default.


21-21: Comment reflow disabled
ReflowComments: false preserves manual comment formatting. Good if you want to control wording and layout.


22-22: Include sorting policy
SortIncludes: Never disables automatic include sorting. Confirm this aligns with the project's manual include grouping conventions.


23-23: Trailing comment spacing
SpacesBeforeTrailingComments: 1 is standard practice and looks correct.

ColumnLimit: 240
ContinuationIndentWidth: 2
IndentPPDirectives: BeforeHash
KeepEmptyLinesAtTheStartOfBlocks: true
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Deprecated empty-lines option
KeepEmptyLinesAtTheStartOfBlocks is deprecated. Update to the current equivalent (e.g., KeepEmptyLinesAtTopOfFunctions).

🤖 Prompt for AI Agents
In the .clang-format file at line 19, replace the deprecated option
KeepEmptyLinesAtTheStartOfBlocks with the current equivalent option
KeepEmptyLinesAtTopOfFunctions to ensure compatibility with the latest
clang-format versions.

@w00000dy
Copy link
Contributor

w00000dy commented Jun 4, 2025

Please don't close this PR. I would like the idea of this PR

@willmmiles willmmiles added keep This issue will never become stale/closed automatically and removed stale This issue will be closed soon because of prolonged inactivity labels Jun 4, 2025
@netmindz netmindz added this to the 0.16.0 candidate milestone Jan 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

keep This issue will never become stale/closed automatically

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants