From 3d5f48dc7ff66b1788bb2a8cd7bb65c47d6e62f4 Mon Sep 17 00:00:00 2001 From: Dmitri Gribenko Date: Tue, 27 Aug 2019 10:56:13 +0000 Subject: [PATCH] Refactor GlobList from an ad-hoc linked list to a vector Summary: I think it makes method implementations more obvious. Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66788 llvm-svn: 370039 --- clang-tools-extra/clang-tidy/GlobList.cpp | 23 +++++++++++-------- clang-tools-extra/clang-tidy/GlobList.h | 28 ++++++++++++++--------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/clang-tools-extra/clang-tidy/GlobList.cpp b/clang-tools-extra/clang-tidy/GlobList.cpp index 4a8b35284172..fad3616644c1 100644 --- a/clang-tools-extra/clang-tidy/GlobList.cpp +++ b/clang-tools-extra/clang-tidy/GlobList.cpp @@ -42,15 +42,20 @@ static llvm::Regex ConsumeGlob(StringRef &GlobList) { return llvm::Regex(RegexText); } -GlobList::GlobList(StringRef Globs) - : Positive(!ConsumeNegativeIndicator(Globs)), Regex(ConsumeGlob(Globs)), - NextGlob(Globs.empty() ? nullptr : new GlobList(Globs)) {} +GlobList::GlobList(StringRef Globs) { + do { + GlobListItem Item; + Item.IsPositive = !ConsumeNegativeIndicator(Globs); + Item.Regex = ConsumeGlob(Globs); + Items.push_back(std::move(Item)); + } while (!Globs.empty()); +} -bool GlobList::contains(StringRef S, bool Contains) { - if (Regex.match(S)) - Contains = Positive; - - if (NextGlob) - Contains = NextGlob->contains(S, Contains); +bool GlobList::contains(StringRef S) { + bool Contains = false; + for (const GlobListItem &Item : Items) { + if (Item.Regex.match(S)) + Contains = Item.IsPositive; + } return Contains; } diff --git a/clang-tools-extra/clang-tidy/GlobList.h b/clang-tools-extra/clang-tidy/GlobList.h index 8015ad713849..4edb03bbad96 100644 --- a/clang-tools-extra/clang-tidy/GlobList.h +++ b/clang-tools-extra/clang-tidy/GlobList.h @@ -12,30 +12,36 @@ #include "clang/Basic/LLVM.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Regex.h" -#include +#include namespace clang { namespace tidy { -/// Read-only set of strings represented as a list of positive and -/// negative globs. Positive globs add all matched strings to the set, negative -/// globs remove them in the order of appearance in the list. +/// Read-only set of strings represented as a list of positive and negative +/// globs. +/// +/// Positive globs add all matched strings to the set, negative globs remove +/// them in the order of appearance in the list. class GlobList { public: - /// \p GlobList is a comma-separated list of globs (only '*' - /// metacharacter is supported) with optional '-' prefix to denote exclusion. + /// \p Globs is a comma-separated list of globs (only the '*' metacharacter is + /// supported) with an optional '-' prefix to denote exclusion. + /// + /// An empty \p Globs string is interpreted as one glob that matches an empty + /// string. GlobList(StringRef Globs); /// Returns \c true if the pattern matches \p S. The result is the last /// matching glob's Positive flag. - bool contains(StringRef S) { return contains(S, false); } + bool contains(StringRef S); private: - bool contains(StringRef S, bool Contains); - bool Positive; - llvm::Regex Regex; - std::unique_ptr NextGlob; + struct GlobListItem { + bool IsPositive; + mutable llvm::Regex Regex; + }; + std::vector Items; }; } // end namespace tidy