Selecting the right approach for a clang based tool

I’m looking to make a tool based on clang and I’m having a little trouble understanding which interface / extensibility approach to use.

I’d like to scan the AST of a program and look for incorrect usage of a specific object/API. An oversimplified example is looking for local variables of a specific type, looking to see which methods on that object are called, and issuing a warning when certain patterns are noticed.

The only other requirement is that it must run on Windows.

In an ideal world I’d be able to hook this up to run automatically during any build, issue custom errors, and perform automatic fix-up, but those are all optional.

From what I can tell there are multiple approaches I could take:

  • LibClang
  • Clang Plugin
  • LibTooling
  • clang-check
  • clang-format
  • clang-tidy

LibClang is the only one I’ve used before. In this case, I know the abstracted view of the AST in LibClang is missing information I need. I could add the missing information to LibClang, but it’s an unknown amount of work and I don’t want to require a custom version of clang while merging changes upstream or force us onto a newer clang version. So I can rule out LibClang.

I assume clang-format does support the level of extensibility required here.

That still leaves 4 options. Which should I be using for this type of thing?

This sounds like a good fit for clang-tidy. Last CppCon there was a very good presentation to get started with that: https://youtu.be/torqlZnu9Ag?si=c84hUCDXdsE_COpn

If you want to see some custom clang-tidy plugins checking for custom APIs, see CMake’s set of plugins here: Utilities/ClangTidyModule · master · CMake / CMake · GitLab

It looks like extending clang-tidy requires building it locally (as opposed to building a DLL that vanilla clang-tidy loads or something along those lines).

It’s also executed separately from normal compilation.

Neither of those are deal-breakers, but it does leave me wondering about alternatives. It seems like a clang plugin is implemented as a dll that is loaded by clang. Is a clang plugin also suitable for this use case? It seems like it will be able to do what I want, but so far it’s unclear if clang plugins work on Windows.