Add additional metadata to function declarations.

This commit is contained in:
Jacob Dufault 2017-05-27 18:53:22 -07:00
parent 3469850c98
commit 1d67d1ee37
37 changed files with 277 additions and 71 deletions

View File

@ -163,12 +163,12 @@ bool CanBeCalledImplicitly(CXIdxEntityKind kind) {
bool CursorSpellingContainsString(CXCursor cursor, CXTranslationUnit cx_tu, std::string scanning_for) {
CXSourceRange range = clang_Cursor_getSpellingNameRange(cursor, 0, 0);
CXToken* tokens;
unsigned int num_tokens;
unsigned num_tokens;
clang_tokenize(cx_tu, range, &tokens, &num_tokens);
bool result = false;
for (size_t i = 0; i < num_tokens; ++i) {
for (unsigned i = 0; i < num_tokens; ++i) {
CXString name = clang_getTokenSpelling(cx_tu, tokens[i]);
if (strcmp(clang_getCString(name), scanning_for.c_str()) == 0) {
result = true;
@ -181,6 +181,47 @@ bool CursorSpellingContainsString(CXCursor cursor, CXTranslationUnit cx_tu, std:
return result;
}
// Returns the document content for the given range. May not work perfectly
// when there are tabs instead of spaces.
std::string GetDocumentContentInRange(CXTranslationUnit cx_tu, CXSourceRange range) {
std::string result;
CXToken* tokens;
unsigned num_tokens;
clang_tokenize(cx_tu, range, &tokens, &num_tokens);
optional<Range> previous_token_range;
for (unsigned i = 0; i < num_tokens; ++i) {
// Add whitespace between the previous token and this one.
Range token_range = Resolve(clang_getTokenExtent(cx_tu, tokens[i]));
if (previous_token_range) {
// Insert newlines.
int16_t line_delta = token_range.start.line - previous_token_range->end.line;
assert(line_delta >= 0);
if (line_delta > 0) {
result.append((size_t)line_delta, '\n');
// Reset column so we insert starting padding.
previous_token_range->end.column = 0;
}
// Insert spaces.
int16_t column_delta = token_range.start.column - previous_token_range->end.column;
assert(column_delta >= 0);
result.append((size_t)column_delta, ' ');
}
previous_token_range = token_range;
// Add token content.
CXString spelling = clang_getTokenSpelling(cx_tu, tokens[i]);
result += clang_getCString(spelling);
clang_disposeString(spelling);
}
clang_disposeTokens(cx_tu, tokens, num_tokens);
return result;
}
} // namespace
@ -983,6 +1024,7 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
case CXIdxEntity_CXXStaticMethod:
case CXIdxEntity_CXXConversionFunction: {
Range decl_spelling = ResolveSpelling(decl->cursor);
Range decl_extent = ResolveExtent(decl->cursor);
clang::Cursor decl_cursor = decl->cursor;
clang::Cursor decl_cursor_resolved = decl_cursor.template_specialization_to_template_definition();
@ -1005,27 +1047,34 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
assert(!func->def.definition_spelling);
assert(!func->def.definition_extent);
func->def.definition_spelling = decl_spelling;
func->def.definition_extent = ResolveExtent(decl->cursor);
func->def.definition_extent = decl_extent;
}
else {
IndexFunc::Declaration declaration;
declaration.spelling = decl_spelling;
declaration.extent = decl_extent;
declaration.content = GetDocumentContentInRange(param->tu->cx_tu, clang_getCursorExtent(decl->cursor));
/*
// Add parameters.
for (clang::Cursor arg : decl_cursor.get_arguments()) {
switch (arg.get_kind()) {
case CXCursor_ParmDecl: {
IndexFunc::DeclarationVariable decl_var;
decl_var.content = arg.get_display_name(); // FIXME/TODO: scan actual tokens.
decl_var.spelling = ResolveSpelling(arg.cx_cursor);
declaration.vars.push_back(decl_var);
Range param_spelling = ResolveSpelling(arg.cx_cursor);
// If the name is empty (which is common for parameters), clang
// will report a range with length 1, which is not correct.
if (param_spelling.start.column == (param_spelling.end.column - 1) &&
arg.get_display_name().empty()) {
param_spelling.end.column -= 1;
}
declaration.param_spellings.push_back(param_spelling);
break;
}
default:
break;
}
}
*/
func->declarations.push_back(declaration);
}

View File

@ -316,15 +316,15 @@ struct IndexFunc {
IndexFuncId id;
struct DeclarationVariable {
// Spelled name of the variable.
Range spelling;
// Full text (including type) of the variable.
std::string content;
};
struct Declaration {
// Range of only the function name.
Range spelling;
std::vector<DeclarationVariable> vars;
// Full range of the declaration.
Range extent;
// Full text of the declaration.
std::string content;
// Location of the parameter names.
std::vector<Range> param_spellings;
};
// Places the function is forward-declared.
@ -350,8 +350,7 @@ struct IndexFunc {
}
};
MAKE_HASHABLE(IndexFunc, t.def.usr);
MAKE_REFLECT_STRUCT(IndexFunc::DeclarationVariable, spelling, content);
MAKE_REFLECT_STRUCT(IndexFunc::Declaration, spelling, vars);
MAKE_REFLECT_STRUCT(IndexFunc::Declaration, spelling, extent, content, param_spellings);
template <typename TypeId,
typename FuncId,

View File

@ -109,9 +109,9 @@ void RunTests() {
//if (path != "tests/templates/namespace_template_class_template_func_usage_folded_into_one.cc") continue;
//if (path != "tests/multi_file/funky_enum.cc") continue;
//if (path != "tests/multi_file/simple_impl.cc") continue;
//if (path != "tests/usage/func_called_implicit_ctor.cc") continue;
//if (path != "tests/templates/implicit_variable_instantiation.cc") continue;
//if (path != "tests/multi_file/bad_type_remove.cc") continue;
//if (path != "tests/inheritance/interface_pure_virtual.cc") continue;
//if (path != "tests/_empty_test.cc") continue;
//if (path != "tests/declaration_vs_definition/func_associated_function_params.cc") continue;
//if (path != "tests/templates/template_class_type_usage_folded_into_one.cc") continue;
//path = "C:/Users/jacob/Desktop/superindex/indexer/" + path;

View File

@ -13,11 +13,17 @@ OUTPUT:
"short_name": "foo",
"detailed_name": "void foo()",
"declarations": [{
"spelling": "1:6-1:9"
"spelling": "1:6-1:9",
"extent": "1:1-1:11",
"content": "void foo()"
}, {
"spelling": "2:6-2:9"
"spelling": "2:6-2:9",
"extent": "2:1-2:11",
"content": "void foo()"
}, {
"spelling": "4:6-4:9"
"spelling": "4:6-4:9",
"extent": "4:1-4:11",
"content": "void foo()"
}],
"definition_spelling": "3:6-3:9",
"definition_extent": "3:1-3:14"

View File

@ -0,0 +1,54 @@
int foo(int, int);
int foo(int aa,
int bb);
int foo(int aaa, int bbb);
int foo(int a, int b) { return 0; }
/*
OUTPUT:
{
"funcs": [{
"id": 0,
"usr": "c:@F@foo#I#I#",
"short_name": "foo",
"detailed_name": "int foo(int, int)",
"declarations": [{
"spelling": "1:5-1:8",
"extent": "1:1-1:18",
"content": "int foo(int, int)",
"param_spellings": ["1:12-1:12", "1:17-1:17"]
}, {
"spelling": "2:5-2:8",
"extent": "2:1-3:16",
"content": "int foo(int aa,\n int bb)",
"param_spellings": ["2:13-2:15", "3:13-3:15"]
}, {
"spelling": "4:5-4:8",
"extent": "4:1-4:26",
"content": "int foo(int aaa, int bbb)",
"param_spellings": ["4:13-4:16", "4:22-4:25"]
}],
"definition_spelling": "5:5-5:8",
"definition_extent": "5:1-5:36"
}],
"vars": [{
"id": 0,
"usr": "c:func_associated_function_params.cc@91@F@foo#I#I#@a",
"short_name": "a",
"detailed_name": "int a",
"definition_spelling": "5:13-5:14",
"definition_extent": "5:9-5:14",
"is_local": true,
"uses": ["5:13-5:14"]
}, {
"id": 1,
"usr": "c:func_associated_function_params.cc@98@F@foo#I#I#@b",
"short_name": "b",
"detailed_name": "int b",
"definition_spelling": "5:20-5:21",
"definition_extent": "5:16-5:21",
"is_local": true,
"uses": ["5:20-5:21"]
}]
}
*/

View File

@ -25,7 +25,9 @@ OUTPUT:
"short_name": "declonly",
"detailed_name": "void Foo::declonly()",
"declarations": [{
"spelling": "2:8-2:16"
"spelling": "2:8-2:16",
"extent": "2:3-2:18",
"content": "void declonly()"
}],
"declaring_type": 0
}, {
@ -34,7 +36,9 @@ OUTPUT:
"short_name": "purevirtual",
"detailed_name": "void Foo::purevirtual()",
"declarations": [{
"spelling": "3:16-3:27"
"spelling": "3:16-3:27",
"extent": "3:3-3:33",
"content": "virtual void purevirtual() = 0"
}],
"declaring_type": 0
}, {
@ -43,7 +47,9 @@ OUTPUT:
"short_name": "def",
"detailed_name": "void Foo::def()",
"declarations": [{
"spelling": "4:8-4:11"
"spelling": "4:8-4:11",
"extent": "4:3-4:13",
"content": "void def()"
}],
"definition_spelling": "7:11-7:14",
"definition_extent": "7:1-7:19",

View File

@ -9,7 +9,10 @@ OUTPUT:
"short_name": "foo",
"detailed_name": "void foo(int, int)",
"declarations": [{
"spelling": "1:6-1:9"
"spelling": "1:6-1:9",
"extent": "1:1-1:23",
"content": "void foo(int a, int b)",
"param_spellings": ["1:14-1:15", "1:21-1:22"]
}]
}]
}

View File

@ -11,7 +11,9 @@ OUTPUT:
"short_name": "foo",
"detailed_name": "void foo()",
"declarations": [{
"spelling": "1:6-1:9"
"spelling": "1:6-1:9",
"extent": "1:1-1:11",
"content": "void foo()"
}],
"definition_spelling": "3:6-3:9",
"definition_extent": "3:1-3:14"

View File

@ -35,7 +35,9 @@ OUTPUT:
"short_name": "foo",
"detailed_name": "void Root::foo()",
"declarations": [{
"spelling": "2:16-2:19"
"spelling": "2:16-2:19",
"extent": "2:3-2:21",
"content": "virtual void foo()"
}],
"declaring_type": 0,
"derived": [1]

View File

@ -25,7 +25,9 @@ OUTPUT:
"short_name": "foo",
"detailed_name": "void Foo::foo()",
"declarations": [{
"spelling": "2:8-2:11"
"spelling": "2:8-2:11",
"extent": "2:3-2:13",
"content": "void foo()"
}],
"declaring_type": 0
}]

View File

@ -1,8 +1,8 @@
class Foo {
void foo();
void foo() const;
};
void Foo::foo() {}
void Foo::foo() const {}
/*
OUTPUT:
@ -19,14 +19,16 @@ OUTPUT:
}],
"funcs": [{
"id": 0,
"usr": "c:@S@Foo@F@foo#",
"usr": "c:@S@Foo@F@foo#1",
"short_name": "foo",
"detailed_name": "void Foo::foo()",
"detailed_name": "void Foo::foo() const",
"declarations": [{
"spelling": "2:8-2:11"
"spelling": "2:8-2:11",
"extent": "2:3-2:19",
"content": "void foo() const"
}],
"definition_spelling": "5:11-5:14",
"definition_extent": "5:1-5:19",
"definition_extent": "5:1-5:25",
"declaring_type": 0
}]
}

View File

@ -13,7 +13,9 @@ OUTPUT: simple_header.h
"short_name": "header",
"detailed_name": "void header()",
"declarations": [{
"spelling": "3:6-3:12"
"spelling": "3:6-3:12",
"extent": "3:1-3:14",
"content": "void header()"
}]
}]
}

View File

@ -21,7 +21,9 @@ OUTPUT: static.h
"short_name": "CreateSharedBuffer",
"detailed_name": "void Buffer::CreateSharedBuffer()",
"declarations": [{
"spelling": "4:15-4:33"
"spelling": "4:15-4:33",
"extent": "4:3-4:35",
"content": "static void CreateSharedBuffer()"
}],
"declaring_type": 0
}]

View File

@ -11,7 +11,9 @@ OUTPUT:
"short_name": "foo",
"detailed_name": "void ::foo()",
"declarations": [{
"spelling": "2:6-2:9"
"spelling": "2:6-2:9",
"extent": "2:1-2:11",
"content": "void foo()"
}]
}]
}

View File

@ -11,7 +11,10 @@ OUTPUT:
"short_name": "foo",
"detailed_name": "void hello::foo(int, int)",
"declarations": [{
"spelling": "2:6-2:9"
"spelling": "2:6-2:9",
"extent": "2:1-2:23",
"content": "void foo(int a, int b)",
"param_spellings": ["2:14-2:15", "2:21-2:22"]
}]
}]
}

View File

@ -23,7 +23,9 @@ OUTPUT:
"short_name": "foo",
"detailed_name": "void hello::Foo::foo()",
"declarations": [{
"spelling": "3:8-3:11"
"spelling": "3:8-3:11",
"extent": "3:3-3:13",
"content": "void foo()"
}],
"declaring_type": 0
}]

View File

@ -25,7 +25,9 @@ OUTPUT:
"short_name": "foo",
"detailed_name": "void hello::Foo::foo()",
"declarations": [{
"spelling": "3:8-3:11"
"spelling": "3:8-3:11",
"extent": "3:3-3:13",
"content": "void foo()"
}],
"definition_spelling": "6:11-6:14",
"definition_extent": "6:1-6:19",

View File

@ -48,7 +48,10 @@ OUTPUT:
"short_name": "LoadCompilationEntriesFromDirectory",
"detailed_name": "std::vector<CompilationEntry> LoadCompilationEntriesFromDirectory(const std::string &)",
"declarations": [{
"spelling": "12:31-12:66"
"spelling": "12:31-12:66",
"extent": "12:1-12:104",
"content": "std::vector<CompilationEntry> LoadCompilationEntriesFromDirectory(const std::string& project_directory)",
"param_spellings": ["12:86-12:103"]
}]
}],
"vars": [{

View File

@ -34,7 +34,10 @@ OUTPUT:
"short_name": "Bar",
"detailed_name": "void Foo::Bar(Template<double> &)",
"declarations": [{
"spelling": "5:8-5:11"
"spelling": "5:8-5:11",
"extent": "5:3-5:30",
"content": "void Bar(Template<double>&)",
"param_spellings": ["5:29-5:29"]
}],
"definition_spelling": "8:11-8:14",
"definition_extent": "8:1-8:36",

View File

@ -31,9 +31,13 @@ OUTPUT:
"short_name": "Foo",
"detailed_name": "void Template::Foo()",
"declarations": [{
"spelling": "3:8-3:11"
"spelling": "3:8-3:11",
"extent": "3:3-3:13",
"content": "void Foo()"
}, {
"spelling": "9:22-9:25"
"spelling": "9:22-9:25",
"extent": "9:1-9:30",
"content": "void Template<void>::Foo() {}"
}],
"definition_spelling": "7:19-7:22",
"definition_extent": "6:1-7:24",

View File

@ -35,7 +35,9 @@ OUTPUT:
"short_name": "Foo",
"detailed_name": "void Foo::Foo()",
"declarations": [{
"spelling": "4:3-4:6"
"spelling": "4:3-4:6",
"extent": "4:3-4:8",
"content": "Foo()"
}],
"definition_spelling": "7:6-7:9",
"definition_extent": "7:1-9:2",

View File

@ -15,7 +15,10 @@ OUTPUT:
"short_name": "called",
"detailed_name": "bool called(bool, bool)",
"declarations": [{
"spelling": "3:6-3:12"
"spelling": "3:6-3:12",
"extent": "3:1-3:28",
"content": "bool called(bool a, bool b)",
"param_spellings": ["3:18-3:19", "3:26-3:27"]
}],
"callers": ["1@6:14-6:20"]
}, {

View File

@ -20,7 +20,9 @@ OUTPUT:
"short_name": "called",
"detailed_name": "void called()",
"declarations": [{
"spelling": "1:6-1:12"
"spelling": "1:6-1:12",
"extent": "1:1-1:14",
"content": "void called()"
}],
"callers": ["1@5:3-5:9"]
}, {

View File

@ -27,7 +27,10 @@ OUTPUT:
"short_name": "Wrapper",
"detailed_name": "void Wrapper::Wrapper(int)",
"declarations": [{
"spelling": "2:3-2:10"
"spelling": "2:3-2:10",
"extent": "2:3-2:17",
"content": "Wrapper(int i)",
"param_spellings": ["2:15-2:16"]
}],
"declaring_type": 0,
"callers": ["~2@8:10-8:16"]

View File

@ -26,7 +26,9 @@ OUTPUT:
"short_name": "Used",
"detailed_name": "void Foo::Used()",
"declarations": [{
"spelling": "2:8-2:12"
"spelling": "2:8-2:12",
"extent": "2:3-2:14",
"content": "void Used()"
}],
"declaring_type": 0,
"callers": ["1@6:18-6:22"]

View File

@ -27,7 +27,9 @@ OUTPUT:
"short_name": "Used",
"detailed_name": "void Foo::Used()",
"declarations": [{
"spelling": "2:8-2:12"
"spelling": "2:8-2:12",
"extent": "2:3-2:14",
"content": "void Used()"
}],
"declaring_type": 0,
"callers": ["1@7:6-7:10"]

View File

@ -12,7 +12,9 @@ OUTPUT:
"short_name": "foo",
"detailed_name": "void foo()",
"declarations": [{
"spelling": "1:6-1:9"
"spelling": "1:6-1:9",
"extent": "1:1-1:11",
"content": "void foo()"
}],
"callers": ["1@4:3-4:6"]
}, {

View File

@ -26,7 +26,9 @@ OUTPUT:
"short_name": "foo",
"detailed_name": "void Foo::foo()",
"declarations": [{
"spelling": "2:8-2:11"
"spelling": "2:8-2:11",
"extent": "2:3-2:13",
"content": "void foo()"
}],
"declaring_type": 0,
"callers": ["1@7:6-7:9"]

View File

@ -15,7 +15,10 @@ OUTPUT:
"short_name": "accept",
"detailed_name": "void accept(T)",
"declarations": [{
"spelling": "2:6-2:12"
"spelling": "2:6-2:12",
"extent": "2:1-2:15",
"content": "void accept(T)",
"param_spellings": ["2:14-2:14"]
}],
"callers": ["1@5:3-5:9", "1@6:3-6:9"]
}, {

View File

@ -132,7 +132,9 @@ OUTPUT:
"short_name": "foo",
"detailed_name": "unique_ptr<S1, S2> *Foo::foo()",
"declarations": [{
"spelling": "65:23-65:26"
"spelling": "65:23-65:26",
"extent": "65:3-65:28",
"content": "unique_ptr<S1, S2>* foo()"
}],
"definition_spelling": "79:26-79:29",
"definition_extent": "79:1-79:51",

View File

@ -23,7 +23,10 @@ OUTPUT:
"short_name": "foo",
"detailed_name": "void foo(Foo *, Foo *)",
"declarations": [{
"spelling": "3:6-3:9"
"spelling": "3:6-3:9",
"extent": "3:1-3:23",
"content": "void foo(Foo* f, Foo*)",
"param_spellings": ["3:15-3:16", "3:22-3:22"]
}],
"definition_spelling": "4:6-4:9",
"definition_extent": "4:1-4:26"

View File

@ -40,9 +40,13 @@ OUTPUT:
"short_name": "foo",
"detailed_name": "Type *foo()",
"declarations": [{
"spelling": "3:7-3:10"
"spelling": "3:7-3:10",
"extent": "3:1-3:12",
"content": "Type* foo()"
}, {
"spelling": "4:7-4:10"
"spelling": "4:7-4:10",
"extent": "4:1-4:12",
"content": "Type* foo()"
}],
"definition_spelling": "5:7-5:10",
"definition_extent": "5:1-5:15"
@ -52,7 +56,10 @@ OUTPUT:
"short_name": "Get",
"detailed_name": "Type *Foo::Get(int)",
"declarations": [{
"spelling": "8:9-8:12"
"spelling": "8:9-8:12",
"extent": "8:3-8:17",
"content": "Type* Get(int)",
"param_spellings": ["8:16-8:16"]
}],
"definition_spelling": "12:12-12:15",
"definition_extent": "12:1-12:23",
@ -63,7 +70,9 @@ OUTPUT:
"short_name": "Empty",
"detailed_name": "void Foo::Empty()",
"declarations": [{
"spelling": "9:8-9:13"
"spelling": "9:8-9:13",
"extent": "9:3-9:15",
"content": "void Empty()"
}],
"definition_spelling": "13:11-13:16",
"definition_extent": "13:1-13:21",
@ -74,7 +83,9 @@ OUTPUT:
"short_name": "external",
"detailed_name": "const Type &external()",
"declarations": [{
"spelling": "15:20-15:28"
"spelling": "15:20-15:28",
"extent": "15:1-15:30",
"content": "extern const Type& external()"
}]
}, {
"id": 4,
@ -82,7 +93,9 @@ OUTPUT:
"short_name": "bar",
"detailed_name": "Type *bar()",
"declarations": [{
"spelling": "17:14-17:17"
"spelling": "17:14-17:17",
"extent": "17:1-17:19",
"content": "static Type* bar()"
}],
"definition_spelling": "18:14-18:17",
"definition_extent": "18:1-18:22"

View File

@ -29,7 +29,9 @@ OUTPUT:
"short_name": "make",
"detailed_name": "Foo *Foo::make()",
"declarations": [{
"spelling": "2:8-2:12"
"spelling": "2:8-2:12",
"extent": "2:3-2:14",
"content": "Foo* make()"
}],
"definition_spelling": "5:11-5:15",
"definition_extent": "5:1-8:2",

View File

@ -33,7 +33,10 @@ OUTPUT:
"short_name": "called",
"detailed_name": "void called(int)",
"declarations": [{
"spelling": "1:6-1:12"
"spelling": "1:6-1:12",
"extent": "1:1-1:19",
"content": "void called(int a)",
"param_spellings": ["1:17-1:18"]
}],
"callers": ["2@14:3-14:9"]
}, {
@ -42,7 +45,9 @@ OUTPUT:
"short_name": "gen",
"detailed_name": "int gen()",
"declarations": [{
"spelling": "3:5-3:8"
"spelling": "3:5-3:8",
"extent": "3:1-3:10",
"content": "int gen()"
}],
"callers": ["2@14:14-14:17"]
}, {

View File

@ -15,7 +15,10 @@ OUTPUT:
"short_name": "called",
"detailed_name": "void called(int)",
"declarations": [{
"spelling": "1:6-1:12"
"spelling": "1:6-1:12",
"extent": "1:1-1:19",
"content": "void called(int a)",
"param_spellings": ["1:17-1:18"]
}],
"callers": ["2@6:3-6:9"]
}, {

View File

@ -37,7 +37,10 @@ OUTPUT:
"short_name": "accept",
"detailed_name": "void accept(int)",
"declarations": [{
"spelling": "7:6-7:12"
"spelling": "7:6-7:12",
"extent": "7:1-7:17",
"content": "void accept(int)",
"param_spellings": ["7:16-7:16"]
}],
"callers": ["2@14:3-14:9", "2@15:3-15:9", "2@17:3-17:9"]
}, {
@ -46,7 +49,10 @@ OUTPUT:
"short_name": "accept",
"detailed_name": "void accept(int *)",
"declarations": [{
"spelling": "8:6-8:12"
"spelling": "8:6-8:12",
"extent": "8:1-8:18",
"content": "void accept(int*)",
"param_spellings": ["8:17-8:17"]
}],
"callers": ["2@16:3-16:9"]
}, {

View File

@ -26,7 +26,10 @@ OUTPUT:
"short_name": "accept",
"detailed_name": "void accept(int)",
"declarations": [{
"spelling": "5:6-5:12"
"spelling": "5:6-5:12",
"extent": "5:1-5:17",
"content": "void accept(int)",
"param_spellings": ["5:16-5:16"]
}],
"callers": ["1@8:3-8:9"]
}, {