Skip to content

Commit e991d25

Browse files
committed
Add clang-format
Signed-off-by: Rémi Achard <remiachard@gmail.com>
1 parent 67a26e4 commit e991d25

42 files changed

Lines changed: 777 additions & 4 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.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
@@ -756,3 +756,58 @@ jobs:
756756
./${{ matrix.build-type }}/consumer
757757
shell: bash
758758
working-directory: _build/tests/cmake-consumer-dist
759+
760+
# ---------------------------------------------------------------------------
761+
# clang-format
762+
# ---------------------------------------------------------------------------
763+
764+
clang-format:
765+
name: 'Linux Latest clang-format'
766+
# Avoid duplicated checks when a pull_request is opened from a local branch.
767+
if: |
768+
github.event_name == 'push' ||
769+
github.event.pull_request.head.repo.full_name != github.repository
770+
runs-on: ubuntu-latest
771+
steps:
772+
- name: Setup Python
773+
uses: actions/setup-python@v5
774+
with:
775+
python-version: 3.12
776+
- name: Install clang-format
777+
run: |
778+
pip install clang-format
779+
- name: Checkout
780+
uses: actions/checkout@v4
781+
- name: Create build directories
782+
run: |
783+
mkdir _install
784+
mkdir _build
785+
- name: Configure
786+
run: |
787+
cmake ../. \
788+
-DCMAKE_INSTALL_PREFIX=../_install \
789+
-DCMAKE_BUILD_TYPE=Release \
790+
-DCMAKE_CXX_STANDARD=17 \
791+
-DBUILD_SHARED_LIBS=ON \
792+
-DOCIO_BUILD_DOCS=OFF \
793+
-DOCIO_BUILD_OPENFX=ON \
794+
-DOCIO_BUILD_GPU_TESTS=OFF \
795+
-DOCIO_USE_SIMD=ON \
796+
-DOCIO_USE_OIIO_FOR_APPS=OFF \
797+
-DOCIO_INSTALL_EXT_PACKAGES=ALL \
798+
-DOCIO_WARNING_AS_ERROR=ON \
799+
-DPython_EXECUTABLE=$(which python)
800+
working-directory: _build
801+
- name: Build
802+
run: |
803+
cmake --build . \
804+
--target clang-format \
805+
--config Release \
806+
-- -j$(nproc)
807+
working-directory: _build
808+
- name: Test clang-format
809+
run: |
810+
if [[ `git status --porcelain` ]]; then
811+
echo "Failing clang-format check, please run locally before submitting the PR."
812+
exit 1
813+
fi

CMakeLists.txt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,3 +533,55 @@ install(
533533
FILES "${OCIO_PROJECT_CONFIG}" "${OCIO_VERSION_CONFIG}"
534534
DESTINATION "${OCIO_CONFIG_INSTALL_DIR}"
535535
)
536+
537+
538+
###########################################################################
539+
# Setup clang-format custom target (based on OpenImageIO).
540+
541+
if (PROJECT_IS_TOP_LEVEL)
542+
set (CLANG_FORMAT_EXE_HINT "" CACHE PATH "clang-format executable's directory (will search if not specified")
543+
set (CLANG_FORMAT_INCLUDES
544+
"include/*.h"
545+
"src/*.h"
546+
"src/*.cpp"
547+
# Ignore tests for the time being as formating tends to make the code
548+
# less readable (declarations of test values, ...).
549+
# "tests/*.h"
550+
# "tests/*.cpp"
551+
"vendor/openfx/*.h"
552+
"vendor/openfx/*.cpp"
553+
CACHE STRING "Glob patterns to include for clang-format"
554+
)
555+
set (CLANG_FORMAT_EXCLUDES
556+
"msl.h"
557+
"vendor/openfx/Support/*"
558+
CACHE STRING "Glob patterns to exclude for clang-format"
559+
)
560+
561+
find_program (CLANG_FORMAT_EXE
562+
NAMES clang-format bin/clang-format
563+
HINTS ${CLANG_FORMAT_EXE_HINT}
564+
ENV CLANG_FORMAT_EXE_HINT
565+
ENV LLVM_DIRECTORY
566+
NO_DEFAULT_PATH
567+
DOC "Path to clang-format executable"
568+
)
569+
find_program (CLANG_FORMAT_EXE
570+
NAMES clang-format bin/clang-format
571+
)
572+
573+
if (CLANG_FORMAT_EXE)
574+
message (STATUS "clang-format found: ${CLANG_FORMAT_EXE}")
575+
# Start with the list of files to include when formatting...
576+
file (GLOB_RECURSE FILES_TO_FORMAT ${CLANG_FORMAT_INCLUDES})
577+
# ... then process any list of excludes we are given
578+
foreach (_pat ${CLANG_FORMAT_EXCLUDES})
579+
file (GLOB_RECURSE _excl ${_pat})
580+
list (REMOVE_ITEM FILES_TO_FORMAT ${_excl})
581+
endforeach ()
582+
file (COPY ${CMAKE_CURRENT_SOURCE_DIR}/.clang-format DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
583+
add_custom_target (clang-format COMMAND "${CLANG_FORMAT_EXE}" --verbose -i -style=file ${FILES_TO_FORMAT} )
584+
else ()
585+
message (STATUS "clang-format not found.")
586+
endif ()
587+
endif ()

docs/guides/contributing/coding_style_guide.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ 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 now 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. When submitting a PR against
28+
OpenColorIO repository, a check will make sure that clang-format has been run
29+
locally before pushing the changes.
30+
31+
Once clang-format is available on your system, from your build directory run
32+
CMake configure step, then build the `clang-format` target::
33+
34+
cmake --build . --target clang-format
35+
2136
File conventions
2237
****************
2338

0 commit comments

Comments
 (0)