<div dir="ltr">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.<div>


<br></div><div>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.</div>


<div><br></div><div>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.</div>

<div><br></div><div>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.</div>

<div><br></div><div>Nick</div><div><br></div></div>