545 Commits

Author SHA1 Message Date
Matheus Izvekov
227e88b943
[clang-tidy] fix misc-unconventional-assign-operator entity match (#154430)
Makes sure UnconventionalAssignOperatorCheck checks if the types
reference the same entity, not the exact declaration.

This adds a new matcher to support this check.

This fixes a regression introduced by #147835. Since this regression was
never released, there are no release notes.

Fixes #153770
2025-08-19 19:28:41 -03:00
Balázs Kéri
a0f325bd41
[clang-tidy] Added check 'misc-override-with-different-visibility' (#140086) 2025-08-18 11:00:42 +02:00
Matheus Izvekov
91cdd35008
[clang] Improve nested name specifier AST representation (#147835)
This is a major change on how we represent nested name qualifications in
the AST.

* The nested name specifier itself and how it's stored is changed. The
prefixes for types are handled within the type hierarchy, which makes
canonicalization for them super cheap, no memory allocation required.
Also translating a type into nested name specifier form becomes a no-op.
An identifier is stored as a DependentNameType. The nested name
specifier gains a lightweight handle class, to be used instead of
passing around pointers, which is similar to what is implemented for
TemplateName. There is still one free bit available, and this handle can
be used within a PointerUnion and PointerIntPair, which should keep
bit-packing aficionados happy.
* The ElaboratedType node is removed, all type nodes in which it could
previously apply to can now store the elaborated keyword and name
qualifier, tail allocating when present.
* TagTypes can now point to the exact declaration found when producing
these, as opposed to the previous situation of there only existing one
TagType per entity. This increases the amount of type sugar retained,
and can have several applications, for example in tracking module
ownership, and other tools which care about source file origins, such as
IWYU. These TagTypes are lazily allocated, in order to limit the
increase in AST size.

This patch offers a great performance benefit.

It greatly improves compilation time for
[stdexec](https://github.com/NVIDIA/stdexec). For one datapoint, for
`test_on2.cpp` in that project, which is the slowest compiling test,
this patch improves `-c` compilation time by about 7.2%, with the
`-fsyntax-only` improvement being at ~12%.

This has great results on compile-time-tracker as well:

![image](https://github.com/user-attachments/assets/700dce98-2cab-4aa8-97d1-b038c0bee831)

This patch also further enables other optimziations in the future, and
will reduce the performance impact of template specialization resugaring
when that lands.

It has some other miscelaneous drive-by fixes.

About the review: Yes the patch is huge, sorry about that. Part of the
reason is that I started by the nested name specifier part, before the
ElaboratedType part, but that had a huge performance downside, as
ElaboratedType is a big performance hog. I didn't have the steam to go
back and change the patch after the fact.

There is also a lot of internal API changes, and it made sense to remove
ElaboratedType in one go, versus removing it from one type at a time, as
that would present much more churn to the users. Also, the nested name
specifier having a different API avoids missing changes related to how
prefixes work now, which could make existing code compile but not work.

How to review: The important changes are all in
`clang/include/clang/AST` and `clang/lib/AST`, with also important
changes in `clang/lib/Sema/TreeTransform.h`.

The rest and bulk of the changes are mostly consequences of the changes
in API.

PS: TagType::getDecl is renamed to `getOriginalDecl` in this patch, just
for easier to rebasing. I plan to rename it back after this lands.

Fixes #136624
Fixes https://github.com/llvm/llvm-project/issues/43179
Fixes https://github.com/llvm/llvm-project/issues/68670
Fixes https://github.com/llvm/llvm-project/issues/92757
2025-08-09 05:06:53 -03:00
Victor Chernyakin
6a98171138
[clang-tidy] Speed up misc-header-include-cycle (#148757)
Performance optimization of misc-header-include-cycle 
based on clangd test on Sema.cpp. Check were slow due 
calls to SM.translateFile. Cost reduction (+-) from 11% to 3%.
2025-07-23 20:14:29 +02:00
Yanzuo Liu
4a9eaad9e1
[Clang][AST][NFC] Introduce NamespaceBaseDecl (#149123)
Add `NamespaceBaseDecl` as common base class of `NamespaceDecl` and
`NamespaceAliasDecl`. This simplifies `NestedNameSpecifier` a bit.

Co-authored-by: Matheus Izvekov <mizvekov@gmail.com>
2025-07-18 09:01:47 +08:00
Victor Chernyakin
4328b69df6
[clang-tidy] Update confusables.txt in misc-confusable-identifiers (#148399)
We're currently on Unicode 14.0.0. This PR updates it to Unicode 16.0.0.
2025-07-14 21:05:33 +03:00
Baranov Victor
d7a17540f8
[clang-tidy][NFC] add '.clang-tidy' config for clang-tidy project (#147793)
Added `.clang-tidy` config as discussed in
[RFC](https://discourse.llvm.org/t/rfc-create-hardened-clang-tidy-config-for-clang-tidy-directory/87247).
Added `bugprone`, `readability`, `modernize`, `performance` checks that
didn't create many warnings.
Fixed minor warnings to make `/clang-tidy` directory complaint with
`clang-tidy-20`.

Disabled checks will be enabled in future PRs after fixing their
warnings.
2025-07-11 17:17:59 +03:00
flovent
6c153e50f8
[clang-tidy] Fix false positives with template in misc-unconventional-assign-operator check (#143292)
Fix false positives when copy assignment operator function in a template
class returns the result of another assignment to `*this`, this check
doesn't consider this situation that there will be a `BinaryOperator`
for assignment rather than `CXXOperatorCallExpr` since `this`'s type is
dependent.

Closes #143237.
2025-07-06 15:06:18 +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
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
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
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
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
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
Kazu Hirata
9384e8e63c
[clang-tools-extra] Use llvm::max_element (NFC) (#140458) 2025-05-18 09:46:39 -07:00
Kazu Hirata
1a40edf2c0
[clang-tools-extra] Use llvm::find_if (NFC) (#140411) 2025-05-17 17:01:37 -07:00
Baranov Victor
842e591577
[clang-tidy][NFC] fix clang-tidy warnings in clang-tools-extra/clang-tidy directory (#136097)
Mostly stylistic changes to `clang-tidy` source code.

Command run:
`python3 clang-tools-extra/clang-tidy/tool/run-clang-tidy.py -p build/
-j $(nproc) clang-tools-extra/clang-tidy`
2025-04-20 20:41:13 +02:00
Zahira Ammarguellat
ebb0e6cb25
[NFC] Remove dead code detected by code sanitizer. (#134385)
The code sanitizer is failing with this error: `Execution cannot reach
this statement.`
The execution code path would early exit at line 928 if `(Lil && Ril) =
true`.
2025-04-04 15:21:47 -04:00
Baranov Victor
d6dcd985c0
[clang-tidy] Fix thread_local false positives in misc-use-internal-linkage check (#132573)
Based on C++ standard (see issue
https://github.com/llvm/llvm-project/issues/131679) and
[StackOverflow](https://stackoverflow.com/questions/22794382/are-c11-thread-local-variables-automatically-static)
`thread_local` variables are implicitly `static` so we should not
suggest adding `static` on a `thread_local` variables. I'd appreciate if
someone else will confirm this too because reading standard is tricky.

However, many people still use `static` and `thread_local` together:
[github
code-search](https://github.com/search?type=code&q=%22static+thread_local%22+language%3AC%2B%2B).
Maybe disabling warnings on `thread_local` should be made as a flag?
WDYT?

Closes https://github.com/llvm/llvm-project/issues/131679.
2025-03-28 21:33:58 +08:00
Congcong Cai
01e505b992
[clang-tidy][misc-const-correctness] fix fp when using const array type. (#133018)
Fixed: #132931
const array is immutable in C/C++ language design, we don't need to
check constness for it.
2025-03-28 06:21:15 +08:00
Kazu Hirata
4a7643400c
[clang-tools-extra] Use *Set::insert_range (NFC) (#132589)
DenseSet, SmallPtrSet, SmallSet, SetVector, and StringSet recently
gained C++23-style insert_range.  This patch replaces:

  Dest.insert(Src.begin(), Src.end());

with:

  Dest.insert_range(Src);
2025-03-23 00:23:19 -07:00
Congcong Cai
1a68269e28
[clang-tidy] support pointee mutation check in misc-const-correctness (#130494)
Co-authored-by: Baranov Victor <bar.victor.2002@gmail.com>
2025-03-14 21:18:43 +08:00
Congcong Cai
5cfc37b345
[clang-tidy][NFC]clean ConstCorrectnessCheck (#130493) 2025-03-11 14:53:06 +08:00
Congcong Cai
6f7570c200
[clang-tidy][misc-use-internal-linkage] fix false positives for function or variable in header file which contains macro expansion (#129594)
When check whether in main file, spelling loc will lead to `<scratch
space>`. instead, expansion loc is close to loc after preprocess. It is
suitable to analyze linkage.
2025-03-08 13:41:24 +08:00
Baranov Victor
e9c8d42b89
[clang-tidy] misc-unused-using-decls: add correct handling of operator"" with template parametes (#129392)
Fixes false-positives when operator"" has template paremetes, e.g.
```cpp
template <char... Ts>
int operator""_r() {
    return {};
}
```
Closes https://github.com/llvm/llvm-project/issues/53444.

---------

Co-authored-by: Congcong Cai <congcongcai0907@163.com>
2025-03-03 13:14:27 +08:00
Baranov Victor
8363b0a6ba
[clang-tidy] add AllowedTypes option to misc-const-correctness (#122951)
Add option `AllowedTypes` which allow users to specify types they want
to exclude from const-correctness check.

Small real-world example:
```cpp
#include <mutex>

int main() {
  std::mutex m;
  std::lock_guard<std::mutex> l(m); // we want to ignore it since std::lock_guard is already immutable.
}
```

Closes issue https://github.com/llvm/llvm-project/issues/122592
2025-02-20 05:47:17 +08:00
earnol
1e14edb898
[clang-tidy] Address false positives in misc-redundant-expression checker (#122841)
This patch addresses situations when misc-redundant-expression checker
provides excessive diagnostics for situations with different macros
having the same value. In particular it addresses situations described
in the initial report of
https://github.com/llvm/llvm-project/issues/118885 are addressed. The
situations which are popped inside discussion like if (A + B == B + A)
for macros are not properly addressed by this patch.
Those changes are also mentioned in Release Notes.
---------

Co-authored-by: Vladislav Aranov <vladislav.aranov@ericsson.com>
Co-authored-by: EugeneZelenko <eugene.zelenko@gmail.com>
2025-02-10 13:16:37 -05:00
Congcong Cai
8e6d6a5510
[clang-tidy][NFC] improve performance misc-unused-using-decls (#123454)
skip header file before register AST Matchers
it can avoid to matcher lots of ast node when lint header file
2025-01-24 19:32:11 +08:00
Congcong Cai
68c6b2e188
[ASTMatchers][NFC] use Matcher<QualType> instead of DynTypedMatcher in TypeLocTypeMatcher (#123450)
There are no template in `TypeLocTypeMatcher`. So we do not need to use
`DynTypedMatcher` which can improve performance
2025-01-23 06:28:43 +08:00
Congcong Cai
909bf38c1f
[clang-tidy][NFC] remove unused field in UnusedUsingDeclsCheck (#123451) 2025-01-18 14:14:50 +08:00
maflcko
6504546abc
[clang-tidy][use-internal-linkage] fix false positive for consteval function (#122141)
Fixes https://github.com/llvm/llvm-project/issues/122096

---------

Co-authored-by: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz>
2025-01-10 09:24:24 +01:00
Maksim Ivanov
1b897f737d
[clang-tidy] Fix misc-unused-parameters on params with attrs (#122286)
Don't suggest to comment-out the parameter name if the parameter has an
attribute that's spelled after the parameter name.

This prevents the parameter's attributes from being wrongly applied to
the parameter's type.

This fixes #122191.
2025-01-09 20:40:31 +01:00
Congcong Cai
222dd235ff
[clang-tidy] use local config (#120004)
follow up patch for #119948.
2024-12-18 18:38:46 +08:00
Congcong Cai
010317e173
[clang-tidy][use-internal-linkage]fix false positives for ExportDecl (#117901)
Fixed: #97190
2024-12-02 13:59:55 +08:00
Congcong Cai
f89fa238fa
[clang-tidy][use-internal-linkage]fix false positives for global overloaded operator new and operator delete (#117945) 2024-11-30 22:14:36 +08:00
vabridgers
8ac2b77a11
[analyzer] Remove alpha.core.IdenticalExpr Checker (#114715)
This change removes the alpha.core.IdenticalExpr static analysis checker
since it's checks are present in the clang-tidy checks
misc-redundant-expression and bugprone-branch-clone. This check was
implemented as a static analysis check using AST matching, and since
alpha and duplicated in 2 clang-tidy checks may be removed.

Co-authored-by: Vince Bridgers <vince.a.bridgers@ericsson.com>
2024-11-29 17:38:59 -06:00
Congcong Cai
e3aafe407a
[clang-tidy] fix false positive use-internal-linkage for func decl without body (#117490)
If in one TU, function only have declaration without body, this function
should be external linkage.
Fixed #117488
2024-11-25 06:46:10 +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
Congcong Cai
6f21a7bdee
[clang-tidy] insert `static` keyword in correct position for misc-use-internal-linkage (#108792)
Fixes: #108760

---------

Co-authored-by: Danny Mösch <danny.moesch@icloud.com>
2024-10-17 23:55:37 +08:00
Kazu Hirata
571354e251
[clang-tidy] Avoid repeated hash lookups (NFC) (#112074) 2024-10-12 08:04:17 -07: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
282fc93f09
[clang-tidy][NFC] optimize unused using decls performance (#110200)
Improve performance by moving the check forward to the matching stage
2024-09-30 21:25:22 +08:00
Julian Schmidt
50320ec967
[NFC][clang-tidy] reword diagnostic note in definitions-in-headers (#106862)
`make as inline` made little sense here, so I changed the `make` to
`mark`
and added `the definition` as well.
2024-09-17 10:42:23 +02:00
Congcong Cai
cdd2c0693b
[clang-tidy] fix misc-unconventional-assign-operator false positive for deducing this (#107409)
Fixes: #107119
When meeting c++23 deducing this, we should skip the first parameter
2024-09-06 19:11:26 +08:00
Thomas Schenker
26c99c4217 [clang-tidy] fix misc-const-correctness to work with function-try-blocks (#99925)
Make the clang-tidy check misc-const-correctness work with
function-try-blocks.

Fixes #99860.
2024-07-23 07:38:49 +00:00
Piotr Zegar
f0fad9f3e0
Revert "[clang-tidy] fix misc-const-correctness to work with function-try-blocks" (#100069)
Reverts llvm/llvm-project#99925
2024-07-23 08:46:40 +02:00
Thomas Schenker
cd9e42cb0f
[clang-tidy] fix misc-const-correctness to work with function-try-blocks (#99925)
Make the clang-tidy check misc-const-correctness work with
function-try-blocks.

Fixes #99860.
2024-07-23 08:17:39 +02:00
Carlos Galvez
29896db09c
[clang-tidy] Do not warn on const variables in misc-use-internal-linkage (#97969)
Since in C++ they already have implicit internal linkage.

Fixes #97947
2024-07-09 12:20:44 +02:00
Congcong Cai
019f525716
[clang-tidy] add fixhint for misc-use-internal-linkage (#96203) 2024-06-26 22:30:15 +08:00
Congcong Cai
c4f83a004b
[clang-tidy] new check misc-use-internal-linkage (#90830)
Add new check misc-use-internal-linkage to detect variable and function
can be marked as static.

---------

Co-authored-by: Danny Mösch <danny.moesch@icloud.com>
2024-06-08 06:46:39 +08:00