Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romix/13db5f4c85b1b20d5ec8546c3a057dc7 to your computer and use it in GitHub Desktop.
Save romix/13db5f4c85b1b20d5ec8546c3a057dc7 to your computer and use it in GitHub Desktop.
/// Build a function signature.
static std::string get_function_signature(IndexFile* db,
const CXIdxDeclInfo* decl) {
// Build the function name, with scope and parameters
std::string returnType;
if (decl->entityInfo->kind != CXCursor_Constructor &&
decl->entityInfo->kind != CXCursor_Destructor) {
returnType = ::ToString(
clang_getTypeSpelling(clang_getCursorResultType(decl->cursor)));
}
std::string signature{returnType};
signature.append(" ");
auto container = decl->semanticContainer;
if (IsTypeDefinition(container)) {
IndexTypeId declaring_type_id = db->ToTypeId(container->cursor);
IndexType* declaring_type_def = db->Resolve(declaring_type_id);
auto container_name = declaring_type_def->def.detailed_name;
signature.append(container_name);
signature.append("::");
}
auto functionName = ::ToString(clang_getCursorSpelling(decl->cursor));
auto pos = functionName.find("<", 0);
if (pos != std::string::npos) {
functionName = functionName.substr(0, pos);
}
signature.append(functionName);
std::string parameter_types = "";
// Add the parameters declarations including their names.
parameter_types.append("(");
int numArgs = clang_Cursor_getNumArguments(decl->cursor);
for (int i = 0; i < numArgs; i++) {
CXCursor arg = clang_Cursor_getArgument(decl->cursor, i);
auto id = ::ToString(clang_getCursorDisplayName(arg));
auto type = ::ToString(clang_getTypeSpelling(clang_getCursorType(arg)));
parameter_types.append(type + ' ' + id + (i < numArgs - 1 ? ", " : ""));
}
if (clang_Cursor_isVariadic(decl->cursor)) {
parameter_types.append(numArgs > 0 ? ", ..." : "...");
}
parameter_types.append(")");
signature.append(parameter_types);
return signature;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment