Path.h 18 KB

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