[cfe-dev] Local typedefs in templates

Johannes Altmanninger via cfe-dev cfe-dev at lists.llvm.org
Wed Apr 12 00:29:57 PDT 2017


Hi,

I have been working on fixing [1]. To me it is not obvious what is the
proper solution is. Maybe someone has an idea?

Consider this small test program:

template <class T>
void Function(T) {
  struct Struct {
    typedef int Something;
    Something x;
  };
}
void test() { Function('c'); }

Compiling it with -Wall -fsyntax-only generates the warning about the
local typedef being unused.

When adding -ast-dump one can see that there are two TypedefDecls, one
for the FunctionTemplateDecl and another for the one instantiation of
the template. The same thing applies when the typedef is dependent on a
template parameter, say "typedef T Something" in this case.

I observed this behaviour:

When the typedef is independent, then neither of the TypedefDecls will
be marked referenced as of now. Additionally, the type of ValueDecls
like "Something x;" has the non-templated typedef as its declaration.

When the typedef is dependent, then only the non-templated TypedefDecl
will be marked referenced. The type of ValueDecls does naturally refer
to the instantiated TypedefDecl.

Is it supposed to be that way?

If the types of declarations always referred to the instantiated
typedefs, it would be straightforward to fix by traversing all
declarations and marking the typedefs of their types referenced.

Alternatively, it would be possible to only check non-instantiated
types on being referenced. Actually, this might to be the simplest one..


Johannes


[1] https://bugs.llvm.org/show_bug.cgi?id=24883



More information about the cfe-dev mailing list