Skip to content

Commit 946eb7a

Browse files
committed
[clang-tidy][NFC] Move CachedGlobList to GlobList.h
Currently it's hidden inside ClangTidyDiagnosticConsumer, so it's hard to know it exists. Given that there are multiple uses of globs in clang-tidy, it makes sense to have these classes publicly available for other use cases that might benefit from it. Also, add unit test by converting the existing tests for GlobList into typed tests. Reviewed By: salman-javed-nz Differential Revision: https://reviews.llvm.org/D113422
1 parent 728736e commit 946eb7a

File tree

5 files changed

+63
-47
lines changed

5 files changed

+63
-47
lines changed

clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -155,29 +155,6 @@ ClangTidyError::ClangTidyError(StringRef CheckName,
155155
: tooling::Diagnostic(CheckName, DiagLevel, BuildDirectory),
156156
IsWarningAsError(IsWarningAsError) {}
157157

158-
class ClangTidyContext::CachedGlobList {
159-
public:
160-
CachedGlobList(StringRef Globs) : Globs(Globs) {}
161-
162-
bool contains(StringRef S) {
163-
switch (auto &Result = Cache[S]) {
164-
case Yes:
165-
return true;
166-
case No:
167-
return false;
168-
case None:
169-
Result = Globs.contains(S) ? Yes : No;
170-
return Result == Yes;
171-
}
172-
llvm_unreachable("invalid enum");
173-
}
174-
175-
private:
176-
GlobList Globs;
177-
enum Tristate { None, Yes, No };
178-
llvm::StringMap<Tristate> Cache;
179-
};
180-
181158
ClangTidyContext::ClangTidyContext(
182159
std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
183160
bool AllowEnablingAnalyzerAlphaCheckers)

clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class CompilationDatabase;
2929
} // namespace tooling
3030

3131
namespace tidy {
32+
class CachedGlobList;
3233

3334
/// A detected error complete with information to display diagnostic and
3435
/// automatic fix.
@@ -191,7 +192,7 @@ class ClangTidyContext {
191192

192193
std::string CurrentFile;
193194
ClangTidyOptions CurrentOptions;
194-
class CachedGlobList;
195+
195196
std::unique_ptr<CachedGlobList> CheckFilter;
196197
std::unique_ptr<CachedGlobList> WarningAsErrorFilter;
197198

clang-tools-extra/clang-tidy/GlobList.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include "GlobList.h"
1010
#include "llvm/ADT/SmallString.h"
1111

12-
using namespace clang;
13-
using namespace tidy;
12+
namespace clang {
13+
namespace tidy {
1414

1515
// Returns true if GlobList starts with the negative indicator ('-'), removes it
1616
// from the GlobList.
@@ -62,3 +62,19 @@ bool GlobList::contains(StringRef S) const {
6262
}
6363
return false;
6464
}
65+
66+
bool CachedGlobList::contains(StringRef S) const {
67+
switch (auto &Result = Cache[S]) {
68+
case Yes:
69+
return true;
70+
case No:
71+
return false;
72+
case None:
73+
Result = GlobList::contains(S) ? Yes : No;
74+
return Result == Yes;
75+
}
76+
llvm_unreachable("invalid enum");
77+
}
78+
79+
} // namespace tidy
80+
} // namespace clang

clang-tools-extra/clang-tidy/GlobList.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "clang/Basic/LLVM.h"
1313
#include "llvm/ADT/SmallVector.h"
14+
#include "llvm/ADT/StringMap.h"
1415
#include "llvm/ADT/StringRef.h"
1516
#include "llvm/Support/Regex.h"
1617

@@ -24,6 +25,8 @@ namespace tidy {
2425
/// them in the order of appearance in the list.
2526
class GlobList {
2627
public:
28+
virtual ~GlobList() = default;
29+
2730
/// \p Globs is a comma-separated list of globs (only the '*' metacharacter is
2831
/// supported) with an optional '-' prefix to denote exclusion.
2932
///
@@ -36,18 +39,31 @@ class GlobList {
3639

3740
/// Returns \c true if the pattern matches \p S. The result is the last
3841
/// matching glob's Positive flag.
39-
bool contains(StringRef S) const;
42+
virtual bool contains(StringRef S) const;
4043

4144
private:
42-
4345
struct GlobListItem {
4446
bool IsPositive;
4547
llvm::Regex Regex;
4648
};
4749
SmallVector<GlobListItem, 0> Items;
4850
};
4951

50-
} // end namespace tidy
51-
} // end namespace clang
52+
/// A \p GlobList that caches search results, so that search is performed only
53+
/// once for the same query.
54+
class CachedGlobList final : public GlobList {
55+
public:
56+
using GlobList::GlobList;
57+
58+
/// \see GlobList::contains
59+
bool contains(StringRef S) const override;
60+
61+
private:
62+
enum Tristate { None, Yes, No };
63+
mutable llvm::StringMap<Tristate> Cache;
64+
};
65+
66+
} // namespace tidy
67+
} // namespace clang
5268

5369
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GLOBLIST_H

clang-tools-extra/unittests/clang-tidy/GlobListTest.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44
namespace clang {
55
namespace tidy {
66

7-
TEST(GlobList, Empty) {
8-
GlobList Filter("");
7+
template <typename GlobListT> struct GlobListTest : public ::testing::Test {};
8+
9+
using GlobListTypes = ::testing::Types<GlobList, CachedGlobList>;
10+
TYPED_TEST_SUITE(GlobListTest, GlobListTypes);
11+
12+
TYPED_TEST(GlobListTest, Empty) {
13+
TypeParam Filter("");
914

1015
EXPECT_TRUE(Filter.contains(""));
1116
EXPECT_FALSE(Filter.contains("aaa"));
1217
}
1318

14-
TEST(GlobList, Nothing) {
15-
GlobList Filter("-*");
19+
TYPED_TEST(GlobListTest, Nothing) {
20+
TypeParam Filter("-*");
1621

1722
EXPECT_FALSE(Filter.contains(""));
1823
EXPECT_FALSE(Filter.contains("a"));
@@ -21,8 +26,8 @@ TEST(GlobList, Nothing) {
2126
EXPECT_FALSE(Filter.contains("*"));
2227
}
2328

24-
TEST(GlobList, Everything) {
25-
GlobList Filter("*");
29+
TYPED_TEST(GlobListTest, Everything) {
30+
TypeParam Filter("*");
2631

2732
EXPECT_TRUE(Filter.contains(""));
2833
EXPECT_TRUE(Filter.contains("aaaa"));
@@ -31,8 +36,8 @@ TEST(GlobList, Everything) {
3136
EXPECT_TRUE(Filter.contains("*"));
3237
}
3338

34-
TEST(GlobList, OneSimplePattern) {
35-
GlobList Filter("aaa");
39+
TYPED_TEST(GlobListTest, OneSimplePattern) {
40+
TypeParam Filter("aaa");
3641

3742
EXPECT_TRUE(Filter.contains("aaa"));
3843
EXPECT_FALSE(Filter.contains(""));
@@ -41,8 +46,8 @@ TEST(GlobList, OneSimplePattern) {
4146
EXPECT_FALSE(Filter.contains("bbb"));
4247
}
4348

44-
TEST(GlobList, TwoSimplePatterns) {
45-
GlobList Filter("aaa,bbb");
49+
TYPED_TEST(GlobListTest, TwoSimplePatterns) {
50+
TypeParam Filter("aaa,bbb");
4651

4752
EXPECT_TRUE(Filter.contains("aaa"));
4853
EXPECT_TRUE(Filter.contains("bbb"));
@@ -52,11 +57,11 @@ TEST(GlobList, TwoSimplePatterns) {
5257
EXPECT_FALSE(Filter.contains("bbbb"));
5358
}
5459

55-
TEST(GlobList, PatternPriority) {
60+
TYPED_TEST(GlobListTest, PatternPriority) {
5661
// The last glob that matches the string decides whether that string is
5762
// included or excluded.
5863
{
59-
GlobList Filter("a*,-aaa");
64+
TypeParam Filter("a*,-aaa");
6065

6166
EXPECT_FALSE(Filter.contains(""));
6267
EXPECT_TRUE(Filter.contains("a"));
@@ -65,7 +70,7 @@ TEST(GlobList, PatternPriority) {
6570
EXPECT_TRUE(Filter.contains("aaaa"));
6671
}
6772
{
68-
GlobList Filter("-aaa,a*");
73+
TypeParam Filter("-aaa,a*");
6974

7075
EXPECT_FALSE(Filter.contains(""));
7176
EXPECT_TRUE(Filter.contains("a"));
@@ -75,15 +80,16 @@ TEST(GlobList, PatternPriority) {
7580
}
7681
}
7782

78-
TEST(GlobList, WhitespacesAtBegin) {
79-
GlobList Filter("-*, a.b.*");
83+
TYPED_TEST(GlobListTest, WhitespacesAtBegin) {
84+
TypeParam Filter("-*, a.b.*");
8085

8186
EXPECT_TRUE(Filter.contains("a.b.c"));
8287
EXPECT_FALSE(Filter.contains("b.c"));
8388
}
8489

85-
TEST(GlobList, Complex) {
86-
GlobList Filter("*,-a.*, -b.*, \r \n a.1.* ,-a.1.A.*,-..,-...,-..+,-*$, -*qwe* ");
90+
TYPED_TEST(GlobListTest, Complex) {
91+
TypeParam Filter(
92+
"*,-a.*, -b.*, \r \n a.1.* ,-a.1.A.*,-..,-...,-..+,-*$, -*qwe* ");
8793

8894
EXPECT_TRUE(Filter.contains("aaa"));
8995
EXPECT_TRUE(Filter.contains("qqq"));

0 commit comments

Comments
 (0)