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