From 42dfaa15a60cea6cd75a2efb419aa2c206d2a127 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Mon, 30 Dec 2024 22:35:46 +0800 Subject: [PATCH] [clang-tidy] add depercation warning for non-whitelisted global options (#121057) We plan to depercate `StrictMode` and `IgnoreMacros` global options after 2 major versions and support local options only for them. This patch introduces the depercation warning. --- .../clang-tidy/ClangTidyCheck.cpp | 32 ++++++++++++------- clang-tools-extra/docs/ReleaseNotes.rst | 3 +- .../checkers/modernize/use-std-format-fmt.cpp | 2 +- .../deprecation-global-option.cpp | 3 ++ 4 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/infrastructure/deprecation-global-option.cpp diff --git a/clang-tools-extra/clang-tidy/ClangTidyCheck.cpp b/clang-tools-extra/clang-tidy/ClangTidyCheck.cpp index 6028bb225813..4aa9fe228ee7 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyCheck.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidyCheck.cpp @@ -7,11 +7,11 @@ //===----------------------------------------------------------------------===// #include "ClangTidyCheck.h" -#include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringRef.h" -#include "llvm/Support/Error.h" +#include "llvm/ADT/StringSet.h" #include "llvm/Support/YAMLParser.h" #include +#include namespace clang::tidy { @@ -62,16 +62,29 @@ ClangTidyCheck::OptionsView::get(StringRef LocalName) const { return std::nullopt; } +static const llvm::StringSet<> DeprecatedGlobalOptions{ + "StrictMode", + "IgnoreMacros", +}; + static ClangTidyOptions::OptionMap::const_iterator findPriorityOption(const ClangTidyOptions::OptionMap &Options, StringRef NamePrefix, StringRef LocalName, - llvm::StringSet<> *Collector) { + ClangTidyContext *Context) { + llvm::StringSet<> *Collector = Context->getOptionsCollector(); if (Collector) { Collector->insert((NamePrefix + LocalName).str()); Collector->insert(LocalName); } auto IterLocal = Options.find((NamePrefix + LocalName).str()); auto IterGlobal = Options.find(LocalName); + // FIXME: temporary solution for deprecation warnings, should be removed + // after 22.x. Warn configuration deps on deprecation global options. + if (IterLocal == Options.end() && IterGlobal != Options.end() && + DeprecatedGlobalOptions.contains(LocalName)) + Context->configurationDiag( + "global option '%0' is deprecated, please use '%1%0' instead.") + << LocalName << NamePrefix; if (IterLocal == Options.end()) return IterGlobal; if (IterGlobal == Options.end()) @@ -83,8 +96,7 @@ findPriorityOption(const ClangTidyOptions::OptionMap &Options, std::optional ClangTidyCheck::OptionsView::getLocalOrGlobal(StringRef LocalName) const { - auto Iter = findPriorityOption(CheckOptions, NamePrefix, LocalName, - Context->getOptionsCollector()); + auto Iter = findPriorityOption(CheckOptions, NamePrefix, LocalName, Context); if (Iter != CheckOptions.end()) return StringRef(Iter->getValue().Value); return std::nullopt; @@ -117,8 +129,7 @@ ClangTidyCheck::OptionsView::get(StringRef LocalName) const { template <> std::optional ClangTidyCheck::OptionsView::getLocalOrGlobal(StringRef LocalName) const { - auto Iter = findPriorityOption(CheckOptions, NamePrefix, LocalName, - Context->getOptionsCollector()); + auto Iter = findPriorityOption(CheckOptions, NamePrefix, LocalName, Context); if (Iter != CheckOptions.end()) { if (auto Result = getAsBool(Iter->getValue().Value, Iter->getKey())) return Result; @@ -157,10 +168,9 @@ std::optional ClangTidyCheck::OptionsView::getEnumInt( bool IgnoreCase) const { if (!CheckGlobal && Context->getOptionsCollector()) Context->getOptionsCollector()->insert((NamePrefix + LocalName).str()); - auto Iter = CheckGlobal - ? findPriorityOption(CheckOptions, NamePrefix, LocalName, - Context->getOptionsCollector()) - : CheckOptions.find((NamePrefix + LocalName).str()); + auto Iter = CheckGlobal ? findPriorityOption(CheckOptions, NamePrefix, + LocalName, Context) + : CheckOptions.find((NamePrefix + LocalName).str()); if (Iter == CheckOptions.end()) return std::nullopt; diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 3cab44015525..1fd9b6077be5 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -119,7 +119,8 @@ Improvements to clang-tidy - Removed :program:`clang-tidy`'s global options for most of checks. All options are changed to local options except `IncludeStyle`, `StrictMode` and - `IgnoreMacros`. + `IgnoreMacros`. Global scoped `StrictMode` and `IgnoreMacros` are deprecated + and will be removed in further releases. .. csv-table:: :header: "Check", "Options removed from global option" diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format-fmt.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format-fmt.cpp index 1eaf18ac1199..71c8af190467 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format-fmt.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format-fmt.cpp @@ -1,6 +1,6 @@ // RUN: %check_clang_tidy %s modernize-use-std-format %t -- \ // RUN: -config="{CheckOptions: { \ -// RUN: StrictMode: true, \ +// RUN: modernize-use-std-format.StrictMode: true, \ // RUN: modernize-use-std-format.StrFormatLikeFunctions: 'fmt::sprintf', \ // RUN: modernize-use-std-format.ReplacementFormatFunction: 'fmt::format', \ // RUN: modernize-use-std-format.FormatHeader: '' \ diff --git a/clang-tools-extra/test/clang-tidy/infrastructure/deprecation-global-option.cpp b/clang-tools-extra/test/clang-tidy/infrastructure/deprecation-global-option.cpp new file mode 100644 index 000000000000..4c9854d22183 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/infrastructure/deprecation-global-option.cpp @@ -0,0 +1,3 @@ +// RUN: clang-tidy %s --config="{CheckOptions:{StrictMode: true}}" -checks="-*,modernize-use-std-format" | FileCheck %s + +// CHECK: warning: global option 'StrictMode' is deprecated, please use 'modernize-use-std-format.StrictMode' instead. [clang-tidy-config]