Generating USRs with clang C++ API

Viewed 95

I'm trying to use clang C++ API to do some code analysis, and I found that sometimes clang::index::generateUSRForDecl crashes.

For instance, given this code:

template<typename T>
struct foo { enum { val = T::val }; };

generating the USR for the DeclRefExpr-referred declaration representing T::val will crash (looks like on access to the non-existent AST context).

Similarly, given (a somewhat more sophisticated) example

struct base 
{ 
   template<typename T> 
   static int helper(const T&); 

   static double helper(...); 
}; 

template<typename T> 
struct derived : base 
{ 
   using type = decltype(helper(T{})); 
};

trying to generate the USR for the DeclRefExpr-referred declaration representing the helper call on the next-to-last line will also crash.

Both of these kind of make sense to me: there is no meaningful entity to refer to until the template in question is instantiated, but I was just curious if there is a sufficiently reasonable precondition that I could check in my code to avoid trying to generate USRs for entities will not work (in this case, crashing libclang)?

0 Answers
Related