Commit Graph

1712 Commits

Author SHA1 Message Date
Julian Schmidt
a776bd1a92 [clang-tidy] support return c ? a : b; in bugprone-return-const-ref-from-parameter (#107657)
A `const &` parameter can also be returned via a conditional operator:
`c ? a : b`. This change adds support for diagnosing returning these
parameters
with conditional operators.
2024-10-31 09:13:51 +01:00
z1nke
27ef549af2 [clang-tidy] Fix crash in modernize-use-designated-initializers check (#113688)
Fix #113652.

When calling `Node.isAggregate()` and `Node.isPOD()`, if `Node` is declared but
not defined, it will result in null pointer dereference (and if assertions are
enabled, it will cause an assertion failure).
2024-10-29 15:48:39 -04:00
Discookie
f5ff3a560f [clang-tidy] Do not emit file path for anonymous enums in readability-enum-initial-value check (#112496)
Previously the name of anonymous enums in the check were `enum 'enum
(unnamed at /full/path/to/file.c:1:1)'`, which breaks reproducibility of
clang-tidy reports when the analyzed project is in a different folder.
2024-10-28 11:19:22 +00:00
vabridgers
3d6923dbac RFC: [clang-tidy] [analyzer] Move nondeterministic pointer usage check to tidy (#110471)
This change moves the `alpha.nondeterministic.PointerSorting` and
`alpha.nondeterministic.PointerIteration` static analyzer checkers to a
single `clang-tidy` check. Those checkers were implemented as simple
`clang-tidy` check-like code, wrapped in the static analyzer framework.
The documentation was updated to describe what the checks can and cannot
do, and testing was completed on a broad set of open-source projects.

Co-authored-by: Vince Bridgers <vince.a.bridgers@ericsson.com>
2024-10-28 03:53:36 -05:00
Congcong Cai
a1d31caa8c [clang-tidy] fix false positive for implicit conversion of comparison result in C23 (#113639)
Fixed #111013
bool will be builtin type in C23 but comparison result in C is still
int.
It is no need to change this kind of implicit cast to explicit cast.
2024-10-28 11:36:37 +08:00
Nicolas van Kempen
3605d9a456 [clang-tidy][readability-container-contains] Fix matching of non-binaryOperator cases (#110386)
Fix #79437.
2024-10-23 14:57:09 -04: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
Jan Voung
6761b24ae2 [clang][dataflow] Cache accessors for bugprone-unchecked-optional-access (#112605)
Treat calls to zero-param const methods as having stable return values
(with a cache) to address issue #58510. The cache is invalidated when
non-const methods are called. This uses the infrastructure from PR
#111006.

For now we cache methods returning:
- ref to optional
- optional by value
- booleans

We can extend that to pointers to optional in a next change.
2024-10-22 10:18:22 -04:00
Tor Shepherd
2eb1699184 [clangd] Add inlay hints for default function arguments (#95712)
The new inlay hints have the `DefaultArguments` kind and can be enabled in config similar to other inlay kint kinds.
2024-10-19 18:19:01 -04:00
Nick Riasanovsky
6d7712a70c [clang-tidy][docs] Replace _not_ in reserved-identifier.rst with *not* (#112162)
Fixes a documentation formatting error where `_not_` was used which has no
special meaning in reST and replaces it with `*not*`.

Closes #111691.
2024-10-17 12:42: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
whisperity
1c38c46b08 [clang-tidy] Make P +- BS / sizeof(*P) opt-outable in bugprone-sizeof-expression (#111178)
In some cases and for projects that deal with a lot of low-level buffers, a
pattern often emerges that an array and its full size, not in the number of
"elements" but in "bytes", are known with no syntax-level connection between
the two values.
To access the array elements, the pointer arithmetic involved will have
to divide 'SizeInBytes' (a numeric value) with `sizeof(*Buffer)`.
Since the previous patch introduced this new warning, potential
false-positives were triggered from `bugprone-sizeof-expression`, as `sizeof`
appeared in pointer arithmetic where integers are scaled.

This patch adds a new check option, `WarnOnOffsetDividedBySizeOf`, which allows
users to opt out of warning about the division case.
In arbitrary projects, it might still be worthwhile to get these warnings until
an opt-out from the detection of scaling issues, especially if a project
might not be using low-level buffers intensively.
2024-10-15 14:42:57 +02:00
Nicolas van Kempen
3484ed9325 [docs][clang-tools-extra] Fix broken document link in ReleaseNotes.rst
Fixes the following error:
```
clang-tools-extra/docs/ReleaseNotes.rst:247: WARNING: unknown document: 'clang-tidy/checks/readability/readability-identifier-naming' [ref.doc]
```
2024-10-14 18:43:33 -04:00
Keith Smiley
c79e5acfe8 [clang-tidy][readability-identifier-naming] Support namespace aliases (#112112)
Fixes: https://github.com/llvm/llvm-project/issues/109385
2024-10-14 09:35:20 -07:00
Christian Kandeler
0cfa6e2092 [clangd] Let DefineOutline tweak handle member functions (#95235)
... of class templates.
2024-10-14 11:00:02 +02:00
Nicolas van Kempen
f1367a473d [clang-tidy][modernize-use-starts-ends-with] Add support for two ends_with patterns (#110448)
Add support for the following two patterns:
```
haystack.compare(haystack.length() - needle.length(), needle.length(), needle) == 0;
haystack.rfind(needle) == (haystack.size() - needle.size());
```
2024-10-11 21:00:38 -04:00
isuckatcs
6d8e966512 [clang-tidy] Portability Template Virtual Member Function Check (#110099)
Introduced a new check that finds cases when an uninstantiated virtual member function in a template class causes cross-compiler incompatibility.
2024-10-10 12:32:39 +02:00
Nicolas van Kempen
0aaac4fe19 [clang-tidy][performance-move-const-arg] Fix crash when argument type has no definition (#111472)
Fix #111450.
2024-10-09 18:16:43 -04:00
Mike Crowe
a199fb1229 [clang-tidy] Only expand <inttypes.h> macros in modernize-use-std-format/print (#97911)
Expanding all macros in the printf/absl::StrFormat format string before
conversion could easily break code if those macros are expanded change
their definition between builds. It's important for this check to expand
the <inttypes.h> PRI macros though, so let's ensure that the presence of
any other macros in the format string causes the check to emit a warning
and not perform any conversion.
2024-10-08 22:03:58 +02:00
hill
adbc37d999 [clang-tidy] Fix incorrect command-line option in docs (#111405)
Updated the `HeaderFilterRegex` description to reference
`--header-filter` instead of the incorrect `--header-filter-regex` in
the clang-tidy documentation.
2024-10-07 20:37:00 +02:00
Carlos Galvez
fb0ef6b66e [clang-tidy] Create bugprone-bitwise-pointer-cast check (#108083)
To detect unsafe usages of casting a pointer to another via copying
the bytes from one into the other, either via std::bit_cast or via
memcpy. This is currently not caught by any other means.

Fixes #106987

---------

Co-authored-by: Carlos Gálvez <carlos.galvez@zenseact.com>
2024-10-06 12:21:09 +02:00
Julian Schmidt
4f0ad8d80a [clang-tidy] support static analyzer checker configuration in --verify-config (#109523)
Previously the configuration options for the Clang Static Analyzer
checkers were not recognized for the verification of the configuration
with `--verify-config`.
Add all available configuration options of CSA checkers as possible
configuration options in a clang-tidy config.
2024-10-03 19:10:36 +02:00
Tor Shepherd
81fcdc6359 [clangd] Add CodeAction to swap operands to binary operators (#78999)
This MR resolves https://github.com/llvm/llvm-project/issues/78998
2024-10-03 18:34:02 +02:00
Congcong Cai
e984d11d72 [clang-tidy] loop convert can handle lambda init capture (#109159)
Fixes: #109083
Current implement ignore the whole lambda capture. This patch wants to
do traverse for capture init expr also
2024-10-03 16:16:49 +08:00
Mike Crowe
aadfba9b2a [clang-tidy] Fix check alphabetical ordering in release notes (#110428) 2024-10-02 09:10:45 +02:00
tigbr
7b8f7beadc [clang-tidy] Add new check bugprone-tagged-union-member-count (#89925)
This patch introduces a new check to find mismatches between the number
of data members in a union and the number enum values present in
variant-like structures.

Variant-like types can look something like this:

```c++
struct variant {
    enum {
        tag1,
        tag2,
    } kind;
    union {
        int i;
        char c;
    } data;
};
```

The kind data member of the variant is supposed to tell which data
member of the union is valid, however if there are fewer enum values
than union members, then it is likely a mistake.

The opposite is not that obvious, because it might be fine to have more
enum values than union data members, but for the time being I am curious
how many real bugs can be caught if we give a warning regardless.

This patch also contains a heuristic where we try to guess whether the
last enum constant is actually supposed to be a tag value for the
variant or whether it is just holding how many enum constants have been
created.

Patch by Gábor Tóthvári!
2024-10-01 13:24:32 +02:00
Congcong Cai
d9853a8a10 [clang-tidy][bugprone-posix-return] support integer literals as LHS (#109302)
Refactor matches to give more generic checker.

---------

Co-authored-by: EugeneZelenko <eugene.zelenko@gmail.com>
2024-09-27 10:05:37 +08:00
Discookie
0b8866d15a [clang-tidy] Add user-defined functions to bugprone-unsafe-functions check (#106350)
Adds the check option `bugprone-unsafe-functions.CustomFunctions` to be
able to match user-defined functions as part of the checker.
Adds the option `bugprone-unsafe-functions.ReportDefaultFunctions` to
disable reporting the default set of functions as well.

The functions names are matched using the same mechanism as the
`matchesAnyListedName` tidy matcher, documented in
`unsafe-functions.rst`.
2024-09-26 12:52:13 +00:00
Thomas Köppe
cebb7c0108 [clang-tidy] modernize-use-nullptr matches "NULL" in templates (#109169)
Make modernize-use-nullptr matcher also match "NULL", but not "0", when
it appears on a substituted type of a template specialization.

Previously, any matches on a substituted type were excluded, but this
meant that a situation like the following is not diagnosed:

```c++
template <typename T>
struct X {
  T val;
  X() { val = NULL; }  // should diagnose
};
```

When the user says `NULL`, we expect that the destination type is always
meant to be a pointer type, so this should be converted to `nullptr`. By
contrast, we do not propose changing a literal `0` in that case, which
appears as initializers of both pointer and integer specializations in
reasonable real code. (If `NULL` is used erroneously in such a
situation, it should be changed to `0` or `{}`.)
2024-09-25 09:25:46 -07:00
Chris Cotter
11c423f9be [clang-tidy] Add support for bsl::optional (#101450) 2024-09-25 10:54:31 -04:00
MichelleCDjunaidi
ce9a2c6521 [clang-tidy][doc] Add external examples (#106675)
Clang has a page where they list out external examples:
https://clang.llvm.org/docs/ExternalClangExamples.html. This mimics this page by adding some useful links specific to clang-tidy.
2024-09-23 18:13:16 +02:00
Afonso Faria
c24418ad91 [clang-tidy] Add std::span to default bugprone-dangling-handle.HandleClasses (#107711)
`std::span` suffers from the same dangling issues as `std::string_view`.
This patch adds `std::span` to the default list of handle classes in
`bugprone-dangling-handle`, allowing clang-tidy to catch e.g. the
following:
```cpp
span<int> f() {
  // All these return values will dangle.
  array<int, 1> A;
  return {A};

  vector<int> Array;
  return {Array};
}
```
2024-09-21 00:23:17 +02:00
Congcong Cai
5fa742eeed [clang-tidy][NFC] add qutation mark for C++ classes in warning message (#109068)
As discussion in
https://github.com/llvm/llvm-project/pull/108555#discussion_r1761841192,
split quotation mark change in a new NFC PR.
It is more readable to use `'std::array'` than `std::array<>`
2024-09-19 22:46:16 +08:00
Aaron Ballman
ed8f788278 Remove clang-pseudo (#109154)
The functionality is incomplete and the authors have since shifted gears
to other work, so this is effectively unmaintained.

The original design document for clang-pseudo can be found at:

https://docs.google.com/document/d/1eGkTOsFja63wsv8v0vd5JdoTonj-NlN3ujGF0T7xDbM/edit
in case anyone wishes to pick this project back up again in the future.

Original RFC: https://discourse.llvm.org/t/removing-pseudo-parser/71131/
2024-09-19 07:54:06 -04:00
Nicolas van Kempen
1be4c9710b [clang-tidy][readability-container-contains] Extend to any class with contains (#107521)
This check will now work out of the box with other containers that have a
`contains` method, such as `folly::F14` or Abseil containers.

It will also work with strings, which are basically just weird containers.
`std::string` and `std::string_view` will have a `contains` method starting with
C++23. `llvm::StringRef` and `folly::StringPiece` are examples of existing 
implementations with a `contains` method.
2024-09-18 14:57:31 -04:00
Aaron Ballman
40c45b6b43 Remove clang-rename (#108988)
clang-rename has largely been superseded by clangd and this project
hasn't received much attention in many years. Further, our
documentation on it still claims it's in very early stages of
development despite being ~10 years old. One of the primary people
driving the tool has mentioned that they don't believe there is a
reason to continue to support it unless it's still being actively
used (https://reviews.llvm.org/D148439#4303202) and I've found no
evidence to suggest that is the case.

Original RFC:
https://discourse.llvm.org/t/rfc-time-to-deprecate-remove-clang-rename/70707
2024-09-18 08:25:58 -04:00
Congcong Cai
785624b282 [clang-tidy]suggest use std::span as replacement of c array in C++20 for modernize-avoid-c-arrays (#108555)
The incompleted C-Array in parameter does not own memory. Instead, It is
equivalent to pointer.
So we should not use `std::array` as recommended modern C++ replacement,
but use `std::vector` and `std::span` in C++20
2024-09-18 08:03:06 +08:00
Mainak Sil
2bda9e1c29 [docs][clang-tidy] Correct StrictMode example in modernize-use-std-print (#108805)
Fix #101397.
2024-09-17 12:33:27 -04:00
Zoltán Porkoláb
267ad430fc [clang-tidy] Extend bugprone-sizeof-expression with matching P +- sizeof(T) and P +- N */ sizeof(T) cases, add cert-arr39-c alias (#106061)
Improved `bugprone-sizeof-expression` check to find suspicious pointer
arithmetic calculations where the pointer is offset by an `alignof()`,
`offsetof()`, or `sizeof()` expression.

Pointer arithmetic expressions implicitly scale the offset added to or
subtracted from the address by the size of the pointee type. Using an
offset expression that is already scaled by the size of the underlying
type effectively results in a squared offset, which is likely an invalid
pointer that points beyond the end of the intended array.

```c
void printEveryEvenIndexElement(int *Array, size_t N) {
  int *P = Array;
  while (P <= Array + N * sizeof(int)) { // Suspicious pointer arithmetics using sizeof()!
    printf("%d ", *P);

    P += 2 * sizeof(int); // Suspicious pointer arithmetics using sizeof()!
  }
}
```

---------

Co-authored-by: Whisperity <whisperity@gmail.com>
2024-09-17 16:42:58 +02:00
Julian Schmidt
caaac84ab2 [clang-tidy] only diagnose definitions in readability-enum-initial-value (#107652)
With the `isDefinition` matcher, the analysis and diagnostics will be
constrained to definitions only. Previously forward declarations were
diagnosed as well.

Fixes #107590
2024-09-17 10:44:22 +02:00
Julian Schmidt
605a9adb43 [clang-tidy] fix false positive in modernize-min-max-use-initializer-list (#107649)
Previously, whenever a replacement was generated by the analysis, a
diagnostic was generated. This became an issue when a call to
`std::min` or `std::max` consisted only of an initializer list with at
least one argument to the list requiring a type cast.
In this case, a single replacement that added a `static_cast` was
created,
that resulted in a diagnostic being issued but with no nested call
to `std::min` or `std::max`.

Instead, explicitly track if a nested call was detected and only emit a
diagnostic if this is the case.

Fixes #107594
2024-09-17 10:43:40 +02: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
Julian Schmidt
6357781e3f [clang-tidy] fix nullptr dereference in bugprone-forwarding-reference (#106856)
Previously, when checking if a `TemplateSpecializationType` is either
`enable_if` or `enable_if_t`, the AST matcher would call
`getTemplateName`, `getASTemplateDecl` and `getTemplatedDecl` in
succession to check the `NamedDecl` returned from `getTemplatedDecl` is
an `std::enable_if[_t]`. In the linked issue, the pointer returned by 
`getTemplatedDecl` is a `nullptr` that is unconditionally accessed, 
resulting in a crash. Instead, the checking is done on the
`TemplateDecl`
returned by `getASTemplateDecl`.

Fixes #106333
2024-09-17 10:42:07 +02:00
Congcong Cai
0c55ad11ab [clang-tidy] fix false positive that floating point variable only used in increment expr in cert-flp30-c (#108706)
Fixes: #108049
cert-flp30-c only provides non-compliant example with normal loop.
Essentially it wants to avoid that floating point variables are used as
loop counters which are checked in condition expr and modified in
increment expr.
This patch wants to give more precise matcheres to identify this cases.
2024-09-16 10:16:48 +08: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
Congcong Cai
b7914dffd6 [clang-tidy] add default error message for performance-avoid-endl (#107867)
use std::endl as default message when matched expr does not have valid
source text

Fixes: #107859
2024-09-14 07:33:06 +08:00
Julian Schmidt
4ef16e3160 [NFC][clang-tidy] document fix to bugprone-return-const-ref-from-parameter (#107641)
Describe how the issue that is diagnosed by this check can be resolved.
Namely, by adding an overload for the xvalue case (`&&` parameter).

Fixes #107600
2024-09-13 19:05:23 +02:00
Congcong Cai
471360b224 [clang-tidy][NFC] fix typo in release note 2024-09-13 21:03:12 +08:00
Mainak Sil
d04c2ed60c [clang-tidy] Improve documentation of bugprone-pointer-arithmetic-on-polymorphic-object (#108324)
Fix #107831.
2024-09-13 00:35:24 -04:00
MichelleCDjunaidi
80fcab8c26 [Docs][clang-query] disclose Windows linetab bug on clang-query tab auto-complete (#107956)
As per
https://github.com/llvm/llvm-project/pull/106672/#issuecomment-2325577815
and https://github.com/llvm/llvm-project/issues/107377, the
documentation should be updated to note that the current bug on Windows
involving ``LineEditor`` causing Tab key related features to not work.

Fixes #107377
2024-09-11 08:32:01 -04:00