1 //===- Strings.cpp -------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
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"))
31 char *buf = itaniumDemangle(name.str().c_str(), nullptr, nullptr, nullptr);
39 Optional<std::string> lld::demangleMSVC(StringRef name) {
41 if (name.consume_front("__imp_"))
42 prefix = "__declspec(dllimport) ";
44 // Demangle only C++ names.
45 if (!name.startswith("?"))
48 char *buf = microsoftDemangle(name.str().c_str(), nullptr, nullptr, nullptr);
56 StringMatcher::StringMatcher(ArrayRef<StringRef> pat) {
57 for (StringRef s : pat) {
58 Expected<GlobPattern> pat = GlobPattern::create(s);
60 error(toString(pat.takeError()));
62 patterns.push_back(*pat);
66 bool StringMatcher::match(StringRef s) const {
67 for (const GlobPattern &pat : patterns)
73 // Converts a hex string (e.g. "deadbeef") to a vector.
74 std::vector<uint8_t> lld::parseHex(StringRef s) {
75 std::vector<uint8_t> hex;
77 StringRef b = s.substr(0, 2);
80 if (!to_integer(b, h, 16)) {
81 error("not a hexadecimal value: " + b);
89 // Returns true if S is valid as a C language identifier.
90 bool lld::isValidCIdentifier(StringRef s) {
91 return !s.empty() && (isAlpha(s[0]) || s[0] == '_') &&
92 std::all_of(s.begin() + 1, s.end(),
93 [](char c) { return c == '_' || isAlnum(c); });
96 // Write the contents of the a buffer to a file
97 void lld::saveBuffer(StringRef buffer, const Twine &path) {
99 raw_fd_ostream os(path.str(), ec, sys::fs::OpenFlags::F_None);
101 error("cannot create " + path + ": " + ec.message());