180814287SRaphael Isemann //===-- FileSpec.cpp ------------------------------------------------------===// 25713a05bSZachary Turner // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 65713a05bSZachary Turner // 75713a05bSZachary Turner //===----------------------------------------------------------------------===// 85713a05bSZachary Turner 95713a05bSZachary Turner #include "lldb/Utility/FileSpec.h" 105713a05bSZachary Turner #include "lldb/Utility/RegularExpression.h" 115713a05bSZachary Turner #include "lldb/Utility/Stream.h" 125713a05bSZachary Turner 13ad8d48f9SJonas Devlieghere #include "llvm/ADT/SmallString.h" 14ad8d48f9SJonas Devlieghere #include "llvm/ADT/SmallVector.h" 155713a05bSZachary Turner #include "llvm/ADT/StringRef.h" 16ad8d48f9SJonas Devlieghere #include "llvm/ADT/Triple.h" 17ad8d48f9SJonas Devlieghere #include "llvm/ADT/Twine.h" 18ad8d48f9SJonas Devlieghere #include "llvm/Support/ErrorOr.h" 195713a05bSZachary Turner #include "llvm/Support/FileSystem.h" 205713a05bSZachary Turner #include "llvm/Support/Program.h" 21ad8d48f9SJonas Devlieghere #include "llvm/Support/raw_ostream.h" 224479ac15SZachary Turner 23672d2c12SJonas Devlieghere #include <algorithm> 24672d2c12SJonas Devlieghere #include <system_error> 25672d2c12SJonas Devlieghere #include <vector> 264479ac15SZachary Turner 2776e47d48SRaphael Isemann #include <cassert> 2876e47d48SRaphael Isemann #include <climits> 2976e47d48SRaphael Isemann #include <cstdio> 3076e47d48SRaphael Isemann #include <cstring> 315713a05bSZachary Turner 325713a05bSZachary Turner using namespace lldb; 335713a05bSZachary Turner using namespace lldb_private; 345713a05bSZachary Turner 355713a05bSZachary Turner namespace { 365713a05bSZachary Turner 372cb7cf8eSPavel Labath static constexpr FileSpec::Style GetNativeStyle() { 38b1cb0b79SNico Weber #if defined(_WIN32) 392cb7cf8eSPavel Labath return FileSpec::Style::windows; 405713a05bSZachary Turner #else 412cb7cf8eSPavel Labath return FileSpec::Style::posix; 425713a05bSZachary Turner #endif 435713a05bSZachary Turner } 445713a05bSZachary Turner 452cb7cf8eSPavel Labath bool PathStyleIsPosix(FileSpec::Style style) { 46a2c9cf4cSMartin Storsjö return llvm::sys::path::is_style_posix(style); 475713a05bSZachary Turner } 485713a05bSZachary Turner 492cb7cf8eSPavel Labath const char *GetPathSeparators(FileSpec::Style style) { 50ad8d48f9SJonas Devlieghere return llvm::sys::path::get_separator(style).data(); 515713a05bSZachary Turner } 525713a05bSZachary Turner 532cb7cf8eSPavel Labath char GetPreferredPathSeparator(FileSpec::Style style) { 542cb7cf8eSPavel Labath return GetPathSeparators(style)[0]; 555713a05bSZachary Turner } 565713a05bSZachary Turner 572cb7cf8eSPavel Labath void Denormalize(llvm::SmallVectorImpl<char> &path, FileSpec::Style style) { 582cb7cf8eSPavel Labath if (PathStyleIsPosix(style)) 595713a05bSZachary Turner return; 605713a05bSZachary Turner 615713a05bSZachary Turner std::replace(path.begin(), path.end(), '/', '\\'); 625713a05bSZachary Turner } 6386188d8aSGreg Clayton 645713a05bSZachary Turner } // end anonymous namespace 655713a05bSZachary Turner 662cb7cf8eSPavel Labath FileSpec::FileSpec() : m_style(GetNativeStyle()) {} 675713a05bSZachary Turner 6805097246SAdrian Prantl // Default constructor that can take an optional full path to a file on disk. 698f3be7a3SJonas Devlieghere FileSpec::FileSpec(llvm::StringRef path, Style style) : m_style(style) { 708f3be7a3SJonas Devlieghere SetFile(path, style); 715713a05bSZachary Turner } 725713a05bSZachary Turner 73a7d4cec4SJonas Devlieghere FileSpec::FileSpec(llvm::StringRef path, const llvm::Triple &triple) 74706cd705SJonas Devlieghere : FileSpec{path, triple.isOSWindows() ? Style::windows : Style::posix} {} 755713a05bSZachary Turner 76c1cc3173SJonas Devlieghere namespace { 77c1cc3173SJonas Devlieghere /// Safely get a character at the specified index. 78c1cc3173SJonas Devlieghere /// 79f05b42e9SAdrian Prantl /// \param[in] path 80c1cc3173SJonas Devlieghere /// A full, partial, or relative path to a file. 81c1cc3173SJonas Devlieghere /// 82f05b42e9SAdrian Prantl /// \param[in] i 83c1cc3173SJonas Devlieghere /// An index into path which may or may not be valid. 84c1cc3173SJonas Devlieghere /// 85f05b42e9SAdrian Prantl /// \return 86c1cc3173SJonas Devlieghere /// The character at index \a i if the index is valid, or 0 if 87c1cc3173SJonas Devlieghere /// the index is not valid. 88c1cc3173SJonas Devlieghere inline char safeCharAtIndex(const llvm::StringRef &path, size_t i) { 89c1cc3173SJonas Devlieghere if (i < path.size()) 90c1cc3173SJonas Devlieghere return path[i]; 91c1cc3173SJonas Devlieghere return 0; 92c1cc3173SJonas Devlieghere } 93c1cc3173SJonas Devlieghere 94c1cc3173SJonas Devlieghere /// Check if a path needs to be normalized. 95c1cc3173SJonas Devlieghere /// 96c1cc3173SJonas Devlieghere /// Check if a path needs to be normalized. We currently consider a 97c1cc3173SJonas Devlieghere /// path to need normalization if any of the following are true 98c1cc3173SJonas Devlieghere /// - path contains "/./" 99c1cc3173SJonas Devlieghere /// - path contains "/../" 100c1cc3173SJonas Devlieghere /// - path contains "//" 101c1cc3173SJonas Devlieghere /// - path ends with "/" 102c1cc3173SJonas Devlieghere /// Paths that start with "./" or with "../" are not considered to 103c1cc3173SJonas Devlieghere /// need normalization since we aren't trying to resolve the path, 104c1cc3173SJonas Devlieghere /// we are just trying to remove redundant things from the path. 105c1cc3173SJonas Devlieghere /// 106f05b42e9SAdrian Prantl /// \param[in] path 107c1cc3173SJonas Devlieghere /// A full, partial, or relative path to a file. 108c1cc3173SJonas Devlieghere /// 109f05b42e9SAdrian Prantl /// \return 110c1cc3173SJonas Devlieghere /// Returns \b true if the path needs to be normalized. 111c1cc3173SJonas Devlieghere bool needsNormalization(const llvm::StringRef &path) { 112c1cc3173SJonas Devlieghere if (path.empty()) 113c1cc3173SJonas Devlieghere return false; 114c1cc3173SJonas Devlieghere // We strip off leading "." values so these paths need to be normalized 115c1cc3173SJonas Devlieghere if (path[0] == '.') 116c1cc3173SJonas Devlieghere return true; 117c1cc3173SJonas Devlieghere for (auto i = path.find_first_of("\\/"); i != llvm::StringRef::npos; 118c1cc3173SJonas Devlieghere i = path.find_first_of("\\/", i + 1)) { 119c1cc3173SJonas Devlieghere const auto next = safeCharAtIndex(path, i+1); 120c1cc3173SJonas Devlieghere switch (next) { 121c1cc3173SJonas Devlieghere case 0: 122c1cc3173SJonas Devlieghere // path separator char at the end of the string which should be 123c1cc3173SJonas Devlieghere // stripped unless it is the one and only character 124c1cc3173SJonas Devlieghere return i > 0; 125c1cc3173SJonas Devlieghere case '/': 126c1cc3173SJonas Devlieghere case '\\': 127c1cc3173SJonas Devlieghere // two path separator chars in the middle of a path needs to be 128c1cc3173SJonas Devlieghere // normalized 129c1cc3173SJonas Devlieghere if (i > 0) 130c1cc3173SJonas Devlieghere return true; 131c1cc3173SJonas Devlieghere ++i; 132c1cc3173SJonas Devlieghere break; 133c1cc3173SJonas Devlieghere 134c1cc3173SJonas Devlieghere case '.': { 135c1cc3173SJonas Devlieghere const auto next_next = safeCharAtIndex(path, i+2); 136c1cc3173SJonas Devlieghere switch (next_next) { 137c1cc3173SJonas Devlieghere default: break; 138c1cc3173SJonas Devlieghere case 0: return true; // ends with "/." 139c1cc3173SJonas Devlieghere case '/': 140c1cc3173SJonas Devlieghere case '\\': 141c1cc3173SJonas Devlieghere return true; // contains "/./" 142c1cc3173SJonas Devlieghere case '.': { 143c1cc3173SJonas Devlieghere const auto next_next_next = safeCharAtIndex(path, i+3); 144c1cc3173SJonas Devlieghere switch (next_next_next) { 145c1cc3173SJonas Devlieghere default: break; 146c1cc3173SJonas Devlieghere case 0: return true; // ends with "/.." 147c1cc3173SJonas Devlieghere case '/': 148c1cc3173SJonas Devlieghere case '\\': 149c1cc3173SJonas Devlieghere return true; // contains "/../" 150c1cc3173SJonas Devlieghere } 151c1cc3173SJonas Devlieghere break; 152c1cc3173SJonas Devlieghere } 153c1cc3173SJonas Devlieghere } 154c1cc3173SJonas Devlieghere } 155c1cc3173SJonas Devlieghere break; 156c1cc3173SJonas Devlieghere 157c1cc3173SJonas Devlieghere default: 158c1cc3173SJonas Devlieghere break; 159c1cc3173SJonas Devlieghere } 160c1cc3173SJonas Devlieghere } 161c1cc3173SJonas Devlieghere return false; 162c1cc3173SJonas Devlieghere } 163c1cc3173SJonas Devlieghere 164c1cc3173SJonas Devlieghere 165c1cc3173SJonas Devlieghere } 1665713a05bSZachary Turner 1678f3be7a3SJonas Devlieghere void FileSpec::SetFile(llvm::StringRef pathname) { SetFile(pathname, m_style); } 168937348cdSJonas Devlieghere 16905097246SAdrian Prantl // Update the contents of this object with a new path. The path will be split 17005097246SAdrian Prantl // up into a directory and filename and stored as uniqued string values for 17105097246SAdrian Prantl // quick comparison and efficient memory usage. 1728f3be7a3SJonas Devlieghere void FileSpec::SetFile(llvm::StringRef pathname, Style style) { 1735713a05bSZachary Turner m_filename.Clear(); 1745713a05bSZachary Turner m_directory.Clear(); 1755713a05bSZachary Turner m_is_resolved = false; 1762cb7cf8eSPavel Labath m_style = (style == Style::native) ? GetNativeStyle() : style; 1775713a05bSZachary Turner 1785713a05bSZachary Turner if (pathname.empty()) 1795713a05bSZachary Turner return; 1805713a05bSZachary Turner 1818f3be7a3SJonas Devlieghere llvm::SmallString<128> resolved(pathname); 1825713a05bSZachary Turner 183776cd7adSGreg Clayton // Normalize the path by removing ".", ".." and other redundant components. 184c1cc3173SJonas Devlieghere if (needsNormalization(resolved)) 1852cb7cf8eSPavel Labath llvm::sys::path::remove_dots(resolved, true, m_style); 186776cd7adSGreg Clayton 187776cd7adSGreg Clayton // Normalize back slashes to forward slashes 1882cb7cf8eSPavel Labath if (m_style == Style::windows) 189776cd7adSGreg Clayton std::replace(resolved.begin(), resolved.end(), '\\', '/'); 1905713a05bSZachary Turner 19139d50b72SGreg Clayton if (resolved.empty()) { 19239d50b72SGreg Clayton // If we have no path after normalization set the path to the current 19339d50b72SGreg Clayton // directory. This matches what python does and also a few other path 19439d50b72SGreg Clayton // utilities. 19539d50b72SGreg Clayton m_filename.SetString("."); 19639d50b72SGreg Clayton return; 19739d50b72SGreg Clayton } 19839d50b72SGreg Clayton 199ad8d48f9SJonas Devlieghere // Split path into filename and directory. We rely on the underlying char 200ad8d48f9SJonas Devlieghere // pointer to be nullptr when the components are empty. 201ad8d48f9SJonas Devlieghere llvm::StringRef filename = llvm::sys::path::filename(resolved, m_style); 202ad8d48f9SJonas Devlieghere if(!filename.empty()) 203ad8d48f9SJonas Devlieghere m_filename.SetString(filename); 2048f3be7a3SJonas Devlieghere 205ad8d48f9SJonas Devlieghere llvm::StringRef directory = llvm::sys::path::parent_path(resolved, m_style); 206ad8d48f9SJonas Devlieghere if(!directory.empty()) 207ad8d48f9SJonas Devlieghere m_directory.SetString(directory); 2085713a05bSZachary Turner } 2095713a05bSZachary Turner 210706cd705SJonas Devlieghere void FileSpec::SetFile(llvm::StringRef path, const llvm::Triple &triple) { 211706cd705SJonas Devlieghere return SetFile(path, triple.isOSWindows() ? Style::windows : Style::posix); 2125713a05bSZachary Turner } 2135713a05bSZachary Turner 21405097246SAdrian Prantl // Convert to pointer operator. This allows code to check any FileSpec objects 21505097246SAdrian Prantl // to see if they contain anything valid using code such as: 2165713a05bSZachary Turner // 2175713a05bSZachary Turner // if (file_spec) 2185713a05bSZachary Turner // {} 2195713a05bSZachary Turner FileSpec::operator bool() const { return m_filename || m_directory; } 2205713a05bSZachary Turner 22105097246SAdrian Prantl // Logical NOT operator. This allows code to check any FileSpec objects to see 22205097246SAdrian Prantl // if they are invalid using code such as: 2235713a05bSZachary Turner // 2245713a05bSZachary Turner // if (!file_spec) 2255713a05bSZachary Turner // {} 2265713a05bSZachary Turner bool FileSpec::operator!() const { return !m_directory && !m_filename; } 2275713a05bSZachary Turner 2285713a05bSZachary Turner bool FileSpec::DirectoryEquals(const FileSpec &rhs) const { 2295713a05bSZachary Turner const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive(); 2305713a05bSZachary Turner return ConstString::Equals(m_directory, rhs.m_directory, case_sensitive); 2315713a05bSZachary Turner } 2325713a05bSZachary Turner 2335713a05bSZachary Turner bool FileSpec::FileEquals(const FileSpec &rhs) const { 2345713a05bSZachary Turner const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive(); 2355713a05bSZachary Turner return ConstString::Equals(m_filename, rhs.m_filename, case_sensitive); 2365713a05bSZachary Turner } 2375713a05bSZachary Turner 2385713a05bSZachary Turner // Equal to operator 2395713a05bSZachary Turner bool FileSpec::operator==(const FileSpec &rhs) const { 2408f3be7a3SJonas Devlieghere return FileEquals(rhs) && DirectoryEquals(rhs); 2415713a05bSZachary Turner } 2425713a05bSZachary Turner 2435713a05bSZachary Turner // Not equal to operator 2445713a05bSZachary Turner bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); } 2455713a05bSZachary Turner 2465713a05bSZachary Turner // Less than operator 2475713a05bSZachary Turner bool FileSpec::operator<(const FileSpec &rhs) const { 2485713a05bSZachary Turner return FileSpec::Compare(*this, rhs, true) < 0; 2495713a05bSZachary Turner } 2505713a05bSZachary Turner 2515713a05bSZachary Turner // Dump a FileSpec object to a stream 2525713a05bSZachary Turner Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) { 2534dac97ebSRaphael Isemann f.Dump(s.AsRawOstream()); 2545713a05bSZachary Turner return s; 2555713a05bSZachary Turner } 2565713a05bSZachary Turner 25705097246SAdrian Prantl // Clear this object by releasing both the directory and filename string values 25805097246SAdrian Prantl // and making them both the empty string. 2595713a05bSZachary Turner void FileSpec::Clear() { 2605713a05bSZachary Turner m_directory.Clear(); 2615713a05bSZachary Turner m_filename.Clear(); 2625713a05bSZachary Turner } 2635713a05bSZachary Turner 26405097246SAdrian Prantl // Compare two FileSpec objects. If "full" is true, then both the directory and 26505097246SAdrian Prantl // the filename must match. If "full" is false, then the directory names for 26605097246SAdrian Prantl // "a" and "b" are only compared if they are both non-empty. This allows a 26705097246SAdrian Prantl // FileSpec object to only contain a filename and it can match FileSpec objects 26805097246SAdrian Prantl // that have matching filenames with different paths. 2695713a05bSZachary Turner // 27005097246SAdrian Prantl // Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" and "1" if 27105097246SAdrian Prantl // "a" is greater than "b". 2725713a05bSZachary Turner int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) { 2735713a05bSZachary Turner int result = 0; 2745713a05bSZachary Turner 2755713a05bSZachary Turner // case sensitivity of compare 2765713a05bSZachary Turner const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive(); 2775713a05bSZachary Turner 2785713a05bSZachary Turner // If full is true, then we must compare both the directory and filename. 2795713a05bSZachary Turner 28005097246SAdrian Prantl // If full is false, then if either directory is empty, then we match on the 28105097246SAdrian Prantl // basename only, and if both directories have valid values, we still do a 28205097246SAdrian Prantl // full compare. This allows for matching when we just have a filename in one 28305097246SAdrian Prantl // of the FileSpec objects. 2845713a05bSZachary Turner 2855713a05bSZachary Turner if (full || (a.m_directory && b.m_directory)) { 2865713a05bSZachary Turner result = ConstString::Compare(a.m_directory, b.m_directory, case_sensitive); 2875713a05bSZachary Turner if (result) 2885713a05bSZachary Turner return result; 2895713a05bSZachary Turner } 2905713a05bSZachary Turner return ConstString::Compare(a.m_filename, b.m_filename, case_sensitive); 2915713a05bSZachary Turner } 2925713a05bSZachary Turner 293776cd7adSGreg Clayton bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full) { 294b18e190bSPavel Labath if (full || (a.GetDirectory() && b.GetDirectory())) 2955713a05bSZachary Turner return a == b; 296b18e190bSPavel Labath 297b18e190bSPavel Labath return a.FileEquals(b); 2985713a05bSZachary Turner } 2995713a05bSZachary Turner 300532290e6SPavel Labath bool FileSpec::Match(const FileSpec &pattern, const FileSpec &file) { 301532290e6SPavel Labath if (pattern.GetDirectory()) 302532290e6SPavel Labath return pattern == file; 303532290e6SPavel Labath if (pattern.GetFilename()) 304532290e6SPavel Labath return pattern.FileEquals(file); 305532290e6SPavel Labath return true; 306532290e6SPavel Labath } 307532290e6SPavel Labath 308841bea93SPavel Labath llvm::Optional<FileSpec::Style> FileSpec::GuessPathStyle(llvm::StringRef absolute_path) { 309841bea93SPavel Labath if (absolute_path.startswith("/")) 310841bea93SPavel Labath return Style::posix; 311841bea93SPavel Labath if (absolute_path.startswith(R"(\\)")) 312841bea93SPavel Labath return Style::windows; 313*f72ae5cbSJaroslav Sevcik if (absolute_path.size() >= 3 && llvm::isAlpha(absolute_path[0]) && 314841bea93SPavel Labath absolute_path.substr(1, 2) == R"(:\)") 315841bea93SPavel Labath return Style::windows; 316841bea93SPavel Labath return llvm::None; 317841bea93SPavel Labath } 318841bea93SPavel Labath 31905097246SAdrian Prantl // Dump the object to the supplied stream. If the object contains a valid 32005097246SAdrian Prantl // directory name, it will be displayed followed by a directory delimiter, and 32105097246SAdrian Prantl // the filename. 3224dac97ebSRaphael Isemann void FileSpec::Dump(llvm::raw_ostream &s) const { 3235713a05bSZachary Turner std::string path{GetPath(true)}; 3244dac97ebSRaphael Isemann s << path; 3252cb7cf8eSPavel Labath char path_separator = GetPreferredPathSeparator(m_style); 3265713a05bSZachary Turner if (!m_filename && !path.empty() && path.back() != path_separator) 3274dac97ebSRaphael Isemann s << path_separator; 3285713a05bSZachary Turner } 3295713a05bSZachary Turner 3302cb7cf8eSPavel Labath FileSpec::Style FileSpec::GetPathStyle() const { return m_style; } 3315713a05bSZachary Turner 3325713a05bSZachary Turner // Directory string get accessor. 3335713a05bSZachary Turner ConstString &FileSpec::GetDirectory() { return m_directory; } 3345713a05bSZachary Turner 3355713a05bSZachary Turner // Directory string const get accessor. 3360e4c4821SAdrian Prantl ConstString FileSpec::GetDirectory() const { return m_directory; } 3375713a05bSZachary Turner 3385713a05bSZachary Turner // Filename string get accessor. 3395713a05bSZachary Turner ConstString &FileSpec::GetFilename() { return m_filename; } 3405713a05bSZachary Turner 3415713a05bSZachary Turner // Filename string const get accessor. 3420e4c4821SAdrian Prantl ConstString FileSpec::GetFilename() const { return m_filename; } 3435713a05bSZachary Turner 34405097246SAdrian Prantl // Extract the directory and path into a fixed buffer. This is needed as the 34505097246SAdrian Prantl // directory and path are stored in separate string values. 3465713a05bSZachary Turner size_t FileSpec::GetPath(char *path, size_t path_max_len, 3475713a05bSZachary Turner bool denormalize) const { 3485713a05bSZachary Turner if (!path) 3495713a05bSZachary Turner return 0; 3505713a05bSZachary Turner 3515713a05bSZachary Turner std::string result = GetPath(denormalize); 3525713a05bSZachary Turner ::snprintf(path, path_max_len, "%s", result.c_str()); 3535713a05bSZachary Turner return std::min(path_max_len - 1, result.length()); 3545713a05bSZachary Turner } 3555713a05bSZachary Turner 3565713a05bSZachary Turner std::string FileSpec::GetPath(bool denormalize) const { 3575713a05bSZachary Turner llvm::SmallString<64> result; 3585713a05bSZachary Turner GetPath(result, denormalize); 3595713a05bSZachary Turner return std::string(result.begin(), result.end()); 3605713a05bSZachary Turner } 3615713a05bSZachary Turner 3625713a05bSZachary Turner const char *FileSpec::GetCString(bool denormalize) const { 36365e5e278SJonas Devlieghere return ConstString{GetPath(denormalize)}.AsCString(nullptr); 3645713a05bSZachary Turner } 3655713a05bSZachary Turner 3665713a05bSZachary Turner void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path, 3675713a05bSZachary Turner bool denormalize) const { 3685713a05bSZachary Turner path.append(m_directory.GetStringRef().begin(), 3695713a05bSZachary Turner m_directory.GetStringRef().end()); 370776cd7adSGreg Clayton // Since the path was normalized and all paths use '/' when stored in these 371776cd7adSGreg Clayton // objects, we don't need to look for the actual syntax specific path 372776cd7adSGreg Clayton // separator, we just look for and insert '/'. 373776cd7adSGreg Clayton if (m_directory && m_filename && m_directory.GetStringRef().back() != '/' && 374776cd7adSGreg Clayton m_filename.GetStringRef().back() != '/') 375776cd7adSGreg Clayton path.insert(path.end(), '/'); 3765713a05bSZachary Turner path.append(m_filename.GetStringRef().begin(), 3775713a05bSZachary Turner m_filename.GetStringRef().end()); 3785713a05bSZachary Turner if (denormalize && !path.empty()) 3792cb7cf8eSPavel Labath Denormalize(path, m_style); 3805713a05bSZachary Turner } 3815713a05bSZachary Turner 3825713a05bSZachary Turner ConstString FileSpec::GetFileNameExtension() const { 3839c1a645aSJonas Devlieghere return ConstString( 3849c1a645aSJonas Devlieghere llvm::sys::path::extension(m_filename.GetStringRef(), m_style)); 3855713a05bSZachary Turner } 3865713a05bSZachary Turner 3875713a05bSZachary Turner ConstString FileSpec::GetFileNameStrippingExtension() const { 3889c1a645aSJonas Devlieghere return ConstString(llvm::sys::path::stem(m_filename.GetStringRef(), m_style)); 3895713a05bSZachary Turner } 3905713a05bSZachary Turner 39105097246SAdrian Prantl // Return the size in bytes that this object takes in memory. This returns the 39205097246SAdrian Prantl // size in bytes of this object, not any shared string values it may refer to. 3935713a05bSZachary Turner size_t FileSpec::MemorySize() const { 3945713a05bSZachary Turner return m_filename.MemorySize() + m_directory.MemorySize(); 3955713a05bSZachary Turner } 3965713a05bSZachary Turner 3975713a05bSZachary Turner FileSpec 3985713a05bSZachary Turner FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const { 3995713a05bSZachary Turner FileSpec ret = *this; 4005713a05bSZachary Turner ret.AppendPathComponent(component); 4015713a05bSZachary Turner return ret; 4025713a05bSZachary Turner } 4035713a05bSZachary Turner 4045713a05bSZachary Turner FileSpec FileSpec::CopyByRemovingLastPathComponent() const { 40524bd63c4SJonas Devlieghere llvm::SmallString<64> current_path; 40624bd63c4SJonas Devlieghere GetPath(current_path, false); 40724bd63c4SJonas Devlieghere if (llvm::sys::path::has_parent_path(current_path, m_style)) 4088f3be7a3SJonas Devlieghere return FileSpec(llvm::sys::path::parent_path(current_path, m_style), 40924bd63c4SJonas Devlieghere m_style); 41024bd63c4SJonas Devlieghere return *this; 4115713a05bSZachary Turner } 4125713a05bSZachary Turner 4135713a05bSZachary Turner ConstString FileSpec::GetLastPathComponent() const { 41424bd63c4SJonas Devlieghere llvm::SmallString<64> current_path; 41524bd63c4SJonas Devlieghere GetPath(current_path, false); 41624bd63c4SJonas Devlieghere return ConstString(llvm::sys::path::filename(current_path, m_style)); 4175713a05bSZachary Turner } 4185713a05bSZachary Turner 4195713a05bSZachary Turner void FileSpec::PrependPathComponent(llvm::StringRef component) { 42024bd63c4SJonas Devlieghere llvm::SmallString<64> new_path(component); 42124bd63c4SJonas Devlieghere llvm::SmallString<64> current_path; 42224bd63c4SJonas Devlieghere GetPath(current_path, false); 42324bd63c4SJonas Devlieghere llvm::sys::path::append(new_path, 42424bd63c4SJonas Devlieghere llvm::sys::path::begin(current_path, m_style), 42524bd63c4SJonas Devlieghere llvm::sys::path::end(current_path), m_style); 4268f3be7a3SJonas Devlieghere SetFile(new_path, m_style); 4275713a05bSZachary Turner } 4285713a05bSZachary Turner 4295713a05bSZachary Turner void FileSpec::PrependPathComponent(const FileSpec &new_path) { 4305713a05bSZachary Turner return PrependPathComponent(new_path.GetPath(false)); 4315713a05bSZachary Turner } 4325713a05bSZachary Turner 4335713a05bSZachary Turner void FileSpec::AppendPathComponent(llvm::StringRef component) { 43424bd63c4SJonas Devlieghere llvm::SmallString<64> current_path; 43524bd63c4SJonas Devlieghere GetPath(current_path, false); 43624bd63c4SJonas Devlieghere llvm::sys::path::append(current_path, m_style, component); 4378f3be7a3SJonas Devlieghere SetFile(current_path, m_style); 4385713a05bSZachary Turner } 4395713a05bSZachary Turner 4405713a05bSZachary Turner void FileSpec::AppendPathComponent(const FileSpec &new_path) { 4415713a05bSZachary Turner return AppendPathComponent(new_path.GetPath(false)); 4425713a05bSZachary Turner } 4435713a05bSZachary Turner 444df8e291eSJonas Devlieghere bool FileSpec::RemoveLastPathComponent() { 445df8e291eSJonas Devlieghere llvm::SmallString<64> current_path; 446df8e291eSJonas Devlieghere GetPath(current_path, false); 447df8e291eSJonas Devlieghere if (llvm::sys::path::has_parent_path(current_path, m_style)) { 4488f3be7a3SJonas Devlieghere SetFile(llvm::sys::path::parent_path(current_path, m_style)); 449df8e291eSJonas Devlieghere return true; 4505713a05bSZachary Turner } 451df8e291eSJonas Devlieghere return false; 4525713a05bSZachary Turner } 4535713a05bSZachary Turner /// Returns true if the filespec represents an implementation source 4545713a05bSZachary Turner /// file (files with a ".c", ".cpp", ".m", ".mm" (many more) 4555713a05bSZachary Turner /// extension). 4565713a05bSZachary Turner /// 457f05b42e9SAdrian Prantl /// \return 4585713a05bSZachary Turner /// \b true if the filespec represents an implementation source 4595713a05bSZachary Turner /// file, \b false otherwise. 4605713a05bSZachary Turner bool FileSpec::IsSourceImplementationFile() const { 4615713a05bSZachary Turner ConstString extension(GetFileNameExtension()); 4625713a05bSZachary Turner if (!extension) 4635713a05bSZachary Turner return false; 4645713a05bSZachary Turner 4655713a05bSZachary Turner static RegularExpression g_source_file_regex(llvm::StringRef( 466ad8d48f9SJonas Devlieghere "^.([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|[" 4675713a05bSZachary Turner "cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO][" 4685713a05bSZachary Turner "rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])" 4695713a05bSZachary Turner "$")); 4705713a05bSZachary Turner return g_source_file_regex.Execute(extension.GetStringRef()); 4715713a05bSZachary Turner } 4725713a05bSZachary Turner 473c1cc3173SJonas Devlieghere bool FileSpec::IsRelative() const { 474c1cc3173SJonas Devlieghere return !IsAbsolute(); 475c1cc3173SJonas Devlieghere } 4765713a05bSZachary Turner 477ad8d48f9SJonas Devlieghere bool FileSpec::IsAbsolute() const { 478ad8d48f9SJonas Devlieghere llvm::SmallString<64> current_path; 479ad8d48f9SJonas Devlieghere GetPath(current_path, false); 480ad8d48f9SJonas Devlieghere 481ad8d48f9SJonas Devlieghere // Early return if the path is empty. 482ad8d48f9SJonas Devlieghere if (current_path.empty()) 483ad8d48f9SJonas Devlieghere return false; 484ad8d48f9SJonas Devlieghere 485ad8d48f9SJonas Devlieghere // We consider paths starting with ~ to be absolute. 486ad8d48f9SJonas Devlieghere if (current_path[0] == '~') 487ad8d48f9SJonas Devlieghere return true; 488ad8d48f9SJonas Devlieghere 489ad8d48f9SJonas Devlieghere return llvm::sys::path::is_absolute(current_path, m_style); 490ad8d48f9SJonas Devlieghere } 4915713a05bSZachary Turner 4927d36d723SPavel Labath void FileSpec::MakeAbsolute(const FileSpec &dir) { 4937d36d723SPavel Labath if (IsRelative()) 4947d36d723SPavel Labath PrependPathComponent(dir); 4957d36d723SPavel Labath } 4967d36d723SPavel Labath 4975713a05bSZachary Turner void llvm::format_provider<FileSpec>::format(const FileSpec &F, 4985713a05bSZachary Turner raw_ostream &Stream, 4995713a05bSZachary Turner StringRef Style) { 500e50f9c41SMartin Storsjö assert((Style.empty() || Style.equals_insensitive("F") || 501e50f9c41SMartin Storsjö Style.equals_insensitive("D")) && 5025713a05bSZachary Turner "Invalid FileSpec style!"); 5035713a05bSZachary Turner 5045713a05bSZachary Turner StringRef dir = F.GetDirectory().GetStringRef(); 5055713a05bSZachary Turner StringRef file = F.GetFilename().GetStringRef(); 5065713a05bSZachary Turner 5075713a05bSZachary Turner if (dir.empty() && file.empty()) { 5085713a05bSZachary Turner Stream << "(empty)"; 5095713a05bSZachary Turner return; 5105713a05bSZachary Turner } 5115713a05bSZachary Turner 512e50f9c41SMartin Storsjö if (Style.equals_insensitive("F")) { 5135713a05bSZachary Turner Stream << (file.empty() ? "(empty)" : file); 5145713a05bSZachary Turner return; 5155713a05bSZachary Turner } 5165713a05bSZachary Turner 5175713a05bSZachary Turner // Style is either D or empty, either way we need to print the directory. 5185713a05bSZachary Turner if (!dir.empty()) { 51905097246SAdrian Prantl // Directory is stored in normalized form, which might be different than 52005097246SAdrian Prantl // preferred form. In order to handle this, we need to cut off the 52105097246SAdrian Prantl // filename, then denormalize, then write the entire denorm'ed directory. 5225713a05bSZachary Turner llvm::SmallString<64> denormalized_dir = dir; 5232cb7cf8eSPavel Labath Denormalize(denormalized_dir, F.GetPathStyle()); 5245713a05bSZachary Turner Stream << denormalized_dir; 5252cb7cf8eSPavel Labath Stream << GetPreferredPathSeparator(F.GetPathStyle()); 5265713a05bSZachary Turner } 5275713a05bSZachary Turner 528e50f9c41SMartin Storsjö if (Style.equals_insensitive("D")) { 5295713a05bSZachary Turner // We only want to print the directory, so now just exit. 5305713a05bSZachary Turner if (dir.empty()) 5315713a05bSZachary Turner Stream << "(empty)"; 5325713a05bSZachary Turner return; 5335713a05bSZachary Turner } 5345713a05bSZachary Turner 5355713a05bSZachary Turner if (!file.empty()) 5365713a05bSZachary Turner Stream << file; 5375713a05bSZachary Turner } 538bc9b6b33SJonas Devlieghere 539bc9b6b33SJonas Devlieghere void llvm::yaml::ScalarEnumerationTraits<FileSpecStyle>::enumeration( 540bc9b6b33SJonas Devlieghere IO &io, FileSpecStyle &value) { 541bc9b6b33SJonas Devlieghere io.enumCase(value, "windows", FileSpecStyle(FileSpec::Style::windows)); 542bc9b6b33SJonas Devlieghere io.enumCase(value, "posix", FileSpecStyle(FileSpec::Style::posix)); 543bc9b6b33SJonas Devlieghere io.enumCase(value, "native", FileSpecStyle(FileSpec::Style::native)); 544bc9b6b33SJonas Devlieghere } 545bc9b6b33SJonas Devlieghere 546bc9b6b33SJonas Devlieghere void llvm::yaml::MappingTraits<FileSpec>::mapping(IO &io, FileSpec &f) { 547bc9b6b33SJonas Devlieghere io.mapRequired("directory", f.m_directory); 548bc9b6b33SJonas Devlieghere io.mapRequired("file", f.m_filename); 549bc9b6b33SJonas Devlieghere io.mapRequired("resolved", f.m_is_resolved); 550bc9b6b33SJonas Devlieghere FileSpecStyle style = f.m_style; 551bc9b6b33SJonas Devlieghere io.mapRequired("style", style); 552bc9b6b33SJonas Devlieghere f.m_style = style; 553bc9b6b33SJonas Devlieghere } 554