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