Skip to content

Commit c539260

Browse files
authored
Clang format 14 (#154)
* Support Clang Format 14 New configuration keys compared to Clang Format 13: * PackConstructorInitializers: Never Do not change previous indentation * QualifierAlignment: Left qualifiers like `const`, `volatile`, `inline` are not left aligned. This can change the previous indentation * RemoveBracesLLVM: false Never remove braces for control statements. Do not change previous indentation * SeparateDefinitionBlocks: Always Adds an empty line between declaration. This can change the previous indentation * Use bin/format utility to format the snippets cpp/formatting/Makefile now relies on bin/format to format the snippets. It does not explicitely call clang-format
1 parent 8692540 commit c539260

12 files changed

+238
-18
lines changed

.bbp-project.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
tools:
2+
ClangFormat:
3+
enable: True
4+
version: ~=14.0
25
CMakeFormat:
36
enable: True
47
Flake8:

.ci/check_code_formatting.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
set -euo pipefail
44

55
VENV=build/venv-code-formatting
6-
CLANG_FORMAT_VERSION=13.0.0
76

87
if [[ ! -d $VENV ]]; then
98
python3 -mvenv "$VENV"
109
"$VENV/bin/pip" install \
11-
clang-format==$CLANG_FORMAT_VERSION \
1210
jinja2 \
1311
pyyaml
1412
fi

cpp/clang-format-14

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
PackConstructorInitializers: Never
82+
QualifierAlignment: Left
83+
ReflowComments: true
84+
RemoveBracesLLVM: false
85+
SeparateDefinitionBlocks: Always
86+
SortIncludes: true
87+
SpaceAfterCStyleCast: true
88+
SpaceAfterTemplateKeyword: true
89+
SpaceBeforeAssignmentOperators: true
90+
SpaceBeforeCpp11BracedList: false
91+
SpaceBeforeCtorInitializerColon: false
92+
SpaceBeforeInheritanceColon: false
93+
SpaceBeforeParens: ControlStatements
94+
SpaceBeforeRangeBasedForLoopColon: false
95+
SpaceInEmptyBlock: false
96+
SpaceInEmptyParentheses: false
97+
SpacesBeforeTrailingComments: 2
98+
SpacesInAngles: false # '< ' style
99+
SpacesInContainerLiterals: false
100+
SpacesInCStyleCastParentheses: false
101+
SpacesInParentheses: false # '(' style
102+
SpacesInSquareBrackets: false
103+
Standard: c++14
104+
TabWidth: 4
105+
UseTab: Never
106+
...

cpp/formatting/Makefile

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@
99
# distclean: clean + remove intermediate files
1010
#
1111

12-
CLANG_FORMAT ?= clang-format
1312
PANDOC ?= pandoc
1413

1514
SNIPPETS = $(wildcard snippets/*.cpp)
16-
CLANG_FORMAT_MAJOR = $(shell $(CLANG_FORMAT) --version | sed -e "s/(.*)//" | awk '{print $$NF}' | awk -v FS=. '{print $$1}')
17-
CLANG_FORMAT_CONFIG = $(shell for i in `seq $(CLANG_FORMAT_MAJOR) -1 7`; do if [ -f ../clang-format-$$i ] ; then echo ../clang-format-$$i ; break; fi; done)
1815

1916
all: README.md
2017

@@ -30,18 +27,8 @@ distclean: clean
3027
README.html: github-pandoc.css
3128

3229
# README.md
33-
README.md: README.md.jinja formatting.py .clang-format $(SNIPPETS)
34-
@# format snippets that triggered rebuild of target
35-
$(foreach snippet,$(filter snippets/%,$?),$(CLANG_FORMAT) -style=file -i $(snippet);)
36-
30+
README.md: README.md.jinja formatting.py $(SNIPPETS)
31+
../../bin/format --lang c++
3732
./formatting.py $< $@
3833

39-
# update all snippets when .clang-format is changed
40-
$(SNIPPETS): .clang-format
41-
$(CLANG_FORMAT) -style=file -i $@
42-
touch $@
43-
44-
.clang-format: $(CLANG_FORMAT_CONFIG)
45-
cp $? $@
46-
4734
.SUFFIXES: .md .html

cpp/formatting/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ union foo {
324324
```cpp
325325
try {
326326
foo();
327+
327328
} catch () {
328329
}
329330

@@ -452,6 +453,7 @@ void sort(It begin, It end) {
452453
//....
453454
}
454455

456+
455457
```
456458
### Clang-Format configuration
457459
* `IndentRequires: False`
@@ -490,6 +492,7 @@ callingSomeLongLongLongLongLongLongLongLongLongLongMethod(
490492
```cpp
491493
namespace out {
492494
int i;
495+
493496
namespace in {
494497
int i;
495498
}
@@ -499,6 +502,19 @@ int i;
499502
### Clang-Format configuration
500503
* `NamespaceIndentation: None`
501504

505+
## The pack constructor initializers style to use.
506+
507+
### Example
508+
```cpp
509+
Constructor()
510+
: aaaaaaaaaaaaaaaaaaaa()
511+
, bbbbbbbbbbbbbbbbbbbb()
512+
, ddddddddddddd()
513+
514+
```
515+
### Clang-Format configuration
516+
* `PackConstructorInitializers: Never`
517+
502518
## pointer and reference are part of the type
503519

504520
### Example
@@ -510,6 +526,63 @@ int& b = getB();
510526
### Clang-Format configuration
511527
* `PointerAlignment: Left`
512528

529+
## Different ways to arrange specifiers and qualifiers (e.g. const/volatile).
530+
531+
### Example
532+
```cpp
533+
const int a;
534+
const int* a;
535+
536+
```
537+
### Clang-Format configuration
538+
* `QualifierAlignment: Left`
539+
540+
## Specifies the use of empty lines to separate definition blocks, including classes, structs,
541+
enums, and functions.
542+
543+
### Example
544+
```cpp
545+
#include <cstring>
546+
547+
struct Foo {
548+
int a, b, c;
549+
};
550+
551+
namespace Ns {
552+
class Bar {
553+
public:
554+
struct Foobar {
555+
int a;
556+
int b;
557+
};
558+
private:
559+
int t;
560+
561+
int method1() {
562+
// ...
563+
}
564+
565+
enum List { ITEM1, ITEM2 };
566+
567+
template <typename T>
568+
int method2(T x) {
569+
// ...
570+
}
571+
572+
int i, j, k;
573+
574+
int method3(int par) {
575+
// ...
576+
}
577+
};
578+
579+
class C {};
580+
} // namespace Ns
581+
582+
```
583+
### Clang-Format configuration
584+
* `SeparateDefinitionBlocks: Always`
585+
513586
## Sort includes in different sections
514587
* System headers
515588
* Dependencies

cpp/formatting/formatting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def get_cf_data(line):
8282

8383

8484
def load_conventions(path):
85-
with open(".clang-format") as istr:
85+
with open("../../.clang-format") as istr:
8686
clang_format = yaml.safe_load(istr)
8787
assert osp.isdir(path)
8888
for file in sorted(glob.glob(path + os.sep + "*.cpp")):

cpp/formatting/snippets/BraceWrapping.BeforeCatch.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
try {
44
foo();
5+
56
} catch () {
67
}

cpp/formatting/snippets/IndentRequires.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ requires Iterator<It>
66
void sort(It begin, It end) {
77
//....
88
}
9+
910
// clang-format on

cpp/formatting/snippets/NamespaceIndentation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace out {
44
int i;
5+
56
namespace in {
67
int i;
78
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// The pack constructor initializers style to use.
2+
3+
Constructor()
4+
: aaaaaaaaaaaaaaaaaaaa()
5+
, bbbbbbbbbbbbbbbbbbbb()
6+
, ddddddddddddd()

0 commit comments

Comments
 (0)