bazel-compile-commands-extractor icon indicating copy to clipboard operation
bazel-compile-commands-extractor copied to clipboard

[Self-Filed] [Feature] Running clang-tidy from the command line

Open cpsauer opened this issue 3 years ago • 3 comments
trafficstars

Hey all! Self-filing an issue to collect interest and feedback.

Some people have been using this tool and compile_commands.json to run clang-tidy from the command line. [Clang-tidy can also run as part of clangd while editing.] Sounds like other tools have some issues around headers, which we can solve.

Doing this well would probably involve creating a target that builds a script that runs a bazel-cached clang-tidy aspect over the codebase.

LMK if interested. Would love some help on this one. I do think it'd be much easier to build well within the context of this tool than standalone.

cpsauer avatar May 24 '22 00:05 cpsauer

An issue that I have with the solution I specified in this comment is that clang-tidy runs sequentially for each code, and It has no caching. So it is very slow for a large project.

So, I guess a Bazel-based solution would be useful here. How hard is it to make bazel-clang-tidy aware of this package? I think it is a matter of checking the existence of compile_command.json and using that.

aminya avatar Nov 03 '22 00:11 aminya

Right o. Yeah, I think the trickiness here is that you need aspects to get the caching, but you need to use aquery (not aspects) to get the right build command. [The old extra_action interface had both...but it's deprecated and was slow/had other issues.]

cpsauer avatar Nov 03 '22 01:11 cpsauer

My quick take on the best way to break down each part:

  1. Dump a compile_commands.json using this tool, presumably with exclude_headers = "all", exclude_external_sources = True. It should be very fast with those settings, so we can probably afford to do it every time.
  2. Add caching to the clang-tidy run, having it reference compile_commands.json. i. It might be easier to just cache with a third party solution like this one or (likely even better) this one, rather than wrangling with bazel, but I'm not sure. Maybe better to do with Bazel. ii. If we did want to do it with Bazel, I think the move would be to do the following: Have an aspect, like the other project, but that depends on the compile_commands.json. It would then have a first set of actions that filters the compile_commands.json down to just the entries for each file--probably sorting them for stability. And then a second set of actions that runs clang_tidy on the files with the commands for that file as a side input. The first action could run really fast--and the second slow one have a high cache hit rate. (We'd need to be careful here about headers and header discovery.)

I probably shouldn't take the lead on this one right now, given everything else going on. But if you take a shot, I'd be down to help!

In short: Would recommend using this tool with exclude_headers = "all", exclude_external_sources = True to generate compile_commands.json. Then experimenting first with piping that into https://github.com/Ericsson/codechecker. If that works well, let's document it so everyone else can benefit, too!

cpsauer avatar Nov 03 '22 01:11 cpsauer