7d4d6d1fc2837bd0741fa43125b79f292e1f2862
[oota-llvm.git] / lib / Support / PathV2.cpp
1 //===-- PathV2.cpp - Implement OS Path 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 implements the operating system PathV2 API.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/PathV2.h"
15 #include "llvm/Support/FileSystem.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include <cctype>
18
19 namespace {
20   using llvm::StringRef;
21
22   bool is_separator(const char value) {
23     switch(value) {
24 #ifdef LLVM_ON_WIN32
25     case '\\': // fall through
26 #endif
27     case '/': return true;
28     default: return false;
29     }
30   }
31
32 #ifdef LLVM_ON_WIN32
33   const StringRef separators = "\\/";
34   const char       prefered_separator = '\\';
35 #else
36   const StringRef separators = "/";
37   const char      prefered_separator = '/';
38 #endif
39
40   StringRef find_first_component(const StringRef  &path) {
41     // Look for this first component in the following order.
42     // * empty (in this case we return an empty string)
43     // * either C: or {//,\\}net.
44     // * {/,\}
45     // * {.,..}
46     // * {file,directory}name
47
48     if (path.empty())
49       return path;
50
51     // C:
52     if (path.size() >= 2 && std::isalpha(path[0]) && path[1] == ':')
53       return StringRef(path.begin(), 2);
54
55     // //net
56     if ((path.size() > 2) &&
57         (path.startswith("\\\\") || path.startswith("//")) &&
58         (path[2] != '\\' && path[2] != '/')) {
59       // Find the next directory separator.
60       size_t end = path.find_first_of("\\/", 2);
61       if (end == StringRef::npos)
62         return path;
63       else
64         return StringRef(path.begin(), end);
65     }
66
67     // {/,\}
68     if (path[0] == '\\' || path[0] == '/')
69       return StringRef(path.begin(), 1);
70
71     if (path.startswith(".."))
72       return StringRef(path.begin(), 2);
73
74     if (path[0] == '.')
75       return StringRef(path.begin(), 1);
76
77     // * {file,directory}name
78     size_t end = path.find_first_of("\\/", 2);
79     if (end == StringRef::npos)
80       return path;
81     else
82       return StringRef(path.begin(), end);
83
84     return StringRef();
85   }
86
87   size_t filename_pos(const StringRef &str) {
88     if (str.size() == 2 &&
89         is_separator(str[0]) &&
90         is_separator(str[1]))
91       return 0;
92
93     if (str.size() > 0 && is_separator(str[str.size() - 1]))
94       return str.size() - 1;
95
96     size_t pos = str.find_last_of(separators, str.size() - 1);
97
98 #ifdef LLVM_ON_WIN32
99     if (pos == StringRef::npos)
100       pos = str.find_last_of(':', str.size() - 2);
101 #endif
102
103     if (pos == StringRef::npos ||
104         (pos == 1 && is_separator(str[0])))
105       return 0;
106
107     return pos + 1;
108   }
109
110   size_t root_dir_start(const StringRef &str) {
111     // case "c:/"
112 #ifdef LLVM_ON_WIN32
113     if (str.size() > 2 &&
114         str[1] == ':' &&
115         is_separator(str[2]))
116       return 2;
117 #endif
118
119     // case "//"
120     if (str.size() == 2 &&
121         is_separator(str[0]) &&
122         str[0] == str[1])
123       return StringRef::npos;
124
125     // case "//net"
126     if (str.size() > 3 &&
127         is_separator(str[0]) &&
128         str[0] == str[1] &&
129         !is_separator(str[2])) {
130       return str.find_first_of(separators, 2);
131     }
132
133     // case "/"
134     if (str.size() > 0 && is_separator(str[0]))
135       return 0;
136
137     return StringRef::npos;
138   }
139
140   size_t parent_path_end(const StringRef &path) {
141     size_t end_pos = filename_pos(path);
142
143     bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
144
145     // Skip separators except for root dir.
146     size_t root_dir_pos = root_dir_start(StringRef(path.begin(), end_pos));
147
148     while(end_pos > 0 &&
149           (end_pos - 1) != root_dir_pos &&
150           is_separator(path[end_pos - 1]))
151       --end_pos;
152
153     if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
154       return StringRef::npos;
155
156     return end_pos;
157   }
158 }
159
160 namespace llvm {
161 namespace sys  {
162 namespace path {
163
164 const_iterator begin(const StringRef &path) {
165   const_iterator i;
166   i.Path      = path;
167   i.Component = find_first_component(path);
168   i.Position  = 0;
169   return i;
170 }
171
172 const_iterator end(const StringRef &path) {
173   const_iterator i;
174   i.Path      = path;
175   i.Position  = path.size();
176   return i;
177 }
178
179 const_iterator &const_iterator::operator++() {
180   assert(Position < Path.size() && "Tried to increment past end!");
181
182   // Increment Position to past the current component
183   Position += Component.size();
184
185   // Check for end.
186   if (Position == Path.size()) {
187     Component = StringRef();
188     return *this;
189   }
190
191   // Both POSIX and Windows treat paths that begin with exactly two separators
192   // specially.
193   bool was_net = Component.size() > 2 &&
194     is_separator(Component[0]) &&
195     Component[1] == Component[0] &&
196     !is_separator(Component[2]);
197
198   // Handle separators.
199   if (is_separator(Path[Position])) {
200     // Root dir.
201     if (was_net
202 #ifdef LLVM_ON_WIN32
203         // c:/
204         || Component.endswith(":")
205 #endif
206         ) {
207       Component = StringRef(Path.begin() + Position, 1);
208       return *this;
209     }
210
211     // Skip extra separators.
212     while (Position != Path.size() &&
213            is_separator(Path[Position])) {
214       ++Position;
215     }
216
217     // Treat trailing '/' as a '.'.
218     if (Position == Path.size()) {
219       --Position;
220       Component = ".";
221       return *this;
222     }
223   }
224
225   // Find next component.
226   size_t end_pos = Path.find_first_of(separators, Position);
227   if (end_pos == StringRef::npos)
228     end_pos = Path.size();
229   Component = StringRef(Path.begin() + Position, end_pos - Position);
230
231   return *this;
232 }
233
234 const_iterator &const_iterator::operator--() {
235   // If we're at the end and the previous char was a '/', return '.'.
236   if (Position == Path.size() &&
237       Path.size() > 1 &&
238       is_separator(Path[Position - 1])
239 #ifdef LLVM_ON_WIN32
240       && Path[Position - 2] != ':'
241 #endif
242       ) {
243     --Position;
244     Component = ".";
245     return *this;
246   }
247
248   // Skip separators unless it's the root directory.
249   size_t root_dir_pos = root_dir_start(Path);
250   size_t end_pos = Position;
251
252   while(end_pos > 0 &&
253         (end_pos - 1) != root_dir_pos &&
254         is_separator(Path[end_pos - 1]))
255     --end_pos;
256
257   // Find next separator.
258   size_t start_pos = filename_pos(StringRef(Path.begin(), end_pos));
259   Component = StringRef(Path.begin() + start_pos, end_pos - start_pos);
260   Position = start_pos;
261   return *this;
262 }
263
264 bool const_iterator::operator==(const const_iterator &RHS) const {
265   return Path.begin() == RHS.Path.begin() &&
266          Position == RHS.Position;
267 }
268
269 bool const_iterator::operator!=(const const_iterator &RHS) const {
270   return !(*this == RHS);
271 }
272
273 ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
274   return Position - RHS.Position;
275 }
276
277 error_code root_path(const StringRef &path, StringRef &result) {
278   const_iterator b = begin(path),
279                  pos = b,
280                  e = end(path);
281   if (b != e) {
282     bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
283     bool has_drive =
284 #ifdef LLVM_ON_WIN32
285       b->endswith(":");
286 #else
287       false;
288 #endif
289
290     if (has_net || has_drive) {
291       if ((++pos != e) && is_separator((*pos)[0])) {
292         // {C:/,//net/}, so get the first two components.
293         result = StringRef(path.begin(), b->size() + pos->size());
294         return make_error_code(errc::success);
295       } else {
296         // just {C:,//net}, return the first component.
297         result = *b;
298         return make_error_code(errc::success);
299       }
300     }
301
302     // POSIX style root directory.
303     if (is_separator((*b)[0])) {
304       result = *b;
305       return make_error_code(errc::success);
306     }
307
308     // No root_path.
309     result = StringRef();
310     return make_error_code(errc::success);
311   }
312
313   // No path :(.
314   result = StringRef();
315   return make_error_code(errc::success);
316 }
317
318 error_code root_name(const StringRef &path, StringRef &result) {
319   const_iterator b = begin(path),
320                  e = end(path);
321   if (b != e) {
322     bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
323     bool has_drive =
324 #ifdef LLVM_ON_WIN32
325       b->endswith(":");
326 #else
327       false;
328 #endif
329
330     if (has_net || has_drive) {
331       // just {C:,//net}, return the first component.
332       result = *b;
333       return make_error_code(errc::success);
334     }
335   }
336
337   // No path or no name.
338   result = StringRef();
339   return make_error_code(errc::success);
340 }
341
342 error_code root_directory(const StringRef &path, StringRef &result) {
343   const_iterator b = begin(path),
344                  pos = b,
345                  e = end(path);
346   if (b != e) {
347     bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
348     bool has_drive =
349 #ifdef LLVM_ON_WIN32
350       b->endswith(":");
351 #else
352       false;
353 #endif
354
355     if ((has_net || has_drive) &&
356         // {C:,//net}, skip to the next component.
357         (++pos != e) && is_separator((*pos)[0])) {
358       result = *pos;
359       return make_error_code(errc::success);
360     }
361
362     // POSIX style root directory.
363     if (!has_net && is_separator((*b)[0])) {
364       result = *b;
365       return make_error_code(errc::success);
366     }
367   }
368
369   // No path or no root.
370   result = StringRef();
371   return make_error_code(errc::success);
372 }
373
374 error_code relative_path(const StringRef &path, StringRef &result) {
375   StringRef root;
376   if (error_code ec = root_path(path, root)) return ec;
377   result = StringRef(path.begin() + root.size(), path.size() - root.size());
378   return make_error_code(errc::success);
379 }
380
381 error_code append(SmallVectorImpl<char> &path, const Twine &a,
382                                                const Twine &b,
383                                                const Twine &c,
384                                                const Twine &d) {
385   SmallString<32> a_storage;
386   SmallString<32> b_storage;
387   SmallString<32> c_storage;
388   SmallString<32> d_storage;
389
390   SmallVector<StringRef, 4> components;
391   if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
392   if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
393   if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
394   if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
395
396   for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
397                                                   e = components.end();
398                                                   i != e; ++i) {
399     bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
400     bool component_has_sep = !i->empty() && is_separator((*i)[0]);
401     bool is_root_name;
402     if (error_code ec = has_root_name(*i, is_root_name)) return ec;
403
404     if (path_has_sep) {
405       // Strip separators from beginning of component.
406       size_t loc = i->find_first_not_of(separators);
407       StringRef c = StringRef(i->begin() + loc, i->size() - loc);
408
409       // Append it.
410       path.append(c.begin(), c.end());
411       continue;
412     }
413
414     if (!component_has_sep && !(path.empty() && is_root_name)) {
415       // Add a separator.
416       path.push_back(prefered_separator);
417     }
418
419     path.append(i->begin(), i->end());
420   }
421
422   return make_error_code(errc::success);
423 }
424
425 error_code make_absolute(SmallVectorImpl<char> &path) {
426   StringRef p(path.data(), path.size());
427
428   bool rootName, rootDirectory;
429   if (error_code ec = has_root_name(p, rootName)) return ec;
430   if (error_code ec = has_root_directory(p, rootDirectory)) return ec;
431
432   // Already absolute.
433   if (rootName && rootDirectory)
434     return make_error_code(errc::success);
435
436   // All of the following conditions will need the current directory.
437   SmallString<128> current_dir;
438   if (error_code ec = current_path(current_dir)) return ec;
439
440   // Relative path. Prepend the current directory.
441   if (!rootName && !rootDirectory) {
442     // Append path to the current directory.
443     if (error_code ec = append(current_dir, p)) return ec;
444     // Set path to the result.
445     path.swap(current_dir);
446     return make_error_code(errc::success);
447   }
448
449   if (!rootName && rootDirectory) {
450     StringRef cdrn;
451     if (error_code ec = root_name(current_dir, cdrn)) return ec;
452     SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
453     if (error_code ec = append(curDirRootName, p)) return ec;
454     // Set path to the result.
455     path.swap(curDirRootName);
456     return make_error_code(errc::success);
457   }
458
459   if (rootName && !rootDirectory) {
460     StringRef pRootName;
461     StringRef bRootDirectory;
462     StringRef bRelativePath;
463     StringRef pRelativePath;
464     if (error_code ec = root_name(p, pRootName)) return ec;
465     if (error_code ec = root_directory(current_dir, bRootDirectory)) return ec;
466     if (error_code ec = relative_path(current_dir, bRelativePath)) return ec;
467     if (error_code ec = relative_path(p, pRelativePath)) return ec;
468
469     SmallString<128> res;
470     if (error_code ec = append(res, pRootName, bRootDirectory,
471                                     bRelativePath, pRelativePath)) return ec;
472     path.swap(res);
473     return make_error_code(errc::success);
474   }
475
476   llvm_unreachable("All rootName and rootDirectory combinations should have "
477                    "occurred above!");
478 }
479
480 error_code parent_path(const StringRef &path, StringRef &result) {
481   size_t end_pos = parent_path_end(path);
482   if (end_pos == StringRef::npos)
483     result = StringRef();
484   else
485     result = StringRef(path.data(), end_pos);
486   return make_error_code(errc::success);
487 }
488
489 error_code remove_filename(SmallVectorImpl<char> &path) {
490   size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
491   if (end_pos == StringRef::npos)
492     return make_error_code(errc::success);
493   path.set_size(end_pos);
494   return make_error_code(errc::success);
495 }
496
497 error_code replace_extension(SmallVectorImpl<char> &path,
498                              const Twine &extension) {
499   StringRef p(path.begin(), path.size());
500   SmallString<32> ext_storage;
501   StringRef ext = extension.toStringRef(ext_storage);
502
503   // Erase existing extension.
504   size_t pos = p.find_last_of('.');
505   if (pos != StringRef::npos && pos >= filename_pos(p))
506     path.set_size(pos);
507
508   // Append '.' if needed.
509   if (ext.size() > 0 && ext[0] != '.')
510     path.push_back('.');
511
512   // Append extension.
513   path.append(ext.begin(), ext.end());
514   return make_error_code(errc::success);
515 }
516
517 error_code native(const Twine &path, SmallVectorImpl<char> &result) {
518   // Clear result.
519   result.set_size(0);
520 #ifdef LLVM_ON_WIN32
521   SmallString<128> path_storage;
522   StringRef p = path.toStringRef(path_storage);
523   result.reserve(p.size());
524   for (StringRef::const_iterator i = p.begin(),
525                                  e = p.end();
526                                  i != e;
527                                  ++i) {
528     if (*i == '/')
529       result.push_back('\\');
530     else
531       result.push_back(*i);
532   }
533 #else
534   path.toVector(result);
535 #endif
536   return make_error_code(errc::success);
537 }
538
539 error_code filename(const StringRef &path, StringRef &result) {
540   result = *(--end(path));
541   return make_error_code(errc::success);
542 }
543
544 error_code stem(const StringRef &path, StringRef &result) {
545   StringRef fname;
546   if (error_code ec = filename(path, fname)) return ec;
547   size_t pos = fname.find_last_of('.');
548   if (pos == StringRef::npos)
549     result = fname;
550   else
551     if ((fname.size() == 1 && fname == ".") ||
552         (fname.size() == 2 && fname == ".."))
553       result = fname;
554     else
555       result = StringRef(fname.begin(), pos);
556
557   return make_error_code(errc::success);
558 }
559
560 error_code extension(const StringRef &path, StringRef &result) {
561   StringRef fname;
562   if (error_code ec = filename(path, fname)) return ec;
563   size_t pos = fname.find_last_of('.');
564   if (pos == StringRef::npos)
565     result = StringRef();
566   else
567     if ((fname.size() == 1 && fname == ".") ||
568         (fname.size() == 2 && fname == ".."))
569       result = StringRef();
570     else
571       result = StringRef(fname.begin() + pos, fname.size() - pos);
572
573   return make_error_code(errc::success);
574 }
575
576 error_code has_root_name(const Twine &path, bool &result) {
577   SmallString<128> path_storage;
578   StringRef p = path.toStringRef(path_storage);
579
580   if (error_code ec = root_name(p, p)) return ec;
581
582   result = !p.empty();
583   return make_error_code(errc::success);
584 }
585
586 error_code has_root_directory(const Twine &path, bool &result) {
587   SmallString<128> path_storage;
588   StringRef p = path.toStringRef(path_storage);
589
590   if (error_code ec = root_directory(p, p)) return ec;
591
592   result = !p.empty();
593   return make_error_code(errc::success);
594 }
595
596 error_code has_root_path(const Twine &path, bool &result) {
597   SmallString<128> path_storage;
598   StringRef p = path.toStringRef(path_storage);
599
600   if (error_code ec = root_path(p, p)) return ec;
601
602   result = !p.empty();
603   return make_error_code(errc::success);
604 }
605
606 error_code has_filename(const Twine &path, bool &result) {
607   SmallString<128> path_storage;
608   StringRef p = path.toStringRef(path_storage);
609
610   if (error_code ec = filename(p, p)) return ec;
611
612   result = !p.empty();
613   return make_error_code(errc::success);
614 }
615
616 error_code has_parent_path(const Twine &path, bool &result) {
617   SmallString<128> path_storage;
618   StringRef p = path.toStringRef(path_storage);
619
620   if (error_code ec = parent_path(p, p)) return ec;
621
622   result = !p.empty();
623   return make_error_code(errc::success);
624 }
625
626 error_code has_stem(const Twine &path, bool &result) {
627   SmallString<128> path_storage;
628   StringRef p = path.toStringRef(path_storage);
629
630   if (error_code ec = stem(p, p)) return ec;
631
632   result = !p.empty();
633   return make_error_code(errc::success);
634 }
635
636 error_code has_extension(const Twine &path, bool &result) {
637   SmallString<128> path_storage;
638   StringRef p = path.toStringRef(path_storage);
639
640   if (error_code ec = extension(p, p)) return ec;
641
642   result = !p.empty();
643   return make_error_code(errc::success);
644 }
645
646 error_code is_absolute(const Twine &path, bool &result) {
647   SmallString<128> path_storage;
648   StringRef p = path.toStringRef(path_storage);
649
650   bool rootDir = false,
651        rootName = false;
652   if (error_code ec = has_root_directory(p, rootDir)) return ec;
653 #ifdef LLVM_ON_WIN32
654   if (error_code ec = has_root_name(p, rootName)) return ec;
655 #else
656   rootName = true;
657 #endif
658
659   result = rootDir && rootName;
660   return make_error_code(errc::success);
661 }
662
663 error_code is_relative(const Twine &path, bool &result) {
664   bool res;
665   error_code ec = is_absolute(path, res);
666   result = !res;
667   return ec;
668 }
669
670 } // end namespace path
671
672 namespace fs {
673
674 error_code create_directories(const Twine &path, bool &existed) {
675   SmallString<128> path_storage;
676   StringRef p = path.toStringRef(path_storage);
677
678   StringRef parent;
679   bool parent_exists;
680   if (error_code ec = path::parent_path(p, parent)) return ec;
681   if (error_code ec = fs::exists(parent, parent_exists)) return ec;
682
683   if (!parent_exists)
684     return create_directories(parent, existed);
685
686   return create_directory(p, existed);
687 }
688
689 } // end namespace fs
690 } // end namespace sys
691 } // end namespace llvm
692
693 // Include the truly platform-specific parts.
694 #if defined(LLVM_ON_UNIX)
695 #include "Unix/PathV2.inc"
696 #endif
697 #if defined(LLVM_ON_WIN32)
698 #include "Windows/PathV2.inc"
699 #endif