Skip to content

Add clang-format definition, and CMake targets for running the formatter #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 165 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
---
Language: Cpp
BasedOnStyle: Mozilla

AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignArrayOfStructures: None
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: Consecutive
AlignConsecutiveDeclarations: None
AlignConsecutiveMacros: Consecutive
AlignEscapedNewlines: Right
AlignOperands: AlignAfterOperator
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: InlineOnly
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes
AttributeMacros:
BinPackArguments: false
BinPackParameters: false
BitFieldColonSpacing: Both
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: Always
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: true
BeforeWhile: true
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Custom
BreakBeforeConceptDeclarations: true
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
BreakStringLiterals: false
ColumnLimit: 120
CommentPragmas: '\(no-format\) $'
CompactNamespaces: true
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: false
DerivePointerAlignment: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: Always
FixNamespaceComments: false
ForEachMacros:
IfMacros:
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^"[^"]+"$'
Priority: 2
SortPriority: 2
- Regex: '^<[0-9A-Za-z_/]+>$'
Priority: 3
SortPriority: 3
- Regex: '^<[Ww]indows.h>$'
Priority: 4
SortPriority: 4
- Regex: '^<(gtest|gmock)/[^>]+\.(h|hh|hpp)>$'
Priority: 7
SortPriority: 7
- Regex: '^<[^>]+\.(h|hh|hpp)>$'
Priority: 8
SortPriority: 8
IncludeIsMainRegex: '$'
IncludeIsMainSourceRegex: '(_impl\.hpp)$'
IndentAccessModifiers: false
IndentCaseBlocks: false
IndentCaseLabels: false
IndentExternBlock: false
IndentGotoLabels: false
IndentPPDirectives: AfterHash
IndentRequiresClause: false
IndentWidth: 4 #
IndentWrappedFunctionNames: true
InsertBraces: true
InsertNewlineAtEOF: true
InsertTrailingCommas: Wrapped
KeepEmptyLinesAtTheStartOfBlocks: false
KeepEmptyLinesAtEOF: true
LambdaBodyIndentation: Signature
#MacroBlockBegin:
#MacroBlockEnd:
MaxEmptyLinesToKeep: 1
NamespaceIndentation: Inner
NamespaceMacros:
PackConstructorInitializers: Never
PenaltyBreakOpenParenthesis: 0
PPIndentWidth: 2 #
PointerAlignment: Left
QualifierAlignment: Custom
QualifierOrder:
- inline
- static
- constexpr
- friend
- const
- restrict
- volatile
- type
RawStringFormats:
ReferenceAlignment: Pointer
ReflowComments: true
RequiresClausePosition: OwnLine
RequiresExpressionIndentation: OuterScope
ShortNamespaceLines: 1
SortIncludes: CaseSensitive
SortUsingDeclarations: true
SpaceAfterCStyleCast: true
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceAroundPointerQualifiers: Default
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeParens: ControlStatementsExceptControlMacros
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: c++20
StatementAttributeLikeMacros:
StatementMacros:
- UNUSED
TabWidth: 4
TypenameMacros:
UseCRLF: false
UseTab: Never
WhitespaceSensitiveMacros:
...
73 changes: 73 additions & 0 deletions cmake/clang-tools.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright (C) 2025 by Arm Limited (or its affiliates). All rights reserved.
include_guard(GLOBAL)

# ###
# Find various optional tools
# ###
find_file(CLANG_FORMAT NAMES "clang-format${CMAKE_EXECUTABLE_SUFFIX}"
NO_CMAKE_FIND_ROOT_PATH)

if(EXISTS ${CLANG_FORMAT})
execute_process(
COMMAND "${CLANG_FORMAT}" --version
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
RESULT_VARIABLE _cf_res
OUTPUT_VARIABLE _cf_out
ERROR_QUIET)
string(REGEX REPLACE "[\n\r\t]" " " _cf_out "${_cf_out}")
string(STRIP "${_cf_out}" _cf_out)

if(NOT "${_cf_res}" EQUAL 0)
message(STATUS "Failed to execute '${CLANG_FORMAT} --version'")
unset(CLANG_FORMAT)
elseif(NOT "${_cf_out}" MATCHES ".*clang-format version ([^ ]+).*")
message(STATUS "Failed to understand '${CLANG_FORMAT} --version' output")
unset(CLANG_FORMAT)
else()
string(REGEX REPLACE ".*clang-format version ([^ ]+).*" "\\1" _cf_ver "${_cf_out}")
message(STATUS "Detected clang-format version '${_cf_ver}'")

if("${_cf_ver}" VERSION_LESS "18.0.0")
message(STATUS "The version of clang-format is too old")
unset(CLANG_FORMAT)
endif()
endif()

unset(_cf_res)
unset(_cf_out)
unset(_cf_ver)
endif()

# ###
# Add a custom targets for running clang-format
# ###
if(EXISTS ${CLANG_FORMAT})
add_custom_target(clang-format
COMMENT "Run clang-format on all targets")
endif()

# used to get the root project directory
file(REAL_PATH "${CMAKE_CURRENT_LIST_DIR}/.." _cf_root)

function(add_clang_tools)
if(EXISTS ${CLANG_FORMAT})
# Determine the name to give the custom target; base it on the folder path relative to the repo root
file(RELATIVE_PATH _cf_relpath "${_cf_root}" "${CMAKE_CURRENT_SOURCE_DIR}")
string(REGEX REPLACE "[^0-9A-Za-z_\-]" "-" _cf_name "${_cf_relpath}")

# find the sources
file(
GLOB_RECURSE _cf_srcs
LIST_DIRECTORIES false
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
CONFIGURE_DEPENDS "*.cpp" "*.hpp")

# add the task
add_custom_target(clang-format-${_cf_name}
COMMAND ${CLANG_FORMAT} -i ${_cf_srcs}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
SOURCES ${_cf_srcs}
COMMENT "Run clang-format on the sources in ${CMAKE_CURRENT_SOURCE_DIR} for ${_cf_name}")
add_dependencies(clang-format clang-format-${_cf_name})
endif()
endfunction()
10 changes: 10 additions & 0 deletions generator/generate_vulkan_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,9 @@ def generate_instance_decls(
file.write('#pragma once\n')
file.write('\n')

file.write('// clang-format off\n')
file.write('\n')

file.write('#include <vulkan/vulkan.h>\n')
file.write('\n')

Expand Down Expand Up @@ -558,6 +561,8 @@ def generate_instance_decls(
file.write('\n'.join(lines))
file.write('\n')

file.write('// clang-format on\n')


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

file.write('// clang-format off\n')
file.write('\n')

file.write('#include <vulkan/vulkan.h>\n')
file.write('\n')

Expand Down Expand Up @@ -750,6 +758,8 @@ def generate_device_decls(
file.write('\n'.join(lines))
file.write('\n')

file.write('// clang-format on\n')


def generate_device_defs(
file: TextIO, mapping: VersionInfo, commands: list[Command]) -> None:
Expand Down
3 changes: 3 additions & 0 deletions generator/vk_codegen/device_defs.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// clang-format off

#include <mutex>

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

{FUNCTION_DEFS}
// clang-format on
4 changes: 4 additions & 0 deletions generator/vk_codegen/device_dispatch_table.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

// clang-format off

#include <vulkan/vulkan.h>

#include "framework/device_functions.hpp"
Expand Down Expand Up @@ -64,3 +66,5 @@ static inline void initDriverDeviceDispatchTable(
}

#undef ENTRY

// clang-format on
3 changes: 3 additions & 0 deletions generator/vk_codegen/instance_defs.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// clang-format off

#include <mutex>

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

{FUNCTION_DEFS}
// clang-format on
4 changes: 4 additions & 0 deletions generator/vk_codegen/instance_dispatch_table.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

// clang-format off

#include <vulkan/vulkan.h>

#include "framework/device_functions.hpp"
Expand Down Expand Up @@ -67,3 +69,5 @@ static inline void initDriverInstanceDispatchTable(
}

#undef ENTRY

// clang-format on
1 change: 1 addition & 0 deletions generator/vk_codegen/root_CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ set(LGL_CONFIG_TRACE 0)
set(LGL_CONFIG_LOG 1)

include(../source_common/compiler_helper.cmake)
include(../cmake/clang-tools.cmake)

# Build steps
add_subdirectory(source)
Expand Down
2 changes: 2 additions & 0 deletions generator/vk_codegen/source_CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,5 @@ if (CMAKE_BUILD_TYPE STREQUAL "Release")
ARGS --strip-all -o ${VK_LAYER_STRIP} $<TARGET_FILE:${VK_LAYER}>
COMMENT "Stripped lib${VK_LAYER}.so to ${VK_LAYER_STRIP}")
endif()

add_clang_tools()
1 change: 1 addition & 0 deletions layer_example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ set(LGL_CONFIG_TRACE 0)
set(LGL_CONFIG_LOG 1)

include(../source_common/compiler_helper.cmake)
include(../cmake/clang-tools.cmake)

# Build steps
add_subdirectory(source)
Expand Down
2 changes: 2 additions & 0 deletions layer_example/source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,5 @@ if (CMAKE_BUILD_TYPE STREQUAL "Release")
ARGS --strip-all -o ${VK_LAYER_STRIP} $<TARGET_FILE:${VK_LAYER}>
COMMENT "Stripped lib${VK_LAYER}.so to ${VK_LAYER_STRIP}")
endif()

add_clang_tools()
Loading