Verify class inheritance for templates, add interesting usage

This commit is contained in:
Jacob Dufault 2017-02-20 14:38:32 -08:00
parent f3edc6e2f0
commit ab7138bd91
5 changed files with 114 additions and 12 deletions

View File

@ -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<TypeId> 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;

View File

@ -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",

View File

@ -0,0 +1,73 @@
template<unsigned int C>
class Base1 {};
template<typename C>
class Base2 {};
template<unsigned int T>
class Derived1 : Base1<T> {};
template<typename T>
class Derived2 : Base2<T> {};
class Derived : Base1<3>, Base2<Derived>, Derived1<4>, Derived2<Derived> {};
/*
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": []
}
*/

View File

@ -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",

View File

@ -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",