2

I have a Lib and multiple Applications. I want to gather usage statistics about function calls to Lib's API from Apps. Basically my current process is:

  • Parse the Lib for all functions/methods
  • Output the info in a formatted way
  • Use that output to gather stats from the Apps.

I'm currently using the libclang API that offers cross-referencing in the form of USRs so I'm building an index of functions and use that when parsing the Apps. The problem is that this API is limited and that's why I want to move to Libtooling.

I've looked through libtooling's API but I haven't been able to find anything similar. So my question is what would be the best way to achieve that "cross-referencing" using Libtooling.

An example would be:

> lib.h

 class Foo {
 public:
       void bar();
 };


> app.cpp

#include "lib.h"

int main(void) {
    Foo f;
    f.bar();
    return 0;
}

And expected output would be a json file:

{
  "name": "bar",
  "location": { "file": "lib.h", "line": 5},
  "references": [{"file": "app.cpp", "line": 8}]
}

1 Answers1

0

If I understand, you are using clang_getCursorUSR from libclang (the C API) and want an equivalent for libtooling (the C++ API).

clang_getCursorUSR is defined in CIndexUSRs.cpp. For Decl objects (which is probably the main use case), it calls clang::index::generateUSRForDecl.

So to do likewise, assuming you have a clang::Decl *decl from somewhere, do something like:

  llvm::SmallVector<char, 80> vec;
  clang::index::generateUSRForDecl(decl, vec);
  string usr(vec.data(), vec.size());
  cout << "USR: " << usr << "\n";

The resulting USR string is claimed to be stable across translation units. It appears to be something like a portable version of a mangled name. An example is c:@S@ID@F@ID#&1$@S@ID#, which is the copy constructor for a class called ID.

Scott McPeak
  • 8,803
  • 2
  • 40
  • 79