# Copyright © 2022 Imagination Technologies Ltd.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

Language: Cpp
Standard: c++11

UseCRLF: false
ColumnLimit: 80

DeriveLineEnding: false
DerivePointerAlignment: false
ExperimentalAutoDetectBinPacking: false

DisableFormat: false

########
# Tabs #
########
UseTab: Never
TabWidth: 3

ConstructorInitializerIndentWidth: 6
ContinuationIndentWidth: 3

IndentWidth: 3
#IndentCaseBlocks: true  # Requires clang-11
IndentCaseLabels: false
#IndentExternBlock: NoIndent  # Requires clang-11
IndentGotoLabels: false
IndentPPDirectives: AfterHash
IndentWrappedFunctionNames: false
AccessModifierOffset: -4  # -IndentWidth

NamespaceIndentation: None

##########
# Braces #
##########
AlignAfterOpenBracket: Align
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
BinPackArguments: false
BinPackParameters: false

Cpp11BracedListStyle: false

########################
# Whitespace Alignment #
########################
AlignConsecutiveAssignments: None
#AlignConsecutiveBitFields: false  # Requires clang-11
AlignConsecutiveDeclarations: None
AlignConsecutiveMacros: None
AlignTrailingComments: false

AlignEscapedNewlines: Left

#AlignOperands: Align  # Requires clang-11
#BitFieldColonSpacing: Both  # Requires clang-12

PointerAlignment: Right
#SpaceAroundPointerQualifiers: Both  # Requires clang-12

SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
#SpaceBeforeParens: ControlStatementsExceptForEachMacros  # Requires clang-11
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesInAngles: Never
SpacesInCStyleCastParentheses: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpacesBeforeTrailingComments: 2

############################
# Multi-line constructions #
############################
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
#AllowShortEnumsOnASingleLine: false  # Requires clang-11
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: false

AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes

BreakBeforeBraces: Custom
BraceWrapping:
  AfterCaseLabel: false
  AfterClass: false
  AfterControlStatement: Never
  AfterEnum: false
  AfterFunction: true
  AfterNamespace: true
  AfterObjCDeclaration: false
  AfterStruct: false
  AfterUnion: false
  BeforeCatch: false
  BeforeElse: false
#  BeforeLambdaBody: false  # Requires clang-11
  IndentBraces: false
  SplitEmptyFunction: true
  SplitEmptyNamespace: true
  SplitEmptyRecord: true

BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true

BreakConstructorInitializers: AfterColon
BreakInheritanceList: AfterColon

BreakStringLiterals: false

CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true

#InsertTrailingCommas: Wrapped  # Requires clang-11

KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1

SortUsingDeclarations: true

############
# Includes #
############
# TODO: Temporary config
IncludeBlocks: Preserve
SortIncludes: Never
# TODO: This requires additional work to clean up headers & includes first
#IncludeBlocks: Regroup
#SortIncludes: true
#IncludeIsMainRegex: '(_test)?$'
##IncludeIsMainSourceRegex: <default>
#IncludeCategories:
#  - Regex:        '^"'
#    Priority:     1

############
# Comments #
############
FixNamespaceComments: false

#############
# Penalties #
#############
# Taken from torvalds/kernel:.clang-format
PenaltyBreakAssignment: 10
PenaltyBreakBeforeFirstCallParameter: 30
PenaltyBreakComment: 10
PenaltyBreakFirstLessLess: 0
PenaltyBreakString: 10
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 100
PenaltyReturnTypeOnItsOwnLine: 60

#######################
# User-defined macros #
#######################
CommentPragmas: '^ IWYU pragma:'

MacroBlockBegin: ''
MacroBlockEnd: ''

#AttributeMacros: []  # Requires clang-12

ForEachMacros: [
  'BITSET_FOREACH_SET',
  'foreach_instr',
  'foreach_instr_safe',
  'foreach_list_typed',
  'foreach_list_typed_from',
  'foreach_list_typed_from_reverse',
  'foreach_list_typed_reverse',
  'foreach_list_typed_reverse_safe',
  'foreach_list_typed_safe',
  'foreach_target',
  'hash_table_foreach',
  'hash_table_u64_foreach',
  'LIST_FOR_EACH_ENTRY',
  'LIST_FOR_EACH_ENTRY_FROM',
  'LIST_FOR_EACH_ENTRY_FROM_REV',
  'LIST_FOR_EACH_ENTRY_SAFE',
  'LIST_FOR_EACH_ENTRY_SAFE_REV',
  'list_for_each_entry',
  'list_for_each_entry_from',
  'list_for_each_entry_from_rev',
  'list_for_each_entry_from_safe',
  'list_for_each_entry_rev',
  'list_for_each_entry_safe',
  'list_for_each_entry_safe_rev',
  'list_pair_for_each_entry',
  'pvr_csb_emit',
  'pvr_csb_emit_merge',
  'pvr_csb_pack',
  'nir_foreach_block',
  'nir_foreach_block_safe',
  'nir_foreach_block_unstructured',
  'nir_foreach_reg_decl',
  'nir_foreach_entrypoint',
  'nir_foreach_entrypoint_safe',
  'nir_foreach_function',
  'nir_foreach_function_with_impl',
  'nir_foreach_instr',
  'nir_foreach_instr_safe',
  'nir_foreach_phi_src',
  'nir_foreach_shader_in_variable',
  'nir_foreach_shader_out_variable',
  'nir_foreach_use',
  'nir_foreach_use_safe',
  'nir_foreach_variable_with_modes',
  'nir_foreach_variable_with_modes_safe',
  'pco_foreach_block_in_func',
  'pco_foreach_block_in_func_from',
  'pco_foreach_block_in_func_from_rev',
  'pco_foreach_block_in_func_rev',
  'pco_foreach_cf_node_in_func',
  'pco_foreach_cf_node_in_func_structured',
  'pco_foreach_cf_node_in_if_else',
  'pco_foreach_cf_node_in_if_epilogue',
  'pco_foreach_cf_node_in_if_interlogue',
  'pco_foreach_cf_node_in_if_prologue',
  'pco_foreach_cf_node_in_if_then',
  'pco_foreach_cf_node_in_loop',
  'pco_foreach_cf_node_in_loop_epilogue',
  'pco_foreach_cf_node_in_loop_interlogue',
  'pco_foreach_cf_node_in_loop_prologue',
  'pco_foreach_func_in_shader',
  'pco_foreach_func_in_shader_rev',
  'pco_foreach_if_in_func',
  'pco_foreach_if_in_func_from',
  'pco_foreach_if_in_func_from_rev',
  'pco_foreach_if_in_func_rev',
  'pco_foreach_ssa_bool_if_in_func',
  'pco_foreach_ssa_if_in_func',
  'pco_foreach_igrp_in_block',
  'pco_foreach_igrp_in_func',
  'pco_foreach_instr_dest',
  'pco_foreach_instr_dest_from',
  'pco_foreach_instr_dest_hwreg',
  'pco_foreach_instr_dest_ssa',
  'pco_foreach_instr_dest_ssa_from',
  'pco_foreach_instr_dest_vreg',
  'pco_foreach_instr_dest_vreg_ssa',
  'pco_foreach_instr_in_block',
  'pco_foreach_instr_in_block_from',
  'pco_foreach_instr_in_block_from_rev',
  'pco_foreach_instr_in_block_rev',
  'pco_foreach_instr_in_block_safe',
  'pco_foreach_instr_in_block_safe_rev',
  'pco_foreach_instr_in_func',
  'pco_foreach_instr_in_func_from',
  'pco_foreach_instr_in_func_from_rev',
  'pco_foreach_instr_in_func_rev',
  'pco_foreach_instr_in_func_safe',
  'pco_foreach_instr_in_func_safe_rev',
  'pco_foreach_instr_in_igrp',
  'pco_foreach_instr_in_igrp_rev',
  'pco_foreach_instr_src',
  'pco_foreach_instr_src_from',
  'pco_foreach_instr_src_hwreg',
  'pco_foreach_instr_src_ssa',
  'pco_foreach_instr_src_ssa_from',
  'pco_foreach_instr_src_vreg',
  'pco_foreach_instr_src_vreg_ssa',
  'pco_foreach_loop_in_func',
  'pco_foreach_loop_in_func_from',
  'pco_foreach_loop_in_func_from_rev',
  'pco_foreach_loop_in_func_rev',
  'pco_foreach_phase',
  'pco_foreach_phase_from',
  'pco_foreach_phase_from_rev',
  'pco_foreach_phase_in_igrp',
  'pco_foreach_phase_in_igrp_from',
  'pco_foreach_phase_in_igrp_from_rev',
  'pco_foreach_phase_in_igrp_rev',
  'pco_foreach_phase_rev',
  'pco_foreach_phi_src_in_instr',
  'pco_foreach_vreg_if_in_func',
  'pco_foreach_vreg_ssa_if_in_func',
  'pvr_foreach_supported_tex_format',
  'pvr_foreach_supported_tex_format_',
  'pvr_foreach_supported_tex_format_compressed',
  'pvr_foreach_supported_tex_format_compressed_',
  'rogue_foreach_block',
  'rogue_foreach_block_safe',
  'rogue_foreach_block_rev',
  'rogue_foreach_block_safe_rev',
  'rogue_foreach_block_use',
  'rogue_foreach_block_use_safe',
  'rogue_foreach_drc_trxn',
  'rogue_foreach_drc_trxn_safe',
  'rogue_foreach_phase_in_set',
  'rogue_foreach_phase_in_set_rev',
  'rogue_foreach_imm_use',
  'rogue_foreach_imm_use_safe',
  'rogue_foreach_instr_group_in_block',
  'rogue_foreach_instr_group_in_block_safe',
  'rogue_foreach_instr_group_in_shader',
  'rogue_foreach_instr_group_in_shader_safe',
  'rogue_foreach_instr_in_block',
  'rogue_foreach_instr_in_block_safe',
  'rogue_foreach_instr_in_block_rev',
  'rogue_foreach_instr_in_block_safe_rev',
  'rogue_foreach_instr_in_shader',
  'rogue_foreach_instr_in_shader_safe',
  'rogue_foreach_instr_in_shader_rev',
  'rogue_foreach_instr_in_shader_safe_rev',
  'rogue_foreach_mod_in_set',
  'rogue_foreach_reg',
  'rogue_foreach_reg_safe',
  'rogue_foreach_reg_use',
  'rogue_foreach_reg_use_safe',
  'rogue_foreach_reg_write',
  'rogue_foreach_reg_write_safe',
  'rogue_foreach_regarray',
  'rogue_foreach_regarray_safe',
  'rogue_foreach_regarray_use',
  'rogue_foreach_regarray_use_safe',
  'rogue_foreach_regarray_write',
  'rogue_foreach_regarray_write_safe',
  'rogue_foreach_subarray',
  'rogue_foreach_subarray_safe',
  'rb_tree_foreach',
  'rb_tree_foreach_safe',
  'u_foreach_bit',
  'u_foreach_bit64',
  'u_vector_foreach',
  'util_dynarray_foreach',
  'util_dynarray_foreach_reverse',
  'vk_foreach_struct',
  'vk_foreach_struct_const',
# FIXME: vk_outarray_append doesn't fit here, remove
# it when a better solution exists for it.
  'vk_outarray_append',
  'vk_outarray_append_typed'
]

NamespaceMacros: [
]

StatementMacros: [
]

TypenameMacros: [
]

#WhitespaceSensitiveMacros: []  # Requires clang-11
