From 16440d09fc496116b8fe1d81995c56ce4deace16 Mon Sep 17 00:00:00 2001 From: Thomas Ramstad Date: Fri, 5 Nov 2021 15:17:15 +0100 Subject: [PATCH] Add clang script and .clang example (#52) Basic usage: clang-format-all src/ Advanced usage: clang-format-all project1/ project2/ project3/ --- .clang-format-2 | 13 ++++++++ clang-format-all | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 .clang-format-2 create mode 100755 clang-format-all diff --git a/.clang-format-2 b/.clang-format-2 new file mode 100644 index 00000000..85c4e21c --- /dev/null +++ b/.clang-format-2 @@ -0,0 +1,13 @@ +# clang-format +--- +Language: Cpp +BasedOnStyle: LLVM +AccessModifierOffset: -4 +IndentWidth: 4 + +# Our includes are not order-agnostic +SortIncludes: false + +# Some of our comments include insightful insight +ReflowComments: false +... diff --git a/clang-format-all b/clang-format-all new file mode 100755 index 00000000..f2744b0c --- /dev/null +++ b/clang-format-all @@ -0,0 +1,84 @@ +#!/bin/bash +# +# clang-format-all: a tool to run clang-format on an entire project +# Copyright (C) 2016 Evan Klitzke +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +function usage { + echo "Usage: $0 DIR..." + exit 1 +} + +if [ $# -eq 0 ]; then + usage +fi + +# Variable that will hold the name of the clang-format command +FMT="" + +# Some distros just call it clang-format. Others (e.g. Ubuntu) are insistent +# that the version number be part of the command. We prefer clang-format if +# that's present, otherwise we work backwards from highest version to lowest +# version. +for clangfmt in clang-format{,-{4,3}.{9,8,7,6,5,4,3,2,1,0}}; do + if which "$clangfmt" &>/dev/null; then + FMT="$clangfmt" + break + fi +done + +# Check if we found a working clang-format +if [ -z "$FMT" ]; then + echo "failed to find clang-format" + exit 1 +fi + +# Check all of the arguments first to make sure they're all directories +for dir in "$@"; do + if [ ! -d "${dir}" ]; then + echo "${dir} is not a directory" + usage + fi +done + +# Find a dominating file, starting from a given directory and going up. +find-dominating-file() { + if [ -r "$1"/"$2" ]; then + return 0 + fi + if [ "$1" = "/" ]; then + return 1 + fi + find-dominating-file "$(realpath "$1"/..)" "$2" + return $? +} + +# Run clang-format -i on all of the things +for dir in "$@"; do + pushd "${dir}" &>/dev/null + if ! find-dominating-file . .clang-format; then + echo "Failed to find dominating .clang-format starting at $PWD" + continue + fi + find . \ + \( -name '*.c' \ + -o -name '*.cc' \ + -o -name '*.cpp' \ + -o -name '*.h' \ + -o -name '*.hh' \ + -o -name '*.hpp' \) \ + -exec "${FMT}" -i '{}' \; + popd &>/dev/null +done \ No newline at end of file