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