Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clang-format: improve and update #41307

Merged
merged 4 commits into from
Jun 15, 2022
Merged

Conversation

gmarull
Copy link
Member

@gmarull gmarull commented Dec 17, 2021

The number of options available on clang-format has become quite
extensive over-time, making a dumped file difficult to read or maintain.
In this patch, the .clang-format has been re-written by inheriting from
LLVM (default) since most of the options can be re-used to match the
Zephyr/Linux coding style conventions.

carlescufi
carlescufi previously approved these changes Dec 22, 2021
Copy link
Member

@carlescufi carlescufi left a comment

Choose a reason for hiding this comment

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

I haven’t tested it but the changes look reasonable.

@ppryga-nordic
Copy link
Collaborator

ppryga-nordic commented Jan 5, 2022

I didn't compare inherited options. I assume that you have tested it and results are exactly the same or even better due to enable options that were commented out for backward compatibility. Am I right with my assumption?

teburd
teburd previously approved these changes Jan 7, 2022
Copy link
Collaborator

@teburd teburd left a comment

Choose a reason for hiding this comment

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

I tried this out on the existing code base and, at least personally, I thought it did a really nice job.

It aligned macro and function arguments that were previously unaligned. It removed extraneous spaces, which there were surprisingly things like

uint32_t x =  123;

Where the double spacing after the = would be fixed to a single space.

It generally made things more, not less, readable. I didn't run checkpatch after, that might be an interesting bout to see if they agree.

@gmarull gmarull added the DNM This PR should not be merged (Do Not Merge) label Jan 7, 2022
@MaureenHelm MaureenHelm removed the dev-review To be discussed in dev-review meeting label Jan 13, 2022
@nashif
Copy link
Member

nashif commented Jan 13, 2022

getting:

clang-format kernel/sem.c
/home/nashif/Work/zephyrproject/zephyr/.clang-format:63:20: error: unknown enumerated scalar
SpaceBeforeParens: ControlStatementsExceptControlMacros
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error reading /home/nashif/Work/zephyrproject/zephyr/.clang-format: Invalid argument

clang-format version 12.0.1 (Fedora 12.0.1-1.fc34)

@gmarull gmarull force-pushed the clang-format branch 2 times, most recently from a157020 to 2b12325 Compare January 24, 2022 17:56
@gmarull
Copy link
Member Author

gmarull commented Jan 24, 2022

getting:

clang-format kernel/sem.c
/home/nashif/Work/zephyrproject/zephyr/.clang-format:63:20: error: unknown enumerated scalar
SpaceBeforeParens: ControlStatementsExceptControlMacros
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error reading /home/nashif/Work/zephyrproject/zephyr/.clang-format: Invalid argument

clang-format version 12.0.1 (Fedora 12.0.1-1.fc34)

Commented out this option (available since 13.0)

@erwango
Copy link
Member

erwango commented Feb 2, 2022

I'm not sure how to do this and avoiding bike-shading but in order to help getting a consensus on the change and not coming back on it later, would it be possible to demonstrate the result if this change, so each one can have a chance to give his opinion and commonly agree on the result?
For sure, one can do it in his side and see if happy with the result, but demonstration result depends on the sample file used. Having a draft PR which uses this change to comment would be nice, IMO.

@nashif
Copy link
Member

nashif commented Feb 10, 2022

@gmarull can this PR also remove .uncrustify.cfg and replace uncrustify with clang-format in the docs as well, related bug #41281 dealing with this assigned to you to close in one shot :)

@github-actions
Copy link

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@github-actions github-actions bot added the Stale label Apr 13, 2022
@teburd teburd removed the Stale label Apr 13, 2022
@teburd
Copy link
Collaborator

teburd commented Apr 13, 2022

This isn't stale, and I still care

@github-actions
Copy link

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

gmarull added 4 commits June 13, 2022 12:01
Update the list of for each macros using the suggested command in the
configuration file.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
The amount of options available on clang-format has become quite
extensive over-time, making a dumped file difficult to read or maintain.
In this patch the .clang-format has been re-written by inheriting from
LLVM (default) since most of the options can be re-used to match the
Zephyr/Linux coding style conventions.

Note: The 14 release has interesting options for Zephyr, in
particular, these new settings would be beneficial:

IfMacros:
  - CHECKIF
SpaceBeforeParens: Custom
SpaceBeforeParensOptions:
  AfterForeachMacros: true
  AfterIfMacros: true

.clang-format should be periodically updated as new clang-format
releases reach Linux distros.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Prefer clang-format over uncrustify for source code formatting.
uncrustify configuration files will be removed in future commits.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Remove uncrustify configuration file in favor of using clang-format, a
tool that is nowadays more popular/powerful for code formatting.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
@cfriedt
Copy link
Member

cfriedt commented Jun 14, 2022

It generally made things more, not less, readable. I didn't run checkpatch after, that might be an interesting bout to see if they agree.

checkpatch.pl is both a formatting tool and linter. The clang solution uses clang-tidy as the linter and clang-format as the formatter. As long as clang-format and checkpatch.pl agree on formatting, we should be good.

It should be noted that the clang tooling actually uses LLVM as the C / C++ parser, so it's quite accurate as it's derived from the AST. IIRC, checkpatch.pl might only be suitable for C, so we may eventually want to replace it with clang-format and also introduce clang-tidy into CI.

Of course, there is also scan-build for static analysis, which uses LLVM and leverages the AST as well.

@gmarull
Copy link
Member Author

gmarull commented Jun 14, 2022

It generally made things more, not less, readable. I didn't run checkpatch after, that might be an interesting bout to see if they agree.

checkpatch.pl is both a formatting tool and linter. The clang solution uses clang-tidy as the linter and clang-format as the formatter. As long as clang-format and checkpatch.pl agree on formatting, we should be good.

It would be nice to introduce clang-tidy as well. The only issue I have observed with clang-format/checkpatch.pl is within macros:

	static const struct jm101_config jm101_config_##i = {                  \
		...							       \
	};                                                                     \
                                                                               \ /* <- clang-format starts line with spaces here, not tab, checkpatch complains about leading spaces */
	DEVICE_DT_INST_DEFINE(i, jm101_init, NULL, &jm101_data_##i,            \

However, I guess we could patch checkpatch to ignore those edge cases. Our abuse of nested macros can confuse clang-format as well, for example,

	static const struct jm101_config jm101_config_##i = {                  \
		.uart = DEVICE_DT_GET(DT_INST_PHANDLE(i, uart)),               \
		.addr = DT_INST_PROP(i, address),                              \
		IF_ENABLED(CONFIG_JM101_TRIGGER,                               \
			   (.touch = GPIO_DT_SPEC_INST_GET_OR(i, touch_gpios,  \
							      {}), )) /* */    \ /* comment added to force line break here, otherwise clang-format doesn't work well */
	};                                                                     \
                                                                               \
	DEVICE_DT_INST_DEFINE(i, jm101_init, NULL, &jm101_data_##i,            \

But overall, I had a good experience with it, specially when refactoring code.

@ppryga-nordic
Copy link
Collaborator

I'm also in favor to use clang-format. From time to time e.g. in BT Controller's code I observe that the formatting is a bit different than done with clang-format. That introduces additional changes to PRs that not always are necessary or welcome by reviewers.
At the end of the day, the more it is used the less of these situations happens.

@cfriedt
Copy link
Member

cfriedt commented Jun 14, 2022

It would be nice to introduce clang-tidy as well. The only issue I have observed with clang-format/checkpatch.pl is within macros:

@gmarull - that mirrors my experiences as well. It's probably something that is worth fixing upstream on behalf of the Zephyr project if we were to adopt clang-format.

@gmarull
Copy link
Member Author

gmarull commented Jun 14, 2022

@gmarull - that mirrors my experiences as well. It's probably something that is worth fixing upstream on behalf of the Zephyr project if we were to adopt clang-format.

Agreed, would be nice to have these small issues fixed upstream.

Copy link
Collaborator

@teburd teburd left a comment

Choose a reason for hiding this comment

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

Yes please

@carlescufi carlescufi requested a review from MaureenHelm June 15, 2022 07:10
@carlescufi
Copy link
Member

+1 to clang-format, @nashif and @MaureenHelm ping

@nashif nashif merged commit d89dc22 into zephyrproject-rtos:main Jun 15, 2022
@gmarull gmarull deleted the clang-format branch June 15, 2022 13:38
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.

10 participants