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