diff --git a/include/llvm/Support/PathV2.h b/include/llvm/Support/PathV2.h index 475082812d4..82d31132bf0 100644 --- a/include/llvm/Support/PathV2.h +++ b/include/llvm/Support/PathV2.h @@ -11,8 +11,6 @@ // TR2/boost filesystem (v3), but modified to remove exception handling and the // path class. // -// All functions return void and their actual work via the last out argument. -// //===----------------------------------------------------------------------===// #ifndef LLVM_SUPPORT_PATHV2_H @@ -126,8 +124,7 @@ void remove_filename(SmallVectorImpl &path); /// @param extension The extension to be added. It may be empty. It may also /// optionally start with a '.', if it does not, one will be /// prepended. -void replace_extension(SmallVectorImpl &path, - const Twine &extension); +void replace_extension(SmallVectorImpl &path, const Twine &extension); /// @brief Append to path. /// @@ -138,9 +135,9 @@ void replace_extension(SmallVectorImpl &path, /// @param path Set to \a path + \a component. /// @param component The component to be appended to \a path. void append(SmallVectorImpl &path, const Twine &a, - const Twine &b = "", - const Twine &c = "", - const Twine &d = ""); + const Twine &b = "", + const Twine &c = "", + const Twine &d = ""); /// @brief Append to path. /// @@ -152,7 +149,7 @@ void append(SmallVectorImpl &path, const Twine &a, /// @param begin Start of components to append. /// @param end One past the end of components to append. void append(SmallVectorImpl &path, - const_iterator begin, const_iterator end); + const_iterator begin, const_iterator end); /// @} /// @name Transforms (or some other better name) @@ -177,8 +174,8 @@ void native(const Twine &path, SmallVectorImpl &result); /// /hello => /// /// @param path Input path. -/// @param result Set to the root name of \a path if it has one, otherwise "". -void root_name(const StringRef &path, StringRef &result); +/// @result The root name of \a path if it has one, otherwise "". +const StringRef root_name(const StringRef &path); /// @brief Get root directory. /// @@ -187,17 +184,17 @@ void root_name(const StringRef &path, StringRef &result); /// d/file.txt => /// /// @param path Input path. -/// @param result Set to the root directory of \a path if it has one, otherwise +/// @result The root directory of \a path if it has one, otherwise /// "". -void root_directory(const StringRef &path, StringRef &result); +const StringRef root_directory(const StringRef &path); /// @brief Get root path. /// /// Equivalent to root_name + root_directory. /// /// @param path Input path. -/// @param result Set to the root path of \a path if it has one, otherwise "". -void root_path(const StringRef &path, StringRef &result); +/// @result The root path of \a path if it has one, otherwise "". +const StringRef root_path(const StringRef &path); /// @brief Get relative path. /// @@ -206,9 +203,8 @@ void root_path(const StringRef &path, StringRef &result); /// /foo/bar => foo/bar /// /// @param path Input path. -/// @param result Set to the path starting after root_path if one exists, -/// otherwise "". -void relative_path(const StringRef &path, StringRef &result); +/// @result The path starting after root_path if one exists, otherwise "". +const StringRef relative_path(const StringRef &path); /// @brief Get parent path. /// @@ -217,8 +213,8 @@ void relative_path(const StringRef &path, StringRef &result); /// foo/../bar => foo/.. /// /// @param path Input path. -/// @param result Set to the parent path of \a path if one exists, otherwise "". -void parent_path(const StringRef &path, StringRef &result); +/// @result The parent path of \a path if one exists, otherwise "". +const StringRef parent_path(const StringRef &path); /// @brief Get filename. /// @@ -228,9 +224,9 @@ void parent_path(const StringRef &path, StringRef &result); /// / => / /// /// @param path Input path. -/// @param result Set to the filename part of \a path. This is defined as the -/// last component of \a path. -void filename(const StringRef &path, StringRef &result); +/// @result The filename part of \a path. This is defined as the last component +/// of \a path. +const StringRef filename(const StringRef &path); /// @brief Get stem. /// @@ -245,8 +241,8 @@ void filename(const StringRef &path, StringRef &result); /// /foo/.. => .. /// /// @param path Input path. -/// @param result Set to the stem of \a path. -void stem(const StringRef &path, StringRef &result); +/// @result The stem of \a path. +const StringRef stem(const StringRef &path); /// @brief Get extension. /// @@ -259,84 +255,84 @@ void stem(const StringRef &path, StringRef &result); /// /foo/.txt => .txt /// /// @param path Input path. -/// @param result Set to the extension of \a path. -void extension(const StringRef &path, StringRef &result); +/// @result The extension of \a path. +const StringRef extension(const StringRef &path); /// @brief Has root name? /// /// root_name != "" /// /// @param path Input path. -/// @param result Set to true if the path has a root name, false otherwise. -void has_root_name(const Twine &path, bool &result); +/// @result True if the path has a root name, false otherwise. +const bool has_root_name(const Twine &path); /// @brief Has root directory? /// /// root_directory != "" /// /// @param path Input path. -/// @param result Set to true if the path has a root directory, false otherwise. -void has_root_directory(const Twine &path, bool &result); +/// @result True if the path has a root directory, false otherwise. +const bool has_root_directory(const Twine &path); /// @brief Has root path? /// /// root_path != "" /// /// @param path Input path. -/// @param result Set to true if the path has a root path, false otherwise. -void has_root_path(const Twine &path, bool &result); +/// @result True if the path has a root path, false otherwise. +const bool has_root_path(const Twine &path); /// @brief Has relative path? /// /// relative_path != "" /// /// @param path Input path. -/// @param result Set to true if the path has a relative path, false otherwise. -void has_relative_path(const Twine &path, bool &result); +/// @result True if the path has a relative path, false otherwise. +const bool has_relative_path(const Twine &path); /// @brief Has parent path? /// /// parent_path != "" /// /// @param path Input path. -/// @param result Set to true if the path has a parent path, false otherwise. -void has_parent_path(const Twine &path, bool &result); +/// @result True if the path has a parent path, false otherwise. +const bool has_parent_path(const Twine &path); /// @brief Has filename? /// /// filename != "" /// /// @param path Input path. -/// @param result Set to true if the path has a filename, false otherwise. -void has_filename(const Twine &path, bool &result); +/// @result True if the path has a filename, false otherwise. +const bool has_filename(const Twine &path); /// @brief Has stem? /// /// stem != "" /// /// @param path Input path. -/// @param result Set to true if the path has a stem, false otherwise. -void has_stem(const Twine &path, bool &result); +/// @result True if the path has a stem, false otherwise. +const bool has_stem(const Twine &path); /// @brief Has extension? /// /// extension != "" /// /// @param path Input path. -/// @param result Set to true if the path has a extension, false otherwise. -void has_extension(const Twine &path, bool &result); +/// @result True if the path has a extension, false otherwise. +const bool has_extension(const Twine &path); /// @brief Is path absolute? /// /// @param path Input path. -/// @param result Set to true if the path is absolute, false if it is not. -void is_absolute(const Twine &path, bool &result); +/// @result True if the path is absolute, false if it is not. +const bool is_absolute(const Twine &path); /// @brief Is path relative? /// /// @param path Input path. -/// @param result Set to true if the path is relative, false if it is not. -void is_relative(const Twine &path, bool &result); +/// @result True if the path is relative, false if it is not. +const bool is_relative(const Twine &path); } // end namespace path } // end namespace sys diff --git a/lib/Support/PathV2.cpp b/lib/Support/PathV2.cpp index 728f0360230..2088c53edb9 100644 --- a/lib/Support/PathV2.cpp +++ b/lib/Support/PathV2.cpp @@ -279,7 +279,7 @@ ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const { return Position - RHS.Position; } -void root_path(const StringRef &path, StringRef &result) { +const StringRef root_path(const StringRef &path) { const_iterator b = begin(path), pos = b, e = end(path); @@ -295,26 +295,23 @@ void root_path(const StringRef &path, StringRef &result) { if (has_net || has_drive) { if ((++pos != e) && is_separator((*pos)[0])) { // {C:/,//net/}, so get the first two components. - result = StringRef(path.begin(), b->size() + pos->size()); - return; + return StringRef(path.begin(), b->size() + pos->size()); } else { // just {C:,//net}, return the first component. - result = *b; - return; + return *b; } } // POSIX style root directory. if (is_separator((*b)[0])) { - result = *b; - return; + return *b; } } - result = StringRef(); + return StringRef(); } -void root_name(const StringRef &path, StringRef &result) { +const StringRef root_name(const StringRef &path) { const_iterator b = begin(path), e = end(path); if (b != e) { @@ -328,17 +325,15 @@ void root_name(const StringRef &path, StringRef &result) { if (has_net || has_drive) { // just {C:,//net}, return the first component. - result = *b; - return; + return *b; } } // No path or no name. - result = StringRef(); - return; + return StringRef(); } -void root_directory(const StringRef &path, StringRef &result) { +const StringRef root_directory(const StringRef &path) { const_iterator b = begin(path), pos = b, e = end(path); @@ -354,27 +349,22 @@ void root_directory(const StringRef &path, StringRef &result) { if ((has_net || has_drive) && // {C:,//net}, skip to the next component. (++pos != e) && is_separator((*pos)[0])) { - result = *pos; - return; + return *pos; } // POSIX style root directory. if (!has_net && is_separator((*b)[0])) { - result = *b; - return; + return *b; } } // No path or no root. - result = StringRef(); - return; + return StringRef(); } -void relative_path(const StringRef &path, StringRef &result) { - StringRef root; - root_path(path, root); - result = StringRef(path.begin() + root.size(), path.size() - root.size()); - return; +const StringRef relative_path(const StringRef &path) { + StringRef root = root_path(path); + return StringRef(path.begin() + root.size(), path.size() - root.size()); } void append(SmallVectorImpl &path, const Twine &a, @@ -397,8 +387,7 @@ void append(SmallVectorImpl &path, const Twine &a, i != e; ++i) { bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]); bool component_has_sep = !i->empty() && is_separator((*i)[0]); - bool is_root_name = false; - has_root_name(*i, is_root_name); + bool is_root_name = has_root_name(*i); if (path_has_sep) { // Strip separators from beginning of component. @@ -419,12 +408,12 @@ void append(SmallVectorImpl &path, const Twine &a, } } -void parent_path(const StringRef &path, StringRef &result) { +const StringRef parent_path(const StringRef &path) { size_t end_pos = parent_path_end(path); if (end_pos == StringRef::npos) - result = StringRef(); + return StringRef(); else - result = StringRef(path.data(), end_pos); + return StringRef(path.data(), end_pos); } void remove_filename(SmallVectorImpl &path) { @@ -433,8 +422,7 @@ void remove_filename(SmallVectorImpl &path) { path.set_size(end_pos); } -void replace_extension(SmallVectorImpl &path, - const Twine &extension) { +void replace_extension(SmallVectorImpl &path, const Twine &extension) { StringRef p(path.begin(), path.size()); SmallString<32> ext_storage; StringRef ext = extension.toStringRef(ext_storage); @@ -473,121 +461,101 @@ void native(const Twine &path, SmallVectorImpl &result) { #endif } -void filename(const StringRef &path, StringRef &result) { - result = *(--end(path)); +const StringRef filename(const StringRef &path) { + return *(--end(path)); } -void stem(const StringRef &path, StringRef &result) { - StringRef fname; - filename(path, fname); +const StringRef stem(const StringRef &path) { + StringRef fname = filename(path); size_t pos = fname.find_last_of('.'); if (pos == StringRef::npos) - result = fname; + return fname; else if ((fname.size() == 1 && fname == ".") || (fname.size() == 2 && fname == "..")) - result = fname; + return fname; else - result = StringRef(fname.begin(), pos); + return StringRef(fname.begin(), pos); } -void extension(const StringRef &path, StringRef &result) { - StringRef fname; - filename(path, fname); +const StringRef extension(const StringRef &path) { + StringRef fname = filename(path); size_t pos = fname.find_last_of('.'); if (pos == StringRef::npos) - result = StringRef(); + return StringRef(); else if ((fname.size() == 1 && fname == ".") || (fname.size() == 2 && fname == "..")) - result = StringRef(); + return StringRef(); else - result = StringRef(fname.begin() + pos, fname.size() - pos); + return StringRef(fname.begin() + pos, fname.size() - pos); } -void has_root_name(const Twine &path, bool &result) { +const bool has_root_name(const Twine &path) { SmallString<128> path_storage; StringRef p = path.toStringRef(path_storage); - root_name(p, p); - - result = !p.empty(); + return !root_name(p).empty(); } -void has_root_directory(const Twine &path, bool &result) { +const bool has_root_directory(const Twine &path) { SmallString<128> path_storage; StringRef p = path.toStringRef(path_storage); - root_directory(p, p); - - result = !p.empty(); + return !root_directory(p).empty(); } -void has_root_path(const Twine &path, bool &result) { +const bool has_root_path(const Twine &path) { SmallString<128> path_storage; StringRef p = path.toStringRef(path_storage); - root_path(p, p); - - result = !p.empty(); + return !root_path(p).empty(); } -void has_filename(const Twine &path, bool &result) { +const bool has_filename(const Twine &path) { SmallString<128> path_storage; StringRef p = path.toStringRef(path_storage); - filename(p, p); - - result = !p.empty(); + return !filename(p).empty(); } -void has_parent_path(const Twine &path, bool &result) { +const bool has_parent_path(const Twine &path) { SmallString<128> path_storage; StringRef p = path.toStringRef(path_storage); - parent_path(p, p); - - result = !p.empty(); + return !parent_path(p).empty(); } -void has_stem(const Twine &path, bool &result) { +const bool has_stem(const Twine &path) { SmallString<128> path_storage; StringRef p = path.toStringRef(path_storage); - stem(p, p); - - result = !p.empty(); + return !stem(p).empty(); } -void has_extension(const Twine &path, bool &result) { +const bool has_extension(const Twine &path) { SmallString<128> path_storage; StringRef p = path.toStringRef(path_storage); - extension(p, p); - - result = !p.empty(); + return !extension(p).empty(); } -void is_absolute(const Twine &path, bool &result) { +const bool is_absolute(const Twine &path) { SmallString<128> path_storage; StringRef p = path.toStringRef(path_storage); - bool rootDir = false, - rootName = false; - has_root_directory(p, rootDir); + bool rootDir = has_root_directory(p), #ifdef LLVM_ON_WIN32 - has_root_name(p, rootName); + rootName = has_root_name(p); #else - rootName = true; + rootName = true; #endif - result = rootDir && rootName; + return rootDir && rootName; } -void is_relative(const Twine &path, bool &result) { - bool res; - is_absolute(path, res); - result = !res; +const bool is_relative(const Twine &path) { + return !is_absolute(path); } } // end namespace path @@ -597,9 +565,8 @@ namespace fs { error_code make_absolute(SmallVectorImpl &path) { StringRef p(path.data(), path.size()); - bool rootName = false, rootDirectory = false; - path::has_root_name(p, rootName); - path::has_root_directory(p, rootDirectory); + bool rootName = path::has_root_name(p), + rootDirectory = path::has_root_directory(p); // Already absolute. if (rootName && rootDirectory) @@ -619,8 +586,7 @@ error_code make_absolute(SmallVectorImpl &path) { } if (!rootName && rootDirectory) { - StringRef cdrn; - path::root_name(current_dir, cdrn); + StringRef cdrn = path::root_name(current_dir); SmallString<128> curDirRootName(cdrn.begin(), cdrn.end()); path::append(curDirRootName, p); // Set path to the result. @@ -629,14 +595,10 @@ error_code make_absolute(SmallVectorImpl &path) { } if (rootName && !rootDirectory) { - StringRef pRootName; - StringRef bRootDirectory; - StringRef bRelativePath; - StringRef pRelativePath; - path::root_name(p, pRootName); - path::root_directory(current_dir, bRootDirectory); - path::relative_path(current_dir, bRelativePath); - path::relative_path(p, pRelativePath); + StringRef pRootName = path::root_name(p); + StringRef bRootDirectory = path::root_directory(current_dir); + StringRef bRelativePath = path::relative_path(current_dir); + StringRef pRelativePath = path::relative_path(p); SmallString<128> res; path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath); @@ -652,9 +614,9 @@ error_code create_directories(const Twine &path, bool &existed) { SmallString<128> path_storage; StringRef p = path.toStringRef(path_storage); - StringRef parent; + StringRef parent = path::parent_path(p); bool parent_exists; - path::parent_path(p, parent); + if (error_code ec = fs::exists(parent, parent_exists)) return ec; if (!parent_exists) diff --git a/lib/Support/Unix/PathV2.inc b/lib/Support/Unix/PathV2.inc index 3e75e9e1127..0facd6f4623 100644 --- a/lib/Support/Unix/PathV2.inc +++ b/lib/Support/Unix/PathV2.inc @@ -324,8 +324,7 @@ error_code unique_file(const Twine &model, int &result_fd, Model.c_str(); // Make model absolute by prepending a temp directory if it's not already. - bool absolute; - path::is_absolute(Twine(Model), absolute); + bool absolute = path::is_absolute(Twine(Model)); if (!absolute) { SmallString<128> TDir; if (error_code ec = TempDir(TDir)) return ec; diff --git a/lib/Support/Windows/PathV2.inc b/lib/Support/Windows/PathV2.inc index 42a50fc0f94..6a1ea939141 100644 --- a/lib/Support/Windows/PathV2.inc +++ b/lib/Support/Windows/PathV2.inc @@ -489,8 +489,7 @@ error_code unique_file(const Twine &model, int &result_fd, if (error_code ec = UTF8ToUTF16(m, model_utf16)) return ec; // Make model absolute by prepending a temp directory if it's not already. - bool absolute; - path::is_absolute(m, absolute); + bool absolute = path::is_absolute(m); if (!absolute) { SmallVector temp_dir; diff --git a/unittests/Support/Path.cpp b/unittests/Support/Path.cpp index 016a7cd5873..fb4d2bcd7a6 100644 --- a/unittests/Support/Path.cpp +++ b/unittests/Support/Path.cpp @@ -83,24 +83,22 @@ TEST(Support, Path) { outs() << "]\n"; #endif - bool bres; - StringRef sfres; - path::has_root_path(*i, bres); - path::root_path(*i, sfres); - path::has_root_name(*i, bres); - path::root_name(*i, sfres); - path::has_root_directory(*i, bres); - path::root_directory(*i, sfres); - path::has_parent_path(*i, bres); - path::parent_path(*i, sfres); - path::has_filename(*i, bres); - path::filename(*i, sfres); - path::has_stem(*i, bres); - path::stem(*i, sfres); - path::has_extension(*i, bres); - path::extension(*i, sfres); - path::is_absolute(*i, bres); - path::is_relative(*i, bres); + path::has_root_path(*i); + path::root_path(*i); + path::has_root_name(*i); + path::root_name(*i); + path::has_root_directory(*i); + path::root_directory(*i); + path::has_parent_path(*i); + path::parent_path(*i); + path::has_filename(*i); + path::filename(*i); + path::has_stem(*i); + path::stem(*i); + path::has_extension(*i); + path::extension(*i); + path::is_absolute(*i); + path::is_relative(*i); SmallString<16> temp_store; temp_store = *i; @@ -111,8 +109,8 @@ TEST(Support, Path) { temp_store = *i; path::replace_extension(temp_store, "ext"); StringRef filename(temp_store.begin(), temp_store.size()), stem, ext; - path::stem(filename, stem); - path::extension(filename, ext); + stem = path::stem(filename); + ext = path::extension(filename); EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str()); path::native(*i, temp_store);