]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lld/Common/Strings.cpp
MFV r366990: less v563.
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lld / Common / Strings.cpp
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/FileSystem.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 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);
31
32   return demangle(std::string(name));
33 }
34
35 SingleStringMatcher::SingleStringMatcher(StringRef Pattern) {
36   if (Pattern.size() > 2 && Pattern.startswith("\"") &&
37       Pattern.endswith("\"")) {
38     ExactMatch = true;
39     ExactPattern = Pattern.substr(1, Pattern.size() - 2);
40   } else {
41     Expected<GlobPattern> Glob = GlobPattern::create(Pattern);
42     if (!Glob) {
43       error(toString(Glob.takeError()));
44       return;
45     }
46     ExactMatch = false;
47     GlobPatternMatcher = *Glob;
48   }
49 }
50
51 bool SingleStringMatcher::match(StringRef s) const {
52   return ExactMatch ? (ExactPattern == s) : GlobPatternMatcher.match(s);
53 }
54
55 bool StringMatcher::match(StringRef s) const {
56   for (const SingleStringMatcher &pat : patterns)
57     if (pat.match(s))
58       return true;
59   return false;
60 }
61
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;
65   while (!s.empty()) {
66     StringRef b = s.substr(0, 2);
67     s = s.substr(2);
68     uint8_t h;
69     if (!to_integer(b, h, 16)) {
70       error("not a hexadecimal value: " + b);
71       return {};
72     }
73     hex.push_back(h);
74   }
75   return hex;
76 }
77
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); });
83 }
84
85 // Write the contents of the a buffer to a file
86 void lld::saveBuffer(StringRef buffer, const Twine &path) {
87   std::error_code ec;
88   raw_fd_ostream os(path.str(), ec, sys::fs::OpenFlags::OF_None);
89   if (ec)
90     error("cannot create " + path + ": " + ec.message());
91   os << buffer;
92 }