File size: 5,486 Bytes
5cdf637 | 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | #!/usr/bin/env python3
## SPDX-License-Identifier: BSD-3-Clause
## Copyright (c) 2021-2026, The OpenROAD Authors
# Usage:
# cd src/<tool>
# ../../etc/FindMessages.py > messages.txt
import argparse
import glob
import os
import re
import sys
from collections import defaultdict
def parse_args():
parser = argparse.ArgumentParser(
description="""
Find logger calls and report sorted message IDs.
Also checks for duplicate message IDs.
""",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"-d",
"--dir",
default=os.getcwd(),
help="Directory to start the search for messages from",
)
parser.add_argument(
"-l",
"--local",
action="store_true",
help="Look only at the local files and don't recurse",
)
parser.add_argument(
"files",
nargs="*",
help="Explicit file paths to scan (alternative to -d directory walking)",
)
args = parser.parse_args()
return args
# The three capture groups are tool, id, and message.
warn_regexp_c = re.compile(
r"""
(?:->|\.) # deref
(?P<type>info|warn|fileWarn|error|fileError|critical) # type
\s*\(\s* # (
(?:utl::)?(?P<tool>[A-Z]{3}) # tool
\s*,\s* # ,
(?P<id>\d+) # id
\s*,\s* # ,
(?P<message>("((?:[^"\\]|\\.)+?\s*)+" )) # message
""",
re.VERBOSE | re.MULTILINE,
)
warn_regexp_tcl = re.compile(
r"""
(?P<type>info|warn|error|critical) # type
\s+ # white-space
(?P<tool>[A-Z]{3}|"[A-Z]{3}") # tool
\s+ # white-space
(?P<id>\d+) # id
\s+ # white-space
(?P<message>"(?:[^"]|\\.)+?") # message
""",
re.VERBOSE | re.MULTILINE,
)
def scan_file(path, file_name, msgs):
# Grab the file contents as a single string
with open(os.path.join(path, file_name), encoding="utf-8") as file_handle:
lines = file_handle.read()
warn_regexp = warn_regexp_tcl if file_name.endswith("tcl") else warn_regexp_c
for match in re.finditer(warn_regexp, lines):
tool = match.group("tool").strip('"')
msg_id = int(match.group("id"))
key = "{} {:04d}".format(tool, msg_id)
# remove quotes and join strings
message = match.group("message")
message = message.replace("\n", "")
message = message.rstrip()[1:-1]
message = re.sub(r'"\s*"', "", message)
message_type = match.group("type").upper()
# Count the newlines before the match starts
line_num = lines[0 : match.start()].count("\n") + 1
position = "{}:{}".format(file_name, line_num)
file_link = os.path.join(path, file_name).strip("../").replace("\\", "/")
file_link = "https://github.com/The-OpenROAD-Project/OpenROAD/tree/master/{}#L{}".format(
file_link, line_num
)
value = "{:25} {} {} {}".format(position, message, message_type, file_link)
msgs[key].add(value)
def scan_dir(path, files, msgs):
for file_name in files:
if re.search(r"\.(c|cc|cpp|cxx|h|hh|yy|ll|i|tcl)$", file_name):
scan_file(path, file_name, msgs)
def main():
args = parse_args()
# "tool id" -> "file:line message"
msgs = defaultdict(set)
if args.files:
# Scan explicitly listed files (used by Bazel where sandbox
# visibility is determined by the declared srcs).
for f in args.files:
path = os.path.dirname(f) or "."
name = os.path.basename(f)
if re.search(r"\.(c|cc|cpp|cxx|h|hh|yy|ll|i|tcl)$", name):
scan_file(path, name, msgs)
elif args.local: # no recursion
files = [
os.path.basename(file) for file in glob.glob(os.path.join(args.dir, "*"))
]
scan_dir(args.dir, files, msgs)
else:
for path, _, files in os.walk(args.dir):
scan_dir(path, files, msgs)
# Group numbers by set name
set_numbers = defaultdict(set)
for key in msgs:
set_name, number = key.split()
set_numbers[set_name].add(int(number))
has_error = False
for key in sorted(msgs):
count = len(msgs[key])
if count > 1:
set_name, number = key.split()
next_free_integer = int(number) + 1
while next_free_integer in set_numbers[set_name]:
next_free_integer += 1
print(
"Error: {} used {} times, next free message id is {}".format(
key, count, next_free_integer
),
file=sys.stderr,
)
for idloc in sorted(msgs[key]):
fileloc, *_ = idloc.split()
file, line = fileloc.split(":")
print(
" Appears in {} on line {} ".format(file, line),
file=sys.stderr,
)
has_error = True
for key in sorted(msgs):
for msg in sorted(msgs[key]):
print(key, msg)
if has_error:
sys.exit(1)
if __name__ == "__main__":
main()
|