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

Custom clang tidy plugin #30886

Merged
merged 28 commits into from
Jun 20, 2019
Merged

Conversation

jbytheway
Copy link
Contributor

@jbytheway jbytheway commented May 26, 2019

Summary

SUMMARY: Infrastructure "Use custom clang-tidy plugin for Cata-specific code checks"

Purpose of change

To permit us to enforce custom rules of style or convention via our own clang-tidy checks.

Describe the solution

I thought this would be easy, but it is rather convoluted.

clang-tidy does not have native plugin support. The approach recommended by the clang devs is to build an entire clang-tidy with your custom checks embedded. This does not fit our use case well.

Therefore, I forked clang-tidy and have published a version there which does have plugin support.

There are two ways this can be used:

  • Download a released binary from my fork project. Because of ABI instability within LLVM & clang, this published version probably only works on Xenial with a specific LLVM version installed. Luckily, that's easy to keep stable because we're running on Travis; it's primarily for Travis's benefit that this exists.
  • Build your own patched clang-tidy and point Cataclysm at is. I have this working and have documented how to do it (see changes to DEVELOPER_TOOLING.md in this PR).

From Cata's perspective the way this works is

  • Download the patched clang-tidy (this is an executable and some headers).
  • Compile our custom checks plugin against those headers.
  • Use LD_PRELOAD to inject our plugin into the clang-tidy run.
  • Enable the cata- checks like any other clang-tidy checks.

So far I've just written one simple check that searches for variables declared long, because we want to get rid of those (for the time being I haven't got rid of them, because I want to verify the check can find them).

Still to do:

Describe alternatives you've considered

My patched clang-tidy does actually support a -plugins option, so you don't have to LD_PRELOAD, but for people running with their own clang-tidy builds, it's easier to get the LD_PRELOAD version working, so that's what I've gone with. However, to run the tests of the plugin does require the -plugins option.

Additional context

I might easily be taking this too far. This is adding a new dependency to Cata's Travis builds, which we might not want. On the other hand, nothing really terrible happens if it breaks; we just don't get our custom checks any more.

On the plus side, if we find this does work well, we can use it as evidence to help persuade the clang devs to include plugin support as standard. That would of course make it all much simpler.

@jbytheway jbytheway force-pushed the custom_clang_tidy branch 22 times, most recently from 779e6db to 0ebc695 Compare May 27, 2019 16:03
@ZhilkinSerg ZhilkinSerg added [C++] Changes (can be) made in C++. Previously named `Code` Code: Build Issues regarding different builds and build environments Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style labels May 27, 2019
@ZhilkinSerg ZhilkinSerg self-assigned this May 27, 2019
@ZhilkinSerg ZhilkinSerg removed their assignment Jun 7, 2019
@jbytheway jbytheway force-pushed the custom_clang_tidy branch from 3b00cc0 to 98150c4 Compare June 8, 2019 11:55
@jbytheway jbytheway force-pushed the custom_clang_tidy branch from 98150c4 to af585f4 Compare June 19, 2019 06:51
jbytheway added 25 commits June 19, 2019 10:31
This was previously working for my local LLVM build.  Now I want it to
work on Travis, so I have prepared a release of clang-tidy that's
suitable to run there, and set it up to work with either version.
To find non-standard check_clang_tidy.py, was previously using an
environment variable.  Switch to a CMake option instead, so that it's
not necessary to set the env var wherever you run the plugins tests.
Previously we would not flag uses of long if cv-qualified or a reference
type.  Now we will.
When variables had a type which was some template parameter which
happened to be long, this caused an error to be reported.  We don't
actually care about such cases; instead we would prefer the error to
appear at the point the template argument is specified.
Should not be using LONG_MIN et al if we're not using long variables.
The check was reporting errors for functions returning long when that
was a template parameter.  We shouldn't complain in this case.
We were reporting problems with deduced long return types (e.g. on
lambdas) where we shouldn't.
Auto types which happen to be long shouldn't trigger a warning.
The analysis runs on compiler-generated member functions.  We need to
ignore variables in those.
When an instantiation of a function template returns long, we don't want
to report that; if it was written as long it will have already been
reported on the template.
size_t is fairly often what people really want when using unsigned int.
These had snuck in while I was getting all my other "removing long" PRs
merged.
@jbytheway jbytheway force-pushed the custom_clang_tidy branch from 66ad683 to 2d801ea Compare June 19, 2019 09:32
@jbytheway jbytheway changed the title [WIP][CR] Custom clang tidy plugin Custom clang tidy plugin Jun 19, 2019
@jbytheway
Copy link
Contributor Author

jbytheway commented Jun 19, 2019

OK, after being side-tracked for a while eliminating long, I believe this is now finally ready.

(Gorgon unhappy due to Makefile changes)

@kevingranade kevingranade merged commit 9e3ade5 into CleverRaven:master Jun 20, 2019
@jbytheway jbytheway deleted the custom_clang_tidy branch June 21, 2019 06:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[C++] Changes (can be) made in C++. Previously named `Code` Code: Build Issues regarding different builds and build environments Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants