src: add some noexcept

This commit is contained in:
Markus F.X.J. Oberhumer
2023-10-26 00:28:36 +02:00
parent 2724039c09
commit 29b4752d0e
4 changed files with 70 additions and 34 deletions
+48 -16
View File
@@ -308,10 +308,21 @@ def main():
if yaml:
parser.add_argument(
"-export-fixes",
metavar="filename",
metavar="file_or_directory",
dest="export_fixes",
help="Create a yaml file to store suggested fixes in, "
"which can be applied with clang-apply-replacements.",
help="A directory or a yaml file to store suggested fixes in, "
"which can be applied with clang-apply-replacements. If the "
"parameter is a directory, the fixes of each compilation unit are "
"stored in individual yaml files in the directory.",
)
else:
parser.add_argument(
"-export-fixes",
metavar="directory",
dest="export_fixes",
help="A directory to store suggested fixes in, which can be applied "
"with clang-apply-replacements. The fixes of each compilation unit are "
"stored in individual yaml files in the directory.",
)
parser.add_argument(
"-j",
@@ -384,14 +395,35 @@ def main():
clang_tidy_binary = find_binary(args.clang_tidy_binary, "clang-tidy", build_path)
tmpdir = None
if args.fix:
clang_apply_replacements_binary = find_binary(
args.clang_apply_replacements_binary, "clang-apply-replacements", build_path
)
if args.fix or (yaml and args.export_fixes):
tmpdir = tempfile.mkdtemp()
combine_fixes = False
export_fixes_dir = None
delete_fixes_dir = False
if args.export_fixes is not None:
# if a directory is given, create it if it does not exist
if args.export_fixes.endswith(os.path.sep) and not os.path.isdir(
args.export_fixes
):
os.makedirs(args.export_fixes)
if not os.path.isdir(args.export_fixes):
if not yaml:
raise RuntimeError(
"Cannot combine fixes in one yaml file. Either install PyYAML or specify an output directory."
)
combine_fixes = True
if os.path.isdir(args.export_fixes):
export_fixes_dir = args.export_fixes
if export_fixes_dir is None and (args.fix or combine_fixes):
export_fixes_dir = tempfile.mkdtemp()
delete_fixes_dir = True
# Load the database and extract all files.
database = json.load(open(os.path.join(build_path, db_path)))
@@ -410,8 +442,8 @@ def main():
matched_files.append(name)
files = sorted(matched_files)
if not files:
if tmpdir:
shutil.rmtree(tmpdir)
if delete_fixes_dir:
shutil.rmtree(export_fixes_dir)
sys.exit(0)
try:
@@ -462,7 +494,7 @@ def main():
args=(
args,
clang_tidy_binary,
tmpdir,
export_fixes_dir,
build_path,
task_queue,
lock,
@@ -486,14 +518,14 @@ def main():
# This is a sad hack. Unfortunately subprocess goes
# bonkers with ctrl-c and we start forking merrily.
print("\nCtrl-C detected, goodbye.")
if tmpdir:
shutil.rmtree(tmpdir)
if delete_fixes_dir:
shutil.rmtree(export_fixes_dir)
os.kill(0, 9)
if yaml and args.export_fixes:
if combine_fixes:
print("Writing fixes to " + args.export_fixes + " ...")
try:
merge_replacement_files(tmpdir, args.export_fixes)
merge_replacement_files(export_fixes_dir, args.export_fixes)
except:
print("Error exporting fixes.\n", file=sys.stderr)
traceback.print_exc()
@@ -502,14 +534,14 @@ def main():
if args.fix:
print("Applying fixes ...")
try:
apply_fixes(args, clang_apply_replacements_binary, tmpdir)
apply_fixes(args, clang_apply_replacements_binary, export_fixes_dir)
except:
print("Error applying fixes.\n", file=sys.stderr)
traceback.print_exc()
return_code = 1
if tmpdir:
shutil.rmtree(tmpdir)
if delete_fixes_dir:
shutil.rmtree(export_fixes_dir)
sys.exit(return_code)