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/FileSystem.h"
14 #include "llvm/Support/GlobPattern.h"
22 // Returns the demangled C++ symbol name for name.
23 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 demangle 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 std::string(name);
32 return demangle(std::string(name));
35 SingleStringMatcher::SingleStringMatcher(StringRef Pattern) {
36 if (Pattern.size() > 2 && Pattern.startswith("\"") &&
37 Pattern.endswith("\"")) {
39 ExactPattern = Pattern.substr(1, Pattern.size() - 2);
41 Expected<GlobPattern> Glob = GlobPattern::create(Pattern);
43 error(toString(Glob.takeError()));
47 GlobPatternMatcher = *Glob;
51 bool SingleStringMatcher::match(StringRef s) const {
52 return ExactMatch ? (ExactPattern == s) : GlobPatternMatcher.match(s);
55 bool StringMatcher::match(StringRef s) const {
56 for (const SingleStringMatcher &pat : patterns)
62 // Converts a hex string (e.g. "deadbeef") to a vector.
63 std::vector<uint8_t> lld::parseHex(StringRef s) {
64 std::vector<uint8_t> hex;
66 StringRef b = s.substr(0, 2);
69 if (!to_integer(b, h, 16)) {
70 error("not a hexadecimal value: " + b);
78 // Returns true if S is valid as a C language identifier.
79 bool lld::isValidCIdentifier(StringRef s) {
80 return !s.empty() && (isAlpha(s[0]) || s[0] == '_') &&
81 std::all_of(s.begin() + 1, s.end(),
82 [](char c) { return c == '_' || isAlnum(c); });
85 // Write the contents of the a buffer to a file
86 void lld::saveBuffer(StringRef buffer, const Twine &path) {
88 raw_fd_ostream os(path.str(), ec, sys::fs::OpenFlags::OF_None);
90 error("cannot create " + path + ": " + ec.message());