]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/Strings.cpp
Merge ^/head r313644 through r313895.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / Strings.cpp
1 //===- Strings.cpp -------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "Strings.h"
11 #include "Config.h"
12 #include "Error.h"
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/Demangle/Demangle.h"
17 #include <algorithm>
18 #include <cstring>
19
20 using namespace llvm;
21 using namespace lld;
22 using namespace lld::elf;
23
24 StringMatcher::StringMatcher(ArrayRef<StringRef> Pat) {
25   for (StringRef S : Pat) {
26     Expected<GlobPattern> Pat = GlobPattern::create(S);
27     if (!Pat)
28       error(toString(Pat.takeError()));
29     else
30       Patterns.push_back(*Pat);
31   }
32 }
33
34 bool StringMatcher::match(StringRef S) const {
35   for (const GlobPattern &Pat : Patterns)
36     if (Pat.match(S))
37       return true;
38   return false;
39 }
40
41 // If an input string is in the form of "foo.N" where N is a number,
42 // return N. Otherwise, returns 65536, which is one greater than the
43 // lowest priority.
44 int elf::getPriority(StringRef S) {
45   size_t Pos = S.rfind('.');
46   if (Pos == StringRef::npos)
47     return 65536;
48   int V;
49   if (S.substr(Pos + 1).getAsInteger(10, V))
50     return 65536;
51   return V;
52 }
53
54 bool elf::hasWildcard(StringRef S) {
55   return S.find_first_of("?*[") != StringRef::npos;
56 }
57
58 StringRef elf::unquote(StringRef S) {
59   if (!S.startswith("\""))
60     return S;
61   return S.substr(1, S.size() - 2);
62 }
63
64 // Converts a hex string (e.g. "deadbeef") to a vector.
65 std::vector<uint8_t> elf::parseHex(StringRef S) {
66   std::vector<uint8_t> Hex;
67   while (!S.empty()) {
68     StringRef B = S.substr(0, 2);
69     S = S.substr(2);
70     uint8_t H;
71     if (B.getAsInteger(16, H)) {
72       error("not a hexadecimal value: " + B);
73       return {};
74     }
75     Hex.push_back(H);
76   }
77   return Hex;
78 }
79
80 static bool isAlpha(char C) {
81   return ('a' <= C && C <= 'z') || ('A' <= C && C <= 'Z') || C == '_';
82 }
83
84 static bool isAlnum(char C) { return isAlpha(C) || ('0' <= C && C <= '9'); }
85
86 // Returns true if S is valid as a C language identifier.
87 bool elf::isValidCIdentifier(StringRef S) {
88   return !S.empty() && isAlpha(S[0]) &&
89          std::all_of(S.begin() + 1, S.end(), isAlnum);
90 }
91
92 // Returns the demangled C++ symbol name for Name.
93 Optional<std::string> elf::demangle(StringRef Name) {
94   // __cxa_demangle can be used to demangle strings other than symbol
95   // names which do not necessarily start with "_Z". Name can be
96   // either a C or C++ symbol. Don't call __cxa_demangle if the name
97   // does not look like a C++ symbol name to avoid getting unexpected
98   // result for a C symbol that happens to match a mangled type name.
99   if (!Name.startswith("_Z"))
100     return None;
101
102   char *Buf = itaniumDemangle(Name.str().c_str(), nullptr, nullptr, nullptr);
103   if (!Buf)
104     return None;
105   std::string S(Buf);
106   free(Buf);
107   return S;
108 }