Skip to content

Commit 8c2bf08

Browse files
authored
Add clang-format definition, and CMake targets for formatting (#74)
The .clang-format tries to align as much as is possible with the existing formatting, though some differences can be noted, particularly around method parameters in declarations.
1 parent de21c90 commit 8c2bf08

File tree

93 files changed

+2931
-3439
lines changed

Some content is hidden

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

93 files changed

+2931
-3439
lines changed

.clang-format

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
---
2+
Language: Cpp
3+
BasedOnStyle: Mozilla
4+
5+
AccessModifierOffset: -4
6+
AlignAfterOpenBracket: Align
7+
AlignArrayOfStructures: None
8+
AlignConsecutiveAssignments: None
9+
AlignConsecutiveBitFields: Consecutive
10+
AlignConsecutiveDeclarations: None
11+
AlignConsecutiveMacros: Consecutive
12+
AlignEscapedNewlines: Right
13+
AlignOperands: AlignAfterOperator
14+
AlignTrailingComments: true
15+
AllowAllArgumentsOnNextLine: false
16+
AllowAllConstructorInitializersOnNextLine: false
17+
AllowAllParametersOfDeclarationOnNextLine: false
18+
AllowShortBlocksOnASingleLine: Never
19+
AllowShortCaseLabelsOnASingleLine: false
20+
AllowShortEnumsOnASingleLine: false
21+
AllowShortFunctionsOnASingleLine: InlineOnly
22+
AllowShortIfStatementsOnASingleLine: Never
23+
AllowShortLambdasOnASingleLine: All
24+
AllowShortLoopsOnASingleLine: false
25+
AlwaysBreakAfterDefinitionReturnType: None
26+
AlwaysBreakAfterReturnType: None
27+
AlwaysBreakBeforeMultilineStrings: false
28+
AlwaysBreakTemplateDeclarations: Yes
29+
AttributeMacros:
30+
BinPackArguments: false
31+
BinPackParameters: false
32+
BitFieldColonSpacing: Both
33+
BraceWrapping:
34+
AfterCaseLabel: true
35+
AfterClass: true
36+
AfterControlStatement: Always
37+
AfterEnum: true
38+
AfterFunction: true
39+
AfterNamespace: true
40+
AfterStruct: true
41+
AfterUnion: true
42+
AfterExternBlock: true
43+
BeforeCatch: true
44+
BeforeElse: true
45+
BeforeLambdaBody: true
46+
BeforeWhile: true
47+
IndentBraces: false
48+
SplitEmptyFunction: true
49+
SplitEmptyRecord: true
50+
SplitEmptyNamespace: true
51+
BreakBeforeBinaryOperators: NonAssignment
52+
BreakBeforeBraces: Custom
53+
BreakBeforeConceptDeclarations: true
54+
BreakBeforeTernaryOperators: true
55+
BreakConstructorInitializers: BeforeColon
56+
BreakInheritanceList: BeforeColon
57+
BreakStringLiterals: false
58+
ColumnLimit: 120
59+
CommentPragmas: '\(no-format\) $'
60+
CompactNamespaces: true
61+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
62+
ConstructorInitializerIndentWidth: 4
63+
ContinuationIndentWidth: 4
64+
Cpp11BracedListStyle: true
65+
DeriveLineEnding: false
66+
DerivePointerAlignment: false
67+
EmptyLineAfterAccessModifier: Never
68+
EmptyLineBeforeAccessModifier: Always
69+
FixNamespaceComments: false
70+
ForEachMacros:
71+
IfMacros:
72+
IncludeBlocks: Regroup
73+
IncludeCategories:
74+
- Regex: '^"[^"]+"$'
75+
Priority: 2
76+
SortPriority: 2
77+
- Regex: '^<[0-9A-Za-z_/]+>$'
78+
Priority: 3
79+
SortPriority: 3
80+
- Regex: '^<[Ww]indows.h>$'
81+
Priority: 4
82+
SortPriority: 4
83+
- Regex: '^<(gtest|gmock)/[^>]+\.(h|hh|hpp)>$'
84+
Priority: 7
85+
SortPriority: 7
86+
- Regex: '^<[^>]+\.(h|hh|hpp)>$'
87+
Priority: 8
88+
SortPriority: 8
89+
IncludeIsMainRegex: '$'
90+
IncludeIsMainSourceRegex: '(_impl\.hpp)$'
91+
IndentAccessModifiers: false
92+
IndentCaseBlocks: false
93+
IndentCaseLabels: false
94+
IndentExternBlock: false
95+
IndentGotoLabels: false
96+
IndentPPDirectives: AfterHash
97+
IndentRequiresClause: false
98+
IndentWidth: 4 #
99+
IndentWrappedFunctionNames: true
100+
InsertBraces: true
101+
InsertNewlineAtEOF: true
102+
InsertTrailingCommas: Wrapped
103+
KeepEmptyLinesAtTheStartOfBlocks: false
104+
KeepEmptyLinesAtEOF: true
105+
LambdaBodyIndentation: Signature
106+
#MacroBlockBegin:
107+
#MacroBlockEnd:
108+
MaxEmptyLinesToKeep: 1
109+
NamespaceIndentation: Inner
110+
NamespaceMacros:
111+
PackConstructorInitializers: Never
112+
PenaltyBreakOpenParenthesis: 0
113+
PPIndentWidth: 2 #
114+
PointerAlignment: Left
115+
QualifierAlignment: Custom
116+
QualifierOrder:
117+
- inline
118+
- static
119+
- constexpr
120+
- friend
121+
- const
122+
- restrict
123+
- volatile
124+
- type
125+
RawStringFormats:
126+
ReferenceAlignment: Pointer
127+
ReflowComments: true
128+
RequiresClausePosition: OwnLine
129+
RequiresExpressionIndentation: OuterScope
130+
ShortNamespaceLines: 1
131+
SortIncludes: CaseSensitive
132+
SortUsingDeclarations: true
133+
SpaceAfterCStyleCast: true
134+
SpaceAfterLogicalNot: false
135+
SpaceAfterTemplateKeyword: false
136+
SpaceAroundPointerQualifiers: Default
137+
SpaceBeforeAssignmentOperators: true
138+
SpaceBeforeCaseColon: false
139+
SpaceBeforeCpp11BracedList: true
140+
SpaceBeforeCtorInitializerColon: true
141+
SpaceBeforeParens: ControlStatementsExceptControlMacros
142+
SpaceBeforeRangeBasedForLoopColon: true
143+
SpaceBeforeSquareBrackets: false
144+
SpaceInEmptyBlock: true
145+
SpaceInEmptyParentheses: false
146+
SpacesBeforeTrailingComments: 1
147+
SpacesInAngles: false
148+
SpacesInCStyleCastParentheses: false
149+
SpacesInConditionalStatement: false
150+
SpacesInContainerLiterals: true
151+
SpacesInLineCommentPrefix:
152+
Minimum: 1
153+
Maximum: -1
154+
SpacesInParentheses: false
155+
SpacesInSquareBrackets: false
156+
Standard: c++20
157+
StatementAttributeLikeMacros:
158+
StatementMacros:
159+
- UNUSED
160+
TabWidth: 4
161+
TypenameMacros:
162+
UseCRLF: false
163+
UseTab: Never
164+
WhitespaceSensitiveMacros:
165+
...

cmake/clang-tools.cmake

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright (C) 2025 by Arm Limited (or its affiliates). All rights reserved.
2+
include_guard(GLOBAL)
3+
4+
# ###
5+
# Find various optional tools
6+
# ###
7+
find_file(CLANG_FORMAT NAMES "clang-format${CMAKE_EXECUTABLE_SUFFIX}"
8+
NO_CMAKE_FIND_ROOT_PATH)
9+
10+
if(EXISTS ${CLANG_FORMAT})
11+
execute_process(
12+
COMMAND "${CLANG_FORMAT}" --version
13+
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
14+
RESULT_VARIABLE _cf_res
15+
OUTPUT_VARIABLE _cf_out
16+
ERROR_QUIET)
17+
string(REGEX REPLACE "[\n\r\t]" " " _cf_out "${_cf_out}")
18+
string(STRIP "${_cf_out}" _cf_out)
19+
20+
if(NOT "${_cf_res}" EQUAL 0)
21+
message(STATUS "Failed to execute '${CLANG_FORMAT} --version'")
22+
unset(CLANG_FORMAT)
23+
elseif(NOT "${_cf_out}" MATCHES ".*clang-format version ([^ ]+).*")
24+
message(STATUS "Failed to understand '${CLANG_FORMAT} --version' output")
25+
unset(CLANG_FORMAT)
26+
else()
27+
string(REGEX REPLACE ".*clang-format version ([^ ]+).*" "\\1" _cf_ver "${_cf_out}")
28+
message(STATUS "Detected clang-format version '${_cf_ver}'")
29+
30+
if("${_cf_ver}" VERSION_LESS "18.0.0")
31+
message(STATUS "The version of clang-format is too old")
32+
unset(CLANG_FORMAT)
33+
endif()
34+
endif()
35+
36+
unset(_cf_res)
37+
unset(_cf_out)
38+
unset(_cf_ver)
39+
endif()
40+
41+
# ###
42+
# Add a custom targets for running clang-format
43+
# ###
44+
if(EXISTS ${CLANG_FORMAT})
45+
add_custom_target(clang-format
46+
COMMENT "Run clang-format on all targets")
47+
endif()
48+
49+
# used to get the root project directory
50+
file(REAL_PATH "${CMAKE_CURRENT_LIST_DIR}/.." _cf_root)
51+
52+
function(add_clang_tools)
53+
if(EXISTS ${CLANG_FORMAT})
54+
# Determine the name to give the custom target; base it on the folder path relative to the repo root
55+
file(RELATIVE_PATH _cf_relpath "${_cf_root}" "${CMAKE_CURRENT_SOURCE_DIR}")
56+
string(REGEX REPLACE "[^0-9A-Za-z_\-]" "-" _cf_name "${_cf_relpath}")
57+
58+
# find the sources
59+
file(
60+
GLOB_RECURSE _cf_srcs
61+
LIST_DIRECTORIES false
62+
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
63+
CONFIGURE_DEPENDS "*.cpp" "*.hpp")
64+
65+
# add the task
66+
add_custom_target(clang-format-${_cf_name}
67+
COMMAND ${CLANG_FORMAT} -i ${_cf_srcs}
68+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
69+
SOURCES ${_cf_srcs}
70+
COMMENT "Run clang-format on the sources in ${CMAKE_CURRENT_SOURCE_DIR} for ${_cf_name}")
71+
add_dependencies(clang-format clang-format-${_cf_name})
72+
endif()
73+
endfunction()

generator/generate_vulkan_common.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,9 @@ def generate_instance_decls(
502502
file.write('#pragma once\n')
503503
file.write('\n')
504504

505+
file.write('// clang-format off\n')
506+
file.write('\n')
507+
505508
file.write('#include <vulkan/vulkan.h>\n')
506509
file.write('\n')
507510

@@ -558,6 +561,8 @@ def generate_instance_decls(
558561
file.write('\n'.join(lines))
559562
file.write('\n')
560563

564+
file.write('// clang-format on\n')
565+
561566

562567
def generate_instance_defs(
563568
file: TextIO, mapping: VersionInfo, commands: list[Command]) -> None:
@@ -695,6 +700,9 @@ def generate_device_decls(
695700
file.write('#pragma once\n')
696701
file.write('\n')
697702

703+
file.write('// clang-format off\n')
704+
file.write('\n')
705+
698706
file.write('#include <vulkan/vulkan.h>\n')
699707
file.write('\n')
700708

@@ -750,6 +758,8 @@ def generate_device_decls(
750758
file.write('\n'.join(lines))
751759
file.write('\n')
752760

761+
file.write('// clang-format on\n')
762+
753763

754764
def generate_device_defs(
755765
file: TextIO, mapping: VersionInfo, commands: list[Command]) -> None:

generator/vk_codegen/device_defs.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// clang-format off
2+
13
#include <mutex>
24

35
// Include from per-layer code
@@ -10,3 +12,4 @@
1012
extern std::mutex g_vulkanLock;
1113

1214
{FUNCTION_DEFS}
15+
// clang-format on

generator/vk_codegen/device_dispatch_table.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
// clang-format off
4+
35
#include <vulkan/vulkan.h>
46

57
#include "framework/device_functions.hpp"
@@ -64,3 +66,5 @@ static inline void initDriverDeviceDispatchTable(
6466
}
6567

6668
#undef ENTRY
69+
70+
// clang-format on

generator/vk_codegen/instance_defs.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// clang-format off
2+
13
#include <mutex>
24

35
// Include from per-layer code
@@ -12,3 +14,4 @@
1214
extern std::mutex g_vulkanLock;
1315

1416
{FUNCTION_DEFS}
17+
// clang-format on

generator/vk_codegen/instance_dispatch_table.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
// clang-format off
4+
35
#include <vulkan/vulkan.h>
46

57
#include "framework/device_functions.hpp"
@@ -67,3 +69,5 @@ static inline void initDriverInstanceDispatchTable(
6769
}
6870

6971
#undef ENTRY
72+
73+
// clang-format on

generator/vk_codegen/root_CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ set(LGL_CONFIG_TRACE 0)
3333
set(LGL_CONFIG_LOG 1)
3434

3535
include(../source_common/compiler_helper.cmake)
36+
include(../cmake/clang-tools.cmake)
3637

3738
# Build steps
3839
add_subdirectory(source)

generator/vk_codegen/source_CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,5 @@ if (CMAKE_BUILD_TYPE STREQUAL "Release")
7070
ARGS --strip-all -o ${VK_LAYER_STRIP} $<TARGET_FILE:${VK_LAYER}>
7171
COMMENT "Stripped lib${VK_LAYER}.so to ${VK_LAYER_STRIP}")
7272
endif()
73+
74+
add_clang_tools()

layer_example/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ set(LGL_CONFIG_TRACE 0)
3333
set(LGL_CONFIG_LOG 1)
3434

3535
include(../source_common/compiler_helper.cmake)
36+
include(../cmake/clang-tools.cmake)
3637

3738
# Build steps
3839
add_subdirectory(source)

0 commit comments

Comments
 (0)