| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
#===- rename_check.py - clang-tidy check renamer ------------*- python -*--===# |
| 4 |
# |
| 5 |
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 6 |
# See https://llvm.org/LICENSE.txt for license information. |
| 7 |
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 8 |
# |
| 9 |
#===-----------------------------------------------------------------------===# |
| 10 |
|
| 11 |
from __future__ import unicode_literals |
| 12 |
|
| 13 |
import argparse |
| 14 |
import glob |
| 15 |
import io |
| 16 |
import os |
| 17 |
import re |
| 18 |
|
| 19 |
def replaceInFileRegex(fileName, sFrom, sTo): |
| 20 |
if sFrom == sTo: |
| 21 |
return |
| 22 |
|
| 23 |
# The documentation files are encoded using UTF-8, however on Windows the |
| 24 |
# default encoding might be different (e.g. CP-1252). To make sure UTF-8 is |
| 25 |
# always used, use `io.open(filename, mode, encoding='utf8')` for reading and |
| 26 |
# writing files here and elsewhere. |
| 27 |
txt = None |
| 28 |
with io.open(fileName, 'r', encoding='utf8') as f: |
| 29 |
txt = f.read() |
| 30 |
|
| 31 |
txt = re.sub(sFrom, sTo, txt) |
| 32 |
print("Replacing '%s' -> '%s' in '%s'..." % (sFrom, sTo, fileName)) |
| 33 |
with io.open(fileName, 'w', encoding='utf8') as f: |
| 34 |
f.write(txt) |
| 35 |
|
| 36 |
|
| 37 |
def replaceInFile(fileName, sFrom, sTo): |
| 38 |
if sFrom == sTo: |
| 39 |
return |
| 40 |
txt = None |
| 41 |
with io.open(fileName, 'r', encoding='utf8') as f: |
| 42 |
txt = f.read() |
| 43 |
|
| 44 |
if sFrom not in txt: |
| 45 |
return |
| 46 |
|
| 47 |
txt = txt.replace(sFrom, sTo) |
| 48 |
print("Replacing '%s' -> '%s' in '%s'..." % (sFrom, sTo, fileName)) |
| 49 |
with io.open(fileName, 'w', encoding='utf8') as f: |
| 50 |
f.write(txt) |
| 51 |
|
| 52 |
|
| 53 |
def generateCommentLineHeader(filename): |
| 54 |
return ''.join(['//===--- ', |
| 55 |
os.path.basename(filename), |
| 56 |
' - clang-tidy ', |
| 57 |
'-' * max(0, 42 - len(os.path.basename(filename))), |
| 58 |
'*- C++ -*-===//']) |
| 59 |
|
| 60 |
|
| 61 |
def generateCommentLineSource(filename): |
| 62 |
return ''.join(['//===--- ', |
| 63 |
os.path.basename(filename), |
| 64 |
' - clang-tidy', |
| 65 |
'-' * max(0, 52 - len(os.path.basename(filename))), |
| 66 |
'-===//']) |
| 67 |
|
| 68 |
|
| 69 |
def fileRename(fileName, sFrom, sTo): |
| 70 |
if sFrom not in fileName or sFrom == sTo: |
| 71 |
return fileName |
| 72 |
newFileName = fileName.replace(sFrom, sTo) |
| 73 |
print("Renaming '%s' -> '%s'..." % (fileName, newFileName)) |
| 74 |
os.rename(fileName, newFileName) |
| 75 |
return newFileName |
| 76 |
|
| 77 |
|
| 78 |
def deleteMatchingLines(fileName, pattern): |
| 79 |
lines = None |
| 80 |
with io.open(fileName, 'r', encoding='utf8') as f: |
| 81 |
lines = f.readlines() |
| 82 |
|
| 83 |
not_matching_lines = [l for l in lines if not re.search(pattern, l)] |
| 84 |
if len(not_matching_lines) == len(lines): |
| 85 |
return False |
| 86 |
|
| 87 |
print("Removing lines matching '%s' in '%s'..." % (pattern, fileName)) |
| 88 |
print(' ' + ' '.join([l for l in lines if re.search(pattern, l)])) |
| 89 |
with io.open(fileName, 'w', encoding='utf8') as f: |
| 90 |
f.writelines(not_matching_lines) |
| 91 |
|
| 92 |
return True |
| 93 |
|
| 94 |
|
| 95 |
def getListOfFiles(clang_tidy_path): |
| 96 |
files = glob.glob(os.path.join(clang_tidy_path, '**'), recursive=True) |
| 97 |
files += glob.glob(os.path.join(clang_tidy_path, '..', 'test', |
| 98 |
'clang-tidy', 'checkers', '**'), recursive=True) |
| 99 |
files += glob.glob(os.path.join(clang_tidy_path, '..', 'docs', |
| 100 |
'clang-tidy', 'checks', '*')) |
| 101 |
return [filename for filename in files if os.path.isfile(filename)] |
| 102 |
|
| 103 |
|
| 104 |
# Adapts the module's CMakelist file. Returns 'True' if it could add a new |
| 105 |
# entry and 'False' if the entry already existed. |
| 106 |
def adapt_cmake(module_path, check_name_camel): |
| 107 |
filename = os.path.join(module_path, 'CMakeLists.txt') |
| 108 |
with io.open(filename, 'r', encoding='utf8') as f: |
| 109 |
lines = f.readlines() |
| 110 |
|
| 111 |
cpp_file = check_name_camel + '.cpp' |
| 112 |
|
| 113 |
# Figure out whether this check already exists. |
| 114 |
for line in lines: |
| 115 |
if line.strip() == cpp_file: |
| 116 |
return False |
| 117 |
|
| 118 |
print('Updating %s...' % filename) |
| 119 |
with io.open(filename, 'w', encoding='utf8') as f: |
| 120 |
cpp_found = False |
| 121 |
file_added = False |
| 122 |
for line in lines: |
| 123 |
cpp_line = line.strip().endswith('.cpp') |
| 124 |
if (not file_added) and (cpp_line or cpp_found): |
| 125 |
cpp_found = True |
| 126 |
if (line.strip() > cpp_file) or (not cpp_line): |
| 127 |
f.write(' ' + cpp_file + '\n') |
| 128 |
file_added = True |
| 129 |
f.write(line) |
| 130 |
|
| 131 |
return True |
| 132 |
|
| 133 |
# Modifies the module to include the new check. |
| 134 |
def adapt_module(module_path, module, check_name, check_name_camel): |
| 135 |
modulecpp = next(iter(filter( |
| 136 |
lambda p: p.lower() == module.lower() + 'tidymodule.cpp', |
| 137 |
os.listdir(module_path)))) |
| 138 |
filename = os.path.join(module_path, modulecpp) |
| 139 |
with io.open(filename, 'r', encoding='utf8') as f: |
| 140 |
lines = f.readlines() |
| 141 |
|
| 142 |
print('Updating %s...' % filename) |
| 143 |
with io.open(filename, 'w', encoding='utf8') as f: |
| 144 |
header_added = False |
| 145 |
header_found = False |
| 146 |
check_added = False |
| 147 |
check_decl = (' CheckFactories.registerCheck<' + check_name_camel + |
| 148 |
'>(\n "' + check_name + '");\n') |
| 149 |
|
| 150 |
for line in lines: |
| 151 |
if not header_added: |
| 152 |
match = re.search('#include "(.*)"', line) |
| 153 |
if match: |
| 154 |
header_found = True |
| 155 |
if match.group(1) > check_name_camel: |
| 156 |
header_added = True |
| 157 |
f.write('#include "' + check_name_camel + '.h"\n') |
| 158 |
elif header_found: |
| 159 |
header_added = True |
| 160 |
f.write('#include "' + check_name_camel + '.h"\n') |
| 161 |
|
| 162 |
if not check_added: |
| 163 |
if line.strip() == '}': |
| 164 |
check_added = True |
| 165 |
f.write(check_decl) |
| 166 |
else: |
| 167 |
match = re.search('registerCheck<(.*)>', line) |
| 168 |
if match and match.group(1) > check_name_camel: |
| 169 |
check_added = True |
| 170 |
f.write(check_decl) |
| 171 |
f.write(line) |
| 172 |
|
| 173 |
|
| 174 |
# Adds a release notes entry. |
| 175 |
def add_release_notes(clang_tidy_path, old_check_name, new_check_name): |
| 176 |
filename = os.path.normpath(os.path.join(clang_tidy_path, |
| 177 |
'../docs/ReleaseNotes.rst')) |
| 178 |
with io.open(filename, 'r', encoding='utf8') as f: |
| 179 |
lines = f.readlines() |
| 180 |
|
| 181 |
lineMatcher = re.compile('Renamed checks') |
| 182 |
nextSectionMatcher = re.compile('Improvements to include-fixer') |
| 183 |
checkMatcher = re.compile('- The \'(.*)') |
| 184 |
|
| 185 |
print('Updating %s...' % filename) |
| 186 |
with io.open(filename, 'w', encoding='utf8') as f: |
| 187 |
note_added = False |
| 188 |
header_found = False |
| 189 |
add_note_here = False |
| 190 |
|
| 191 |
for line in lines: |
| 192 |
if not note_added: |
| 193 |
match = lineMatcher.match(line) |
| 194 |
match_next = nextSectionMatcher.match(line) |
| 195 |
match_check = checkMatcher.match(line) |
| 196 |
if match_check: |
| 197 |
last_check = match_check.group(1) |
| 198 |
if last_check > old_check_name: |
| 199 |
add_note_here = True |
| 200 |
|
| 201 |
if match_next: |
| 202 |
add_note_here = True |
| 203 |
|
| 204 |
if match: |
| 205 |
header_found = True |
| 206 |
f.write(line) |
| 207 |
continue |
| 208 |
|
| 209 |
if line.startswith('^^^^'): |
| 210 |
f.write(line) |
| 211 |
continue |
| 212 |
|
| 213 |
if header_found and add_note_here: |
| 214 |
if not line.startswith('^^^^'): |
| 215 |
f.write("""- The '%s' check was renamed to :doc:`%s |
| 216 |
<clang-tidy/checks/%s>` |
| 217 |
|
| 218 |
""" % (old_check_name, new_check_name, new_check_name)) |
| 219 |
note_added = True |
| 220 |
|
| 221 |
f.write(line) |
| 222 |
|
| 223 |
def main(): |
| 224 |
parser = argparse.ArgumentParser(description='Rename clang-tidy check.') |
| 225 |
parser.add_argument('old_check_name', type=str, |
| 226 |
help='Old check name.') |
| 227 |
parser.add_argument('new_check_name', type=str, |
| 228 |
help='New check name.') |
| 229 |
parser.add_argument('--check_class_name', type=str, |
| 230 |
help='Old name of the class implementing the check.') |
| 231 |
args = parser.parse_args() |
| 232 |
|
| 233 |
old_module = args.old_check_name.split('-')[0] |
| 234 |
new_module = args.new_check_name.split('-')[0] |
| 235 |
if args.check_class_name: |
| 236 |
check_name_camel = args.check_class_name |
| 237 |
else: |
| 238 |
check_name_camel = (''.join(map(lambda elem: elem.capitalize(), |
| 239 |
args.old_check_name.split('-')[1:])) + |
| 240 |
'Check') |
| 241 |
|
| 242 |
new_check_name_camel = (''.join(map(lambda elem: elem.capitalize(), |
| 243 |
args.new_check_name.split('-')[1:])) + |
| 244 |
'Check') |
| 245 |
|
| 246 |
clang_tidy_path = os.path.dirname(__file__) |
| 247 |
|
| 248 |
header_guard_variants = [ |
| 249 |
(args.old_check_name.replace('-', '_')).upper() + '_CHECK', |
| 250 |
(old_module + '_' + check_name_camel).upper(), |
| 251 |
(old_module + '_' + new_check_name_camel).upper(), |
| 252 |
args.old_check_name.replace('-', '_').upper()] |
| 253 |
header_guard_new = (new_module + '_' + new_check_name_camel).upper() |
| 254 |
|
| 255 |
old_module_path = os.path.join(clang_tidy_path, old_module) |
| 256 |
new_module_path = os.path.join(clang_tidy_path, new_module) |
| 257 |
|
| 258 |
if (args.old_check_name != args.new_check_name): |
| 259 |
# Remove the check from the old module. |
| 260 |
cmake_lists = os.path.join(old_module_path, 'CMakeLists.txt') |
| 261 |
check_found = deleteMatchingLines(cmake_lists, '\\b' + check_name_camel) |
| 262 |
if not check_found: |
| 263 |
print("Check name '%s' not found in %s. Exiting." % |
| 264 |
(check_name_camel, cmake_lists)) |
| 265 |
return 1 |
| 266 |
|
| 267 |
modulecpp = next(iter(filter( |
| 268 |
lambda p: p.lower() == old_module.lower() + 'tidymodule.cpp', |
| 269 |
os.listdir(old_module_path)))) |
| 270 |
deleteMatchingLines(os.path.join(old_module_path, modulecpp), |
| 271 |
'\\b' + check_name_camel + '|\\b' + args.old_check_name) |
| 272 |
|
| 273 |
old_check_filename = '-'.join(args.old_check_name.split('-')[1:]) |
| 274 |
new_check_filename = '-'.join(args.new_check_name.split('-')[1:]) |
| 275 |
|
| 276 |
for filename in getListOfFiles(clang_tidy_path): |
| 277 |
originalName = filename |
| 278 |
filename = fileRename(filename, old_check_filename, new_check_filename) |
| 279 |
filename = fileRename(filename, check_name_camel, new_check_name_camel) |
| 280 |
replaceInFile(filename, generateCommentLineHeader(originalName), |
| 281 |
generateCommentLineHeader(filename)) |
| 282 |
replaceInFile(filename, generateCommentLineSource(originalName), |
| 283 |
generateCommentLineSource(filename)) |
| 284 |
for header_guard in header_guard_variants: |
| 285 |
replaceInFile(filename, header_guard, header_guard_new) |
| 286 |
|
| 287 |
if args.new_check_name + '.rst' in filename: |
| 288 |
replaceInFile( |
| 289 |
filename, |
| 290 |
args.old_check_name + '\n' + '=' * len(args.old_check_name) + '\n', |
| 291 |
args.new_check_name + '\n' + '=' * len(args.new_check_name) + '\n') |
| 292 |
|
| 293 |
replaceInFile(filename, args.old_check_name, args.new_check_name) |
| 294 |
replaceInFile(filename, old_module + '::' + check_name_camel, |
| 295 |
new_module + '::' + new_check_name_camel) |
| 296 |
replaceInFile(filename, old_module + '/' + check_name_camel, |
| 297 |
new_module + '/' + new_check_name_camel) |
| 298 |
replaceInFile(filename, check_name_camel, new_check_name_camel) |
| 299 |
|
| 300 |
if old_module != new_module or new_module == 'llvm': |
| 301 |
if new_module == 'llvm': |
| 302 |
new_namespace = new_module + '_check' |
| 303 |
else: |
| 304 |
new_namespace = new_module |
| 305 |
check_implementation_files = glob.glob( |
| 306 |
os.path.join(old_module_path, new_check_name_camel + '*')) |
| 307 |
for filename in check_implementation_files: |
| 308 |
# Move check implementation to the directory of the new module. |
| 309 |
filename = fileRename(filename, old_module_path, new_module_path) |
| 310 |
replaceInFileRegex(filename, |
| 311 |
'namespace clang::tidy::' + old_module + '[^ \n]*', |
| 312 |
'namespace clang::tidy::' + new_namespace) |
| 313 |
|
| 314 |
if (args.old_check_name == args.new_check_name): |
| 315 |
return |
| 316 |
|
| 317 |
# Add check to the new module. |
| 318 |
adapt_cmake(new_module_path, new_check_name_camel) |
| 319 |
adapt_module(new_module_path, new_module, args.new_check_name, |
| 320 |
new_check_name_camel) |
| 321 |
|
| 322 |
os.system(os.path.join(clang_tidy_path, 'add_new_check.py') |
| 323 |
+ ' --update-docs') |
| 324 |
add_release_notes(clang_tidy_path, args.old_check_name, args.new_check_name) |
| 325 |
|
| 326 |
|
| 327 |
if __name__ == '__main__': |
| 328 |
main() |