From ab7138bd915c9d0b0ea2585e6d8cafaa8990d30c Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Mon, 20 Feb 2017 14:38:32 -0800 Subject: [PATCH] Verify class inheritance for templates, add interesting usage --- main.cpp | 16 ++-- tests/inheritance/class_inherit.cc | 25 ++++++- .../class_inherit_templated_parent.cc | 73 +++++++++++++++++++ tests/inheritance/class_multiple_inherit.cc | 9 ++- tests/inheritance/function_override.cc | 3 +- 5 files changed, 114 insertions(+), 12 deletions(-) create mode 100644 tests/inheritance/class_inherit_templated_parent.cc diff --git a/main.cpp b/main.cpp index 5e02f363..535c5d37 100644 --- a/main.cpp +++ b/main.cpp @@ -1015,12 +1015,13 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { for (unsigned int i = 0; i < class_info->numBases; ++i) { const CXIdxBaseClassInfo* base_class = class_info->bases[i]; - TypeId parent_type_id = db->ToTypeId(clang::Cursor(base_class->cursor).get_referenced().get_usr()); - TypeDef* parent_type_def = db->Resolve(parent_type_id); - TypeDef* type_def = db->Resolve(type_id); // type_def ptr could be invalidated by ToTypeId. - - parent_type_def->derived.push_back(type_id); - type_def->parents.push_back(parent_type_id); + std::optional parent_type_id = ResolveDeclToType(db, base_class->cursor, true /*is_interesting*/, decl->semanticContainer, decl->lexicalContainer); + TypeDef* type_def = db->Resolve(type_id); // type_def ptr could be invalidated by ResolveDeclToType. + if (parent_type_id) { + TypeDef* parent_type_def = db->Resolve(parent_type_id.value()); + parent_type_def->derived.push_back(type_id); + type_def->parents.push_back(parent_type_id.value()); + } } break; } @@ -1157,7 +1158,7 @@ ParsingDatabase Parse(std::string filename) { clang::Index index(0 /*excludeDeclarationsFromPCH*/, 0 /*displayDiagnostics*/); clang::TranslationUnit tu(index, filename, args); - Dump(tu.document_cursor()); + //Dump(tu.document_cursor()); CXIndexAction index_action = clang_IndexAction_create(index.cx_index); @@ -1296,6 +1297,7 @@ int main(int argc, char** argv) { //if (path != "tests/usage/func_usage_template_func.cc") continue; //if (path != "tests/usage/usage_inside_of_call.cc") continue; //if (path != "tests/foobar.cc") continue; + //if (path != "tests/inheritance/class_inherit_templated_parent.cc") continue; // Parse expected output from the test, parse it into JSON document. std::string expected_output; diff --git a/tests/inheritance/class_inherit.cc b/tests/inheritance/class_inherit.cc index c301dfa9..ac723166 100644 --- a/tests/inheritance/class_inherit.cc +++ b/tests/inheritance/class_inherit.cc @@ -2,6 +2,28 @@ class Parent {}; class Derived : public Parent {}; /* +// TODO: Insert interesting usage for derived types. Maybe we should change out +// interesting usage approach for types, and instead find a list of "uninteresting" usages. +// Rather, what I think we should do is this +// +// t -> interesting +// f > uninteresting +// fileid 0 +// row 5 +// column 7 +// this could all be packed into 64 bits +// "usages": { "t@0:5:7" } +// +// interesting: 1 bit (2) +// file: 29 bits (536,870,912) +// line: 20 bits (1,048,576) +// column: 14 bits (16,384) +// +// When inserting a new usage, default to interesting, but if already present +// don't flip it to uninteresting. +// +// When importing we remap file ids. + OUTPUT: { "types": [{ @@ -11,7 +33,8 @@ OUTPUT: "qualified_name": "Parent", "definition": "tests/inheritance/class_inherit.cc:1:7", "derived": [1], - "all_uses": ["tests/inheritance/class_inherit.cc:1:7", "tests/inheritance/class_inherit.cc:2:24"] + "all_uses": ["tests/inheritance/class_inherit.cc:1:7", "tests/inheritance/class_inherit.cc:2:24"], + "interesting_uses": ["tests/inheritance/class_inherit.cc:2:24"] }, { "id": 1, "usr": "c:@S@Derived", diff --git a/tests/inheritance/class_inherit_templated_parent.cc b/tests/inheritance/class_inherit_templated_parent.cc new file mode 100644 index 00000000..e8d47384 --- /dev/null +++ b/tests/inheritance/class_inherit_templated_parent.cc @@ -0,0 +1,73 @@ +template +class Base1 {}; + +template +class Base2 {}; + +template +class Derived1 : Base1 {}; + +template +class Derived2 : Base2 {}; + +class Derived : Base1<3>, Base2, Derived1<4>, Derived2 {}; + +/* +OUTPUT: +{ + "types": [{ + "id": 0, + "usr": "c:@ST>1#Ni@Base1", + "short_name": "Base1", + "qualified_name": "Base1", + "definition": "tests/inheritance/class_inherit_templated_parent.cc:2:7", + "derived": [2, 5], + "all_uses": ["tests/inheritance/class_inherit_templated_parent.cc:2:7", "tests/inheritance/class_inherit_templated_parent.cc:8:18", "tests/inheritance/class_inherit_templated_parent.cc:13:17"], + "interesting_uses": ["tests/inheritance/class_inherit_templated_parent.cc:8:18", "tests/inheritance/class_inherit_templated_parent.cc:13:17"] + }, { + "id": 1, + "usr": "c:@ST>1#T@Base2", + "short_name": "Base2", + "qualified_name": "Base2", + "definition": "tests/inheritance/class_inherit_templated_parent.cc:5:7", + "derived": [3, 5], + "all_uses": ["tests/inheritance/class_inherit_templated_parent.cc:5:7", "tests/inheritance/class_inherit_templated_parent.cc:11:18", "tests/inheritance/class_inherit_templated_parent.cc:13:27"], + "interesting_uses": ["tests/inheritance/class_inherit_templated_parent.cc:11:18", "tests/inheritance/class_inherit_templated_parent.cc:13:27"] + }, { + "id": 2, + "usr": "c:@ST>1#Ni@Derived1", + "short_name": "Derived1", + "qualified_name": "Derived1", + "definition": "tests/inheritance/class_inherit_templated_parent.cc:8:7", + "parents": [0], + "derived": [5], + "all_uses": ["tests/inheritance/class_inherit_templated_parent.cc:8:7", "tests/inheritance/class_inherit_templated_parent.cc:13:43"], + "interesting_uses": ["tests/inheritance/class_inherit_templated_parent.cc:13:43"] + }, { + "id": 3, + "usr": "c:@ST>1#T@Derived2", + "short_name": "Derived2", + "qualified_name": "Derived2", + "definition": "tests/inheritance/class_inherit_templated_parent.cc:11:7", + "parents": [1], + "derived": [5], + "all_uses": ["tests/inheritance/class_inherit_templated_parent.cc:11:7", "tests/inheritance/class_inherit_templated_parent.cc:13:56"], + "interesting_uses": ["tests/inheritance/class_inherit_templated_parent.cc:13:56"] + }, { + "id": 4, + "usr": "c:class_inherit_templated_parent.cc@154", + "interesting_uses": ["tests/inheritance/class_inherit_templated_parent.cc:11:24"] + }, { + "id": 5, + "usr": "c:@S@Derived", + "short_name": "Derived", + "qualified_name": "Derived", + "definition": "tests/inheritance/class_inherit_templated_parent.cc:13:7", + "parents": [0, 1, 2, 3], + "all_uses": ["tests/inheritance/class_inherit_templated_parent.cc:13:7", "tests/inheritance/class_inherit_templated_parent.cc:13:33", "tests/inheritance/class_inherit_templated_parent.cc:13:65"], + "interesting_uses": ["tests/inheritance/class_inherit_templated_parent.cc:13:33", "tests/inheritance/class_inherit_templated_parent.cc:13:65"] + }], + "functions": [], + "variables": [] +} +*/ \ No newline at end of file diff --git a/tests/inheritance/class_multiple_inherit.cc b/tests/inheritance/class_multiple_inherit.cc index 8357b60e..923d64d1 100644 --- a/tests/inheritance/class_multiple_inherit.cc +++ b/tests/inheritance/class_multiple_inherit.cc @@ -13,7 +13,8 @@ OUTPUT: "qualified_name": "Root", "definition": "tests/inheritance/class_multiple_inherit.cc:1:7", "derived": [1, 2], - "all_uses": ["tests/inheritance/class_multiple_inherit.cc:1:7", "tests/inheritance/class_multiple_inherit.cc:2:24", "tests/inheritance/class_multiple_inherit.cc:3:24"] + "all_uses": ["tests/inheritance/class_multiple_inherit.cc:1:7", "tests/inheritance/class_multiple_inherit.cc:2:24", "tests/inheritance/class_multiple_inherit.cc:3:24"], + "interesting_uses": ["tests/inheritance/class_multiple_inherit.cc:2:24", "tests/inheritance/class_multiple_inherit.cc:3:24"] }, { "id": 1, "usr": "c:@S@MiddleA", @@ -22,7 +23,8 @@ OUTPUT: "definition": "tests/inheritance/class_multiple_inherit.cc:2:7", "parents": [0], "derived": [3], - "all_uses": ["tests/inheritance/class_multiple_inherit.cc:2:7", "tests/inheritance/class_multiple_inherit.cc:4:24"] + "all_uses": ["tests/inheritance/class_multiple_inherit.cc:2:7", "tests/inheritance/class_multiple_inherit.cc:4:24"], + "interesting_uses": ["tests/inheritance/class_multiple_inherit.cc:4:24"] }, { "id": 2, "usr": "c:@S@MiddleB", @@ -31,7 +33,8 @@ OUTPUT: "definition": "tests/inheritance/class_multiple_inherit.cc:3:7", "parents": [0], "derived": [3], - "all_uses": ["tests/inheritance/class_multiple_inherit.cc:3:7", "tests/inheritance/class_multiple_inherit.cc:4:40"] + "all_uses": ["tests/inheritance/class_multiple_inherit.cc:3:7", "tests/inheritance/class_multiple_inherit.cc:4:40"], + "interesting_uses": ["tests/inheritance/class_multiple_inherit.cc:4:40"] }, { "id": 3, "usr": "c:@S@Derived", diff --git a/tests/inheritance/function_override.cc b/tests/inheritance/function_override.cc index 50dc94c3..efb03b08 100644 --- a/tests/inheritance/function_override.cc +++ b/tests/inheritance/function_override.cc @@ -15,7 +15,8 @@ OUTPUT: "qualified_name": "Root", "definition": "tests/inheritance/function_override.cc:1:7", "derived": [1], - "all_uses": ["tests/inheritance/function_override.cc:1:7", "tests/inheritance/function_override.cc:4:24"] + "all_uses": ["tests/inheritance/function_override.cc:1:7", "tests/inheritance/function_override.cc:4:24"], + "interesting_uses": ["tests/inheritance/function_override.cc:4:24"] }, { "id": 1, "usr": "c:@S@Derived",