Skip to content

Commit 5d02877

Browse files
committed
merge main into amd-staging
2 parents f219eeb + 59d72b5 commit 5d02877

File tree

72 files changed

+2692
-1671
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+2692
-1671
lines changed

clang-tools-extra/clang-tidy/llvm/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_clang_library(clangTidyLLVMModule STATIC
1212
PreferStaticOverAnonymousNamespaceCheck.cpp
1313
TwineLocalCheck.cpp
1414
UseNewMLIROpBuilderCheck.cpp
15+
UseRangesCheck.cpp
1516

1617
LINK_LIBS
1718
clangTidy

clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "PreferStaticOverAnonymousNamespaceCheck.h"
2020
#include "TwineLocalCheck.h"
2121
#include "UseNewMLIROpBuilderCheck.h"
22+
#include "UseRangesCheck.h"
2223

2324
namespace clang::tidy {
2425
namespace llvm_check {
@@ -43,6 +44,7 @@ class LLVMModule : public ClangTidyModule {
4344
CheckFactories.registerCheck<TwineLocalCheck>("llvm-twine-local");
4445
CheckFactories.registerCheck<UseNewMlirOpBuilderCheck>(
4546
"llvm-use-new-mlir-op-builder");
47+
CheckFactories.registerCheck<UseRangesCheck>("llvm-use-ranges");
4648
}
4749

4850
ClangTidyOptions getModuleOptions() override {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//===--- UseRangesCheck.cpp - clang-tidy ----------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "UseRangesCheck.h"
10+
11+
namespace clang::tidy::llvm_check {
12+
13+
namespace {
14+
15+
class StdToLLVMReplacer : public utils::UseRangesCheck::Replacer {
16+
public:
17+
explicit StdToLLVMReplacer(
18+
ArrayRef<utils::UseRangesCheck::Signature> Signatures)
19+
: Signatures(Signatures) {}
20+
21+
ArrayRef<utils::UseRangesCheck::Signature>
22+
getReplacementSignatures() const override {
23+
return Signatures;
24+
}
25+
26+
std::optional<std::string>
27+
getReplaceName(const NamedDecl &OriginalName) const override {
28+
return ("llvm::" + OriginalName.getName()).str();
29+
}
30+
31+
std::optional<std::string>
32+
getHeaderInclusion(const NamedDecl &) const override {
33+
return "llvm/ADT/STLExtras.h";
34+
}
35+
36+
private:
37+
ArrayRef<utils::UseRangesCheck::Signature> Signatures;
38+
};
39+
40+
} // namespace
41+
42+
utils::UseRangesCheck::ReplacerMap UseRangesCheck::getReplacerMap() const {
43+
ReplacerMap Results;
44+
45+
static const Signature SingleSig = {{0}};
46+
static const Signature TwoSig = {{0}, {2}};
47+
48+
const auto AddStdToLLVM =
49+
[&Results](llvm::IntrusiveRefCntPtr<Replacer> Replacer,
50+
std::initializer_list<StringRef> Names) {
51+
for (const auto &Name : Names) {
52+
Results.try_emplace(("::std::" + Name).str(), Replacer);
53+
}
54+
};
55+
56+
// Single range algorithms
57+
AddStdToLLVM(llvm::makeIntrusiveRefCnt<StdToLLVMReplacer>(SingleSig),
58+
{"all_of", "any_of",
59+
"none_of", "for_each",
60+
"find", "find_if",
61+
"find_if_not", "fill",
62+
"count", "count_if",
63+
"copy", "copy_if",
64+
"transform", "replace",
65+
"remove_if", "stable_sort",
66+
"partition", "partition_point",
67+
"is_sorted", "min_element",
68+
"max_element", "binary_search",
69+
"lower_bound", "upper_bound",
70+
"unique", "uninitialized_copy"});
71+
72+
// Two range algorithms
73+
AddStdToLLVM(llvm::makeIntrusiveRefCnt<StdToLLVMReplacer>(TwoSig),
74+
{"equal", "mismatch", "includes"});
75+
76+
return Results;
77+
}
78+
79+
UseRangesCheck::UseRangesCheck(StringRef Name, ClangTidyContext *Context)
80+
: utils::UseRangesCheck(Name, Context) {}
81+
82+
DiagnosticBuilder UseRangesCheck::createDiag(const CallExpr &Call) {
83+
return diag(Call.getBeginLoc(), "use an LLVM range-based algorithm");
84+
}
85+
86+
ArrayRef<std::pair<StringRef, StringRef>>
87+
UseRangesCheck::getFreeBeginEndMethods() const {
88+
static constexpr std::pair<StringRef, StringRef> Refs[] = {
89+
{"::std::begin", "::std::end"},
90+
{"::std::cbegin", "::std::cend"},
91+
{"::std::rbegin", "::std::rend"},
92+
{"::std::crbegin", "::std::crend"},
93+
};
94+
return Refs;
95+
}
96+
97+
} // namespace clang::tidy::llvm_check
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===--- UseRangesCheck.h - clang-tidy --------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_USERANGESCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_USERANGESCHECK_H
11+
12+
#include "../utils/UseRangesCheck.h"
13+
14+
namespace clang::tidy::llvm_check {
15+
16+
/// Finds calls to STL iterator algorithms that can be replaced with LLVM
17+
/// range-based algorithms from `llvm/ADT/STLExtras.h`.
18+
///
19+
/// For the user-facing documentation see:
20+
/// http://clang.llvm.org/extra/clang-tidy/checks/llvm/use-ranges.html
21+
class UseRangesCheck : public utils::UseRangesCheck {
22+
public:
23+
UseRangesCheck(StringRef Name, ClangTidyContext *Context);
24+
25+
ReplacerMap getReplacerMap() const override;
26+
DiagnosticBuilder createDiag(const CallExpr &Call) override;
27+
ArrayRef<std::pair<StringRef, StringRef>>
28+
getFreeBeginEndMethods() const override;
29+
};
30+
31+
} // namespace clang::tidy::llvm_check
32+
33+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_USERANGESCHECK_H

clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ def main():
258258
help="Upgrades clang-tidy warnings to errors. Same format as '-checks'.",
259259
default="",
260260
)
261+
parser.add_argument(
262+
"-hide-progress",
263+
action="store_true",
264+
help="Hide progress",
265+
)
261266

262267
clang_tidy_args = []
263268
argv = sys.argv[1:]
@@ -312,7 +317,8 @@ def main():
312317
if max_task_count == 0:
313318
max_task_count = multiprocessing.cpu_count()
314319
max_task_count = min(len(lines_by_file), max_task_count)
315-
print(f"Running clang-tidy in {max_task_count} threads...")
320+
if not args.hide_progress:
321+
print(f"Running clang-tidy in {max_task_count} threads...")
316322

317323
combine_fixes = False
318324
export_fixes_dir = None
@@ -408,7 +414,8 @@ def main():
408414
return_code = 1
409415

410416
if combine_fixes:
411-
print("Writing fixes to " + args.export_fixes + " ...")
417+
if not args.hide_progress:
418+
print(f"Writing fixes to {args.export_fixes} ...")
412419
try:
413420
merge_replacement_files(export_fixes_dir, args.export_fixes)
414421
except:

clang-tools-extra/clang-tidy/tool/run-clang-tidy.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,11 @@ async def main() -> None:
576576
action="store_true",
577577
help="Enable per-check timing profiles, and print a report",
578578
)
579+
parser.add_argument(
580+
"-hide-progress",
581+
action="store_true",
582+
help="Hide progress",
583+
)
579584
args = parser.parse_args()
580585

581586
db_path = "compile_commands.json"
@@ -681,13 +686,11 @@ async def main() -> None:
681686
file_name_re = re.compile("|".join(args.files))
682687
files = {f for f in files if file_name_re.search(f)}
683688

684-
print(
685-
f"Running clang-tidy in {max_task} threads for",
686-
len(files),
687-
"files out of",
688-
number_files_in_database,
689-
"in compilation database ...",
690-
)
689+
if not args.hide_progress:
690+
print(
691+
f"Running clang-tidy in {max_task} threads for {len(files)} files "
692+
f"out of {number_files_in_database} in compilation database ..."
693+
)
691694

692695
returncode = 0
693696
semaphore = asyncio.Semaphore(max_task)
@@ -716,13 +719,15 @@ async def main() -> None:
716719
result.stderr += f"{result.filename}: terminated by signal {-result.returncode}\n"
717720
progress = f"[{i + 1: >{len(f'{len(files)}')}}/{len(files)}]"
718721
runtime = f"[{result.elapsed:.1f}s]"
719-
print(f"{progress}{runtime} {' '.join(result.invocation)}")
722+
if not args.hide_progress:
723+
print(f"{progress}{runtime} {' '.join(result.invocation)}")
720724
if result.stdout:
721725
print(result.stdout, end=("" if result.stderr else "\n"))
722726
if result.stderr:
723727
print(result.stderr)
724728
except asyncio.CancelledError:
725-
print("\nCtrl-C detected, goodbye.")
729+
if not args.hide_progress:
730+
print("\nCtrl-C detected, goodbye.")
726731
for task in tasks:
727732
task.cancel()
728733
if delete_fixes_dir:
@@ -742,7 +747,8 @@ async def main() -> None:
742747
print("No profiling data found.")
743748

744749
if combine_fixes:
745-
print(f"Writing fixes to {args.export_fixes} ...")
750+
if not args.hide_progress:
751+
print(f"Writing fixes to {args.export_fixes} ...")
746752
try:
747753
assert export_fixes_dir
748754
merge_replacement_files(export_fixes_dir, args.export_fixes)
@@ -752,7 +758,8 @@ async def main() -> None:
752758
returncode = 1
753759

754760
if args.fix:
755-
print("Applying fixes ...")
761+
if not args.hide_progress:
762+
print("Applying fixes ...")
756763
try:
757764
assert export_fixes_dir
758765
apply_fixes(args, clang_apply_replacements_binary, export_fixes_dir)

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ Improvements to clang-tidy
135135
:program:`clang-tidy-20`. Users should use the check-specific options of the
136136
same name instead.
137137

138+
- Improved :program:`run-clang-tidy.py` and :program:`clang-tidy-diff.py`
139+
scripts by adding the `-hide-progress` option to suppress progress and
140+
informational messages.
141+
138142
New checks
139143
^^^^^^^^^^
140144

@@ -157,6 +161,12 @@ New checks
157161
Checks for uses of MLIR's old/to be deprecated ``OpBuilder::create<T>`` form
158162
and suggests using ``T::create`` instead.
159163

164+
- New :doc:`llvm-use-ranges
165+
<clang-tidy/checks/llvm/use-ranges>` check.
166+
167+
Finds calls to STL library iterator algorithms that could be replaced with
168+
LLVM range-based algorithms from ``llvm/ADT/STLExtras.h``.
169+
160170
- New :doc:`misc-override-with-different-visibility
161171
<clang-tidy/checks/misc/override-with-different-visibility>` check.
162172

clang-tools-extra/docs/clang-tidy/checks/list.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,13 @@ Clang-Tidy Checks
249249
:doc:`linuxkernel-must-check-errs <linuxkernel/must-check-errs>`,
250250
:doc:`llvm-header-guard <llvm/header-guard>`,
251251
:doc:`llvm-include-order <llvm/include-order>`, "Yes"
252-
:doc:`llvm-use-new-mlir-op-builder <llvm/use-new-mlir-op-builder>`, "Yes"
253252
:doc:`llvm-namespace-comment <llvm/namespace-comment>`,
254253
:doc:`llvm-prefer-isa-or-dyn-cast-in-conditionals <llvm/prefer-isa-or-dyn-cast-in-conditionals>`, "Yes"
255254
:doc:`llvm-prefer-register-over-unsigned <llvm/prefer-register-over-unsigned>`, "Yes"
256255
:doc:`llvm-prefer-static-over-anonymous-namespace <llvm/prefer-static-over-anonymous-namespace>`,
257256
:doc:`llvm-twine-local <llvm/twine-local>`, "Yes"
257+
:doc:`llvm-use-new-mlir-op-builder <llvm/use-new-mlir-op-builder>`, "Yes"
258+
:doc:`llvm-use-ranges <llvm/use-ranges>`, "Yes"
258259
:doc:`llvmlibc-callee-namespace <llvmlibc/callee-namespace>`,
259260
:doc:`llvmlibc-implementation-in-namespace <llvmlibc/implementation-in-namespace>`,
260261
:doc:`llvmlibc-inline-function-decl <llvmlibc/inline-function-decl>`, "Yes"
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
.. title:: clang-tidy - llvm-use-ranges
2+
3+
llvm-use-ranges
4+
===============
5+
6+
Finds calls to STL library iterator algorithms that could be replaced with
7+
LLVM range-based algorithms from ``llvm/ADT/STLExtras.h``.
8+
9+
Example
10+
-------
11+
12+
.. code-block:: c++
13+
14+
auto it = std::find(vec.begin(), vec.end(), value);
15+
bool all = std::all_of(vec.begin(), vec.end(),
16+
[](int x) { return x > 0; });
17+
18+
Transforms to:
19+
20+
.. code-block:: c++
21+
22+
auto it = llvm::find(vec, value);
23+
bool all = llvm::all_of(vec, [](int x) { return x > 0; });
24+
25+
Supported algorithms
26+
--------------------
27+
28+
Calls to the following STL algorithms are checked:
29+
30+
``std::all_of``,
31+
``std::any_of``,
32+
``std::binary_search``,
33+
``std::copy``,
34+
``std::copy_if``,
35+
``std::count``,
36+
``std::count_if``,
37+
``std::equal``,
38+
``std::fill``,
39+
``std::find``,
40+
``std::find_if``,
41+
``std::find_if_not``,
42+
``std::for_each``,
43+
``std::includes``,
44+
``std::is_sorted``,
45+
``std::lower_bound``,
46+
``std::max_element``,
47+
``std::min_element``,
48+
``std::mismatch``,
49+
``std::none_of``,
50+
``std::partition``,
51+
``std::partition_point``,
52+
``std::remove_if``,
53+
``std::replace``,
54+
``std::stable_sort``,
55+
``std::transform``,
56+
``std::uninitialized_copy``,
57+
``std::unique``,
58+
``std::upper_bound``.
59+
60+
The check will add the necessary ``#include "llvm/ADT/STLExtras.h"`` directive
61+
when applying fixes.

0 commit comments

Comments
 (0)