Commit Graph

10522 Commits

Author SHA1 Message Date
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
Mythreya
d2d5203bf4 [clangd] Consistent precedence between --header-insertion and HeaderInsertion (#146235)
In PR #128503, the CLI option would take precedence over the config option
only if it was set to `never`. This commit ensures the CLI option always takes
precedence over the config option.
2025-06-29 10:47:49 -04:00
Kazu Hirata
402baea0a9 [modularize] Use std::tie to implement operator< (NFC) (#146220)
std::tie clearly expresses the intent while slightly shortening the
code.
2025-06-28 13:04:00 -07: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
Erick Velez
6d817810da [clang-doc] serialize IsBuiltIn and IsTemplate for types (#146149)
IsBuiltIn and IsTemplate were being emitted as their default values.
2025-06-27 16:17:39 -07:00
Erick Velez
ab1e4d55d8 [clang-doc] refactor BitcodeReader::readSubBlock (#145835)
Reduce boilerplate code in readSubBlock by creating a callable from a higher-order lambda based on the block's add need.
2025-06-26 20:44:14 -07:00
Erick Velez
066a14d4d4 [clang-doc] refactor JSONGenerator array usage (#145595)
Improve code reuse by calling serializeArray in more generic cases
instead of creating and reserving arrays on their own.
2025-06-26 10:11:02 -07:00
Kazu Hirata
87729bcbb8 [clangd] Migrate away from std::nullopt (NFC) (#145841)
ArrayRef has a constructor that accepts std::nullopt.  This
constructor dates back to the days when we still had llvm::Optional.

Since the use of std::nullopt outside the context of std::optional is
kind of abuse and not intuitive to new comers, I would like to move
away from the constructor and eventually remove it.

This patch replaces std::nullopt with {}.
2025-06-26 08:40:55 -07: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
Erick Velez
b8ea65025d [clang-doc] document global variables (#145070)
Visit and map VarDecls to document variables declared in namespace scope.
2025-06-24 11:36:31 -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
Vladimir Vuksanovic
a17b5bce8c [clang-reorder-fields] Prevent rewriting unsupported cases (#142149)
Add checks to prevent rewriting when doing so might result in incorrect
code. The following cases are checked:
- There are multiple field declarations in one statement like `int a, b`
- Multiple fields are created from a single macro expansion
- Preprocessor directives are present in the struct
2025-06-22 19:00:11 -07:00
Erick Velez
056b52df34 [clang-doc] Precommit test for global variables (#145069) 2025-06-21 11:56:35 -07: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
Erick Velez
8050a6e073 [clang-doc] add support for concepts (#144430)
Add support for documenting concepts. This handles concepts and constraints on function and class templates.

Atomic constraints are not considered yet. We don't order constraints based on their conjunctive or disjunctive properties.
2025-06-20 17:39:31 -07:00
Erick Velez
2dfcc4375f [clang-doc] Precommit concept tests (#144160) 2025-06-20 16:38:20 -07: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
Piotr Idzik
3c7df98c7b [clang-tidy] Add missing colon in the docs of performance-enum-size (#144525)
There is a syntax error in the provided code example - this PR fixes it.

I did a quick search - I could not find similar _typos_.
2025-06-17 21:59:53 +01:00
someoneinjd
8513066f2c [clangd] Implement LSP 3.17 positionEncoding (#142903)
This PR adds support for the `positionEncoding` client capability
introduced in LSP 3.17. Clangd can now negotiate the position encoding
with the client during initialization.

Fix https://github.com/clangd/clangd/issues/1746

Co-authored-by: kadir çetinkaya <kadircetinkaya.06.tr@gmail.com>
2025-06-17 19:23:14 +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
Aaron Ballman
ddea4fe85a Fix some "not all control paths return" warnings; NFC 2025-06-16 07:51:25 -04:00
Vladimir Vuksanovic
34c85ed2bc [clang-reorder-fields] Use expanded location for macros (#142147)
Fixes macros being replaced instead of their expansion.

Closes #52632
2025-06-15 18:07:51 -04:00
Kazu Hirata
2669664605 [modularize] Use range-based for loops (NFC) (#144244) 2025-06-15 10:32:30 -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
Erick Velez
7f69cd578d [clang-doc] remove default label on some switches (#143919)
LLVM style prefers no default label on fully covered switches to warn if
new enums are added. This patch removes the default label for that
purpose or uses IT_default instead of default if that was the only enum
not covered.
2025-06-13 16:35:30 -07:00
Aleksandr Platonov
ca5040990e [clangd] Collect references in array designators (#140356) 2025-06-13 18:32:42 +03:00
Aaron Ballman
9eef4d1c5f Remove delayed typo expressions (#143423)
This removes the delayed typo correction functionality from Clang
(regular typo correction still remains) due to fragility of the
solution.

An RFC was posted here:
https://discourse.llvm.org/t/rfc-removing-support-for-delayed-typo-correction/86631
and while that RFC was asking for folks to consider stepping up to be
maintainers, and we did have a few new contributors show some interest,
experiments show that it's likely worth it to remove this functionality
entirely and focus efforts on improving regular typo correction.

This removal fixes ~20 open issues (quite possibly more), improves
compile time performance by roughly .3-.4%
(https://llvm-compile-time-tracker.com/?config=Overview&stat=instructions%3Au&remote=AaronBallman&sortBy=date),
and does not appear to regress diagnostic behavior in a way we wouldn't
find acceptable.

Fixes #142457
Fixes #139913
Fixes #138850
Fixes #137867
Fixes #137860
Fixes #107840
Fixes #93308
Fixes #69470
Fixes #59391
Fixes #58172
Fixes #46215
Fixes #45915
Fixes #45891
Fixes #44490
Fixes #36703
Fixes #32903
Fixes #23312
Fixes #69874
2025-06-13 06:45:40 -04: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
Longsheng Mou
52360d195b [NFC] Use llvm::includes instead of std::includes (#143542)
This PR follows up #143297.
2025-06-12 09:27:27 +08: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
Erick Velez
47918e7cb7 [clang-doc] add namespaces to JSON generator (#143209)
Emit namespaces to JSON. Also adds tests for namespaces and non-member constructs.
2025-06-10 10:35:53 -07:00
Erick Velez
1c3320cdde [clang-doc] add a JSON generator (#142483)
Adds a JSON generator backend to emit mapped information as JSON. This will enable a better testing format for upcoming changes. It can also potentially serve to feed our other backend generators in the future, like Mustache which already serializes information to JSON before emitting as HTML.

This patch contains functionality to emit classes and provides most of the basis of the generator.
2025-06-10 08:39:42 -07:00
Hans Wennborg
5471d933af Disable clangd/test/module_dependencies.test on Windows
The test fails (sometimes); see discussion on https://github.com/llvm/llvm-project/pull/142828
2025-06-10 13:04:51 +02: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
Nathan Ridge
392bd577e3 [clangd] Guard against trivial FunctionProtoTypeLoc when creating inlay hints (#143087)
Fixes https://github.com/llvm/llvm-project/issues/142608
2025-06-09 00:33:20 -04:00
Kazu Hirata
240ff854ad [clangd] Use llvm::find (NFC) (#143317) 2025-06-08 16:18:16 -07: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
Kazu Hirata
0613f8b9e4 [clang-move] Teach getDeclarationList to return ArrayRef (NFC) (#143278)
getDeclarationList is used only for read-only access to the array.  I
don't think it's actually meant to return by value.
2025-06-08 01:34:02 -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