3 # Copyright (C) 2020 Free Software Foundation, Inc.
5 # This file is part of GCC.
7 # GCC is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3, or (at your option)
12 # GCC is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with GCC; see the file COPYING. If not, write to
19 # the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 # Boston, MA 02110-1301, USA.
22 # This script parses a .diff file generated with 'diff -up' or 'diff -cp'
23 # and adds a skeleton ChangeLog file to the file. It does not try to be
24 # too smart when parsing function names, but it produces a reasonable
27 # Author: Martin Liska <mliska@suse.cz>
33 from itertools
import takewhile
37 from unidiff
import PatchSet
39 pr_regex
= re
.compile(r
'(\/(\/|\*)|[Cc*!])\s+(?P<pr>PR [a-z+-]+\/[0-9]+)')
40 dr_regex
= re
.compile(r
'(\/(\/|\*)|[Cc*!])\s+(?P<dr>DR [0-9]+)')
41 identifier_regex
= re
.compile(r
'^([a-zA-Z0-9_#].*)')
42 comment_regex
= re
.compile(r
'^\/\*')
43 struct_regex
= re
.compile(r
'^(class|struct|union|enum)\s+'
44 r
'(GTY\(.*\)\s+)?([a-zA-Z0-9_]+)')
45 macro_regex
= re
.compile(r
'#\s*(define|undef)\s+([a-zA-Z0-9_]+)')
46 super_macro_regex
= re
.compile(r
'^DEF[A-Z0-9_]+\s*\(([a-zA-Z0-9_]+)')
47 fn_regex
= re
.compile(r
'([a-zA-Z_][^()\s]*)\s*\([^*]')
48 template_and_param_regex
= re
.compile(r
'<[^<>]*>')
49 bugzilla_url
= 'https://gcc.gnu.org/bugzilla/rest.cgi/bug?id=%s&' \
50 'include_fields=summary'
52 function_extensions
= set(['.c', '.cpp', '.C', '.cc', '.h', '.inc', '.def'])
55 Generate ChangeLog template for PATCH.
56 PATCH must be generated using diff(1)'s -up or -cp options
57 (or their equivalent in git).
60 script_folder
= os
.path
.realpath(__file__
)
61 gcc_root
= os
.path
.dirname(os
.path
.dirname(script_folder
))
64 def find_changelog(path
):
65 folder
= os
.path
.split(path
)[0]
67 if os
.path
.exists(os
.path
.join(gcc_root
, folder
, 'ChangeLog')):
69 folder
= os
.path
.dirname(folder
)
72 raise AssertionError()
75 def extract_function_name(line
):
76 if comment_regex
.match(line
):
78 m
= struct_regex
.search(line
)
81 return m
.group(1) + ' ' + m
.group(3)
82 m
= macro_regex
.search(line
)
86 m
= super_macro_regex
.search(line
)
90 m
= fn_regex
.search(line
)
92 # Discard template and function parameters.
94 fn
= re
.sub(template_and_param_regex
, '', fn
)
99 def try_add_function(functions
, line
):
100 fn
= extract_function_name(line
)
101 if fn
and fn
not in functions
:
106 def sort_changelog_files(changed_file
):
107 return (changed_file
.is_added_file
, changed_file
.is_removed_file
)
110 def get_pr_titles(prs
):
113 id = pr
.split('/')[-1]
114 r
= requests
.get(bugzilla_url
% id)
115 bugs
= r
.json()['bugs']
117 output
+= '%s - %s\n' % (pr
, bugs
[0]['summary'])
124 def generate_changelog(data
, no_functions
=False, fill_pr_titles
=False):
129 diff
= PatchSet(data
)
132 changelog
= find_changelog(file.path
)
133 if changelog
not in changelogs
:
134 changelogs
[changelog
] = []
135 changelog_list
.append(changelog
)
136 changelogs
[changelog
].append(file)
138 # Extract PR entries from newly added tests
139 if 'testsuite' in file.path
and file.is_added_file
:
140 for line
in list(file)[0]:
141 m
= pr_regex
.search(line
.value
)
147 m
= dr_regex
.search(line
.value
)
156 out
+= get_pr_titles(prs
)
158 # sort ChangeLog so that 'testsuite' is at the end
159 for changelog
in sorted(changelog_list
, key
=lambda x
: 'testsuite' in x
):
160 files
= changelogs
[changelog
]
161 out
+= '%s:\n' % os
.path
.join(changelog
, 'ChangeLog')
165 # new and deleted files should be at the end
166 for file in sorted(files
, key
=sort_changelog_files
):
167 assert file.path
.startswith(changelog
)
168 in_tests
= 'testsuite' in changelog
or 'testsuite' in file.path
169 relative_path
= file.path
[len(changelog
):].lstrip('/')
171 if file.is_added_file
:
172 msg
= 'New test' if in_tests
else 'New file'
173 out
+= '\t* %s: %s.\n' % (relative_path
, msg
)
174 elif file.is_removed_file
:
175 out
+= '\t* %s: Removed.\n' % (relative_path
)
176 elif hasattr(file, 'is_rename') and file.is_rename
:
177 out
+= '\t* %s: Moved to...\n' % (relative_path
)
178 new_path
= file.target_file
[2:]
179 # A file can be theoretically moved to a location that
180 # belongs to a different ChangeLog. Let user fix it.
181 if new_path
.startswith(changelog
):
182 new_path
= new_path
[len(changelog
):].lstrip('/')
183 out
+= '\t* %s: ...here.\n' % (new_path
)
187 # Do not add function names for testsuite files
188 extension
= os
.path
.splitext(relative_path
)[1]
189 if not in_tests
and extension
in function_extensions
:
191 modified_visited
= False
194 m
= identifier_regex
.match(line
.value
)
195 if line
.is_added
or line
.is_removed
:
196 if not line
.value
.strip():
198 modified_visited
= True
199 if m
and try_add_function(functions
,
203 elif line
.is_context
:
204 if last_fn
and modified_visited
:
205 try_add_function(functions
, last_fn
)
207 modified_visited
= False
211 modified_visited
= False
213 try_add_function(functions
,
216 out
+= '\t* %s (%s):\n' % (relative_path
, functions
[0])
217 for fn
in functions
[1:]:
218 out
+= '\t(%s):\n' % fn
220 out
+= '\t* %s:\n' % relative_path
225 if __name__
== '__main__':
226 parser
= argparse
.ArgumentParser(description
=help_message
)
227 parser
.add_argument('input', nargs
='?',
228 help='Patch file (or missing, read standard input)')
229 parser
.add_argument('-s', '--no-functions', action
='store_true',
230 help='Do not generate function names in ChangeLogs')
231 parser
.add_argument('-p', '--fill-up-bug-titles', action
='store_true',
232 help='Download title of mentioned PRs')
233 parser
.add_argument('-c', '--changelog',
234 help='Append the ChangeLog to a git commit message '
236 args
= parser
.parse_args()
237 if args
.input == '-':
240 input = open(args
.input) if args
.input else sys
.stdin
242 output
= generate_changelog(data
, args
.no_functions
,
243 args
.fill_up_bug_titles
)
245 lines
= open(args
.changelog
).read().split('\n')
246 start
= list(takewhile(lambda l
: not l
.startswith('#'), lines
))
247 end
= lines
[len(start
):]
248 with
open(args
.changelog
, 'w') as f
:
254 # append 2 empty lines
256 f
.write('\n'.join(start
))
259 f
.write('\n'.join(end
))
261 print(output
, end
='')