274 Commits

Author SHA1 Message Date
Carlos Galvez
014ab736dc
[clang-tidy] Do not pass any file when listing checks in run_clang_ti… (#137286)
…dy.py

Currently, run_clang_tidy.py does not correctly display the list of
checks picked up from the top-level .clang-tidy file. The reason for
that is that we are passing an empty string as input file.

However, that's not how we are supposed to use clang-tidy to list
checks. Per
65eccb463d,
we simply should not pass any file at all - the internal code of
clang-tidy will pass a "dummy" file if that's the case and get the
.clang-tidy file from the current working directory.

Fixes #136659

Co-authored-by: Carlos Gálvez <carlos.galvez@zenseact.com>
2025-04-29 11:13:30 +02: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
Julian Ruess
ccfabe8380
[clang-tidy-diff] Add an option to treat warnings as errors (#128221)
This patch allows to treat warnings as erros using clang-tidy-diff.

Co-authored-by: Piotr Zegar <me@piotrzegar.pl>
2025-03-17 15:55:40 +08:00
zotnhucucbot
bc74625f50
[clang-tidy] Add an option to exclude files not present in the compile database (#120348)
A change list may include files that are not part of the compile
database, which can cause clang-tidy to fail (e.g., due to missing
included headers). To prevent false negatives, we should allow to skip
processing these files.
2025-01-15 07:59:03 +08: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
34f7000734
[clang-tidy]link LLVMTargetParser (#121072)
Fix build issue introduced in #120547
2024-12-25 11:07:05 +08: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
Thomas Fransham
1f2c08b33b
[clang-tools-extra] Fix add_clang_library usage (#109321)
If a add_clang_library call doesn't specify building as static or shared
library they are implicitly added to the list static libraries that is
linked in to clang-cpp shared library here.

315ba77406/clang/cmake/modules/AddClang.cmake (L107)
Because the clang-tools-extra libraries targets were declared after
clang-cpp they by luck never got linked to clang-cpp.
This change is required for clang symbol visibility macros on windows to
work correctly for clang tools since we need to distinguish if a target
being built will be importing or exporting clang symbols from the
clang-cpp DLL.
2024-10-08 09:22:19 +03:00
Nicolas van Kempen
86e5c5468a
[clang-tidy][run-clang-tidy] Fix minor shutdown noise (#105724)
On my new machine, the script outputs some shutdown noise:
```
Ctrl-C detected, goodbye.
Traceback (most recent call last):
  File "/home/nvankempen/llvm-project/./clang-tools-extra/clang-tidy/tool/run-clang-tidy.py", line 626, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.10/asyncio/base_events.py", line 636, in run_until_complete
    self.run_forever()
  File "/usr/lib/python3.10/asyncio/base_events.py", line 603, in run_forever
    self._run_once()
  File "/usr/lib/python3.10/asyncio/base_events.py", line 1871, in _run_once
    event_list = self._selector.select(timeout)
  File "/usr/lib/python3.10/selectors.py", line 469, in select
    fd_event_list = self._selector.poll(timeout, max_ev)
KeyboardInterrupt
```

This fixes it. Also remove an unused typing import.
Relevant documentation:
https://docs.python.org/3/library/asyncio-runner.html#handling-keyboard-interruption
2024-09-09 11:15:28 -04:00
Nicolas van Kempen
c49770c60f
[NFC] Prefer subprocess.DEVNULL over os.devnull (#106500)
There is no need to support Python 2.7 anymore, Python 3.3+ has
`subprocess.DEVNULL`. This is good practice and also prevents file
handles from
staying open unnecessarily.

Also remove a couple unused or unneeded `__future__` imports.
2024-08-30 19:26:49 +01:00
Steven Wu
b8c560f159
[CMake] Remove EXPORT_SYMBOLS_FOR_PLUGINS from #102138 (#102396)
Partially remove some of the changes from #102138 as
EXPORT_SYMBOLS_FOR_PLUGINS doesn't work on all the configurations.
2024-08-08 06:00:11 -07:00
Steven Wu
01b488faab
Reapply "[CMake] Fold export_executable_symbols_* into function args. (#101741)" (#102138)
Fix the builds with LLVM_TOOL_LLVM_DRIVER_BUILD enabled.

LLVM_ENABLE_EXPORTED_SYMBOLS_IN_EXECUTABLES is not completely
compatible with export_executable_symbols as the later will be ignored
if the previous is set to NO.

Fix the issue by passing if symbols need to be exported to
llvm_add_exectuable so the link flag can be determined directly
without calling export_executable_symbols_* later.
2024-08-07 09:12:15 -07:00
Steven Wu
f9b69a378c Revert "[CMake] Fold export_executable_symbols_* into function args. (#101741)"
This reverts commit 5c56b46a32a8856a022a54291bc9294068f7ddbd. This break
lld build when using GENERATE_DRIVER.
2024-08-06 06:08:16 -07:00
Steven Wu
5c56b46a32
[CMake] Fold export_executable_symbols_* into function args. (#101741)
`LLVM_ENABLE_EXPORTED_SYMBOLS_IN_EXECUTABLES` is not completely
compatible with `export_executable_symbols` as the later will be ignored
if the previous is set to NO.

 Fix the issue by passing if symbols need to be exported to
`llvm_add_exectuable` so the link flag can be determined directly
without calling `export_executable_symbols_*` later.
2024-08-05 19:08:27 -07:00
Nicolas van Kempen
315561c867
[run-clang-tidy.py] Refactor, add progress indicator, add type hints (#89490)
[There is
work](https://discourse.llvm.org/t/rfc-upgrading-llvms-minimum-required-python-version/67571)
to make Python 3.8 the minimum Python version for LLVM.

I edited this script because I wanted some indicator of progress while
going through files.
It now outputs `[XX/YYY]` with the number of processed and total files
after each completion.

The current version of this script is compatible downto Python 3.6 (this
is PyYAML's minimum version).
It would probably work with older Python 3 versions with an older PyYAML
or when YAML is disabled.

With the updates here, it is compatible downto Python 3.7. Python 3.7
was released June 2018.

https://github.com/llvm/llvm-project/pull/89302 is also touching this
file, I don't mind rebasing on top of that work if needed.

### Summary

 -  Add type annotations.
 -  Replace `threading` + `queue` with `asyncio`.
- **Add indicator of processed files over total files**. This is what I
set out to do initially.
- Only print the filename after completion, not the entire Clang-Tidy
invocation command. I find this neater but the behavior can easily be
restored.
2024-07-22 20:23:49 +02: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
Congcong Cai
6b29965fd9
[clang-tidy] align all help message in run-clang-tidy (#96199) 2024-06-26 08:05:00 +08:00
Eisuke Kawashima
d63be475e8
[clang-tidy] fix(clang-tools-extra/**.py): fix invalid escape sequences (#94028) 2024-06-10 21:05:40 +02: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
Alexander Scholz
8dfc023e80
[run-clang-tidy.py] Add option to ignore source files from compilation database (#82416)
I added the option -source-filter to the
`run-clang-tidy.py` script in the clang-tools-extra.

This option allows for handing over a regex, to filter out source files
from the compilation database (not run `clang-tidy` on them).
2024-02-25 17:42:57 +01:00
Michael Lettrich
c019ed972f
Allow to pass config file to clang-tidy-diff (#75457)
Adds a `-config-file` command line option that passes on the path of
.`clang-tidy` or custom config file to the `clang-tidy` executable.
2023-12-26 08:40:40 +01: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
Mark de Wever
df4cf0b937 [clang-tidy] Fixes a typo in the help message.
It contained 'thewarnings' without a space. It also fixes some odd
formatting in the text introduced by

commit dd3c26a045c081620375a878159f536758baba6e
Author: Tobias Hieta <tobias@hieta.se>
Date:   Wed May 17 10:56:49 2023 +0200

    [NFC][Py Reformat] Reformat python files in clang and clang-tools-extra
2023-10-31 18:41:36 +01:00
Amadeus Gebauer
49af6502c6
[run-clang-tidy] Accept export directory if PyYAML is not installed (#69700)
If PyYAML is not installed, the `-export-fixes` can be used to specify a
directory (not a file).

Mentioning @PiotrZSL @dyung 

Follows #69453
2023-10-20 13:40:38 +02:00
Amadeus Gebauer
5557d98341
[run-clang-tidy,clang-tidy-diff] Accept directory as value for -export-fixes (#69453)
Adding an additional parameter to run_clang_tidy.py to accept a
directory where the clang-tidy fixes are saved to. This directory can
then be used to run `clang-apply-replacements`.

Closes #69450
2023-10-20 09:22:49 +02: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
4294bca5e4 [clang-tidy] Add exit code support to clang-tidy-diff.py
Modify script to fail when some run clang-tidy
command fails. Based on run_clang-tidy.

Fixes: #65000

Reviewed By: carlosgalvezp

Differential Revision: https://reviews.llvm.org/D158929
2023-08-31 19:58:15 +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
Julian Schmidt
c3da99275a [clang-tidy] fix None tmpdir when exporting fixes in run-clang-tidy
Differential https://reviews.llvm.org/D145477 removed the check for `(yaml and args.export_fixes)` in line 303 to skip looking for the `clang-apply-replacements` binary. However, the `tmpdir` variable was set in this true branch when exporting fixes and therefore is `None` when invoking run-clang-tidy with `run-clang-tidy -p . -export-fixes fixes.yaml`.

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D157773
2023-08-12 16:18:33 +00:00
Jon Roelofs
2fb1c1082c
cmake: add missing dependencies on ClangDriverOptions tablegen
The modules build trips over this frequently because there is no textual
include of the tablegen output, but the module includes it.

Differential revision: https://reviews.llvm.org/D157119
2023-08-04 10:27:19 -07: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
Shivam Gupta
4754407032 [clang-tidy][clang-tidy-diff.py] Change shebang from python to python3 2023-07-23 19:24:11 +05:30
indev
2499beeb5a [clang-tidy] run-clang-tidy: forward warnings-as-errors argument
Forward `warnings-as-errors` argument to clang-tidy.

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D152625
2023-06-12 12:24:08 +00:00
Tobias Hieta
dd3c26a045
[NFC][Py Reformat] Reformat python files in clang and clang-tools-extra
This is an ongoing series of commits that are reformatting our
Python code.

Reformatting is done with `black`.

If you end up having problems merging this commit because you
have made changes to a python file, the best way to handle that
is to run git checkout --ours <yourfile> and then reformat it
with black.

If you run into any problems, post to discourse about it and
we will try to help.

RFC Thread below:

https://discourse.llvm.org/t/rfc-document-and-standardize-python-code-style

Reviewed By: MatzeB

Differential Revision: https://reviews.llvm.org/D150761
2023-05-23 08:29:52 +02: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
Anonymous
144e236441 run-clang-tidy.py should only search for the clang-apply-replacements if really needed
run-clang-tidy.py should only search for the clang-apply-replacements if really needed.

Reviewed By: carlosgalvezp

Differential Revision: https://reviews.llvm.org/D145477
2023-03-07 14:52:34 +05:30
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
4718da5060 [clang-tidy][NFC] Use C++17 nested namespaces in clang-tidy headers
We forgot to apply the change to headers in the previous patch,
due to missing "-header-filter" in the run-clang-tidy invocation.

Differential Revision: https://reviews.llvm.org/D142307
2023-01-23 21:23:16 +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