| 1 | //===--- tools/extra/clang-tidy/GlobList.cpp ------------------------------===// |
|---|---|
| 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 | #include "GlobList.h" |
| 10 | #include "llvm/ADT/STLExtras.h" |
| 11 | #include "llvm/ADT/SmallString.h" |
| 12 | |
| 13 | namespace clang::tidy { |
| 14 | |
| 15 | // Returns true if GlobList starts with the negative indicator ('-'), removes it |
| 16 | // from the GlobList. |
| 17 | static bool consumeNegativeIndicator(StringRef &GlobList) { |
| 18 | GlobList = GlobList.trim(); |
| 19 | return GlobList.consume_front(Prefix: "-"); |
| 20 | } |
| 21 | |
| 22 | // Extracts the first glob from the comma-separated list of globs, |
| 23 | // removes it and the trailing comma from the GlobList and |
| 24 | // returns the extracted glob. |
| 25 | static llvm::StringRef extractNextGlob(StringRef &GlobList) { |
| 26 | StringRef UntrimmedGlob = GlobList.substr(Start: 0, N: GlobList.find_first_of(Chars: ",\n")); |
| 27 | StringRef Glob = UntrimmedGlob.trim(); |
| 28 | GlobList = GlobList.substr(Start: UntrimmedGlob.size() + 1); |
| 29 | return Glob; |
| 30 | } |
| 31 | |
| 32 | static llvm::Regex createRegexFromGlob(StringRef &Glob) { |
| 33 | SmallString<128> RegexText("^"); |
| 34 | StringRef MetaChars("()^$|*+?.[]\\{}"); |
| 35 | for (char C : Glob) { |
| 36 | if (C == '*') |
| 37 | RegexText.push_back(Elt: '.'); |
| 38 | else if (MetaChars.contains(C)) |
| 39 | RegexText.push_back(Elt: '\\'); |
| 40 | RegexText.push_back(Elt: C); |
| 41 | } |
| 42 | RegexText.push_back(Elt: '$'); |
| 43 | return {RegexText.str()}; |
| 44 | } |
| 45 | |
| 46 | GlobList::GlobList(StringRef Globs, bool KeepNegativeGlobs /* =true */) { |
| 47 | Items.reserve(N: Globs.count(C: ',') + Globs.count(C: '\n') + 1); |
| 48 | do { |
| 49 | GlobListItem Item; |
| 50 | Item.IsPositive = !consumeNegativeIndicator(GlobList&: Globs); |
| 51 | Item.Text = extractNextGlob(GlobList&: Globs); |
| 52 | Item.Regex = createRegexFromGlob(Glob&: Item.Text); |
| 53 | if (Item.IsPositive || KeepNegativeGlobs) |
| 54 | Items.push_back(Elt: std::move(Item)); |
| 55 | } while (!Globs.empty()); |
| 56 | } |
| 57 | |
| 58 | bool GlobList::contains(StringRef S) const { |
| 59 | // Iterating the container backwards as the last match determins if S is in |
| 60 | // the list. |
| 61 | for (const GlobListItem &Item : llvm::reverse(C: Items)) { |
| 62 | if (Item.Regex.match(String: S)) |
| 63 | return Item.IsPositive; |
| 64 | } |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | bool CachedGlobList::contains(StringRef S) const { |
| 69 | auto Entry = Cache.try_emplace(Key: S); |
| 70 | bool &Value = Entry.first->getValue(); |
| 71 | // If the entry was just inserted, determine its required value. |
| 72 | if (Entry.second) |
| 73 | Value = GlobList::contains(S); |
| 74 | return Value; |
| 75 | } |
| 76 | |
| 77 | } // namespace clang::tidy |
| 78 |
