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