1 //===- Strings.cpp -------------------------------------------------------===// 2 // 3 // The LLVM Linker 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 "lld/Common/Strings.h" 11 #include "lld/Common/ErrorHandler.h" 12 #include "lld/Common/LLVM.h" 13 #include "llvm/Demangle/Demangle.h" 14 #include "llvm/Support/GlobPattern.h" 15 #include <algorithm> 16 #include <mutex> 17 #include <vector> 18 19 using namespace llvm; 20 using namespace lld; 21 22 // Returns the demangled C++ symbol name for Name. 23 Optional<std::string> lld::demangleItanium(StringRef Name) { 24 // itaniumDemangle can be used to demangle strings other than symbol 25 // names which do not necessarily start with "_Z". Name can be 26 // either a C or C++ symbol. Don't call itaniumDemangle if the name 27 // does not look like a C++ symbol name to avoid getting unexpected 28 // result for a C symbol that happens to match a mangled type name. 29 if (!Name.startswith("_Z")) 30 return None; 31 32 char *Buf = itaniumDemangle(Name.str().c_str(), nullptr, nullptr, nullptr); 33 if (!Buf) 34 return None; 35 std::string S(Buf); 36 free(Buf); 37 return S; 38 } 39 40 Optional<std::string> lld::demangleMSVC(StringRef Name) { 41 std::string Prefix; 42 if (Name.consume_front("__imp_")) 43 Prefix = "__declspec(dllimport) "; 44 45 // Demangle only C++ names. 46 if (!Name.startswith("?")) 47 return None; 48 49 char *Buf = microsoftDemangle(Name.str().c_str(), nullptr, nullptr, nullptr); 50 if (!Buf) 51 return None; 52 std::string S(Buf); 53 free(Buf); 54 return Prefix + S; 55 } 56 57 StringMatcher::StringMatcher(ArrayRef<StringRef> Pat) { 58 for (StringRef S : Pat) { 59 Expected<GlobPattern> Pat = GlobPattern::create(S); 60 if (!Pat) 61 error(toString(Pat.takeError())); 62 else 63 Patterns.push_back(*Pat); 64 } 65 } 66 67 bool StringMatcher::match(StringRef S) const { 68 for (const GlobPattern &Pat : Patterns) 69 if (Pat.match(S)) 70 return true; 71 return false; 72 } 73 74 // Converts a hex string (e.g. "deadbeef") to a vector. 75 std::vector<uint8_t> lld::parseHex(StringRef S) { 76 std::vector<uint8_t> Hex; 77 while (!S.empty()) { 78 StringRef B = S.substr(0, 2); 79 S = S.substr(2); 80 uint8_t H; 81 if (!to_integer(B, H, 16)) { 82 error("not a hexadecimal value: " + B); 83 return {}; 84 } 85 Hex.push_back(H); 86 } 87 return Hex; 88 } 89 90 // Returns true if S is valid as a C language identifier. 91 bool lld::isValidCIdentifier(StringRef S) { 92 return !S.empty() && (isAlpha(S[0]) || S[0] == '_') && 93 std::all_of(S.begin() + 1, S.end(), 94 [](char C) { return C == '_' || isAlnum(C); }); 95 } 96 97 // Write the contents of the a buffer to a file 98 void lld::saveBuffer(StringRef Buffer, const Twine &Path) { 99 std::error_code EC; 100 raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None); 101 if (EC) 102 error("cannot create " + Path + ": " + EC.message()); 103 OS << Buffer; 104 } 105