[cfe-dev] Type source info for function template instances.

John McCall rjmccall at apple.com
Fri Mar 5 12:44:37 PST 2010


On Mar 5, 2010, at 12:23 PM, Enea Zaffanella wrote:

> John McCall wrote:
>> Can you not "reattach" default argument expressions during
>> TemplateDeclInstantiator::SubstFunctionType()?  Or even better,
>> transform them appropriately in TreeTransform.  I would be much more
>> comfortable doing either of these than suppressing diagnostics
>> assumed to be redundant.
>> John.
> 
> Hello John.
> 
> Please find attached a revised patch.
> 
> In this version, we no longer visit parameters twice,
> so we no longer need to suppress diagnostics.
> 
> We (re-)discovered that we are NOT allowed to substitute the default argument expressions when instantiating the function decl, because this could eagerly generate diagnostics that should only be produced if and when the default argument is actually used.
> 
> We added a new helper named
>   SubstFunctionTypeSourceInfo
> which mimics the behavior of the existing
>   SubstFunctionType
> but produces a TypeSourceInfo instead of a QualType.
> We factored the common code that checks parameters in
>   CheckInstantiatedParams.
> 
> 
> The patch passes all but a single test:
> 
> Failing Tests (1):
>    Clang :: SemaTemplate/instantiate-expr-2.cpp
> 
> An assertion is failing:
> 
> clang: Sema.h:3416: clang::Decl* clang::Sema::LocalInstantiationScope::getInstantiationOf(const clang::Decl*): Assertion `Result && "declaration was not instantiated in this scope!"' failed.
> 
> The relevant code in the example is the following:
> ================
> template<typename T, unsigned long N> struct IntegralConstant { };
> 
> template<typename T>
> struct X0 {
>  void f(T x, IntegralConstant<T, sizeof(x)>);
> };
> 
> void test_X0(X0<int> x, IntegralConstant<int, sizeof(int)> ic) {
>  x.f(5,ic);
> }
> ================
> 
> The assertion crash disappears if you replace sizeof(x) with sizeof(T) in the declaration of method X0<T>::f.
> 
> Could this be an already existing, but hidden bug that was uncovered by our patch?

Hmm, yes.  Test case which crashes on ToT (doesn't work in gcc at all, which doesn't do parameter scopes properly):

  template <class T> class Foo {
    void (*ptr)(T x, char (&array)[sizeof(x)]);
  };
  template class Foo<int>;

We need to override TreeTransform::TransformFunctionProtoType in TemplateInstantiator;  it can just call your new method.

John.



More information about the cfe-dev mailing list