Skip to content

Commit 3084c7a

Browse files
committed
[Support] Use std::filesystem::remove_all() in remove_directories()
See llvm#118677 for details.
1 parent 14a259f commit 3084c7a

File tree

3 files changed

+8
-66
lines changed

3 files changed

+8
-66
lines changed

llvm/lib/Support/Path.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/Support/Process.h"
2323
#include "llvm/Support/Signals.h"
2424
#include <cctype>
25+
#include <filesystem>
2526

2627
#if !defined(_MSC_VER) && !defined(__MINGW32__)
2728
#include <unistd.h>
@@ -1206,6 +1207,13 @@ namespace llvm {
12061207
namespace sys {
12071208
namespace fs {
12081209

1210+
std::error_code remove_directories(const Twine &path, bool IgnoreErrors) {
1211+
const std::filesystem::path Path(path.str());
1212+
std::error_code EC;
1213+
std::filesystem::remove_all(Path, EC);
1214+
return IgnoreErrors ? std::error_code() : EC;
1215+
}
1216+
12091217
TempFile::TempFile(StringRef Name, int FD)
12101218
: TmpName(std::string(Name)), FD(FD) {}
12111219
TempFile::TempFile(TempFile &&Other) { *this = std::move(Other); }

llvm/lib/Support/Unix/Path.inc

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,46 +1272,6 @@ std::error_code closeFile(file_t &F) {
12721272
return Process::SafelyCloseFileDescriptor(TmpF);
12731273
}
12741274

1275-
template <typename T>
1276-
static std::error_code remove_directories_impl(const T &Entry,
1277-
bool IgnoreErrors) {
1278-
std::error_code EC;
1279-
directory_iterator Begin(Entry, EC, false);
1280-
directory_iterator End;
1281-
while (Begin != End) {
1282-
auto &Item = *Begin;
1283-
ErrorOr<basic_file_status> st = Item.status();
1284-
if (st) {
1285-
if (is_directory(*st)) {
1286-
EC = remove_directories_impl(Item, IgnoreErrors);
1287-
if (EC && !IgnoreErrors)
1288-
return EC;
1289-
}
1290-
1291-
EC = fs::remove(Item.path(), true);
1292-
if (EC && !IgnoreErrors)
1293-
return EC;
1294-
} else if (!IgnoreErrors) {
1295-
return st.getError();
1296-
}
1297-
1298-
Begin.increment(EC);
1299-
if (EC && !IgnoreErrors)
1300-
return EC;
1301-
}
1302-
return std::error_code();
1303-
}
1304-
1305-
std::error_code remove_directories(const Twine &path, bool IgnoreErrors) {
1306-
auto EC = remove_directories_impl(path, IgnoreErrors);
1307-
if (EC && !IgnoreErrors)
1308-
return EC;
1309-
EC = fs::remove(path, true);
1310-
if (EC && !IgnoreErrors)
1311-
return EC;
1312-
return std::error_code();
1313-
}
1314-
13151275
std::error_code real_path(const Twine &path, SmallVectorImpl<char> &dest,
13161276
bool expand_tilde) {
13171277
dest.clear();

llvm/lib/Support/Windows/Path.inc

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,32 +1372,6 @@ std::error_code closeFile(file_t &F) {
13721372
return std::error_code();
13731373
}
13741374

1375-
std::error_code remove_directories(const Twine &path, bool IgnoreErrors) {
1376-
// Convert to utf-16.
1377-
SmallVector<wchar_t, 128> Path16;
1378-
std::error_code EC = widenPath(path, Path16);
1379-
if (EC && !IgnoreErrors)
1380-
return EC;
1381-
1382-
// SHFileOperation() accepts a list of paths, and so must be double null-
1383-
// terminated to indicate the end of the list. The buffer is already null
1384-
// terminated, but since that null character is not considered part of the
1385-
// vector's size, pushing another one will just consume that byte. So we
1386-
// need to push 2 null terminators.
1387-
Path16.push_back(0);
1388-
Path16.push_back(0);
1389-
1390-
SHFILEOPSTRUCTW shfos = {};
1391-
shfos.wFunc = FO_DELETE;
1392-
shfos.pFrom = Path16.data();
1393-
shfos.fFlags = FOF_NO_UI;
1394-
1395-
int result = ::SHFileOperationW(&shfos);
1396-
if (result != 0 && !IgnoreErrors)
1397-
return mapWindowsError(result);
1398-
return std::error_code();
1399-
}
1400-
14011375
static void expandTildeExpr(SmallVectorImpl<char> &Path) {
14021376
// Path does not begin with a tilde expression.
14031377
if (Path.empty() || Path[0] != '~')

0 commit comments

Comments
 (0)