[cfe-dev] Endless operator-> chain causing infinite loop
Richard Smith
richard at metafoo.co.uk
Tue Oct 22 12:14:25 PDT 2013
On Tue, Oct 22, 2013 at 2:45 AM, Rahul Jain <1989.rahuljain at gmail.com>wrote:
>
> Hi all,
>
> clang version 3.4 (192772)
>
> This is with respect to the following gcc testsuite TC:
>
> template< int n >
> struct a {
> a< n+1 > operator->()
> {
> return a< n+1 >();
> }
> };
>
> int main() {
> a<0>()->x;
> }
>
>
> This TC goes into an infinite loop when compiled. Ideally it should throw
> the error
> recursive template instantiation exceeded maximum depth of 256.
>
That's not the right behavior; there's no recursive template instantiation
here. Each operator-> is instantiated from within the context of 'main',
not from within some other instantiation.
If we want to limit this, we should put a limit on the number of times we
go around the loop looking for an overloaded operator->
in Sema::ActOnStartCXXMemberReference. However, I've seen people use this
in practice in template metaprogramming to get around the recursive
template instantiation depth, so this might break existing code.
On initial investigation I found that when the constructor
> Sema::InstantiatingTemplate::
> InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
> Decl *Entity,
> SourceRange InstantiationRange);
>
> is run on each recursive object creation,
> the first thing the constructor does is check whether the recursive
> instantiation depth is reached or not by calling the function
>
> Invalid = CheckInstantiationDepth(PointOfInstantiation,
> InstantiationRange);
>
> The above function checks whether the size of
> SemaRef.ActiveTemplateInstantiations(a container which stacks all the
> template instantiations originating from a particular PointOfInstantiation)
> is within the limit as specified by templateinstantiationdepth(256 by
> default).
>
> So far, so good.
>
> Now when CheckInstantiationDepth returns false, the constructor pushes the
> current Inst using the following statement:
> SemaRef.ActiveTemplateInstantiations.push_back(Inst);
>
> Also the push_back function correctly increments the EndX value.
>
>
> So ideally the size of SemaRef.ActiveTemplateInstantiations should
> increase from 1 to 2 to 3 to .....256 and than the error should get printed.
>
> But, the EndX value which was incremented in the push_back function call
> is no longer reflected
> in the size computation done as part of the function call
> CheckInstantiationDepth(PointOfInstantiation,
> InstantiationRange);.
>
> i.e SemaRef.ActiveTemplateInstantiations.size() always returns zero in the
> function CheckInstantiationDepth.
>
> My question is where is the EndX value getting reset after it was rightly
> incremented in the push_back function call?
>
>
> Am I missing something in my analysis above? Any help on the same would be
> appreciated.
>
> Thanks,
> Rahul
>
>
>
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20131022/f4a9e81d/attachment.html>
More information about the cfe-dev
mailing list