15713a05bSZachary Turner //===-- FileSpec.cpp --------------------------------------------*- C++ -*-===// 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 27672d2c12SJonas Devlieghere #include <assert.h> 28672d2c12SJonas Devlieghere #include <limits.h> 29672d2c12SJonas Devlieghere #include <stdio.h> 30672d2c12SJonas Devlieghere #include <string.h> 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) { 462cb7cf8eSPavel Labath return (style == FileSpec::Style::posix || 472cb7cf8eSPavel Labath (style == FileSpec::Style::native && 482cb7cf8eSPavel Labath GetNativeStyle() == FileSpec::Style::posix)); 495713a05bSZachary Turner } 505713a05bSZachary Turner 512cb7cf8eSPavel Labath const char *GetPathSeparators(FileSpec::Style style) { 52ad8d48f9SJonas Devlieghere return llvm::sys::path::get_separator(style).data(); 535713a05bSZachary Turner } 545713a05bSZachary Turner 552cb7cf8eSPavel Labath char GetPreferredPathSeparator(FileSpec::Style style) { 562cb7cf8eSPavel Labath return GetPathSeparators(style)[0]; 575713a05bSZachary Turner } 585713a05bSZachary Turner 592cb7cf8eSPavel Labath void Denormalize(llvm::SmallVectorImpl<char> &path, FileSpec::Style style) { 602cb7cf8eSPavel Labath if (PathStyleIsPosix(style)) 615713a05bSZachary Turner return; 625713a05bSZachary Turner 635713a05bSZachary Turner std::replace(path.begin(), path.end(), '/', '\\'); 645713a05bSZachary Turner } 6586188d8aSGreg Clayton 665713a05bSZachary Turner } // end anonymous namespace 675713a05bSZachary Turner 682cb7cf8eSPavel Labath FileSpec::FileSpec() : m_style(GetNativeStyle()) {} 695713a05bSZachary Turner 705713a05bSZachary Turner //------------------------------------------------------------------ 7105097246SAdrian Prantl // Default constructor that can take an optional full path to a file on disk. 725713a05bSZachary Turner //------------------------------------------------------------------ 738f3be7a3SJonas Devlieghere FileSpec::FileSpec(llvm::StringRef path, Style style) : m_style(style) { 748f3be7a3SJonas Devlieghere SetFile(path, style); 755713a05bSZachary Turner } 765713a05bSZachary Turner 778f3be7a3SJonas Devlieghere FileSpec::FileSpec(llvm::StringRef path, const llvm::Triple &Triple) 788f3be7a3SJonas Devlieghere : FileSpec{path, Triple.isOSWindows() ? Style::windows : Style::posix} {} 795713a05bSZachary Turner 805713a05bSZachary Turner //------------------------------------------------------------------ 815713a05bSZachary Turner // Copy constructor 825713a05bSZachary Turner //------------------------------------------------------------------ 835713a05bSZachary Turner FileSpec::FileSpec(const FileSpec &rhs) 845713a05bSZachary Turner : m_directory(rhs.m_directory), m_filename(rhs.m_filename), 852cb7cf8eSPavel Labath m_is_resolved(rhs.m_is_resolved), m_style(rhs.m_style) {} 865713a05bSZachary Turner 875713a05bSZachary Turner //------------------------------------------------------------------ 885713a05bSZachary Turner // Copy constructor 895713a05bSZachary Turner //------------------------------------------------------------------ 905713a05bSZachary Turner FileSpec::FileSpec(const FileSpec *rhs) : m_directory(), m_filename() { 915713a05bSZachary Turner if (rhs) 925713a05bSZachary Turner *this = *rhs; 935713a05bSZachary Turner } 945713a05bSZachary Turner 955713a05bSZachary Turner //------------------------------------------------------------------ 965713a05bSZachary Turner // Virtual destructor in case anyone inherits from this class. 975713a05bSZachary Turner //------------------------------------------------------------------ 985713a05bSZachary Turner FileSpec::~FileSpec() {} 995713a05bSZachary Turner 100c1cc3173SJonas Devlieghere namespace { 101c1cc3173SJonas Devlieghere //------------------------------------------------------------------ 102c1cc3173SJonas Devlieghere /// Safely get a character at the specified index. 103c1cc3173SJonas Devlieghere /// 104*f05b42e9SAdrian Prantl /// \param[in] path 105c1cc3173SJonas Devlieghere /// A full, partial, or relative path to a file. 106c1cc3173SJonas Devlieghere /// 107*f05b42e9SAdrian Prantl /// \param[in] i 108c1cc3173SJonas Devlieghere /// An index into path which may or may not be valid. 109c1cc3173SJonas Devlieghere /// 110*f05b42e9SAdrian Prantl /// \return 111c1cc3173SJonas Devlieghere /// The character at index \a i if the index is valid, or 0 if 112c1cc3173SJonas Devlieghere /// the index is not valid. 113c1cc3173SJonas Devlieghere //------------------------------------------------------------------ 114c1cc3173SJonas Devlieghere inline char safeCharAtIndex(const llvm::StringRef &path, size_t i) { 115c1cc3173SJonas Devlieghere if (i < path.size()) 116c1cc3173SJonas Devlieghere return path[i]; 117c1cc3173SJonas Devlieghere return 0; 118c1cc3173SJonas Devlieghere } 119c1cc3173SJonas Devlieghere 120c1cc3173SJonas Devlieghere //------------------------------------------------------------------ 121c1cc3173SJonas Devlieghere /// Check if a path needs to be normalized. 122c1cc3173SJonas Devlieghere /// 123c1cc3173SJonas Devlieghere /// Check if a path needs to be normalized. We currently consider a 124c1cc3173SJonas Devlieghere /// path to need normalization if any of the following are true 125c1cc3173SJonas Devlieghere /// - path contains "/./" 126c1cc3173SJonas Devlieghere /// - path contains "/../" 127c1cc3173SJonas Devlieghere /// - path contains "//" 128c1cc3173SJonas Devlieghere /// - path ends with "/" 129c1cc3173SJonas Devlieghere /// Paths that start with "./" or with "../" are not considered to 130c1cc3173SJonas Devlieghere /// need normalization since we aren't trying to resolve the path, 131c1cc3173SJonas Devlieghere /// we are just trying to remove redundant things from the path. 132c1cc3173SJonas Devlieghere /// 133*f05b42e9SAdrian Prantl /// \param[in] path 134c1cc3173SJonas Devlieghere /// A full, partial, or relative path to a file. 135c1cc3173SJonas Devlieghere /// 136*f05b42e9SAdrian Prantl /// \return 137c1cc3173SJonas Devlieghere /// Returns \b true if the path needs to be normalized. 138c1cc3173SJonas Devlieghere //------------------------------------------------------------------ 139c1cc3173SJonas Devlieghere bool needsNormalization(const llvm::StringRef &path) { 140c1cc3173SJonas Devlieghere if (path.empty()) 141c1cc3173SJonas Devlieghere return false; 142c1cc3173SJonas Devlieghere // We strip off leading "." values so these paths need to be normalized 143c1cc3173SJonas Devlieghere if (path[0] == '.') 144c1cc3173SJonas Devlieghere return true; 145c1cc3173SJonas Devlieghere for (auto i = path.find_first_of("\\/"); i != llvm::StringRef::npos; 146c1cc3173SJonas Devlieghere i = path.find_first_of("\\/", i + 1)) { 147c1cc3173SJonas Devlieghere const auto next = safeCharAtIndex(path, i+1); 148c1cc3173SJonas Devlieghere switch (next) { 149c1cc3173SJonas Devlieghere case 0: 150c1cc3173SJonas Devlieghere // path separator char at the end of the string which should be 151c1cc3173SJonas Devlieghere // stripped unless it is the one and only character 152c1cc3173SJonas Devlieghere return i > 0; 153c1cc3173SJonas Devlieghere case '/': 154c1cc3173SJonas Devlieghere case '\\': 155c1cc3173SJonas Devlieghere // two path separator chars in the middle of a path needs to be 156c1cc3173SJonas Devlieghere // normalized 157c1cc3173SJonas Devlieghere if (i > 0) 158c1cc3173SJonas Devlieghere return true; 159c1cc3173SJonas Devlieghere ++i; 160c1cc3173SJonas Devlieghere break; 161c1cc3173SJonas Devlieghere 162c1cc3173SJonas Devlieghere case '.': { 163c1cc3173SJonas Devlieghere const auto next_next = safeCharAtIndex(path, i+2); 164c1cc3173SJonas Devlieghere switch (next_next) { 165c1cc3173SJonas Devlieghere default: break; 166c1cc3173SJonas Devlieghere case 0: return true; // ends with "/." 167c1cc3173SJonas Devlieghere case '/': 168c1cc3173SJonas Devlieghere case '\\': 169c1cc3173SJonas Devlieghere return true; // contains "/./" 170c1cc3173SJonas Devlieghere case '.': { 171c1cc3173SJonas Devlieghere const auto next_next_next = safeCharAtIndex(path, i+3); 172c1cc3173SJonas Devlieghere switch (next_next_next) { 173c1cc3173SJonas Devlieghere default: break; 174c1cc3173SJonas Devlieghere case 0: return true; // ends with "/.." 175c1cc3173SJonas Devlieghere case '/': 176c1cc3173SJonas Devlieghere case '\\': 177c1cc3173SJonas Devlieghere return true; // contains "/../" 178c1cc3173SJonas Devlieghere } 179c1cc3173SJonas Devlieghere break; 180c1cc3173SJonas Devlieghere } 181c1cc3173SJonas Devlieghere } 182c1cc3173SJonas Devlieghere } 183c1cc3173SJonas Devlieghere break; 184c1cc3173SJonas Devlieghere 185c1cc3173SJonas Devlieghere default: 186c1cc3173SJonas Devlieghere break; 187c1cc3173SJonas Devlieghere } 188c1cc3173SJonas Devlieghere } 189c1cc3173SJonas Devlieghere return false; 190c1cc3173SJonas Devlieghere } 191c1cc3173SJonas Devlieghere 192c1cc3173SJonas Devlieghere 193c1cc3173SJonas Devlieghere } 1945713a05bSZachary Turner //------------------------------------------------------------------ 1955713a05bSZachary Turner // Assignment operator. 1965713a05bSZachary Turner //------------------------------------------------------------------ 1975713a05bSZachary Turner const FileSpec &FileSpec::operator=(const FileSpec &rhs) { 1985713a05bSZachary Turner if (this != &rhs) { 1995713a05bSZachary Turner m_directory = rhs.m_directory; 2005713a05bSZachary Turner m_filename = rhs.m_filename; 2015713a05bSZachary Turner m_is_resolved = rhs.m_is_resolved; 2022cb7cf8eSPavel Labath m_style = rhs.m_style; 2035713a05bSZachary Turner } 2045713a05bSZachary Turner return *this; 2055713a05bSZachary Turner } 2065713a05bSZachary Turner 2078f3be7a3SJonas Devlieghere void FileSpec::SetFile(llvm::StringRef pathname) { SetFile(pathname, m_style); } 208937348cdSJonas Devlieghere 2095713a05bSZachary Turner //------------------------------------------------------------------ 21005097246SAdrian Prantl // Update the contents of this object with a new path. The path will be split 21105097246SAdrian Prantl // up into a directory and filename and stored as uniqued string values for 21205097246SAdrian Prantl // quick comparison and efficient memory usage. 2135713a05bSZachary Turner //------------------------------------------------------------------ 2148f3be7a3SJonas Devlieghere void FileSpec::SetFile(llvm::StringRef pathname, Style style) { 2155713a05bSZachary Turner m_filename.Clear(); 2165713a05bSZachary Turner m_directory.Clear(); 2175713a05bSZachary Turner m_is_resolved = false; 2182cb7cf8eSPavel Labath m_style = (style == Style::native) ? GetNativeStyle() : style; 2195713a05bSZachary Turner 2205713a05bSZachary Turner if (pathname.empty()) 2215713a05bSZachary Turner return; 2225713a05bSZachary Turner 2238f3be7a3SJonas Devlieghere llvm::SmallString<128> resolved(pathname); 2245713a05bSZachary Turner 225776cd7adSGreg Clayton // Normalize the path by removing ".", ".." and other redundant components. 226c1cc3173SJonas Devlieghere if (needsNormalization(resolved)) 2272cb7cf8eSPavel Labath llvm::sys::path::remove_dots(resolved, true, m_style); 228776cd7adSGreg Clayton 229776cd7adSGreg Clayton // Normalize back slashes to forward slashes 2302cb7cf8eSPavel Labath if (m_style == Style::windows) 231776cd7adSGreg Clayton std::replace(resolved.begin(), resolved.end(), '\\', '/'); 2325713a05bSZachary Turner 23339d50b72SGreg Clayton if (resolved.empty()) { 23439d50b72SGreg Clayton // If we have no path after normalization set the path to the current 23539d50b72SGreg Clayton // directory. This matches what python does and also a few other path 23639d50b72SGreg Clayton // utilities. 23739d50b72SGreg Clayton m_filename.SetString("."); 23839d50b72SGreg Clayton return; 23939d50b72SGreg Clayton } 24039d50b72SGreg Clayton 241ad8d48f9SJonas Devlieghere // Split path into filename and directory. We rely on the underlying char 242ad8d48f9SJonas Devlieghere // pointer to be nullptr when the components are empty. 243ad8d48f9SJonas Devlieghere llvm::StringRef filename = llvm::sys::path::filename(resolved, m_style); 244ad8d48f9SJonas Devlieghere if(!filename.empty()) 245ad8d48f9SJonas Devlieghere m_filename.SetString(filename); 2468f3be7a3SJonas Devlieghere 247ad8d48f9SJonas Devlieghere llvm::StringRef directory = llvm::sys::path::parent_path(resolved, m_style); 248ad8d48f9SJonas Devlieghere if(!directory.empty()) 249ad8d48f9SJonas Devlieghere m_directory.SetString(directory); 2505713a05bSZachary Turner } 2515713a05bSZachary Turner 2528f3be7a3SJonas Devlieghere void FileSpec::SetFile(llvm::StringRef path, const llvm::Triple &Triple) { 2538f3be7a3SJonas Devlieghere return SetFile(path, Triple.isOSWindows() ? Style::windows : Style::posix); 2545713a05bSZachary Turner } 2555713a05bSZachary Turner 2565713a05bSZachary Turner //---------------------------------------------------------------------- 25705097246SAdrian Prantl // Convert to pointer operator. This allows code to check any FileSpec objects 25805097246SAdrian Prantl // to see if they contain anything valid using code such as: 2595713a05bSZachary Turner // 2605713a05bSZachary Turner // if (file_spec) 2615713a05bSZachary Turner // {} 2625713a05bSZachary Turner //---------------------------------------------------------------------- 2635713a05bSZachary Turner FileSpec::operator bool() const { return m_filename || m_directory; } 2645713a05bSZachary Turner 2655713a05bSZachary Turner //---------------------------------------------------------------------- 26605097246SAdrian Prantl // Logical NOT operator. This allows code to check any FileSpec objects to see 26705097246SAdrian Prantl // if they are invalid using code such as: 2685713a05bSZachary Turner // 2695713a05bSZachary Turner // if (!file_spec) 2705713a05bSZachary Turner // {} 2715713a05bSZachary Turner //---------------------------------------------------------------------- 2725713a05bSZachary Turner bool FileSpec::operator!() const { return !m_directory && !m_filename; } 2735713a05bSZachary Turner 2745713a05bSZachary Turner bool FileSpec::DirectoryEquals(const FileSpec &rhs) const { 2755713a05bSZachary Turner const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive(); 2765713a05bSZachary Turner return ConstString::Equals(m_directory, rhs.m_directory, case_sensitive); 2775713a05bSZachary Turner } 2785713a05bSZachary Turner 2795713a05bSZachary Turner bool FileSpec::FileEquals(const FileSpec &rhs) const { 2805713a05bSZachary Turner const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive(); 2815713a05bSZachary Turner return ConstString::Equals(m_filename, rhs.m_filename, case_sensitive); 2825713a05bSZachary Turner } 2835713a05bSZachary Turner 2845713a05bSZachary Turner //------------------------------------------------------------------ 2855713a05bSZachary Turner // Equal to operator 2865713a05bSZachary Turner //------------------------------------------------------------------ 2875713a05bSZachary Turner bool FileSpec::operator==(const FileSpec &rhs) const { 2888f3be7a3SJonas Devlieghere return FileEquals(rhs) && DirectoryEquals(rhs); 2895713a05bSZachary Turner } 2905713a05bSZachary Turner 2915713a05bSZachary Turner //------------------------------------------------------------------ 2925713a05bSZachary Turner // Not equal to operator 2935713a05bSZachary Turner //------------------------------------------------------------------ 2945713a05bSZachary Turner bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); } 2955713a05bSZachary Turner 2965713a05bSZachary Turner //------------------------------------------------------------------ 2975713a05bSZachary Turner // Less than operator 2985713a05bSZachary Turner //------------------------------------------------------------------ 2995713a05bSZachary Turner bool FileSpec::operator<(const FileSpec &rhs) const { 3005713a05bSZachary Turner return FileSpec::Compare(*this, rhs, true) < 0; 3015713a05bSZachary Turner } 3025713a05bSZachary Turner 3035713a05bSZachary Turner //------------------------------------------------------------------ 3045713a05bSZachary Turner // Dump a FileSpec object to a stream 3055713a05bSZachary Turner //------------------------------------------------------------------ 3065713a05bSZachary Turner Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) { 3075713a05bSZachary Turner f.Dump(&s); 3085713a05bSZachary Turner return s; 3095713a05bSZachary Turner } 3105713a05bSZachary Turner 3115713a05bSZachary Turner //------------------------------------------------------------------ 31205097246SAdrian Prantl // Clear this object by releasing both the directory and filename string values 31305097246SAdrian Prantl // and making them both the empty string. 3145713a05bSZachary Turner //------------------------------------------------------------------ 3155713a05bSZachary Turner void FileSpec::Clear() { 3165713a05bSZachary Turner m_directory.Clear(); 3175713a05bSZachary Turner m_filename.Clear(); 3185713a05bSZachary Turner } 3195713a05bSZachary Turner 3205713a05bSZachary Turner //------------------------------------------------------------------ 32105097246SAdrian Prantl // Compare two FileSpec objects. If "full" is true, then both the directory and 32205097246SAdrian Prantl // the filename must match. If "full" is false, then the directory names for 32305097246SAdrian Prantl // "a" and "b" are only compared if they are both non-empty. This allows a 32405097246SAdrian Prantl // FileSpec object to only contain a filename and it can match FileSpec objects 32505097246SAdrian Prantl // that have matching filenames with different paths. 3265713a05bSZachary Turner // 32705097246SAdrian Prantl // Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" and "1" if 32805097246SAdrian Prantl // "a" is greater than "b". 3295713a05bSZachary Turner //------------------------------------------------------------------ 3305713a05bSZachary Turner int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) { 3315713a05bSZachary Turner int result = 0; 3325713a05bSZachary Turner 3335713a05bSZachary Turner // case sensitivity of compare 3345713a05bSZachary Turner const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive(); 3355713a05bSZachary Turner 3365713a05bSZachary Turner // If full is true, then we must compare both the directory and filename. 3375713a05bSZachary Turner 33805097246SAdrian Prantl // If full is false, then if either directory is empty, then we match on the 33905097246SAdrian Prantl // basename only, and if both directories have valid values, we still do a 34005097246SAdrian Prantl // full compare. This allows for matching when we just have a filename in one 34105097246SAdrian Prantl // of the FileSpec objects. 3425713a05bSZachary Turner 3435713a05bSZachary Turner if (full || (a.m_directory && b.m_directory)) { 3445713a05bSZachary Turner result = ConstString::Compare(a.m_directory, b.m_directory, case_sensitive); 3455713a05bSZachary Turner if (result) 3465713a05bSZachary Turner return result; 3475713a05bSZachary Turner } 3485713a05bSZachary Turner return ConstString::Compare(a.m_filename, b.m_filename, case_sensitive); 3495713a05bSZachary Turner } 3505713a05bSZachary Turner 351776cd7adSGreg Clayton bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full) { 3525713a05bSZachary Turner // case sensitivity of equality test 3535713a05bSZachary Turner const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive(); 3545713a05bSZachary Turner 355c1cc3173SJonas Devlieghere const bool filenames_equal = ConstString::Equals(a.m_filename, 356c1cc3173SJonas Devlieghere b.m_filename, 357c1cc3173SJonas Devlieghere case_sensitive); 35897e4f472SJim Ingham 359776cd7adSGreg Clayton if (!filenames_equal) 36097e4f472SJim Ingham return false; 36197e4f472SJim Ingham 3625713a05bSZachary Turner if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty())) 36397e4f472SJim Ingham return filenames_equal; 3645713a05bSZachary Turner 3655713a05bSZachary Turner return a == b; 3665713a05bSZachary Turner } 3675713a05bSZachary Turner 368841bea93SPavel Labath llvm::Optional<FileSpec::Style> FileSpec::GuessPathStyle(llvm::StringRef absolute_path) { 369841bea93SPavel Labath if (absolute_path.startswith("/")) 370841bea93SPavel Labath return Style::posix; 371841bea93SPavel Labath if (absolute_path.startswith(R"(\\)")) 372841bea93SPavel Labath return Style::windows; 373841bea93SPavel Labath if (absolute_path.size() > 3 && llvm::isAlpha(absolute_path[0]) && 374841bea93SPavel Labath absolute_path.substr(1, 2) == R"(:\)") 375841bea93SPavel Labath return Style::windows; 376841bea93SPavel Labath return llvm::None; 377841bea93SPavel Labath } 378841bea93SPavel Labath 3795713a05bSZachary Turner //------------------------------------------------------------------ 38005097246SAdrian Prantl // Dump the object to the supplied stream. If the object contains a valid 38105097246SAdrian Prantl // directory name, it will be displayed followed by a directory delimiter, and 38205097246SAdrian Prantl // the filename. 3835713a05bSZachary Turner //------------------------------------------------------------------ 3845713a05bSZachary Turner void FileSpec::Dump(Stream *s) const { 3855713a05bSZachary Turner if (s) { 3865713a05bSZachary Turner std::string path{GetPath(true)}; 3875713a05bSZachary Turner s->PutCString(path); 3882cb7cf8eSPavel Labath char path_separator = GetPreferredPathSeparator(m_style); 3895713a05bSZachary Turner if (!m_filename && !path.empty() && path.back() != path_separator) 3905713a05bSZachary Turner s->PutChar(path_separator); 3915713a05bSZachary Turner } 3925713a05bSZachary Turner } 3935713a05bSZachary Turner 3942cb7cf8eSPavel Labath FileSpec::Style FileSpec::GetPathStyle() const { return m_style; } 3955713a05bSZachary Turner 3965713a05bSZachary Turner //------------------------------------------------------------------ 3975713a05bSZachary Turner // Directory string get accessor. 3985713a05bSZachary Turner //------------------------------------------------------------------ 3995713a05bSZachary Turner ConstString &FileSpec::GetDirectory() { return m_directory; } 4005713a05bSZachary Turner 4015713a05bSZachary Turner //------------------------------------------------------------------ 4025713a05bSZachary Turner // Directory string const get accessor. 4035713a05bSZachary Turner //------------------------------------------------------------------ 4040e4c4821SAdrian Prantl ConstString FileSpec::GetDirectory() const { return m_directory; } 4055713a05bSZachary Turner 4065713a05bSZachary Turner //------------------------------------------------------------------ 4075713a05bSZachary Turner // Filename string get accessor. 4085713a05bSZachary Turner //------------------------------------------------------------------ 4095713a05bSZachary Turner ConstString &FileSpec::GetFilename() { return m_filename; } 4105713a05bSZachary Turner 4115713a05bSZachary Turner //------------------------------------------------------------------ 4125713a05bSZachary Turner // Filename string const get accessor. 4135713a05bSZachary Turner //------------------------------------------------------------------ 4140e4c4821SAdrian Prantl ConstString FileSpec::GetFilename() const { return m_filename; } 4155713a05bSZachary Turner 4165713a05bSZachary Turner //------------------------------------------------------------------ 41705097246SAdrian Prantl // Extract the directory and path into a fixed buffer. This is needed as the 41805097246SAdrian Prantl // directory and path are stored in separate string values. 4195713a05bSZachary Turner //------------------------------------------------------------------ 4205713a05bSZachary Turner size_t FileSpec::GetPath(char *path, size_t path_max_len, 4215713a05bSZachary Turner bool denormalize) const { 4225713a05bSZachary Turner if (!path) 4235713a05bSZachary Turner return 0; 4245713a05bSZachary Turner 4255713a05bSZachary Turner std::string result = GetPath(denormalize); 4265713a05bSZachary Turner ::snprintf(path, path_max_len, "%s", result.c_str()); 4275713a05bSZachary Turner return std::min(path_max_len - 1, result.length()); 4285713a05bSZachary Turner } 4295713a05bSZachary Turner 4305713a05bSZachary Turner std::string FileSpec::GetPath(bool denormalize) const { 4315713a05bSZachary Turner llvm::SmallString<64> result; 4325713a05bSZachary Turner GetPath(result, denormalize); 4335713a05bSZachary Turner return std::string(result.begin(), result.end()); 4345713a05bSZachary Turner } 4355713a05bSZachary Turner 4365713a05bSZachary Turner const char *FileSpec::GetCString(bool denormalize) const { 43765e5e278SJonas Devlieghere return ConstString{GetPath(denormalize)}.AsCString(nullptr); 4385713a05bSZachary Turner } 4395713a05bSZachary Turner 4405713a05bSZachary Turner void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path, 4415713a05bSZachary Turner bool denormalize) const { 4425713a05bSZachary Turner path.append(m_directory.GetStringRef().begin(), 4435713a05bSZachary Turner m_directory.GetStringRef().end()); 444776cd7adSGreg Clayton // Since the path was normalized and all paths use '/' when stored in these 445776cd7adSGreg Clayton // objects, we don't need to look for the actual syntax specific path 446776cd7adSGreg Clayton // separator, we just look for and insert '/'. 447776cd7adSGreg Clayton if (m_directory && m_filename && m_directory.GetStringRef().back() != '/' && 448776cd7adSGreg Clayton m_filename.GetStringRef().back() != '/') 449776cd7adSGreg Clayton path.insert(path.end(), '/'); 4505713a05bSZachary Turner path.append(m_filename.GetStringRef().begin(), 4515713a05bSZachary Turner m_filename.GetStringRef().end()); 4525713a05bSZachary Turner if (denormalize && !path.empty()) 4532cb7cf8eSPavel Labath Denormalize(path, m_style); 4545713a05bSZachary Turner } 4555713a05bSZachary Turner 4565713a05bSZachary Turner ConstString FileSpec::GetFileNameExtension() const { 4579c1a645aSJonas Devlieghere return ConstString( 4589c1a645aSJonas Devlieghere llvm::sys::path::extension(m_filename.GetStringRef(), m_style)); 4595713a05bSZachary Turner } 4605713a05bSZachary Turner 4615713a05bSZachary Turner ConstString FileSpec::GetFileNameStrippingExtension() const { 4629c1a645aSJonas Devlieghere return ConstString(llvm::sys::path::stem(m_filename.GetStringRef(), m_style)); 4635713a05bSZachary Turner } 4645713a05bSZachary Turner 4655713a05bSZachary Turner //------------------------------------------------------------------ 46605097246SAdrian Prantl // Return the size in bytes that this object takes in memory. This returns the 46705097246SAdrian Prantl // size in bytes of this object, not any shared string values it may refer to. 4685713a05bSZachary Turner //------------------------------------------------------------------ 4695713a05bSZachary Turner size_t FileSpec::MemorySize() const { 4705713a05bSZachary Turner return m_filename.MemorySize() + m_directory.MemorySize(); 4715713a05bSZachary Turner } 4725713a05bSZachary Turner 4735713a05bSZachary Turner FileSpec 4745713a05bSZachary Turner FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const { 4755713a05bSZachary Turner FileSpec ret = *this; 4765713a05bSZachary Turner ret.AppendPathComponent(component); 4775713a05bSZachary Turner return ret; 4785713a05bSZachary Turner } 4795713a05bSZachary Turner 4805713a05bSZachary Turner FileSpec FileSpec::CopyByRemovingLastPathComponent() const { 48124bd63c4SJonas Devlieghere llvm::SmallString<64> current_path; 48224bd63c4SJonas Devlieghere GetPath(current_path, false); 48324bd63c4SJonas Devlieghere if (llvm::sys::path::has_parent_path(current_path, m_style)) 4848f3be7a3SJonas Devlieghere return FileSpec(llvm::sys::path::parent_path(current_path, m_style), 48524bd63c4SJonas Devlieghere m_style); 48624bd63c4SJonas Devlieghere return *this; 4875713a05bSZachary Turner } 4885713a05bSZachary Turner 4895713a05bSZachary Turner ConstString FileSpec::GetLastPathComponent() const { 49024bd63c4SJonas Devlieghere llvm::SmallString<64> current_path; 49124bd63c4SJonas Devlieghere GetPath(current_path, false); 49224bd63c4SJonas Devlieghere return ConstString(llvm::sys::path::filename(current_path, m_style)); 4935713a05bSZachary Turner } 4945713a05bSZachary Turner 4955713a05bSZachary Turner void FileSpec::PrependPathComponent(llvm::StringRef component) { 49624bd63c4SJonas Devlieghere llvm::SmallString<64> new_path(component); 49724bd63c4SJonas Devlieghere llvm::SmallString<64> current_path; 49824bd63c4SJonas Devlieghere GetPath(current_path, false); 49924bd63c4SJonas Devlieghere llvm::sys::path::append(new_path, 50024bd63c4SJonas Devlieghere llvm::sys::path::begin(current_path, m_style), 50124bd63c4SJonas Devlieghere llvm::sys::path::end(current_path), m_style); 5028f3be7a3SJonas Devlieghere SetFile(new_path, m_style); 5035713a05bSZachary Turner } 5045713a05bSZachary Turner 5055713a05bSZachary Turner void FileSpec::PrependPathComponent(const FileSpec &new_path) { 5065713a05bSZachary Turner return PrependPathComponent(new_path.GetPath(false)); 5075713a05bSZachary Turner } 5085713a05bSZachary Turner 5095713a05bSZachary Turner void FileSpec::AppendPathComponent(llvm::StringRef component) { 51024bd63c4SJonas Devlieghere llvm::SmallString<64> current_path; 51124bd63c4SJonas Devlieghere GetPath(current_path, false); 51224bd63c4SJonas Devlieghere llvm::sys::path::append(current_path, m_style, component); 5138f3be7a3SJonas Devlieghere SetFile(current_path, m_style); 5145713a05bSZachary Turner } 5155713a05bSZachary Turner 5165713a05bSZachary Turner void FileSpec::AppendPathComponent(const FileSpec &new_path) { 5175713a05bSZachary Turner return AppendPathComponent(new_path.GetPath(false)); 5185713a05bSZachary Turner } 5195713a05bSZachary Turner 520df8e291eSJonas Devlieghere bool FileSpec::RemoveLastPathComponent() { 521df8e291eSJonas Devlieghere llvm::SmallString<64> current_path; 522df8e291eSJonas Devlieghere GetPath(current_path, false); 523df8e291eSJonas Devlieghere if (llvm::sys::path::has_parent_path(current_path, m_style)) { 5248f3be7a3SJonas Devlieghere SetFile(llvm::sys::path::parent_path(current_path, m_style)); 525df8e291eSJonas Devlieghere return true; 5265713a05bSZachary Turner } 527df8e291eSJonas Devlieghere return false; 5285713a05bSZachary Turner } 5295713a05bSZachary Turner //------------------------------------------------------------------ 5305713a05bSZachary Turner /// Returns true if the filespec represents an implementation source 5315713a05bSZachary Turner /// file (files with a ".c", ".cpp", ".m", ".mm" (many more) 5325713a05bSZachary Turner /// extension). 5335713a05bSZachary Turner /// 534*f05b42e9SAdrian Prantl /// \return 5355713a05bSZachary Turner /// \b true if the filespec represents an implementation source 5365713a05bSZachary Turner /// file, \b false otherwise. 5375713a05bSZachary Turner //------------------------------------------------------------------ 5385713a05bSZachary Turner bool FileSpec::IsSourceImplementationFile() const { 5395713a05bSZachary Turner ConstString extension(GetFileNameExtension()); 5405713a05bSZachary Turner if (!extension) 5415713a05bSZachary Turner return false; 5425713a05bSZachary Turner 5435713a05bSZachary Turner static RegularExpression g_source_file_regex(llvm::StringRef( 544ad8d48f9SJonas Devlieghere "^.([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|[" 5455713a05bSZachary Turner "cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO][" 5465713a05bSZachary Turner "rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])" 5475713a05bSZachary Turner "$")); 5485713a05bSZachary Turner return g_source_file_regex.Execute(extension.GetStringRef()); 5495713a05bSZachary Turner } 5505713a05bSZachary Turner 551c1cc3173SJonas Devlieghere bool FileSpec::IsRelative() const { 552c1cc3173SJonas Devlieghere return !IsAbsolute(); 553c1cc3173SJonas Devlieghere } 5545713a05bSZachary Turner 555ad8d48f9SJonas Devlieghere bool FileSpec::IsAbsolute() const { 556ad8d48f9SJonas Devlieghere llvm::SmallString<64> current_path; 557ad8d48f9SJonas Devlieghere GetPath(current_path, false); 558ad8d48f9SJonas Devlieghere 559ad8d48f9SJonas Devlieghere // Early return if the path is empty. 560ad8d48f9SJonas Devlieghere if (current_path.empty()) 561ad8d48f9SJonas Devlieghere return false; 562ad8d48f9SJonas Devlieghere 563ad8d48f9SJonas Devlieghere // We consider paths starting with ~ to be absolute. 564ad8d48f9SJonas Devlieghere if (current_path[0] == '~') 565ad8d48f9SJonas Devlieghere return true; 566ad8d48f9SJonas Devlieghere 567ad8d48f9SJonas Devlieghere return llvm::sys::path::is_absolute(current_path, m_style); 568ad8d48f9SJonas Devlieghere } 5695713a05bSZachary Turner 5707d36d723SPavel Labath void FileSpec::MakeAbsolute(const FileSpec &dir) { 5717d36d723SPavel Labath if (IsRelative()) 5727d36d723SPavel Labath PrependPathComponent(dir); 5737d36d723SPavel Labath } 5747d36d723SPavel Labath 5755713a05bSZachary Turner void llvm::format_provider<FileSpec>::format(const FileSpec &F, 5765713a05bSZachary Turner raw_ostream &Stream, 5775713a05bSZachary Turner StringRef Style) { 5785713a05bSZachary Turner assert( 5795713a05bSZachary Turner (Style.empty() || Style.equals_lower("F") || Style.equals_lower("D")) && 5805713a05bSZachary Turner "Invalid FileSpec style!"); 5815713a05bSZachary Turner 5825713a05bSZachary Turner StringRef dir = F.GetDirectory().GetStringRef(); 5835713a05bSZachary Turner StringRef file = F.GetFilename().GetStringRef(); 5845713a05bSZachary Turner 5855713a05bSZachary Turner if (dir.empty() && file.empty()) { 5865713a05bSZachary Turner Stream << "(empty)"; 5875713a05bSZachary Turner return; 5885713a05bSZachary Turner } 5895713a05bSZachary Turner 5905713a05bSZachary Turner if (Style.equals_lower("F")) { 5915713a05bSZachary Turner Stream << (file.empty() ? "(empty)" : file); 5925713a05bSZachary Turner return; 5935713a05bSZachary Turner } 5945713a05bSZachary Turner 5955713a05bSZachary Turner // Style is either D or empty, either way we need to print the directory. 5965713a05bSZachary Turner if (!dir.empty()) { 59705097246SAdrian Prantl // Directory is stored in normalized form, which might be different than 59805097246SAdrian Prantl // preferred form. In order to handle this, we need to cut off the 59905097246SAdrian Prantl // filename, then denormalize, then write the entire denorm'ed directory. 6005713a05bSZachary Turner llvm::SmallString<64> denormalized_dir = dir; 6012cb7cf8eSPavel Labath Denormalize(denormalized_dir, F.GetPathStyle()); 6025713a05bSZachary Turner Stream << denormalized_dir; 6032cb7cf8eSPavel Labath Stream << GetPreferredPathSeparator(F.GetPathStyle()); 6045713a05bSZachary Turner } 6055713a05bSZachary Turner 6065713a05bSZachary Turner if (Style.equals_lower("D")) { 6075713a05bSZachary Turner // We only want to print the directory, so now just exit. 6085713a05bSZachary Turner if (dir.empty()) 6095713a05bSZachary Turner Stream << "(empty)"; 6105713a05bSZachary Turner return; 6115713a05bSZachary Turner } 6125713a05bSZachary Turner 6135713a05bSZachary Turner if (!file.empty()) 6145713a05bSZachary Turner Stream << file; 6155713a05bSZachary Turner } 616