Skip to content

Commit 8692540

Browse files
authored
ClangFormat 13: add config and snippets (#108)
Add configuration file for ClangFormat 13 as well as C++ snippets highlighting new features. * prepare settings for future C++ 20 idioms like `requires` and `concepts` * add new clang format 13 keys without changing the previous behavior Additions compared to file clang-format-12: * AlignArrayOfStructures: None * BreakBeforeConceptDeclarations: true * EmptyLineBeforeAccessModifier: Leave * EmptyLineAfterAccessModifier: Leave * IndentAccessModifiers: false * IndentPPDirectives: None * IndentRequires: false * LambdaBodyIndentation: Signature * PPIndentWidth: -1
1 parent fc96a15 commit 8692540

File tree

8 files changed

+234
-0
lines changed

8 files changed

+234
-0
lines changed

cpp/clang-format-13

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
AccessModifierOffset: -2
3+
AlignAfterOpenBracket: Align
4+
AlignArrayOfStructures: None
5+
AlignConsecutiveAssignments: false
6+
AlignConsecutiveDeclarations: false
7+
AlignConsecutiveMacros: true
8+
AlignEscapedNewlinesLeft: true
9+
AlignOperands: true
10+
AlignTrailingComments: true
11+
AllowAllParametersOfDeclarationOnNextLine: false
12+
AllowShortBlocksOnASingleLine: false
13+
AllowShortCaseLabelsOnASingleLine: false
14+
AllowShortFunctionsOnASingleLine: Empty
15+
AllowShortIfStatementsOnASingleLine: false
16+
AllowShortLoopsOnASingleLine: false
17+
AlwaysBreakBeforeMultilineStrings: true
18+
AlwaysBreakTemplateDeclarations: true
19+
BasedOnStyle: WebKit
20+
BinPackArguments: false
21+
BinPackParameters: false
22+
BraceWrapping:
23+
AfterClass: false
24+
AfterControlStatement: false
25+
AfterEnum: false
26+
AfterExternBlock: false
27+
AfterFunction: false
28+
AfterNamespace: false
29+
AfterStruct: false
30+
AfterUnion: false
31+
BeforeCatch: false
32+
BeforeElse: false
33+
BreakBeforeBraces: Custom
34+
BreakBeforeBinaryOperators: false
35+
BreakBeforeConceptDeclarations: true
36+
BreakBeforeTernaryOperators: true
37+
BreakConstructorInitializersBeforeComma: true
38+
BreakStringLiterals: true
39+
ColumnLimit: 100
40+
CommentPragmas: '^ IWYU pragma:'
41+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
42+
ConstructorInitializerIndentWidth: 4
43+
ContinuationIndentWidth: 4
44+
Cpp11BracedListStyle: true
45+
DerivePointerAlignment: false
46+
DerivePointerBinding: true
47+
EmptyLineBeforeAccessModifier: Leave
48+
EmptyLineAfterAccessModifier: Leave
49+
ExperimentalAutoDetectBinPacking: false
50+
FixNamespaceComments: true
51+
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
52+
IncludeCategories:
53+
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
54+
Priority: 2
55+
- Regex: '^(<|"(gtest|isl|json)/)'
56+
Priority: 3
57+
- Regex: '.*'
58+
Priority: 1
59+
IncludeIsMainRegex: '$'
60+
IndentAccessModifiers: false
61+
IndentCaseLabels: false
62+
IndentPPDirectives: None
63+
IndentRequires: false
64+
IndentWidth: 4
65+
IndentWrappedFunctionNames: false
66+
KeepEmptyLinesAtTheStartOfBlocks: false
67+
LambdaBodyIndentation: Signature
68+
Language: Cpp
69+
MaxEmptyLinesToKeep: 2
70+
NamespaceIndentation: None
71+
PenaltyBreakAssignment: 40
72+
PenaltyBreakBeforeFirstCallParameter: 100
73+
PenaltyBreakComment: 60
74+
PenaltyBreakFirstLessLess: 120
75+
PenaltyBreakString: 1000
76+
PenaltyExcessCharacter: 1000000
77+
PenaltyReturnTypeOnItsOwnLine: 200
78+
PointerAlignment: Left
79+
PointerBindsToType: true
80+
PPIndentWidth: -1
81+
ReflowComments: true
82+
SortIncludes: true
83+
SpaceAfterCStyleCast: true
84+
SpaceAfterTemplateKeyword: true
85+
SpaceBeforeAssignmentOperators: true
86+
SpaceBeforeCpp11BracedList: false
87+
SpaceBeforeCtorInitializerColon: false
88+
SpaceBeforeInheritanceColon: false
89+
SpaceBeforeParens: ControlStatements
90+
SpaceBeforeRangeBasedForLoopColon: false
91+
SpaceInEmptyBlock: false
92+
SpaceInEmptyParentheses: false
93+
SpacesBeforeTrailingComments: 2
94+
SpacesInAngles: false # '< ' style
95+
SpacesInContainerLiterals: false
96+
SpacesInCStyleCastParentheses: false
97+
SpacesInParentheses: false # '(' style
98+
SpacesInSquareBrackets: false
99+
Standard: c++14
100+
TabWidth: 4
101+
UseTab: Never
102+
...

cpp/formatting/README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ thisIsAVeryLongMethodName(thisIsTheFirstArgument,
4646
### Clang-Format configuration
4747
* `AlignAfterOpenBracket: Align`
4848

49+
## Do not align fields in array initialization
50+
51+
### Example
52+
```cpp
53+
struct test demo[] = {{56, 23, "hello"}, {-1, 93463, "world"}, {7, 5, "!!"}};
54+
55+
```
56+
### Clang-Format configuration
57+
* `AlignArrayOfStructures: None`
58+
4959
## Do not align consecutive assignments
5060
5161
### Example
@@ -350,6 +360,19 @@ bool value =
350360
### Clang-Format configuration
351361
* `BreakBeforeBinaryOperators: False`
352362

363+
## New line before C++20 `concept` directive
364+
365+
### Example
366+
```cpp
367+
template <typename T>
368+
concept Hashable = requires(T a) {
369+
{ std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;
370+
};
371+
372+
```
373+
### Clang-Format configuration
374+
* `BreakBeforeConceptDeclarations: True`
375+
353376
## break before long ternary operators
354377

355378
### Example
@@ -373,6 +396,28 @@ f(MyMap[{composite, key}]) new int[3]{1, 2, 3};
373396
### Clang-Format configuration
374397
* `Cpp11BracedListStyle: True`
375398
399+
## No line break restriction after access modifiers
400+
401+
### Example
402+
```cpp
403+
struct foo {
404+
private:
405+
int i;
406+
407+
protected:
408+
int j;
409+
/* comment */
410+
public:
411+
foo() {}
412+
413+
private:
414+
protected:
415+
};
416+
417+
```
418+
### Clang-Format configuration
419+
* `EmptyLineBeforeAccessModifier: Leave`
420+
376421
## Recall namespace when closing it
377422

378423
### Example
@@ -383,6 +428,34 @@ foo();
383428

384429
```
385430
431+
## Do no indent preprocesor directives
432+
433+
### Example
434+
```cpp
435+
#if FOO
436+
#if BAR
437+
#include <foo>
438+
#endif // BAR
439+
#endif // FOO
440+
441+
```
442+
### Clang-Format configuration
443+
* `IndentPPDirectives: None`
444+
445+
## line break before C++20 `requires` directive
446+
447+
### Example
448+
```cpp
449+
template <typename It>
450+
requires Iterator<It>
451+
void sort(It begin, It end) {
452+
//....
453+
}
454+
455+
```
456+
### Clang-Format configuration
457+
* `IndentRequires: False`
458+
386459
## Use 4 columns for indentation
387460
388461
### Example
@@ -398,6 +471,19 @@ void f() {
398471
### Clang-Format configuration
399472
* `IndentWidth: 4`
400473

474+
## Indent lambda body based on the signature of the lambda
475+
476+
### Example
477+
```cpp
478+
callingSomeLongLongLongLongLongLongLongLongLongLongMethod(
479+
[](SomeReallyLongLambdaSignatureArgument foo, SomeReallyLongLambdaSignatureArgument bar) {
480+
return;
481+
});
482+
483+
```
484+
### Clang-Format configuration
485+
* `LambdaBodyIndentation: Signature`
486+
401487
## Do not indent when entering a *namespace*
402488
403489
### Example
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Do not align fields in array initialization
2+
3+
struct test demo[] = {{56, 23, "hello"}, {-1, 93463, "world"}, {7, 5, "!!"}};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// New line before C++20 `concept` directive
2+
3+
template <typename T>
4+
concept Hashable = requires(T a) {
5+
{ std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;
6+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// No line break restriction after access modifiers
2+
3+
struct foo {
4+
private:
5+
int i;
6+
7+
protected:
8+
int j;
9+
/* comment */
10+
public:
11+
foo() {}
12+
13+
private:
14+
protected:
15+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Do no indent preprocesor directives
2+
3+
#if FOO
4+
#if BAR
5+
#include <foo>
6+
#endif // BAR
7+
#endif // FOO
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// line break before C++20 `requires` directive
2+
3+
template <typename It>
4+
requires Iterator<It>
5+
// clang-format off
6+
void sort(It begin, It end) {
7+
//....
8+
}
9+
// clang-format on
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Indent lambda body based on the signature of the lambda
2+
3+
callingSomeLongLongLongLongLongLongLongLongLongLongMethod(
4+
[](SomeReallyLongLambdaSignatureArgument foo, SomeReallyLongLambdaSignatureArgument bar) {
5+
return;
6+
});

0 commit comments

Comments
 (0)