blob: 2de10df85965637b89746478f3c03b6c23e7cf78 [file] [log] [blame]
Miguel Young de la Sotafc174e82019-12-17 13:12:15 -06001#!/usr/bin/env python3
2# Copyright lowRISC contributors.
3# Licensed under the Apache License, Version 2.0, see LICENSE for details.
4# SPDX-License-Identifier: Apache-2.0
5
6# fix_include_guard.py script takes any number of C or C++ header files as
7# input, and formats their include guards to conform to the style mandated
8# by Google C++. See
9# https://google.github.io/styleguide/cppguide.html#The__define_Guard
10#
11# clang-format does not, unfortunately, have support for automatically
12# generating the correct include guards; hence the existence of this script.
13#
14# This script will write the names of all affected files to stdout; if no such
15# output is present, all files have the correct guards. This script is
16# idempotent.
17
18import argparse
19import os
20import sys
21import re
22
23from pathlib import Path
24
25PROJECT_NAME = "OPENTITAN"
26
27# This file is $REPO_TOP/util/fix_include_guard.py, so it takes two parent()
28# calls to get back to the top.
29REPO_TOP = Path(__file__).resolve().parent.parent
30
31
32def main():
33 parser = argparse.ArgumentParser()
34 parser.add_argument(
35 '--dry-run',
36 action='store_true',
37 help='report writes which would have happened')
38 parser.add_argument(
39 'headers',
40 type=str,
41 nargs='+',
42 help='headers to fix guards for')
43 args = parser.parse_args()
44
45 total_fixes = 0
46 for header_path in args.headers:
47 header = Path(header_path).resolve().relative_to(REPO_TOP)
48 if header.suffix != '.h' or 'vendor' in header.parts:
49 continue
50
51 uppercase_dir = re.sub(r'[^\w]', '_', str(header.parent)).upper()
52 uppercase_stem = re.sub(r'[^\w]', '_', str(header.stem)).upper()
53 guard = '%s_%s_%s_H_' % (PROJECT_NAME, uppercase_dir, uppercase_stem)
54
55 header_text = header.read_text()
56 header_original = header_text
57
58 # Find the old guard name, which will be the first #ifndef in the file.
59 old_guard = re.search(r'#ifndef +(\w+)', header_text).group(1)
60
61 # Fix the guards at the top, which are guaranteed to be there.
62 header_text = re.sub('#(ifndef|define) +%s' % (old_guard, ),
63 r'#\1 %s' % (guard, ), header_text)
64
65 # Fix up the endif. Since this is the last thing in the file, and it
66 # might be missing the comment, we just truncate the file, and add on
67 # the required guard end.
68 header_text = header_text[:header_text.rindex('#endif')]
69 header_text += "#endif // %s\n" % (guard, )
70
71 if header_text != header_original:
72 print('Fixing header: "%s"' % (header, ), file=sys.stdout)
73 total_fixes += 1
74 if not args.dry_run:
75 header.write_text(header_text)
76
77 print('Fixed %d files.' % (total_fixes, ), file=sys.stderr)
78
79
80if __name__ == '__main__':
81 main()