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
GetNativeStyle()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
PathStyleIsPosix(FileSpec::Style style)452cb7cf8eSPavel Labath bool PathStyleIsPosix(FileSpec::Style style) {
46a2c9cf4cSMartin Storsjö return llvm::sys::path::is_style_posix(style);
475713a05bSZachary Turner }
485713a05bSZachary Turner
GetPathSeparators(FileSpec::Style style)492cb7cf8eSPavel Labath const char *GetPathSeparators(FileSpec::Style style) {
50ad8d48f9SJonas Devlieghere return llvm::sys::path::get_separator(style).data();
515713a05bSZachary Turner }
525713a05bSZachary Turner
GetPreferredPathSeparator(FileSpec::Style style)532cb7cf8eSPavel Labath char GetPreferredPathSeparator(FileSpec::Style style) {
542cb7cf8eSPavel Labath return GetPathSeparators(style)[0];
555713a05bSZachary Turner }
565713a05bSZachary Turner
Denormalize(llvm::SmallVectorImpl<char> & path,FileSpec::Style style)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
FileSpec()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.
FileSpec(llvm::StringRef path,Style style)698f3be7a3SJonas Devlieghere FileSpec::FileSpec(llvm::StringRef path, Style style) : m_style(style) {
708f3be7a3SJonas Devlieghere SetFile(path, style);
715713a05bSZachary Turner }
725713a05bSZachary Turner
FileSpec(llvm::StringRef path,const llvm::Triple & triple)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.
safeCharAtIndex(const llvm::StringRef & path,size_t i)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.
needsNormalization(const llvm::StringRef & path)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
SetFile(llvm::StringRef pathname)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.
SetFile(llvm::StringRef pathname,Style style)1728f3be7a3SJonas Devlieghere void FileSpec::SetFile(llvm::StringRef pathname, Style style) {
173*1b4b12a3SNico Weber m_filename.Clear();
174*1b4b12a3SNico Weber m_directory.Clear();
175*1b4b12a3SNico Weber 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
SetFile(llvm::StringRef path,const llvm::Triple & triple)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 // {}
operator bool() const2195713a05bSZachary 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 // {}
operator !() const2265713a05bSZachary Turner bool FileSpec::operator!() const { return !m_directory && !m_filename; }
2275713a05bSZachary Turner
DirectoryEquals(const FileSpec & rhs) const2285713a05bSZachary 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
FileEquals(const FileSpec & rhs) const2335713a05bSZachary 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
operator ==(const FileSpec & rhs) const2395713a05bSZachary 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
operator !=(const FileSpec & rhs) const2445713a05bSZachary Turner bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); }
2455713a05bSZachary Turner
2465713a05bSZachary Turner // Less than operator
operator <(const FileSpec & rhs) const2475713a05bSZachary 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
operator <<(Stream & s,const FileSpec & f)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.
Clear()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".
Compare(const FileSpec & a,const FileSpec & b,bool full)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
Equal(const FileSpec & a,const FileSpec & b,bool full)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
Match(const FileSpec & pattern,const FileSpec & file)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
GuessPathStyle(llvm::StringRef absolute_path)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;
313f72ae5cbSJaroslav Sevcik if (absolute_path.size() >= 3 && llvm::isAlpha(absolute_path[0]) &&
314b548f584SMartin Storsjö (absolute_path.substr(1, 2) == R"(:\)" ||
315b548f584SMartin Storsjö absolute_path.substr(1, 2) == R"(:/)"))
316841bea93SPavel Labath return Style::windows;
317841bea93SPavel Labath return llvm::None;
318841bea93SPavel Labath }
319841bea93SPavel Labath
32005097246SAdrian Prantl // Dump the object to the supplied stream. If the object contains a valid
32105097246SAdrian Prantl // directory name, it will be displayed followed by a directory delimiter, and
32205097246SAdrian Prantl // the filename.
Dump(llvm::raw_ostream & s) const3234dac97ebSRaphael Isemann void FileSpec::Dump(llvm::raw_ostream &s) const {
3245713a05bSZachary Turner std::string path{GetPath(true)};
3254dac97ebSRaphael Isemann s << path;
3262cb7cf8eSPavel Labath char path_separator = GetPreferredPathSeparator(m_style);
3275713a05bSZachary Turner if (!m_filename && !path.empty() && path.back() != path_separator)
3284dac97ebSRaphael Isemann s << path_separator;
3295713a05bSZachary Turner }
3305713a05bSZachary Turner
GetPathStyle() const3312cb7cf8eSPavel Labath FileSpec::Style FileSpec::GetPathStyle() const { return m_style; }
3325713a05bSZachary Turner
333*1b4b12a3SNico Weber // Directory string get accessor.
GetDirectory()334*1b4b12a3SNico Weber ConstString &FileSpec::GetDirectory() { return m_directory; }
3355713a05bSZachary Turner
336*1b4b12a3SNico Weber // Directory string const get accessor.
GetDirectory() const337*1b4b12a3SNico Weber ConstString FileSpec::GetDirectory() const { return m_directory; }
3385713a05bSZachary Turner
339*1b4b12a3SNico Weber // Filename string get accessor.
GetFilename()340*1b4b12a3SNico Weber ConstString &FileSpec::GetFilename() { return m_filename; }
3415713a05bSZachary Turner
342*1b4b12a3SNico Weber // Filename string const get accessor.
GetFilename() const343*1b4b12a3SNico Weber ConstString FileSpec::GetFilename() const { return m_filename; }
3445713a05bSZachary Turner
34505097246SAdrian Prantl // Extract the directory and path into a fixed buffer. This is needed as the
34605097246SAdrian Prantl // directory and path are stored in separate string values.
GetPath(char * path,size_t path_max_len,bool denormalize) const3475713a05bSZachary Turner size_t FileSpec::GetPath(char *path, size_t path_max_len,
3485713a05bSZachary Turner bool denormalize) const {
3495713a05bSZachary Turner if (!path)
3505713a05bSZachary Turner return 0;
3515713a05bSZachary Turner
3525713a05bSZachary Turner std::string result = GetPath(denormalize);
3535713a05bSZachary Turner ::snprintf(path, path_max_len, "%s", result.c_str());
3545713a05bSZachary Turner return std::min(path_max_len - 1, result.length());
3555713a05bSZachary Turner }
3565713a05bSZachary Turner
GetPath(bool denormalize) const3575713a05bSZachary Turner std::string FileSpec::GetPath(bool denormalize) const {
3585713a05bSZachary Turner llvm::SmallString<64> result;
3595713a05bSZachary Turner GetPath(result, denormalize);
3608cc9fa6fSKazu Hirata return static_cast<std::string>(result);
3615713a05bSZachary Turner }
3625713a05bSZachary Turner
GetCString(bool denormalize) const363*1b4b12a3SNico Weber const char *FileSpec::GetCString(bool denormalize) const {
364*1b4b12a3SNico Weber return ConstString{GetPath(denormalize)}.AsCString(nullptr);
3655713a05bSZachary Turner }
3665713a05bSZachary Turner
GetPath(llvm::SmallVectorImpl<char> & path,bool denormalize) const3675713a05bSZachary Turner void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path,
3685713a05bSZachary Turner bool denormalize) const {
3695713a05bSZachary Turner path.append(m_directory.GetStringRef().begin(),
3705713a05bSZachary Turner m_directory.GetStringRef().end());
371776cd7adSGreg Clayton // Since the path was normalized and all paths use '/' when stored in these
372776cd7adSGreg Clayton // objects, we don't need to look for the actual syntax specific path
373776cd7adSGreg Clayton // separator, we just look for and insert '/'.
374776cd7adSGreg Clayton if (m_directory && m_filename && m_directory.GetStringRef().back() != '/' &&
375776cd7adSGreg Clayton m_filename.GetStringRef().back() != '/')
376776cd7adSGreg Clayton path.insert(path.end(), '/');
3775713a05bSZachary Turner path.append(m_filename.GetStringRef().begin(),
3785713a05bSZachary Turner m_filename.GetStringRef().end());
3795713a05bSZachary Turner if (denormalize && !path.empty())
3802cb7cf8eSPavel Labath Denormalize(path, m_style);
3815713a05bSZachary Turner }
3825713a05bSZachary Turner
GetFileNameExtension() const3835713a05bSZachary Turner ConstString FileSpec::GetFileNameExtension() const {
3849c1a645aSJonas Devlieghere return ConstString(
3859c1a645aSJonas Devlieghere llvm::sys::path::extension(m_filename.GetStringRef(), m_style));
3865713a05bSZachary Turner }
3875713a05bSZachary Turner
GetFileNameStrippingExtension() const3885713a05bSZachary Turner ConstString FileSpec::GetFileNameStrippingExtension() const {
3899c1a645aSJonas Devlieghere return ConstString(llvm::sys::path::stem(m_filename.GetStringRef(), m_style));
3905713a05bSZachary Turner }
3915713a05bSZachary Turner
39205097246SAdrian Prantl // Return the size in bytes that this object takes in memory. This returns the
39305097246SAdrian Prantl // size in bytes of this object, not any shared string values it may refer to.
MemorySize() const3945713a05bSZachary Turner size_t FileSpec::MemorySize() const {
3955713a05bSZachary Turner return m_filename.MemorySize() + m_directory.MemorySize();
3965713a05bSZachary Turner }
3975713a05bSZachary Turner
3985713a05bSZachary Turner FileSpec
CopyByAppendingPathComponent(llvm::StringRef component) const3995713a05bSZachary Turner FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const {
4005713a05bSZachary Turner FileSpec ret = *this;
4015713a05bSZachary Turner ret.AppendPathComponent(component);
4025713a05bSZachary Turner return ret;
4035713a05bSZachary Turner }
4045713a05bSZachary Turner
CopyByRemovingLastPathComponent() const4055713a05bSZachary Turner FileSpec FileSpec::CopyByRemovingLastPathComponent() const {
40624bd63c4SJonas Devlieghere llvm::SmallString<64> current_path;
40724bd63c4SJonas Devlieghere GetPath(current_path, false);
40824bd63c4SJonas Devlieghere if (llvm::sys::path::has_parent_path(current_path, m_style))
4098f3be7a3SJonas Devlieghere return FileSpec(llvm::sys::path::parent_path(current_path, m_style),
41024bd63c4SJonas Devlieghere m_style);
41124bd63c4SJonas Devlieghere return *this;
4125713a05bSZachary Turner }
4135713a05bSZachary Turner
GetLastPathComponent() const4145713a05bSZachary Turner ConstString FileSpec::GetLastPathComponent() const {
41524bd63c4SJonas Devlieghere llvm::SmallString<64> current_path;
41624bd63c4SJonas Devlieghere GetPath(current_path, false);
41724bd63c4SJonas Devlieghere return ConstString(llvm::sys::path::filename(current_path, m_style));
4185713a05bSZachary Turner }
4195713a05bSZachary Turner
PrependPathComponent(llvm::StringRef component)4205713a05bSZachary Turner void FileSpec::PrependPathComponent(llvm::StringRef component) {
42124bd63c4SJonas Devlieghere llvm::SmallString<64> new_path(component);
42224bd63c4SJonas Devlieghere llvm::SmallString<64> current_path;
42324bd63c4SJonas Devlieghere GetPath(current_path, false);
42424bd63c4SJonas Devlieghere llvm::sys::path::append(new_path,
42524bd63c4SJonas Devlieghere llvm::sys::path::begin(current_path, m_style),
42624bd63c4SJonas Devlieghere llvm::sys::path::end(current_path), m_style);
4278f3be7a3SJonas Devlieghere SetFile(new_path, m_style);
4285713a05bSZachary Turner }
4295713a05bSZachary Turner
PrependPathComponent(const FileSpec & new_path)4305713a05bSZachary Turner void FileSpec::PrependPathComponent(const FileSpec &new_path) {
4315713a05bSZachary Turner return PrependPathComponent(new_path.GetPath(false));
4325713a05bSZachary Turner }
4335713a05bSZachary Turner
AppendPathComponent(llvm::StringRef component)4345713a05bSZachary Turner void FileSpec::AppendPathComponent(llvm::StringRef component) {
43524bd63c4SJonas Devlieghere llvm::SmallString<64> current_path;
43624bd63c4SJonas Devlieghere GetPath(current_path, false);
43724bd63c4SJonas Devlieghere llvm::sys::path::append(current_path, m_style, component);
4388f3be7a3SJonas Devlieghere SetFile(current_path, m_style);
4395713a05bSZachary Turner }
4405713a05bSZachary Turner
AppendPathComponent(const FileSpec & new_path)4415713a05bSZachary Turner void FileSpec::AppendPathComponent(const FileSpec &new_path) {
4425713a05bSZachary Turner return AppendPathComponent(new_path.GetPath(false));
4435713a05bSZachary Turner }
4445713a05bSZachary Turner
RemoveLastPathComponent()445df8e291eSJonas Devlieghere bool FileSpec::RemoveLastPathComponent() {
446df8e291eSJonas Devlieghere llvm::SmallString<64> current_path;
447df8e291eSJonas Devlieghere GetPath(current_path, false);
448df8e291eSJonas Devlieghere if (llvm::sys::path::has_parent_path(current_path, m_style)) {
4498f3be7a3SJonas Devlieghere SetFile(llvm::sys::path::parent_path(current_path, m_style));
450df8e291eSJonas Devlieghere return true;
4515713a05bSZachary Turner }
452df8e291eSJonas Devlieghere return false;
4535713a05bSZachary Turner }
4545713a05bSZachary Turner /// Returns true if the filespec represents an implementation source
4555713a05bSZachary Turner /// file (files with a ".c", ".cpp", ".m", ".mm" (many more)
4565713a05bSZachary Turner /// extension).
4575713a05bSZachary Turner ///
458f05b42e9SAdrian Prantl /// \return
4595713a05bSZachary Turner /// \b true if the filespec represents an implementation source
4605713a05bSZachary Turner /// file, \b false otherwise.
IsSourceImplementationFile() const4615713a05bSZachary Turner bool FileSpec::IsSourceImplementationFile() const {
4625713a05bSZachary Turner ConstString extension(GetFileNameExtension());
4635713a05bSZachary Turner if (!extension)
4645713a05bSZachary Turner return false;
4655713a05bSZachary Turner
4665713a05bSZachary Turner static RegularExpression g_source_file_regex(llvm::StringRef(
467ad8d48f9SJonas Devlieghere "^.([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|["
4685713a05bSZachary Turner "cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO]["
4695713a05bSZachary Turner "rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])"
4705713a05bSZachary Turner "$"));
4715713a05bSZachary Turner return g_source_file_regex.Execute(extension.GetStringRef());
4725713a05bSZachary Turner }
4735713a05bSZachary Turner
IsRelative() const474c1cc3173SJonas Devlieghere bool FileSpec::IsRelative() const {
475c1cc3173SJonas Devlieghere return !IsAbsolute();
476c1cc3173SJonas Devlieghere }
4775713a05bSZachary Turner
IsAbsolute() const478ad8d48f9SJonas Devlieghere bool FileSpec::IsAbsolute() const {
479*1b4b12a3SNico Weber llvm::SmallString<64> current_path;
480*1b4b12a3SNico Weber GetPath(current_path, false);
481ad8d48f9SJonas Devlieghere
482*1b4b12a3SNico Weber // Early return if the path is empty.
483*1b4b12a3SNico Weber if (current_path.empty())
484*1b4b12a3SNico Weber return false;
485ad8d48f9SJonas Devlieghere
486ad8d48f9SJonas Devlieghere // We consider paths starting with ~ to be absolute.
487*1b4b12a3SNico Weber if (current_path[0] == '~')
488*1b4b12a3SNico Weber return true;
489ad8d48f9SJonas Devlieghere
490*1b4b12a3SNico Weber return llvm::sys::path::is_absolute(current_path, m_style);
491ad8d48f9SJonas Devlieghere }
4925713a05bSZachary Turner
MakeAbsolute(const FileSpec & dir)4937d36d723SPavel Labath void FileSpec::MakeAbsolute(const FileSpec &dir) {
4947d36d723SPavel Labath if (IsRelative())
4957d36d723SPavel Labath PrependPathComponent(dir);
4967d36d723SPavel Labath }
4977d36d723SPavel Labath
format(const FileSpec & F,raw_ostream & Stream,StringRef Style)4985713a05bSZachary Turner void llvm::format_provider<FileSpec>::format(const FileSpec &F,
4995713a05bSZachary Turner raw_ostream &Stream,
5005713a05bSZachary Turner StringRef Style) {
501e50f9c41SMartin Storsjö assert((Style.empty() || Style.equals_insensitive("F") ||
502e50f9c41SMartin Storsjö Style.equals_insensitive("D")) &&
5035713a05bSZachary Turner "Invalid FileSpec style!");
5045713a05bSZachary Turner
5055713a05bSZachary Turner StringRef dir = F.GetDirectory().GetStringRef();
5065713a05bSZachary Turner StringRef file = F.GetFilename().GetStringRef();
5075713a05bSZachary Turner
5085713a05bSZachary Turner if (dir.empty() && file.empty()) {
5095713a05bSZachary Turner Stream << "(empty)";
5105713a05bSZachary Turner return;
5115713a05bSZachary Turner }
5125713a05bSZachary Turner
513e50f9c41SMartin Storsjö if (Style.equals_insensitive("F")) {
5145713a05bSZachary Turner Stream << (file.empty() ? "(empty)" : file);
5155713a05bSZachary Turner return;
5165713a05bSZachary Turner }
5175713a05bSZachary Turner
5185713a05bSZachary Turner // Style is either D or empty, either way we need to print the directory.
5195713a05bSZachary Turner if (!dir.empty()) {
52005097246SAdrian Prantl // Directory is stored in normalized form, which might be different than
52105097246SAdrian Prantl // preferred form. In order to handle this, we need to cut off the
52205097246SAdrian Prantl // filename, then denormalize, then write the entire denorm'ed directory.
5235713a05bSZachary Turner llvm::SmallString<64> denormalized_dir = dir;
5242cb7cf8eSPavel Labath Denormalize(denormalized_dir, F.GetPathStyle());
5255713a05bSZachary Turner Stream << denormalized_dir;
5262cb7cf8eSPavel Labath Stream << GetPreferredPathSeparator(F.GetPathStyle());
5275713a05bSZachary Turner }
5285713a05bSZachary Turner
529e50f9c41SMartin Storsjö if (Style.equals_insensitive("D")) {
5305713a05bSZachary Turner // We only want to print the directory, so now just exit.
5315713a05bSZachary Turner if (dir.empty())
5325713a05bSZachary Turner Stream << "(empty)";
5335713a05bSZachary Turner return;
5345713a05bSZachary Turner }
5355713a05bSZachary Turner
5365713a05bSZachary Turner if (!file.empty())
5375713a05bSZachary Turner Stream << file;
5385713a05bSZachary Turner }
539bc9b6b33SJonas Devlieghere
enumeration(IO & io,FileSpecStyle & value)540bc9b6b33SJonas Devlieghere void llvm::yaml::ScalarEnumerationTraits<FileSpecStyle>::enumeration(
541bc9b6b33SJonas Devlieghere IO &io, FileSpecStyle &value) {
542bc9b6b33SJonas Devlieghere io.enumCase(value, "windows", FileSpecStyle(FileSpec::Style::windows));
543bc9b6b33SJonas Devlieghere io.enumCase(value, "posix", FileSpecStyle(FileSpec::Style::posix));
544bc9b6b33SJonas Devlieghere io.enumCase(value, "native", FileSpecStyle(FileSpec::Style::native));
545bc9b6b33SJonas Devlieghere }
546bc9b6b33SJonas Devlieghere
mapping(IO & io,FileSpec & f)547bc9b6b33SJonas Devlieghere void llvm::yaml::MappingTraits<FileSpec>::mapping(IO &io, FileSpec &f) {
548bc9b6b33SJonas Devlieghere io.mapRequired("directory", f.m_directory);
549bc9b6b33SJonas Devlieghere io.mapRequired("file", f.m_filename);
550bc9b6b33SJonas Devlieghere io.mapRequired("resolved", f.m_is_resolved);
551bc9b6b33SJonas Devlieghere FileSpecStyle style = f.m_style;
552bc9b6b33SJonas Devlieghere io.mapRequired("style", style);
553bc9b6b33SJonas Devlieghere f.m_style = style;
554bc9b6b33SJonas Devlieghere }
555