From b33bd54922f74a92ca0d8f82301ffbeae3d95c33 Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Mon, 20 Feb 2017 22:11:47 -0800 Subject: [PATCH] support anonymous structs --- main.cpp | 22 +++++++++--- tests/types/anonymous_struct.cc | 61 +++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 tests/types/anonymous_struct.cc diff --git a/main.cpp b/main.cpp index a52ea9c0..e2195729 100644 --- a/main.cpp +++ b/main.cpp @@ -1052,16 +1052,24 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { case CXIdxEntity_Struct: case CXIdxEntity_CXXClass: { - ns->RegisterQualifiedName(decl->entityInfo->USR, decl->semanticContainer, decl->entityInfo->name); - TypeId type_id = db->ToTypeId(decl->entityInfo->USR); TypeDef* type_def = db->Resolve(type_id); // TODO: Eventually run with this if. Right now I want to iron out bugs this may shadow. // TODO: For type section, verify if this ever runs for non definitions? //if (!decl->isRedeclaration) { - type_def->short_name = decl->entityInfo->name; + + // name can be null in an anonymous struct (see tests/types/anonymous_struct.cc). + if (decl->entityInfo->name) { + ns->RegisterQualifiedName(decl->entityInfo->USR, decl->semanticContainer, decl->entityInfo->name); + type_def->short_name = decl->entityInfo->name; + } + else { + type_def->short_name = ""; + } + type_def->qualified_name = ns->QualifiedName(decl->semanticContainer, type_def->short_name); + // } assert(decl->isDefinition); @@ -1237,6 +1245,7 @@ void indexEntityReference(CXClientData client_data, const CXIdxEntityRefInfo* re } } +static bool DUMP_AST = true; ParsingDatabase Parse(std::string filename) { @@ -1245,7 +1254,8 @@ ParsingDatabase Parse(std::string filename) { clang::Index index(0 /*excludeDeclarationsFromPCH*/, 0 /*displayDiagnostics*/); clang::TranslationUnit tu(index, filename, args); - Dump(tu.document_cursor()); + if (DUMP_AST) + Dump(tu.document_cursor()); CXIndexAction index_action = clang_IndexAction_create(index.cx_index); @@ -1369,6 +1379,8 @@ int main(int argc, char** argv) { return 0; */ + DUMP_AST = true; + for (std::string path : GetFilesInFolder("tests")) { //if (path != "tests/declaration_vs_definition/class_member_static.cc") continue; //if (path != "tests/enums/enum_class_decl.cc") continue; @@ -1385,7 +1397,7 @@ int main(int argc, char** argv) { //if (path != "tests/usage/type_usage_typedef_and_using.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; + //if (path != "tests/types/anonymous_struct.cc") continue; // Parse expected output from the test, parse it into JSON document. std::string expected_output; diff --git a/tests/types/anonymous_struct.cc b/tests/types/anonymous_struct.cc new file mode 100644 index 00000000..c1557487 --- /dev/null +++ b/tests/types/anonymous_struct.cc @@ -0,0 +1,61 @@ +union vector3 { + struct { float x, y, z; }; + float v[3]; +}; + +/* +OUTPUT: +{ + "types": [{ + "id": 0, + "usr": "c:@U@vector3", + "short_name": "vector3", + "qualified_name": "vector3", + "definition": "1:1:7", + "vars": [3], + "uses": ["*1:1:7"] + }, { + "id": 1, + "usr": "c:@U@vector3@Sa", + "short_name": "", + "qualified_name": "vector3::", + "definition": "1:2:3", + "vars": [0, 1, 2], + "uses": ["*1:2:3"] + }], + "functions": [], + "variables": [{ + "id": 0, + "usr": "c:@U@vector3@Sa@FI@x", + "short_name": "x", + "qualified_name": "x", + "definition": "1:2:18", + "declaring_type": 1, + "uses": ["1:2:18"] + }, { + "id": 1, + "usr": "c:@U@vector3@Sa@FI@y", + "short_name": "y", + "qualified_name": "y", + "definition": "1:2:21", + "declaring_type": 1, + "uses": ["1:2:21"] + }, { + "id": 2, + "usr": "c:@U@vector3@Sa@FI@z", + "short_name": "z", + "qualified_name": "z", + "definition": "1:2:24", + "declaring_type": 1, + "uses": ["1:2:24"] + }, { + "id": 3, + "usr": "c:@U@vector3@FI@v", + "short_name": "v", + "qualified_name": "vector3::v", + "definition": "1:3:9", + "declaring_type": 0, + "uses": ["1:3:9"] + }] +} +*/ \ No newline at end of file