File: check_all_groups.py

package info (click to toggle)
pico-sdk 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,960 kB
  • sloc: ansic: 150,165; asm: 13,474; python: 2,885; cpp: 2,192; yacc: 381; lex: 270; makefile: 33; sh: 15; javascript: 13
file content (136 lines) | stat: -rwxr-xr-x 5,955 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
#
# Copyright (c) 2025 Raspberry Pi Ltd
#
# SPDX-License-Identifier: BSD-3-Clause
#
#
# Script to check that the "groups" in various contexts all match up. (like an enhanced version of check_doxygen_groups.py)
# Note that it only reports the *first* instance of a missing group, not all occurrences.
#
# Usage:
#
# tools/check_all_groups.py <root of repo>
#   (you'll probably want to pipe the output through ` | grep -v cmsis` )

import re
import sys
import os

scandir = sys.argv[1]

DEFGROUP_NAME = r'\defgroup'
DEFGROUP_RE = re.compile(r'{}\s+(\w+)'.format(re.escape(DEFGROUP_NAME)))
INGROUP_NAME = r'\ingroup'
INGROUP_RE = re.compile(r'{}\s+(\w+)'.format(re.escape(INGROUP_NAME)))
DOCS_INDEX_HEADER = 'docs/index.h'

BASE_CONFIG_NAME = 'PICO_CONFIG'
CONFIG_RE = re.compile(r'//\s+{}:\s+(\w+),\s+([^,]+)(?:,\s+(.*))?$'.format(BASE_CONFIG_NAME))
BASE_CMAKE_CONFIG_NAME = 'PICO_CMAKE_CONFIG'
CMAKE_CONFIG_RE = re.compile(r'#\s+{}:\s+([\w-]+),\s+([^,]+)(?:,\s+(.*))?$'.format(BASE_CMAKE_CONFIG_NAME))
BASE_BUILD_DEFINE_NAME = 'PICO_BUILD_DEFINE'
BUILD_DEFINE_RE = re.compile(r'#\s+{}:\s+(\w+),\s+([^,]+)(?:,\s+(.*))?$'.format(BASE_BUILD_DEFINE_NAME))
BASE_GROUP_NAME = 'group='
GROUP_RE = re.compile(r'\b{}(\w+)\b'.format(BASE_GROUP_NAME))

def_groups = {}
in_groups = {}
doc_groups = {}
config_groups = {}
cmake_config_groups = {}
build_define_groups = {}
any_errors = False

def get_group_from_config_attrs(attr_str):
    m = GROUP_RE.search(attr_str)
    if m:
        return m.group(1)

# Scan all .c and .h and .S and .cmake and CMakeLists.txt files in the specific path, recursively.

for dirpath, dirnames, filenames in os.walk(scandir):
    for filename in filenames:
        file_ext = os.path.splitext(filename)[1]
        if filename == 'CMakeLists.txt' or file_ext in ('.c', '.h', '.S', '.cmake'):
            file_path = os.path.join(dirpath, filename)
            with open(file_path) as fh:
                for line in fh.readlines():
                    m = DEFGROUP_RE.search(line)
                    if m:
                        group = m.group(1)
                        if file_path.endswith(DOCS_INDEX_HEADER):
                            if group in doc_groups:
                                any_errors = True
                                print("{} uses {} {} but so does {}".format(doc_groups[group], DEFGROUP_NAME, group, file_path))
                            else:
                                doc_groups[group] = file_path
                        else:
                            if group in def_groups:
                                any_errors = True
                                print("{} uses {} {} but so does {}".format(def_groups[group], DEFGROUP_NAME, group, file_path))
                            else:
                                def_groups[group] = file_path
                    else:
                        m = INGROUP_RE.search(line)
                        if m:
                            group = m.group(1)
                            if group not in in_groups:
                                in_groups[group] = file_path
                        else:
                            m = CONFIG_RE.search(line)
                            if m:
                                group = get_group_from_config_attrs(m.group(3))
                                if group not in config_groups:
                                    config_groups[group] = file_path
                            else:
                                m = CMAKE_CONFIG_RE.search(line)
                                if m:
                                    group = get_group_from_config_attrs(m.group(3))
                                    if group not in cmake_config_groups:
                                        cmake_config_groups[group] = file_path
                                else:
                                    m = BUILD_DEFINE_RE.search(line)
                                    if m:
                                        group = get_group_from_config_attrs(m.group(3))
                                        if group not in build_define_groups:
                                            build_define_groups[group] = file_path

seen_groups = set()
for group, file_path in in_groups.items():
    seen_groups.add(group)
    if group not in def_groups and group not in doc_groups:
        any_errors = True
        print("{} uses {} {} which was never defined".format(file_path, INGROUP_NAME, group))
for group, file_path in config_groups.items():
    seen_groups.add(group)
    if group not in def_groups and group not in doc_groups:
        any_errors = True
        print("{} uses {} {}{} which was never defined".format(file_path, BASE_CONFIG_NAME, BASE_GROUP_NAME, group))
for group, file_path in cmake_config_groups.items():
    seen_groups.add(group)
    if group == 'build': # dummy group
        continue
    if group not in def_groups and group not in doc_groups:
        any_errors = True
        print("{} uses {} {}{} which was never defined".format(file_path, BASE_CMAKE_CONFIG_NAME, BASE_GROUP_NAME, group))
for group, file_path in build_define_groups.items():
    seen_groups.add(group)
    if group == 'build': # dummy group
        continue
    if group not in def_groups and group not in doc_groups:
        any_errors = True
        print("{} uses {} {}{} which was never defined".format(file_path, BASE_BUILD_DEFINE_NAME, BASE_GROUP_NAME, group))

for group in doc_groups.keys():
    seen_groups.add(group)
    if group not in seen_groups and group not in def_groups:
        any_errors = True
        print("{} uses {} {} which doesn't appear anywhere else".format(DOCS_INDEX_HEADER, DEFGROUP_NAME, group))

unused_groups = set(def_groups.keys()) - seen_groups
if unused_groups:
    any_errors = True
    print("The following groups were defined with {} but never referenced:\n{}".format(DEFGROUP_NAME, sorted(unused_groups)))

sys.exit(any_errors)