From dbfb56bebdb279a2a94096a821d3b7d3345962c2 Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Wed, 1 Dec 2010 00:52:28 +0000 Subject: [PATCH] Support/PathV2: Add remove_filename implementation. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120513 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/PathV2.h | 2 +- lib/Support/PathV2.cpp | 8 ++++++++ unittests/Support/Path.cpp | 4 ++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/llvm/Support/PathV2.h b/include/llvm/Support/PathV2.h index e1b0a1d0e36..c3feca7acc5 100644 --- a/include/llvm/Support/PathV2.h +++ b/include/llvm/Support/PathV2.h @@ -140,7 +140,7 @@ error_code make_absolute(SmallVectorImpl &path); /// @brief Remove the last component from \a path if it exists. /// /// directory/filename.cpp => directory/ -/// directory/ => directory/ +/// directory/ => directory /// /// @param path A path that is modified to not have a file component. /// @returns errc::success if \a path's file name has been removed (or there was diff --git a/lib/Support/PathV2.cpp b/lib/Support/PathV2.cpp index a97f3ec711f..df401bd46ba 100644 --- a/lib/Support/PathV2.cpp +++ b/lib/Support/PathV2.cpp @@ -503,6 +503,14 @@ error_code parent_path(const StringRef &path, StringRef &result) { return make_error_code(errc::success); } +error_code remove_filename(SmallVectorImpl &path) { + size_t end_pos = parent_path_end(StringRef(path.begin(), path.size())); + if (end_pos == StringRef::npos) + return make_error_code(errc::success); + path.set_size(end_pos); + return make_error_code(errc::success); +} + } } } diff --git a/unittests/Support/Path.cpp b/unittests/Support/Path.cpp index b8f818ff1db..5ab74e14f70 100644 --- a/unittests/Support/Path.cpp +++ b/unittests/Support/Path.cpp @@ -99,6 +99,10 @@ TEST(Support, Path) { if (error_code ec = sys::path::make_absolute(temp_store)) ASSERT_FALSE(ec.message().c_str()); outs() << " make_absolute: " << temp_store << '\n'; + temp_store = *i; + if (error_code ec = sys::path::remove_filename(temp_store)) + ASSERT_FALSE(ec.message().c_str()); + outs() << " remove_filename: " << temp_store << '\n'; outs().flush(); }