4

I want the python script run-clang-tidy to:

  1. check files with .cc, .h, or .hpp extension
  2. exclude anything in any folder called build

For example,

  • check <project folder>/src/../include/solitaire/helloworld.h
  • exclude <project folder>/build/<folder>/catch_reporters_all.hpp

I try the following:

cd <project folder>
# -- Make a `compiled_commands.json` -- #
mkdir build
cd build
cmake .. -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
# -- Run in the build folder -- #
run-clang-tidy '^((?!/build/).)*(\.cc|\.h)$' \
    -header-filter '(\.cc|\.h|.hpp)$'

But it reports warning for:

<project folder>/build/_deps/catch2-src/src/catch2/
    ../catch2/reporters/catch_reporters_all.hpp:21:9:
error: header guard does not follow preferred style
    [llvm-header-guard,-warnings-as-errors]

Setting the -header-filter option to '^((?!/build/).)*(\.cc|\.h|.hpp)$' still doesn't work. That would exclude helloworld.h:

<project folder>/src/../include/solitaire/helloworld.h:1:1: 
error: header is missing header guard
    [llvm-header-guard,-warnings-as-errors]

What went wrong?

Crawl Cycle
  • 257
  • 2
  • 8

1 Answers1

0

You can use below given code this have your root directory, file_extentions which needs to be listed and exclude directories which will be excluded from search

import os
import sys

def list_files_with_extensions(root_dir, file_extensions, exclude_directories):
    matched_files = []

    for root, dirs, files in os.walk(root_dir):
        # Exclude directories starting with any of the specified exclude_directories strings
        dirs[:] = [d for d in dirs if not any(d.startswith(exclude_dir) for exclude_dir in exclude_directories)]

        for file in files:
            if any(file.endswith(file_extension) for file_extension in file_extensions):
                matched_files.append(os.path.join(root, file))

    return matched_files

if len(sys.argv) < 4:
    print("Usage: python script.py <root_folder> <file_extension1,file_extension2,...> <exclude_directory1,exclude_directory2,...>")
    sys.exit(1)

root_folder = sys.argv[1]
file_extensions = sys.argv[2].split(',')
exclude_directories = sys.argv[3].split(',')

matched_files = list_files_with_extensions(root_folder, file_extensions, exclude_directories)

for file in matched_files:
    print(file)