[clang-tools-extra] [NFC][clang-tidy] Add type annotations to check_clang_tidy (PR #133140)
Nicolas van Kempen via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 27 10:30:14 PDT 2025
https://github.com/nicovank updated https://github.com/llvm/llvm-project/pull/133140
>From 48bb3c79069c9bad5ee01865058a155368586b2e Mon Sep 17 00:00:00 2001
From: Nicolas van Kempen <nvankemp at gmail.com>
Date: Thu, 27 Mar 2025 13:28:56 -0400
Subject: [PATCH] [NFC][clang-tidy] Add type annotations to check_clang_tidy
I'm looking to make some changes in this file, start by typing it.
```
> python3 -m mypy --strict clang-tools-extra/test/clang-tidy/check_clang_tidy.py
Success: no issues found in 1 source file
```
---
.../test/clang-tidy/check_clang_tidy.py | 41 ++++++++++---------
1 file changed, 22 insertions(+), 19 deletions(-)
diff --git a/clang-tools-extra/test/clang-tidy/check_clang_tidy.py b/clang-tools-extra/test/clang-tidy/check_clang_tidy.py
index 5e39c05f76d86..e861a23947fc6 100755
--- a/clang-tools-extra/test/clang-tidy/check_clang_tidy.py
+++ b/clang-tools-extra/test/clang-tidy/check_clang_tidy.py
@@ -48,15 +48,16 @@
import re
import subprocess
import sys
+from typing import List, Tuple
-def write_file(file_name, text):
+def write_file(file_name: str, text: str) -> None:
with open(file_name, "w", encoding="utf-8") as f:
f.write(text)
f.truncate()
-def try_run(args, raise_error=True):
+def try_run(args: List[str], raise_error: bool = True) -> str:
try:
process_output = subprocess.check_output(args, stderr=subprocess.STDOUT).decode(
errors="ignore"
@@ -71,12 +72,13 @@ def try_run(args, raise_error=True):
# This class represents the appearance of a message prefix in a file.
class MessagePrefix:
- def __init__(self, label):
+
+ def __init__(self, label: str) -> None:
self.has_message = False
- self.prefixes = []
+ self.prefixes: List[str] = []
self.label = label
- def check(self, file_check_suffix, input_text):
+ def check(self, file_check_suffix: str, input_text: str) -> bool:
self.prefix = self.label + file_check_suffix
self.has_message = self.prefix in input_text
if self.has_message:
@@ -85,7 +87,8 @@ def check(self, file_check_suffix, input_text):
class CheckRunner:
- def __init__(self, args, extra_args):
+
+ def __init__(self, args: argparse.Namespace, extra_args: List[str]) -> None:
self.resource_dir = args.resource_dir
self.assume_file_name = args.assume_filename
self.input_file_name = args.input_file_name
@@ -143,11 +146,11 @@ def __init__(self, args, extra_args):
if self.resource_dir is not None:
self.clang_extra_args.append("-resource-dir=%s" % self.resource_dir)
- def read_input(self):
+ def read_input(self) -> None:
with open(self.input_file_name, "r", encoding="utf-8") as input_file:
self.input_text = input_file.read()
- def get_prefixes(self):
+ def get_prefixes(self) -> None:
for suffix in self.check_suffix:
if suffix and not re.match("^[A-Z0-9\\-]+$", suffix):
sys.exit(
@@ -189,7 +192,7 @@ def get_prefixes(self):
)
assert expect_diagnosis or self.expect_no_diagnosis
- def prepare_test_inputs(self):
+ def prepare_test_inputs(self) -> None:
# Remove the contents of the CHECK lines to avoid CHECKs matching on
# themselves. We need to keep the comments to preserve line numbers while
# avoiding empty lines which could potentially trigger formatting-related
@@ -198,7 +201,7 @@ def prepare_test_inputs(self):
write_file(self.temp_file_name, cleaned_test)
write_file(self.original_file_name, cleaned_test)
- def run_clang_tidy(self):
+ def run_clang_tidy(self) -> str:
args = (
[
"clang-tidy",
@@ -238,11 +241,11 @@ def run_clang_tidy(self):
print("------------------------------------------------------------------")
return clang_tidy_output
- def check_no_diagnosis(self, clang_tidy_output):
+ def check_no_diagnosis(self, clang_tidy_output: str) -> None:
if clang_tidy_output != "":
sys.exit("No diagnostics were expected, but found the ones above")
- def check_fixes(self):
+ def check_fixes(self) -> None:
if self.has_check_fixes:
try_run(
[
@@ -254,7 +257,7 @@ def check_fixes(self):
]
)
- def check_messages(self, clang_tidy_output):
+ def check_messages(self, clang_tidy_output: str) -> None:
if self.has_check_messages:
messages_file = self.temp_file_name + ".msg"
write_file(messages_file, clang_tidy_output)
@@ -268,7 +271,7 @@ def check_messages(self, clang_tidy_output):
]
)
- def check_notes(self, clang_tidy_output):
+ def check_notes(self, clang_tidy_output: str) -> None:
if self.has_check_notes:
notes_file = self.temp_file_name + ".notes"
filtered_output = [
@@ -287,7 +290,7 @@ def check_notes(self, clang_tidy_output):
]
)
- def run(self):
+ def run(self) -> None:
self.read_input()
if self.export_fixes is None:
self.get_prefixes()
@@ -313,7 +316,7 @@ def run(self):
C_STANDARDS = ["c99", ("c11", "c1x"), "c17", ("c23", "c2x"), "c2y"]
-def expand_std(std):
+def expand_std(std: str) -> List[str]:
split_std, or_later, _ = std.partition("-or-later")
if not or_later:
@@ -335,11 +338,11 @@ def expand_std(std):
return [std]
-def csv(string):
+def csv(string: str) -> List[str]:
return string.split(",")
-def parse_arguments():
+def parse_arguments() -> Tuple[argparse.Namespace, List[str]]:
parser = argparse.ArgumentParser(
prog=pathlib.Path(__file__).stem,
description=__doc__,
@@ -374,7 +377,7 @@ def parse_arguments():
return parser.parse_known_args()
-def main():
+def main() -> None:
args, extra_args = parse_arguments()
abbreviated_stds = args.std
More information about the cfe-commits
mailing list