Skip to content

Commit

Permalink
Merge fixes files of clang-tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
rhaschke committed Nov 2, 2022
1 parent 4fcabdf commit f69e5fa
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
48 changes: 48 additions & 0 deletions industrial_ci/src/tests/merge_fixes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Copyright (c) 2022, Robert Haschke
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import yaml
import sys


def merge_fixes(files):
"""Merge all fixes files into mergefile"""
# The fixes suggested by clang-tidy >= 4.0.0 are given under
# the top level key 'Diagnostics' in the output yaml files
mergefile = files[0]
mergekey = "Diagnostics"
merged = []
for file in files:
try:
with open(file, 'r') as inp:
content = yaml.safe_load(inp)
if not content:
continue # Skip empty files.
merged.extend(content.get(mergekey, []))
except FileNotFoundError:
pass

with open(mergefile, 'w') as out:
if merged:
# Assemble output dict with MainSourceFile=''.
output = {'MainSourceFile': '', mergekey: merged}
yaml.safe_dump(output, out)


if __name__ == "__main__":
merge_fixes(sys.argv[1:])
16 changes: 16 additions & 0 deletions industrial_ci/src/tests/source_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,24 @@ function run_clang_tidy_check {

ici_hook "before_clang_tidy_checks"

# replace -export-fixes <filename> with temporary file
local fixes_final=""
local fixes_tmp
local num_args=${#clang_tidy_args[@]}
fixes_tmp=$(mktemp)
for (( i=0; i<num_args; i++ )); do
if [ "${clang_tidy_args[i]}" == "-export-fixes" ]; then
fixes_final="${clang_tidy_args[i+1]}"
clang_tidy_args[i+1]="$fixes_tmp"
fi
done

# run clang-tidy checks on all build folders in target_ws
while read -r db; do
run_clang_tidy "$target_ws/src" warnings errors "$db" "${clang_tidy_args[@]}"
if [ -n "${fixes_final}" ]; then
"${ICI_SRC_PATH}/tests/merge_fixes.py" "$fixes_final" "$fixes_tmp"
fi
done < <(find "$target_ws/build" -mindepth 2 -name compile_commands.json) # -mindepth 2, because colcon puts a compile_commands.json into the build folder

ici_hook "after_clang_tidy_checks"
Expand Down

0 comments on commit f69e5fa

Please sign in to comment.