-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Similar to the "ninja scan-build" target for C, add a clippy internal tool that runs clippy-driver on all crates in the project. The approach used is more efficient than with "ninja scan-build", and does not require rerunning Meson in a separate build directory; it uses the introspection data to find the compiler arguments for the target and invokes clippy-driver with a slightly modified command line. This could actually be applied to scan-build as well, reusing the run_tool_on_targets() function. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
- Loading branch information
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# Copyright 2024 The Meson development team | ||
|
||
from __future__ import annotations | ||
from collections import defaultdict | ||
import os | ||
import tempfile | ||
import typing as T | ||
|
||
from .run_tool import run_tool_on_targets, run_with_buffered_output | ||
from .. import build, mlog | ||
from ..mesonlib import MachineChoice, PerMachine | ||
|
||
if T.TYPE_CHECKING: | ||
from ..compilers.rust import RustCompiler | ||
|
||
class ClippyDriver: | ||
def __init__(self, build: build.Build, tempdir: str): | ||
self.tools: PerMachine[T.List[str]] = PerMachine([], []) | ||
self.warned: T.DefaultDict[str, bool] = defaultdict(lambda: False) | ||
self.tempdir = tempdir | ||
for machine in MachineChoice: | ||
compilers = build.environment.coredata.compilers[machine] | ||
if 'rust' in compilers: | ||
compiler = T.cast('RustCompiler', compilers['rust']) | ||
self.tools[machine] = compiler.get_rust_tool('clippy-driver', build.environment) | ||
|
||
def warn_missing_clippy(self, machine: str) -> None: | ||
if self.warned[machine]: | ||
return | ||
mlog.warning(f'clippy-driver not found for {machine} machine') | ||
self.warned[machine] = True | ||
|
||
def __call__(self, target: T.Dict[str, T.Any]) -> T.Iterable[T.Coroutine[None, None, int]]: | ||
for src_block in target['target_sources']: | ||
if src_block['language'] == 'rust': | ||
clippy = getattr(self.tools, src_block['machine']) | ||
if not clippy: | ||
self.warn_missing_clippy(src_block['machine']) | ||
continue | ||
|
||
cmdlist = list(clippy) | ||
prev = None | ||
for arg in src_block['parameters']: | ||
if prev: | ||
prev = None | ||
continue | ||
elif arg in {'--emit', '--out-dir'}: | ||
prev = arg | ||
else: | ||
cmdlist.append(arg) | ||
|
||
cmdlist.extend(src_block['sources']) | ||
# the default for --emit is to go all the way to linking, | ||
# and --emit dep-info= is not enough for clippy to do | ||
# enough analysis, so use --emit metadata. | ||
cmdlist.append('--emit') | ||
cmdlist.append('metadata') | ||
cmdlist.append('--out-dir') | ||
cmdlist.append(self.tempdir) | ||
yield run_with_buffered_output(cmdlist) | ||
|
||
def run(args: T.List[str]) -> int: | ||
os.chdir(args[0]) | ||
build_data = build.load(os.getcwd()) | ||
with tempfile.TemporaryDirectory() as d: | ||
return run_tool_on_targets(ClippyDriver(build_data, d)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters