Skip to content

Commit 0213fee

Browse files
committed
[NFC][clang-tidy] Add type annotations to add_new_check.py
``` python3 -m mypy --strict clang-tools-extra/clang-tidy/add_new_check.py Success: no issues found in 1 source file ```
1 parent 2f4232d commit 0213fee

File tree

1 file changed

+49
-37
lines changed

1 file changed

+49
-37
lines changed

clang-tools-extra/clang-tidy/add_new_check.py

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,19 @@
88
#
99
# ===-----------------------------------------------------------------------===#
1010

11-
from __future__ import print_function
12-
from __future__ import unicode_literals
13-
1411
import argparse
1512
import io
1613
import itertools
1714
import os
1815
import re
1916
import sys
2017
import textwrap
18+
from typing import Optional, Tuple
2119

2220

2321
# Adapts the module's CMakelist file. Returns 'True' if it could add a new
2422
# entry and 'False' if the entry already existed.
25-
def adapt_cmake(module_path, check_name_camel):
23+
def adapt_cmake(module_path: str, check_name_camel: str) -> bool:
2624
filename = os.path.join(module_path, "CMakeLists.txt")
2725

2826
# The documentation files are encoded using UTF-8, however on Windows the
@@ -57,14 +55,14 @@ def adapt_cmake(module_path, check_name_camel):
5755

5856
# Adds a header for the new check.
5957
def write_header(
60-
module_path,
61-
module,
62-
namespace,
63-
check_name,
64-
check_name_camel,
65-
description,
66-
lang_restrict,
67-
):
58+
module_path: str,
59+
module: str,
60+
namespace: str,
61+
check_name: str,
62+
check_name_camel: str,
63+
description: str,
64+
lang_restrict: str,
65+
) -> None:
6866
wrapped_desc = "\n".join(
6967
textwrap.wrap(
7068
description, width=80, initial_indent="/// ", subsequent_indent="/// "
@@ -139,7 +137,9 @@ class %(check_name_camel)s : public ClangTidyCheck {
139137

140138

141139
# Adds the implementation of the new check.
142-
def write_implementation(module_path, module, namespace, check_name_camel):
140+
def write_implementation(
141+
module_path: str, module: str, namespace: str, check_name_camel: str
142+
) -> None:
143143
filename = os.path.join(module_path, check_name_camel) + ".cpp"
144144
print("Creating %s..." % filename)
145145
with io.open(filename, "w", encoding="utf8", newline="\n") as f:
@@ -187,7 +187,7 @@ def write_implementation(module_path, module, namespace, check_name_camel):
187187

188188

189189
# Returns the source filename that implements the module.
190-
def get_module_filename(module_path, module):
190+
def get_module_filename(module_path: str, module: str) -> str:
191191
modulecpp = list(
192192
filter(
193193
lambda p: p.lower() == module.lower() + "tidymodule.cpp",
@@ -198,7 +198,9 @@ def get_module_filename(module_path, module):
198198

199199

200200
# Modifies the module to include the new check.
201-
def adapt_module(module_path, module, check_name, check_name_camel):
201+
def adapt_module(
202+
module_path: str, module: str, check_name: str, check_name_camel: str
203+
) -> None:
202204
filename = get_module_filename(module_path, module)
203205
with io.open(filename, "r", encoding="utf8") as f:
204206
lines = f.readlines()
@@ -217,10 +219,10 @@ def adapt_module(module_path, module, check_name, check_name_camel):
217219
+ '");\n'
218220
)
219221

220-
lines = iter(lines)
222+
lines_iter = iter(lines)
221223
try:
222224
while True:
223-
line = next(lines)
225+
line = next(lines_iter)
224226
if not header_added:
225227
match = re.search('#include "(.*)"', line)
226228
if match:
@@ -247,10 +249,11 @@ def adapt_module(module_path, module, check_name, check_name_camel):
247249
# If we didn't find the check name on this line, look on the
248250
# next one.
249251
prev_line = line
250-
line = next(lines)
252+
line = next(lines_iter)
251253
match = re.search(' *"([^"]*)"', line)
252254
if match:
253255
current_check_name = match.group(1)
256+
assert current_check_name
254257
if current_check_name > check_fq_name:
255258
check_added = True
256259
f.write(check_decl)
@@ -262,7 +265,9 @@ def adapt_module(module_path, module, check_name, check_name_camel):
262265

263266

264267
# Adds a release notes entry.
265-
def add_release_notes(module_path, module, check_name, description):
268+
def add_release_notes(
269+
module_path: str, module: str, check_name: str, description: str
270+
) -> None:
266271
wrapped_desc = "\n".join(
267272
textwrap.wrap(
268273
description, width=80, initial_indent=" ", subsequent_indent=" "
@@ -324,7 +329,13 @@ def add_release_notes(module_path, module, check_name, description):
324329

325330

326331
# Adds a test for the check.
327-
def write_test(module_path, module, check_name, test_extension, test_standard):
332+
def write_test(
333+
module_path: str,
334+
module: str,
335+
check_name: str,
336+
test_extension: str,
337+
test_standard: str,
338+
) -> None:
328339
if test_standard:
329340
test_standard = f"-std={test_standard}-or-later "
330341
check_name_dashes = module + "-" + check_name
@@ -362,7 +373,7 @@ def write_test(module_path, module, check_name, test_extension, test_standard):
362373
)
363374

364375

365-
def get_actual_filename(dirname, filename):
376+
def get_actual_filename(dirname: str, filename: str) -> str:
366377
if not os.path.isdir(dirname):
367378
return ""
368379
name = os.path.join(dirname, filename)
@@ -376,7 +387,7 @@ def get_actual_filename(dirname, filename):
376387

377388

378389
# Recreates the list of checks in the docs/clang-tidy/checks directory.
379-
def update_checks_list(clang_tidy_path):
390+
def update_checks_list(clang_tidy_path: str) -> None:
380391
docs_dir = os.path.join(clang_tidy_path, "../docs/clang-tidy/checks")
381392
filename = os.path.normpath(os.path.join(docs_dir, "list.rst"))
382393
# Read the content of the current list.rst file
@@ -390,12 +401,12 @@ def update_checks_list(clang_tidy_path):
390401
for file in filter(
391402
lambda s: s.endswith(".rst"), os.listdir(os.path.join(docs_dir, subdir))
392403
):
393-
doc_files.append([subdir, file])
404+
doc_files.append((subdir, file))
394405
doc_files.sort()
395406

396407
# We couldn't find the source file from the check name, so try to find the
397408
# class name that corresponds to the check in the module file.
398-
def filename_from_module(module_name, check_name):
409+
def filename_from_module(module_name: str, check_name: str) -> str:
399410
module_path = os.path.join(clang_tidy_path, module_name)
400411
if not os.path.isdir(module_path):
401412
return ""
@@ -433,7 +444,7 @@ def filename_from_module(module_name, check_name):
433444
return ""
434445

435446
# Examine code looking for a c'tor definition to get the base class name.
436-
def get_base_class(code, check_file):
447+
def get_base_class(code: str, check_file: str) -> str:
437448
check_class_name = os.path.splitext(os.path.basename(check_file))[0]
438449
ctor_pattern = check_class_name + r"\([^:]*\)\s*:\s*([A-Z][A-Za-z0-9]*Check)\("
439450
matches = re.search(r"\s+" + check_class_name + "::" + ctor_pattern, code)
@@ -452,7 +463,7 @@ def get_base_class(code, check_file):
452463
return ""
453464

454465
# Some simple heuristics to figure out if a check has an autofix or not.
455-
def has_fixits(code):
466+
def has_fixits(code: str) -> bool:
456467
for needle in [
457468
"FixItHint",
458469
"ReplacementText",
@@ -464,7 +475,7 @@ def has_fixits(code):
464475
return False
465476

466477
# Try to figure out of the check supports fixits.
467-
def has_auto_fix(check_name):
478+
def has_auto_fix(check_name: str) -> str:
468479
dirname, _, check_name = check_name.partition("-")
469480

470481
check_file = get_actual_filename(
@@ -499,7 +510,7 @@ def has_auto_fix(check_name):
499510

500511
return ""
501512

502-
def process_doc(doc_file):
513+
def process_doc(doc_file: Tuple[str, str]) -> Tuple[str, Optional[re.Match[str]]]:
503514
check_name = doc_file[0] + "-" + doc_file[1].replace(".rst", "")
504515

505516
with io.open(os.path.join(docs_dir, *doc_file), "r", encoding="utf8") as doc:
@@ -508,13 +519,13 @@ def process_doc(doc_file):
508519

509520
if match:
510521
# Orphan page, don't list it.
511-
return "", ""
522+
return "", None
512523

513524
match = re.search(r".*:http-equiv=refresh: \d+;URL=(.*).html(.*)", content)
514525
# Is it a redirect?
515526
return check_name, match
516527

517-
def format_link(doc_file):
528+
def format_link(doc_file: Tuple[str, str]) -> str:
518529
check_name, match = process_doc(doc_file)
519530
if not match and check_name and not check_name.startswith("clang-analyzer-"):
520531
return " :doc:`%(check_name)s <%(module)s/%(check)s>`,%(autofix)s\n" % {
@@ -526,7 +537,7 @@ def format_link(doc_file):
526537
else:
527538
return ""
528539

529-
def format_link_alias(doc_file):
540+
def format_link_alias(doc_file: Tuple[str, str]) -> str:
530541
check_name, match = process_doc(doc_file)
531542
if (match or (check_name.startswith("clang-analyzer-"))) and check_name:
532543
module = doc_file[0]
@@ -543,6 +554,7 @@ def format_link_alias(doc_file):
543554
ref_end = "_"
544555
else:
545556
redirect_parts = re.search(r"^\.\./([^/]*)/([^/]*)$", match.group(1))
557+
assert redirect_parts
546558
title = redirect_parts[1] + "-" + redirect_parts[2]
547559
target = redirect_parts[1] + "/" + redirect_parts[2]
548560
autofix = has_auto_fix(title)
@@ -599,7 +611,7 @@ def format_link_alias(doc_file):
599611

600612

601613
# Adds a documentation for the check.
602-
def write_docs(module_path, module, check_name):
614+
def write_docs(module_path: str, module: str, check_name: str) -> None:
603615
check_name_dashes = module + "-" + check_name
604616
filename = os.path.normpath(
605617
os.path.join(
@@ -623,15 +635,15 @@ def write_docs(module_path, module, check_name):
623635
)
624636

625637

626-
def get_camel_name(check_name):
638+
def get_camel_name(check_name: str) -> str:
627639
return "".join(map(lambda elem: elem.capitalize(), check_name.split("-")))
628640

629641

630-
def get_camel_check_name(check_name):
642+
def get_camel_check_name(check_name: str) -> str:
631643
return get_camel_name(check_name) + "Check"
632644

633645

634-
def main():
646+
def main() -> None:
635647
language_to_extension = {
636648
"c": "c",
637649
"c++": "cpp",
@@ -754,7 +766,7 @@ def main():
754766
language_restrict = (
755767
f"%(lang)s.{cpp_language_to_requirements.get(args.standard, 'CPlusPlus')}"
756768
)
757-
elif language in ["objc", "objc++"]:
769+
else: # "objc" or "objc++"
758770
language_restrict = "%(lang)s.ObjC"
759771

760772
write_header(
@@ -769,7 +781,7 @@ def main():
769781
write_implementation(module_path, module, namespace, check_name_camel)
770782
adapt_module(module_path, module, check_name, check_name_camel)
771783
add_release_notes(module_path, module, check_name, description)
772-
test_extension = language_to_extension.get(language)
784+
test_extension = language_to_extension[language]
773785
write_test(module_path, module, check_name, test_extension, args.standard)
774786
write_docs(module_path, module, check_name)
775787
update_checks_list(clang_tidy_path)

0 commit comments

Comments
 (0)