1 //===-- FileSpec.cpp --------------------------------------------*- 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 #include "lldb/Utility/FileSpec.h"
11 #include "lldb/Utility/RegularExpression.h"
12 #include "lldb/Utility/Stream.h"
13 #include "lldb/Utility/TildeExpressionResolver.h"
14 
15 #include "llvm/ADT/SmallString.h" // for SmallString
16 #include "llvm/ADT/SmallVector.h" // for SmallVectorTemplat...
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Triple.h"         // for Triple
19 #include "llvm/ADT/Twine.h"          // for Twine
20 #include "llvm/Config/llvm-config.h" // for LLVM_ON_WIN32
21 #include "llvm/Support/ErrorOr.h"    // for ErrorOr
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/Path.h"
24 #include "llvm/Support/Program.h"
25 #include "llvm/Support/raw_ostream.h" // for raw_ostream, fs
26 
27 #include <algorithm>    // for replace, min, unique
28 #include <system_error> // for error_code
29 #include <vector>       // for vector
30 
31 #include <assert.h> // for assert
32 #include <stdio.h>  // for size_t, NULL, snpr...
33 #include <string.h> // for strcmp
34 
35 using namespace lldb;
36 using namespace lldb_private;
37 
38 namespace {
39 
40 static constexpr FileSpec::PathSyntax GetNativeSyntax() {
41 #if defined(LLVM_ON_WIN32)
42   return FileSpec::ePathSyntaxWindows;
43 #else
44   return FileSpec::ePathSyntaxPosix;
45 #endif
46 }
47 
48 bool PathSyntaxIsPosix(FileSpec::PathSyntax syntax) {
49   return (syntax == FileSpec::ePathSyntaxPosix ||
50           (syntax == FileSpec::ePathSyntaxHostNative &&
51            GetNativeSyntax() == FileSpec::ePathSyntaxPosix));
52 }
53 
54 const char *GetPathSeparators(FileSpec::PathSyntax syntax) {
55   return PathSyntaxIsPosix(syntax) ? "/" : "\\/";
56 }
57 
58 char GetPreferredPathSeparator(FileSpec::PathSyntax syntax) {
59   return GetPathSeparators(syntax)[0];
60 }
61 
62 bool IsPathSeparator(char value, FileSpec::PathSyntax syntax) {
63   return value == '/' || (!PathSyntaxIsPosix(syntax) && value == '\\');
64 }
65 
66 void Normalize(llvm::SmallVectorImpl<char> &path, FileSpec::PathSyntax syntax) {
67   if (PathSyntaxIsPosix(syntax))
68     return;
69 
70   std::replace(path.begin(), path.end(), '\\', '/');
71   // Windows path can have \\ slashes which can be changed by replace
72   // call above to //. Here we remove the duplicate.
73   auto iter = std::unique(path.begin(), path.end(), [](char &c1, char &c2) {
74     return (c1 == '/' && c2 == '/');
75   });
76   path.erase(iter, path.end());
77 }
78 
79 void Denormalize(llvm::SmallVectorImpl<char> &path,
80                  FileSpec::PathSyntax syntax) {
81   if (PathSyntaxIsPosix(syntax))
82     return;
83 
84   std::replace(path.begin(), path.end(), '/', '\\');
85 }
86 
87 size_t FilenamePos(llvm::StringRef str, FileSpec::PathSyntax syntax) {
88   if (str.size() == 2 && IsPathSeparator(str[0], syntax) && str[0] == str[1])
89     return 0;
90 
91   if (str.size() > 0 && IsPathSeparator(str.back(), syntax))
92     return str.size() - 1;
93 
94   size_t pos = str.find_last_of(GetPathSeparators(syntax), str.size() - 1);
95 
96   if (!PathSyntaxIsPosix(syntax) && pos == llvm::StringRef::npos)
97     pos = str.find_last_of(':', str.size() - 2);
98 
99   if (pos == llvm::StringRef::npos ||
100       (pos == 1 && IsPathSeparator(str[0], syntax)))
101     return 0;
102 
103   return pos + 1;
104 }
105 
106 size_t RootDirStart(llvm::StringRef str, FileSpec::PathSyntax syntax) {
107   // case "c:/"
108   if (!PathSyntaxIsPosix(syntax) &&
109       (str.size() > 2 && str[1] == ':' && IsPathSeparator(str[2], syntax)))
110     return 2;
111 
112   // case "//"
113   if (str.size() == 2 && IsPathSeparator(str[0], syntax) && str[0] == str[1])
114     return llvm::StringRef::npos;
115 
116   // case "//net"
117   if (str.size() > 3 && IsPathSeparator(str[0], syntax) && str[0] == str[1] &&
118       !IsPathSeparator(str[2], syntax))
119     return str.find_first_of(GetPathSeparators(syntax), 2);
120 
121   // case "/"
122   if (str.size() > 0 && IsPathSeparator(str[0], syntax))
123     return 0;
124 
125   return llvm::StringRef::npos;
126 }
127 
128 size_t ParentPathEnd(llvm::StringRef path, FileSpec::PathSyntax syntax) {
129   size_t end_pos = FilenamePos(path, syntax);
130 
131   bool filename_was_sep =
132       path.size() > 0 && IsPathSeparator(path[end_pos], syntax);
133 
134   // Skip separators except for root dir.
135   size_t root_dir_pos = RootDirStart(path.substr(0, end_pos), syntax);
136 
137   while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
138          IsPathSeparator(path[end_pos - 1], syntax))
139     --end_pos;
140 
141   if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
142     return llvm::StringRef::npos;
143 
144   return end_pos;
145 }
146 
147 } // end anonymous namespace
148 
149 void FileSpec::Resolve(llvm::SmallVectorImpl<char> &path) {
150   if (path.empty())
151     return;
152 
153   llvm::SmallString<32> Source(path.begin(), path.end());
154   StandardTildeExpressionResolver Resolver;
155   Resolver.ResolveFullPath(Source, path);
156 
157   // Save a copy of the original path that's passed in
158   llvm::SmallString<128> original_path(path.begin(), path.end());
159 
160   llvm::sys::fs::make_absolute(path);
161   if (!llvm::sys::fs::exists(path)) {
162     path.clear();
163     path.append(original_path.begin(), original_path.end());
164   }
165 }
166 
167 FileSpec::FileSpec() : m_syntax(GetNativeSyntax()) {}
168 
169 //------------------------------------------------------------------
170 // Default constructor that can take an optional full path to a
171 // file on disk.
172 //------------------------------------------------------------------
173 FileSpec::FileSpec(llvm::StringRef path, bool resolve_path, PathSyntax syntax)
174     : m_syntax(syntax) {
175   SetFile(path, resolve_path, syntax);
176 }
177 
178 FileSpec::FileSpec(llvm::StringRef path, bool resolve_path,
179                    const llvm::Triple &Triple)
180     : FileSpec{path, resolve_path,
181                Triple.isOSWindows() ? ePathSyntaxWindows : ePathSyntaxPosix} {}
182 
183 //------------------------------------------------------------------
184 // Copy constructor
185 //------------------------------------------------------------------
186 FileSpec::FileSpec(const FileSpec &rhs)
187     : m_directory(rhs.m_directory), m_filename(rhs.m_filename),
188       m_is_resolved(rhs.m_is_resolved), m_syntax(rhs.m_syntax) {}
189 
190 //------------------------------------------------------------------
191 // Copy constructor
192 //------------------------------------------------------------------
193 FileSpec::FileSpec(const FileSpec *rhs) : m_directory(), m_filename() {
194   if (rhs)
195     *this = *rhs;
196 }
197 
198 //------------------------------------------------------------------
199 // Virtual destructor in case anyone inherits from this class.
200 //------------------------------------------------------------------
201 FileSpec::~FileSpec() {}
202 
203 //------------------------------------------------------------------
204 // Assignment operator.
205 //------------------------------------------------------------------
206 const FileSpec &FileSpec::operator=(const FileSpec &rhs) {
207   if (this != &rhs) {
208     m_directory = rhs.m_directory;
209     m_filename = rhs.m_filename;
210     m_is_resolved = rhs.m_is_resolved;
211     m_syntax = rhs.m_syntax;
212   }
213   return *this;
214 }
215 
216 //------------------------------------------------------------------
217 // Update the contents of this object with a new path. The path will
218 // be split up into a directory and filename and stored as uniqued
219 // string values for quick comparison and efficient memory usage.
220 //------------------------------------------------------------------
221 void FileSpec::SetFile(llvm::StringRef pathname, bool resolve,
222                        PathSyntax syntax) {
223   // CLEANUP: Use StringRef for string handling.  This function is kind of a
224   // mess and the unclear semantics of RootDirStart and ParentPathEnd make
225   // it very difficult to understand this function.  There's no reason this
226   // function should be particularly complicated or difficult to understand.
227   m_filename.Clear();
228   m_directory.Clear();
229   m_is_resolved = false;
230   m_syntax = (syntax == ePathSyntaxHostNative) ? GetNativeSyntax() : syntax;
231 
232   if (pathname.empty())
233     return;
234 
235   llvm::SmallString<64> resolved(pathname);
236 
237   if (resolve) {
238     FileSpec::Resolve(resolved);
239     m_is_resolved = true;
240   }
241 
242   Normalize(resolved, m_syntax);
243 
244   llvm::StringRef resolve_path_ref(resolved.c_str());
245   size_t dir_end = ParentPathEnd(resolve_path_ref, m_syntax);
246   if (dir_end == 0) {
247     m_filename.SetString(resolve_path_ref);
248     return;
249   }
250 
251   m_directory.SetString(resolve_path_ref.substr(0, dir_end));
252 
253   size_t filename_begin = dir_end;
254   size_t root_dir_start = RootDirStart(resolve_path_ref, m_syntax);
255   while (filename_begin != llvm::StringRef::npos &&
256          filename_begin < resolve_path_ref.size() &&
257          filename_begin != root_dir_start &&
258          IsPathSeparator(resolve_path_ref[filename_begin], m_syntax))
259     ++filename_begin;
260   m_filename.SetString((filename_begin == llvm::StringRef::npos ||
261                         filename_begin >= resolve_path_ref.size())
262                            ? "."
263                            : resolve_path_ref.substr(filename_begin));
264 }
265 
266 void FileSpec::SetFile(llvm::StringRef path, bool resolve,
267                        const llvm::Triple &Triple) {
268   return SetFile(path, resolve,
269                  Triple.isOSWindows() ? ePathSyntaxWindows : ePathSyntaxPosix);
270 }
271 
272 //----------------------------------------------------------------------
273 // Convert to pointer operator. This allows code to check any FileSpec
274 // objects to see if they contain anything valid using code such as:
275 //
276 //  if (file_spec)
277 //  {}
278 //----------------------------------------------------------------------
279 FileSpec::operator bool() const { return m_filename || m_directory; }
280 
281 //----------------------------------------------------------------------
282 // Logical NOT operator. This allows code to check any FileSpec
283 // objects to see if they are invalid using code such as:
284 //
285 //  if (!file_spec)
286 //  {}
287 //----------------------------------------------------------------------
288 bool FileSpec::operator!() const { return !m_directory && !m_filename; }
289 
290 bool FileSpec::DirectoryEquals(const FileSpec &rhs) const {
291   const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
292   return ConstString::Equals(m_directory, rhs.m_directory, case_sensitive);
293 }
294 
295 bool FileSpec::FileEquals(const FileSpec &rhs) const {
296   const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
297   return ConstString::Equals(m_filename, rhs.m_filename, case_sensitive);
298 }
299 
300 //------------------------------------------------------------------
301 // Equal to operator
302 //------------------------------------------------------------------
303 bool FileSpec::operator==(const FileSpec &rhs) const {
304   if (!FileEquals(rhs))
305     return false;
306   if (DirectoryEquals(rhs))
307     return true;
308 
309   // TODO: determine if we want to keep this code in here.
310   // The code below was added to handle a case where we were
311   // trying to set a file and line breakpoint and one path
312   // was resolved, and the other not and the directory was
313   // in a mount point that resolved to a more complete path:
314   // "/tmp/a.c" == "/private/tmp/a.c". I might end up pulling
315   // this out...
316   if (IsResolved() && rhs.IsResolved()) {
317     // Both paths are resolved, no need to look further...
318     return false;
319   }
320 
321   FileSpec resolved_lhs(*this);
322 
323   // If "this" isn't resolved, resolve it
324   if (!IsResolved()) {
325     if (resolved_lhs.ResolvePath()) {
326       // This path wasn't resolved but now it is. Check if the resolved
327       // directory is the same as our unresolved directory, and if so,
328       // we can mark this object as resolved to avoid more future resolves
329       m_is_resolved = (m_directory == resolved_lhs.m_directory);
330     } else
331       return false;
332   }
333 
334   FileSpec resolved_rhs(rhs);
335   if (!rhs.IsResolved()) {
336     if (resolved_rhs.ResolvePath()) {
337       // rhs's path wasn't resolved but now it is. Check if the resolved
338       // directory is the same as rhs's unresolved directory, and if so,
339       // we can mark this object as resolved to avoid more future resolves
340       rhs.m_is_resolved = (rhs.m_directory == resolved_rhs.m_directory);
341     } else
342       return false;
343   }
344 
345   // If we reach this point in the code we were able to resolve both paths
346   // and since we only resolve the paths if the basenames are equal, then
347   // we can just check if both directories are equal...
348   return DirectoryEquals(rhs);
349 }
350 
351 //------------------------------------------------------------------
352 // Not equal to operator
353 //------------------------------------------------------------------
354 bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); }
355 
356 //------------------------------------------------------------------
357 // Less than operator
358 //------------------------------------------------------------------
359 bool FileSpec::operator<(const FileSpec &rhs) const {
360   return FileSpec::Compare(*this, rhs, true) < 0;
361 }
362 
363 //------------------------------------------------------------------
364 // Dump a FileSpec object to a stream
365 //------------------------------------------------------------------
366 Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) {
367   f.Dump(&s);
368   return s;
369 }
370 
371 //------------------------------------------------------------------
372 // Clear this object by releasing both the directory and filename
373 // string values and making them both the empty string.
374 //------------------------------------------------------------------
375 void FileSpec::Clear() {
376   m_directory.Clear();
377   m_filename.Clear();
378 }
379 
380 //------------------------------------------------------------------
381 // Compare two FileSpec objects. If "full" is true, then both
382 // the directory and the filename must match. If "full" is false,
383 // then the directory names for "a" and "b" are only compared if
384 // they are both non-empty. This allows a FileSpec object to only
385 // contain a filename and it can match FileSpec objects that have
386 // matching filenames with different paths.
387 //
388 // Return -1 if the "a" is less than "b", 0 if "a" is equal to "b"
389 // and "1" if "a" is greater than "b".
390 //------------------------------------------------------------------
391 int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) {
392   int result = 0;
393 
394   // case sensitivity of compare
395   const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
396 
397   // If full is true, then we must compare both the directory and filename.
398 
399   // If full is false, then if either directory is empty, then we match on
400   // the basename only, and if both directories have valid values, we still
401   // do a full compare. This allows for matching when we just have a filename
402   // in one of the FileSpec objects.
403 
404   if (full || (a.m_directory && b.m_directory)) {
405     result = ConstString::Compare(a.m_directory, b.m_directory, case_sensitive);
406     if (result)
407       return result;
408   }
409   return ConstString::Compare(a.m_filename, b.m_filename, case_sensitive);
410 }
411 
412 bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full,
413                      bool remove_backups) {
414   static ConstString g_dot_string(".");
415   static ConstString g_dot_dot_string("..");
416 
417   // case sensitivity of equality test
418   const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
419 
420   bool filenames_equal = ConstString::Equals(a.m_filename,
421                                              b.m_filename,
422                                              case_sensitive);
423 
424   // The only way two FileSpecs can be equal if their filenames are
425   // unequal is if we are removing backups and one or the other filename
426   // is a backup string:
427 
428   if (!filenames_equal && !remove_backups)
429       return false;
430 
431   bool last_component_is_dot = ConstString::Equals(a.m_filename, g_dot_string)
432                                || ConstString::Equals(a.m_filename,
433                                                       g_dot_dot_string)
434                                || ConstString::Equals(b.m_filename,
435                                                       g_dot_string)
436                                || ConstString::Equals(b.m_filename,
437                                                       g_dot_dot_string);
438 
439   if (!filenames_equal && !last_component_is_dot)
440     return false;
441 
442   if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty()))
443     return filenames_equal;
444 
445   if (remove_backups == false)
446     return a == b;
447 
448   if (a == b)
449     return true;
450 
451   return Equal(a.GetNormalizedPath(), b.GetNormalizedPath(), full, false);
452 }
453 
454 FileSpec FileSpec::GetNormalizedPath() const {
455   // Fast path. Do nothing if the path is not interesting.
456   if (!m_directory.GetStringRef().contains(".") &&
457       !m_directory.GetStringRef().contains("//") &&
458       m_filename.GetStringRef() != ".." && m_filename.GetStringRef() != ".")
459     return *this;
460 
461   llvm::SmallString<64> path, result;
462   const bool normalize = false;
463   GetPath(path, normalize);
464   llvm::StringRef rest(path);
465 
466   // We will not go below root dir.
467   size_t root_dir_start = RootDirStart(path, m_syntax);
468   const bool absolute = root_dir_start != llvm::StringRef::npos;
469   if (absolute) {
470     result += rest.take_front(root_dir_start + 1);
471     rest = rest.drop_front(root_dir_start + 1);
472   } else {
473     if (m_syntax == ePathSyntaxWindows && path.size() > 2 && path[1] == ':') {
474       result += rest.take_front(2);
475       rest = rest.drop_front(2);
476     }
477   }
478 
479   bool anything_added = false;
480   llvm::SmallVector<llvm::StringRef, 0> components, processed;
481   rest.split(components, '/', -1, false);
482   processed.reserve(components.size());
483   for (auto component : components) {
484     if (component == ".")
485       continue; // Skip these.
486     if (component != "..") {
487       processed.push_back(component);
488       continue; // Regular file name.
489     }
490     if (!processed.empty()) {
491       processed.pop_back();
492       continue; // Dots. Go one level up if we can.
493     }
494     if (absolute)
495       continue; // We're at the top level. Cannot go higher than that. Skip.
496 
497     result += component; // We're a relative path. We need to keep these.
498     result += '/';
499     anything_added = true;
500   }
501   for (auto component : processed) {
502     result += component;
503     result += '/';
504     anything_added = true;
505   }
506   if (anything_added)
507     result.pop_back(); // Pop last '/'.
508   else if (result.empty())
509     result = ".";
510 
511   return FileSpec(result, false, m_syntax);
512 }
513 
514 //------------------------------------------------------------------
515 // Dump the object to the supplied stream. If the object contains
516 // a valid directory name, it will be displayed followed by a
517 // directory delimiter, and the filename.
518 //------------------------------------------------------------------
519 void FileSpec::Dump(Stream *s) const {
520   if (s) {
521     std::string path{GetPath(true)};
522     s->PutCString(path);
523     char path_separator = GetPreferredPathSeparator(m_syntax);
524     if (!m_filename && !path.empty() && path.back() != path_separator)
525       s->PutChar(path_separator);
526   }
527 }
528 
529 //------------------------------------------------------------------
530 // Returns true if the file exists.
531 //------------------------------------------------------------------
532 bool FileSpec::Exists() const { return llvm::sys::fs::exists(GetPath()); }
533 
534 bool FileSpec::Readable() const {
535   return GetPermissions() & llvm::sys::fs::perms::all_read;
536 }
537 
538 bool FileSpec::ResolveExecutableLocation() {
539   // CLEANUP: Use StringRef for string handling.
540   if (!m_directory) {
541     const char *file_cstr = m_filename.GetCString();
542     if (file_cstr) {
543       const std::string file_str(file_cstr);
544       llvm::ErrorOr<std::string> error_or_path =
545           llvm::sys::findProgramByName(file_str);
546       if (!error_or_path)
547         return false;
548       std::string path = error_or_path.get();
549       llvm::StringRef dir_ref = llvm::sys::path::parent_path(path);
550       if (!dir_ref.empty()) {
551         // FindProgramByName returns "." if it can't find the file.
552         if (strcmp(".", dir_ref.data()) == 0)
553           return false;
554 
555         m_directory.SetCString(dir_ref.data());
556         if (Exists())
557           return true;
558         else {
559           // If FindProgramByName found the file, it returns the directory +
560           // filename in its return results.
561           // We need to separate them.
562           FileSpec tmp_file(dir_ref.data(), false);
563           if (tmp_file.Exists()) {
564             m_directory = tmp_file.m_directory;
565             return true;
566           }
567         }
568       }
569     }
570   }
571 
572   return false;
573 }
574 
575 bool FileSpec::ResolvePath() {
576   if (m_is_resolved)
577     return true; // We have already resolved this path
578 
579   // SetFile(...) will set m_is_resolved correctly if it can resolve the path
580   SetFile(GetPath(false), true);
581   return m_is_resolved;
582 }
583 
584 uint64_t FileSpec::GetByteSize() const {
585   uint64_t Size = 0;
586   if (llvm::sys::fs::file_size(GetPath(), Size))
587     return 0;
588   return Size;
589 }
590 
591 FileSpec::PathSyntax FileSpec::GetPathSyntax() const { return m_syntax; }
592 
593 uint32_t FileSpec::GetPermissions() const {
594   namespace fs = llvm::sys::fs;
595   fs::file_status st;
596   if (fs::status(GetPath(), st, false))
597     return fs::perms::perms_not_known;
598 
599   return st.permissions();
600 }
601 
602 //------------------------------------------------------------------
603 // Directory string get accessor.
604 //------------------------------------------------------------------
605 ConstString &FileSpec::GetDirectory() { return m_directory; }
606 
607 //------------------------------------------------------------------
608 // Directory string const get accessor.
609 //------------------------------------------------------------------
610 const ConstString &FileSpec::GetDirectory() const { return m_directory; }
611 
612 //------------------------------------------------------------------
613 // Filename string get accessor.
614 //------------------------------------------------------------------
615 ConstString &FileSpec::GetFilename() { return m_filename; }
616 
617 //------------------------------------------------------------------
618 // Filename string const get accessor.
619 //------------------------------------------------------------------
620 const ConstString &FileSpec::GetFilename() const { return m_filename; }
621 
622 //------------------------------------------------------------------
623 // Extract the directory and path into a fixed buffer. This is
624 // needed as the directory and path are stored in separate string
625 // values.
626 //------------------------------------------------------------------
627 size_t FileSpec::GetPath(char *path, size_t path_max_len,
628                          bool denormalize) const {
629   if (!path)
630     return 0;
631 
632   std::string result = GetPath(denormalize);
633   ::snprintf(path, path_max_len, "%s", result.c_str());
634   return std::min(path_max_len - 1, result.length());
635 }
636 
637 std::string FileSpec::GetPath(bool denormalize) const {
638   llvm::SmallString<64> result;
639   GetPath(result, denormalize);
640   return std::string(result.begin(), result.end());
641 }
642 
643 const char *FileSpec::GetCString(bool denormalize) const {
644   return ConstString{GetPath(denormalize)}.AsCString(NULL);
645 }
646 
647 void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path,
648                        bool denormalize) const {
649   path.append(m_directory.GetStringRef().begin(),
650               m_directory.GetStringRef().end());
651   if (m_directory && m_filename &&
652       !IsPathSeparator(m_directory.GetStringRef().back(), m_syntax))
653     path.insert(path.end(), GetPreferredPathSeparator(m_syntax));
654   path.append(m_filename.GetStringRef().begin(),
655               m_filename.GetStringRef().end());
656   Normalize(path, m_syntax);
657   if (denormalize && !path.empty())
658     Denormalize(path, m_syntax);
659 }
660 
661 ConstString FileSpec::GetFileNameExtension() const {
662   if (m_filename) {
663     const char *filename = m_filename.GetCString();
664     const char *dot_pos = strrchr(filename, '.');
665     if (dot_pos && dot_pos[1] != '\0')
666       return ConstString(dot_pos + 1);
667   }
668   return ConstString();
669 }
670 
671 ConstString FileSpec::GetFileNameStrippingExtension() const {
672   const char *filename = m_filename.GetCString();
673   if (filename == NULL)
674     return ConstString();
675 
676   const char *dot_pos = strrchr(filename, '.');
677   if (dot_pos == NULL)
678     return m_filename;
679 
680   return ConstString(filename, dot_pos - filename);
681 }
682 
683 //------------------------------------------------------------------
684 // Return the size in bytes that this object takes in memory. This
685 // returns the size in bytes of this object, not any shared string
686 // values it may refer to.
687 //------------------------------------------------------------------
688 size_t FileSpec::MemorySize() const {
689   return m_filename.MemorySize() + m_directory.MemorySize();
690 }
691 
692 void FileSpec::EnumerateDirectory(llvm::StringRef dir_path,
693                                   bool find_directories, bool find_files,
694                                   bool find_other,
695                                   EnumerateDirectoryCallbackType callback,
696                                   void *callback_baton) {
697   namespace fs = llvm::sys::fs;
698   std::error_code EC;
699   fs::recursive_directory_iterator Iter(dir_path, EC);
700   fs::recursive_directory_iterator End;
701   for (; Iter != End && !EC; Iter.increment(EC)) {
702     const auto &Item = *Iter;
703     llvm::ErrorOr<fs::basic_file_status> Status = Item.status();
704     if (!Status)
705       break;
706     if (!find_files && fs::is_regular_file(*Status))
707       continue;
708     if (!find_directories && fs::is_directory(*Status))
709       continue;
710     if (!find_other && fs::is_other(*Status))
711       continue;
712 
713     FileSpec Spec(Item.path(), false);
714     auto Result = callback(callback_baton, Status->type(), Spec);
715     if (Result == eEnumerateDirectoryResultQuit)
716       return;
717     if (Result == eEnumerateDirectoryResultNext) {
718       // Default behavior is to recurse.  Opt out if the callback doesn't want
719       // this behavior.
720       Iter.no_push();
721     }
722   }
723 }
724 
725 FileSpec
726 FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const {
727   FileSpec ret = *this;
728   ret.AppendPathComponent(component);
729   return ret;
730 }
731 
732 FileSpec FileSpec::CopyByRemovingLastPathComponent() const {
733   // CLEANUP: Use StringRef for string handling.
734   const bool resolve = false;
735   if (m_filename.IsEmpty() && m_directory.IsEmpty())
736     return FileSpec("", resolve);
737   if (m_directory.IsEmpty())
738     return FileSpec("", resolve);
739   if (m_filename.IsEmpty()) {
740     const char *dir_cstr = m_directory.GetCString();
741     const char *last_slash_ptr = ::strrchr(dir_cstr, '/');
742 
743     // check for obvious cases before doing the full thing
744     if (!last_slash_ptr)
745       return FileSpec("", resolve);
746     if (last_slash_ptr == dir_cstr)
747       return FileSpec("/", resolve);
748 
749     size_t last_slash_pos = last_slash_ptr - dir_cstr + 1;
750     ConstString new_path(dir_cstr, last_slash_pos);
751     return FileSpec(new_path.GetCString(), resolve);
752   } else
753     return FileSpec(m_directory.GetCString(), resolve);
754 }
755 
756 ConstString FileSpec::GetLastPathComponent() const {
757   // CLEANUP: Use StringRef for string handling.
758   if (m_filename)
759     return m_filename;
760   if (m_directory) {
761     const char *dir_cstr = m_directory.GetCString();
762     const char *last_slash_ptr = ::strrchr(dir_cstr, '/');
763     if (last_slash_ptr == NULL)
764       return m_directory;
765     if (last_slash_ptr == dir_cstr) {
766       if (last_slash_ptr[1] == 0)
767         return ConstString(last_slash_ptr);
768       else
769         return ConstString(last_slash_ptr + 1);
770     }
771     if (last_slash_ptr[1] != 0)
772       return ConstString(last_slash_ptr + 1);
773     const char *penultimate_slash_ptr = last_slash_ptr;
774     while (*penultimate_slash_ptr) {
775       --penultimate_slash_ptr;
776       if (penultimate_slash_ptr == dir_cstr)
777         break;
778       if (*penultimate_slash_ptr == '/')
779         break;
780     }
781     ConstString result(penultimate_slash_ptr + 1,
782                        last_slash_ptr - penultimate_slash_ptr);
783     return result;
784   }
785   return ConstString();
786 }
787 
788 static std::string
789 join_path_components(FileSpec::PathSyntax syntax,
790                      const std::vector<llvm::StringRef> components) {
791   std::string result;
792   for (size_t i = 0; i < components.size(); ++i) {
793     if (components[i].empty())
794       continue;
795     result += components[i];
796     if (i != components.size() - 1 &&
797         !IsPathSeparator(components[i].back(), syntax))
798       result += GetPreferredPathSeparator(syntax);
799   }
800 
801   return result;
802 }
803 
804 void FileSpec::PrependPathComponent(llvm::StringRef component) {
805   if (component.empty())
806     return;
807 
808   const bool resolve = false;
809   if (m_filename.IsEmpty() && m_directory.IsEmpty()) {
810     SetFile(component, resolve);
811     return;
812   }
813 
814   std::string result =
815       join_path_components(m_syntax, {component, m_directory.GetStringRef(),
816                                       m_filename.GetStringRef()});
817   SetFile(result, resolve, m_syntax);
818 }
819 
820 void FileSpec::PrependPathComponent(const FileSpec &new_path) {
821   return PrependPathComponent(new_path.GetPath(false));
822 }
823 
824 void FileSpec::AppendPathComponent(llvm::StringRef component) {
825   if (component.empty())
826     return;
827 
828   component = component.drop_while(
829       [this](char c) { return IsPathSeparator(c, m_syntax); });
830 
831   std::string result =
832       join_path_components(m_syntax, {m_directory.GetStringRef(),
833                                       m_filename.GetStringRef(), component});
834 
835   SetFile(result, false, m_syntax);
836 }
837 
838 void FileSpec::AppendPathComponent(const FileSpec &new_path) {
839   return AppendPathComponent(new_path.GetPath(false));
840 }
841 
842 void FileSpec::RemoveLastPathComponent() {
843   // CLEANUP: Use StringRef for string handling.
844 
845   const bool resolve = false;
846   if (m_filename.IsEmpty() && m_directory.IsEmpty()) {
847     SetFile("", resolve);
848     return;
849   }
850   if (m_directory.IsEmpty()) {
851     SetFile("", resolve);
852     return;
853   }
854   if (m_filename.IsEmpty()) {
855     const char *dir_cstr = m_directory.GetCString();
856     const char *last_slash_ptr = ::strrchr(dir_cstr, '/');
857 
858     // check for obvious cases before doing the full thing
859     if (!last_slash_ptr) {
860       SetFile("", resolve);
861       return;
862     }
863     if (last_slash_ptr == dir_cstr) {
864       SetFile("/", resolve);
865       return;
866     }
867     size_t last_slash_pos = last_slash_ptr - dir_cstr + 1;
868     ConstString new_path(dir_cstr, last_slash_pos);
869     SetFile(new_path.GetCString(), resolve);
870   } else
871     SetFile(m_directory.GetCString(), resolve);
872 }
873 //------------------------------------------------------------------
874 /// Returns true if the filespec represents an implementation source
875 /// file (files with a ".c", ".cpp", ".m", ".mm" (many more)
876 /// extension).
877 ///
878 /// @return
879 ///     \b true if the filespec represents an implementation source
880 ///     file, \b false otherwise.
881 //------------------------------------------------------------------
882 bool FileSpec::IsSourceImplementationFile() const {
883   ConstString extension(GetFileNameExtension());
884   if (!extension)
885     return false;
886 
887   static RegularExpression g_source_file_regex(llvm::StringRef(
888       "^([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|["
889       "cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO]["
890       "rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])"
891       "$"));
892   return g_source_file_regex.Execute(extension.GetStringRef());
893 }
894 
895 bool FileSpec::IsRelative() const {
896   const char *dir = m_directory.GetCString();
897   llvm::StringRef directory(dir ? dir : "");
898 
899   if (directory.size() > 0) {
900     if (PathSyntaxIsPosix(m_syntax)) {
901       // If the path doesn't start with '/' or '~', return true
902       switch (directory[0]) {
903       case '/':
904       case '~':
905         return false;
906       default:
907         return true;
908       }
909     } else {
910       if (directory.size() >= 2 && directory[1] == ':')
911         return false;
912       if (directory[0] == '/')
913         return false;
914       return true;
915     }
916   } else if (m_filename) {
917     // No directory, just a basename, return true
918     return true;
919   }
920   return false;
921 }
922 
923 bool FileSpec::IsAbsolute() const { return !FileSpec::IsRelative(); }
924 
925 void llvm::format_provider<FileSpec>::format(const FileSpec &F,
926                                              raw_ostream &Stream,
927                                              StringRef Style) {
928   assert(
929       (Style.empty() || Style.equals_lower("F") || Style.equals_lower("D")) &&
930       "Invalid FileSpec style!");
931 
932   StringRef dir = F.GetDirectory().GetStringRef();
933   StringRef file = F.GetFilename().GetStringRef();
934 
935   if (dir.empty() && file.empty()) {
936     Stream << "(empty)";
937     return;
938   }
939 
940   if (Style.equals_lower("F")) {
941     Stream << (file.empty() ? "(empty)" : file);
942     return;
943   }
944 
945   // Style is either D or empty, either way we need to print the directory.
946   if (!dir.empty()) {
947     // Directory is stored in normalized form, which might be different
948     // than preferred form.  In order to handle this, we need to cut off
949     // the filename, then denormalize, then write the entire denorm'ed
950     // directory.
951     llvm::SmallString<64> denormalized_dir = dir;
952     Denormalize(denormalized_dir, F.GetPathSyntax());
953     Stream << denormalized_dir;
954     Stream << GetPreferredPathSeparator(F.GetPathSyntax());
955   }
956 
957   if (Style.equals_lower("D")) {
958     // We only want to print the directory, so now just exit.
959     if (dir.empty())
960       Stream << "(empty)";
961     return;
962   }
963 
964   if (!file.empty())
965     Stream << file;
966 }
967