standardese / cppast

Library to parse and work with the C++ AST
Other
1.7k stars 165 forks source link

Error parsing nested class template #166

Open deadlocklogic opened 1 year ago

deadlocklogic commented 1 year ago

Explanation of the error.

Input:

namespace ns {
  template<typename T1>
  struct Test1 {
    template<typename T2>
    struct Test2 {
    };
  };
}

template <typename T1, typename T2>
void test(typename ns::Test1<T1>::template Test2<T2> s)
{
}

Output:

[libclang parser] [error] test.h:16: unable to find end of function prefix
deadlocklogic commented 1 year ago

I think the library by design don't expect nested templates because consider building a cpp_template_instantiation_type you need a cpp_template_ref which most of the time is build manually but in the case of nested templates I don't exactly know what should the arguments be. Maybe each indexed entity should return its own basic_cpp_entity_ref so in worst case an AST visit could eventually find the desired value.

deadlocklogic commented 1 year ago

After little digging, I found the offending line: https://github.com/foonathan/cppast/blob/f00df6675d87c6983033d270728c57a55cd3db22/src/libclang/cxtokenizer.cpp#L282 The function test is a definition but clang_isCursorDefinition is returning 0. I am not sure if it is a clang bug, need further investigation.

Update:

After trying with cindex python I noticed the same bug. Interestingly, if I make the function constexpr the bug disappears and now is treated as a definition (as it should). So I don't know what to do here or if there is a workaround (maybe submit an llvm ticket?). Thanks.