Commit Graph

10531 Commits

Author SHA1 Message Date
Kazu Hirata
838b91d7f6 [clangd] Drop const from a return type (NFC) (#146623)
We don't need const on a return type.
2025-07-01 22:49:56 -07: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
Ilya Biryukov
cf9374933d [Modularize] Make Location::operator bool explicit
This unbreaks C++20 buildbot that was broken since
402baea0a9.

With implicit conversion in C++20 compilation mode the spaceship
will unintentionally be based on `operator bool`:

```cpp
auto foo(Location L, Location R) {
  return L <=> R;
  // Equivalent to the following line due to implicit conversions.
  // return L.operator bool() <=> R.operator bool();
}
```

The spaceship operator is rarely used explicitly, but its implicit uses
in the STL may cause surprising results, as exposed by the use of  `std::tie`
in 402baea0a9, which ended up changing the
comparisons results unintentionally.
2025-07-01 16:38:07 +02:00
Mythreya
d9d9ab8698 [clang][CodeComplete] skip explicit obj param in code completion string (#146258)
Fixes clangd/clangd#2339
2025-07-01 08:33:56 -04:00
Haojian Wu
2ee884a9db [clang-doc] Remove the unused clangd header.
This header seems to be included unintentionally.
2025-07-01 12:07:35 +02:00
Erick Velez
a68e4470c1 [clang-doc] serialize friends (#146165)
Parse friends into a new FriendInfo and serialize them in JSON. We keep track of the friend declaration's template and function information if applicable.
2025-06-30 12:43:52 -07: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
Erick Velez
ba84d0c8d7 [clang-doc] Precommit friends test (#146164) 2025-06-30 11:47:09 -07:00
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