versioner: use unique_ptr to handle ownership of FTS*.

Bug: None
Test: python run_tests.py
Change-Id: I510063e9b57afda4f5492198cd40c15fc6380d2d
This commit is contained in:
Josh Gao 2016-11-09 18:28:37 -08:00
parent 11b362d284
commit cdbf6fe94f
3 changed files with 14 additions and 11 deletions

View File

@ -439,8 +439,13 @@ bool preprocessHeaders(const std::string& dst_dir, const std::string& src_dir,
// Copy over the original headers before preprocessing.
char* fts_paths[2] = { const_cast<char*>(src_dir.c_str()), nullptr };
FTS* fts = fts_open(fts_paths, FTS_LOGICAL, nullptr);
while (FTSENT* ent = fts_read(fts)) {
std::unique_ptr<FTS, decltype(&fts_close)> fts(fts_open(fts_paths, FTS_LOGICAL, nullptr),
fts_close);
if (!fts) {
err(1, "failed to open directory %s", src_dir.c_str());
}
while (FTSENT* ent = fts_read(fts.get())) {
llvm::StringRef path = ent->fts_path;
if (!path.startswith(src_dir)) {
err(1, "path '%s' doesn't start with source dir '%s'", ent->fts_path, src_dir.c_str());
@ -461,7 +466,6 @@ bool preprocessHeaders(const std::string& dst_dir, const std::string& src_dir,
dst_path.c_str());
}
}
fts_close(fts);
for (const auto& file_it : guards) {
file_lines[file_it.first] = readFileLines(file_it.first);

View File

@ -41,14 +41,14 @@ std::vector<std::string> collectHeaders(const std::string& directory) {
std::vector<std::string> headers;
char* dir_argv[2] = { const_cast<char*>(directory.c_str()), nullptr };
FTS* fts = fts_open(dir_argv, FTS_LOGICAL | FTS_NOCHDIR, nullptr);
std::unique_ptr<FTS, decltype(&fts_close)> fts(
fts_open(dir_argv, FTS_LOGICAL | FTS_NOCHDIR, nullptr), fts_close);
if (!fts) {
err(1, "failed to open directory '%s'", directory.c_str());
}
FTSENT* ent;
while ((ent = fts_read(fts))) {
while (FTSENT* ent = fts_read(fts.get())) {
if (ent->fts_info & (FTS_D | FTS_DP)) {
continue;
}
@ -60,7 +60,6 @@ std::vector<std::string> collectHeaders(const std::string& directory) {
headers.push_back(ent->fts_path);
}
fts_close(fts);
return headers;
}

View File

@ -36,12 +36,14 @@ using namespace clang::vfs;
static void addDirectoryToVFS(InMemoryFileSystem* vfs, const std::string& path) {
char* paths[] = { const_cast<char*>(path.c_str()), nullptr };
FTS* fts = fts_open(paths, FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOCHDIR, nullptr);
std::unique_ptr<FTS, decltype(&fts_close)> fts(
fts_open(paths, FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOCHDIR, nullptr), fts_close);
if (!fts) {
err(1, "failed to open directory %s", path.c_str());
}
while (FTSENT* ent = fts_read(fts)) {
while (FTSENT* ent = fts_read(fts.get())) {
if ((ent->fts_info & FTS_F) == 0) {
continue;
}
@ -61,8 +63,6 @@ static void addDirectoryToVFS(InMemoryFileSystem* vfs, const std::string& path)
errx(1, "failed to add file '%s'", file_path);
}
}
fts_close(fts);
}
llvm::IntrusiveRefCntPtr<FileSystem> createCommonVFS(const std::string& header_dir,