blob: 0a10fcad99c5c51826785b037767d8bd4fe573cd [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 Turner8d48cd62017-03-22 17:33:23 +000013#include "lldb/Utility/TildeExpressionResolver.h"
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000014
Zachary Turner4479ac12017-04-06 18:12:24 +000015#include "llvm/ADT/SmallString.h" // for SmallString
16#include "llvm/ADT/SmallVector.h" // for SmallVectorTemplat...
Caroline Tice391a9602010-09-12 00:10:52 +000017#include "llvm/ADT/StringRef.h"
Zachary Turner4479ac12017-04-06 18:12:24 +000018#include "llvm/ADT/Triple.h" // for Triple
19#include "llvm/ADT/Twine.h" // for Twine
Zachary Turner4479ac12017-04-06 18:12:24 +000020#include "llvm/Support/ErrorOr.h" // for ErrorOr
Zachary Turner3f559742014-08-07 17:33:36 +000021#include "llvm/Support/FileSystem.h"
Greg Clayton38a61402010-12-02 23:20:03 +000022#include "llvm/Support/Path.h"
23#include "llvm/Support/Program.h"
Zachary Turner4479ac12017-04-06 18:12:24 +000024#include "llvm/Support/raw_ostream.h" // for raw_ostream, fs
25
26#include <algorithm> // for replace, min, unique
27#include <system_error> // for error_code
28#include <vector> // for vector
29
30#include <assert.h> // for assert
31#include <stdio.h> // for size_t, NULL, snpr...
32#include <string.h> // for strcmp
Caroline Tice391a9602010-09-12 00:10:52 +000033
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034using namespace lldb;
35using namespace lldb_private;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036
Chaoren Lin1c614fe2015-05-28 17:02:45 +000037namespace {
38
Zachary Turner7d86ee52017-03-08 17:56:08 +000039static constexpr FileSpec::PathSyntax GetNativeSyntax() {
Nico Weberb1cb0b792018-04-10 13:33:45 +000040#if defined(_WIN32)
Zachary Turner7d86ee52017-03-08 17:56:08 +000041 return FileSpec::ePathSyntaxWindows;
42#else
43 return FileSpec::ePathSyntaxPosix;
44#endif
45}
46
Kate Stoneb9c1b512016-09-06 20:57:50 +000047bool PathSyntaxIsPosix(FileSpec::PathSyntax syntax) {
48 return (syntax == FileSpec::ePathSyntaxPosix ||
49 (syntax == FileSpec::ePathSyntaxHostNative &&
Zachary Turner7d86ee52017-03-08 17:56:08 +000050 GetNativeSyntax() == FileSpec::ePathSyntaxPosix));
Chaoren Lin1c614fe2015-05-28 17:02:45 +000051}
52
Kate Stoneb9c1b512016-09-06 20:57:50 +000053const char *GetPathSeparators(FileSpec::PathSyntax syntax) {
54 return PathSyntaxIsPosix(syntax) ? "/" : "\\/";
Pavel Labath144119b2016-04-04 14:39:12 +000055}
56
Zachary Turnerfe83ad82016-09-27 20:48:37 +000057char GetPreferredPathSeparator(FileSpec::PathSyntax syntax) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 return GetPathSeparators(syntax)[0];
Pavel Labath144119b2016-04-04 14:39:12 +000059}
60
Kate Stoneb9c1b512016-09-06 20:57:50 +000061bool IsPathSeparator(char value, FileSpec::PathSyntax syntax) {
62 return value == '/' || (!PathSyntaxIsPosix(syntax) && value == '\\');
Chaoren Lin1c614fe2015-05-28 17:02:45 +000063}
64
Greg Clayton776cd7a2018-04-27 15:45:58 +000065inline llvm::sys::path::Style
66LLVMPathSyntax(FileSpec::PathSyntax lldb_syntax) {
67 switch (lldb_syntax) {
68 case FileSpec::ePathSyntaxPosix:
69 return llvm::sys::path::Style::posix;
70 case FileSpec::ePathSyntaxWindows:
71 return llvm::sys::path::Style::windows;
Greg Clayton776cd7a2018-04-27 15:45:58 +000072 case FileSpec::ePathSyntaxHostNative:
73 return llvm::sys::path::Style::native;
74 };
75 return llvm::sys::path::Style::native;
Chaoren Lin1c614fe2015-05-28 17:02:45 +000076}
77
Kate Stoneb9c1b512016-09-06 20:57:50 +000078void Denormalize(llvm::SmallVectorImpl<char> &path,
79 FileSpec::PathSyntax syntax) {
80 if (PathSyntaxIsPosix(syntax))
81 return;
Chaoren Lin1c614fe2015-05-28 17:02:45 +000082
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 std::replace(path.begin(), path.end(), '/', '\\');
Chaoren Lin1c614fe2015-05-28 17:02:45 +000084}
85
Kate Stoneb9c1b512016-09-06 20:57:50 +000086size_t FilenamePos(llvm::StringRef str, FileSpec::PathSyntax syntax) {
87 if (str.size() == 2 && IsPathSeparator(str[0], syntax) && str[0] == str[1])
88 return 0;
Pavel Labath144119b2016-04-04 14:39:12 +000089
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 if (str.size() > 0 && IsPathSeparator(str.back(), syntax))
91 return str.size() - 1;
Pavel Labath144119b2016-04-04 14:39:12 +000092
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 size_t pos = str.find_last_of(GetPathSeparators(syntax), str.size() - 1);
Pavel Labath144119b2016-04-04 14:39:12 +000094
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 if (!PathSyntaxIsPosix(syntax) && pos == llvm::StringRef::npos)
96 pos = str.find_last_of(':', str.size() - 2);
Pavel Labath144119b2016-04-04 14:39:12 +000097
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 if (pos == llvm::StringRef::npos ||
99 (pos == 1 && IsPathSeparator(str[0], syntax)))
100 return 0;
Pavel Labath144119b2016-04-04 14:39:12 +0000101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 return pos + 1;
Chaoren Lin1c614fe2015-05-28 17:02:45 +0000103}
104
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105size_t RootDirStart(llvm::StringRef str, FileSpec::PathSyntax syntax) {
106 // case "c:/"
107 if (!PathSyntaxIsPosix(syntax) &&
108 (str.size() > 2 && str[1] == ':' && IsPathSeparator(str[2], syntax)))
109 return 2;
Pavel Labath144119b2016-04-04 14:39:12 +0000110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 // case "//"
112 if (str.size() == 2 && IsPathSeparator(str[0], syntax) && str[0] == str[1])
Pavel Labath144119b2016-04-04 14:39:12 +0000113 return llvm::StringRef::npos;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114
115 // case "//net"
116 if (str.size() > 3 && IsPathSeparator(str[0], syntax) && str[0] == str[1] &&
117 !IsPathSeparator(str[2], syntax))
118 return str.find_first_of(GetPathSeparators(syntax), 2);
119
120 // case "/"
121 if (str.size() > 0 && IsPathSeparator(str[0], syntax))
122 return 0;
123
124 return llvm::StringRef::npos;
Pavel Labath144119b2016-04-04 14:39:12 +0000125}
126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127size_t ParentPathEnd(llvm::StringRef path, FileSpec::PathSyntax syntax) {
128 size_t end_pos = FilenamePos(path, syntax);
Pavel Labath144119b2016-04-04 14:39:12 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 bool filename_was_sep =
131 path.size() > 0 && IsPathSeparator(path[end_pos], syntax);
Pavel Labath144119b2016-04-04 14:39:12 +0000132
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 // Skip separators except for root dir.
134 size_t root_dir_pos = RootDirStart(path.substr(0, end_pos), syntax);
Pavel Labath144119b2016-04-04 14:39:12 +0000135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
137 IsPathSeparator(path[end_pos - 1], syntax))
138 --end_pos;
Pavel Labath144119b2016-04-04 14:39:12 +0000139
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
141 return llvm::StringRef::npos;
Pavel Labath144119b2016-04-04 14:39:12 +0000142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143 return end_pos;
Pavel Labath144119b2016-04-04 14:39:12 +0000144}
145
146} // end anonymous namespace
147
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148void FileSpec::Resolve(llvm::SmallVectorImpl<char> &path) {
149 if (path.empty())
150 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151
Zachary Turner8d48cd62017-03-22 17:33:23 +0000152 llvm::SmallString<32> Source(path.begin(), path.end());
153 StandardTildeExpressionResolver Resolver;
154 Resolver.ResolveFullPath(Source, path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 // Save a copy of the original path that's passed in
157 llvm::SmallString<128> original_path(path.begin(), path.end());
Jason Molenda671a29d2015-02-25 02:35:25 +0000158
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 llvm::sys::fs::make_absolute(path);
160 if (!llvm::sys::fs::exists(path)) {
161 path.clear();
162 path.append(original_path.begin(), original_path.end());
163 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164}
165
Zachary Turner7d86ee52017-03-08 17:56:08 +0000166FileSpec::FileSpec() : m_syntax(GetNativeSyntax()) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167
168//------------------------------------------------------------------
169// Default constructor that can take an optional full path to a
170// file on disk.
171//------------------------------------------------------------------
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000172FileSpec::FileSpec(llvm::StringRef path, bool resolve_path, PathSyntax syntax)
Sam McCall6f43d9d2016-11-15 10:58:16 +0000173 : m_syntax(syntax) {
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000174 SetFile(path, resolve_path, syntax);
Jim Ingham0909e5f2010-09-16 00:57:33 +0000175}
176
Zachary Turner8c6b5462017-03-06 23:42:44 +0000177FileSpec::FileSpec(llvm::StringRef path, bool resolve_path,
178 const llvm::Triple &Triple)
179 : FileSpec{path, resolve_path,
180 Triple.isOSWindows() ? ePathSyntaxWindows : ePathSyntaxPosix} {}
Chaoren Linf34f4102015-05-09 01:21:32 +0000181
Jim Ingham0909e5f2010-09-16 00:57:33 +0000182//------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183// Copy constructor
184//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185FileSpec::FileSpec(const FileSpec &rhs)
186 : m_directory(rhs.m_directory), m_filename(rhs.m_filename),
187 m_is_resolved(rhs.m_is_resolved), m_syntax(rhs.m_syntax) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188
189//------------------------------------------------------------------
190// Copy constructor
191//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192FileSpec::FileSpec(const FileSpec *rhs) : m_directory(), m_filename() {
193 if (rhs)
194 *this = *rhs;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195}
196
197//------------------------------------------------------------------
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000198// Virtual destructor in case anyone inherits from this class.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200FileSpec::~FileSpec() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201
Greg Clayton776cd7a2018-04-27 15:45:58 +0000202namespace {
203//------------------------------------------------------------------
204/// Safely get a character at the specified index.
205///
206/// @param[in] path
207/// A full, partial, or relative path to a file.
208///
209/// @param[in] i
210/// An index into path which may or may not be valid.
211///
212/// @return
213/// The character at index \a i if the index is valid, or 0 if
214/// the index is not valid.
215//------------------------------------------------------------------
216inline char safeCharAtIndex(const llvm::StringRef &path, size_t i) {
217 if (i < path.size())
218 return path[i];
219 return 0;
220}
221
222//------------------------------------------------------------------
223/// Check if a path needs to be normalized.
224///
225/// Check if a path needs to be normalized. We currently consider a
226/// path to need normalization if any of the following are true
227/// - path contains "/./"
228/// - path contains "/../"
229/// - path contains "//"
230/// - path ends with "/"
231/// Paths that start with "./" or with "../" are not considered to
232/// need normalization since we aren't trying to resolve the path,
233/// we are just trying to remove redundant things from the path.
234///
235/// @param[in] path
236/// A full, partial, or relative path to a file.
237///
Greg Clayton776cd7a2018-04-27 15:45:58 +0000238/// @return
239/// Returns \b true if the path needs to be normalized.
240//------------------------------------------------------------------
Pavel Labath410c5ac2018-04-30 12:59:14 +0000241bool needsNormalization(const llvm::StringRef &path) {
Greg Clayton27a0e102018-04-27 21:10:07 +0000242 if (path.empty())
243 return false;
244 // We strip off leading "." values so these paths need to be normalized
245 if (path[0] == '.')
246 return true;
247 for (auto i = path.find_first_of("\\/"); i != llvm::StringRef::npos;
248 i = path.find_first_of("\\/", i + 1)) {
Greg Clayton776cd7a2018-04-27 15:45:58 +0000249 const auto next = safeCharAtIndex(path, i+1);
250 switch (next) {
251 case 0:
252 // path separator char at the end of the string which should be
253 // stripped unless it is the one and only character
254 return i > 0;
255 case '/':
256 case '\\':
257 // two path separator chars in the middle of a path needs to be
258 // normalized
Greg Clayton27a0e102018-04-27 21:10:07 +0000259 if (i > 0)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000260 return true;
261 ++i;
262 break;
263
264 case '.': {
265 const auto next_next = safeCharAtIndex(path, i+2);
266 switch (next_next) {
267 default: break;
268 case 0: return true; // ends with "/."
269 case '/':
270 case '\\':
Greg Clayton27a0e102018-04-27 21:10:07 +0000271 return true; // contains "/./"
Greg Clayton776cd7a2018-04-27 15:45:58 +0000272 case '.': {
273 const auto next_next_next = safeCharAtIndex(path, i+3);
274 switch (next_next_next) {
275 default: break;
276 case 0: return true; // ends with "/.."
277 case '/':
278 case '\\':
Greg Clayton27a0e102018-04-27 21:10:07 +0000279 return true; // contains "/../"
Greg Clayton776cd7a2018-04-27 15:45:58 +0000280 }
281 break;
282 }
283 }
284 }
285 break;
286
287 default:
288 break;
289 }
290 }
291 return false;
292}
293
294
295}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296//------------------------------------------------------------------
297// Assignment operator.
298//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299const FileSpec &FileSpec::operator=(const FileSpec &rhs) {
300 if (this != &rhs) {
301 m_directory = rhs.m_directory;
302 m_filename = rhs.m_filename;
303 m_is_resolved = rhs.m_is_resolved;
304 m_syntax = rhs.m_syntax;
305 }
306 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307}
308
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309//------------------------------------------------------------------
310// Update the contents of this object with a new path. The path will
311// be split up into a directory and filename and stored as uniqued
312// string values for quick comparison and efficient memory usage.
313//------------------------------------------------------------------
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000314void FileSpec::SetFile(llvm::StringRef pathname, bool resolve,
315 PathSyntax syntax) {
316 // CLEANUP: Use StringRef for string handling. This function is kind of a
317 // mess and the unclear semantics of RootDirStart and ParentPathEnd make
318 // it very difficult to understand this function. There's no reason this
319 // function should be particularly complicated or difficult to understand.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320 m_filename.Clear();
321 m_directory.Clear();
322 m_is_resolved = false;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000323 m_syntax = (syntax == ePathSyntaxHostNative) ? GetNativeSyntax() : syntax;
Zachary Turnerdf62f202014-08-07 17:33:07 +0000324
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000325 if (pathname.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000326 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328 llvm::SmallString<64> resolved(pathname);
Zachary Turnerdf62f202014-08-07 17:33:07 +0000329
Kate Stoneb9c1b512016-09-06 20:57:50 +0000330 if (resolve) {
331 FileSpec::Resolve(resolved);
332 m_is_resolved = true;
333 }
Zachary Turnerc7a17ed2014-12-01 23:13:32 +0000334
Greg Clayton776cd7a2018-04-27 15:45:58 +0000335 // Normalize the path by removing ".", ".." and other redundant components.
Pavel Labath410c5ac2018-04-30 12:59:14 +0000336 if (needsNormalization(resolved))
337 llvm::sys::path::remove_dots(resolved, true, LLVMPathSyntax(m_syntax));
Greg Clayton776cd7a2018-04-27 15:45:58 +0000338
339 // Normalize back slashes to forward slashes
Pavel Labath410c5ac2018-04-30 12:59:14 +0000340 if (m_syntax == FileSpec::ePathSyntaxWindows)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000341 std::replace(resolved.begin(), resolved.end(), '\\', '/');
Pavel Labatha212c582016-04-14 09:38:06 +0000342
Kate Stoneb9c1b512016-09-06 20:57:50 +0000343 llvm::StringRef resolve_path_ref(resolved.c_str());
Zachary Turner827d5d72016-12-16 04:27:00 +0000344 size_t dir_end = ParentPathEnd(resolve_path_ref, m_syntax);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000345 if (dir_end == 0) {
346 m_filename.SetString(resolve_path_ref);
347 return;
348 }
Pavel Labath144119b2016-04-04 14:39:12 +0000349
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 m_directory.SetString(resolve_path_ref.substr(0, dir_end));
Pavel Labath144119b2016-04-04 14:39:12 +0000351
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352 size_t filename_begin = dir_end;
Zachary Turner827d5d72016-12-16 04:27:00 +0000353 size_t root_dir_start = RootDirStart(resolve_path_ref, m_syntax);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354 while (filename_begin != llvm::StringRef::npos &&
355 filename_begin < resolve_path_ref.size() &&
356 filename_begin != root_dir_start &&
Zachary Turner827d5d72016-12-16 04:27:00 +0000357 IsPathSeparator(resolve_path_ref[filename_begin], m_syntax))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000358 ++filename_begin;
359 m_filename.SetString((filename_begin == llvm::StringRef::npos ||
360 filename_begin >= resolve_path_ref.size())
361 ? "."
362 : resolve_path_ref.substr(filename_begin));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363}
364
Zachary Turner8c6b5462017-03-06 23:42:44 +0000365void FileSpec::SetFile(llvm::StringRef path, bool resolve,
366 const llvm::Triple &Triple) {
367 return SetFile(path, resolve,
368 Triple.isOSWindows() ? ePathSyntaxWindows : ePathSyntaxPosix);
Chaoren Lin44145d72015-05-29 19:52:37 +0000369}
370
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371//----------------------------------------------------------------------
372// Convert to pointer operator. This allows code to check any FileSpec
373// objects to see if they contain anything valid using code such as:
374//
375// if (file_spec)
376// {}
377//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378FileSpec::operator bool() const { return m_filename || m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379
380//----------------------------------------------------------------------
381// Logical NOT operator. This allows code to check any FileSpec
382// objects to see if they are invalid using code such as:
383//
384// if (!file_spec)
385// {}
386//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387bool FileSpec::operator!() const { return !m_directory && !m_filename; }
388
389bool FileSpec::DirectoryEquals(const FileSpec &rhs) const {
390 const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
391 return ConstString::Equals(m_directory, rhs.m_directory, case_sensitive);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392}
393
Kate Stoneb9c1b512016-09-06 20:57:50 +0000394bool FileSpec::FileEquals(const FileSpec &rhs) const {
395 const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
396 return ConstString::Equals(m_filename, rhs.m_filename, case_sensitive);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000397}
398
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399//------------------------------------------------------------------
400// Equal to operator
401//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402bool FileSpec::operator==(const FileSpec &rhs) const {
403 if (!FileEquals(rhs))
404 return false;
405 if (DirectoryEquals(rhs))
406 return true;
Zachary Turner47c03462016-02-24 21:26:47 +0000407
Kate Stoneb9c1b512016-09-06 20:57:50 +0000408 // TODO: determine if we want to keep this code in here.
409 // The code below was added to handle a case where we were
410 // trying to set a file and line breakpoint and one path
411 // was resolved, and the other not and the directory was
412 // in a mount point that resolved to a more complete path:
413 // "/tmp/a.c" == "/private/tmp/a.c". I might end up pulling
414 // this out...
415 if (IsResolved() && rhs.IsResolved()) {
416 // Both paths are resolved, no need to look further...
417 return false;
418 }
Zachary Turner47c03462016-02-24 21:26:47 +0000419
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 FileSpec resolved_lhs(*this);
Zachary Turner47c03462016-02-24 21:26:47 +0000421
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422 // If "this" isn't resolved, resolve it
423 if (!IsResolved()) {
424 if (resolved_lhs.ResolvePath()) {
425 // This path wasn't resolved but now it is. Check if the resolved
426 // directory is the same as our unresolved directory, and if so,
427 // we can mark this object as resolved to avoid more future resolves
428 m_is_resolved = (m_directory == resolved_lhs.m_directory);
429 } else
430 return false;
431 }
Zachary Turner47c03462016-02-24 21:26:47 +0000432
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433 FileSpec resolved_rhs(rhs);
434 if (!rhs.IsResolved()) {
435 if (resolved_rhs.ResolvePath()) {
436 // rhs's path wasn't resolved but now it is. Check if the resolved
437 // directory is the same as rhs's unresolved directory, and if so,
438 // we can mark this object as resolved to avoid more future resolves
439 rhs.m_is_resolved = (rhs.m_directory == resolved_rhs.m_directory);
440 } else
441 return false;
442 }
Zachary Turner47c03462016-02-24 21:26:47 +0000443
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444 // If we reach this point in the code we were able to resolve both paths
445 // and since we only resolve the paths if the basenames are equal, then
446 // we can just check if both directories are equal...
447 return DirectoryEquals(rhs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448}
449
450//------------------------------------------------------------------
451// Not equal to operator
452//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454
455//------------------------------------------------------------------
456// Less than operator
457//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000458bool FileSpec::operator<(const FileSpec &rhs) const {
459 return FileSpec::Compare(*this, rhs, true) < 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460}
461
462//------------------------------------------------------------------
463// Dump a FileSpec object to a stream
464//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) {
466 f.Dump(&s);
467 return s;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468}
469
470//------------------------------------------------------------------
471// Clear this object by releasing both the directory and filename
472// string values and making them both the empty string.
473//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474void FileSpec::Clear() {
475 m_directory.Clear();
476 m_filename.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477}
478
479//------------------------------------------------------------------
480// Compare two FileSpec objects. If "full" is true, then both
481// the directory and the filename must match. If "full" is false,
482// then the directory names for "a" and "b" are only compared if
483// they are both non-empty. This allows a FileSpec object to only
484// contain a filename and it can match FileSpec objects that have
485// matching filenames with different paths.
486//
487// Return -1 if the "a" is less than "b", 0 if "a" is equal to "b"
488// and "1" if "a" is greater than "b".
489//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) {
491 int result = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492
Kate Stoneb9c1b512016-09-06 20:57:50 +0000493 // case sensitivity of compare
494 const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
Zachary Turner47c03462016-02-24 21:26:47 +0000495
Kate Stoneb9c1b512016-09-06 20:57:50 +0000496 // If full is true, then we must compare both the directory and filename.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497
Kate Stoneb9c1b512016-09-06 20:57:50 +0000498 // If full is false, then if either directory is empty, then we match on
499 // the basename only, and if both directories have valid values, we still
500 // do a full compare. This allows for matching when we just have a filename
501 // in one of the FileSpec objects.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503 if (full || (a.m_directory && b.m_directory)) {
504 result = ConstString::Compare(a.m_directory, b.m_directory, case_sensitive);
505 if (result)
506 return result;
507 }
508 return ConstString::Compare(a.m_filename, b.m_filename, case_sensitive);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509}
510
Greg Clayton776cd7a2018-04-27 15:45:58 +0000511bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full) {
Jim Ingham97e4f472017-03-27 19:12:25 +0000512
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 // case sensitivity of equality test
514 const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
Jim Ingham97e4f472017-03-27 19:12:25 +0000515
Greg Clayton776cd7a2018-04-27 15:45:58 +0000516 const bool filenames_equal = ConstString::Equals(a.m_filename,
517 b.m_filename,
518 case_sensitive);
Jim Ingham97e4f472017-03-27 19:12:25 +0000519
Greg Clayton776cd7a2018-04-27 15:45:58 +0000520 if (!filenames_equal)
Jim Ingham97e4f472017-03-27 19:12:25 +0000521 return false;
522
Kate Stoneb9c1b512016-09-06 20:57:50 +0000523 if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty()))
Jim Ingham97e4f472017-03-27 19:12:25 +0000524 return filenames_equal;
Pavel Labath218770b2016-10-31 16:22:07 +0000525
Greg Clayton776cd7a2018-04-27 15:45:58 +0000526 return a == b;
Jim Ingham96a15962014-11-15 01:54:26 +0000527}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000528
529//------------------------------------------------------------------
530// Dump the object to the supplied stream. If the object contains
531// a valid directory name, it will be displayed followed by a
532// directory delimiter, and the filename.
533//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000534void FileSpec::Dump(Stream *s) const {
535 if (s) {
536 std::string path{GetPath(true)};
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000537 s->PutCString(path);
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000538 char path_separator = GetPreferredPathSeparator(m_syntax);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000539 if (!m_filename && !path.empty() && path.back() != path_separator)
540 s->PutChar(path_separator);
541 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000542}
543
544//------------------------------------------------------------------
545// Returns true if the file exists.
546//------------------------------------------------------------------
Zachary Turner7d86ee52017-03-08 17:56:08 +0000547bool FileSpec::Exists() const { return llvm::sys::fs::exists(GetPath()); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000548
Kate Stoneb9c1b512016-09-06 20:57:50 +0000549bool FileSpec::Readable() const {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000550 return GetPermissions() & llvm::sys::fs::perms::all_read;
Greg Clayton5acc1252014-08-15 18:00:45 +0000551}
552
Kate Stoneb9c1b512016-09-06 20:57:50 +0000553bool FileSpec::ResolveExecutableLocation() {
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000554 // CLEANUP: Use StringRef for string handling.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000555 if (!m_directory) {
556 const char *file_cstr = m_filename.GetCString();
557 if (file_cstr) {
558 const std::string file_str(file_cstr);
559 llvm::ErrorOr<std::string> error_or_path =
560 llvm::sys::findProgramByName(file_str);
561 if (!error_or_path)
Jim Ingham0909e5f2010-09-16 00:57:33 +0000562 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563 std::string path = error_or_path.get();
564 llvm::StringRef dir_ref = llvm::sys::path::parent_path(path);
565 if (!dir_ref.empty()) {
566 // FindProgramByName returns "." if it can't find the file.
567 if (strcmp(".", dir_ref.data()) == 0)
568 return false;
569
570 m_directory.SetCString(dir_ref.data());
571 if (Exists())
572 return true;
573 else {
574 // If FindProgramByName found the file, it returns the directory +
575 // filename in its return results.
576 // We need to separate them.
577 FileSpec tmp_file(dir_ref.data(), false);
578 if (tmp_file.Exists()) {
579 m_directory = tmp_file.m_directory;
580 return true;
581 }
582 }
583 }
584 }
585 }
586
587 return false;
Jim Ingham0909e5f2010-09-16 00:57:33 +0000588}
589
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590bool FileSpec::ResolvePath() {
591 if (m_is_resolved)
592 return true; // We have already resolved this path
593
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594 // SetFile(...) will set m_is_resolved correctly if it can resolve the path
Pavel Labath9bd69ad2017-03-13 09:46:15 +0000595 SetFile(GetPath(false), true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000596 return m_is_resolved;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000597}
598
Kate Stoneb9c1b512016-09-06 20:57:50 +0000599uint64_t FileSpec::GetByteSize() const {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000600 uint64_t Size = 0;
601 if (llvm::sys::fs::file_size(GetPath(), Size))
602 return 0;
603 return Size;
Zachary Turnerdf62f202014-08-07 17:33:07 +0000604}
605
Kate Stoneb9c1b512016-09-06 20:57:50 +0000606FileSpec::PathSyntax FileSpec::GetPathSyntax() const { return m_syntax; }
607
Pavel Labath30e6cbf2017-03-07 13:19:15 +0000608uint32_t FileSpec::GetPermissions() const {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000609 namespace fs = llvm::sys::fs;
610 fs::file_status st;
611 if (fs::status(GetPath(), st, false))
612 return fs::perms::perms_not_known;
613
614 return st.permissions();
Greg Claytonfbb76342013-11-20 21:07:01 +0000615}
616
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617//------------------------------------------------------------------
618// Directory string get accessor.
619//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000620ConstString &FileSpec::GetDirectory() { return m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621
622//------------------------------------------------------------------
623// Directory string const get accessor.
624//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625const ConstString &FileSpec::GetDirectory() const { return m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626
627//------------------------------------------------------------------
628// Filename string get accessor.
629//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000630ConstString &FileSpec::GetFilename() { return m_filename; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000631
632//------------------------------------------------------------------
633// Filename string const get accessor.
634//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000635const ConstString &FileSpec::GetFilename() const { return m_filename; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000636
637//------------------------------------------------------------------
638// Extract the directory and path into a fixed buffer. This is
639// needed as the directory and path are stored in separate string
640// values.
641//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000642size_t FileSpec::GetPath(char *path, size_t path_max_len,
643 bool denormalize) const {
644 if (!path)
645 return 0;
Zachary Turnerb6d99242014-08-08 23:54:35 +0000646
Kate Stoneb9c1b512016-09-06 20:57:50 +0000647 std::string result = GetPath(denormalize);
648 ::snprintf(path, path_max_len, "%s", result.c_str());
649 return std::min(path_max_len - 1, result.length());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650}
651
Kate Stoneb9c1b512016-09-06 20:57:50 +0000652std::string FileSpec::GetPath(bool denormalize) const {
653 llvm::SmallString<64> result;
654 GetPath(result, denormalize);
655 return std::string(result.begin(), result.end());
Jason Molendaa7ae4672013-04-29 09:46:43 +0000656}
657
Kate Stoneb9c1b512016-09-06 20:57:50 +0000658const char *FileSpec::GetCString(bool denormalize) const {
659 return ConstString{GetPath(denormalize)}.AsCString(NULL);
Chaoren Lind3173f32015-05-29 19:52:29 +0000660}
661
Kate Stoneb9c1b512016-09-06 20:57:50 +0000662void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path,
663 bool denormalize) const {
664 path.append(m_directory.GetStringRef().begin(),
665 m_directory.GetStringRef().end());
Greg Clayton776cd7a2018-04-27 15:45:58 +0000666 // Since the path was normalized and all paths use '/' when stored in these
667 // objects, we don't need to look for the actual syntax specific path
668 // separator, we just look for and insert '/'.
669 if (m_directory && m_filename && m_directory.GetStringRef().back() != '/' &&
670 m_filename.GetStringRef().back() != '/')
671 path.insert(path.end(), '/');
Kate Stoneb9c1b512016-09-06 20:57:50 +0000672 path.append(m_filename.GetStringRef().begin(),
673 m_filename.GetStringRef().end());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000674 if (denormalize && !path.empty())
675 Denormalize(path, m_syntax);
Zachary Turner4e8ddf52015-04-09 18:08:50 +0000676}
677
Kate Stoneb9c1b512016-09-06 20:57:50 +0000678ConstString FileSpec::GetFileNameExtension() const {
679 if (m_filename) {
Enrico Granataa9dbf432011-10-17 21:45:27 +0000680 const char *filename = m_filename.GetCString();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000681 const char *dot_pos = strrchr(filename, '.');
682 if (dot_pos && dot_pos[1] != '\0')
683 return ConstString(dot_pos + 1);
684 }
685 return ConstString();
686}
687
688ConstString FileSpec::GetFileNameStrippingExtension() const {
689 const char *filename = m_filename.GetCString();
690 if (filename == NULL)
691 return ConstString();
692
693 const char *dot_pos = strrchr(filename, '.');
694 if (dot_pos == NULL)
695 return m_filename;
696
697 return ConstString(filename, dot_pos - filename);
Enrico Granataa9dbf432011-10-17 21:45:27 +0000698}
699
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000700//------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701// Return the size in bytes that this object takes in memory. This
702// returns the size in bytes of this object, not any shared string
703// values it may refer to.
704//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000705size_t FileSpec::MemorySize() const {
706 return m_filename.MemorySize() + m_directory.MemorySize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000707}
708
Zachary Turner1f875342017-03-13 02:44:39 +0000709void FileSpec::EnumerateDirectory(llvm::StringRef dir_path,
710 bool find_directories, bool find_files,
711 bool find_other,
712 EnumerateDirectoryCallbackType callback,
713 void *callback_baton) {
714 namespace fs = llvm::sys::fs;
715 std::error_code EC;
716 fs::recursive_directory_iterator Iter(dir_path, EC);
717 fs::recursive_directory_iterator End;
718 for (; Iter != End && !EC; Iter.increment(EC)) {
719 const auto &Item = *Iter;
Peter Collingbourne0dfdb442017-10-10 22:19:46 +0000720 llvm::ErrorOr<fs::basic_file_status> Status = Item.status();
721 if (!Status)
Zachary Turner1f875342017-03-13 02:44:39 +0000722 break;
Peter Collingbourne0dfdb442017-10-10 22:19:46 +0000723 if (!find_files && fs::is_regular_file(*Status))
Zachary Turner1f875342017-03-13 02:44:39 +0000724 continue;
Peter Collingbourne0dfdb442017-10-10 22:19:46 +0000725 if (!find_directories && fs::is_directory(*Status))
Zachary Turner1f875342017-03-13 02:44:39 +0000726 continue;
Peter Collingbourne0dfdb442017-10-10 22:19:46 +0000727 if (!find_other && fs::is_other(*Status))
Zachary Turner1f875342017-03-13 02:44:39 +0000728 continue;
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000729
Zachary Turner1f875342017-03-13 02:44:39 +0000730 FileSpec Spec(Item.path(), false);
Peter Collingbourne0dfdb442017-10-10 22:19:46 +0000731 auto Result = callback(callback_baton, Status->type(), Spec);
Zachary Turner1f875342017-03-13 02:44:39 +0000732 if (Result == eEnumerateDirectoryResultQuit)
733 return;
734 if (Result == eEnumerateDirectoryResultNext) {
735 // Default behavior is to recurse. Opt out if the callback doesn't want
736 // this behavior.
737 Iter.no_push();
Greg Clayton58c65f02015-06-29 18:29:00 +0000738 }
Zachary Turner1f875342017-03-13 02:44:39 +0000739 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000740}
741
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000742FileSpec
743FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000744 FileSpec ret = *this;
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000745 ret.AppendPathComponent(component);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000746 return ret;
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000747}
748
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749FileSpec FileSpec::CopyByRemovingLastPathComponent() const {
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000750 // CLEANUP: Use StringRef for string handling.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000751 const bool resolve = false;
752 if (m_filename.IsEmpty() && m_directory.IsEmpty())
753 return FileSpec("", resolve);
754 if (m_directory.IsEmpty())
755 return FileSpec("", resolve);
756 if (m_filename.IsEmpty()) {
757 const char *dir_cstr = m_directory.GetCString();
758 const char *last_slash_ptr = ::strrchr(dir_cstr, '/');
759
760 // check for obvious cases before doing the full thing
761 if (!last_slash_ptr)
762 return FileSpec("", resolve);
763 if (last_slash_ptr == dir_cstr)
764 return FileSpec("/", resolve);
765
766 size_t last_slash_pos = last_slash_ptr - dir_cstr + 1;
767 ConstString new_path(dir_cstr, last_slash_pos);
768 return FileSpec(new_path.GetCString(), resolve);
769 } else
770 return FileSpec(m_directory.GetCString(), resolve);
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000771}
772
Kate Stoneb9c1b512016-09-06 20:57:50 +0000773ConstString FileSpec::GetLastPathComponent() const {
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000774 // CLEANUP: Use StringRef for string handling.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775 if (m_filename)
776 return m_filename;
777 if (m_directory) {
778 const char *dir_cstr = m_directory.GetCString();
779 const char *last_slash_ptr = ::strrchr(dir_cstr, '/');
780 if (last_slash_ptr == NULL)
781 return m_directory;
782 if (last_slash_ptr == dir_cstr) {
783 if (last_slash_ptr[1] == 0)
784 return ConstString(last_slash_ptr);
785 else
786 return ConstString(last_slash_ptr + 1);
787 }
788 if (last_slash_ptr[1] != 0)
789 return ConstString(last_slash_ptr + 1);
790 const char *penultimate_slash_ptr = last_slash_ptr;
791 while (*penultimate_slash_ptr) {
792 --penultimate_slash_ptr;
793 if (penultimate_slash_ptr == dir_cstr)
794 break;
795 if (*penultimate_slash_ptr == '/')
796 break;
797 }
798 ConstString result(penultimate_slash_ptr + 1,
799 last_slash_ptr - penultimate_slash_ptr);
800 return result;
801 }
802 return ConstString();
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000803}
804
Pavel Labath59d725c2017-01-16 10:07:02 +0000805static std::string
806join_path_components(FileSpec::PathSyntax syntax,
807 const std::vector<llvm::StringRef> components) {
808 std::string result;
809 for (size_t i = 0; i < components.size(); ++i) {
810 if (components[i].empty())
811 continue;
812 result += components[i];
813 if (i != components.size() - 1 &&
814 !IsPathSeparator(components[i].back(), syntax))
815 result += GetPreferredPathSeparator(syntax);
816 }
817
818 return result;
819}
820
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000821void FileSpec::PrependPathComponent(llvm::StringRef component) {
822 if (component.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000823 return;
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000824
Kate Stoneb9c1b512016-09-06 20:57:50 +0000825 const bool resolve = false;
826 if (m_filename.IsEmpty() && m_directory.IsEmpty()) {
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000827 SetFile(component, resolve);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000828 return;
829 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000830
Pavel Labath59d725c2017-01-16 10:07:02 +0000831 std::string result =
832 join_path_components(m_syntax, {component, m_directory.GetStringRef(),
833 m_filename.GetStringRef()});
Pavel Labath238169d2017-01-16 12:15:42 +0000834 SetFile(result, resolve, m_syntax);
Chaoren Lind3173f32015-05-29 19:52:29 +0000835}
836
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837void FileSpec::PrependPathComponent(const FileSpec &new_path) {
838 return PrependPathComponent(new_path.GetPath(false));
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000839}
840
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000841void FileSpec::AppendPathComponent(llvm::StringRef component) {
842 if (component.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000843 return;
844
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000845 component = component.drop_while(
846 [this](char c) { return IsPathSeparator(c, m_syntax); });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847
Pavel Labath59d725c2017-01-16 10:07:02 +0000848 std::string result =
849 join_path_components(m_syntax, {m_directory.GetStringRef(),
850 m_filename.GetStringRef(), component});
Kate Stoneb9c1b512016-09-06 20:57:50 +0000851
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000852 SetFile(result, false, m_syntax);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000853}
854
855void FileSpec::AppendPathComponent(const FileSpec &new_path) {
856 return AppendPathComponent(new_path.GetPath(false));
857}
858
859void FileSpec::RemoveLastPathComponent() {
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000860 // CLEANUP: Use StringRef for string handling.
861
Kate Stoneb9c1b512016-09-06 20:57:50 +0000862 const bool resolve = false;
863 if (m_filename.IsEmpty() && m_directory.IsEmpty()) {
864 SetFile("", resolve);
865 return;
866 }
867 if (m_directory.IsEmpty()) {
868 SetFile("", resolve);
869 return;
870 }
871 if (m_filename.IsEmpty()) {
872 const char *dir_cstr = m_directory.GetCString();
873 const char *last_slash_ptr = ::strrchr(dir_cstr, '/');
874
875 // check for obvious cases before doing the full thing
876 if (!last_slash_ptr) {
877 SetFile("", resolve);
878 return;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000879 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000880 if (last_slash_ptr == dir_cstr) {
881 SetFile("/", resolve);
882 return;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000883 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000884 size_t last_slash_pos = last_slash_ptr - dir_cstr + 1;
885 ConstString new_path(dir_cstr, last_slash_pos);
886 SetFile(new_path.GetCString(), resolve);
887 } else
888 SetFile(m_directory.GetCString(), resolve);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000889}
Greg Clayton1f746072012-08-29 21:13:06 +0000890//------------------------------------------------------------------
891/// Returns true if the filespec represents an implementation source
892/// file (files with a ".c", ".cpp", ".m", ".mm" (many more)
893/// extension).
894///
895/// @return
896/// \b true if the filespec represents an implementation source
897/// file, \b false otherwise.
898//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000899bool FileSpec::IsSourceImplementationFile() const {
900 ConstString extension(GetFileNameExtension());
Zachary Turner95eae422016-09-21 16:01:28 +0000901 if (!extension)
902 return false;
903
904 static RegularExpression g_source_file_regex(llvm::StringRef(
905 "^([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|["
906 "cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO]["
907 "rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])"
908 "$"));
909 return g_source_file_regex.Execute(extension.GetStringRef());
Greg Clayton1f746072012-08-29 21:13:06 +0000910}
911
Kate Stoneb9c1b512016-09-06 20:57:50 +0000912bool FileSpec::IsRelative() const {
913 const char *dir = m_directory.GetCString();
914 llvm::StringRef directory(dir ? dir : "");
Zachary Turner270e99a2014-12-08 21:36:42 +0000915
Kate Stoneb9c1b512016-09-06 20:57:50 +0000916 if (directory.size() > 0) {
917 if (PathSyntaxIsPosix(m_syntax)) {
918 // If the path doesn't start with '/' or '~', return true
919 switch (directory[0]) {
920 case '/':
921 case '~':
922 return false;
923 default:
Greg Claytona0ca6602012-10-18 16:33:33 +0000924 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000925 }
926 } else {
927 if (directory.size() >= 2 && directory[1] == ':')
928 return false;
929 if (directory[0] == '/')
930 return false;
931 return true;
Greg Claytona0ca6602012-10-18 16:33:33 +0000932 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000933 } else if (m_filename) {
934 // No directory, just a basename, return true
935 return true;
936 }
937 return false;
Greg Claytona0ca6602012-10-18 16:33:33 +0000938}
Chaoren Lin372e9062015-06-09 17:54:27 +0000939
Kate Stoneb9c1b512016-09-06 20:57:50 +0000940bool FileSpec::IsAbsolute() const { return !FileSpec::IsRelative(); }
Zachary Turner827d5d72016-12-16 04:27:00 +0000941
942void llvm::format_provider<FileSpec>::format(const FileSpec &F,
943 raw_ostream &Stream,
944 StringRef Style) {
945 assert(
946 (Style.empty() || Style.equals_lower("F") || Style.equals_lower("D")) &&
947 "Invalid FileSpec style!");
948
949 StringRef dir = F.GetDirectory().GetStringRef();
950 StringRef file = F.GetFilename().GetStringRef();
951
952 if (dir.empty() && file.empty()) {
953 Stream << "(empty)";
954 return;
955 }
956
957 if (Style.equals_lower("F")) {
958 Stream << (file.empty() ? "(empty)" : file);
959 return;
960 }
961
962 // Style is either D or empty, either way we need to print the directory.
963 if (!dir.empty()) {
964 // Directory is stored in normalized form, which might be different
965 // than preferred form. In order to handle this, we need to cut off
966 // the filename, then denormalize, then write the entire denorm'ed
967 // directory.
968 llvm::SmallString<64> denormalized_dir = dir;
969 Denormalize(denormalized_dir, F.GetPathSyntax());
970 Stream << denormalized_dir;
971 Stream << GetPreferredPathSeparator(F.GetPathSyntax());
972 }
973
974 if (Style.equals_lower("D")) {
975 // We only want to print the directory, so now just exit.
976 if (dir.empty())
977 Stream << "(empty)";
978 return;
979 }
980
981 if (!file.empty())
982 Stream << file;
983}