From 9b351ce52faa76aab2564e9fba5f9fd3394344f6 Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Mon, 20 Feb 2017 23:33:05 -0800 Subject: [PATCH] emit less info for system defs --- main.cpp | 42 ++++++++++++++++++++++++++++++++++++------ tests/foobar.cc | 1 + 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/main.cpp b/main.cpp index ddd71aed..c73d82fb 100644 --- a/main.cpp +++ b/main.cpp @@ -159,9 +159,14 @@ using FuncRef = Ref; using VarRef = Ref; -// NOTE: declaration is empty if there is no forward declaration! +// TODO: skip as much forward-processing as possible when |is_system_def| is +// set to false. +// TODO: Either eliminate the defs created as a by-product of cross-referencing, +// or do not emit things we don't have definitions for. struct TypeDef { + bool is_system_def = false; + // General metadata. TypeId id; std::string usr; @@ -202,6 +207,9 @@ struct TypeDef { } void AddUsage(Location loc, bool insert_if_not_present = true) { + if (is_system_def) + return; + for (int i = uses.size() - 1; i >= 0; --i) { if (uses[i].IsEqualTo(loc)) { if (loc.interesting) @@ -216,6 +224,8 @@ struct TypeDef { }; struct FuncDef { + bool is_system_def = false; + // General metadata. FuncId id; std::string usr; @@ -253,6 +263,8 @@ struct FuncDef { }; struct VarDef { + bool is_system_def = false; + // General metadata. VarId id; std::string usr; @@ -476,6 +488,8 @@ std::string ParsingDatabase::ToString() { writer.Key("types"); writer.StartArray(); for (TypeDef& def : types) { + if (def.is_system_def) continue; + writer.StartObject(); WRITE(id); WRITE(usr); @@ -497,6 +511,8 @@ std::string ParsingDatabase::ToString() { writer.Key("functions"); writer.StartArray(); for (FuncDef& def : funcs) { + if (def.is_system_def) continue; + writer.StartObject(); WRITE(id); WRITE(usr); @@ -519,6 +535,8 @@ std::string ParsingDatabase::ToString() { writer.Key("variables"); writer.StartArray(); for (VarDef& def : vars) { + if (def.is_system_def) continue; + writer.StartObject(); WRITE(id); WRITE(usr); @@ -861,6 +879,8 @@ std::optional ResolveDeclToType(ParsingDatabase* db, clang::Cursor decl_ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { + bool is_system_def = clang_Location_isInSystemHeader(clang_getCursorLocation(decl->cursor)); + IndexParam* param = static_cast(client_data); ParsingDatabase* db = param->db; NamespaceHelper* ns = param->ns; @@ -881,6 +901,8 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { VarId var_id = db->ToVarId(decl->entityInfo->USR); VarDef* var_def = db->Resolve(var_id); + var_def->is_system_def = is_system_def; + // TODO: Eventually run with this if. Right now I want to iron out bugs this may shadow. // TODO: Verify this gets called multiple times //if (!decl->isRedeclaration) { @@ -920,11 +942,14 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { case CXIdxEntity_CXXDestructor: case CXIdxEntity_CXXInstanceMethod: case CXIdxEntity_CXXStaticMethod: + case CXIdxEntity_CXXConversionFunction: { clang::Cursor decl_cursor = decl->cursor; FuncId func_id = db->ToFuncId(decl->entityInfo->USR); FuncDef* func_def = db->Resolve(func_id); + func_def->is_system_def = is_system_def; + // TODO: Eventually run with this if. Right now I want to iron out bugs this may shadow. //if (!decl->isRedeclaration) { func_def->short_name = decl->entityInfo->name; @@ -1040,6 +1065,8 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { TypeId type_id = db->ToTypeId(decl->entityInfo->USR); TypeDef* type_def = db->Resolve(type_id); + type_def->is_system_def = is_system_def; + if (alias_of) type_def->alias_of = alias_of.value(); @@ -1060,6 +1087,8 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { TypeId type_id = db->ToTypeId(decl->entityInfo->USR); TypeDef* type_def = db->Resolve(type_id); + type_def->is_system_def = is_system_def; + // 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) { @@ -1147,6 +1176,7 @@ void indexEntityReference(CXClientData client_data, const CXIdxEntityRefInfo* re break; } + case CXIdxEntity_CXXConversionFunction: case CXIdxEntity_CXXStaticMethod: case CXIdxEntity_CXXInstanceMethod: case CXIdxEntity_Function: @@ -1282,7 +1312,7 @@ ParsingDatabase Parse(std::string filename) { NamespaceHelper ns; IndexParam param(&db, &ns); clang_indexTranslationUnit(index_action, ¶m, callbacks, sizeof(callbacks), - CXIndexOpt_IndexFunctionLocalSymbols, tu.cx_tu); + CXIndexOpt_IndexFunctionLocalSymbols | CXIndexOpt_SkipParsedBodiesInSession, tu.cx_tu); clang_IndexAction_dispose(index_action); @@ -1406,7 +1436,7 @@ int main(int argc, char** argv) { //if (path != "tests/usage/func_usage_addr_method.cc") continue; //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/foobar.cc") continue; //if (path != "tests/types/anonymous_struct.cc") continue; // Parse expected output from the test, parse it into JSON document. @@ -1420,8 +1450,8 @@ int main(int argc, char** argv) { ParsingDatabase db = Parse(path); std::string actual_output = db.ToString(); - WriteToFile("output.json", actual_output); - break; + //WriteToFile("output.json", actual_output); + //break; rapidjson::Document actual; actual.Parse(actual_output.c_str()); @@ -1442,7 +1472,7 @@ int main(int argc, char** argv) { } } - //std::cin.get(); + std::cin.get(); return 0; } diff --git a/tests/foobar.cc b/tests/foobar.cc index f8af6a85..4db43b17 100644 --- a/tests/foobar.cc +++ b/tests/foobar.cc @@ -5,6 +5,7 @@ int gen() { return 1; } void foo() { called(gen() * gen()); } + /* OUTPUT: {