[clang] [analyzer][NFC] Introduce framework for checker families (PR #139256)

DonĂ¡t Nagy via cfe-commits cfe-commits at lists.llvm.org
Thu May 15 08:27:54 PDT 2025


NagyDonat wrote:

I prototyped a "get the name of template argument as string" solution which is sufficient for our use cases (where the checker family is a non-templated class type) and works on all supported versions of all three compilers (GCC, clang, MSVC) that are supported for LLVM compilation:
https://godbolt.org/z/8b9YGfvdG

```c++
#include <iostream>
#include <string>

class MyFancyCheckerFamily {};

template <class T>
std::string getTypeName() {
#ifndef _MSC_VER
    std::string res = __PRETTY_FUNCTION__;
    int start = res.find('='), end = res.find(';');
    if (end == -1)
        end = res.find(']');
    if (start == -1 || end == -1)
        return "unknown-checker-family"; // paranoia, should not happen
    return res.substr(start + 2, end - start - 2);
#else
    std::string res = __FUNCSIG__;
    int start = res.find("<class "), end = res.find('>');
    if (start == -1 || end == -1)
        return "unknown-checker-family"; // paranoia, should not happen
    return res.substr(start + 7, end - start - 7);
#endif
}

int main() {
  std::cout << getTypeName<MyFancyCheckerFamily>() << std::endl;
  return 0;
}
```
If I invoke this machinery from the registration functions of the checker families (where each checker type is available as a template argument), then it can "magically" ensure that `getTagDescription()` (i.e. the debug name) returns the class name of the checker family as a string (and there is nothing to do within the implementation of the checker family).

This is a bit ugly, because there is no standard way to stringify a type, so I need to rely on the macros `__PRETTY_FUNCTION__` (GCC and clang with slightly different content) / `__FUNCSIG__` (MSVC), but I don't think that there is a cleaner solution that doesn't burden the classes derived from `CheckerFamily` with any boilerplate (like overriding `getTagDescription()`).

@steakhal (or anybody else) What do you think about this?

https://github.com/llvm/llvm-project/pull/139256


More information about the cfe-commits mailing list