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" 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/ADT/StringRef.h" 19 #include "llvm/Support/Error.h" 20 #include <optional> 21 22 namespace 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 `*` matches all 26 /// characters and does not exclude path separators. 27 /// 28 /// * `?` matches a single character. 29 /// * `*` matches zero or more characters. 30 /// * `[<chars>]` matches one character in the bracket. Character ranges, 31 /// e.g., `[a-z]`, and negative sets via `[^ab]` or `[!ab]` are also 32 /// supported. 33 /// * `{<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 `{,}` are treated as 36 /// literals. 37 /// * `\` escapes the next character so it is treated as a literal. 38 /// 39 /// Some known edge cases are: 40 /// * The literal `]` is allowed as the first character in a character class, 41 /// i.e., `[]]` is valid and matches the literal `]`. 42 /// * The empty character class, i.e., `[]`, is invalid. 43 /// * Empty or singleton brace expansions, e.g., `{}`, `{a}`, are invalid. 44 /// * The literals `}` and `,` that are not inside a brace expansion are taken 45 /// as literals, e.g., `,}` is valid but `{` is not. 46 /// 47 /// Examples: 48 /// * `*[/\\]foo.{c,cpp}` will match (unix or windows) paths to files named 49 /// `foo.c` or `foo.cpp`. 50 /// * `_Z{N,NK,}S[tabsoid]*` will match mangled C++ standard library functions. 51 class GlobPattern { 52 public: 53 /// \param Pat the pattern to match against 54 /// \param MaxSubPatterns if provided limit the number of allowed subpatterns 55 /// created from expanding braces otherwise disable 56 /// brace expansion 57 static Expected<GlobPattern> 58 create(StringRef Pat, std::optional<size_t> MaxSubPatterns = {}); 59 /// \returns \p true if \p S matches this glob pattern 60 bool match(StringRef S) const; 61 62 // Returns true for glob pattern "*". Can be used to avoid expensive 63 // preparation/acquisition of the input for match(). 64 bool isTrivialMatchAll() const { 65 if (!Prefix.empty()) 66 return false; 67 if (SubGlobs.size() != 1) 68 return false; 69 return SubGlobs[0].getPat() == "*"; 70 } 71 72 private: 73 StringRef Prefix; 74 75 struct SubGlobPattern { 76 /// \param Pat the pattern to match against 77 static Expected<SubGlobPattern> create(StringRef Pat); 78 /// \returns \p true if \p S matches this glob pattern 79 bool match(StringRef S) const; 80 StringRef getPat() const { return StringRef(Pat.data(), Pat.size()); } 81 82 // Brackets with their end position and matched bytes. 83 struct Bracket { 84 size_t NextOffset; 85 BitVector Bytes; 86 }; 87 SmallVector<Bracket, 0> Brackets; 88 SmallVector<char, 0> Pat; 89 }; 90 SmallVector<SubGlobPattern, 1> SubGlobs; 91 }; 92 } 93 94 #endif // LLVM_SUPPORT_GLOBPATTERN_H 95