1 //===- Strings.cpp -------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lld/Common/Strings.h" 10 #include "lld/Common/ErrorHandler.h" 11 #include "lld/Common/LLVM.h" 12 #include "llvm/Demangle/Demangle.h" 13 #include "llvm/Support/GlobPattern.h" 14 #include <algorithm> 15 #include <mutex> 16 #include <vector> 17 18 using namespace llvm; 19 using namespace lld; 20 21 // Returns the demangled C++ symbol name for Name. 22 Optional<std::string> lld::demangleItanium(StringRef name) { 23 // itaniumDemangle can be used to demangle strings other than symbol 24 // names which do not necessarily start with "_Z". Name can be 25 // either a C or C++ symbol. Don't call itaniumDemangle if the name 26 // does not look like a C++ symbol name to avoid getting unexpected 27 // result for a C symbol that happens to match a mangled type name. 28 if (!name.startswith("_Z")) 29 return None; 30 31 char *buf = itaniumDemangle(name.str().c_str(), nullptr, nullptr, nullptr); 32 if (!buf) 33 return None; 34 std::string s(buf); 35 free(buf); 36 return s; 37 } 38 39 StringMatcher::StringMatcher(ArrayRef<StringRef> pat) { 40 for (StringRef s : pat) { 41 Expected<GlobPattern> pat = GlobPattern::create(s); 42 if (!pat) 43 error(toString(pat.takeError())); 44 else 45 patterns.push_back(*pat); 46 } 47 } 48 49 bool StringMatcher::match(StringRef s) const { 50 for (const GlobPattern &pat : patterns) 51 if (pat.match(s)) 52 return true; 53 return false; 54 } 55 56 // Converts a hex string (e.g. "deadbeef") to a vector. 57 std::vector<uint8_t> lld::parseHex(StringRef s) { 58 std::vector<uint8_t> hex; 59 while (!s.empty()) { 60 StringRef b = s.substr(0, 2); 61 s = s.substr(2); 62 uint8_t h; 63 if (!to_integer(b, h, 16)) { 64 error("not a hexadecimal value: " + b); 65 return {}; 66 } 67 hex.push_back(h); 68 } 69 return hex; 70 } 71 72 // Returns true if S is valid as a C language identifier. 73 bool lld::isValidCIdentifier(StringRef s) { 74 return !s.empty() && (isAlpha(s[0]) || s[0] == '_') && 75 std::all_of(s.begin() + 1, s.end(), 76 [](char c) { return c == '_' || isAlnum(c); }); 77 } 78 79 // Write the contents of the a buffer to a file 80 void lld::saveBuffer(StringRef buffer, const Twine &path) { 81 std::error_code ec; 82 raw_fd_ostream os(path.str(), ec, sys::fs::OpenFlags::OF_None); 83 if (ec) 84 error("cannot create " + path + ": " + ec.message()); 85 os << buffer; 86 } 87