Index: clang-tools-extra/clang-tidy/abseil/AbseilTidyModule.cpp =================================================================== --- clang-tools-extra/clang-tidy/abseil/AbseilTidyModule.cpp +++ clang-tools-extra/clang-tidy/abseil/AbseilTidyModule.cpp @@ -20,6 +20,7 @@ #include "FasterStrsplitDelimiterCheck.h" #include "NoInternalDependenciesCheck.h" #include "NoNamespaceCheck.h" +#include "PrefixedThreadAnnotationsCheck.h" #include "RedundantStrcatCallsCheck.h" #include "StringFindStartswithCheck.h" #include "StrCatAppendCheck.h" @@ -55,6 +56,8 @@ CheckFactories.registerCheck( "abseil-no-internal-dependencies"); CheckFactories.registerCheck("abseil-no-namespace"); + CheckFactories.registerCheck( + "abseil-prefixed-thread-annotations"); CheckFactories.registerCheck( "abseil-redundant-strcat-calls"); CheckFactories.registerCheck( Index: clang-tools-extra/clang-tidy/abseil/CMakeLists.txt =================================================================== --- clang-tools-extra/clang-tidy/abseil/CMakeLists.txt +++ clang-tools-extra/clang-tidy/abseil/CMakeLists.txt @@ -14,6 +14,7 @@ FasterStrsplitDelimiterCheck.cpp NoInternalDependenciesCheck.cpp NoNamespaceCheck.cpp + PrefixedThreadAnnotationsCheck.cpp RedundantStrcatCallsCheck.cpp StrCatAppendCheck.cpp StringFindStartswithCheck.cpp Index: clang-tools-extra/clang-tidy/abseil/PrefixedThreadAnnotationsCheck.h =================================================================== --- /dev/null +++ clang-tools-extra/clang-tidy/abseil/PrefixedThreadAnnotationsCheck.h @@ -0,0 +1,37 @@ +//===--- PrefixedThreadAnnotationsCheck.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_ABSEIL_PREFIXEDTHREADANNOTATIONSCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_PREFIXEDTHREADANNOTATIONSCHECK_H + +#include "../ClangTidyCheck.h" +#include "clang/Lex/PPCallbacks.h" + +namespace clang { +namespace tidy { +namespace abseil { + +/// This check finds usages of deprecated Abseil thread annotation macros and +/// migrates them to the new macros that are prefixed with `ABSL_`. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/abseil-prefixed-thread-annotations.html +class PrefixedThreadAnnotationsCheck : public ClangTidyCheck { + public: + PrefixedThreadAnnotationsCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + + void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, + Preprocessor *ModuleExpanderPP) override; +}; + +} // namespace abseil +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_PREFIXEDTHREADANNOTATIONSCHECK_H Index: clang-tools-extra/clang-tidy/abseil/PrefixedThreadAnnotationsCheck.cpp =================================================================== --- /dev/null +++ clang-tools-extra/clang-tidy/abseil/PrefixedThreadAnnotationsCheck.cpp @@ -0,0 +1,58 @@ +//===--- PrefixedThreadAnnotationsCheck.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 "PrefixedThreadAnnotationsCheck.h" + +namespace clang { +namespace tidy { +namespace abseil { + +constexpr char kAbseilThreadAnnotationsHeader[] = + "absl/base/internal/thread_annotations.h"; + +namespace { + +class PrefixedThreadAnnotationsPPCallbacks : public PPCallbacks { + public: + PrefixedThreadAnnotationsPPCallbacks(ClangTidyCheck &Check, + const SourceManager &SM) + : Check(Check), SM(SM) {} + + void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD, + SourceRange Range, const MacroArgs *Args) override; + + private: + ClangTidyCheck &Check; + const SourceManager &SM; +}; + +void PrefixedThreadAnnotationsPPCallbacks::MacroExpands( + const Token &MacroNameTok, const MacroDefinition &MD, SourceRange Range, + const MacroArgs *Args) { + llvm::StringRef actual_header = + SM.getFilename(MD.getMacroInfo()->getDefinitionLoc()); + llvm::StringRef original_header = + llvm::StringRef(kAbseilThreadAnnotationsHeader); + if (actual_header.endswith(original_header)) { + FixItHint hint = FixItHint::CreateInsertion(Range.getBegin(), "ABSL_"); + Check.diag(Range.getBegin(), "usage of unprefixed thread annotation") + << hint; + } +} + +} // End of anonymous namespace. + +void PrefixedThreadAnnotationsCheck::registerPPCallbacks( + const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) { + PP->addPPCallbacks( + llvm::make_unique(*this, SM)); +} + +} // namespace abseil +} // namespace tidy +} // namespace clang Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -89,6 +89,12 @@ Finds and fixes cases where ``absl::Duration`` values are being converted to numeric types and back again. +- New :doc:`abseil-prefixed-thread-annotations + ` check. + + Checks for usages of deprecated Abseil thread annotation macros and + migrates them to the new macros that are prefixed with `ABSL_`. + - New :doc:`abseil-time-comparison ` check. Index: clang-tools-extra/docs/clang-tidy/checks/abseil-prefixed-thread-annotations.rst =================================================================== --- /dev/null +++ clang-tools-extra/docs/clang-tidy/checks/abseil-prefixed-thread-annotations.rst @@ -0,0 +1,28 @@ +.. title:: clang-tidy - abseil-prefixed-thread-annotations + +abseil-prefixed-thread-annotations +================================== + +This check finds usages of deprecated Abseil thread annotation macros and +migrates them to the new macros that are prefixed with `ABSL_`. + +Examples: + +.. code-block:: c++ +// Original - Usage of deprecated and unprefixed Abseil thread annotations. +#include "absl/base/thread_annotations.h" + +class X { + // ... + private: + absl::Mutex m_; + int state_ GUARDED_BY(m_); +}; + +// Suggestion - Addition of ABSL_ prefix. +class X { + // ... + private: + absl::Mutex m_; + int state_ ABSL_GUARDED_BY(m_); +}; 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 @@ -15,6 +15,7 @@ abseil-faster-strsplit-delimiter abseil-no-internal-dependencies abseil-no-namespace + abseil-prefixed-thread-annotations abseil-redundant-strcat-calls abseil-str-cat-append abseil-string-find-startswith Index: clang-tools-extra/test/clang-tidy/Inputs/absl/base/internal/thread_annotations.h =================================================================== --- /dev/null +++ clang-tools-extra/test/clang-tidy/Inputs/absl/base/internal/thread_annotations.h @@ -0,0 +1 @@ +#define SCOPED_LOCKABLE Index: clang-tools-extra/test/clang-tidy/Inputs/absl/base/thread_annotations.h =================================================================== --- /dev/null +++ clang-tools-extra/test/clang-tidy/Inputs/absl/base/thread_annotations.h @@ -0,0 +1,3 @@ +#include "absl/base/internal/thread_annotations.h" + +#define ABSL_SCOPED_LOCKABLE Index: clang-tools-extra/test/clang-tidy/abseil-prefixed-thread-annotations.cpp =================================================================== --- /dev/null +++ clang-tools-extra/test/clang-tidy/abseil-prefixed-thread-annotations.cpp @@ -0,0 +1,9 @@ +// RUN: %check_clang_tidy %s abseil-prefixed-thread-annotations %t -- -- -I%S/Inputs + +#include "absl/base/thread_annotations.h" + +class SCOPED_LOCKABLE X {}; +// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: usage of unprefixed thread annotation [abseil-prefixed-thread-annotations] +// CHECK-FIXES: class ABSL_SCOPED_LOCKABLE X {}; + +class ABSL_SCOPED_LOCKABLE Y {}; Index: llvm/utils/gn/secondary/clang-tools-extra/clang-tidy/abseil/BUILD.gn =================================================================== --- llvm/utils/gn/secondary/clang-tools-extra/clang-tidy/abseil/BUILD.gn +++ llvm/utils/gn/secondary/clang-tools-extra/clang-tidy/abseil/BUILD.gn @@ -25,6 +25,7 @@ "FasterStrsplitDelimiterCheck.cpp", "NoInternalDependenciesCheck.cpp", "NoNamespaceCheck.cpp", + "PrefixedThreadAnnotationsCheck.cpp", "RedundantStrcatCallsCheck.cpp", "StrCatAppendCheck.cpp", "StringFindStartswithCheck.cpp",