181 Commits

Author SHA1 Message Date
David Rivera
556e4dbdcd
[clang-tidy] Fix performance-move-const-arg false negative in ternary… (#128402)
This PR aims to fix `performance-move-const-arg` #126515

## Changes
Enhanced the `performance-move-arg` check in Clang-Tidy to detect cases
where `std::move` is used
in **ternary operator expressions which was not being matched therefore
being tagged as a false negative**

## Testing
- A new mock class has been where the changes have been tested & all
tests pass

I'd appreciate any feedback since this is my first time contributing to
LLVM.
2025-02-27 19:35:10 +08:00
Congcong Cai
a00586171c
[clang-tidy]improve performance-unnecessary-value-param performance (#128383)
Tolerate fix-it breaking compilation when functions is used as pointers.
`isReferencedOutsideOfCallExpr` will visit the whole translate unit for
each matched function decls. It will waste lots of cpu time in some big
cpp files.
But the benefits of this validation are limited. Lots of function usage
are out of current translation unit.

After removing this validation step, the check profiling changes from
5.7 to 1.1 in SemaExprCXX.cpp, which is similar to version 18.
2025-02-26 20:39:41 +08:00
Barnabás Pőcze
a5364444bd
[clang-tidy] performance-unnecessary-copy-initialization: Consider static functions (#119974)
Static member functions can be considered the same way as free functions
are, so do that.
2025-01-12 10:07:52 +01:00
Congcong Cai
aaadaee7b2
[clang-tidy] remove misuse of getLocalOrGlobal for non common used options (#119948)
[RFC](https://discourse.llvm.org/t/rfc-global-option-rules-for-clang-tidy/83647)
2024-12-15 05:04:32 +08:00
Vlad Serebrennikov
1b0fcf1e42
[clang-tidy][NFC] Replace usages of DeclSpec::TQ with Qualifiers::TQ (#113295)
This patch improves, but doens't fully resolve the layering violation,
which stems from relying on Sema. There's one function that needs to
convert enumerator to a string (`buildQualifier` in
`FixItHintUtils.cpp`), but `Qualifiers::TQ` doesn't offer such function.
Even more, the set of enumerators is not complete compared to
`DeclSpec::TQ`, so I'm afraid that this would be a functional change.
2024-10-22 22:21:08 +04:00
Nicolas van Kempen
0aaac4fe19
[clang-tidy][performance-move-const-arg] Fix crash when argument type has no definition (#111472)
Fix #111450.
2024-10-09 18:16:43 -04:00
Thomas Fransham
1f2c08b33b
[clang-tools-extra] Fix add_clang_library usage (#109321)
If a add_clang_library call doesn't specify building as static or shared
library they are implicitly added to the list static libraries that is
linked in to clang-cpp shared library here.

315ba77406/clang/cmake/modules/AddClang.cmake (L107)
Because the clang-tools-extra libraries targets were declared after
clang-cpp they by luck never got linked to clang-cpp.
This change is required for clang symbol visibility macros on windows to
work correctly for clang tools since we need to distinguish if a target
being built will be importing or exporting clang symbols from the
clang-cpp DLL.
2024-10-08 09:22:19 +03:00
Congcong Cai
b7914dffd6
[clang-tidy] add default error message for performance-avoid-endl (#107867)
use std::endl as default message when matched expr does not have valid
source text

Fixes: #107859
2024-09-14 07:33:06 +08:00
Clement Courbet
dd23b34789
[clang-tidy][performance-unnecessary-value-param] Make handleMoveFix virtual (#99867)
... so that downstream checks can override behaviour to do additional
processing.

Refactor the rest of the logic to `handleConstRefFix` (which is also
`virtual`).

This is otherwise and NFC.

This is similar to https://github.com/llvm/llvm-project/pull/73921 but
for `performance-unnecessary-value-param`.
2024-07-23 11:32:26 +02:00
Vitaly Goldshteyn
8e42e0d28a
[clang-tidy] Allow unnecessary-value-param to match templated functions including lambdas with auto. (#97767)
Clang-Tidy unnecessary-value-param value param will be triggered for
templated functions if at least one instantiontion with expensive to
copy type is present in translation unit.

It is relatively common mistake to write lambda functions with auto
arguments for expensive to copy types.
2024-07-15 21:26:39 +02:00
Congcong Cai
dcbe0d43b5
[clang-tidy] fix false negatives for performance-inefficient-vector-operation (#95667)
Fixes: #95596
Avoid false negatives caused by different variable definition type and
variable initial value type in loop initialization expression.
2024-06-16 12:01:50 +08:00
Congcong Cai
a78c104a23
[clang-tidy]fix false positives of the result of std::move() is used as rvalue for performance-move-const-arg (#95633)
Fixes: #86404

---------

Co-authored-by: Danny Mösch <danny.moesch@icloud.com>
2024-06-15 21:06:30 +08:00
Clement Courbet
415a82c64a
[clang-tidy] doesNotMutateObject: Handle calls to member functions … (#94362)
…and operators that have non-const overloads.

This allows  `unnecessary-copy-initialization` to warn on more cases.

The common case is a class with a a set of const/non-sconst overloads
(e.g. std::vector::operator[]).

```
void F() {
  std::vector<Expensive> v;
  // ...

  const Expensive e = v[i];
}
```
2024-06-10 11:15:11 +02:00
Congcong Cai
f40f4fcee5
[clang analysis] ExprMutationAnalyzer support recursive forwarding reference (#88843)
Reapply for #88765.
Partially fixes: #60895.
2024-04-17 09:57:30 +08:00
Piotr Zegar
11a411a49b Revert "[clang-tidy][NFC] Remove duplicated code"
This reverts commit b6f6be4b500ff64c23a5103ac3311cb74519542f.
2024-03-31 15:06:49 +00:00
Piotr Zegar
b6f6be4b50 [clang-tidy][NFC] Remove duplicated code
Remove duplicated matchers by moving some of them to
utils/Matchers.h. Add some anonymous namespaces and
renamed some code to avoid ODR issues.
2024-03-31 14:58:27 +00:00
Clement Courbet
94ca854d3c
[clang-tidy] Add support for determining constness of more expressions. (#82617)
This uses a more systematic approach for determining whcich
`DeclRefExpr`s mutate the underlying object: Instead of using a few
matchers, we walk up the AST until we find a parent that we can prove
cannot change the underlying object.

This allows us to handle most address taking and dereference, bindings
to value and const& variables, and track constness of pointee (see
changes in DeclRefExprUtilsTest.cpp).

This allows supporting more patterns in
`performance-unnecessary-copy-initialization`.

Those two patterns are relatively common:

```
const auto e = (*vector_ptr)[i]
```

and

```
const auto e = vector_ptr->at(i);
```

In our codebase, we have around 25% additional findings from
`performance-unnecessary-copy-initialization` with this change. I did
not see any additional false positives.
2024-02-26 09:55:07 +01:00
Clement Courbet
8fd32b96ca
[clang-tidy] Handle C++ structured bindings in performance-for-range-copy (#77105)
Right now we are not triggering on:

```
for (auto [x, y] : container) {
  // const-only access
}
```
2024-01-16 10:03:19 +01:00
Da-Viper
356c2c2399
Fix #75686: add iter_swap and iter_move to the matched name (#76117)
Added support for iter_swap, iter_move in bugprone-exception-escape 
and performance-noexcept-swap checks.

Fixes  #75686
2024-01-14 10:48:47 +01:00
Kazu Hirata
0f1721c480 [clang-tidy] Use StringRef::contains (NFC) 2023-12-23 22:30:03 -08:00
Clement Courbet
04ce9a34ea
[clang-tidy] performance-unnecessary-copy-init: Add a hook... (#73921)
... so that derived checks can can observe for which
variables a warning has been emitted. Does nothing by default, which
makes this an NFC.
2023-12-07 08:31:16 +01:00
Piotr Zegar
3716b5b4ba
[clang-tidy] Improve performance-enum-size to exclude empty enums (#71640)
Enums without enumerators (empty) are now excluded from analysis as it's
not possible to peroperly determinate new narrowed type, and such enums
can be used in diffrent way, like as strong-types.

Closes #71544
2023-11-08 11:22:31 +01:00
Piotr Zegar
cbdc3e1bf9 [clang-tidy][NFC] Fix cppcoreguidelines-init-variables findings
Fix issues found by clang-tidy in clang-tidy source directory.
2023-08-27 11:59:03 +00:00
Piotr Zegar
01c8bf6fd4 [clang-tidy][NFC] Fix llvm-else-after-return findings
Fix issues found by clang-tidy in clang-tidy source directory.
2023-08-27 11:59:02 +00:00
Piotr Zegar
8ba103ca68 [clang-tidy][NFC] Fix readability-static-accessed-through-instance findings
Fix issues found by clang-tidy in clang-tidy source directory.
2023-08-27 08:52:09 +00:00
Fabian Wolff
5b95d17da1 [clang-tidy] performance-faster-string-find generates incorrect fixes for single quote character literals
Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D157436
2023-08-09 00:55:45 +02:00
Piotr Zegar
2553e2403a [clang-tidy] Fix false-positives in performanc-noexcept-swap
Enforce a stricter match with the swap function signature, eliminating false-positives.

Fixes: #64303

Reviewed By: carlosgalvezp

Differential Revision: https://reviews.llvm.org/D157185
2023-08-06 09:23:21 +00:00
Jon Roelofs
2fb1c1082c
cmake: add missing dependencies on ClangDriverOptions tablegen
The modules build trips over this frequently because there is no textual
include of the tablegen output, but the module includes it.

Differential revision: https://reviews.llvm.org/D157119
2023-08-04 10:27:19 -07:00
Piotr Zegar
3397dae69e [clang-tidy] Add performance-enum-size check
Finds enum type definitions that could use smaller integral type as a base.

Reviewed By: xgupta

Differential Revision: https://reviews.llvm.org/D144135
2023-07-25 17:50:19 +00:00
Piotr Zegar
1d0759e6c3 [clang-tidy][NFC] Fix list.rst generation when adding check
add_new_check.py does not work properly for checks that
generate fixes in base class. Adding some comments to those
checks in order to fix list.rst generation.
2023-07-22 15:55:22 +00:00
AMS21
82d4dc20ef [clang-tidy] Improve performance-move-const-arg message when no move constructor is available
We now display a simple note if the reason is that the used class does not
support move semantics.

This fixes llvm#62550

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D153220
2023-06-18 11:40:47 +00:00
AMS21
845618cf69 [clang-tidy] Refactor common code from the Noexcept*Checks into NoexceptFunctionCheck
As discussed in the https://reviews.llvm.org/D148697 review.

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D153198
2023-06-18 06:50:05 +00:00
AMS21
474a2b9367 [clang-tidy] Add more checks for functions which should be noexcept
Added new checks
- `performance-noexcept-destructor`
- `performance-noexcept-swap`

Also added cppcoreguidlines aliases for the 2 new checks as well as `performance-noexcept-move-constructor`

This fixes llvm#62154

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D148697
2023-06-13 18:56:57 +00:00
Clement Courbet
9182c679dd [clang-tidy]performance-no-automatic-move: fix false negative on const T&& ctors.
We were only handling `const T&`/`T&&` ctor pairs, and we were missing uref-based ctors.

Differential Revision: https://reviews.llvm.org/D151092
2023-05-24 15:05:39 +02:00
AMS21
cd893308b5 [clang-tidy] Add performance-avoid-endl check
This check flags uses of `std::endl` on streams and suggests using the newline character `'\n'` instead. `std::endl` performs two operations: it writes a newline character to the output stream and then flushes the stream buffer, which can be less efficient than writing a single newline character using `'\n'`.

This fixes llvm#35321

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D148318
2023-04-22 10:50:51 +00:00
AMS21
5abe338f2a [clang-tidy] Fix false positve for defaulted move constructor in performance-noexcept-move-constructor
Previously a struct like this:

template <typename>
struct A { A(A&&) = default; };

Would trigger a false positive, since even though it is not marked as
noexcept it still is due to the `= default`.
Now we only give a warning if the defaulted move constructor is
actually declared as throwing and correctly resolve it if they are
defaulted.

This fixes llvm#56026, llvm#41414, llvm#38081

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D146922
2023-04-14 17:43:37 +00:00
Logan Gnanapragasam
e51f46705e [clang-tidy] ignore NRVO const variables in performance-no-automatic-move.
In the following code:

```cc
struct Obj {
  Obj();
  Obj(const Obj &);
  Obj(Obj &&);
  virtual ~Obj();
};

Obj ConstNrvo() {
  const Obj obj;
  return obj;
}
```

performance-no-automatic-move warns about the constness of `obj`. However, NRVO
is applied to `obj`, so the `const` should have no effect on performance.

This change modifies the matcher to exclude NRVO variables.

#clang-tidy

Reviewed By: courbet, PiotrZSL

Differential Revision: https://reviews.llvm.org/D147419
2023-04-03 17:01:59 +00:00
Carlos Galvez
4718da5060 [clang-tidy][NFC] Use C++17 nested namespaces in clang-tidy headers
We forgot to apply the change to headers in the previous patch,
due to missing "-header-filter" in the run-clang-tidy invocation.

Differential Revision: https://reviews.llvm.org/D142307
2023-01-23 21:23:16 +00:00
Carlos Galvez
7d2ea6c422 [clang-tidy][NFC] Use C++17 nested namespaces in the clang-tidy folder
Fix applied by running:

run-clang-tidy.py -checks=-*,modernize-concat-nested-namespaces

Differential Revision: https://reviews.llvm.org/D141770
2023-01-14 18:51:39 +00:00
Kazu Hirata
29ffafb575 [clang-tools-extra] Remove remaining uses of llvm::Optional (NFC)
This patch removes the unused "using" declaration and removes #include
"llvm/ADT/Optional.h".

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-07 20:34:53 -08:00
Kazu Hirata
f71ffd3b73 [clang-tools-extra] Use std::optional instead of llvm::Optional (NFC)
This patch replaces (llvm::|)Optional< with std::optional<.  I'll post
a separate patch to clean up the "using" declarations, #include
"llvm/ADT/Optional.h", etc.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-07 20:19:42 -08:00
Kazu Hirata
71f557355d [clang-tools-extra] Add #include <optional> (NFC)
This patch adds #include <optional> to those files containing
llvm::Optional<...> or Optional<...>.

I'll post a separate patch to actually replace llvm::Optional with
std::optional.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-07 20:02:20 -08:00
Fangrui Song
b0de363024 [clang] Remove uses of ::testing::Matcher<const Optional<T> &>
Change a few functions (getCheckTraversalKind, some clang/Tooling/ API, etc)
from llvm::Optional to std::optional.
2022-12-14 06:42:35 +00:00
Kazu Hirata
cd8702efe7 [clang-tidy] Use std::nullopt instead of None (NFC)
This patch mechanically replaces None with std::nullopt where the
compiler would warn if None were deprecated.  The intent is to reduce
the amount of manual work required in migrating from Optional to
std::optional.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-03 11:54:48 -08:00
Kazu Hirata
e125e6c429 [clang-tools-extra] Use llvm::find (NFC) 2022-10-30 09:41:55 -07:00
Matheus Izvekov
bcd9ba2b7e
[clang] Track the templated entity in type substitution.
This is a change to how we represent type subsitution in the AST.
Instead of only storing the replaced type, we track the templated
entity we are substituting, plus an index.
We modify MLTAL to track the templated entity at each level.

Otherwise, it's much more expensive to go from the template parameter back
to the templated entity, and not possible to do in some cases, as when
we instantiate outer templates, parameters might still reference the
original entity.

This also allows us to very cheaply lookup the templated entity we saw in
the naming context and find the corresponding argument it was replaced
from, such as for implementing template specialization resugaring.

Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>

Differential Revision: https://reviews.llvm.org/D131858
2022-10-15 22:08:36 +02:00
Kazu Hirata
20f0f15a40 Use StringRef::contains (NFC) 2022-08-28 23:29:02 -07:00
Gabriel Ravier
0ed2bd9311
[clang-tools-extra] Fixed a number of typos
I went over the output of the following mess of a command:

`(ulimit -m 2000000; ulimit -v 2000000; git ls-files -z | parallel --xargs -0 cat | aspell list --mode=none --ignore-case | grep -E '^[A-Za-z][a-z]*$' | sort | uniq -c | sort -n | grep -vE '.{25}' | aspell pipe -W3 | grep : | cut -d' ' -f2 | less)`

and proceeded to spend a few days looking at it to find probable typos
and fixed a few hundred of them in all of the llvm project (note, the
ones I found are not anywhere near all of them, but it seems like a
good start).

Reviewed By: kadircet

Differential Revision: https://reviews.llvm.org/D130826
2022-08-01 15:32:25 +02:00
Richard
6e566bc552 [clang-tidy] Organize check doc files into subdirectories (NFC)
- Rename doc files to subdirs by module
- Update release notes and check list to use subdirs
- Update add_new_check.py to handle doc subdirs

Differential Revision: https://reviews.llvm.org/D126495
2022-06-16 16:06:20 -06:00
Sockke
c98b3a8cd9 Fix performance-unnecessary-value-param for template specialization
The checker missed a check for parameter type of primary template of specialization template and this could cause build breakages.

Reviewed By: aaron.ballman, flx

Differential Revision: https://reviews.llvm.org/D116593
2022-05-30 09:55:53 +08:00