GlobList.h 2.0 KB

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