1 //===-- Path.cpp - Implement OS Path Concept --------------------*- 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 // This header file implements the operating system Path concept. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Support/Path.h" 15 #include "llvm/Config/config.h" 16 #include "llvm/Support/Endian.h" 17 #include "llvm/Support/FileSystem.h" 18 #include "llvm/Support/PathV1.h" 19 #include <cassert> 20 #include <cstring> 21 #include <ostream> 22 using namespace llvm; 23 using namespace sys; 24 namespace { 25 using support::ulittle32_t; 26 } 27 28 //===----------------------------------------------------------------------===// 29 //=== WARNING: Implementation here must contain only TRULY operating system 30 //=== independent code. 31 //===----------------------------------------------------------------------===// 32 33 bool Path::operator==(const Path &that) const { 34 return path == that.path; 35 } 36 37 bool Path::operator<(const Path& that) const { 38 return path < that.path; 39 } 40 41 bool 42 Path::isArchive() const { 43 fs::file_magic type; 44 if (fs::identify_magic(str(), type)) 45 return false; 46 return type == fs::file_magic::archive; 47 } 48 49 bool 50 Path::isDynamicLibrary() const { 51 fs::file_magic type; 52 if (fs::identify_magic(str(), type)) 53 return false; 54 switch (type) { 55 default: return false; 56 case fs::file_magic::macho_fixed_virtual_memory_shared_lib: 57 case fs::file_magic::macho_dynamically_linked_shared_lib: 58 case fs::file_magic::macho_dynamically_linked_shared_lib_stub: 59 case fs::file_magic::elf_shared_object: 60 case fs::file_magic::pecoff_executable: return true; 61 } 62 } 63 64 bool 65 Path::isObjectFile() const { 66 fs::file_magic type; 67 if (fs::identify_magic(str(), type) || type == fs::file_magic::unknown) 68 return false; 69 return true; 70 } 71 72 void 73 Path::appendSuffix(StringRef suffix) { 74 if (!suffix.empty()) { 75 path.append("."); 76 path.append(suffix); 77 } 78 } 79 80 bool 81 Path::isBitcodeFile() const { 82 fs::file_magic type; 83 if (fs::identify_magic(str(), type)) 84 return false; 85 return type == fs::file_magic::bitcode; 86 } 87 88 bool Path::hasMagicNumber(StringRef Magic) const { 89 std::string actualMagic; 90 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size()))) 91 return Magic == actualMagic; 92 return false; 93 } 94 95 // Include the truly platform-specific parts of this class. 96 #if defined(LLVM_ON_UNIX) 97 #include "Unix/Path.inc" 98 #endif 99 #if defined(LLVM_ON_WIN32) 100 #include "Windows/Path.inc" 101 #endif 102