From 7f6354f9c8810321337ef705b132684497f45fa5 Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Sat, 18 Feb 2017 18:44:04 -0800 Subject: [PATCH] wip --- main.cpp | 41 ++++++++++++------- tests/usage/func_usage_addr_func.cc | 4 -- .../usage/type_usage_declare_param_unnamed.cc | 7 +--- tests/vars/function_param_unnamed.cc | 11 +---- 4 files changed, 29 insertions(+), 34 deletions(-) diff --git a/main.cpp b/main.cpp index 811b7649..1fbadb85 100644 --- a/main.cpp +++ b/main.cpp @@ -85,7 +85,7 @@ struct TypeDef { std::vector uses; TypeDef(TypeId id, const std::string& usr) : id(id), usr(usr) { - //assert(usr.size() > 0); + assert(usr.size() > 0); //std::cout << "Creating type with usr " << usr << std::endl; } }; @@ -117,7 +117,9 @@ struct FuncDef { // Usages. std::vector uses; - FuncDef(FuncId id, const std::string& usr) : id(id), usr(usr) {} + FuncDef(FuncId id, const std::string& usr) : id(id), usr(usr) { + assert(usr.size() > 0); + } }; struct VarDef { @@ -138,7 +140,9 @@ struct VarDef { // Usages. std::vector uses; - VarDef(VarId id, const std::string& usr) : id(id), usr(usr) {} + VarDef(VarId id, const std::string& usr) : id(id), usr(usr) { + assert(usr.size() > 0); + } }; @@ -562,24 +566,33 @@ void InsertTypeUsageAtLocation(ParsingDatabase* db, clang::Type type, const clan void HandleVarDecl(ParsingDatabase* db, NamespaceStack* ns, clang::Cursor var, std::optional declaring_type) { //Dump(var); - VarId var_id = db->ToVarId(var.get_usr()); + // Add a usage to the type of the variable. + InsertTypeUsageAtLocation(db, var.get_type(), var.get_source_location()); + + // Note: if there is no USR then there can be no declaring type, as all + // member variables of a class must have a name. Only function parameters + // can be nameless. + std::string var_usr = var.get_usr(); + if (var_usr.size() == 0) { + assert(var.get_kind() == CXCursor_ParmDecl); + return; + } + + VarId var_id = db->ToVarId(var_usr); + VarDef* var_def = db->Resolve(var_id); declaring_type = ResolveDeclaringType(CXCursor_FieldDecl, db, var, declaring_type); - - // TODO: We could use RAII to verify we don't modify db while have a *Def - // instance alive. - VarDef* var_def = db->Resolve(var_id); - var_def->short_name = var.get_spelling(); - var_def->qualified_name = - ns->ComputeQualifiedName(db, declaring_type, var_def->short_name); - if (declaring_type && !var_def->declaration) { + // Note: If USR is null there can be no declaring type. db->Resolve(declaring_type.value())->vars.push_back(var_id); var_def->declaring_type = declaring_type; } - // Add a usage to the type of the variable. - InsertTypeUsageAtLocation(db, var.get_type(), var.get_source_location()); + // TODO: We could use RAII to verify we don't modify db while have a *Def + // instance alive. + var_def->short_name = var.get_spelling(); + var_def->qualified_name = + ns->ComputeQualifiedName(db, declaring_type, var_def->short_name); // We don't do any additional processing for non-definitions. if (!var.is_definition()) { diff --git a/tests/usage/func_usage_addr_func.cc b/tests/usage/func_usage_addr_func.cc index 15946af7..01ea1880 100644 --- a/tests/usage/func_usage_addr_func.cc +++ b/tests/usage/func_usage_addr_func.cc @@ -37,10 +37,6 @@ OUTPUT: }], "variables": [{ "id": 0, - "declaration": "tests/usage/func_usage_addr_func.cc:1:19", - "initializations": ["tests/usage/func_usage_addr_func.cc:1:19"] - }, { - "id": 1, "usr": "c:func_usage_addr_func.cc@61@F@user#@x", "short_name": "x", "qualified_name": "x", diff --git a/tests/usage/type_usage_declare_param_unnamed.cc b/tests/usage/type_usage_declare_param_unnamed.cc index 53046e75..76d929f7 100644 --- a/tests/usage/type_usage_declare_param_unnamed.cc +++ b/tests/usage/type_usage_declare_param_unnamed.cc @@ -18,11 +18,6 @@ OUTPUT: "qualified_name": "foo", "definition": "tests/usage/type_usage_declare_param_unnamed.cc:2:6" }], - "variables": [{ - "id": 0, - "declaration": "tests/usage/type_usage_declare_param_unnamed.cc:2:22", - "initializations": ["tests/usage/type_usage_declare_param_unnamed.cc:2:22"], - "variable_type": 0 - }] + "variables": [] } */ \ No newline at end of file diff --git a/tests/vars/function_param_unnamed.cc b/tests/vars/function_param_unnamed.cc index c8f678bc..5aa93ab9 100644 --- a/tests/vars/function_param_unnamed.cc +++ b/tests/vars/function_param_unnamed.cc @@ -1,10 +1,5 @@ void foo(int, int) {} /* -// TODO: We should probably not emit variables for unnamed variables. But we -// still need to emit reference information. -// TODO: This test is broken. Notice how we only emit one variable because -// unnamed vars have no usr! - OUTPUT: { "types": [], @@ -15,10 +10,6 @@ OUTPUT: "qualified_name": "foo", "definition": "tests/vars/function_param_unnamed.cc:1:6" }], - "variables": [{ - "id": 0, - "declaration": "tests/vars/function_param_unnamed.cc:1:13", - "initializations": ["tests/vars/function_param_unnamed.cc:1:13", "tests/vars/function_param_unnamed.cc:1:18"] - }] + "variables": [] } */ \ No newline at end of file