Skip to content

Commit 42dfaa1

Browse files
authored
[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.
1 parent ff936ce commit 42dfaa1

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

clang-tools-extra/clang-tidy/ClangTidyCheck.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "ClangTidyCheck.h"
10-
#include "llvm/ADT/SmallString.h"
1110
#include "llvm/ADT/StringRef.h"
12-
#include "llvm/Support/Error.h"
11+
#include "llvm/ADT/StringSet.h"
1312
#include "llvm/Support/YAMLParser.h"
1413
#include <optional>
14+
#include <string>
1515

1616
namespace clang::tidy {
1717

@@ -62,16 +62,29 @@ ClangTidyCheck::OptionsView::get(StringRef LocalName) const {
6262
return std::nullopt;
6363
}
6464

65+
static const llvm::StringSet<> DeprecatedGlobalOptions{
66+
"StrictMode",
67+
"IgnoreMacros",
68+
};
69+
6570
static ClangTidyOptions::OptionMap::const_iterator
6671
findPriorityOption(const ClangTidyOptions::OptionMap &Options,
6772
StringRef NamePrefix, StringRef LocalName,
68-
llvm::StringSet<> *Collector) {
73+
ClangTidyContext *Context) {
74+
llvm::StringSet<> *Collector = Context->getOptionsCollector();
6975
if (Collector) {
7076
Collector->insert((NamePrefix + LocalName).str());
7177
Collector->insert(LocalName);
7278
}
7379
auto IterLocal = Options.find((NamePrefix + LocalName).str());
7480
auto IterGlobal = Options.find(LocalName);
81+
// FIXME: temporary solution for deprecation warnings, should be removed
82+
// after 22.x. Warn configuration deps on deprecation global options.
83+
if (IterLocal == Options.end() && IterGlobal != Options.end() &&
84+
DeprecatedGlobalOptions.contains(LocalName))
85+
Context->configurationDiag(
86+
"global option '%0' is deprecated, please use '%1%0' instead.")
87+
<< LocalName << NamePrefix;
7588
if (IterLocal == Options.end())
7689
return IterGlobal;
7790
if (IterGlobal == Options.end())
@@ -83,8 +96,7 @@ findPriorityOption(const ClangTidyOptions::OptionMap &Options,
8396

8497
std::optional<StringRef>
8598
ClangTidyCheck::OptionsView::getLocalOrGlobal(StringRef LocalName) const {
86-
auto Iter = findPriorityOption(CheckOptions, NamePrefix, LocalName,
87-
Context->getOptionsCollector());
99+
auto Iter = findPriorityOption(CheckOptions, NamePrefix, LocalName, Context);
88100
if (Iter != CheckOptions.end())
89101
return StringRef(Iter->getValue().Value);
90102
return std::nullopt;
@@ -117,8 +129,7 @@ ClangTidyCheck::OptionsView::get<bool>(StringRef LocalName) const {
117129
template <>
118130
std::optional<bool>
119131
ClangTidyCheck::OptionsView::getLocalOrGlobal<bool>(StringRef LocalName) const {
120-
auto Iter = findPriorityOption(CheckOptions, NamePrefix, LocalName,
121-
Context->getOptionsCollector());
132+
auto Iter = findPriorityOption(CheckOptions, NamePrefix, LocalName, Context);
122133
if (Iter != CheckOptions.end()) {
123134
if (auto Result = getAsBool(Iter->getValue().Value, Iter->getKey()))
124135
return Result;
@@ -157,10 +168,9 @@ std::optional<int64_t> ClangTidyCheck::OptionsView::getEnumInt(
157168
bool IgnoreCase) const {
158169
if (!CheckGlobal && Context->getOptionsCollector())
159170
Context->getOptionsCollector()->insert((NamePrefix + LocalName).str());
160-
auto Iter = CheckGlobal
161-
? findPriorityOption(CheckOptions, NamePrefix, LocalName,
162-
Context->getOptionsCollector())
163-
: CheckOptions.find((NamePrefix + LocalName).str());
171+
auto Iter = CheckGlobal ? findPriorityOption(CheckOptions, NamePrefix,
172+
LocalName, Context)
173+
: CheckOptions.find((NamePrefix + LocalName).str());
164174
if (Iter == CheckOptions.end())
165175
return std::nullopt;
166176

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ Improvements to clang-tidy
119119

120120
- Removed :program:`clang-tidy`'s global options for most of checks. All options
121121
are changed to local options except `IncludeStyle`, `StrictMode` and
122-
`IgnoreMacros`.
122+
`IgnoreMacros`. Global scoped `StrictMode` and `IgnoreMacros` are deprecated
123+
and will be removed in further releases.
123124

124125
.. csv-table::
125126
:header: "Check", "Options removed from global option"

clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format-fmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %check_clang_tidy %s modernize-use-std-format %t -- \
22
// RUN: -config="{CheckOptions: { \
3-
// RUN: StrictMode: true, \
3+
// RUN: modernize-use-std-format.StrictMode: true, \
44
// RUN: modernize-use-std-format.StrFormatLikeFunctions: 'fmt::sprintf', \
55
// RUN: modernize-use-std-format.ReplacementFormatFunction: 'fmt::format', \
66
// RUN: modernize-use-std-format.FormatHeader: '<fmt/core.h>' \
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// RUN: clang-tidy %s --config="{CheckOptions:{StrictMode: true}}" -checks="-*,modernize-use-std-format" | FileCheck %s
2+
3+
// CHECK: warning: global option 'StrictMode' is deprecated, please use 'modernize-use-std-format.StrictMode' instead. [clang-tidy-config]

0 commit comments

Comments
 (0)