[cfe-dev] Endless operator-> chain causing infinite loop

Rahul Jain 1989.rahuljain at gmail.com
Tue Oct 22 02:45:43 PDT 2013


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.

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20131022/e85dda69/attachment.html>


More information about the cfe-dev mailing list