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