blob: b805fd8323cff5ac15af8ec597d079c3d8d9eb33 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- FileSpec.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Zachary Turner5713a052017-03-22 18:40:07 +000010#include "lldb/Utility/FileSpec.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000011#include "lldb/Utility/RegularExpression.h"
12#include "lldb/Utility/Stream.h"
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000013
Jonas Devliegheread8d48f2018-06-13 16:23:21 +000014#include "llvm/ADT/SmallString.h"
15#include "llvm/ADT/SmallVector.h"
Caroline Tice391a9602010-09-12 00:10:52 +000016#include "llvm/ADT/StringRef.h"
Jonas Devliegheread8d48f2018-06-13 16:23:21 +000017#include "llvm/ADT/Triple.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/Support/ErrorOr.h"
Zachary Turner3f559742014-08-07 17:33:36 +000020#include "llvm/Support/FileSystem.h"
Greg Clayton38a61402010-12-02 23:20:03 +000021#include "llvm/Support/Program.h"
Jonas Devliegheread8d48f2018-06-13 16:23:21 +000022#include "llvm/Support/raw_ostream.h"
Zachary Turner4479ac12017-04-06 18:12:24 +000023
24#include <algorithm> // for replace, min, unique
25#include <system_error> // for error_code
26#include <vector> // for vector
27
28#include <assert.h> // for assert
Raphael Isemann691e92b2018-08-28 22:17:28 +000029#include <limits.h> // for PATH_MAX
Zachary Turner4479ac12017-04-06 18:12:24 +000030#include <stdio.h> // for size_t, NULL, snpr...
31#include <string.h> // for strcmp
Caroline Tice391a9602010-09-12 00:10:52 +000032
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033using namespace lldb;
34using namespace lldb_private;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035
Chaoren Lin1c614fe2015-05-28 17:02:45 +000036namespace {
37
Pavel Labath2cb7cf82018-05-14 14:52:47 +000038static constexpr FileSpec::Style GetNativeStyle() {
Nico Weberb1cb0b792018-04-10 13:33:45 +000039#if defined(_WIN32)
Pavel Labath2cb7cf82018-05-14 14:52:47 +000040 return FileSpec::Style::windows;
Zachary Turner7d86ee52017-03-08 17:56:08 +000041#else
Pavel Labath2cb7cf82018-05-14 14:52:47 +000042 return FileSpec::Style::posix;
Zachary Turner7d86ee52017-03-08 17:56:08 +000043#endif
44}
45
Pavel Labath2cb7cf82018-05-14 14:52:47 +000046bool PathStyleIsPosix(FileSpec::Style style) {
47 return (style == FileSpec::Style::posix ||
48 (style == FileSpec::Style::native &&
49 GetNativeStyle() == FileSpec::Style::posix));
Chaoren Lin1c614fe2015-05-28 17:02:45 +000050}
51
Pavel Labath2cb7cf82018-05-14 14:52:47 +000052const char *GetPathSeparators(FileSpec::Style style) {
Jonas Devliegheread8d48f2018-06-13 16:23:21 +000053 return llvm::sys::path::get_separator(style).data();
Pavel Labath144119b2016-04-04 14:39:12 +000054}
55
Pavel Labath2cb7cf82018-05-14 14:52:47 +000056char GetPreferredPathSeparator(FileSpec::Style style) {
57 return GetPathSeparators(style)[0];
Pavel Labath144119b2016-04-04 14:39:12 +000058}
59
Pavel Labath2cb7cf82018-05-14 14:52:47 +000060void Denormalize(llvm::SmallVectorImpl<char> &path, FileSpec::Style style) {
61 if (PathStyleIsPosix(style))
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 return;
Chaoren Lin1c614fe2015-05-28 17:02:45 +000063
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 std::replace(path.begin(), path.end(), '/', '\\');
Chaoren Lin1c614fe2015-05-28 17:02:45 +000065}
Greg Clayton86188d82018-05-21 14:14:36 +000066
Pavel Labath144119b2016-04-04 14:39:12 +000067} // end anonymous namespace
68
Pavel Labath2cb7cf82018-05-14 14:52:47 +000069FileSpec::FileSpec() : m_style(GetNativeStyle()) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070
71//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +000072// Default constructor that can take an optional full path to a file on disk.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073//------------------------------------------------------------------
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +000074FileSpec::FileSpec(llvm::StringRef path, Style style) : m_style(style) {
75 SetFile(path, style);
Jim Ingham0909e5f2010-09-16 00:57:33 +000076}
77
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +000078FileSpec::FileSpec(llvm::StringRef path, const llvm::Triple &Triple)
79 : FileSpec{path, Triple.isOSWindows() ? Style::windows : Style::posix} {}
Chaoren Linf34f4102015-05-09 01:21:32 +000080
Jim Ingham0909e5f2010-09-16 00:57:33 +000081//------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082// Copy constructor
83//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000084FileSpec::FileSpec(const FileSpec &rhs)
85 : m_directory(rhs.m_directory), m_filename(rhs.m_filename),
Pavel Labath2cb7cf82018-05-14 14:52:47 +000086 m_is_resolved(rhs.m_is_resolved), m_style(rhs.m_style) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087
88//------------------------------------------------------------------
89// Copy constructor
90//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000091FileSpec::FileSpec(const FileSpec *rhs) : m_directory(), m_filename() {
92 if (rhs)
93 *this = *rhs;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094}
95
96//------------------------------------------------------------------
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000097// Virtual destructor in case anyone inherits from this class.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000099FileSpec::~FileSpec() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000101namespace {
102//------------------------------------------------------------------
103/// Safely get a character at the specified index.
104///
105/// @param[in] path
106/// A full, partial, or relative path to a file.
107///
108/// @param[in] i
109/// An index into path which may or may not be valid.
110///
111/// @return
112/// The character at index \a i if the index is valid, or 0 if
113/// the index is not valid.
114//------------------------------------------------------------------
115inline char safeCharAtIndex(const llvm::StringRef &path, size_t i) {
116 if (i < path.size())
117 return path[i];
118 return 0;
119}
120
121//------------------------------------------------------------------
122/// Check if a path needs to be normalized.
123///
124/// Check if a path needs to be normalized. We currently consider a
125/// path to need normalization if any of the following are true
126/// - path contains "/./"
127/// - path contains "/../"
128/// - path contains "//"
129/// - path ends with "/"
130/// Paths that start with "./" or with "../" are not considered to
131/// need normalization since we aren't trying to resolve the path,
132/// we are just trying to remove redundant things from the path.
133///
134/// @param[in] path
135/// A full, partial, or relative path to a file.
136///
137/// @return
138/// Returns \b true if the path needs to be normalized.
139//------------------------------------------------------------------
140bool needsNormalization(const llvm::StringRef &path) {
141 if (path.empty())
142 return false;
143 // We strip off leading "." values so these paths need to be normalized
144 if (path[0] == '.')
145 return true;
146 for (auto i = path.find_first_of("\\/"); i != llvm::StringRef::npos;
147 i = path.find_first_of("\\/", i + 1)) {
148 const auto next = safeCharAtIndex(path, i+1);
149 switch (next) {
150 case 0:
151 // path separator char at the end of the string which should be
152 // stripped unless it is the one and only character
153 return i > 0;
154 case '/':
155 case '\\':
156 // two path separator chars in the middle of a path needs to be
157 // normalized
158 if (i > 0)
159 return true;
160 ++i;
161 break;
162
163 case '.': {
164 const auto next_next = safeCharAtIndex(path, i+2);
165 switch (next_next) {
166 default: break;
167 case 0: return true; // ends with "/."
168 case '/':
169 case '\\':
170 return true; // contains "/./"
171 case '.': {
172 const auto next_next_next = safeCharAtIndex(path, i+3);
173 switch (next_next_next) {
174 default: break;
175 case 0: return true; // ends with "/.."
176 case '/':
177 case '\\':
178 return true; // contains "/../"
179 }
180 break;
181 }
182 }
183 }
184 break;
185
186 default:
187 break;
188 }
189 }
190 return false;
191}
192
193
194}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195//------------------------------------------------------------------
196// Assignment operator.
197//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198const FileSpec &FileSpec::operator=(const FileSpec &rhs) {
199 if (this != &rhs) {
200 m_directory = rhs.m_directory;
201 m_filename = rhs.m_filename;
202 m_is_resolved = rhs.m_is_resolved;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000203 m_style = rhs.m_style;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 }
205 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206}
207
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000208void FileSpec::SetFile(llvm::StringRef pathname) { SetFile(pathname, m_style); }
Jonas Devlieghere937348c2018-06-13 22:08:14 +0000209
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000211// Update the contents of this object with a new path. The path will be split
212// up into a directory and filename and stored as uniqued string values for
213// quick comparison and efficient memory usage.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214//------------------------------------------------------------------
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000215void FileSpec::SetFile(llvm::StringRef pathname, Style style) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 m_filename.Clear();
217 m_directory.Clear();
218 m_is_resolved = false;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000219 m_style = (style == Style::native) ? GetNativeStyle() : style;
Zachary Turnerdf62f202014-08-07 17:33:07 +0000220
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000221 if (pathname.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000224 llvm::SmallString<128> resolved(pathname);
Zachary Turnerc7a17ed2014-12-01 23:13:32 +0000225
Greg Clayton776cd7a2018-04-27 15:45:58 +0000226 // Normalize the path by removing ".", ".." and other redundant components.
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000227 if (needsNormalization(resolved))
228 llvm::sys::path::remove_dots(resolved, true, m_style);
Greg Clayton776cd7a2018-04-27 15:45:58 +0000229
230 // Normalize back slashes to forward slashes
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000231 if (m_style == Style::windows)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000232 std::replace(resolved.begin(), resolved.end(), '\\', '/');
Pavel Labatha212c582016-04-14 09:38:06 +0000233
Greg Clayton39d50b72018-05-17 16:12:38 +0000234 if (resolved.empty()) {
235 // If we have no path after normalization set the path to the current
236 // directory. This matches what python does and also a few other path
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000237 // utilities.
Greg Clayton39d50b72018-05-17 16:12:38 +0000238 m_filename.SetString(".");
239 return;
240 }
241
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000242 // Split path into filename and directory. We rely on the underlying char
243 // pointer to be nullptr when the components are empty.
244 llvm::StringRef filename = llvm::sys::path::filename(resolved, m_style);
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000245 if(!filename.empty())
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000246 m_filename.SetString(filename);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000247
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000248 llvm::StringRef directory = llvm::sys::path::parent_path(resolved, m_style);
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000249 if(!directory.empty())
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000250 m_directory.SetString(directory);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251}
252
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000253void FileSpec::SetFile(llvm::StringRef path, const llvm::Triple &Triple) {
254 return SetFile(path, Triple.isOSWindows() ? Style::windows : Style::posix);
Chaoren Lin44145d72015-05-29 19:52:37 +0000255}
256
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000258// Convert to pointer operator. This allows code to check any FileSpec objects
259// to see if they contain anything valid using code such as:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260//
261// if (file_spec)
262// {}
263//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264FileSpec::operator bool() const { return m_filename || m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265
266//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000267// Logical NOT operator. This allows code to check any FileSpec objects to see
268// if they are invalid using code such as:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269//
270// if (!file_spec)
271// {}
272//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273bool FileSpec::operator!() const { return !m_directory && !m_filename; }
274
275bool FileSpec::DirectoryEquals(const FileSpec &rhs) const {
276 const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
277 return ConstString::Equals(m_directory, rhs.m_directory, case_sensitive);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278}
279
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280bool FileSpec::FileEquals(const FileSpec &rhs) const {
281 const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
282 return ConstString::Equals(m_filename, rhs.m_filename, case_sensitive);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000283}
284
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285//------------------------------------------------------------------
286// Equal to operator
287//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288bool FileSpec::operator==(const FileSpec &rhs) const {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000289 return FileEquals(rhs) && DirectoryEquals(rhs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290}
291
292//------------------------------------------------------------------
293// Not equal to operator
294//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296
297//------------------------------------------------------------------
298// Less than operator
299//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000300bool FileSpec::operator<(const FileSpec &rhs) const {
301 return FileSpec::Compare(*this, rhs, true) < 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302}
303
304//------------------------------------------------------------------
305// Dump a FileSpec object to a stream
306//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000307Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) {
308 f.Dump(&s);
309 return s;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310}
311
312//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000313// Clear this object by releasing both the directory and filename string values
314// and making them both the empty string.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000316void FileSpec::Clear() {
317 m_directory.Clear();
318 m_filename.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000319}
320
321//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000322// Compare two FileSpec objects. If "full" is true, then both the directory and
323// the filename must match. If "full" is false, then the directory names for
324// "a" and "b" are only compared if they are both non-empty. This allows a
325// FileSpec object to only contain a filename and it can match FileSpec objects
326// that have matching filenames with different paths.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327//
Adrian Prantl05097242018-04-30 16:49:04 +0000328// Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" and "1" if
329// "a" is greater than "b".
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000330//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) {
332 int result = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333
Kate Stoneb9c1b512016-09-06 20:57:50 +0000334 // case sensitivity of compare
335 const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
Zachary Turner47c03462016-02-24 21:26:47 +0000336
Kate Stoneb9c1b512016-09-06 20:57:50 +0000337 // If full is true, then we must compare both the directory and filename.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000338
Adrian Prantl05097242018-04-30 16:49:04 +0000339 // If full is false, then if either directory is empty, then we match on the
340 // basename only, and if both directories have valid values, we still do a
341 // full compare. This allows for matching when we just have a filename in one
342 // of the FileSpec objects.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 if (full || (a.m_directory && b.m_directory)) {
345 result = ConstString::Compare(a.m_directory, b.m_directory, case_sensitive);
346 if (result)
347 return result;
348 }
349 return ConstString::Compare(a.m_filename, b.m_filename, case_sensitive);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350}
351
Greg Clayton776cd7a2018-04-27 15:45:58 +0000352bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 // case sensitivity of equality test
354 const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000355
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000356 const bool filenames_equal = ConstString::Equals(a.m_filename,
357 b.m_filename,
358 case_sensitive);
Jim Ingham97e4f472017-03-27 19:12:25 +0000359
Greg Clayton776cd7a2018-04-27 15:45:58 +0000360 if (!filenames_equal)
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000361 return false;
Jim Ingham97e4f472017-03-27 19:12:25 +0000362
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty()))
Jim Ingham97e4f472017-03-27 19:12:25 +0000364 return filenames_equal;
Pavel Labath218770b2016-10-31 16:22:07 +0000365
Greg Clayton776cd7a2018-04-27 15:45:58 +0000366 return a == b;
Jim Ingham96a15962014-11-15 01:54:26 +0000367}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368
369//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000370// Dump the object to the supplied stream. If the object contains a valid
371// directory name, it will be displayed followed by a directory delimiter, and
372// the filename.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000374void FileSpec::Dump(Stream *s) const {
375 if (s) {
376 std::string path{GetPath(true)};
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000377 s->PutCString(path);
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000378 char path_separator = GetPreferredPathSeparator(m_style);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379 if (!m_filename && !path.empty() && path.back() != path_separator)
380 s->PutChar(path_separator);
381 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382}
383
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000384FileSpec::Style FileSpec::GetPathStyle() const { return m_style; }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000385
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386//------------------------------------------------------------------
387// Directory string get accessor.
388//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389ConstString &FileSpec::GetDirectory() { return m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390
391//------------------------------------------------------------------
392// Directory string const get accessor.
393//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000394const ConstString &FileSpec::GetDirectory() const { return m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395
396//------------------------------------------------------------------
397// Filename string get accessor.
398//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399ConstString &FileSpec::GetFilename() { return m_filename; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400
401//------------------------------------------------------------------
402// Filename string const get accessor.
403//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404const ConstString &FileSpec::GetFilename() const { return m_filename; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405
406//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000407// Extract the directory and path into a fixed buffer. This is needed as the
408// directory and path are stored in separate string values.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000410size_t FileSpec::GetPath(char *path, size_t path_max_len,
411 bool denormalize) const {
412 if (!path)
413 return 0;
Zachary Turnerb6d99242014-08-08 23:54:35 +0000414
Kate Stoneb9c1b512016-09-06 20:57:50 +0000415 std::string result = GetPath(denormalize);
416 ::snprintf(path, path_max_len, "%s", result.c_str());
417 return std::min(path_max_len - 1, result.length());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418}
419
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420std::string FileSpec::GetPath(bool denormalize) const {
421 llvm::SmallString<64> result;
422 GetPath(result, denormalize);
423 return std::string(result.begin(), result.end());
Jason Molendaa7ae4672013-04-29 09:46:43 +0000424}
425
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426const char *FileSpec::GetCString(bool denormalize) const {
427 return ConstString{GetPath(denormalize)}.AsCString(NULL);
Chaoren Lind3173f32015-05-29 19:52:29 +0000428}
429
Kate Stoneb9c1b512016-09-06 20:57:50 +0000430void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path,
431 bool denormalize) const {
432 path.append(m_directory.GetStringRef().begin(),
433 m_directory.GetStringRef().end());
Greg Clayton776cd7a2018-04-27 15:45:58 +0000434 // Since the path was normalized and all paths use '/' when stored in these
435 // objects, we don't need to look for the actual syntax specific path
436 // separator, we just look for and insert '/'.
437 if (m_directory && m_filename && m_directory.GetStringRef().back() != '/' &&
438 m_filename.GetStringRef().back() != '/')
439 path.insert(path.end(), '/');
Kate Stoneb9c1b512016-09-06 20:57:50 +0000440 path.append(m_filename.GetStringRef().begin(),
441 m_filename.GetStringRef().end());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000442 if (denormalize && !path.empty())
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000443 Denormalize(path, m_style);
Zachary Turner4e8ddf52015-04-09 18:08:50 +0000444}
445
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446ConstString FileSpec::GetFileNameExtension() const {
Jonas Devlieghere9c1a6452018-06-13 16:36:07 +0000447 return ConstString(
448 llvm::sys::path::extension(m_filename.GetStringRef(), m_style));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449}
450
451ConstString FileSpec::GetFileNameStrippingExtension() const {
Jonas Devlieghere9c1a6452018-06-13 16:36:07 +0000452 return ConstString(llvm::sys::path::stem(m_filename.GetStringRef(), m_style));
Enrico Granataa9dbf432011-10-17 21:45:27 +0000453}
454
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000456// Return the size in bytes that this object takes in memory. This returns the
457// size in bytes of this object, not any shared string values it may refer to.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459size_t FileSpec::MemorySize() const {
460 return m_filename.MemorySize() + m_directory.MemorySize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461}
462
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000463FileSpec
464FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465 FileSpec ret = *this;
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000466 ret.AppendPathComponent(component);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 return ret;
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000468}
469
Kate Stoneb9c1b512016-09-06 20:57:50 +0000470FileSpec FileSpec::CopyByRemovingLastPathComponent() const {
Jonas Devlieghere24bd63c42018-06-24 10:18:01 +0000471 llvm::SmallString<64> current_path;
472 GetPath(current_path, false);
473 if (llvm::sys::path::has_parent_path(current_path, m_style))
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000474 return FileSpec(llvm::sys::path::parent_path(current_path, m_style),
Jonas Devlieghere24bd63c42018-06-24 10:18:01 +0000475 m_style);
476 return *this;
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000477}
478
Kate Stoneb9c1b512016-09-06 20:57:50 +0000479ConstString FileSpec::GetLastPathComponent() const {
Jonas Devlieghere24bd63c42018-06-24 10:18:01 +0000480 llvm::SmallString<64> current_path;
481 GetPath(current_path, false);
482 return ConstString(llvm::sys::path::filename(current_path, m_style));
Pavel Labath59d725c2017-01-16 10:07:02 +0000483}
484
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000485void FileSpec::PrependPathComponent(llvm::StringRef component) {
Jonas Devlieghere24bd63c42018-06-24 10:18:01 +0000486 llvm::SmallString<64> new_path(component);
487 llvm::SmallString<64> current_path;
488 GetPath(current_path, false);
489 llvm::sys::path::append(new_path,
490 llvm::sys::path::begin(current_path, m_style),
491 llvm::sys::path::end(current_path), m_style);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000492 SetFile(new_path, m_style);
Chaoren Lind3173f32015-05-29 19:52:29 +0000493}
494
Kate Stoneb9c1b512016-09-06 20:57:50 +0000495void FileSpec::PrependPathComponent(const FileSpec &new_path) {
496 return PrependPathComponent(new_path.GetPath(false));
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000497}
498
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000499void FileSpec::AppendPathComponent(llvm::StringRef component) {
Jonas Devlieghere24bd63c42018-06-24 10:18:01 +0000500 llvm::SmallString<64> current_path;
501 GetPath(current_path, false);
502 llvm::sys::path::append(current_path, m_style, component);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000503 SetFile(current_path, m_style);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504}
505
506void FileSpec::AppendPathComponent(const FileSpec &new_path) {
507 return AppendPathComponent(new_path.GetPath(false));
508}
509
Jonas Devliegheredf8e2912018-05-30 13:03:16 +0000510bool FileSpec::RemoveLastPathComponent() {
511 llvm::SmallString<64> current_path;
512 GetPath(current_path, false);
513 if (llvm::sys::path::has_parent_path(current_path, m_style)) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000514 SetFile(llvm::sys::path::parent_path(current_path, m_style));
Jonas Devliegheredf8e2912018-05-30 13:03:16 +0000515 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 }
Jonas Devliegheredf8e2912018-05-30 13:03:16 +0000517 return false;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000518}
Greg Clayton1f746072012-08-29 21:13:06 +0000519//------------------------------------------------------------------
520/// Returns true if the filespec represents an implementation source
521/// file (files with a ".c", ".cpp", ".m", ".mm" (many more)
522/// extension).
523///
524/// @return
525/// \b true if the filespec represents an implementation source
526/// file, \b false otherwise.
527//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528bool FileSpec::IsSourceImplementationFile() const {
529 ConstString extension(GetFileNameExtension());
Zachary Turner95eae422016-09-21 16:01:28 +0000530 if (!extension)
531 return false;
532
533 static RegularExpression g_source_file_regex(llvm::StringRef(
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000534 "^.([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|["
Zachary Turner95eae422016-09-21 16:01:28 +0000535 "cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO]["
536 "rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])"
537 "$"));
538 return g_source_file_regex.Execute(extension.GetStringRef());
Greg Clayton1f746072012-08-29 21:13:06 +0000539}
540
Jonas Devliegherec1cc3172018-06-25 10:11:53 +0000541bool FileSpec::IsRelative() const {
542 return !IsAbsolute();
543}
Chaoren Lin372e9062015-06-09 17:54:27 +0000544
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000545bool FileSpec::IsAbsolute() const {
546 llvm::SmallString<64> current_path;
547 GetPath(current_path, false);
548
549 // Early return if the path is empty.
550 if (current_path.empty())
551 return false;
552
553 // We consider paths starting with ~ to be absolute.
554 if (current_path[0] == '~')
555 return true;
556
557 return llvm::sys::path::is_absolute(current_path, m_style);
558}
Zachary Turner827d5d72016-12-16 04:27:00 +0000559
560void llvm::format_provider<FileSpec>::format(const FileSpec &F,
561 raw_ostream &Stream,
562 StringRef Style) {
563 assert(
564 (Style.empty() || Style.equals_lower("F") || Style.equals_lower("D")) &&
565 "Invalid FileSpec style!");
566
567 StringRef dir = F.GetDirectory().GetStringRef();
568 StringRef file = F.GetFilename().GetStringRef();
569
570 if (dir.empty() && file.empty()) {
571 Stream << "(empty)";
572 return;
573 }
574
575 if (Style.equals_lower("F")) {
576 Stream << (file.empty() ? "(empty)" : file);
577 return;
578 }
579
580 // Style is either D or empty, either way we need to print the directory.
581 if (!dir.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000582 // Directory is stored in normalized form, which might be different than
583 // preferred form. In order to handle this, we need to cut off the
584 // filename, then denormalize, then write the entire denorm'ed directory.
Zachary Turner827d5d72016-12-16 04:27:00 +0000585 llvm::SmallString<64> denormalized_dir = dir;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000586 Denormalize(denormalized_dir, F.GetPathStyle());
Zachary Turner827d5d72016-12-16 04:27:00 +0000587 Stream << denormalized_dir;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000588 Stream << GetPreferredPathSeparator(F.GetPathStyle());
Zachary Turner827d5d72016-12-16 04:27:00 +0000589 }
590
591 if (Style.equals_lower("D")) {
592 // We only want to print the directory, so now just exit.
593 if (dir.empty())
594 Stream << "(empty)";
595 return;
596 }
597
598 if (!file.empty())
599 Stream << file;
600}