1 //===-- FileSpec.cpp --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Utility/FileSpec.h"
11 #include "lldb/Utility/RegularExpression.h"
12 #include "lldb/Utility/Stream.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Support/ErrorOr.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/Program.h"
22 #include "llvm/Support/raw_ostream.h"
25 #include <system_error>
34 using namespace lldb_private;
38 static constexpr FileSpec::Style GetNativeStyle() {
40 return FileSpec::Style::windows;
42 return FileSpec::Style::posix;
46 bool PathStyleIsPosix(FileSpec::Style style) {
47 return (style == FileSpec::Style::posix ||
48 (style == FileSpec::Style::native &&
49 GetNativeStyle() == FileSpec::Style::posix));
52 const char *GetPathSeparators(FileSpec::Style style) {
53 return llvm::sys::path::get_separator(style).data();
56 char GetPreferredPathSeparator(FileSpec::Style style) {
57 return GetPathSeparators(style)[0];
60 void Denormalize(llvm::SmallVectorImpl<char> &path, FileSpec::Style style) {
61 if (PathStyleIsPosix(style))
64 std::replace(path.begin(), path.end(), '/', '\\');
67 } // end anonymous namespace
69 FileSpec::FileSpec() : m_style(GetNativeStyle()) {}
71 //------------------------------------------------------------------
72 // Default constructor that can take an optional full path to a file on disk.
73 //------------------------------------------------------------------
74 FileSpec::FileSpec(llvm::StringRef path, Style style) : m_style(style) {
78 FileSpec::FileSpec(llvm::StringRef path, const llvm::Triple &Triple)
79 : FileSpec{path, Triple.isOSWindows() ? Style::windows : Style::posix} {}
81 //------------------------------------------------------------------
83 //------------------------------------------------------------------
84 FileSpec::FileSpec(const FileSpec &rhs)
85 : m_directory(rhs.m_directory), m_filename(rhs.m_filename),
86 m_is_resolved(rhs.m_is_resolved), m_style(rhs.m_style) {}
88 //------------------------------------------------------------------
90 //------------------------------------------------------------------
91 FileSpec::FileSpec(const FileSpec *rhs) : m_directory(), m_filename() {
96 //------------------------------------------------------------------
97 // Virtual destructor in case anyone inherits from this class.
98 //------------------------------------------------------------------
99 FileSpec::~FileSpec() {}
102 //------------------------------------------------------------------
103 /// Safely get a character at the specified index.
106 /// A full, partial, or relative path to a file.
109 /// An index into path which may or may not be valid.
112 /// The character at index \a i if the index is valid, or 0 if
113 /// the index is not valid.
114 //------------------------------------------------------------------
115 inline char safeCharAtIndex(const llvm::StringRef &path, size_t i) {
121 //------------------------------------------------------------------
122 /// Check if a path needs to be normalized.
124 /// Check if a path needs to be normalized. We currently consider a
125 /// path to need normalization if any of the following are true
126 /// - path contains "/./"
127 /// - path contains "/../"
128 /// - path contains "//"
129 /// - path ends with "/"
130 /// Paths that start with "./" or with "../" are not considered to
131 /// need normalization since we aren't trying to resolve the path,
132 /// we are just trying to remove redundant things from the path.
135 /// A full, partial, or relative path to a file.
138 /// Returns \b true if the path needs to be normalized.
139 //------------------------------------------------------------------
140 bool needsNormalization(const llvm::StringRef &path) {
143 // We strip off leading "." values so these paths need to be normalized
146 for (auto i = path.find_first_of("\\/"); i != llvm::StringRef::npos;
147 i = path.find_first_of("\\/", i + 1)) {
148 const auto next = safeCharAtIndex(path, i+1);
151 // path separator char at the end of the string which should be
152 // stripped unless it is the one and only character
156 // two path separator chars in the middle of a path needs to be
164 const auto next_next = safeCharAtIndex(path, i+2);
167 case 0: return true; // ends with "/."
170 return true; // contains "/./"
172 const auto next_next_next = safeCharAtIndex(path, i+3);
173 switch (next_next_next) {
175 case 0: return true; // ends with "/.."
178 return true; // contains "/../"
195 //------------------------------------------------------------------
196 // Assignment operator.
197 //------------------------------------------------------------------
198 const FileSpec &FileSpec::operator=(const FileSpec &rhs) {
200 m_directory = rhs.m_directory;
201 m_filename = rhs.m_filename;
202 m_is_resolved = rhs.m_is_resolved;
203 m_style = rhs.m_style;
208 void FileSpec::SetFile(llvm::StringRef pathname) { SetFile(pathname, m_style); }
210 //------------------------------------------------------------------
211 // Update the contents of this object with a new path. The path will be split
212 // up into a directory and filename and stored as uniqued string values for
213 // quick comparison and efficient memory usage.
214 //------------------------------------------------------------------
215 void FileSpec::SetFile(llvm::StringRef pathname, Style style) {
218 m_is_resolved = false;
219 m_style = (style == Style::native) ? GetNativeStyle() : style;
221 if (pathname.empty())
224 llvm::SmallString<128> resolved(pathname);
226 // Normalize the path by removing ".", ".." and other redundant components.
227 if (needsNormalization(resolved))
228 llvm::sys::path::remove_dots(resolved, true, m_style);
230 // Normalize back slashes to forward slashes
231 if (m_style == Style::windows)
232 std::replace(resolved.begin(), resolved.end(), '\\', '/');
234 if (resolved.empty()) {
235 // If we have no path after normalization set the path to the current
236 // directory. This matches what python does and also a few other path
238 m_filename.SetString(".");
242 // Split path into filename and directory. We rely on the underlying char
243 // pointer to be nullptr when the components are empty.
244 llvm::StringRef filename = llvm::sys::path::filename(resolved, m_style);
245 if(!filename.empty())
246 m_filename.SetString(filename);
248 llvm::StringRef directory = llvm::sys::path::parent_path(resolved, m_style);
249 if(!directory.empty())
250 m_directory.SetString(directory);
253 void FileSpec::SetFile(llvm::StringRef path, const llvm::Triple &Triple) {
254 return SetFile(path, Triple.isOSWindows() ? Style::windows : Style::posix);
257 //----------------------------------------------------------------------
258 // Convert to pointer operator. This allows code to check any FileSpec objects
259 // to see if they contain anything valid using code such as:
263 //----------------------------------------------------------------------
264 FileSpec::operator bool() const { return m_filename || m_directory; }
266 //----------------------------------------------------------------------
267 // Logical NOT operator. This allows code to check any FileSpec objects to see
268 // if they are invalid using code such as:
272 //----------------------------------------------------------------------
273 bool FileSpec::operator!() const { return !m_directory && !m_filename; }
275 bool FileSpec::DirectoryEquals(const FileSpec &rhs) const {
276 const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
277 return ConstString::Equals(m_directory, rhs.m_directory, case_sensitive);
280 bool FileSpec::FileEquals(const FileSpec &rhs) const {
281 const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
282 return ConstString::Equals(m_filename, rhs.m_filename, case_sensitive);
285 //------------------------------------------------------------------
287 //------------------------------------------------------------------
288 bool FileSpec::operator==(const FileSpec &rhs) const {
289 return FileEquals(rhs) && DirectoryEquals(rhs);
292 //------------------------------------------------------------------
293 // Not equal to operator
294 //------------------------------------------------------------------
295 bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); }
297 //------------------------------------------------------------------
298 // Less than operator
299 //------------------------------------------------------------------
300 bool FileSpec::operator<(const FileSpec &rhs) const {
301 return FileSpec::Compare(*this, rhs, true) < 0;
304 //------------------------------------------------------------------
305 // Dump a FileSpec object to a stream
306 //------------------------------------------------------------------
307 Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) {
312 //------------------------------------------------------------------
313 // Clear this object by releasing both the directory and filename string values
314 // and making them both the empty string.
315 //------------------------------------------------------------------
316 void FileSpec::Clear() {
321 //------------------------------------------------------------------
322 // Compare two FileSpec objects. If "full" is true, then both the directory and
323 // the filename must match. If "full" is false, then the directory names for
324 // "a" and "b" are only compared if they are both non-empty. This allows a
325 // FileSpec object to only contain a filename and it can match FileSpec objects
326 // that have matching filenames with different paths.
328 // Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" and "1" if
329 // "a" is greater than "b".
330 //------------------------------------------------------------------
331 int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) {
334 // case sensitivity of compare
335 const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
337 // If full is true, then we must compare both the directory and filename.
339 // If full is false, then if either directory is empty, then we match on the
340 // basename only, and if both directories have valid values, we still do a
341 // full compare. This allows for matching when we just have a filename in one
342 // of the FileSpec objects.
344 if (full || (a.m_directory && b.m_directory)) {
345 result = ConstString::Compare(a.m_directory, b.m_directory, case_sensitive);
349 return ConstString::Compare(a.m_filename, b.m_filename, case_sensitive);
352 bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full) {
353 // case sensitivity of equality test
354 const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
356 const bool filenames_equal = ConstString::Equals(a.m_filename,
360 if (!filenames_equal)
363 if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty()))
364 return filenames_equal;
369 //------------------------------------------------------------------
370 // Dump the object to the supplied stream. If the object contains a valid
371 // directory name, it will be displayed followed by a directory delimiter, and
373 //------------------------------------------------------------------
374 void FileSpec::Dump(Stream *s) const {
376 std::string path{GetPath(true)};
378 char path_separator = GetPreferredPathSeparator(m_style);
379 if (!m_filename && !path.empty() && path.back() != path_separator)
380 s->PutChar(path_separator);
384 FileSpec::Style FileSpec::GetPathStyle() const { return m_style; }
386 //------------------------------------------------------------------
387 // Directory string get accessor.
388 //------------------------------------------------------------------
389 ConstString &FileSpec::GetDirectory() { return m_directory; }
391 //------------------------------------------------------------------
392 // Directory string const get accessor.
393 //------------------------------------------------------------------
394 const ConstString &FileSpec::GetDirectory() const { return m_directory; }
396 //------------------------------------------------------------------
397 // Filename string get accessor.
398 //------------------------------------------------------------------
399 ConstString &FileSpec::GetFilename() { return m_filename; }
401 //------------------------------------------------------------------
402 // Filename string const get accessor.
403 //------------------------------------------------------------------
404 const ConstString &FileSpec::GetFilename() const { return m_filename; }
406 //------------------------------------------------------------------
407 // Extract the directory and path into a fixed buffer. This is needed as the
408 // directory and path are stored in separate string values.
409 //------------------------------------------------------------------
410 size_t FileSpec::GetPath(char *path, size_t path_max_len,
411 bool denormalize) const {
415 std::string result = GetPath(denormalize);
416 ::snprintf(path, path_max_len, "%s", result.c_str());
417 return std::min(path_max_len - 1, result.length());
420 std::string FileSpec::GetPath(bool denormalize) const {
421 llvm::SmallString<64> result;
422 GetPath(result, denormalize);
423 return std::string(result.begin(), result.end());
426 const char *FileSpec::GetCString(bool denormalize) const {
427 return ConstString{GetPath(denormalize)}.AsCString(nullptr);
430 void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path,
431 bool denormalize) const {
432 path.append(m_directory.GetStringRef().begin(),
433 m_directory.GetStringRef().end());
434 // Since the path was normalized and all paths use '/' when stored in these
435 // objects, we don't need to look for the actual syntax specific path
436 // separator, we just look for and insert '/'.
437 if (m_directory && m_filename && m_directory.GetStringRef().back() != '/' &&
438 m_filename.GetStringRef().back() != '/')
439 path.insert(path.end(), '/');
440 path.append(m_filename.GetStringRef().begin(),
441 m_filename.GetStringRef().end());
442 if (denormalize && !path.empty())
443 Denormalize(path, m_style);
446 ConstString FileSpec::GetFileNameExtension() const {
448 llvm::sys::path::extension(m_filename.GetStringRef(), m_style));
451 ConstString FileSpec::GetFileNameStrippingExtension() const {
452 return ConstString(llvm::sys::path::stem(m_filename.GetStringRef(), m_style));
455 //------------------------------------------------------------------
456 // Return the size in bytes that this object takes in memory. This returns the
457 // size in bytes of this object, not any shared string values it may refer to.
458 //------------------------------------------------------------------
459 size_t FileSpec::MemorySize() const {
460 return m_filename.MemorySize() + m_directory.MemorySize();
464 FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const {
465 FileSpec ret = *this;
466 ret.AppendPathComponent(component);
470 FileSpec FileSpec::CopyByRemovingLastPathComponent() const {
471 llvm::SmallString<64> current_path;
472 GetPath(current_path, false);
473 if (llvm::sys::path::has_parent_path(current_path, m_style))
474 return FileSpec(llvm::sys::path::parent_path(current_path, m_style),
479 ConstString FileSpec::GetLastPathComponent() const {
480 llvm::SmallString<64> current_path;
481 GetPath(current_path, false);
482 return ConstString(llvm::sys::path::filename(current_path, m_style));
485 void FileSpec::PrependPathComponent(llvm::StringRef component) {
486 llvm::SmallString<64> new_path(component);
487 llvm::SmallString<64> current_path;
488 GetPath(current_path, false);
489 llvm::sys::path::append(new_path,
490 llvm::sys::path::begin(current_path, m_style),
491 llvm::sys::path::end(current_path), m_style);
492 SetFile(new_path, m_style);
495 void FileSpec::PrependPathComponent(const FileSpec &new_path) {
496 return PrependPathComponent(new_path.GetPath(false));
499 void FileSpec::AppendPathComponent(llvm::StringRef component) {
500 llvm::SmallString<64> current_path;
501 GetPath(current_path, false);
502 llvm::sys::path::append(current_path, m_style, component);
503 SetFile(current_path, m_style);
506 void FileSpec::AppendPathComponent(const FileSpec &new_path) {
507 return AppendPathComponent(new_path.GetPath(false));
510 bool FileSpec::RemoveLastPathComponent() {
511 llvm::SmallString<64> current_path;
512 GetPath(current_path, false);
513 if (llvm::sys::path::has_parent_path(current_path, m_style)) {
514 SetFile(llvm::sys::path::parent_path(current_path, m_style));
519 //------------------------------------------------------------------
520 /// Returns true if the filespec represents an implementation source
521 /// file (files with a ".c", ".cpp", ".m", ".mm" (many more)
525 /// \b true if the filespec represents an implementation source
526 /// file, \b false otherwise.
527 //------------------------------------------------------------------
528 bool FileSpec::IsSourceImplementationFile() const {
529 ConstString extension(GetFileNameExtension());
533 static RegularExpression g_source_file_regex(llvm::StringRef(
534 "^.([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|["
535 "cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO]["
536 "rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])"
538 return g_source_file_regex.Execute(extension.GetStringRef());
541 bool FileSpec::IsRelative() const {
542 return !IsAbsolute();
545 bool FileSpec::IsAbsolute() const {
546 llvm::SmallString<64> current_path;
547 GetPath(current_path, false);
549 // Early return if the path is empty.
550 if (current_path.empty())
553 // We consider paths starting with ~ to be absolute.
554 if (current_path[0] == '~')
557 return llvm::sys::path::is_absolute(current_path, m_style);
560 void llvm::format_provider<FileSpec>::format(const FileSpec &F,
564 (Style.empty() || Style.equals_lower("F") || Style.equals_lower("D")) &&
565 "Invalid FileSpec style!");
567 StringRef dir = F.GetDirectory().GetStringRef();
568 StringRef file = F.GetFilename().GetStringRef();
570 if (dir.empty() && file.empty()) {
575 if (Style.equals_lower("F")) {
576 Stream << (file.empty() ? "(empty)" : file);
580 // Style is either D or empty, either way we need to print the directory.
582 // Directory is stored in normalized form, which might be different than
583 // preferred form. In order to handle this, we need to cut off the
584 // filename, then denormalize, then write the entire denorm'ed directory.
585 llvm::SmallString<64> denormalized_dir = dir;
586 Denormalize(denormalized_dir, F.GetPathStyle());
587 Stream << denormalized_dir;
588 Stream << GetPreferredPathSeparator(F.GetPathStyle());
591 if (Style.equals_lower("D")) {
592 // We only want to print the directory, so now just exit.