From cdbf6fe94f1907e29aa816471c9d093375ccba2f Mon Sep 17 00:00:00 2001 From: Josh Gao Date: Wed, 9 Nov 2016 18:28:37 -0800 Subject: [PATCH] versioner: use unique_ptr to handle ownership of FTS*. Bug: None Test: python run_tests.py Change-Id: I510063e9b57afda4f5492198cd40c15fc6380d2d --- tools/versioner/src/Preprocessor.cpp | 10 +++++++--- tools/versioner/src/Utils.cpp | 7 +++---- tools/versioner/src/VFS.cpp | 8 ++++---- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/tools/versioner/src/Preprocessor.cpp b/tools/versioner/src/Preprocessor.cpp index c863e6cba..86bb225a7 100644 --- a/tools/versioner/src/Preprocessor.cpp +++ b/tools/versioner/src/Preprocessor.cpp @@ -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(src_dir.c_str()), nullptr }; - FTS* fts = fts_open(fts_paths, FTS_LOGICAL, nullptr); - while (FTSENT* ent = fts_read(fts)) { + std::unique_ptr 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); diff --git a/tools/versioner/src/Utils.cpp b/tools/versioner/src/Utils.cpp index 380611036..ef7facc08 100644 --- a/tools/versioner/src/Utils.cpp +++ b/tools/versioner/src/Utils.cpp @@ -41,14 +41,14 @@ std::vector collectHeaders(const std::string& directory) { std::vector headers; char* dir_argv[2] = { const_cast(directory.c_str()), nullptr }; - FTS* fts = fts_open(dir_argv, FTS_LOGICAL | FTS_NOCHDIR, nullptr); + std::unique_ptr 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 collectHeaders(const std::string& directory) { headers.push_back(ent->fts_path); } - fts_close(fts); return headers; } diff --git a/tools/versioner/src/VFS.cpp b/tools/versioner/src/VFS.cpp index cd6d367b2..1aa7229ae 100644 --- a/tools/versioner/src/VFS.cpp +++ b/tools/versioner/src/VFS.cpp @@ -36,12 +36,14 @@ using namespace clang::vfs; static void addDirectoryToVFS(InMemoryFileSystem* vfs, const std::string& path) { char* paths[] = { const_cast(path.c_str()), nullptr }; - FTS* fts = fts_open(paths, FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOCHDIR, nullptr); + std::unique_ptr 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 createCommonVFS(const std::string& header_dir,