[PATCH] D58731: [clang-tidy] added cppcoreguidelines-explicit-virtual-functions
lewmpk created this revision. lewmpk added reviewers: alexfh, alexfh_. Herald added subscribers: cfe-commits, kbarton, xazax.hun, nemanjai. Herald added a project: clang. Addresses the bugzilla bug #30397. modernize-use-override suggests that destructors require the override specifier and the CPP core guidelines do not recommend this. Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D58731 Files: clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/modernize/UseOverrideCheck.cpp clang-tidy/modernize/UseOverrideCheck.h Index: clang-tidy/modernize/UseOverrideCheck.h === --- clang-tidy/modernize/UseOverrideCheck.h +++ clang-tidy/modernize/UseOverrideCheck.h @@ -19,9 +19,13 @@ class UseOverrideCheck : public ClangTidyCheck { public: UseOverrideCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context), +IgnoreDestructors(Options.get("IgnoreDestructors", false)) {} void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + +protected: + const bool IgnoreDestructors; }; } // namespace modernize Index: clang-tidy/modernize/UseOverrideCheck.cpp === --- clang-tidy/modernize/UseOverrideCheck.cpp +++ clang-tidy/modernize/UseOverrideCheck.cpp @@ -19,8 +19,16 @@ void UseOverrideCheck::registerMatchers(MatchFinder *Finder) { // Only register the matcher for C++11. - if (getLangOpts().CPlusPlus11) -Finder->addMatcher(cxxMethodDecl(isOverride()).bind("method"), this); + if (getLangOpts().CPlusPlus11) { +if (IgnoreDestructors) { + Finder->addMatcher( + cxxMethodDecl(isOverride(), unless(cxxDestructorDecl())) + .bind("method"), + this); +} else { + Finder->addMatcher(cxxMethodDecl(isOverride()).bind("method"), this); +} + } } // Re-lex the tokens to get precise locations to insert 'override' and remove Index: clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp === --- clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp +++ clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp @@ -12,6 +12,7 @@ #include "../misc/NonPrivateMemberVariablesInClassesCheck.h" #include "../misc/UnconventionalAssignOperatorCheck.h" #include "../modernize/AvoidCArraysCheck.h" +#include "../modernize/UseOverrideCheck.h" #include "../readability/MagicNumbersCheck.h" #include "AvoidGotoCheck.h" #include "InterfacesGlobalInitCheck.h" @@ -46,6 +47,8 @@ "cppcoreguidelines-avoid-goto"); CheckFactories.registerCheck( "cppcoreguidelines-avoid-magic-numbers"); +CheckFactories.registerCheck( +"cppcoreguidelines-explicit-virtual-functions"); CheckFactories.registerCheck( "cppcoreguidelines-interfaces-global-init"); CheckFactories.registerCheck( @@ -91,6 +94,9 @@ Opts["cppcoreguidelines-non-private-member-variables-in-classes." "IgnoreClassesWithAllMemberVariablesBeingPublic"] = "1"; +Opts["cppcoreguidelines-explicit-virtual-functions." + "IgnoreDestructors"] = "1"; + return Options; } }; Index: clang-tidy/modernize/UseOverrideCheck.h === --- clang-tidy/modernize/UseOverrideCheck.h +++ clang-tidy/modernize/UseOverrideCheck.h @@ -19,9 +19,13 @@ class UseOverrideCheck : public ClangTidyCheck { public: UseOverrideCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context), +IgnoreDestructors(Options.get("IgnoreDestructors", false)) {} void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + +protected: + const bool IgnoreDestructors; }; } // namespace modernize Index: clang-tidy/modernize/UseOverrideCheck.cpp === --- clang-tidy/modernize/UseOverrideCheck.cpp +++ clang-tidy/modernize/UseOverrideCheck.cpp @@ -19,8 +19,16 @@ void UseOverrideCheck::registerMatchers(MatchFinder *Finder) { // Only register the matcher for C++11. - if (getLangOpts().CPlusPlus11) -Finder->addMatcher(cxxMethodDecl(isOverride()).bind("method"), this); + if (getLangOpts().CPlusPlus11) { +if (IgnoreDestructors) { + Finder->addMatcher( + cxxMethodDecl(isOverride(), unless(cxxDestructorDecl())) + .bind("method"), + this); +} else { + Finder->addMatcher(cxxMethodDecl(isOverride()).bind("method"), this); +} + } } // Re-lex the tokens to get precise locations to insert 'override' and remove Index: clang-tidy/cppcoreguidelines
[PATCH] D58731: [clang-tidy] added cppcoreguidelines-explicit-virtual-functions
lewmpk updated this revision to Diff 188622. lewmpk marked an inline comment as done. lewmpk added a comment. - addressed comments - provided tests - updated documentation - updated release notes CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58731/new/ https://reviews.llvm.org/D58731 Files: clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/modernize/UseOverrideCheck.cpp clang-tidy/modernize/UseOverrideCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/modernize-use-override.rst test/clang-tidy/modernize-use-override-no-destructors.cpp Index: test/clang-tidy/modernize-use-override-no-destructors.cpp === --- /dev/null +++ test/clang-tidy/modernize-use-override-no-destructors.cpp @@ -0,0 +1,17 @@ +// RUN: %check_clang_tidy %s modernize-use-override %t -- \ +// RUN: -config="{CheckOptions: [{key: modernize-use-override.IgnoreDestructors, value: '1'" \ +// RUN: -- -std=c++11 + +struct Base { + virtual ~Base(){}; + virtual void f(){}; +}; + +struct Simple : public Base { + virtual ~Simple(); + // CHECK-MESSAGES-NOT: warning: + // CHECK-FIXES: {{^}} void b() override; + virtual void f(){}; + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: prefer using 'override' or (rarely) 'final' instead of 'virtual' [modernize-use-override] + // CHECK-FIXES: {{^}} void f() override; +}; Index: docs/clang-tidy/checks/modernize-use-override.rst === --- docs/clang-tidy/checks/modernize-use-override.rst +++ docs/clang-tidy/checks/modernize-use-override.rst @@ -5,3 +5,11 @@ Use C++11's ``override`` and remove ``virtual`` where applicable. + +Options +--- + +.. option:: IgnoreDestructors + + If set to non-zero, this check with not diagnose destructors. Default is '0'. + \ No newline at end of file Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -67,6 +67,11 @@ Improvements to clang-tidy -- +- New :doc:`cppcoreguidelines-explicit-virtual-functions + ` check. + + Checks whether virtual functions other than destructors can specify 'override' instead. + - New :doc:`abseil-duration-addition ` check. Index: clang-tidy/modernize/UseOverrideCheck.h === --- clang-tidy/modernize/UseOverrideCheck.h +++ clang-tidy/modernize/UseOverrideCheck.h @@ -19,9 +19,13 @@ class UseOverrideCheck : public ClangTidyCheck { public: UseOverrideCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context), +IgnoreDestructors(Options.get("IgnoreDestructors", false)) {} void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + +private: + const bool IgnoreDestructors; }; } // namespace modernize Index: clang-tidy/modernize/UseOverrideCheck.cpp === --- clang-tidy/modernize/UseOverrideCheck.cpp +++ clang-tidy/modernize/UseOverrideCheck.cpp @@ -19,8 +19,15 @@ void UseOverrideCheck::registerMatchers(MatchFinder *Finder) { // Only register the matcher for C++11. - if (getLangOpts().CPlusPlus11) -Finder->addMatcher(cxxMethodDecl(isOverride()).bind("method"), this); + if (getLangOpts().CPlusPlus11) { +if (IgnoreDestructors) + Finder->addMatcher( + cxxMethodDecl(isOverride(), unless(cxxDestructorDecl())) + .bind("method"), + this); +else + Finder->addMatcher(cxxMethodDecl(isOverride()).bind("method"), this); + } } // Re-lex the tokens to get precise locations to insert 'override' and remove Index: clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp === --- clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp +++ clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp @@ -12,6 +12,7 @@ #include "../misc/NonPrivateMemberVariablesInClassesCheck.h" #include "../misc/UnconventionalAssignOperatorCheck.h" #include "../modernize/AvoidCArraysCheck.h" +#include "../modernize/UseOverrideCheck.h" #include "../readability/MagicNumbersCheck.h" #include "AvoidGotoCheck.h" #include "InterfacesGlobalInitCheck.h" @@ -46,6 +47,8 @@ "cppcoreguidelines-avoid-goto"); CheckFactories.registerCheck( "cppcoreguidelines-avoid-magic-numbers"); +CheckFactories.registerCheck( +"cppcoreguidelines-explicit-virtual-functions"); CheckFactories.registerCheck( "cppcoreguidelines-interfaces-global-init"); CheckFactories.registerCheck( @@ -91,6 +94,9 @@ Opts["cppcoreguidelines-non-private-member-variables-in-classes." "IgnoreClassesWithAllMemberVari
[PATCH] D58731: [clang-tidy] added cppcoreguidelines-explicit-virtual-functions
lewmpk marked 2 inline comments as done and an inline comment as not done. lewmpk added inline comments. Comment at: clang-tidy/modernize/UseOverrideCheck.h:23 + : ClangTidyCheck(Name, Context), +IgnoreDestructors(Options.get("IgnoreDestructors", false)) {} void registerMatchers(ast_matchers::MatchFinder *Finder) override; JonasToth wrote: > That requires additional methods for the function for the configuration to > function properly. Please see other checks and implement that the same way. Could you elaborate? I looked at other checks and they seem to implement it the same way. /llvm/tools/clang/tools/extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp:74 ``` CheckFunctionCalls(Options.get("CheckFunctionCalls", false)), ``` CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58731/new/ https://reviews.llvm.org/D58731 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D58731: [clang-tidy] added cppcoreguidelines-explicit-virtual-functions
lewmpk updated this revision to Diff 188628. lewmpk added a comment. addressed comments CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58731/new/ https://reviews.llvm.org/D58731 Files: clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/modernize/UseOverrideCheck.cpp clang-tidy/modernize/UseOverrideCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/cppcoreguidelines-explicit-virtual-functions.rst docs/clang-tidy/checks/modernize-use-override.rst test/clang-tidy/modernize-use-override-no-destructors.cpp Index: test/clang-tidy/modernize-use-override-no-destructors.cpp === --- /dev/null +++ test/clang-tidy/modernize-use-override-no-destructors.cpp @@ -0,0 +1,17 @@ +// RUN: %check_clang_tidy %s modernize-use-override %t -- \ +// RUN: -config="{CheckOptions: [{key: modernize-use-override.IgnoreDestructors, value: '1'" \ +// RUN: -- -std=c++11 + +struct Base { + virtual ~Base() {} + virtual void f() {} +}; + +struct Simple : public Base { + virtual ~Simple(); + // CHECK-MESSAGES-NOT: warning: + // CHECK-FIXES: {{^}} void b() override; + virtual void f() {} + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: prefer using 'override' or (rarely) 'final' instead of 'virtual' [modernize-use-override] + // CHECK-FIXES: {{^}} void f() override; +}; Index: docs/clang-tidy/checks/modernize-use-override.rst === --- docs/clang-tidy/checks/modernize-use-override.rst +++ docs/clang-tidy/checks/modernize-use-override.rst @@ -5,3 +5,11 @@ Use C++11's ``override`` and remove ``virtual`` where applicable. + +Options +--- + +.. option:: IgnoreDestructors + + If set to non-zero, this check with not diagnose destructors. Default is '0'. + \ No newline at end of file Index: docs/clang-tidy/checks/cppcoreguidelines-explicit-virtual-functions.rst === --- /dev/null +++ docs/clang-tidy/checks/cppcoreguidelines-explicit-virtual-functions.rst @@ -0,0 +1,10 @@ +.. title:: clang-tidy - cppcoreguidelines-explicit-virtual-functions +.. meta:: + :http-equiv=refresh: 5;URL=cppcoreguidelines-explicit-virtual-functions.html + +cppcoreguidelines-explicit-virtual-functions + + +The cppcoreguidelines-explicit-virtual-functions check is an alias, please see +`modernize-use-override `_ +for more information. Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -67,6 +67,11 @@ Improvements to clang-tidy -- +- New alias :doc:`cppcoreguidelines-explicit-virtual-functions + ` to + :doc:`modernize-use-override + ` was added. + - New :doc:`abseil-duration-addition ` check. Index: clang-tidy/modernize/UseOverrideCheck.h === --- clang-tidy/modernize/UseOverrideCheck.h +++ clang-tidy/modernize/UseOverrideCheck.h @@ -19,9 +19,14 @@ class UseOverrideCheck : public ClangTidyCheck { public: UseOverrideCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context), +IgnoreDestructors(Options.get("IgnoreDestructors", false)) {} void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + void storeOptions(ClangTidyOptions::OptionMap &Opts) override; + +private: + const bool IgnoreDestructors; }; } // namespace modernize Index: clang-tidy/modernize/UseOverrideCheck.cpp === --- clang-tidy/modernize/UseOverrideCheck.cpp +++ clang-tidy/modernize/UseOverrideCheck.cpp @@ -17,9 +17,20 @@ namespace tidy { namespace modernize { +void UseOverrideCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "IgnoreDestructors", IgnoreDestructors); +} + void UseOverrideCheck::registerMatchers(MatchFinder *Finder) { // Only register the matcher for C++11. - if (getLangOpts().CPlusPlus11) + if (!getLangOpts().CPlusPlus11) +return; + + if (IgnoreDestructors) +Finder->addMatcher( +cxxMethodDecl(isOverride(), unless(cxxDestructorDecl())).bind("method"), +this); + else Finder->addMatcher(cxxMethodDecl(isOverride()).bind("method"), this); } Index: clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp === --- clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp +++ clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp @@ -12,6 +12,7 @@ #include "../misc/NonPrivateMemberVariablesInClassesCheck.h" #include "../misc/UnconventionalAssignOperatorCheck.h" #include "../modernize/AvoidCArraysCheck.h" +#include "
[PATCH] D58731: [clang-tidy] added cppcoreguidelines-explicit-virtual-functions
lewmpk added a comment. thanks for the feedback everyone (and the warm welcome) CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58731/new/ https://reviews.llvm.org/D58731 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D58731: [clang-tidy] added cppcoreguidelines-explicit-virtual-functions
lewmpk added a comment. I'm trying to find a way to run the test. If someone else has already tested it then yes please. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58731/new/ https://reviews.llvm.org/D58731 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D58731: [clang-tidy] added cppcoreguidelines-explicit-virtual-functions
lewmpk updated this revision to Diff 188638. lewmpk added a comment. - fixed tests - fixed typo in documentation CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58731/new/ https://reviews.llvm.org/D58731 Files: clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/modernize/UseOverrideCheck.cpp clang-tidy/modernize/UseOverrideCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/cppcoreguidelines-explicit-virtual-functions.rst docs/clang-tidy/checks/modernize-use-override.rst test/clang-tidy/modernize-use-override-no-destructors.cpp Index: test/clang-tidy/modernize-use-override-no-destructors.cpp === --- /dev/null +++ test/clang-tidy/modernize-use-override-no-destructors.cpp @@ -0,0 +1,16 @@ +// RUN: %check_clang_tidy %s modernize-use-override %t -- \ +// RUN: -config="{CheckOptions: [{key: modernize-use-override.IgnoreDestructors, value: 1}]}" \ +// RUN: -- -std=c++11 + +struct Base { + virtual ~Base(); + virtual void f(); +}; + +struct Simple : public Base { + virtual ~Simple(); + // CHECK-MESSAGES-NOT: warning: + virtual void f(); + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: prefer using + // CHECK-FIXES: {{^}} void f() override; +}; Index: docs/clang-tidy/checks/modernize-use-override.rst === --- docs/clang-tidy/checks/modernize-use-override.rst +++ docs/clang-tidy/checks/modernize-use-override.rst @@ -5,3 +5,10 @@ Use C++11's ``override`` and remove ``virtual`` where applicable. + +Options +--- + +.. option:: IgnoreDestructors + + If set to non-zero, this check will not diagnose destructors. Default is '0'. Index: docs/clang-tidy/checks/cppcoreguidelines-explicit-virtual-functions.rst === --- /dev/null +++ docs/clang-tidy/checks/cppcoreguidelines-explicit-virtual-functions.rst @@ -0,0 +1,10 @@ +.. title:: clang-tidy - cppcoreguidelines-explicit-virtual-functions +.. meta:: + :http-equiv=refresh: 5;URL=cppcoreguidelines-explicit-virtual-functions.html + +cppcoreguidelines-explicit-virtual-functions + + +The cppcoreguidelines-explicit-virtual-functions check is an alias, please see +`modernize-use-override `_ +for more information. Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -67,6 +67,11 @@ Improvements to clang-tidy -- +- New alias :doc:`cppcoreguidelines-explicit-virtual-functions + ` to + :doc:`modernize-use-override + ` was added. + - New :doc:`abseil-duration-addition ` check. Index: clang-tidy/modernize/UseOverrideCheck.h === --- clang-tidy/modernize/UseOverrideCheck.h +++ clang-tidy/modernize/UseOverrideCheck.h @@ -19,9 +19,14 @@ class UseOverrideCheck : public ClangTidyCheck { public: UseOverrideCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context), +IgnoreDestructors(Options.get("IgnoreDestructors", false)) {} void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + void storeOptions(ClangTidyOptions::OptionMap &Opts) override; + +private: + const bool IgnoreDestructors; }; } // namespace modernize Index: clang-tidy/modernize/UseOverrideCheck.cpp === --- clang-tidy/modernize/UseOverrideCheck.cpp +++ clang-tidy/modernize/UseOverrideCheck.cpp @@ -17,9 +17,20 @@ namespace tidy { namespace modernize { +void UseOverrideCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "IgnoreDestructors", IgnoreDestructors); +} + void UseOverrideCheck::registerMatchers(MatchFinder *Finder) { // Only register the matcher for C++11. - if (getLangOpts().CPlusPlus11) + if (!getLangOpts().CPlusPlus11) +return; + + if (IgnoreDestructors) +Finder->addMatcher( +cxxMethodDecl(isOverride(), unless(cxxDestructorDecl())).bind("method"), +this); + else Finder->addMatcher(cxxMethodDecl(isOverride()).bind("method"), this); } Index: clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp === --- clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp +++ clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp @@ -12,6 +12,7 @@ #include "../misc/NonPrivateMemberVariablesInClassesCheck.h" #include "../misc/UnconventionalAssignOperatorCheck.h" #include "../modernize/AvoidCArraysCheck.h" +#include "../modernize/UseOverrideCheck.h" #include "../readability/MagicNumbersCheck.h" #include "AvoidGotoCheck.h" #include "InterfacesGlo
[PATCH] D58731: [clang-tidy] added cppcoreguidelines-explicit-virtual-functions
lewmpk updated this revision to Diff 188642. lewmpk added a comment. cleaned up documentation CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58731/new/ https://reviews.llvm.org/D58731 Files: clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/modernize/UseOverrideCheck.cpp clang-tidy/modernize/UseOverrideCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/cppcoreguidelines-explicit-virtual-functions.rst docs/clang-tidy/checks/modernize-use-override.rst test/clang-tidy/modernize-use-override-no-destructors.cpp Index: test/clang-tidy/modernize-use-override-no-destructors.cpp === --- /dev/null +++ test/clang-tidy/modernize-use-override-no-destructors.cpp @@ -0,0 +1,16 @@ +// RUN: %check_clang_tidy %s modernize-use-override %t -- \ +// RUN: -config="{CheckOptions: [{key: modernize-use-override.IgnoreDestructors, value: 1}]}" \ +// RUN: -- -std=c++11 + +struct Base { + virtual ~Base(); + virtual void f(); +}; + +struct Simple : public Base { + virtual ~Simple(); + // CHECK-MESSAGES-NOT: warning: + virtual void f(); + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: prefer using + // CHECK-FIXES: {{^}} void f() override; +}; Index: docs/clang-tidy/checks/modernize-use-override.rst === --- docs/clang-tidy/checks/modernize-use-override.rst +++ docs/clang-tidy/checks/modernize-use-override.rst @@ -3,5 +3,11 @@ modernize-use-override == - Use C++11's ``override`` and remove ``virtual`` where applicable. + +Options +--- + +.. option:: IgnoreDestructors + + If set to non-zero, this check will not diagnose destructors. Default is `0`. Index: docs/clang-tidy/checks/cppcoreguidelines-explicit-virtual-functions.rst === --- /dev/null +++ docs/clang-tidy/checks/cppcoreguidelines-explicit-virtual-functions.rst @@ -0,0 +1,10 @@ +.. title:: clang-tidy - cppcoreguidelines-explicit-virtual-functions +.. meta:: + :http-equiv=refresh: 5;URL=cppcoreguidelines-explicit-virtual-functions.html + +cppcoreguidelines-explicit-virtual-functions + + +The cppcoreguidelines-explicit-virtual-functions check is an alias, please see +`modernize-use-override `_ +for more information. Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -67,6 +67,11 @@ Improvements to clang-tidy -- +- New alias :doc:`cppcoreguidelines-explicit-virtual-functions + ` to + :doc:`modernize-use-override + ` was added. + - New :doc:`abseil-duration-addition ` check. Index: clang-tidy/modernize/UseOverrideCheck.h === --- clang-tidy/modernize/UseOverrideCheck.h +++ clang-tidy/modernize/UseOverrideCheck.h @@ -19,9 +19,14 @@ class UseOverrideCheck : public ClangTidyCheck { public: UseOverrideCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context), +IgnoreDestructors(Options.get("IgnoreDestructors", false)) {} void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + void storeOptions(ClangTidyOptions::OptionMap &Opts) override; + +private: + const bool IgnoreDestructors; }; } // namespace modernize Index: clang-tidy/modernize/UseOverrideCheck.cpp === --- clang-tidy/modernize/UseOverrideCheck.cpp +++ clang-tidy/modernize/UseOverrideCheck.cpp @@ -17,9 +17,20 @@ namespace tidy { namespace modernize { +void UseOverrideCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "IgnoreDestructors", IgnoreDestructors); +} + void UseOverrideCheck::registerMatchers(MatchFinder *Finder) { // Only register the matcher for C++11. - if (getLangOpts().CPlusPlus11) + if (!getLangOpts().CPlusPlus11) +return; + + if (IgnoreDestructors) +Finder->addMatcher( +cxxMethodDecl(isOverride(), unless(cxxDestructorDecl())).bind("method"), +this); + else Finder->addMatcher(cxxMethodDecl(isOverride()).bind("method"), this); } Index: clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp === --- clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp +++ clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp @@ -12,6 +12,7 @@ #include "../misc/NonPrivateMemberVariablesInClassesCheck.h" #include "../misc/UnconventionalAssignOperatorCheck.h" #include "../modernize/AvoidCArraysCheck.h" +#include "../modernize/UseOverrideCheck.h" #include "../readability/MagicNumbersCheck.h" #include "AvoidGotoCheck.
[PATCH] D58764: [clang-tidy] ignore predefined expressions in cppcoreguidelines-pro-bounds-array-to-pointer-decay check
lewmpk created this revision. lewmpk added a reviewer: clang-tools-extra. lewmpk added projects: clang, clang-tools-extra. Herald added subscribers: cfe-commits, kbarton, xazax.hun, nemanjai. Bugzilla: 40852 c++ int main() { const char* a = __FILE__; const char* b = __FUNCTION__; } variable `b` is now not marked as an error by `cppcoreguidelines-pro-bounds-array-to-pointer-decay` check Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D58764 Files: clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp Index: test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp === --- test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp +++ test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp @@ -44,6 +44,9 @@ return ("clang"); // OK, ParenExpr hides the literal-pointer decay } +const char *line = __FILE__; // OK +const char *func = __FUNCTION__; // OK, predefined value to pointer + void f2(void *const *); void bug25362() { void *a[2]; Index: clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp === --- clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp +++ clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp @@ -56,12 +56,14 @@ // 1) just before array subscription // 2) inside a range-for over an array // 3) if it converts a string literal to a pointer + // 4) if it converts a predefined value to a pointer Finder->addMatcher( implicitCastExpr( unless(hasParent(arraySubscriptExpr())), unless(hasParentIgnoringImpCasts(explicitCastExpr())), unless(isInsideOfRangeBeginEndStmt()), - unless(hasSourceExpression(ignoringParens(stringLiteral() + unless(hasSourceExpression(ignoringParens(stringLiteral(, + unless(hasSourceExpression(ignoringParens(predefinedExpr() .bind("cast"), this); } Index: test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp === --- test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp +++ test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp @@ -44,6 +44,9 @@ return ("clang"); // OK, ParenExpr hides the literal-pointer decay } +const char *line = __FILE__; // OK +const char *func = __FUNCTION__; // OK, predefined value to pointer + void f2(void *const *); void bug25362() { void *a[2]; Index: clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp === --- clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp +++ clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp @@ -56,12 +56,14 @@ // 1) just before array subscription // 2) inside a range-for over an array // 3) if it converts a string literal to a pointer + // 4) if it converts a predefined value to a pointer Finder->addMatcher( implicitCastExpr( unless(hasParent(arraySubscriptExpr())), unless(hasParentIgnoringImpCasts(explicitCastExpr())), unless(isInsideOfRangeBeginEndStmt()), - unless(hasSourceExpression(ignoringParens(stringLiteral() + unless(hasSourceExpression(ignoringParens(stringLiteral(, + unless(hasSourceExpression(ignoringParens(predefinedExpr() .bind("cast"), this); } ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D58764: [clang-tidy] ignore predefined expressions in cppcoreguidelines-pro-bounds-array-to-pointer-decay check
lewmpk added a comment. Okk, it seems that the consensus is that `__FUNCTION__` should not be cast to `char*`. ( I'll research a bit before I pick up a task in the future :) ) I'm happy to abandon this diff - any objections? Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58764/new/ https://reviews.llvm.org/D58764 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D57087: [clang-tidy] add OverrideMacro to modernize-use-override check
lewmpk added a comment. I'm happy to land this ASAP but I don't have commit rights CHANGES SINCE LAST ACTION https://reviews.llvm.org/D57087/new/ https://reviews.llvm.org/D57087 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D58731: [clang-tidy] added cppcoreguidelines-explicit-virtual-functions
lewmpk added a comment. I'm happy to land this ASAP but I don't have commit rights CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58731/new/ https://reviews.llvm.org/D58731 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk created this revision. lewmpk added reviewers: hokein, aaron.ballman, alexfh, JonasToth. Herald added subscribers: cfe-commits, jdoerfert, kbarton, xazax.hun, mgorny, nemanjai. Herald added a project: clang. lewmpk added a subscriber: clang-tools-extra. Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D58818 Files: clang-tidy/cppcoreguidelines/CMakeLists.txt clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst docs/clang-tidy/checks/list.rst test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp Index: test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp === --- /dev/null +++ test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp @@ -0,0 +1,31 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-use-raii-locks %t + +// Mock implementation of std::mutex +namespace std { +struct mutex { +void lock(); +void unlock(); +}; +} + +void warn_me() { +std::mutex m; +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); +} + +void ignore_me1() { +std::mutex m; +m.unlock(); +m.lock(); +// CHECK-MESSAGES-NOT: warning: +} + +void ignore_me2() { +std::mutex m1; +std::mutex m2; +m1.lock(); +m2.unlock(); +// CHECK-MESSAGES-NOT: warning: +} Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -118,6 +118,7 @@ cppcoreguidelines-pro-type-vararg cppcoreguidelines-slicing cppcoreguidelines-special-member-functions + cppcoreguidelines-use-raii-locks fuchsia-default-arguments fuchsia-header-anon-namespaces (redirects to google-build-namespaces) fuchsia-multiple-inheritance Index: docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst === --- /dev/null +++ docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst @@ -0,0 +1,8 @@ +.. title:: clang-tidy - cppcoreguidelines-use-raii-locks + +cppcoreguidelines-use-raii-locks + + +The explicit use of std::mutex functions ``lock()`` and ``unlock()`` is error +prone and should be avoided. It's recommended to use RAII-style locking +mechanisms such as ``std::lock_guard`` or ``std::unique_lock``. Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,11 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`cppcoreguidelines-use-raii-locks + ` check. + + Checks for explicit usage of ``std::mutex`` functions ``lock()`` and ``unlock``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name ` check. Index: clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h === --- /dev/null +++ clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h @@ -0,0 +1,34 @@ +//===--- UseRaiiLocksCheck.h - clang-tidy ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USERAIILOCKSCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USERAIILOCKSCHECK_H + +#include "../ClangTidy.h" +#include +namespace clang { +namespace tidy { +namespace cppcoreguidelines { + +/// Check for instances of explicit std::mutex lock() and unlock() calls +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-use-raii-locks.html +class UseRaiiLocksCheck : public ClangTidyCheck { +public: + UseRaiiLocksCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace cppcoreguidelines +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USERAIILOCKSCHECK_H Index: clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp === --- /dev/null +++ clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp @@ -0,0 +1,84 @@ +//===--- UseRaiiLocksCheck.cpp - clang-tidy ---===// +// +// Part of the LLVM Proj
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk updated this revision to Diff 188893. lewmpk added a comment. fixed ordering of cppcoreguidelines module checks Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58818/new/ https://reviews.llvm.org/D58818 Files: clang-tidy/cppcoreguidelines/CMakeLists.txt clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst docs/clang-tidy/checks/list.rst test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp Index: test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp === --- /dev/null +++ test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp @@ -0,0 +1,31 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-use-raii-locks %t + +// Mock implementation of std::mutex +namespace std { +struct mutex { +void lock(); +void unlock(); +}; +} + +void warn_me() { +std::mutex m; +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); +} + +void ignore_me1() { +std::mutex m; +m.unlock(); +m.lock(); +// CHECK-MESSAGES-NOT: warning: +} + +void ignore_me2() { +std::mutex m1; +std::mutex m2; +m1.lock(); +m2.unlock(); +// CHECK-MESSAGES-NOT: warning: +} Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -118,6 +118,7 @@ cppcoreguidelines-pro-type-vararg cppcoreguidelines-slicing cppcoreguidelines-special-member-functions + cppcoreguidelines-use-raii-locks fuchsia-default-arguments fuchsia-header-anon-namespaces (redirects to google-build-namespaces) fuchsia-multiple-inheritance Index: docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst === --- /dev/null +++ docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst @@ -0,0 +1,8 @@ +.. title:: clang-tidy - cppcoreguidelines-use-raii-locks + +cppcoreguidelines-use-raii-locks + + +The explicit use of std::mutex functions ``lock()`` and ``unlock()`` is error +prone and should be avoided. It's recommended to use RAII-style locking +mechanisms such as ``std::lock_guard`` or ``std::unique_lock``. Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,11 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`cppcoreguidelines-use-raii-locks + ` check. + + Checks for explicit usage of ``std::mutex`` functions ``lock()`` and ``unlock``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name ` check. Index: clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h === --- /dev/null +++ clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h @@ -0,0 +1,34 @@ +//===--- UseRaiiLocksCheck.h - clang-tidy ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USERAIILOCKSCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USERAIILOCKSCHECK_H + +#include "../ClangTidy.h" +#include +namespace clang { +namespace tidy { +namespace cppcoreguidelines { + +/// Check for instances of explicit std::mutex lock() and unlock() calls +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-use-raii-locks.html +class UseRaiiLocksCheck : public ClangTidyCheck { +public: + UseRaiiLocksCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace cppcoreguidelines +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USERAIILOCKSCHECK_H Index: clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp === --- /dev/null +++ clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp @@ -0,0 +1,84 @@ +//===--- UseRaiiLocksCheck.cpp - clang-tidy ---===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://ll
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk added a comment. I'll improve the tests :) Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58818/new/ https://reviews.llvm.org/D58818 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk updated this revision to Diff 188901. lewmpk added a comment. removed unneccesary include Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58818/new/ https://reviews.llvm.org/D58818 Files: clang-tidy/cppcoreguidelines/CMakeLists.txt clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst docs/clang-tidy/checks/list.rst test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp Index: test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp === --- /dev/null +++ test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp @@ -0,0 +1,80 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-use-raii-locks %t + +// Mock implementation of std::mutex +namespace std { +struct mutex { + void lock(); + void unlock(); +}; +} // namespace std + +void warn_me1() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me2() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + { +std::mutex m; +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); + } + m.unlock(); +} + +void warn_me3() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me4() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me5() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.unlock(); +} + +void ignore_me1() { + std::mutex m; + m.unlock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me2() { + std::mutex m1; + std::mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -118,6 +118,7 @@ cppcoreguidelines-pro-type-vararg cppcoreguidelines-slicing cppcoreguidelines-special-member-functions + cppcoreguidelines-use-raii-locks fuchsia-default-arguments fuchsia-header-anon-namespaces (redirects to google-build-namespaces) fuchsia-multiple-inheritance Index: docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst === --- /dev/null +++ docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst @@ -0,0 +1,8 @@ +.. title:: clang-tidy - cppcoreguidelines-use-raii-locks + +cppcoreguidelines-use-raii-locks + + +The explicit use of std::mutex functions ``lock()`` and ``unlock()`` is error +prone and should be avoided. It's recommended to use RAII-style locking +mechanisms such as ``std::lock_guard`` or ``std::unique_lock``. Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,11 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`cppcoreguidelines-use-raii-locks + ` check. + + Checks for explicit usage of ``std::mutex`` functions ``lock()`` and ``unlock``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name ` check. Index: clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h === --- /dev/null +++ clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h @@ -0,0 +1,34 @@ +//===--- UseRaiiLocksCheck.h - clang-tidy ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USERAIILOCKSCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USERAIILOCKSCHECK_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace cppcoreguidelines { + +/// Check for instances of explicit std::mutex lock() and unlock() calls +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-use-raii-lock
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk updated this revision to Diff 188900. lewmpk added a comment. fixed nested use case Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58818/new/ https://reviews.llvm.org/D58818 Files: clang-tidy/cppcoreguidelines/CMakeLists.txt clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst docs/clang-tidy/checks/list.rst test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp Index: test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp === --- /dev/null +++ test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp @@ -0,0 +1,80 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-use-raii-locks %t + +// Mock implementation of std::mutex +namespace std { +struct mutex { + void lock(); + void unlock(); +}; +} // namespace std + +void warn_me1() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me2() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + { +std::mutex m; +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); + } + m.unlock(); +} + +void warn_me3() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me4() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me5() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.unlock(); +} + +void ignore_me1() { + std::mutex m; + m.unlock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me2() { + std::mutex m1; + std::mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -118,6 +118,7 @@ cppcoreguidelines-pro-type-vararg cppcoreguidelines-slicing cppcoreguidelines-special-member-functions + cppcoreguidelines-use-raii-locks fuchsia-default-arguments fuchsia-header-anon-namespaces (redirects to google-build-namespaces) fuchsia-multiple-inheritance Index: docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst === --- /dev/null +++ docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst @@ -0,0 +1,8 @@ +.. title:: clang-tidy - cppcoreguidelines-use-raii-locks + +cppcoreguidelines-use-raii-locks + + +The explicit use of std::mutex functions ``lock()`` and ``unlock()`` is error +prone and should be avoided. It's recommended to use RAII-style locking +mechanisms such as ``std::lock_guard`` or ``std::unique_lock``. Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,11 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`cppcoreguidelines-use-raii-locks + ` check. + + Checks for explicit usage of ``std::mutex`` functions ``lock()`` and ``unlock``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name ` check. Index: clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h === --- /dev/null +++ clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h @@ -0,0 +1,34 @@ +//===--- UseRaiiLocksCheck.h - clang-tidy ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USERAIILOCKSCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USERAIILOCKSCHECK_H + +#include "../ClangTidy.h" +#include +namespace clang { +namespace tidy { +namespace cppcoreguidelines { + +/// Check for instances of explicit std::mutex lock() and unlock() calls +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-use-raii-l
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk updated this revision to Diff 188908. lewmpk added a comment. handle other basiclockable types Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58818/new/ https://reviews.llvm.org/D58818 Files: clang-tidy/cppcoreguidelines/CMakeLists.txt clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst docs/clang-tidy/checks/list.rst test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp Index: test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp === --- /dev/null +++ test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp @@ -0,0 +1,90 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-use-raii-locks %t + +// Mock implementation of std::mutex +namespace std { +struct mutex { + void lock(); + void unlock(); +}; +typedef mutex recursive_mutex; +} // namespace std + + +void warn_me1() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me2() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + { +std::mutex m; +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); + } + m.unlock(); +} + +void warn_me3() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me4() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me5() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.unlock(); +} + +void ignore_me1() { + std::mutex m; + m.unlock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me2() { + std::mutex m1; + std::mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} + +void ignore_me3() { + std::recursive_mutex m1; + std::recursive_mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -118,6 +118,7 @@ cppcoreguidelines-pro-type-vararg cppcoreguidelines-slicing cppcoreguidelines-special-member-functions + cppcoreguidelines-use-raii-locks fuchsia-default-arguments fuchsia-header-anon-namespaces (redirects to google-build-namespaces) fuchsia-multiple-inheritance Index: docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst === --- /dev/null +++ docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst @@ -0,0 +1,8 @@ +.. title:: clang-tidy - cppcoreguidelines-use-raii-locks + +cppcoreguidelines-use-raii-locks + + +The explicit use of std::mutex functions ``lock()`` and ``unlock()`` is error +prone and should be avoided. It's recommended to use RAII-style locking +mechanisms such as ``std::lock_guard`` or ``std::unique_lock``. Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,11 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`cppcoreguidelines-use-raii-locks + ` check. + + Checks for explicit usage of ``std::mutex`` functions ``lock()`` and ``unlock``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name ` check. Index: clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h === --- /dev/null +++ clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h @@ -0,0 +1,42 @@ +//===--- UseRaiiLocksCheck.h - clang-tidy ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USERAIILOCKSCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USERAIILOCKSCHECK_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace cppcoreguidelines { + +/// Check for inst
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk updated this revision to Diff 188910. lewmpk added a comment. renamed option for cppcoreguidelines-use-raii-locks Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58818/new/ https://reviews.llvm.org/D58818 Files: clang-tidy/cppcoreguidelines/CMakeLists.txt clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst docs/clang-tidy/checks/list.rst test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp Index: test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp === --- /dev/null +++ test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp @@ -0,0 +1,90 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-use-raii-locks %t + +// Mock implementation of std::mutex +namespace std { +struct mutex { + void lock(); + void unlock(); +}; +typedef mutex recursive_mutex; +} // namespace std + + +void warn_me1() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me2() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + { +std::mutex m; +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); + } + m.unlock(); +} + +void warn_me3() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me4() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me5() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.unlock(); +} + +void ignore_me1() { + std::mutex m; + m.unlock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me2() { + std::mutex m1; + std::mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} + +void ignore_me3() { + std::recursive_mutex m1; + std::recursive_mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -118,6 +118,7 @@ cppcoreguidelines-pro-type-vararg cppcoreguidelines-slicing cppcoreguidelines-special-member-functions + cppcoreguidelines-use-raii-locks fuchsia-default-arguments fuchsia-header-anon-namespaces (redirects to google-build-namespaces) fuchsia-multiple-inheritance Index: docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst === --- /dev/null +++ docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst @@ -0,0 +1,16 @@ +.. title:: clang-tidy - cppcoreguidelines-use-raii-locks + +cppcoreguidelines-use-raii-locks + + +The explicit use of std::mutex functions ``lock()`` and ``unlock()`` is error +prone and should be avoided. It's recommended to use RAII-style locking +mechanisms such as ``std::lock_guard`` or ``std::unique_lock``. + +Options +--- + +.. option:: LockableTypes + + Semicolon-separated list of fully qualified names of types that support + locking and unlocking a mutex; Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,11 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`cppcoreguidelines-use-raii-locks + ` check. + + Checks for explicit usage of ``std::mutex`` functions ``lock()`` and ``unlock``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name ` check. Index: clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h === --- /dev/null +++ clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h @@ -0,0 +1,42 @@ +//===--- UseRaiiLocksCheck.h - clang-tidy ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USERAIILOCKSCHECK_H +#define LLVM_CLAN
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk updated this revision to Diff 188911. lewmpk added a comment. fixed documentation Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58818/new/ https://reviews.llvm.org/D58818 Files: clang-tidy/cppcoreguidelines/CMakeLists.txt clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst docs/clang-tidy/checks/list.rst test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp Index: test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp === --- /dev/null +++ test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp @@ -0,0 +1,90 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-use-raii-locks %t + +// Mock implementation of std::mutex +namespace std { +struct mutex { + void lock(); + void unlock(); +}; +typedef mutex recursive_mutex; +} // namespace std + + +void warn_me1() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me2() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + { +std::mutex m; +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); + } + m.unlock(); +} + +void warn_me3() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me4() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me5() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.unlock(); +} + +void ignore_me1() { + std::mutex m; + m.unlock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me2() { + std::mutex m1; + std::mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} + +void ignore_me3() { + std::recursive_mutex m1; + std::recursive_mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -118,6 +118,7 @@ cppcoreguidelines-pro-type-vararg cppcoreguidelines-slicing cppcoreguidelines-special-member-functions + cppcoreguidelines-use-raii-locks fuchsia-default-arguments fuchsia-header-anon-namespaces (redirects to google-build-namespaces) fuchsia-multiple-inheritance Index: docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst === --- /dev/null +++ docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst @@ -0,0 +1,18 @@ +.. title:: clang-tidy - cppcoreguidelines-use-raii-locks + +cppcoreguidelines-use-raii-locks + + +The explicit use of std::mutex functions ``lock()`` and ``unlock()`` is error +prone and should be avoided. It's recommended to use RAII-style locking +mechanisms such as ``std::lock_guard`` or ``std::unique_lock``. + +Options +--- + +.. option:: LockableTypes + + Semicolon-separated list of fully qualified names of types that support + locking and unlocking a mutex. + Defaults to: ``::std::mutex;::std::recursive_mutex;::std::timed_mutex; + ::std::recursive_timed_mutex;::std::unique_lock``. Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,11 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`cppcoreguidelines-use-raii-locks + ` check. + + Checks for explicit usage of ``std::mutex`` functions ``lock()`` and ``unlock``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name ` check. Index: clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h === --- /dev/null +++ clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h @@ -0,0 +1,42 @@ +//===--- UseRaiiLocksCheck.h - clang-tidy ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk updated this revision to Diff 188912. lewmpk added a comment. fixed documentation again Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58818/new/ https://reviews.llvm.org/D58818 Files: clang-tidy/cppcoreguidelines/CMakeLists.txt clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst docs/clang-tidy/checks/list.rst test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp Index: test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp === --- /dev/null +++ test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp @@ -0,0 +1,90 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-use-raii-locks %t + +// Mock implementation of std::mutex +namespace std { +struct mutex { + void lock(); + void unlock(); +}; +typedef mutex recursive_mutex; +} // namespace std + + +void warn_me1() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me2() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + { +std::mutex m; +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); + } + m.unlock(); +} + +void warn_me3() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me4() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me5() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.unlock(); +} + +void ignore_me1() { + std::mutex m; + m.unlock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me2() { + std::mutex m1; + std::mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} + +void ignore_me3() { + std::recursive_mutex m1; + std::recursive_mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -118,6 +118,7 @@ cppcoreguidelines-pro-type-vararg cppcoreguidelines-slicing cppcoreguidelines-special-member-functions + cppcoreguidelines-use-raii-locks fuchsia-default-arguments fuchsia-header-anon-namespaces (redirects to google-build-namespaces) fuchsia-multiple-inheritance Index: docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst === --- /dev/null +++ docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst @@ -0,0 +1,18 @@ +.. title:: clang-tidy - cppcoreguidelines-use-raii-locks + +cppcoreguidelines-use-raii-locks + + +The explicit use of ``std::mutex`` functions ``lock()``, ``try_lock()`` and ``unlock()`` is error +prone and should be avoided. It's recommended to use RAII-style locking +mechanisms such as ``std::lock_guard`` or ``std::unique_lock``. + +Options +--- + +.. option:: LockableTypes + + Semicolon-separated list of fully qualified names of types that support + locking and unlocking a mutex. + Defaults to: ``::std::mutex;::std::recursive_mutex;::std::timed_mutex; + ::std::recursive_timed_mutex;::std::unique_lock``. Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,12 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`cppcoreguidelines-use-raii-locks + ` check. + + Checks for explicit usage of``std::mutex`` functions ``lock()``, + ``try_lock()`` and ``unlock()``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name ` check. Index: clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h === --- /dev/null +++ clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h @@ -0,0 +1,42 @@ +//===--- UseRaiiLocksCheck.h - clang-tidy ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk updated this revision to Diff 188913. lewmpk added a comment. fixed documentation formatting Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58818/new/ https://reviews.llvm.org/D58818 Files: clang-tidy/cppcoreguidelines/CMakeLists.txt clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst docs/clang-tidy/checks/list.rst test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp Index: test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp === --- /dev/null +++ test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp @@ -0,0 +1,90 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-use-raii-locks %t + +// Mock implementation of std::mutex +namespace std { +struct mutex { + void lock(); + void unlock(); +}; +typedef mutex recursive_mutex; +} // namespace std + + +void warn_me1() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me2() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + { +std::mutex m; +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); + } + m.unlock(); +} + +void warn_me3() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me4() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me5() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.unlock(); +} + +void ignore_me1() { + std::mutex m; + m.unlock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me2() { + std::mutex m1; + std::mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} + +void ignore_me3() { + std::recursive_mutex m1; + std::recursive_mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -118,6 +118,7 @@ cppcoreguidelines-pro-type-vararg cppcoreguidelines-slicing cppcoreguidelines-special-member-functions + cppcoreguidelines-use-raii-locks fuchsia-default-arguments fuchsia-header-anon-namespaces (redirects to google-build-namespaces) fuchsia-multiple-inheritance Index: docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst === --- /dev/null +++ docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst @@ -0,0 +1,19 @@ +.. title:: clang-tidy - cppcoreguidelines-use-raii-locks + +cppcoreguidelines-use-raii-locks + + +The explicit use of ``std::mutex`` functions ``lock()``, ``try_lock()`` and +``unlock()`` is error prone and should be avoided. +It's recommended to use RAII-style locking mechanisms such as +``std::lock_guard`` or ``std::unique_lock``. + +Options +--- + +.. option:: LockableTypes + + Semicolon-separated list of fully qualified names of types that support + locking and unlocking a mutex. + Defaults to: ``::std::mutex;::std::recursive_mutex;::std::timed_mutex; + ::std::recursive_timed_mutex;::std::unique_lock``. Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,12 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`cppcoreguidelines-use-raii-locks + ` check. + + Checks for explicit usage of``std::mutex`` functions ``lock()``, + ``try_lock()`` and ``unlock()``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name ` check. Index: clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h === --- /dev/null +++ clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h @@ -0,0 +1,42 @@ +//===--- UseRaiiLocksCheck.h - clang-tidy ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk marked 3 inline comments as done. lewmpk added inline comments. Comment at: clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp:26 + recordType(hasDeclaration(cxxRecordDecl(hasName("::std::mutex")); + // Match expressions of type mutex or mutex pointer + const auto MutexExpr = lebedev.ri wrote: > Terminology: *this* doesn't match anything. > It's a matcher, yes, but it's just a lambda. > The actual match happens at the end. I was trying to describe its intent, not its action. Does anyone have any suggestions for a clearer comment? Comment at: clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h:13-14 +#include "../ClangTidy.h" +#include +namespace clang { +namespace tidy { lebedev.ri wrote: > Separate with newline it seems that most checks are this way. it was autogenerated by the ``add_new_check.py`` script. Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58818/new/ https://reviews.llvm.org/D58818 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk updated this revision to Diff 188915. lewmpk added a comment. fixed documentation formatting Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58818/new/ https://reviews.llvm.org/D58818 Files: clang-tidy/cppcoreguidelines/CMakeLists.txt clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst docs/clang-tidy/checks/list.rst test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp Index: test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp === --- /dev/null +++ test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp @@ -0,0 +1,90 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-use-raii-locks %t + +// Mock implementation of std::mutex +namespace std { +struct mutex { + void lock(); + void unlock(); +}; +typedef mutex recursive_mutex; +} // namespace std + + +void warn_me1() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me2() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + { +std::mutex m; +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); + } + m.unlock(); +} + +void warn_me3() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me4() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me5() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.unlock(); +} + +void ignore_me1() { + std::mutex m; + m.unlock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me2() { + std::mutex m1; + std::mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} + +void ignore_me3() { + std::recursive_mutex m1; + std::recursive_mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -118,6 +118,7 @@ cppcoreguidelines-pro-type-vararg cppcoreguidelines-slicing cppcoreguidelines-special-member-functions + cppcoreguidelines-use-raii-locks fuchsia-default-arguments fuchsia-header-anon-namespaces (redirects to google-build-namespaces) fuchsia-multiple-inheritance Index: docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst === --- /dev/null +++ docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst @@ -0,0 +1,19 @@ +.. title:: clang-tidy - cppcoreguidelines-use-raii-locks + +cppcoreguidelines-use-raii-locks + + +The explicit use of ``std::mutex`` functions ``lock()``, ``try_lock()`` and +``unlock()`` is error prone and should be avoided. +It's recommended to use RAII-style locking mechanisms such as +``std::lock_guard`` or ``std::unique_lock``. + +Options +--- + +.. option:: LockableTypes + + Semicolon-separated list of fully qualified names of types that support + locking and unlocking a mutex. + Defaults to: ``::std::mutex;::std::recursive_mutex;::std::timed_mutex; + ::std::recursive_timed_mutex;::std::unique_lock``. Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,12 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`cppcoreguidelines-use-raii-locks + ` check. + + Checks for explicit usage of``std::mutex`` functions ``lock()``, + ``try_lock()`` and ``unlock()``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name ` check. Index: clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h === --- /dev/null +++ clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h @@ -0,0 +1,42 @@ +//===--- UseRaiiLocksCheck.h - clang-tidy ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===-
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk updated this revision to Diff 188918. lewmpk added a comment. improved tests Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58818/new/ https://reviews.llvm.org/D58818 Files: clang-tidy/cppcoreguidelines/CMakeLists.txt clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst docs/clang-tidy/checks/list.rst test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp Index: test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp === --- /dev/null +++ test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp @@ -0,0 +1,92 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-use-raii-locks %t + +// Mock implementation of std::mutex +namespace std { +struct mutex { + void lock(); + void unlock(); +}; +typedef mutex recursive_mutex; +} // namespace std + +void warn_me1() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me2() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + { +std::mutex m; +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); + } + m.unlock(); +} + +void warn_me3() { + std::mutex m1; + std::mutex m2; + { +m1.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +{ + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use RAII + m2.unlock(); +} +m1.unlock(); + } +} + +void warn_me4() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me5() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.unlock(); +} + +void ignore_me1() { + std::mutex m; + m.unlock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me2() { + std::mutex m1; + std::mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} + +void ignore_me3() { + std::recursive_mutex m1; + std::recursive_mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -118,6 +118,7 @@ cppcoreguidelines-pro-type-vararg cppcoreguidelines-slicing cppcoreguidelines-special-member-functions + cppcoreguidelines-use-raii-locks fuchsia-default-arguments fuchsia-header-anon-namespaces (redirects to google-build-namespaces) fuchsia-multiple-inheritance Index: docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst === --- /dev/null +++ docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst @@ -0,0 +1,19 @@ +.. title:: clang-tidy - cppcoreguidelines-use-raii-locks + +cppcoreguidelines-use-raii-locks + + +The explicit use of ``std::mutex`` functions ``lock()``, ``try_lock()`` and +``unlock()`` is error prone and should be avoided. +It's recommended to use RAII-style locking mechanisms such as +``std::lock_guard`` or ``std::unique_lock``. + +Options +--- + +.. option:: LockableTypes + + Semicolon-separated list of fully qualified names of types that support + locking and unlocking a mutex. + Defaults to: ``::std::mutex;::std::recursive_mutex;::std::timed_mutex; + ::std::recursive_timed_mutex;::std::unique_lock``. Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,12 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`cppcoreguidelines-use-raii-locks + ` check. + + Checks for explicit usage of``std::mutex`` functions ``lock()``, + ``try_lock()`` and ``unlock()``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name ` check. Index: clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h === --- /dev/null +++ clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h @@ -0,0 +1,42 @@ +//===--- UseRaiiLocksCheck.h - clang-tidy ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===---
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk updated this revision to Diff 188919. lewmpk added a comment. support try_lock_for and try_lock_until Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58818/new/ https://reviews.llvm.org/D58818 Files: clang-tidy/cppcoreguidelines/CMakeLists.txt clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst docs/clang-tidy/checks/list.rst test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp Index: test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp === --- /dev/null +++ test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp @@ -0,0 +1,92 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-use-raii-locks %t + +// Mock implementation of std::mutex +namespace std { +struct mutex { + void lock(); + void unlock(); +}; +typedef mutex recursive_mutex; +} // namespace std + +void warn_me1() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me2() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + { +std::mutex m; +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); + } + m.unlock(); +} + +void warn_me3() { + std::mutex m1; + std::mutex m2; + { +m1.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +{ + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use RAII + m2.unlock(); +} +m1.unlock(); + } +} + +void warn_me4() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me5() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.unlock(); +} + +void ignore_me1() { + std::mutex m; + m.unlock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me2() { + std::mutex m1; + std::mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} + +void ignore_me3() { + std::recursive_mutex m1; + std::recursive_mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -118,6 +118,7 @@ cppcoreguidelines-pro-type-vararg cppcoreguidelines-slicing cppcoreguidelines-special-member-functions + cppcoreguidelines-use-raii-locks fuchsia-default-arguments fuchsia-header-anon-namespaces (redirects to google-build-namespaces) fuchsia-multiple-inheritance Index: docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst === --- /dev/null +++ docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst @@ -0,0 +1,19 @@ +.. title:: clang-tidy - cppcoreguidelines-use-raii-locks + +cppcoreguidelines-use-raii-locks + + +The explicit use of ``std::mutex`` functions ``lock()``, ``try_lock()`` and +``unlock()`` is error prone and should be avoided. +It's recommended to use RAII-style locking mechanisms such as +``std::lock_guard`` or ``std::unique_lock``. + +Options +--- + +.. option:: LockableTypes + + Semicolon-separated list of fully qualified names of types that support + locking and unlocking a mutex. + Defaults to: ``::std::mutex;::std::recursive_mutex;::std::timed_mutex; + ::std::recursive_timed_mutex;::std::unique_lock``. Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,12 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`cppcoreguidelines-use-raii-locks + ` check. + + Checks for explicit usage of``std::mutex`` functions ``lock()``, + ``try_lock()`` and ``unlock()``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name ` check. Index: clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h === --- /dev/null +++ clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h @@ -0,0 +1,42 @@ +//===--- UseRaiiLocksCheck.h - clang-tidy ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLV
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk updated this revision to Diff 188920. lewmpk added a comment. remove support for try_lock_for and try_lock_until Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58818/new/ https://reviews.llvm.org/D58818 Files: clang-tidy/cppcoreguidelines/CMakeLists.txt clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst docs/clang-tidy/checks/list.rst test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp Index: test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp === --- /dev/null +++ test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp @@ -0,0 +1,92 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-use-raii-locks %t + +// Mock implementation of std::mutex +namespace std { +struct mutex { + void lock(); + void unlock(); +}; +typedef mutex recursive_mutex; +} // namespace std + +void warn_me1() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me2() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + { +std::mutex m; +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); + } + m.unlock(); +} + +void warn_me3() { + std::mutex m1; + std::mutex m2; + { +m1.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +{ + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use RAII + m2.unlock(); +} +m1.unlock(); + } +} + +void warn_me4() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me5() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.unlock(); +} + +void ignore_me1() { + std::mutex m; + m.unlock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me2() { + std::mutex m1; + std::mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} + +void ignore_me3() { + std::recursive_mutex m1; + std::recursive_mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -118,6 +118,7 @@ cppcoreguidelines-pro-type-vararg cppcoreguidelines-slicing cppcoreguidelines-special-member-functions + cppcoreguidelines-use-raii-locks fuchsia-default-arguments fuchsia-header-anon-namespaces (redirects to google-build-namespaces) fuchsia-multiple-inheritance Index: docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst === --- /dev/null +++ docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst @@ -0,0 +1,19 @@ +.. title:: clang-tidy - cppcoreguidelines-use-raii-locks + +cppcoreguidelines-use-raii-locks + + +The explicit use of ``std::mutex`` functions ``lock()``, ``try_lock()`` and +``unlock()`` is error prone and should be avoided. +It's recommended to use RAII-style locking mechanisms such as +``std::lock_guard`` or ``std::unique_lock``. + +Options +--- + +.. option:: LockableTypes + + Semicolon-separated list of fully qualified names of types that support + locking and unlocking a mutex. + Defaults to: ``::std::mutex;::std::recursive_mutex;::std::timed_mutex; + ::std::recursive_timed_mutex;::std::unique_lock``. Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -91,6 +91,12 @@ Finds and fixes ``absl::Time`` subtraction expressions to do subtraction in the Time domain instead of the numeric domain. +- New :doc:`cppcoreguidelines-use-raii-locks + ` check. + + Checks for explicit usage of``std::mutex`` functions ``lock()``, + ``try_lock()`` and ``unlock()``. + - New :doc:`google-readability-avoid-underscore-in-googletest-name ` check. Index: clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h === --- /dev/null +++ clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h @@ -0,0 +1,42 @@ +//===--- UseRaiiLocksCheck.h - clang-tidy ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk updated this revision to Diff 189054. lewmpk added a comment. refactor and improved tests Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58818/new/ https://reviews.llvm.org/D58818 Files: clang-tidy/cppcoreguidelines/CMakeLists.txt clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst docs/clang-tidy/checks/list.rst test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp Index: test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp === --- /dev/null +++ test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp @@ -0,0 +1,194 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-use-raii-locks %t + +// Mock implementation of std::mutex +namespace std { +struct mutex { + void lock(); + void try_lock(); + void unlock(); +}; +typedef mutex recursive_mutex; +} // namespace std + +void warn_me1_simple() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me2_nested() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + { +std::mutex m; +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); + } + m.unlock(); +} + +void warn_me3_nested_extra() { + std::mutex m1; + std::mutex m2; + { +m1.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +{ + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use RAII + m2.unlock(); +} +m1.unlock(); + } +} + +void warn_me4_multi_mutex() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me5_multi_mutex_extra() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.unlock(); +} + +void warn_me6() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); + m.try_lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me7_loop() { + std::mutex m; + for (auto i = 0; i < 3; i++) { +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); + } +} + +template +void attempt(M m) { + m.lock(); + // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me8_template_func() { + std::mutex m; + attempt(m); +} + +// clang-format off +#define ATTEMPT(m) {\ +m.lock();\ +m.unlock();\ + } +// clang-format on + +void warn_me9_macro() { + std::mutex m; + ATTEMPT(m); + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use RAII +} + +void warn_me10_inline() { + std::mutex m; + // clang-format off + m.lock(); m.unlock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + // clang-format on +} + +void warn_me11_recursive_mutex() { + std::recursive_mutex m; + m.lock(); + // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void ignore_me1_rev_order() { + std::mutex m; + m.unlock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me2_diff_mtx() { + std::mutex m1; + std::mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} + +void ignore_me3() { + std::recursive_mutex m1; + std::recursive_mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} + +void ignore_me4() { + std::mutex m; + m.unlock(); + m.lock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me5_inline() { + std::mutex m; + m.unlock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me6_seperate_scopes() { + std::mutex m; + { +m.lock(); +// CHECK-MESSAGES-NOT: warning: + } + { +m.unlock(); + } +} + +void ignore_me7_seperate_scopes_nested() { + std::mutex m; + { +m.lock(); +// CHECK-MESSAGES-NOT: warning: +{ + m.unlock(); +} + } +} Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -118,6 +118,7 @@ cppcoreguidelines-pro-type-vararg cppcoreguidelines-slicing cppcoreguidelines-special-member-functions + cppcoreguidelines-use-raii-locks fuchsia-default-arguments fuchsia-header-anon-namespaces (redirects to google-build-namespaces) fuchsia-multiple-inherit
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk updated this revision to Diff 189055. lewmpk added a comment. added support for boost lockable types Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58818/new/ https://reviews.llvm.org/D58818 Files: clang-tidy/cppcoreguidelines/CMakeLists.txt clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst docs/clang-tidy/checks/list.rst test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp Index: test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp === --- /dev/null +++ test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp @@ -0,0 +1,194 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-use-raii-locks %t + +// Mock implementation of std::mutex +namespace std { +struct mutex { + void lock(); + void try_lock(); + void unlock(); +}; +typedef mutex recursive_mutex; +} // namespace std + +void warn_me1_simple() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me2_nested() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + { +std::mutex m; +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); + } + m.unlock(); +} + +void warn_me3_nested_extra() { + std::mutex m1; + std::mutex m2; + { +m1.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +{ + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use RAII + m2.unlock(); +} +m1.unlock(); + } +} + +void warn_me4_multi_mutex() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me5_multi_mutex_extra() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.unlock(); +} + +void warn_me6() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); + m.try_lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me7_loop() { + std::mutex m; + for (auto i = 0; i < 3; i++) { +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); + } +} + +template +void attempt(M m) { + m.lock(); + // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me8_template_func() { + std::mutex m; + attempt(m); +} + +// clang-format off +#define ATTEMPT(m) {\ +m.lock();\ +m.unlock();\ + } +// clang-format on + +void warn_me9_macro() { + std::mutex m; + ATTEMPT(m); + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use RAII +} + +void warn_me10_inline() { + std::mutex m; + // clang-format off + m.lock(); m.unlock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + // clang-format on +} + +void warn_me11_recursive_mutex() { + std::recursive_mutex m; + m.lock(); + // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void ignore_me1_rev_order() { + std::mutex m; + m.unlock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me2_diff_mtx() { + std::mutex m1; + std::mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} + +void ignore_me3() { + std::recursive_mutex m1; + std::recursive_mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} + +void ignore_me4() { + std::mutex m; + m.unlock(); + m.lock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me5_inline() { + std::mutex m; + m.unlock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me6_seperate_scopes() { + std::mutex m; + { +m.lock(); +// CHECK-MESSAGES-NOT: warning: + } + { +m.unlock(); + } +} + +void ignore_me7_seperate_scopes_nested() { + std::mutex m; + { +m.lock(); +// CHECK-MESSAGES-NOT: warning: +{ + m.unlock(); +} + } +} Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -118,6 +118,7 @@ cppcoreguidelines-pro-type-vararg cppcoreguidelines-slicing cppcoreguidelines-special-member-functions + cppcoreguidelines-use-raii-locks fuchsia-default-arguments fuchsia-header-anon-namespaces (redirects to google-build-namespaces) fuchsia-multi
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk marked 7 inline comments as done. lewmpk added inline comments. Comment at: test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp:4 +// Mock implementation of std::mutex +namespace std { +struct mutex { JonasToth wrote: > Please add more tests > > What happens for this? > ``` > void foo() { > std::mutex m; > m.lock(); > m.unlock(); > m.lock(); > m.unlock(); > m.try_lock(); > m.lock(); > m.unlock(); > } > ``` > > - Please add tests for templates, where the lock-type is a template parameter > - please add tests where the locking happens within macros > - please add tests for usage within loops > - where cases like `std::mutex m1; std::mutex &m2 = m1; // usage`. This > should not be diagnosed, right? I've added a test case for your example, templates, macros and loops. I can't catch the case `std::mutex m1; std::mutex &m2 = m1; // usage`, but i can catch trivial cases. Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58818/new/ https://reviews.llvm.org/D58818 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk updated this revision to Diff 189078. lewmpk marked an inline comment as done. lewmpk added a comment. added example in docs and explicitly specified types for some variables Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58818/new/ https://reviews.llvm.org/D58818 Files: clang-tidy/cppcoreguidelines/CMakeLists.txt clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst docs/clang-tidy/checks/list.rst test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp Index: test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp === --- /dev/null +++ test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp @@ -0,0 +1,194 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-use-raii-locks %t + +// Mock implementation of std::mutex +namespace std { +struct mutex { + void lock(); + void try_lock(); + void unlock(); +}; +typedef mutex recursive_mutex; +} // namespace std + +void warn_me1_simple() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me2_nested() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + { +std::mutex m; +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); + } + m.unlock(); +} + +void warn_me3_nested_extra() { + std::mutex m1; + std::mutex m2; + { +m1.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +{ + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use RAII + m2.unlock(); +} +m1.unlock(); + } +} + +void warn_me4_multi_mutex() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me5_multi_mutex_extra() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.unlock(); +} + +void warn_me6() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); + m.try_lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me7_loop() { + std::mutex m; + for (auto i = 0; i < 3; i++) { +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); + } +} + +template +void attempt(M m) { + m.lock(); + // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me8_template_func() { + std::mutex m; + attempt(m); +} + +// clang-format off +#define ATTEMPT(m) {\ +m.lock();\ +m.unlock();\ + } +// clang-format on + +void warn_me9_macro() { + std::mutex m; + ATTEMPT(m); + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use RAII +} + +void warn_me10_inline() { + std::mutex m; + // clang-format off + m.lock(); m.unlock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + // clang-format on +} + +void warn_me11_recursive_mutex() { + std::recursive_mutex m; + m.lock(); + // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void ignore_me1_rev_order() { + std::mutex m; + m.unlock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me2_diff_mtx() { + std::mutex m1; + std::mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} + +void ignore_me3() { + std::recursive_mutex m1; + std::recursive_mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} + +void ignore_me4() { + std::mutex m; + m.unlock(); + m.lock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me5_inline() { + std::mutex m; + m.unlock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me6_seperate_scopes() { + std::mutex m; + { +m.lock(); +// CHECK-MESSAGES-NOT: warning: + } + { +m.unlock(); + } +} + +void ignore_me7_seperate_scopes_nested() { + std::mutex m; + { +m.lock(); +// CHECK-MESSAGES-NOT: warning: +{ + m.unlock(); +} + } +} Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -118,6 +118,7 @@ cppcoreguidelines-pro-type-vararg cppcoreguidelines-slicing cppcoreguidelines-special-member-functions + cppcoreguidelines-use-raii-locks fuchsia-default-arguments fuchsia-header
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk marked an inline comment as done. lewmpk added inline comments. Comment at: test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp:90 + for (auto i = 0; i < 3; i++) { +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII JonasToth wrote: > I got another one that I think defeats the analysis: > > ``` > while (true) > { > > my_label: > m.lock(); > > if (some_condition) > goto my_label; > > m.unlock(); > } > ``` > Usage of `goto` can interfer and make the situation more complicated. I am > not asking you to implement that correctly (not sure if even possible with > pure clang-tidy) but I think a pathological example should be documented for > the user. why would this defeat the analysis? Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58818/new/ https://reviews.llvm.org/D58818 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D58818: [clang-tidy] added cppcoreguidelines-use-raii-locks check
lewmpk updated this revision to Diff 189157. lewmpk added a comment. match lock() and unlock() calls by decl Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58818/new/ https://reviews.llvm.org/D58818 Files: clang-tidy/cppcoreguidelines/CMakeLists.txt clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.cpp clang-tidy/cppcoreguidelines/UseRaiiLocksCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/cppcoreguidelines-use-raii-locks.rst docs/clang-tidy/checks/list.rst test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp Index: test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp === --- /dev/null +++ test/clang-tidy/cppcoreguidelines-use-raii-locks.cpp @@ -0,0 +1,194 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-use-raii-locks %t + +// Mock implementation of std::mutex +namespace std { +struct mutex { + void lock(); + void try_lock(); + void unlock(); +}; +typedef mutex recursive_mutex; +} // namespace std + +void warn_me1_simple() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me2_nested() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + { +std::mutex m; +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); + } + m.unlock(); +} + +void warn_me3_nested_extra() { + std::mutex m1; + std::mutex m2; + { +m1.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +{ + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use RAII + m2.unlock(); +} +m1.unlock(); + } +} + +void warn_me4_multi_mutex() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.unlock(); +} + +void warn_me5_multi_mutex_extra() { + std::mutex m1; + std::mutex m2; + + m1.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m2.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m1.unlock(); + m2.unlock(); +} + +void warn_me6() { + std::mutex m; + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); + m.try_lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.lock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me7_loop() { + std::mutex m; + for (auto i = 0; i < 3; i++) { +m.lock(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use RAII +m.unlock(); + } +} + +template +void attempt(M m) { + m.lock(); + // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void warn_me8_template_func() { + std::mutex m; + attempt(m); +} + +// clang-format off +#define ATTEMPT(m) {\ +m.lock();\ +m.unlock();\ + } +// clang-format on + +void warn_me9_macro() { + std::mutex m; + ATTEMPT(m); + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use RAII +} + +void warn_me10_inline() { + std::mutex m; + // clang-format off + m.lock(); m.unlock(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use RAII + // clang-format on +} + +void warn_me11_recursive_mutex() { + std::recursive_mutex m; + m.lock(); + // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use RAII + m.unlock(); +} + +void ignore_me1_rev_order() { + std::mutex m; + m.unlock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me2_diff_mtx() { + std::mutex m1; + std::mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} + +void ignore_me3() { + std::recursive_mutex m1; + std::recursive_mutex m2; + m1.lock(); + // CHECK-MESSAGES-NOT: warning: + m2.unlock(); +} + +void ignore_me4() { + std::mutex m; + m.unlock(); + m.lock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me5_inline() { + std::mutex m; + m.unlock(); + m.lock(); + // CHECK-MESSAGES-NOT: warning: +} + +void ignore_me6_seperate_scopes() { + std::mutex m; + { +m.lock(); +// CHECK-MESSAGES-NOT: warning: + } + { +m.unlock(); + } +} + +void ignore_me7_seperate_scopes_nested() { + std::mutex m; + { +m.lock(); +// CHECK-MESSAGES-NOT: warning: +{ + m.unlock(); +} + } +} Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -118,6 +118,7 @@ cppcoreguidelines-pro-type-vararg cppcoreguidelines-slicing cppcoreguidelines-special-member-functions + cppcoreguidelines-use-raii-locks fuchsia-default-arguments fuchsia-header-anon-namespaces (redirects to google-build-namespaces) fuchsia-mult