Broken name range for function template specialization (and a related question)

Abramo Bagnara abramo.bagnara at bugseng.com
Tue Jul 15 03:58:26 PDT 2014


Tracking down a failure in one of our applications I've obtained the
following testcase:

template<typename T>
int operator+(T&);

template<>
int operator+(class c&);

As far as I can see clang first produce an instantiation for function
template, then produces the explicit specialization as a redecl of the
former one.

The first decl is not attached to visitable AST, but only available as
redeclaration of explicit specialization (as its canonical decl).

In SemaTemplate.cpp near line 6706 we have:

  // Note: do not overwrite location info if previous template
  // specialization kind was explicit.
  TemplateSpecializationKind TSK =
SpecInfo->getTemplateSpecializationKind();
  if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) {
    Specialization->setLocation(FD->getLocation());
    // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr
    // function can differ from the template declaration with respect to
    // the constexpr specifier.
    Specialization->setConstexpr(FD->isConstexpr());
  }

The problem is that setLocation copy only a part of name info locations,
so the canonical decl ends up having a name range that starts from token
`operator' of specialization and ends at token `+' of template (i.e. a
negative/broken range).

Likely this is easily fixable copying both edges of range (I'm guessing,
please confirm that) but I'd like also to understand why we need to have
*two* declarations (with one of them not reachable with ordinary visits).

-- 
Abramo Bagnara

BUGSENG srl - http://bugseng.com
mailto:abramo.bagnara at bugseng.com



More information about the cfe-commits mailing list