blob: 944f01d9000163f1bbf7878bc8d798a7f90c331a [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/Program.h"
Zachary Turner4479ac12017-04-06 18:12:24 +000023#include "llvm/Support/raw_ostream.h" // for raw_ostream, fs
24
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) {
53 return PathStyleIsPosix(style) ? "/" : "\\/";
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}
Pavel Labath144119b2016-04-04 14:39:12 +000070} // end anonymous namespace
71
Kate Stoneb9c1b512016-09-06 20:57:50 +000072void FileSpec::Resolve(llvm::SmallVectorImpl<char> &path) {
73 if (path.empty())
74 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075
Zachary Turner8d48cd62017-03-22 17:33:23 +000076 llvm::SmallString<32> Source(path.begin(), path.end());
77 StandardTildeExpressionResolver Resolver;
78 Resolver.ResolveFullPath(Source, path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 // Save a copy of the original path that's passed in
81 llvm::SmallString<128> original_path(path.begin(), path.end());
Jason Molenda671a29d2015-02-25 02:35:25 +000082
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 llvm::sys::fs::make_absolute(path);
84 if (!llvm::sys::fs::exists(path)) {
85 path.clear();
86 path.append(original_path.begin(), original_path.end());
87 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088}
89
Pavel Labath2cb7cf82018-05-14 14:52:47 +000090FileSpec::FileSpec() : m_style(GetNativeStyle()) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091
92//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +000093// Default constructor that can take an optional full path to a file on disk.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094//------------------------------------------------------------------
Pavel Labath2cb7cf82018-05-14 14:52:47 +000095FileSpec::FileSpec(llvm::StringRef path, bool resolve_path, Style style)
96 : m_style(style) {
97 SetFile(path, resolve_path, style);
Jim Ingham0909e5f2010-09-16 00:57:33 +000098}
99
Zachary Turner8c6b5462017-03-06 23:42:44 +0000100FileSpec::FileSpec(llvm::StringRef path, bool resolve_path,
101 const llvm::Triple &Triple)
102 : FileSpec{path, resolve_path,
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000103 Triple.isOSWindows() ? Style::windows : Style::posix} {}
Chaoren Linf34f4102015-05-09 01:21:32 +0000104
Jim Ingham0909e5f2010-09-16 00:57:33 +0000105//------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106// Copy constructor
107//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108FileSpec::FileSpec(const FileSpec &rhs)
109 : m_directory(rhs.m_directory), m_filename(rhs.m_filename),
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000110 m_is_resolved(rhs.m_is_resolved), m_style(rhs.m_style) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111
112//------------------------------------------------------------------
113// Copy constructor
114//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115FileSpec::FileSpec(const FileSpec *rhs) : m_directory(), m_filename() {
116 if (rhs)
117 *this = *rhs;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118}
119
120//------------------------------------------------------------------
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000121// Virtual destructor in case anyone inherits from this class.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123FileSpec::~FileSpec() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000124
Greg Clayton776cd7a2018-04-27 15:45:58 +0000125namespace {
126//------------------------------------------------------------------
127/// Safely get a character at the specified index.
128///
129/// @param[in] path
130/// A full, partial, or relative path to a file.
131///
132/// @param[in] i
133/// An index into path which may or may not be valid.
134///
135/// @return
136/// The character at index \a i if the index is valid, or 0 if
137/// the index is not valid.
138//------------------------------------------------------------------
139inline char safeCharAtIndex(const llvm::StringRef &path, size_t i) {
140 if (i < path.size())
141 return path[i];
142 return 0;
143}
144
145//------------------------------------------------------------------
146/// Check if a path needs to be normalized.
147///
148/// Check if a path needs to be normalized. We currently consider a
149/// path to need normalization if any of the following are true
150/// - path contains "/./"
151/// - path contains "/../"
152/// - path contains "//"
153/// - path ends with "/"
154/// Paths that start with "./" or with "../" are not considered to
155/// need normalization since we aren't trying to resolve the path,
156/// we are just trying to remove redundant things from the path.
157///
158/// @param[in] path
159/// A full, partial, or relative path to a file.
160///
Greg Clayton776cd7a2018-04-27 15:45:58 +0000161/// @return
162/// Returns \b true if the path needs to be normalized.
163//------------------------------------------------------------------
Pavel Labath410c5ac2018-04-30 12:59:14 +0000164bool needsNormalization(const llvm::StringRef &path) {
Greg Clayton27a0e102018-04-27 21:10:07 +0000165 if (path.empty())
166 return false;
167 // We strip off leading "." values so these paths need to be normalized
168 if (path[0] == '.')
169 return true;
170 for (auto i = path.find_first_of("\\/"); i != llvm::StringRef::npos;
171 i = path.find_first_of("\\/", i + 1)) {
Greg Clayton776cd7a2018-04-27 15:45:58 +0000172 const auto next = safeCharAtIndex(path, i+1);
173 switch (next) {
174 case 0:
175 // path separator char at the end of the string which should be
176 // stripped unless it is the one and only character
177 return i > 0;
178 case '/':
179 case '\\':
180 // two path separator chars in the middle of a path needs to be
181 // normalized
Greg Clayton27a0e102018-04-27 21:10:07 +0000182 if (i > 0)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000183 return true;
184 ++i;
185 break;
186
187 case '.': {
188 const auto next_next = safeCharAtIndex(path, i+2);
189 switch (next_next) {
190 default: break;
191 case 0: return true; // ends with "/."
192 case '/':
193 case '\\':
Greg Clayton27a0e102018-04-27 21:10:07 +0000194 return true; // contains "/./"
Greg Clayton776cd7a2018-04-27 15:45:58 +0000195 case '.': {
196 const auto next_next_next = safeCharAtIndex(path, i+3);
197 switch (next_next_next) {
198 default: break;
199 case 0: return true; // ends with "/.."
200 case '/':
201 case '\\':
Greg Clayton27a0e102018-04-27 21:10:07 +0000202 return true; // contains "/../"
Greg Clayton776cd7a2018-04-27 15:45:58 +0000203 }
204 break;
205 }
206 }
207 }
208 break;
209
210 default:
211 break;
212 }
213 }
214 return false;
215}
216
217
218}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000219//------------------------------------------------------------------
220// Assignment operator.
221//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222const FileSpec &FileSpec::operator=(const FileSpec &rhs) {
223 if (this != &rhs) {
224 m_directory = rhs.m_directory;
225 m_filename = rhs.m_filename;
226 m_is_resolved = rhs.m_is_resolved;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000227 m_style = rhs.m_style;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228 }
229 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000230}
231
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000233// Update the contents of this object with a new path. The path will be split
234// up into a directory and filename and stored as uniqued string values for
235// quick comparison and efficient memory usage.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236//------------------------------------------------------------------
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000237void FileSpec::SetFile(llvm::StringRef pathname, bool resolve, Style style) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238 m_filename.Clear();
239 m_directory.Clear();
240 m_is_resolved = false;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000241 m_style = (style == Style::native) ? GetNativeStyle() : style;
Zachary Turnerdf62f202014-08-07 17:33:07 +0000242
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000243 if (pathname.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246 llvm::SmallString<64> resolved(pathname);
Zachary Turnerdf62f202014-08-07 17:33:07 +0000247
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248 if (resolve) {
249 FileSpec::Resolve(resolved);
250 m_is_resolved = true;
251 }
Zachary Turnerc7a17ed2014-12-01 23:13:32 +0000252
Greg Clayton776cd7a2018-04-27 15:45:58 +0000253 // Normalize the path by removing ".", ".." and other redundant components.
Pavel Labath410c5ac2018-04-30 12:59:14 +0000254 if (needsNormalization(resolved))
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000255 llvm::sys::path::remove_dots(resolved, true, m_style);
Greg Clayton776cd7a2018-04-27 15:45:58 +0000256
257 // Normalize back slashes to forward slashes
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000258 if (m_style == Style::windows)
Greg Clayton776cd7a2018-04-27 15:45:58 +0000259 std::replace(resolved.begin(), resolved.end(), '\\', '/');
Pavel Labatha212c582016-04-14 09:38:06 +0000260
Greg Clayton39d50b72018-05-17 16:12:38 +0000261 if (resolved.empty()) {
262 // If we have no path after normalization set the path to the current
263 // directory. This matches what python does and also a few other path
264 // utilities.
265 m_filename.SetString(".");
266 return;
267 }
268
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000269 m_filename.SetString(llvm::sys::path::filename(resolved, m_style));
270 llvm::StringRef dir = llvm::sys::path::parent_path(resolved, m_style);
Pavel Labathe7306b12018-05-11 11:55:34 +0000271 if (!dir.empty())
272 m_directory.SetString(dir);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273}
274
Zachary Turner8c6b5462017-03-06 23:42:44 +0000275void FileSpec::SetFile(llvm::StringRef path, bool resolve,
276 const llvm::Triple &Triple) {
277 return SetFile(path, resolve,
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000278 Triple.isOSWindows() ? Style::windows : Style::posix);
Chaoren Lin44145d72015-05-29 19:52:37 +0000279}
280
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000282// Convert to pointer operator. This allows code to check any FileSpec objects
283// to see if they contain anything valid using code such as:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284//
285// if (file_spec)
286// {}
287//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288FileSpec::operator bool() const { return m_filename || m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289
290//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000291// Logical NOT operator. This allows code to check any FileSpec objects to see
292// if they are invalid 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 +0000297bool FileSpec::operator!() const { return !m_directory && !m_filename; }
298
299bool FileSpec::DirectoryEquals(const FileSpec &rhs) const {
300 const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
301 return ConstString::Equals(m_directory, rhs.m_directory, case_sensitive);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302}
303
Kate Stoneb9c1b512016-09-06 20:57:50 +0000304bool FileSpec::FileEquals(const FileSpec &rhs) const {
305 const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
306 return ConstString::Equals(m_filename, rhs.m_filename, case_sensitive);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000307}
308
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309//------------------------------------------------------------------
310// Equal to operator
311//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000312bool FileSpec::operator==(const FileSpec &rhs) const {
313 if (!FileEquals(rhs))
314 return false;
315 if (DirectoryEquals(rhs))
316 return true;
Zachary Turner47c03462016-02-24 21:26:47 +0000317
Kate Stoneb9c1b512016-09-06 20:57:50 +0000318 // TODO: determine if we want to keep this code in here.
Adrian Prantl05097242018-04-30 16:49:04 +0000319 // The code below was added to handle a case where we were trying to set a
320 // file and line breakpoint and one path was resolved, and the other not and
321 // the directory was in a mount point that resolved to a more complete path:
322 // "/tmp/a.c" == "/private/tmp/a.c". I might end up pulling this out...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323 if (IsResolved() && rhs.IsResolved()) {
324 // Both paths are resolved, no need to look further...
325 return false;
326 }
Zachary Turner47c03462016-02-24 21:26:47 +0000327
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328 FileSpec resolved_lhs(*this);
Zachary Turner47c03462016-02-24 21:26:47 +0000329
Kate Stoneb9c1b512016-09-06 20:57:50 +0000330 // If "this" isn't resolved, resolve it
331 if (!IsResolved()) {
332 if (resolved_lhs.ResolvePath()) {
333 // This path wasn't resolved but now it is. Check if the resolved
Adrian Prantl05097242018-04-30 16:49:04 +0000334 // directory is the same as our unresolved directory, and if so, we can
335 // mark this object as resolved to avoid more future resolves
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336 m_is_resolved = (m_directory == resolved_lhs.m_directory);
337 } else
338 return false;
339 }
Zachary Turner47c03462016-02-24 21:26:47 +0000340
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341 FileSpec resolved_rhs(rhs);
342 if (!rhs.IsResolved()) {
343 if (resolved_rhs.ResolvePath()) {
344 // rhs's path wasn't resolved but now it is. Check if the resolved
Adrian Prantl05097242018-04-30 16:49:04 +0000345 // directory is the same as rhs's unresolved directory, and if so, we can
346 // mark this object as resolved to avoid more future resolves
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347 rhs.m_is_resolved = (rhs.m_directory == resolved_rhs.m_directory);
348 } else
349 return false;
350 }
Zachary Turner47c03462016-02-24 21:26:47 +0000351
Adrian Prantl05097242018-04-30 16:49:04 +0000352 // If we reach this point in the code we were able to resolve both paths and
353 // since we only resolve the paths if the basenames are equal, then we can
354 // just check if both directories are equal...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355 return DirectoryEquals(rhs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000356}
357
358//------------------------------------------------------------------
359// Not equal to operator
360//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362
363//------------------------------------------------------------------
364// Less than operator
365//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366bool FileSpec::operator<(const FileSpec &rhs) const {
367 return FileSpec::Compare(*this, rhs, true) < 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368}
369
370//------------------------------------------------------------------
371// Dump a FileSpec object to a stream
372//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000373Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) {
374 f.Dump(&s);
375 return s;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376}
377
378//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000379// Clear this object by releasing both the directory and filename string values
380// and making them both the empty string.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382void FileSpec::Clear() {
383 m_directory.Clear();
384 m_filename.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385}
386
387//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000388// Compare two FileSpec objects. If "full" is true, then both the directory and
389// the filename must match. If "full" is false, then the directory names for
390// "a" and "b" are only compared if they are both non-empty. This allows a
391// FileSpec object to only contain a filename and it can match FileSpec objects
392// that have matching filenames with different paths.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393//
Adrian Prantl05097242018-04-30 16:49:04 +0000394// Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" and "1" if
395// "a" is greater than "b".
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) {
398 int result = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400 // case sensitivity of compare
401 const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
Zachary Turner47c03462016-02-24 21:26:47 +0000402
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 // If full is true, then we must compare both the directory and filename.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404
Adrian Prantl05097242018-04-30 16:49:04 +0000405 // If full is false, then if either directory is empty, then we match on the
406 // basename only, and if both directories have valid values, we still do a
407 // full compare. This allows for matching when we just have a filename in one
408 // of the FileSpec objects.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409
Kate Stoneb9c1b512016-09-06 20:57:50 +0000410 if (full || (a.m_directory && b.m_directory)) {
411 result = ConstString::Compare(a.m_directory, b.m_directory, case_sensitive);
412 if (result)
413 return result;
414 }
415 return ConstString::Compare(a.m_filename, b.m_filename, case_sensitive);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416}
417
Greg Clayton776cd7a2018-04-27 15:45:58 +0000418bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full) {
Jim Ingham97e4f472017-03-27 19:12:25 +0000419
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 // case sensitivity of equality test
421 const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
Jim Ingham97e4f472017-03-27 19:12:25 +0000422
Greg Clayton776cd7a2018-04-27 15:45:58 +0000423 const bool filenames_equal = ConstString::Equals(a.m_filename,
424 b.m_filename,
425 case_sensitive);
Jim Ingham97e4f472017-03-27 19:12:25 +0000426
Greg Clayton776cd7a2018-04-27 15:45:58 +0000427 if (!filenames_equal)
Jim Ingham97e4f472017-03-27 19:12:25 +0000428 return false;
429
Kate Stoneb9c1b512016-09-06 20:57:50 +0000430 if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty()))
Jim Ingham97e4f472017-03-27 19:12:25 +0000431 return filenames_equal;
Pavel Labath218770b2016-10-31 16:22:07 +0000432
Greg Clayton776cd7a2018-04-27 15:45:58 +0000433 return a == b;
Jim Ingham96a15962014-11-15 01:54:26 +0000434}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435
436//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000437// Dump the object to the supplied stream. If the object contains a valid
438// directory name, it will be displayed followed by a directory delimiter, and
439// the filename.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441void FileSpec::Dump(Stream *s) const {
442 if (s) {
443 std::string path{GetPath(true)};
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000444 s->PutCString(path);
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000445 char path_separator = GetPreferredPathSeparator(m_style);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 if (!m_filename && !path.empty() && path.back() != path_separator)
447 s->PutChar(path_separator);
448 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000449}
450
451//------------------------------------------------------------------
452// Returns true if the file exists.
453//------------------------------------------------------------------
Zachary Turner7d86ee52017-03-08 17:56:08 +0000454bool FileSpec::Exists() const { return llvm::sys::fs::exists(GetPath()); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456bool FileSpec::Readable() const {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000457 return GetPermissions() & llvm::sys::fs::perms::all_read;
Greg Clayton5acc1252014-08-15 18:00:45 +0000458}
459
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460bool FileSpec::ResolveExecutableLocation() {
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000461 // CLEANUP: Use StringRef for string handling.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 if (!m_directory) {
463 const char *file_cstr = m_filename.GetCString();
464 if (file_cstr) {
465 const std::string file_str(file_cstr);
466 llvm::ErrorOr<std::string> error_or_path =
467 llvm::sys::findProgramByName(file_str);
468 if (!error_or_path)
Jim Ingham0909e5f2010-09-16 00:57:33 +0000469 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000470 std::string path = error_or_path.get();
471 llvm::StringRef dir_ref = llvm::sys::path::parent_path(path);
472 if (!dir_ref.empty()) {
473 // FindProgramByName returns "." if it can't find the file.
474 if (strcmp(".", dir_ref.data()) == 0)
475 return false;
476
477 m_directory.SetCString(dir_ref.data());
478 if (Exists())
479 return true;
480 else {
481 // If FindProgramByName found the file, it returns the directory +
Adrian Prantl05097242018-04-30 16:49:04 +0000482 // filename in its return results. We need to separate them.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483 FileSpec tmp_file(dir_ref.data(), false);
484 if (tmp_file.Exists()) {
485 m_directory = tmp_file.m_directory;
486 return true;
487 }
488 }
489 }
490 }
491 }
492
493 return false;
Jim Ingham0909e5f2010-09-16 00:57:33 +0000494}
495
Kate Stoneb9c1b512016-09-06 20:57:50 +0000496bool FileSpec::ResolvePath() {
497 if (m_is_resolved)
498 return true; // We have already resolved this path
499
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500 // SetFile(...) will set m_is_resolved correctly if it can resolve the path
Pavel Labath9bd69ad2017-03-13 09:46:15 +0000501 SetFile(GetPath(false), true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000502 return m_is_resolved;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503}
504
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505uint64_t FileSpec::GetByteSize() const {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000506 uint64_t Size = 0;
507 if (llvm::sys::fs::file_size(GetPath(), Size))
508 return 0;
509 return Size;
Zachary Turnerdf62f202014-08-07 17:33:07 +0000510}
511
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000512FileSpec::Style FileSpec::GetPathStyle() const { return m_style; }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513
Pavel Labath30e6cbf2017-03-07 13:19:15 +0000514uint32_t FileSpec::GetPermissions() const {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000515 namespace fs = llvm::sys::fs;
516 fs::file_status st;
517 if (fs::status(GetPath(), st, false))
518 return fs::perms::perms_not_known;
519
520 return st.permissions();
Greg Claytonfbb76342013-11-20 21:07:01 +0000521}
522
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523//------------------------------------------------------------------
524// Directory string get accessor.
525//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000526ConstString &FileSpec::GetDirectory() { return m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527
528//------------------------------------------------------------------
529// Directory string const get accessor.
530//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000531const ConstString &FileSpec::GetDirectory() const { return m_directory; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000532
533//------------------------------------------------------------------
534// Filename string get accessor.
535//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000536ConstString &FileSpec::GetFilename() { return m_filename; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000537
538//------------------------------------------------------------------
539// Filename string const get accessor.
540//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000541const ConstString &FileSpec::GetFilename() const { return m_filename; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000542
543//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000544// Extract the directory and path into a fixed buffer. This is needed as the
545// directory and path are stored in separate string values.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000547size_t FileSpec::GetPath(char *path, size_t path_max_len,
548 bool denormalize) const {
549 if (!path)
550 return 0;
Zachary Turnerb6d99242014-08-08 23:54:35 +0000551
Kate Stoneb9c1b512016-09-06 20:57:50 +0000552 std::string result = GetPath(denormalize);
553 ::snprintf(path, path_max_len, "%s", result.c_str());
554 return std::min(path_max_len - 1, result.length());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555}
556
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557std::string FileSpec::GetPath(bool denormalize) const {
558 llvm::SmallString<64> result;
559 GetPath(result, denormalize);
560 return std::string(result.begin(), result.end());
Jason Molendaa7ae4672013-04-29 09:46:43 +0000561}
562
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563const char *FileSpec::GetCString(bool denormalize) const {
564 return ConstString{GetPath(denormalize)}.AsCString(NULL);
Chaoren Lind3173f32015-05-29 19:52:29 +0000565}
566
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path,
568 bool denormalize) const {
569 path.append(m_directory.GetStringRef().begin(),
570 m_directory.GetStringRef().end());
Greg Clayton776cd7a2018-04-27 15:45:58 +0000571 // Since the path was normalized and all paths use '/' when stored in these
572 // objects, we don't need to look for the actual syntax specific path
573 // separator, we just look for and insert '/'.
574 if (m_directory && m_filename && m_directory.GetStringRef().back() != '/' &&
575 m_filename.GetStringRef().back() != '/')
576 path.insert(path.end(), '/');
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577 path.append(m_filename.GetStringRef().begin(),
578 m_filename.GetStringRef().end());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579 if (denormalize && !path.empty())
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000580 Denormalize(path, m_style);
Zachary Turner4e8ddf52015-04-09 18:08:50 +0000581}
582
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583ConstString FileSpec::GetFileNameExtension() const {
584 if (m_filename) {
Enrico Granataa9dbf432011-10-17 21:45:27 +0000585 const char *filename = m_filename.GetCString();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586 const char *dot_pos = strrchr(filename, '.');
587 if (dot_pos && dot_pos[1] != '\0')
588 return ConstString(dot_pos + 1);
589 }
590 return ConstString();
591}
592
593ConstString FileSpec::GetFileNameStrippingExtension() const {
594 const char *filename = m_filename.GetCString();
595 if (filename == NULL)
596 return ConstString();
597
598 const char *dot_pos = strrchr(filename, '.');
599 if (dot_pos == NULL)
600 return m_filename;
601
602 return ConstString(filename, dot_pos - filename);
Enrico Granataa9dbf432011-10-17 21:45:27 +0000603}
604
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605//------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000606// Return the size in bytes that this object takes in memory. This returns the
607// size in bytes of this object, not any shared string values it may refer to.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000608//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000609size_t FileSpec::MemorySize() const {
610 return m_filename.MemorySize() + m_directory.MemorySize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611}
612
Zachary Turner1f875342017-03-13 02:44:39 +0000613void FileSpec::EnumerateDirectory(llvm::StringRef dir_path,
614 bool find_directories, bool find_files,
615 bool find_other,
616 EnumerateDirectoryCallbackType callback,
617 void *callback_baton) {
618 namespace fs = llvm::sys::fs;
619 std::error_code EC;
620 fs::recursive_directory_iterator Iter(dir_path, EC);
621 fs::recursive_directory_iterator End;
622 for (; Iter != End && !EC; Iter.increment(EC)) {
623 const auto &Item = *Iter;
Peter Collingbourne0dfdb442017-10-10 22:19:46 +0000624 llvm::ErrorOr<fs::basic_file_status> Status = Item.status();
625 if (!Status)
Zachary Turner1f875342017-03-13 02:44:39 +0000626 break;
Peter Collingbourne0dfdb442017-10-10 22:19:46 +0000627 if (!find_files && fs::is_regular_file(*Status))
Zachary Turner1f875342017-03-13 02:44:39 +0000628 continue;
Peter Collingbourne0dfdb442017-10-10 22:19:46 +0000629 if (!find_directories && fs::is_directory(*Status))
Zachary Turner1f875342017-03-13 02:44:39 +0000630 continue;
Peter Collingbourne0dfdb442017-10-10 22:19:46 +0000631 if (!find_other && fs::is_other(*Status))
Zachary Turner1f875342017-03-13 02:44:39 +0000632 continue;
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000633
Zachary Turner1f875342017-03-13 02:44:39 +0000634 FileSpec Spec(Item.path(), false);
Peter Collingbourne0dfdb442017-10-10 22:19:46 +0000635 auto Result = callback(callback_baton, Status->type(), Spec);
Zachary Turner1f875342017-03-13 02:44:39 +0000636 if (Result == eEnumerateDirectoryResultQuit)
637 return;
638 if (Result == eEnumerateDirectoryResultNext) {
639 // Default behavior is to recurse. Opt out if the callback doesn't want
640 // this behavior.
641 Iter.no_push();
Greg Clayton58c65f02015-06-29 18:29:00 +0000642 }
Zachary Turner1f875342017-03-13 02:44:39 +0000643 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000644}
645
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000646FileSpec
647FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000648 FileSpec ret = *this;
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000649 ret.AppendPathComponent(component);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000650 return ret;
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000651}
652
Kate Stoneb9c1b512016-09-06 20:57:50 +0000653FileSpec FileSpec::CopyByRemovingLastPathComponent() const {
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000654 // CLEANUP: Use StringRef for string handling.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000655 const bool resolve = false;
656 if (m_filename.IsEmpty() && m_directory.IsEmpty())
657 return FileSpec("", resolve);
658 if (m_directory.IsEmpty())
659 return FileSpec("", resolve);
660 if (m_filename.IsEmpty()) {
661 const char *dir_cstr = m_directory.GetCString();
662 const char *last_slash_ptr = ::strrchr(dir_cstr, '/');
663
664 // check for obvious cases before doing the full thing
665 if (!last_slash_ptr)
666 return FileSpec("", resolve);
667 if (last_slash_ptr == dir_cstr)
668 return FileSpec("/", resolve);
669
670 size_t last_slash_pos = last_slash_ptr - dir_cstr + 1;
671 ConstString new_path(dir_cstr, last_slash_pos);
672 return FileSpec(new_path.GetCString(), resolve);
673 } else
674 return FileSpec(m_directory.GetCString(), resolve);
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000675}
676
Kate Stoneb9c1b512016-09-06 20:57:50 +0000677ConstString FileSpec::GetLastPathComponent() const {
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000678 // CLEANUP: Use StringRef for string handling.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000679 if (m_filename)
680 return m_filename;
681 if (m_directory) {
682 const char *dir_cstr = m_directory.GetCString();
683 const char *last_slash_ptr = ::strrchr(dir_cstr, '/');
684 if (last_slash_ptr == NULL)
685 return m_directory;
686 if (last_slash_ptr == dir_cstr) {
687 if (last_slash_ptr[1] == 0)
688 return ConstString(last_slash_ptr);
689 else
690 return ConstString(last_slash_ptr + 1);
691 }
692 if (last_slash_ptr[1] != 0)
693 return ConstString(last_slash_ptr + 1);
694 const char *penultimate_slash_ptr = last_slash_ptr;
695 while (*penultimate_slash_ptr) {
696 --penultimate_slash_ptr;
697 if (penultimate_slash_ptr == dir_cstr)
698 break;
699 if (*penultimate_slash_ptr == '/')
700 break;
701 }
702 ConstString result(penultimate_slash_ptr + 1,
703 last_slash_ptr - penultimate_slash_ptr);
704 return result;
705 }
706 return ConstString();
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000707}
708
Pavel Labath59d725c2017-01-16 10:07:02 +0000709static std::string
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000710join_path_components(FileSpec::Style style,
Pavel Labath59d725c2017-01-16 10:07:02 +0000711 const std::vector<llvm::StringRef> components) {
712 std::string result;
713 for (size_t i = 0; i < components.size(); ++i) {
714 if (components[i].empty())
715 continue;
716 result += components[i];
717 if (i != components.size() - 1 &&
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000718 !IsPathSeparator(components[i].back(), style))
719 result += GetPreferredPathSeparator(style);
Pavel Labath59d725c2017-01-16 10:07:02 +0000720 }
721
722 return result;
723}
724
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000725void FileSpec::PrependPathComponent(llvm::StringRef component) {
726 if (component.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000727 return;
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000728
Kate Stoneb9c1b512016-09-06 20:57:50 +0000729 const bool resolve = false;
730 if (m_filename.IsEmpty() && m_directory.IsEmpty()) {
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000731 SetFile(component, resolve);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000732 return;
733 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000734
Pavel Labath59d725c2017-01-16 10:07:02 +0000735 std::string result =
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000736 join_path_components(m_style, {component, m_directory.GetStringRef(),
737 m_filename.GetStringRef()});
738 SetFile(result, resolve, m_style);
Chaoren Lind3173f32015-05-29 19:52:29 +0000739}
740
Kate Stoneb9c1b512016-09-06 20:57:50 +0000741void FileSpec::PrependPathComponent(const FileSpec &new_path) {
742 return PrependPathComponent(new_path.GetPath(false));
Chaoren Lin0c5a9c12015-06-05 00:28:06 +0000743}
744
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000745void FileSpec::AppendPathComponent(llvm::StringRef component) {
746 if (component.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000747 return;
748
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000749 component = component.drop_while(
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000750 [this](char c) { return IsPathSeparator(c, m_style); });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000751
Pavel Labath59d725c2017-01-16 10:07:02 +0000752 std::string result =
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000753 join_path_components(m_style, {m_directory.GetStringRef(),
754 m_filename.GetStringRef(), component});
Kate Stoneb9c1b512016-09-06 20:57:50 +0000755
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000756 SetFile(result, false, m_style);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000757}
758
759void FileSpec::AppendPathComponent(const FileSpec &new_path) {
760 return AppendPathComponent(new_path.GetPath(false));
761}
762
763void FileSpec::RemoveLastPathComponent() {
Zachary Turnerfe83ad82016-09-27 20:48:37 +0000764 // CLEANUP: Use StringRef for string handling.
765
Kate Stoneb9c1b512016-09-06 20:57:50 +0000766 const bool resolve = false;
767 if (m_filename.IsEmpty() && m_directory.IsEmpty()) {
768 SetFile("", resolve);
769 return;
770 }
771 if (m_directory.IsEmpty()) {
772 SetFile("", resolve);
773 return;
774 }
775 if (m_filename.IsEmpty()) {
776 const char *dir_cstr = m_directory.GetCString();
777 const char *last_slash_ptr = ::strrchr(dir_cstr, '/');
778
779 // check for obvious cases before doing the full thing
780 if (!last_slash_ptr) {
781 SetFile("", resolve);
782 return;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000783 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000784 if (last_slash_ptr == dir_cstr) {
785 SetFile("/", resolve);
786 return;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000787 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000788 size_t last_slash_pos = last_slash_ptr - dir_cstr + 1;
789 ConstString new_path(dir_cstr, last_slash_pos);
790 SetFile(new_path.GetCString(), resolve);
791 } else
792 SetFile(m_directory.GetCString(), resolve);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000793}
Greg Clayton1f746072012-08-29 21:13:06 +0000794//------------------------------------------------------------------
795/// Returns true if the filespec represents an implementation source
796/// file (files with a ".c", ".cpp", ".m", ".mm" (many more)
797/// extension).
798///
799/// @return
800/// \b true if the filespec represents an implementation source
801/// file, \b false otherwise.
802//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000803bool FileSpec::IsSourceImplementationFile() const {
804 ConstString extension(GetFileNameExtension());
Zachary Turner95eae422016-09-21 16:01:28 +0000805 if (!extension)
806 return false;
807
808 static RegularExpression g_source_file_regex(llvm::StringRef(
809 "^([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|["
810 "cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO]["
811 "rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])"
812 "$"));
813 return g_source_file_regex.Execute(extension.GetStringRef());
Greg Clayton1f746072012-08-29 21:13:06 +0000814}
815
Kate Stoneb9c1b512016-09-06 20:57:50 +0000816bool FileSpec::IsRelative() const {
817 const char *dir = m_directory.GetCString();
818 llvm::StringRef directory(dir ? dir : "");
Zachary Turner270e99a2014-12-08 21:36:42 +0000819
Kate Stoneb9c1b512016-09-06 20:57:50 +0000820 if (directory.size() > 0) {
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000821 if (PathStyleIsPosix(m_style)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000822 // If the path doesn't start with '/' or '~', return true
823 switch (directory[0]) {
824 case '/':
825 case '~':
826 return false;
827 default:
Greg Claytona0ca6602012-10-18 16:33:33 +0000828 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000829 }
830 } else {
831 if (directory.size() >= 2 && directory[1] == ':')
832 return false;
833 if (directory[0] == '/')
834 return false;
835 return true;
Greg Claytona0ca6602012-10-18 16:33:33 +0000836 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837 } else if (m_filename) {
838 // No directory, just a basename, return true
839 return true;
840 }
841 return false;
Greg Claytona0ca6602012-10-18 16:33:33 +0000842}
Chaoren Lin372e9062015-06-09 17:54:27 +0000843
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844bool FileSpec::IsAbsolute() const { return !FileSpec::IsRelative(); }
Zachary Turner827d5d72016-12-16 04:27:00 +0000845
846void llvm::format_provider<FileSpec>::format(const FileSpec &F,
847 raw_ostream &Stream,
848 StringRef Style) {
849 assert(
850 (Style.empty() || Style.equals_lower("F") || Style.equals_lower("D")) &&
851 "Invalid FileSpec style!");
852
853 StringRef dir = F.GetDirectory().GetStringRef();
854 StringRef file = F.GetFilename().GetStringRef();
855
856 if (dir.empty() && file.empty()) {
857 Stream << "(empty)";
858 return;
859 }
860
861 if (Style.equals_lower("F")) {
862 Stream << (file.empty() ? "(empty)" : file);
863 return;
864 }
865
866 // Style is either D or empty, either way we need to print the directory.
867 if (!dir.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000868 // Directory is stored in normalized form, which might be different than
869 // preferred form. In order to handle this, we need to cut off the
870 // filename, then denormalize, then write the entire denorm'ed directory.
Zachary Turner827d5d72016-12-16 04:27:00 +0000871 llvm::SmallString<64> denormalized_dir = dir;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000872 Denormalize(denormalized_dir, F.GetPathStyle());
Zachary Turner827d5d72016-12-16 04:27:00 +0000873 Stream << denormalized_dir;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000874 Stream << GetPreferredPathSeparator(F.GetPathStyle());
Zachary Turner827d5d72016-12-16 04:27:00 +0000875 }
876
877 if (Style.equals_lower("D")) {
878 // We only want to print the directory, so now just exit.
879 if (dir.empty())
880 Stream << "(empty)";
881 return;
882 }
883
884 if (!file.empty())
885 Stream << file;
886}