297 Commits

Author SHA1 Message Date
v1nh1shungry
326d749a36
[clang-tidy] Fix cppcoreguidelines-prefer-member-initializer false positive for inherited members (#153941)
```cpp
struct Base {
  int m;
};

template <class T>
struct Derived : Base {
  Derived() { m = 0; }
};
```

would previously generate the following output:

```
<source>:7:15: warning: 'm' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
    7 |   Derived() { m = 0; }
      |               ^~~~~~
      |             : m(0)
```

This patch fixes this false positive.

Note that before this patch the checker won't give false positive for

```cpp
struct Derived : Base {
  Derived() { m = 0; }
};
```

and the constructor's AST is

```
`-CXXConstructorDecl 0x557df03d1fb0 <line:7:3, col:22> col:3 Derived 'void ()' implicit-inline
    |-CXXCtorInitializer 'Base'
    | `-CXXConstructExpr 0x557df03d2748 <col:3> 'Base' 'void () noexcept'
    `-CompoundStmt 0x557df03d2898 <col:13, col:22>
      `-BinaryOperator 0x557df03d2878 <col:15, col:19> 'int' lvalue '='
        |-MemberExpr 0x557df03d2828 <col:15> 'int' lvalue ->m 0x557df03d1c40
        | `-ImplicitCastExpr 0x557df03d2808 <col:15> 'Base *' <UncheckedDerivedToBase (Base)>
        |   `-CXXThisExpr 0x557df03d27f8 <col:15> 'Derived *' implicit this
        `-IntegerLiteral 0x557df03d2858 <col:19> 'int' 0
```

so `isAssignmentToMemberOf` would return empty due to


f0967fca04/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp (L118-L119)

Fixes #104400
2025-08-17 11:42:38 +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
Baranov Victor
5325f2b930
[clang-tidy][NFC] Enable 'performance-move-const-arg' in '.clang-tidy' config (#148549)
Set `performance-move-const-arg.CheckTriviallyCopyableMove` option to
`false` because "trivially copyable" is too strict and give warning for
e.g. `MixData` class:
1fbfa333f6/clang-tools-extra/clang-tidy/bugprone/EasilySwappableParametersCheck.cpp (L389)
Here:

1fbfa333f6/clang-tools-extra/clang-tidy/bugprone/EasilySwappableParametersCheck.cpp (L504-L505)
I find `std::move` here useful.
2025-08-03 13:18:24 +03:00
Victor Chernyakin
92ef8e3419
[clang-tidy] Teach cppcoreguidelines-interfaces-global-init about constinit (#148334)
This check already understands how `constexpr` makes initialization
order problems impossible, and C++20's `constinit` provides the exact
same guarantees.
2025-07-15 00:10:10 +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
Victor Chernyakin
1e3f6a6c4f
[clang-tidy][NFC] Prefer constexpr llvm::StringLiteral over const char * (#147301)
Some of these are even global mutable state — probably not what was
intended!
```cpp
static const char *AnalyzerCheckNamePrefix = "clang-analyzer-";
```
2025-07-08 20:48:51 +03:00
Baranov Victor
5a9e6babd8
[clang-tidy] fix false negatives with type aliases in cppcoreguidlines-pro-bounds-pointer-arithmetic check (#139430)
Fixed false negatives with type aliases in
`cppcoreguidlines-pro-bounds-pointer-arithmetic` check.
Added tests with pointer arithmetic in template functions to make test
cases more robust.

Closes https://github.com/llvm/llvm-project/issues/139241.
2025-07-04 09:49:26 +03:00
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
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
e435558ff9
[clang-tidy] add 'IgnoreMarcos' option to 'special-member-functions' check (#143550) 2025-06-24 08:39:05 +03:00
Baranov Victor
05491e0359
[clang-tidy] add 'IgnoreMarcos' option to 'avoid-goto' check (#143554) 2025-06-23 16:27:18 +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
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
68070f908b
[clang-tidy][NFC] run clang-format over 'cert', 'cppcore', 'fuchsia',… (#143316)
… 'google' checks
2025-06-08 23:22:55 +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
Yanzuo Liu
b7db2e1fc0
[Clang-Tidy][NFC] Simplify check cppcoreguidelines-missing-std-forward (#138504)
Remove `CaptureInCopy` because the cases handled by it are covered by
`CaptureByRefExplicit`.
2025-05-07 21:39:31 +02:00
Congcong Cai
b34ed25dd5 [clang-tidy][NFC] add link libs for bugprone module
Fixed bug issue introduced in #120245
2024-12-29 19:51:55 +08:00
Congcong Cai
e45e091b90
[clang-tidy] swap cppcoreguidelines-narrowing-conversions and bugprone-narrowing-conversions (#120245)
According to #116591.
> Coding guidelines should "cherry-pick" (and posddsibly
configure/harden/make more strict) base checks.
We should move narrowing conversion to bugprone and keep alias in
cppcoreguidelines
2024-12-29 19:22:25 +08: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
Congcong Cai
605b8dad5c
[clang-tidy] Fix false positive in cppcoreguidelines-avoid-const-or-ref-data-members when detecting templated classes with inheritance (#115180)
`hasSimpleCopyConstructor` series of functions are not reliable when
these functions are not resolved. We need to manually resolve the status
of these functions from its base classes.
Fixes: #111985.
2024-11-25 06:47:22 +08:00
Julian Schmidt
028ea71fdd
[clang-tidy] fix insertion location for function pointers in cppcoreguidelines-init-variables (#112091)
Previously, the insertion location for the `= nullptr` fix would be
after the variable name. However, if the variable is of type function
pointer that is not an alias, then the insertion would happen inside the
type specification: `void (*a1)(void*);` -> `void (*a1 =
nullptr)(void*);`.
With this change, the insertion location will be at the next
'terminator'. That is, at the next `,` or `;`, as that will finish the
current declaration: `void (a1)(void*) = nullptr;`.

Fixes #112089
2024-11-10 18:26:42 +01:00
Konstantin Romanov
0fbf91ab8e
[clang-tidy] Fix cppcoreguidelines-pro-type-union-access if memLoc is invalid (#104540)
Fixes #102945.
2024-10-22 17:05:00 -07:00
Kazu Hirata
c911b0a73c
[clang-tidy] Avoid repeated hash lookups (NFC) (#111628) 2024-10-09 06:45:44 -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
7deca859e5
[clang-tidy] fix false positive when member initialization depends on structured binging variable in cppcoreguidelines-prefer-member-initializer (#108743)
Fixes: #82970

Detecting dependiences with `varDecl` is too strict. It will ignore the
`bingingDecl`.
This patch wants to use `valueDecl` to match more cases including
`bingingDecl`.
2024-09-16 09:04:32 +08:00
Kazu Hirata
33ceb2dd75
[clang-tidy] Avoid repeated hash lookups (NFC) (#107490) 2024-09-05 19:04:30 -07:00
Pascal Jungblut
7c50187b7d
[clang-tidy] Option to ignore anonymous namespaces in avoid-non-const-global-variables (#93827)
Add an option to ignore warnings for cppcoreguidelines
avoid-non-const-global-variables.

Understandably, the core guidelines discourage non const global
variables, even at the TU level (see
https://github.com/isocpp/CppCoreGuidelines/issues/2195). However,
having a small TU with an interface that uses a non const variable from
an anonymous namespace can be a valid choice.

This adds an option that disables the warning just for anonymous
namespaces, i.e. at the file level. The default is still to show a
warning, just as before.
2024-07-01 21:12:42 +02:00
Congcong Cai
b06da39cae
[clang-tidy] ignoring macro with hash preprocessing token in cppcoreguidelines-macro-usage (#95265)
`#` and `##` preprocessing tokens cannot be replaced by constexpr
function. It should be ignored in check.
2024-06-13 09:36:33 +08:00
Piotr Zegar
54c6ee922a
[clang-tidy] Add AllowImplicitlyDeletedCopyOrMove option to cppcoreguidelines-special-member-functions (#71683)
Improved cppcoreguidelines-special-member-functions check with a new
option AllowImplicitlyDeletedCopyOrMove, which removes the requirement
for explicit copy or move special member functions when they are already
implicitly deleted.

Closes #62392
2024-05-15 17:53:03 +02:00
Julian Schmidt
3b5a121a24
[clang-tidy][NFC] replace comparison of begin and end iterators with range empty (#91994)
Improves readability by changing comparisons of `*_begin` and `*_end`
iterators into `.empty()` on their range.
2024-05-15 14:35:07 +02:00
Timm Baeder
3d56ea05b6
[clang][NFC] Fix FieldDecl::isUnnamedBitfield() capitalization (#89048)
We always capitalize bitfield as "BitField".
2024-04-18 07:39:29 +02:00
Danny Mösch
16b3e43a03
[clang-tidy] Ignore non-forwarded arguments if they are unused (#87832) 2024-04-08 23:54:29 +02: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
Piotr Zegar
0a40f5d1a0
[clang-tidy] Add support for lambdas in cppcoreguidelines-owning-memory (#77246)
Implement proper support for lambdas and sub-functions/classes.
Moved from https://reviews.llvm.org/D157285

Fixes: #59389
2024-03-19 20:13:20 +01:00
Qizhi Hu
8b326d5946
[clang-tidy] fix false negative in cppcoreguidelines-missing-std-forward (#83987)
Try to fix https://github.com/llvm/llvm-project/issues/83845
When `std::forward` is invoked in a function, make sure it uses correct
parameter by checking if the bounded `var` equals the parameter.

Co-authored-by: huqizhi <836744285@qq.com>
2024-03-06 09:27:32 +08:00
AMS21
7b11e2ec39
[clang-tidy] Fix cppcoreguidelines-missing-std-forward false positive for deleted functions (#83055)
Improved check by no longer giving false positives for deleted functions.
2024-02-27 19:35:11 +01:00
Congcong Cai
e9cec39239
[clang-tidy] fix incorrect hint for InitListExpr in prefer-member-initializer (#81560) 2024-02-16 09:18:21 +08:00
Carlos Galvez
6f32d6a4f3
[clang-tidy] Remove enforcement of rule C.48 from cppcoreguidelines-prefer-member-init (#80330)
This functionality already exists in
cppcoreguidelines-use-default-member-init. It was deprecated from this
check in clang-tidy 17.

This allows us to fully decouple this check from the corresponding
modernize check, which has an unhealthy dependency.

Fixes https://github.com/llvm/llvm-project/issues/62169

---------

Co-authored-by: Carlos Gálvez <carlos.galvez@zenseact.com>
2024-02-01 21:16:24 +01:00
Piotr Zegar
6a80e56ad0
[clang-tidy] Fix macros handling in cppcoreguidelines-prefer-member-initializer (#72037)
Produces now valid fixes for a member variables initialized with macros.
Correctly uses expansion location instead of location inside macro to
get init code.

Close #70189
2024-01-22 16:17:33 +01:00
Qizhi Hu
d08482924e
[clang-tidy] fix false positive in cppcoreguidelines-missing-std-forward (#77056)
Parameter variable which is forwarded in lambda capture list or in body
by reference is reasonable and current version of this check produces
false positive on these cases. This patch try to fix the
[issue](https://github.com/llvm/llvm-project/issues/68105)

Co-authored-by: huqizhi <836744285@qq.com>
2024-01-06 19:50:00 +08:00
Congcong Cai
43e13fdc9e
[NFC][clang-tidy]refactor isAssignmentToMemberOf in PreferMemberInitializerCheck (#71006) 2023-11-02 19:30:06 +08:00
Congcong Cai
263746754a
[clang-tidy]Fix PreferMemberInitializer false positive for reassignment (#70316)
- assignment twice cannot be simplified to once when assigning to
reference type
- safe assignment cannot be advanced before unsafe assignment
2023-10-27 23:56:25 +08:00
Piotr Zegar
c02b2ab8ab
[clang-tidy] Add StrictMode to cppcoreguidelines-pro-type-static-cast-downcast (#69529)
Add StrictMode option that controls behavior whatever
warnings are emitted for casts on non-polymorphic types.

Fixes: #69414
2023-10-26 21:06:19 +02:00
Congcong Cai
f5063bf7ed [clang-tidy][NFC]refactor PreferMemberInitializerCheck for readability 2023-10-26 13:37:50 +08:00
Piotr Zegar
fd06155acb
[clang-tidy] Improved cppcoreguidelines-narrowing-conversions.IgnoreConversionFromTypes (#69242)
Extended IgnoreConversionFromTypes option to include
types without a declaration, such as built-in types.
2023-10-26 07:16:25 +02:00
Piotr Zegar
af07d7ba88
[clang-tidy] Improved cppcoreguidelines-pro-type-const-cast (#69501)
Improved cppcoreguidelines-pro-type-const-cast check to ignore casts to
const type (controlled by option) and casts in implicitly invoked code.

Fixes #69319
2023-10-26 07:11:01 +02:00
Piotr Zegar
20d210285b
[clang-tidy] Ignore deleted functions in cppcoreguidelines-rvalue-reference-param-not-moved (#69514)
Ignore functions and constructors that are maked deleted or defaulted in
cppcoreguidelines-rvalue-reference-param-not-moved check.

Fixes #69412
2023-10-25 20:41:19 +02:00
AMS21
bb6a98c8d2
[clang-tidy] Ignore unused parameters in rvalue-reference-param-not-moved check (#69045)
With this patch we no longer issue a warning for unused parameters which
are marked as such.

This fixes #68209
2023-10-14 22:51:50 +02:00