blob: 25519b6e7c72e334d69a555aae1989b86a4cccab [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
Jonas Devliegheread8d48f2018-06-13 16:23:21 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/SmallVector.h"
Caroline Tice391a9602010-09-12 00:10:52 +000017#include "llvm/ADT/StringRef.h"
Jonas Devliegheread8d48f2018-06-13 16:23:21 +000018#include "llvm/ADT/Triple.h"
19#include "llvm/ADT/Twine.h"
20#include "llvm/Support/ErrorOr.h"
Zachary Turner3f559742014-08-07 17:33:36 +000021#include "llvm/Support/FileSystem.h"
Greg Clayton38a61402010-12-02 23:20:03 +000022#include "llvm/Support/Program.h"
Jonas Devliegheread8d48f2018-06-13 16:23:21 +000023#include "llvm/Support/raw_ostream.h"
Zachary Turner4479ac12017-04-06 18:12:24 +000024
25#include <algorithm> // for replace, min, unique
26#include <system_error> // for error_code
27#include <vector> // for vector
28
29#include <assert.h> // for assert
30#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 +000060bool IsPathSeparator(char value, FileSpec::Style style) {
61 return value == '/' || (!PathStyleIsPosix(style) && value == '\\');
Chaoren Lin1c614fe2015-05-28 17:02:45 +000062}
63
Pavel Labath2cb7cf82018-05-14 14:52:47 +000064void Denormalize(llvm::SmallVectorImpl<char> &path, FileSpec::Style style) {
65 if (PathStyleIsPosix(style))
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 return;
Chaoren Lin1c614fe2015-05-28 17:02:45 +000067
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 std::replace(path.begin(), path.end(), '/', '\\');
Chaoren Lin1c614fe2015-05-28 17:02:45 +000069}
Greg Clayton86188d82018-05-21 14:14:36 +000070
Pavel Labath144119b2016-04-04 14:39:12 +000071} // end anonymous namespace
72
Kate Stoneb9c1b512016-09-06 20:57:50 +000073void FileSpec::Resolve(llvm::SmallVectorImpl<char> &path) {
74 if (path.empty())
75 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000076
Zachary Turner8d48cd62017-03-22 17:33:23 +000077 llvm::SmallString<32> Source(path.begin(), path.end());
78 StandardTildeExpressionResolver Resolver;
79 Resolver.ResolveFullPath(Source, path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 // Save a copy of the original path that's passed in
82 llvm::SmallString<128> original_path(path.begin(), path.end());
Jason Molenda671a29d2015-02-25 02:35:25 +000083
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 llvm::sys::fs::make_absolute(path);
85 if (!llvm::sys::fs::exists(path)) {
86 path.clear();
87 path.append(original_path.begin(), original_path.end());
88 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089}
90
Pavel Labath2cb7cf82018-05-14 14:52:47 +000091FileSpec::FileSpec() : m_style(GetNativeStyle()) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092
93//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +000094// Default constructor that can take an optional full path to a file on disk.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095//------------------------------------------------------------------
Pavel Labath2cb7cf82018-05-14 14:52:47 +000096FileSpec::FileSpec(llvm::StringRef path, bool resolve_path, Style style)
97 : m_style(style) {
98 SetFile(path, resolve_path, style);
Jim Ingham0909e5f2010-09-16 00:57:33 +000099}
100
Zachary Turner8c6b5462017-03-06 23:42:44 +0000101FileSpec::FileSpec(llvm::StringRef path, bool resolve_path,
102 const llvm::Triple &Triple)
103 : FileSpec{path, resolve_path,
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000104 Triple.isOSWindows() ? Style::windows : Style::posix} {}
Chaoren Linf34f4102015-05-09 01:21:32 +0000105
Jim Ingham0909e5f2010-09-16 00:57:33 +0000106//------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107// Copy constructor
108//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109FileSpec::FileSpec(const FileSpec &rhs)
110 : m_directory(rhs.m_directory), m_filename(rhs.m_filename),
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000111 m_is_resolved(rhs.m_is_resolved), m_style(rhs.m_style) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112
113//------------------------------------------------------------------
114// Copy constructor
115//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116FileSpec::FileSpec(const FileSpec *rhs) : m_directory(), m_filename() {
117 if (rhs)
118 *this = *rhs;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119}
120
121//------------------------------------------------------------------
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000122// Virtual destructor in case anyone inherits from this class.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124FileSpec::~FileSpec() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125
Greg Clayton776cd7a2018-04-27 15:45:58 +0000126namespace {
127//------------------------------------------------------------------
128/// Safely get a character at the specified index.
129///
130/// @param[in] path
131/// A full, partial, or relative path to a file.
132///
133/// @param[in] i
134/// An index into path which may or may not be valid.
135///
136/// @return
137/// The character at index \a i if the index is valid, or 0 if
138/// the index is not valid.
139//------------------------------------------------------------------
140inline char safeCharAtIndex(const llvm::StringRef &path, size_t i) {
141 if (i < path.size())
142 return path[i];
143 return 0;
144}
145
146//------------------------------------------------------------------
147/// Check if a path needs to be normalized.
148///
149/// Check if a path needs to be normalized. We currently consider a
150/// path to need normalization if any of the following are true
151/// - path contains "/./"
152/// - path contains "/../"
153/// - path contains "//"
154/// - path ends with "/"
155/// Paths that start with "./" or with "../" are not considered to
156/// need normalization since we aren't trying to resolve the path,
157/// we are just trying to remove redundant things from the path.
158///
159/// @param[in] path
160/// A full, partial, or relative path to a file.
161///
Greg Clayton776cd7a2018-04-27 15:45:58 +0000162/// @return
163/// Returns \b true if the path needs to be normalized.
164//------------------------------------------------------------------
Pavel Labath410c5ac2018-04-30 12:59:14 +0000165bool needsNormalization(const llvm::StringRef &path) {
Greg Clayton27a0e102018-04-27 21:10:07 +0000166 if (path.empty())
167 return false;
168 // We strip off leading "." values so these paths need to be normalized
169 if (path[0] == '.')
170 return true;
171 for (auto i = path.find_first_of("\\/"); i != llvm::StringRef::npos;
172 i = path.find_first_of("\\/", i + 1)) {
Greg Clayton776cd7a2018-04-27 15:45:58 +0000173 const auto next = safeCharAtIndex(path, i+1);
174 switch (next) {
175 case 0:
176 // path separator char at the end of the string which should be
177 // stripped unless it is the one and only character
178 return i > 0;
179 case '/':
180 case '\\':
181 // two path separator chars in the middle of a path needs to be
182 // normalized
Greg Clayton27a0e102018-04-27 21:10:07 +0000183 if (i > 0)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000184 return true;
185 ++i;
186 break;
187
188 case '.': {
189 const auto next_next = safeCharAtIndex(path, i+2);
190 switch (next_next) {
191 default: break;
192 case 0: return true; // ends with "/."
193 case '/':
194 case '\\':
Greg Clayton27a0e102018-04-27 21:10:07 +0000195 return true; // contains "/./"
Greg Clayton776cd7a2018-04-27 15:45:58 +0000196 case '.': {
197 const auto next_next_next = safeCharAtIndex(path, i+3);
198 switch (next_next_next) {
199 default: break;
200 case 0: return true; // ends with "/.."
201 case '/':
202 case '\\':
Greg Clayton27a0e102018-04-27 21:10:07 +0000203 return true; // contains "/../"
Greg Clayton776cd7a2018-04-27 15:45:58 +0000204 }
205 break;
206 }
207 }
208 }
209 break;
210
211 default:
212 break;
213 }
214 }
215 return false;
216}
217
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000218
Greg Clayton776cd7a2018-04-27 15:45:58 +0000219}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220//------------------------------------------------------------------
221// Assignment operator.
222//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223const FileSpec &FileSpec::operator=(const FileSpec &rhs) {
224 if (this != &rhs) {
225 m_directory = rhs.m_directory;
226 m_filename = rhs.m_filename;
227 m_is_resolved = rhs.m_is_resolved;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000228 m_style = rhs.m_style;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229 }
230 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000231}
232
Jonas Devlieghere937348c2018-06-13 22:08:14 +0000233void FileSpec::SetFile(llvm::StringRef pathname, bool resolve) {
234 SetFile(pathname, resolve, m_style);
235}
236
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000238// Update the contents of this object with a new path. The path will be split
239// up into a directory and filename and stored as uniqued string values for
240// quick comparison and efficient memory usage.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241//------------------------------------------------------------------
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000242void FileSpec::SetFile(llvm::StringRef pathname, bool resolve, Style style) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 m_filename.Clear();
244 m_directory.Clear();
245 m_is_resolved = false;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000246 m_style = (style == Style::native) ? GetNativeStyle() : style;
Zachary Turnerdf62f202014-08-07 17:33:07 +0000247
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000248 if (pathname.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 llvm::SmallString<64> resolved(pathname);
Zachary Turnerdf62f202014-08-07 17:33:07 +0000252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 if (resolve) {
254 FileSpec::Resolve(resolved);
255 m_is_resolved = true;
256 }
Zachary Turnerc7a17ed2014-12-01 23:13:32 +0000257
Greg Clayton776cd7a2018-04-27 15:45:58 +0000258 // Normalize the path by removing ".", ".." and other redundant components.
Pavel Labath410c5ac2018-04-30 12:59:14 +0000259 if (needsNormalization(resolved))
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000260 llvm::sys::path::remove_dots(resolved, true, m_style);
Greg Clayton776cd7a2018-04-27 15:45:58 +0000261
262 // Normalize back slashes to forward slashes
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000263 if (m_style == Style::windows)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000264 std::replace(resolved.begin(), resolved.end(), '\\', '/');
Pavel Labatha212c582016-04-14 09:38:06 +0000265
Greg Clayton39d50b72018-05-17 16:12:38 +0000266 if (resolved.empty()) {
267 // If we have no path after normalization set the path to the current
268 // directory. This matches what python does and also a few other path
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000269 // utilities.
Greg Clayton39d50b72018-05-17 16:12:38 +0000270 m_filename.SetString(".");
271 return;
272 }
273
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000274 // Split path into filename and directory. We rely on the underlying char
275 // pointer to be nullptr when the components are empty.
276 llvm::StringRef filename = llvm::sys::path::filename(resolved, m_style);
277 if(!filename.empty())
278 m_filename.SetString(filename);
279 llvm::StringRef directory = llvm::sys::path::parent_path(resolved, m_style);
280 if(!directory.empty())
281 m_directory.SetString(directory);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282}
283
Zachary Turner8c6b5462017-03-06 23:42:44 +0000284void FileSpec::SetFile(llvm::StringRef path, bool resolve,
285 const llvm::Triple &Triple) {
286 return SetFile(path, resolve,
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000287 Triple.isOSWindows() ? Style::windows : Style::posix);
Chaoren Lin44145d72015-05-29 19:52:37 +0000288}
289
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000291// Convert to pointer operator. This allows code to check any FileSpec objects
292// to see if they contain anything valid using code such as:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293//
294// if (file_spec)
295// {}
296//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297FileSpec::operator bool() const { return m_filename || m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298
299//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000300// Logical NOT operator. This allows code to check any FileSpec objects to see
301// if they are invalid using code such as:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302//
303// if (!file_spec)
304// {}
305//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000306bool FileSpec::operator!() const { return !m_directory && !m_filename; }
307
308bool FileSpec::DirectoryEquals(const FileSpec &rhs) const {
309 const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
310 return ConstString::Equals(m_directory, rhs.m_directory, case_sensitive);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311}
312
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313bool FileSpec::FileEquals(const FileSpec &rhs) const {
314 const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
315 return ConstString::Equals(m_filename, rhs.m_filename, case_sensitive);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000316}
317
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000318//------------------------------------------------------------------
319// Equal to operator
320//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321bool FileSpec::operator==(const FileSpec &rhs) const {
322 if (!FileEquals(rhs))
323 return false;
324 if (DirectoryEquals(rhs))
325 return true;
Zachary Turner47c03462016-02-24 21:26:47 +0000326
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327 // TODO: determine if we want to keep this code in here.
Adrian Prantl05097242018-04-30 16:49:04 +0000328 // The code below was added to handle a case where we were trying to set a
329 // file and line breakpoint and one path was resolved, and the other not and
330 // the directory was in a mount point that resolved to a more complete path:
331 // "/tmp/a.c" == "/private/tmp/a.c". I might end up pulling this out...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332 if (IsResolved() && rhs.IsResolved()) {
333 // Both paths are resolved, no need to look further...
334 return false;
335 }
Zachary Turner47c03462016-02-24 21:26:47 +0000336
Kate Stoneb9c1b512016-09-06 20:57:50 +0000337 FileSpec resolved_lhs(*this);
Zachary Turner47c03462016-02-24 21:26:47 +0000338
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339 // If "this" isn't resolved, resolve it
340 if (!IsResolved()) {
341 if (resolved_lhs.ResolvePath()) {
342 // This path wasn't resolved but now it is. Check if the resolved
Adrian Prantl05097242018-04-30 16:49:04 +0000343 // directory is the same as our unresolved directory, and if so, we can
344 // mark this object as resolved to avoid more future resolves
Kate Stoneb9c1b512016-09-06 20:57:50 +0000345 m_is_resolved = (m_directory == resolved_lhs.m_directory);
346 } else
347 return false;
348 }
Zachary Turner47c03462016-02-24 21:26:47 +0000349
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 FileSpec resolved_rhs(rhs);
351 if (!rhs.IsResolved()) {
352 if (resolved_rhs.ResolvePath()) {
353 // rhs's path wasn't resolved but now it is. Check if the resolved
Adrian Prantl05097242018-04-30 16:49:04 +0000354 // directory is the same as rhs's unresolved directory, and if so, we can
355 // mark this object as resolved to avoid more future resolves
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 rhs.m_is_resolved = (rhs.m_directory == resolved_rhs.m_directory);
357 } else
358 return false;
359 }
Zachary Turner47c03462016-02-24 21:26:47 +0000360
Adrian Prantl05097242018-04-30 16:49:04 +0000361 // If we reach this point in the code we were able to resolve both paths and
362 // since we only resolve the paths if the basenames are equal, then we can
363 // just check if both directories are equal...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364 return DirectoryEquals(rhs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365}
366
367//------------------------------------------------------------------
368// Not equal to operator
369//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371
372//------------------------------------------------------------------
373// Less than operator
374//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375bool FileSpec::operator<(const FileSpec &rhs) const {
376 return FileSpec::Compare(*this, rhs, true) < 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377}
378
379//------------------------------------------------------------------
380// Dump a FileSpec object to a stream
381//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) {
383 f.Dump(&s);
384 return s;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385}
386
387//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000388// Clear this object by releasing both the directory and filename string values
389// and making them both the empty string.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391void FileSpec::Clear() {
392 m_directory.Clear();
393 m_filename.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394}
395
396//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000397// Compare two FileSpec objects. If "full" is true, then both the directory and
398// the filename must match. If "full" is false, then the directory names for
399// "a" and "b" are only compared if they are both non-empty. This allows a
400// FileSpec object to only contain a filename and it can match FileSpec objects
401// that have matching filenames with different paths.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402//
Adrian Prantl05097242018-04-30 16:49:04 +0000403// Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" and "1" if
404// "a" is greater than "b".
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) {
407 int result = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408
Kate Stoneb9c1b512016-09-06 20:57:50 +0000409 // case sensitivity of compare
410 const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
Zachary Turner47c03462016-02-24 21:26:47 +0000411
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 // If full is true, then we must compare both the directory and filename.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413
Adrian Prantl05097242018-04-30 16:49:04 +0000414 // If full is false, then if either directory is empty, then we match on the
415 // basename only, and if both directories have valid values, we still do a
416 // full compare. This allows for matching when we just have a filename in one
417 // of the FileSpec objects.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419 if (full || (a.m_directory && b.m_directory)) {
420 result = ConstString::Compare(a.m_directory, b.m_directory, case_sensitive);
421 if (result)
422 return result;
423 }
424 return ConstString::Compare(a.m_filename, b.m_filename, case_sensitive);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425}
426
Greg Clayton776cd7a2018-04-27 15:45:58 +0000427bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428 // case sensitivity of equality test
429 const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000430
Greg Clayton776cd7a2018-04-27 15:45:58 +0000431 const bool filenames_equal = ConstString::Equals(a.m_filename,
432 b.m_filename,
433 case_sensitive);
Jim Ingham97e4f472017-03-27 19:12:25 +0000434
Greg Clayton776cd7a2018-04-27 15:45:58 +0000435 if (!filenames_equal)
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000436 return false;
Jim Ingham97e4f472017-03-27 19:12:25 +0000437
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438 if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty()))
Jim Ingham97e4f472017-03-27 19:12:25 +0000439 return filenames_equal;
Pavel Labath218770b2016-10-31 16:22:07 +0000440
Greg Clayton776cd7a2018-04-27 15:45:58 +0000441 return a == b;
Jim Ingham96a15962014-11-15 01:54:26 +0000442}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443
444//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000445// Dump the object to the supplied stream. If the object contains a valid
446// directory name, it will be displayed followed by a directory delimiter, and
447// the filename.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449void FileSpec::Dump(Stream *s) const {
450 if (s) {
451 std::string path{GetPath(true)};
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000452 s->PutCString(path);
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000453 char path_separator = GetPreferredPathSeparator(m_style);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000454 if (!m_filename && !path.empty() && path.back() != path_separator)
455 s->PutChar(path_separator);
456 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457}
458
459//------------------------------------------------------------------
460// Returns true if the file exists.
461//------------------------------------------------------------------
Zachary Turner7d86ee52017-03-08 17:56:08 +0000462bool FileSpec::Exists() const { return llvm::sys::fs::exists(GetPath()); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464bool FileSpec::Readable() const {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000465 return GetPermissions() & llvm::sys::fs::perms::all_read;
Greg Clayton5acc1252014-08-15 18:00:45 +0000466}
467
Kate Stoneb9c1b512016-09-06 20:57:50 +0000468bool FileSpec::ResolveExecutableLocation() {
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000469 // CLEANUP: Use StringRef for string handling.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000470 if (!m_directory) {
471 const char *file_cstr = m_filename.GetCString();
472 if (file_cstr) {
473 const std::string file_str(file_cstr);
474 llvm::ErrorOr<std::string> error_or_path =
475 llvm::sys::findProgramByName(file_str);
476 if (!error_or_path)
Jim Ingham0909e5f2010-09-16 00:57:33 +0000477 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478 std::string path = error_or_path.get();
479 llvm::StringRef dir_ref = llvm::sys::path::parent_path(path);
480 if (!dir_ref.empty()) {
481 // FindProgramByName returns "." if it can't find the file.
482 if (strcmp(".", dir_ref.data()) == 0)
483 return false;
484
485 m_directory.SetCString(dir_ref.data());
486 if (Exists())
487 return true;
488 else {
489 // If FindProgramByName found the file, it returns the directory +
Adrian Prantl05097242018-04-30 16:49:04 +0000490 // filename in its return results. We need to separate them.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000491 FileSpec tmp_file(dir_ref.data(), false);
492 if (tmp_file.Exists()) {
493 m_directory = tmp_file.m_directory;
494 return true;
495 }
496 }
497 }
498 }
499 }
500
501 return false;
Jim Ingham0909e5f2010-09-16 00:57:33 +0000502}
503
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504bool FileSpec::ResolvePath() {
505 if (m_is_resolved)
506 return true; // We have already resolved this path
507
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508 // SetFile(...) will set m_is_resolved correctly if it can resolve the path
Jonas Devlieghere937348c2018-06-13 22:08:14 +0000509 SetFile(GetPath(false), true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510 return m_is_resolved;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000511}
512
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513uint64_t FileSpec::GetByteSize() const {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000514 uint64_t Size = 0;
515 if (llvm::sys::fs::file_size(GetPath(), Size))
516 return 0;
517 return Size;
Zachary Turnerdf62f202014-08-07 17:33:07 +0000518}
519
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000520FileSpec::Style FileSpec::GetPathStyle() const { return m_style; }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000521
Pavel Labath30e6cbf2017-03-07 13:19:15 +0000522uint32_t FileSpec::GetPermissions() const {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000523 namespace fs = llvm::sys::fs;
524 fs::file_status st;
525 if (fs::status(GetPath(), st, false))
526 return fs::perms::perms_not_known;
527
528 return st.permissions();
Greg Claytonfbb76342013-11-20 21:07:01 +0000529}
530
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531//------------------------------------------------------------------
532// Directory string get accessor.
533//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000534ConstString &FileSpec::GetDirectory() { return m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000535
536//------------------------------------------------------------------
537// Directory string const get accessor.
538//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000539const ConstString &FileSpec::GetDirectory() const { return m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000540
541//------------------------------------------------------------------
542// Filename string get accessor.
543//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000544ConstString &FileSpec::GetFilename() { return m_filename; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000545
546//------------------------------------------------------------------
547// Filename string const get accessor.
548//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000549const ConstString &FileSpec::GetFilename() const { return m_filename; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550
551//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000552// Extract the directory and path into a fixed buffer. This is needed as the
553// directory and path are stored in separate string values.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000554//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000555size_t FileSpec::GetPath(char *path, size_t path_max_len,
556 bool denormalize) const {
557 if (!path)
558 return 0;
Zachary Turnerb6d99242014-08-08 23:54:35 +0000559
Kate Stoneb9c1b512016-09-06 20:57:50 +0000560 std::string result = GetPath(denormalize);
561 ::snprintf(path, path_max_len, "%s", result.c_str());
562 return std::min(path_max_len - 1, result.length());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000563}
564
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565std::string FileSpec::GetPath(bool denormalize) const {
566 llvm::SmallString<64> result;
567 GetPath(result, denormalize);
568 return std::string(result.begin(), result.end());
Jason Molendaa7ae4672013-04-29 09:46:43 +0000569}
570
Kate Stoneb9c1b512016-09-06 20:57:50 +0000571const char *FileSpec::GetCString(bool denormalize) const {
572 return ConstString{GetPath(denormalize)}.AsCString(NULL);
Chaoren Lind3173f32015-05-29 19:52:29 +0000573}
574
Kate Stoneb9c1b512016-09-06 20:57:50 +0000575void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path,
576 bool denormalize) const {
577 path.append(m_directory.GetStringRef().begin(),
578 m_directory.GetStringRef().end());
Greg Clayton776cd7a2018-04-27 15:45:58 +0000579 // Since the path was normalized and all paths use '/' when stored in these
580 // objects, we don't need to look for the actual syntax specific path
581 // separator, we just look for and insert '/'.
582 if (m_directory && m_filename && m_directory.GetStringRef().back() != '/' &&
583 m_filename.GetStringRef().back() != '/')
584 path.insert(path.end(), '/');
Kate Stoneb9c1b512016-09-06 20:57:50 +0000585 path.append(m_filename.GetStringRef().begin(),
586 m_filename.GetStringRef().end());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000587 if (denormalize && !path.empty())
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000588 Denormalize(path, m_style);
Zachary Turner4e8ddf52015-04-09 18:08:50 +0000589}
590
Kate Stoneb9c1b512016-09-06 20:57:50 +0000591ConstString FileSpec::GetFileNameExtension() const {
Jonas Devlieghere9c1a6452018-06-13 16:36:07 +0000592 return ConstString(
593 llvm::sys::path::extension(m_filename.GetStringRef(), m_style));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594}
595
596ConstString FileSpec::GetFileNameStrippingExtension() const {
Jonas Devlieghere9c1a6452018-06-13 16:36:07 +0000597 return ConstString(llvm::sys::path::stem(m_filename.GetStringRef(), m_style));
Enrico Granataa9dbf432011-10-17 21:45:27 +0000598}
599
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000601// Return the size in bytes that this object takes in memory. This returns the
602// size in bytes of this object, not any shared string values it may refer to.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000603//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000604size_t FileSpec::MemorySize() const {
605 return m_filename.MemorySize() + m_directory.MemorySize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000606}
607
Zachary Turner1f875342017-03-13 02:44:39 +0000608void FileSpec::EnumerateDirectory(llvm::StringRef dir_path,
609 bool find_directories, bool find_files,
610 bool find_other,
611 EnumerateDirectoryCallbackType callback,
612 void *callback_baton) {
613 namespace fs = llvm::sys::fs;
614 std::error_code EC;
615 fs::recursive_directory_iterator Iter(dir_path, EC);
616 fs::recursive_directory_iterator End;
617 for (; Iter != End && !EC; Iter.increment(EC)) {
618 const auto &Item = *Iter;
Peter Collingbourne0dfdb442017-10-10 22:19:46 +0000619 llvm::ErrorOr<fs::basic_file_status> Status = Item.status();
620 if (!Status)
Zachary Turner1f875342017-03-13 02:44:39 +0000621 break;
Peter Collingbourne0dfdb442017-10-10 22:19:46 +0000622 if (!find_files && fs::is_regular_file(*Status))
Zachary Turner1f875342017-03-13 02:44:39 +0000623 continue;
Peter Collingbourne0dfdb442017-10-10 22:19:46 +0000624 if (!find_directories && fs::is_directory(*Status))
Zachary Turner1f875342017-03-13 02:44:39 +0000625 continue;
Peter Collingbourne0dfdb442017-10-10 22:19:46 +0000626 if (!find_other && fs::is_other(*Status))
Zachary Turner1f875342017-03-13 02:44:39 +0000627 continue;
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000628
Zachary Turner1f875342017-03-13 02:44:39 +0000629 FileSpec Spec(Item.path(), false);
Peter Collingbourne0dfdb442017-10-10 22:19:46 +0000630 auto Result = callback(callback_baton, Status->type(), Spec);
Zachary Turner1f875342017-03-13 02:44:39 +0000631 if (Result == eEnumerateDirectoryResultQuit)
632 return;
633 if (Result == eEnumerateDirectoryResultNext) {
634 // Default behavior is to recurse. Opt out if the callback doesn't want
635 // this behavior.
636 Iter.no_push();
Greg Clayton58c65f02015-06-29 18:29:00 +0000637 }
Zachary Turner1f875342017-03-13 02:44:39 +0000638 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000639}
640
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000641FileSpec
642FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000643 FileSpec ret = *this;
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000644 ret.AppendPathComponent(component);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000645 return ret;
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000646}
647
Kate Stoneb9c1b512016-09-06 20:57:50 +0000648FileSpec FileSpec::CopyByRemovingLastPathComponent() const {
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000649 // CLEANUP: Use StringRef for string handling.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000650 const bool resolve = false;
651 if (m_filename.IsEmpty() && m_directory.IsEmpty())
652 return FileSpec("", resolve);
653 if (m_directory.IsEmpty())
654 return FileSpec("", resolve);
655 if (m_filename.IsEmpty()) {
656 const char *dir_cstr = m_directory.GetCString();
657 const char *last_slash_ptr = ::strrchr(dir_cstr, '/');
658
659 // check for obvious cases before doing the full thing
660 if (!last_slash_ptr)
661 return FileSpec("", resolve);
662 if (last_slash_ptr == dir_cstr)
663 return FileSpec("/", resolve);
664
665 size_t last_slash_pos = last_slash_ptr - dir_cstr + 1;
666 ConstString new_path(dir_cstr, last_slash_pos);
667 return FileSpec(new_path.GetCString(), resolve);
668 } else
669 return FileSpec(m_directory.GetCString(), resolve);
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000670}
671
Kate Stoneb9c1b512016-09-06 20:57:50 +0000672ConstString FileSpec::GetLastPathComponent() const {
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000673 // CLEANUP: Use StringRef for string handling.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000674 if (m_filename)
675 return m_filename;
676 if (m_directory) {
677 const char *dir_cstr = m_directory.GetCString();
678 const char *last_slash_ptr = ::strrchr(dir_cstr, '/');
679 if (last_slash_ptr == NULL)
680 return m_directory;
681 if (last_slash_ptr == dir_cstr) {
682 if (last_slash_ptr[1] == 0)
683 return ConstString(last_slash_ptr);
684 else
685 return ConstString(last_slash_ptr + 1);
686 }
687 if (last_slash_ptr[1] != 0)
688 return ConstString(last_slash_ptr + 1);
689 const char *penultimate_slash_ptr = last_slash_ptr;
690 while (*penultimate_slash_ptr) {
691 --penultimate_slash_ptr;
692 if (penultimate_slash_ptr == dir_cstr)
693 break;
694 if (*penultimate_slash_ptr == '/')
695 break;
696 }
697 ConstString result(penultimate_slash_ptr + 1,
698 last_slash_ptr - penultimate_slash_ptr);
699 return result;
700 }
701 return ConstString();
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000702}
703
Pavel Labath59d725c2017-01-16 10:07:02 +0000704static std::string
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000705join_path_components(FileSpec::Style style,
Pavel Labath59d725c2017-01-16 10:07:02 +0000706 const std::vector<llvm::StringRef> components) {
707 std::string result;
708 for (size_t i = 0; i < components.size(); ++i) {
709 if (components[i].empty())
710 continue;
711 result += components[i];
712 if (i != components.size() - 1 &&
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000713 !IsPathSeparator(components[i].back(), style))
714 result += GetPreferredPathSeparator(style);
Pavel Labath59d725c2017-01-16 10:07:02 +0000715 }
716
717 return result;
718}
719
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000720void FileSpec::PrependPathComponent(llvm::StringRef component) {
721 if (component.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000722 return;
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000723
Kate Stoneb9c1b512016-09-06 20:57:50 +0000724 const bool resolve = false;
725 if (m_filename.IsEmpty() && m_directory.IsEmpty()) {
Jonas Devlieghere937348c2018-06-13 22:08:14 +0000726 SetFile(component, resolve);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000727 return;
728 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000729
Pavel Labath59d725c2017-01-16 10:07:02 +0000730 std::string result =
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000731 join_path_components(m_style, {component, m_directory.GetStringRef(),
732 m_filename.GetStringRef()});
Jonas Devlieghere937348c2018-06-13 22:08:14 +0000733 SetFile(result, resolve);
Chaoren Lind3173f32015-05-29 19:52:29 +0000734}
735
Kate Stoneb9c1b512016-09-06 20:57:50 +0000736void FileSpec::PrependPathComponent(const FileSpec &new_path) {
737 return PrependPathComponent(new_path.GetPath(false));
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000738}
739
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000740void FileSpec::AppendPathComponent(llvm::StringRef component) {
741 if (component.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000742 return;
743
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000744 component = component.drop_while(
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000745 [this](char c) { return IsPathSeparator(c, m_style); });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000746
Pavel Labath59d725c2017-01-16 10:07:02 +0000747 std::string result =
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000748 join_path_components(m_style, {m_directory.GetStringRef(),
749 m_filename.GetStringRef(), component});
Kate Stoneb9c1b512016-09-06 20:57:50 +0000750
Jonas Devlieghere937348c2018-06-13 22:08:14 +0000751 SetFile(result, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000752}
753
754void FileSpec::AppendPathComponent(const FileSpec &new_path) {
755 return AppendPathComponent(new_path.GetPath(false));
756}
757
Jonas Devliegheredf8e2912018-05-30 13:03:16 +0000758bool FileSpec::RemoveLastPathComponent() {
759 llvm::SmallString<64> current_path;
760 GetPath(current_path, false);
761 if (llvm::sys::path::has_parent_path(current_path, m_style)) {
Jonas Devlieghere937348c2018-06-13 22:08:14 +0000762 SetFile(llvm::sys::path::parent_path(current_path, m_style), false);
Jonas Devliegheredf8e2912018-05-30 13:03:16 +0000763 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764 }
Jonas Devliegheredf8e2912018-05-30 13:03:16 +0000765 return false;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000766}
Greg Clayton1f746072012-08-29 21:13:06 +0000767//------------------------------------------------------------------
768/// Returns true if the filespec represents an implementation source
769/// file (files with a ".c", ".cpp", ".m", ".mm" (many more)
770/// extension).
771///
772/// @return
773/// \b true if the filespec represents an implementation source
774/// file, \b false otherwise.
775//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776bool FileSpec::IsSourceImplementationFile() const {
777 ConstString extension(GetFileNameExtension());
Zachary Turner95eae422016-09-21 16:01:28 +0000778 if (!extension)
779 return false;
780
781 static RegularExpression g_source_file_regex(llvm::StringRef(
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000782 "^.([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|["
Zachary Turner95eae422016-09-21 16:01:28 +0000783 "cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO]["
784 "rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])"
785 "$"));
786 return g_source_file_regex.Execute(extension.GetStringRef());
Greg Clayton1f746072012-08-29 21:13:06 +0000787}
788
Kate Stoneb9c1b512016-09-06 20:57:50 +0000789bool FileSpec::IsRelative() const {
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000790 return !IsAbsolute();
Greg Claytona0ca6602012-10-18 16:33:33 +0000791}
Chaoren Lin372e9062015-06-09 17:54:27 +0000792
Jonas Devliegheread8d48f2018-06-13 16:23:21 +0000793bool FileSpec::IsAbsolute() const {
794 llvm::SmallString<64> current_path;
795 GetPath(current_path, false);
796
797 // Early return if the path is empty.
798 if (current_path.empty())
799 return false;
800
801 // We consider paths starting with ~ to be absolute.
802 if (current_path[0] == '~')
803 return true;
804
805 return llvm::sys::path::is_absolute(current_path, m_style);
806}
Zachary Turner827d5d72016-12-16 04:27:00 +0000807
808void llvm::format_provider<FileSpec>::format(const FileSpec &F,
809 raw_ostream &Stream,
810 StringRef Style) {
811 assert(
812 (Style.empty() || Style.equals_lower("F") || Style.equals_lower("D")) &&
813 "Invalid FileSpec style!");
814
815 StringRef dir = F.GetDirectory().GetStringRef();
816 StringRef file = F.GetFilename().GetStringRef();
817
818 if (dir.empty() && file.empty()) {
819 Stream << "(empty)";
820 return;
821 }
822
823 if (Style.equals_lower("F")) {
824 Stream << (file.empty() ? "(empty)" : file);
825 return;
826 }
827
828 // Style is either D or empty, either way we need to print the directory.
829 if (!dir.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000830 // Directory is stored in normalized form, which might be different than
831 // preferred form. In order to handle this, we need to cut off the
832 // filename, then denormalize, then write the entire denorm'ed directory.
Zachary Turner827d5d72016-12-16 04:27:00 +0000833 llvm::SmallString<64> denormalized_dir = dir;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000834 Denormalize(denormalized_dir, F.GetPathStyle());
Zachary Turner827d5d72016-12-16 04:27:00 +0000835 Stream << denormalized_dir;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000836 Stream << GetPreferredPathSeparator(F.GetPathStyle());
Zachary Turner827d5d72016-12-16 04:27:00 +0000837 }
838
839 if (Style.equals_lower("D")) {
840 // We only want to print the directory, so now just exit.
841 if (dir.empty())
842 Stream << "(empty)";
843 return;
844 }
845
846 if (!file.empty())
847 Stream << file;
848}