Skip to content

Commit f153827

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 258301f commit f153827

19 files changed

+103
-514
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/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

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ Author: Michael Tautschnig
2121

2222
#include <util/cmdline.h>
2323
#include <util/config.h>
24-
#include <util/file_util.h>
2524
#include <util/get_base_name.h>
2625
#include <util/run.h>
2726
#include <util/tempdir.h>
@@ -31,6 +30,7 @@ Author: Michael Tautschnig
3130
#include "goto_cc_cmdline.h"
3231
#include "hybrid_binary.h"
3332

33+
#include <filesystem>
3434
#include <fstream> // IWYU pragma: keep
3535
#include <iostream>
3636

@@ -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

+24-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Date: June 2006
1515

1616
#include <util/cmdline.h>
1717
#include <util/config.h>
18-
#include <util/file_util.h>
1918
#include <util/get_base_name.h>
2019
#include <util/prefix.h>
2120
#include <util/run.h>
@@ -39,6 +38,7 @@ Date: June 2006
3938
#include <linking/static_lifetime_init.h>
4039

4140
#include <cstring>
41+
#include <filesystem>
4242
#include <fstream>
4343
#include <iostream>
4444

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

219219
tmp_dirs.push_back(tstr);
220-
set_current_path(tmp_dirs.back());
220+
std::filesystem::current_path(tmp_dirs.back());
221221

222222
// unpack now
223-
int ret =
224-
run("ar", {"ar", "x", concat_dir_file(working_directory, file_name)});
223+
int ret = run(
224+
"ar",
225+
{"ar",
226+
"x",
227+
std::filesystem::path(working_directory).append(file_name).string()});
225228
if(ret != 0)
226229
{
227230
log.error() << "Failed to extract archive " << file_name << messaget::eom;
@@ -233,7 +236,9 @@ bool compilet::add_files_from_archive(
233236
temporary_filet tmp_file_out("", "");
234237
int ret = run(
235238
"ar",
236-
{"ar", "t", concat_dir_file(working_directory, file_name)},
239+
{"ar",
240+
"t",
241+
std::filesystem::path(working_directory).append(file_name).string()},
237242
"",
238243
tmp_file_out(),
239244
"");
@@ -248,7 +253,7 @@ bool compilet::add_files_from_archive(
248253

249254
while(!in.fail() && std::getline(in, line))
250255
{
251-
std::string t = concat_dir_file(tstr, line);
256+
std::string t = std::filesystem::path(tstr).append(line).string();
252257

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

260265
if(!thin_archive)
261-
set_current_path(working_directory);
266+
std::filesystem::current_path(working_directory);
262267

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

273278
for(const auto &library_path : library_paths)
274279
{
275-
library_file_name = concat_dir_file(library_path, "lib" + name + ".a");
280+
library_file_name =
281+
std::filesystem::path(library_path).append("lib" + name + ".a").string();
276282

277283
std::ifstream in(library_file_name);
278284

279285
if(in.is_open())
280286
return !add_input_file(library_file_name);
281287
else
282288
{
283-
library_file_name = concat_dir_file(library_path, "lib" + name + ".so");
289+
library_file_name = std::filesystem::path(library_path)
290+
.append("lib" + name + ".so")
291+
.string();
284292

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

411419
if(!output_directory_object.empty())
412-
cfn = concat_dir_file(output_directory_object, file_name_with_obj_ext);
420+
{
421+
cfn = std::filesystem::path(output_directory_object)
422+
.append(file_name_with_obj_ext)
423+
.string();
424+
}
413425
else
414426
cfn = file_name_with_obj_ext;
415427
}
@@ -648,7 +660,7 @@ compilet::compilet(cmdlinet &_cmdline, message_handlert &mh, bool Werror)
648660
mode=COMPILE_LINK_EXECUTABLE;
649661
echo_file_name=false;
650662
wrote_object=false;
651-
working_directory=get_current_working_directory();
663+
working_directory = std::filesystem::current_path().string();
652664

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

667679
for(const auto &dir : tmp_dirs)
668-
delete_directory(dir);
680+
std::filesystem::remove_all(dir);
669681
}
670682

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

src/goto-cc/gcc_mode.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ Author: CM Wintersteiger, 2006
2121

2222
#include <util/cmdline.h>
2323
#include <util/config.h>
24-
#include <util/file_util.h>
2524
#include <util/get_base_name.h>
2625
#include <util/invariant.h>
2726
#include <util/prefix.h>
@@ -35,6 +34,7 @@ Author: CM Wintersteiger, 2006
3534
#include "hybrid_binary.h"
3635
#include "linker_script_merge.h"
3736

37+
#include <filesystem>
3838
#include <fstream> // IWYU pragma: keep
3939
#include <iostream>
4040
#include <numeric>
@@ -1006,9 +1006,9 @@ int gcc_modet::gcc_hybrid_binary(compilet &compiler)
10061006

10071007
try
10081008
{
1009-
file_rename(*it, bin_name);
1009+
std::filesystem::rename(*it, bin_name);
10101010
}
1011-
catch(const cprover_exception_baset &e)
1011+
catch(const std::filesystem::filesystem_error &e)
10121012
{
10131013
log.error() << "Rename failed: " << e.what() << messaget::eom;
10141014
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
{
@@ -183,9 +183,9 @@ int ld_modet::ld_hybrid_binary(
183183

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

src/goto-cc/ms_cl_mode.cpp

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

22-
#include <iostream>
23-
2422
#include <util/config.h>
25-
#include <util/file_util.h>
2623
#include <util/get_base_name.h>
2724
#include <util/message.h>
2825

2926
#include "compile.h"
3027
#include "ms_cl_version.h"
3128

29+
#include <filesystem>
30+
#include <iostream>
31+
3232
static bool has_directory_suffix(const std::string &path)
3333
{
3434
// MS CL decides whether a parameter is a directory on the
@@ -128,7 +128,7 @@ int ms_cl_modet::doit()
128128
{
129129
compiler.output_directory_object = Fo_value;
130130

131-
if(!is_directory(Fo_value))
131+
if(!std::filesystem::is_directory(Fo_value))
132132
log.warning() << "not a directory: " << Fo_value << messaget::eom;
133133
}
134134
else
@@ -154,7 +154,7 @@ int ms_cl_modet::doit()
154154
has_directory_suffix(compiler.output_file_executable) &&
155155
cmdline.args.size() >= 1)
156156
{
157-
if(!is_directory(compiler.output_file_executable))
157+
if(!std::filesystem::is_directory(compiler.output_file_executable))
158158
{
159159
log.warning() << "not a directory: " << compiler.output_file_executable
160160
<< messaget::eom;

0 commit comments

Comments
 (0)