C++: Limit template instantiation depth

A recursive template generates infinite expansions.

Consider the following example:

template <class R1>
struct Base
{
};

template<typename R>
struct Derived :
  Base<
    typename Derived<typename Base<R>::type>::type,
    typename Derived<typename Base<R>::type>::type
  >::type
{};

R is instantiated as Base<R>::type, which causes another
instantiation of R into Base<Base<R>> etc...

This is not a solution, but a workaround.

Task-number: QTCREATORBUG-15141
Change-Id: Ib04f70275e07919e2cb6c7fb61a2045bd52f4a7d
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@theqtcompany.com>
This commit is contained in:
Orgad Shaneh
2015-11-11 23:13:19 +02:00
committed by Orgad Shaneh
parent a27cd12538
commit 0bcddcd014
3 changed files with 28 additions and 0 deletions

View File

@@ -1549,6 +1549,7 @@ CreateBindings::CreateBindings(Document::Ptr thisDocument, const Snapshot &snaps
: _snapshot(snapshot)
, _control(QSharedPointer<Control>(new Control))
, _expandTemplates(false)
, _depth(0)
{
_globalNamespace = allocLookupScope(/*parent = */ 0, /*name = */ 0);
_currentLookupScope = _globalNamespace;
@@ -1978,8 +1979,13 @@ void CreateBindings::initializeSubst(Clone &cloner,
{
const unsigned argumentCountOfSpecialization = specialization->templateParameterCount();
if (_depth > 15)
return;
++_depth;
for (unsigned i = 0; i < argumentCountOfSpecialization; ++i)
resolveTemplateArgument(cloner, subst, origin, specialization, instantiation, i);
--_depth;
}
} // namespace CPlusPlus

View File

@@ -209,6 +209,7 @@ private:
LookupScope *_globalNamespace;
LookupScope *_currentLookupScope;
bool _expandTemplates;
int _depth;
};
class CPLUSPLUS_EXPORT LookupContext