LLVM 20.0.0git
GlobPattern.h
Go to the documentation of this file.
1//===-- GlobPattern.h - glob pattern matcher implementation -*- 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// This file implements a glob pattern matcher.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_SUPPORT_GLOBPATTERN_H
14#define LLVM_SUPPORT_GLOBPATTERN_H
15
16#include "llvm/ADT/BitVector.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/Support/Error.h"
20#include <optional>
21
22namespace llvm {
23
24/// This class implements a glob pattern matcher similar to the one found in
25/// bash, but with some key differences. Namely, that \p "*" matches all
26/// characters and does not exclude path separators.
27///
28/// * \p "?" matches a single character.
29/// * \p "*" matches zero or more characters.
30/// * \p "[<chars>]" matches one character in the bracket. Character ranges,
31/// e.g., \p "[a-z]", and negative sets via \p "[^ab]" or \p "[!ab]" are also
32/// supported.
33/// * \p "{<glob>,...}" matches one of the globs in the list. Nested brace
34/// expansions are not supported. If \p MaxSubPatterns is empty then
35/// brace expansions are not supported and characters \p "{,}" are treated as
36/// literals.
37/// * \p "\\" (a single backslash) escapes the next character so it is treated
38/// as a literal.
39///
40/// Some known edge cases are:
41/// * \p "]" is allowed as the first character in a character class, i.e.,
42/// \p "[]]" is valid and matches the literal \p "]".
43/// * The empty character class, i.e., \p "[]", is invalid.
44/// * Empty or singleton brace expansions, e.g., \p "{}", \p "{a}", are invalid.
45/// * \p "}" and \p "," that are not inside a brace expansion are taken as
46/// literals, e.g., \p ",}" is valid but \p "{" is not.
47///
48/// For example, \p "*[/\\\\]foo.{c,cpp}" (with two backslashes) will match
49/// (unix or windows) paths to all files named \p "foo.c" or \p "foo.cpp".
51public:
52 /// \param Pat the pattern to match against
53 /// \param MaxSubPatterns if provided limit the number of allowed subpatterns
54 /// created from expanding braces otherwise disable
55 /// brace expansion
57 create(StringRef Pat, std::optional<size_t> MaxSubPatterns = {});
58 /// \returns \p true if \p S matches this glob pattern
59 bool match(StringRef S) const;
60
61 // Returns true for glob pattern "*". Can be used to avoid expensive
62 // preparation/acquisition of the input for match().
63 bool isTrivialMatchAll() const {
64 if (!Prefix.empty())
65 return false;
66 if (SubGlobs.size() != 1)
67 return false;
68 return SubGlobs[0].getPat() == "*";
69 }
70
71private:
72 StringRef Prefix;
73
74 struct SubGlobPattern {
75 /// \param Pat the pattern to match against
76 static Expected<SubGlobPattern> create(StringRef Pat);
77 /// \returns \p true if \p S matches this glob pattern
78 bool match(StringRef S) const;
79 StringRef getPat() const { return StringRef(Pat.data(), Pat.size()); }
80
81 // Brackets with their end position and matched bytes.
82 struct Bracket {
83 size_t NextOffset;
85 };
88 };
90};
91}
92
93#endif // LLVM_SUPPORT_GLOBPATTERN_H
This file implements the BitVector class.
This file defines the SmallVector class.
Tagged union holding either a T or a Error.
Definition: Error.h:481
This class implements a glob pattern matcher similar to the one found in bash, but with some key diff...
Definition: GlobPattern.h:50
bool match(StringRef S) const
bool isTrivialMatchAll() const
Definition: GlobPattern.h:63
static Expected< GlobPattern > create(StringRef Pat, std::optional< size_t > MaxSubPatterns={})
size_t size() const
Definition: SmallVector.h:78
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:150
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:144
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18