clang-tools  14.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/SmallString.h"
11 
12 using namespace clang;
13 using namespace 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  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.
28 static llvm::Regex consumeGlob(StringRef &GlobList) {
29  StringRef UntrimmedGlob = GlobList.substr(0, GlobList.find(','));
30  StringRef Glob = UntrimmedGlob.trim();
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('.');
37  else if (MetaChars.contains(C))
38  RegexText.push_back('\\');
39  RegexText.push_back(C);
40  }
41  RegexText.push_back('$');
42  return llvm::Regex(RegexText);
43 }
44 
45 GlobList::GlobList(StringRef Globs, bool KeepNegativeGlobs /* =true */) {
46  Items.reserve(Globs.count(',') + 1);
47  do {
48  GlobListItem Item;
49  Item.IsPositive = !consumeNegativeIndicator(Globs);
50  Item.Regex = consumeGlob(Globs);
51  if (Item.IsPositive || KeepNegativeGlobs)
52  Items.push_back(std::move(Item));
53  } while (!Globs.empty());
54 }
55 
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)) {
60  if (Item.Regex.match(S))
61  return Item.IsPositive;
62  }
63  return false;
64 }
clang::tidy::GlobList
Read-only set of strings represented as a list of positive and negative globs.
Definition: GlobList.h:25
consumeNegativeIndicator
static bool consumeNegativeIndicator(StringRef &GlobList)
Definition: GlobList.cpp:17
GlobList.h
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:45
consumeGlob
static llvm::Regex consumeGlob(StringRef &GlobList)
Definition: GlobList.cpp:28
C
const Criteria C
Definition: FunctionCognitiveComplexityCheck.cpp:93
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::GlobList::contains
bool contains(StringRef S) const
Returns true if the pattern matches S.
Definition: GlobList.cpp:56