Commit Graph

3498 Commits

Author SHA1 Message Date
flovent
5a8d096ae3 [clang-tidy] Fix false positive for cppcoreguidelines-pro-bounds-pointer-arithmetic (#127394)
this PR fixes #126424
for `ArraySubScriptExpr`, `hasBase` Matcher will get right operand when
it is not integer type, but is not for sure that left operand is integer
type. For the example code below `hasBase` will get `r` for the
Subsequent matching and causing false positive.
```
template <typename R>
int f(std::map<R*, int>& map, R* r) {
  return map[r];
}
```
so is needed to see if index is integer type to avoid this situation.
2025-07-02 17:41:24 +03:00
Richard Smith
c56c349d39 [clang-tidy] Switch misc-confusable-identifiers check to a faster algorithm. (#130369)
Optimizations:

- Only build the skeleton for each identifier once, rather than once for
each declaration of that identifier.
- Only compute the contexts in which identifiers are declared for
identifiers that have the same skeleton as another identifier in the
translation unit.
- Only compare pairs of declarations that are declared in related
contexts, rather than comparing all pairs of declarations with the same
skeleton.

Also simplify by removing the caching of enclosing `DeclContext` sets,
because with the above changes we don't even compute the enclosing
`DeclContext` sets in common cases. Instead, we terminate the traversal
to enclosing `DeclContext`s immediately if we've already found another
declaration in that context with the same identifier. (This optimization
is not currently applied to the `forallBases` traversal, but could be
applied there too if needed.)

This also fixes two bugs that together caused the check to fail to find
some of the issues it was looking for:

- The old check skipped comparisons of declarations from different
contexts unless both declarations were type template parameters. This
caused the checker to not warn on some instances of the CVE it is
intended to detect.
- The old check skipped comparisons of declarations in all base classes
other than the first one found by the traversal. This appears to be an
oversight, incorrectly returning `false` rather than `true` from the
`forallBases` callback, which terminates traversal.

This also fixes an issue where the check would have false positives for
template parameters and function parameters in some cases, because those
parameters sometimes have a parent `DeclContext` that is the parent of
the parameterized entity, or sometimes is the translation unit. In
either case, this would cause warnings about declarations that are never
visible together in any scope.

This decreases the runtime of this check, especially in the common case
where there are few or no skeletons with two or more different
identifiers. Running this check over LLVM, clang, and clang-tidy, the
wall time for the check as reported by clang-tidy's internal profiler is
reduced from 5202.86s to 3900.90s.
2025-07-01 13:31:46 -07:00
Dimitrije Dobrota
0f291e5787 [clang-tidy] Add flag to specify an alternative to std::move in cppcoreguidelines-rvalue-reference-param-not-moved (#138757)
Since std::move is nothing more than a cast, part of STL and not the
language itself, it's easy to provide a custom implementation if one
wishes not to include the entirety of <utility>.

Added flag (MoveFunction) provides a way to continue using this
essential check even with the custom implementation of moving.

---------

Co-authored-by: EugeneZelenko <eugene.zelenko@gmail.com>
2025-06-30 22:36:23 +03:00
Dimitrije Dobrota
6a57af8d03 [clang-tidy] Add flag to specify an alternative to std::forward (#138755)
Since std::forward is nothing more than a cast, part of STL and not the
language itself, it's easy to provide a custom implementation if one
wishes not to include the entirety of <utility>.

Added flag (ForwardFunction) provides a way to continue using this
essential check even with the custom implementation of forwarding.

---------

Co-authored-by: EugeneZelenko <eugene.zelenko@gmail.com>
2025-06-30 22:13:33 +03:00
Baranov Victor
a3a60e03e2 [clang-tidy] add new check: modernize-use-scoped-lock (#126434)
Add new clang-tidy check that finds uses of `std::lock_guard` and suggests
replacing them with C++17's more flexible and safer alternative
`std::scoped_lock`.

Here is a small description of how it works for better understanding of
the code:
Two separate AST matchers are registered:

- The first one matches declarations of `std::lock_guard` that are
single in their scope (only one `std::lock_guard` in `CompoundStmt`).
It's an easy case, we can emit warning right away.

- The second one matches `CompoundStmt`'s that have multiple
`std::lock_guard` declarations, which means that we may have consecutive
declarations of `std::lock_guard` that can be replaced by a single
`std::scoped_lock`. In order to ensure that declarations are
consecutive, we need to loop over `Stmt`'s in `CompoundStmt`. Here is a
small example:
```cpp
{
  std::mutex m1, m2;
  std::lock(m1, m2);
  std::lock_guard<std::mutex> l1(m, std::adopt_lock); // first declaration of 'std::lock_guard'
  std::lock_guard<std::mutex> l2(m, std::adopt_lock); // second declaration of 'std::lock_guard' that can be merged with first using 'scoped_lock'
}
```

This PR closes https://github.com/llvm/llvm-project/issues/107839.
2025-06-29 22:34:32 +03:00
Björn Svensson
8351752dbc [clang-tidy] Fix false positives in readability-redundant-inline-specifier (#135391)
The out-of-line explicitly-defaulted definition is not the first
declaration, so it is not implicitly inline.

Alt. reference:
9.5.2 (3) Explicitly-defaulted functions in
[N4950](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/n4950.pdf).
or https://timsong-cpp.github.io/cppwp/n4861/dcl.fct.def.default#3

Fixes #130745

---------

Signed-off-by: Björn Svensson <bjorn.a.svensson@est.tech>
2025-06-28 09:10:34 +03:00
Kazu Hirata
df79c40c98 [clang-tidy Fix a warning
This patch fixes:

  clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp:371:26:
  error: variable 'E' set but not used
  [-Werror,-Wunused-but-set-variable]
2025-06-25 10:14:34 -07:00
Malavika Samak
86026ee623 [clang-tidy] Warn about misuse of sizeof operator in loops. (#143205)
The sizeof operator misuses in loop conditionals can be a source of
bugs. The common misuse is attempting to retrieve the number of elements
in the array by using the sizeof expression and forgetting to divide the
value by the sizeof the array elements. This results in an incorrect
computation of the array length and requires a warning from the sizeof
checker.

Example:
```
 int array[20];

void test_for_loop() {
  // Needs warning.
  for(int i = 0; i < sizeof(array); i++) {
    array[i] = i;
  }
}

void test_while_loop() {

  int count = 0;
  // Needs warning. 
  while(count < sizeof(array)) {
    array[count] = 0;
    count = count + 2;
  }
}
```
rdar://151403083

---------

Co-authored-by: MalavikaSamak <malavika2@apple.com>
2025-06-25 10:04:10 -07:00
Baranov Victor
e435558ff9 [clang-tidy] add 'IgnoreMarcos' option to 'special-member-functions' check (#143550) 2025-06-24 08:39:05 +03:00
Baranov Victor
c594f6e697 Revert "[clang-tidy] Add new check readability-use-numeric-limits" (#145355)
Reverts llvm/llvm-project#127430 due to stable asan buildbot failures:
https://lab.llvm.org/buildbot/#/builders/169
2025-06-23 19:39:01 +03:00
Baranov Victor
05491e0359 [clang-tidy] add 'IgnoreMarcos' option to 'avoid-goto' check (#143554) 2025-06-23 16:27:18 +03:00
Katherine Whitlock
e7dd223ec4 [clang-tidy] Add new check readability-use-numeric-limits (#127430)
The adds a check that replaces specific numeric literals like `32767`
with the equivalent call to `std::numeric_limits` (such as
`std::numeric_limits<int16_t>::max())`.

Partially addresses #34434, but notably does not handle cases listed in
the title post such as `~0` and `-1`.
2025-06-21 21:10:20 +03:00
Baranov Victor
1b5d6ec685 [clang-tidy] count class member initializers as statements in 'readability-function-size' (#131669)
Improve `readability-function-size` by counting class member
initializers as statements.
Relates to https://github.com/llvm/llvm-project/issues/131126
2025-06-21 13:14:19 +03:00
Philipp Jung
669627d0c7 Add check 'cppcoreguidelines-use-enum-class' (#138282)
Warn on non-class enum definitions as suggested by the Core Guidelines:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-class
2025-06-18 11:02:53 +02:00
Dmitry Polukhin
26d082d330 [clang-tidy][performance-unnecessary-value-param] Avoid in coroutines (#140912)
Summary:
Replacing by-value parameters with passing by-reference is not safe for
coroutines because the caller may be executed in parallel with the
callee, which increases the chances of resulting in dangling references
and hard-to-find crashes. See for the reference
[cppcoreguidelines-avoid-reference-coroutine-parameters](https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/avoid-reference-coroutine-parameters.html).

Test Plan: check-clang-tools
2025-06-17 09:47:15 +01:00
Baranov Victor
f46c44dbc0 [clang-tidy][NFC] change patterns 'anyOf(..., anything())' to 'optionally(...)' (#143558)
Writing `optionally()` instead of `anyOf(..., anything())` lowers code
size and gives the author's intention better.
2025-06-14 10:55:42 +03:00
David Rivera
3c1053811e Revert "[clang-tidy] Improve integer comparison by matching valid expressions outside implicitCastExpr" (#143944)
Reverts llvm/llvm-project#134188
related: https://github.com/llvm/llvm-project/issues/143927
2025-06-12 14:33:06 -04:00
Baranov Victor
94877ce1b4 [clang-tidy][NFC] fix 'misc-use-internal-linkage' check warnings (#143482)
Run misc-use-internal-linkage check over clang-tidy code. 
Also fixed a couple of other clang-tidy warnings.

Apart from issues in header files, all '.cpp' in
`clang-tools-extra/clang-tidy` must be clang-tidy clear now.
2025-06-10 23:23:37 +03:00
David Rivera
e65d323166 [clang-tidy] Improve integer comparison by matching valid expressions outside implicitCastExpr (#134188)
Aims to fix #127471
Covered the edge case where an int expression is not necessarily
directly wrapped around an 'ImplicitCastExpr' which seemed to be a
requirement in 'use-integer-sign-comparison.cpp' check to trigger.

**For instance**:

```cpp
#include <vector>

bool f() {
  std::vector<int> v;
  unsigned int i = 0;

  return i >= v.size();
}
```
2025-06-10 10:57:11 +03:00
Baranov Victor
5213c57cb1 [clang-tidy][NFC] run clang-format over clang-tidy checks and tool code. (#143324) 2025-06-09 21:54:48 +03:00
Baranov Victor
68070f908b [clang-tidy][NFC] run clang-format over 'cert', 'cppcore', 'fuchsia',… (#143316)
… 'google' checks
2025-06-08 23:22:55 +03:00
Baranov Victor
ce46adb8b7 [clang-tidy][NFC] run clang-format over 'android', 'boost' and 'bugprone' checks (#143315) 2025-06-08 23:22:05 +03:00
Baranov Victor
65d66625b3 [clang-tidy][NFC] run clang-format over abseil and altera checks. (#143314) 2025-06-08 23:21:35 +03:00
Baranov Victor
55c86c5f77 [clang-tidy][NFC] fix formatting of namespace-comment-check (#143305)
Fixed formatting and codestyle issues in `namespace-comment-check`

Follow up to https://github.com/llvm/llvm-project/pull/124265.
2025-06-08 13:51:17 +02:00
Thorsten Klein
00eb22fff9 added option google-readability-namespace-comments.AllowNoNamespaceComments (#124265)
New option AllowNoNamespaceComments for
`google-readability-namespace-comments.AllowNoNamespaceComments` is
added.

When true, the check will allow that no namespace comment is present. If
a namespace comment is added but it is not matching, the check will
fail. Default is `false`

Fixes #124264
2025-06-08 13:19:40 +03:00
Kazu Hirata
6edfc6ce6c [clang-tools-extra] Use llvm::any_of (NFC) (#143281) 2025-06-08 01:34:24 -07:00
flovent
239c8ac268 [clang-tidy] Fix false positives with deducing this in readability-convert-member-functions-to-static check (#141391)
Add check for `DeclRefExpr` which points to an explicit object
parameter.

Fixes #141381.

---------

Co-authored-by: fubowen <fubowen@protomail.com>
Co-authored-by: flovent <flbven@protomail.com>
2025-06-08 09:58:24 +03:00
Carlos Galvez
30d8aebbe2 [clang-tidy] Add option to disable bugprone-multi-level-pointer-conversion in C code (#141209)
Sometimes a project may want to enable this check only in C++, and
disable it in C, since the patterns the check warns about are quite
common and idiomatic in C, and there are no better alternatives.
    
Fixes #140659

Co-authored-by: Carlos Gálvez <carlos.galvez@zenseact.com>
2025-06-07 21:52:23 +02:00
Baranov Victor
0f03f8ab2e [clang-tidy] Add support for lambda-expression in use-trailing-return-type check (#135383)
Add support for lambda-expression in `use-trailing-return-type` check.

Added two new options:
1. `TransformFunctions` will trigger function declarations to use
trailing return type.
2. `TransformLambdas` will trigger lambda expression to use trailing
return type if it was not stated explicitly.

Fixed false positives when lambda was matched as a function in C++11
mode.

Closes https://github.com/llvm/llvm-project/issues/95711
2025-06-06 10:46:41 +03:00
Nick Sarnie
3b9ebe9201 [clang] Simplify device kernel attributes (#137882)
We have multiple different attributes in clang representing device
kernels for specific targets/languages. Refactor them into one attribute
with different spellings to make it more easily scalable for new
languages/targets.

---------

Signed-off-by: Sarnie, Nick <nick.sarnie@intel.com>
2025-06-05 14:15:38 +00:00
flovent
a12f4f0031 [clang-tidy] Add check for assignment or comparision operators' operand in readability-math-missing-parentheses (#141345)
Fixes false negative in #141249. 

Add check for math binary operators which are operands of assignment or
comparision operators.

Closes #141249.
2025-06-05 15:18:01 +03:00
Kazu Hirata
86f8be6a7c [clang-tools-extra] Remove unused local variables (NFC) (#142881) 2025-06-04 22:43:59 -07:00
Matheus Izvekov
c95189f877 [clang-tidy] NFCI: remove non-functional matcher from SizeofExpressionCheck (#142654)
This matcher would never match anything, because all record types
as-written would be wrapped in an ElaboratedType.

Just fixing that leads to a matcher which has too many false positives
to be useful.

The warning message itself is not great either, it has a hard-coded type
name.
2025-06-04 09:26:09 +02:00
FabianWolff
317f3bdcc1 [clang-tidy] Fix formatting and add release notes entry (#141584)
Follow-up to #141092.
2025-05-27 21:52:59 +02:00
FabianWolff
47d5e94acb [clang-tidy] readability-redundant-smartptr-get: disable for smart pointers to arrays (#141092)
Currently we generate an incorrect suggestion for shared/unique pointers
to arrays; for instance ([Godbolt](https://godbolt.org/z/Tens1reGP)):
```c++
#include <memory>

void test_shared_ptr_to_array() {
  std::shared_ptr<int[]> i;
  auto s = sizeof(*i.get());
}
```
```
<source>:5:20: warning: redundant get() call on smart pointer [readability-redundant-smartptr-get]
    5 |   auto s = sizeof(*i.get());
      |                    ^~~~~~~
      |                    i
1 warning generated.
```
`sizeof(*i)` is incorrect, though, because the array specialization of
`std::shared/unique_ptr` does not have an `operator*()`. Therefore I
have disabled this check for smart pointers to arrays for now; future
work could, of course, improve on this by suggesting, say,
`sizeof(i[0])` in the above example.
2025-05-27 12:06:08 +02:00
Tommy Chen
4cb25e2d37 [clang-tidy] Add avoid-pragma-once. (#140388)
#139618
2025-05-27 05:57:51 +08:00
Baranov Victor
ac0a880f4e [clang-tidy] added AllowedTypes option to readability-qualified-auto check (#136571)
Added `AllowedTypes` option to `readability-qualified-auto` check

Fixes https://github.com/llvm/llvm-project/issues/63461.
2025-05-26 13:25:35 +03:00
Baranov Victor
eda3e96b40 [clang-tidy] Fix false positives in bugprone-crtp-constructor-accessibility check (#132543)
Fix false positives in `bugprone-crtp-constructor-accessibility` check
on deleted constructors that cannot be used to construct objects, even
if they have public or protected access.

Closes https://github.com/llvm/llvm-project/issues/131737.
2025-05-26 06:25:19 +03:00
Kazu Hirata
ba007a60d0 [clang-tidy] Remove unused includes (NFC) (#141420)
These are identified by misc-include-cleaner.  I've filtered out those
that break builds.  Also, I'm staying away from llvm-config.h,
config.h, and Compiler.h, which likely cause platform- or
compiler-specific build failures.
2025-05-25 10:55:36 -07:00
David Rivera
4675f2287c [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (#129370)
This aims to address a portion of #122480 by adding matchers on binary
operators. **This allows the detection of explicit arithmetic operations
within initializers.**
2025-05-24 20:15:36 +03:00
Baranov Victor
fb99a850b6 [clang-tidy][NFC] Refactor modernize-use-trailing-return-type-check check code and tests (#140759)
- Deleted unused includes
- Deleted useless braces
- Converted private methods to static function to improve compilations
speed and readability
- Modernized tests to use `cxx-or-later`
2025-05-24 19:55:51 +03:00
Kazu Hirata
05674b21fe [clang-tidy] Use std:::string::find with std::string_view (NFC) (#141188)
std::string::rfind accepts anything that can be converted to
std::string_view starting in C++17.  Since StringRef can be converted
to std::string_view, we do not need to create a temporary instance of
std::string here.
2025-05-22 23:50:55 -07:00
Jan Svoboda
656d9ba934 [clang] Fix LogDiagnosticPrinter.h and ClangTidyPlugin.cpp after 9e306ad4 (#141131) 2025-05-22 13:39:54 -07:00
Jan Svoboda
13e1a2cb22 Reapply "[clang] Remove intrusive reference count from DiagnosticOptions (#139584)"
This reverts commit e2a885537f. Build failures were fixed right away and reverting the original commit without the fixes breaks the build again.
2025-05-22 12:52:03 -07:00
Kazu Hirata
e2a885537f Revert "[clang] Remove intrusive reference count from DiagnosticOptions (#139584)"
This reverts commit 9e306ad460.

Multiple builtbot failures have been reported:
https://github.com/llvm/llvm-project/pull/139584
2025-05-22 12:44:20 -07:00
Jan Svoboda
9e306ad460 [clang] Remove intrusive reference count from DiagnosticOptions (#139584)
The `DiagnosticOptions` class is currently intrusively
reference-counted, which makes reasoning about its lifetime very
difficult in some cases. For example, `CompilerInvocation` owns the
`DiagnosticOptions` instance (wrapped in `llvm::IntrusiveRefCntPtr`) and
only exposes an accessor returning `DiagnosticOptions &`. One would
think this gives `CompilerInvocation` exclusive ownership of the object,
but that's not the case:

```c++
void shareOwnership(CompilerInvocation &CI) {
  llvm::IntrusiveRefCntPtr<DiagnosticOptions> CoOwner = &CI.getDiagnosticOptions();
  // ...
}
```

This is a perfectly valid pattern that is being actually used in the
codebase.

I would like to ensure the ownership of `DiagnosticOptions` by
`CompilerInvocation` is guaranteed to be exclusive. This can be
leveraged for a copy-on-write optimization later on. This PR changes
usages of `DiagnosticOptions` across `clang`, `clang-tools-extra` and
`lldb` to not be intrusively reference-counted.
2025-05-22 12:33:52 -07:00
Daan De Meyer
c46a394df9 [clang-tidy] Add UnusedIncludes/MissingIncludes options to misc-include-cleaner (#140600)
These mimick the same options from clangd and allow using the check to
only check for unused includes or missing includes.
2025-05-21 16:41:04 +02:00
Baranov Victor
64dcf78935 [clang-tidy][NFC] Refactor modernize-pass-by-value check code and tests (#140753)
- Deleted unused includes
- Deleted useless braces
- Modernized tests to use `CHECK-MESSAGES-NOT` and `CHECK-FIXES-NOT` for
better readability and maintainability
2025-05-21 10:34:45 +02:00
Kazu Hirata
5bd3c44b79 [clang-tools-extra] Remove redundant control flow statements (NFC) (#140846) 2025-05-21 00:29:17 -07:00
Carlos Galvez
322d0197f5 [clang-tidy] Do not flag strerror in concurrency-mt-unsafe (#140520)
The docs of the check state:

> Glibc’s list is compiled from GNU web documentation with a search for
MT-Safe tag

And strerror fulfills exactly that:
https://www.gnu.org/software/libc/manual/html_node/Error-Messages.html

> Function: char * strerror (int errnum)
> Preliminary: | MT-Safe | AS-Unsafe heap i18n | AC-Unsafe mem | See
POSIX Safety Concepts.

So concurrency-mt-unsafe should not flag it.

Fixes #140515

---------

Co-authored-by: Carlos Gálvez <carlos.galvez@zenseact.com>
Co-authored-by: EugeneZelenko <eugene.zelenko@gmail.com>
2025-05-19 21:27:37 +02:00