10294 Commits

Author SHA1 Message Date
Baranov Victor
2909c420f6
[clang-tidy] modernize-use-starts-ends-with: fix false positives on find and rfind (#129564)
Also document cases with two or three arguments (matching default arguments)
this check matches.

Closes https://github.com/llvm/llvm-project/issues/129498.
2025-03-22 08:07:58 -04:00
Mohamed Emad
98dc8bb892
[clang-doc][NFC] fix description typo (#132310)
Fix the description typo for `JsScripts` in the context

discussed in #131280
2025-03-21 16:08:14 -07:00
Matheus Izvekov
14f7bd63b9
Reland: [clang] preserve class type sugar when taking pointer to member (#132401)
Original PR: #130537
Originally reverted due to revert of dependent commit. Relanding with no
changes.

This changes the MemberPointerType representation to use a
NestedNameSpecifier instead of a Type to represent the base class.

Since the qualifiers are always parsed as nested names, there was an
impedance mismatch when converting these back and forth into types, and
this led to issues in preserving sugar.

The nested names are indeed a better match for these, as the differences
which a QualType can represent cannot be expressed syntatically, and
they represent the use case more exactly, being either dependent or
referring to a CXXRecord, unqualified.

This patch also makes the MemberPointerType able to represent sugar for
a {up/downcast}cast conversion of the base class, although for now the
underlying type is canonical, as preserving the sugar up to that point
requires further work.

As usual, includes a few drive-by fixes in order to make use of the
improvements.
2025-03-21 13:20:52 -03:00
Matheus Izvekov
1416566449
Reland: [clang] NFC: Clear some uses of MemberPointerType::getClass (#132317)
Relands Original PR: https://github.com/llvm/llvm-project/pull/131965
Addresses
https://github.com/llvm/llvm-project/pull/131965#issuecomment-2741619498
* Fixes isIncompleteType for injected classes

This clears up some uses of getClass on MemberPointerType when
equivalent uses of getMostRecentCXXRecordDecl would be just as simple or
simpler.
    
This is split-off from a larger patch which removes getClass, in order
to facilitate review.
2025-03-21 10:54:24 -03:00
Paul Kirth
3fbc9b9efc
[clang-doc] Correct improper file paths in HTML output (#132103)
In index.js the logic of the ternary operator was backwards, preventing
us from generating the correct file paths, or relative paths in the HTML
output.
2025-03-20 14:11:42 -07:00
Paul Kirth
3923a6b09c
[clang-doc][NFC] Remove unnecessary directory cleanup (#132101)
The tests all remove the directory at test start, and it only prevents
inspecting the test artifacts to remove them at the end of the test run.
2025-03-20 14:08:46 -07:00
Paul Kirth
a87a64b2e4
[clang-doc] Avoid deref of invalid std::optional (#131939)
Since our existing guard is insufficient to prevent accessing the
std::optional when in an invalid state, guard the access with
`.value_or()`. This maintains the desired behavior, without running into
UB.

The new test should prevent regressions.

Fixes #131697
2025-03-20 14:04:58 -07:00
Matheus Izvekov
335a4614de
Revert "[clang] NFC: Clear some uses of MemberPointerType::getClass" (#132281)
Reverts llvm/llvm-project#131965

Reverted due to issue reported here:
https://github.com/llvm/llvm-project/pull/131965#issuecomment-2741619498
2025-03-20 17:54:21 -03:00
Matheus Izvekov
5151e6d7fe
Revert "Reland: [clang] preserve class type sugar when taking pointer to member" (#132280)
Reverts llvm/llvm-project#132234

Needs to be reverted due to dependency.

This blocks reverting another PR, see here:
https://github.com/llvm/llvm-project/pull/131965#issuecomment-2741619498
2025-03-20 17:52:48 -03:00
Matheus Izvekov
578f38cd08
Reland: [clang] preserve class type sugar when taking pointer to member (#132234)
Original PR: #130537
Reland after updating lldb too.

This changes the MemberPointerType representation to use a
NestedNameSpecifier instead of a Type to represent the base class.

Since the qualifiers are always parsed as nested names, there was an
impedance mismatch when converting these back and forth into types, and
this led to issues in preserving sugar.

The nested names are indeed a better match for these, as the differences
which a QualType can represent cannot be expressed syntatically, and
they represent the use case more exactly, being either dependent or
referring to a CXXRecord, unqualified.

This patch also makes the MemberPointerType able to represent sugar for
a {up/downcast}cast conversion of the base class, although for now the
underlying type is canonical, as preserving the sugar up to that point
requires further work.

As usual, includes a few drive-by fixes in order to make use of the
improvements.
2025-03-20 15:33:54 -03:00
Matheus Izvekov
6cd62ad08c
Revert "[clang] improve class type sugar preservation in pointers to members" (#132215)
Reverts llvm/llvm-project#130537

This missed updating lldb, which we didn't notice due to lack of
pre-commit CI.
2025-03-20 11:09:25 -03:00
Matheus Izvekov
9b1f905b48
[clang] improve class type sugar preservation in pointers to members (#130537)
This changes the MemberPointerType representation to use a
NestedNameSpecifier instead of a Type to represent the class.

Since the qualifiers are always parsed as nested names, there was an
impedance mismatch when converting these back and forth into types, and
this led to issues in preserving sugar.

The nested names are indeed a better match for these, as the differences
which a QualType can represent cannot be expressed syntactically, and it
also represents the use case more exactly, being either dependent or
referring to a CXXRecord, unqualified.

This patch also makes the MemberPointerType able to represent sugar for
a {up/downcast}cast conversion of the base class, although for now the
underlying type is canonical, as preserving the sugar up to that point
requires further work.

As usual, includes a few drive-by fixes in order to make use of the
improvements, and removing some duplications, for example
CheckBaseClassAccess is deduplicated from across SemaAccess and
SemaCast.
2025-03-20 10:30:24 -03:00
Mythreya
9cdbc47144
[clangd] Add HeaderInsertion config option (#128503)
This is the config file equivalent of the `--header-insertion` command line option

Fixes https://github.com/clangd/clangd/issues/2032
2025-03-20 02:22:29 -04:00
Matheus Izvekov
fd7be0d2e9
[clang] NFC: Clear some uses of MemberPointerType::getClass (#131965) 2025-03-19 21:36:10 -03:00
Paul Kirth
77ac5a2d57
[clang-doc] Enable MD tests in basic-project (#131924)
For some reason the MD tests don't appear to have ever run, despite
having checks. This patch adds a new set of RUN lines that will
exercise the markdown generation.
2025-03-19 16:14:24 -07:00
Alexander Shaposhnikov
297f0b3f4c
[CudaSPIRV] Allow using integral non-type template parameters as attribute args (#131546)
Allow using integral non-type template parameters as attribute arguments
of
reqd_work_group_size and work_group_size_hint.

Test plan:
ninja check-all
2025-03-19 10:11:18 -07:00
Paul Kirth
93b7c1b4b2
[clang-doc] Add testing without --repository for HTML documentation (#131894)
https://github.com/llvm/llvm-project/issues/131697 reported a crash when
--repository was unused, so make sure we test the project in both
configurations.
2025-03-19 08:24:10 -07:00
Aaron Ballman
449cdfacc0
Suppress pedantic diagnostic for a file not ending in EOL (#131794)
WG14 added N3411 to the list of papers which apply to older versions of
C in C2y, and WG21 adopted CWG787 as a Defect Report in C++11. So we no
longer should be issuing a pedantic diagnostic about a file which does
not end with a newline character.

We do, however, continue to support -Wnewline-eof as an opt-in
diagnostic.
2025-03-19 07:49:16 -04:00
Jan Voung
6f659b0060
[clang][dataflow] For bugprone-unchecked-optional-access report range (#131055)
Report the range in diagnostics, in addition to the location
in case the range helps disambiguate a little in chained `->`
expressions.
```
b->a->f->x = 1;
^~~~~~~
```
instead of just:
```
b->a->f->x = 1;
^
```
As a followup we should probably also report the location/range
of an `->` if that operator is used. Like:
```
b->a->f->x = 1;
       ^~
```
2025-03-17 16:04:15 -04:00
Julian Ruess
ccfabe8380
[clang-tidy-diff] Add an option to treat warnings as errors (#128221)
This patch allows to treat warnings as erros using clang-tidy-diff.

Co-authored-by: Piotr Zegar <me@piotrzegar.pl>
2025-03-17 15:55:40 +08:00
Congcong Cai
3b1e18c2db
[clang-tidy] Add new check bugprone-capture-this-by-field (#130297)
Finds lambda captures that capture the ``this`` pointer and store it as
class
members without handle the copy and move constructors and the
assignments.

Capture this in a lambda and store it as a class member is dangerous
because the
lambda can outlive the object it captures. Especially when the object is
copied
or moved, the captured ``this`` pointer will be implicitly propagated to
the
new object. Most of the time, people will believe that the captured
``this``
pointer points to the new object, which will lead to bugs.

Fixes: #120863

---------

Co-authored-by: Baranov Victor <70346889+vbvictor@users.noreply.github.com>
Co-authored-by: Baranov Victor <bar.victor.2002@gmail.com>
2025-03-17 15:11:43 +08:00
David Rivera
5e65b40f9c
[clang-tidy] detect explicit casting within modernize-use-default-member-init (#129408)
This aims to fix a portion of #122480. Added some matchers to detect
explicit casting which utilize builtin types as its source expression.
these are the various forms of casting supported I thought would useful
for this check:

- C Style explicit casting
- Static explicit casting
- Functional explicit casting
2025-03-15 23:15:12 +01:00
Baranov Victor
471f034b1c
[clang-tidy][NFC] clean ReleaseNotes.rst (#130626)
1. Fixed ordering to be alphabetical
2. Made lines no more than 80 characters long
3. Fixed typos
2025-03-14 23:31:50 +01:00
Jan Svoboda
c84d8e8f1c
[clang][modules] Introduce new ModuleCache interface (#131193)
This PR adds new `ModuleCache` interface to Clang's implicitly-built
modules machinery. The main motivation for this change is to create a
second implementation that uses a more efficient kind of
`llvm::AdvisoryLock` during dependency scanning.

In addition to the lock abstraction, the `ModuleCache` interface also
manages the existing `InMemoryModuleCache` instance. I found that
compared to keeping these separate/independent, the code is a bit
simpler now, since these are two tightly coupled concepts. I can
envision a more efficient implementation of the `InMemoryModuleCache`
for the single-process case too, which will be much easier to implement
with the current setup.

This is not intended to be a functional change.
2025-03-14 11:32:39 -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
Carlos Galvez
e4a8969e56
[clang-tidy] Avoid processing declarations in system headers (#128150)
[clang-tidy] Avoid processing declarations in system headers

Currently, clang-tidy processes the entire TranslationUnit, including
declarations in system headers. However, the work done in system
headers is discarded at the very end when presenting results, unless
the SystemHeaders option is active.

This is a lot of wasted work, and makes clang-tidy very slow.
In comparison, clangd only processes declarations in the main file,
and it's claimed to be 10x faster than clang-tidy:

https://github.com/lljbash/clangd-tidy

To solve this problem, we can apply a similar solution done in clangd
into clang-tidy. We do this by changing the traversal scope from the
default TranslationUnitDecl, to only contain the top-level declarations
that are _not_ part of system headers. We do this in the
MatchASTConsumer class, so the logic can be reused by other tools.
This behavior is currently off by default, and only clang-tidy
enables skipping system headers. If wanted, this behavior can be
activated by other tools in follow-up patches.

I had to move MatchFinderOptions out of the MatchFinder class,
because otherwise I could not set a default value for the
"bool SkipSystemHeaders" member otherwise. The compiler error message
was "default member initializer required before the end of its
enclosing class".

Note: this behavior is not active if the user requests warnings from
system headers via the SystemHeaders option.

Note2: out of all the unit tests, only one of them fails:

readability/identifier-naming-anon-record-fields.cpp

This is because the limited traversal scope no longer includes the
"CXXRecordDecl" of the global anonymous union, see:
https://github.com/llvm/llvm-project/issues/130618

I have not found a way to make this work. For now, document the
technical debt introduced.

Note3: I have purposely decided to make this new feature enabled by
default, instead of adding a new "opt-in/opt-out" flag. Having a new
flag would mean duplicating all our tests to ensure they work in both
modes, which would be infeasible. Having it enabled by default allow
people to get the benefits immediately. Given that all unit tests pass,
the risk for regressions is low. Even if that's the case, the only
issue would be false negatives (fewer things are detected), which
are much more tolerable than false positives.

Credits: original implementation by @njames93, here:
https://reviews.llvm.org/D150126

This implementation is simpler in the sense that it does not consider
HeaderFilterRegex to filter even further. A follow-up patch could
include the functionality if wanted.

Fixes #52959

Co-authored-by: Carlos Gálvez <carlos.galvez@zenseact.com>
2025-03-14 13:16:17 +01:00
Congcong Cai
0e4ba47ca8
[clang-tidy] support to detect conversion in make_optional for bugprone-optional-value-conversion (#130417)
Add support for std::make_optional.

Fixes #119554
2025-03-12 06:12:09 +08:00
Jan Svoboda
eaca60d1a9
[clang] Hide the DiagnosticOptions pointer from CompilerInvocation (#106274)
This PR hides the reference-counter pointer that holds
`DiagnosticOptions` from the public API of `CompilerInvocation`. This
gives `CompilerInvocation` an exclusive control over the lifetime of
this member, which will eventually be leveraged to implement a
copy-on-write behavior.

The only client that currently accesses that pointer is
`clangd::buildPreamble()` which takes care to reset it so that it's not
reset concurrently. This code is made redundant by making the reference
count of `DiagnosticOptions` atomic.
2025-03-11 10:29:39 -07:00
Tommy Chen
d22d14375d
[clang-tidy] support different precisions (#130540)
Support float and long double versions of the math functions for
UseStdNumbersCheck.
For example, after this commit the check is able to catch `sqrtf(2)` and
`expl(1)`.

Fixes: #130325
2025-03-11 18:05:01 +01:00
David Rivera
517c6778ea
[clang-tidy] Add check on constexpr & static values in modernize-use-default-member-init (#129425)
This aims to fix a portion of #122480. Simply added a check to look for
either compile time initialized variables or static stored variables.
2025-03-11 18:00:43 +01:00
Congcong Cai
5cfc37b345
[clang-tidy][NFC]clean ConstCorrectnessCheck (#130493) 2025-03-11 14:53:06 +08:00
Congcong Cai
318bef91ea
[clang-tidy][NFC]refactor matcher for bugprone-optional-value-conversion (#130415)
The old `constructFrom` has hidden requirement which TypeMatcher must be used before ArgumentMatcher because there are bind inside.
Inlining this function to make it more intuitive.
2025-03-11 14:52:18 +08:00
Baranov Victor
75f76d482c
[clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (#121291)
Add new clang-tidy check that finds potentially erroneous calls to
``reset()`` method on smart pointers when
the pointee type also has a ``reset()`` method.

It's easy to make typo and delete object because the difference between
``.`` and ``->`` is really small.

Sometimes IDE's autocomplete will change ``->`` to ``.`` automatically.
For example, developer wrote ``ptr->res`` but after pressing _Tab_ it
became ``ptr.reset()``.

Fixes #120908
2025-03-11 07:22:15 +01:00
Andrewyuan34
2a3e782f40
[clang-tidy] Fix invalid fixit from modernize-use-ranges for nullptr used with std::unique_ptr (#127162)
This PR fixes issue #124815 by correcting the handling of `nullptr` with
`std::unique_ptr` in the `modernize-use-ranges` check.

Updated the logic to suppress warnings for `nullptr` in `std::find`.
2025-03-09 20:09:59 +08:00
Khalil Estell
85d60a441a
[clangd] Add BuiltinHeaders config option (#129459)
This option, under `CompileFlags`, governs whether clangd uses its own
built-in headers (`Clangd` option value) or the built-in headers of the driver
in the file's compile command (`QueryDriver` option value, applicable to
cases where `--query-driver` is used to instruct clangd to ask the driver
for its system include paths).

The default value is `Clangd`, preserving clangd's current defaut behaviour.

Fixes clangd/clangd#2074
2025-03-08 19:16:37 -05: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
Aaron Ballman
b19ed9c043
[C2y] Implement WG14 N3409 (#130299)
This paper removes UB around use of void expressions. Previously, code
like this had undefined behavior:
```
  void foo(void) {
    (void)(void)1;
    extern void x;
    x;
  }
```
and this is now well-defined in C2y. Functionally, this now means that
it is valid to use `void` as a `_Generic` association.
2025-03-07 14:46:29 -05:00
erichkeane
52552ce53b Reapply "[NFC] Change the return type of getTraversalScope to ArrayRef"
vitalybuka identified a fix here that fixes the issue, and lets us make
fewer copies!  This applies his patch plus reapplys the original.

This reverts commit c4c29b95a6e6809017e71e85f33faecfe85d88b2.
2025-03-06 09:43:37 -08:00
kadir çetinkaya
8c61ef17c3
[clangd] Explicitly block until async task completes (#130077)
We started seeing some use-after-frees starting with
https://github.com/llvm/llvm-project/pull/125433.

This patch ensures we explicitly block for the async task, if there's
one, before destructing `std::future`, independent of the stdlib
implementation.
2025-03-06 13:38:07 +01:00
Nathan Ridge
8266cd9f84
[clangd] Disable cppcoreguidelines-macro-to-enum clang-tidy checker (#129478)
Clangd does not support its checker because the checker relies on having
seen preprocessor conditionals that occur in the preamble, and clangd
does not currently replay those.

This checker was already disabled under its main name,
modernize-macro-to-enum (https://github.com/clangd/clangd/issues/1464).
This commit disables it under the alternative name
cppcoreguidelines-macro-to-enum as well.

Fixes https://github.com/llvm/llvm-project/issues/127965
2025-03-04 03:46:54 -05: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
Congcong Cai
60afce2df9
[clang-tidy] fix fp when modifying variant by `operator[]` with template in parameters (#128407)
`ArraySubscriptExpr` can switch base and idx. For dependent array
subscript access, we should check both base and idx conservatively.
2025-03-02 19:39:49 +08:00
Marco C.
dd3c4fbec9
[clang-tidy][doc] Contributing.rst update snippet and docs (#129209)
This reflects the add_new_check.py changes: isLanguageVersionSupported
is now overridden by default by the script

The changes were instroduced in
https://github.com/llvm/llvm-project/pull/100129

Thanks
2025-02-28 20:18:13 +01:00
Valentyn Yukhymenko
818bca820f
[clang-tidy] [dataflow] Cache reference accessors for bugprone-unchecked-optional-access (#128437)
Fixes https://github.com/llvm/llvm-project/issues/126283

Extending https://github.com/llvm/llvm-project/pull/112605 to cache
const getters which return references.

Fixes false positives from const reference accessors to object
containing optional member
2025-02-28 13:27:20 -05:00
Ujan RoyBandyopadhyay
440ea3ecdc
[clangd] Reduce superfluous rename conflicts (#121515)
This commit adds a namespace check to the code for detecting name
collisions, allowing `bar` to be renamed to `foo` in the following
snippet:

```c
typedef struct foo {} Foo;
Foo bar;
```

Previously, such a rename would fail because a declaration for `foo`
already exists in the same scope.
2025-02-27 13:06:03 -06:00
Jan Voung
eeb672a47c
[clang-tidy] Add a release note about unchecked-optional-access smart pointer caching (#122290)
With caching added in https://github.com/llvm/llvm-project/pull/120249,
the `IgnoreSmartPointerDereference` option shouldn't be needed anymore.

Other caching also added earlier:
https://github.com/llvm/llvm-project/pull/112605
2025-02-27 10:35:24 -05:00
Congcong Cai
56762b7ace
[clang-tidy] Add new check bugprone-unintended-char-ostream-output (#127720)
It wants to find unintended character output from `uint8_t` and `int8_t`
to an ostream.
e.g.
```c++
uint8_t v = 9;
std::cout << v;
```

---------

Co-authored-by: whisperity <whisperity@gmail.com>
2025-02-27 19:36:24 +08:00
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
Chuanqi Xu
ae839b0250
[clangd] [C++20] [Modules] Add scanning cache (#125988)
Previously, everytime we want to get a source file declaring a specific
module, we need to scan the whole projects again and again. The
performance is super bad. This patch tries to improve this by
introducing a simple cache.
2025-02-26 16:03:04 +08:00