carlosgalvezp created this revision. carlosgalvezp added reviewers: aaron.ballman, whisperity. Herald added subscribers: rnkovacs, xazax.hun. carlosgalvezp requested review of this revision. Herald added a project: clang-tools-extra. Herald added a subscriber: cfe-commits.
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 this 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. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D113422 Files: clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h clang-tools-extra/clang-tidy/GlobList.cpp clang-tools-extra/clang-tidy/GlobList.h clang-tools-extra/unittests/clang-tidy/GlobListTest.cpp
Index: clang-tools-extra/unittests/clang-tidy/GlobListTest.cpp =================================================================== --- clang-tools-extra/unittests/clang-tidy/GlobListTest.cpp +++ clang-tools-extra/unittests/clang-tidy/GlobListTest.cpp @@ -4,15 +4,20 @@ namespace clang { namespace tidy { -TEST(GlobList, Empty) { - GlobList Filter(""); +template <typename GlobListT> struct GlobListTest : public ::testing::Test {}; + +using GlobListTypes = ::testing::Types<GlobList, CachedGlobList>; +TYPED_TEST_SUITE(GlobListTest, GlobListTypes); + +TYPED_TEST(GlobListTest, Empty) { + TypeParam Filter(""); EXPECT_TRUE(Filter.contains("")); EXPECT_FALSE(Filter.contains("aaa")); } -TEST(GlobList, Nothing) { - GlobList Filter("-*"); +TYPED_TEST(GlobListTest, Nothing) { + TypeParam Filter("-*"); EXPECT_FALSE(Filter.contains("")); EXPECT_FALSE(Filter.contains("a")); @@ -21,8 +26,8 @@ EXPECT_FALSE(Filter.contains("*")); } -TEST(GlobList, Everything) { - GlobList Filter("*"); +TYPED_TEST(GlobListTest, Everything) { + TypeParam Filter("*"); EXPECT_TRUE(Filter.contains("")); EXPECT_TRUE(Filter.contains("aaaa")); @@ -31,8 +36,8 @@ EXPECT_TRUE(Filter.contains("*")); } -TEST(GlobList, OneSimplePattern) { - GlobList Filter("aaa"); +TYPED_TEST(GlobListTest, OneSimplePattern) { + TypeParam Filter("aaa"); EXPECT_TRUE(Filter.contains("aaa")); EXPECT_FALSE(Filter.contains("")); @@ -41,8 +46,8 @@ EXPECT_FALSE(Filter.contains("bbb")); } -TEST(GlobList, TwoSimplePatterns) { - GlobList Filter("aaa,bbb"); +TYPED_TEST(GlobListTest, TwoSimplePatterns) { + TypeParam Filter("aaa,bbb"); EXPECT_TRUE(Filter.contains("aaa")); EXPECT_TRUE(Filter.contains("bbb")); @@ -52,11 +57,11 @@ EXPECT_FALSE(Filter.contains("bbbb")); } -TEST(GlobList, PatternPriority) { +TYPED_TEST(GlobListTest, PatternPriority) { // The last glob that matches the string decides whether that string is // included or excluded. { - GlobList Filter("a*,-aaa"); + TypeParam Filter("a*,-aaa"); EXPECT_FALSE(Filter.contains("")); EXPECT_TRUE(Filter.contains("a")); @@ -65,7 +70,7 @@ EXPECT_TRUE(Filter.contains("aaaa")); } { - GlobList Filter("-aaa,a*"); + TypeParam Filter("-aaa,a*"); EXPECT_FALSE(Filter.contains("")); EXPECT_TRUE(Filter.contains("a")); @@ -75,15 +80,16 @@ } } -TEST(GlobList, WhitespacesAtBegin) { - GlobList Filter("-*, a.b.*"); +TYPED_TEST(GlobListTest, WhitespacesAtBegin) { + TypeParam Filter("-*, a.b.*"); EXPECT_TRUE(Filter.contains("a.b.c")); EXPECT_FALSE(Filter.contains("b.c")); } -TEST(GlobList, Complex) { - GlobList Filter("*,-a.*, -b.*, \r \n a.1.* ,-a.1.A.*,-..,-...,-..+,-*$, -*qwe* "); +TYPED_TEST(GlobListTest, Complex) { + TypeParam Filter( + "*,-a.*, -b.*, \r \n a.1.* ,-a.1.A.*,-..,-...,-..+,-*$, -*qwe* "); EXPECT_TRUE(Filter.contains("aaa")); EXPECT_TRUE(Filter.contains("qqq")); Index: clang-tools-extra/clang-tidy/GlobList.h =================================================================== --- clang-tools-extra/clang-tidy/GlobList.h +++ clang-tools-extra/clang-tidy/GlobList.h @@ -11,6 +11,7 @@ #include "clang/Basic/LLVM.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Regex.h" @@ -39,7 +40,6 @@ bool contains(StringRef S) const; private: - struct GlobListItem { bool IsPositive; llvm::Regex Regex; @@ -47,6 +47,22 @@ SmallVector<GlobListItem, 0> Items; }; +/// A \p GlobList that caches search results, so that search is performed only +/// once for the same query. +class CachedGlobList { +public: + /// \see GlobList::GlobList + CachedGlobList(StringRef Globs, bool KeepNegativeGlobs = true); + + /// \see GlobList::contains + bool contains(StringRef S); + +private: + GlobList Globs; + enum Tristate { None, Yes, No }; + llvm::StringMap<Tristate> Cache; +}; + } // end namespace tidy } // end namespace clang Index: clang-tools-extra/clang-tidy/GlobList.cpp =================================================================== --- clang-tools-extra/clang-tidy/GlobList.cpp +++ clang-tools-extra/clang-tidy/GlobList.cpp @@ -62,3 +62,19 @@ } return false; } + +CachedGlobList::CachedGlobList(StringRef Globs, bool KeepNegativeGlobs) + : Globs(Globs, KeepNegativeGlobs) {} + +bool CachedGlobList::contains(StringRef S) { + switch (auto &Result = Cache[S]) { + case Yes: + return true; + case No: + return false; + case None: + Result = Globs.contains(S) ? Yes : No; + return Result == Yes; + } + llvm_unreachable("invalid enum"); +} Index: clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h =================================================================== --- clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h +++ clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h @@ -29,6 +29,7 @@ } // namespace tooling namespace tidy { +class CachedGlobList; /// A detected error complete with information to display diagnostic and /// automatic fix. @@ -196,7 +197,7 @@ std::string CurrentFile; ClangTidyOptions CurrentOptions; - class CachedGlobList; + std::unique_ptr<CachedGlobList> CheckFilter; std::unique_ptr<CachedGlobList> WarningAsErrorFilter; Index: clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp =================================================================== --- clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp +++ clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp @@ -149,29 +149,6 @@ : tooling::Diagnostic(CheckName, DiagLevel, BuildDirectory), IsWarningAsError(IsWarningAsError) {} -class ClangTidyContext::CachedGlobList { -public: - CachedGlobList(StringRef Globs) : Globs(Globs) {} - - bool contains(StringRef S) { - switch (auto &Result = Cache[S]) { - case Yes: - return true; - case No: - return false; - case None: - Result = Globs.contains(S) ? Yes : No; - return Result == Yes; - } - llvm_unreachable("invalid enum"); - } - -private: - GlobList Globs; - enum Tristate { None, Yes, No }; - llvm::StringMap<Tristate> Cache; -}; - ClangTidyContext::ClangTidyContext( std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider, bool AllowEnablingAnalyzerAlphaCheckers)
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits