Skip to content

Commit fc96a15

Browse files
tristan0x1uc
andauthored
simplify ClangTidy.merge_clang_tidy_checks (#142)
Fixes #125 Co-authored-by: Luc Grosheintz <[email protected]>
1 parent a9738bf commit fc96a15

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

cpp/lib.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -867,35 +867,26 @@ def merge_clang_tidy_checks(cls, orig_checks, new_checks):
867867
"""Merge 2 'Checks' ClangTidy configuration key values"""
868868
if orig_checks is None:
869869
return new_checks
870+
if new_checks is None:
871+
return orig_checks
870872
orig_checks = [check.strip() for check in orig_checks.split(",")]
871873
new_checks = [check.strip() for check in new_checks.split(",")]
872874

873875
for new_check in new_checks:
874-
if new_check.startswith("-"):
875-
name = new_check[1:]
876-
# remove check when check=google-runtime-references to_=-google-*
877-
orig_checks = list(
878-
check for check in orig_checks if not fnmatch(check, name)
879-
)
880-
# remove check when check=-google-runtime-references
881-
# to_=-google-* (simplification)
882-
orig_checks = list(
883-
check for check in orig_checks if not fnmatch(check, new_check)
884-
)
885-
else:
886-
# remove check when check=-google-runtime-references to_=google-*
887-
orig_checks = list(
888-
check
889-
for check in orig_checks
890-
if not fnmatch(check, "-" + new_check)
891-
)
892-
# remove check when check=google-runtime-references
893-
# to_=google-* (simplification)
894-
orig_checks = list(
895-
check for check in orig_checks if not fnmatch(check, new_check)
896-
)
876+
name_without_prefix = (
877+
new_check[1:] if new_check.startswith("-") else new_check
878+
)
879+
name_with_prefix = "-" + name_without_prefix
880+
orig_checks = list(
881+
check for check in orig_checks if not fnmatch(check, name_with_prefix)
882+
)
883+
orig_checks = list(
884+
check
885+
for check in orig_checks
886+
if not fnmatch(check, name_without_prefix)
887+
)
897888
orig_checks.append(new_check)
898-
return ",".join(orig_checks)
889+
return ",".join(c for c in orig_checks if c)
899890

900891

901892
class TaskDescription(

dev/test_lib.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@
99
import cpp.lib # noqa: E402
1010

1111

12+
def test_clang_tidy_conf_merger():
13+
orig_checks = "foo-*,bar-pika,-bar-foo"
14+
test_func = cpp.lib.ClangTidy.merge_clang_tidy_checks
15+
16+
assert test_func(orig_checks, None) == orig_checks
17+
assert test_func(orig_checks, "") == orig_checks
18+
assert test_func(orig_checks, "-bar-pika") == "foo-*,-bar-foo,-bar-pika"
19+
assert test_func(orig_checks, "bar-pika") == "foo-*,-bar-foo,bar-pika"
20+
assert test_func(orig_checks, "-bar-*") == "foo-*,-bar-*"
21+
assert test_func(orig_checks, "bar-*") == "foo-*,bar-*"
22+
assert test_func(orig_checks, "-bar-*") == "foo-*,-bar-*"
23+
assert test_func(orig_checks, "-bar-foo") == "foo-*,bar-pika,-bar-foo"
24+
assert test_func(orig_checks, "bar-foo") == "foo-*,bar-pika,bar-foo"
25+
26+
assert test_func("bar-foo", "-bar-*") == "-bar-*"
27+
28+
assert test_func("", "-bar-pika") == "-bar-pika"
29+
assert test_func("", "bar-foo") == "bar-foo"
30+
assert test_func(None, None) is None
31+
32+
1233
def test_where():
1334
"""Test cpp.lib.where function"""
1435
with tempfile.TemporaryDirectory() as bin_dir:

0 commit comments

Comments
 (0)