LLVM 24.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"
20#include "llvm/Support/Error.h"
21#include <optional>
22#include <string>
23
24namespace llvm {
25
26/// This class implements a glob pattern matcher similar to the one found in
27/// bash, but with some key differences. Namely, that `*` matches all
28/// characters and does not exclude path separators.
29///
30/// * `?` matches a single character.
31/// * `*` matches zero or more characters.
32/// * `[<chars>]` matches one character in the bracket. Character ranges,
33/// e.g., `[a-z]`, and negative sets via `[^ab]` or `[!ab]` are also
34/// supported.
35/// * `{<glob>,...}` matches one of the globs in the list. Nested brace
36/// expansions are not supported. If \p MaxSubPatterns is empty then
37/// brace expansions are not supported and characters `{,}` are treated as
38/// literals.
39/// * `\` escapes the next character so it is treated as a literal.
40///
41/// Some known edge cases are:
42/// * The literal `]` is allowed as the first character in a character class,
43/// i.e., `[]]` is valid and matches the literal `]`.
44/// * The empty character class, i.e., `[]`, is invalid.
45/// * Empty or singleton brace expansions, e.g., `{}`, `{a}`, are invalid.
46/// * The literals `}` and `,` that are not inside a brace expansion are taken
47/// as literals, e.g., `,}` is valid but `{` is not.
48///
49/// Examples:
50/// * `*[/\\]foo.{c,cpp}` will match (unix or windows) paths to files named
51/// `foo.c` or `foo.cpp`.
52/// * `_Z{N,NK,}S[tabsoid]*` will match mangled C++ standard library functions.
54public:
55 /// \param Pat the pattern to match against
56 /// \param MaxSubPatterns if provided limit the number of allowed subpatterns
57 /// created from expanding braces otherwise disable
58 /// brace expansion
60 create(StringRef Pat, std::optional<size_t> MaxSubPatterns = {},
61 bool SlashAgnostic = false);
62 /// \returns \p true if \p S matches this glob pattern
63 LLVM_ABI bool match(StringRef S) const;
64
65 /// \returns the single string this pattern matches, if the pattern contains
66 /// no unescaped metacharacters; otherwise std::nullopt. Escapes are resolved,
67 /// so `a\*b` yields `a*b`. Characters that are only special in context (`]`,
68 /// `}`, `,`) do not make a pattern non-literal.
69 LLVM_ABI std::optional<std::string> asLiteral() const;
70
71 // Returns true for glob pattern "*". Can be used to avoid expensive
72 // preparation/acquisition of the input for match().
73 bool isTrivialMatchAll() const {
74 if (PrefixSize)
75 return false;
76 if (SuffixSize)
77 return false;
78 if (SubGlobs.size() != 1)
79 return false;
80 return SubGlobs[0].getPat() == "*";
81 }
82
83 // The following functions are just shortcuts for faster matching. They are
84 // conservative to simplify implementations.
85
86 // Returns plain prefix of the pattern.
87 StringRef prefix() const { return Pattern.take_front(PrefixSize); }
88 // Returns plain suffix of the pattern.
89 StringRef suffix() const { return Pattern.take_back(SuffixSize); }
90 // Returns the longest plain substring of the pattern between prefix and
91 // suffix.
93
94private:
96 size_t PrefixSize = 0;
97 size_t SuffixSize = 0;
98 bool SlashAgnostic = false;
99
100 struct SubGlobPattern {
101 /// \param Pat the pattern to match against
103 bool SlashAgnostic);
104 /// \returns \p true if \p S matches this glob pattern
105 LLVM_ABI bool match(StringRef S, bool SlashAgnostic) const;
106 StringRef getPat() const { return StringRef(Pat.data(), Pat.size()); }
107 /// \returns \p true if this sub-pattern matches exactly one string.
108 bool isLiteral() const { return Brackets.empty() && !HasWildcard; }
109
110 // Brackets with their end position and matched bytes.
111 struct Bracket {
114 };
116 // Set while parsing if an unescaped '*' or '?' is present.
117 bool HasWildcard = false;
119 };
121};
122}
123
124#endif // LLVM_SUPPORT_GLOBPATTERN_H
This file implements the BitVector class.
#define LLVM_ABI
Definition Compiler.h:215
This file defines the SmallVector class.
Tagged union holding either a T or a Error.
Definition Error.h:485
This class implements a glob pattern matcher similar to the one found in bash, but with some key diff...
Definition GlobPattern.h:53
LLVM_ABI StringRef longest_substr() const
static LLVM_ABI Expected< GlobPattern > create(StringRef Pat, std::optional< size_t > MaxSubPatterns={}, bool SlashAgnostic=false)
StringRef suffix() const
Definition GlobPattern.h:89
StringRef prefix() const
Definition GlobPattern.h:87
LLVM_ABI bool match(StringRef S) const
bool isTrivialMatchAll() const
Definition GlobPattern.h:73
LLVM_ABI std::optional< std::string > asLiteral() const
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:138
StringRef take_back(size_t N=1) const
Return a StringRef equal to 'this' but with only the last N elements remaining.
Definition StringRef.h:615
StringRef take_front(size_t N=1) const
Return a StringRef equal to 'this' but with only the first N elements remaining.
Definition StringRef.h:606
bool match(Val *V, const Pattern &P)
This is an optimization pass for GlobalISel generic memory operations.