blob: 7fb6e9db72c8cfe8f34b3e983e8c6492ddd4f79b [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- FileSpec.cpp --------------------------------------------*- C++ -*-===//
2//
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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078// Copy constructor
Kate Stoneb9c1b512016-09-06 20:57:50 +000079FileSpec::FileSpec(const FileSpec *rhs) : m_directory(), m_filename() {
80 if (rhs)
81 *this = *rhs;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082}
83
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000084// Virtual destructor in case anyone inherits from this class.
Kate Stoneb9c1b512016-09-06 20:57:50 +000085FileSpec::~FileSpec() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086
Jonas Devliegherec1cc3172018-06-25 10:11:53 +000087namespace {
Jonas Devliegherec1cc3172018-06-25 10:11:53 +000088/// Safely get a character at the specified index.
89///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000090/// \param[in] path
Jonas Devliegherec1cc3172018-06-25 10:11:53 +000091/// A full, partial, or relative path to a file.
92///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000093/// \param[in] i
Jonas Devliegherec1cc3172018-06-25 10:11:53 +000094/// An index into path which may or may not be valid.
95///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000096/// \return
Jonas Devliegherec1cc3172018-06-25 10:11:53 +000097/// The character at index \a i if the index is valid, or 0 if
98/// the index is not valid.
Jonas Devliegherec1cc3172018-06-25 10:11:53 +000099inline char safeCharAtIndex(const llvm::StringRef &path, size_t i) {
100 if (i < path.size())
101 return path[i];
102 return 0;
103}
104
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000105/// Check if a path needs to be normalized.
106///
107/// Check if a path needs to be normalized. We currently consider a
108/// path to need normalization if any of the following are true
109/// - path contains "/./"
110/// - path contains "/../"
111/// - path contains "//"
112/// - path ends with "/"
113/// Paths that start with "./" or with "../" are not considered to
114/// need normalization since we aren't trying to resolve the path,
115/// we are just trying to remove redundant things from the path.
116///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000117/// \param[in] path
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000118/// A full, partial, or relative path to a file.
119///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000120/// \return
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000121/// Returns \b true if the path needs to be normalized.
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000122bool needsNormalization(const llvm::StringRef &path) {
123 if (path.empty())
124 return false;
125 // We strip off leading "." values so these paths need to be normalized
126 if (path[0] == '.')
127 return true;
128 for (auto i = path.find_first_of("\\/"); i != llvm::StringRef::npos;
129 i = path.find_first_of("\\/", i + 1)) {
130 const auto next = safeCharAtIndex(path, i+1);
131 switch (next) {
132 case 0:
133 // path separator char at the end of the string which should be
134 // stripped unless it is the one and only character
135 return i > 0;
136 case '/':
137 case '\\':
138 // two path separator chars in the middle of a path needs to be
139 // normalized
140 if (i > 0)
141 return true;
142 ++i;
143 break;
144
145 case '.': {
146 const auto next_next = safeCharAtIndex(path, i+2);
147 switch (next_next) {
148 default: break;
149 case 0: return true; // ends with "/."
150 case '/':
151 case '\\':
152 return true; // contains "/./"
153 case '.': {
154 const auto next_next_next = safeCharAtIndex(path, i+3);
155 switch (next_next_next) {
156 default: break;
157 case 0: return true; // ends with "/.."
158 case '/':
159 case '\\':
160 return true; // contains "/../"
161 }
162 break;
163 }
164 }
165 }
166 break;
167
168 default:
169 break;
170 }
171 }
172 return false;
173}
174
175
176}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000178void FileSpec::SetFile(llvm::StringRef pathname) { SetFile(pathname, m_style); }
Jonas Devlieghere937348c2018-06-13 22:08:14 +0000179
Adrian Prantl05097242018-04-30 16:49:04 +0000180// Update the contents of this object with a new path. The path will be split
181// up into a directory and filename and stored as uniqued string values for
182// quick comparison and efficient memory usage.
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000183void FileSpec::SetFile(llvm::StringRef pathname, Style style) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 m_filename.Clear();
185 m_directory.Clear();
186 m_is_resolved = false;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000187 m_style = (style == Style::native) ? GetNativeStyle() : style;
Zachary Turnerdf62f202014-08-07 17:33:07 +0000188
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000189 if (pathname.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000192 llvm::SmallString<128> resolved(pathname);
Zachary Turnerc7a17ed2014-12-01 23:13:32 +0000193
Greg Clayton776cd7a2018-04-27 15:45:58 +0000194 // Normalize the path by removing ".", ".." and other redundant components.
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000195 if (needsNormalization(resolved))
196 llvm::sys::path::remove_dots(resolved, true, m_style);
Greg Clayton776cd7a2018-04-27 15:45:58 +0000197
198 // Normalize back slashes to forward slashes
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000199 if (m_style == Style::windows)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000200 std::replace(resolved.begin(), resolved.end(), '\\', '/');
Pavel Labatha212c582016-04-14 09:38:06 +0000201
Greg Clayton39d50b72018-05-17 16:12:38 +0000202 if (resolved.empty()) {
203 // If we have no path after normalization set the path to the current
204 // directory. This matches what python does and also a few other path
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000205 // utilities.
Greg Clayton39d50b72018-05-17 16:12:38 +0000206 m_filename.SetString(".");
207 return;
208 }
209
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000210 // Split path into filename and directory. We rely on the underlying char
211 // pointer to be nullptr when the components are empty.
212 llvm::StringRef filename = llvm::sys::path::filename(resolved, m_style);
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000213 if(!filename.empty())
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000214 m_filename.SetString(filename);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000215
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000216 llvm::StringRef directory = llvm::sys::path::parent_path(resolved, m_style);
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000217 if(!directory.empty())
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000218 m_directory.SetString(directory);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000219}
220
Jonas Devlieghere706cd702019-08-15 05:09:09 +0000221void FileSpec::SetFile(llvm::StringRef path, const llvm::Triple &triple) {
222 return SetFile(path, triple.isOSWindows() ? Style::windows : Style::posix);
Chaoren Lin44145d72015-05-29 19:52:37 +0000223}
224
Adrian Prantl05097242018-04-30 16:49:04 +0000225// Convert to pointer operator. This allows code to check any FileSpec objects
226// to see if they contain anything valid using code such as:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000227//
228// if (file_spec)
229// {}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230FileSpec::operator bool() const { return m_filename || m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000231
Adrian Prantl05097242018-04-30 16:49:04 +0000232// Logical NOT operator. This allows code to check any FileSpec objects to see
233// if they are invalid using code such as:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000234//
235// if (!file_spec)
236// {}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237bool FileSpec::operator!() const { return !m_directory && !m_filename; }
238
239bool FileSpec::DirectoryEquals(const FileSpec &rhs) const {
240 const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
241 return ConstString::Equals(m_directory, rhs.m_directory, case_sensitive);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242}
243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244bool FileSpec::FileEquals(const FileSpec &rhs) const {
245 const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
246 return ConstString::Equals(m_filename, rhs.m_filename, case_sensitive);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000247}
248
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249// Equal to operator
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250bool FileSpec::operator==(const FileSpec &rhs) const {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000251 return FileEquals(rhs) && DirectoryEquals(rhs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252}
253
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254// Not equal to operator
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257// Less than operator
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258bool FileSpec::operator<(const FileSpec &rhs) const {
259 return FileSpec::Compare(*this, rhs, true) < 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260}
261
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262// Dump a FileSpec object to a stream
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) {
264 f.Dump(&s);
265 return s;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000266}
267
Adrian Prantl05097242018-04-30 16:49:04 +0000268// Clear this object by releasing both the directory and filename string values
269// and making them both the empty string.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270void FileSpec::Clear() {
271 m_directory.Clear();
272 m_filename.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273}
274
Adrian Prantl05097242018-04-30 16:49:04 +0000275// Compare two FileSpec objects. If "full" is true, then both the directory and
276// the filename must match. If "full" is false, then the directory names for
277// "a" and "b" are only compared if they are both non-empty. This allows a
278// FileSpec object to only contain a filename and it can match FileSpec objects
279// that have matching filenames with different paths.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280//
Adrian Prantl05097242018-04-30 16:49:04 +0000281// Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" and "1" if
282// "a" is greater than "b".
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) {
284 int result = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 // case sensitivity of compare
287 const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
Zachary Turner47c03462016-02-24 21:26:47 +0000288
Kate Stoneb9c1b512016-09-06 20:57:50 +0000289 // If full is true, then we must compare both the directory and filename.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290
Adrian Prantl05097242018-04-30 16:49:04 +0000291 // If full is false, then if either directory is empty, then we match on the
292 // basename only, and if both directories have valid values, we still do a
293 // full compare. This allows for matching when we just have a filename in one
294 // of the FileSpec objects.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295
Kate Stoneb9c1b512016-09-06 20:57:50 +0000296 if (full || (a.m_directory && b.m_directory)) {
297 result = ConstString::Compare(a.m_directory, b.m_directory, case_sensitive);
298 if (result)
299 return result;
300 }
301 return ConstString::Compare(a.m_filename, b.m_filename, case_sensitive);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302}
303
Greg Clayton776cd7a2018-04-27 15:45:58 +0000304bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full) {
Pavel Labathb18e1902019-11-28 13:48:05 +0100305 if (full || (a.GetDirectory() && b.GetDirectory()))
306 return a == b;
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000307
Pavel Labathb18e1902019-11-28 13:48:05 +0100308 return a.FileEquals(b);
Jim Ingham96a15962014-11-15 01:54:26 +0000309}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310
Pavel Labath841bea92019-02-11 14:11:00 +0000311llvm::Optional<FileSpec::Style> FileSpec::GuessPathStyle(llvm::StringRef absolute_path) {
312 if (absolute_path.startswith("/"))
313 return Style::posix;
314 if (absolute_path.startswith(R"(\\)"))
315 return Style::windows;
316 if (absolute_path.size() > 3 && llvm::isAlpha(absolute_path[0]) &&
317 absolute_path.substr(1, 2) == R"(:\)")
318 return Style::windows;
319 return llvm::None;
320}
321
Adrian Prantl05097242018-04-30 16:49:04 +0000322// Dump the object to the supplied stream. If the object contains a valid
323// directory name, it will be displayed followed by a directory delimiter, and
324// the filename.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325void FileSpec::Dump(Stream *s) const {
326 if (s) {
327 std::string path{GetPath(true)};
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000328 s->PutCString(path);
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000329 char path_separator = GetPreferredPathSeparator(m_style);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000330 if (!m_filename && !path.empty() && path.back() != path_separator)
331 s->PutChar(path_separator);
332 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333}
334
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000335FileSpec::Style FileSpec::GetPathStyle() const { return m_style; }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337// Directory string get accessor.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338ConstString &FileSpec::GetDirectory() { return m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340// Directory string const get accessor.
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000341ConstString FileSpec::GetDirectory() const { return m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343// Filename string get accessor.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344ConstString &FileSpec::GetFilename() { return m_filename; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346// Filename string const get accessor.
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000347ConstString FileSpec::GetFilename() const { return m_filename; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348
Adrian Prantl05097242018-04-30 16:49:04 +0000349// Extract the directory and path into a fixed buffer. This is needed as the
350// directory and path are stored in separate string values.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351size_t FileSpec::GetPath(char *path, size_t path_max_len,
352 bool denormalize) const {
353 if (!path)
354 return 0;
Zachary Turnerb6d99242014-08-08 23:54:35 +0000355
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 std::string result = GetPath(denormalize);
357 ::snprintf(path, path_max_len, "%s", result.c_str());
358 return std::min(path_max_len - 1, result.length());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359}
360
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361std::string FileSpec::GetPath(bool denormalize) const {
362 llvm::SmallString<64> result;
363 GetPath(result, denormalize);
364 return std::string(result.begin(), result.end());
Jason Molendaa7ae4672013-04-29 09:46:43 +0000365}
366
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367const char *FileSpec::GetCString(bool denormalize) const {
Jonas Devlieghere65e5e2782018-12-13 00:15:17 +0000368 return ConstString{GetPath(denormalize)}.AsCString(nullptr);
Chaoren Lind3173f32015-05-29 19:52:29 +0000369}
370
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path,
372 bool denormalize) const {
373 path.append(m_directory.GetStringRef().begin(),
374 m_directory.GetStringRef().end());
Greg Clayton776cd7a2018-04-27 15:45:58 +0000375 // Since the path was normalized and all paths use '/' when stored in these
376 // objects, we don't need to look for the actual syntax specific path
377 // separator, we just look for and insert '/'.
378 if (m_directory && m_filename && m_directory.GetStringRef().back() != '/' &&
379 m_filename.GetStringRef().back() != '/')
380 path.insert(path.end(), '/');
Kate Stoneb9c1b512016-09-06 20:57:50 +0000381 path.append(m_filename.GetStringRef().begin(),
382 m_filename.GetStringRef().end());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383 if (denormalize && !path.empty())
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000384 Denormalize(path, m_style);
Zachary Turner4e8ddf52015-04-09 18:08:50 +0000385}
386
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387ConstString FileSpec::GetFileNameExtension() const {
Jonas Devlieghere9c1a6452018-06-13 16:36:07 +0000388 return ConstString(
389 llvm::sys::path::extension(m_filename.GetStringRef(), m_style));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000390}
391
392ConstString FileSpec::GetFileNameStrippingExtension() const {
Jonas Devlieghere9c1a6452018-06-13 16:36:07 +0000393 return ConstString(llvm::sys::path::stem(m_filename.GetStringRef(), m_style));
Enrico Granataa9dbf432011-10-17 21:45:27 +0000394}
395
Adrian Prantl05097242018-04-30 16:49:04 +0000396// Return the size in bytes that this object takes in memory. This returns the
397// size in bytes of this object, not any shared string values it may refer to.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398size_t FileSpec::MemorySize() const {
399 return m_filename.MemorySize() + m_directory.MemorySize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400}
401
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000402FileSpec
403FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404 FileSpec ret = *this;
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000405 ret.AppendPathComponent(component);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406 return ret;
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000407}
408
Kate Stoneb9c1b512016-09-06 20:57:50 +0000409FileSpec FileSpec::CopyByRemovingLastPathComponent() const {
Jonas Devlieghere24bd63c42018-06-24 10:18:01 +0000410 llvm::SmallString<64> current_path;
411 GetPath(current_path, false);
412 if (llvm::sys::path::has_parent_path(current_path, m_style))
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000413 return FileSpec(llvm::sys::path::parent_path(current_path, m_style),
Jonas Devlieghere24bd63c42018-06-24 10:18:01 +0000414 m_style);
415 return *this;
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000416}
417
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418ConstString FileSpec::GetLastPathComponent() const {
Jonas Devlieghere24bd63c42018-06-24 10:18:01 +0000419 llvm::SmallString<64> current_path;
420 GetPath(current_path, false);
421 return ConstString(llvm::sys::path::filename(current_path, m_style));
Pavel Labath59d725c2017-01-16 10:07:02 +0000422}
423
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000424void FileSpec::PrependPathComponent(llvm::StringRef component) {
Jonas Devlieghere24bd63c42018-06-24 10:18:01 +0000425 llvm::SmallString<64> new_path(component);
426 llvm::SmallString<64> current_path;
427 GetPath(current_path, false);
428 llvm::sys::path::append(new_path,
429 llvm::sys::path::begin(current_path, m_style),
430 llvm::sys::path::end(current_path), m_style);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000431 SetFile(new_path, m_style);
Chaoren Lind3173f32015-05-29 19:52:29 +0000432}
433
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434void FileSpec::PrependPathComponent(const FileSpec &new_path) {
435 return PrependPathComponent(new_path.GetPath(false));
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000436}
437
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000438void FileSpec::AppendPathComponent(llvm::StringRef component) {
Jonas Devlieghere24bd63c42018-06-24 10:18:01 +0000439 llvm::SmallString<64> current_path;
440 GetPath(current_path, false);
441 llvm::sys::path::append(current_path, m_style, component);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000442 SetFile(current_path, m_style);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443}
444
445void FileSpec::AppendPathComponent(const FileSpec &new_path) {
446 return AppendPathComponent(new_path.GetPath(false));
447}
448
Jonas Devliegheredf8e2912018-05-30 13:03:16 +0000449bool FileSpec::RemoveLastPathComponent() {
450 llvm::SmallString<64> current_path;
451 GetPath(current_path, false);
452 if (llvm::sys::path::has_parent_path(current_path, m_style)) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000453 SetFile(llvm::sys::path::parent_path(current_path, m_style));
Jonas Devliegheredf8e2912018-05-30 13:03:16 +0000454 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455 }
Jonas Devliegheredf8e2912018-05-30 13:03:16 +0000456 return false;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000457}
Greg Clayton1f746072012-08-29 21:13:06 +0000458/// Returns true if the filespec represents an implementation source
459/// file (files with a ".c", ".cpp", ".m", ".mm" (many more)
460/// extension).
461///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000462/// \return
Greg Clayton1f746072012-08-29 21:13:06 +0000463/// \b true if the filespec represents an implementation source
464/// file, \b false otherwise.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465bool FileSpec::IsSourceImplementationFile() const {
466 ConstString extension(GetFileNameExtension());
Zachary Turner95eae422016-09-21 16:01:28 +0000467 if (!extension)
468 return false;
469
470 static RegularExpression g_source_file_regex(llvm::StringRef(
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000471 "^.([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|["
Zachary Turner95eae422016-09-21 16:01:28 +0000472 "cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO]["
473 "rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])"
474 "$"));
475 return g_source_file_regex.Execute(extension.GetStringRef());
Greg Clayton1f746072012-08-29 21:13:06 +0000476}
477
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000478bool FileSpec::IsRelative() const {
479 return !IsAbsolute();
480}
Chaoren Lin372e9062015-06-09 17:54:27 +0000481
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000482bool FileSpec::IsAbsolute() const {
483 llvm::SmallString<64> current_path;
484 GetPath(current_path, false);
485
486 // Early return if the path is empty.
487 if (current_path.empty())
488 return false;
489
490 // We consider paths starting with ~ to be absolute.
491 if (current_path[0] == '~')
492 return true;
493
494 return llvm::sys::path::is_absolute(current_path, m_style);
495}
Zachary Turner827d5d72016-12-16 04:27:00 +0000496
Pavel Labath7d36d722019-01-16 12:30:41 +0000497void FileSpec::MakeAbsolute(const FileSpec &dir) {
498 if (IsRelative())
499 PrependPathComponent(dir);
500}
501
Zachary Turner827d5d72016-12-16 04:27:00 +0000502void llvm::format_provider<FileSpec>::format(const FileSpec &F,
503 raw_ostream &Stream,
504 StringRef Style) {
505 assert(
506 (Style.empty() || Style.equals_lower("F") || Style.equals_lower("D")) &&
507 "Invalid FileSpec style!");
508
509 StringRef dir = F.GetDirectory().GetStringRef();
510 StringRef file = F.GetFilename().GetStringRef();
511
512 if (dir.empty() && file.empty()) {
513 Stream << "(empty)";
514 return;
515 }
516
517 if (Style.equals_lower("F")) {
518 Stream << (file.empty() ? "(empty)" : file);
519 return;
520 }
521
522 // Style is either D or empty, either way we need to print the directory.
523 if (!dir.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000524 // Directory is stored in normalized form, which might be different than
525 // preferred form. In order to handle this, we need to cut off the
526 // filename, then denormalize, then write the entire denorm'ed directory.
Zachary Turner827d5d72016-12-16 04:27:00 +0000527 llvm::SmallString<64> denormalized_dir = dir;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000528 Denormalize(denormalized_dir, F.GetPathStyle());
Zachary Turner827d5d72016-12-16 04:27:00 +0000529 Stream << denormalized_dir;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000530 Stream << GetPreferredPathSeparator(F.GetPathStyle());
Zachary Turner827d5d72016-12-16 04:27:00 +0000531 }
532
533 if (Style.equals_lower("D")) {
534 // We only want to print the directory, so now just exit.
535 if (dir.empty())
536 Stream << "(empty)";
537 return;
538 }
539
540 if (!file.empty())
541 Stream << file;
542}