xref: /freebsd/contrib/llvm-project/lld/Common/Strings.cpp (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
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   // demangleItanium() can be called for all symbols. Only demangle C++ symbols,
25   // to avoid getting unexpected result for a C symbol that happens to match a
26   // mangled type name such as "Pi" (which would demangle to "int*").
27   if (!name.startswith("_Z") && !name.startswith("__Z") &&
28       !name.startswith("___Z") && !name.startswith("____Z"))
29     return std::string(name);
30 
31   return demangle(std::string(name));
32 }
33 
34 SingleStringMatcher::SingleStringMatcher(StringRef Pattern) {
35   if (Pattern.size() > 2 && Pattern.startswith("\"") &&
36       Pattern.endswith("\"")) {
37     ExactMatch = true;
38     ExactPattern = Pattern.substr(1, Pattern.size() - 2);
39   } else {
40     Expected<GlobPattern> Glob = GlobPattern::create(Pattern);
41     if (!Glob) {
42       error(toString(Glob.takeError()));
43       return;
44     }
45     ExactMatch = false;
46     GlobPatternMatcher = *Glob;
47   }
48 }
49 
50 bool SingleStringMatcher::match(StringRef s) const {
51   return ExactMatch ? (ExactPattern == s) : GlobPatternMatcher.match(s);
52 }
53 
54 bool StringMatcher::match(StringRef s) const {
55   for (const SingleStringMatcher &pat : patterns)
56     if (pat.match(s))
57       return true;
58   return false;
59 }
60 
61 // Converts a hex string (e.g. "deadbeef") to a vector.
62 std::vector<uint8_t> lld::parseHex(StringRef s) {
63   std::vector<uint8_t> hex;
64   while (!s.empty()) {
65     StringRef b = s.substr(0, 2);
66     s = s.substr(2);
67     uint8_t h;
68     if (!to_integer(b, h, 16)) {
69       error("not a hexadecimal value: " + b);
70       return {};
71     }
72     hex.push_back(h);
73   }
74   return hex;
75 }
76 
77 // Returns true if S is valid as a C language identifier.
78 bool lld::isValidCIdentifier(StringRef s) {
79   return !s.empty() && !isDigit(s[0]) &&
80          llvm::all_of(s, [](char c) { return isAlnum(c) || c == '_'; });
81 }
82 
83 // Write the contents of the a buffer to a file
84 void lld::saveBuffer(StringRef buffer, const Twine &path) {
85   std::error_code ec;
86   raw_fd_ostream os(path.str(), ec, sys::fs::OpenFlags::OF_None);
87   if (ec)
88     error("cannot create " + path + ": " + ec.message());
89   os << buffer;
90 }
91