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

Add clang-tidy support for static code analysis #2648

Merged
merged 7 commits into from
Apr 23, 2024

Conversation

mikee47
Copy link
Contributor

@mikee47 mikee47 commented May 28, 2023

This PR allows static code analysis using clang-tidy, suggested by #2616.

Clang has more limited constexpr support than GCC so cannot parse some of the FlashString and NanoTime code without modification. However, these changes are only made when __clang__ is defined and do not affect regular builds with GCC.

This PR also includes some basic code fixes which clang identifies. There are a lot more to look at.

To try it out, build a project with:

make CLANG_TIDY=clang-tidy

Add additional parameters like this:

make CLANG_TIDY="clang-tidy --fix --fix-errors"

Notes:

  • Don't use -j option as clang-tidy output and fixes don't get serialised correctly.
  • Settings are in the main .clang-tidy file. A custom version can be used and passed on the command line.
  • I've been using clang 17.0.6 https://releases.llvm.org/17.0.1/tools/clang/tools/extra/docs/clang-tidy/index.html
  • Only source files which haven't been built are inspected. So, to restrict which code gets processed built the entire application then 'clean' the relevant modules before proceeding with clang-tidy.
  • No object code is generated by clang.

@SmingHub SmingHub deleted a comment from what-the-diff bot May 28, 2023
@mikee47 mikee47 force-pushed the feature/clang-tidy branch from d4d6732 to b16ddf5 Compare May 28, 2023 13:10
@mikee47 mikee47 force-pushed the feature/clang-tidy branch from b16ddf5 to 2bd33be Compare April 19, 2024 20:24
@mikee47 mikee47 changed the title [WIP] Try out clang-tidy [WIP] Add clang-tidy support for static code analysis Apr 19, 2024
@mikee47 mikee47 mentioned this pull request Apr 20, 2024
@mikee47 mikee47 force-pushed the feature/clang-tidy branch 2 times, most recently from e0b6772 to 4b9fc84 Compare April 21, 2024 20:23
slaff pushed a commit that referenced this pull request Apr 22, 2024
This PR proposes some changes to the Hosted classes

**Update simpleRPC to master**

With clang-tidy #2648 some failures occur in the `simpleRPC` library. Updating this to current master solves the issues.
I've also put all the code into the `simpleRPC` namespace which sorts out the conflict with `Vector` but makes it available for use if required.

**Tidy HostTests module**

Also decode the anonymous 'packet' blob so we can see what's in it and compare with spec. should we wish to update it.

**Use abstract base class for callbacks**

Simplifies implementation since usually all callbacks are required.
Compiler ensures all methods have implementations.
`clang-tidy` gave potential memory leak indication because of Delegates. Not sure if that's real but this fixes it.
mikee47 added 5 commits April 22, 2024 15:15
Code doesn't get compiled so skip linking stage.
Remove `ENABLE_CLANG_TIDY`, use `CLANG_TIDY`, allows specific versions of clang-tidy to be used.
e.g. cannot use `static_cast` so FlashString needs some fudging to get it to compile.
Also clang doesn't support constexpr math (yet) so workaround that.
Compiler can optimise ones in headers, but a few others worth doing
Because CSocket virtual destructor calls virtual `close` method which doesn't behave as expected.
This one's simple to fix but there are some other cases in the framework (Networking) which will need to be looked at separately.
@mikee47 mikee47 force-pushed the feature/clang-tidy branch from 4b9fc84 to 1ed7de3 Compare April 22, 2024 14:15
@slaff slaff added this to the 5.2.0 milestone Apr 22, 2024
@mikee47
Copy link
Contributor Author

mikee47 commented Apr 22, 2024

I'm starting to get a feel for the power of this tool! For example:

make CLANG_TIDY="clang-tidy --checks='-*,modernize-use-equals-default' --fix"

Will apply a single class of fix across the entire codebase. And the results are consistent and work very well.

Same is not true for all checks but I think this is probably the way to go when we decide to apply these sorts of changes.

What I'm currently not seeing is an easy way to track changes made into submodules...

We should consider globally applying these fixes across the codebase (either in this PR or a separate one):

@mikee47 mikee47 force-pushed the feature/clang-tidy branch from 1ed7de3 to 96aeb44 Compare April 22, 2024 19:45
@mikee47 mikee47 changed the title [WIP] Add clang-tidy support for static code analysis Add clang-tidy support for static code analysis Apr 22, 2024
@mikee47 mikee47 requested a review from slaff April 22, 2024 19:46
@slaff slaff merged commit 75a7481 into SmingHub:develop Apr 23, 2024
46 checks passed
@mikee47 mikee47 deleted the feature/clang-tidy branch April 23, 2024 09:42
@slaff slaff mentioned this pull request May 8, 2024
5 tasks
slaff pushed a commit that referenced this pull request Jun 12, 2024
This PR adds the `CLANG_BUILD` for host builds. The toolchain detection logic is in the main build.mk file as there might be future support for clang toolchains for actual devices. It's also a revision to the existing logic which checks GCC compiler version.

Try it out using `make SMING_SOC=host CLANG_BUILD=1`.
To build with a specific (installed) version of clang, for example clang-15, use `CLANG_BUILD=15` .
Further customisation can be made by editing `Sming/Arch/Host/build.mk`.

Clang-tidy support (#2648) is also improved as there are some compiler flag differences between GCC and clang which are now shared between CLANG_TIDY and CLANG_BUILD operation.

An extra CI build has been added using clang.

Further to #2773, the default toolchain for macos is a version of clang (Apple Clang). This PR doesn't quite support that because there are other issues to address, but it's a step in the right direction.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants