Skip to content
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

feat: add clangd-tidy-diff script for lintings of source control deltas [1.1] #12

Open
wants to merge 1 commit into
base: v1.1.x
Choose a base branch
from
Open
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: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ pip install clangd-tidy

```
usage: clangd-tidy [--allow-extensions ALLOW_EXTENSIONS]
[--fail-on-severity SEVERITY] [-f] [-o OUTPUT] [--tqdm]
[--fail-on-severity SEVERITY] [-f] [-o OUTPUT]
[--line-filter LINE_FILTER] [--tqdm]
[--github] [--git-root GIT_ROOT] [-c] [--context CONTEXT]
[--color {auto,always,never}] [-v]
[-p COMPILE_COMMANDS_DIR] [-j JOBS]
Expand Down Expand Up @@ -84,6 +85,8 @@ check options:
output options:
-o OUTPUT, --output OUTPUT
Output file for diagnostics. [default: stdout]
--line-filter LINE_FILTER
A JSON with a list of files and line ranges that will act as a filter for diagnostics. Compatible with clang-tidy --line-filter format.
--tqdm Show a progress bar (tqdm required).
--github Append workflow commands for GitHub Actions to output.
--git-root GIT_ROOT Specifies the root directory of the Git repository.
Expand Down
3 changes: 2 additions & 1 deletion clangd_tidy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .clangd_tidy_diff_cli import clang_tidy_diff
from .main_cli import main_cli
from .version import __version__

__all__ = ["main_cli", "__version__"]
__all__ = ["main_cli", "clang_tidy_diff", "__version__"]
14 changes: 12 additions & 2 deletions clangd_tidy/args.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import argparse
import os
import pathlib
import sys
import os

from .lsp.messages import DiagnosticSeverity
from .lines_filter import LineFilter

from .lsp.messages import DiagnosticSeverity
from .version import __version__

__all__ = ["SEVERITY_INT", "parse_args"]
Expand Down Expand Up @@ -76,6 +77,15 @@ def parse_args() -> argparse.Namespace:
default=sys.stdout,
help="Output file for diagnostics. [default: stdout]",
)
output_group.add_argument(
"--line-filter",
default=LineFilter(),
type=LineFilter.parse_line_filter,
help=(
"A JSON with a list of files and line ranges that will act as a filter for diagnostics."
" Compatible with clang-tidy --line-filter parameter format."
),
)
output_group.add_argument(
"--tqdm", action="store_true", help="Show a progress bar (tqdm required)."
)
Expand Down
91 changes: 91 additions & 0 deletions clangd_tidy/clangd_tidy_diff_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""
Receives a diff on stdin and runs clangd-tidy only on the changed lines.
This is useful to slowly onboard a codebase to linting or to find regressions.
Inspired by clang-tidy-diff.py from the LLVM project.

Example usage with git:
git diff -U0 HEAD^^..HEAD | clangd-tidy-diff -p my/build
"""

import argparse
import json
import re
import subprocess
import sys

from .version import __version__

ADDED_FILE_NAME_REGEX = re.compile(r'^\+\+\+ "?(?P<prefix>.*?/)(?P<file>[^\s"]*)')
ADDED_LINES_REGEX = re.compile(r"^@@.*\+(?P<line>\d+)(,(?P<count>\d+))?")


def clang_tidy_diff():
parser = argparse.ArgumentParser(
description="Runs clangd-tidy against modified files,"
" and returns diagnostics only on changed lines."
)
parser.add_argument(
"-V", "--version", action="version", version=f"%(prog)s {__version__}"
)
parser.add_argument(
"-p",
"--compile-commands-dir",
help="Specify a path to look for compile_commands.json. If the path is invalid, clangd-tidy will look in the current directory and parent paths of each source file.",
)
parser.add_argument(
"--pass-arg",
action="append",
help="Pass this argument to clangd-tidy (can be used multiple times)",
)
args = parser.parse_args()

last_file = None
lines_per_file = {}
for line in sys.stdin:
m = re.search(ADDED_FILE_NAME_REGEX, line)
if m:
last_file = m.group("file")

if last_file is None:
continue

m = re.search(ADDED_LINES_REGEX, line)
if m is None:
continue

start_line = int(m.group("line"))
line_count = 1
if m.group("count") is not None:
line_count = int(m.group("count"))
if line_count == 0:
continue

end_line = start_line + line_count - 1
lines_per_file.setdefault(last_file, []).append([start_line, end_line])

if len(lines_per_file) == 0:
print("No relevant changes found.")
sys.exit(0)

filters = []
for file, lines in lines_per_file.items():
filters.append({"name": file, "lines": lines})

filters_json = json.dumps(filters)
command = ["clangd-tidy", "--line-filter", filters_json]

if args.compile_commands_dir:
command.extend(["--compile-commands-dir", args.compile_commands_dir])

if args.pass_arg:
command.extend(args.pass_arg)

files = list(lines_per_file.keys())
command.append("--")
command.extend(files)

sys.exit(subprocess.run(command).returncode)


if __name__ == "__main__":
clang_tidy_diff()
112 changes: 112 additions & 0 deletions clangd_tidy/lines_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import argparse
import json
from dataclasses import dataclass, field
from pathlib import Path

from .diagnostic_formatter import DiagnosticCollection


@dataclass
class FileLineFilter:
"""
Filters diagnostics in line ranges of a specific file
"""

file_name: str
"""
File name to filter, can be any postfix of a filtered file path
"""

line_ranges: list[tuple[int, int]] = field(default_factory=list)
"""
List of inclusive line ranges where diagnostics will be emitted
"""

def allows(self, file_path: str, start_line: int, end_line: int) -> bool:
if not file_path.endswith(self.file_name):
return False

if len(self.line_ranges) == 0:
return True

for allowed_lines in self.line_ranges:
if self.interval_intersect(allowed_lines, (start_line, end_line)):
return True

return False

@staticmethod
def interval_intersect(
interval1: tuple[int, int], interval2: tuple[int, int]
) -> bool:
a, b = interval1
c, d = interval2

return max(a, c) <= min(b, d)


@dataclass
class LineFilter:
"""
Filters diagnostics by line ranges.
This is meant to be compatible with clang-tidy --line-filter syntax.
"""

file_line_filters: list[FileLineFilter] = field(default_factory=list)

def allows(self, file_path: str, start_line: int, end_line: int) -> bool:
if len(self.file_line_filters) == 0:
return True

for file_line_filter in self.file_line_filters:
if file_line_filter.allows(file_path, start_line, end_line):
return True

return False

def filter_diagnostics(
self, file: str | Path, diagnostics: list[dict]
) -> list[dict]:
def allow_diagnostic(diagnostic: dict) -> bool:
return self.allows(
str(file),
diagnostic.range.start.line + 1,
diagnostic.range.end.line + 1,
)

filtered = filter(allow_diagnostic, diagnostics)
return list(filtered)

def filter_all_diagnostics(
self, all_diagnostics: DiagnosticCollection
) -> DiagnosticCollection:
return {
file: self.filter_diagnostics(file, diag)
for file, diag in all_diagnostics.items()
}

@staticmethod
def parse_line_filter(s: str) -> "LineFilter":
try:
line_filter = json.loads(s)
except json.JSONDecodeError as e:
raise argparse.ArgumentTypeError(
f"Invalid line filter JSON string: {e=} [{s=}]"
) from e

filter_list = [LineFilter._parse_file_line_filter(f) for f in line_filter]
return LineFilter(filter_list)

@staticmethod
def _parse_file_line_filter(single_filter: dict) -> FileLineFilter:
ranges = []
lines = single_filter.get("lines", [])
for line_range in lines:
assert len(line_range) == 2, (
"Line ranges should be an inclusive range"
f" of the form [start, end] {line_range=} "
)
ranges.append(tuple(line_range))

name = single_filter["name"]
return FileLineFilter(name, ranges)
5 changes: 4 additions & 1 deletion clangd_tidy/main_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

import cattrs

from .args import parse_args, SEVERITY_INT
from .args import SEVERITY_INT, parse_args
from .diagnostic_formatter import (
CompactDiagnosticFormatter,
DiagnosticCollection,
FancyDiagnosticFormatter,
GithubActionWorkflowCommandDiagnosticFormatter,
)
from .lines_filter import LineFilter
from .lsp import ClangdAsync, RequestResponsePair
from .lsp.messages import (
Diagnostic,
Expand Down Expand Up @@ -163,6 +164,8 @@ def main_cli():
max_pending_requests=args.jobs * 2,
).acquire_diagnostics()

file_diagnostics = args.line_filter.filter_all_diagnostics(file_diagnostics)

formatter = (
FancyDiagnosticFormatter(
extra_context=args.context,
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ classifiers = [

[project.scripts]
clangd-tidy = "clangd_tidy:main_cli"
clangd-tidy-diff = "clangd_tidy:clang_tidy_diff"

[project.urls]
"Homepage" = "https://github.com/lljbash/clangd-tidy"
Expand Down
Loading