Skip to content

Add support for class templates #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

jbcoe
Copy link
Owner

@jbcoe jbcoe commented Jun 7, 2023

Currently fails to correctly get class template argument count

Currently fails to correctly get class template argument count
@jbcoe
Copy link
Owner Author

jbcoe commented Jun 7, 2023

cursor.get_num_template_arguments() is currently returning -1 which looks like a bug, maybe in libclang or cindex.py.

Maybe we can try building libclang locally to see what's going wrong?

https://clang.llvm.org/get_started.html has instructions.

@jbcoe
Copy link
Owner Author

jbcoe commented Jun 7, 2023

clang has

int clang_Cursor_getNumTemplateArguments(CXCursor C) {
  CXCursorKind kind = clang_getCursorKind(C);
  if (kind != CXCursor_FunctionDecl && kind != CXCursor_StructDecl &&
      kind != CXCursor_ClassDecl &&
      kind != CXCursor_ClassTemplatePartialSpecialization) {
    return -1;
  }

  if (const auto *FD =
          llvm::dyn_cast_if_present<clang::FunctionDecl>(getCursorDecl(C))) {
    const FunctionTemplateSpecializationInfo *SpecInfo =
        FD->getTemplateSpecializationInfo();
    if (!SpecInfo) {
      return -1;
    }
    return SpecInfo->TemplateArguments->size();
  }
    
  if (const auto *SD =
          llvm::dyn_cast_if_present<clang::ClassTemplateSpecializationDecl>(
              getCursorDecl(C))) {
    return SD->getTemplateArgs().size();
  }
    
  return -1;
} 

which won't work for CXCursor_ClassTemplate which is what our Python code picks up.

I'm not sure what we should do differently.

@jbcoe
Copy link
Owner Author

jbcoe commented Jun 7, 2023

This clang-commit looks relevant: https://reviews.llvm.org/D5621

@jbcoe
Copy link
Owner Author

jbcoe commented Jun 7, 2023

The issue here is the distinction between template arguments and template parameters.

libclang's docs in clang/include/clang-c/Index.h say

/**
 * Returns the number of template args of a function, struct, or class decl
 * representing a template specialization.
 *
 * If the argument cursor cannot be converted into a template function
 * declaration, -1 is returned.
 *
 * For example, for the following declaration and specialization:
 *   template <typename T, int kInt, bool kBool>
 *   void foo() { ... }
 *
 *   template <>
 *   void foo<float, -7, true>();
 *
 * The value 3 would be returned from this call.
 */
CINDEX_LINKAGE int clang_Cursor_getNumTemplateArguments(CXCursor C);

I think we need to add and expose clang_Cursor_getNumTemplateParameters to access parameters from py_cppmodel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant