blob: 1ec5d60e278040758d11ba41ebfeea9fa6e5ab56 [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- FileSpec.cpp ------------------------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Zachary Turner5713a052017-03-22 18:40:07 +00009#include "lldb/Utility/FileSpec.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000010#include "lldb/Utility/RegularExpression.h"
11#include "lldb/Utility/Stream.h"
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000012
Jonas Devliegheread8d48f2018-06-13 16:23:21 +000013#include "llvm/ADT/SmallString.h"
14#include "llvm/ADT/SmallVector.h"
Caroline Tice391a9602010-09-12 00:10:52 +000015#include "llvm/ADT/StringRef.h"
Jonas Devliegheread8d48f2018-06-13 16:23:21 +000016#include "llvm/ADT/Triple.h"
17#include "llvm/ADT/Twine.h"
18#include "llvm/Support/ErrorOr.h"
Zachary Turner3f559742014-08-07 17:33:36 +000019#include "llvm/Support/FileSystem.h"
Greg Clayton38a61402010-12-02 23:20:03 +000020#include "llvm/Support/Program.h"
Jonas Devliegheread8d48f2018-06-13 16:23:21 +000021#include "llvm/Support/raw_ostream.h"
Zachary Turner4479ac12017-04-06 18:12:24 +000022
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000023#include <algorithm>
24#include <system_error>
25#include <vector>
Zachary Turner4479ac12017-04-06 18:12:24 +000026
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000027#include <assert.h>
28#include <limits.h>
29#include <stdio.h>
30#include <string.h>
Caroline Tice391a9602010-09-12 00:10:52 +000031
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032using namespace lldb;
33using namespace lldb_private;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034
Chaoren Lin1c614fe2015-05-28 17:02:45 +000035namespace {
36
Pavel Labath2cb7cf82018-05-14 14:52:47 +000037static constexpr FileSpec::Style GetNativeStyle() {
Nico Weberb1cb0b792018-04-10 13:33:45 +000038#if defined(_WIN32)
Pavel Labath2cb7cf82018-05-14 14:52:47 +000039 return FileSpec::Style::windows;
Zachary Turner7d86ee52017-03-08 17:56:08 +000040#else
Pavel Labath2cb7cf82018-05-14 14:52:47 +000041 return FileSpec::Style::posix;
Zachary Turner7d86ee52017-03-08 17:56:08 +000042#endif
43}
44
Pavel Labath2cb7cf82018-05-14 14:52:47 +000045bool PathStyleIsPosix(FileSpec::Style style) {
46 return (style == FileSpec::Style::posix ||
47 (style == FileSpec::Style::native &&
48 GetNativeStyle() == FileSpec::Style::posix));
Chaoren Lin1c614fe2015-05-28 17:02:45 +000049}
50
Pavel Labath2cb7cf82018-05-14 14:52:47 +000051const char *GetPathSeparators(FileSpec::Style style) {
Jonas Devliegheread8d48f2018-06-13 16:23:21 +000052 return llvm::sys::path::get_separator(style).data();
Pavel Labath144119b2016-04-04 14:39:12 +000053}
54
Pavel Labath2cb7cf82018-05-14 14:52:47 +000055char GetPreferredPathSeparator(FileSpec::Style style) {
56 return GetPathSeparators(style)[0];
Pavel Labath144119b2016-04-04 14:39:12 +000057}
58
Pavel Labath2cb7cf82018-05-14 14:52:47 +000059void Denormalize(llvm::SmallVectorImpl<char> &path, FileSpec::Style style) {
60 if (PathStyleIsPosix(style))
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 return;
Chaoren Lin1c614fe2015-05-28 17:02:45 +000062
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 std::replace(path.begin(), path.end(), '/', '\\');
Chaoren Lin1c614fe2015-05-28 17:02:45 +000064}
Greg Clayton86188d82018-05-21 14:14:36 +000065
Pavel Labath144119b2016-04-04 14:39:12 +000066} // end anonymous namespace
67
Pavel Labath2cb7cf82018-05-14 14:52:47 +000068FileSpec::FileSpec() : m_style(GetNativeStyle()) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069
Adrian Prantl05097242018-04-30 16:49:04 +000070// Default constructor that can take an optional full path to a file on disk.
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +000071FileSpec::FileSpec(llvm::StringRef path, Style style) : m_style(style) {
72 SetFile(path, style);
Jim Ingham0909e5f2010-09-16 00:57:33 +000073}
74
Jonas Devliegherea7d4cec2019-08-15 04:35:46 +000075FileSpec::FileSpec(llvm::StringRef path, const llvm::Triple &triple)
Jonas Devlieghere706cd702019-08-15 05:09:09 +000076 : FileSpec{path, triple.isOSWindows() ? Style::windows : Style::posix} {}
Chaoren Linf34f4102015-05-09 01:21:32 +000077
Jonas Devliegherec1cc3172018-06-25 10:11:53 +000078namespace {
Jonas Devliegherec1cc3172018-06-25 10:11:53 +000079/// Safely get a character at the specified index.
80///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000081/// \param[in] path
Jonas Devliegherec1cc3172018-06-25 10:11:53 +000082/// A full, partial, or relative path to a file.
83///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000084/// \param[in] i
Jonas Devliegherec1cc3172018-06-25 10:11:53 +000085/// An index into path which may or may not be valid.
86///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000087/// \return
Jonas Devliegherec1cc3172018-06-25 10:11:53 +000088/// The character at index \a i if the index is valid, or 0 if
89/// the index is not valid.
Jonas Devliegherec1cc3172018-06-25 10:11:53 +000090inline char safeCharAtIndex(const llvm::StringRef &path, size_t i) {
91 if (i < path.size())
92 return path[i];
93 return 0;
94}
95
Jonas Devliegherec1cc3172018-06-25 10:11:53 +000096/// Check if a path needs to be normalized.
97///
98/// Check if a path needs to be normalized. We currently consider a
99/// path to need normalization if any of the following are true
100/// - path contains "/./"
101/// - path contains "/../"
102/// - path contains "//"
103/// - path ends with "/"
104/// Paths that start with "./" or with "../" are not considered to
105/// need normalization since we aren't trying to resolve the path,
106/// we are just trying to remove redundant things from the path.
107///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000108/// \param[in] path
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000109/// A full, partial, or relative path to a file.
110///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000111/// \return
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000112/// Returns \b true if the path needs to be normalized.
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000113bool needsNormalization(const llvm::StringRef &path) {
114 if (path.empty())
115 return false;
116 // We strip off leading "." values so these paths need to be normalized
117 if (path[0] == '.')
118 return true;
119 for (auto i = path.find_first_of("\\/"); i != llvm::StringRef::npos;
120 i = path.find_first_of("\\/", i + 1)) {
121 const auto next = safeCharAtIndex(path, i+1);
122 switch (next) {
123 case 0:
124 // path separator char at the end of the string which should be
125 // stripped unless it is the one and only character
126 return i > 0;
127 case '/':
128 case '\\':
129 // two path separator chars in the middle of a path needs to be
130 // normalized
131 if (i > 0)
132 return true;
133 ++i;
134 break;
135
136 case '.': {
137 const auto next_next = safeCharAtIndex(path, i+2);
138 switch (next_next) {
139 default: break;
140 case 0: return true; // ends with "/."
141 case '/':
142 case '\\':
143 return true; // contains "/./"
144 case '.': {
145 const auto next_next_next = safeCharAtIndex(path, i+3);
146 switch (next_next_next) {
147 default: break;
148 case 0: return true; // ends with "/.."
149 case '/':
150 case '\\':
151 return true; // contains "/../"
152 }
153 break;
154 }
155 }
156 }
157 break;
158
159 default:
160 break;
161 }
162 }
163 return false;
164}
165
166
167}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000169void FileSpec::SetFile(llvm::StringRef pathname) { SetFile(pathname, m_style); }
Jonas Devlieghere937348c2018-06-13 22:08:14 +0000170
Adrian Prantl05097242018-04-30 16:49:04 +0000171// Update the contents of this object with a new path. The path will be split
172// up into a directory and filename and stored as uniqued string values for
173// quick comparison and efficient memory usage.
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000174void FileSpec::SetFile(llvm::StringRef pathname, Style style) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 m_filename.Clear();
176 m_directory.Clear();
177 m_is_resolved = false;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000178 m_style = (style == Style::native) ? GetNativeStyle() : style;
Zachary Turnerdf62f202014-08-07 17:33:07 +0000179
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000180 if (pathname.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000183 llvm::SmallString<128> resolved(pathname);
Zachary Turnerc7a17ed2014-12-01 23:13:32 +0000184
Greg Clayton776cd7a2018-04-27 15:45:58 +0000185 // Normalize the path by removing ".", ".." and other redundant components.
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000186 if (needsNormalization(resolved))
187 llvm::sys::path::remove_dots(resolved, true, m_style);
Greg Clayton776cd7a2018-04-27 15:45:58 +0000188
189 // Normalize back slashes to forward slashes
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000190 if (m_style == Style::windows)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000191 std::replace(resolved.begin(), resolved.end(), '\\', '/');
Pavel Labatha212c582016-04-14 09:38:06 +0000192
Greg Clayton39d50b72018-05-17 16:12:38 +0000193 if (resolved.empty()) {
194 // If we have no path after normalization set the path to the current
195 // directory. This matches what python does and also a few other path
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000196 // utilities.
Greg Clayton39d50b72018-05-17 16:12:38 +0000197 m_filename.SetString(".");
198 return;
199 }
200
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000201 // Split path into filename and directory. We rely on the underlying char
202 // pointer to be nullptr when the components are empty.
203 llvm::StringRef filename = llvm::sys::path::filename(resolved, m_style);
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000204 if(!filename.empty())
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000205 m_filename.SetString(filename);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000206
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000207 llvm::StringRef directory = llvm::sys::path::parent_path(resolved, m_style);
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000208 if(!directory.empty())
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000209 m_directory.SetString(directory);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210}
211
Jonas Devlieghere706cd702019-08-15 05:09:09 +0000212void FileSpec::SetFile(llvm::StringRef path, const llvm::Triple &triple) {
213 return SetFile(path, triple.isOSWindows() ? Style::windows : Style::posix);
Chaoren Lin44145d72015-05-29 19:52:37 +0000214}
215
Adrian Prantl05097242018-04-30 16:49:04 +0000216// Convert to pointer operator. This allows code to check any FileSpec objects
217// to see if they contain anything valid using code such as:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000218//
219// if (file_spec)
220// {}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221FileSpec::operator bool() const { return m_filename || m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222
Adrian Prantl05097242018-04-30 16:49:04 +0000223// Logical NOT operator. This allows code to check any FileSpec objects to see
224// if they are invalid using code such as:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000225//
226// if (!file_spec)
227// {}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228bool FileSpec::operator!() const { return !m_directory && !m_filename; }
229
230bool FileSpec::DirectoryEquals(const FileSpec &rhs) const {
231 const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
232 return ConstString::Equals(m_directory, rhs.m_directory, case_sensitive);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233}
234
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235bool FileSpec::FileEquals(const FileSpec &rhs) const {
236 const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
237 return ConstString::Equals(m_filename, rhs.m_filename, case_sensitive);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000238}
239
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240// Equal to operator
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241bool FileSpec::operator==(const FileSpec &rhs) const {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000242 return FileEquals(rhs) && DirectoryEquals(rhs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000243}
244
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245// Not equal to operator
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248// Less than operator
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249bool FileSpec::operator<(const FileSpec &rhs) const {
250 return FileSpec::Compare(*this, rhs, true) < 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251}
252
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253// Dump a FileSpec object to a stream
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) {
Raphael Isemann4dac97e2019-12-06 08:38:23 +0100255 f.Dump(s.AsRawOstream());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256 return s;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257}
258
Adrian Prantl05097242018-04-30 16:49:04 +0000259// Clear this object by releasing both the directory and filename string values
260// and making them both the empty string.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261void FileSpec::Clear() {
262 m_directory.Clear();
263 m_filename.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264}
265
Adrian Prantl05097242018-04-30 16:49:04 +0000266// Compare two FileSpec objects. If "full" is true, then both the directory and
267// the filename must match. If "full" is false, then the directory names for
268// "a" and "b" are only compared if they are both non-empty. This allows a
269// FileSpec object to only contain a filename and it can match FileSpec objects
270// that have matching filenames with different paths.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271//
Adrian Prantl05097242018-04-30 16:49:04 +0000272// Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" and "1" if
273// "a" is greater than "b".
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) {
275 int result = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 // case sensitivity of compare
278 const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
Zachary Turner47c03462016-02-24 21:26:47 +0000279
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 // If full is true, then we must compare both the directory and filename.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281
Adrian Prantl05097242018-04-30 16:49:04 +0000282 // If full is false, then if either directory is empty, then we match on the
283 // basename only, and if both directories have valid values, we still do a
284 // full compare. This allows for matching when we just have a filename in one
285 // of the FileSpec objects.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286
Kate Stoneb9c1b512016-09-06 20:57:50 +0000287 if (full || (a.m_directory && b.m_directory)) {
288 result = ConstString::Compare(a.m_directory, b.m_directory, case_sensitive);
289 if (result)
290 return result;
291 }
292 return ConstString::Compare(a.m_filename, b.m_filename, case_sensitive);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293}
294
Greg Clayton776cd7a2018-04-27 15:45:58 +0000295bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full) {
Pavel Labathb18e1902019-11-28 13:48:05 +0100296 if (full || (a.GetDirectory() && b.GetDirectory()))
297 return a == b;
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000298
Pavel Labathb18e1902019-11-28 13:48:05 +0100299 return a.FileEquals(b);
Jim Ingham96a15962014-11-15 01:54:26 +0000300}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301
Pavel Labath532290e2019-11-29 11:31:00 +0100302bool FileSpec::Match(const FileSpec &pattern, const FileSpec &file) {
303 if (pattern.GetDirectory())
304 return pattern == file;
305 if (pattern.GetFilename())
306 return pattern.FileEquals(file);
307 return true;
308}
309
Pavel Labath841bea92019-02-11 14:11:00 +0000310llvm::Optional<FileSpec::Style> FileSpec::GuessPathStyle(llvm::StringRef absolute_path) {
311 if (absolute_path.startswith("/"))
312 return Style::posix;
313 if (absolute_path.startswith(R"(\\)"))
314 return Style::windows;
315 if (absolute_path.size() > 3 && llvm::isAlpha(absolute_path[0]) &&
316 absolute_path.substr(1, 2) == R"(:\)")
317 return Style::windows;
318 return llvm::None;
319}
320
Adrian Prantl05097242018-04-30 16:49:04 +0000321// Dump the object to the supplied stream. If the object contains a valid
322// directory name, it will be displayed followed by a directory delimiter, and
323// the filename.
Raphael Isemann4dac97e2019-12-06 08:38:23 +0100324void FileSpec::Dump(llvm::raw_ostream &s) const {
325 std::string path{GetPath(true)};
326 s << path;
327 char path_separator = GetPreferredPathSeparator(m_style);
328 if (!m_filename && !path.empty() && path.back() != path_separator)
329 s << path_separator;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000330}
331
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000332FileSpec::Style FileSpec::GetPathStyle() const { return m_style; }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334// Directory string get accessor.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335ConstString &FileSpec::GetDirectory() { return m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337// Directory string const get accessor.
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000338ConstString FileSpec::GetDirectory() const { return m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340// Filename string get accessor.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341ConstString &FileSpec::GetFilename() { return m_filename; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343// Filename string const get accessor.
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000344ConstString FileSpec::GetFilename() const { return m_filename; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345
Adrian Prantl05097242018-04-30 16:49:04 +0000346// Extract the directory and path into a fixed buffer. This is needed as the
347// directory and path are stored in separate string values.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348size_t FileSpec::GetPath(char *path, size_t path_max_len,
349 bool denormalize) const {
350 if (!path)
351 return 0;
Zachary Turnerb6d99242014-08-08 23:54:35 +0000352
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 std::string result = GetPath(denormalize);
354 ::snprintf(path, path_max_len, "%s", result.c_str());
355 return std::min(path_max_len - 1, result.length());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000356}
357
Kate Stoneb9c1b512016-09-06 20:57:50 +0000358std::string FileSpec::GetPath(bool denormalize) const {
359 llvm::SmallString<64> result;
360 GetPath(result, denormalize);
361 return std::string(result.begin(), result.end());
Jason Molendaa7ae4672013-04-29 09:46:43 +0000362}
363
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364const char *FileSpec::GetCString(bool denormalize) const {
Jonas Devlieghere65e5e2782018-12-13 00:15:17 +0000365 return ConstString{GetPath(denormalize)}.AsCString(nullptr);
Chaoren Lind3173f32015-05-29 19:52:29 +0000366}
367
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path,
369 bool denormalize) const {
370 path.append(m_directory.GetStringRef().begin(),
371 m_directory.GetStringRef().end());
Greg Clayton776cd7a2018-04-27 15:45:58 +0000372 // Since the path was normalized and all paths use '/' when stored in these
373 // objects, we don't need to look for the actual syntax specific path
374 // separator, we just look for and insert '/'.
375 if (m_directory && m_filename && m_directory.GetStringRef().back() != '/' &&
376 m_filename.GetStringRef().back() != '/')
377 path.insert(path.end(), '/');
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 path.append(m_filename.GetStringRef().begin(),
379 m_filename.GetStringRef().end());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380 if (denormalize && !path.empty())
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000381 Denormalize(path, m_style);
Zachary Turner4e8ddf52015-04-09 18:08:50 +0000382}
383
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384ConstString FileSpec::GetFileNameExtension() const {
Jonas Devlieghere9c1a6452018-06-13 16:36:07 +0000385 return ConstString(
386 llvm::sys::path::extension(m_filename.GetStringRef(), m_style));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387}
388
389ConstString FileSpec::GetFileNameStrippingExtension() const {
Jonas Devlieghere9c1a6452018-06-13 16:36:07 +0000390 return ConstString(llvm::sys::path::stem(m_filename.GetStringRef(), m_style));
Enrico Granataa9dbf432011-10-17 21:45:27 +0000391}
392
Adrian Prantl05097242018-04-30 16:49:04 +0000393// Return the size in bytes that this object takes in memory. This returns the
394// size in bytes of this object, not any shared string values it may refer to.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395size_t FileSpec::MemorySize() const {
396 return m_filename.MemorySize() + m_directory.MemorySize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397}
398
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000399FileSpec
400FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000401 FileSpec ret = *this;
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000402 ret.AppendPathComponent(component);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 return ret;
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000404}
405
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406FileSpec FileSpec::CopyByRemovingLastPathComponent() const {
Jonas Devlieghere24bd63c42018-06-24 10:18:01 +0000407 llvm::SmallString<64> current_path;
408 GetPath(current_path, false);
409 if (llvm::sys::path::has_parent_path(current_path, m_style))
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000410 return FileSpec(llvm::sys::path::parent_path(current_path, m_style),
Jonas Devlieghere24bd63c42018-06-24 10:18:01 +0000411 m_style);
412 return *this;
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000413}
414
Kate Stoneb9c1b512016-09-06 20:57:50 +0000415ConstString FileSpec::GetLastPathComponent() const {
Jonas Devlieghere24bd63c42018-06-24 10:18:01 +0000416 llvm::SmallString<64> current_path;
417 GetPath(current_path, false);
418 return ConstString(llvm::sys::path::filename(current_path, m_style));
Pavel Labath59d725c2017-01-16 10:07:02 +0000419}
420
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000421void FileSpec::PrependPathComponent(llvm::StringRef component) {
Jonas Devlieghere24bd63c42018-06-24 10:18:01 +0000422 llvm::SmallString<64> new_path(component);
423 llvm::SmallString<64> current_path;
424 GetPath(current_path, false);
425 llvm::sys::path::append(new_path,
426 llvm::sys::path::begin(current_path, m_style),
427 llvm::sys::path::end(current_path), m_style);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000428 SetFile(new_path, m_style);
Chaoren Lind3173f32015-05-29 19:52:29 +0000429}
430
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431void FileSpec::PrependPathComponent(const FileSpec &new_path) {
432 return PrependPathComponent(new_path.GetPath(false));
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000433}
434
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000435void FileSpec::AppendPathComponent(llvm::StringRef component) {
Jonas Devlieghere24bd63c42018-06-24 10:18:01 +0000436 llvm::SmallString<64> current_path;
437 GetPath(current_path, false);
438 llvm::sys::path::append(current_path, m_style, component);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000439 SetFile(current_path, m_style);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000440}
441
442void FileSpec::AppendPathComponent(const FileSpec &new_path) {
443 return AppendPathComponent(new_path.GetPath(false));
444}
445
Jonas Devliegheredf8e2912018-05-30 13:03:16 +0000446bool FileSpec::RemoveLastPathComponent() {
447 llvm::SmallString<64> current_path;
448 GetPath(current_path, false);
449 if (llvm::sys::path::has_parent_path(current_path, m_style)) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000450 SetFile(llvm::sys::path::parent_path(current_path, m_style));
Jonas Devliegheredf8e2912018-05-30 13:03:16 +0000451 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000452 }
Jonas Devliegheredf8e2912018-05-30 13:03:16 +0000453 return false;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000454}
Greg Clayton1f746072012-08-29 21:13:06 +0000455/// Returns true if the filespec represents an implementation source
456/// file (files with a ".c", ".cpp", ".m", ".mm" (many more)
457/// extension).
458///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000459/// \return
Greg Clayton1f746072012-08-29 21:13:06 +0000460/// \b true if the filespec represents an implementation source
461/// file, \b false otherwise.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462bool FileSpec::IsSourceImplementationFile() const {
463 ConstString extension(GetFileNameExtension());
Zachary Turner95eae422016-09-21 16:01:28 +0000464 if (!extension)
465 return false;
466
467 static RegularExpression g_source_file_regex(llvm::StringRef(
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000468 "^.([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|["
Zachary Turner95eae422016-09-21 16:01:28 +0000469 "cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO]["
470 "rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])"
471 "$"));
472 return g_source_file_regex.Execute(extension.GetStringRef());
Greg Clayton1f746072012-08-29 21:13:06 +0000473}
474
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000475bool FileSpec::IsRelative() const {
476 return !IsAbsolute();
477}
Chaoren Lin372e9062015-06-09 17:54:27 +0000478
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000479bool FileSpec::IsAbsolute() const {
480 llvm::SmallString<64> current_path;
481 GetPath(current_path, false);
482
483 // Early return if the path is empty.
484 if (current_path.empty())
485 return false;
486
487 // We consider paths starting with ~ to be absolute.
488 if (current_path[0] == '~')
489 return true;
490
491 return llvm::sys::path::is_absolute(current_path, m_style);
492}
Zachary Turner827d5d72016-12-16 04:27:00 +0000493
Pavel Labath7d36d722019-01-16 12:30:41 +0000494void FileSpec::MakeAbsolute(const FileSpec &dir) {
495 if (IsRelative())
496 PrependPathComponent(dir);
497}
498
Zachary Turner827d5d72016-12-16 04:27:00 +0000499void llvm::format_provider<FileSpec>::format(const FileSpec &F,
500 raw_ostream &Stream,
501 StringRef Style) {
502 assert(
503 (Style.empty() || Style.equals_lower("F") || Style.equals_lower("D")) &&
504 "Invalid FileSpec style!");
505
506 StringRef dir = F.GetDirectory().GetStringRef();
507 StringRef file = F.GetFilename().GetStringRef();
508
509 if (dir.empty() && file.empty()) {
510 Stream << "(empty)";
511 return;
512 }
513
514 if (Style.equals_lower("F")) {
515 Stream << (file.empty() ? "(empty)" : file);
516 return;
517 }
518
519 // Style is either D or empty, either way we need to print the directory.
520 if (!dir.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000521 // Directory is stored in normalized form, which might be different than
522 // preferred form. In order to handle this, we need to cut off the
523 // filename, then denormalize, then write the entire denorm'ed directory.
Zachary Turner827d5d72016-12-16 04:27:00 +0000524 llvm::SmallString<64> denormalized_dir = dir;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000525 Denormalize(denormalized_dir, F.GetPathStyle());
Zachary Turner827d5d72016-12-16 04:27:00 +0000526 Stream << denormalized_dir;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000527 Stream << GetPreferredPathSeparator(F.GetPathStyle());
Zachary Turner827d5d72016-12-16 04:27:00 +0000528 }
529
530 if (Style.equals_lower("D")) {
531 // We only want to print the directory, so now just exit.
532 if (dir.empty())
533 Stream << "(empty)";
534 return;
535 }
536
537 if (!file.empty())
538 Stream << file;
539}
Jonas Devliegherebc9b6b32020-03-12 09:51:59 -0700540
541void llvm::yaml::ScalarEnumerationTraits<FileSpecStyle>::enumeration(
542 IO &io, FileSpecStyle &value) {
543 io.enumCase(value, "windows", FileSpecStyle(FileSpec::Style::windows));
544 io.enumCase(value, "posix", FileSpecStyle(FileSpec::Style::posix));
545 io.enumCase(value, "native", FileSpecStyle(FileSpec::Style::native));
546}
547
548void llvm::yaml::MappingTraits<FileSpec>::mapping(IO &io, FileSpec &f) {
549 io.mapRequired("directory", f.m_directory);
550 io.mapRequired("file", f.m_filename);
551 io.mapRequired("resolved", f.m_is_resolved);
552 FileSpecStyle style = f.m_style;
553 io.mapRequired("style", style);
554 f.m_style = style;
555}