Index: clang-tools-extra/clang-tidy/misc/CMakeLists.txt =================================================================== --- clang-tools-extra/clang-tidy/misc/CMakeLists.txt +++ clang-tools-extra/clang-tidy/misc/CMakeLists.txt @@ -2,6 +2,7 @@ add_clang_library(clangTidyMiscModule DefinitionsInHeadersCheck.cpp + DispatchOnceAssignmentCheck.cpp MiscTidyModule.cpp MisplacedConstCheck.cpp NewDeleteOverloadsCheck.cpp Index: clang-tools-extra/clang-tidy/misc/DispatchOnceAssignmentCheck.h =================================================================== --- /dev/null +++ clang-tools-extra/clang-tidy/misc/DispatchOnceAssignmentCheck.h @@ -0,0 +1,36 @@ +//===--- DispatchOnceAssignmentCheck.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_MISC_DISPATCHONCEASSIGNMENTCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_DISPATCHONCEASSIGNMENTCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang { +namespace tidy { +namespace misc { + +/// Finds explicit assignment to variables of type dispatch_once_t, which should +/// not be assigned values directly in application code. +/// +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/misc-dispatch-once-assignment.html +class DispatchOnceAssignmentCheck : public ClangTidyCheck { +public: + DispatchOnceAssignmentCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace misc +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_DISPATCHONCEASSIGNMENTCHECK_H Index: clang-tools-extra/clang-tidy/misc/DispatchOnceAssignmentCheck.cpp =================================================================== --- /dev/null +++ clang-tools-extra/clang-tidy/misc/DispatchOnceAssignmentCheck.cpp @@ -0,0 +1,40 @@ +//===--- DispatchOnceAssignmentCheck.cpp - clang-tidy ---------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "DispatchOnceAssignmentCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Expr.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace misc { + +void DispatchOnceAssignmentCheck::registerMatchers(MatchFinder *Finder) { + // Find assignments where the LHS has type dispatch_once_t. + Finder->addMatcher( + binaryOperator(isAssignmentOperator(), + hasLHS(hasType(asString("dispatch_once_t")))) + .bind("assignment"), + this); +} + +void DispatchOnceAssignmentCheck::check( + const MatchFinder::MatchResult &Result) { + if (const auto *Op = Result.Nodes.getNodeAs("assignment")) { + diag(Op->getExprLoc(), + "do not assign to dispatch_once_t variables"); + } +} + +} // namespace misc +} // namespace tidy +} // namespace clang Index: clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp =================================================================== --- clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp +++ clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp @@ -10,6 +10,7 @@ #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" #include "DefinitionsInHeadersCheck.h" +#include "DispatchOnceAssignmentCheck.h" #include "MisplacedConstCheck.h" #include "NewDeleteOverloadsCheck.h" #include "NonCopyableObjects.h" @@ -32,6 +33,8 @@ void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck( "misc-definitions-in-headers"); + CheckFactories.registerCheck( + "misc-dispatch-once-assignment"); CheckFactories.registerCheck("misc-misplaced-const"); CheckFactories.registerCheck( "misc-new-delete-overloads"); Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -73,6 +73,11 @@ Finds instances where variables with static storage are initialized dynamically in header files. +- New :doc:`misc-dispatch-once-assignment + ` check. + + Finds instances of explicitly writing to ``dispatch_once_t`` variables. + - New :doc:`linuxkernel-must-use-errs ` check. Index: clang-tools-extra/docs/clang-tidy/checks/list.rst =================================================================== --- clang-tools-extra/docs/clang-tidy/checks/list.rst +++ clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -278,6 +278,7 @@ llvm-prefer-register-over-unsigned llvm-twine-local misc-definitions-in-headers + misc-dispatch-once-assignment misc-misplaced-const misc-new-delete-overloads misc-non-copyable-objects Index: clang-tools-extra/docs/clang-tidy/checks/misc-dispatch-once-assignment.rst =================================================================== --- /dev/null +++ clang-tools-extra/docs/clang-tidy/checks/misc-dispatch-once-assignment.rst @@ -0,0 +1,9 @@ +.. title:: clang-tidy - misc-dispatch-once-assignment + +misc-dispatch-once-assignment +============================= + +Finds assignments to variables of type ``dispatch_once_t`` and warns to avoid +assigning to them. Libdispatch intends that such writes should be guarded +and making such direct writes may potentially violate the run-once protections +intended by the library. Index: clang-tools-extra/test/clang-tidy/misc-dispatch-once-assignment.cpp =================================================================== --- /dev/null +++ clang-tools-extra/test/clang-tidy/misc-dispatch-once-assignment.cpp @@ -0,0 +1,15 @@ +// RUN: %check_clang_tidy %s misc-dispatch-once-assignment %t + +typedef int dispatch_once_t; +extern void dispatch_once(dispatch_once_t *pred, void(^block)(void)); + +static dispatch_once_t onceToken; + +void DoOnce(void(^block)(void)) { + dispatch_once(&onceToken, block); +} + +void ResetOnce() { + onceToken = 0; + // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not assign to dispatch_once_t variables [misc-dispatch-once-assignment] +}