Skip to content

Commit 5d1b0e8

Browse files
committed
Replace file_util.{h,cpp} by std::filesystem
With C++ 17 we can use the STL-provided implementation instead of rolling our own (platform-dependent) code.
1 parent aaf66b0 commit 5d1b0e8

20 files changed

+114
-526
lines changed

jbmc/src/java_bytecode/java_class_loader_base.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ Author: Daniel Kroening, [email protected]
88

99
#include "java_class_loader_base.h"
1010

11-
#include "jar_file.h"
12-
#include "java_bytecode_parse_tree.h"
13-
#include "java_bytecode_parser.h"
14-
15-
#include <util/file_util.h>
1611
#include <util/message.h>
1712
#include <util/prefix.h>
1813
#include <util/suffix.h>
1914

15+
#include "jar_file.h"
16+
#include "java_bytecode_parse_tree.h"
17+
#include "java_bytecode_parser.h"
18+
19+
#include <filesystem>
2020
#include <fstream>
2121

2222
void java_class_loader_baset::add_classpath_entry(
@@ -40,7 +40,7 @@ void java_class_loader_baset::add_classpath_entry(
4040
}
4141
else
4242
{
43-
if(is_directory(path))
43+
if(std::filesystem::is_directory(path))
4444
{
4545
classpath_entries.push_back(
4646
classpath_entryt(classpath_entryt::DIRECTORY, path));
@@ -197,7 +197,8 @@ java_class_loader_baset::get_class_from_directory(
197197
{
198198
// Look in the given directory
199199
const std::string class_file = class_name_to_os_file(class_name);
200-
const std::string full_path = concat_dir_file(path, class_file);
200+
const std::string full_path =
201+
std::filesystem::path(path).append(class_file).string();
201202

202203
if(std::ifstream(full_path))
203204
{

jbmc/unit/java-testing-utils/load_java_class.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,18 @@ Author: Diffblue Ltd.
88

99
#include "load_java_class.h"
1010

11-
#include <iostream>
12-
#include <testing-utils/free_form_cmdline.h>
13-
#include <testing-utils/message.h>
14-
#include <testing-utils/use_catch.h>
15-
1611
#include <util/config.h>
1712
#include <util/options.h>
1813
#include <util/suffix.h>
1914

15+
#include <java_bytecode/java_bytecode_language.h>
2016
#include <java_bytecode/lazy_goto_model.h>
17+
#include <testing-utils/free_form_cmdline.h>
18+
#include <testing-utils/message.h>
19+
#include <testing-utils/use_catch.h>
2120

22-
#include <java_bytecode/java_bytecode_language.h>
23-
#include <util/file_util.h>
21+
#include <filesystem>
22+
#include <iostream>
2423

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

155154
// if this fails it indicates the class was not loaded

src/crangler/c_wrangler.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ Author: Daniel Kroening, [email protected]
1717

1818
#include <util/cprover_prefix.h>
1919
#include <util/exception_utils.h>
20-
#include <util/file_util.h>
2120
#include <util/json.h>
2221
#include <util/optional.h>
2322
#include <util/prefix.h>

src/goto-analyzer/unreachable_instructions.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Date: April 2016
1313

1414
#include "unreachable_instructions.h"
1515

16-
#include <util/file_util.h>
1716
#include <util/json_irep.h>
1817
#include <util/options.h>
1918
#include <util/xml.h>
@@ -23,6 +22,8 @@ Date: April 2016
2322
#include <analyses/ai.h>
2423
#include <analyses/cfg_dominators.h>
2524

25+
#include <filesystem>
26+
2627
typedef std::map<unsigned, goto_programt::const_targett> dead_mapt;
2728

2829
static void unreachable_instructions(
@@ -106,9 +107,10 @@ file_name_string_opt(const source_locationt &source_location)
106107
if(source_location.get_file().empty())
107108
return {};
108109

109-
return concat_dir_file(
110-
id2string(source_location.get_working_directory()),
111-
id2string(source_location.get_file()));
110+
return std::filesystem::path(
111+
id2string(source_location.get_working_directory()))
112+
.append(id2string(source_location.get_file()))
113+
.string();
112114
}
113115

114116
static void add_to_json(

src/goto-cc/as_mode.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,8 @@ Author: Michael Tautschnig
1919
#include <sysexits.h>
2020
#endif
2121

22-
#include <fstream>
23-
#include <iostream>
24-
2522
#include <util/cmdline.h>
2623
#include <util/config.h>
27-
#include <util/file_util.h>
2824
#include <util/get_base_name.h>
2925
#include <util/run.h>
3026
#include <util/tempdir.h>
@@ -34,6 +30,10 @@ Author: Michael Tautschnig
3430
#include "goto_cc_cmdline.h"
3531
#include "hybrid_binary.h"
3632

33+
#include <filesystem>
34+
#include <fstream>
35+
#include <iostream>
36+
3737
static std::string assembler_name(
3838
const cmdlinet &cmdline,
3939
const std::string &base_name)
@@ -303,9 +303,9 @@ int as_modet::as_hybrid_binary(const compilet &compiler)
303303
std::string saved = output_file + ".goto-cc-saved";
304304
try
305305
{
306-
file_rename(output_file, saved);
306+
std::filesystem::rename(output_file, saved);
307307
}
308-
catch(const cprover_exception_baset &e)
308+
catch(const std::filesystem::filesystem_error &e)
309309
{
310310
log.error() << "Rename failed: " << e.what() << messaget::eom;
311311
return 1;

src/goto-cc/compile.cpp

+28-16
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,8 @@ Date: June 2006
1313

1414
#include "compile.h"
1515

16-
#include <cstring>
17-
#include <fstream>
18-
#include <iostream>
19-
2016
#include <util/cmdline.h>
2117
#include <util/config.h>
22-
#include <util/file_util.h>
2318
#include <util/get_base_name.h>
2419
#include <util/prefix.h>
2520
#include <util/run.h>
@@ -28,6 +23,11 @@ Date: June 2006
2823
#include <util/tempfile.h>
2924
#include <util/version.h>
3025

26+
#include <cstring>
27+
#include <filesystem>
28+
#include <fstream>
29+
#include <iostream>
30+
3131
#ifdef _MSC_VER
3232
# include <util/unicode.h>
3333
#endif
@@ -222,11 +222,14 @@ bool compilet::add_files_from_archive(
222222
tstr = get_temporary_directory("goto-cc.XXXXXX");
223223

224224
tmp_dirs.push_back(tstr);
225-
set_current_path(tmp_dirs.back());
225+
std::filesystem::current_path(tmp_dirs.back());
226226

227227
// unpack now
228-
int ret =
229-
run("ar", {"ar", "x", concat_dir_file(working_directory, file_name)});
228+
int ret = run(
229+
"ar",
230+
{"ar",
231+
"x",
232+
std::filesystem::path(working_directory).append(file_name).string()});
230233
if(ret != 0)
231234
{
232235
log.error() << "Failed to extract archive " << file_name << messaget::eom;
@@ -238,7 +241,9 @@ bool compilet::add_files_from_archive(
238241
temporary_filet tmp_file_out("", "");
239242
int ret = run(
240243
"ar",
241-
{"ar", "t", concat_dir_file(working_directory, file_name)},
244+
{"ar",
245+
"t",
246+
std::filesystem::path(working_directory).append(file_name).string()},
242247
"",
243248
tmp_file_out(),
244249
"");
@@ -253,7 +258,7 @@ bool compilet::add_files_from_archive(
253258

254259
while(!in.fail() && std::getline(in, line))
255260
{
256-
std::string t = concat_dir_file(tstr, line);
261+
std::string t = std::filesystem::path(tstr).append(line).string();
257262

258263
if(is_goto_binary(t, log.get_message_handler()))
259264
object_files.push_back(t);
@@ -263,7 +268,7 @@ bool compilet::add_files_from_archive(
263268
}
264269

265270
if(!thin_archive)
266-
set_current_path(working_directory);
271+
std::filesystem::current_path(working_directory);
267272

268273
return false;
269274
}
@@ -277,15 +282,18 @@ bool compilet::find_library(const std::string &name)
277282

278283
for(const auto &library_path : library_paths)
279284
{
280-
library_file_name = concat_dir_file(library_path, "lib" + name + ".a");
285+
library_file_name =
286+
std::filesystem::path(library_path).append("lib" + name + ".a").string();
281287

282288
std::ifstream in(library_file_name);
283289

284290
if(in.is_open())
285291
return !add_input_file(library_file_name);
286292
else
287293
{
288-
library_file_name = concat_dir_file(library_path, "lib" + name + ".so");
294+
library_file_name = std::filesystem::path(library_path)
295+
.append("lib" + name + ".so")
296+
.string();
289297

290298
switch(detect_file_type(library_file_name, log.get_message_handler()))
291299
{
@@ -414,7 +422,11 @@ optionalt<symbol_tablet> compilet::compile()
414422
get_base_name(file_name, true) + "." + object_file_extension;
415423

416424
if(!output_directory_object.empty())
417-
cfn = concat_dir_file(output_directory_object, file_name_with_obj_ext);
425+
{
426+
cfn = std::filesystem::path(output_directory_object)
427+
.append(file_name_with_obj_ext)
428+
.string();
429+
}
418430
else
419431
cfn = file_name_with_obj_ext;
420432
}
@@ -657,7 +669,7 @@ compilet::compilet(cmdlinet &_cmdline, message_handlert &mh, bool Werror)
657669
mode=COMPILE_LINK_EXECUTABLE;
658670
echo_file_name=false;
659671
wrote_object=false;
660-
working_directory=get_current_working_directory();
672+
working_directory = std::filesystem::current_path().string();
661673

662674
if(cmdline.isset("export-function-local-symbols"))
663675
{
@@ -674,7 +686,7 @@ compilet::~compilet()
674686
// clean up temp dirs
675687

676688
for(const auto &dir : tmp_dirs)
677-
delete_directory(dir);
689+
std::filesystem::remove_all(dir);
678690
}
679691

680692
std::size_t compilet::function_body_count(const goto_functionst &functions)

src/goto-cc/gcc_mode.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,8 @@ Author: CM Wintersteiger, 2006
1919
#include <sysexits.h>
2020
#endif
2121

22-
#include <iostream>
23-
#include <fstream>
24-
#include <numeric>
25-
2622
#include <util/cmdline.h>
2723
#include <util/config.h>
28-
#include <util/file_util.h>
2924
#include <util/get_base_name.h>
3025
#include <util/invariant.h>
3126
#include <util/prefix.h>
@@ -39,6 +34,11 @@ Author: CM Wintersteiger, 2006
3934
#include "hybrid_binary.h"
4035
#include "linker_script_merge.h"
4136

37+
#include <filesystem>
38+
#include <fstream>
39+
#include <iostream>
40+
#include <numeric>
41+
4242
static std::string compiler_name(
4343
const cmdlinet &cmdline,
4444
const std::string &base_name)
@@ -945,9 +945,9 @@ int gcc_modet::gcc_hybrid_binary(compilet &compiler)
945945

946946
try
947947
{
948-
file_rename(*it, bin_name);
948+
std::filesystem::rename(*it, bin_name);
949949
}
950-
catch(const cprover_exception_baset &e)
950+
catch(const std::filesystem::filesystem_error &e)
951951
{
952952
log.error() << "Rename failed: " << e.what() << messaget::eom;
953953
return 1;

src/goto-cc/hybrid_binary.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ Author: Michael Tautschnig, 2018
1111

1212
#include "hybrid_binary.h"
1313

14-
#include <util/file_util.h>
1514
#include <util/message.h>
1615
#include <util/run.h>
1716
#include <util/suffix.h>
1817

1918
#include <cstring>
19+
#include <filesystem>
2020

2121
#if defined(__APPLE__)
2222
# include <sys/stat.h>
@@ -80,7 +80,7 @@ int hybrid_binary(
8080
}
8181

8282
// delete the goto binary
83-
bool remove_result = file_remove(goto_binary_file);
83+
bool remove_result = std::filesystem::remove(goto_binary_file);
8484
if(!remove_result)
8585
{
8686
message.error() << "Remove failed: " << std::strerror(errno)
@@ -140,7 +140,7 @@ int hybrid_binary(
140140
}
141141

142142
// delete the goto binary
143-
bool remove_result = file_remove(goto_binary_file);
143+
bool remove_result = std::filesystem::remove(goto_binary_file);
144144
if(!remove_result)
145145
{
146146
message.error() << "Remove failed: " << std::strerror(errno)

src/goto-cc/ld_mode.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,8 @@ Author: CM Wintersteiger, 2006
1919
#include <sysexits.h>
2020
#endif
2121

22-
#include <cstring>
23-
#include <fstream>
24-
#include <iostream>
25-
2622
#include <util/cmdline.h>
2723
#include <util/config.h>
28-
#include <util/file_util.h>
2924
#include <util/invariant.h>
3025
#include <util/run.h>
3126

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

32+
#include <cstring>
33+
#include <filesystem>
34+
#include <fstream>
35+
#include <iostream>
36+
3737
static std::string
3838
linker_name(const cmdlinet &cmdline, const std::string &base_name)
3939
{
@@ -181,9 +181,9 @@ int ld_modet::ld_hybrid_binary(
181181

182182
try
183183
{
184-
file_rename(output_file, goto_binary);
184+
std::filesystem::rename(output_file, goto_binary);
185185
}
186-
catch(const cprover_exception_baset &e)
186+
catch(const std::filesystem::filesystem_error &e)
187187
{
188188
log.error() << "Rename failed: " << e.what() << messaget::eom;
189189
return 1;

0 commit comments

Comments
 (0)