[cfe-dev] [libclang] get actual specified calling convention from source

jagannatharjun via cfe-dev cfe-dev at lists.llvm.org
Mon Jan 14 10:07:25 PST 2019


I am trying to extract the function declaration from the function definition, for this, I need the actual specified function calling convention from the original function definition, current I am using something like this:
 string Convert(const CXString& s)
{
    string result = clang_getCString(s);
    clang_disposeString(s);
    return result;
}

void print_function_prototype(CXCursor cursor) {
  auto type = clang_getCursorType(cursor);

  auto function_name = Convert(clang_getCursorSpelling(cursor));
  auto return_type = Convert(clang_getTypeSpelling(clang_getResultType(type)));
  auto calling_convention = std::to_string(clang_getFunctionTypeCallingConv(type));

  std::cout << return_type << " " << calling_convention << " " << function_name
            << "(";

  int num_args = clang_Cursor_getNumArguments(cursor);
  for (int i = 0; i < num_args; ++i) {
    auto arg_cursor = clang_Cursor_getArgument(cursor, i);
    auto arg_name = Convert(clang_getCursorSpelling(arg_cursor));
    auto arg_data_type =
        Convert(clang_getTypeSpelling(clang_getArgType(type, i)));
    std::cout << (i > 0 ? "," : "") << arg_data_type << " " << arg_name;
  }

  std::cout << ");\n";
}

int main() {
CXIndex index = clang_createIndex(0, 0);
  const char *CompilerArgs[] = {"-IE:/Cpp/Projects/Headers", "-fms-extensions",
                                "-fms-compatibility"};
  auto CompilerArgsCount = sizeof(CompilerArgs) / sizeof(CompilerArgs[0]);
  CXTranslationUnit unit = clang_parseTranslationUnit(
      index, "test.cpp", CompilerArgs,
      CompilerArgsCount, nullptr, 0, CXTranslationUnit_None);
  if (unit == nullptr) {
    cerr << "Unable to parse translation unit. Quitting." << endl;
    exit(-1);
  }

  CXCursor cursor = clang_getTranslationUnitCursor(unit);
  clang_visitChildren(
      cursor,
      [](CXCursor c, CXCursor parent, CXClientData client_data) {
        if (clang_Location_isFromMainFile(clang_getCursorLocation(c)))
          if (clang_getCursorKind(c) == CXCursorKind::CXCursor_FunctionDecl) {
            print_function_prototype(c);
          }
        return CXChildVisit_Recurse;
      },
      nullptr);

  clang_disposeTranslationUnit(unit);
  clang_disposeIndex(index);
}
now calling convention like __stdcall will be ignored on x64(clang_getFunctionTypeCallingConv(type) == 1), but I want to get it irrespective of architecture.


Sent from Mail for Windows 10

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20190114/f8a22c2f/attachment.html>


More information about the cfe-dev mailing list