Path.h 18 KB

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