Skip to content
Open
Changes from 1 commit
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
84 changes: 60 additions & 24 deletions llvm/utils/git/code-lint-helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,69 @@


class LintArgs:
start_rev: str
end_rev: str
repo: str
changed_files: Sequence[str]
token: str
verbose: bool = True
issue_number: int = 0
build_path: str = "build"
clang_tidy_binary: str = "clang-tidy"
__start_rev: str
__end_rev: str
__repo: str
__changed_files: List[str]
__token: str
__verbose: bool
__issue_number: int
__build_path: str
__clang_tidy_binary: str

def __init__(self, args: argparse.Namespace) -> None:
if not args is None:
self.start_rev = args.start_rev
self.end_rev = args.end_rev
self.repo = args.repo
self.token = args.token
self.changed_files = (
self.__start_rev = args.start_rev
self.__end_rev = args.end_rev
self.__repo = args.repo
self.__token = args.token
self.__changed_files = (
args.changed_files.split(",") if args.changed_files else []
Copy link
Member Author

@zeyi2 zeyi2 Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the original script at L301, it was:

    changed_files = []
    if args.changed_files:
        changed_files = args.changed_files.split(",")

I moved it here, it is basically the same logic.

)
self.issue_number = args.issue_number
self.verbose = args.verbose
self.build_path = args.build_path
self.clang_tidy_binary = args.clang_tidy_binary
self.__issue_number = args.issue_number
self.__verbose = args.verbose
self.__build_path = args.build_path
self.__clang_tidy_binary = args.clang_tidy_binary

@property
def start_rev(self) -> str:
return self.__start_rev

@property
def end_rev(self) -> str:
return self.__end_rev

@property
def repo(self) -> str:
return self.__repo

@property
def changed_files(self) -> List[str]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this particular place it may make sense to return Sequence, if self.__changed_files should be invariant for external users.

return self.__changed_files

@property
def token(self) -> str:
return self.__token

@property
def verbose(self) -> bool:
return self.__verbose

@property
def issue_number(self) -> int:
return self.__issue_number

@property
def build_path(self) -> str:
return self.__build_path

@property
def clang_tidy_binary(self) -> str:
return self.__clang_tidy_binary


class LintHelper:
COMMENT_TAG = "<!--LLVM CODE LINT COMMENT: {linter}-->"
COMMENT_TAG: Final = "<!--LLVM CODE LINT COMMENT: {linter}-->"
name: str
friendly_name: str
comment: Dict[str, Any] = {}
Expand Down Expand Up @@ -180,8 +216,8 @@ class ClangTidyLintHelper(LintHelper):
name: Final = "clang-tidy"
friendly_name: Final = "C/C++ code linter"

def instructions(self, cpp_files: Sequence[str], args: LintArgs) -> str:
files_str = " ".join(cpp_files)
def instructions(self, files_to_lint: Sequence[str], args: LintArgs) -> str:
files_str = " ".join(files_to_lint)
return f"""
git diff -U0 origin/main...HEAD -- {files_str} |
python3 clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py \
Expand All @@ -207,8 +243,8 @@ def _should_lint_file(self, filepath: str) -> bool:
# TODO: Add more rules when enabling other projects to use clang-tidy in CI.
return filepath.startswith("clang-tools-extra/clang-tidy/")

def run_linter_tool(self, cpp_files: Sequence[str], args: LintArgs) -> str:
if not cpp_files:
def run_linter_tool(self, files_to_lint: Sequence[str], args: LintArgs) -> str:
if not files_to_lint:
return ""

git_diff_cmd = [
Expand All @@ -218,7 +254,7 @@ def run_linter_tool(self, cpp_files: Sequence[str], args: LintArgs) -> str:
f"{args.start_rev}...{args.end_rev}",
"--",
]
git_diff_cmd.extend(cpp_files)
git_diff_cmd.extend(files_to_lint)

diff_proc = subprocess.run(
git_diff_cmd,
Expand Down