Skip to content

Instantly share code, notes, and snippets.

@fzls
Last active August 14, 2017 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fzls/ffecb922a433194135fa501ffc8ead2f to your computer and use it in GitHub Desktop.
Save fzls/ffecb922a433194135fa501ffc8ead2f to your computer and use it in GitHub Desktop.
format directories with clang format and astyle
# only fix pointer and reference position after using clang-format
--align-pointer=name
--align-reference=name
--mode=c
--suffix=none
--quiet
---
Language: Cpp
# Use my own style
# BasedOnStyle: Google
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Allman
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: AfterColon
BreakAfterJavaFieldAnnotations: true
BreakStringLiterals: true
# !!!!修改最大行宽在这里!!!
ColumnLimit: 180
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeCategories:
- Regex: 'stdafx.h|.*Pch.h'
Priority: -1
# - Regex: '^"(llvm|llvm-c|clang|clang-c)/'
# Priority: 2
# - Regex: '^(<|"(gtest|gmock|isl|json)/)'
# Priority: 3
# - Regex: '.*'
# Priority: 1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: true
IndentWidth: 4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: 'DECLARE_STRING_MAP_BEGIN|DECLARE_STRING_BEGIN'
MacroBlockEnd: 'DECLARE_STRING_MAP_END|DECLARE_STRING_END'
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
# TODO: what does following penalties do?
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 4
UseTab: Never
# ...
#!/bin/bash
echo "========================================================================="
echo "start update to latest version"
cp "/d/zju同步盘/ZJU CLOUDV2/个人文件/clang-format/.astylerc" ./.astylerc -f
echo " > astyle settings updated"
cp "/d/zju同步盘/ZJU CLOUDV2/个人文件/clang-format/.clang-format" ./.clang-format -f
echo " > clang format settings updated"
cp "/d/zju同步盘/ZJU CLOUDV2/个人文件/clang-format/format.sh" ./format.sh -f
echo " > format shell script updated"
echo "Update finished"
echo "========================================================================="
sh ./format.sh
read -n 1 -s -r -p "Press any key to continue"
#!/bin/bash
# This script reformats source files using the clang-format utility.
# Set the list of source directories on the "for" line below.
#
# The file .clang-format in this directory specifies the formatting parameters.
#
# Files are changed in-place, so make sure you don't have anything open in an
# editor, and you may want to commit before formatting in case of awryness.
#
# Note that clang-format is not included with OS X or Xcode; you must
# install it yourself. There are multiple ways to do this:
#
# - If you use Xcode, install the ClangFormat-Xcode plugin. See instructions at
# <https://github.com/travisjeffery/ClangFormat-Xcode/>.
# After installation, the executable can be found at
# $HOME/Library/Application Support/Alcatraz/Plug-ins/ClangFormat/bin/clang-format.
#
# - Download an LLVM release from <http://llvm.org/releases/download.html>.
# For OS X, use the pre-built binaries for "Darwin".
#
# - Build the LLVM tools from source. See the documentation at <http://llvm.org>.
# Change this if your clang-format executable is somewhere else
ClangFormatPath="/d/zju同步盘/ZJU CLOUDV2/个人文件/clang-format/clang-format.exe"
"/d/zju同步盘/ZJU CLOUDV2/个人文件/clang-format/clang-format.exe" -version
echo start processing directory $(pwd)
echo With files as follows:
echo -----------------------------------
# find . -not \( -path "./.localhistory/*" -prune \) -iname *.cpp -or -iname *.h | tee /dev/tty | xargs "/d/zju同步盘/ZJU CLOUDV2/个人文件/clang-format/clang-format.exe" -i
TotalCount=`find . -not \( -path "./.localhistory/*" -prune \) -iname *.cpp -or -iname *.h | wc -l`
echo "Total file count is : $TotalCount"
i=0
find . -not \( -path "./.localhistory/*" -prune \) -iname *.cpp -or -iname *.h | while read file_path; do
((++i))
printf "[ $i/$TotalCount ] : Processing file '$file_path'"
"/d/zju同步盘/ZJU CLOUDV2/个人文件/clang-format/clang-format.exe" -i $file_path
printf "..... Clang-format Done"
"/d/zju同步盘/ZJU CLOUDV2/个人文件/clang-format/AStyle.exe" --options=".astylerc" $file_path
printf "..... Astyle Done\n"
done
echo -----------------------------------
echo All finished
@fzls
Copy link
Author

fzls commented Aug 14, 2017

install

  1. just copy _update_format_settings_and_format.sh to project root directories

config

  1. change /d/zju同步盘/ZJU CLOUDV2/个人文件/clang-format/ to the directory contains these files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment