[cfe-dev] some notes about templates

Nick Lewycky nlewycky at google.com
Fri Jun 28 17:07:47 PDT 2013


A while ago, Richard Smith and I dug into a couple bugs in the general
vicinity of templates, and came up with some ideas for changes to clang
we'd like to see. As is, neither of us are presently working on any of
this, but we though we should write them up in a place where they're
publicly readable.

ActiveTemplateInstantiations shouldn't be a SmallVector in Sema, they
should be allocated by a BumpPtrAllocator in ASTContext. Then we can put
ActiveTemplateInstantions in the AST, which will requiring changing it a
bit (no pointers to sema:: objects), but we also want to add a template
depth, correct point-of-instantiation SourceLocation and add missing
instantiation records (such as instantiation due to overload resolution).
An awesome outcome of this will be that we'll be able to produce template
stack traces even when we aren't actively doing template instantiation.
That would in turn allow us to do other things like flatten instantiation
instead of making it recursive.

Notice that I mentioned correct point-of-instantiations? It turns out we
get them wrong in many cases, but nothing notices. We started to notice
when we added a new warning that tries to (efficiently!) enforce the rule
that a template must be the same (ie., name lookup would return the same
result) at each possible point of instantiation. If we store the point of
instantiation at the earliest possible PoI, then instantiation as much as
we can as late as we can, we can check that all the declarations found
through name lookup are before the PoI. The attached patch implements this,
but unfortunately it isn't useful now because the PoI is so often wrong.

There is also a cluster of small issues with templates declared inside
functions. We can remove PendingLocalImplicitInstantiations because we
needn't defer instantiations inside a function. When the function is itself
a template, we need to make sure that we recursively instantiate everything
lexically inside of it (excluding other templates which can themselves be
instantiated when needed), and currently our visitor gets confused by this
and misses some cases.

Nick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130628/87c2faf1/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: warn-point-of-instantiation-differences.diff
Type: application/octet-stream
Size: 3010 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130628/87c2faf1/attachment.obj>


More information about the cfe-dev mailing list