]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/Strings.cpp
MFC r343601:
[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 "lld/Common/ErrorHandler.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 // Converts a hex string (e.g. "deadbeef") to a vector.
42 std::vector<uint8_t> elf::parseHex(StringRef S) {
43   std::vector<uint8_t> Hex;
44   while (!S.empty()) {
45     StringRef B = S.substr(0, 2);
46     S = S.substr(2);
47     uint8_t H;
48     if (!to_integer(B, H, 16)) {
49       error("not a hexadecimal value: " + B);
50       return {};
51     }
52     Hex.push_back(H);
53   }
54   return Hex;
55 }
56
57 // Returns true if S is valid as a C language identifier.
58 bool elf::isValidCIdentifier(StringRef S) {
59   return !S.empty() && (isAlpha(S[0]) || S[0] == '_') &&
60          std::all_of(S.begin() + 1, S.end(),
61                      [](char C) { return C == '_' || isAlnum(C); });
62 }