Late-parsing function templates in tools

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Late-parsing function templates in tools

Simon Dardis via cfe-dev
Hello all,

In include-what-you-use, we need to parse and AST-visit templates
whether they're instantiated or not, in order to record all uses. This
is somewhat at odds with `-fdelayed-template-parsing` for MSVC
compatibility. Uninstantiated function templates are never visited in
this mode. So we have a hack, suggested here [1] that explicitly
parses these function templates, implemented here [2].

This works surprisingly well, but we've run into a corner case
triggered by llvm/include/llvm/Support/MathExtras.h. When run under
include-what-you-use on Windows, the GNU builtins are still available,
so we end up trying to parse the equivalent of the following code:

    template <class T>
    int myctz(unsigned int value) {
     return __builtin_ctz(value);

Essentially a GNU builtin called from a function template. This causes
the call to LateTemplateParser to crash because parsing the builtin
needs the TUScope member to be non-null. More details in a patch by
@zjturner here [3].

Some open questions borrowed from that review:

Can we seed Sema with a valid `TUScope` before invoking
`LateTemplateParser`, and if so, how?

Or is this because we invoke the parser multiple times? Can we avoid
that somehow?

For us, it sure would be nice if Clang had a more proofed API for
doing this somewhere, but I'm not sure where it belongs or how it
might be implemented.

Many thanks for any input,
- Kim

cfe-dev mailing list
[hidden email]