Skip to content

Instantly share code, notes, and snippets.

@vampy
Last active November 5, 2016 13:02
Show Gist options
  • Save vampy/e4a2a616f2b895af5a5b57dae9dac69b to your computer and use it in GitHub Desktop.
Save vampy/e4a2a616f2b895af5a5b57dae9dac69b to your computer and use it in GitHub Desktop.
Clang format scrips files I use
# See https://clangformat.com/
# http://clang-format.me/
# http://clang.llvm.org/docs/ClangFormatStyleOptions.html
---
Language: Cpp
# The extra indent or outdent of access modifiers, e.g. public:
AccessModifierOffset: -4
# If true, horizontally aligns arguments after an open bracket.
AlignAfterOpenBracket: true
AlignEscapedNewlinesLeft: true
# If true, horizontally align operands of binary and ternary expressions.
AlignOperands: true
AlignTrailingComments: true
# If true, aligns consecutive assignments.
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AlwaysBreakAfterDefinitionReturnType: false
AlwaysBreakTemplateDeclarations: true
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakAfterReturnType: None
BreakBeforeBinaryOperators: All
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakBeforeBraces: Allman
# If false, a function call’s or function definition’s parameters will either all be on the same line or will have one line each.
BinPackParameters: false
BinPackArguments: false
# Pointer and reference alignment style.
PointerAlignment: Left
# If the constructor initializers don’t fit on a line, put each initializer on its own line.
ConstructorInitializerAllOnOneLineOrOnePerLine: true
DerivePointerAlignment: false
ExperimentalAutoDetectBinPacking: false
IndentCaseLabels: false
IndentWrappedFunctionNames: false
IndentFunctionDeclarationAfterType: false
MaxEmptyLinesToKeep: 1
KeepEmptyLinesAtTheStartOfBlocks: true
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
# https://stackoverflow.com/questions/26635370/in-clang-format-what-do-the-penalties-do
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakString: 1000
PenaltyBreakFirstLessLess: 120
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
Cpp11BracedListStyle: true
# Indent only in inner namespaces (nested in other namespaces).
NamespaceIndentation: Inner
# The number of spaces before trailing line comments (// - comments).
SpacesBeforeTrailingComments: 1
ColumnLimit: 120
Standard: Cpp11
IndentWidth: 4
ConstructorInitializerIndentWidth: 4
TabWidth: 4
ContinuationIndentWidth: 4
UseTab: Never
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpacesInAngles: false
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SpaceAfterCStyleCast: false
SpacesInContainerLiterals: true
SpaceBeforeAssignmentOperators: true
CommentPragmas: '^ IWYU pragma:'
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
IncludeCategories:
- Regex: '^<.*\.h>'
Priority: 1
- Regex: '^<.*'
Priority: 2
- Regex: '.*'
Priority: 3
SpaceBeforeParens: ControlStatements
DisableFormat: false
...
#!/bin/bash
# safe https://sipb.mit.edu/doc/safe-shell/
set -euf -o pipefail
function usage()
{
echo "Usage: $0 <directory>|--help"
}
# Try different clang-format commands...
if type "clang-format" > /dev/null 2>&1; then
CLANG_FORMAT="clang-format"
elif type "clang-format-3.8" > /dev/null 2>&1; then
CLANG_FORMAT="clang-format-3.8"
elif type "clang-format-3.7" > /dev/null 2>&1; then
CLANG_FORMAT="clang-format-3.7"
else
CLANG_FORMAT=""
fi
if [ -z "$CLANG_FORMAT" ]; then
echo "No clang-format command can be found"
exit 1
fi
echo "Using: $("$CLANG_FORMAT" --version)"
# no arguments
if [ $# -eq 0 ]; then
usage
exit 1
fi
dir="$1"
# display help
if [ "$dir" = "--help" ]; then
usage
exit 0
fi
# path does not exist
if ! [ -d "$dir" ]; then
if [ -f "$dir" ]; then
echo "Can not format a file, use: $CLANG_FORMAT -style=file -i $dir"
else
echo "Directory $1 does not exist"
fi
usage
exit 1
fi
# format
read -r -p "Are you sure? [y/N] " response
response=${response,,} # tolower
if [[ $response =~ ^(yes|y)$ ]]; then
echo "Formating..."
find "$dir" -regex ".*\.\(hpp\|cpp\|c\|h\)" -print0 | xargs -0 "$CLANG_FORMAT" -style=file -i
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment