Skip to content

clang-tidy: run tool only on source files participating in targets #14736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/markdown/snippets/clang-tidy-improvement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## `clang-tidy`'s auto-generated targets correctly select source files

In previous versions, the target would run `clang-tidy` on _every_ C-like source files (.c, .h, .cpp, .hpp). It did not work correctly because some files, especially headers, are not intended to be consumed as is.

It will now run only on source files participating in targets.
4 changes: 2 additions & 2 deletions mesonbuild/scripts/clangtidy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import shutil
import sys

from .run_tool import run_clang_tool, run_with_buffered_output
from .run_tool import run_with_buffered_output, run_clang_tool_on_sources
from ..environment import detect_clangtidy, detect_clangapply
import typing as T

Expand Down Expand Up @@ -56,7 +56,7 @@ def run(args: T.List[str]) -> int:
fixesdir.unlink()
fixesdir.mkdir(parents=True)

tidyret = run_clang_tool('clang-tidy', srcdir, builddir, run_clang_tidy, tidyexe, builddir, fixesdir)
tidyret = run_clang_tool_on_sources('clang-tidy', srcdir, builddir, run_clang_tidy, tidyexe, builddir, fixesdir)
if fixesdir is not None:
print('Applying fix-its...')
applyret = subprocess.run(applyexe + ['-format', '-style=file', '-ignore-insert-conflict', fixesdir]).returncode
Expand Down
20 changes: 20 additions & 0 deletions mesonbuild/scripts/run_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,26 @@ def wrapper(path: Path) -> T.Iterable[T.Coroutine[None, None, int]]:
yield fn(path, *args)
return asyncio.run(_run_workers(all_clike_files(name, srcdir, builddir), wrapper))

def run_clang_tool_on_sources(name: str, srcdir: Path, builddir: Path, fn: T.Callable[..., T.Coroutine[None, None, int]], *args: T.Any) -> int:
if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())

source_files = set()
with open('meson-info/intro-targets.json', encoding='utf-8') as fp:
targets = json.load(fp)

for target in targets:
for target_source in target.get('target_sources') or []:
for source in target_source.get('sources') or []:
source_files.add(Path(source))

clike_files = set(all_clike_files(name, srcdir, builddir))
source_files = source_files.intersection(clike_files)

def wrapper(path: Path) -> T.Iterable[T.Coroutine[None, None, int]]:
yield fn(path, *args)
return asyncio.run(_run_workers(source_files, wrapper))

def run_tool_on_targets(fn: T.Callable[[T.Dict[str, T.Any]],
T.Iterable[T.Coroutine[None, None, int]]]) -> int:
if sys.platform == 'win32':
Expand Down
Loading