Remove use of GNU extension to resolve Clang warning.
[oota-llvm.git] / include / llvm / Support / FileSystem.h
1 //===- llvm/Support/FileSystem.h - File System OS 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::fs namespace. It is designed after
11 // TR2/boost filesystem (v3), but modified to remove exception handling and the
12 // path class.
13 //
14 // All functions return an error_code and their actual work via the last out
15 // argument. The out argument is defined if and only if errc::success is
16 // returned. A function may return any error code in the generic or system
17 // category. However, they shall be equivalent to any error conditions listed
18 // in each functions respective documentation if the condition applies. [ note:
19 // this does not guarantee that error_code will be in the set of explicitly
20 // listed codes, but it does guarantee that if any of the explicitly listed
21 // errors occur, the correct error_code will be used ]. All functions may
22 // return errc::not_enough_memory if there is not enough memory to complete the
23 // operation.
24 //
25 //===----------------------------------------------------------------------===//
26
27 #ifndef LLVM_SUPPORT_FILE_SYSTEM_H
28 #define LLVM_SUPPORT_FILE_SYSTEM_H
29
30 #include "llvm/ADT/IntrusiveRefCntPtr.h"
31 #include "llvm/ADT/SmallString.h"
32 #include "llvm/ADT/Twine.h"
33 #include "llvm/Support/DataTypes.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/system_error.h"
36 #include <ctime>
37 #include <iterator>
38 #include <stack>
39 #include <string>
40 #include <vector>
41
42 #if HAVE_SYS_STAT_H
43 #include <sys/stat.h>
44 #endif
45
46 namespace llvm {
47 namespace sys {
48 namespace fs {
49
50 /// file_type - An "enum class" enumeration for the file system's view of the
51 ///             type.
52 struct file_type {
53   enum _ {
54     status_error,
55     file_not_found,
56     regular_file,
57     directory_file,
58     symlink_file,
59     block_file,
60     character_file,
61     fifo_file,
62     socket_file,
63     type_unknown
64   };
65
66   file_type(_ v) : v_(v) {}
67   explicit file_type(int v) : v_(_(v)) {}
68   operator int() const {return v_;}
69
70 private:
71   int v_;
72 };
73
74 /// copy_option - An "enum class" enumeration of copy semantics for copy
75 ///               operations.
76 struct copy_option {
77   enum _ {
78     fail_if_exists,
79     overwrite_if_exists
80   };
81
82   copy_option(_ v) : v_(v) {}
83   explicit copy_option(int v) : v_(_(v)) {}
84   operator int() const {return v_;}
85
86 private:
87   int v_;
88 };
89
90 /// space_info - Self explanatory.
91 struct space_info {
92   uint64_t capacity;
93   uint64_t free;
94   uint64_t available;
95 };
96
97 /// file_status - Represents the result of a call to stat and friends. It has
98 ///               a platform specific member to store the result.
99 class file_status
100 {
101   #if defined(LLVM_ON_UNIX)
102   dev_t fs_st_dev;
103   ino_t fs_st_ino;
104   #elif defined (LLVM_ON_WIN32)
105   uint32_t LastWriteTimeHigh;
106   uint32_t LastWriteTimeLow;
107   uint32_t VolumeSerialNumber;
108   uint32_t FileSizeHigh;
109   uint32_t FileSizeLow;
110   uint32_t FileIndexHigh;
111   uint32_t FileIndexLow;
112   #endif
113   friend bool equivalent(file_status A, file_status B);
114   friend error_code status(const Twine &path, file_status &result);
115   file_type Type;
116 public:
117   explicit file_status(file_type v=file_type::status_error)
118     : Type(v) {}
119
120   file_type type() const { return Type; }
121   void type(file_type v) { Type = v; }
122 };
123
124 /// file_magic - An "enum class" enumeration of file types based on magic (the first
125 ///         N bytes of the file).
126 struct file_magic {
127   enum _ {
128     unknown = 0,              ///< Unrecognized file
129     bitcode,                  ///< Bitcode file
130     archive,                  ///< ar style archive file
131     elf_relocatable,          ///< ELF Relocatable object file
132     elf_executable,           ///< ELF Executable image
133     elf_shared_object,        ///< ELF dynamically linked shared lib
134     elf_core,                 ///< ELF core image
135     macho_object,             ///< Mach-O Object file
136     macho_executable,         ///< Mach-O Executable
137     macho_fixed_virtual_memory_shared_lib, ///< Mach-O Shared Lib, FVM
138     macho_core,               ///< Mach-O Core File
139     macho_preload_executabl,  ///< Mach-O Preloaded Executable
140     macho_dynamically_linked_shared_lib, ///< Mach-O dynlinked shared lib
141     macho_dynamic_linker,     ///< The Mach-O dynamic linker
142     macho_bundle,             ///< Mach-O Bundle file
143     macho_dynamically_linked_shared_lib_stub, ///< Mach-O Shared lib stub
144     macho_dsym_companion,     ///< Mach-O dSYM companion file
145     coff_object,              ///< COFF object file
146     pecoff_executable         ///< PECOFF executable file
147   };
148
149   bool is_object() const {
150     return v_ == unknown ? false : true;
151   }
152
153   file_magic() : v_(unknown) {}
154   file_magic(_ v) : v_(v) {}
155   explicit file_magic(int v) : v_(_(v)) {}
156   operator int() const {return v_;}
157
158 private:
159   int v_;
160 };
161
162 /// @}
163 /// @name Physical Operators
164 /// @{
165
166 /// @brief Make \a path an absolute path.
167 ///
168 /// Makes \a path absolute using the current directory if it is not already. An
169 /// empty \a path will result in the current directory.
170 ///
171 /// /absolute/path   => /absolute/path
172 /// relative/../path => <current-directory>/relative/../path
173 ///
174 /// @param path A path that is modified to be an absolute path.
175 /// @returns errc::success if \a path has been made absolute, otherwise a
176 ///          platform specific error_code.
177 error_code make_absolute(SmallVectorImpl<char> &path);
178
179 /// @brief Copy the file at \a from to the path \a to.
180 ///
181 /// @param from The path to copy the file from.
182 /// @param to The path to copy the file to.
183 /// @param copt Behavior if \a to already exists.
184 /// @returns errc::success if the file has been successfully copied.
185 ///          errc::file_exists if \a to already exists and \a copt ==
186 ///          copy_option::fail_if_exists. Otherwise a platform specific
187 ///          error_code.
188 error_code copy_file(const Twine &from, const Twine &to,
189                      copy_option copt = copy_option::fail_if_exists);
190
191 /// @brief Create all the non-existent directories in path.
192 ///
193 /// @param path Directories to create.
194 /// @param existed Set to true if \a path already existed, false otherwise.
195 /// @returns errc::success if is_directory(path) and existed have been set,
196 ///          otherwise a platform specific error_code.
197 error_code create_directories(const Twine &path, bool &existed);
198
199 /// @brief Create the directory in path.
200 ///
201 /// @param path Directory to create.
202 /// @param existed Set to true if \a path already existed, false otherwise.
203 /// @returns errc::success if is_directory(path) and existed have been set,
204 ///          otherwise a platform specific error_code.
205 error_code create_directory(const Twine &path, bool &existed);
206
207 /// @brief Create a hard link from \a from to \a to.
208 ///
209 /// @param to The path to hard link to.
210 /// @param from The path to hard link from. This is created.
211 /// @returns errc::success if exists(to) && exists(from) && equivalent(to, from)
212 ///          , otherwise a platform specific error_code.
213 error_code create_hard_link(const Twine &to, const Twine &from);
214
215 /// @brief Create a symbolic link from \a from to \a to.
216 ///
217 /// @param to The path to symbolically link to.
218 /// @param from The path to symbolically link from. This is created.
219 /// @returns errc::success if exists(to) && exists(from) && is_symlink(from),
220 ///          otherwise a platform specific error_code.
221 error_code create_symlink(const Twine &to, const Twine &from);
222
223 /// @brief Get the current path.
224 ///
225 /// @param result Holds the current path on return.
226 /// @results errc::success if the current path has been stored in result,
227 ///          otherwise a platform specific error_code.
228 error_code current_path(SmallVectorImpl<char> &result);
229
230 /// @brief Remove path. Equivalent to POSIX remove().
231 ///
232 /// @param path Input path.
233 /// @param existed Set to true if \a path existed, false if it did not.
234 ///                undefined otherwise.
235 /// @results errc::success if path has been removed and existed has been
236 ///          successfully set, otherwise a platform specific error_code.
237 error_code remove(const Twine &path, bool &existed);
238
239 /// @brief Recursively remove all files below \a path, then \a path. Files are
240 ///        removed as if by POSIX remove().
241 ///
242 /// @param path Input path.
243 /// @param num_removed Number of files removed.
244 /// @results errc::success if path has been removed and num_removed has been
245 ///          successfully set, otherwise a platform specific error_code.
246 error_code remove_all(const Twine &path, uint32_t &num_removed);
247
248 /// @brief Rename \a from to \a to. Files are renamed as if by POSIX rename().
249 ///
250 /// @param from The path to rename from.
251 /// @param to The path to rename to. This is created.
252 error_code rename(const Twine &from, const Twine &to);
253
254 /// @brief Resize path to size. File is resized as if by POSIX truncate().
255 ///
256 /// @param path Input path.
257 /// @param size Size to resize to.
258 /// @returns errc::success if \a path has been resized to \a size, otherwise a
259 ///          platform specific error_code.
260 error_code resize_file(const Twine &path, uint64_t size);
261
262 /// @}
263 /// @name Physical Observers
264 /// @{
265
266 /// @brief Does file exist?
267 ///
268 /// @param status A file_status previously returned from stat.
269 /// @results True if the file represented by status exists, false if it does
270 ///          not.
271 bool exists(file_status status);
272
273 /// @brief Does file exist?
274 ///
275 /// @param path Input path.
276 /// @param result Set to true if the file represented by status exists, false if
277 ///               it does not. Undefined otherwise.
278 /// @results errc::success if result has been successfully set, otherwise a
279 ///          platform specific error_code.
280 error_code exists(const Twine &path, bool &result);
281
282 /// @brief Simpler version of exists for clients that don't need to
283 ///        differentiate between an error and false.
284 inline bool exists(const Twine &path) {
285   bool result;
286   return !exists(path, result) && result;
287 }
288
289 /// @brief Do file_status's represent the same thing?
290 ///
291 /// @param A Input file_status.
292 /// @param B Input file_status.
293 ///
294 /// assert(status_known(A) || status_known(B));
295 ///
296 /// @results True if A and B both represent the same file system entity, false
297 ///          otherwise.
298 bool equivalent(file_status A, file_status B);
299
300 /// @brief Do paths represent the same thing?
301 ///
302 /// assert(status_known(A) || status_known(B));
303 ///
304 /// @param A Input path A.
305 /// @param B Input path B.
306 /// @param result Set to true if stat(A) and stat(B) have the same device and
307 ///               inode (or equivalent).
308 /// @results errc::success if result has been successfully set, otherwise a
309 ///          platform specific error_code.
310 error_code equivalent(const Twine &A, const Twine &B, bool &result);
311
312 /// @brief Simpler version of equivalent for clients that don't need to
313 ///        differentiate between an error and false.
314 inline bool equivalent(const Twine &A, const Twine &B) {
315   bool result;
316   return !equivalent(A, B, result) && result;
317 }
318
319 /// @brief Get file size.
320 ///
321 /// @param path Input path.
322 /// @param result Set to the size of the file in \a path.
323 /// @returns errc::success if result has been successfully set, otherwise a
324 ///          platform specific error_code.
325 error_code file_size(const Twine &path, uint64_t &result);
326
327 /// @brief Does status represent a directory?
328 ///
329 /// @param status A file_status previously returned from status.
330 /// @results status.type() == file_type::directory_file.
331 bool is_directory(file_status status);
332
333 /// @brief Is path a directory?
334 ///
335 /// @param path Input path.
336 /// @param result Set to true if \a path is a directory, false if it is not.
337 ///               Undefined otherwise.
338 /// @results errc::success if result has been successfully set, otherwise a
339 ///          platform specific error_code.
340 error_code is_directory(const Twine &path, bool &result);
341
342 /// @brief Does status represent a regular file?
343 ///
344 /// @param status A file_status previously returned from status.
345 /// @results status_known(status) && status.type() == file_type::regular_file.
346 bool is_regular_file(file_status status);
347
348 /// @brief Is path a regular file?
349 ///
350 /// @param path Input path.
351 /// @param result Set to true if \a path is a regular file, false if it is not.
352 ///               Undefined otherwise.
353 /// @results errc::success if result has been successfully set, otherwise a
354 ///          platform specific error_code.
355 error_code is_regular_file(const Twine &path, bool &result);
356
357 /// @brief Does this status represent something that exists but is not a
358 ///        directory, regular file, or symlink?
359 ///
360 /// @param status A file_status previously returned from status.
361 /// @results exists(s) && !is_regular_file(s) && !is_directory(s) &&
362 ///          !is_symlink(s)
363 bool is_other(file_status status);
364
365 /// @brief Is path something that exists but is not a directory,
366 ///        regular file, or symlink?
367 ///
368 /// @param path Input path.
369 /// @param result Set to true if \a path exists, but is not a directory, regular
370 ///               file, or a symlink, false if it does not. Undefined otherwise.
371 /// @results errc::success if result has been successfully set, otherwise a
372 ///          platform specific error_code.
373 error_code is_other(const Twine &path, bool &result);
374
375 /// @brief Does status represent a symlink?
376 ///
377 /// @param status A file_status previously returned from stat.
378 /// @param result status.type() == symlink_file.
379 bool is_symlink(file_status status);
380
381 /// @brief Is path a symlink?
382 ///
383 /// @param path Input path.
384 /// @param result Set to true if \a path is a symlink, false if it is not.
385 ///               Undefined otherwise.
386 /// @results errc::success if result has been successfully set, otherwise a
387 ///          platform specific error_code.
388 error_code is_symlink(const Twine &path, bool &result);
389
390 /// @brief Get file status as if by POSIX stat().
391 ///
392 /// @param path Input path.
393 /// @param result Set to the file status.
394 /// @results errc::success if result has been successfully set, otherwise a
395 ///          platform specific error_code.
396 error_code status(const Twine &path, file_status &result);
397
398 /// @brief Is status available?
399 ///
400 /// @param path Input path.
401 /// @results True if status() != status_error.
402 bool status_known(file_status s);
403
404 /// @brief Is status available?
405 ///
406 /// @param path Input path.
407 /// @param result Set to true if status() != status_error.
408 /// @results errc::success if result has been successfully set, otherwise a
409 ///          platform specific error_code.
410 error_code status_known(const Twine &path, bool &result);
411
412 /// @brief Generate a unique path and open it as a file.
413 ///
414 /// Generates a unique path suitable for a temporary file and then opens it as a
415 /// file. The name is based on \a model with '%' replaced by a random char in
416 /// [0-9a-f]. If \a model is not an absolute path, a suitable temporary
417 /// directory will be prepended.
418 ///
419 /// This is an atomic operation. Either the file is created and opened, or the
420 /// file system is left untouched.
421 ///
422 /// clang-%%-%%-%%-%%-%%.s => /tmp/clang-a0-b1-c2-d3-e4.s
423 ///
424 /// @param model Name to base unique path off of.
425 /// @param result_fs Set to the opened file's file descriptor.
426 /// @param result_path Set to the opened file's absolute path.
427 /// @param makeAbsolute If true and @model is not an absolute path, a temp
428 ///        directory will be prepended.
429 /// @results errc::success if result_{fd,path} have been successfully set,
430 ///          otherwise a platform specific error_code.
431 error_code unique_file(const Twine &model, int &result_fd,
432                        SmallVectorImpl<char> &result_path,
433                        bool makeAbsolute = true, unsigned mode = 0600);
434
435 /// @brief Canonicalize path.
436 ///
437 /// Sets result to the file system's idea of what path is. The result is always
438 /// absolute and has the same capitalization as the file system.
439 ///
440 /// @param path Input path.
441 /// @param result Set to the canonicalized version of \a path.
442 /// @results errc::success if result has been successfully set, otherwise a
443 ///          platform specific error_code.
444 error_code canonicalize(const Twine &path, SmallVectorImpl<char> &result);
445
446 /// @brief Are \a path's first bytes \a magic?
447 ///
448 /// @param path Input path.
449 /// @param magic Byte sequence to compare \a path's first len(magic) bytes to.
450 /// @results errc::success if result has been successfully set, otherwise a
451 ///          platform specific error_code.
452 error_code has_magic(const Twine &path, const Twine &magic, bool &result);
453
454 /// @brief Get \a path's first \a len bytes.
455 ///
456 /// @param path Input path.
457 /// @param len Number of magic bytes to get.
458 /// @param result Set to the first \a len bytes in the file pointed to by
459 ///               \a path. Or the entire file if file_size(path) < len, in which
460 ///               case result.size() returns the size of the file.
461 /// @results errc::success if result has been successfully set,
462 ///          errc::value_too_large if len is larger then the file pointed to by
463 ///          \a path, otherwise a platform specific error_code.
464 error_code get_magic(const Twine &path, uint32_t len,
465                      SmallVectorImpl<char> &result);
466
467 /// @brief Identify the type of a binary file based on how magical it is.
468 file_magic identify_magic(StringRef magic);
469
470 /// @brief Get and identify \a path's type based on its content.
471 ///
472 /// @param path Input path.
473 /// @param result Set to the type of file, or LLVMFileType::Unknown_FileType.
474 /// @results errc::success if result has been successfully set, otherwise a
475 ///          platform specific error_code.
476 error_code identify_magic(const Twine &path, file_magic &result);
477
478 /// @brief Get library paths the system linker uses.
479 ///
480 /// @param result Set to the list of system library paths.
481 /// @results errc::success if result has been successfully set, otherwise a
482 ///          platform specific error_code.
483 error_code GetSystemLibraryPaths(SmallVectorImpl<std::string> &result);
484
485 /// @brief Get bitcode library paths the system linker uses
486 ///        + LLVM_LIB_SEARCH_PATH + LLVM_LIBDIR.
487 ///
488 /// @param result Set to the list of bitcode library paths.
489 /// @results errc::success if result has been successfully set, otherwise a
490 ///          platform specific error_code.
491 error_code GetBitcodeLibraryPaths(SmallVectorImpl<std::string> &result);
492
493 /// @brief Find a library.
494 ///
495 /// Find the path to a library using its short name. Use the system
496 /// dependent library paths to locate the library.
497 ///
498 /// c => /usr/lib/libc.so
499 ///
500 /// @param short_name Library name one would give to the system linker.
501 /// @param result Set to the absolute path \a short_name represents.
502 /// @results errc::success if result has been successfully set, otherwise a
503 ///          platform specific error_code.
504 error_code FindLibrary(const Twine &short_name, SmallVectorImpl<char> &result);
505
506 /// @brief Get absolute path of main executable.
507 ///
508 /// @param argv0 The program name as it was spelled on the command line.
509 /// @param MainAddr Address of some symbol in the executable (not in a library).
510 /// @param result Set to the absolute path of the current executable.
511 /// @results errc::success if result has been successfully set, otherwise a
512 ///          platform specific error_code.
513 error_code GetMainExecutable(const char *argv0, void *MainAddr,
514                              SmallVectorImpl<char> &result);
515
516 /// @}
517 /// @name Iterators
518 /// @{
519
520 /// directory_entry - A single entry in a directory. Caches the status either
521 /// from the result of the iteration syscall, or the first time status is
522 /// called.
523 class directory_entry {
524   std::string Path;
525   mutable file_status Status;
526
527 public:
528   explicit directory_entry(const Twine &path, file_status st = file_status())
529     : Path(path.str())
530     , Status(st) {}
531
532   directory_entry() {}
533
534   void assign(const Twine &path, file_status st = file_status()) {
535     Path = path.str();
536     Status = st;
537   }
538
539   void replace_filename(const Twine &filename, file_status st = file_status());
540
541   const std::string &path() const { return Path; }
542   error_code status(file_status &result) const;
543
544   bool operator==(const directory_entry& rhs) const { return Path == rhs.Path; }
545   bool operator!=(const directory_entry& rhs) const { return !(*this == rhs); }
546   bool operator< (const directory_entry& rhs) const;
547   bool operator<=(const directory_entry& rhs) const;
548   bool operator> (const directory_entry& rhs) const;
549   bool operator>=(const directory_entry& rhs) const;
550 };
551
552 namespace detail {
553   struct DirIterState;
554
555   error_code directory_iterator_construct(DirIterState&, StringRef);
556   error_code directory_iterator_increment(DirIterState&);
557   error_code directory_iterator_destruct(DirIterState&);
558
559   /// DirIterState - Keeps state for the directory_iterator. It is reference
560   /// counted in order to preserve InputIterator semantics on copy.
561   struct DirIterState : public RefCountedBase<DirIterState> {
562     DirIterState()
563       : IterationHandle(0) {}
564
565     ~DirIterState() {
566       directory_iterator_destruct(*this);
567     }
568
569     intptr_t IterationHandle;
570     directory_entry CurrentEntry;
571   };
572 }
573
574 /// directory_iterator - Iterates through the entries in path. There is no
575 /// operator++ because we need an error_code. If it's really needed we can make
576 /// it call report_fatal_error on error.
577 class directory_iterator {
578   IntrusiveRefCntPtr<detail::DirIterState> State;
579
580 public:
581   explicit directory_iterator(const Twine &path, error_code &ec) {
582     State = new detail::DirIterState;
583     SmallString<128> path_storage;
584     ec = detail::directory_iterator_construct(*State,
585             path.toStringRef(path_storage));
586   }
587
588   explicit directory_iterator(const directory_entry &de, error_code &ec) {
589     State = new detail::DirIterState;
590     ec = detail::directory_iterator_construct(*State, de.path());
591   }
592
593   /// Construct end iterator.
594   directory_iterator() : State(new detail::DirIterState) {}
595
596   // No operator++ because we need error_code.
597   directory_iterator &increment(error_code &ec) {
598     ec = directory_iterator_increment(*State);
599     return *this;
600   }
601
602   const directory_entry &operator*() const { return State->CurrentEntry; }
603   const directory_entry *operator->() const { return &State->CurrentEntry; }
604
605   bool operator==(const directory_iterator &RHS) const {
606     return State->CurrentEntry == RHS.State->CurrentEntry;
607   }
608
609   bool operator!=(const directory_iterator &RHS) const {
610     return !(*this == RHS);
611   }
612   // Other members as required by
613   // C++ Std, 24.1.1 Input iterators [input.iterators]
614 };
615
616 namespace detail {
617   /// RecDirIterState - Keeps state for the recursive_directory_iterator. It is
618   /// reference counted in order to preserve InputIterator semantics on copy.
619   struct RecDirIterState : public RefCountedBase<RecDirIterState> {
620     RecDirIterState()
621       : Level(0)
622       , HasNoPushRequest(false) {}
623
624     std::stack<directory_iterator, std::vector<directory_iterator> > Stack;
625     uint16_t Level;
626     bool HasNoPushRequest;
627   };
628 }
629
630 /// recursive_directory_iterator - Same as directory_iterator except for it
631 /// recurses down into child directories.
632 class recursive_directory_iterator {
633   IntrusiveRefCntPtr<detail::RecDirIterState> State;
634
635 public:
636   recursive_directory_iterator() {}
637   explicit recursive_directory_iterator(const Twine &path, error_code &ec)
638     : State(new detail::RecDirIterState) {
639     State->Stack.push(directory_iterator(path, ec));
640     if (State->Stack.top() == directory_iterator())
641       State.reset();
642   }
643   // No operator++ because we need error_code.
644   recursive_directory_iterator &increment(error_code &ec) {
645     static const directory_iterator end_itr;
646
647     if (State->HasNoPushRequest)
648       State->HasNoPushRequest = false;
649     else {
650       file_status st;
651       if ((ec = State->Stack.top()->status(st))) return *this;
652       if (is_directory(st)) {
653         State->Stack.push(directory_iterator(*State->Stack.top(), ec));
654         if (ec) return *this;
655         if (State->Stack.top() != end_itr) {
656           ++State->Level;
657           return *this;
658         }
659         State->Stack.pop();
660       }
661     }
662
663     while (!State->Stack.empty()
664            && State->Stack.top().increment(ec) == end_itr) {
665       State->Stack.pop();
666       --State->Level;
667     }
668
669     // Check if we are done. If so, create an end iterator.
670     if (State->Stack.empty())
671       State.reset();
672
673     return *this;
674   }
675
676   const directory_entry &operator*() const { return *State->Stack.top(); }
677   const directory_entry *operator->() const { return &*State->Stack.top(); }
678
679   // observers
680   /// Gets the current level. Starting path is at level 0.
681   int level() const { return State->Level; }
682
683   /// Returns true if no_push has been called for this directory_entry.
684   bool no_push_request() const { return State->HasNoPushRequest; }
685
686   // modifiers
687   /// Goes up one level if Level > 0.
688   void pop() {
689     assert(State && "Cannot pop and end itertor!");
690     assert(State->Level > 0 && "Cannot pop an iterator with level < 1");
691
692     static const directory_iterator end_itr;
693     error_code ec;
694     do {
695       if (ec)
696         report_fatal_error("Error incrementing directory iterator.");
697       State->Stack.pop();
698       --State->Level;
699     } while (!State->Stack.empty()
700              && State->Stack.top().increment(ec) == end_itr);
701
702     // Check if we are done. If so, create an end iterator.
703     if (State->Stack.empty())
704       State.reset();
705   }
706
707   /// Does not go down into the current directory_entry.
708   void no_push() { State->HasNoPushRequest = true; }
709
710   bool operator==(const recursive_directory_iterator &RHS) const {
711     return State == RHS.State;
712   }
713
714   bool operator!=(const recursive_directory_iterator &RHS) const {
715     return !(*this == RHS);
716   }
717   // Other members as required by
718   // C++ Std, 24.1.1 Input iterators [input.iterators]
719 };
720
721 /// @}
722
723 } // end namespace fs
724 } // end namespace sys
725 } // end namespace llvm
726
727 #endif