Skip to content

Commit 774c2e3

Browse files
author
Remi Achard
committed
clang-format base setup
1 parent d9eb50c commit 774c2e3

6 files changed

Lines changed: 383 additions & 0 deletions

File tree

.clang-format

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
---
2+
Language: Cpp
3+
# BasedOnStyle: LLVM
4+
AccessModifierOffset: -4
5+
AlignAfterOpenBracket: AlwaysBreak
6+
AlignArrayOfStructures: Right
7+
AlignConsecutiveAssignments:
8+
Enabled: true
9+
AcrossEmptyLines: false
10+
AcrossComments: false
11+
AlignCompound: false
12+
PadOperators: true
13+
AlignConsecutiveBitFields:
14+
Enabled: false
15+
AcrossEmptyLines: false
16+
AcrossComments: false
17+
AlignCompound: false
18+
PadOperators: false
19+
AlignConsecutiveDeclarations:
20+
Enabled: false
21+
AcrossEmptyLines: false
22+
AcrossComments: false
23+
AlignCompound: false
24+
PadOperators: false
25+
AlignConsecutiveMacros:
26+
Enabled: false
27+
AcrossEmptyLines: false
28+
AcrossComments: false
29+
AlignCompound: false
30+
PadOperators: false
31+
AlignConsecutiveShortCaseStatements:
32+
Enabled: true
33+
AcrossEmptyLines: false
34+
AcrossComments: false
35+
AlignCaseColons: false
36+
AlignEscapedNewlines: Right
37+
AlignOperands: Align
38+
AlignTrailingComments:
39+
Kind: Always
40+
OverEmptyLines: 0
41+
AllowAllArgumentsOnNextLine: false
42+
AllowAllParametersOfDeclarationOnNextLine: false
43+
AllowShortBlocksOnASingleLine: Never
44+
AllowShortCaseLabelsOnASingleLine: false
45+
AllowShortEnumsOnASingleLine: true
46+
AllowShortFunctionsOnASingleLine: InlineOnly
47+
AllowShortIfStatementsOnASingleLine: Never
48+
AllowShortLambdasOnASingleLine: All
49+
AllowShortLoopsOnASingleLine: false
50+
AlwaysBreakAfterDefinitionReturnType: None
51+
AlwaysBreakAfterReturnType: None
52+
AlwaysBreakBeforeMultilineStrings: false
53+
AlwaysBreakTemplateDeclarations: MultiLine
54+
AttributeMacros:
55+
- __capability
56+
BinPackArguments: false
57+
BinPackParameters: false
58+
BitFieldColonSpacing: Both
59+
BraceWrapping:
60+
AfterCaseLabel: true
61+
AfterClass: true
62+
AfterControlStatement: Always
63+
AfterEnum: true
64+
AfterExternBlock: true
65+
AfterFunction: true
66+
AfterNamespace: true
67+
AfterObjCDeclaration: true
68+
AfterStruct: true
69+
AfterUnion: true
70+
BeforeCatch: true
71+
BeforeElse: true
72+
BeforeLambdaBody: false
73+
BeforeWhile: true
74+
IndentBraces: false
75+
SplitEmptyFunction: true
76+
SplitEmptyRecord: true
77+
SplitEmptyNamespace: true
78+
BreakAfterAttributes: Never
79+
BreakAfterJavaFieldAnnotations: false
80+
BreakArrays: true
81+
BreakBeforeBinaryOperators: All
82+
BreakBeforeConceptDeclarations: Always
83+
BreakBeforeBraces: Custom
84+
BreakBeforeInlineASMColon: OnlyMultiline
85+
BreakBeforeTernaryOperators: true
86+
BreakConstructorInitializers: BeforeComma
87+
BreakInheritanceList: BeforeColon
88+
BreakStringLiterals: true
89+
ColumnLimit: 100
90+
CommentPragmas: '^ IWYU pragma:'
91+
CompactNamespaces: false
92+
ConstructorInitializerIndentWidth: 4
93+
ContinuationIndentWidth: 4
94+
Cpp11BracedListStyle: true
95+
DerivePointerAlignment: false
96+
DisableFormat: false
97+
EmptyLineAfterAccessModifier: Never
98+
EmptyLineBeforeAccessModifier: LogicalBlock
99+
ExperimentalAutoDetectBinPacking: false
100+
FixNamespaceComments: true
101+
ForEachMacros:
102+
- foreach
103+
- Q_FOREACH
104+
- BOOST_FOREACH
105+
IfMacros:
106+
- KJ_IF_MAYBE
107+
IncludeBlocks: Preserve
108+
IncludeCategories:
109+
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
110+
Priority: 2
111+
SortPriority: 0
112+
CaseSensitive: false
113+
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
114+
Priority: 3
115+
SortPriority: 0
116+
CaseSensitive: false
117+
- Regex: '.*'
118+
Priority: 1
119+
SortPriority: 0
120+
CaseSensitive: false
121+
IncludeIsMainRegex: '(Test)?$'
122+
IncludeIsMainSourceRegex: ''
123+
IndentAccessModifiers: false
124+
IndentCaseBlocks: false
125+
IndentCaseLabels: true
126+
IndentExternBlock: AfterExternBlock
127+
IndentGotoLabels: true
128+
IndentPPDirectives: None
129+
IndentRequiresClause: true
130+
IndentWidth: 4
131+
IndentWrappedFunctionNames: false
132+
InsertBraces: false
133+
InsertNewlineAtEOF: false
134+
InsertTrailingCommas: None
135+
IntegerLiteralSeparator:
136+
Binary: 0
137+
BinaryMinDigits: 0
138+
Decimal: 0
139+
DecimalMinDigits: 0
140+
Hex: 0
141+
HexMinDigits: 0
142+
JavaScriptQuotes: Leave
143+
JavaScriptWrapImports: true
144+
KeepEmptyLinesAtTheStartOfBlocks: true
145+
KeepEmptyLinesAtEOF: false
146+
LambdaBodyIndentation: Signature
147+
LineEnding: DeriveLF
148+
MacroBlockBegin: ''
149+
MacroBlockEnd: ''
150+
MaxEmptyLinesToKeep: 1
151+
NamespaceIndentation: None
152+
ObjCBinPackProtocolList: Auto
153+
ObjCBlockIndentWidth: 2
154+
ObjCBreakBeforeNestedBlockParam: true
155+
ObjCSpaceAfterProperty: false
156+
ObjCSpaceBeforeProtocolList: true
157+
PackConstructorInitializers: Never
158+
PenaltyBreakAssignment: 2
159+
PenaltyBreakBeforeFirstCallParameter: 19
160+
PenaltyBreakComment: 300
161+
PenaltyBreakFirstLessLess: 120
162+
PenaltyBreakOpenParenthesis: 0
163+
PenaltyBreakString: 1000
164+
PenaltyBreakTemplateDeclaration: 10
165+
PenaltyExcessCharacter: 1000000
166+
PenaltyIndentedWhitespace: 0
167+
PenaltyReturnTypeOnItsOwnLine: 200
168+
PointerAlignment: Middle
169+
PPIndentWidth: -1
170+
QualifierAlignment: Leave
171+
ReferenceAlignment: Pointer
172+
ReflowComments: true
173+
RemoveBracesLLVM: false
174+
RemoveParentheses: Leave
175+
RemoveSemicolon: false
176+
RequiresClausePosition: OwnLine
177+
RequiresExpressionIndentation: OuterScope
178+
SeparateDefinitionBlocks: Leave
179+
ShortNamespaceLines: 1
180+
SortIncludes: CaseSensitive
181+
SortJavaStaticImport: Before
182+
SortUsingDeclarations: LexicographicNumeric
183+
SpaceAfterCStyleCast: false
184+
SpaceAfterLogicalNot: false
185+
SpaceAfterTemplateKeyword: true
186+
SpaceAroundPointerQualifiers: Default
187+
SpaceBeforeAssignmentOperators: true
188+
SpaceBeforeCaseColon: false
189+
SpaceBeforeCpp11BracedList: false
190+
SpaceBeforeCtorInitializerColon: true
191+
SpaceBeforeInheritanceColon: true
192+
SpaceBeforeJsonColon: false
193+
SpaceBeforeParens: ControlStatements
194+
SpaceBeforeParensOptions:
195+
AfterControlStatements: true
196+
AfterForeachMacros: true
197+
AfterFunctionDefinitionName: false
198+
AfterFunctionDeclarationName: false
199+
AfterIfMacros: true
200+
AfterOverloadedOperator: false
201+
AfterRequiresInClause: false
202+
AfterRequiresInExpression: false
203+
BeforeNonEmptyParentheses: false
204+
SpaceBeforeRangeBasedForLoopColon: true
205+
SpaceBeforeSquareBrackets: false
206+
SpaceInEmptyBlock: false
207+
SpacesBeforeTrailingComments: 1
208+
SpacesInAngles: Never
209+
SpacesInContainerLiterals: true
210+
SpacesInLineCommentPrefix:
211+
Minimum: 1
212+
Maximum: -1
213+
SpacesInParens: Never
214+
SpacesInParensOptions:
215+
InCStyleCasts: false
216+
InConditionalStatements: false
217+
InEmptyParentheses: false
218+
Other: false
219+
SpacesInSquareBrackets: false
220+
Standard: c++20
221+
StatementAttributeLikeMacros:
222+
- Q_EMIT
223+
StatementMacros:
224+
- Q_UNUSED
225+
- QT_REQUIRE_VERSION
226+
TabWidth: 4
227+
UseTab: Never
228+
VerilogBreakBetweenInstancePorts: true
229+
WhitespaceSensitiveMacros:
230+
- BOOST_PP_STRINGIZE
231+
- CF_SWIFT_NAME
232+
- NS_SWIFT_NAME
233+
- PP_STRINGIZE
234+
- STRINGIZE
235+
...

.git-blame-ignore-revs

Whitespace-only changes.

.github/workflows/ci_workflow.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,3 +883,58 @@ jobs:
883883
./${{ matrix.build-type }}/consumer
884884
shell: bash
885885
working-directory: _build/tests/cmake-consumer-dist
886+
887+
# ---------------------------------------------------------------------------
888+
# clang-format
889+
# ---------------------------------------------------------------------------
890+
891+
clang-format:
892+
name: 'Linux Latest clang-format'
893+
# Avoid duplicated checks when a pull_request is opened from a local branch.
894+
if: |
895+
github.event_name == 'push' ||
896+
github.event.pull_request.head.repo.full_name != github.repository
897+
runs-on: ubuntu-latest
898+
steps:
899+
- name: Setup Python
900+
uses: actions/setup-python@v5
901+
with:
902+
python-version: 3.12
903+
- name: Install clang-format
904+
run: |
905+
pip install clang-format==22.1.0
906+
- name: Checkout
907+
uses: actions/checkout@v4
908+
- name: Create build directories
909+
run: |
910+
mkdir _install
911+
mkdir _build
912+
- name: Configure
913+
run: |
914+
cmake ../. \
915+
-DCMAKE_INSTALL_PREFIX=../_install \
916+
-DCMAKE_BUILD_TYPE=Release \
917+
-DCMAKE_CXX_STANDARD=20 \
918+
-DBUILD_SHARED_LIBS=ON \
919+
-DOCIO_BUILD_DOCS=OFF \
920+
-DOCIO_BUILD_OPENFX=ON \
921+
-DOCIO_BUILD_GPU_TESTS=OFF \
922+
-DOCIO_USE_SIMD=ON \
923+
-DOCIO_USE_OIIO_FOR_APPS=OFF \
924+
-DOCIO_INSTALL_EXT_PACKAGES=ALL \
925+
-DOCIO_WARNING_AS_ERROR=ON \
926+
-DPython_EXECUTABLE=$(which python)
927+
working-directory: _build
928+
- name: Build
929+
run: |
930+
cmake --build . \
931+
--target clang-format \
932+
--config Release \
933+
-- -j$(nproc)
934+
working-directory: _build
935+
- name: Test clang-format
936+
run: |
937+
if [[ `git status --porcelain` ]]; then
938+
echo "Failing clang-format check, please run locally before submitting the PR."
939+
exit 1
940+
fi

CMakeLists.txt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,3 +539,71 @@ install(
539539
FILES "${OCIO_PROJECT_CONFIG}" "${OCIO_VERSION_CONFIG}"
540540
DESTINATION "${OCIO_CONFIG_INSTALL_DIR}"
541541
)
542+
543+
544+
###########################################################################
545+
# Setup clang-format custom target (based on OpenImageIO).
546+
# Note: skip all of this checking, setup, and cmake-format target if this
547+
# is being built as a subproject.
548+
549+
if (PROJECT_IS_TOP_LEVEL)
550+
set (CLANG_FORMAT_EXE_HINT "" CACHE PATH "clang-format executable's directory (will search if not specified")
551+
set (CLANG_FORMAT_INCLUDES
552+
"include/*.h"
553+
"src/*.h"
554+
"src/*.cpp"
555+
# Not enabled for now, requires a lot of code annotation
556+
# to protect the test cases data.
557+
# "tests/*.h"
558+
# "tests/*.cpp"
559+
"vendor/openfx/*.h"
560+
"vendor/openfx/*.cpp"
561+
CACHE STRING "Glob patterns to include for clang-format"
562+
)
563+
set (CLANG_FORMAT_EXCLUDES
564+
# clang-format complains about Objective-C
565+
"msl.h"
566+
# SIMD headers have unusual formating we want to preserve
567+
"src/OpenColorIO/AVX.h"
568+
"src/OpenColorIO/AVX2.h"
569+
"src/OpenColorIO/AVX512.h"
570+
"src/OpenColorIO/SSE.h"
571+
"src/OpenColorIO/SSE2.h"
572+
# Ignore vendored in OpenFX support library
573+
"vendor/openfx/Support/*"
574+
CACHE STRING "Glob patterns to exclude for clang-format"
575+
)
576+
577+
find_program (CLANG_FORMAT_EXE
578+
NAMES clang-format bin/clang-format
579+
HINTS ${CLANG_FORMAT_EXE_HINT}
580+
ENV CLANG_FORMAT_EXE_HINT
581+
ENV LLVM_DIRECTORY
582+
NO_DEFAULT_PATH
583+
DOC "Path to clang-format executable"
584+
)
585+
find_program (CLANG_FORMAT_EXE
586+
NAMES clang-format bin/clang-format
587+
)
588+
589+
if (CLANG_FORMAT_EXE)
590+
message (STATUS "clang-format found: ${CLANG_FORMAT_EXE}")
591+
592+
# Start with the list of files to include when formatting...
593+
file (GLOB_RECURSE FILES_TO_FORMAT ${CLANG_FORMAT_INCLUDES})
594+
# ... then process any list of excludes we are given
595+
foreach (_pat ${CLANG_FORMAT_EXCLUDES})
596+
file (GLOB_RECURSE _excl ${_pat})
597+
list (REMOVE_ITEM FILES_TO_FORMAT ${_excl})
598+
endforeach ()
599+
600+
file (COPY
601+
${CMAKE_CURRENT_SOURCE_DIR}/.clang-format
602+
DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
603+
604+
add_custom_target (clang-format
605+
COMMAND "${CLANG_FORMAT_EXE}" --verbose -i -style=file ${FILES_TO_FORMAT} )
606+
else ()
607+
message (STATUS "clang-format not found.")
608+
endif ()
609+
endif ()

docs/guides/contributing/coding_style_guide.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,28 @@ There are two main rules when contributing to OpenColorIO:
1818
Use your head and ask for advice if your common sense seems to disagree with
1919
the conventions.
2020

21+
clang-format
22+
************
23+
24+
OpenColorIO uses `clang-format
25+
<https://clang.llvm.org/docs/ClangFormat.html>` to enforce its coding
26+
conventions. You can obtain clang-format from various sources, including
27+
packages managers and Python pip install. The command below shows how
28+
the CI workflow installs clang-format, the version is pinned to avoid
29+
breaking the build if a new version introduce behavioral changes. Please
30+
note CMake configure expects clang-format to be available on your path.
31+
32+
pip install clang-format==22.1.0
33+
34+
When submitting a PR against
35+
OpenColorIO repository, a check will make sure that clang-format has been run
36+
locally before pushing the changes.
37+
38+
Once clang-format is available on your system, from your build directory run
39+
CMake configure step, then build the `clang-format` target::
40+
41+
cmake --build . --target clang-format
42+
2143
File conventions
2244
****************
2345

0 commit comments

Comments
 (0)