Commit Graph

2448 Commits

Author SHA1 Message Date
Fangrui Song b9ca972b1f [clang-tidy] Add portability-std-allocator-const check
Report use of `std::vector<const T>` (and similar containers of const
elements). These are now allowed in standard C++ due to undefined
`std::allocator<const T>`. They do not compile with libstdc++ or MSVC.
Future libc++ will remove the extension (D120996).
See docs/clang-tidy/checks/portability-std-allocator-const.rst for detail.

I have attempted clean-up in a large code base. Here are some statistics:

* 98% are related to the container `std::vector`, among `deque/forward_list/list/multiset/queue/set/stack/vector`.
* 24% are related to `std::vector<const std::string>`.
* Both `std::vector<const absl::string_view>` and `std::vector<const int>` contribute 2%. The other contributors spread over various class types.

The check can be useful to other large code bases and may serve as an example
for future libc++ strictness improvement.

Note: on MSVC where -fdelayed-template-parsing is the default, the check cannot
catch cases in uninstantiated templates.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D123655
2022-04-14 11:13:41 -07:00
Nico Weber dd47ab750b Revert "[clang-tidy] Add portability-std-allocator-const check"
This reverts commit 73da7eed8f.
Breaks check-clang-tools on Windows, see comment on
https://reviews.llvm.org/D123655
2022-04-14 09:20:51 -04:00
Jan Svoboda d79ad2f1db [clang][lex] NFCI: Use FileEntryRef in PPCallbacks::InclusionDirective()
This patch changes type of the `File` parameter in `PPCallbacks::InclusionDirective()` from `const FileEntry *` to `Optional<FileEntryRef>`.

With the API change in place, this patch then removes some uses of the deprecated `FileEntry::getName()` (e.g. in `DependencyGraph.cpp` and `ModuleDependencyCollector.cpp`).

Reviewed By: dexonsmith, bnbarham

Differential Revision: https://reviews.llvm.org/D123574
2022-04-14 10:46:12 +02:00
Fangrui Song 73da7eed8f [clang-tidy] Add portability-std-allocator-const check
Report use of ``std::vector<const T>`` (and similar containers of const
elements). These are now allowed in standard C++ due to undefined
``std::allocator<const T>``. They do not compile with libstdc++ or MSVC.
Future libc++ will remove the extension (D120996).
See docs/clang-tidy/checks/portability-std-allocator-const.rst for detail.

I have attempted clean-up in a large code base. Here are some statistics:

* 98% are related to the container `std::vector`, among `deque/forward_list/list/multiset/queue/set/stack/vector`.
* 24% are related to `std::vector<const std::string>`.
* Both `std::vector<const absl::string_view>` and `std::vector<const int>` contribute 2%. The other contributors spread over various class types.

The check can be useful to other large code bases and may serve as an example
for future libc++ strictness improvement.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D123655
2022-04-13 22:35:11 -07:00
Fabian Wolff a18634b74f [clang-tidy] Never consider assignments as equivalent in `misc-redundant-expression` check
Fixes https://github.com/llvm/llvm-project/issues/35853.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D122535
2022-04-12 16:03:14 +02:00
Richard d563c2d0e5 [clang-tidy] Support parenthesized literals in modernize-macro-to-enum
When scanning a macro expansion to examine it as a candidate enum,
first strip off arbitrary matching parentheses from the outside in,
then examine what remains to see if it is Lit, +Lit, -Lit or ~Lit.
If not, reject it as a possible enum candidate.

Differential Revision: https://reviews.llvm.org/D123479

Fixes #54843
2022-04-11 14:06:48 -06:00
Richard 88a7508b1f [clang-tidy] Deal with keyword tokens in preprocessor conditions
When a "keyword" token like __restrict was present in a macro condition,
modernize-macro-to-enum would assert in non-release builds.  However,
even for a "keyword" token, calling getIdentifierInfo()->getName() would
retrieve the text of the token, which is what we want.  Our intention is
to scan names that appear in conditional expressions in potential enum
clusters and invalidate those clusters if they contain the name.

Also, guard against "raw identifiers" appearing as potential enums.
This shouldn't happen, but it doesn't hurt to generalize the code.

Differential Revision: https://reviews.llvm.org/D123349

Fixes #54775
2022-04-08 16:06:06 -06:00
Nathan James 0e0b0feff1
[clang-tidy] Make performance-inefficient-vector-operation work on members
Fixes https://llvm.org/PR50157

Adds support for when the container being read from in a range-for is a member of a struct.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D101624
2022-04-08 14:17:41 +01:00
Nathan James d0fcbb3783
[clang-tidy] Fix invalid fix-it for cppcoreguidelines-prefer-member-initializer
Fixes https://github.com/llvm/llvm-project/issues/53515.

Reviewed By: LegalizeAdulthood

Differential Revision: https://reviews.llvm.org/D118927
2022-04-07 19:13:50 +01:00
Benjamin Kramer 817df7999a [clang-tidy] Silence unused variable warning in release builds. NFCI. 2022-04-07 11:00:28 +02:00
Balázs Kéri cc7ed0caac [clang-tidy] bugprone-signal-handler: Message improvement and code refactoring.
Another change of the code design.
Code simplified again, now there is a single place to check
a handler function and less functions for bug report emitting.
More details are added to the bug report messages.

Reviewed By: whisperity

Differential Revision: https://reviews.llvm.org/D118370
2022-04-07 09:38:58 +02:00
Aaron Siddhartha Mondal 04b42c99f6 Fix typo in new -config-file option
The new -config-file option introduced by 9e1f4f1 was accidentally
referenced as args.config_path on the python side. This patch renames
args.config_path to args.config_file.

To avoid confusion with python file objects, the input argument for
get_tidy_invocation has been renamed from config_path to
config_file_path.

See GitHub issue #54728 for a discussion.
2022-04-05 16:28:49 -04:00
Richard f547fc89c0 [clang-tidy] Add modernize-macro-to-enum check
[buildbot issues fixed]

This check performs basic analysis of macros and replaces them
with an anonymous unscoped enum.  Using an unscoped anonymous enum
ensures that everywhere the macro token was used previously, the
enumerator name may be safely used.

Potential macros for replacement must meet the following constraints:
- Macros must expand only to integral literal tokens.  The unary
  operators plus, minus and tilde are recognized to allow for positive,
  negative and bitwise negated integers.
- Macros must be defined on sequential source file lines, or with
  only comment lines in between macro definitions.
- Macros must all be defined in the same source file.
- Macros must not be defined within a conditional compilation block.
- Macros must not be defined adjacent to other preprocessor directives.
- Macros must not be used in preprocessor conditions

Each cluster of macros meeting the above constraints is presumed to
be a set of values suitable for replacement by an anonymous enum.
From there, a developer can give the anonymous enum a name and
continue refactoring to a scoped enum if desired.  Comments on the
same line as a macro definition or between subsequent macro definitions
are preserved in the output.  No formatting is assumed in the provided
replacements.

The check cppcoreguidelines-macro-to-enum is an alias for this check.

Fixes #27408

Differential Revision: https://reviews.llvm.org/D117522
2022-04-01 15:24:21 -06:00
Danny Mösch ff60af91ac [clang-tidy] Utilize comparison operation implemented in APInt
This is a fix for #53963.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D122544
2022-03-28 23:32:58 +02:00
Douglas Yung cef52105bd Revert "[clang-tidy] Add modernize-macro-to-enum check"
This reverts commit 39b80c8380.

This change was causing build failures on several build bots:
- https://lab.llvm.org/buildbot/#/builders/139/builds/19210
- https://lab.llvm.org/buildbot/#/builders/93/builds/7956
2022-03-25 11:53:42 -07:00
Richard 39b80c8380 [clang-tidy] Add modernize-macro-to-enum check
This check performs basic analysis of macros and replaces them
with an anonymous unscoped enum.  Using an unscoped anonymous enum
ensures that everywhere the macro token was used previously, the
enumerator name may be safely used.

Potential macros for replacement must meet the following constraints:
- Macros must expand only to integral literal tokens.  The unary
  operators plus, minus and tilde are recognized to allow for positive,
  negative and bitwise negated integers.
- Macros must be defined on sequential source file lines, or with
  only comment lines in between macro definitions.
- Macros must all be defined in the same source file.
- Macros must not be defined within a conditional compilation block.
- Macros must not be defined adjacent to other preprocessor directives.
- Macros must not be used in preprocessor conditions

Each cluster of macros meeting the above constraints is presumed to
be a set of values suitable for replacement by an anonymous enum.
From there, a developer can give the anonymous enum a name and
continue refactoring to a scoped enum if desired.  Comments on the
same line as a macro definition or between subsequent macro definitions
are preserved in the output.  No formatting is assumed in the provided
replacements.

The check cppcoreguidelines-macro-to-enum is an alias for this check.

Fixes #27408

Differential Revision: https://reviews.llvm.org/D117522
2022-03-25 09:45:55 -06:00
Fabian Wolff 07675b0b38 [clang-tidy] Fix false positives in `misc-redundant-expression` check
Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D122111
2022-03-23 00:32:45 +01:00
Eric Li 9edeceaece [libTooling] Generalize string explanation as templated metadata
Change RewriteRule from holding an `Explanation` to being able to generate
arbitrary metadata. Where TransformerClangTidyCheck was interested in a string
description for the diagnostic, other tools may be interested in richer metadata
at a higher level of abstraction than at the edit level (which is currently
available as ASTEdit::Metadata).

Reviewed By: ymandel

Differential Revision: https://reviews.llvm.org/D120360
2022-03-21 20:39:35 +00:00
Yitzhak Mandelbaum 8351726e6d Revert "[libTooling] Generalize string explanation as templated metadata"
This reverts commit 18440547d3. Causing failures
in some build modes.

e.g. https://lab.llvm.org/buildbot/#/builders/217/builds/1886
2022-03-21 19:06:59 +00:00
Eric Li 18440547d3 [libTooling] Generalize string explanation as templated metadata
Change RewriteRule from holding an `Explanation` to being able to generate
arbitrary metadata. Where TransformerClangTidyCheck was interested in a string
description for the diagnostic, other tools may be interested in richer metadata
at a higher level of abstraction than at the edit level (which is currently
available as ASTEdit::Metadata).

Reviewed By: ymandel

Differential Revision: https://reviews.llvm.org/D120360
2022-03-21 18:45:39 +00:00
Evgeny Shulgin fc354d3752 [clang-tidy] Skip parentheses in `readability-make-member-function-const`
The checker should ignore parentheses when looking whether the
function should be marked as `const`.

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

Reviewed By: mgehre-amd, njames93

Differential Revision: https://reviews.llvm.org/D122075
2022-03-21 14:18:17 +03:00
Shreyas Atre 9e1f4f1398 [clang-tidy][run-clang-tidy.py] Add --config-file=<string> option
Link to the GitHub Issue: https://github.com/llvm/llvm-project/issues/53745

Added config_path variable within the python script which makes the
required call to the clang-tidy binary with --config-file option.
If the config_path is None then config will be used. No error is raised
if both are given but silently chooses config_path over config
2022-03-17 07:30:28 -04:00
Evgeny Shulgin 6043520c20 [clang-tidy] Don't check decltype return types in `readability-const-return-type`
The checker removes `const`s that are superfluos and badly affect
readability. `decltype(auto)`/`decltype(expr)` are often const-qualified, but
have no effect on readability and usually can't stop being const-qualified
without significant code change.

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

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D119470
2022-03-17 14:27:09 +03:00
Danny Mösch 2b21fc5520 Allow newline characters as separators for checks in Clang-Tidy configurations
This is a fix for #53737. In addition to commas, newline characters are
considered as separators of checks.
2022-03-15 14:30:13 -04:00
Keith Smiley 5da83eeb91 clang-tidy: discover binaries in build dir
This changes the clang-tidy script to discover binaries you've built
locally without having to pass them.

Differential Revision: https://reviews.llvm.org/D100692
2022-03-14 09:04:36 -07:00
Balazs Benics 665879b9e7 [clang-tidy][docs] Fix wrong url in DontModifyStdNamespaceCheck
It was probably a copy-paste mistake.
The check was added as `cert-dcl58-cpp`, so the doc should point there.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D121373
2022-03-13 17:51:00 +01:00
Balazs Benics d321f09926 [clang-tidy][docs][NFC] Update URL and docs of PostfixOperatorCheck
The docs URL was dangling, and the docs suggested that it has no fixits,
but it actually had.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D121372
2022-03-13 17:51:00 +01:00
Richard 602ea53cb7 [clang-tidy] Refactor: Prefer single quoted strings over double quoted strings [NFC] 2022-03-11 15:08:24 -07:00
Jun Zhang ac616fbb05
[Clang-tidy] Check the existence of ElaboratedType's qualifiers
The ElaboratedType can have no qualifiers, so we should check it before
use.

Fix #issue53874(https://github.com/llvm/llvm-project/issues/53874)

Differential Revision: https://reviews.llvm.org/D119949
2022-03-01 23:52:44 +08:00
Sockke ba54ebeb5e [clang-tidy] Fix `readability-const-return-type` for pure virtual function.
It cannot match a `pure virtual function`. This patch fixes this behavior.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D116439
2022-03-01 20:55:28 +08:00
Whisperity 416e689ecd [clang-tidy] Fix `readability-suspicious-call-argument` crash for arguments without name-like identifier
As originally reported by @steakhal in
http://github.com/llvm/llvm-project/issues/54074, the name extraction logic of
`readability-suspicious-call-argument` crashes if the argument passed to a
function was a function call to a non-trivially named entity (e.g. an operator).

Fixed this crash case by ignoring such constructs and considering them as having
no name.

Reviewed By: aaron.ballman, steakhal

Differential Revision: http://reviews.llvm.org/D120555
2022-02-25 16:24:27 +01:00
Sockke 6cbf15e9b5 [clang-tidy] Fix `readability-non-const-parameter` for parameter referenced by an lvalue
The checker missed a check for a case when the parameter is referenced by an lvalue and this could cause build breakages.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D117090
2022-02-25 14:53:11 +08:00
Nathan James 79353f940c
[clang-tidy][NFC] Remove Tristate from CachedGlobList
The tristate is a little redundant as we can determine if the item was already in the cache based on the return from try_emplace.

Reviewed By: salman-javed-nz

Differential Revision: https://reviews.llvm.org/D120196
2022-02-23 08:35:31 +00:00
Kesavan Yogeswaran c57b8ca721
[clang-tidy] Provide fine control of color in run-clang-tidy
D90110 modified the behavior of `run-clang-tidy` to always pass the
`--use-color` option to clang-tidy, which enabled colored diagnostics
output regardless of TTY status or .clang-tidy settings. This left the
user with no option to disable the colored output.

This presents an issue when trying to parse the output of run-clang-tidy
programmaticall, as the output is polluted with ANSI escape characters.

This PR fixes this issue in two ways:
1. It restores the default behavior of `run-clang-tidy` to let
   `clang-tidy` decide whether to color output. This allows the user to
   configure color via the `UseColor` option in a .clang-tidy file.
2. It adds mutually exclusive, optional `-use-color` and `-no-use-color`
   argument flags that let the user explicitly set the color option via
   the invocation.

After this change the default behavior of `run-clang-tidy` when no
.clang-tidy file is available is now to show no color, presumably
because `clang-tidy` detects that the output is being piped and defaults
to not showing colored output. This seems like an acceptable tradeoff
to respect .clang-tidy configurations, as users can still use the
`-use-color` option to explicitly enable color.

Fixes #49441 (50097 in Bugzilla)

Reviewed By: njames93

Differential Revision: https://reviews.llvm.org/D119562
2022-02-20 22:00:28 +00:00
Evgeny Shulgin fc84ebfff3 [clang-tidy] Ignore variable template partial specializations in `misc-definitions-in-headers`
Variable template partial specializations are inline and can't lead
to ODR-violations. The checker now ignores them.

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

Reviewed By: hokein

Differential Revision: https://reviews.llvm.org/D119098
2022-02-14 11:38:27 +01:00
Simon Pilgrim 5d1e3ed3e2 [clang-tidy] SimplifyBooleanExprCheck - use cast<> instead of dyn_cast<> to avoid dereference of nullptr
The IfStmt pointer is always referenced inside the replaceCompoundReturnWithCondition call, so assert the cast is correct instead of returning nullptr
2022-02-12 19:59:30 +00:00
Simon Pilgrim 7ad94bd74b [clang-tidy] ContainerSizeEmptyCheck::check - simplify isa<> and dyn_cast<> repeated calls
Just use dyn_cast<> to determine literal + container values from the binop
2022-02-12 11:31:27 +00:00
Simon Pilgrim 869c066ca8 [clang-tidy] RedundantControlFlowCheck::issueDiagnostic - use cast<> instead of dyn_cast<> to avoid dereference of nullptr
The pointer is referenced immediately, so assert the cast is correct instead of returning nullptr
2022-02-12 11:05:59 +00:00
Simon Pilgrim 9acba5206a [clang-tidy] FunctionASTVisitor::TraverseStmt - use cast<> instead of dyn_cast<> to avoid dereference of nullptr
The pointer is referenced immediately, so assert the cast is correct instead of returning nullptr
2022-02-12 11:04:20 +00:00
Simon Pilgrim ed5d659038 [clang-tidy] NoexceptMoveConstructorCheck::check - use castAs<> instead of getAs<> to avoid dereference of nullptr
The pointer is referenced immediately, so assert the cast is correct instead of returning nullptr
2022-02-12 10:57:09 +00:00
Simon Pilgrim 99acc0dc64 [clang-tidy] UseNoexceptCheck::check - use cast<> instead of dyn_cast<> to avoid dereference of nullptr
The pointer is referenced immediately, so assert the cast is correct instead of returning nullptr
2022-02-12 10:51:48 +00:00
Simon Pilgrim 003e038264 [clang-tidy] getOutermostNamespace - remove redundant nullptr check
The pointer is always dereferenced
2022-02-12 10:49:54 +00:00
Simon Pilgrim 5bc13f9d94 [clang-tidy] ProBoundsConstantArrayIndexCheck::check - use cast<> instead of dyn_cast<> to avoid dereference of nullptr
The pointer is referenced immediately, so assert the cast is correct instead of returning nullptr
2022-02-12 10:43:21 +00:00
Simon Pilgrim 389bee71ea [clang-tidy] checkOverridingFunctionReturnType - use castAs<> instead of getAs<> to avoid dereference of nullptr
The pointers are referenced immediately, so assert the cast is correct instead of returning nullptr
2022-02-12 10:41:43 +00:00
Kirill Bobyrev 100ec80ab5 [clang-tidy] Add early exit for defaulted FunctionDecls
This prevents matching of defaulted comparison operators.

Resolves: https://github.com/llvm/llvm-project/issues/53355

Author: Febbe (Fabian Keßler)
2022-02-10 20:29:26 +01:00
Greg Miller d038faea46 [clang-tidy] add option performance-move-const-arg.CheckMoveToConstRef
This option allows callers to disable the warning from
https://clang.llvm.org/extra/clang-tidy/checks/performance-move-const-arg.html
that would warn on the following

```
void f(const string &s);
string s;
f(std::move(s));  // ALLOWED if performance-move-const-arg.CheckMoveToConstRef=false
```

The reason people might want to disable this check, is because it allows
callers to use `std::move()` or not based on local reasoning about the
argument, and without having to care about how the function `f` accepts
the argument. Indeed, `f` might accept the argument by const-ref today,
but change to by-value tomorrow, and if the caller had moved the
argument that they were finished with, the code would work as
efficiently as possible regardless of how `f` accepted the parameter.

Reviewed By: ymandel

Differential Revision: https://reviews.llvm.org/D119370
2022-02-10 13:31:07 +00:00
Corentin Jabot c151225096 [C++2b] Implement multidimentional subscript operator
Implement P2128R6 in C++23 mode.

Unlike GCC's implementation, this doesn't try to recover when a user
meant to use a comma expression.

Because the syntax changes meaning in C++23, the patch is *NOT*
implemented as an extension. Instead, declaring an array with not
exactly 1 parameter is an error in older languages modes. There is an
off-by-default extension warning in C++23 mode.

Unlike the standard, we supports default arguments;

Ie, we assume, based on conversations in WG21, that the proposed
resolution to CWG2507 will be accepted.

We allow arrays OpenMP sections and C++23 multidimensional array to
coexist:

[a , b] multi dimensional array
[a : b] open mp section
[a, b: c] // error

The rest of the patch is relatively straight forward: we take care to
support an arbitrary number of arguments everywhere.
2022-02-08 12:10:47 -05:00
Jesko Appelfeller 0851970af5 Make run-clang-tidy.py print the configured checks correctly
The test invocation at the start of run-clang-tidy.py (line 257) prints
all enabled checks - meaning either the default set or anything
configured via the -checks option. If any checks were (un-)configured
via the -config option, these are not printed. This is confusing to the
user, since the list of checks that are printed may be different from
the list of checks that are used by the non-testing calls to clang-tidy,
where the -config option is passed correctly.

This patch adds the -config option to the test invocation of clang-tidy
at the start of the script. This means that checks (un-)configured via
the -config option (rather than the -checks option) are applied
correctly, when printing the list of enabled checks.
2022-02-08 08:32:04 -05:00
Balázs Kéri 29fc5e0245 [clang-tidy] Fixed a compile warning (NFC).
Fixed a "override" related warning in SharedPtrArrayMismatchCheck.h.
Related to differential revision: https://reviews.llvm.org/D117306
2022-02-07 14:56:27 +01:00
Balázs Kéri c63522e6ba [clang-tidy] Add new check 'shared-ptr-array-mismatch'.
Reviewed By: LegalizeAdulthood

Differential Revision: https://reviews.llvm.org/D117306
2022-02-07 12:57:58 +01:00