Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- Strings.h ------------------------------------------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 21 | // name, it returns name. |
| 22 | std::string demangleItanium(llvm::StringRef name); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 23 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 24 | std::vector<uint8_t> parseHex(llvm::StringRef s); |
| 25 | bool isValidCIdentifier(llvm::StringRef s); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 26 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 27 | // Write the contents of the a buffer to a file |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 28 | void saveBuffer(llvm::StringRef buffer, const llvm::Twine &path); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 29 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 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 { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 34 | public: |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 35 | // Create a StringPattern from Pattern to be matched exactly irregardless |
| 36 | // of globbing characters if ExactMatch is true. |
| 37 | SingleStringMatcher(llvm::StringRef Pattern); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 38 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 39 | // Match s against this pattern, exactly if ExactMatch is true. |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 40 | bool match(llvm::StringRef s) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 41 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 42 | // Returns true for pattern "*" which will match all inputs. |
| 43 | bool isTrivialMatchAll() const { |
| 44 | return !ExactMatch && GlobPatternMatcher.isTrivialMatchAll(); |
| 45 | } |
| 46 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 47 | private: |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 48 | // Whether to do an exact match irregardless of the presence of wildcard |
| 49 | // character. |
| 50 | bool ExactMatch; |
| 51 | |
| 52 | // GlobPattern object if not doing an exact match. |
| 53 | llvm::GlobPattern GlobPatternMatcher; |
| 54 | |
| 55 | // StringRef to match exactly if doing an exact match. |
| 56 | llvm::StringRef ExactPattern; |
| 57 | }; |
| 58 | |
| 59 | // This class represents multiple patterns to match against. A pattern can |
| 60 | // either be a double-quoted text that should be matched exactly after removing |
| 61 | // the quoted marks or a glob pattern. |
| 62 | class StringMatcher { |
| 63 | private: |
| 64 | // Patterns to match against. |
| 65 | std::vector<SingleStringMatcher> patterns; |
| 66 | |
| 67 | public: |
| 68 | StringMatcher() = default; |
| 69 | |
| 70 | // Matcher for a single pattern. |
| 71 | StringMatcher(llvm::StringRef Pattern) |
| 72 | : patterns({SingleStringMatcher(Pattern)}) {} |
| 73 | |
| 74 | // Add a new pattern to the existing ones to match against. |
| 75 | void addPattern(SingleStringMatcher Matcher) { patterns.push_back(Matcher); } |
| 76 | |
| 77 | bool empty() const { return patterns.empty(); } |
| 78 | |
| 79 | // Match s against the patterns. |
| 80 | bool match(llvm::StringRef s) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 81 | }; |
| 82 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 83 | } // namespace lld |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 84 | |
| 85 | #endif |