LLVM  8.0.0svn
Path.h
Go to the documentation of this file.
1 //===- llvm/Support/Path.h - Path Operating System Concept ------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the llvm::sys::path namespace. It is designed after
11 // TR2/boost filesystem (v3), but modified to remove exception handling and the
12 // path class.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_SUPPORT_PATH_H
17 #define LLVM_SUPPORT_PATH_H
18 
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/ADT/iterator.h"
21 #include "llvm/Support/DataTypes.h"
22 #include <iterator>
23 #include <system_error>
24 
25 namespace llvm {
26 namespace sys {
27 namespace path {
28 
29 enum class Style { windows, posix, native };
30 
31 /// @name Lexical Component Iterator
32 /// @{
33 
34 /// Path iterator.
35 ///
36 /// This is an input iterator that iterates over the individual components in
37 /// \a path. The traversal order is as follows:
38 /// * The root-name element, if present.
39 /// * The root-directory element, if present.
40 /// * Each successive filename element, if present.
41 /// * Dot, if one or more trailing non-root slash characters are present.
42 /// Traversing backwards is possible with \a reverse_iterator
43 ///
44 /// Iteration examples. Each component is separated by ',':
45 /// @code
46 /// / => /
47 /// /foo => /,foo
48 /// foo/ => foo,.
49 /// /foo/bar => /,foo,bar
50 /// ../ => ..,.
51 /// C:\foo\bar => C:,/,foo,bar
52 /// @endcode
54  : public iterator_facade_base<const_iterator, std::input_iterator_tag,
55  const StringRef> {
56  StringRef Path; ///< The entire path.
57  StringRef Component; ///< The current component. Not necessarily in Path.
58  size_t Position; ///< The iterators current position within Path.
59  Style S; ///< The path style to use.
60 
61  // An end iterator has Position = Path.size() + 1.
62  friend const_iterator begin(StringRef path, Style style);
63  friend const_iterator end(StringRef path);
64 
65 public:
66  reference operator*() const { return Component; }
67  const_iterator &operator++(); // preincrement
68  bool operator==(const const_iterator &RHS) const;
69 
70  /// Difference in bytes between this and RHS.
71  ptrdiff_t operator-(const const_iterator &RHS) const;
72 };
73 
74 /// Reverse path iterator.
75 ///
76 /// This is an input iterator that iterates over the individual components in
77 /// \a path in reverse order. The traversal order is exactly reversed from that
78 /// of \a const_iterator
80  : public iterator_facade_base<reverse_iterator, std::input_iterator_tag,
81  const StringRef> {
82  StringRef Path; ///< The entire path.
83  StringRef Component; ///< The current component. Not necessarily in Path.
84  size_t Position; ///< The iterators current position within Path.
85  Style S; ///< The path style to use.
86 
87  friend reverse_iterator rbegin(StringRef path, Style style);
88  friend reverse_iterator rend(StringRef path);
89 
90 public:
91  reference operator*() const { return Component; }
92  reverse_iterator &operator++(); // preincrement
93  bool operator==(const reverse_iterator &RHS) const;
94 
95  /// Difference in bytes between this and RHS.
96  ptrdiff_t operator-(const reverse_iterator &RHS) const;
97 };
98 
99 /// Get begin iterator over \a path.
100 /// @param path Input path.
101 /// @returns Iterator initialized with the first component of \a path.
103 
104 /// Get end iterator over \a path.
105 /// @param path Input path.
106 /// @returns Iterator initialized to the end of \a path.
108 
109 /// Get reverse begin iterator over \a path.
110 /// @param path Input path.
111 /// @returns Iterator initialized with the first reverse component of \a path.
113 
114 /// Get reverse end iterator over \a path.
115 /// @param path Input path.
116 /// @returns Iterator initialized to the reverse end of \a path.
118 
119 /// @}
120 /// @name Lexical Modifiers
121 /// @{
122 
123 /// Remove the last component from \a path unless it is the root dir.
124 ///
125 /// @code
126 /// directory/filename.cpp => directory/
127 /// directory/ => directory
128 /// filename.cpp => <empty>
129 /// / => /
130 /// @endcode
131 ///
132 /// @param path A path that is modified to not have a file component.
134 
135 /// Replace the file extension of \a path with \a extension.
136 ///
137 /// @code
138 /// ./filename.cpp => ./filename.extension
139 /// ./filename => ./filename.extension
140 /// ./ => ./.extension
141 /// @endcode
142 ///
143 /// @param path A path that has its extension replaced with \a extension.
144 /// @param extension The extension to be added. It may be empty. It may also
145 /// optionally start with a '.', if it does not, one will be
146 /// prepended.
148  Style style = Style::native);
149 
150 /// Replace matching path prefix with another path.
151 ///
152 /// @code
153 /// /foo, /old, /new => /foo
154 /// /old/foo, /old, /new => /new/foo
155 /// /foo, <empty>, /new => /new/foo
156 /// /old/foo, /old, <empty> => /foo
157 /// @endcode
158 ///
159 /// @param Path If \a Path starts with \a OldPrefix modify to instead
160 /// start with \a NewPrefix.
161 /// @param OldPrefix The path prefix to strip from \a Path.
162 /// @param NewPrefix The path prefix to replace \a NewPrefix with.
164  const StringRef &OldPrefix, const StringRef &NewPrefix,
165  Style style = Style::native);
166 
167 /// Append to path.
168 ///
169 /// @code
170 /// /foo + bar/f => /foo/bar/f
171 /// /foo/ + bar/f => /foo/bar/f
172 /// foo + bar/f => foo/bar/f
173 /// @endcode
174 ///
175 /// @param path Set to \a path + \a component.
176 /// @param a The component to be appended to \a path.
177 void append(SmallVectorImpl<char> &path, const Twine &a,
178  const Twine &b = "",
179  const Twine &c = "",
180  const Twine &d = "");
181 
182 void append(SmallVectorImpl<char> &path, Style style, const Twine &a,
183  const Twine &b = "", const Twine &c = "", const Twine &d = "");
184 
185 /// Append to path.
186 ///
187 /// @code
188 /// /foo + [bar,f] => /foo/bar/f
189 /// /foo/ + [bar,f] => /foo/bar/f
190 /// foo + [bar,f] => foo/bar/f
191 /// @endcode
192 ///
193 /// @param path Set to \a path + [\a begin, \a end).
194 /// @param begin Start of components to append.
195 /// @param end One past the end of components to append.
197  const_iterator end, Style style = Style::native);
198 
199 /// @}
200 /// @name Transforms (or some other better name)
201 /// @{
202 
203 /// Convert path to the native form. This is used to give paths to users and
204 /// operating system calls in the platform's normal way. For example, on Windows
205 /// all '/' are converted to '\'.
206 ///
207 /// @param path A path that is transformed to native format.
208 /// @param result Holds the result of the transformation.
209 void native(const Twine &path, SmallVectorImpl<char> &result,
210  Style style = Style::native);
211 
212 /// Convert path to the native form in place. This is used to give paths to
213 /// users and operating system calls in the platform's normal way. For example,
214 /// on Windows all '/' are converted to '\'.
215 ///
216 /// @param path A path that is transformed to native format.
217 void native(SmallVectorImpl<char> &path, Style style = Style::native);
218 
219 /// Replaces backslashes with slashes if Windows.
220 ///
221 /// @param path processed path
222 /// @result The result of replacing backslashes with forward slashes if Windows.
223 /// On Unix, this function is a no-op because backslashes are valid path
224 /// chracters.
225 std::string convert_to_slash(StringRef path, Style style = Style::native);
226 
227 /// @}
228 /// @name Lexical Observers
229 /// @{
230 
231 /// Get root name.
232 ///
233 /// @code
234 /// //net/hello => //net
235 /// c:/hello => c: (on Windows, on other platforms nothing)
236 /// /hello => <empty>
237 /// @endcode
238 ///
239 /// @param path Input path.
240 /// @result The root name of \a path if it has one, otherwise "".
242 
243 /// Get root directory.
244 ///
245 /// @code
246 /// /goo/hello => /
247 /// c:/hello => /
248 /// d/file.txt => <empty>
249 /// @endcode
250 ///
251 /// @param path Input path.
252 /// @result The root directory of \a path if it has one, otherwise
253 /// "".
255 
256 /// Get root path.
257 ///
258 /// Equivalent to root_name + root_directory.
259 ///
260 /// @param path Input path.
261 /// @result The root path of \a path if it has one, otherwise "".
263 
264 /// Get relative path.
265 ///
266 /// @code
267 /// C:\hello\world => hello\world
268 /// foo/bar => foo/bar
269 /// /foo/bar => foo/bar
270 /// @endcode
271 ///
272 /// @param path Input path.
273 /// @result The path starting after root_path if one exists, otherwise "".
275 
276 /// Get parent path.
277 ///
278 /// @code
279 /// / => <empty>
280 /// /foo => /
281 /// foo/../bar => foo/..
282 /// @endcode
283 ///
284 /// @param path Input path.
285 /// @result The parent path of \a path if one exists, otherwise "".
287 
288 /// Get filename.
289 ///
290 /// @code
291 /// /foo.txt => foo.txt
292 /// . => .
293 /// .. => ..
294 /// / => /
295 /// @endcode
296 ///
297 /// @param path Input path.
298 /// @result The filename part of \a path. This is defined as the last component
299 /// of \a path.
301 
302 /// Get stem.
303 ///
304 /// If filename contains a dot but not solely one or two dots, result is the
305 /// substring of filename ending at (but not including) the last dot. Otherwise
306 /// it is filename.
307 ///
308 /// @code
309 /// /foo/bar.txt => bar
310 /// /foo/bar => bar
311 /// /foo/.txt => <empty>
312 /// /foo/. => .
313 /// /foo/.. => ..
314 /// @endcode
315 ///
316 /// @param path Input path.
317 /// @result The stem of \a path.
319 
320 /// Get extension.
321 ///
322 /// If filename contains a dot but not solely one or two dots, result is the
323 /// substring of filename starting at (and including) the last dot, and ending
324 /// at the end of \a path. Otherwise "".
325 ///
326 /// @code
327 /// /foo/bar.txt => .txt
328 /// /foo/bar => <empty>
329 /// /foo/.txt => .txt
330 /// @endcode
331 ///
332 /// @param path Input path.
333 /// @result The extension of \a path.
335 
336 /// Check whether the given char is a path separator on the host OS.
337 ///
338 /// @param value a character
339 /// @result true if \a value is a path separator character on the host OS
340 bool is_separator(char value, Style style = Style::native);
341 
342 /// Return the preferred separator for this platform.
343 ///
344 /// @result StringRef of the preferred separator, null-terminated.
346 
347 /// Get the typical temporary directory for the system, e.g.,
348 /// "/var/tmp" or "C:/TEMP"
349 ///
350 /// @param erasedOnReboot Whether to favor a path that is erased on reboot
351 /// rather than one that potentially persists longer. This parameter will be
352 /// ignored if the user or system has set the typical environment variable
353 /// (e.g., TEMP on Windows, TMPDIR on *nix) to specify a temporary directory.
354 ///
355 /// @param result Holds the resulting path name.
356 void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result);
357 
358 /// Get the user's home directory.
359 ///
360 /// @param result Holds the resulting path name.
361 /// @result True if a home directory is set, false otherwise.
363 
364 /// Has root name?
365 ///
366 /// root_name != ""
367 ///
368 /// @param path Input path.
369 /// @result True if the path has a root name, false otherwise.
370 bool has_root_name(const Twine &path, Style style = Style::native);
371 
372 /// Has root directory?
373 ///
374 /// root_directory != ""
375 ///
376 /// @param path Input path.
377 /// @result True if the path has a root directory, false otherwise.
378 bool has_root_directory(const Twine &path, Style style = Style::native);
379 
380 /// Has root path?
381 ///
382 /// root_path != ""
383 ///
384 /// @param path Input path.
385 /// @result True if the path has a root path, false otherwise.
386 bool has_root_path(const Twine &path, Style style = Style::native);
387 
388 /// Has relative path?
389 ///
390 /// relative_path != ""
391 ///
392 /// @param path Input path.
393 /// @result True if the path has a relative path, false otherwise.
394 bool has_relative_path(const Twine &path, Style style = Style::native);
395 
396 /// Has parent path?
397 ///
398 /// parent_path != ""
399 ///
400 /// @param path Input path.
401 /// @result True if the path has a parent path, false otherwise.
402 bool has_parent_path(const Twine &path, Style style = Style::native);
403 
404 /// Has filename?
405 ///
406 /// filename != ""
407 ///
408 /// @param path Input path.
409 /// @result True if the path has a filename, false otherwise.
410 bool has_filename(const Twine &path, Style style = Style::native);
411 
412 /// Has stem?
413 ///
414 /// stem != ""
415 ///
416 /// @param path Input path.
417 /// @result True if the path has a stem, false otherwise.
418 bool has_stem(const Twine &path, Style style = Style::native);
419 
420 /// Has extension?
421 ///
422 /// extension != ""
423 ///
424 /// @param path Input path.
425 /// @result True if the path has a extension, false otherwise.
426 bool has_extension(const Twine &path, Style style = Style::native);
427 
428 /// Is path absolute?
429 ///
430 /// @param path Input path.
431 /// @result True if the path is absolute, false if it is not.
432 bool is_absolute(const Twine &path, Style style = Style::native);
433 
434 /// Is path relative?
435 ///
436 /// @param path Input path.
437 /// @result True if the path is relative, false if it is not.
438 bool is_relative(const Twine &path, Style style = Style::native);
439 
440 /// Remove redundant leading "./" pieces and consecutive separators.
441 ///
442 /// @param path Input path.
443 /// @result The cleaned-up \a path.
445 
446 /// In-place remove any './' and optionally '../' components from a path.
447 ///
448 /// @param path processed path
449 /// @param remove_dot_dot specify if '../' (except for leading "../") should be
450 /// removed
451 /// @result True if path was changed
452 bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot = false,
453  Style style = Style::native);
454 
455 #if defined(_WIN32)
456 std::error_code widenPath(const Twine &Path8, SmallVectorImpl<wchar_t> &Path16);
457 #endif
458 
459 } // end namespace path
460 } // end namespace sys
461 } // end namespace llvm
462 
463 #endif
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:618
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:259
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:499
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:250
void replace_path_prefix(SmallVectorImpl< char > &Path, const StringRef &OldPrefix, const StringRef &NewPrefix, Style style=Style::native)
Replace matching path prefix with another path.
Definition: Path.cpp:524
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
reference operator*() const
Definition: Path.h:66
bool has_extension(const Twine &path, Style style=Style::native)
Has extension?
Definition: Path.cpp:681
std::string convert_to_slash(StringRef path, Style style=Style::native)
Replaces backslashes with slashes if Windows.
Definition: Path.cpp:581
StringRef remove_leading_dotslash(StringRef path, Style style=Style::native)
Remove redundant leading "./" pieces and consecutive separators.
Definition: Path.cpp:703
bool has_stem(const Twine &path, Style style=Style::native)
Has stem?
Definition: Path.cpp:674
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:480
reverse_iterator rbegin(StringRef path, Style style=Style::native)
Get reverse begin iterator over path.
Definition: Path.cpp:321
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition: Path.cpp:688
StringRef root_path(StringRef path, Style style=Style::native)
Get root path.
Definition: Path.cpp:371
Position
Position to insert a new instruction relative to an existing instruction.
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:68
bool has_parent_path(const Twine &path, Style style=Style::native)
Has parent path?
Definition: Path.cpp:667
bool has_filename(const Twine &path, Style style=Style::native)
Has filename?
Definition: Path.cpp:660
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
Definition: Path.cpp:491
StringRef get_separator(Style style=Style::native)
Return the preferred separator for this platform.
Definition: Path.cpp:626
StringRef root_directory(StringRef path, Style style=Style::native)
Get root directory.
Definition: Path.cpp:414
void replace_extension(SmallVectorImpl< char > &path, const Twine &extension, Style style=Style::native)
Replace the file extension of path with extension.
Definition: Path.cpp:505
bool remove_dots(SmallVectorImpl< char > &path, bool remove_dot_dot=false, Style style=Style::native)
In-place remove any &#39;.
Definition: Path.cpp:741
StringRef root_name(StringRef path, Style style=Style::native)
Get root name.
Definition: Path.cpp:397
reference operator*() const
Definition: Path.h:91
bool has_root_directory(const Twine &path, Style style=Style::native)
Has root directory?
Definition: Path.cpp:639
Path iterator.
Definition: Path.h:53
Reverse path iterator.
Definition: Path.h:79
bool has_root_path(const Twine &path, Style style=Style::native)
Has root path?
Definition: Path.cpp:646
bool has_relative_path(const Twine &path, Style style=Style::native)
Has relative path?
Definition: Path.cpp:653
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:590
bool is_relative(const Twine &path, Style style=Style::native)
Is path relative?
Definition: Path.cpp:699
APInt operator-(APInt)
Definition: APInt.h:2038
reverse_iterator rend(StringRef path)
Get reverse end iterator over path.
Definition: Path.cpp:329
void system_temp_directory(bool erasedOnReboot, SmallVectorImpl< char > &result)
Get the typical temporary directory for the system, e.g., "/var/tmp" or "C:/TEMP".
StringRef relative_path(StringRef path, Style style=Style::native)
Get relative path.
Definition: Path.cpp:437
bool home_directory(SmallVectorImpl< char > &result)
Get the user&#39;s home directory.
StringRef stem(StringRef path, Style style=Style::native)
Get stem.
Definition: Path.cpp:592
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:1961
bool has_root_name(const Twine &path, Style style=Style::native)
Has root name?
Definition: Path.cpp:632
StringRef extension(StringRef path, Style style=Style::native)
Get extension.
Definition: Path.cpp:605