clang-tools 10.0.0

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(" \r\n");
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.find(C) != StringRef::npos)
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) {
46 do {
47 GlobListItem Item;
48 Item.IsPositive = !ConsumeNegativeIndicator(Globs);
49 Item.Regex = ConsumeGlob(Globs);
50 Items.push_back(std::move(Item));
51 } while (!Globs.empty());
52 }
53
54 bool GlobList::contains(StringRef S) {
55 bool Contains = false;
56 for (const GlobListItem &Item : Items) {
57 if (Item.Regex.match(S))
58 Contains = Item.IsPositive;
59 }
60 return Contains;
61 }
clang::tidy::GlobList
Read-only set of strings represented as a list of positive and negative globs.
Definition: GlobList.h:25
ConsumeGlob
static llvm::Regex ConsumeGlob(StringRef &GlobList)
Definition: GlobList.cpp:28
clang::tidy::GlobList::GlobList
GlobList(StringRef Globs)
Globs is a comma-separated list of globs (only the &#39;*&#39; metacharacter is supported) with an optional &#39;...
Definition: GlobList.cpp:45
ConsumeNegativeIndicator
static bool ConsumeNegativeIndicator(StringRef &GlobList)
Definition: GlobList.cpp:17
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::GlobList::contains
bool contains(StringRef S)
Returns true if the pattern matches S.
Definition: GlobList.cpp:54
GlobList.h