Clang-format, automatically format source code: Difference between revisions
(Created page with "clang-format allows you to use a simple text file to define how you want your source to look like and then format it automatically. The configuration file consists of choices,...") |
No edit summary |
||
Line 1: | Line 1: | ||
== Info == |
|||
clang-format allows you to use a simple text file to define how you want your source to look like and then format it automatically. The configuration file consists of choices, you can only select between prepared variants for prepared cases. |
clang-format allows you to use a simple text file to define how you want your source to look like and then format it automatically. The configuration file consists of choices, you can only select between prepared variants for prepared cases. |
||
== URL == |
|||
You can get a start here: https://clang.llvm.org/ |
You can get a start here: https://clang.llvm.org/ |
||
== Debian == |
|||
For Debian, you are in luck: |
For Debian, you are in luck: |
||
sudo apt-get install clang-format |
sudo apt-get install clang-format |
||
== file .clang-format == |
|||
Then you create ONE .clang-format file which you put in the top most directory of your c++ projects. Once! |
Then you create ONE .clang-format file which you put in the top most directory of your c++ projects. Once! |
||
Line 129: | Line 136: | ||
... |
... |
||
</pre> |
</pre> |
||
== Makefile == |
|||
Now you add to your Makefile something like this: |
Now you add to your Makefile something like this: |
||
Line 144: | Line 153: | ||
And then you go to your project, in the example above you would go to work/c++/project-1 and there you call clang-format: |
And then you go to your project, in the example above you would go to work/c++/project-1 and there you call clang-format: |
||
make cf |
make cf |
||
== Misc == |
|||
Make sure to not mix real code changes and clang-format changes in your history or you will hate yourself. Or others will hate you. |
Make sure to not mix real code changes and clang-format changes in your history or you will hate yourself. Or others will hate you. |
Latest revision as of 16:08, 30 November 2020
Info
clang-format allows you to use a simple text file to define how you want your source to look like and then format it automatically. The configuration file consists of choices, you can only select between prepared variants for prepared cases.
URL
You can get a start here: https://clang.llvm.org/
Debian
For Debian, you are in luck:
sudo apt-get install clang-format
file .clang-format
Then you create ONE .clang-format file which you put in the top most directory of your c++ projects. Once! If you have "work/c++/project-1" and work/c++/project-2" then you put ".clang-format" into "work/c++/".
.clang-format:
--- Language: Cpp # BasedOnStyle: LLVM AccessModifierOffset: 2 AlignAfterOpenBracket: Align AlignConsecutiveAssignments: true AlignConsecutiveDeclarations: true AlignEscapedNewlines: Left AlignOperands: true AlignTrailingComments: true AllowAllParametersOfDeclarationOnNextLine: true AllowShortBlocksOnASingleLine: true AllowShortCaseLabelsOnASingleLine: true AllowShortFunctionsOnASingleLine: InlineOnly AllowShortIfStatementsOnASingleLine: false AllowShortLoopsOnASingleLine: false AlwaysBreakAfterDefinitionReturnType: None AlwaysBreakAfterReturnType: None AlwaysBreakBeforeMultilineStrings: false AlwaysBreakTemplateDeclarations: MultiLine BinPackArguments: true BinPackParameters: true BraceWrapping: AfterClass: true AfterControlStatement: true AfterEnum: false AfterFunction: true AfterNamespace: true AfterObjCDeclaration: true AfterStruct: true AfterUnion: true AfterExternBlock: true BeforeCatch: true BeforeElse: true IndentBraces: true SplitEmptyFunction: true SplitEmptyRecord: true SplitEmptyNamespace: true BreakBeforeBinaryOperators: NonAssignment BreakBeforeBraces: Allman BreakBeforeInheritanceComma: true BreakInheritanceList: AfterColon BreakBeforeTernaryOperators: true BreakConstructorInitializersBeforeComma: true BreakConstructorInitializers: BeforeComma BreakAfterJavaFieldAnnotations: false BreakStringLiterals: true ColumnLimit: 110 CommentPragmas: '^ IWYU pragma:' CompactNamespaces: false ConstructorInitializerAllOnOneLineOrOnePerLine: false ConstructorInitializerIndentWidth: 4 ContinuationIndentWidth: 4 Cpp11BracedListStyle: true DerivePointerAlignment: false DisableFormat: false FixNamespaceComments: false ForEachMacros: - foreach - Q_FOREACH - BOOST_FOREACH IncludeBlocks: Preserve IncludeCategories: - Regex: '^"(llvm|llvm-c|clang|clang-c)/' Priority: 2 - Regex: '^(<|"(gtest|gmock|isl|json)/)' Priority: 3 - Regex: '.*' Priority: 1 IncludeIsMainRegex: '(Test)?$' IndentCaseLabels: false # with newer clang-format: IndentPPDirectives: BeforeHash IndentPPDirectives: None IndentWidth: 4 IndentWrappedFunctionNames: true JavaScriptQuotes: Leave JavaScriptWrapImports: true KeepEmptyLinesAtTheStartOfBlocks: false MacroBlockBegin: '' MacroBlockEnd: '' MaxEmptyLinesToKeep: 1 NamespaceIndentation: All ObjCBinPackProtocolList: Auto ObjCBlockIndentWidth: 4 ObjCSpaceAfterProperty: false ObjCSpaceBeforeProtocolList: true PenaltyBreakAssignment: 2 PenaltyBreakBeforeFirstCallParameter: 19 PenaltyBreakComment: 300 PenaltyBreakFirstLessLess: 120 PenaltyBreakString: 1000 PenaltyBreakTemplateDeclaration: 10 PenaltyExcessCharacter: 1000000 PenaltyReturnTypeOnItsOwnLine: 60 PointerAlignment: Left ReflowComments: true SortIncludes: true SortUsingDeclarations: true SpaceAfterCStyleCast: true SpaceAfterTemplateKeyword: true SpaceBeforeAssignmentOperators: true SpaceBeforeCpp11BracedList: true SpaceBeforeCtorInitializerColon: true SpaceBeforeInheritanceColon: true SpaceBeforeParens: ControlStatements SpaceBeforeRangeBasedForLoopColon: true SpaceInEmptyParentheses: false SpacesBeforeTrailingComments: 1 SpacesInAngles: false SpacesInContainerLiterals: false SpacesInCStyleCastParentheses: false SpacesInParentheses: false SpacesInSquareBrackets: false Standard: Cpp11 TabWidth: 4 UseTab: Never ...
Makefile
Now you add to your Makefile something like this:
# clang-format .PHONY: cf cf: @clang-format -style=file -i include/*.h @clang-format -style=file -i src/*.cpp
Depending on your project you might have only one src directory (and no include directory) or you might even have no src directory).
And then you go to your project, in the example above you would go to work/c++/project-1 and there you call clang-format:
make cf
Misc
Make sure to not mix real code changes and clang-format changes in your history or you will hate yourself. Or others will hate you.