Add basic RAII wrapper for DIR*

At some point we should also fold the wreaddir_* functions into this new
dir_t struct as well.
This commit is contained in:
Mahmoud Al-Qudsi
2018-09-22 06:28:19 -05:00
parent 794b448c00
commit d31cc08fd9
3 changed files with 50 additions and 23 deletions

View File

@@ -30,27 +30,26 @@
#include "wutil.h" // IWYU pragma: keep
class function_info_t {
public:
/// Immutable properties of the function.
std::shared_ptr<const function_properties_t> props;
/// Function description. This may be changed after the function is created.
wcstring description;
/// File where this function was defined (intern'd string).
const wchar_t *const definition_file;
/// Mapping of all variables that were inherited from the function definition scope to their
/// values.
const std::map<wcstring, env_var_t> inherit_vars;
/// Flag for specifying that this function was automatically loaded.
const bool is_autoload;
public:
/// Immutable properties of the function.
std::shared_ptr<const function_properties_t> props;
/// Function description. This may be changed after the function is created.
wcstring description;
/// File where this function was defined (intern'd string).
const wchar_t *const definition_file;
/// Mapping of all variables that were inherited from the function definition scope to their
/// values.
const std::map<wcstring, env_var_t> inherit_vars;
/// Flag for specifying that this function was automatically loaded.
const bool is_autoload;
/// Constructs relevant information from the function_data.
function_info_t(function_data_t data, const wchar_t *filename, bool autoload);
/// Constructs relevant information from the function_data.
function_info_t(function_data_t data, const wchar_t *filename, bool autoload);
/// Used by function_copy.
function_info_t(const function_info_t &data, const wchar_t *filename, bool autoload);
/// Used by function_copy.
function_info_t(const function_info_t &data, const wchar_t *filename, bool autoload);
};
/// Table containing all functions.
typedef std::unordered_map<wcstring, function_info_t> function_map_t;
static function_map_t loaded_functions;
@@ -110,12 +109,11 @@ static void autoload_names(std::unordered_set<wcstring> &names, int get_hidden)
for (i = 0; i < path_list.size(); i++) {
const wcstring &ndir_str = path_list.at(i);
const wchar_t *ndir = (wchar_t *)ndir_str.c_str();
DIR *dir = wopendir(ndir);
if (!dir) continue;
dir_t dir(ndir_str);
if (!dir.valid()) continue;
wcstring name;
while (wreaddir(dir, name)) {
while (dir.read(name)) {
const wchar_t *fn = name.c_str();
const wchar_t *suffix;
if (!get_hidden && fn[0] == L'_') continue;
@@ -126,7 +124,6 @@ static void autoload_names(std::unordered_set<wcstring> &names, int get_hidden)
names.insert(name);
}
}
closedir(dir);
}
}
@@ -327,7 +324,8 @@ int function_get_definition_lineno(const wcstring &name) {
scoped_rlock locker(functions_lock);
const function_info_t *func = function_get(name);
if (!func) return -1;
// return one plus the number of newlines at offsets less than the start of our function's statement (which includes the header).
// return one plus the number of newlines at offsets less than the start of our function's
// statement (which includes the header).
// TODO: merge with line_offset_of_character_at_offset?
auto block_stat = func->props->body_node.try_get_parent<grammar::block_statement>();
assert(block_stat && "Function body is not part of block statement");