1 //===-- 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 10 #include "lldb/Utility/FileSpec.h" 11 #include "lldb/Utility/RegularExpression.h" 12 #include "lldb/Utility/Stream.h" 13 #include "lldb/Utility/TildeExpressionResolver.h" 14 15 #include "llvm/ADT/SmallString.h" // for SmallString 16 #include "llvm/ADT/SmallVector.h" // for SmallVectorTemplat... 17 #include "llvm/ADT/StringRef.h" 18 #include "llvm/ADT/Triple.h" // for Triple 19 #include "llvm/ADT/Twine.h" // for Twine 20 #include "llvm/Support/ErrorOr.h" // for ErrorOr 21 #include "llvm/Support/FileSystem.h" 22 #include "llvm/Support/Path.h" 23 #include "llvm/Support/Program.h" 24 #include "llvm/Support/raw_ostream.h" // for raw_ostream, fs 25 26 #include <algorithm> // for replace, min, unique 27 #include <system_error> // for error_code 28 #include <vector> // for vector 29 30 #include <assert.h> // for assert 31 #include <stdio.h> // for size_t, NULL, snpr... 32 #include <string.h> // for strcmp 33 34 using namespace lldb; 35 using namespace lldb_private; 36 37 namespace { 38 39 static constexpr FileSpec::PathSyntax GetNativeSyntax() { 40 #if defined(_WIN32) 41 return FileSpec::ePathSyntaxWindows; 42 #else 43 return FileSpec::ePathSyntaxPosix; 44 #endif 45 } 46 47 bool PathSyntaxIsPosix(FileSpec::PathSyntax syntax) { 48 return (syntax == FileSpec::ePathSyntaxPosix || 49 (syntax == FileSpec::ePathSyntaxHostNative && 50 GetNativeSyntax() == FileSpec::ePathSyntaxPosix)); 51 } 52 53 const char *GetPathSeparators(FileSpec::PathSyntax syntax) { 54 return PathSyntaxIsPosix(syntax) ? "/" : "\\/"; 55 } 56 57 char GetPreferredPathSeparator(FileSpec::PathSyntax syntax) { 58 return GetPathSeparators(syntax)[0]; 59 } 60 61 bool IsPathSeparator(char value, FileSpec::PathSyntax syntax) { 62 return value == '/' || (!PathSyntaxIsPosix(syntax) && value == '\\'); 63 } 64 65 inline llvm::sys::path::Style 66 LLVMPathSyntax(FileSpec::PathSyntax lldb_syntax) { 67 switch (lldb_syntax) { 68 case FileSpec::ePathSyntaxPosix: 69 return llvm::sys::path::Style::posix; 70 case FileSpec::ePathSyntaxWindows: 71 return llvm::sys::path::Style::windows; 72 case FileSpec::ePathSyntaxHostNative: 73 return llvm::sys::path::Style::native; 74 }; 75 return llvm::sys::path::Style::native; 76 } 77 78 void Denormalize(llvm::SmallVectorImpl<char> &path, 79 FileSpec::PathSyntax syntax) { 80 if (PathSyntaxIsPosix(syntax)) 81 return; 82 83 std::replace(path.begin(), path.end(), '/', '\\'); 84 } 85 86 size_t FilenamePos(llvm::StringRef str, FileSpec::PathSyntax syntax) { 87 if (str.size() == 2 && IsPathSeparator(str[0], syntax) && str[0] == str[1]) 88 return 0; 89 90 if (str.size() > 0 && IsPathSeparator(str.back(), syntax)) 91 return str.size() - 1; 92 93 size_t pos = str.find_last_of(GetPathSeparators(syntax), str.size() - 1); 94 95 if (!PathSyntaxIsPosix(syntax) && pos == llvm::StringRef::npos) 96 pos = str.find_last_of(':', str.size() - 2); 97 98 if (pos == llvm::StringRef::npos || 99 (pos == 1 && IsPathSeparator(str[0], syntax))) 100 return 0; 101 102 return pos + 1; 103 } 104 105 size_t RootDirStart(llvm::StringRef str, FileSpec::PathSyntax syntax) { 106 // case "c:/" 107 if (!PathSyntaxIsPosix(syntax) && 108 (str.size() > 2 && str[1] == ':' && IsPathSeparator(str[2], syntax))) 109 return 2; 110 111 // case "//" 112 if (str.size() == 2 && IsPathSeparator(str[0], syntax) && str[0] == str[1]) 113 return llvm::StringRef::npos; 114 115 // case "//net" 116 if (str.size() > 3 && IsPathSeparator(str[0], syntax) && str[0] == str[1] && 117 !IsPathSeparator(str[2], syntax)) 118 return str.find_first_of(GetPathSeparators(syntax), 2); 119 120 // case "/" 121 if (str.size() > 0 && IsPathSeparator(str[0], syntax)) 122 return 0; 123 124 return llvm::StringRef::npos; 125 } 126 127 size_t ParentPathEnd(llvm::StringRef path, FileSpec::PathSyntax syntax) { 128 size_t end_pos = FilenamePos(path, syntax); 129 130 bool filename_was_sep = 131 path.size() > 0 && IsPathSeparator(path[end_pos], syntax); 132 133 // Skip separators except for root dir. 134 size_t root_dir_pos = RootDirStart(path.substr(0, end_pos), syntax); 135 136 while (end_pos > 0 && (end_pos - 1) != root_dir_pos && 137 IsPathSeparator(path[end_pos - 1], syntax)) 138 --end_pos; 139 140 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep) 141 return llvm::StringRef::npos; 142 143 return end_pos; 144 } 145 146 } // end anonymous namespace 147 148 void FileSpec::Resolve(llvm::SmallVectorImpl<char> &path) { 149 if (path.empty()) 150 return; 151 152 llvm::SmallString<32> Source(path.begin(), path.end()); 153 StandardTildeExpressionResolver Resolver; 154 Resolver.ResolveFullPath(Source, path); 155 156 // Save a copy of the original path that's passed in 157 llvm::SmallString<128> original_path(path.begin(), path.end()); 158 159 llvm::sys::fs::make_absolute(path); 160 if (!llvm::sys::fs::exists(path)) { 161 path.clear(); 162 path.append(original_path.begin(), original_path.end()); 163 } 164 } 165 166 FileSpec::FileSpec() : m_syntax(GetNativeSyntax()) {} 167 168 //------------------------------------------------------------------ 169 // Default constructor that can take an optional full path to a file on disk. 170 //------------------------------------------------------------------ 171 FileSpec::FileSpec(llvm::StringRef path, bool resolve_path, PathSyntax syntax) 172 : m_syntax(syntax) { 173 SetFile(path, resolve_path, syntax); 174 } 175 176 FileSpec::FileSpec(llvm::StringRef path, bool resolve_path, 177 const llvm::Triple &Triple) 178 : FileSpec{path, resolve_path, 179 Triple.isOSWindows() ? ePathSyntaxWindows : ePathSyntaxPosix} {} 180 181 //------------------------------------------------------------------ 182 // Copy constructor 183 //------------------------------------------------------------------ 184 FileSpec::FileSpec(const FileSpec &rhs) 185 : m_directory(rhs.m_directory), m_filename(rhs.m_filename), 186 m_is_resolved(rhs.m_is_resolved), m_syntax(rhs.m_syntax) {} 187 188 //------------------------------------------------------------------ 189 // Copy constructor 190 //------------------------------------------------------------------ 191 FileSpec::FileSpec(const FileSpec *rhs) : m_directory(), m_filename() { 192 if (rhs) 193 *this = *rhs; 194 } 195 196 //------------------------------------------------------------------ 197 // Virtual destructor in case anyone inherits from this class. 198 //------------------------------------------------------------------ 199 FileSpec::~FileSpec() {} 200 201 namespace { 202 //------------------------------------------------------------------ 203 /// Safely get a character at the specified index. 204 /// 205 /// @param[in] path 206 /// A full, partial, or relative path to a file. 207 /// 208 /// @param[in] i 209 /// An index into path which may or may not be valid. 210 /// 211 /// @return 212 /// The character at index \a i if the index is valid, or 0 if 213 /// the index is not valid. 214 //------------------------------------------------------------------ 215 inline char safeCharAtIndex(const llvm::StringRef &path, size_t i) { 216 if (i < path.size()) 217 return path[i]; 218 return 0; 219 } 220 221 //------------------------------------------------------------------ 222 /// Check if a path needs to be normalized. 223 /// 224 /// Check if a path needs to be normalized. We currently consider a 225 /// path to need normalization if any of the following are true 226 /// - path contains "/./" 227 /// - path contains "/../" 228 /// - path contains "//" 229 /// - path ends with "/" 230 /// Paths that start with "./" or with "../" are not considered to 231 /// need normalization since we aren't trying to resolve the path, 232 /// we are just trying to remove redundant things from the path. 233 /// 234 /// @param[in] path 235 /// A full, partial, or relative path to a file. 236 /// 237 /// @return 238 /// Returns \b true if the path needs to be normalized. 239 //------------------------------------------------------------------ 240 bool needsNormalization(const llvm::StringRef &path) { 241 if (path.empty()) 242 return false; 243 // We strip off leading "." values so these paths need to be normalized 244 if (path[0] == '.') 245 return true; 246 for (auto i = path.find_first_of("\\/"); i != llvm::StringRef::npos; 247 i = path.find_first_of("\\/", i + 1)) { 248 const auto next = safeCharAtIndex(path, i+1); 249 switch (next) { 250 case 0: 251 // path separator char at the end of the string which should be 252 // stripped unless it is the one and only character 253 return i > 0; 254 case '/': 255 case '\\': 256 // two path separator chars in the middle of a path needs to be 257 // normalized 258 if (i > 0) 259 return true; 260 ++i; 261 break; 262 263 case '.': { 264 const auto next_next = safeCharAtIndex(path, i+2); 265 switch (next_next) { 266 default: break; 267 case 0: return true; // ends with "/." 268 case '/': 269 case '\\': 270 return true; // contains "/./" 271 case '.': { 272 const auto next_next_next = safeCharAtIndex(path, i+3); 273 switch (next_next_next) { 274 default: break; 275 case 0: return true; // ends with "/.." 276 case '/': 277 case '\\': 278 return true; // contains "/../" 279 } 280 break; 281 } 282 } 283 } 284 break; 285 286 default: 287 break; 288 } 289 } 290 return false; 291 } 292 293 294 } 295 //------------------------------------------------------------------ 296 // Assignment operator. 297 //------------------------------------------------------------------ 298 const FileSpec &FileSpec::operator=(const FileSpec &rhs) { 299 if (this != &rhs) { 300 m_directory = rhs.m_directory; 301 m_filename = rhs.m_filename; 302 m_is_resolved = rhs.m_is_resolved; 303 m_syntax = rhs.m_syntax; 304 } 305 return *this; 306 } 307 308 //------------------------------------------------------------------ 309 // Update the contents of this object with a new path. The path will be split 310 // up into a directory and filename and stored as uniqued string values for 311 // quick comparison and efficient memory usage. 312 //------------------------------------------------------------------ 313 void FileSpec::SetFile(llvm::StringRef pathname, bool resolve, 314 PathSyntax syntax) { 315 // CLEANUP: Use StringRef for string handling. This function is kind of a 316 // mess and the unclear semantics of RootDirStart and ParentPathEnd make it 317 // very difficult to understand this function. There's no reason this 318 // function should be particularly complicated or difficult to understand. 319 m_filename.Clear(); 320 m_directory.Clear(); 321 m_is_resolved = false; 322 m_syntax = (syntax == ePathSyntaxHostNative) ? GetNativeSyntax() : syntax; 323 324 if (pathname.empty()) 325 return; 326 327 llvm::SmallString<64> resolved(pathname); 328 329 if (resolve) { 330 FileSpec::Resolve(resolved); 331 m_is_resolved = true; 332 } 333 334 // Normalize the path by removing ".", ".." and other redundant components. 335 if (needsNormalization(resolved)) 336 llvm::sys::path::remove_dots(resolved, true, LLVMPathSyntax(m_syntax)); 337 338 // Normalize back slashes to forward slashes 339 if (m_syntax == FileSpec::ePathSyntaxWindows) 340 std::replace(resolved.begin(), resolved.end(), '\\', '/'); 341 342 llvm::StringRef resolve_path_ref(resolved.c_str()); 343 size_t dir_end = ParentPathEnd(resolve_path_ref, m_syntax); 344 if (dir_end == 0) { 345 m_filename.SetString(resolve_path_ref); 346 return; 347 } 348 349 m_directory.SetString(resolve_path_ref.substr(0, dir_end)); 350 351 size_t filename_begin = dir_end; 352 size_t root_dir_start = RootDirStart(resolve_path_ref, m_syntax); 353 while (filename_begin != llvm::StringRef::npos && 354 filename_begin < resolve_path_ref.size() && 355 filename_begin != root_dir_start && 356 IsPathSeparator(resolve_path_ref[filename_begin], m_syntax)) 357 ++filename_begin; 358 m_filename.SetString((filename_begin == llvm::StringRef::npos || 359 filename_begin >= resolve_path_ref.size()) 360 ? "." 361 : resolve_path_ref.substr(filename_begin)); 362 } 363 364 void FileSpec::SetFile(llvm::StringRef path, bool resolve, 365 const llvm::Triple &Triple) { 366 return SetFile(path, resolve, 367 Triple.isOSWindows() ? ePathSyntaxWindows : ePathSyntaxPosix); 368 } 369 370 //---------------------------------------------------------------------- 371 // Convert to pointer operator. This allows code to check any FileSpec objects 372 // to see if they contain anything valid using code such as: 373 // 374 // if (file_spec) 375 // {} 376 //---------------------------------------------------------------------- 377 FileSpec::operator bool() const { return m_filename || m_directory; } 378 379 //---------------------------------------------------------------------- 380 // Logical NOT operator. This allows code to check any FileSpec objects to see 381 // if they are invalid using code such as: 382 // 383 // if (!file_spec) 384 // {} 385 //---------------------------------------------------------------------- 386 bool FileSpec::operator!() const { return !m_directory && !m_filename; } 387 388 bool FileSpec::DirectoryEquals(const FileSpec &rhs) const { 389 const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive(); 390 return ConstString::Equals(m_directory, rhs.m_directory, case_sensitive); 391 } 392 393 bool FileSpec::FileEquals(const FileSpec &rhs) const { 394 const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive(); 395 return ConstString::Equals(m_filename, rhs.m_filename, case_sensitive); 396 } 397 398 //------------------------------------------------------------------ 399 // Equal to operator 400 //------------------------------------------------------------------ 401 bool FileSpec::operator==(const FileSpec &rhs) const { 402 if (!FileEquals(rhs)) 403 return false; 404 if (DirectoryEquals(rhs)) 405 return true; 406 407 // TODO: determine if we want to keep this code in here. 408 // The code below was added to handle a case where we were trying to set a 409 // file and line breakpoint and one path was resolved, and the other not and 410 // the directory was in a mount point that resolved to a more complete path: 411 // "/tmp/a.c" == "/private/tmp/a.c". I might end up pulling this out... 412 if (IsResolved() && rhs.IsResolved()) { 413 // Both paths are resolved, no need to look further... 414 return false; 415 } 416 417 FileSpec resolved_lhs(*this); 418 419 // If "this" isn't resolved, resolve it 420 if (!IsResolved()) { 421 if (resolved_lhs.ResolvePath()) { 422 // This path wasn't resolved but now it is. Check if the resolved 423 // directory is the same as our unresolved directory, and if so, we can 424 // mark this object as resolved to avoid more future resolves 425 m_is_resolved = (m_directory == resolved_lhs.m_directory); 426 } else 427 return false; 428 } 429 430 FileSpec resolved_rhs(rhs); 431 if (!rhs.IsResolved()) { 432 if (resolved_rhs.ResolvePath()) { 433 // rhs's path wasn't resolved but now it is. Check if the resolved 434 // directory is the same as rhs's unresolved directory, and if so, we can 435 // mark this object as resolved to avoid more future resolves 436 rhs.m_is_resolved = (rhs.m_directory == resolved_rhs.m_directory); 437 } else 438 return false; 439 } 440 441 // If we reach this point in the code we were able to resolve both paths and 442 // since we only resolve the paths if the basenames are equal, then we can 443 // just check if both directories are equal... 444 return DirectoryEquals(rhs); 445 } 446 447 //------------------------------------------------------------------ 448 // Not equal to operator 449 //------------------------------------------------------------------ 450 bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); } 451 452 //------------------------------------------------------------------ 453 // Less than operator 454 //------------------------------------------------------------------ 455 bool FileSpec::operator<(const FileSpec &rhs) const { 456 return FileSpec::Compare(*this, rhs, true) < 0; 457 } 458 459 //------------------------------------------------------------------ 460 // Dump a FileSpec object to a stream 461 //------------------------------------------------------------------ 462 Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) { 463 f.Dump(&s); 464 return s; 465 } 466 467 //------------------------------------------------------------------ 468 // Clear this object by releasing both the directory and filename string values 469 // and making them both the empty string. 470 //------------------------------------------------------------------ 471 void FileSpec::Clear() { 472 m_directory.Clear(); 473 m_filename.Clear(); 474 } 475 476 //------------------------------------------------------------------ 477 // Compare two FileSpec objects. If "full" is true, then both the directory and 478 // the filename must match. If "full" is false, then the directory names for 479 // "a" and "b" are only compared if they are both non-empty. This allows a 480 // FileSpec object to only contain a filename and it can match FileSpec objects 481 // that have matching filenames with different paths. 482 // 483 // Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" and "1" if 484 // "a" is greater than "b". 485 //------------------------------------------------------------------ 486 int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) { 487 int result = 0; 488 489 // case sensitivity of compare 490 const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive(); 491 492 // If full is true, then we must compare both the directory and filename. 493 494 // If full is false, then if either directory is empty, then we match on the 495 // basename only, and if both directories have valid values, we still do a 496 // full compare. This allows for matching when we just have a filename in one 497 // of the FileSpec objects. 498 499 if (full || (a.m_directory && b.m_directory)) { 500 result = ConstString::Compare(a.m_directory, b.m_directory, case_sensitive); 501 if (result) 502 return result; 503 } 504 return ConstString::Compare(a.m_filename, b.m_filename, case_sensitive); 505 } 506 507 bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full) { 508 509 // case sensitivity of equality test 510 const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive(); 511 512 const bool filenames_equal = ConstString::Equals(a.m_filename, 513 b.m_filename, 514 case_sensitive); 515 516 if (!filenames_equal) 517 return false; 518 519 if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty())) 520 return filenames_equal; 521 522 return a == b; 523 } 524 525 //------------------------------------------------------------------ 526 // Dump the object to the supplied stream. If the object contains a valid 527 // directory name, it will be displayed followed by a directory delimiter, and 528 // the filename. 529 //------------------------------------------------------------------ 530 void FileSpec::Dump(Stream *s) const { 531 if (s) { 532 std::string path{GetPath(true)}; 533 s->PutCString(path); 534 char path_separator = GetPreferredPathSeparator(m_syntax); 535 if (!m_filename && !path.empty() && path.back() != path_separator) 536 s->PutChar(path_separator); 537 } 538 } 539 540 //------------------------------------------------------------------ 541 // Returns true if the file exists. 542 //------------------------------------------------------------------ 543 bool FileSpec::Exists() const { return llvm::sys::fs::exists(GetPath()); } 544 545 bool FileSpec::Readable() const { 546 return GetPermissions() & llvm::sys::fs::perms::all_read; 547 } 548 549 bool FileSpec::ResolveExecutableLocation() { 550 // CLEANUP: Use StringRef for string handling. 551 if (!m_directory) { 552 const char *file_cstr = m_filename.GetCString(); 553 if (file_cstr) { 554 const std::string file_str(file_cstr); 555 llvm::ErrorOr<std::string> error_or_path = 556 llvm::sys::findProgramByName(file_str); 557 if (!error_or_path) 558 return false; 559 std::string path = error_or_path.get(); 560 llvm::StringRef dir_ref = llvm::sys::path::parent_path(path); 561 if (!dir_ref.empty()) { 562 // FindProgramByName returns "." if it can't find the file. 563 if (strcmp(".", dir_ref.data()) == 0) 564 return false; 565 566 m_directory.SetCString(dir_ref.data()); 567 if (Exists()) 568 return true; 569 else { 570 // If FindProgramByName found the file, it returns the directory + 571 // filename in its return results. We need to separate them. 572 FileSpec tmp_file(dir_ref.data(), false); 573 if (tmp_file.Exists()) { 574 m_directory = tmp_file.m_directory; 575 return true; 576 } 577 } 578 } 579 } 580 } 581 582 return false; 583 } 584 585 bool FileSpec::ResolvePath() { 586 if (m_is_resolved) 587 return true; // We have already resolved this path 588 589 // SetFile(...) will set m_is_resolved correctly if it can resolve the path 590 SetFile(GetPath(false), true); 591 return m_is_resolved; 592 } 593 594 uint64_t FileSpec::GetByteSize() const { 595 uint64_t Size = 0; 596 if (llvm::sys::fs::file_size(GetPath(), Size)) 597 return 0; 598 return Size; 599 } 600 601 FileSpec::PathSyntax FileSpec::GetPathSyntax() const { return m_syntax; } 602 603 uint32_t FileSpec::GetPermissions() const { 604 namespace fs = llvm::sys::fs; 605 fs::file_status st; 606 if (fs::status(GetPath(), st, false)) 607 return fs::perms::perms_not_known; 608 609 return st.permissions(); 610 } 611 612 //------------------------------------------------------------------ 613 // Directory string get accessor. 614 //------------------------------------------------------------------ 615 ConstString &FileSpec::GetDirectory() { return m_directory; } 616 617 //------------------------------------------------------------------ 618 // Directory string const get accessor. 619 //------------------------------------------------------------------ 620 const ConstString &FileSpec::GetDirectory() const { return m_directory; } 621 622 //------------------------------------------------------------------ 623 // Filename string get accessor. 624 //------------------------------------------------------------------ 625 ConstString &FileSpec::GetFilename() { return m_filename; } 626 627 //------------------------------------------------------------------ 628 // Filename string const get accessor. 629 //------------------------------------------------------------------ 630 const ConstString &FileSpec::GetFilename() const { return m_filename; } 631 632 //------------------------------------------------------------------ 633 // Extract the directory and path into a fixed buffer. This is needed as the 634 // directory and path are stored in separate string values. 635 //------------------------------------------------------------------ 636 size_t FileSpec::GetPath(char *path, size_t path_max_len, 637 bool denormalize) const { 638 if (!path) 639 return 0; 640 641 std::string result = GetPath(denormalize); 642 ::snprintf(path, path_max_len, "%s", result.c_str()); 643 return std::min(path_max_len - 1, result.length()); 644 } 645 646 std::string FileSpec::GetPath(bool denormalize) const { 647 llvm::SmallString<64> result; 648 GetPath(result, denormalize); 649 return std::string(result.begin(), result.end()); 650 } 651 652 const char *FileSpec::GetCString(bool denormalize) const { 653 return ConstString{GetPath(denormalize)}.AsCString(NULL); 654 } 655 656 void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path, 657 bool denormalize) const { 658 path.append(m_directory.GetStringRef().begin(), 659 m_directory.GetStringRef().end()); 660 // Since the path was normalized and all paths use '/' when stored in these 661 // objects, we don't need to look for the actual syntax specific path 662 // separator, we just look for and insert '/'. 663 if (m_directory && m_filename && m_directory.GetStringRef().back() != '/' && 664 m_filename.GetStringRef().back() != '/') 665 path.insert(path.end(), '/'); 666 path.append(m_filename.GetStringRef().begin(), 667 m_filename.GetStringRef().end()); 668 if (denormalize && !path.empty()) 669 Denormalize(path, m_syntax); 670 } 671 672 ConstString FileSpec::GetFileNameExtension() const { 673 if (m_filename) { 674 const char *filename = m_filename.GetCString(); 675 const char *dot_pos = strrchr(filename, '.'); 676 if (dot_pos && dot_pos[1] != '\0') 677 return ConstString(dot_pos + 1); 678 } 679 return ConstString(); 680 } 681 682 ConstString FileSpec::GetFileNameStrippingExtension() const { 683 const char *filename = m_filename.GetCString(); 684 if (filename == NULL) 685 return ConstString(); 686 687 const char *dot_pos = strrchr(filename, '.'); 688 if (dot_pos == NULL) 689 return m_filename; 690 691 return ConstString(filename, dot_pos - filename); 692 } 693 694 //------------------------------------------------------------------ 695 // Return the size in bytes that this object takes in memory. This returns the 696 // size in bytes of this object, not any shared string values it may refer to. 697 //------------------------------------------------------------------ 698 size_t FileSpec::MemorySize() const { 699 return m_filename.MemorySize() + m_directory.MemorySize(); 700 } 701 702 void FileSpec::EnumerateDirectory(llvm::StringRef dir_path, 703 bool find_directories, bool find_files, 704 bool find_other, 705 EnumerateDirectoryCallbackType callback, 706 void *callback_baton) { 707 namespace fs = llvm::sys::fs; 708 std::error_code EC; 709 fs::recursive_directory_iterator Iter(dir_path, EC); 710 fs::recursive_directory_iterator End; 711 for (; Iter != End && !EC; Iter.increment(EC)) { 712 const auto &Item = *Iter; 713 llvm::ErrorOr<fs::basic_file_status> Status = Item.status(); 714 if (!Status) 715 break; 716 if (!find_files && fs::is_regular_file(*Status)) 717 continue; 718 if (!find_directories && fs::is_directory(*Status)) 719 continue; 720 if (!find_other && fs::is_other(*Status)) 721 continue; 722 723 FileSpec Spec(Item.path(), false); 724 auto Result = callback(callback_baton, Status->type(), Spec); 725 if (Result == eEnumerateDirectoryResultQuit) 726 return; 727 if (Result == eEnumerateDirectoryResultNext) { 728 // Default behavior is to recurse. Opt out if the callback doesn't want 729 // this behavior. 730 Iter.no_push(); 731 } 732 } 733 } 734 735 FileSpec 736 FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const { 737 FileSpec ret = *this; 738 ret.AppendPathComponent(component); 739 return ret; 740 } 741 742 FileSpec FileSpec::CopyByRemovingLastPathComponent() const { 743 // CLEANUP: Use StringRef for string handling. 744 const bool resolve = false; 745 if (m_filename.IsEmpty() && m_directory.IsEmpty()) 746 return FileSpec("", resolve); 747 if (m_directory.IsEmpty()) 748 return FileSpec("", resolve); 749 if (m_filename.IsEmpty()) { 750 const char *dir_cstr = m_directory.GetCString(); 751 const char *last_slash_ptr = ::strrchr(dir_cstr, '/'); 752 753 // check for obvious cases before doing the full thing 754 if (!last_slash_ptr) 755 return FileSpec("", resolve); 756 if (last_slash_ptr == dir_cstr) 757 return FileSpec("/", resolve); 758 759 size_t last_slash_pos = last_slash_ptr - dir_cstr + 1; 760 ConstString new_path(dir_cstr, last_slash_pos); 761 return FileSpec(new_path.GetCString(), resolve); 762 } else 763 return FileSpec(m_directory.GetCString(), resolve); 764 } 765 766 ConstString FileSpec::GetLastPathComponent() const { 767 // CLEANUP: Use StringRef for string handling. 768 if (m_filename) 769 return m_filename; 770 if (m_directory) { 771 const char *dir_cstr = m_directory.GetCString(); 772 const char *last_slash_ptr = ::strrchr(dir_cstr, '/'); 773 if (last_slash_ptr == NULL) 774 return m_directory; 775 if (last_slash_ptr == dir_cstr) { 776 if (last_slash_ptr[1] == 0) 777 return ConstString(last_slash_ptr); 778 else 779 return ConstString(last_slash_ptr + 1); 780 } 781 if (last_slash_ptr[1] != 0) 782 return ConstString(last_slash_ptr + 1); 783 const char *penultimate_slash_ptr = last_slash_ptr; 784 while (*penultimate_slash_ptr) { 785 --penultimate_slash_ptr; 786 if (penultimate_slash_ptr == dir_cstr) 787 break; 788 if (*penultimate_slash_ptr == '/') 789 break; 790 } 791 ConstString result(penultimate_slash_ptr + 1, 792 last_slash_ptr - penultimate_slash_ptr); 793 return result; 794 } 795 return ConstString(); 796 } 797 798 static std::string 799 join_path_components(FileSpec::PathSyntax syntax, 800 const std::vector<llvm::StringRef> components) { 801 std::string result; 802 for (size_t i = 0; i < components.size(); ++i) { 803 if (components[i].empty()) 804 continue; 805 result += components[i]; 806 if (i != components.size() - 1 && 807 !IsPathSeparator(components[i].back(), syntax)) 808 result += GetPreferredPathSeparator(syntax); 809 } 810 811 return result; 812 } 813 814 void FileSpec::PrependPathComponent(llvm::StringRef component) { 815 if (component.empty()) 816 return; 817 818 const bool resolve = false; 819 if (m_filename.IsEmpty() && m_directory.IsEmpty()) { 820 SetFile(component, resolve); 821 return; 822 } 823 824 std::string result = 825 join_path_components(m_syntax, {component, m_directory.GetStringRef(), 826 m_filename.GetStringRef()}); 827 SetFile(result, resolve, m_syntax); 828 } 829 830 void FileSpec::PrependPathComponent(const FileSpec &new_path) { 831 return PrependPathComponent(new_path.GetPath(false)); 832 } 833 834 void FileSpec::AppendPathComponent(llvm::StringRef component) { 835 if (component.empty()) 836 return; 837 838 component = component.drop_while( 839 [this](char c) { return IsPathSeparator(c, m_syntax); }); 840 841 std::string result = 842 join_path_components(m_syntax, {m_directory.GetStringRef(), 843 m_filename.GetStringRef(), component}); 844 845 SetFile(result, false, m_syntax); 846 } 847 848 void FileSpec::AppendPathComponent(const FileSpec &new_path) { 849 return AppendPathComponent(new_path.GetPath(false)); 850 } 851 852 void FileSpec::RemoveLastPathComponent() { 853 // CLEANUP: Use StringRef for string handling. 854 855 const bool resolve = false; 856 if (m_filename.IsEmpty() && m_directory.IsEmpty()) { 857 SetFile("", resolve); 858 return; 859 } 860 if (m_directory.IsEmpty()) { 861 SetFile("", resolve); 862 return; 863 } 864 if (m_filename.IsEmpty()) { 865 const char *dir_cstr = m_directory.GetCString(); 866 const char *last_slash_ptr = ::strrchr(dir_cstr, '/'); 867 868 // check for obvious cases before doing the full thing 869 if (!last_slash_ptr) { 870 SetFile("", resolve); 871 return; 872 } 873 if (last_slash_ptr == dir_cstr) { 874 SetFile("/", resolve); 875 return; 876 } 877 size_t last_slash_pos = last_slash_ptr - dir_cstr + 1; 878 ConstString new_path(dir_cstr, last_slash_pos); 879 SetFile(new_path.GetCString(), resolve); 880 } else 881 SetFile(m_directory.GetCString(), resolve); 882 } 883 //------------------------------------------------------------------ 884 /// Returns true if the filespec represents an implementation source 885 /// file (files with a ".c", ".cpp", ".m", ".mm" (many more) 886 /// extension). 887 /// 888 /// @return 889 /// \b true if the filespec represents an implementation source 890 /// file, \b false otherwise. 891 //------------------------------------------------------------------ 892 bool FileSpec::IsSourceImplementationFile() const { 893 ConstString extension(GetFileNameExtension()); 894 if (!extension) 895 return false; 896 897 static RegularExpression g_source_file_regex(llvm::StringRef( 898 "^([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|[" 899 "cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO][" 900 "rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])" 901 "$")); 902 return g_source_file_regex.Execute(extension.GetStringRef()); 903 } 904 905 bool FileSpec::IsRelative() const { 906 const char *dir = m_directory.GetCString(); 907 llvm::StringRef directory(dir ? dir : ""); 908 909 if (directory.size() > 0) { 910 if (PathSyntaxIsPosix(m_syntax)) { 911 // If the path doesn't start with '/' or '~', return true 912 switch (directory[0]) { 913 case '/': 914 case '~': 915 return false; 916 default: 917 return true; 918 } 919 } else { 920 if (directory.size() >= 2 && directory[1] == ':') 921 return false; 922 if (directory[0] == '/') 923 return false; 924 return true; 925 } 926 } else if (m_filename) { 927 // No directory, just a basename, return true 928 return true; 929 } 930 return false; 931 } 932 933 bool FileSpec::IsAbsolute() const { return !FileSpec::IsRelative(); } 934 935 void llvm::format_provider<FileSpec>::format(const FileSpec &F, 936 raw_ostream &Stream, 937 StringRef Style) { 938 assert( 939 (Style.empty() || Style.equals_lower("F") || Style.equals_lower("D")) && 940 "Invalid FileSpec style!"); 941 942 StringRef dir = F.GetDirectory().GetStringRef(); 943 StringRef file = F.GetFilename().GetStringRef(); 944 945 if (dir.empty() && file.empty()) { 946 Stream << "(empty)"; 947 return; 948 } 949 950 if (Style.equals_lower("F")) { 951 Stream << (file.empty() ? "(empty)" : file); 952 return; 953 } 954 955 // Style is either D or empty, either way we need to print the directory. 956 if (!dir.empty()) { 957 // Directory is stored in normalized form, which might be different than 958 // preferred form. In order to handle this, we need to cut off the 959 // filename, then denormalize, then write the entire denorm'ed directory. 960 llvm::SmallString<64> denormalized_dir = dir; 961 Denormalize(denormalized_dir, F.GetPathSyntax()); 962 Stream << denormalized_dir; 963 Stream << GetPreferredPathSeparator(F.GetPathSyntax()); 964 } 965 966 if (Style.equals_lower("D")) { 967 // We only want to print the directory, so now just exit. 968 if (dir.empty()) 969 Stream << "(empty)"; 970 return; 971 } 972 973 if (!file.empty()) 974 Stream << file; 975 } 976