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