xref: /freebsd/contrib/llvm-project/lld/Common/Strings.cpp (revision 2f513db72b034fd5ef7f080b11be5c711c15186a)
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 Optional<std::string> lld::demangleMSVC(StringRef name) {
40   std::string prefix;
41   if (name.consume_front("__imp_"))
42     prefix = "__declspec(dllimport) ";
43 
44   // Demangle only C++ names.
45   if (!name.startswith("?"))
46     return None;
47 
48   char *buf = microsoftDemangle(name.str().c_str(), nullptr, nullptr, nullptr);
49   if (!buf)
50     return None;
51   std::string s(buf);
52   free(buf);
53   return prefix + s;
54 }
55 
56 StringMatcher::StringMatcher(ArrayRef<StringRef> pat) {
57   for (StringRef s : pat) {
58     Expected<GlobPattern> pat = GlobPattern::create(s);
59     if (!pat)
60       error(toString(pat.takeError()));
61     else
62       patterns.push_back(*pat);
63   }
64 }
65 
66 bool StringMatcher::match(StringRef s) const {
67   for (const GlobPattern &pat : patterns)
68     if (pat.match(s))
69       return true;
70   return false;
71 }
72 
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;
76   while (!s.empty()) {
77     StringRef b = s.substr(0, 2);
78     s = s.substr(2);
79     uint8_t h;
80     if (!to_integer(b, h, 16)) {
81       error("not a hexadecimal value: " + b);
82       return {};
83     }
84     hex.push_back(h);
85   }
86   return hex;
87 }
88 
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); });
94 }
95 
96 // Write the contents of the a buffer to a file
97 void lld::saveBuffer(StringRef buffer, const Twine &path) {
98   std::error_code ec;
99   raw_fd_ostream os(path.str(), ec, sys::fs::OpenFlags::F_None);
100   if (ec)
101     error("cannot create " + path + ": " + ec.message());
102   os << buffer;
103 }
104