diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000000..4b5f426689 --- /dev/null +++ b/.clang-format @@ -0,0 +1,98 @@ +# Clang-format configuration for Zeek. This configuration requires +# at least clang-format 12.0.1 to format correctly. +# +# The easiest way to run this from the command-line is using the +# python script in auxil/run-clang-format: +# +# python3 auxil/run-clang-format/run-clang-format.py --clang-format-executable /path/to/clang-format -r src -i + +Language: Cpp +Standard: c++17 + +BreakBeforeBraces: Whitesmiths + +# BraceWrapping: +# AfterCaseLabel: true +# AfterClass: false +# AfterControlStatement: Always +# AfterEnum: false +# AfterFunction: true +# AfterNamespace: false +# AfterStruct: false +# AfterUnion: false +# AfterExternBlock: false +# BeforeCatch: true +# BeforeElse: true +# BeforeWhile: false +# IndentBraces: true +# SplitEmptyFunction: false +# SplitEmptyRecord: false +# SplitEmptyNamespace: false + +AccessModifierOffset: -4 +AlignAfterOpenBracket: true +AlignTrailingComments: false +AllowShortBlocksOnASingleLine: Empty +AllowShortEnumsOnASingleLine: true +AllowShortFunctionsOnASingleLine: Inline +AllowShortIfStatementsOnASingleLine: false +AllowShortLambdasOnASingleLine: Empty +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterReturnType: None +BinPackArguments: true +BinPackParameters: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +ColumnLimit: 100 +ConstructorInitializerAllOnOneLineOrOnePerLine: false +FixNamespaceComments: false +IndentCaseLabels: true +IndentCaseBlocks: true +IndentExternBlock: NoIndent +IndentPPDirectives: None +IndentWidth: 4 +NamespaceIndentation: None +PointerAlignment: Left +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyBlock: true +SpaceInEmptyParentheses: false +SpacesInAngles: false +SpacesInConditionalStatement: true +SpacesInContainerLiterals: false +SpacesInParentheses: false +TabWidth: 4 +UseTab: AlignWithSpaces + +IncludeBlocks: Regroup + +# Include categories go like this: +# 0: reserved, since this automatically is the primary header for any .cc file +# 1: zeek-config.h +# 2: any c-style header +# 3: any c++-style header +# 4: any header that starts with "zeek/" +# 5: everything else, which should catch any of the auto-generated code from the +# build directory as well +# +# Sections 0-1 and 2-3 get group together in their respective blocks +IncludeCategories: + - Regex: '^"zeek-config\.h"' + Priority: 0 + SortPriority: 1 + - Regex: '^<[[:print:]]+\.(h|hh)>' + Priority: 2 + SortPriority: 2 + - Regex: '^<[[:print:]]+>' + Priority: 2 + SortPriority: 3 + - Regex: '^"zeek/' + Priority: 4 + - Regex: '.*' + Priority: 5 \ No newline at end of file diff --git a/.clang-format-ignore b/.clang-format-ignore new file mode 100644 index 0000000000..50f6bce6a5 --- /dev/null +++ b/.clang-format-ignore @@ -0,0 +1,17 @@ +# Ignore everything 3rdparty +src/3rdparty/* + +# These are files that are technically sourced from other places but aren't in 3rdparty +# and shouldn't be reformatted. +src/ConvertUTF.* +src/bro_inet_ntop.* +src/bsd-getopt-long.* +src/in_cksum.* +src/nb_dns.* +src/modp_numtoa.* +src/patricia.* +src/strsep.c +src/setsignal.c + +# These files are generated code +src/DebugCmdInfoConstants.* \ No newline at end of file diff --git a/.gitmodules b/.gitmodules index 4318212fe0..e3b149e9f9 100644 --- a/.gitmodules +++ b/.gitmodules @@ -49,3 +49,6 @@ [submodule "auxil/zeek-client"] path = auxil/zeek-client url = https://github.com/zeek/zeek-client +[submodule "auxil/run-clang-format"] + path = auxil/run-clang-format + url = https://github.com/Sarcasm/run-clang-format diff --git a/auxil/run-clang-format b/auxil/run-clang-format new file mode 160000 index 0000000000..39081c9c42 --- /dev/null +++ b/auxil/run-clang-format @@ -0,0 +1 @@ +Subproject commit 39081c9c42768ab5e8321127a7494ad1647c6a2f diff --git a/ci/run-clang-format.sh b/ci/run-clang-format.sh new file mode 100755 index 0000000000..3bd6d5d898 --- /dev/null +++ b/ci/run-clang-format.sh @@ -0,0 +1,65 @@ +#! /bin/sh +# +# Copyright (c) 2020 by the Zeek Project. See LICENSE for details. + +base=$(git rev-parse --show-toplevel) +fix=0 +pre_commit_hook=0 + +# Directories to run on by default. When changing, adapt .pre-commit-config.yam +# as well. +files="src" + +error() { + test "${pre_commit_hook}" = 0 && echo "$@" >&2 && exit 1 + exit 0 +} + +if [ $# != 0 ]; then + case "$1" in + --fixit) + shift + fix=1 + ;; + + --pre-commit-hook) + shift + fix=1 + pre_commit_hook=1 + ;; + + -*) + echo "usage: $(basename $0) [--fixit | --pre-commit-hook] []" + exit 1 + esac +fi + +test $# != 0 && files="$@" + +if [ -z "${CLANG_FORMAT}" ]; then + CLANG_FORMAT=$(which clang-format 2>/dev/null) +fi + +if [ -z "${CLANG_FORMAT}" -o ! -x "${CLANG_FORMAT}" ]; then + error "Cannot find clang-format. If not in PATH, set CLANG_FORMAT." +fi + +if ! (cd / && ${CLANG_FORMAT} -dump-config | grep -q SpacesInConditionalStatement); then + error "${CLANG_FORMAT} does not support SpacesInConditionalStatement. Install custom version and put it into PATH, or point CLANG_FORMAT to it." +fi + +if [ ! -e .clang-format ]; then + error "Must execute in top-level directory." +fi + +cmd="${base}/auxil/run-clang-format/run-clang-format.py -r --clang-format-executable ${CLANG_FORMAT} --exclude '*/3rdparty/*' ${files}" +tmp=/tmp/$(basename $0).$$.tmp +trap "rm -f ${tmp}" EXIT +eval "${cmd}" >"${tmp}" + +if [ "${fix}" = 1 ]; then + test -s "${tmp}" && cat "${tmp}" | git apply -p0 + true +else + cat "${tmp}" +fi diff --git a/src/Func.cc b/src/Func.cc index f06108c983..93bd57c12d 100644 --- a/src/Func.cc +++ b/src/Func.cc @@ -55,6 +55,9 @@ #include "zeek/iosource/PktSrc.h" #include "zeek/iosource/PktDumper.h" +// Ignore clang-format's reordering of include files here so that it doesn't +// break what symbols are available when, which keeps the build from breaking. +// clang-format off #include "zeek.bif.func_h" #include "stats.bif.func_h" #include "reporter.bif.func_h" @@ -72,6 +75,7 @@ #include "supervisor.bif.func_def" #include "packet_analysis.bif.func_def" #include "CPP-load.bif.func_def" +// clang-format on extern RETSIGTYPE sig_handler(int signo);