Skip to content

Replace file_util.{h,cpp} by std::filesystem #8033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions jbmc/src/java_bytecode/java_class_loader_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ Author: Daniel Kroening, [email protected]

#include "java_class_loader_base.h"

#include "jar_file.h"
#include "java_bytecode_parse_tree.h"
#include "java_bytecode_parser.h"

#include <util/file_util.h>
#include <util/message.h>
#include <util/prefix.h>
#include <util/suffix.h>

#include "jar_file.h"
#include "java_bytecode_parse_tree.h"
#include "java_bytecode_parser.h"

#include <filesystem>
#include <fstream>

void java_class_loader_baset::add_classpath_entry(
Expand All @@ -40,7 +40,7 @@ void java_class_loader_baset::add_classpath_entry(
}
else
{
if(is_directory(path))
if(std::filesystem::is_directory(path))
{
classpath_entries.push_back(
classpath_entryt(classpath_entryt::DIRECTORY, path));
Expand Down Expand Up @@ -197,7 +197,8 @@ java_class_loader_baset::get_class_from_directory(
{
// Look in the given directory
const std::string class_file = class_name_to_os_file(class_name);
const std::string full_path = concat_dir_file(path, class_file);
const std::string full_path =
std::filesystem::path(path).append(class_file).string();

if(std::ifstream(full_path))
{
Expand Down
15 changes: 7 additions & 8 deletions jbmc/unit/java-testing-utils/load_java_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@ Author: Diffblue Ltd.

#include "load_java_class.h"

#include <iostream>
#include <testing-utils/free_form_cmdline.h>
#include <testing-utils/message.h>
#include <testing-utils/use_catch.h>

#include <util/config.h>
#include <util/options.h>
#include <util/suffix.h>

#include <java_bytecode/java_bytecode_language.h>
#include <java_bytecode/lazy_goto_model.h>
#include <testing-utils/free_form_cmdline.h>
#include <testing-utils/message.h>
#include <testing-utils/use_catch.h>

#include <java_bytecode/java_bytecode_language.h>
#include <util/file_util.h>
#include <filesystem>
#include <iostream>

/// Go through the process of loading, type-checking and finalising loading a
/// specific class file to build the symbol table. The functions are converted
Expand Down Expand Up @@ -149,7 +148,7 @@ goto_modelt load_goto_model_from_java_class(
// Log the working directory to help people identify the common error
// of wrong working directory (should be the `unit` directory when running
// the unit tests).
std::string path = get_current_working_directory();
std::string path = std::filesystem::current_path().string();
INFO("Working directory: " << path);

// if this fails it indicates the class was not loaded
Expand Down
10 changes: 6 additions & 4 deletions src/goto-analyzer/unreachable_instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Date: April 2016

#include "unreachable_instructions.h"

#include <util/file_util.h>
#include <util/json_irep.h>
#include <util/options.h>
#include <util/xml.h>
Expand All @@ -23,6 +22,8 @@ Date: April 2016
#include <analyses/ai.h>
#include <analyses/cfg_dominators.h>

#include <filesystem>

typedef std::map<unsigned, goto_programt::const_targett> dead_mapt;

static void unreachable_instructions(
Expand Down Expand Up @@ -106,9 +107,10 @@ file_name_string_opt(const source_locationt &source_location)
if(source_location.get_file().empty())
return {};

return concat_dir_file(
id2string(source_location.get_working_directory()),
id2string(source_location.get_file()));
return std::filesystem::path(
id2string(source_location.get_working_directory()))
.append(id2string(source_location.get_file()))
.string();
}

static void add_to_json(
Expand Down
6 changes: 3 additions & 3 deletions src/goto-cc/as_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Author: Michael Tautschnig

#include <util/cmdline.h>
#include <util/config.h>
#include <util/file_util.h>
#include <util/get_base_name.h>
#include <util/run.h>
#include <util/tempdir.h>
Expand All @@ -31,6 +30,7 @@ Author: Michael Tautschnig
#include "goto_cc_cmdline.h"
#include "hybrid_binary.h"

#include <filesystem>
#include <fstream> // IWYU pragma: keep
#include <iostream>

Expand Down Expand Up @@ -303,9 +303,9 @@ int as_modet::as_hybrid_binary(const compilet &compiler)
std::string saved = output_file + ".goto-cc-saved";
try
{
file_rename(output_file, saved);
std::filesystem::rename(output_file, saved);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any tests for this (even manual)? The codecov warning is a bit concerning when refactoring bits like that, which follow some diverging codepaths like exceptions being triggered, etc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the only time this is genuinely put to a test at the moment is the "Build Xen with CPROVER tools" CI job. We don't have regression tests for goto-as (as_modet) at this time.

}
catch(const cprover_exception_baset &e)
catch(const std::filesystem::filesystem_error &e)
{
log.error() << "Rename failed: " << e.what() << messaget::eom;
return 1;
Expand Down
36 changes: 24 additions & 12 deletions src/goto-cc/compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Date: June 2006

#include <util/cmdline.h>
#include <util/config.h>
#include <util/file_util.h>
#include <util/get_base_name.h>
#include <util/prefix.h>
#include <util/run.h>
Expand All @@ -39,6 +38,7 @@ Date: June 2006
#include <linking/static_lifetime_init.h>

#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>

Expand Down Expand Up @@ -217,11 +217,14 @@ bool compilet::add_files_from_archive(
tstr = get_temporary_directory("goto-cc.XXXXXX");

tmp_dirs.push_back(tstr);
set_current_path(tmp_dirs.back());
std::filesystem::current_path(tmp_dirs.back());

// unpack now
int ret =
run("ar", {"ar", "x", concat_dir_file(working_directory, file_name)});
int ret = run(
"ar",
{"ar",
"x",
std::filesystem::path(working_directory).append(file_name).string()});
if(ret != 0)
{
log.error() << "Failed to extract archive " << file_name << messaget::eom;
Expand All @@ -233,7 +236,9 @@ bool compilet::add_files_from_archive(
temporary_filet tmp_file_out("", "");
int ret = run(
"ar",
{"ar", "t", concat_dir_file(working_directory, file_name)},
{"ar",
"t",
std::filesystem::path(working_directory).append(file_name).string()},
"",
tmp_file_out(),
"");
Expand All @@ -248,7 +253,7 @@ bool compilet::add_files_from_archive(

while(!in.fail() && std::getline(in, line))
{
std::string t = concat_dir_file(tstr, line);
std::string t = std::filesystem::path(tstr).append(line).string();

if(is_goto_binary(t, log.get_message_handler()))
object_files.push_back(t);
Expand All @@ -258,7 +263,7 @@ bool compilet::add_files_from_archive(
}

if(!thin_archive)
set_current_path(working_directory);
std::filesystem::current_path(working_directory);

return false;
}
Expand All @@ -272,15 +277,18 @@ bool compilet::find_library(const std::string &name)

for(const auto &library_path : library_paths)
{
library_file_name = concat_dir_file(library_path, "lib" + name + ".a");
library_file_name =
std::filesystem::path(library_path).append("lib" + name + ".a").string();

std::ifstream in(library_file_name);

if(in.is_open())
return !add_input_file(library_file_name);
else
{
library_file_name = concat_dir_file(library_path, "lib" + name + ".so");
library_file_name = std::filesystem::path(library_path)
.append("lib" + name + ".so")
.string();

switch(detect_file_type(library_file_name, log.get_message_handler()))
{
Expand Down Expand Up @@ -409,7 +417,11 @@ optionalt<symbol_tablet> compilet::compile()
get_base_name(file_name, true) + "." + object_file_extension;

if(!output_directory_object.empty())
cfn = concat_dir_file(output_directory_object, file_name_with_obj_ext);
{
cfn = std::filesystem::path(output_directory_object)
.append(file_name_with_obj_ext)
.string();
}
else
cfn = file_name_with_obj_ext;
}
Expand Down Expand Up @@ -648,7 +660,7 @@ compilet::compilet(cmdlinet &_cmdline, message_handlert &mh, bool Werror)
mode=COMPILE_LINK_EXECUTABLE;
echo_file_name=false;
wrote_object=false;
working_directory=get_current_working_directory();
working_directory = std::filesystem::current_path().string();

if(cmdline.isset("export-function-local-symbols"))
{
Expand All @@ -665,7 +677,7 @@ compilet::~compilet()
// clean up temp dirs

for(const auto &dir : tmp_dirs)
delete_directory(dir);
std::filesystem::remove_all(dir);
}

std::size_t compilet::function_body_count(const goto_functionst &functions)
Expand Down
6 changes: 3 additions & 3 deletions src/goto-cc/gcc_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Author: CM Wintersteiger, 2006

#include <util/cmdline.h>
#include <util/config.h>
#include <util/file_util.h>
#include <util/get_base_name.h>
#include <util/invariant.h>
#include <util/prefix.h>
Expand All @@ -35,6 +34,7 @@ Author: CM Wintersteiger, 2006
#include "hybrid_binary.h"
#include "linker_script_merge.h"

#include <filesystem>
#include <fstream> // IWYU pragma: keep
#include <iostream>
#include <numeric>
Expand Down Expand Up @@ -1006,9 +1006,9 @@ int gcc_modet::gcc_hybrid_binary(compilet &compiler)

try
{
file_rename(*it, bin_name);
std::filesystem::rename(*it, bin_name);
}
catch(const cprover_exception_baset &e)
catch(const std::filesystem::filesystem_error &e)
{
log.error() << "Rename failed: " << e.what() << messaget::eom;
return 1;
Expand Down
6 changes: 3 additions & 3 deletions src/goto-cc/hybrid_binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ Author: Michael Tautschnig, 2018

#include "hybrid_binary.h"

#include <util/file_util.h>
#include <util/message.h>
#include <util/run.h>
#include <util/suffix.h>

#include <cstring>
#include <filesystem>

#if defined(__APPLE__)
# include <sys/stat.h>
Expand Down Expand Up @@ -80,7 +80,7 @@ int hybrid_binary(
}

// delete the goto binary
bool remove_result = file_remove(goto_binary_file);
bool remove_result = std::filesystem::remove(goto_binary_file);
if(!remove_result)
{
message.error() << "Remove failed: " << std::strerror(errno)
Expand Down Expand Up @@ -140,7 +140,7 @@ int hybrid_binary(
}

// delete the goto binary
bool remove_result = file_remove(goto_binary_file);
bool remove_result = std::filesystem::remove(goto_binary_file);
if(!remove_result)
{
message.error() << "Remove failed: " << std::strerror(errno)
Expand Down
14 changes: 7 additions & 7 deletions src/goto-cc/ld_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@ Author: CM Wintersteiger, 2006
#include <sysexits.h>
#endif

#include <cstring>
#include <fstream>
#include <iostream>

#include <util/cmdline.h>
#include <util/config.h>
#include <util/file_util.h>
#include <util/invariant.h>
#include <util/run.h>

Expand All @@ -34,6 +29,11 @@ Author: CM Wintersteiger, 2006
#include "hybrid_binary.h"
#include "linker_script_merge.h"

#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>

static std::string
linker_name(const cmdlinet &cmdline, const std::string &base_name)
{
Expand Down Expand Up @@ -183,9 +183,9 @@ int ld_modet::ld_hybrid_binary(

try
{
file_rename(output_file, goto_binary);
std::filesystem::rename(output_file, goto_binary);
}
catch(const cprover_exception_baset &e)
catch(const std::filesystem::filesystem_error &e)
{
log.error() << "Rename failed: " << e.what() << messaget::eom;
return 1;
Expand Down
10 changes: 5 additions & 5 deletions src/goto-cc/ms_cl_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ Author: CM Wintersteiger, 2006
#include <sysexits.h>
#endif

#include <iostream>

#include <util/config.h>
#include <util/file_util.h>
#include <util/get_base_name.h>
#include <util/message.h>

#include "compile.h"
#include "ms_cl_version.h"

#include <filesystem>
#include <iostream>

static bool has_directory_suffix(const std::string &path)
{
// MS CL decides whether a parameter is a directory on the
Expand Down Expand Up @@ -128,7 +128,7 @@ int ms_cl_modet::doit()
{
compiler.output_directory_object = Fo_value;

if(!is_directory(Fo_value))
if(!std::filesystem::is_directory(Fo_value))
log.warning() << "not a directory: " << Fo_value << messaget::eom;
}
else
Expand All @@ -154,7 +154,7 @@ int ms_cl_modet::doit()
has_directory_suffix(compiler.output_file_executable) &&
cmdline.args.size() >= 1)
{
if(!is_directory(compiler.output_file_executable))
if(!std::filesystem::is_directory(compiler.output_file_executable))
{
log.warning() << "not a directory: " << compiler.output_file_executable
<< messaget::eom;
Expand Down
Loading