From e54d70e464e93daa3058e6a34584f1d073609efa Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Sat, 18 Feb 2017 16:53:31 -0800 Subject: [PATCH] all-tests-passing --- libclangmm/Cursor.cc | 18 +++++++++++++---- libclangmm/Cursor.h | 4 ++++ main.cpp | 20 +++++++++++++++---- tests/usage/func_usage_addr_func.cc | 3 ++- tests/usage/func_usage_addr_method.cc | 3 ++- tests/usage/func_usage_call_method.cc | 7 +++---- .../usage/func_usage_class_inline_var_def.cc | 3 ++- tests/usage/func_usage_forward_decl_method.cc | 7 +++---- .../usage/type_usage_as_template_parameter.cc | 8 ++++++++ tests/usage/type_usage_declare_extern.cc | 3 ++- .../usage/type_usage_on_parameter_unnamed.cc | 8 ++++++++ tests/usage/type_usage_on_static.cc | 2 ++ tests/vars/class_member.cc | 3 ++- tests/vars/class_static_member.cc | 3 ++- tests/vars/class_static_member_decl_only.cc | 3 +++ tests/vars/function_local.cc | 3 ++- tests/vars/function_param.cc | 3 ++- tests/vars/function_param_unnamed.cc | 3 ++- tests/vars/function_shadow_local.cc | 3 ++- tests/vars/function_shadow_param.cc | 3 ++- tests/vars/global_variable.cc | 3 ++- tests/vars/global_variable_decl_only.cc | 5 ++++- 22 files changed, 89 insertions(+), 29 deletions(-) create mode 100644 tests/usage/type_usage_as_template_parameter.cc create mode 100644 tests/usage/type_usage_on_parameter_unnamed.cc diff --git a/libclangmm/Cursor.cc b/libclangmm/Cursor.cc index b7837178..2be38a49 100644 --- a/libclangmm/Cursor.cc +++ b/libclangmm/Cursor.cc @@ -19,10 +19,24 @@ std::string Type::get_usr() const { return clang::Cursor(clang_getTypeDeclaration(cx_type)).get_usr(); } +Type Type::strip_qualifiers() const { + //CXRefQualifierKind qualifiers = clang_Type_getCXXRefQualifier(cx_type) + switch (cx_type.kind) { + case CXType_Pointer: + return clang_getPointeeType(cx_type); + } + + return cx_type; +} + std::string Type::get_spelling() const { return ToString(clang_getTypeSpelling(cx_type)); } +SourceLocation Cursor::get_source_location() const { + return SourceLocation(clang_getCursorLocation(cx_cursor)); +} + Type Type::get_return_type() const { return Type(clang_getResultType(cx_type)); } @@ -64,10 +78,6 @@ Type Cursor::get_type() const { return Type(clang_getCursorType(cx_cursor)); } -SourceLocation Cursor::get_source_location() const { - return SourceLocation(clang_getCursorLocation(cx_cursor)); -} - /* SourceRange Cursor::get_source_range() const { return SourceRange(clang_getCursorExtent(cx_cursor)); diff --git a/libclangmm/Cursor.h b/libclangmm/Cursor.h index 96bcc38b..3a0b04e5 100644 --- a/libclangmm/Cursor.h +++ b/libclangmm/Cursor.h @@ -21,6 +21,10 @@ public: std::string get_usr() const; std::string get_spelling() const; + + // Try to resolve this type and remove qualifies, ie, Foo* will become Foo + Type strip_qualifiers() const; + Type get_return_type() const; std::vector get_arguments() const; diff --git a/main.cpp b/main.cpp index 190c2604..2482bcab 100644 --- a/main.cpp +++ b/main.cpp @@ -84,7 +84,9 @@ struct TypeDef { // Usages. std::vector uses; - TypeDef(TypeId id, const std::string& usr) : id(id), usr(usr) {} + TypeDef(TypeId id, const std::string& usr) : id(id), usr(usr) { + std::cout << "Creating type type with usr " << usr << std::endl; + } }; struct FuncDef { @@ -527,7 +529,12 @@ void Dump(clang::Cursor cursor) { - +// TODO: See if we can improve type usage reporting, for example +// void foo() { +// Foo x; +// } +// The usage on |Foo| will be reported at the |x| variable location. We should +// report it at the start of |Foo| instead. void HandleVarDecl(ParsingDatabase* db, NamespaceStack* ns, clang::Cursor var, std::optional declaring_type) { @@ -549,6 +556,11 @@ void HandleVarDecl(ParsingDatabase* db, NamespaceStack* ns, clang::Cursor var, s var_def->declaring_type = declaring_type; } + // Add a usage to the type of the variable. + clang::Type var_type = var.get_type().strip_qualifiers(); + TypeId var_type_id = db->ToTypeId(var_type.get_usr()); + db->Resolve(var_type_id)->uses.push_back(var.get_source_location()); + // We don't do any additional processing for non-definitions. if (!var.is_definition()) { var_def->declaration = var.get_source_location(); @@ -563,7 +575,7 @@ void HandleVarDecl(ParsingDatabase* db, NamespaceStack* ns, clang::Cursor var, s // TODO: Figure out how to scan initializations properly. We probably need // to scan for assignment statement, or definition+ctor. var_def->initializations.push_back(var.get_source_location()); - var_def->variable_type = db->ToTypeId(var.get_type().get_usr()); + var_def->variable_type = db->ToTypeId(var.get_type().strip_qualifiers().get_usr()); } @@ -935,7 +947,7 @@ void DiffDocuments(rapidjson::Document& expected, rapidjson::Document& actual) { int main(int argc, char** argv) { for (std::string path : GetFilesInFolder("tests")) { // TODO: Fix all existing tests. - //if (path != "tests/usage/func_usage_class_inline_var_def.cc") continue; + //if (path != "tests/usage/type_usage_declare_extern.cc") continue; // Parse expected output from the test, parse it into JSON document. std::string expected_output; diff --git a/tests/usage/func_usage_addr_func.cc b/tests/usage/func_usage_addr_func.cc index b9b95d81..02d4e12c 100644 --- a/tests/usage/func_usage_addr_func.cc +++ b/tests/usage/func_usage_addr_func.cc @@ -13,7 +13,8 @@ void user() { OUTPUT: { "types": [{ - "id": 0 + "id": 0, + "uses": ["tests/usage/func_usage_addr_func.cc:1:19", "tests/usage/func_usage_addr_func.cc:6:8"] }], "functions": [{ "id": 0, diff --git a/tests/usage/func_usage_addr_method.cc b/tests/usage/func_usage_addr_method.cc index 7a7d5997..5f95af5e 100644 --- a/tests/usage/func_usage_addr_method.cc +++ b/tests/usage/func_usage_addr_method.cc @@ -18,7 +18,8 @@ OUTPUT: "definition": "tests/usage/func_usage_addr_method.cc:1:8", "funcs": [0] }, { - "id": 1 + "id": 1, + "uses": ["tests/usage/func_usage_addr_method.cc:6:8"] }], "functions": [{ "id": 0, diff --git a/tests/usage/func_usage_call_method.cc b/tests/usage/func_usage_call_method.cc index 9110d9a6..a1a0f21c 100644 --- a/tests/usage/func_usage_call_method.cc +++ b/tests/usage/func_usage_call_method.cc @@ -16,9 +16,8 @@ OUTPUT: "short_name": "Foo", "qualified_name": "Foo", "definition": "tests/usage/func_usage_call_method.cc:1:8", - "funcs": [0] - }, { - "id": 1 + "funcs": [0], + "uses": ["tests/usage/func_usage_call_method.cc:6:8"] }], "functions": [{ "id": 0, @@ -44,7 +43,7 @@ OUTPUT: "qualified_name": "f", "declaration": "tests/usage/func_usage_call_method.cc:6:8", "initializations": ["tests/usage/func_usage_call_method.cc:6:8"], - "variable_type": 1 + "variable_type": 0 }] } */ \ No newline at end of file diff --git a/tests/usage/func_usage_class_inline_var_def.cc b/tests/usage/func_usage_class_inline_var_def.cc index 02015589..d588648a 100644 --- a/tests/usage/func_usage_class_inline_var_def.cc +++ b/tests/usage/func_usage_class_inline_var_def.cc @@ -20,7 +20,8 @@ OUTPUT: "definition": "tests/usage/func_usage_class_inline_var_def.cc:5:7", "vars": [0] }, { - "id": 1 + "id": 1, + "uses": ["tests/usage/func_usage_class_inline_var_def.cc:6:7"] }], "functions": [{ "id": 0, diff --git a/tests/usage/func_usage_forward_decl_method.cc b/tests/usage/func_usage_forward_decl_method.cc index 9fa54efb..af9f3803 100644 --- a/tests/usage/func_usage_forward_decl_method.cc +++ b/tests/usage/func_usage_forward_decl_method.cc @@ -15,9 +15,8 @@ OUTPUT: "short_name": "Foo", "qualified_name": "Foo", "definition": "tests/usage/func_usage_forward_decl_method.cc:1:8", - "funcs": [0] - }, { - "id": 1 + "funcs": [0], + "uses": ["tests/usage/func_usage_forward_decl_method.cc:6:8"] }], "functions": [{ "id": 0, @@ -43,7 +42,7 @@ OUTPUT: "qualified_name": "f", "declaration": "tests/usage/func_usage_forward_decl_method.cc:6:8", "initializations": ["tests/usage/func_usage_forward_decl_method.cc:6:8"], - "variable_type": 1 + "variable_type": 0 }] } */ \ No newline at end of file diff --git a/tests/usage/type_usage_as_template_parameter.cc b/tests/usage/type_usage_as_template_parameter.cc new file mode 100644 index 00000000..4f70c7a9 --- /dev/null +++ b/tests/usage/type_usage_as_template_parameter.cc @@ -0,0 +1,8 @@ +/* +OUTPUT: +{ + "types": [], + "functions": [], + "variables": [] +} +*/ \ No newline at end of file diff --git a/tests/usage/type_usage_declare_extern.cc b/tests/usage/type_usage_declare_extern.cc index 1a400d87..2718ff40 100644 --- a/tests/usage/type_usage_declare_extern.cc +++ b/tests/usage/type_usage_declare_extern.cc @@ -9,7 +9,8 @@ OUTPUT: "usr": "c:@S@T", "short_name": "T", "qualified_name": "T", - "definition": "tests/usage/type_usage_declare_extern.cc:1:8" + "definition": "tests/usage/type_usage_declare_extern.cc:1:8", + "uses": ["tests/usage/type_usage_declare_extern.cc:3:10"] }], "functions": [], "variables": [{ diff --git a/tests/usage/type_usage_on_parameter_unnamed.cc b/tests/usage/type_usage_on_parameter_unnamed.cc new file mode 100644 index 00000000..4f70c7a9 --- /dev/null +++ b/tests/usage/type_usage_on_parameter_unnamed.cc @@ -0,0 +1,8 @@ +/* +OUTPUT: +{ + "types": [], + "functions": [], + "variables": [] +} +*/ \ No newline at end of file diff --git a/tests/usage/type_usage_on_static.cc b/tests/usage/type_usage_on_static.cc index 4f70c7a9..b184bb07 100644 --- a/tests/usage/type_usage_on_static.cc +++ b/tests/usage/type_usage_on_static.cc @@ -1,4 +1,6 @@ /* +// TODO: Idea with this test is to verify we will capture std::unique_ptr + OUTPUT: { "types": [], diff --git a/tests/vars/class_member.cc b/tests/vars/class_member.cc index 789951db..af22bfd2 100644 --- a/tests/vars/class_member.cc +++ b/tests/vars/class_member.cc @@ -12,7 +12,8 @@ OUTPUT: "definition": "tests/vars/class_member.cc:1:7", "vars": [0] }, { - "id": 1 + "id": 1, + "uses": ["tests/vars/class_member.cc:2:7"] }], "functions": [], "variables": [{ diff --git a/tests/vars/class_static_member.cc b/tests/vars/class_static_member.cc index 951c1a0e..fb8474cd 100644 --- a/tests/vars/class_static_member.cc +++ b/tests/vars/class_static_member.cc @@ -13,7 +13,8 @@ OUTPUT: "definition": "tests/vars/class_static_member.cc:1:7", "vars": [0] }, { - "id": 1 + "id": 1, + "uses": ["tests/vars/class_static_member.cc:2:14", "tests/vars/class_static_member.cc:4:10"] }], "functions": [], "variables": [{ diff --git a/tests/vars/class_static_member_decl_only.cc b/tests/vars/class_static_member_decl_only.cc index b3ef0431..853ac835 100644 --- a/tests/vars/class_static_member_decl_only.cc +++ b/tests/vars/class_static_member_decl_only.cc @@ -11,6 +11,9 @@ OUTPUT: "qualified_name": "Foo", "definition": "tests/vars/class_static_member_decl_only.cc:1:7", "vars": [0] + }, { + "id": 1, + "uses": ["tests/vars/class_static_member_decl_only.cc:2:14"] }], "functions": [], "variables": [{ diff --git a/tests/vars/function_local.cc b/tests/vars/function_local.cc index 92789ae0..55d7270c 100644 --- a/tests/vars/function_local.cc +++ b/tests/vars/function_local.cc @@ -5,7 +5,8 @@ void foo() { OUTPUT: { "types": [{ - "id": 0 + "id": 0, + "uses": ["tests/vars/function_local.cc:2:7"] }], "functions": [{ "id": 0, diff --git a/tests/vars/function_param.cc b/tests/vars/function_param.cc index 620f0d7d..d79f9421 100644 --- a/tests/vars/function_param.cc +++ b/tests/vars/function_param.cc @@ -3,7 +3,8 @@ void foo(int p0, int p1) {} OUTPUT: { "types": [{ - "id": 0 + "id": 0, + "uses": ["tests/vars/function_param.cc:1:14", "tests/vars/function_param.cc:1:22"] }], "functions": [{ "id": 0, diff --git a/tests/vars/function_param_unnamed.cc b/tests/vars/function_param_unnamed.cc index 7536b0da..647874b0 100644 --- a/tests/vars/function_param_unnamed.cc +++ b/tests/vars/function_param_unnamed.cc @@ -8,7 +8,8 @@ void foo(int, int) {} OUTPUT: { "types": [{ - "id": 0 + "id": 0, + "uses": ["tests/vars/function_param_unnamed.cc:1:13", "tests/vars/function_param_unnamed.cc:1:18"] }], "functions": [{ "id": 0, diff --git a/tests/vars/function_shadow_local.cc b/tests/vars/function_shadow_local.cc index d3e8543b..0224bdcd 100644 --- a/tests/vars/function_shadow_local.cc +++ b/tests/vars/function_shadow_local.cc @@ -8,7 +8,8 @@ void foo() { OUTPUT: { "types": [{ - "id": 0 + "id": 0, + "uses": ["tests/vars/function_shadow_local.cc:2:7", "tests/vars/function_shadow_local.cc:4:9"] }], "functions": [{ "id": 0, diff --git a/tests/vars/function_shadow_param.cc b/tests/vars/function_shadow_param.cc index b51422ef..b2b07ed8 100644 --- a/tests/vars/function_shadow_param.cc +++ b/tests/vars/function_shadow_param.cc @@ -5,7 +5,8 @@ void foo(int p) { OUTPUT: { "types": [{ - "id": 0 + "id": 0, + "uses": ["tests/vars/function_shadow_param.cc:1:14", "tests/vars/function_shadow_param.cc:2:7"] }], "functions": [{ "id": 0, diff --git a/tests/vars/global_variable.cc b/tests/vars/global_variable.cc index 540479e0..a896c6fe 100644 --- a/tests/vars/global_variable.cc +++ b/tests/vars/global_variable.cc @@ -3,7 +3,8 @@ static int global = 0; OUTPUT: { "types": [{ - "id": 0 + "id": 0, + "uses": ["tests/vars/global_variable.cc:1:12"] }], "functions": [], "variables": [{ diff --git a/tests/vars/global_variable_decl_only.cc b/tests/vars/global_variable_decl_only.cc index 335126fa..cebd8187 100644 --- a/tests/vars/global_variable_decl_only.cc +++ b/tests/vars/global_variable_decl_only.cc @@ -2,7 +2,10 @@ extern int global; /* OUTPUT: { - "types": [], + "types": [{ + "id": 0, + "uses": ["tests/vars/global_variable_decl_only.cc:1:12"] + }], "functions": [], "variables": [{ "id": 0,