]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/Strings.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r308421, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / Strings.h
1 //===- Strings.h ------------------------------------------------*- C++ -*-===//
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 #ifndef LLD_ELF_STRINGS_H
11 #define LLD_ELF_STRINGS_H
12
13 #include "lld/Core/LLVM.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/BitVector.h"
16 #include "llvm/ADT/Optional.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/GlobPattern.h"
19 #include <vector>
20
21 namespace lld {
22 namespace elf {
23
24 std::vector<uint8_t> parseHex(StringRef S);
25 bool isValidCIdentifier(StringRef S);
26
27 // This is a lazy version of StringRef. String size is computed lazily
28 // when it is needed. It is more efficient than StringRef to instantiate
29 // if you have a string whose size is unknown.
30 //
31 // ELF string tables contain a lot of null-terminated strings.
32 // Most of them are not necessary for the linker because they are names
33 // of local symbols and the linker doesn't use local symbol names for
34 // name resolution. So, we use this class to represents strings read
35 // from string tables.
36 class StringRefZ {
37 public:
38   StringRefZ() : Start(nullptr), Size(0) {}
39   StringRefZ(const char *S, size_t Size) : Start(S), Size(Size) {}
40
41   /*implicit*/ StringRefZ(const char *S) : Start(S), Size(-1) {}
42
43   /*implicit*/ StringRefZ(llvm::StringRef S)
44       : Start(S.data()), Size(S.size()) {}
45
46   operator llvm::StringRef() const {
47     if (Size == (size_t)-1)
48       Size = strlen(Start);
49     return {Start, Size};
50   }
51
52 private:
53   const char *Start;
54   mutable size_t Size;
55 };
56
57 // This class represents multiple glob patterns.
58 class StringMatcher {
59 public:
60   StringMatcher() = default;
61   explicit StringMatcher(ArrayRef<StringRef> Pat);
62
63   bool match(StringRef S) const;
64
65 private:
66   std::vector<llvm::GlobPattern> Patterns;
67 };
68
69 // Returns a demangled C++ symbol name. If Name is not a mangled
70 // name, it returns Optional::None.
71 llvm::Optional<std::string> demangle(StringRef Name);
72
73 inline ArrayRef<uint8_t> toArrayRef(StringRef S) {
74   return {(const uint8_t *)S.data(), S.size()};
75 }
76 } // namespace elf
77 } // namespace lld
78
79 #endif