Skip to content

Commit 58e085a

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 cd25cfb commit 58e085a

20 files changed

+59
-488
lines changed

jbmc/src/java_bytecode/java_class_loader_base.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ Author: Daniel Kroening, [email protected]
1212
#include "java_bytecode_parse_tree.h"
1313
#include "java_bytecode_parser.h"
1414

15-
#include <util/file_util.h>
1615
#include <util/message.h>
1716
#include <util/prefix.h>
1817
#include <util/suffix.h>
1918

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,7 @@ 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 = std::filesystem::path(path).append(class_file);
201201

202202
if(std::ifstream(full_path))
203203
{

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

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

99
#include "load_java_class.h"
1010

11+
#include <filesystem>
1112
#include <iostream>
1213
#include <testing-utils/free_form_cmdline.h>
1314
#include <testing-utils/message.h>
@@ -20,7 +21,6 @@ Author: Diffblue Ltd.
2021
#include <java_bytecode/lazy_goto_model.h>
2122

2223
#include <java_bytecode/java_bytecode_language.h>
23-
#include <util/file_util.h>
2424

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

155155
// 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

+4-3
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,8 +107,8 @@ 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()),
110+
return std::filesystem::path(
111+
id2string(source_location.get_working_directory())).append(
111112
id2string(source_location.get_file()));
112113
}
113114

src/goto-cc/as_mode.cpp

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

22+
#include <filesystem>
2223
#include <fstream>
2324
#include <iostream>
2425

2526
#include <util/cmdline.h>
2627
#include <util/config.h>
27-
#include <util/file_util.h>
2828
#include <util/get_base_name.h>
2929
#include <util/run.h>
3030
#include <util/tempdir.h>
@@ -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

+11-11
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ Date: June 2006
1414
#include "compile.h"
1515

1616
#include <cstring>
17+
#include <filesystem>
1718
#include <fstream>
1819
#include <iostream>
1920

2021
#include <util/cmdline.h>
2122
#include <util/config.h>
22-
#include <util/file_util.h>
2323
#include <util/get_base_name.h>
2424
#include <util/prefix.h>
2525
#include <util/run.h>
@@ -222,11 +222,11 @@ 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
228228
int ret =
229-
run("ar", {"ar", "x", concat_dir_file(working_directory, file_name)});
229+
run("ar", {"ar", "x", std::filesystem::path(working_directory).append(file_name)});
230230
if(ret != 0)
231231
{
232232
log.error() << "Failed to extract archive " << file_name << messaget::eom;
@@ -238,7 +238,7 @@ bool compilet::add_files_from_archive(
238238
temporary_filet tmp_file_out("", "");
239239
int ret = run(
240240
"ar",
241-
{"ar", "t", concat_dir_file(working_directory, file_name)},
241+
{"ar", "t", std::filesystem::path(working_directory).append(file_name)},
242242
"",
243243
tmp_file_out(),
244244
"");
@@ -253,7 +253,7 @@ bool compilet::add_files_from_archive(
253253

254254
while(!in.fail() && std::getline(in, line))
255255
{
256-
std::string t = concat_dir_file(tstr, line);
256+
std::string t = std::filesystem::path(tstr).append(line);
257257

258258
if(is_goto_binary(t, log.get_message_handler()))
259259
object_files.push_back(t);
@@ -263,7 +263,7 @@ bool compilet::add_files_from_archive(
263263
}
264264

265265
if(!thin_archive)
266-
set_current_path(working_directory);
266+
std::filesystem::current_path(working_directory);
267267

268268
return false;
269269
}
@@ -277,15 +277,15 @@ bool compilet::find_library(const std::string &name)
277277

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

282282
std::ifstream in(library_file_name);
283283

284284
if(in.is_open())
285285
return !add_input_file(library_file_name);
286286
else
287287
{
288-
library_file_name = concat_dir_file(library_path, "lib" + name + ".so");
288+
library_file_name = std::filesystem::path(library_path).append("lib" + name + ".so");
289289

290290
switch(detect_file_type(library_file_name, log.get_message_handler()))
291291
{
@@ -414,7 +414,7 @@ optionalt<symbol_tablet> compilet::compile()
414414
get_base_name(file_name, true) + "." + object_file_extension;
415415

416416
if(!output_directory_object.empty())
417-
cfn = concat_dir_file(output_directory_object, file_name_with_obj_ext);
417+
cfn = std::filesystem::path(output_directory_object).append(file_name_with_obj_ext);
418418
else
419419
cfn = file_name_with_obj_ext;
420420
}
@@ -657,7 +657,7 @@ compilet::compilet(cmdlinet &_cmdline, message_handlert &mh, bool Werror)
657657
mode=COMPILE_LINK_EXECUTABLE;
658658
echo_file_name=false;
659659
wrote_object=false;
660-
working_directory=get_current_working_directory();
660+
working_directory=std::filesystem::current_path();
661661

662662
if(cmdline.isset("export-function-local-symbols"))
663663
{
@@ -674,7 +674,7 @@ compilet::~compilet()
674674
// clean up temp dirs
675675

676676
for(const auto &dir : tmp_dirs)
677-
delete_directory(dir);
677+
std::filesystem::remove_all(dir);
678678
}
679679

680680
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
@@ -19,13 +19,13 @@ Author: CM Wintersteiger, 2006
1919
#include <sysexits.h>
2020
#endif
2121

22+
#include <filesystem>
2223
#include <iostream>
2324
#include <fstream>
2425
#include <numeric>
2526

2627
#include <util/cmdline.h>
2728
#include <util/config.h>
28-
#include <util/file_util.h>
2929
#include <util/get_base_name.h>
3030
#include <util/invariant.h>
3131
#include <util/prefix.h>
@@ -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

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ Author: CM Wintersteiger, 2006
2020
#endif
2121

2222
#include <cstring>
23+
#include <filesystem>
2324
#include <fstream>
2425
#include <iostream>
2526

2627
#include <util/cmdline.h>
2728
#include <util/config.h>
28-
#include <util/file_util.h>
2929
#include <util/invariant.h>
3030
#include <util/run.h>
3131

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

src/goto-cc/ms_cl_mode.cpp

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

22+
#include <filesystem>
2223
#include <iostream>
2324

2425
#include <util/config.h>
25-
#include <util/file_util.h>
2626
#include <util/get_base_name.h>
2727
#include <util/message.h>
2828

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

src/goto-instrument/count_eloc.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ Date: December 2012
1313

1414
#include "count_eloc.h"
1515

16+
#include <filesystem>
1617
#include <iostream>
1718
#include <unordered_set>
1819

19-
#include <util/file_util.h>
2020
#include <util/pointer_expr.h>
2121
#include <util/pointer_offset_size.h>
2222
#include <util/prefix.h>
@@ -75,7 +75,7 @@ void list_eloc(const goto_modelt &goto_model)
7575
{
7676
std::string file=id2string(lines.first);
7777
if(!files.first.empty())
78-
file=concat_dir_file(id2string(files.first), file);
78+
file=std::filesystem::path(id2string(files.first)).append(file);
7979

8080
for(const irep_idt &line : lines.second)
8181
std::cout << file << ':' << line << '\n';

src/util/Makefile

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ SRC = arith_tools.cpp \
1717
expr_initializer.cpp \
1818
expr_util.cpp \
1919
exception_utils.cpp \
20-
file_util.cpp \
2120
find_macros.cpp \
2221
find_symbols.cpp \
2322
fixedbv.cpp \

0 commit comments

Comments
 (0)