Index: include/llvm/Support/FileSystem.h =================================================================== --- include/llvm/Support/FileSystem.h +++ include/llvm/Support/FileSystem.h @@ -956,14 +956,18 @@ SmallString<128> path_storage; ec = detail::directory_iterator_construct( *State, path.toStringRef(path_storage), FollowSymlinks); + if (!ec) + check_current_entry(ec); } explicit directory_iterator(const directory_entry &de, std::error_code &ec, bool follow_symlinks = true) - : FollowSymlinks(follow_symlinks) { + : FollowSymlinks(follow_symlinks) { State = std::make_shared(); - ec = - detail::directory_iterator_construct(*State, de.path(), FollowSymlinks); + ec = detail::directory_iterator_construct( + *State, de.path(), FollowSymlinks); + if (!ec) + check_current_entry(ec); } /// Construct end iterator. @@ -972,6 +976,8 @@ // No operator++ because we need error_code. directory_iterator &increment(std::error_code &ec) { ec = directory_iterator_increment(*State); + if (!ec) + check_current_entry(ec); return *this; } @@ -993,6 +999,15 @@ } // Other members as required by // C++ Std, 24.1.1 Input iterators [input.iterators] + +private: + // Checks if current entry is valid and populates error code. For example, + // current entry may not exist due to broken symbol links. + void check_current_entry(std::error_code &ec) { + ErrorOr status = State->CurrentEntry.status(); + if (!status) + ec = status.getError(); + } }; namespace detail { @@ -1030,11 +1045,9 @@ if (State->HasNoPushRequest) State->HasNoPushRequest = false; else { - ErrorOr st = State->Stack.top()->status(); - if (!st) return *this; - if (is_directory(*st)) { + ErrorOr status = State->Stack.top()->status(); + if (status && is_directory(*status)) { State->Stack.push(directory_iterator(*State->Stack.top(), ec, Follow)); - if (ec) return *this; if (State->Stack.top() != end_itr) { ++State->Level; return *this; Index: tools/llvm-cov/CodeCoverage.cpp =================================================================== --- tools/llvm-cov/CodeCoverage.cpp +++ tools/llvm-cov/CodeCoverage.cpp @@ -199,7 +199,7 @@ if (PathRemapping) addCollectedPath(Path); else - error("Missing source file", Path); + warning("Source file doesn't exit, proceeded by ignoring it.", Path); return; } @@ -211,12 +211,16 @@ if (llvm::sys::fs::is_directory(Status)) { std::error_code EC; for (llvm::sys::fs::recursive_directory_iterator F(Path, EC), E; - F != E && !EC; F.increment(EC)) { + F != E; F.increment(EC)) { + + if (EC) { + warning(EC.message(), F->path()); + continue; + } + if (llvm::sys::fs::is_regular_file(F->path())) addCollectedPath(F->path()); } - if (EC) - warning(EC.message(), Path); } } Index: unittests/Support/Path.cpp =================================================================== --- unittests/Support/Path.cpp +++ unittests/Support/Path.cpp @@ -886,13 +886,19 @@ typedef std::vector v_t; v_t visited; - - // The directory iterator doesn't stat the file, so we should be able to - // iterate over the whole directory. + v_t NamesOfBrokenSymlinks = {"a", "ba", "bc", "c", "e"}; std::error_code ec; + + // Broken symbol links are expected to throw an error. for (fs::directory_iterator i(Twine(TestDirectory) + "/symlink", ec), e; i != e; i.increment(ec)) { - ASSERT_NO_ERROR(ec); + std::string CurrentFileName = path::filename(i->path()); + if (std::find(NamesOfBrokenSymlinks.begin(), NamesOfBrokenSymlinks.end(), + CurrentFileName) == NamesOfBrokenSymlinks.end()) { + ASSERT_NO_ERROR(ec); + } else { + ASSERT_TRUE(ec); + } visited.push_back(path::filename(i->path())); } std::sort(visited.begin(), visited.end()); @@ -901,31 +907,27 @@ ASSERT_TRUE(std::equal(visited.begin(), visited.end(), expected.begin())); visited.clear(); - // The recursive directory iterator has to stat the file, so we need to skip - // the broken symlinks. - for (fs::recursive_directory_iterator - i(Twine(TestDirectory) + "/symlink", ec), - e; - i != e; i.increment(ec)) { - ASSERT_NO_ERROR(ec); - - ErrorOr status = i->status(); - if (status.getError() == - std::make_error_code(std::errc::no_such_file_or_directory)) { - i.no_push(); - continue; + // Broken symbol links are expected to throw an error. + for (fs::recursive_directory_iterator i( + Twine(TestDirectory) + "/symlink", ec), e; i != e; i.increment(ec)) { + std::string CurrentFileName = path::filename(i->path()); + if (std::find(NamesOfBrokenSymlinks.begin(), NamesOfBrokenSymlinks.end(), + CurrentFileName) == NamesOfBrokenSymlinks.end()) { + ASSERT_NO_ERROR(ec); + } else { + ASSERT_TRUE(ec); } - visited.push_back(path::filename(i->path())); + visited.push_back(CurrentFileName); } std::sort(visited.begin(), visited.end()); - expected = {"b", "bb", "d", "da", "dd", "ddd", "ddd"}; - ASSERT_TRUE(visited.size() == expected.size()); + // There should be two "ddd" due to the symbol link from "da" to "dd". + expected = {"a", "b", "ba", "bb", "bc", "c", "d", "da", "dd", "ddd", "ddd", + "e"}; + ASSERT_EQ(expected.size(), visited.size()); ASSERT_TRUE(std::equal(visited.begin(), visited.end(), expected.begin())); visited.clear(); - // This recursive directory iterator doesn't follow symlinks, so we don't need - // to skip them. for (fs::recursive_directory_iterator i(Twine(TestDirectory) + "/symlink", ec, /*follow_symlinks=*/false), e;