1 //===--- GlobList.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 LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GLOBLIST_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GLOBLIST_H
11 
12 #include "clang/Basic/LLVM.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/Regex.h"
17 
18 namespace clang {
19 namespace tidy {
20 
21 /// Read-only set of strings represented as a list of positive and negative
22 /// globs.
23 ///
24 /// Positive globs add all matched strings to the set, negative globs remove
25 /// them in the order of appearance in the list.
26 class GlobList {
27 public:
28   virtual ~GlobList() = default;
29 
30   /// \p Globs is a comma-separated list of globs (only the '*' metacharacter is
31   /// supported) with an optional '-' prefix to denote exclusion.
32   ///
33   /// An empty \p Globs string is interpreted as one glob that matches an empty
34   /// string.
35   ///
36   /// \p KeepNegativeGlobs a bool flag indicating whether to keep negative
37   /// globs from \p Globs or not. When false, negative globs are simply ignored.
38   GlobList(StringRef Globs, bool KeepNegativeGlobs = true);
39 
40   /// Returns \c true if the pattern matches \p S. The result is the last
41   /// matching glob's Positive flag.
42   virtual bool contains(StringRef S) const;
43 
44 private:
45   struct GlobListItem {
46     bool IsPositive;
47     llvm::Regex Regex;
48   };
49   SmallVector<GlobListItem, 0> Items;
50 };
51 
52 /// A \p GlobList that caches search results, so that search is performed only
53 /// once for the same query.
54 class CachedGlobList final : public GlobList {
55 public:
56   using GlobList::GlobList;
57 
58   /// \see GlobList::contains
59   bool contains(StringRef S) const override;
60 
61 private:
62   mutable llvm::StringMap<bool> Cache;
63 };
64 
65 } // namespace tidy
66 } // namespace clang
67 
68 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GLOBLIST_H
69