Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- llvm/Support/Path.h - Path Operating System Concept ------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file declares the llvm::sys::path namespace. It is designed after |
| 10 | // TR2/boost filesystem (v3), but modified to remove exception handling and the |
| 11 | // path class. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_SUPPORT_PATH_H |
| 16 | #define LLVM_SUPPORT_PATH_H |
| 17 | |
| 18 | #include "llvm/ADT/Twine.h" |
| 19 | #include "llvm/ADT/iterator.h" |
| 20 | #include "llvm/Support/DataTypes.h" |
| 21 | #include <iterator> |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 22 | #include <system_error> |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 23 | |
| 24 | namespace llvm { |
| 25 | namespace sys { |
| 26 | namespace path { |
| 27 | |
| 28 | enum class Style { windows, posix, native }; |
| 29 | |
| 30 | /// @name Lexical Component Iterator |
| 31 | /// @{ |
| 32 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 33 | /// Path iterator. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 34 | /// |
| 35 | /// This is an input iterator that iterates over the individual components in |
| 36 | /// \a path. The traversal order is as follows: |
| 37 | /// * The root-name element, if present. |
| 38 | /// * The root-directory element, if present. |
| 39 | /// * Each successive filename element, if present. |
| 40 | /// * Dot, if one or more trailing non-root slash characters are present. |
| 41 | /// Traversing backwards is possible with \a reverse_iterator |
| 42 | /// |
| 43 | /// Iteration examples. Each component is separated by ',': |
| 44 | /// @code |
| 45 | /// / => / |
| 46 | /// /foo => /,foo |
| 47 | /// foo/ => foo,. |
| 48 | /// /foo/bar => /,foo,bar |
| 49 | /// ../ => ..,. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 50 | /// C:\foo\bar => C:,\,foo,bar |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 51 | /// @endcode |
| 52 | class const_iterator |
| 53 | : public iterator_facade_base<const_iterator, std::input_iterator_tag, |
| 54 | const StringRef> { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 55 | StringRef Path; ///< The entire path. |
| 56 | StringRef Component; ///< The current component. Not necessarily in Path. |
| 57 | size_t Position = 0; ///< The iterators current position within Path. |
| 58 | Style S = Style::native; ///< The path style to use. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 59 | |
| 60 | // An end iterator has Position = Path.size() + 1. |
| 61 | friend const_iterator begin(StringRef path, Style style); |
| 62 | friend const_iterator end(StringRef path); |
| 63 | |
| 64 | public: |
| 65 | reference operator*() const { return Component; } |
| 66 | const_iterator &operator++(); // preincrement |
| 67 | bool operator==(const const_iterator &RHS) const; |
| 68 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 69 | /// Difference in bytes between this and RHS. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 70 | ptrdiff_t operator-(const const_iterator &RHS) const; |
| 71 | }; |
| 72 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 73 | /// Reverse path iterator. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 74 | /// |
| 75 | /// This is an input iterator that iterates over the individual components in |
| 76 | /// \a path in reverse order. The traversal order is exactly reversed from that |
| 77 | /// of \a const_iterator |
| 78 | class reverse_iterator |
| 79 | : public iterator_facade_base<reverse_iterator, std::input_iterator_tag, |
| 80 | const StringRef> { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 81 | StringRef Path; ///< The entire path. |
| 82 | StringRef Component; ///< The current component. Not necessarily in Path. |
| 83 | size_t Position = 0; ///< The iterators current position within Path. |
| 84 | Style S = Style::native; ///< The path style to use. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 85 | |
| 86 | friend reverse_iterator rbegin(StringRef path, Style style); |
| 87 | friend reverse_iterator rend(StringRef path); |
| 88 | |
| 89 | public: |
| 90 | reference operator*() const { return Component; } |
| 91 | reverse_iterator &operator++(); // preincrement |
| 92 | bool operator==(const reverse_iterator &RHS) const; |
| 93 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 94 | /// Difference in bytes between this and RHS. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 95 | ptrdiff_t operator-(const reverse_iterator &RHS) const; |
| 96 | }; |
| 97 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 98 | /// Get begin iterator over \a path. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 99 | /// @param path Input path. |
| 100 | /// @returns Iterator initialized with the first component of \a path. |
| 101 | const_iterator begin(StringRef path, Style style = Style::native); |
| 102 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 103 | /// Get end iterator over \a path. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 104 | /// @param path Input path. |
| 105 | /// @returns Iterator initialized to the end of \a path. |
| 106 | const_iterator end(StringRef path); |
| 107 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 108 | /// Get reverse begin iterator over \a path. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 109 | /// @param path Input path. |
| 110 | /// @returns Iterator initialized with the first reverse component of \a path. |
| 111 | reverse_iterator rbegin(StringRef path, Style style = Style::native); |
| 112 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 113 | /// Get reverse end iterator over \a path. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 114 | /// @param path Input path. |
| 115 | /// @returns Iterator initialized to the reverse end of \a path. |
| 116 | reverse_iterator rend(StringRef path); |
| 117 | |
| 118 | /// @} |
| 119 | /// @name Lexical Modifiers |
| 120 | /// @{ |
| 121 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 122 | /// Remove the last component from \a path unless it is the root dir. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 123 | /// |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 124 | /// Similar to the POSIX "dirname" utility. |
| 125 | /// |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 126 | /// @code |
| 127 | /// directory/filename.cpp => directory/ |
| 128 | /// directory/ => directory |
| 129 | /// filename.cpp => <empty> |
| 130 | /// / => / |
| 131 | /// @endcode |
| 132 | /// |
| 133 | /// @param path A path that is modified to not have a file component. |
| 134 | void remove_filename(SmallVectorImpl<char> &path, Style style = Style::native); |
| 135 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 136 | /// Replace the file extension of \a path with \a extension. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 137 | /// |
| 138 | /// @code |
| 139 | /// ./filename.cpp => ./filename.extension |
| 140 | /// ./filename => ./filename.extension |
| 141 | /// ./ => ./.extension |
| 142 | /// @endcode |
| 143 | /// |
| 144 | /// @param path A path that has its extension replaced with \a extension. |
| 145 | /// @param extension The extension to be added. It may be empty. It may also |
| 146 | /// optionally start with a '.', if it does not, one will be |
| 147 | /// prepended. |
| 148 | void replace_extension(SmallVectorImpl<char> &path, const Twine &extension, |
| 149 | Style style = Style::native); |
| 150 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 151 | /// Replace matching path prefix with another path. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 152 | /// |
| 153 | /// @code |
| 154 | /// /foo, /old, /new => /foo |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 155 | /// /old, /old, /new => /new |
| 156 | /// /old, /old/, /new => /old |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 157 | /// /old/foo, /old, /new => /new/foo |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 158 | /// /old/foo, /old/, /new => /new/foo |
| 159 | /// /old/foo, /old/, /new/ => /new/foo |
| 160 | /// /oldfoo, /old, /new => /oldfoo |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 161 | /// /foo, <empty>, /new => /new/foo |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 162 | /// /foo, <empty>, new => new/foo |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 163 | /// /old/foo, /old, <empty> => /foo |
| 164 | /// @endcode |
| 165 | /// |
| 166 | /// @param Path If \a Path starts with \a OldPrefix modify to instead |
| 167 | /// start with \a NewPrefix. |
| 168 | /// @param OldPrefix The path prefix to strip from \a Path. |
| 169 | /// @param NewPrefix The path prefix to replace \a NewPrefix with. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 170 | /// @param style The style used to match the prefix. Exact match using |
| 171 | /// Posix style, case/separator insensitive match for Windows style. |
| 172 | /// @result true if \a Path begins with OldPrefix |
| 173 | bool replace_path_prefix(SmallVectorImpl<char> &Path, StringRef OldPrefix, |
| 174 | StringRef NewPrefix, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 175 | Style style = Style::native); |
| 176 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 177 | /// Append to path. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 178 | /// |
| 179 | /// @code |
| 180 | /// /foo + bar/f => /foo/bar/f |
| 181 | /// /foo/ + bar/f => /foo/bar/f |
| 182 | /// foo + bar/f => foo/bar/f |
| 183 | /// @endcode |
| 184 | /// |
| 185 | /// @param path Set to \a path + \a component. |
| 186 | /// @param a The component to be appended to \a path. |
| 187 | void append(SmallVectorImpl<char> &path, const Twine &a, |
| 188 | const Twine &b = "", |
| 189 | const Twine &c = "", |
| 190 | const Twine &d = ""); |
| 191 | |
| 192 | void append(SmallVectorImpl<char> &path, Style style, const Twine &a, |
| 193 | const Twine &b = "", const Twine &c = "", const Twine &d = ""); |
| 194 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 195 | /// Append to path. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 196 | /// |
| 197 | /// @code |
| 198 | /// /foo + [bar,f] => /foo/bar/f |
| 199 | /// /foo/ + [bar,f] => /foo/bar/f |
| 200 | /// foo + [bar,f] => foo/bar/f |
| 201 | /// @endcode |
| 202 | /// |
| 203 | /// @param path Set to \a path + [\a begin, \a end). |
| 204 | /// @param begin Start of components to append. |
| 205 | /// @param end One past the end of components to append. |
| 206 | void append(SmallVectorImpl<char> &path, const_iterator begin, |
| 207 | const_iterator end, Style style = Style::native); |
| 208 | |
| 209 | /// @} |
| 210 | /// @name Transforms (or some other better name) |
| 211 | /// @{ |
| 212 | |
| 213 | /// Convert path to the native form. This is used to give paths to users and |
| 214 | /// operating system calls in the platform's normal way. For example, on Windows |
| 215 | /// all '/' are converted to '\'. |
| 216 | /// |
| 217 | /// @param path A path that is transformed to native format. |
| 218 | /// @param result Holds the result of the transformation. |
| 219 | void native(const Twine &path, SmallVectorImpl<char> &result, |
| 220 | Style style = Style::native); |
| 221 | |
| 222 | /// Convert path to the native form in place. This is used to give paths to |
| 223 | /// users and operating system calls in the platform's normal way. For example, |
| 224 | /// on Windows all '/' are converted to '\'. |
| 225 | /// |
| 226 | /// @param path A path that is transformed to native format. |
| 227 | void native(SmallVectorImpl<char> &path, Style style = Style::native); |
| 228 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 229 | /// Replaces backslashes with slashes if Windows. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 230 | /// |
| 231 | /// @param path processed path |
| 232 | /// @result The result of replacing backslashes with forward slashes if Windows. |
| 233 | /// On Unix, this function is a no-op because backslashes are valid path |
| 234 | /// chracters. |
| 235 | std::string convert_to_slash(StringRef path, Style style = Style::native); |
| 236 | |
| 237 | /// @} |
| 238 | /// @name Lexical Observers |
| 239 | /// @{ |
| 240 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 241 | /// Get root name. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 242 | /// |
| 243 | /// @code |
| 244 | /// //net/hello => //net |
| 245 | /// c:/hello => c: (on Windows, on other platforms nothing) |
| 246 | /// /hello => <empty> |
| 247 | /// @endcode |
| 248 | /// |
| 249 | /// @param path Input path. |
| 250 | /// @result The root name of \a path if it has one, otherwise "". |
| 251 | StringRef root_name(StringRef path, Style style = Style::native); |
| 252 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 253 | /// Get root directory. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 254 | /// |
| 255 | /// @code |
| 256 | /// /goo/hello => / |
| 257 | /// c:/hello => / |
| 258 | /// d/file.txt => <empty> |
| 259 | /// @endcode |
| 260 | /// |
| 261 | /// @param path Input path. |
| 262 | /// @result The root directory of \a path if it has one, otherwise |
| 263 | /// "". |
| 264 | StringRef root_directory(StringRef path, Style style = Style::native); |
| 265 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 266 | /// Get root path. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 267 | /// |
| 268 | /// Equivalent to root_name + root_directory. |
| 269 | /// |
| 270 | /// @param path Input path. |
| 271 | /// @result The root path of \a path if it has one, otherwise "". |
| 272 | StringRef root_path(StringRef path, Style style = Style::native); |
| 273 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 274 | /// Get relative path. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 275 | /// |
| 276 | /// @code |
| 277 | /// C:\hello\world => hello\world |
| 278 | /// foo/bar => foo/bar |
| 279 | /// /foo/bar => foo/bar |
| 280 | /// @endcode |
| 281 | /// |
| 282 | /// @param path Input path. |
| 283 | /// @result The path starting after root_path if one exists, otherwise "". |
| 284 | StringRef relative_path(StringRef path, Style style = Style::native); |
| 285 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 286 | /// Get parent path. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 287 | /// |
| 288 | /// @code |
| 289 | /// / => <empty> |
| 290 | /// /foo => / |
| 291 | /// foo/../bar => foo/.. |
| 292 | /// @endcode |
| 293 | /// |
| 294 | /// @param path Input path. |
| 295 | /// @result The parent path of \a path if one exists, otherwise "". |
| 296 | StringRef parent_path(StringRef path, Style style = Style::native); |
| 297 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 298 | /// Get filename. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 299 | /// |
| 300 | /// @code |
| 301 | /// /foo.txt => foo.txt |
| 302 | /// . => . |
| 303 | /// .. => .. |
| 304 | /// / => / |
| 305 | /// @endcode |
| 306 | /// |
| 307 | /// @param path Input path. |
| 308 | /// @result The filename part of \a path. This is defined as the last component |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 309 | /// of \a path. Similar to the POSIX "basename" utility. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 310 | StringRef filename(StringRef path, Style style = Style::native); |
| 311 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 312 | /// Get stem. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 313 | /// |
| 314 | /// If filename contains a dot but not solely one or two dots, result is the |
| 315 | /// substring of filename ending at (but not including) the last dot. Otherwise |
| 316 | /// it is filename. |
| 317 | /// |
| 318 | /// @code |
| 319 | /// /foo/bar.txt => bar |
| 320 | /// /foo/bar => bar |
| 321 | /// /foo/.txt => <empty> |
| 322 | /// /foo/. => . |
| 323 | /// /foo/.. => .. |
| 324 | /// @endcode |
| 325 | /// |
| 326 | /// @param path Input path. |
| 327 | /// @result The stem of \a path. |
| 328 | StringRef stem(StringRef path, Style style = Style::native); |
| 329 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 330 | /// Get extension. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 331 | /// |
| 332 | /// If filename contains a dot but not solely one or two dots, result is the |
| 333 | /// substring of filename starting at (and including) the last dot, and ending |
| 334 | /// at the end of \a path. Otherwise "". |
| 335 | /// |
| 336 | /// @code |
| 337 | /// /foo/bar.txt => .txt |
| 338 | /// /foo/bar => <empty> |
| 339 | /// /foo/.txt => .txt |
| 340 | /// @endcode |
| 341 | /// |
| 342 | /// @param path Input path. |
| 343 | /// @result The extension of \a path. |
| 344 | StringRef extension(StringRef path, Style style = Style::native); |
| 345 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 346 | /// Check whether the given char is a path separator on the host OS. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 347 | /// |
| 348 | /// @param value a character |
| 349 | /// @result true if \a value is a path separator character on the host OS |
| 350 | bool is_separator(char value, Style style = Style::native); |
| 351 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 352 | /// Return the preferred separator for this platform. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 353 | /// |
| 354 | /// @result StringRef of the preferred separator, null-terminated. |
| 355 | StringRef get_separator(Style style = Style::native); |
| 356 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 357 | /// Get the typical temporary directory for the system, e.g., |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 358 | /// "/var/tmp" or "C:/TEMP" |
| 359 | /// |
| 360 | /// @param erasedOnReboot Whether to favor a path that is erased on reboot |
| 361 | /// rather than one that potentially persists longer. This parameter will be |
| 362 | /// ignored if the user or system has set the typical environment variable |
| 363 | /// (e.g., TEMP on Windows, TMPDIR on *nix) to specify a temporary directory. |
| 364 | /// |
| 365 | /// @param result Holds the resulting path name. |
| 366 | void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result); |
| 367 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 368 | /// Get the user's home directory. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 369 | /// |
| 370 | /// @param result Holds the resulting path name. |
| 371 | /// @result True if a home directory is set, false otherwise. |
| 372 | bool home_directory(SmallVectorImpl<char> &result); |
| 373 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 374 | /// Get the directory where packages should read user-specific configurations. |
| 375 | /// e.g. $XDG_CONFIG_HOME. |
| 376 | /// |
| 377 | /// @param result Holds the resulting path name. |
| 378 | /// @result True if the appropriate path was determined, it need not exist. |
| 379 | bool user_config_directory(SmallVectorImpl<char> &result); |
| 380 | |
| 381 | /// Get the directory where installed packages should put their |
| 382 | /// machine-local cache, e.g. $XDG_CACHE_HOME. |
| 383 | /// |
| 384 | /// @param result Holds the resulting path name. |
| 385 | /// @result True if the appropriate path was determined, it need not exist. |
| 386 | bool cache_directory(SmallVectorImpl<char> &result); |
| 387 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 388 | /// Has root name? |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 389 | /// |
| 390 | /// root_name != "" |
| 391 | /// |
| 392 | /// @param path Input path. |
| 393 | /// @result True if the path has a root name, false otherwise. |
| 394 | bool has_root_name(const Twine &path, Style style = Style::native); |
| 395 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 396 | /// Has root directory? |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 397 | /// |
| 398 | /// root_directory != "" |
| 399 | /// |
| 400 | /// @param path Input path. |
| 401 | /// @result True if the path has a root directory, false otherwise. |
| 402 | bool has_root_directory(const Twine &path, Style style = Style::native); |
| 403 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 404 | /// Has root path? |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 405 | /// |
| 406 | /// root_path != "" |
| 407 | /// |
| 408 | /// @param path Input path. |
| 409 | /// @result True if the path has a root path, false otherwise. |
| 410 | bool has_root_path(const Twine &path, Style style = Style::native); |
| 411 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 412 | /// Has relative path? |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 413 | /// |
| 414 | /// relative_path != "" |
| 415 | /// |
| 416 | /// @param path Input path. |
| 417 | /// @result True if the path has a relative path, false otherwise. |
| 418 | bool has_relative_path(const Twine &path, Style style = Style::native); |
| 419 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 420 | /// Has parent path? |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 421 | /// |
| 422 | /// parent_path != "" |
| 423 | /// |
| 424 | /// @param path Input path. |
| 425 | /// @result True if the path has a parent path, false otherwise. |
| 426 | bool has_parent_path(const Twine &path, Style style = Style::native); |
| 427 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 428 | /// Has filename? |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 429 | /// |
| 430 | /// filename != "" |
| 431 | /// |
| 432 | /// @param path Input path. |
| 433 | /// @result True if the path has a filename, false otherwise. |
| 434 | bool has_filename(const Twine &path, Style style = Style::native); |
| 435 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 436 | /// Has stem? |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 437 | /// |
| 438 | /// stem != "" |
| 439 | /// |
| 440 | /// @param path Input path. |
| 441 | /// @result True if the path has a stem, false otherwise. |
| 442 | bool has_stem(const Twine &path, Style style = Style::native); |
| 443 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 444 | /// Has extension? |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 445 | /// |
| 446 | /// extension != "" |
| 447 | /// |
| 448 | /// @param path Input path. |
| 449 | /// @result True if the path has a extension, false otherwise. |
| 450 | bool has_extension(const Twine &path, Style style = Style::native); |
| 451 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 452 | /// Is path absolute? |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 453 | /// |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 454 | /// According to cppreference.com, C++17 states: "An absolute path is a path |
| 455 | /// that unambiguously identifies the location of a file without reference to |
| 456 | /// an additional starting location." |
| 457 | /// |
| 458 | /// In other words, the rules are: |
| 459 | /// 1) POSIX style paths with nonempty root directory are absolute. |
| 460 | /// 2) Windows style paths with nonempty root name and root directory are |
| 461 | /// absolute. |
| 462 | /// 3) No other paths are absolute. |
| 463 | /// |
| 464 | /// \see has_root_name |
| 465 | /// \see has_root_directory |
| 466 | /// |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 467 | /// @param path Input path. |
| 468 | /// @result True if the path is absolute, false if it is not. |
| 469 | bool is_absolute(const Twine &path, Style style = Style::native); |
| 470 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 471 | /// Is path absolute using GNU rules? |
| 472 | /// |
| 473 | /// GNU rules are: |
| 474 | /// 1) Paths starting with a path separator are absolute. |
| 475 | /// 2) Windows style paths are also absolute if they start with a character |
| 476 | /// followed by ':'. |
| 477 | /// 3) No other paths are absolute. |
| 478 | /// |
| 479 | /// On Windows style the path "C:\Users\Default" has "C:" as root name and "\" |
| 480 | /// as root directory. |
| 481 | /// |
| 482 | /// Hence "C:" on Windows is absolute under GNU rules and not absolute under |
| 483 | /// C++17 because it has no root directory. Likewise "/" and "\" on Windows are |
| 484 | /// absolute under GNU and are not absolute under C++17 due to empty root name. |
| 485 | /// |
| 486 | /// \see has_root_name |
| 487 | /// \see has_root_directory |
| 488 | /// |
| 489 | /// @param path Input path. |
| 490 | /// @param style The style of \p path (e.g. Windows or POSIX). "native" style |
| 491 | /// means to derive the style from the host. |
| 492 | /// @result True if the path is absolute following GNU rules, false if it is |
| 493 | /// not. |
| 494 | bool is_absolute_gnu(const Twine &path, Style style = Style::native); |
| 495 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 496 | /// Is path relative? |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 497 | /// |
| 498 | /// @param path Input path. |
| 499 | /// @result True if the path is relative, false if it is not. |
| 500 | bool is_relative(const Twine &path, Style style = Style::native); |
| 501 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 502 | /// Remove redundant leading "./" pieces and consecutive separators. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 503 | /// |
| 504 | /// @param path Input path. |
| 505 | /// @result The cleaned-up \a path. |
| 506 | StringRef remove_leading_dotslash(StringRef path, Style style = Style::native); |
| 507 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 508 | /// In-place remove any './' and optionally '../' components from a path. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 509 | /// |
| 510 | /// @param path processed path |
| 511 | /// @param remove_dot_dot specify if '../' (except for leading "../") should be |
| 512 | /// removed |
| 513 | /// @result True if path was changed |
| 514 | bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot = false, |
| 515 | Style style = Style::native); |
| 516 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 517 | } // end namespace path |
| 518 | } // end namespace sys |
| 519 | } // end namespace llvm |
| 520 | |
| 521 | #endif |