Skip to content

Commit b6acf80

Browse files
committed
dt: Add a check for undocumented compatible strings in kernel
Add a make target, dt_compatible_check, to extract compatible strings from kernel sources and check if they are documented by a schema. At least version v2022.08 of dtschema with dt-check-compatible is required. This check can also be run manually on specific files or directories: scripts/dtc/dt-extract-compatibles drivers/clk/ | \ xargs dt-check-compatible -v -s Documentation/devicetree/bindings/processed-schema.json Currently, there are about 3800 undocumented compatible strings. Most of these are cases where the binding is not yet converted (given there are 1900 .txt binding files remaining). Link: https://lore.kernel.org/all/20220916012510.2718170-1-robh@kernel.org/ Signed-off-by: Rob Herring <robh@kernel.org>
1 parent d7c6ea0 commit b6acf80

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

Documentation/devicetree/bindings/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,6 @@ always-$(CHECK_DT_BINDING) += $(patsubst $(srctree)/$(src)/%.yaml,%.example.dtb,
7575
# build artifacts here before they are processed by scripts/Makefile.clean
7676
clean-files = $(shell find $(obj) \( -name '*.example.dts' -o \
7777
-name '*.example.dtb' \) -delete 2>/dev/null)
78+
79+
dt_compatible_check: $(obj)/processed-schema.json
80+
$(Q)$(srctree)/scripts/dtc/dt-extract-compatibles $(srctree) | xargs dt-check-compatible -v -s $<

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,10 @@ PHONY += dt_binding_check
14191419
dt_binding_check: scripts_dtc
14201420
$(Q)$(MAKE) $(build)=Documentation/devicetree/bindings
14211421

1422+
PHONY += dt_compatible_check
1423+
dt_compatible_check: dt_binding_check
1424+
$(Q)$(MAKE) $(build)=Documentation/devicetree/bindings $@
1425+
14221426
# ---------------------------------------------------------------------------
14231427
# Modules
14241428

scripts/dtc/dt-extract-compatibles

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python3
2+
# SPDX-License-Identifier: GPL-2.0-only
3+
4+
import os
5+
import glob
6+
import re
7+
import argparse
8+
9+
10+
def parse_of_declare_macros(data):
11+
""" Find all compatible strings in OF_DECLARE() style macros """
12+
compat_list = []
13+
# CPU_METHOD_OF_DECLARE does not have a compatible string
14+
for m in re.finditer(r'(?<!CPU_METHOD_)(IRQCHIP|OF)_(DECLARE|MATCH)(_DRIVER)?\(.*?\)', data):
15+
try:
16+
compat = re.search(r'"(.*?)"', m[0])[1]
17+
except:
18+
# Fails on compatible strings in #define, so just skip
19+
continue
20+
compat_list += [compat]
21+
22+
return compat_list
23+
24+
25+
def parse_of_device_id(data):
26+
""" Find all compatible strings in of_device_id structs """
27+
compat_list = []
28+
for m in re.finditer(r'of_device_id\s+[a-zA-Z0-9_]+\[\]\s*=\s*({.*?);', data):
29+
compat_list += re.findall(r'\.compatible\s+=\s+"([a-zA-Z0-9_\-,]+)"', m[1])
30+
31+
return compat_list
32+
33+
34+
def parse_compatibles(file):
35+
with open(file, 'r', encoding='utf-8') as f:
36+
data = f.read().replace('\n', '')
37+
38+
compat_list = parse_of_declare_macros(data)
39+
compat_list += parse_of_device_id(data)
40+
41+
return compat_list
42+
43+
def print_compat(filename, compatibles):
44+
if not compatibles:
45+
return
46+
if show_filename:
47+
compat_str = ' '.join(compatibles)
48+
print(filename + ": compatible(s): " + compat_str)
49+
else:
50+
print(*compatibles, sep='\n')
51+
52+
show_filename = False
53+
54+
if __name__ == "__main__":
55+
ap = argparse.ArgumentParser()
56+
ap.add_argument("cfile", type=str, nargs='*', help="C source files or directories to parse")
57+
ap.add_argument('-H', '--with-filename', help="Print filename with compatibles", action="store_true")
58+
args = ap.parse_args()
59+
60+
show_filename = args.with_filename
61+
62+
for f in args.cfile:
63+
if os.path.isdir(f):
64+
for filename in glob.iglob(f + "/**/*.c", recursive=True):
65+
compat_list = parse_compatibles(filename)
66+
print_compat(filename, compat_list)
67+
else:
68+
compat_list = parse_compatibles(f)
69+
print_compat(f, compat_list)

0 commit comments

Comments
 (0)