[cfe-dev] Not Unique USR of Template Class Type Param Using libclang

Huang Yaolong airekans at gmail.com
Thu Aug 22 00:35:15 PDT 2013


Hi, all.

I'm trying to index C++ source code into DB using libclang's cursor
facility.
One of the key things is that I can use USR to get the indexed cursor from
DB.
But when I tried to index a source file with template class definition with
macro, the USR for all template type parameter are the same:

// test_template.cpp
#define TEMP_CLASS(name) \
    template<typename T1, typename T2> \
    class name {  };

#define TWO_TEMP_CLASS(name) \
    TEMP_CLASS(name##1) \
    TEMP_CLASS(name##2)

TWO_TEMP_CLASS(Test)

void foo() {}


Note that for T1, T2 of class Test1 and Test2, the USR are
"test_template.cpp at 172". So with this USR, I cannot distinguish between
Test1::T1 and Test2::T2. The locations, extents, spelling and USR of
Test1::T1 and Test2::T2.

Is this a bug?

I check the source code of "clang/lib/Index/USRGeneration.cpp", it just use
GenLoc function to generate the USR of template type parameter:

void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D)
{
  GenLoc(D);
  return;
}

And non type template parameter also have this problem.
I've fixed this with a custom patch:

void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D)
{
  VisitDeclContext(D->getDeclContext());
  Out << "@";
  if (!EmitDeclName(D)) {
    Out << "@";
  }
  GenLoc(D);
  return;
}

With the class USR and the spelling before the GenLoc, the USR is
guaranteed to be unique.
Am I missing anything?
And the USR generation rule is not so clear to me. Is there any guildline
about how to generate it?

----
Best regards,
Yaolong Huang(Curtis)

Briontech China

Blog: http://airekans.github.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130822/799ec837/attachment.html>


More information about the cfe-dev mailing list