Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:33

0001 //===- llvm/Support/Path.h - Path Operating System Concept ------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file declares the llvm::sys::path namespace. It is designed after
0010 // TR2/boost filesystem (v3), but modified to remove exception handling and the
0011 // path class.
0012 //
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_SUPPORT_PATH_H
0016 #define LLVM_SUPPORT_PATH_H
0017 
0018 #include "llvm/ADT/Twine.h"
0019 #include "llvm/ADT/iterator.h"
0020 #include "llvm/Support/DataTypes.h"
0021 #include <iterator>
0022 
0023 namespace llvm {
0024 namespace sys {
0025 namespace path {
0026 
0027 enum class Style {
0028   native,
0029   posix,
0030   windows_slash,
0031   windows_backslash,
0032   windows = windows_backslash, // deprecated
0033 };
0034 
0035 /// Check if \p S uses POSIX path rules.
0036 constexpr bool is_style_posix(Style S) {
0037   if (S == Style::posix)
0038     return true;
0039   if (S != Style::native)
0040     return false;
0041 #if defined(_WIN32)
0042   return false;
0043 #else
0044   return true;
0045 #endif
0046 }
0047 
0048 /// Check if \p S uses Windows path rules.
0049 constexpr bool is_style_windows(Style S) { return !is_style_posix(S); }
0050 
0051 /// @name Lexical Component Iterator
0052 /// @{
0053 
0054 /// Path iterator.
0055 ///
0056 /// This is an input iterator that iterates over the individual components in
0057 /// \a path. The traversal order is as follows:
0058 /// * The root-name element, if present.
0059 /// * The root-directory element, if present.
0060 /// * Each successive filename element, if present.
0061 /// * Dot, if one or more trailing non-root slash characters are present.
0062 /// Traversing backwards is possible with \a reverse_iterator
0063 ///
0064 /// Iteration examples. Each component is separated by ',':
0065 /// @code
0066 ///   /          => /
0067 ///   /foo       => /,foo
0068 ///   foo/       => foo,.
0069 ///   /foo/bar   => /,foo,bar
0070 ///   ../        => ..,.
0071 ///   C:\foo\bar => C:,\,foo,bar
0072 /// @endcode
0073 class const_iterator
0074     : public iterator_facade_base<const_iterator, std::input_iterator_tag,
0075                                   const StringRef> {
0076   StringRef Path;          ///< The entire path.
0077   StringRef Component;     ///< The current component. Not necessarily in Path.
0078   size_t    Position = 0;  ///< The iterators current position within Path.
0079   Style S = Style::native; ///< The path style to use.
0080 
0081   // An end iterator has Position = Path.size() + 1.
0082   friend const_iterator begin(StringRef path, Style style);
0083   friend const_iterator end(StringRef path);
0084 
0085 public:
0086   reference operator*() const { return Component; }
0087   const_iterator &operator++();    // preincrement
0088   bool operator==(const const_iterator &RHS) const;
0089 
0090   /// Difference in bytes between this and RHS.
0091   ptrdiff_t operator-(const const_iterator &RHS) const;
0092 };
0093 
0094 /// Reverse path iterator.
0095 ///
0096 /// This is an input iterator that iterates over the individual components in
0097 /// \a path in reverse order. The traversal order is exactly reversed from that
0098 /// of \a const_iterator
0099 class reverse_iterator
0100     : public iterator_facade_base<reverse_iterator, std::input_iterator_tag,
0101                                   const StringRef> {
0102   StringRef Path;          ///< The entire path.
0103   StringRef Component;     ///< The current component. Not necessarily in Path.
0104   size_t    Position = 0;  ///< The iterators current position within Path.
0105   Style S = Style::native; ///< The path style to use.
0106 
0107   friend reverse_iterator rbegin(StringRef path, Style style);
0108   friend reverse_iterator rend(StringRef path);
0109 
0110 public:
0111   reference operator*() const { return Component; }
0112   reverse_iterator &operator++();    // preincrement
0113   bool operator==(const reverse_iterator &RHS) const;
0114 
0115   /// Difference in bytes between this and RHS.
0116   ptrdiff_t operator-(const reverse_iterator &RHS) const;
0117 };
0118 
0119 /// Get begin iterator over \a path.
0120 /// @param path Input path.
0121 /// @returns Iterator initialized with the first component of \a path.
0122 const_iterator begin(StringRef path LLVM_LIFETIME_BOUND,
0123                      Style style = Style::native);
0124 
0125 /// Get end iterator over \a path.
0126 /// @param path Input path.
0127 /// @returns Iterator initialized to the end of \a path.
0128 const_iterator end(StringRef path LLVM_LIFETIME_BOUND);
0129 
0130 /// Get reverse begin iterator over \a path.
0131 /// @param path Input path.
0132 /// @returns Iterator initialized with the first reverse component of \a path.
0133 reverse_iterator rbegin(StringRef path LLVM_LIFETIME_BOUND,
0134                         Style style = Style::native);
0135 
0136 /// Get reverse end iterator over \a path.
0137 /// @param path Input path.
0138 /// @returns Iterator initialized to the reverse end of \a path.
0139 reverse_iterator rend(StringRef path LLVM_LIFETIME_BOUND);
0140 
0141 /// @}
0142 /// @name Lexical Modifiers
0143 /// @{
0144 
0145 /// Remove the last component from \a path unless it is the root dir.
0146 ///
0147 /// Similar to the POSIX "dirname" utility.
0148 ///
0149 /// @code
0150 ///   directory/filename.cpp => directory/
0151 ///   directory/             => directory
0152 ///   filename.cpp           => <empty>
0153 ///   /                      => /
0154 /// @endcode
0155 ///
0156 /// @param path A path that is modified to not have a file component.
0157 void remove_filename(SmallVectorImpl<char> &path, Style style = Style::native);
0158 
0159 /// Replace the file extension of \a path with \a extension.
0160 ///
0161 /// @code
0162 ///   ./filename.cpp => ./filename.extension
0163 ///   ./filename     => ./filename.extension
0164 ///   ./             => ./.extension
0165 /// @endcode
0166 ///
0167 /// @param path A path that has its extension replaced with \a extension.
0168 /// @param extension The extension to be added. It may be empty. It may also
0169 ///                  optionally start with a '.', if it does not, one will be
0170 ///                  prepended.
0171 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension,
0172                        Style style = Style::native);
0173 
0174 /// Replace matching path prefix with another path.
0175 ///
0176 /// @code
0177 ///   /foo, /old, /new => /foo
0178 ///   /old, /old, /new => /new
0179 ///   /old, /old/, /new => /old
0180 ///   /old/foo, /old, /new => /new/foo
0181 ///   /old/foo, /old/, /new => /new/foo
0182 ///   /old/foo, /old/, /new/ => /new/foo
0183 ///   /oldfoo, /old, /new => /oldfoo
0184 ///   /foo, <empty>, /new => /new/foo
0185 ///   /foo, <empty>, new => new/foo
0186 ///   /old/foo, /old, <empty> => /foo
0187 /// @endcode
0188 ///
0189 /// @param Path If \a Path starts with \a OldPrefix modify to instead
0190 ///        start with \a NewPrefix.
0191 /// @param OldPrefix The path prefix to strip from \a Path.
0192 /// @param NewPrefix The path prefix to replace \a NewPrefix with.
0193 /// @param style The style used to match the prefix. Exact match using
0194 /// Posix style, case/separator insensitive match for Windows style.
0195 /// @result true if \a Path begins with OldPrefix
0196 bool replace_path_prefix(SmallVectorImpl<char> &Path, StringRef OldPrefix,
0197                          StringRef NewPrefix,
0198                          Style style = Style::native);
0199 
0200 /// Remove redundant leading "./" pieces and consecutive separators.
0201 ///
0202 /// @param path Input path.
0203 /// @result The cleaned-up \a path.
0204 StringRef remove_leading_dotslash(StringRef path LLVM_LIFETIME_BOUND,
0205                                   Style style = Style::native);
0206 
0207 /// In-place remove any './' and optionally '../' components from a path.
0208 ///
0209 /// @param path processed path
0210 /// @param remove_dot_dot specify if '../' (except for leading "../") should be
0211 /// removed
0212 /// @result True if path was changed
0213 bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot = false,
0214                  Style style = Style::native);
0215 
0216 /// Append to path.
0217 ///
0218 /// @code
0219 ///   /foo  + bar/f => /foo/bar/f
0220 ///   /foo/ + bar/f => /foo/bar/f
0221 ///   foo   + bar/f => foo/bar/f
0222 /// @endcode
0223 ///
0224 /// @param path Set to \a path + \a component.
0225 /// @param a The component to be appended to \a path.
0226 void append(SmallVectorImpl<char> &path, const Twine &a,
0227                                          const Twine &b = "",
0228                                          const Twine &c = "",
0229                                          const Twine &d = "");
0230 
0231 void append(SmallVectorImpl<char> &path, Style style, const Twine &a,
0232             const Twine &b = "", const Twine &c = "", const Twine &d = "");
0233 
0234 /// Append to path.
0235 ///
0236 /// @code
0237 ///   /foo  + [bar,f] => /foo/bar/f
0238 ///   /foo/ + [bar,f] => /foo/bar/f
0239 ///   foo   + [bar,f] => foo/bar/f
0240 /// @endcode
0241 ///
0242 /// @param path Set to \a path + [\a begin, \a end).
0243 /// @param begin Start of components to append.
0244 /// @param end One past the end of components to append.
0245 void append(SmallVectorImpl<char> &path, const_iterator begin,
0246             const_iterator end, Style style = Style::native);
0247 
0248 /// @}
0249 /// @name Transforms (or some other better name)
0250 /// @{
0251 
0252 /// Convert path to the native form. This is used to give paths to users and
0253 /// operating system calls in the platform's normal way. For example, on Windows
0254 /// all '/' are converted to '\'. On Unix, it converts all '\' to '/'.
0255 ///
0256 /// @param path A path that is transformed to native format.
0257 /// @param result Holds the result of the transformation.
0258 void native(const Twine &path, SmallVectorImpl<char> &result,
0259             Style style = Style::native);
0260 
0261 /// Convert path to the native form in place. This is used to give paths to
0262 /// users and operating system calls in the platform's normal way. For example,
0263 /// on Windows all '/' are converted to '\'.
0264 ///
0265 /// @param path A path that is transformed to native format.
0266 void native(SmallVectorImpl<char> &path, Style style = Style::native);
0267 
0268 /// For Windows path styles, convert path to use the preferred path separators.
0269 /// For other styles, do nothing.
0270 ///
0271 /// @param path A path that is transformed to preferred format.
0272 inline void make_preferred(SmallVectorImpl<char> &path,
0273                            Style style = Style::native) {
0274   if (!is_style_windows(style))
0275     return;
0276   native(path, style);
0277 }
0278 
0279 /// Replaces backslashes with slashes if Windows.
0280 ///
0281 /// @param path processed path
0282 /// @result The result of replacing backslashes with forward slashes if Windows.
0283 /// On Unix, this function is a no-op because backslashes are valid path
0284 /// chracters.
0285 std::string convert_to_slash(StringRef path, Style style = Style::native);
0286 
0287 /// @}
0288 /// @name Lexical Observers
0289 /// @{
0290 
0291 /// Get root name.
0292 ///
0293 /// @code
0294 ///   //net/hello => //net
0295 ///   c:/hello    => c: (on Windows, on other platforms nothing)
0296 ///   /hello      => <empty>
0297 /// @endcode
0298 ///
0299 /// @param path Input path.
0300 /// @result The root name of \a path if it has one, otherwise "".
0301 StringRef root_name(StringRef path LLVM_LIFETIME_BOUND,
0302                     Style style = Style::native);
0303 
0304 /// Get root directory.
0305 ///
0306 /// @code
0307 ///   /goo/hello => /
0308 ///   c:/hello   => /
0309 ///   d/file.txt => <empty>
0310 /// @endcode
0311 ///
0312 /// @param path Input path.
0313 /// @result The root directory of \a path if it has one, otherwise
0314 ///               "".
0315 StringRef root_directory(StringRef path LLVM_LIFETIME_BOUND,
0316                          Style style = Style::native);
0317 
0318 /// Get root path.
0319 ///
0320 /// Equivalent to root_name + root_directory.
0321 ///
0322 /// @param path Input path.
0323 /// @result The root path of \a path if it has one, otherwise "".
0324 StringRef root_path(StringRef path LLVM_LIFETIME_BOUND,
0325                     Style style = Style::native);
0326 
0327 /// Get relative path.
0328 ///
0329 /// @code
0330 ///   C:\hello\world => hello\world
0331 ///   foo/bar        => foo/bar
0332 ///   /foo/bar       => foo/bar
0333 /// @endcode
0334 ///
0335 /// @param path Input path.
0336 /// @result The path starting after root_path if one exists, otherwise "".
0337 StringRef relative_path(StringRef path LLVM_LIFETIME_BOUND,
0338                         Style style = Style::native);
0339 
0340 /// Get parent path.
0341 ///
0342 /// @code
0343 ///   /          => <empty>
0344 ///   /foo       => /
0345 ///   foo/../bar => foo/..
0346 /// @endcode
0347 ///
0348 /// @param path Input path.
0349 /// @result The parent path of \a path if one exists, otherwise "".
0350 StringRef parent_path(StringRef path LLVM_LIFETIME_BOUND,
0351                       Style style = Style::native);
0352 
0353 /// Get filename.
0354 ///
0355 /// @code
0356 ///   /foo.txt    => foo.txt
0357 ///   .          => .
0358 ///   ..         => ..
0359 ///   /          => /
0360 /// @endcode
0361 ///
0362 /// @param path Input path.
0363 /// @result The filename part of \a path. This is defined as the last component
0364 ///         of \a path. Similar to the POSIX "basename" utility.
0365 StringRef filename(StringRef path LLVM_LIFETIME_BOUND,
0366                    Style style = Style::native);
0367 
0368 /// Get stem.
0369 ///
0370 /// If filename contains a dot but not solely one or two dots, result is the
0371 /// substring of filename ending at (but not including) the last dot. Otherwise
0372 /// it is filename.
0373 ///
0374 /// @code
0375 ///   /foo/bar.txt => bar
0376 ///   /foo/bar     => bar
0377 ///   /foo/.txt    => <empty>
0378 ///   /foo/.       => .
0379 ///   /foo/..      => ..
0380 /// @endcode
0381 ///
0382 /// @param path Input path.
0383 /// @result The stem of \a path.
0384 StringRef stem(StringRef path LLVM_LIFETIME_BOUND, Style style = Style::native);
0385 
0386 /// Get extension.
0387 ///
0388 /// If filename contains a dot but not solely one or two dots, result is the
0389 /// substring of filename starting at (and including) the last dot, and ending
0390 /// at the end of \a path. Otherwise "".
0391 ///
0392 /// @code
0393 ///   /foo/bar.txt => .txt
0394 ///   /foo/bar     => <empty>
0395 ///   /foo/.txt    => .txt
0396 /// @endcode
0397 ///
0398 /// @param path Input path.
0399 /// @result The extension of \a path.
0400 StringRef extension(StringRef path LLVM_LIFETIME_BOUND,
0401                     Style style = Style::native);
0402 
0403 /// Check whether the given char is a path separator on the host OS.
0404 ///
0405 /// @param value a character
0406 /// @result true if \a value is a path separator character on the host OS
0407 bool is_separator(char value, Style style = Style::native);
0408 
0409 /// Return the preferred separator for this platform.
0410 ///
0411 /// @result StringRef of the preferred separator, null-terminated.
0412 StringRef get_separator(Style style = Style::native);
0413 
0414 /// Get the typical temporary directory for the system, e.g.,
0415 /// "/var/tmp" or "C:/TEMP"
0416 ///
0417 /// @param erasedOnReboot Whether to favor a path that is erased on reboot
0418 /// rather than one that potentially persists longer. This parameter will be
0419 /// ignored if the user or system has set the typical environment variable
0420 /// (e.g., TEMP on Windows, TMPDIR on *nix) to specify a temporary directory.
0421 ///
0422 /// @param result Holds the resulting path name.
0423 void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result);
0424 
0425 /// Get the user's home directory.
0426 ///
0427 /// @param result Holds the resulting path name.
0428 /// @result True if a home directory is set, false otherwise.
0429 bool home_directory(SmallVectorImpl<char> &result);
0430 
0431 /// Get the directory where packages should read user-specific configurations.
0432 /// e.g. $XDG_CONFIG_HOME.
0433 ///
0434 /// @param result Holds the resulting path name.
0435 /// @result True if the appropriate path was determined, it need not exist.
0436 bool user_config_directory(SmallVectorImpl<char> &result);
0437 
0438 /// Get the directory where installed packages should put their
0439 /// machine-local cache, e.g. $XDG_CACHE_HOME.
0440 ///
0441 /// @param result Holds the resulting path name.
0442 /// @result True if the appropriate path was determined, it need not exist.
0443 bool cache_directory(SmallVectorImpl<char> &result);
0444 
0445 /// Has root name?
0446 ///
0447 /// root_name != ""
0448 ///
0449 /// @param path Input path.
0450 /// @result True if the path has a root name, false otherwise.
0451 bool has_root_name(const Twine &path, Style style = Style::native);
0452 
0453 /// Has root directory?
0454 ///
0455 /// root_directory != ""
0456 ///
0457 /// @param path Input path.
0458 /// @result True if the path has a root directory, false otherwise.
0459 bool has_root_directory(const Twine &path, Style style = Style::native);
0460 
0461 /// Has root path?
0462 ///
0463 /// root_path != ""
0464 ///
0465 /// @param path Input path.
0466 /// @result True if the path has a root path, false otherwise.
0467 bool has_root_path(const Twine &path, Style style = Style::native);
0468 
0469 /// Has relative path?
0470 ///
0471 /// relative_path != ""
0472 ///
0473 /// @param path Input path.
0474 /// @result True if the path has a relative path, false otherwise.
0475 bool has_relative_path(const Twine &path, Style style = Style::native);
0476 
0477 /// Has parent path?
0478 ///
0479 /// parent_path != ""
0480 ///
0481 /// @param path Input path.
0482 /// @result True if the path has a parent path, false otherwise.
0483 bool has_parent_path(const Twine &path, Style style = Style::native);
0484 
0485 /// Has filename?
0486 ///
0487 /// filename != ""
0488 ///
0489 /// @param path Input path.
0490 /// @result True if the path has a filename, false otherwise.
0491 bool has_filename(const Twine &path, Style style = Style::native);
0492 
0493 /// Has stem?
0494 ///
0495 /// stem != ""
0496 ///
0497 /// @param path Input path.
0498 /// @result True if the path has a stem, false otherwise.
0499 bool has_stem(const Twine &path, Style style = Style::native);
0500 
0501 /// Has extension?
0502 ///
0503 /// extension != ""
0504 ///
0505 /// @param path Input path.
0506 /// @result True if the path has a extension, false otherwise.
0507 bool has_extension(const Twine &path, Style style = Style::native);
0508 
0509 /// Is path absolute?
0510 ///
0511 /// According to cppreference.com, C++17 states: "An absolute path is a path
0512 /// that unambiguously identifies the location of a file without reference to
0513 /// an additional starting location."
0514 ///
0515 /// In other words, the rules are:
0516 /// 1) POSIX style paths with nonempty root directory are absolute.
0517 /// 2) Windows style paths with nonempty root name and root directory are
0518 ///    absolute.
0519 /// 3) No other paths are absolute.
0520 ///
0521 /// \see has_root_name
0522 /// \see has_root_directory
0523 ///
0524 /// @param path Input path.
0525 /// @result True if the path is absolute, false if it is not.
0526 bool is_absolute(const Twine &path, Style style = Style::native);
0527 
0528 /// Is path absolute using GNU rules?
0529 ///
0530 /// GNU rules are:
0531 /// 1) Paths starting with a path separator are absolute.
0532 /// 2) Windows style paths are also absolute if they start with a character
0533 ///    followed by ':'.
0534 /// 3) No other paths are absolute.
0535 ///
0536 /// On Windows style the path "C:\Users\Default" has "C:" as root name and "\"
0537 /// as root directory.
0538 ///
0539 /// Hence "C:" on Windows is absolute under GNU rules and not absolute under
0540 /// C++17 because it has no root directory. Likewise "/" and "\" on Windows are
0541 /// absolute under GNU and are not absolute under C++17 due to empty root name.
0542 ///
0543 /// \see has_root_name
0544 /// \see has_root_directory
0545 ///
0546 /// @param path Input path.
0547 /// @param style The style of \p path (e.g. Windows or POSIX). "native" style
0548 /// means to derive the style from the host.
0549 /// @result True if the path is absolute following GNU rules, false if it is
0550 /// not.
0551 bool is_absolute_gnu(const Twine &path, Style style = Style::native);
0552 
0553 /// Is path relative?
0554 ///
0555 /// @param path Input path.
0556 /// @result True if the path is relative, false if it is not.
0557 bool is_relative(const Twine &path, Style style = Style::native);
0558 
0559 } // end namespace path
0560 } // end namespace sys
0561 } // end namespace llvm
0562 
0563 #endif