1#!/usr/bin/env python3
2#
3# Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7#
8# Little script to check that every \ingroup has a matching \defgroup
9#
10# Usage:
11#
12# Run from the root of the tree to check
13
14
15import subprocess
16import re
17import sys
18import os
19
20groups = {}
21any_errors = False
22
23res = subprocess.run(['git', 'grep', '\\defgroup'], check=True, stdout=subprocess.PIPE)
24for line in res.stdout.decode('utf8').split('\n'):
25    m = re.match(r'^(\S+):.*\\defgroup\s+(\w+)', line)
26    if m:
27        filename = m.group(1)
28        group = m.group(2)
29        if os.path.basename(filename) in ('check_doxygen_groups.py', 'index.h'):
30            continue
31        if group in groups:
32            any_errors = True
33            print("{} uses \\defgroup {} but so does {}".format(groups[group], group, filename))
34        else:
35            groups[group] = filename
36
37res = subprocess.run(['git', 'grep', '\\ingroup'], check=True, stdout=subprocess.PIPE)
38for line in res.stdout.decode('utf8').split('\n'):
39    m = re.match(r'^(\S+):.*\\ingroup\s+(\w+)', line)
40    if m:
41        filename = m.group(1)
42        group = m.group(2)
43        if os.path.basename(filename) in ('check_doxygen_groups.py', 'index.h'):
44            continue
45        if group not in groups:
46            any_errors = True
47            print("{} uses \\ingroup {} which was never defined".format(filename, group))
48
49sys.exit(any_errors)
50