我正在尝试从函数定义中提取函数声明,为此,我需要从原始函数定义中实际指定的函数调用约定,当前我正在使用类似这样的东西:
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);
}
现在在__stdcall之类的调用约定将在x64(clang_getFunctionTypeCallingConv(type)== 1)上被忽略,但是无论结构如何,我都希望得到它。