1 //===- Strings.h ------------------------------------------------*- C++ -*-===//
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 #ifndef LLD_STRINGS_H
10 #define LLD_STRINGS_H
11 
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/GlobPattern.h"
16 #include <string>
17 #include <vector>
18 
19 namespace lld {
20 // Returns a demangled C++ symbol name. If Name is not a mangled
21 // name, it returns name.
22 std::string demangleItanium(llvm::StringRef name);
23 
24 std::vector<uint8_t> parseHex(llvm::StringRef s);
25 bool isValidCIdentifier(llvm::StringRef s);
26 
27 // Write the contents of the a buffer to a file
28 void saveBuffer(llvm::StringRef buffer, const llvm::Twine &path);
29 
30 // A single pattern to match against. A pattern can either be double-quoted
31 // text that should be matched exactly after removing the quoting marks or a
32 // glob pattern in the sense of GlobPattern.
33 class SingleStringMatcher {
34 public:
35   // Create a StringPattern from Pattern to be matched exactly irregardless
36   // of globbing characters if ExactMatch is true.
37   SingleStringMatcher(llvm::StringRef Pattern);
38 
39   // Match s against this pattern, exactly if ExactMatch is true.
40   bool match(llvm::StringRef s) const;
41 
42 private:
43   // Whether to do an exact match irregardless of the presence of wildcard
44   // character.
45   bool ExactMatch;
46 
47   // GlobPattern object if not doing an exact match.
48   llvm::GlobPattern GlobPatternMatcher;
49 
50   // StringRef to match exactly if doing an exact match.
51   llvm::StringRef ExactPattern;
52 };
53 
54 // This class represents multiple patterns to match against. A pattern can
55 // either be a double-quoted text that should be matched exactly after removing
56 // the quoted marks or a glob pattern.
57 class StringMatcher {
58 private:
59   // Patterns to match against.
60   std::vector<SingleStringMatcher> patterns;
61 
62 public:
63   StringMatcher() = default;
64 
65   // Matcher for a single pattern.
66   StringMatcher(llvm::StringRef Pattern)
67       : patterns({SingleStringMatcher(Pattern)}) {}
68 
69   // Add a new pattern to the existing ones to match against.
70   void addPattern(SingleStringMatcher Matcher) { patterns.push_back(Matcher); }
71 
72   bool empty() { return patterns.empty(); }
73 
74   // Match s against the patterns.
75   bool match(llvm::StringRef s) const;
76 };
77 
78 } // namespace lld
79 
80 #endif
81