[cfe-dev] Late-parsing function templates in tools

Kim Gräsman via cfe-dev cfe-dev at lists.llvm.org
Thu Jun 29 07:42:02 PDT 2017


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

[1] http://clang-developers.42468.n3.nabble.com/Tooling-vs-fdelayed-template-parsing-WAS-Clang-cl-exe-and-the-VC-preprocessor-td4040899.html
[2] https://github.com/include-what-you-use/include-what-you-use/blob/master/iwyu.cc#L3530
[3] https://reviews.llvm.org/D31697



More information about the cfe-dev mailing list