Refactor function_get_[desc/definition] to pass-by-reference

This commit is contained in:
Mahmoud Al-Qudsi 2018-09-26 20:35:30 -05:00
parent a02c54c8c7
commit a0110d296c
4 changed files with 14 additions and 13 deletions

View File

@ -124,8 +124,8 @@ static wcstring functions_def(const wcstring &name) {
CHECK(!name.empty(), L""); //!OCLINT(multiple unary operator) CHECK(!name.empty(), L""); //!OCLINT(multiple unary operator)
wcstring out; wcstring out;
wcstring desc, def; wcstring desc, def;
function_get_desc(name, &desc); function_get_desc(name, desc);
function_get_definition(name, &def); function_get_definition(name, def);
event_t search(EVENT_ANY); event_t search(EVENT_ANY);
search.function_name = name; search.function_name = name;
std::vector<std::shared_ptr<event_t>> ev; std::vector<std::shared_ptr<event_t>> ev;
@ -243,7 +243,7 @@ static int report_function_metadata(const wchar_t *funcname, bool verbose, io_st
path = L"stdin"; path = L"stdin";
} }
shadows_scope = props->shadow_scope ? L"scope-shadowing" : L"no-scope-shadowing"; shadows_scope = props->shadow_scope ? L"scope-shadowing" : L"no-scope-shadowing";
function_get_desc(funcname, &description); function_get_desc(funcname, description);
description = escape_string(description, ESCAPE_NO_QUOTED); description = escape_string(description, ESCAPE_NO_QUOTED);
} }

View File

@ -644,9 +644,9 @@ void completer_t::complete_cmd_desc(const wcstring &str) {
/// Returns a description for the specified function, or an empty string if none. /// Returns a description for the specified function, or an empty string if none.
static wcstring complete_function_desc(const wcstring &fn) { static wcstring complete_function_desc(const wcstring &fn) {
wcstring result; wcstring result;
bool has_description = function_get_desc(fn, &result); bool has_description = function_get_desc(fn, result);
if (!has_description) { if (!has_description) {
function_get_definition(fn, &result); function_get_definition(fn, result);
} }
return result; return result;
} }

View File

@ -228,6 +228,7 @@ void function_remove(const wcstring &name) {
if (function_remove_ignore_autoload(name)) function_autoloader.unload(name); if (function_remove_ignore_autoload(name)) function_autoloader.unload(name);
} }
/// Returns a function by name if it has been loaded, returns false otherwise. Does not autoload.
static const function_info_t *function_get(const wcstring &name) { static const function_info_t *function_get(const wcstring &name) {
// The caller must lock the functions_lock before calling this; however our mutex is currently // The caller must lock the functions_lock before calling this; however our mutex is currently
// recursive, so trylock will never fail. We need a way to correctly check if a lock is locked // recursive, so trylock will never fail. We need a way to correctly check if a lock is locked
@ -240,11 +241,11 @@ static const function_info_t *function_get(const wcstring &name) {
return &iter->second; return &iter->second;
} }
bool function_get_definition(const wcstring &name, wcstring *out_definition) { bool function_get_definition(const wcstring &name, wcstring &out_definition) {
scoped_rlock locker(functions_lock); scoped_rlock locker(functions_lock);
const function_info_t *func = function_get(name); const function_info_t *func = function_get(name);
if (func && out_definition) { if (func) {
out_definition->assign(func->props->body_node.get_source(func->props->parsed_source->src)); out_definition = func->props->body_node.get_source(func->props->parsed_source->src);
} }
return func != NULL; return func != NULL;
} }
@ -255,12 +256,12 @@ std::map<wcstring, env_var_t> function_get_inherit_vars(const wcstring &name) {
return func ? func->inherit_vars : std::map<wcstring, env_var_t>(); return func ? func->inherit_vars : std::map<wcstring, env_var_t>();
} }
bool function_get_desc(const wcstring &name, wcstring *out_desc) { bool function_get_desc(const wcstring &name, wcstring &out_desc) {
// Empty length string goes to NULL. // Empty length string goes to NULL.
scoped_rlock locker(functions_lock); scoped_rlock locker(functions_lock);
const function_info_t *func = function_get(name); const function_info_t *func = function_get(name);
if (out_desc && func && !func->description.empty()) { if (func && !func->description.empty()) {
out_desc->assign(_(func->description.c_str())); out_desc = _(func->description.c_str());
return true; return true;
} }

View File

@ -59,12 +59,12 @@ std::shared_ptr<const function_properties_t> function_get_properties(const wcstr
/// Returns by reference the definition of the function with the name \c name. Returns true if /// Returns by reference the definition of the function with the name \c name. Returns true if
/// successful, false if no function with the given name exists. /// successful, false if no function with the given name exists.
/// This does not trigger autoloading. /// This does not trigger autoloading.
bool function_get_definition(const wcstring &name, wcstring *out_definition); bool function_get_definition(const wcstring &name, wcstring &out_definition);
/// Returns by reference the description of the function with the name \c name. Returns true if the /// Returns by reference the description of the function with the name \c name. Returns true if the
/// function exists and has a nonempty description, false if it does not. /// function exists and has a nonempty description, false if it does not.
/// This does not trigger autoloading. /// This does not trigger autoloading.
bool function_get_desc(const wcstring &name, wcstring *out_desc); bool function_get_desc(const wcstring &name, wcstring &out_desc);
/// Sets the description of the function with the name \c name. /// Sets the description of the function with the name \c name.
void function_set_desc(const wcstring &name, const wcstring &desc); void function_set_desc(const wcstring &name, const wcstring &desc);