|
| 1 | +import re |
| 2 | +import enum |
| 3 | +from dataclasses import dataclass |
| 4 | +from sys import stderr |
| 5 | +from typing import Optional, List |
| 6 | +import dataclasses |
| 7 | +import json |
| 8 | +import argparse |
| 9 | + |
| 10 | +""" |
| 11 | +Regexes for matching warnings: |
| 12 | +/tmp/libsnowflakeclient/include/snowflake/SF_CRTFunctionSafe.h:223:16: warning: unused parameter 'in_sizeOfBuffer' [-Wunused-parameter] <- message |
| 13 | + 223 | size_t in_sizeOfBuffer, <- snippet |
| 14 | +""" |
| 15 | +message_re = re.compile(r"""(?P<file_path>[^:]*):(?P<line>\d+):(?:(?P<col>\d+):)?\s+warning:(?P<message>.*)\[(?P<flag>.*)\]""") |
| 16 | + |
| 17 | +class ParserState(enum.Enum): |
| 18 | + MESSAGE = "MESSAGE" |
| 19 | + SNIPPET = "SNIPPET" |
| 20 | + |
| 21 | +@dataclass |
| 22 | +class CompilerWarning: |
| 23 | + file_path: str |
| 24 | + line: int |
| 25 | + column: Optional[int] |
| 26 | + message: str |
| 27 | + flag: str |
| 28 | + snippet: Optional[str] |
| 29 | + source: str |
| 30 | + def key(self): |
| 31 | + return self.file_path, self.message, self.flag |
| 32 | + |
| 33 | +@dataclass |
| 34 | +class WarningDiff: |
| 35 | + new: List[CompilerWarning] |
| 36 | + old: List[CompilerWarning] |
| 37 | + |
| 38 | +""" |
| 39 | +Parses warnings from compiler output |
| 40 | +""" |
| 41 | +def parse_warnings(path: str) -> List[CompilerWarning]: |
| 42 | + warnings = [] |
| 43 | + state = ParserState.MESSAGE |
| 44 | + with open(path, "r") as f: |
| 45 | + lines = f.readlines() |
| 46 | + for line in lines: |
| 47 | + if state == ParserState.MESSAGE: |
| 48 | + m_match = message_re.match(line) |
| 49 | + if not m_match: |
| 50 | + continue |
| 51 | + |
| 52 | + col = m_match.group("col") |
| 53 | + if col: |
| 54 | + col = int(col) |
| 55 | + |
| 56 | + warning = CompilerWarning( |
| 57 | + file_path=m_match.group("file_path"), |
| 58 | + line=int(m_match.group("line")), |
| 59 | + column=col, |
| 60 | + message=m_match.group("message"), |
| 61 | + flag=m_match.group("flag"), |
| 62 | + snippet=None, |
| 63 | + source=line |
| 64 | + ) |
| 65 | + |
| 66 | + warnings.append(warning) |
| 67 | + state = ParserState.SNIPPET |
| 68 | + continue |
| 69 | + |
| 70 | + if state == ParserState.SNIPPET: |
| 71 | + warning.snippet = line |
| 72 | + warning.source += line |
| 73 | + |
| 74 | + state = ParserState.MESSAGE |
| 75 | + continue |
| 76 | + |
| 77 | + result = [] |
| 78 | + for w in warnings: |
| 79 | + if w not in result: |
| 80 | + result.append(w) |
| 81 | + return result |
| 82 | + |
| 83 | +def dump_warnings(warnings: List[CompilerWarning]) -> str: |
| 84 | + warnings_as_dict = [dataclasses.asdict(w) for w in warnings] |
| 85 | + return json.dumps(warnings_as_dict, indent=2) |
| 86 | + |
| 87 | +def load_warnings(warnings_json: str) -> List[CompilerWarning]: |
| 88 | + warnings_as_dict = json.loads(warnings_json) |
| 89 | + return [CompilerWarning(**w) for w in warnings_as_dict] |
| 90 | + |
| 91 | +def write(path: str, data: str): |
| 92 | + with open(path, "w") as f: |
| 93 | + f.write(data) |
| 94 | + |
| 95 | +def read(path: str) -> str: |
| 96 | + with open(path, "r") as f: |
| 97 | + return f.read() |
| 98 | + |
| 99 | +def generate_report(path: str, new_warnings: List[CompilerWarning], old_warnings: List[CompilerWarning]): |
| 100 | + with open(path, "w") as f: |
| 101 | + diff = {} |
| 102 | + for nw in new_warnings: |
| 103 | + if nw.key() not in diff: |
| 104 | + diff[nw.key()] = WarningDiff(new=[], old=[]) |
| 105 | + |
| 106 | + diff[nw.key()].new.append(nw) |
| 107 | + |
| 108 | + for ow in old_warnings: |
| 109 | + if ow.key() not in diff: |
| 110 | + diff[ow.key()] = WarningDiff(new=[], old=[]) |
| 111 | + |
| 112 | + diff[ow.key()].old.append(ow) |
| 113 | + |
| 114 | + failed = False |
| 115 | + for d in diff.values(): |
| 116 | + if len(d.new) > len(d.old): |
| 117 | + failed = True |
| 118 | + |
| 119 | + if failed: |
| 120 | + f.write("### Failed\n\n") |
| 121 | + else: |
| 122 | + f.write("### Succeeded\n\n") |
| 123 | + |
| 124 | + for d in diff.values(): |
| 125 | + balance = len(d.new) - len(d.old) |
| 126 | + if balance < 0: |
| 127 | + f.write("- Removed {} compiler warnings from {} [{}].\n".format(-balance, d.old[0].file_path, d.old[0].flag)) |
| 128 | + |
| 129 | + if balance > 0: |
| 130 | + f.write("- Added {} compiler warnings to {} [{}]. Please remove following warnings:\n".format(balance, d.new[0].file_path, d.new[0].flag)) |
| 131 | + for w in d.new: |
| 132 | + f.write("```\n") |
| 133 | + f.write(w.source) |
| 134 | + f.write("```\n") |
| 135 | + |
| 136 | +parser = argparse.ArgumentParser( |
| 137 | + prog='generate_warning_report', |
| 138 | + description='Generate compiler warning report', |
| 139 | + epilog='Text at the bottom of help' |
| 140 | +) |
| 141 | + |
| 142 | +parser.add_argument('--build-log', required=True) # option that takes a value |
| 143 | +parser.add_argument('--load-warnings', required=True) |
| 144 | +parser.add_argument('--dump-warnings', required=True) |
| 145 | +parser.add_argument('--report', required=True) |
| 146 | +args = parser.parse_args() |
| 147 | + |
| 148 | +new_warnings = parse_warnings(args.build_log) |
| 149 | +old_warnings = load_warnings(read(args.load_warnings)) |
| 150 | +generate_report(args.report, new_warnings, old_warnings) |
| 151 | +write(args.dump_warnings, dump_warnings(new_warnings)) |
| 152 | + |
0 commit comments