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