Issue 76037
Summary [Question] Get the number of template parameters of a un-speciliazed function template declaration
Labels new issue
Assignees
Reporter isVoid
    Hello LLVM community. Please point me to a different direction if this is not the right place for submitting a help request.

Recently I'm experimenting AST parsing with the clang-C API. Everything worked well so far until I'm starting to process template function arguments, the usual `clang_Cursor_getNumTemplateArguments` doesn't seem to perform what it's expected to do. Here's a min repro:

```c++
//template.hpp
template<typename T, int i, template<typename> class C>
void foo(T t, C<T> c) {
 c.push_back(i);
}
```

```c++
#include <clang-c/Index.h>

#include <iostream>
#include <string>

int main()
{
    // Create translation unit
    CXIndex index = clang_createIndex(0, 1);
    CXTranslationUnit unit = clang_createTranslationUnitFromSourceFile(
        index,
 "template.hpp",
        0,
        nullptr,
        0,
 nullptr);

    // Get cursor
    CXCursor cursor = clang_getTranslationUnitCursor(unit);

    // Traverse
 clang_visitChildren(
        cursor,
        [](CXCursor c, CXCursor parent, CXClientData client_data) {
            CXCursorKind kind = clang_getCursorKind(c);
            CXString name = clang_getCursorSpelling(c);
            if (std::string(clang_getCString(name)) == "foo")
            {
 int num_params = clang_Cursor_getNumArguments(c);
 int num_template_params = clang_Cursor_getNumTemplateArguments(c);
 printf("%s %s %dargs %dtempargs\n",
 clang_getCString(name),
 clang_getCString(clang_getCursorKindSpelling(kind)),
 num_params,
                    num_template_params
 );
                clang_disposeString(name);
                return CXChildVisit_Break;
            }
            
            return CXChildVisit_Recurse;
        },
 nullptr);
}
```

What I expected to print:
```
foo FunctionTemplate 2args 3tempargs
```
Why I think this way: 2 args are the function parameters (`t` and `c`). 3 tempargs are the 3 template parameters `T, i, C`.

What I got:
```
foo FunctionTemplate -1args -1tempargs
```

Would appreciate any reply!
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to