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