From: Remi Gacogne Date: Fri, 20 Oct 2023 15:32:10 +0000 (+0200) Subject: Fix clang-tidy analysis X-Git-Tag: rec-5.0.0-beta1~23^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=93e51e834f49d9fd41209adbf83d2005f16ebbdb;p=thirdparty%2Fpdns.git Fix clang-tidy analysis --- diff --git a/.github/scripts/git-filter.py b/.github/scripts/git-filter.py index aa05904f0a..d1206cd65a 100755 --- a/.github/scripts/git-filter.py +++ b/.github/scripts/git-filter.py @@ -7,35 +7,69 @@ the current directory. """ +import argparse import os import sys +from pathlib import Path import helpers import unidiff +def create_argument_parser(): + """Create command-line argument parser.""" + parser = argparse.ArgumentParser( + description="Filter git diff files that are not in the product" + ) + parser.add_argument( + "--product", + type=str, + required=True, + help="Product (auth, dnsdist or rec)", + ) + return parser.parse_args() + def main(): """Start the script.""" - # It might be tempting to normalize the paths here instead of - # rewriting the compilation database, but then clang-tidy - # loses the depth of files in the repository, outputing for - # example "credentials.cc" instead of "pdns/credentials.cc" + args = create_argument_parser() + product = args.product + compdb = helpers.load_compdb("compile_commands.json") compdb = helpers.index_compdb(compdb) - pdns_path = os.path.join("pdns", "") - cwd = os.getcwd() - root = helpers.get_repo_root() + cwd = Path(os.getcwd()) diff = sys.stdin.read() patch_set = unidiff.PatchSet(diff) for patch in patch_set: - path = os.path.join(root, patch.path) - if path in compdb: - print(patch) + path = Path(patch.path) + if product == 'auth': + path = Path(cwd).joinpath(path) else: - msg = f"Skipping {path}: it is not in the compilation db" - print(msg, file=sys.stderr) + if str(path).startswith('modules'): + print(f'Skipping {path}: modules do not apply to {product}', file=sys.stderr) + continue + + if str(path).startswith('ext'): + subpath = Path(cwd).joinpath(path) + else: + subpath = Path(cwd).joinpath(path.name) + + if not subpath.exists(): + print(f'Skipping {path}: does not exist for {product} ({subpath})', file=sys.stderr) + continue + + path = subpath + if patch.source_file is not None: + patch.source_file = str(path) + patch.target_file = str(path) + + if not str(path) in compdb: + print(f'Skipping {path}: it is not in the compilation db', file=sys.stderr) + continue + + print(patch, file=sys.stderr) + print(patch) return 0 diff --git a/.github/scripts/helpers.py b/.github/scripts/helpers.py index 36969c8060..cc170d1eed 100644 --- a/.github/scripts/helpers.py +++ b/.github/scripts/helpers.py @@ -1,9 +1,7 @@ """Helpers for dealing with git, compilation databases, etc.""" -import pathlib import json import os -import sys import git import yaml @@ -48,28 +46,3 @@ def index_compdb(file_contents): filename = os.path.join(item["directory"], item["file"]) result.add(filename) return result - -def normalize_dist_dir(version, distPath): - """Map the path of a source file from inside the dist directory - to its path in the git repository.""" - # get rid of the distdir path, to get file paths as they are in the repository - repositoryPath = pathlib.Path(get_repo_root()).resolve() - distPath = pathlib.Path(distPath).resolve() - if f'pdns-{version}' in distPath.parts: - # authoritative or tool - authPath = repositoryPath.joinpath(f'pdns-{version}').resolve() - relativeToAuth = distPath.relative_to(authPath) - return str(repositoryPath.joinpath(relativeToAuth)) - - if f'pdns-recursor-{version}' in distPath.parts: - recPath = repositoryPath.joinpath('pdns', 'recursordist', f'pdns-recursor-{version}').resolve() - relativeToRec = distPath.relative_to(recPath) - return str(repositoryPath.joinpath('pdns', 'recursordist', relativeToRec).resolve()) - - if f'dnsdist-{version}' in distPath.parts: - dnsdistPath = repositoryPath.joinpath('pdns', 'dnsdistdist', f'dnsdist-{version}').resolve() - relativeToDist = distPath.relative_to(dnsdistPath) - return str(repositoryPath.joinpath('pdns', 'dnsdistdist', relativeToDist).resolve()) - - print(f'Unable to map {distPath}', file=sys.stderr) - return str(distPath) diff --git a/.github/scripts/normalize_paths_in_compilation_database.py b/.github/scripts/normalize_paths_in_compilation_database.py deleted file mode 100644 index f5645fb390..0000000000 --- a/.github/scripts/normalize_paths_in_compilation_database.py +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env python3 - -import argparse -import os - -import json - -import helpers - -def create_argument_parser(): - """Create command-line argument parser.""" - parser = argparse.ArgumentParser( - description="Normalize paths in compilation database" - ) - parser.add_argument( - "--version", - type=str, - required=True, - help="Version number of the current build", - ) - parser.add_argument('database') - return parser.parse_args() - -if __name__ == "__main__": - """Start the script.""" - args = create_argument_parser() - - compDB = helpers.load_compdb(args.database) - for entry in compDB: - for key in ['file', 'directory']: - if key in entry: - entry[key] = helpers.normalize_dist_dir(args.version, entry[key]) - - with open(args.database + '.temp', 'w', encoding='utf-8') as outputFile: - json.dump(compDB, outputFile, ensure_ascii=False, indent=2) - - os.rename(args.database + '.temp', args.database) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 104b12d475..adb9369618 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -110,13 +110,9 @@ jobs: inv ci-auth-make-bear - run: ln -s .clang-tidy.full .clang-tidy if: matrix.product == 'auth' - - run: cp ./pdns/compile_commands.json . - if: matrix.product == 'auth' - - run: cat compile_commands.json - if: matrix.product == 'auth' - name: Run clang-tidy for auth if: matrix.product == 'auth' - run: git diff -U0 HEAD^..HEAD | python3 .github/scripts/git-filter.py | /usr/bin/clang-tidy-diff-${CLANG_VERSION}.py -clang-tidy-binary /usr/bin/clang-tidy-${CLANG_VERSION} -extra-arg=-ferror-limit=0 -p1 -export-fixes clang-tidy-auth.yml + run: git diff -U0 HEAD^..HEAD | python3 .github/scripts/git-filter.py --product auth | /usr/bin/clang-tidy-diff-${CLANG_VERSION}.py -clang-tidy-binary /usr/bin/clang-tidy-${CLANG_VERSION} -extra-arg=-ferror-limit=0 -p0 -export-fixes clang-tidy-auth.yml - name: Print clang-tidy fixes YAML for auth if: matrix.product == 'auth' shell: bash @@ -160,17 +156,16 @@ jobs: working-directory: ./pdns/dnsdistdist/ run: | inv ci-dnsdist-make-bear - - run: ln -s .clang-tidy.full .clang-tidy - if: matrix.product == 'dnsdist' - - run: cp ./pdns/dnsdistdist/compile_commands.json . - if: matrix.product == 'dnsdist' - - run: cat compile_commands.json + - run: ln -s ../../.clang-tidy.full .clang-tidy if: matrix.product == 'dnsdist' + working-directory: ./pdns/dnsdistdist/ - name: Run clang-tidy for dnsdist if: matrix.product == 'dnsdist' - run: git diff -U0 HEAD^..HEAD | python3 .github/scripts/git-filter.py | /usr/bin/clang-tidy-diff-${CLANG_VERSION}.py -clang-tidy-binary /usr/bin/clang-tidy-${CLANG_VERSION} -extra-arg=-ferror-limit=0 -p1 -export-fixes clang-tidy-dnsdist.yml + working-directory: ./pdns/dnsdistdist/ + run: git diff -U0 HEAD^..HEAD | python3 ../../.github/scripts/git-filter.py --product dnsdist | /usr/bin/clang-tidy-diff-${CLANG_VERSION}.py -clang-tidy-binary /usr/bin/clang-tidy-${CLANG_VERSION} -extra-arg=-ferror-limit=0 -p0 -export-fixes clang-tidy-dnsdist.yml - name: Print clang-tidy fixes YAML for dnsdist if: matrix.product == 'dnsdist' + working-directory: ./pdns/dnsdistdist/ shell: bash run: | if [ -f clang-tidy-dnsdist.yml ]; then @@ -179,11 +174,12 @@ jobs: - name: Result annotations for dnsdist if: matrix.product == 'dnsdist' id: clang-tidy-annotations-dnsdist + working-directory: ./pdns/dnsdistdist/ shell: bash run: | if [ -f clang-tidy-dnsdist.yml ]; then set +e - python3 .github/scripts/clang-tidy.py --fixes-file clang-tidy-dnsdist.yml + python3 ../../.github/scripts/clang-tidy.py --fixes-file clang-tidy-dnsdist.yml echo "failed=$?" >> $GITHUB_OUTPUT fi @@ -209,17 +205,16 @@ jobs: working-directory: ./pdns/recursordist/ run: | CONCURRENCY=4 inv ci-rec-make-bear - - run: ln -s .clang-tidy.full .clang-tidy - if: matrix.product == 'rec' - - run: cp ./pdns/recursordist/compile_commands.json . - if: matrix.product == 'rec' - - run: cat compile_commands.json + - run: ln -s ../../.clang-tidy.full .clang-tidy if: matrix.product == 'rec' + working-directory: ./pdns/recursordist/ - name: Run clang-tidy for rec if: matrix.product == 'rec' - run: git diff -U0 HEAD^..HEAD | python3 .github/scripts/git-filter.py | /usr/bin/clang-tidy-diff-${CLANG_VERSION}.py -clang-tidy-binary /usr/bin/clang-tidy-${CLANG_VERSION} -extra-arg=-ferror-limit=0 -p1 -export-fixes clang-tidy-rec.yml + working-directory: ./pdns/recursordist/ + run: git diff -U0 HEAD^..HEAD | python3 ../../.github/scripts/git-filter.py --product rec | /usr/bin/clang-tidy-diff-${CLANG_VERSION}.py -clang-tidy-binary /usr/bin/clang-tidy-${CLANG_VERSION} -extra-arg=-ferror-limit=0 -p0 -export-fixes clang-tidy-rec.yml - name: Print clang-tidy fixes YAML for rec if: matrix.product == 'rec' + working-directory: ./pdns/recursordist/ shell: bash run: | if [ -f clang-tidy-rec.yml ]; then @@ -228,11 +223,12 @@ jobs: - name: Result annotations for rec if: matrix.product == 'rec' id: clang-tidy-annotations-rec + working-directory: ./pdns/recursordist/ shell: bash run: | if [ -f clang-tidy-rec.yml ]; then set +e - python3 .github/scripts/clang-tidy.py --fixes-file clang-tidy-rec.yml + python3 ../../.github/scripts/clang-tidy.py --fixes-file clang-tidy-rec.yml echo "failed=$?" >> $GITHUB_OUTPUT fi diff --git a/tasks.py b/tasks.py index 66cc25e929..00e7dd160a 100644 --- a/tasks.py +++ b/tasks.py @@ -596,9 +596,7 @@ def ci_auth_make(c): @task def ci_auth_make_bear(c): concurrency = os.getenv('CONCURRENCY', 8) - # Needed for clang-tidy -line-filter vs project structure shenanigans - with c.cd('pdns'): - c.run(f'bear --append -- make -j{concurrency} -k V=1 -C ..') + c.run(f'bear --append -- make -j{concurrency} -k V=1') @task def ci_rec_make(c):