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