LLVM 22.0.0git
Path.h
Go to the documentation of this file.
1//===- llvm/Support/Path.h - Path Operating System Concept ------*- C++ -*-===//
2//
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
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"
22#include <iterator>
23
24namespace llvm {
25namespace sys {
26namespace path {
27
35
36/// Check if \p S uses POSIX path rules.
37constexpr 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
49/// Check if \p S uses Windows path rules.
50constexpr bool is_style_windows(Style S) { return !is_style_posix(S); }
51
52/// @name Lexical Component Iterator
53/// @{
54
55/// Path iterator.
56///
57/// This is an input iterator that iterates over the individual components in
58/// \a path. The traversal order is as follows:
59/// * The root-name element, if present.
60/// * The root-directory element, if present.
61/// * Each successive filename element, if present.
62/// * Dot, if one or more trailing non-root slash characters are present.
63/// Traversing backwards is possible with \a reverse_iterator
64///
65/// Iteration examples. Each component is separated by ',':
66/// @code
67/// / => /
68/// /foo => /,foo
69/// foo/ => foo,.
70/// /foo/bar => /,foo,bar
71/// ../ => ..,.
72/// C:\foo\bar => C:,\,foo,bar
73/// @endcode
75 : public iterator_facade_base<const_iterator, std::input_iterator_tag,
76 const StringRef> {
77 StringRef Path; ///< The entire path.
78 StringRef Component; ///< The current component. Not necessarily in Path.
79 size_t Position = 0; ///< The iterators current position within Path.
80 Style S = Style::native; ///< The path style to use.
81
82 // An end iterator has Position = Path.size() + 1.
85
86public:
87 reference operator*() const { return Component; }
88 LLVM_ABI const_iterator &operator++(); // preincrement
89 LLVM_ABI bool operator==(const const_iterator &RHS) const;
90
91 /// Difference in bytes between this and RHS.
93};
94
95/// Reverse path iterator.
96///
97/// This is an input iterator that iterates over the individual components in
98/// \a path in reverse order. The traversal order is exactly reversed from that
99/// of \a const_iterator
101 : public iterator_facade_base<reverse_iterator, std::input_iterator_tag,
102 const StringRef> {
103 StringRef Path; ///< The entire path.
104 StringRef Component; ///< The current component. Not necessarily in Path.
105 size_t Position = 0; ///< The iterators current position within Path.
106 Style S = Style::native; ///< The path style to use.
107
110
111public:
112 reference operator*() const { return Component; }
113 LLVM_ABI reverse_iterator &operator++(); // preincrement
114 LLVM_ABI bool operator==(const reverse_iterator &RHS) const;
115
116 /// Difference in bytes between this and RHS.
118};
119
120/// Get begin iterator over \a path.
121/// @param path Input path.
122/// @returns Iterator initialized with the first component of \a path.
124 Style style = Style::native);
125
126/// Get end iterator over \a path.
127/// @param path Input path.
128/// @returns Iterator initialized to the end of \a path.
130
131/// Get reverse begin iterator over \a path.
132/// @param path Input path.
133/// @returns Iterator initialized with the first reverse component of \a path.
135 Style style = Style::native);
136
137/// Get reverse end iterator over \a path.
138/// @param path Input path.
139/// @returns Iterator initialized to the reverse end of \a path.
141
142/// @}
143/// @name Lexical Modifiers
144/// @{
145
146/// Remove the last component from \a path unless it is the root dir.
147///
148/// Similar to the POSIX "dirname" utility.
149///
150/// @code
151/// directory/filename.cpp => directory/
152/// directory/ => directory
153/// filename.cpp => <empty>
154/// / => /
155/// @endcode
156///
157/// @param path A path that is modified to not have a file component.
159 Style style = Style::native);
160
161/// Replace the file extension of \a path with \a extension.
162///
163/// @code
164/// ./filename.cpp => ./filename.extension
165/// ./filename => ./filename.extension
166/// ./ => ./.extension
167/// @endcode
168///
169/// @param path A path that has its extension replaced with \a extension.
170/// @param extension The extension to be added. It may be empty. It may also
171/// optionally start with a '.', if it does not, one will be
172/// prepended.
174 const Twine &extension,
175 Style style = Style::native);
176
177/// Replace matching path prefix with another path.
178///
179/// @code
180/// /foo, /old, /new => /foo
181/// /old, /old, /new => /new
182/// /old, /old/, /new => /old
183/// /old/foo, /old, /new => /new/foo
184/// /old/foo, /old/, /new => /new/foo
185/// /old/foo, /old/, /new/ => /new/foo
186/// /oldfoo, /old, /new => /oldfoo
187/// /foo, <empty>, /new => /new/foo
188/// /foo, <empty>, new => new/foo
189/// /old/foo, /old, <empty> => /foo
190/// @endcode
191///
192/// @param Path If \a Path starts with \a OldPrefix modify to instead
193/// start with \a NewPrefix.
194/// @param OldPrefix The path prefix to strip from \a Path.
195/// @param NewPrefix The path prefix to replace \a NewPrefix with.
196/// @param style The style used to match the prefix. Exact match using
197/// Posix style, case/separator insensitive match for Windows style.
198/// @result true if \a Path begins with OldPrefix
200 StringRef OldPrefix, StringRef NewPrefix,
201 Style style = Style::native);
202
203/// Remove redundant leading "./" pieces and consecutive separators.
204///
205/// @param path Input path.
206/// @result The cleaned-up \a path.
208 Style style = Style::native);
209
210/// In-place remove any './' and optionally '../' components from a path.
211///
212/// @param path processed path
213/// @param remove_dot_dot specify if '../' (except for leading "../") should be
214/// removed
215/// @result True if path was changed
217 bool remove_dot_dot = false,
218 Style style = Style::native);
219
220/// Append to path.
221///
222/// @code
223/// /foo + bar/f => /foo/bar/f
224/// /foo/ + bar/f => /foo/bar/f
225/// foo + bar/f => foo/bar/f
226/// foo + /bar/f => foo/bar/f (FIXME: will be changed to /bar/f to align
227/// with C++17 std::filesystem::path::append)
228/// @endcode
229///
230/// @param path Set to \a path + \a component.
231/// @param a The component to be appended to \a path.
233 const Twine &b = "", const Twine &c = "",
234 const Twine &d = "");
235
236LLVM_ABI void append(SmallVectorImpl<char> &path, Style style, const Twine &a,
237 const Twine &b = "", const Twine &c = "",
238 const Twine &d = "");
239
240/// Append to path.
241///
242/// @code
243/// /foo + [bar,f] => /foo/bar/f
244/// /foo/ + [bar,f] => /foo/bar/f
245/// foo + [bar,f] => foo/bar/f
246/// @endcode
247///
248/// @param path Set to \a path + [\a begin, \a end).
249/// @param begin Start of components to append.
250/// @param end One past the end of components to append.
253
254/// @}
255/// @name Transforms (or some other better name)
256/// @{
257
258/// Convert path to the native form. This is used to give paths to users and
259/// operating system calls in the platform's normal way. For example, on Windows
260/// all '/' are converted to '\'. On Unix, it converts all '\' to '/'.
261///
262/// @param path A path that is transformed to native format.
263/// @param result Holds the result of the transformation.
264LLVM_ABI void native(const Twine &path, SmallVectorImpl<char> &result,
265 Style style = Style::native);
266
267/// Convert path to the native form in place. This is used to give paths to
268/// users and operating system calls in the platform's normal way. For example,
269/// on Windows all '/' are converted to '\'.
270///
271/// @param path A path that is transformed to native format.
273
274/// For Windows path styles, convert path to use the preferred path separators.
275/// For other styles, do nothing.
276///
277/// @param path A path that is transformed to preferred format.
279 Style style = Style::native) {
280 if (!is_style_windows(style))
281 return;
282 native(path, style);
283}
284
285/// Replaces backslashes with slashes if Windows.
286///
287/// @param path processed path
288/// @result The result of replacing backslashes with forward slashes if Windows.
289/// On Unix, this function is a no-op because backslashes are valid path
290/// chracters.
292 Style style = Style::native);
293
294/// @}
295/// @name Lexical Observers
296/// @{
297
298/// Get root name.
299///
300/// @code
301/// //net/hello => //net
302/// c:/hello => c: (on Windows, on other platforms nothing)
303/// /hello => <empty>
304/// @endcode
305///
306/// @param path Input path.
307/// @result The root name of \a path if it has one, otherwise "".
309 Style style = Style::native);
310
311/// Get root directory.
312///
313/// @code
314/// /goo/hello => /
315/// c:/hello => /
316/// d/file.txt => <empty>
317/// @endcode
318///
319/// @param path Input path.
320/// @result The root directory of \a path if it has one, otherwise
321/// "".
323 Style style = Style::native);
324
325/// Get root path.
326///
327/// Equivalent to root_name + root_directory.
328///
329/// @param path Input path.
330/// @result The root path of \a path if it has one, otherwise "".
332 Style style = Style::native);
333
334/// Get relative path.
335///
336/// @code
337/// C:\hello\world => hello\world
338/// foo/bar => foo/bar
339/// /foo/bar => foo/bar
340/// @endcode
341///
342/// @param path Input path.
343/// @result The path starting after root_path if one exists, otherwise "".
345 Style style = Style::native);
346
347/// Get parent path.
348///
349/// @code
350/// / => <empty>
351/// /foo => /
352/// foo/../bar => foo/..
353/// @endcode
354///
355/// @param path Input path.
356/// @result The parent path of \a path if one exists, otherwise "".
358 Style style = Style::native);
359
360/// Get filename.
361///
362/// @code
363/// /foo.txt => foo.txt
364/// . => .
365/// .. => ..
366/// / => /
367/// @endcode
368///
369/// @param path Input path.
370/// @result The filename part of \a path. This is defined as the last component
371/// of \a path. Similar to the POSIX "basename" utility.
373 Style style = Style::native);
374
375/// Get stem.
376///
377/// If filename contains a dot but not solely one or two dots, result is the
378/// substring of filename ending at (but not including) the last dot. Otherwise
379/// it is filename.
380///
381/// @code
382/// /foo/bar.txt => bar
383/// /foo/bar => bar
384/// /foo/.txt => <empty>
385/// /foo/. => .
386/// /foo/.. => ..
387/// @endcode
388///
389/// @param path Input path.
390/// @result The stem of \a path.
392 Style style = Style::native);
393
394/// Get extension.
395///
396/// If filename contains a dot but not solely one or two dots, result is the
397/// substring of filename starting at (and including) the last dot, and ending
398/// at the end of \a path. Otherwise "".
399///
400/// @code
401/// /foo/bar.txt => .txt
402/// /foo/bar => <empty>
403/// /foo/.txt => .txt
404/// @endcode
405///
406/// @param path Input path.
407/// @result The extension of \a path.
409 Style style = Style::native);
410
411/// Check whether the given char is a path separator on the host OS.
412///
413/// @param value a character
414/// @result true if \a value is a path separator character on the host OS
415LLVM_ABI bool is_separator(char value, Style style = Style::native);
416
417/// Return the preferred separator for this platform.
418///
419/// @result StringRef of the preferred separator, null-terminated.
421
422/// Get the typical temporary directory for the system, e.g.,
423/// "/var/tmp" or "C:/TEMP"
424///
425/// @param erasedOnReboot Whether to favor a path that is erased on reboot
426/// rather than one that potentially persists longer. This parameter will be
427/// ignored if the user or system has set the typical environment variable
428/// (e.g., TEMP on Windows, TMPDIR on *nix) to specify a temporary directory.
429///
430/// @param result Holds the resulting path name.
431LLVM_ABI void system_temp_directory(bool erasedOnReboot,
432 SmallVectorImpl<char> &result);
433
434/// Get the user's home directory.
435///
436/// @param result Holds the resulting path name.
437/// @result True if a home directory is set, false otherwise.
439
440/// Get the directory where packages should read user-specific configurations.
441/// e.g. $XDG_CONFIG_HOME.
442///
443/// @param result Holds the resulting path name.
444/// @result True if the appropriate path was determined, it need not exist.
446
447/// Get the directory where installed packages should put their
448/// machine-local cache, e.g. $XDG_CACHE_HOME.
449///
450/// @param result Holds the resulting path name.
451/// @result True if the appropriate path was determined, it need not exist.
453
454/// Has root name?
455///
456/// root_name != ""
457///
458/// @param path Input path.
459/// @result True if the path has a root name, false otherwise.
460LLVM_ABI bool has_root_name(const Twine &path, Style style = Style::native);
461
462/// Has root directory?
463///
464/// root_directory != ""
465///
466/// @param path Input path.
467/// @result True if the path has a root directory, false otherwise.
469 Style style = Style::native);
470
471/// Has root path?
472///
473/// root_path != ""
474///
475/// @param path Input path.
476/// @result True if the path has a root path, false otherwise.
477LLVM_ABI bool has_root_path(const Twine &path, Style style = Style::native);
478
479/// Has relative path?
480///
481/// relative_path != ""
482///
483/// @param path Input path.
484/// @result True if the path has a relative path, false otherwise.
486
487/// Has parent path?
488///
489/// parent_path != ""
490///
491/// @param path Input path.
492/// @result True if the path has a parent path, false otherwise.
494
495/// Has filename?
496///
497/// filename != ""
498///
499/// @param path Input path.
500/// @result True if the path has a filename, false otherwise.
501LLVM_ABI bool has_filename(const Twine &path, Style style = Style::native);
502
503/// Has stem?
504///
505/// stem != ""
506///
507/// @param path Input path.
508/// @result True if the path has a stem, false otherwise.
509LLVM_ABI bool has_stem(const Twine &path, Style style = Style::native);
510
511/// Has extension?
512///
513/// extension != ""
514///
515/// @param path Input path.
516/// @result True if the path has a extension, false otherwise.
517LLVM_ABI bool has_extension(const Twine &path, Style style = Style::native);
518
519/// Is path absolute?
520///
521/// According to cppreference.com, C++17 states: "An absolute path is a path
522/// that unambiguously identifies the location of a file without reference to
523/// an additional starting location."
524///
525/// In other words, the rules are:
526/// 1) POSIX style paths with nonempty root directory are absolute.
527/// 2) Windows style paths with nonempty root name and root directory are
528/// absolute.
529/// 3) No other paths are absolute.
530///
531/// \see has_root_name
532/// \see has_root_directory
533///
534/// @param path Input path.
535/// @result True if the path is absolute, false if it is not.
536LLVM_ABI bool is_absolute(const Twine &path, Style style = Style::native);
537
538/// Is path absolute using GNU rules?
539///
540/// GNU rules are:
541/// 1) Paths starting with a path separator are absolute.
542/// 2) Windows style paths are also absolute if they start with a character
543/// followed by ':'.
544/// 3) No other paths are absolute.
545///
546/// On Windows style the path "C:\Users\Default" has "C:" as root name and "\"
547/// as root directory.
548///
549/// Hence "C:" on Windows is absolute under GNU rules and not absolute under
550/// C++17 because it has no root directory. Likewise "/" and "\" on Windows are
551/// absolute under GNU and are not absolute under C++17 due to empty root name.
552///
553/// \see has_root_name
554/// \see has_root_directory
555///
556/// @param path Input path.
557/// @param style The style of \p path (e.g. Windows or POSIX). "native" style
558/// means to derive the style from the host.
559/// @result True if the path is absolute following GNU rules, false if it is
560/// not.
562
563/// Is path relative?
564///
565/// @param path Input path.
566/// @result True if the path is relative, false if it is not.
567LLVM_ABI bool is_relative(const Twine &path, Style style = Style::native);
568
569} // end namespace path
570} // end namespace sys
571} // end namespace llvm
572
573#endif
#define LLVM_ABI
Definition Compiler.h:213
#define LLVM_LIFETIME_BOUND
Definition Compiler.h:435
Value * RHS
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition iterator.h:80
LLVM_ABI const_iterator & operator++()
Definition Path.cpp:242
LLVM_ABI friend const_iterator begin(StringRef path, Style style)
Get begin iterator over path.
Definition Path.cpp:226
LLVM_ABI friend const_iterator end(StringRef path)
Get end iterator over path.
Definition Path.cpp:235
LLVM_ABI bool operator==(const const_iterator &RHS) const
Definition Path.cpp:289
LLVM_ABI ptrdiff_t operator-(const const_iterator &RHS) const
Difference in bytes between this and RHS.
Definition Path.cpp:293
reference operator*() const
Definition Path.h:87
Reverse path iterator.
Definition Path.h:102
LLVM_ABI friend reverse_iterator rend(StringRef path)
Get reverse end iterator over path.
LLVM_ABI bool operator==(const reverse_iterator &RHS) const
Definition Path.cpp:339
LLVM_ABI ptrdiff_t operator-(const reverse_iterator &RHS) const
Difference in bytes between this and RHS.
Definition Path.cpp:344
LLVM_ABI reverse_iterator & operator++()
Definition Path.cpp:314
LLVM_ABI friend reverse_iterator rbegin(StringRef path, Style style)
Get reverse begin iterator over path.
reference operator*() const
Definition Path.h:112
LLVM_ABI StringRef get_separator(Style style=Style::native)
Return the preferred separator for this platform.
Definition Path.cpp:609
LLVM_ABI StringRef root_path(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get root path.
Definition Path.cpp:348
LLVM_ABI void remove_filename(SmallVectorImpl< char > &path, Style style=Style::native)
Remove the last component from path unless it is the root dir.
Definition Path.cpp:474
LLVM_ABI bool has_relative_path(const Twine &path, Style style=Style::native)
Has relative path?
Definition Path.cpp:636
LLVM_ABI StringRef stem(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get stem.
Definition Path.cpp:579
LLVM_ABI bool has_root_name(const Twine &path, Style style=Style::native)
Has root name?
Definition Path.cpp:615
LLVM_ABI void replace_extension(SmallVectorImpl< char > &path, const Twine &extension, Style style=Style::native)
Replace the file extension of path with extension.
Definition Path.cpp:480
LLVM_ABI bool cache_directory(SmallVectorImpl< char > &result)
Get the directory where installed packages should put their machine-local cache, e....
LLVM_ABI const_iterator begin(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get begin iterator over path.
Definition Path.cpp:226
LLVM_ABI bool user_config_directory(SmallVectorImpl< char > &result)
Get the directory where packages should read user-specific configurations.
LLVM_ABI bool remove_dots(SmallVectorImpl< char > &path, bool remove_dot_dot=false, Style style=Style::native)
In-place remove any '.
Definition Path.cpp:715
LLVM_ABI bool has_root_path(const Twine &path, Style style=Style::native)
Has root path?
Definition Path.cpp:629
constexpr bool is_style_posix(Style S)
Check if S uses POSIX path rules.
Definition Path.h:37
LLVM_ABI StringRef parent_path(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get parent path.
Definition Path.cpp:467
LLVM_ABI bool has_parent_path(const Twine &path, Style style=Style::native)
Has parent path?
Definition Path.cpp:650
void make_preferred(SmallVectorImpl< char > &path, Style style=Style::native)
For Windows path styles, convert path to use the preferred path separators.
Definition Path.h:278
LLVM_ABI bool is_relative(const Twine &path, Style style=Style::native)
Is path relative?
Definition Path.cpp:699
LLVM_ABI void system_temp_directory(bool erasedOnReboot, SmallVectorImpl< char > &result)
Get the typical temporary directory for the system, e.g., "/var/tmp" or "C:/TEMP".
LLVM_ABI bool has_extension(const Twine &path, Style style=Style::native)
Has extension?
Definition Path.cpp:664
LLVM_ABI bool is_absolute_gnu(const Twine &path, Style style=Style::native)
Is path absolute using GNU rules?
Definition Path.cpp:681
LLVM_ABI StringRef filename(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get filename.
Definition Path.cpp:577
LLVM_ABI StringRef remove_leading_dotslash(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Remove redundant leading "./" pieces and consecutive separators.
LLVM_ABI std::string convert_to_slash(StringRef path, Style style=Style::native)
Replaces backslashes with slashes if Windows.
Definition Path.cpp:568
LLVM_ABI void native(const Twine &path, SmallVectorImpl< char > &result, Style style=Style::native)
Convert path to the native form.
Definition Path.cpp:540
LLVM_ABI bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition Path.cpp:671
LLVM_ABI bool has_stem(const Twine &path, Style style=Style::native)
Has stem?
Definition Path.cpp:657
constexpr bool is_style_windows(Style S)
Check if S uses Windows path rules.
Definition Path.h:50
LLVM_ABI StringRef root_name(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get root name.
Definition Path.cpp:373
LLVM_ABI StringRef root_directory(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get root directory.
Definition Path.cpp:390
LLVM_ABI bool replace_path_prefix(SmallVectorImpl< char > &Path, StringRef OldPrefix, StringRef NewPrefix, Style style=Style::native)
Replace matching path prefix with another path.
Definition Path.cpp:518
LLVM_ABI void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition Path.cpp:456
LLVM_ABI StringRef extension(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get extension.
Definition Path.cpp:590
LLVM_ABI reverse_iterator rend(StringRef path LLVM_LIFETIME_BOUND)
Get reverse end iterator over path.
LLVM_ABI reverse_iterator rbegin(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get reverse begin iterator over path.
LLVM_ABI bool has_filename(const Twine &path, Style style=Style::native)
Has filename?
Definition Path.cpp:643
LLVM_ABI const_iterator end(StringRef path LLVM_LIFETIME_BOUND)
Get end iterator over path.
Definition Path.cpp:235
LLVM_ABI bool home_directory(SmallVectorImpl< char > &result)
Get the user's home directory.
LLVM_ABI bool is_separator(char value, Style style=Style::native)
Check whether the given char is a path separator on the host OS.
Definition Path.cpp:601
LLVM_ABI StringRef relative_path(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get relative path.
Definition Path.cpp:413
LLVM_ABI bool has_root_directory(const Twine &path, Style style=Style::native)
Has root directory?
Definition Path.cpp:622
This is an optimization pass for GlobalISel generic memory operations.