[cfe-dev] AST template definition VS. template instantiations

Douglas Gregor dgregor at apple.com
Wed Aug 25 20:56:12 PDT 2010


On Aug 25, 2010, at 1:21 PM, Benoit Belley wrote:

> Hi Everyone,
> 
> While traversing the Clang AST in a custom static analyzer check (using the RecursiveASTVisitor), I need to determine whether the given function is a template definition because it is part of a template class, template function or a partial specialization of a class template. The reason I need to know this is that template definitions are not fully semantically checked, while template instantiations are. For example, the operator new() referenced by a CXXNewExpr is unresolved in a template definition.

To be super-precise, the operator new() referenced by a CXXNewExpr *might* be unresolved in a template definition. It depends on whether the types and arguments involved are dependent.

> Things are not so clear cut because classes and functions can be nested, and therefore one has to look at all of the enclosing scopes to determine if a given FunctionDecl is actually part of a template.
> 
> I have come out with the following function to perform such a check and I am wondering if there is an easier way to achieve this:
> 
> // Recursively examines the parent contexts to determine if any of them
> // represents either a function or a class template.
> static bool isTemplatedContext(DeclContext* ctx) 
> {
> [snip code]
> }

This is mostly correct, but you don't need it: DeclContext::isDependentContext() does exactly what you need.

	- Doug



More information about the cfe-dev mailing list