157 Commits

Author SHA1 Message Date
Baranov Victor
ed26993976
[clang-tidy] Improve "-quiet" option by suppressing "xxx warnings generated" (#154012)
Before this change, `-quiet` mode in clang-tidy generated meaningless
messages `xxx warnings generated` in output:
```cpp
// main.cpp
#include <iostream>

int main() {
  std::cout << 42;
}
```
```console
> clang-tidy -checks='-*,readability-magic-numbers' -quiet main.cpp

82 warnings generated.
main.cpp:4:16: warning: 42 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
    4 |   std::cout << 42;
      |                ^
```

As you can see, `82 warnings generated.` does not say much `quiet` mode,
this patch removes this message completely:

```console
> ./build/bin/clang-tidy -p build -checks='-*,readability-magic-numbers' -quiet main.cpp

main.cpp:4:16: warning: 42 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
    4 |   std::cout << 42;
      |                ^
```

In contrast, when running without `quiet`, It gives some meaningful
information because we know how many messages were suppressed thus
calculating total messages count:
```console
> clang-tidy -checks='-*,readability-magic-numbers' main.cpp

82 warnings generated.
main.cpp:4:16: warning: 42 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
    4 |   std::cout << 42;
      |                ^
Suppressed 81 warnings (81 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
```

Fixes #47042
2025-08-19 21:47:51 +03:00
Carlos Galvez
3b6d8798ba
[clang-tidy][doc] Improve documentation of the -line-filter flag (#153372)
Fixes #25589

Co-authored-by: Carlos Gálvez <carlos.galvez@zenseact.com>
2025-08-14 07:55:20 +02:00
Keith Smiley
455105f1c6
[clang-tidy] Improve --verify-config documentation (#148699)
Previously it wasn't clear that passing this disables otherwise running
clang-tidy.
2025-07-14 13:16:05 -07:00
Baranov Victor
b56aebaf82
[clang-tidy] Add filtering of check options by enabled checks in '--dump-config' (#147142)
Added function to filter out `CheckOptions` that come from
`ClangTidyOptions::getDefaults()`, but does not have a corresponding
check enabled in the `Checks` configuration.

Fixes https://github.com/llvm/llvm-project/issues/146693.
2025-07-13 23:44:30 +03: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
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
Baranov Victor
842e591577
[clang-tidy][NFC] fix clang-tidy warnings in clang-tools-extra/clang-tidy directory (#136097)
Mostly stylistic changes to `clang-tidy` source code.

Command run:
`python3 clang-tools-extra/clang-tidy/tool/run-clang-tidy.py -p build/
-j $(nproc) clang-tools-extra/clang-tidy`
2025-04-20 20:41:13 +02:00
hill
7ecbeace01
[clang-tidy] fix incorrect argument names in documentation for ExtraArgs and ExtraArgsBefore (#120963) 2024-12-26 22:44:14 +01:00
Congcong Cai
2ff614aaa6
[clang-tidy] support parameters file in command line (#120547)
Fixes: #103499
2024-12-24 23:24:14 +08:00
Congcong Cai
e5de2a2df4
[clang-tidy][NFC] extract options verify to separately function (#120768) 2024-12-21 07:07:19 +08:00
Congcong Cai
4558e45e7e
[clang-tidy] add option to avoid "no checks enabled" error (#96122)
When clang-tidy get an empty checks, it will throw "no checks enabled"
error and exit with non-zero return value.
It make clang-tidy's wrapper program confused when in big project some
files don't want to be checked and use `-checks=-*` to disable all
checks.

---------

Co-authored-by: Danny Mösch <danny.moesch@icloud.com>
2024-06-27 07:27:58 +08:00
Justin Cady
cc54129b98
Add option to exclude headers from clang-tidy analysis (#91400)
This is a renewed attempt to land @toddlipcon's D34654. The comments on
that patch indicate a broad desire for some ability to ignore headers.

After considering various options, including migrating to std::regex, I
believe this is the best path forward. It's intuitive to have separate
regexes for including headers versus excluding them, and this approach
has the added benefit of being completely opt-in. No existing configs
will break, regardless of existing HeaderFilterRegex values.

This functionality is useful for improving performance when analyzing a
targeted subset of code, as well as in cases where some collection of
headers cannot be modified (third party source, for example).
2024-05-14 08:48:04 -04:00
Félix-Antoine Constantin
f94ed6f797
[clang-tidy] Improved --verify-config when using literal style in config file (#85591)
Specifying checks using the literal style (|) in the clang-tidy config
file is currently supported but was not implemented for the
--verify-config options. This means that clang-tidy would work properly
but, using the --verify-config option would raise an error due to some
checks not being parsed properly.

Fixes #53737
2024-04-22 20:05:32 +02:00
Kazu Hirata
76bbbcb41b [clang-tidy] Use StringRef::{starts,ends}_with (NFC)
This patch replaces uses of StringRef::{starts,ends}with with
StringRef::{starts,ends}_with for consistency with
std::{string,string_view}::{starts,ends}_with in C++20.

I'm planning to deprecate and eventually remove
StringRef::{starts,ends}with.
2023-12-13 23:11:05 -08:00
Piotr Zegar
ff8b4c8b2c [clang-tidy][NFC] Minor cleanup in ClangTidyMain.cpp
Extracted some code from a clangTidyMain function into
separate functions.
2023-09-16 06:17:57 +00:00
Piotr Zegar
ec5f4be452 [clang-tidy][NFC] Fix modernize-return-braced-init-list findings
Fix issues found by clang-tidy in clang-tidy source directory.
2023-08-27 08:52:10 +00:00
Piotr Zegar
b530eeea5e [clang-tidy] Add --enable-module-headers-parsing option
Change default behavior in Clang-tidy and skip by default
module headers parsing. That functionality is buggy and
slow in C++20, and provide tiny benefit.

Reviewed By: carlosgalvezp

Differential Revision: https://reviews.llvm.org/D156161
2023-07-25 18:31:17 +00:00
Carlos Galvez
26f476286f [clang-tidy] Support SystemHeaders in .clang-tidy
A previous patch update the clang-tidy documentation
incorrectly claiming that SystemHeaders can be provided
in the .clang-tidy configuration file.

This patch adds support for it, together with tests.

Differential Revision: https://reviews.llvm.org/D149899
2023-05-07 16:36:30 +00:00
Carlos Galvez
132f1d31fd [clang-tidy] Support specifying checks as a list in the config file
Specifying checks as a string is convenient for quickly using
clang-tidy to run a handful of checks. However it is not
suitable for projects that have a long list of enabled or
disabled checks. It is specially troublesome in case one
wants to interleave comments with the checks, to explain
why they are enabled or disabled.

Currently this can be achieved via multiline strings in YAML,
but it's error-prone. For example, comments must end with a
comma for clang-tidy to continue processing the list of globs;
a missing comma will make clang-tidy silently ignore the rest
of the list.

Instead, enable passing a native YAML list to the "Checks"
option in the config file. The implementation is done such
that the old behavior is kept: a user can pass a string
or a list. We can consider deprecating passing the checks
as a string altogether in a future release, to simplify
the internal logic of the YAML parser.

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

Differential Revision: https://reviews.llvm.org/D147876
2023-04-10 19:31:33 +00:00
Carlos Galvez
5b37cddff8 [clang-tidy] Introduce HeaderFileExtensions and ImplementationFileExtensions options
Re-introduce the patch that was reverted previously.
In the first attempt, the checks would not be able to
read from the global option, since getLocalOrGlobal
only works with string types. Additional logic is needed
in order to support both use cases in the transition
period. All that logic will be removed when the local
options are fully removed.

We have a number of checks designed to analyze problems
in header files only, for example:

bugprone-suspicious-include
google-build-namespaces
llvm-header-guard
misc-definitions-in-header
...

All these checks duplicate the same logic and options
to determine whether a location is placed in the main
source file or in the header. More checks are coming
up with similar requirements.

Thus, to remove duplication, let's move this option
to the top-level configuration of clang-tidy (since
it's something all checks should share).

Add a deprecation notice for all checks that use the
local option, prompting to update to the global option.

Differential Revision: https://reviews.llvm.org/D142655
2023-02-19 13:44:11 +00:00
Carlos Galvez
2706919f91 [clang-tidy][doc] Improve clang-tidy documentation
- Specify that the .clang-tidy file is in YAML format.
- Document the options that may be used in the .clang-tidy file,
- Add missing documentation for existing options (User).
- Fix spurious newline after the dash that comes after every
  command-line option. This was inconsistent with single-line
  descriptions, which lacked a newline. The description is now
  aligned with the dash and the corresponding command-line option,
  more visually pleasing.

This enables documenting upcoming global clang-tidy
configuration options.

Differential Revision: https://reviews.llvm.org/D141144
2023-02-09 12:19:36 +00:00
Carlos Galvez
c7575fcb68 Revert "[clang-tidy] Introduce HeaderFileExtensions and ImplementationFileExtensions options"
This reverts commit 4240c9146248ac0a91c45dee421c6ef07709ba74.

The current solution won't work since getLocalOrGlobal does not
support returning a vector. More work needs to be put into
ensuring both the local and global way of setting the options
are available during the transition period.
2023-01-25 05:17:00 +00:00
Carlos Galvez
4240c91462 [clang-tidy] Introduce HeaderFileExtensions and ImplementationFileExtensions options
We have a number of checks designed to analyze problems
in header files only, for example:

bugprone-suspicious-include
google-build-namespaces
llvm-header-guard
misc-definitions-in-header
...

All these checks duplicate the same logic and options
to determine whether a location is placed in the main
source file or in the header. More checks are coming
up with similar requirements.

Thus, to remove duplication, let's move this option
to the top-level configuration of clang-tidy (since
it's something all checks should share).

Since the checks fetch the option via getLocalOrGlobal,
the behavior is unchanged.

Add a deprecation notice for all checks that use the
local option, prompting to update to the global option.

The functionality for parsing the option will need to
remain in the checks during the transition period.
Once the local options are fully removed, the goal
is to store the parsed options in the ClangTidyContext,
that checks can easily have access to.

Differential Revision: https://reviews.llvm.org/D141000
2023-01-23 20:58:34 +00:00
Carlos Galvez
7d2ea6c422 [clang-tidy][NFC] Use C++17 nested namespaces in the clang-tidy folder
Fix applied by running:

run-clang-tidy.py -checks=-*,modernize-concat-nested-namespaces

Differential Revision: https://reviews.llvm.org/D141770
2023-01-14 18:51:39 +00:00
Krzysztof Parzyszek
c589730ad5 [YAML] Convert Optional to std::optional 2022-12-06 12:49:32 -08:00
Krzysztof Parzyszek
3c255f679c Process: convert Optional to std::optional
This applies to GetEnv and FindInEnvPath.
2022-12-06 09:56:14 -08:00
Kazu Hirata
a5f368af6d [clang-tidy] Use structured bindings (NFC) 2022-11-06 20:48:55 -08:00
Kazu Hirata
70257fab68 Use any_of (NFC) 2022-07-22 01:05:17 -07:00
Nathan James
fbf611ed2a
[clang-tidy] Extend spelling for CheckOptions
The current way to specify CheckOptions is pretty verbose and unintuitive.
Given that the options are a dictionary it makes much more sense to treat them as such in the config files.
Example:
```
CheckOptions: {SomeCheck.Option: true, SomeCheck.OtherOption: 'ignore'}
# Or
CheckOptions:
  SomeCheck.Option: true
  SomeCheck.OtherOption: 'ignore'
```

This change will still handle the old syntax with no issue, ensuring we don't screw up users current config files.

The only observable differences are support for the new syntax and `-dump=config` will emit using the new syntax.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D128337
2022-06-23 19:59:31 +01:00
Nathan James
5ca68d5845
[clang-tidy] Add -verify-config command line argument
Adds a `-verify-config` command line argument, that when specified will verify the Checks and CheckOptions fields in the config files:
 - A warning will be raised for any check that doesn't correspond to a registered check, a suggestion will also be emitted for close misses.
 - A warning will be raised for any check glob(containing *) that doesn't match any registered check.
 - A warning will be raised for any CheckOption that isn't read by any registered check, a suggestion will also be emitted for close misses.

This can be useful if debuging why a certain check isn't enabled, or the options are being handled as you expect them to be.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D127446
2022-06-23 19:23:09 +01:00
Jameson Nash
84f137a590 Reland "enable plugins for clang-tidy"
This reverts commit ab3b89855c5318f0009e1f016ffe5b1483507fd0 but
disables the new test if the user has disabled support for building it.
2022-02-01 17:37:24 -05:00
Petr Hosek
ab3b89855c Revert "enable plugins for clang-tidy"
This reverts commit 36892727e4f19a60778e371d78f8fb09d8122c85 which
breaks the build when LLVM_INSTALL_TOOLCHAIN_ONLY is enabled with:

  CMake Error at cmake/modules/AddLLVM.cmake:683 (add_dependencies):
  The dependency target "clang-tidy-headers" of target "CTTestTidyModule"
  does not exist.
2022-01-31 00:55:43 -08:00
Jameson Nash
36892727e4 enable plugins for clang-tidy
Fixes #32739

Differential Revision: https://reviews.llvm.org/D111100
2022-01-29 14:21:19 -05:00
Nathan James
abbe9e227e
[clang-tidy] Added command line option fix-notes
Added an option to control whether to apply the fixes found in notes attached to clang tidy errors or not.
Diagnostics may contain multiple notes each offering different ways to fix the issue, for that reason the default behaviour should be to not look at fixes found in notes.
Instead offer up all the available fix-its in the output but don't try to apply the first one unless `-fix-notes` is supplied.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D84924
2021-03-01 22:07:11 +00:00
Nathan James
64badecd44
[clang-tidy][NFC] Reduce copies of Intrusive..FileSystem
Swapped a few instances where a move is more optimal or the target doesn't need to hold a reference.
2020-12-17 14:09:08 +00:00
Nathan James
34d2688a50
[clang-tidy] Use a MemoryBufferRef when parsing configuration files.
Using a MemoryBufferRef, If there is an error parsing, we can point the user to the location of the file.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D93024
2020-12-10 14:52:45 +00:00
Nathan James
bedf3a0f50
[clang-tidy][NFC] Use moves instead of copies when constructing OptionsProviders.
Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D92267
2020-12-10 11:34:57 +00:00
Nathan James
06db8f984f
[clang-tidy] Merge options inplace instead of copying
Changed `ClangTidyOptions::mergeWith` to operate on the instance instead of returning a copy. The old mergeWith method has been renamed to merge and marked as nodiscard, to aid in disambiguating which one is which.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D91184
2020-11-12 18:19:12 +00:00
Hiral Oza
d6a468d622 [clang-tidy] adding "--config-file=<file-path>" to specify custom config file.
Let clang-tidy to read config from specified file.
Example:
$ clang-tidy --config-file=/some/path/myTidyConfig --list-checks --
...this will read config from '/some/path/myTidyConfig'.

ClangTidyMain.cpp reads ConfigFile into string and then assigned read data to 'Config' i.e. makes like '--config' code flow internally.

May speed-up tidy runtime since now it will just look-up <file-path>
instead of searching ".clang-tidy" in parent-dir(s).

Directly specifying config path helps setting build dependencies.

Thanks to @DmitryPolukhin for valuable suggestion. This patch now propose
change only in ClangTidyMain.cpp.

Reviewed By: DmitryPolukhin

Differential Revision: https://reviews.llvm.org/D89936
2020-11-03 11:59:46 +00:00
Nathan James
860aefd078
[clang-tidy][NFC] Remove unnecessary includes throughout clang-tidy header files
Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D82661
2020-06-29 16:05:52 +01:00
Nathan James
4836188ad9
[clang-tidy] Extend InheritParentConfig to CommandLineConfig
Extend the `InheritParentConfig` support introduced in D75184 for the command line option `--config`.
The current behaviour of `--config` is to when set, disable looking for `.clang-tidy` configuration files.
This new behaviour lets you set `InheritParentConfig` to true in the command line to then look for `.clang-tidy` configuration files to be merged with what's been specified on the command line.

Reviewed By: DmitryPolukhin

Differential Revision: https://reviews.llvm.org/D81949
2020-06-19 12:02:19 +01:00
hyd-dev
d9b8aada82
[clang-tidy] Add --use-color command line option and UseColor option to control colors in diagnostics
This patch adds `--use-color` command line option and `UseColor` option to clang-tidy to control colors in diagnostics. With these options, users can force colorful output. This is useful when using clang-tidy with parallelization command line tools (like ninja and GNU parallel), as they often pipe clang-tidy's standard output and make the colors disappear.

Reviewed By: njames93

Differential Revision: https://reviews.llvm.org/D79477
2020-06-18 16:16:14 +01:00
Nathan James
ccd127008a
[clang-tidy] warnings-as-error no longer exits with ErrorCount
When using `-warnings-as-errors`, If there are any warnings promoted to errors, clang-tidy exits with the number of warnings. This really isn't needed and can cause issues when the number of warnings doesn't fit into 8 bits as POSIX terminals aren't designed to handle more than that.
This addresses https://bugs.llvm.org/show_bug.cgi?id=46305.

Bug originally added in D15528

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D81953
2020-06-17 14:35:37 +01:00
Nathan James
5952125691 clang-tidy and clang-query wont crash with invalid command line options
Motivated by [[ https://bugs.llvm.org/show_bug.cgi?id=46141 | clang-tidy crashed for unknown command line argument. ]]

Reviewed By: aaron.ballman, thakis

Differential Revision: https://reviews.llvm.org/D80879
2020-05-31 17:41:29 +01:00
Nathan James
f4b0ebb89b Revert "clang-tidy and clang-query wont crash with invalid command line options"
This reverts commit f23ddbe3c3ae5f40b99ba272afc3d16b800ba8b9.
2020-05-31 16:40:09 +01:00
Nathan James
f23ddbe3c3 clang-tidy and clang-query wont crash with invalid command line options
Summary: Motivated by [[ https://bugs.llvm.org/show_bug.cgi?id=46141 | clang-tidy crashed for unknown command line argument. ]]

Reviewers: aaron.ballman, alexfh

Reviewed By: aaron.ballman

Subscribers: cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D80879
2020-05-31 16:01:41 +01:00
Dmitry Polukhin
cb1ee34e9d [clang-tidy] Optional inheritance of file configs from parent directories
Summary:
Without this patch clang-tidy stops finding file configs on the nearest
.clang-tidy file. In some cases it is not very convenient because it
results in common parts duplication into every child .clang-tidy file.
This diff adds optional config inheritance from the parent directories
config files.

Test Plan:

Added test cases in existing config test.

Reviewers: alexfh, gribozavr2, klimek, hokein

Subscribers: njames93, arphaman, xazax.hun, aheejin, cfe-commits

Tags: #clang, #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D75184
2020-04-15 06:41:31 -07:00
Benjamin Kramer
adcd026838 Make llvm::StringRef to std::string conversions explicit.
This is how it should've been and brings it more in line with
std::string_view. There should be no functional change here.

This is mostly mechanical from a custom clang-tidy check, with a lot of
manual fixups. It uncovers a lot of minor inefficiencies.

This doesn't actually modify StringRef yet, I'll do that in a follow-up.
2020-01-28 23:25:25 +01:00
Dmitry Polukhin
3f8b100e94 [clang-tidy] Add library for clang-tidy main function
Summary:
This library allows to create clang-tidy tools with custom checks outside of llvm repo
using prebuilt clang release tarball.

Test Plan:
Checked that clang-tidy works as before. New library exists in istall dir.

Reviewers: smeenai, gribozavr, stephanemoore

Subscribers: mgorny, xazax.hun, cfe-commits

Tags: #clang-tools-extra, #clang

Differential Revision: https://reviews.llvm.org/D73300
2020-01-24 13:00:45 -08:00
Rui Ueyama
a7acba29c1 Use InitLLVM in clang-tidy
Update clang-tidy to use InitLLVM, like several other llvm tools that
were previously updated. On Windows, this allows clang-tidy to operate
on arguments containing characters which cannot be represented in the
system's ANSI code page such as filenames with Unicode characters.

Fixes bugzilla bug 43751.

Patch by Tristan Labelle.

Differential Revision: https://reviews.llvm.org/D70694
2019-11-28 13:50:35 +09:00