15713a05bSZachary Turner //===-- FileSpec.cpp --------------------------------------------*- C++ -*-===//
25713a05bSZachary Turner //
35713a05bSZachary Turner //                     The LLVM Compiler Infrastructure
45713a05bSZachary Turner //
55713a05bSZachary Turner // This file is distributed under the University of Illinois Open Source
65713a05bSZachary Turner // License. See LICENSE.TXT for details.
75713a05bSZachary Turner //
85713a05bSZachary Turner //===----------------------------------------------------------------------===//
95713a05bSZachary Turner 
105713a05bSZachary Turner #include "lldb/Utility/FileSpec.h"
115713a05bSZachary Turner #include "lldb/Utility/RegularExpression.h"
125713a05bSZachary Turner #include "lldb/Utility/Stream.h"
135713a05bSZachary Turner 
14ad8d48f9SJonas Devlieghere #include "llvm/ADT/SmallString.h"
15ad8d48f9SJonas Devlieghere #include "llvm/ADT/SmallVector.h"
165713a05bSZachary Turner #include "llvm/ADT/StringRef.h"
17ad8d48f9SJonas Devlieghere #include "llvm/ADT/Triple.h"
18ad8d48f9SJonas Devlieghere #include "llvm/ADT/Twine.h"
19ad8d48f9SJonas Devlieghere #include "llvm/Support/ErrorOr.h"
205713a05bSZachary Turner #include "llvm/Support/FileSystem.h"
215713a05bSZachary Turner #include "llvm/Support/Program.h"
22ad8d48f9SJonas Devlieghere #include "llvm/Support/raw_ostream.h"
234479ac15SZachary Turner 
24672d2c12SJonas Devlieghere #include <algorithm>
25672d2c12SJonas Devlieghere #include <system_error>
26672d2c12SJonas Devlieghere #include <vector>
274479ac15SZachary Turner 
28672d2c12SJonas Devlieghere #include <assert.h>
29672d2c12SJonas Devlieghere #include <limits.h>
30672d2c12SJonas Devlieghere #include <stdio.h>
31672d2c12SJonas Devlieghere #include <string.h>
325713a05bSZachary Turner 
335713a05bSZachary Turner using namespace lldb;
345713a05bSZachary Turner using namespace lldb_private;
355713a05bSZachary Turner 
365713a05bSZachary Turner namespace {
375713a05bSZachary Turner 
382cb7cf8eSPavel Labath static constexpr FileSpec::Style GetNativeStyle() {
39b1cb0b79SNico Weber #if defined(_WIN32)
402cb7cf8eSPavel Labath   return FileSpec::Style::windows;
415713a05bSZachary Turner #else
422cb7cf8eSPavel Labath   return FileSpec::Style::posix;
435713a05bSZachary Turner #endif
445713a05bSZachary Turner }
455713a05bSZachary Turner 
462cb7cf8eSPavel Labath bool PathStyleIsPosix(FileSpec::Style style) {
472cb7cf8eSPavel Labath   return (style == FileSpec::Style::posix ||
482cb7cf8eSPavel Labath           (style == FileSpec::Style::native &&
492cb7cf8eSPavel Labath            GetNativeStyle() == FileSpec::Style::posix));
505713a05bSZachary Turner }
515713a05bSZachary Turner 
522cb7cf8eSPavel Labath const char *GetPathSeparators(FileSpec::Style style) {
53ad8d48f9SJonas Devlieghere   return llvm::sys::path::get_separator(style).data();
545713a05bSZachary Turner }
555713a05bSZachary Turner 
562cb7cf8eSPavel Labath char GetPreferredPathSeparator(FileSpec::Style style) {
572cb7cf8eSPavel Labath   return GetPathSeparators(style)[0];
585713a05bSZachary Turner }
595713a05bSZachary Turner 
602cb7cf8eSPavel Labath void Denormalize(llvm::SmallVectorImpl<char> &path, FileSpec::Style style) {
612cb7cf8eSPavel Labath   if (PathStyleIsPosix(style))
625713a05bSZachary Turner     return;
635713a05bSZachary Turner 
645713a05bSZachary Turner   std::replace(path.begin(), path.end(), '/', '\\');
655713a05bSZachary Turner }
6686188d8aSGreg Clayton 
675713a05bSZachary Turner } // end anonymous namespace
685713a05bSZachary Turner 
692cb7cf8eSPavel Labath FileSpec::FileSpec() : m_style(GetNativeStyle()) {}
705713a05bSZachary Turner 
715713a05bSZachary Turner //------------------------------------------------------------------
7205097246SAdrian Prantl // Default constructor that can take an optional full path to a file on disk.
735713a05bSZachary Turner //------------------------------------------------------------------
748f3be7a3SJonas Devlieghere FileSpec::FileSpec(llvm::StringRef path, Style style) : m_style(style) {
758f3be7a3SJonas Devlieghere   SetFile(path, style);
765713a05bSZachary Turner }
775713a05bSZachary Turner 
788f3be7a3SJonas Devlieghere FileSpec::FileSpec(llvm::StringRef path, const llvm::Triple &Triple)
798f3be7a3SJonas Devlieghere     : FileSpec{path, Triple.isOSWindows() ? Style::windows : Style::posix} {}
805713a05bSZachary Turner 
815713a05bSZachary Turner //------------------------------------------------------------------
825713a05bSZachary Turner // Copy constructor
835713a05bSZachary Turner //------------------------------------------------------------------
845713a05bSZachary Turner FileSpec::FileSpec(const FileSpec &rhs)
855713a05bSZachary Turner     : m_directory(rhs.m_directory), m_filename(rhs.m_filename),
862cb7cf8eSPavel Labath       m_is_resolved(rhs.m_is_resolved), m_style(rhs.m_style) {}
875713a05bSZachary Turner 
885713a05bSZachary Turner //------------------------------------------------------------------
895713a05bSZachary Turner // Copy constructor
905713a05bSZachary Turner //------------------------------------------------------------------
915713a05bSZachary Turner FileSpec::FileSpec(const FileSpec *rhs) : m_directory(), m_filename() {
925713a05bSZachary Turner   if (rhs)
935713a05bSZachary Turner     *this = *rhs;
945713a05bSZachary Turner }
955713a05bSZachary Turner 
965713a05bSZachary Turner //------------------------------------------------------------------
975713a05bSZachary Turner // Virtual destructor in case anyone inherits from this class.
985713a05bSZachary Turner //------------------------------------------------------------------
995713a05bSZachary Turner FileSpec::~FileSpec() {}
1005713a05bSZachary Turner 
101c1cc3173SJonas Devlieghere namespace {
102c1cc3173SJonas Devlieghere //------------------------------------------------------------------
103c1cc3173SJonas Devlieghere /// Safely get a character at the specified index.
104c1cc3173SJonas Devlieghere ///
105c1cc3173SJonas Devlieghere /// @param[in] path
106c1cc3173SJonas Devlieghere ///     A full, partial, or relative path to a file.
107c1cc3173SJonas Devlieghere ///
108c1cc3173SJonas Devlieghere /// @param[in] i
109c1cc3173SJonas Devlieghere ///     An index into path which may or may not be valid.
110c1cc3173SJonas Devlieghere ///
111c1cc3173SJonas Devlieghere /// @return
112c1cc3173SJonas Devlieghere ///   The character at index \a i if the index is valid, or 0 if
113c1cc3173SJonas Devlieghere ///   the index is not valid.
114c1cc3173SJonas Devlieghere //------------------------------------------------------------------
115c1cc3173SJonas Devlieghere inline char safeCharAtIndex(const llvm::StringRef &path, size_t i) {
116c1cc3173SJonas Devlieghere   if (i < path.size())
117c1cc3173SJonas Devlieghere     return path[i];
118c1cc3173SJonas Devlieghere   return 0;
119c1cc3173SJonas Devlieghere }
120c1cc3173SJonas Devlieghere 
121c1cc3173SJonas Devlieghere //------------------------------------------------------------------
122c1cc3173SJonas Devlieghere /// Check if a path needs to be normalized.
123c1cc3173SJonas Devlieghere ///
124c1cc3173SJonas Devlieghere /// Check if a path needs to be normalized. We currently consider a
125c1cc3173SJonas Devlieghere /// path to need normalization if any of the following are true
126c1cc3173SJonas Devlieghere ///  - path contains "/./"
127c1cc3173SJonas Devlieghere ///  - path contains "/../"
128c1cc3173SJonas Devlieghere ///  - path contains "//"
129c1cc3173SJonas Devlieghere ///  - path ends with "/"
130c1cc3173SJonas Devlieghere /// Paths that start with "./" or with "../" are not considered to
131c1cc3173SJonas Devlieghere /// need normalization since we aren't trying to resolve the path,
132c1cc3173SJonas Devlieghere /// we are just trying to remove redundant things from the path.
133c1cc3173SJonas Devlieghere ///
134c1cc3173SJonas Devlieghere /// @param[in] path
135c1cc3173SJonas Devlieghere ///     A full, partial, or relative path to a file.
136c1cc3173SJonas Devlieghere ///
137c1cc3173SJonas Devlieghere /// @return
138c1cc3173SJonas Devlieghere ///   Returns \b true if the path needs to be normalized.
139c1cc3173SJonas Devlieghere //------------------------------------------------------------------
140c1cc3173SJonas Devlieghere bool needsNormalization(const llvm::StringRef &path) {
141c1cc3173SJonas Devlieghere   if (path.empty())
142c1cc3173SJonas Devlieghere     return false;
143c1cc3173SJonas Devlieghere   // We strip off leading "." values so these paths need to be normalized
144c1cc3173SJonas Devlieghere   if (path[0] == '.')
145c1cc3173SJonas Devlieghere     return true;
146c1cc3173SJonas Devlieghere   for (auto i = path.find_first_of("\\/"); i != llvm::StringRef::npos;
147c1cc3173SJonas Devlieghere        i = path.find_first_of("\\/", i + 1)) {
148c1cc3173SJonas Devlieghere     const auto next = safeCharAtIndex(path, i+1);
149c1cc3173SJonas Devlieghere     switch (next) {
150c1cc3173SJonas Devlieghere       case 0:
151c1cc3173SJonas Devlieghere         // path separator char at the end of the string which should be
152c1cc3173SJonas Devlieghere         // stripped unless it is the one and only character
153c1cc3173SJonas Devlieghere         return i > 0;
154c1cc3173SJonas Devlieghere       case '/':
155c1cc3173SJonas Devlieghere       case '\\':
156c1cc3173SJonas Devlieghere         // two path separator chars in the middle of a path needs to be
157c1cc3173SJonas Devlieghere         // normalized
158c1cc3173SJonas Devlieghere         if (i > 0)
159c1cc3173SJonas Devlieghere           return true;
160c1cc3173SJonas Devlieghere         ++i;
161c1cc3173SJonas Devlieghere         break;
162c1cc3173SJonas Devlieghere 
163c1cc3173SJonas Devlieghere       case '.': {
164c1cc3173SJonas Devlieghere           const auto next_next = safeCharAtIndex(path, i+2);
165c1cc3173SJonas Devlieghere           switch (next_next) {
166c1cc3173SJonas Devlieghere             default: break;
167c1cc3173SJonas Devlieghere             case 0: return true; // ends with "/."
168c1cc3173SJonas Devlieghere             case '/':
169c1cc3173SJonas Devlieghere             case '\\':
170c1cc3173SJonas Devlieghere               return true; // contains "/./"
171c1cc3173SJonas Devlieghere             case '.': {
172c1cc3173SJonas Devlieghere               const auto next_next_next = safeCharAtIndex(path, i+3);
173c1cc3173SJonas Devlieghere               switch (next_next_next) {
174c1cc3173SJonas Devlieghere                 default: break;
175c1cc3173SJonas Devlieghere                 case 0: return true; // ends with "/.."
176c1cc3173SJonas Devlieghere                 case '/':
177c1cc3173SJonas Devlieghere                 case '\\':
178c1cc3173SJonas Devlieghere                   return true; // contains "/../"
179c1cc3173SJonas Devlieghere               }
180c1cc3173SJonas Devlieghere               break;
181c1cc3173SJonas Devlieghere             }
182c1cc3173SJonas Devlieghere           }
183c1cc3173SJonas Devlieghere         }
184c1cc3173SJonas Devlieghere         break;
185c1cc3173SJonas Devlieghere 
186c1cc3173SJonas Devlieghere       default:
187c1cc3173SJonas Devlieghere         break;
188c1cc3173SJonas Devlieghere     }
189c1cc3173SJonas Devlieghere   }
190c1cc3173SJonas Devlieghere   return false;
191c1cc3173SJonas Devlieghere }
192c1cc3173SJonas Devlieghere 
193c1cc3173SJonas Devlieghere 
194c1cc3173SJonas Devlieghere }
1955713a05bSZachary Turner //------------------------------------------------------------------
1965713a05bSZachary Turner // Assignment operator.
1975713a05bSZachary Turner //------------------------------------------------------------------
1985713a05bSZachary Turner const FileSpec &FileSpec::operator=(const FileSpec &rhs) {
1995713a05bSZachary Turner   if (this != &rhs) {
2005713a05bSZachary Turner     m_directory = rhs.m_directory;
2015713a05bSZachary Turner     m_filename = rhs.m_filename;
2025713a05bSZachary Turner     m_is_resolved = rhs.m_is_resolved;
2032cb7cf8eSPavel Labath     m_style = rhs.m_style;
2045713a05bSZachary Turner   }
2055713a05bSZachary Turner   return *this;
2065713a05bSZachary Turner }
2075713a05bSZachary Turner 
2088f3be7a3SJonas Devlieghere void FileSpec::SetFile(llvm::StringRef pathname) { SetFile(pathname, m_style); }
209937348cdSJonas Devlieghere 
2105713a05bSZachary Turner //------------------------------------------------------------------
21105097246SAdrian Prantl // Update the contents of this object with a new path. The path will be split
21205097246SAdrian Prantl // up into a directory and filename and stored as uniqued string values for
21305097246SAdrian Prantl // quick comparison and efficient memory usage.
2145713a05bSZachary Turner //------------------------------------------------------------------
2158f3be7a3SJonas Devlieghere void FileSpec::SetFile(llvm::StringRef pathname, Style style) {
2165713a05bSZachary Turner   m_filename.Clear();
2175713a05bSZachary Turner   m_directory.Clear();
2185713a05bSZachary Turner   m_is_resolved = false;
2192cb7cf8eSPavel Labath   m_style = (style == Style::native) ? GetNativeStyle() : style;
2205713a05bSZachary Turner 
2215713a05bSZachary Turner   if (pathname.empty())
2225713a05bSZachary Turner     return;
2235713a05bSZachary Turner 
2248f3be7a3SJonas Devlieghere   llvm::SmallString<128> resolved(pathname);
2255713a05bSZachary Turner 
226776cd7adSGreg Clayton   // Normalize the path by removing ".", ".." and other redundant components.
227c1cc3173SJonas Devlieghere   if (needsNormalization(resolved))
2282cb7cf8eSPavel Labath     llvm::sys::path::remove_dots(resolved, true, m_style);
229776cd7adSGreg Clayton 
230776cd7adSGreg Clayton   // Normalize back slashes to forward slashes
2312cb7cf8eSPavel Labath   if (m_style == Style::windows)
232776cd7adSGreg Clayton     std::replace(resolved.begin(), resolved.end(), '\\', '/');
2335713a05bSZachary Turner 
23439d50b72SGreg Clayton   if (resolved.empty()) {
23539d50b72SGreg Clayton     // If we have no path after normalization set the path to the current
23639d50b72SGreg Clayton     // directory. This matches what python does and also a few other path
23739d50b72SGreg Clayton     // utilities.
23839d50b72SGreg Clayton     m_filename.SetString(".");
23939d50b72SGreg Clayton     return;
24039d50b72SGreg Clayton   }
24139d50b72SGreg Clayton 
242ad8d48f9SJonas Devlieghere   // Split path into filename and directory. We rely on the underlying char
243ad8d48f9SJonas Devlieghere   // pointer to be nullptr when the components are empty.
244ad8d48f9SJonas Devlieghere   llvm::StringRef filename = llvm::sys::path::filename(resolved, m_style);
245ad8d48f9SJonas Devlieghere   if(!filename.empty())
246ad8d48f9SJonas Devlieghere     m_filename.SetString(filename);
2478f3be7a3SJonas Devlieghere 
248ad8d48f9SJonas Devlieghere   llvm::StringRef directory = llvm::sys::path::parent_path(resolved, m_style);
249ad8d48f9SJonas Devlieghere   if(!directory.empty())
250ad8d48f9SJonas Devlieghere     m_directory.SetString(directory);
2515713a05bSZachary Turner }
2525713a05bSZachary Turner 
2538f3be7a3SJonas Devlieghere void FileSpec::SetFile(llvm::StringRef path, const llvm::Triple &Triple) {
2548f3be7a3SJonas Devlieghere   return SetFile(path, Triple.isOSWindows() ? Style::windows : Style::posix);
2555713a05bSZachary Turner }
2565713a05bSZachary Turner 
2575713a05bSZachary Turner //----------------------------------------------------------------------
25805097246SAdrian Prantl // Convert to pointer operator. This allows code to check any FileSpec objects
25905097246SAdrian Prantl // to see if they contain anything valid using code such as:
2605713a05bSZachary Turner //
2615713a05bSZachary Turner //  if (file_spec)
2625713a05bSZachary Turner //  {}
2635713a05bSZachary Turner //----------------------------------------------------------------------
2645713a05bSZachary Turner FileSpec::operator bool() const { return m_filename || m_directory; }
2655713a05bSZachary Turner 
2665713a05bSZachary Turner //----------------------------------------------------------------------
26705097246SAdrian Prantl // Logical NOT operator. This allows code to check any FileSpec objects to see
26805097246SAdrian Prantl // if they are invalid using code such as:
2695713a05bSZachary Turner //
2705713a05bSZachary Turner //  if (!file_spec)
2715713a05bSZachary Turner //  {}
2725713a05bSZachary Turner //----------------------------------------------------------------------
2735713a05bSZachary Turner bool FileSpec::operator!() const { return !m_directory && !m_filename; }
2745713a05bSZachary Turner 
2755713a05bSZachary Turner bool FileSpec::DirectoryEquals(const FileSpec &rhs) const {
2765713a05bSZachary Turner   const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
2775713a05bSZachary Turner   return ConstString::Equals(m_directory, rhs.m_directory, case_sensitive);
2785713a05bSZachary Turner }
2795713a05bSZachary Turner 
2805713a05bSZachary Turner bool FileSpec::FileEquals(const FileSpec &rhs) const {
2815713a05bSZachary Turner   const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
2825713a05bSZachary Turner   return ConstString::Equals(m_filename, rhs.m_filename, case_sensitive);
2835713a05bSZachary Turner }
2845713a05bSZachary Turner 
2855713a05bSZachary Turner //------------------------------------------------------------------
2865713a05bSZachary Turner // Equal to operator
2875713a05bSZachary Turner //------------------------------------------------------------------
2885713a05bSZachary Turner bool FileSpec::operator==(const FileSpec &rhs) const {
2898f3be7a3SJonas Devlieghere   return FileEquals(rhs) && DirectoryEquals(rhs);
2905713a05bSZachary Turner }
2915713a05bSZachary Turner 
2925713a05bSZachary Turner //------------------------------------------------------------------
2935713a05bSZachary Turner // Not equal to operator
2945713a05bSZachary Turner //------------------------------------------------------------------
2955713a05bSZachary Turner bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); }
2965713a05bSZachary Turner 
2975713a05bSZachary Turner //------------------------------------------------------------------
2985713a05bSZachary Turner // Less than operator
2995713a05bSZachary Turner //------------------------------------------------------------------
3005713a05bSZachary Turner bool FileSpec::operator<(const FileSpec &rhs) const {
3015713a05bSZachary Turner   return FileSpec::Compare(*this, rhs, true) < 0;
3025713a05bSZachary Turner }
3035713a05bSZachary Turner 
3045713a05bSZachary Turner //------------------------------------------------------------------
3055713a05bSZachary Turner // Dump a FileSpec object to a stream
3065713a05bSZachary Turner //------------------------------------------------------------------
3075713a05bSZachary Turner Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) {
3085713a05bSZachary Turner   f.Dump(&s);
3095713a05bSZachary Turner   return s;
3105713a05bSZachary Turner }
3115713a05bSZachary Turner 
3125713a05bSZachary Turner //------------------------------------------------------------------
31305097246SAdrian Prantl // Clear this object by releasing both the directory and filename string values
31405097246SAdrian Prantl // and making them both the empty string.
3155713a05bSZachary Turner //------------------------------------------------------------------
3165713a05bSZachary Turner void FileSpec::Clear() {
3175713a05bSZachary Turner   m_directory.Clear();
3185713a05bSZachary Turner   m_filename.Clear();
3195713a05bSZachary Turner }
3205713a05bSZachary Turner 
3215713a05bSZachary Turner //------------------------------------------------------------------
32205097246SAdrian Prantl // Compare two FileSpec objects. If "full" is true, then both the directory and
32305097246SAdrian Prantl // the filename must match. If "full" is false, then the directory names for
32405097246SAdrian Prantl // "a" and "b" are only compared if they are both non-empty. This allows a
32505097246SAdrian Prantl // FileSpec object to only contain a filename and it can match FileSpec objects
32605097246SAdrian Prantl // that have matching filenames with different paths.
3275713a05bSZachary Turner //
32805097246SAdrian Prantl // Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" and "1" if
32905097246SAdrian Prantl // "a" is greater than "b".
3305713a05bSZachary Turner //------------------------------------------------------------------
3315713a05bSZachary Turner int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) {
3325713a05bSZachary Turner   int result = 0;
3335713a05bSZachary Turner 
3345713a05bSZachary Turner   // case sensitivity of compare
3355713a05bSZachary Turner   const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
3365713a05bSZachary Turner 
3375713a05bSZachary Turner   // If full is true, then we must compare both the directory and filename.
3385713a05bSZachary Turner 
33905097246SAdrian Prantl   // If full is false, then if either directory is empty, then we match on the
34005097246SAdrian Prantl   // basename only, and if both directories have valid values, we still do a
34105097246SAdrian Prantl   // full compare. This allows for matching when we just have a filename in one
34205097246SAdrian Prantl   // of the FileSpec objects.
3435713a05bSZachary Turner 
3445713a05bSZachary Turner   if (full || (a.m_directory && b.m_directory)) {
3455713a05bSZachary Turner     result = ConstString::Compare(a.m_directory, b.m_directory, case_sensitive);
3465713a05bSZachary Turner     if (result)
3475713a05bSZachary Turner       return result;
3485713a05bSZachary Turner   }
3495713a05bSZachary Turner   return ConstString::Compare(a.m_filename, b.m_filename, case_sensitive);
3505713a05bSZachary Turner }
3515713a05bSZachary Turner 
352776cd7adSGreg Clayton bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full) {
3535713a05bSZachary Turner   // case sensitivity of equality test
3545713a05bSZachary Turner   const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
3555713a05bSZachary Turner 
356c1cc3173SJonas Devlieghere   const bool filenames_equal = ConstString::Equals(a.m_filename,
357c1cc3173SJonas Devlieghere                                                    b.m_filename,
358c1cc3173SJonas Devlieghere                                                    case_sensitive);
35997e4f472SJim Ingham 
360776cd7adSGreg Clayton   if (!filenames_equal)
36197e4f472SJim Ingham     return false;
36297e4f472SJim Ingham 
3635713a05bSZachary Turner   if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty()))
36497e4f472SJim Ingham     return filenames_equal;
3655713a05bSZachary Turner 
3665713a05bSZachary Turner   return a == b;
3675713a05bSZachary Turner }
3685713a05bSZachary Turner 
3695713a05bSZachary Turner //------------------------------------------------------------------
37005097246SAdrian Prantl // Dump the object to the supplied stream. If the object contains a valid
37105097246SAdrian Prantl // directory name, it will be displayed followed by a directory delimiter, and
37205097246SAdrian Prantl // the filename.
3735713a05bSZachary Turner //------------------------------------------------------------------
3745713a05bSZachary Turner void FileSpec::Dump(Stream *s) const {
3755713a05bSZachary Turner   if (s) {
3765713a05bSZachary Turner     std::string path{GetPath(true)};
3775713a05bSZachary Turner     s->PutCString(path);
3782cb7cf8eSPavel Labath     char path_separator = GetPreferredPathSeparator(m_style);
3795713a05bSZachary Turner     if (!m_filename && !path.empty() && path.back() != path_separator)
3805713a05bSZachary Turner       s->PutChar(path_separator);
3815713a05bSZachary Turner   }
3825713a05bSZachary Turner }
3835713a05bSZachary Turner 
3842cb7cf8eSPavel Labath FileSpec::Style FileSpec::GetPathStyle() const { return m_style; }
3855713a05bSZachary Turner 
3865713a05bSZachary Turner //------------------------------------------------------------------
3875713a05bSZachary Turner // Directory string get accessor.
3885713a05bSZachary Turner //------------------------------------------------------------------
3895713a05bSZachary Turner ConstString &FileSpec::GetDirectory() { return m_directory; }
3905713a05bSZachary Turner 
3915713a05bSZachary Turner //------------------------------------------------------------------
3925713a05bSZachary Turner // Directory string const get accessor.
3935713a05bSZachary Turner //------------------------------------------------------------------
3945713a05bSZachary Turner const ConstString &FileSpec::GetDirectory() const { return m_directory; }
3955713a05bSZachary Turner 
3965713a05bSZachary Turner //------------------------------------------------------------------
3975713a05bSZachary Turner // Filename string get accessor.
3985713a05bSZachary Turner //------------------------------------------------------------------
3995713a05bSZachary Turner ConstString &FileSpec::GetFilename() { return m_filename; }
4005713a05bSZachary Turner 
4015713a05bSZachary Turner //------------------------------------------------------------------
4025713a05bSZachary Turner // Filename string const get accessor.
4035713a05bSZachary Turner //------------------------------------------------------------------
4045713a05bSZachary Turner const ConstString &FileSpec::GetFilename() const { return m_filename; }
4055713a05bSZachary Turner 
4065713a05bSZachary Turner //------------------------------------------------------------------
40705097246SAdrian Prantl // Extract the directory and path into a fixed buffer. This is needed as the
40805097246SAdrian Prantl // directory and path are stored in separate string values.
4095713a05bSZachary Turner //------------------------------------------------------------------
4105713a05bSZachary Turner size_t FileSpec::GetPath(char *path, size_t path_max_len,
4115713a05bSZachary Turner                          bool denormalize) const {
4125713a05bSZachary Turner   if (!path)
4135713a05bSZachary Turner     return 0;
4145713a05bSZachary Turner 
4155713a05bSZachary Turner   std::string result = GetPath(denormalize);
4165713a05bSZachary Turner   ::snprintf(path, path_max_len, "%s", result.c_str());
4175713a05bSZachary Turner   return std::min(path_max_len - 1, result.length());
4185713a05bSZachary Turner }
4195713a05bSZachary Turner 
4205713a05bSZachary Turner std::string FileSpec::GetPath(bool denormalize) const {
4215713a05bSZachary Turner   llvm::SmallString<64> result;
4225713a05bSZachary Turner   GetPath(result, denormalize);
4235713a05bSZachary Turner   return std::string(result.begin(), result.end());
4245713a05bSZachary Turner }
4255713a05bSZachary Turner 
4265713a05bSZachary Turner const char *FileSpec::GetCString(bool denormalize) const {
427*65e5e278SJonas Devlieghere   return ConstString{GetPath(denormalize)}.AsCString(nullptr);
4285713a05bSZachary Turner }
4295713a05bSZachary Turner 
4305713a05bSZachary Turner void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path,
4315713a05bSZachary Turner                        bool denormalize) const {
4325713a05bSZachary Turner   path.append(m_directory.GetStringRef().begin(),
4335713a05bSZachary Turner               m_directory.GetStringRef().end());
434776cd7adSGreg Clayton   // Since the path was normalized and all paths use '/' when stored in these
435776cd7adSGreg Clayton   // objects, we don't need to look for the actual syntax specific path
436776cd7adSGreg Clayton   // separator, we just look for and insert '/'.
437776cd7adSGreg Clayton   if (m_directory && m_filename && m_directory.GetStringRef().back() != '/' &&
438776cd7adSGreg Clayton       m_filename.GetStringRef().back() != '/')
439776cd7adSGreg Clayton     path.insert(path.end(), '/');
4405713a05bSZachary Turner   path.append(m_filename.GetStringRef().begin(),
4415713a05bSZachary Turner               m_filename.GetStringRef().end());
4425713a05bSZachary Turner   if (denormalize && !path.empty())
4432cb7cf8eSPavel Labath     Denormalize(path, m_style);
4445713a05bSZachary Turner }
4455713a05bSZachary Turner 
4465713a05bSZachary Turner ConstString FileSpec::GetFileNameExtension() const {
4479c1a645aSJonas Devlieghere   return ConstString(
4489c1a645aSJonas Devlieghere       llvm::sys::path::extension(m_filename.GetStringRef(), m_style));
4495713a05bSZachary Turner }
4505713a05bSZachary Turner 
4515713a05bSZachary Turner ConstString FileSpec::GetFileNameStrippingExtension() const {
4529c1a645aSJonas Devlieghere   return ConstString(llvm::sys::path::stem(m_filename.GetStringRef(), m_style));
4535713a05bSZachary Turner }
4545713a05bSZachary Turner 
4555713a05bSZachary Turner //------------------------------------------------------------------
45605097246SAdrian Prantl // Return the size in bytes that this object takes in memory. This returns the
45705097246SAdrian Prantl // size in bytes of this object, not any shared string values it may refer to.
4585713a05bSZachary Turner //------------------------------------------------------------------
4595713a05bSZachary Turner size_t FileSpec::MemorySize() const {
4605713a05bSZachary Turner   return m_filename.MemorySize() + m_directory.MemorySize();
4615713a05bSZachary Turner }
4625713a05bSZachary Turner 
4635713a05bSZachary Turner FileSpec
4645713a05bSZachary Turner FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const {
4655713a05bSZachary Turner   FileSpec ret = *this;
4665713a05bSZachary Turner   ret.AppendPathComponent(component);
4675713a05bSZachary Turner   return ret;
4685713a05bSZachary Turner }
4695713a05bSZachary Turner 
4705713a05bSZachary Turner FileSpec FileSpec::CopyByRemovingLastPathComponent() const {
47124bd63c4SJonas Devlieghere   llvm::SmallString<64> current_path;
47224bd63c4SJonas Devlieghere   GetPath(current_path, false);
47324bd63c4SJonas Devlieghere   if (llvm::sys::path::has_parent_path(current_path, m_style))
4748f3be7a3SJonas Devlieghere     return FileSpec(llvm::sys::path::parent_path(current_path, m_style),
47524bd63c4SJonas Devlieghere                     m_style);
47624bd63c4SJonas Devlieghere   return *this;
4775713a05bSZachary Turner }
4785713a05bSZachary Turner 
4795713a05bSZachary Turner ConstString FileSpec::GetLastPathComponent() const {
48024bd63c4SJonas Devlieghere   llvm::SmallString<64> current_path;
48124bd63c4SJonas Devlieghere   GetPath(current_path, false);
48224bd63c4SJonas Devlieghere   return ConstString(llvm::sys::path::filename(current_path, m_style));
4835713a05bSZachary Turner }
4845713a05bSZachary Turner 
4855713a05bSZachary Turner void FileSpec::PrependPathComponent(llvm::StringRef component) {
48624bd63c4SJonas Devlieghere   llvm::SmallString<64> new_path(component);
48724bd63c4SJonas Devlieghere   llvm::SmallString<64> current_path;
48824bd63c4SJonas Devlieghere   GetPath(current_path, false);
48924bd63c4SJonas Devlieghere   llvm::sys::path::append(new_path,
49024bd63c4SJonas Devlieghere                           llvm::sys::path::begin(current_path, m_style),
49124bd63c4SJonas Devlieghere                           llvm::sys::path::end(current_path), m_style);
4928f3be7a3SJonas Devlieghere   SetFile(new_path, m_style);
4935713a05bSZachary Turner }
4945713a05bSZachary Turner 
4955713a05bSZachary Turner void FileSpec::PrependPathComponent(const FileSpec &new_path) {
4965713a05bSZachary Turner   return PrependPathComponent(new_path.GetPath(false));
4975713a05bSZachary Turner }
4985713a05bSZachary Turner 
4995713a05bSZachary Turner void FileSpec::AppendPathComponent(llvm::StringRef component) {
50024bd63c4SJonas Devlieghere   llvm::SmallString<64> current_path;
50124bd63c4SJonas Devlieghere   GetPath(current_path, false);
50224bd63c4SJonas Devlieghere   llvm::sys::path::append(current_path, m_style, component);
5038f3be7a3SJonas Devlieghere   SetFile(current_path, m_style);
5045713a05bSZachary Turner }
5055713a05bSZachary Turner 
5065713a05bSZachary Turner void FileSpec::AppendPathComponent(const FileSpec &new_path) {
5075713a05bSZachary Turner   return AppendPathComponent(new_path.GetPath(false));
5085713a05bSZachary Turner }
5095713a05bSZachary Turner 
510df8e291eSJonas Devlieghere bool FileSpec::RemoveLastPathComponent() {
511df8e291eSJonas Devlieghere   llvm::SmallString<64> current_path;
512df8e291eSJonas Devlieghere   GetPath(current_path, false);
513df8e291eSJonas Devlieghere   if (llvm::sys::path::has_parent_path(current_path, m_style)) {
5148f3be7a3SJonas Devlieghere     SetFile(llvm::sys::path::parent_path(current_path, m_style));
515df8e291eSJonas Devlieghere     return true;
5165713a05bSZachary Turner   }
517df8e291eSJonas Devlieghere   return false;
5185713a05bSZachary Turner }
5195713a05bSZachary Turner //------------------------------------------------------------------
5205713a05bSZachary Turner /// Returns true if the filespec represents an implementation source
5215713a05bSZachary Turner /// file (files with a ".c", ".cpp", ".m", ".mm" (many more)
5225713a05bSZachary Turner /// extension).
5235713a05bSZachary Turner ///
5245713a05bSZachary Turner /// @return
5255713a05bSZachary Turner ///     \b true if the filespec represents an implementation source
5265713a05bSZachary Turner ///     file, \b false otherwise.
5275713a05bSZachary Turner //------------------------------------------------------------------
5285713a05bSZachary Turner bool FileSpec::IsSourceImplementationFile() const {
5295713a05bSZachary Turner   ConstString extension(GetFileNameExtension());
5305713a05bSZachary Turner   if (!extension)
5315713a05bSZachary Turner     return false;
5325713a05bSZachary Turner 
5335713a05bSZachary Turner   static RegularExpression g_source_file_regex(llvm::StringRef(
534ad8d48f9SJonas Devlieghere       "^.([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|["
5355713a05bSZachary Turner       "cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO]["
5365713a05bSZachary Turner       "rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])"
5375713a05bSZachary Turner       "$"));
5385713a05bSZachary Turner   return g_source_file_regex.Execute(extension.GetStringRef());
5395713a05bSZachary Turner }
5405713a05bSZachary Turner 
541c1cc3173SJonas Devlieghere bool FileSpec::IsRelative() const {
542c1cc3173SJonas Devlieghere   return !IsAbsolute();
543c1cc3173SJonas Devlieghere }
5445713a05bSZachary Turner 
545ad8d48f9SJonas Devlieghere bool FileSpec::IsAbsolute() const {
546ad8d48f9SJonas Devlieghere   llvm::SmallString<64> current_path;
547ad8d48f9SJonas Devlieghere   GetPath(current_path, false);
548ad8d48f9SJonas Devlieghere 
549ad8d48f9SJonas Devlieghere   // Early return if the path is empty.
550ad8d48f9SJonas Devlieghere   if (current_path.empty())
551ad8d48f9SJonas Devlieghere     return false;
552ad8d48f9SJonas Devlieghere 
553ad8d48f9SJonas Devlieghere   // We consider paths starting with ~ to be absolute.
554ad8d48f9SJonas Devlieghere   if (current_path[0] == '~')
555ad8d48f9SJonas Devlieghere     return true;
556ad8d48f9SJonas Devlieghere 
557ad8d48f9SJonas Devlieghere   return llvm::sys::path::is_absolute(current_path, m_style);
558ad8d48f9SJonas Devlieghere }
5595713a05bSZachary Turner 
5605713a05bSZachary Turner void llvm::format_provider<FileSpec>::format(const FileSpec &F,
5615713a05bSZachary Turner                                              raw_ostream &Stream,
5625713a05bSZachary Turner                                              StringRef Style) {
5635713a05bSZachary Turner   assert(
5645713a05bSZachary Turner       (Style.empty() || Style.equals_lower("F") || Style.equals_lower("D")) &&
5655713a05bSZachary Turner       "Invalid FileSpec style!");
5665713a05bSZachary Turner 
5675713a05bSZachary Turner   StringRef dir = F.GetDirectory().GetStringRef();
5685713a05bSZachary Turner   StringRef file = F.GetFilename().GetStringRef();
5695713a05bSZachary Turner 
5705713a05bSZachary Turner   if (dir.empty() && file.empty()) {
5715713a05bSZachary Turner     Stream << "(empty)";
5725713a05bSZachary Turner     return;
5735713a05bSZachary Turner   }
5745713a05bSZachary Turner 
5755713a05bSZachary Turner   if (Style.equals_lower("F")) {
5765713a05bSZachary Turner     Stream << (file.empty() ? "(empty)" : file);
5775713a05bSZachary Turner     return;
5785713a05bSZachary Turner   }
5795713a05bSZachary Turner 
5805713a05bSZachary Turner   // Style is either D or empty, either way we need to print the directory.
5815713a05bSZachary Turner   if (!dir.empty()) {
58205097246SAdrian Prantl     // Directory is stored in normalized form, which might be different than
58305097246SAdrian Prantl     // preferred form.  In order to handle this, we need to cut off the
58405097246SAdrian Prantl     // filename, then denormalize, then write the entire denorm'ed directory.
5855713a05bSZachary Turner     llvm::SmallString<64> denormalized_dir = dir;
5862cb7cf8eSPavel Labath     Denormalize(denormalized_dir, F.GetPathStyle());
5875713a05bSZachary Turner     Stream << denormalized_dir;
5882cb7cf8eSPavel Labath     Stream << GetPreferredPathSeparator(F.GetPathStyle());
5895713a05bSZachary Turner   }
5905713a05bSZachary Turner 
5915713a05bSZachary Turner   if (Style.equals_lower("D")) {
5925713a05bSZachary Turner     // We only want to print the directory, so now just exit.
5935713a05bSZachary Turner     if (dir.empty())
5945713a05bSZachary Turner       Stream << "(empty)";
5955713a05bSZachary Turner     return;
5965713a05bSZachary Turner   }
5975713a05bSZachary Turner 
5985713a05bSZachary Turner   if (!file.empty())
5995713a05bSZachary Turner     Stream << file;
6005713a05bSZachary Turner }
601