blob: 72101f93b133f7eba53969061e769f9d741a9164 [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
70//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +000071// Default constructor that can take an optional full path to a file on disk.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072//------------------------------------------------------------------
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +000073FileSpec::FileSpec(llvm::StringRef path, Style style) : m_style(style) {
74 SetFile(path, style);
Jim Ingham0909e5f2010-09-16 00:57:33 +000075}
76
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +000077FileSpec::FileSpec(llvm::StringRef path, const llvm::Triple &Triple)
78 : FileSpec{path, Triple.isOSWindows() ? Style::windows : Style::posix} {}
Chaoren Linf34f4102015-05-09 01:21:32 +000079
Jim Ingham0909e5f2010-09-16 00:57:33 +000080//------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081// Copy constructor
82//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000083FileSpec::FileSpec(const FileSpec &rhs)
84 : m_directory(rhs.m_directory), m_filename(rhs.m_filename),
Pavel Labath2cb7cf82018-05-14 14:52:47 +000085 m_is_resolved(rhs.m_is_resolved), m_style(rhs.m_style) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086
87//------------------------------------------------------------------
88// Copy constructor
89//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000090FileSpec::FileSpec(const FileSpec *rhs) : m_directory(), m_filename() {
91 if (rhs)
92 *this = *rhs;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000093}
94
95//------------------------------------------------------------------
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000096// Virtual destructor in case anyone inherits from this class.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000098FileSpec::~FileSpec() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000100namespace {
101//------------------------------------------------------------------
102/// Safely get a character at the specified index.
103///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000104/// \param[in] path
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000105/// A full, partial, or relative path to a file.
106///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000107/// \param[in] i
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000108/// An index into path which may or may not be valid.
109///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000110/// \return
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000111/// The character at index \a i if the index is valid, or 0 if
112/// the index is not valid.
113//------------------------------------------------------------------
114inline char safeCharAtIndex(const llvm::StringRef &path, size_t i) {
115 if (i < path.size())
116 return path[i];
117 return 0;
118}
119
120//------------------------------------------------------------------
121/// Check if a path needs to be normalized.
122///
123/// Check if a path needs to be normalized. We currently consider a
124/// path to need normalization if any of the following are true
125/// - path contains "/./"
126/// - path contains "/../"
127/// - path contains "//"
128/// - path ends with "/"
129/// Paths that start with "./" or with "../" are not considered to
130/// need normalization since we aren't trying to resolve the path,
131/// we are just trying to remove redundant things from the path.
132///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000133/// \param[in] path
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000134/// A full, partial, or relative path to a file.
135///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000136/// \return
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000137/// Returns \b true if the path needs to be normalized.
138//------------------------------------------------------------------
139bool needsNormalization(const llvm::StringRef &path) {
140 if (path.empty())
141 return false;
142 // We strip off leading "." values so these paths need to be normalized
143 if (path[0] == '.')
144 return true;
145 for (auto i = path.find_first_of("\\/"); i != llvm::StringRef::npos;
146 i = path.find_first_of("\\/", i + 1)) {
147 const auto next = safeCharAtIndex(path, i+1);
148 switch (next) {
149 case 0:
150 // path separator char at the end of the string which should be
151 // stripped unless it is the one and only character
152 return i > 0;
153 case '/':
154 case '\\':
155 // two path separator chars in the middle of a path needs to be
156 // normalized
157 if (i > 0)
158 return true;
159 ++i;
160 break;
161
162 case '.': {
163 const auto next_next = safeCharAtIndex(path, i+2);
164 switch (next_next) {
165 default: break;
166 case 0: return true; // ends with "/."
167 case '/':
168 case '\\':
169 return true; // contains "/./"
170 case '.': {
171 const auto next_next_next = safeCharAtIndex(path, i+3);
172 switch (next_next_next) {
173 default: break;
174 case 0: return true; // ends with "/.."
175 case '/':
176 case '\\':
177 return true; // contains "/../"
178 }
179 break;
180 }
181 }
182 }
183 break;
184
185 default:
186 break;
187 }
188 }
189 return false;
190}
191
192
193}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194//------------------------------------------------------------------
195// Assignment operator.
196//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197const FileSpec &FileSpec::operator=(const FileSpec &rhs) {
198 if (this != &rhs) {
199 m_directory = rhs.m_directory;
200 m_filename = rhs.m_filename;
201 m_is_resolved = rhs.m_is_resolved;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000202 m_style = rhs.m_style;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203 }
204 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205}
206
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000207void FileSpec::SetFile(llvm::StringRef pathname) { SetFile(pathname, m_style); }
Jonas Devlieghere937348c2018-06-13 22:08:14 +0000208
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000210// Update the contents of this object with a new path. The path will be split
211// up into a directory and filename and stored as uniqued string values for
212// quick comparison and efficient memory usage.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213//------------------------------------------------------------------
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000214void FileSpec::SetFile(llvm::StringRef pathname, Style style) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215 m_filename.Clear();
216 m_directory.Clear();
217 m_is_resolved = false;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000218 m_style = (style == Style::native) ? GetNativeStyle() : style;
Zachary Turnerdf62f202014-08-07 17:33:07 +0000219
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000220 if (pathname.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000223 llvm::SmallString<128> resolved(pathname);
Zachary Turnerc7a17ed2014-12-01 23:13:32 +0000224
Greg Clayton776cd7a2018-04-27 15:45:58 +0000225 // Normalize the path by removing ".", ".." and other redundant components.
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000226 if (needsNormalization(resolved))
227 llvm::sys::path::remove_dots(resolved, true, m_style);
Greg Clayton776cd7a2018-04-27 15:45:58 +0000228
229 // Normalize back slashes to forward slashes
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000230 if (m_style == Style::windows)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000231 std::replace(resolved.begin(), resolved.end(), '\\', '/');
Pavel Labatha212c582016-04-14 09:38:06 +0000232
Greg Clayton39d50b72018-05-17 16:12:38 +0000233 if (resolved.empty()) {
234 // If we have no path after normalization set the path to the current
235 // directory. This matches what python does and also a few other path
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000236 // utilities.
Greg Clayton39d50b72018-05-17 16:12:38 +0000237 m_filename.SetString(".");
238 return;
239 }
240
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000241 // Split path into filename and directory. We rely on the underlying char
242 // pointer to be nullptr when the components are empty.
243 llvm::StringRef filename = llvm::sys::path::filename(resolved, m_style);
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000244 if(!filename.empty())
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000245 m_filename.SetString(filename);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000246
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000247 llvm::StringRef directory = llvm::sys::path::parent_path(resolved, m_style);
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000248 if(!directory.empty())
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000249 m_directory.SetString(directory);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250}
251
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000252void FileSpec::SetFile(llvm::StringRef path, const llvm::Triple &Triple) {
253 return SetFile(path, Triple.isOSWindows() ? Style::windows : Style::posix);
Chaoren Lin44145d72015-05-29 19:52:37 +0000254}
255
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000257// Convert to pointer operator. This allows code to check any FileSpec objects
258// to see if they contain anything valid using code such as:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259//
260// if (file_spec)
261// {}
262//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263FileSpec::operator bool() const { return m_filename || m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264
265//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000266// Logical NOT operator. This allows code to check any FileSpec objects to see
267// if they are invalid using code such as:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268//
269// if (!file_spec)
270// {}
271//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272bool FileSpec::operator!() const { return !m_directory && !m_filename; }
273
274bool FileSpec::DirectoryEquals(const FileSpec &rhs) const {
275 const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
276 return ConstString::Equals(m_directory, rhs.m_directory, case_sensitive);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277}
278
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279bool FileSpec::FileEquals(const FileSpec &rhs) const {
280 const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
281 return ConstString::Equals(m_filename, rhs.m_filename, case_sensitive);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000282}
283
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284//------------------------------------------------------------------
285// Equal to operator
286//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000287bool FileSpec::operator==(const FileSpec &rhs) const {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000288 return FileEquals(rhs) && DirectoryEquals(rhs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289}
290
291//------------------------------------------------------------------
292// Not equal to operator
293//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295
296//------------------------------------------------------------------
297// Less than operator
298//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299bool FileSpec::operator<(const FileSpec &rhs) const {
300 return FileSpec::Compare(*this, rhs, true) < 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301}
302
303//------------------------------------------------------------------
304// Dump a FileSpec object to a stream
305//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000306Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) {
307 f.Dump(&s);
308 return s;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309}
310
311//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000312// Clear this object by releasing both the directory and filename string values
313// and making them both the empty string.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315void FileSpec::Clear() {
316 m_directory.Clear();
317 m_filename.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000318}
319
320//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000321// Compare two FileSpec objects. If "full" is true, then both the directory and
322// the filename must match. If "full" is false, then the directory names for
323// "a" and "b" are only compared if they are both non-empty. This allows a
324// FileSpec object to only contain a filename and it can match FileSpec objects
325// that have matching filenames with different paths.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326//
Adrian Prantl05097242018-04-30 16:49:04 +0000327// Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" and "1" if
328// "a" is greater than "b".
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000330int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) {
331 int result = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333 // case sensitivity of compare
334 const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
Zachary Turner47c03462016-02-24 21:26:47 +0000335
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336 // If full is true, then we must compare both the directory and filename.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337
Adrian Prantl05097242018-04-30 16:49:04 +0000338 // If full is false, then if either directory is empty, then we match on the
339 // basename only, and if both directories have valid values, we still do a
340 // full compare. This allows for matching when we just have a filename in one
341 // of the FileSpec objects.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342
Kate Stoneb9c1b512016-09-06 20:57:50 +0000343 if (full || (a.m_directory && b.m_directory)) {
344 result = ConstString::Compare(a.m_directory, b.m_directory, case_sensitive);
345 if (result)
346 return result;
347 }
348 return ConstString::Compare(a.m_filename, b.m_filename, case_sensitive);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349}
350
Greg Clayton776cd7a2018-04-27 15:45:58 +0000351bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352 // case sensitivity of equality test
353 const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000354
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000355 const bool filenames_equal = ConstString::Equals(a.m_filename,
356 b.m_filename,
357 case_sensitive);
Jim Ingham97e4f472017-03-27 19:12:25 +0000358
Greg Clayton776cd7a2018-04-27 15:45:58 +0000359 if (!filenames_equal)
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000360 return false;
Jim Ingham97e4f472017-03-27 19:12:25 +0000361
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362 if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty()))
Jim Ingham97e4f472017-03-27 19:12:25 +0000363 return filenames_equal;
Pavel Labath218770b2016-10-31 16:22:07 +0000364
Greg Clayton776cd7a2018-04-27 15:45:58 +0000365 return a == b;
Jim Ingham96a15962014-11-15 01:54:26 +0000366}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000367
Pavel Labath841bea92019-02-11 14:11:00 +0000368llvm::Optional<FileSpec::Style> FileSpec::GuessPathStyle(llvm::StringRef absolute_path) {
369 if (absolute_path.startswith("/"))
370 return Style::posix;
371 if (absolute_path.startswith(R"(\\)"))
372 return Style::windows;
373 if (absolute_path.size() > 3 && llvm::isAlpha(absolute_path[0]) &&
374 absolute_path.substr(1, 2) == R"(:\)")
375 return Style::windows;
376 return llvm::None;
377}
378
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000380// Dump the object to the supplied stream. If the object contains a valid
381// directory name, it will be displayed followed by a directory delimiter, and
382// the filename.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384void FileSpec::Dump(Stream *s) const {
385 if (s) {
386 std::string path{GetPath(true)};
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000387 s->PutCString(path);
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000388 char path_separator = GetPreferredPathSeparator(m_style);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 if (!m_filename && !path.empty() && path.back() != path_separator)
390 s->PutChar(path_separator);
391 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392}
393
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000394FileSpec::Style FileSpec::GetPathStyle() const { return m_style; }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396//------------------------------------------------------------------
397// Directory string get accessor.
398//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399ConstString &FileSpec::GetDirectory() { return m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400
401//------------------------------------------------------------------
402// Directory string const get accessor.
403//------------------------------------------------------------------
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000404ConstString FileSpec::GetDirectory() const { return m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405
406//------------------------------------------------------------------
407// Filename string get accessor.
408//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000409ConstString &FileSpec::GetFilename() { return m_filename; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410
411//------------------------------------------------------------------
412// Filename string const get accessor.
413//------------------------------------------------------------------
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000414ConstString FileSpec::GetFilename() const { return m_filename; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415
416//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000417// Extract the directory and path into a fixed buffer. This is needed as the
418// directory and path are stored in separate string values.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420size_t FileSpec::GetPath(char *path, size_t path_max_len,
421 bool denormalize) const {
422 if (!path)
423 return 0;
Zachary Turnerb6d99242014-08-08 23:54:35 +0000424
Kate Stoneb9c1b512016-09-06 20:57:50 +0000425 std::string result = GetPath(denormalize);
426 ::snprintf(path, path_max_len, "%s", result.c_str());
427 return std::min(path_max_len - 1, result.length());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000428}
429
Kate Stoneb9c1b512016-09-06 20:57:50 +0000430std::string FileSpec::GetPath(bool denormalize) const {
431 llvm::SmallString<64> result;
432 GetPath(result, denormalize);
433 return std::string(result.begin(), result.end());
Jason Molendaa7ae4672013-04-29 09:46:43 +0000434}
435
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436const char *FileSpec::GetCString(bool denormalize) const {
Jonas Devlieghere65e5e2782018-12-13 00:15:17 +0000437 return ConstString{GetPath(denormalize)}.AsCString(nullptr);
Chaoren Lind3173f32015-05-29 19:52:29 +0000438}
439
Kate Stoneb9c1b512016-09-06 20:57:50 +0000440void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path,
441 bool denormalize) const {
442 path.append(m_directory.GetStringRef().begin(),
443 m_directory.GetStringRef().end());
Greg Clayton776cd7a2018-04-27 15:45:58 +0000444 // Since the path was normalized and all paths use '/' when stored in these
445 // objects, we don't need to look for the actual syntax specific path
446 // separator, we just look for and insert '/'.
447 if (m_directory && m_filename && m_directory.GetStringRef().back() != '/' &&
448 m_filename.GetStringRef().back() != '/')
449 path.insert(path.end(), '/');
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450 path.append(m_filename.GetStringRef().begin(),
451 m_filename.GetStringRef().end());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000452 if (denormalize && !path.empty())
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000453 Denormalize(path, m_style);
Zachary Turner4e8ddf52015-04-09 18:08:50 +0000454}
455
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456ConstString FileSpec::GetFileNameExtension() const {
Jonas Devlieghere9c1a6452018-06-13 16:36:07 +0000457 return ConstString(
458 llvm::sys::path::extension(m_filename.GetStringRef(), m_style));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459}
460
461ConstString FileSpec::GetFileNameStrippingExtension() const {
Jonas Devlieghere9c1a6452018-06-13 16:36:07 +0000462 return ConstString(llvm::sys::path::stem(m_filename.GetStringRef(), m_style));
Enrico Granataa9dbf432011-10-17 21:45:27 +0000463}
464
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000466// Return the size in bytes that this object takes in memory. This returns the
467// size in bytes of this object, not any shared string values it may refer to.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469size_t FileSpec::MemorySize() const {
470 return m_filename.MemorySize() + m_directory.MemorySize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000471}
472
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000473FileSpec
474FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000475 FileSpec ret = *this;
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000476 ret.AppendPathComponent(component);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477 return ret;
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000478}
479
Kate Stoneb9c1b512016-09-06 20:57:50 +0000480FileSpec FileSpec::CopyByRemovingLastPathComponent() const {
Jonas Devlieghere24bd63c42018-06-24 10:18:01 +0000481 llvm::SmallString<64> current_path;
482 GetPath(current_path, false);
483 if (llvm::sys::path::has_parent_path(current_path, m_style))
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000484 return FileSpec(llvm::sys::path::parent_path(current_path, m_style),
Jonas Devlieghere24bd63c42018-06-24 10:18:01 +0000485 m_style);
486 return *this;
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000487}
488
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489ConstString FileSpec::GetLastPathComponent() const {
Jonas Devlieghere24bd63c42018-06-24 10:18:01 +0000490 llvm::SmallString<64> current_path;
491 GetPath(current_path, false);
492 return ConstString(llvm::sys::path::filename(current_path, m_style));
Pavel Labath59d725c2017-01-16 10:07:02 +0000493}
494
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000495void FileSpec::PrependPathComponent(llvm::StringRef component) {
Jonas Devlieghere24bd63c42018-06-24 10:18:01 +0000496 llvm::SmallString<64> new_path(component);
497 llvm::SmallString<64> current_path;
498 GetPath(current_path, false);
499 llvm::sys::path::append(new_path,
500 llvm::sys::path::begin(current_path, m_style),
501 llvm::sys::path::end(current_path), m_style);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000502 SetFile(new_path, m_style);
Chaoren Lind3173f32015-05-29 19:52:29 +0000503}
504
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505void FileSpec::PrependPathComponent(const FileSpec &new_path) {
506 return PrependPathComponent(new_path.GetPath(false));
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000507}
508
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000509void FileSpec::AppendPathComponent(llvm::StringRef component) {
Jonas Devlieghere24bd63c42018-06-24 10:18:01 +0000510 llvm::SmallString<64> current_path;
511 GetPath(current_path, false);
512 llvm::sys::path::append(current_path, m_style, component);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000513 SetFile(current_path, m_style);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514}
515
516void FileSpec::AppendPathComponent(const FileSpec &new_path) {
517 return AppendPathComponent(new_path.GetPath(false));
518}
519
Jonas Devliegheredf8e2912018-05-30 13:03:16 +0000520bool FileSpec::RemoveLastPathComponent() {
521 llvm::SmallString<64> current_path;
522 GetPath(current_path, false);
523 if (llvm::sys::path::has_parent_path(current_path, m_style)) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000524 SetFile(llvm::sys::path::parent_path(current_path, m_style));
Jonas Devliegheredf8e2912018-05-30 13:03:16 +0000525 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000526 }
Jonas Devliegheredf8e2912018-05-30 13:03:16 +0000527 return false;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000528}
Greg Clayton1f746072012-08-29 21:13:06 +0000529//------------------------------------------------------------------
530/// Returns true if the filespec represents an implementation source
531/// file (files with a ".c", ".cpp", ".m", ".mm" (many more)
532/// extension).
533///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000534/// \return
Greg Clayton1f746072012-08-29 21:13:06 +0000535/// \b true if the filespec represents an implementation source
536/// file, \b false otherwise.
537//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538bool FileSpec::IsSourceImplementationFile() const {
539 ConstString extension(GetFileNameExtension());
Zachary Turner95eae422016-09-21 16:01:28 +0000540 if (!extension)
541 return false;
542
543 static RegularExpression g_source_file_regex(llvm::StringRef(
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000544 "^.([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|["
Zachary Turner95eae422016-09-21 16:01:28 +0000545 "cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO]["
546 "rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])"
547 "$"));
548 return g_source_file_regex.Execute(extension.GetStringRef());
Greg Clayton1f746072012-08-29 21:13:06 +0000549}
550
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000551bool FileSpec::IsRelative() const {
552 return !IsAbsolute();
553}
Chaoren Lin372e9062015-06-09 17:54:27 +0000554
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000555bool FileSpec::IsAbsolute() const {
556 llvm::SmallString<64> current_path;
557 GetPath(current_path, false);
558
559 // Early return if the path is empty.
560 if (current_path.empty())
561 return false;
562
563 // We consider paths starting with ~ to be absolute.
564 if (current_path[0] == '~')
565 return true;
566
567 return llvm::sys::path::is_absolute(current_path, m_style);
568}
Zachary Turner827d5d72016-12-16 04:27:00 +0000569
Pavel Labath7d36d722019-01-16 12:30:41 +0000570void FileSpec::MakeAbsolute(const FileSpec &dir) {
571 if (IsRelative())
572 PrependPathComponent(dir);
573}
574
Zachary Turner827d5d72016-12-16 04:27:00 +0000575void llvm::format_provider<FileSpec>::format(const FileSpec &F,
576 raw_ostream &Stream,
577 StringRef Style) {
578 assert(
579 (Style.empty() || Style.equals_lower("F") || Style.equals_lower("D")) &&
580 "Invalid FileSpec style!");
581
582 StringRef dir = F.GetDirectory().GetStringRef();
583 StringRef file = F.GetFilename().GetStringRef();
584
585 if (dir.empty() && file.empty()) {
586 Stream << "(empty)";
587 return;
588 }
589
590 if (Style.equals_lower("F")) {
591 Stream << (file.empty() ? "(empty)" : file);
592 return;
593 }
594
595 // Style is either D or empty, either way we need to print the directory.
596 if (!dir.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000597 // Directory is stored in normalized form, which might be different than
598 // preferred form. In order to handle this, we need to cut off the
599 // filename, then denormalize, then write the entire denorm'ed directory.
Zachary Turner827d5d72016-12-16 04:27:00 +0000600 llvm::SmallString<64> denormalized_dir = dir;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000601 Denormalize(denormalized_dir, F.GetPathStyle());
Zachary Turner827d5d72016-12-16 04:27:00 +0000602 Stream << denormalized_dir;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000603 Stream << GetPreferredPathSeparator(F.GetPathStyle());
Zachary Turner827d5d72016-12-16 04:27:00 +0000604 }
605
606 if (Style.equals_lower("D")) {
607 // We only want to print the directory, so now just exit.
608 if (dir.empty())
609 Stream << "(empty)";
610 return;
611 }
612
613 if (!file.empty())
614 Stream << file;
615}