diff --git a/.ci/azure-pipelines/formatting.yaml b/.ci/azure-pipelines/formatting.yaml index 84935030af7..16ea3ff0128 100644 --- a/.ci/azure-pipelines/formatting.yaml +++ b/.ci/azure-pipelines/formatting.yaml @@ -7,7 +7,7 @@ jobs: - checkout: self # find the commit hash on a quick non-forced update too fetchDepth: 10 - - script: ./.dev/format.sh $(which clang-format) . + - script: python ./.dev/format.py . displayName: 'Run clang-format' - script: git diff > formatting.patch displayName: 'Compute diff' diff --git a/.dev/format.py b/.dev/format.py new file mode 100644 index 00000000000..720abc7a273 --- /dev/null +++ b/.dev/format.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 + +import os +import sys +import subprocess +import argparse + +EXTENSIONS = (".c", ".h", ".cpp", ".hpp", ".cxx", ".hxx", ".cu") +WHITELIST_FILE = os.path.join(".dev", "whitelist.txt") + +def load_whitelist(): + if not os.path.isfile(WHITELIST_FILE): + print(f"Could not find whitelist file at {WHITELIST_FILE}") + sys.exit(167) + with open(WHITELIST_FILE, "r") as f: + return [line.strip() for line in f if line.strip()] + +def is_in_whitelist(file_path, whitelist): + file_path = os.path.normpath(file_path) + for w in whitelist: + w_norm = os.path.normpath(w) + if os.path.commonpath([file_path, w_norm]) == w_norm or file_path.startswith(w_norm + os.sep): + return True + return False + +def find_files(path): + if os.path.isfile(path): + if path.endswith(EXTENSIONS): + yield path + elif os.path.isdir(path): + for root, _, files in os.walk(path): + for file in files: + if file.endswith(EXTENSIONS): + yield os.path.join(root, file) + +def main(): + parser = argparse.ArgumentParser( + description="Format C/C++/CUDA files using clang-format via pipx." + ) + parser.add_argument( + "files", + nargs="+", + help="List of files to check/format, or a single '.' to check all whitelisted files." + ) + args = parser.parse_args() + + whitelist = load_whitelist() + manual_mode = len(args.files) == 1 and args.files[0] == "." + + if manual_mode: + all_files = [] + for rel_path in whitelist: + abs_path = os.path.join(os.getcwd(), rel_path) + all_files.extend(find_files(abs_path)) + else: + all_files = [ + f for f in args.files + if f.endswith(EXTENSIONS) and os.path.isfile(f) and is_in_whitelist(f, whitelist) + ] + + all_files = list(set(all_files)) # Remove duplicates + + if not all_files: + print("No files found to format.") + sys.exit(0) + + check_cmd = ["pipx", "run", "clang-format==14.0.3", "--dry-run", "--Werror", "--style=file"] + all_files + result = subprocess.run(check_cmd, capture_output=True, text=True) + if result.returncode != 0: + format_cmd = ["pipx", "run", "clang-format==14.0.3", "-i", "--style=file"] + all_files + subprocess.run(format_cmd) + if not manual_mode: + sys.exit(1) # Only fail for pre-commit + # In manual mode, just continue and exit 0 + + sys.exit(0) + +if __name__ == "__main__": + main() diff --git a/.dev/format.sh b/.dev/format.sh index e9bf4f3359e..29bc869f976 100755 --- a/.dev/format.sh +++ b/.dev/format.sh @@ -7,9 +7,6 @@ # $ sh /pcl/format.sh `which clang-format` /pcl format() { - # don't use a directory with whitespace - local whitelist="apps/3d_rec_framework apps/in_hand_scanner apps/include apps/modeler apps/src benchmarks 2d geometry ml octree simulation stereo tracking registration gpu/containers gpu/segmentation" - local PCL_DIR="${2}" local formatter="${1}" @@ -18,17 +15,22 @@ format() { exit 166 fi - # check for self if [ ! -f "${PCL_DIR}/.dev/format.sh" ]; then echo "Please ensure that PCL_SOURCE_DIR is passed as the second argument" exit 166 fi - for dir in ${whitelist}; do + local whitelist_file="${PCL_DIR}/.dev/whitelist.txt" + if [ ! -f "${whitelist_file}" ]; then + echo "Could not find whitelist file at ${whitelist_file}" + exit 167 + fi + + while IFS= read -r dir || [ -n "$dir" ]; do + [ -z "$dir" ] && continue path=${PCL_DIR}/${dir} - find ${path} -type f -iname *.[ch] -o -iname *.[ch]pp -o -iname *.[ch]xx \ - -iname *.cu | xargs -n1 ${formatter} -i -style=file - done + find ${path} -type f \( -iname "*.[ch]" -o -iname "*.[ch]pp" -o -iname "*.[ch]xx" -o -iname "*.cu" \) | xargs -n1 ${formatter} -i -style=file + done < "${whitelist_file}" } -format $@ +format "$@" diff --git a/.dev/whitelist.txt b/.dev/whitelist.txt new file mode 100644 index 00000000000..70185112ef6 --- /dev/null +++ b/.dev/whitelist.txt @@ -0,0 +1,16 @@ +apps/3d_rec_framework +apps/in_hand_scanner +apps/include +apps/modeler +apps/src +benchmarks +2d +geometry +ml +octree +simulation +stereo +tracking +registration +gpu/containers +gpu/segmentation diff --git a/gpu/containers/src/repacks.cu b/gpu/containers/src/repacks.cu index e99956a7500..e36b69a76c1 100644 --- a/gpu/containers/src/repacks.cu +++ b/gpu/containers/src/repacks.cu @@ -3,97 +3,94 @@ #include -namespace pcl +namespace pcl { +namespace device { +struct Info { + enum { SIZE = 4 }; + int data[SIZE]; +}; + +template +struct Point { + int data[n]; +}; + +template +__global__ void +deviceCopyFields4B(const Info info, const int size, const void* input, void* output) { - namespace device - { - struct Info - { - enum { SIZE = 4 }; - int data[SIZE]; - }; - - template - struct Point - { - int data[n]; - }; - - template - __global__ void deviceCopyFields4B(const Info info, const int size, const void* input, void* output) - { - int idx = blockIdx.x * blockDim.x + threadIdx.x; - - if (idx < size) - { - Point point_in = reinterpret_cast* >( input)[idx]; - Point point_out = reinterpret_cast< Point* >(output)[idx]; + int idx = blockIdx.x * blockDim.x + threadIdx.x; - for(int i = 0; i < Info::SIZE; ++i) - { - int byte; - int code = info.data[i]; + if (idx < size) { + Point point_in = reinterpret_cast*>(input)[idx]; + Point point_out = reinterpret_cast*>(output)[idx]; - byte = ((code >> 0) & 0xFF); + for (int i = 0; i < Info::SIZE; ++i) { + int byte; + int code = info.data[i]; - if (byte == 0xFF) - break; - else - point_out.data[byte >> 4] = point_in.data[byte & 0xF]; + byte = ((code >> 0) & 0xFF); - byte = ((code >> 8) & 0xFF); + if (byte == 0xFF) + break; + else + point_out.data[byte >> 4] = point_in.data[byte & 0xF]; - if (byte == 0xFF) - break; - else - point_out.data[byte >> 4] = point_in.data[byte & 0xF]; + byte = ((code >> 8) & 0xFF); - byte = ((code >> 16) & 0xFF); + if (byte == 0xFF) + break; + else + point_out.data[byte >> 4] = point_in.data[byte & 0xF]; - if (byte == 0xFF) - break; - else - point_out.data[byte >> 4] = point_in.data[byte & 0xF]; + byte = ((code >> 16) & 0xFF); - byte = ((code >> 24) & 0xFF); + if (byte == 0xFF) + break; + else + point_out.data[byte >> 4] = point_in.data[byte & 0xF]; - if (byte == 0xFF) - break; - else - point_out.data[byte >> 4] = point_in.data[byte & 0xF]; - } + byte = ((code >> 24) & 0xFF); - reinterpret_cast< Point* >(output)[idx] = point_out; - } - }; + if (byte == 0xFF) + break; + else + point_out.data[byte >> 4] = point_in.data[byte & 0xF]; + } - template - void cf(int info[4], int size, const void* input, void* output) - { - Info i; - std::copy(info, info + 4, i.data); + reinterpret_cast*>(output)[idx] = point_out; + } +}; - dim3 block(256); - dim3 grid(divUp(size, block.x)); +template +void +cf(int info[4], int size, const void* input, void* output) +{ + Info i; + std::copy(info, info + 4, i.data); - deviceCopyFields4B<<>>(i, size, input, output); - cudaSafeCall ( cudaGetLastError () ); - cudaSafeCall (cudaDeviceSynchronize ()); - } + dim3 block(256); + dim3 grid(divUp(size, block.x)); - using copy_fields_t = void (*)(int info[4], int size, const void* input, void* output); - } + deviceCopyFields4B<<>>(i, size, input, output); + cudaSafeCall(cudaGetLastError()); + cudaSafeCall(cudaDeviceSynchronize()); } -namespace pcl -{ - namespace gpu - { - using namespace pcl::device; +using copy_fields_t = void (*)(int info[4], int size, const void* input, void* output); +} // namespace device +} // namespace pcl - PCL_EXPORTS void copyFieldsImpl(int in_size, int out_size, int rules[4], int size, const void* input, void* output) - { - pcl::device::copy_fields_t funcs[16][16] = +namespace pcl { +namespace gpu { +using namespace pcl::device; + +PCL_EXPORTS void +copyFieldsImpl( + int in_size, int out_size, int rules[4], int size, const void* input, void* output) +{ + // clang-format off + pcl::device::copy_fields_t funcs[16][16] = { { /**/ cf<1,1>, cf<1, 2>, cf<1, 3>, cf<1, 4>, /**/ cf<1, 5>, cf<1, 6>, cf<1, 7>, cf<1, 8>, /**/ cf<1, 9>, cf<1,10>, cf<1, 11>, cf<1, 12>, /**/ cf<1, 13>, cf<1, 14>, cf<1, 15>, cf<1,16> }, { /**/ cf<2,1>, cf<2, 2>, cf<2, 3>, cf<2, 4>, /**/ cf<2, 5>, cf<2, 6>, cf<2, 7>, cf<2, 8>, /**/ cf<2, 9>, cf<1,10>, cf<2, 11>, cf<2, 12>, /**/ cf<2, 13>, cf<2, 14>, cf<2, 15>, cf<2,16> }, @@ -112,9 +109,8 @@ namespace pcl { /**/ cf<15,1>, cf<15,2>, cf<15,3>, cf<15,4>, /**/ cf<15,5>, cf<15,6>, cf<15,7>, cf<15,8>, /**/ cf<15,9>, cf<1,10>, cf<15,11>, cf<15,12>, /**/ cf<15,13>, cf<15,14>, cf<15,15>, cf<15,16> }, { /**/ cf<16,1>, cf<16,2>, cf<16,3>, cf<16,4>, /**/ cf<16,5>, cf<16,6>, cf<16,7>, cf<16,8>, /**/ cf<16,9>, cf<1,10>, cf<16,11>, cf<16,12>, /**/ cf<16,13>, cf<16,14>, cf<16,15>, cf<16,16> } }; - - funcs[in_size-1][out_size-1](rules, size, input, output); - } - } + // clang-format on + funcs[in_size - 1][out_size - 1](rules, size, input, output); } - +} // namespace gpu +} // namespace pcl