diff options
author | Michael J. Spencer <bigcheesegs@gmail.com> | 2011-01-15 20:39:36 +0000 |
---|---|---|
committer | Michael J. Spencer <bigcheesegs@gmail.com> | 2011-01-15 20:39:36 +0000 |
commit | 28f0ed5c9de4a68f34c0219d4ab83652c4647150 (patch) | |
tree | 999622dc7275347a61e4a674455c7beaf7ded075 /lib/Support | |
parent | b6516aeef12a05aa47515f76e18fc426d85babbd (diff) | |
download | external_llvm-28f0ed5c9de4a68f34c0219d4ab83652c4647150.zip external_llvm-28f0ed5c9de4a68f34c0219d4ab83652c4647150.tar.gz external_llvm-28f0ed5c9de4a68f34c0219d4ab83652c4647150.tar.bz2 |
Support/PathV2: Add identify_magic.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123548 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support')
-rw-r--r-- | lib/Support/Path.cpp | 57 | ||||
-rw-r--r-- | lib/Support/PathV2.cpp | 10 |
2 files changed, 33 insertions, 34 deletions
diff --git a/lib/Support/Path.cpp b/lib/Support/Path.cpp index 4689208..e5e875b 100644 --- a/lib/Support/Path.cpp +++ b/lib/Support/Path.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/Path.h" +#include "llvm/Support/FileSystem.h" #include "llvm/Config/config.h" #include "llvm/Support/FileSystem.h" #include <cassert> @@ -141,42 +142,33 @@ sys::IdentifyFileType(const char *magic, unsigned length) { bool Path::isArchive() const { - std::string Magic; - if (getMagicNumber(Magic, 8)) - if (IdentifyFileType(Magic.c_str(), Magic.length()) == Archive_FileType) - return true; - return false; + LLVMFileType type; + if (fs::identify_magic(str(), type)) + return false; + return type == Archive_FileType; } bool Path::isDynamicLibrary() const { - std::string Magic; - if (getMagicNumber(Magic, 64)) - switch (IdentifyFileType(Magic.c_str(), - static_cast<unsigned>(Magic.length()))) { - default: return false; - case Mach_O_FixedVirtualMemorySharedLib_FileType: - case Mach_O_DynamicallyLinkedSharedLib_FileType: - case Mach_O_DynamicallyLinkedSharedLibStub_FileType: - case ELF_SharedObject_FileType: - case COFF_FileType: return true; - } - - return false; + LLVMFileType type; + if (fs::identify_magic(str(), type)) + return false; + switch (type) { + default: return false; + case Mach_O_FixedVirtualMemorySharedLib_FileType: + case Mach_O_DynamicallyLinkedSharedLib_FileType: + case Mach_O_DynamicallyLinkedSharedLibStub_FileType: + case ELF_SharedObject_FileType: + case COFF_FileType: return true; + } } bool Path::isObjectFile() const { - std::string Magic; - if (getMagicNumber(Magic, 64)) - if (IdentifyFileType(Magic.c_str(), - static_cast<unsigned>(Magic.length())) - != Unknown_FileType) { - // Everything in LLVMFileType is currently an object file. - return true; - } - - return false; + LLVMFileType type; + if (fs::identify_magic(str(), type) || type == Unknown_FileType) + return false; + return true; } Path @@ -210,13 +202,10 @@ Path::appendSuffix(StringRef suffix) { bool Path::isBitcodeFile() const { - std::string actualMagic; - if (!getMagicNumber(actualMagic, 4)) + LLVMFileType type; + if (fs::identify_magic(str(), type)) return false; - LLVMFileType FT = - IdentifyFileType(actualMagic.c_str(), - static_cast<unsigned>(actualMagic.length())); - return FT == Bitcode_FileType; + return type == Bitcode_FileType; } bool Path::hasMagicNumber(StringRef Magic) const { diff --git a/lib/Support/PathV2.cpp b/lib/Support/PathV2.cpp index 2e818a5..3463876 100644 --- a/lib/Support/PathV2.cpp +++ b/lib/Support/PathV2.cpp @@ -703,6 +703,16 @@ error_code has_magic(const Twine &path, const Twine &magic, bool &result) { return success; } +error_code identify_magic(const Twine &path, LLVMFileType &result) { + SmallString<32> Magic; + error_code ec = get_magic(path, Magic.capacity(), Magic); + if (ec && ec != errc::value_too_large) + return ec; + + result = IdentifyFileType(Magic.data(), Magic.size()); + return success; +} + namespace { error_code remove_all_r(StringRef path, file_type ft, uint32_t &count) { if (ft == file_type::directory_file) { |