Skip to contentSkip to conversation

treewide: add .clang-format configuration file

RFC PATCH Superseded Rahul Sandhu 4 participants 22 messages Sep 28, 2025 – Oct 21, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 treewide: add .clang-format configuration file

Currently only an RFC.

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

.clang-format is also read by various editors and tooling for writing C code. It may also be worth adding an editorconfig file in the future as well?

It may well also be worth adding a format target to the Makefiles, is that something that would be useful?

A few other things to consider to do in the future:
1. Reformat all existing code. I understand this is a big change, hence the RFC, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.
2. Possibly add a CI target to check that all code is formatted as per the new clang-format configuration? The options --dry-run as well as -Werror can be passed to clang-format for this.

Comments/feedback appreciated, thanks.

---
 .clang-format   | 804 ++++++++++++++++++++++++++++++++++++++++++++++++
 CONTRIBUTING.md |   2 +
 2 files changed, 806 insertions(+)
 create mode 100644 .clang-format
.clang-format new file +804 -0
new file mode 100644
index 00000000..48405c54
@@ -0,0 +1,804 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# clang-format configuration file. Intended for clang-format >= 11.
+#
+# For more information, see:
+#
+#   Documentation/dev-tools/clang-format.rst
+#   https://clang.llvm.org/docs/ClangFormat.html
+#   https://clang.llvm.org/docs/ClangFormatStyleOptions.html
+#
+---
+AccessModifierOffset: -4
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlines: Left
+AlignOperands: true
+AlignTrailingComments: false
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: false
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:
+  AfterClass: false
+  AfterControlStatement: false
+  AfterEnum: false
+  AfterFunction: true
+  AfterNamespace: true
+  AfterObjCDeclaration: false
+  AfterStruct: false
+  AfterUnion: false
+  AfterExternBlock: false
+  BeforeCatch: false
+  BeforeElse: false
+  IndentBraces: false
+  SplitEmptyFunction: true
+  SplitEmptyRecord: true
+  SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Custom
+BreakBeforeInheritanceComma: false
+BreakBeforeTernaryOperators: false
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeComma
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: false
+ColumnLimit: 80
+CommentPragmas: '^ IWYU pragma:'
+CompactNamespaces: false
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+ConstructorInitializerIndentWidth: 8
+ContinuationIndentWidth: 8
+Cpp11BracedListStyle: false
+DerivePointerAlignment: false
+DisableFormat: false
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: false
+
+# Taken from:
+#   git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' include/ tools/ \
+#   | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$,  - '\1'," \
+#   | LC_ALL=C sort -u
+ForEachMacros:
+  - '__ata_qc_for_each'
+  - '__bio_for_each_bvec'
+  - '__bio_for_each_segment'
+  - '__evlist__for_each_entry'
+  - '__evlist__for_each_entry_continue'
+  - '__evlist__for_each_entry_from'
+  - '__evlist__for_each_entry_reverse'
+  - '__evlist__for_each_entry_safe'
+  - '__for_each_mem_range'
+  - '__for_each_mem_range_rev'
+  - '__for_each_thread'
+  - '__hlist_for_each_rcu'
+  - '__map__for_each_symbol_by_name'
+  - '__pci_bus_for_each_res0'
+  - '__pci_bus_for_each_res1'
+  - '__pci_dev_for_each_res0'
+  - '__pci_dev_for_each_res1'
+  - '__perf_evlist__for_each_entry'
+  - '__perf_evlist__for_each_entry_reverse'
+  - '__perf_evlist__for_each_entry_safe'
+  - '__rq_for_each_bio'
+  - '__shost_for_each_device'
+  - '__sym_for_each'
+  - '_for_each_counter'
+  - 'apei_estatus_for_each_section'
+  - 'ata_for_each_dev'
+  - 'ata_for_each_link'
+  - 'ata_qc_for_each'
+  - 'ata_qc_for_each_raw'
+  - 'ata_qc_for_each_with_internal'
+  - 'ax25_for_each'
+  - 'ax25_uid_for_each'
+  - 'bio_for_each_bvec'
+  - 'bio_for_each_bvec_all'
+  - 'bio_for_each_folio_all'
+  - 'bio_for_each_integrity_vec'
+  - 'bio_for_each_segment'
+  - 'bio_for_each_segment_all'
+  - 'bio_list_for_each'
+  - 'bip_for_each_vec'
+  - 'bond_for_each_slave'
+  - 'bond_for_each_slave_rcu'
+  - 'bpf_for_each'
+  - 'bpf_for_each_reg_in_vstate'
+  - 'bpf_for_each_reg_in_vstate_mask'
+  - 'bpf_for_each_spilled_reg'
+  - 'bpf_object__for_each_map'
+  - 'bpf_object__for_each_program'
+  - 'btree_for_each_safe128'
+  - 'btree_for_each_safe32'
+  - 'btree_for_each_safe64'
+  - 'btree_for_each_safel'
+  - 'card_for_each_dev'
+  - 'cgroup_taskset_for_each'
+  - 'cgroup_taskset_for_each_leader'
+  - 'cpu_aggr_map__for_each_idx'
+  - 'cpufreq_for_each_efficient_entry_idx'
+  - 'cpufreq_for_each_entry'
+  - 'cpufreq_for_each_entry_idx'
+  - 'cpufreq_for_each_valid_entry'
+  - 'cpufreq_for_each_valid_entry_idx'
+  - 'css_for_each_child'
+  - 'css_for_each_descendant_post'
+  - 'css_for_each_descendant_pre'
+  - 'damon_for_each_region'
+  - 'damon_for_each_region_from'
+  - 'damon_for_each_region_safe'
+  - 'damon_for_each_scheme'
+  - 'damon_for_each_scheme_safe'
+  - 'damon_for_each_target'
+  - 'damon_for_each_target_safe'
+  - 'damos_for_each_filter'
+  - 'damos_for_each_filter_safe'
+  - 'damos_for_each_ops_filter'
+  - 'damos_for_each_ops_filter_safe'
+  - 'damos_for_each_quota_goal'
+  - 'damos_for_each_quota_goal_safe'
+  - 'data__for_each_file'
+  - 'data__for_each_file_new'
+  - 'data__for_each_file_start'
+  - 'def_for_each_cpu'
+  - 'device_for_each_child_node'
+  - 'device_for_each_child_node_scoped'
+  - 'dma_fence_array_for_each'
+  - 'dma_fence_chain_for_each'
+  - 'dma_fence_unwrap_for_each'
+  - 'dma_resv_for_each_fence'
+  - 'dma_resv_for_each_fence_unlocked'
+  - 'do_for_each_ftrace_op'
+  - 'drm_atomic_crtc_for_each_plane'
+  - 'drm_atomic_crtc_state_for_each_plane'
+  - 'drm_atomic_crtc_state_for_each_plane_state'
+  - 'drm_atomic_for_each_plane_damage'
+  - 'drm_client_for_each_connector_iter'
+  - 'drm_client_for_each_modeset'
+  - 'drm_connector_for_each_possible_encoder'
+  - 'drm_exec_for_each_locked_object'
+  - 'drm_exec_for_each_locked_object_reverse'
+  - 'drm_for_each_bridge_in_chain'
+  - 'drm_for_each_connector_iter'
+  - 'drm_for_each_crtc'
+  - 'drm_for_each_crtc_reverse'
+  - 'drm_for_each_encoder'
+  - 'drm_for_each_encoder_mask'
+  - 'drm_for_each_fb'
+  - 'drm_for_each_legacy_plane'
+  - 'drm_for_each_plane'
+  - 'drm_for_each_plane_mask'
+  - 'drm_for_each_privobj'
+  - 'drm_gem_for_each_gpuvm_bo'
+  - 'drm_gem_for_each_gpuvm_bo_safe'
+  - 'drm_gpusvm_for_each_range'
+  - 'drm_gpuva_for_each_op'
+  - 'drm_gpuva_for_each_op_from_reverse'
+  - 'drm_gpuva_for_each_op_reverse'
+  - 'drm_gpuva_for_each_op_safe'
+  - 'drm_gpuvm_bo_for_each_va'
+  - 'drm_gpuvm_bo_for_each_va_safe'
+  - 'drm_gpuvm_for_each_va'
+  - 'drm_gpuvm_for_each_va_range'
+  - 'drm_gpuvm_for_each_va_range_safe'
+  - 'drm_gpuvm_for_each_va_safe'
+  - 'drm_mm_for_each_hole'
+  - 'drm_mm_for_each_node'
+  - 'drm_mm_for_each_node_in_range'
+  - 'drm_mm_for_each_node_safe'
+  - 'dsa_switch_for_each_available_port'
+  - 'dsa_switch_for_each_cpu_port'
+  - 'dsa_switch_for_each_cpu_port_continue_reverse'
+  - 'dsa_switch_for_each_port'
+  - 'dsa_switch_for_each_port_continue_reverse'
+  - 'dsa_switch_for_each_port_safe'
+  - 'dsa_switch_for_each_user_port'
+  - 'dsa_switch_for_each_user_port_continue_reverse'
+  - 'dsa_tree_for_each_cpu_port'
+  - 'dsa_tree_for_each_user_port'
+  - 'dsa_tree_for_each_user_port_continue_reverse'
+  - 'dso__for_each_symbol'
+  - 'elf_hash_for_each_possible'
+  - 'elf_symtab__for_each_symbol'
+  - 'evlist__for_each_cpu'
+  - 'evlist__for_each_entry'
+  - 'evlist__for_each_entry_continue'
+  - 'evlist__for_each_entry_from'
+  - 'evlist__for_each_entry_reverse'
+  - 'evlist__for_each_entry_safe'
+  - 'flow_action_for_each'
+  - 'for_each_acpi_consumer_dev'
+  - 'for_each_acpi_dev_match'
+  - 'for_each_active_dev_scope'
+  - 'for_each_active_drhd_unit'
+  - 'for_each_active_iommu'
+  - 'for_each_active_irq'
+  - 'for_each_active_route'
+  - 'for_each_aggr_pgid'
+  - 'for_each_alloc_capable_rdt_resource'
+  - 'for_each_and_bit'
+  - 'for_each_andnot_bit'
+  - 'for_each_available_child_of_node'
+  - 'for_each_available_child_of_node_scoped'
+  - 'for_each_bench'
+  - 'for_each_bio'
+  - 'for_each_board_func_rsrc'
+  - 'for_each_btf_ext_rec'
+  - 'for_each_btf_ext_sec'
+  - 'for_each_bvec'
+  - 'for_each_capable_rdt_resource'
+  - 'for_each_card_auxs'
+  - 'for_each_card_auxs_safe'
+  - 'for_each_card_components'
+  - 'for_each_card_dapms'
+  - 'for_each_card_pre_auxs'
+  - 'for_each_card_prelinks'
+  - 'for_each_card_rtds'
+  - 'for_each_card_rtds_safe'
+  - 'for_each_card_widgets'
+  - 'for_each_card_widgets_safe'
+  - 'for_each_cgroup_storage_type'
+  - 'for_each_child_of_node'
+  - 'for_each_child_of_node_scoped'
+  - 'for_each_child_of_node_with_prefix'
+  - 'for_each_clear_bit'
+  - 'for_each_clear_bit_from'
+  - 'for_each_clear_bitrange'
+  - 'for_each_clear_bitrange_from'
+  - 'for_each_cmd'
+  - 'for_each_cmsghdr'
+  - 'for_each_collection'
+  - 'for_each_comp_order'
+  - 'for_each_compatible_node'
+  - 'for_each_component_dais'
+  - 'for_each_component_dais_safe'
+  - 'for_each_conduit'
+  - 'for_each_console'
+  - 'for_each_console_srcu'
+  - 'for_each_cpu'
+  - 'for_each_cpu_and'
+  - 'for_each_cpu_andnot'
+  - 'for_each_cpu_from'
+  - 'for_each_cpu_or'
+  - 'for_each_cpu_wrap'
+  - 'for_each_dapm_widgets'
+  - 'for_each_dedup_cand'
+  - 'for_each_dev_addr'
+  - 'for_each_dev_scope'
+  - 'for_each_dma_cap_mask'
+  - 'for_each_dpcm_be'
+  - 'for_each_dpcm_be_rollback'
+  - 'for_each_dpcm_be_safe'
+  - 'for_each_dpcm_fe'
+  - 'for_each_drhd_unit'
+  - 'for_each_dss_dev'
+  - 'for_each_efi_memory_desc'
+  - 'for_each_efi_memory_desc_in_map'
+  - 'for_each_element'
+  - 'for_each_element_extid'
+  - 'for_each_element_id'
+  - 'for_each_enabled_cpu'
+  - 'for_each_endpoint_of_node'
+  - 'for_each_event'
+  - 'for_each_event_tps'
+  - 'for_each_evictable_lru'
+  - 'for_each_fib6_node_rt_rcu'
+  - 'for_each_fib6_walker_rt'
+  - 'for_each_file_lock'
+  - 'for_each_free_mem_pfn_range_in_zone_from'
+  - 'for_each_free_mem_range'
+  - 'for_each_free_mem_range_reverse'
+  - 'for_each_func_rsrc'
+  - 'for_each_gpiochip_node'
+  - 'for_each_group_evsel'
+  - 'for_each_group_evsel_head'
+  - 'for_each_group_member'
+  - 'for_each_group_member_head'
+  - 'for_each_hstate'
+  - 'for_each_hwgpio'
+  - 'for_each_hwgpio_in_range'
+  - 'for_each_if'
+  - 'for_each_inject_fn'
+  - 'for_each_insn'
+  - 'for_each_insn_op_loc'
+  - 'for_each_insn_prefix'
+  - 'for_each_intid'
+  - 'for_each_iommu'
+  - 'for_each_ip_tunnel_rcu'
+  - 'for_each_irq_desc'
+  - 'for_each_irq_nr'
+  - 'for_each_lang'
+  - 'for_each_link_ch_maps'
+  - 'for_each_link_codecs'
+  - 'for_each_link_cpus'
+  - 'for_each_link_platforms'
+  - 'for_each_lru'
+  - 'for_each_matching_node'
+  - 'for_each_matching_node_and_match'
+  - 'for_each_media_entity_data_link'
+  - 'for_each_mem_pfn_range'
+  - 'for_each_mem_range'
+  - 'for_each_mem_range_rev'
+  - 'for_each_mem_region'
+  - 'for_each_member'
+  - 'for_each_memory'
+  - 'for_each_migratetype_order'
+  - 'for_each_missing_reg'
+  - 'for_each_mle_subelement'
+  - 'for_each_mod_mem_type'
+  - 'for_each_mon_capable_rdt_resource'
+  - 'for_each_mp_bvec'
+  - 'for_each_net'
+  - 'for_each_net_continue_reverse'
+  - 'for_each_net_rcu'
+  - 'for_each_netdev'
+  - 'for_each_netdev_continue'
+  - 'for_each_netdev_continue_rcu'
+  - 'for_each_netdev_continue_reverse'
+  - 'for_each_netdev_dump'
+  - 'for_each_netdev_feature'
+  - 'for_each_netdev_in_bond_rcu'
+  - 'for_each_netdev_rcu'
+  - 'for_each_netdev_reverse'
+  - 'for_each_netdev_safe'
+  - 'for_each_new_connector_in_state'
+  - 'for_each_new_crtc_in_state'
+  - 'for_each_new_mst_mgr_in_state'
+  - 'for_each_new_plane_in_state'
+  - 'for_each_new_plane_in_state_reverse'
+  - 'for_each_new_private_obj_in_state'
+  - 'for_each_new_reg'
+  - 'for_each_nhlt_endpoint'
+  - 'for_each_nhlt_endpoint_fmtcfg'
+  - 'for_each_nhlt_fmtcfg'
+  - 'for_each_node'
+  - 'for_each_node_by_name'
+  - 'for_each_node_by_type'
+  - 'for_each_node_mask'
+  - 'for_each_node_numadist'
+  - 'for_each_node_state'
+  - 'for_each_node_with_cpus'
+  - 'for_each_node_with_property'
+  - 'for_each_nonreserved_multicast_dest_pgid'
+  - 'for_each_numa_hop_mask'
+  - 'for_each_of_allnodes'
+  - 'for_each_of_allnodes_from'
+  - 'for_each_of_cpu_node'
+  - 'for_each_of_graph_port'
+  - 'for_each_of_graph_port_endpoint'
+  - 'for_each_of_pci_range'
+  - 'for_each_old_connector_in_state'
+  - 'for_each_old_crtc_in_state'
+  - 'for_each_old_mst_mgr_in_state'
+  - 'for_each_old_plane_in_state'
+  - 'for_each_old_private_obj_in_state'
+  - 'for_each_oldnew_connector_in_state'
+  - 'for_each_oldnew_crtc_in_state'
+  - 'for_each_oldnew_mst_mgr_in_state'
+  - 'for_each_oldnew_plane_in_state'
+  - 'for_each_oldnew_plane_in_state_reverse'
+  - 'for_each_oldnew_private_obj_in_state'
+  - 'for_each_online_cpu'
+  - 'for_each_online_cpu_wrap'
+  - 'for_each_online_node'
+  - 'for_each_online_pgdat'
+  - 'for_each_or_bit'
+  - 'for_each_page_ext'
+  - 'for_each_path'
+  - 'for_each_pci_bridge'
+  - 'for_each_pci_dev'
+  - 'for_each_pcm_streams'
+  - 'for_each_physmem_range'
+  - 'for_each_populated_zone'
+  - 'for_each_possible_cpu'
+  - 'for_each_possible_cpu_wrap'
+  - 'for_each_present_blessed_reg'
+  - 'for_each_present_cpu'
+  - 'for_each_present_section_nr'
+  - 'for_each_prime_number'
+  - 'for_each_prime_number_from'
+  - 'for_each_probe_cache_entry'
+  - 'for_each_process'
+  - 'for_each_process_thread'
+  - 'for_each_prop_codec_conf'
+  - 'for_each_prop_dai_codec'
+  - 'for_each_prop_dai_cpu'
+  - 'for_each_prop_dlc_codecs'
+  - 'for_each_prop_dlc_cpus'
+  - 'for_each_prop_dlc_platforms'
+  - 'for_each_property_of_node'
+  - 'for_each_rdt_resource'
+  - 'for_each_reg'
+  - 'for_each_reg_filtered'
+  - 'for_each_reloc'
+  - 'for_each_reloc_from'
+  - 'for_each_requested_gpio'
+  - 'for_each_requested_gpio_in_range'
+  - 'for_each_reserved_child_of_node'
+  - 'for_each_reserved_mem_range'
+  - 'for_each_reserved_mem_region'
+  - 'for_each_rtd_ch_maps'
+  - 'for_each_rtd_codec_dais'
+  - 'for_each_rtd_components'
+  - 'for_each_rtd_cpu_dais'
+  - 'for_each_rtd_dais'
+  - 'for_each_rtd_dais_reverse'
+  - 'for_each_sband_iftype_data'
+  - 'for_each_script'
+  - 'for_each_sec'
+  - 'for_each_set_bit'
+  - 'for_each_set_bit_from'
+  - 'for_each_set_bit_wrap'
+  - 'for_each_set_bitrange'
+  - 'for_each_set_bitrange_from'
+  - 'for_each_set_clump8'
+  - 'for_each_sg'
+  - 'for_each_sg_dma_page'
+  - 'for_each_sg_page'
+  - 'for_each_sgtable_dma_page'
+  - 'for_each_sgtable_dma_sg'
+  - 'for_each_sgtable_page'
+  - 'for_each_sgtable_sg'
+  - 'for_each_sibling_event'
+  - 'for_each_sta_active_link'
+  - 'for_each_subelement'
+  - 'for_each_subelement_extid'
+  - 'for_each_subelement_id'
+  - 'for_each_sublist'
+  - 'for_each_subsystem'
+  - 'for_each_suite'
+  - 'for_each_supported_activate_fn'
+  - 'for_each_supported_inject_fn'
+  - 'for_each_sym'
+  - 'for_each_thread'
+  - 'for_each_token'
+  - 'for_each_unicast_dest_pgid'
+  - 'for_each_valid_link'
+  - 'for_each_vif_active_link'
+  - 'for_each_vma'
+  - 'for_each_vma_range'
+  - 'for_each_vsi'
+  - 'for_each_wakeup_source'
+  - 'for_each_zone'
+  - 'for_each_zone_zonelist'
+  - 'for_each_zone_zonelist_nodemask'
+  - 'func_for_each_insn'
+  - 'fwnode_for_each_available_child_node'
+  - 'fwnode_for_each_child_node'
+  - 'fwnode_for_each_parent_node'
+  - 'fwnode_graph_for_each_endpoint'
+  - 'gadget_for_each_ep'
+  - 'genradix_for_each'
+  - 'genradix_for_each_from'
+  - 'genradix_for_each_reverse'
+  - 'hash_for_each'
+  - 'hash_for_each_possible'
+  - 'hash_for_each_possible_rcu'
+  - 'hash_for_each_possible_rcu_notrace'
+  - 'hash_for_each_possible_safe'
+  - 'hash_for_each_rcu'
+  - 'hash_for_each_safe'
+  - 'hashmap__for_each_entry'
+  - 'hashmap__for_each_entry_safe'
+  - 'hashmap__for_each_key_entry'
+  - 'hashmap__for_each_key_entry_safe'
+  - 'hctx_for_each_ctx'
+  - 'hists__for_each_format'
+  - 'hists__for_each_sort_list'
+  - 'hlist_bl_for_each_entry'
+  - 'hlist_bl_for_each_entry_rcu'
+  - 'hlist_bl_for_each_entry_safe'
+  - 'hlist_for_each'
+  - 'hlist_for_each_entry'
+  - 'hlist_for_each_entry_continue'
+  - 'hlist_for_each_entry_continue_rcu'
+  - 'hlist_for_each_entry_continue_rcu_bh'
+  - 'hlist_for_each_entry_from'
+  - 'hlist_for_each_entry_from_rcu'
+  - 'hlist_for_each_entry_rcu'
+  - 'hlist_for_each_entry_rcu_bh'
+  - 'hlist_for_each_entry_rcu_notrace'
+  - 'hlist_for_each_entry_safe'
+  - 'hlist_for_each_entry_srcu'
+  - 'hlist_for_each_safe'
+  - 'hlist_nulls_for_each_entry'
+  - 'hlist_nulls_for_each_entry_from'
+  - 'hlist_nulls_for_each_entry_rcu'
+  - 'hlist_nulls_for_each_entry_safe'
+  - 'i3c_bus_for_each_i2cdev'
+  - 'i3c_bus_for_each_i3cdev'
+  - 'idr_for_each_entry'
+  - 'idr_for_each_entry_continue'
+  - 'idr_for_each_entry_continue_ul'
+  - 'idr_for_each_entry_ul'
+  - 'iio_for_each_active_channel'
+  - 'in_dev_for_each_ifa_rcu'
+  - 'in_dev_for_each_ifa_rtnl'
+  - 'in_dev_for_each_ifa_rtnl_net'
+  - 'inet_bind_bucket_for_each'
+  - 'interval_tree_for_each_span'
+  - 'intlist__for_each_entry'
+  - 'intlist__for_each_entry_safe'
+  - 'kcore_copy__for_each_phdr'
+  - 'key_for_each'
+  - 'key_for_each_safe'
+  - 'klp_for_each_func'
+  - 'klp_for_each_func_safe'
+  - 'klp_for_each_func_static'
+  - 'klp_for_each_object'
+  - 'klp_for_each_object_safe'
+  - 'klp_for_each_object_static'
+  - 'kunit_suite_for_each_test_case'
+  - 'kvm_for_each_memslot'
+  - 'kvm_for_each_memslot_in_gfn_range'
+  - 'kvm_for_each_vcpu'
+  - 'libbpf_nla_for_each_attr'
+  - 'list_for_each'
+  - 'list_for_each_codec'
+  - 'list_for_each_codec_safe'
+  - 'list_for_each_continue'
+  - 'list_for_each_entry'
+  - 'list_for_each_entry_continue'
+  - 'list_for_each_entry_continue_rcu'
+  - 'list_for_each_entry_continue_reverse'
+  - 'list_for_each_entry_from'
+  - 'list_for_each_entry_from_rcu'
+  - 'list_for_each_entry_from_reverse'
+  - 'list_for_each_entry_lockless'
+  - 'list_for_each_entry_rcu'
+  - 'list_for_each_entry_reverse'
+  - 'list_for_each_entry_safe'
+  - 'list_for_each_entry_safe_continue'
+  - 'list_for_each_entry_safe_from'
+  - 'list_for_each_entry_safe_reverse'
+  - 'list_for_each_entry_srcu'
+  - 'list_for_each_from'
+  - 'list_for_each_prev'
+  - 'list_for_each_prev_safe'
+  - 'list_for_each_rcu'
+  - 'list_for_each_safe'
+  - 'llist_for_each'
+  - 'llist_for_each_entry'
+  - 'llist_for_each_entry_safe'
+  - 'llist_for_each_safe'
+  - 'lwq_for_each_safe'
+  - 'map__for_each_symbol'
+  - 'map__for_each_symbol_by_name'
+  - 'mas_for_each'
+  - 'mas_for_each_rev'
+  - 'mci_for_each_dimm'
+  - 'media_device_for_each_entity'
+  - 'media_device_for_each_intf'
+  - 'media_device_for_each_link'
+  - 'media_device_for_each_pad'
+  - 'media_entity_for_each_pad'
+  - 'media_pipeline_for_each_entity'
+  - 'media_pipeline_for_each_pad'
+  - 'mlx5_lag_for_each_peer_mdev'
+  - 'mptcp_for_each_subflow'
+  - 'msi_domain_for_each_desc'
+  - 'msi_for_each_desc'
+  - 'mt_for_each'
+  - 'nanddev_io_for_each_block'
+  - 'nanddev_io_for_each_page'
+  - 'neigh_for_each_in_bucket'
+  - 'neigh_for_each_in_bucket_rcu'
+  - 'neigh_for_each_in_bucket_safe'
+  - 'netdev_for_each_lower_dev'
+  - 'netdev_for_each_lower_private'
+  - 'netdev_for_each_lower_private_rcu'
+  - 'netdev_for_each_mc_addr'
+  - 'netdev_for_each_synced_mc_addr'
+  - 'netdev_for_each_synced_uc_addr'
+  - 'netdev_for_each_uc_addr'
+  - 'netdev_for_each_upper_dev_rcu'
+  - 'netdev_hw_addr_list_for_each'
+  - 'nft_rule_for_each_expr'
+  - 'nla_for_each_attr'
+  - 'nla_for_each_attr_type'
+  - 'nla_for_each_nested'
+  - 'nla_for_each_nested_type'
+  - 'nlmsg_for_each_attr'
+  - 'nlmsg_for_each_msg'
+  - 'nr_neigh_for_each'
+  - 'nr_neigh_for_each_safe'
+  - 'nr_node_for_each'
+  - 'nr_node_for_each_safe'
+  - 'of_for_each_phandle'
+  - 'of_property_for_each_string'
+  - 'of_property_for_each_u32'
+  - 'pci_bus_for_each_resource'
+  - 'pci_dev_for_each_resource'
+  - 'pcl_for_each_chunk'
+  - 'pcl_for_each_segment'
+  - 'pcm_for_each_format'
+  - 'perf_config_items__for_each_entry'
+  - 'perf_config_sections__for_each_entry'
+  - 'perf_config_set__for_each_entry'
+  - 'perf_cpu_map__for_each_cpu'
+  - 'perf_cpu_map__for_each_cpu_skip_any'
+  - 'perf_cpu_map__for_each_idx'
+  - 'perf_evlist__for_each_entry'
+  - 'perf_evlist__for_each_entry_reverse'
+  - 'perf_evlist__for_each_entry_safe'
+  - 'perf_evlist__for_each_evsel'
+  - 'perf_evlist__for_each_mmap'
+  - 'perf_evsel_for_each_per_thread_period_safe'
+  - 'perf_hpp_list__for_each_format'
+  - 'perf_hpp_list__for_each_format_safe'
+  - 'perf_hpp_list__for_each_sort_list'
+  - 'perf_hpp_list__for_each_sort_list_safe'
+  - 'plist_for_each'
+  - 'plist_for_each_continue'
+  - 'plist_for_each_entry'
+  - 'plist_for_each_entry_continue'
+  - 'plist_for_each_entry_safe'
+  - 'plist_for_each_safe'
+  - 'pnp_for_each_card'
+  - 'pnp_for_each_dev'
+  - 'protocol_for_each_card'
+  - 'protocol_for_each_dev'
+  - 'queue_for_each_hw_ctx'
+  - 'radix_tree_for_each_slot'
+  - 'radix_tree_for_each_tagged'
+  - 'rb_for_each'
+  - 'rbtree_postorder_for_each_entry_safe'
+  - 'rdma_for_each_block'
+  - 'rdma_for_each_port'
+  - 'rdma_umem_for_each_dma_block'
+  - 'resource_list_for_each_entry'
+  - 'resource_list_for_each_entry_safe'
+  - 'rhl_for_each_entry_rcu'
+  - 'rhl_for_each_rcu'
+  - 'rht_for_each'
+  - 'rht_for_each_entry'
+  - 'rht_for_each_entry_from'
+  - 'rht_for_each_entry_rcu'
+  - 'rht_for_each_entry_rcu_from'
+  - 'rht_for_each_entry_safe'
+  - 'rht_for_each_from'
+  - 'rht_for_each_rcu'
+  - 'rht_for_each_rcu_from'
+  - 'rq_for_each_bvec'
+  - 'rq_for_each_segment'
+  - 'rq_list_for_each'
+  - 'rq_list_for_each_safe'
+  - 'sample_read_group__for_each'
+  - 'scsi_for_each_prot_sg'
+  - 'scsi_for_each_sg'
+  - 'sctp_for_each_hentry'
+  - 'sctp_skb_for_each'
+  - 'sec_for_each_insn'
+  - 'sec_for_each_insn_continue'
+  - 'sec_for_each_insn_from'
+  - 'sec_for_each_sym'
+  - 'shdma_for_each_chan'
+  - 'shost_for_each_device'
+  - 'sk_for_each'
+  - 'sk_for_each_bound'
+  - 'sk_for_each_bound_safe'
+  - 'sk_for_each_entry_offset_rcu'
+  - 'sk_for_each_from'
+  - 'sk_for_each_rcu'
+  - 'sk_for_each_safe'
+  - 'sk_nulls_for_each'
+  - 'sk_nulls_for_each_from'
+  - 'sk_nulls_for_each_rcu'
+  - 'snd_array_for_each'
+  - 'snd_pcm_group_for_each_entry'
+  - 'snd_soc_dapm_widget_for_each_path'
+  - 'snd_soc_dapm_widget_for_each_path_safe'
+  - 'snd_soc_dapm_widget_for_each_sink_path'
+  - 'snd_soc_dapm_widget_for_each_source_path'
+  - 'sparsebit_for_each_set_range'
+  - 'strlist__for_each_entry'
+  - 'strlist__for_each_entry_safe'
+  - 'sym_for_each_insn'
+  - 'sym_for_each_insn_continue_reverse'
+  - 'symbols__for_each_entry'
+  - 'tb_property_for_each'
+  - 'tcf_act_for_each_action'
+  - 'tcf_exts_for_each_action'
+  - 'test_suite__for_each_test_case'
+  - 'tool_pmu__for_each_event'
+  - 'ttm_bo_lru_for_each_reserved_guarded'
+  - 'ttm_resource_manager_for_each_res'
+  - 'udp_lrpa_for_each_entry_rcu'
+  - 'udp_portaddr_for_each_entry'
+  - 'udp_portaddr_for_each_entry_rcu'
+  - 'usb_hub_for_each_child'
+  - 'v4l2_device_for_each_subdev'
+  - 'v4l2_m2m_for_each_dst_buf'
+  - 'v4l2_m2m_for_each_dst_buf_safe'
+  - 'v4l2_m2m_for_each_src_buf'
+  - 'v4l2_m2m_for_each_src_buf_safe'
+  - 'virtio_device_for_each_vq'
+  - 'vkms_config_for_each_connector'
+  - 'vkms_config_for_each_crtc'
+  - 'vkms_config_for_each_encoder'
+  - 'vkms_config_for_each_plane'
+  - 'vkms_config_connector_for_each_possible_encoder'
+  - 'vkms_config_encoder_for_each_possible_crtc'
+  - 'vkms_config_plane_for_each_possible_crtc'
+  - 'while_for_each_ftrace_op'
+  - 'workloads__for_each'
+  - 'xa_for_each'
+  - 'xa_for_each_marked'
+  - 'xa_for_each_range'
+  - 'xa_for_each_start'
+  - 'xas_for_each'
+  - 'xas_for_each_conflict'
+  - 'xas_for_each_marked'
+  - 'xbc_array_for_each_value'
+  - 'xbc_for_each_key_value'
+  - 'xbc_node_for_each_array_value'
+  - 'xbc_node_for_each_child'
+  - 'xbc_node_for_each_key_value'
+  - 'xbc_node_for_each_subkey'
+  - 'ynl_attr_for_each'
+  - 'ynl_attr_for_each_nested'
+  - 'ynl_attr_for_each_payload'
+  - 'zorro_for_each_dev'
+
+IncludeBlocks: Preserve
+IncludeCategories:
+  - Regex: '.*'
+    Priority: 1
+IncludeIsMainRegex: '(Test)?$'
+IndentCaseLabels: false
+IndentGotoLabels: false
+IndentPPDirectives: None
+IndentWidth: 8
+IndentWrappedFunctionNames: false
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: false
+MacroBlockBegin: ''
+MacroBlockEnd: ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Auto
+ObjCBlockIndentWidth: 8
+ObjCSpaceAfterProperty: true
+ObjCSpaceBeforeProtocolList: true
+
+# Taken from git's rules
+PenaltyBreakAssignment: 10
+PenaltyBreakBeforeFirstCallParameter: 30
+PenaltyBreakComment: 10
+PenaltyBreakFirstLessLess: 0
+PenaltyBreakString: 10
+PenaltyExcessCharacter: 100
+PenaltyReturnTypeOnItsOwnLine: 60
+
+PointerAlignment: Right
+ReflowComments: false
+SortIncludes: false
+SortUsingDeclarations: false
+SpaceAfterCStyleCast: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatementsExceptForEachMacros
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles: false
+SpacesInContainerLiterals: false
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard: Cpp03
+TabWidth: 8
+UseTab: Always
+...
CONTRIBUTING.md +2 -0
index c501cf84..7ec8cb0f 100644
@@ -53,6 +53,8 @@ When preparing patches, please follow these guidelines:
 -   Separate large patches into logical patches
 -   Patch descriptions must end with your "Signed-off-by" line. This means your
     code meets the Developer's certificate of origin, see below.
+-   C code should be formatted using clang-format, using the .clang-format
+    configuration file at the root of this repository.
 
 When adding new, large features or tools it is best to discuss the
 design on the mailing list prior to submitting the patch.
Signature · 1 line
2.50.1
#1 lore raw

RFC PATCH v2 1/1 treewide: add .clang-format configuration file

Currently only an RFC.

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

.clang-format is also read by various editors and tooling for writing C code. It may also be worth adding an editorconfig file in the future as well?

It may well also be worth adding a format target to the Makefiles, is that something that would be useful?

A few other things to consider to do in the future:
1. Reformat all existing code. I understand this is a big change, hence the RFC, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.
2. Possibly add a CI target to check that all code is formatted as per the new clang-format configuration? The options --dry-run as well as -Werror can be passed to clang-format for this.

Comments/feedback appreciated, thanks.

---
 .clang-format   | 130 ++++++++++++++++++++++++++++++++++++++++++++++++
 CONTRIBUTING.md |   2 +
 2 files changed, 132 insertions(+)
 create mode 100644 .clang-format

v2: remove linux kernel ForEachMacros and replace them with ours

.clang-format new file +130 -0
new file mode 100644
index 00000000..35595d87
@@ -0,0 +1,130 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# clang-format configuration file. Intended for clang-format >= 11.
+#
+# For more information, see:
+#
+#   Documentation/dev-tools/clang-format.rst
+#   https://clang.llvm.org/docs/ClangFormat.html
+#   https://clang.llvm.org/docs/ClangFormatStyleOptions.html
+#
+---
+AccessModifierOffset: -4
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlines: Left
+AlignOperands: true
+AlignTrailingComments: false
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: false
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:
+  AfterClass: false
+  AfterControlStatement: false
+  AfterEnum: false
+  AfterFunction: true
+  AfterNamespace: true
+  AfterObjCDeclaration: false
+  AfterStruct: false
+  AfterUnion: false
+  AfterExternBlock: false
+  BeforeCatch: false
+  BeforeElse: false
+  IndentBraces: false
+  SplitEmptyFunction: true
+  SplitEmptyRecord: true
+  SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Custom
+BreakBeforeInheritanceComma: false
+BreakBeforeTernaryOperators: false
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeComma
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: false
+ColumnLimit: 80
+CommentPragmas: '^ IWYU pragma:'
+CompactNamespaces: false
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+ConstructorInitializerIndentWidth: 8
+ContinuationIndentWidth: 8
+Cpp11BracedListStyle: false
+DerivePointerAlignment: false
+DisableFormat: false
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: false
+
+# Taken from:
+#   git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' \
+#   | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$,  - '\1'," \
+#   | LC_ALL=C sort -u
+ForEachMacros:
+  - 'cil_list_for_each'
+  - 'cil_stack_for_each'
+  - 'cil_stack_for_each_starting_at'
+  - 'ebitmap_for_each_bit'
+  - 'ebitmap_for_each_positive_bit'
+
+IncludeBlocks: Preserve
+IncludeCategories:
+  - Regex: '.*'
+    Priority: 1
+IncludeIsMainRegex: '(Test)?$'
+IndentCaseLabels: false
+IndentGotoLabels: false
+IndentPPDirectives: None
+IndentWidth: 8
+IndentWrappedFunctionNames: false
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: false
+MacroBlockBegin: ''
+MacroBlockEnd: ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Auto
+ObjCBlockIndentWidth: 8
+ObjCSpaceAfterProperty: true
+ObjCSpaceBeforeProtocolList: true
+
+# Taken from git's rules
+PenaltyBreakAssignment: 10
+PenaltyBreakBeforeFirstCallParameter: 30
+PenaltyBreakComment: 10
+PenaltyBreakFirstLessLess: 0
+PenaltyBreakString: 10
+PenaltyExcessCharacter: 100
+PenaltyReturnTypeOnItsOwnLine: 60
+
+PointerAlignment: Right
+ReflowComments: false
+SortIncludes: false
+SortUsingDeclarations: false
+SpaceAfterCStyleCast: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatementsExceptForEachMacros
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles: false
+SpacesInContainerLiterals: false
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard: Cpp03
+TabWidth: 8
+UseTab: Always
+...
CONTRIBUTING.md +2 -0
index c501cf84..7ec8cb0f 100644
@@ -53,6 +53,8 @@ When preparing patches, please follow these guidelines:
 -   Separate large patches into logical patches
 -   Patch descriptions must end with your "Signed-off-by" line. This means your
     code meets the Developer's certificate of origin, see below.
+-   C code should be formatted using clang-format, using the .clang-format
+    configuration file at the root of this repository.
 
 When adding new, large features or tools it is best to discuss the
 design on the mailing list prior to submitting the patch.
Signature · 1 line
2.50.1
maintainer
#2 lore raw
Quoted 10 lines · Rahul Sandhu

Currently only an RFC.

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

Quoted 3 lines

.clang-format is also read by various editors and tooling for writing C code. It may also be worth adding an editorconfig file in the future as well?

Can't hurt.

Quoted 2 lines

It may well also be worth adding a format target to the Makefiles, is that something that would be useful?

IMHO, yes.

Quoted 6 lines

A few other things to consider to do in the future:
1. Reformat all existing code. I understand this is a big change, hence the RFC, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.

Not opposed but will defer to the distro package maintainers on when they want to apply such a change since it makes back-porting future patches more painful for them.

Quoted 3 lines

2. Possibly add a CI target to check that all code is formatted as per the new clang-format configuration? The options --dry-run as well as -Werror can be passed to clang-format for this.

Sounds reasonable too.

Quoted 2 lines

Comments/feedback appreciated, thanks.

No strong opinions; would be fine with any standardized coding style. For comparison, selinux-testsuite has tools/check-syntax which can be used to check or fix coding style problems for C and perl code, but I'd be fine with using clang-format here as in the kernel.

Quoted 162 lines
---
 .clang-format   | 130 ++++++++++++++++++++++++++++++++++++++++++++++++
 CONTRIBUTING.md |   2 +
 2 files changed, 132 insertions(+)
 create mode 100644 .clang-format

v2: remove linux kernel ForEachMacros and replace them with ours

.clang-format new file +130 -0
new file mode 100644
index 00000000..35595d87
@@ -0,0 +1,130 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# clang-format configuration file. Intended for clang-format >= 11.
+#
+# For more information, see:
+#
+#   Documentation/dev-tools/clang-format.rst
+#   https://clang.llvm.org/docs/ClangFormat.html
+#   https://clang.llvm.org/docs/ClangFormatStyleOptions.html
+#
+---
+AccessModifierOffset: -4
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlines: Left
+AlignOperands: true
+AlignTrailingComments: false
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: false
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:
+  AfterClass: false
+  AfterControlStatement: false
+  AfterEnum: false
+  AfterFunction: true
+  AfterNamespace: true
+  AfterObjCDeclaration: false
+  AfterStruct: false
+  AfterUnion: false
+  AfterExternBlock: false
+  BeforeCatch: false
+  BeforeElse: false
+  IndentBraces: false
+  SplitEmptyFunction: true
+  SplitEmptyRecord: true
+  SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Custom
+BreakBeforeInheritanceComma: false
+BreakBeforeTernaryOperators: false
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeComma
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: false
+ColumnLimit: 80
+CommentPragmas: '^ IWYU pragma:'
+CompactNamespaces: false
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+ConstructorInitializerIndentWidth: 8
+ContinuationIndentWidth: 8
+Cpp11BracedListStyle: false
+DerivePointerAlignment: false
+DisableFormat: false
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: false
+
+# Taken from:
+#   git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' \
+#   | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$,  - '\1'," \
+#   | LC_ALL=C sort -u
+ForEachMacros:
+  - 'cil_list_for_each'
+  - 'cil_stack_for_each'
+  - 'cil_stack_for_each_starting_at'
+  - 'ebitmap_for_each_bit'
+  - 'ebitmap_for_each_positive_bit'
+
+IncludeBlocks: Preserve
+IncludeCategories:
+  - Regex: '.*'
+    Priority: 1
+IncludeIsMainRegex: '(Test)?$'
+IndentCaseLabels: false
+IndentGotoLabels: false
+IndentPPDirectives: None
+IndentWidth: 8
+IndentWrappedFunctionNames: false
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: false
+MacroBlockBegin: ''
+MacroBlockEnd: ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Auto
+ObjCBlockIndentWidth: 8
+ObjCSpaceAfterProperty: true
+ObjCSpaceBeforeProtocolList: true
+
+# Taken from git's rules
+PenaltyBreakAssignment: 10
+PenaltyBreakBeforeFirstCallParameter: 30
+PenaltyBreakComment: 10
+PenaltyBreakFirstLessLess: 0
+PenaltyBreakString: 10
+PenaltyExcessCharacter: 100
+PenaltyReturnTypeOnItsOwnLine: 60
+
+PointerAlignment: Right
+ReflowComments: false
+SortIncludes: false
+SortUsingDeclarations: false
+SpaceAfterCStyleCast: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatementsExceptForEachMacros
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles: false
+SpacesInContainerLiterals: false
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard: Cpp03
+TabWidth: 8
+UseTab: Always
+...
CONTRIBUTING.md +2 -1
index c501cf84..7ec8cb0f 100644
@@ -53,6 +53,8 @@ When preparing patches, please follow these guidelines:
 -   Separate large patches into logical patches
 -   Patch descriptions must end with your "Signed-off-by" line. This means your
     code meets the Developer's certificate of origin, see below.
+-   C code should be formatted using clang-format, using the .clang-format
+    configuration file at the root of this repository.
 
 When adding new, large features or tools it is best to discuss the
 design on the mailing list prior to submitting the patch.
--

2.50.1

#3 lore raw
Quoted 29 lines · Stephen Smalley
Quoted 10 lines · Rahul Sandhu

Currently only an RFC.

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

Quoted 3 lines

.clang-format is also read by various editors and tooling for writing C code. It may also be worth adding an editorconfig file in the future as well?

Can't hurt.

Quoted 2 lines

It may well also be worth adding a format target to the Makefiles, is that something that would be useful?

IMHO, yes.

Quoted 6 lines

A few other things to consider to do in the future:
1. Reformat all existing code. I understand this is a big change, hence the RFC, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.

Quoted 3 lines

Not opposed but will defer to the distro package maintainers on when they want to apply such a change since it makes back-porting future patches more painful for them.

As a Fedora and RHEL maintainer, this might be annoying in the short term, but I see it as an improvement in the long term.

If it's well-documented, with configuration files and a helper script for formatting/checking available, and CI is set up, I would not be opposed to this.

Quoted 176 lines
Quoted 3 lines

2. Possibly add a CI target to check that all code is formatted as per the new clang-format configuration? The options --dry-run as well as -Werror can be passed to clang-format for this.

Sounds reasonable too.

Quoted 2 lines

Comments/feedback appreciated, thanks.

No strong opinions; would be fine with any standardized coding style. For comparison, selinux-testsuite has tools/check-syntax which can be used to check or fix coding style problems for C and perl code, but I'd be fine with using clang-format here as in the kernel.

Quoted 162 lines
---
 .clang-format   | 130 ++++++++++++++++++++++++++++++++++++++++++++++++
 CONTRIBUTING.md |   2 +
 2 files changed, 132 insertions(+)
 create mode 100644 .clang-format

v2: remove linux kernel ForEachMacros and replace them with ours

.clang-format new file +130 -0
new file mode 100644
index 00000000..35595d87
@@ -0,0 +1,130 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# clang-format configuration file. Intended for clang-format >= 11.
+#
+# For more information, see:
+#
+#   Documentation/dev-tools/clang-format.rst
+#   https://clang.llvm.org/docs/ClangFormat.html
+#   https://clang.llvm.org/docs/ClangFormatStyleOptions.html
+#
+---
+AccessModifierOffset: -4
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlines: Left
+AlignOperands: true
+AlignTrailingComments: false
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: false
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:
+  AfterClass: false
+  AfterControlStatement: false
+  AfterEnum: false
+  AfterFunction: true
+  AfterNamespace: true
+  AfterObjCDeclaration: false
+  AfterStruct: false
+  AfterUnion: false
+  AfterExternBlock: false
+  BeforeCatch: false
+  BeforeElse: false
+  IndentBraces: false
+  SplitEmptyFunction: true
+  SplitEmptyRecord: true
+  SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Custom
+BreakBeforeInheritanceComma: false
+BreakBeforeTernaryOperators: false
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeComma
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: false
+ColumnLimit: 80
+CommentPragmas: '^ IWYU pragma:'
+CompactNamespaces: false
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+ConstructorInitializerIndentWidth: 8
+ContinuationIndentWidth: 8
+Cpp11BracedListStyle: false
+DerivePointerAlignment: false
+DisableFormat: false
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: false
+
+# Taken from:
+#   git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' \
+#   | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$,  - '\1'," \
+#   | LC_ALL=C sort -u
+ForEachMacros:
+  - 'cil_list_for_each'
+  - 'cil_stack_for_each'
+  - 'cil_stack_for_each_starting_at'
+  - 'ebitmap_for_each_bit'
+  - 'ebitmap_for_each_positive_bit'
+
+IncludeBlocks: Preserve
+IncludeCategories:
+  - Regex: '.*'
+    Priority: 1
+IncludeIsMainRegex: '(Test)?$'
+IndentCaseLabels: false
+IndentGotoLabels: false
+IndentPPDirectives: None
+IndentWidth: 8
+IndentWrappedFunctionNames: false
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: false
+MacroBlockBegin: ''
+MacroBlockEnd: ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Auto
+ObjCBlockIndentWidth: 8
+ObjCSpaceAfterProperty: true
+ObjCSpaceBeforeProtocolList: true
+
+# Taken from git's rules
+PenaltyBreakAssignment: 10
+PenaltyBreakBeforeFirstCallParameter: 30
+PenaltyBreakComment: 10
+PenaltyBreakFirstLessLess: 0
+PenaltyBreakString: 10
+PenaltyExcessCharacter: 100
+PenaltyReturnTypeOnItsOwnLine: 60
+
+PointerAlignment: Right
+ReflowComments: false
+SortIncludes: false
+SortUsingDeclarations: false
+SpaceAfterCStyleCast: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatementsExceptForEachMacros
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles: false
+SpacesInContainerLiterals: false
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard: Cpp03
+TabWidth: 8
+UseTab: Always
+...
CONTRIBUTING.md +2 -1
index c501cf84..7ec8cb0f 100644
@@ -53,6 +53,8 @@ When preparing patches, please follow these guidelines:
 -   Separate large patches into logical patches
 -   Patch descriptions must end with your "Signed-off-by" line. This means your
     code meets the Developer's certificate of origin, see below.
+-   C code should be formatted using clang-format, using the .clang-format
+    configuration file at the root of this repository.
 
 When adding new, large features or tools it is best to discuss the
 design on the mailing list prior to submitting the patch.
--

2.50.1

#4 lore raw

PATCH v3 1/1 treewide: add .clang-format configuration file

Currently only an RFC.

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

As well as this, replace all the existing indent targets with format targets. Commands used to find and replace those targets:

git grep -l -E '(\.\./)*scripts/Lindent' | xargs sed -i -E 's@(\.\./)*scripts/Lindent@clang-format -i@g' git grep -l 'indent' -- '*Makefile' | xargs sed -i 's/indent/format/g'

Also add some empty format targets to Makefiles that previously were missing an indent target so that make format does not error.

A few other things to consider to do in the future:
1. Reformat all existing code. I understand this is a big change, hence the RFC, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.
2. Possibly add a CI target to check that all code is formatted as per the new clang-format configuration? The options --dry-run as well as -Werror can be passed to clang-format for this.

Comments/feedback appreciated, thanks.

---
 .clang-format                            | 130 +++++++++++++++++++++++
 CONTRIBUTING.md                          |   2 +
 Makefile                                 |   2 +-
 checkpolicy/Makefile                     |   4 +-
 dbus/Makefile                            |   2 +
 gui/Makefile                             |   2 +-
 libselinux/Makefile                      |   2 +-
 libselinux/include/Makefile              |   4 +-
 libselinux/man/Makefile                  |   2 +-
 libselinux/src/Makefile                  |   4 +-
 libselinux/utils/Makefile                |   4 +-
 libsemanage/Makefile                     |   2 +-
 libsemanage/include/Makefile             |   4 +-
 libsemanage/src/Makefile                 |   4 +-
 libsemanage/utils/Makefile               |   2 +-
 libsepol/Makefile                        |   2 +-
 libsepol/include/Makefile                |   4 +-
 libsepol/src/Makefile                    |   4 +-
 libsepol/utils/Makefile                  |   4 +-
 mcstrans/Makefile                        |   2 +
 policycoreutils/Makefile                 |   2 +-
 policycoreutils/hll/Makefile             |   2 +-
 policycoreutils/hll/pp/Makefile          |   4 +-
 policycoreutils/load_policy/Makefile     |   4 +-
 policycoreutils/man/Makefile             |   2 +
 policycoreutils/newrole/Makefile         |   4 +-
 policycoreutils/po/Makefile              |   2 +
 policycoreutils/run_init/Makefile        |   4 +-
 policycoreutils/scripts/Makefile         |   2 +-
 policycoreutils/secon/Makefile           |   4 +-
 policycoreutils/semodule/Makefile        |   4 +-
 policycoreutils/sestatus/Makefile        |   4 +-
 policycoreutils/setfiles/Makefile        |   4 +-
 policycoreutils/setsebool/Makefile       |   4 +-
 policycoreutils/unsetfiles/Makefile      |   4 +-
 python/Makefile                          |   2 +-
 python/audit2allow/Makefile              |   4 +-
 python/chcat/Makefile                    |   2 +-
 python/po/Makefile                       |   2 +
 python/semanage/Makefile                 |   2 +-
 python/sepolgen/Makefile                 |   2 +-
 python/sepolgen/src/Makefile             |   2 +-
 python/sepolicy/Makefile                 |   2 +
 restorecond/Makefile                     |   4 +-
 sandbox/Makefile                         |   4 +-
 scripts/Lindent                          |  18 ----
 secilc/Makefile                          |   2 +
 semodule-utils/Makefile                  |   2 +-
 semodule-utils/semodule_expand/Makefile  |   4 +-
 semodule-utils/semodule_link/Makefile    |   4 +-
 semodule-utils/semodule_package/Makefile |   4 +-
 51 files changed, 212 insertions(+), 84 deletions(-)
 create mode 100644 .clang-format
 delete mode 100755 scripts/Lindent

v2: remove linux kernel ForEachMacros and replace them with ours
v3: replace the indent target with the new format target. also remove any
mention of .editorconfig from the commit message; those changes are better suited for another patch (they're small and self-contained enough that they're a smaller thing to merge).

.clang-format new file +130 -0
new file mode 100644
index 00000000..35595d87
@@ -0,0 +1,130 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# clang-format configuration file. Intended for clang-format >= 11.
+#
+# For more information, see:
+#
+#   Documentation/dev-tools/clang-format.rst
+#   https://clang.llvm.org/docs/ClangFormat.html
+#   https://clang.llvm.org/docs/ClangFormatStyleOptions.html
+#
+---
+AccessModifierOffset: -4
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlines: Left
+AlignOperands: true
+AlignTrailingComments: false
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: false
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:
+  AfterClass: false
+  AfterControlStatement: false
+  AfterEnum: false
+  AfterFunction: true
+  AfterNamespace: true
+  AfterObjCDeclaration: false
+  AfterStruct: false
+  AfterUnion: false
+  AfterExternBlock: false
+  BeforeCatch: false
+  BeforeElse: false
+  IndentBraces: false
+  SplitEmptyFunction: true
+  SplitEmptyRecord: true
+  SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Custom
+BreakBeforeInheritanceComma: false
+BreakBeforeTernaryOperators: false
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeComma
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: false
+ColumnLimit: 80
+CommentPragmas: '^ IWYU pragma:'
+CompactNamespaces: false
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+ConstructorInitializerIndentWidth: 8
+ContinuationIndentWidth: 8
+Cpp11BracedListStyle: false
+DerivePointerAlignment: false
+DisableFormat: false
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: false
+
+# Taken from:
+#   git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' \
+#   | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$,  - '\1'," \
+#   | LC_ALL=C sort -u
+ForEachMacros:
+  - 'cil_list_for_each'
+  - 'cil_stack_for_each'
+  - 'cil_stack_for_each_starting_at'
+  - 'ebitmap_for_each_bit'
+  - 'ebitmap_for_each_positive_bit'
+
+IncludeBlocks: Preserve
+IncludeCategories:
+  - Regex: '.*'
+    Priority: 1
+IncludeIsMainRegex: '(Test)?$'
+IndentCaseLabels: false
+IndentGotoLabels: false
+IndentPPDirectives: None
+IndentWidth: 8
+IndentWrappedFunctionNames: false
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: false
+MacroBlockBegin: ''
+MacroBlockEnd: ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Auto
+ObjCBlockIndentWidth: 8
+ObjCSpaceAfterProperty: true
+ObjCSpaceBeforeProtocolList: true
+
+# Taken from git's rules
+PenaltyBreakAssignment: 10
+PenaltyBreakBeforeFirstCallParameter: 30
+PenaltyBreakComment: 10
+PenaltyBreakFirstLessLess: 0
+PenaltyBreakString: 10
+PenaltyExcessCharacter: 100
+PenaltyReturnTypeOnItsOwnLine: 60
+
+PointerAlignment: Right
+ReflowComments: false
+SortIncludes: false
+SortUsingDeclarations: false
+SpaceAfterCStyleCast: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatementsExceptForEachMacros
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles: false
+SpacesInContainerLiterals: false
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard: Cpp03
+TabWidth: 8
+UseTab: Always
+...
CONTRIBUTING.md +2 -0
index c501cf84..7ec8cb0f 100644
@@ -53,6 +53,8 @@ When preparing patches, please follow these guidelines:
 -   Separate large patches into logical patches
 -   Patch descriptions must end with your "Signed-off-by" line. This means your
     code meets the Developer's certificate of origin, see below.
+-   C code should be formatted using clang-format, using the .clang-format
+    configuration file at the root of this repository.
 
 When adding new, large features or tools it is best to discuss the
 design on the mailing list prior to submitting the patch.
Makefile +1 -1
index 2ffba8e9..db065aa4 100644
@@ -36,7 +36,7 @@ ifneq ($(DESTDIR),)
 	export LIBSEPOLA
 endif
 
-all install relabel clean test indent:
+all install relabel clean test format:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
checkpolicy/Makefile +2 -2
index 6e8008e3..edc5a30e 100644
@@ -78,5 +78,5 @@ clean:
 	-rm -f $(TARGETS) $(CHECKPOLOBJS) $(CHECKMODOBJS) y.tab.c y.tab.h lex.yy.c tests/testpol.conf tests/testpol.bin
 	$(MAKE) -C test clean
 
-indent:
-	../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
+format:
+	clang-format -i $(filter-out $(GENERATED),$(wildcard *.[ch]))
dbus/Makefile +2 -0
index 53143aff..a6d77d6e 100644
@@ -16,4 +16,6 @@ install:
 
 relabel:
 
+format:
+
 test:
gui/Makefile +1 -1
index b29610d4..9ffa3604 100644
@@ -60,7 +60,7 @@ install: all
 clean:
 	(cd po && $(MAKE) $@)
 
-indent:
+format:
 
 relabel:
 
libselinux/Makefile +1 -1
index a50b6491..e863e2e5 100644
@@ -50,7 +50,7 @@ COMPILER := clang
 endif
 export COMPILER
 
-all install relabel clean distclean indent:
+all install relabel clean distclean format:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
libselinux/include/Makefile +2 -2
index 8ab9ce92..0ff28d1d 100644
@@ -10,8 +10,8 @@ install: all
 
 relabel:
 
-indent:
-	../../scripts/Lindent $(wildcard selinux/*.h)
+format:
+	clang-format -i $(wildcard selinux/*.h)
 
 distclean clean:
 	-rm -f selinux/*~
libselinux/man/Makefile +1 -1
index fdc0825f..42b1662a 100644
@@ -35,4 +35,4 @@ install: all
 
 relabel:
 
-indent distclean clean:
+format distclean clean:
libselinux/src/Makefile +2 -2
index 261c22d4..55520025 100644
@@ -223,7 +223,7 @@ clean: clean-pywrap clean-rubywrap
 distclean: clean
 	rm -f $(GENERATED) $(SWIGFILES)
 
-indent:
-	../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
+format:
+	clang-format -i $(filter-out $(GENERATED),$(wildcard *.[ch]))
 
 .PHONY: all clean clean-pywrap clean-rubywrap pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
libselinux/utils/Makefile +2 -2
index 6b1dc7c9..5213de8e 100644
@@ -72,8 +72,8 @@ clean:
 
 distclean: clean
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
 
 relabel:
 
libsemanage/Makefile +1 -1
index dbd240da..e363cdde 100644
@@ -29,7 +29,7 @@ clean distclean:
 	$(MAKE) -C src $@
 	$(MAKE) -C tests $@
 
-indent:
+format:
 	$(MAKE) -C src $@
 	$(MAKE) -C include $@
 
libsemanage/include/Makefile +2 -2
index 6e44a28a..b2c195c5 100644
@@ -8,5 +8,5 @@ install: all
 	test -d $(DESTDIR)$(INCDIR) || install -m 755 -d $(DESTDIR)$(INCDIR)
 	install -m 644 $(wildcard semanage/*.h) $(DESTDIR)$(INCDIR)
 
-indent:
-	../../scripts/Lindent $(wildcard semanage/*.h)
+format:
+	clang-format -i $(wildcard semanage/*.h)
libsemanage/src/Makefile +2 -2
index fa3449fb..f2d8e634 100644
@@ -167,7 +167,7 @@ clean:
 distclean: clean
 	rm -f $(GENERATED) $(SWIGFILES)
 
-indent:
-	../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
+format:
+	clang-format -i $(filter-out $(GENERATED),$(wildcard *.[ch]))
 
 .PHONY: all clean pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
libsemanage/utils/Makefile +1 -1
index 5b8fbb6b..94d576d9 100644
@@ -13,7 +13,7 @@ clean:
 
 distclean: clean
 
-indent:
+format:
 
 relabel:
 
libsepol/Makefile +1 -1
index 5b5d03e2..2af93ace 100644
@@ -20,7 +20,7 @@ clean:
 	$(MAKE) -C utils clean
 	$(MAKE) -C tests clean
 
-indent:
+format:
 	$(MAKE) -C src $@
 	$(MAKE) -C include $@
 	$(MAKE) -C utils $@
libsepol/include/Makefile +2 -2
index 1ad4ecab..fa8a96d6 100644
@@ -13,5 +13,5 @@ install: all
 	install -m 644 $(wildcard sepol/policydb/*.h) $(DESTDIR)$(INCDIR)/policydb
 	install -m 644 $(wildcard $(CILDIR)/include/cil/*.h) $(DESTDIR)$(INCDIR)/cil
 
-indent:
-	../../scripts/Lindent $(wildcard sepol/*.h)
+format:
+	clang-format -i $(wildcard sepol/*.h)
libsepol/src/Makefile +2 -2
index a1aed072..86404f4b 100644
@@ -104,6 +104,6 @@ relabel:
 clean: 
 	-rm -f $(LIBPC) $(LIBMAP) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(TARGET) $(CIL_GENERATED)
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
 
libsepol/utils/Makefile +2 -2
index 31932c11..0824f64f 100644
@@ -18,8 +18,8 @@ install: all
 clean:
 	-rm -f $(TARGETS) *.o 
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
 
 relabel:
 
mcstrans/Makefile +2 -0
index b20279ab..28d8c7bc 100644
@@ -21,4 +21,6 @@ clean:
 
 relabel:
 
+format:
+
 test:
policycoreutils/Makefile +1 -1
index 7c9706e3..212fbf22 100644
@@ -6,7 +6,7 @@ LIBSELINUX_LDLIBS := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH):../libselinux/s
 LIBSEMANAGE_LDLIBS := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH):../libsemanage/src" $(PKG_CONFIG) --libs libsemanage)
 export LIBSELINUX_LDLIBS LIBSEMANAGE_LDLIBS
 
-all install relabel clean indent:
+all install relabel clean format:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
policycoreutils/hll/Makefile +1 -1
index fe720e57..fc3f54db 100644
@@ -1,6 +1,6 @@
 SUBDIRS = pp
 
-all install relabel clean indent:
+all install relabel clean format:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
policycoreutils/hll/pp/Makefile +2 -2
index 3c9b0efc..2aed5ffa 100644
@@ -26,5 +26,5 @@ relabel:
 clean:
 	-rm -f pp $(PP_OBJS)
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
policycoreutils/load_policy/Makefile +2 -2
index c61fee70..0359719e 100644
@@ -28,8 +28,8 @@ install: all
 clean:
 	-rm -f $(TARGETS) *.o 
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
 
 relabel:
 	/sbin/restorecon $(DESTDIR)$(SBINDIR)/load_policy 
policycoreutils/man/Makefile +2 -0
index a4539f24..1f4ae9c7 100644
@@ -19,3 +19,5 @@ install: all
 	done
 
 relabel:
+
+format:
policycoreutils/newrole/Makefile +2 -2
index df64483f..6081da3f 100644
@@ -87,8 +87,8 @@ endif
 clean:
 	rm -f newrole *.o 
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
 
 relabel: install
 	/sbin/restorecon $(DESTDIR)$(BINDIR)/newrole
policycoreutils/po/Makefile +2 -0
index 5ff92bff..14e711db 100644
@@ -81,4 +81,6 @@ report:
 
 relabel:
 
+format:
+
 test:
policycoreutils/run_init/Makefile +2 -2
index c89fffb9..f22e6c24 100644
@@ -53,8 +53,8 @@ endif
 clean:
 	-rm -f $(TARGETS) *.o 
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
 
 relabel: install
 	/sbin/restorecon $(DESTDIR)$(SBINDIR)/run_init $(DESTDIR)$(SBINDIR)/open_init_pty
policycoreutils/scripts/Makefile +1 -1
index 6d8196c6..01957b5e 100644
@@ -21,6 +21,6 @@ install: all
 
 clean:
 
-indent:
+format:
 
 relabel:
policycoreutils/secon/Makefile +2 -2
index c9296f19..ac978a1d 100644
@@ -35,8 +35,8 @@ relabel:
 clean:
 	rm -f *.o core* secon *~ *.bak
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
 
 bare: clean
 
policycoreutils/semodule/Makefile +2 -2
index 018ee2ca..372e8647 100644
@@ -36,6 +36,6 @@ relabel:
 clean:
 	-rm -f semodule *.o genhomedircon
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
 
policycoreutils/sestatus/Makefile +2 -2
index 1ce617c0..0f3c2b83 100644
@@ -41,7 +41,7 @@ install: all
 clean:
 	rm -f sestatus *.o
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
 
 relabel:
policycoreutils/setfiles/Makefile +2 -2
index dab6313c..1029c4b7 100644
@@ -43,8 +43,8 @@ install: all
 clean:
 	rm -f setfiles restorecon restorecon_xattr *.o
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
 
 relabel: install
 	$(DESTDIR)$(SBINDIR)/restorecon $(DESTDIR)$(SBINDIR)/setfiles $(DESTDIR)$(SBINDIR)/restorecon_xattr
policycoreutils/setsebool/Makefile +2 -2
index 87494c55..5283b788 100644
@@ -36,6 +36,6 @@ relabel:
 clean:
 	-rm -f setsebool *.o
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
 
policycoreutils/unsetfiles/Makefile +2 -2
index 9cb51494..714fa4dd 100644
@@ -20,8 +20,8 @@ install: all
 clean:
 	-rm -f unsetfiles *.o
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
 
 relabel: install
 	/sbin/restorecon $(DESTDIR)$(SBINDIR)/unsetfiles
python/Makefile +1 -1
index 00312dbd..c4c31a17 100644
@@ -1,6 +1,6 @@
 SUBDIRS = sepolicy audit2allow semanage sepolgen chcat po
 
-all install relabel clean indent test:
+all install relabel clean format test:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
python/audit2allow/Makefile +2 -2
index 28b8f7f2..427007b6 100644
@@ -51,7 +51,7 @@ install: all
 clean:
 	rm -f *~ *.o sepolgen-ifgen-attr-helper test_dummy_policy
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
 
 relabel: ;
python/chcat/Makefile +1 -1
index 7b3ee17f..376c1978 100644
@@ -21,7 +21,7 @@ install: all
 
 clean:
 
-indent:
+format:
 
 relabel:
 
python/po/Makefile +2 -0
index 3ff9a47d..ca62356f 100644
@@ -82,4 +82,6 @@ report:
 
 relabel:
 
+format:
+
 test:
python/semanage/Makefile +1 -1
index 628d135a..54fa8146 100644
@@ -35,6 +35,6 @@ test:
 	@$(PYTHON) test-semanage.py -a
 clean:
 
-indent:
+format:
 
 relabel:
python/sepolgen/Makefile +1 -1
index 3aed3304..a4a3b99e 100644
@@ -11,7 +11,7 @@ clean:
 	rm -f *~ *.pyc
 	rm -f parser.out parsetab.py
 
-indent: ;
+format: ;
 
 test:
 	$(MAKE) -C tests $@
python/sepolgen/src/Makefile +1 -1
index 2815a13d..72e8b356 100644
@@ -12,7 +12,7 @@ clean:
 	rm -f *~ *.pyc
 	rm -f parser.out parsetab.py
 
-indent: ;
+format: ;
 
 
 test: ;
python/sepolicy/Makefile +2 -0
index 1a26cfdc..8147d423 100644
@@ -43,3 +43,5 @@ install:
 	install -m 644 $(BASHCOMPLETIONS) $(DESTDIR)$(BASHCOMPLETIONDIR)/sepolicy
 
 relabel:
+
+format:
restorecond/Makefile +2 -2
index b8b86eb4..573535c6 100644
@@ -59,7 +59,7 @@ relabel: install
 clean:
 	-rm -f restorecond *.o *~
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
 
 test:
sandbox/Makefile +2 -2
index 0799ab7c..2737e532 100644
@@ -50,7 +50,7 @@ clean:
 	-rm -f seunshare *.o *~
 	(cd po && $(MAKE) $@)
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
 
 relabel:
scripts/Lindent deleted +0 -18
deleted file mode 100755
index 9c4b3e2b..00000000
@@ -1,18 +0,0 @@
-#!/bin/sh
-PARAM="-npro -kr -i8 -ts8 -sob -l80 -ss -ncs -cp1"
-RES=`indent --version`
-V1=`echo $RES | cut -d' ' -f3 | cut -d'.' -f1`
-V2=`echo $RES | cut -d' ' -f3 | cut -d'.' -f2`
-V3=`echo $RES | cut -d' ' -f3 | cut -d'.' -f3`
-if [ $V1 -gt 2 ]; then
-  PARAM="$PARAM -il0"
-elif [ $V1 -eq 2 ]; then
-  if [ $V2 -gt 2 ]; then
-    PARAM="$PARAM -il0";
-  elif [ $V2 -eq 2 ]; then
-    if [ $V3 -ge 10 ]; then
-      PARAM="$PARAM -il0"
-    fi
-  fi
-fi
-indent $PARAM "$@"
secilc/Makefile +2 -0
index ef7bc8cd..2518933f 100644
@@ -87,4 +87,6 @@ clean:
 
 relabel:
 
+format:
+
 .PHONY: all clean test install doc relabel man
semodule-utils/Makefile +1 -1
index e0a65792..0ab7d0dc 100644
@@ -1,6 +1,6 @@
 SUBDIRS = semodule_package semodule_link semodule_expand
 
-all install relabel clean indent:
+all install relabel clean format:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
semodule-utils/semodule_expand/Makefile +2 -2
index ad776b15..1e5f836d 100644
@@ -28,6 +28,6 @@ relabel:
 clean:
 	-rm -f semodule_expand *.o
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
 
semodule-utils/semodule_link/Makefile +2 -2
index 936d161c..6c603b7c 100644
@@ -28,6 +28,6 @@ relabel:
 clean:
 	-rm -f semodule_link *.o
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
 
semodule-utils/semodule_package/Makefile +2 -2
index 6a289f73..c5823cc0 100644
@@ -30,6 +30,6 @@ relabel:
 clean:
 	-rm -f semodule_package semodule_unpackage *.o
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
 
Signature · 1 line
2.50.1
#5 lore raw
Quoted 890 lines · Rahul Sandhu

Currently only an RFC.

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

As well as this, replace all the existing indent targets with format targets. Commands used to find and replace those targets:

git grep -l -E '(\.\./)*scripts/Lindent' | xargs sed -i -E 's@(\.\./)*scripts/Lindent@clang-format -i@g' git grep -l 'indent' -- '*Makefile' | xargs sed -i 's/indent/format/g'

Also add some empty format targets to Makefiles that previously were missing an indent target so that make format does not error.

A few other things to consider to do in the future:
1. Reformat all existing code. I understand this is a big change, hence the RFC, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.
2. Possibly add a CI target to check that all code is formatted as per the new clang-format configuration? The options --dry-run as well as -Werror can be passed to clang-format for this.

Comments/feedback appreciated, thanks.

---

.clang-format                            | 130 +++++++++++++++++++++++
CONTRIBUTING.md                          |   2 +
Makefile                                 |   2 +-
checkpolicy/Makefile                     |   4 +-
dbus/Makefile                            |   2 +
gui/Makefile                             |   2 +-
libselinux/Makefile                      |   2 +-
libselinux/include/Makefile              |   4 +-
libselinux/man/Makefile                  |   2 +-
libselinux/src/Makefile                  |   4 +-
libselinux/utils/Makefile                |   4 +-
libsemanage/Makefile                     |   2 +-
libsemanage/include/Makefile             |   4 +-
libsemanage/src/Makefile                 |   4 +-
libsemanage/utils/Makefile               |   2 +-
libsepol/Makefile                        |   2 +-
libsepol/include/Makefile                |   4 +-
libsepol/src/Makefile                    |   4 +-
libsepol/utils/Makefile                  |   4 +-
mcstrans/Makefile                        |   2 +
policycoreutils/Makefile                 |   2 +-
policycoreutils/hll/Makefile             |   2 +-
policycoreutils/hll/pp/Makefile          |   4 +-
policycoreutils/load_policy/Makefile     |   4 +-
policycoreutils/man/Makefile             |   2 +
policycoreutils/newrole/Makefile         |   4 +-
policycoreutils/po/Makefile              |   2 +
policycoreutils/run_init/Makefile        |   4 +-
policycoreutils/scripts/Makefile         |   2 +-
policycoreutils/secon/Makefile           |   4 +-
policycoreutils/semodule/Makefile        |   4 +-
policycoreutils/sestatus/Makefile        |   4 +-
policycoreutils/setfiles/Makefile        |   4 +-
policycoreutils/setsebool/Makefile       |   4 +-
policycoreutils/unsetfiles/Makefile      |   4 +-
python/Makefile                          |   2 +-
python/audit2allow/Makefile              |   4 +-
python/chcat/Makefile                    |   2 +-
python/po/Makefile                       |   2 +
python/semanage/Makefile                 |   2 +-
python/sepolgen/Makefile                 |   2 +-
python/sepolgen/src/Makefile             |   2 +-
python/sepolicy/Makefile                 |   2 +
restorecond/Makefile                     |   4 +-
sandbox/Makefile                         |   4 +-
scripts/Lindent                          |  18 ----
secilc/Makefile                          |   2 +
semodule-utils/Makefile                  |   2 +-
semodule-utils/semodule_expand/Makefile  |   4 +-
semodule-utils/semodule_link/Makefile    |   4 +-
semodule-utils/semodule_package/Makefile |   4 +-
51 files changed, 212 insertions(+), 84 deletions(-)

create mode 100644 .clang-format
delete mode 100755 scripts/Lindent

v2: remove linux kernel ForEachMacros and replace them with ours
v3: replace the indent target with the new format target. also remove any
mention of .editorconfig from the commit message; those changes are better suited for another patch (they're small and self-contained enough that they're a smaller thing to merge).

.clang-format new file +130 -0
new file mode 100644
index 00000000..35595d87
@@ -0,0 +1,130 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# clang-format configuration file. Intended for clang-format >= 11.
+#
+# For more information, see:
+#
+#   Documentation/dev-tools/clang-format.rst
+#   https://clang.llvm.org/docs/ClangFormat.html
+#   https://clang.llvm.org/docs/ClangFormatStyleOptions.html
+#
+---
+AccessModifierOffset: -4
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlines: Left
+AlignOperands: true
+AlignTrailingComments: false
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: false
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:
+  AfterClass: false
+  AfterControlStatement: false
+  AfterEnum: false
+  AfterFunction: true
+  AfterNamespace: true
+  AfterObjCDeclaration: false
+  AfterStruct: false
+  AfterUnion: false
+  AfterExternBlock: false
+  BeforeCatch: false
+  BeforeElse: false
+  IndentBraces: false
+  SplitEmptyFunction: true
+  SplitEmptyRecord: true
+  SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Custom
+BreakBeforeInheritanceComma: false
+BreakBeforeTernaryOperators: false
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeComma
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: false
+ColumnLimit: 80
+CommentPragmas: '^ IWYU pragma:'
+CompactNamespaces: false
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+ConstructorInitializerIndentWidth: 8
+ContinuationIndentWidth: 8
+Cpp11BracedListStyle: false
+DerivePointerAlignment: false
+DisableFormat: false
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: false
+
+# Taken from:
+#   git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' \
+#   | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$,  - '\1'," \
+#   | LC_ALL=C sort -u
+ForEachMacros:
+  - 'cil_list_for_each'
+  - 'cil_stack_for_each'
+  - 'cil_stack_for_each_starting_at'
+  - 'ebitmap_for_each_bit'
+  - 'ebitmap_for_each_positive_bit'
+
+IncludeBlocks: Preserve
+IncludeCategories:
+  - Regex: '.*'
+    Priority: 1
+IncludeIsMainRegex: '(Test)?$'
+IndentCaseLabels: false
+IndentGotoLabels: false
+IndentPPDirectives: None
+IndentWidth: 8
+IndentWrappedFunctionNames: false
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: false
+MacroBlockBegin: ''
+MacroBlockEnd: ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Auto
+ObjCBlockIndentWidth: 8
+ObjCSpaceAfterProperty: true
+ObjCSpaceBeforeProtocolList: true
+
+# Taken from git's rules
+PenaltyBreakAssignment: 10
+PenaltyBreakBeforeFirstCallParameter: 30
+PenaltyBreakComment: 10
+PenaltyBreakFirstLessLess: 0
+PenaltyBreakString: 10
+PenaltyExcessCharacter: 100
+PenaltyReturnTypeOnItsOwnLine: 60
+
+PointerAlignment: Right
+ReflowComments: false
+SortIncludes: false
+SortUsingDeclarations: false
+SpaceAfterCStyleCast: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatementsExceptForEachMacros
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles: false
+SpacesInContainerLiterals: false
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard: Cpp03
+TabWidth: 8
+UseTab: Always
+...
CONTRIBUTING.md +2 -2
index c501cf84..7ec8cb0f 100644
@@ -53,6 +53,8 @@ When preparing patches, please follow these guidelines:
-   Separate large patches into logical patches
-   Patch descriptions must end with your "Signed-off-by" line. This means your
    code meets the Developer's certificate of origin, see below.
+-   C code should be formatted using clang-format, using the .clang-format
+    configuration file at the root of this repository.
 

When adding new, large features or tools it is best to discuss the design on the mailing list prior to submitting the patch.

Makefile +0 -0
index 2ffba8e9..db065aa4 100644
@@ -36,7 +36,7 @@ ifneq ($(DESTDIR),)
 	export LIBSEPOLA

endif

Makefile +1 -1
-all install relabel clean test indent:
+all install relabel clean test format:
	@for subdir in $(SUBDIRS); do \
		(cd $$subdir && $(MAKE) $@) || exit 1; \
	done
checkpolicy/Makefile +2 -2
index 6e8008e3..edc5a30e 100644
@@ -78,5 +78,5 @@ clean:
 	-rm -f $(TARGETS) $(CHECKPOLOBJS) $(CHECKMODOBJS) y.tab.c y.tab.h lex.yy.c tests/testpol.conf tests/testpol.bin
 	$(MAKE) -C test clean
 
-indent:
-	../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
+format:
+	clang-format -i $(filter-out $(GENERATED),$(wildcard *.[ch]))
dbus/Makefile +0 -0
index 53143aff..a6d77d6e 100644
@@ -16,4 +16,6 @@ install:
 

relabel:

dbus/Makefile +2 -0
+format:
+

test:

gui/Makefile +0 -0
index b29610d4..9ffa3604 100644
@@ -60,7 +60,7 @@ install: all

clean:

	(cd po && $(MAKE) $@)
gui/Makefile +1 -1
-indent:
+format:

relabel:

libselinux/Makefile +0 -0
index a50b6491..e863e2e5 100644
@@ -50,7 +50,7 @@ COMPILER := clang

endif
export COMPILER

libselinux/Makefile +1 -1
-all install relabel clean distclean indent:
+all install relabel clean distclean format:
	@for subdir in $(SUBDIRS); do \
		(cd $$subdir && $(MAKE) $@) || exit 1; \
	done
libselinux/include/Makefile +0 -0
index 8ab9ce92..0ff28d1d 100644
@@ -10,8 +10,8 @@ install: all
 

relabel:

libselinux/include/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(wildcard selinux/*.h)
+format:
+	clang-format -i $(wildcard selinux/*.h)

distclean clean:

	-rm -f selinux/*~
libselinux/man/Makefile +0 -0
index fdc0825f..42b1662a 100644
@@ -35,4 +35,4 @@ install: all
 

relabel:

libselinux/man/Makefile +1 -1
-indent distclean clean:
+format distclean clean:
libselinux/src/Makefile +0 -0
index 261c22d4..55520025 100644
@@ -223,7 +223,7 @@ clean: clean-pywrap clean-rubywrap

distclean: clean

	rm -f $(GENERATED) $(SWIGFILES)
libselinux/src/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
+format:
+	clang-format -i $(filter-out $(GENERATED),$(wildcard *.[ch]))

.PHONY: all clean clean-pywrap clean-rubywrap pywrap rubywrap swigify install install-pywrap install-rubywrap distclean

libselinux/utils/Makefile +0 -0
index 6b1dc7c9..5213de8e 100644
@@ -72,8 +72,8 @@ clean:
 

distclean: clean

libselinux/utils/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])

relabel:

libsemanage/Makefile +1 -1
index dbd240da..e363cdde 100644
@@ -29,7 +29,7 @@ clean distclean:
 	$(MAKE) -C src $@
 	$(MAKE) -C tests $@
 
-indent:
+format:
 	$(MAKE) -C src $@
 	$(MAKE) -C include $@
 
libsemanage/include/Makefile +2 -2
index 6e44a28a..b2c195c5 100644
@@ -8,5 +8,5 @@ install: all
 	test -d $(DESTDIR)$(INCDIR) || install -m 755 -d $(DESTDIR)$(INCDIR)
 	install -m 644 $(wildcard semanage/*.h) $(DESTDIR)$(INCDIR)
 
-indent:
-	../../scripts/Lindent $(wildcard semanage/*.h)
+format:
+	clang-format -i $(wildcard semanage/*.h)
libsemanage/src/Makefile +0 -0
index fa3449fb..f2d8e634 100644
@@ -167,7 +167,7 @@ clean:

distclean: clean

	rm -f $(GENERATED) $(SWIGFILES)
libsemanage/src/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
+format:
+	clang-format -i $(filter-out $(GENERATED),$(wildcard *.[ch]))

.PHONY: all clean pywrap rubywrap swigify install install-pywrap install-rubywrap distclean

libsemanage/utils/Makefile +0 -0
index 5b8fbb6b..94d576d9 100644
@@ -13,7 +13,7 @@ clean:
 

distclean: clean

libsemanage/utils/Makefile +1 -1
-indent:
+format:

relabel:

libsepol/Makefile +1 -1
index 5b5d03e2..2af93ace 100644
@@ -20,7 +20,7 @@ clean:
 	$(MAKE) -C utils clean
 	$(MAKE) -C tests clean
 
-indent:
+format:
 	$(MAKE) -C src $@
 	$(MAKE) -C include $@
 	$(MAKE) -C utils $@
libsepol/include/Makefile +2 -2
index 1ad4ecab..fa8a96d6 100644
@@ -13,5 +13,5 @@ install: all
 	install -m 644 $(wildcard sepol/policydb/*.h) $(DESTDIR)$(INCDIR)/policydb
 	install -m 644 $(wildcard $(CILDIR)/include/cil/*.h) $(DESTDIR)$(INCDIR)/cil
 
-indent:
-	../../scripts/Lindent $(wildcard sepol/*.h)
+format:
+	clang-format -i $(wildcard sepol/*.h)
libsepol/src/Makefile +0 -0
index a1aed072..86404f4b 100644
@@ -104,6 +104,6 @@ relabel:

clean:

	-rm -f $(LIBPC) $(LIBMAP) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(TARGET) $(CIL_GENERATED)
libsepol/src/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
libsepol/utils/Makefile +0 -0
index 31932c11..0824f64f 100644
@@ -18,8 +18,8 @@ install: all

clean:

	-rm -f $(TARGETS) *.o 
libsepol/utils/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])

relabel:

mcstrans/Makefile +0 -0
index b20279ab..28d8c7bc 100644
@@ -21,4 +21,6 @@ clean:
 

relabel:

mcstrans/Makefile +2 -0
+format:
+

test:

policycoreutils/Makefile +0 -0
index 7c9706e3..212fbf22 100644
@@ -6,7 +6,7 @@ LIBSELINUX_LDLIBS := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH):../libselinux/s
 LIBSEMANAGE_LDLIBS := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH):../libsemanage/src" $(PKG_CONFIG) --libs libsemanage)

export LIBSELINUX_LDLIBS LIBSEMANAGE_LDLIBS

policycoreutils/Makefile +1 -1
-all install relabel clean indent:
+all install relabel clean format:
	@for subdir in $(SUBDIRS); do \
		(cd $$subdir && $(MAKE) $@) || exit 1; \
	done
policycoreutils/hll/Makefile +0 -0
index fe720e57..fc3f54db 100644
@@ -1,6 +1,6 @@

SUBDIRS = pp

policycoreutils/hll/Makefile +1 -1
-all install relabel clean indent:
+all install relabel clean format:
	@for subdir in $(SUBDIRS); do \
		(cd $$subdir && $(MAKE) $@) || exit 1; \
	done
policycoreutils/hll/pp/Makefile +0 -0
index 3c9b0efc..2aed5ffa 100644
@@ -26,5 +26,5 @@ relabel:

clean:

	-rm -f pp $(PP_OBJS)
policycoreutils/hll/pp/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
policycoreutils/load_policy/Makefile +0 -0
index c61fee70..0359719e 100644
@@ -28,8 +28,8 @@ install: all

clean:

	-rm -f $(TARGETS) *.o 
policycoreutils/load_policy/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])

relabel:

	/sbin/restorecon $(DESTDIR)$(SBINDIR)/load_policy 
policycoreutils/man/Makefile +0 -0
index a4539f24..1f4ae9c7 100644
@@ -19,3 +19,5 @@ install: all
 	done
 

relabel:

policycoreutils/man/Makefile +2 -0
+
+format:
policycoreutils/newrole/Makefile +0 -0
index df64483f..6081da3f 100644
@@ -87,8 +87,8 @@ endif

clean:
rm -f newrole *.o

policycoreutils/newrole/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])

relabel: install

	/sbin/restorecon $(DESTDIR)$(BINDIR)/newrole
policycoreutils/po/Makefile +0 -0
index 5ff92bff..14e711db 100644
@@ -81,4 +81,6 @@ report:
 

relabel:

policycoreutils/po/Makefile +2 -0
+format:
+

test:

policycoreutils/run_init/Makefile +0 -0
index c89fffb9..f22e6c24 100644
@@ -53,8 +53,8 @@ endif

clean:

	-rm -f $(TARGETS) *.o 
policycoreutils/run_init/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])

relabel: install

	/sbin/restorecon $(DESTDIR)$(SBINDIR)/run_init $(DESTDIR)$(SBINDIR)/open_init_pty
policycoreutils/scripts/Makefile +0 -0
index 6d8196c6..01957b5e 100644
@@ -21,6 +21,6 @@ install: all
 

clean:

policycoreutils/scripts/Makefile +1 -1
-indent:
+format:

relabel:

policycoreutils/secon/Makefile +0 -0
index c9296f19..ac978a1d 100644
@@ -35,8 +35,8 @@ relabel:

clean:

	rm -f *.o core* secon *~ *.bak
policycoreutils/secon/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])

bare: clean

policycoreutils/semodule/Makefile +0 -0
index 018ee2ca..372e8647 100644
@@ -36,6 +36,6 @@ relabel:

clean:
-rm -f semodule *.o genhomedircon

policycoreutils/semodule/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
policycoreutils/sestatus/Makefile +0 -0
index 1ce617c0..0f3c2b83 100644
@@ -41,7 +41,7 @@ install: all

clean:
rm -f sestatus *.o

policycoreutils/sestatus/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])

relabel:

policycoreutils/setfiles/Makefile +0 -0
index dab6313c..1029c4b7 100644
@@ -43,8 +43,8 @@ install: all

clean:
rm -f setfiles restorecon restorecon_xattr *.o

policycoreutils/setfiles/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])

relabel: install

	$(DESTDIR)$(SBINDIR)/restorecon $(DESTDIR)$(SBINDIR)/setfiles $(DESTDIR)$(SBINDIR)/restorecon_xattr
policycoreutils/setsebool/Makefile +0 -0
index 87494c55..5283b788 100644
@@ -36,6 +36,6 @@ relabel:

clean:
-rm -f setsebool *.o

policycoreutils/setsebool/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
policycoreutils/unsetfiles/Makefile +0 -0
index 9cb51494..714fa4dd 100644
@@ -20,8 +20,8 @@ install: all

clean:
-rm -f unsetfiles *.o

policycoreutils/unsetfiles/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])

relabel: install

	/sbin/restorecon $(DESTDIR)$(SBINDIR)/unsetfiles
python/Makefile +0 -0
index 00312dbd..c4c31a17 100644
@@ -1,6 +1,6 @@

SUBDIRS = sepolicy audit2allow semanage sepolgen chcat po

python/Makefile +1 -1
-all install relabel clean indent test:
+all install relabel clean format test:
	@for subdir in $(SUBDIRS); do \
		(cd $$subdir && $(MAKE) $@) || exit 1; \
	done
python/audit2allow/Makefile +0 -0
index 28b8f7f2..427007b6 100644
@@ -51,7 +51,7 @@ install: all

clean:
rm -f *~ *.o sepolgen-ifgen-attr-helper test_dummy_policy

python/audit2allow/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])

relabel: ;

python/chcat/Makefile +0 -0
index 7b3ee17f..376c1978 100644
@@ -21,7 +21,7 @@ install: all
 

clean:

python/chcat/Makefile +1 -1
-indent:
+format:

relabel:

python/po/Makefile +0 -0
index 3ff9a47d..ca62356f 100644
@@ -82,4 +82,6 @@ report:
 

relabel:

python/po/Makefile +2 -0
+format:
+

test:

python/semanage/Makefile +0 -0
index 628d135a..54fa8146 100644
@@ -35,6 +35,6 @@ test:
 	@$(PYTHON) test-semanage.py -a

clean:

python/semanage/Makefile +1 -1
-indent:
+format:

relabel:

python/sepolgen/Makefile +1 -1
index 3aed3304..a4a3b99e 100644
@@ -11,7 +11,7 @@ clean:
 	rm -f *~ *.pyc
 	rm -f parser.out parsetab.py
 
-indent: ;
+format: ;
 

test:

	$(MAKE) -C tests $@
python/sepolgen/src/Makefile +1 -1
index 2815a13d..72e8b356 100644
@@ -12,7 +12,7 @@ clean:
 	rm -f *~ *.pyc
 	rm -f parser.out parsetab.py
 
-indent: ;
+format: ;
 
 
 test: ;
python/sepolicy/Makefile +0 -0
index 1a26cfdc..8147d423 100644
@@ -43,3 +43,5 @@ install:
 	install -m 644 $(BASHCOMPLETIONS) $(DESTDIR)$(BASHCOMPLETIONDIR)/sepolicy
 

relabel:

python/sepolicy/Makefile +2 -0
+
+format:
restorecond/Makefile +0 -0
index b8b86eb4..573535c6 100644
@@ -59,7 +59,7 @@ relabel: install

clean:
-rm -f restorecond *.o *~

restorecond/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])

test:

sandbox/Makefile +2 -2
index 0799ab7c..2737e532 100644
@@ -50,7 +50,7 @@ clean:
 	-rm -f seunshare *.o *~
 	(cd po && $(MAKE) $@)
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
 

relabel:

scripts/Lindent deleted +0 -18
deleted file mode 100755
index 9c4b3e2b..00000000
@@ -1,18 +0,0 @@
-#!/bin/sh
-PARAM="-npro -kr -i8 -ts8 -sob -l80 -ss -ncs -cp1"
-RES=`indent --version`
-V1=`echo $RES | cut -d' ' -f3 | cut -d'.' -f1`
-V2=`echo $RES | cut -d' ' -f3 | cut -d'.' -f2`
-V3=`echo $RES | cut -d' ' -f3 | cut -d'.' -f3`
-if [ $V1 -gt 2 ]; then
-  PARAM="$PARAM -il0"
-elif [ $V1 -eq 2 ]; then
-  if [ $V2 -gt 2 ]; then
-    PARAM="$PARAM -il0";
-  elif [ $V2 -eq 2 ]; then
-    if [ $V3 -ge 10 ]; then
-      PARAM="$PARAM -il0"
-    fi
-  fi
-fi
-indent $PARAM "$@"
secilc/Makefile +0 -0
index ef7bc8cd..2518933f 100644
@@ -87,4 +87,6 @@ clean:
 

relabel:

secilc/Makefile +2 -0
+format:
+

.PHONY: all clean test install doc relabel man

semodule-utils/Makefile +0 -0
index e0a65792..0ab7d0dc 100644
@@ -1,6 +1,6 @@

SUBDIRS = semodule_package semodule_link semodule_expand

semodule-utils/Makefile +1 -1
-all install relabel clean indent:
+all install relabel clean format:
	@for subdir in $(SUBDIRS); do \
		(cd $$subdir && $(MAKE) $@) || exit 1; \
	done
semodule-utils/semodule_expand/Makefile +0 -0
index ad776b15..1e5f836d 100644
@@ -28,6 +28,6 @@ relabel:

clean:
-rm -f semodule_expand *.o

semodule-utils/semodule_expand/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
semodule-utils/semodule_link/Makefile +0 -0
index 936d161c..6c603b7c 100644
@@ -28,6 +28,6 @@ relabel:

clean:
-rm -f semodule_link *.o

semodule-utils/semodule_link/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
semodule-utils/semodule_package/Makefile +0 -0
index 6a289f73..c5823cc0 100644
@@ -30,6 +30,6 @@ relabel:

clean:
-rm -f semodule_package semodule_unpackage *.o

semodule-utils/semodule_package/Makefile +2 -2
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
+format:
+	clang-format -i $(wildcard *.[ch])
Signature · 2 lines
-- 
2.50.1

Oops, I forgot to send this as an RFC, apologies - it still is one! I'll sent the next patch as an RFC again.

While we're still talking about code style/formatting, I think it also would be nice to add some clarification about usage of braces. Right now, the usage of bracing is somewhat mixed: there are many places where one line statements don't have braces, and vice versa. It would be good to decide what the style regarding braces would be here and add it to the CONTRIBUTING guide.

Regarding my own personal opinion, I think it would be best off to have braces surrounding all statements (if/for/while, etc). It adds consistency to all of those kinds of statements, and it also helps avoid bugs such as the infamous goto fail; vulnerability[1]. It's a fairly small trade off for a bit of added safety. However, maintainer opinions are of course very much welcome here.

Regards,
Rahul

maintainer
#6 lore raw
Quoted 45 lines · Rahul Sandhu
Quoted 28 lines · Rahul Sandhu

Currently only an RFC.

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

As well as this, replace all the existing indent targets with format targets. Commands used to find and replace those targets:

git grep -l -E '(\.\./)*scripts/Lindent' | xargs sed -i -E 's@(\.\./)*scripts/Lindent@clang-format -i@g' git grep -l 'indent' -- '*Makefile' | xargs sed -i 's/indent/format/g'

Also add some empty format targets to Makefiles that previously were missing an indent target so that make format does not error.

A few other things to consider to do in the future:
1. Reformat all existing code. I understand this is a big change, hence the RFC, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.
2. Possibly add a CI target to check that all code is formatted as per the new clang-format configuration? The options --dry-run as well as -Werror can be passed to clang-format for this.

Quoted 4 lines

Comments/feedback appreciated, thanks.

---

Oops, I forgot to send this as an RFC, apologies - it still is one! I'll sent the next patch as an RFC again.

While we're still talking about code style/formatting, I think it also would be nice to add some clarification about usage of braces. Right now, the usage of bracing is somewhat mixed: there are many places where one line statements don't have braces, and vice versa. It would be good to decide what the style regarding braces would be here and add it to the CONTRIBUTING guide.

Quoted 6 lines

Regarding my own personal opinion, I think it would be best off to have braces surrounding all statements (if/for/while, etc). It adds consistency to all of those kinds of statements, and it also helps avoid bugs such as the infamous goto fail; vulnerability[1]. It's a fairly small trade off for a bit of added safety. However, maintainer opinions are of course very much welcome here.

I would tend to be consistent with the kernel CodingStyle i.e. no { } for singe-line statements, but this is not a hill I will die on.

maintainer
#7 lore raw
In reply to Rahul Sandhu
Quoted 19 lines · Rahul Sandhu

Currently only an RFC.

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

As well as this, replace all the existing indent targets with format targets. Commands used to find and replace those targets:

git grep -l -E '(\.\./)*scripts/Lindent' | xargs sed -i -E 's@(\.\./)*scripts/Lindent@clang-format -i@g' git grep -l 'indent' -- '*Makefile' | xargs sed -i 's/indent/format/g'

Also add some empty format targets to Makefiles that previously were missing an indent target so that make format does not error.

Quoted 8 lines

A few other things to consider to do in the future:
1. Reformat all existing code. I understand this is a big change, hence the RFC, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.
2. Possibly add a CI target to check that all code is formatted as per the new clang-format configuration? The options --dry-run as well as -Werror can be passed to clang-format for this.

Yes please.

Quoted 19 lines

Comments/feedback appreciated, thanks.

---

v2: remove linux kernel ForEachMacros and replace them with ours
v3: replace the indent target with the new format target. also remove any
mention of .editorconfig from the commit message; those changes are better suited for another patch (they're small and self-contained enough that they're a smaller thing to merge).

mcstrans/Makefile +0 -0
index b20279ab..28d8c7bc 100644

mcstrans/Makefile clean: · line 25

Quoted 5 lines
mcstrans/Makefile +2 -0
@@ -21,4 +21,6 @@ clean:
 
 relabel:
 
+format:
+

We should add targets for mcstrans/utils/*.c and mcstrans/src/*.[hc]

secilc/Makefile clean: · line 91

Quoted 10 lines
secilc/Makefile +2 -0
index ef7bc8cd..2518933f 100644
@@ -87,4 +87,6 @@ clean:
 
 relabel:
 
+format:
+

Should add a target for secilc/*.c

#8 lore raw
Quoted 29 lines · Stephen Smalley
Quoted 19 lines · Rahul Sandhu

Currently only an RFC.

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

As well as this, replace all the existing indent targets with format targets. Commands used to find and replace those targets:

git grep -l -E '(\.\./)*scripts/Lindent' | xargs sed -i -E 's@(\.\./)*scripts/Lindent@clang-format -i@g' git grep -l 'indent' -- '*Makefile' | xargs sed -i 's/indent/format/g'

Also add some empty format targets to Makefiles that previously were missing an indent target so that make format does not error.

Quoted 8 lines

A few other things to consider to do in the future:
1. Reformat all existing code. I understand this is a big change, hence the RFC, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.
2. Possibly add a CI target to check that all code is formatted as per the new clang-format configuration? The options --dry-run as well as -Werror can be passed to clang-format for this.

Quoted 1 line

Yes please.

Should this be added in another patch? My concern is that CI will fail should this be merged without formatting all code.

Quoted 21 lines
Quoted 21 lines

Comments/feedback appreciated, thanks.

---

v2: remove linux kernel ForEachMacros and replace them with ours
v3: replace the indent target with the new format target. also remove any
mention of .editorconfig from the commit message; those changes are better suited for another patch (they're small and self-contained enough that they're a smaller thing to merge).

mcstrans/Makefile +0 -0
index b20279ab..28d8c7bc 100644
@@ -21,4 +21,6 @@ clean:
 
Quoted 6 lines

mcstrans/Makefile clean: · line 25

Quoted 4 lines
mcstrans/Makefile +2 -0
 relabel:
 
+format:
+

We should add targets for mcstrans/utils/*.c and mcstrans/src/*.[hc]

I wrote a simple check-format target in the Makefile:

# We shouldn't have any unformatted files in the repo without an explicit exception. # Given that this is used with the check-format target, which does not modify any

# source files, merely checking if they're formatted properly, it's fine to rely on
# find to get all C source and header files.
CHECK_FORMAT_SOURCE_FILES := $(shell find $(SUBDIRS) -type f \( -name '*.c' -o -name '*.h' \))

check-format:

	clang-format --dry-run -Werror $(CHECK_FORMAT_SOURCE_FILES)

However, after running make format and make check-format, I noticed a whole load of failures caused by a load of other missing targets. I'm thinking that it may just be better off using that same find command to get all c sources and headers, and have a single, global format target. Afterall, in pushed code after we've formatted it all, I don't see any reason that some code should be left unformatted (especially as we have things such as the clang-format off comments for specific sub-sections of code), and while working locally if someone wishes to format only a specific globbed set of files, something like this should work fine:
clang-format -i src/*.c

Thoughts? Suggestions?

Quoted 13 lines

secilc/Makefile clean: · line 91

Quoted 11 lines
secilc/Makefile +2 -0
index ef7bc8cd..2518933f 100644
@@ -87,4 +87,6 @@ clean:
 
 relabel:
 
+format:
+

Should add a target for secilc/*.c

maintainer
#9 lore raw
Quoted 31 lines · Rahul Sandhu
Quoted 29 lines · Stephen Smalley
Quoted 27 lines · Rahul Sandhu

Currently only an RFC.

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

As well as this, replace all the existing indent targets with format targets. Commands used to find and replace those targets:

git grep -l -E '(\.\./)*scripts/Lindent' | xargs sed -i -E 's@(\.\./)*scripts/Lindent@clang-format -i@g' git grep -l 'indent' -- '*Makefile' | xargs sed -i 's/indent/format/g'

Also add some empty format targets to Makefiles that previously were missing an indent target so that make format does not error.

A few other things to consider to do in the future:
1. Reformat all existing code. I understand this is a big change, hence the RFC, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.
2. Possibly add a CI target to check that all code is formatted as per the new clang-format configuration? The options --dry-run as well as -Werror can be passed to clang-format for this.

Quoted 4 lines
Quoted 1 line

Yes please.

Should this be added in another patch? My concern is that CI will fail should this be merged without formatting all code.

Yes, we can wait on it.

Quoted 35 lines
Quoted 6 lines
Quoted 6 lines
mcstrans/Makefile +0 -0
index b20279ab..28d8c7bc 100644
@@ -21,4 +21,6 @@ clean:
 
Quoted 6 lines

mcstrans/Makefile clean: · line 25

Quoted 4 lines
mcstrans/Makefile +2 -0
 relabel:
 
+format:
+

We should add targets for mcstrans/utils/*.c and mcstrans/src/*.[hc]

I wrote a simple check-format target in the Makefile:

# We shouldn't have any unformatted files in the repo without an explicit exception. # Given that this is used with the check-format target, which does not modify any

# source files, merely checking if they're formatted properly, it's fine to rely on
# find to get all C source and header files.
CHECK_FORMAT_SOURCE_FILES := $(shell find $(SUBDIRS) -type f \( -name '*.c' -o -name '*.h' \))

check-format:

        clang-format --dry-run -Werror $(CHECK_FORMAT_SOURCE_FILES)

However, after running make format and make check-format, I noticed a whole load of failures caused by a load of other missing targets. I'm thinking that it may just be better off using that same find command to get all c sources and headers, and have a single, global format target. Afterall, in pushed code after we've formatted it all, I don't see any reason that some code should be left unformatted (especially as we have things such as the clang-format off comments for specific sub-sections of code), and while working locally if someone wishes to format only a specific globbed set of files, something like this should work fine:
clang-format -i src/*.c

Quoted 1 line

Thoughts? Suggestions?

Only caveat is that the selinux userspace tree is released as separate archives for each packaged component so downstream users might not have the top-level Makefile.
But I'm fine either way.

#10 lore raw

RFC PATCH v4 1/1 treewide: add .clang-format configuration file

Currently only an RFC.

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

As well as this, replace all the existing indent targets with a single toplevel format target. Managing all the source files to be formatted is not pretty to maintain, and doesn't really give us much.

Also define a toplevel check-format target to verify that all code is formatted properly. This only becomes useful in the future once we have reformatted all existing code, but is wired up for now.

For the future:
1. Reformat all existing code. I understand this is a big change, hence the RFC, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.
2. Add a CI target to check that all code is formatted as per the new clang-format configuration. The check-format target can be used for this.

---
 .clang-format                            | 130 +++++++++++++++++++++++
 CONTRIBUTING.md                          |   2 +
 Makefile                                 |  11 +-
 checkpolicy/Makefile                     |   3 -
 gui/Makefile                             |   2 -
 libselinux/Makefile                      |   2 +-
 libselinux/include/Makefile              |   4 -
 libselinux/man/Makefile                  |   2 +-
 libselinux/src/Makefile                  |   3 -
 libselinux/utils/Makefile                |   4 -
 libsemanage/Makefile                     |   4 -
 libsemanage/include/Makefile             |   3 -
 libsemanage/src/Makefile                 |   3 -
 libsemanage/utils/Makefile               |   3 -
 libsepol/Makefile                        |   6 --
 libsepol/include/Makefile                |   3 -
 libsepol/src/Makefile                    |   4 -
 libsepol/utils/Makefile                  |   4 -
 mcstrans/src/Makefile                    |   1 -
 mcstrans/utils/Makefile                  |   1 -
 policycoreutils/Makefile                 |   2 +-
 policycoreutils/hll/Makefile             |   2 +-
 policycoreutils/hll/pp/Makefile          |   3 -
 policycoreutils/load_policy/Makefile     |   3 -
 policycoreutils/man/Makefile             |   2 -
 policycoreutils/newrole/Makefile         |   3 -
 policycoreutils/run_init/Makefile        |   3 -
 policycoreutils/scripts/Makefile         |   2 -
 policycoreutils/secon/Makefile           |   3 -
 policycoreutils/semodule/Makefile        |   4 -
 policycoreutils/sestatus/Makefile        |   3 -
 policycoreutils/setfiles/Makefile        |   3 -
 policycoreutils/setsebool/Makefile       |   4 -
 policycoreutils/unsetfiles/Makefile      |   3 -
 python/Makefile                          |   2 +-
 python/audit2allow/Makefile              |   3 -
 python/chcat/Makefile                    |   2 -
 python/semanage/Makefile                 |   2 -
 python/sepolgen/Makefile                 |   5 -
 python/sepolgen/src/Makefile             |   6 --
 restorecond/Makefile                     |   3 -
 sandbox/Makefile                         |   3 -
 scripts/Lindent                          |  18 ----
 semodule-utils/Makefile                  |   2 +-
 semodule-utils/semodule_expand/Makefile  |   4 -
 semodule-utils/semodule_link/Makefile    |   4 -
 semodule-utils/semodule_package/Makefile |   4 -
 47 files changed, 148 insertions(+), 145 deletions(-)
 create mode 100644 .clang-format
 delete mode 100755 scripts/Lindent

v2: remove linux kernel ForEachMacros and replace them with ours
v3: replace the indent target with the new format target. also remove any
mention of .editorconfig from the commit message; those changes are better suited for another patch (they're small and self-contained enough that they're a smaller thing to merge).
v4: drop the subdir format targets entirely, replacing with a single toplevel
format and check-format target.

.clang-format new file +130 -0
new file mode 100644
index 00000000..35595d87
@@ -0,0 +1,130 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# clang-format configuration file. Intended for clang-format >= 11.
+#
+# For more information, see:
+#
+#   Documentation/dev-tools/clang-format.rst
+#   https://clang.llvm.org/docs/ClangFormat.html
+#   https://clang.llvm.org/docs/ClangFormatStyleOptions.html
+#
+---
+AccessModifierOffset: -4
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlines: Left
+AlignOperands: true
+AlignTrailingComments: false
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: false
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:
+  AfterClass: false
+  AfterControlStatement: false
+  AfterEnum: false
+  AfterFunction: true
+  AfterNamespace: true
+  AfterObjCDeclaration: false
+  AfterStruct: false
+  AfterUnion: false
+  AfterExternBlock: false
+  BeforeCatch: false
+  BeforeElse: false
+  IndentBraces: false
+  SplitEmptyFunction: true
+  SplitEmptyRecord: true
+  SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Custom
+BreakBeforeInheritanceComma: false
+BreakBeforeTernaryOperators: false
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeComma
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: false
+ColumnLimit: 80
+CommentPragmas: '^ IWYU pragma:'
+CompactNamespaces: false
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+ConstructorInitializerIndentWidth: 8
+ContinuationIndentWidth: 8
+Cpp11BracedListStyle: false
+DerivePointerAlignment: false
+DisableFormat: false
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: false
+
+# Taken from:
+#   git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' \
+#   | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$,  - '\1'," \
+#   | LC_ALL=C sort -u
+ForEachMacros:
+  - 'cil_list_for_each'
+  - 'cil_stack_for_each'
+  - 'cil_stack_for_each_starting_at'
+  - 'ebitmap_for_each_bit'
+  - 'ebitmap_for_each_positive_bit'
+
+IncludeBlocks: Preserve
+IncludeCategories:
+  - Regex: '.*'
+    Priority: 1
+IncludeIsMainRegex: '(Test)?$'
+IndentCaseLabels: false
+IndentGotoLabels: false
+IndentPPDirectives: None
+IndentWidth: 8
+IndentWrappedFunctionNames: false
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: false
+MacroBlockBegin: ''
+MacroBlockEnd: ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Auto
+ObjCBlockIndentWidth: 8
+ObjCSpaceAfterProperty: true
+ObjCSpaceBeforeProtocolList: true
+
+# Taken from git's rules
+PenaltyBreakAssignment: 10
+PenaltyBreakBeforeFirstCallParameter: 30
+PenaltyBreakComment: 10
+PenaltyBreakFirstLessLess: 0
+PenaltyBreakString: 10
+PenaltyExcessCharacter: 100
+PenaltyReturnTypeOnItsOwnLine: 60
+
+PointerAlignment: Right
+ReflowComments: false
+SortIncludes: false
+SortUsingDeclarations: false
+SpaceAfterCStyleCast: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatementsExceptForEachMacros
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles: false
+SpacesInContainerLiterals: false
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard: Cpp03
+TabWidth: 8
+UseTab: Always
+...
CONTRIBUTING.md +2 -0
index c501cf84..7ec8cb0f 100644
@@ -53,6 +53,8 @@ When preparing patches, please follow these guidelines:
 -   Separate large patches into logical patches
 -   Patch descriptions must end with your "Signed-off-by" line. This means your
     code meets the Developer's certificate of origin, see below.
+-   C code should be formatted using clang-format, using the .clang-format
+    configuration file at the root of this repository.
 
 When adding new, large features or tools it is best to discuss the
 design on the mailing list prior to submitting the patch.
Makefile +10 -1
index 2ffba8e9..972d5525 100644
@@ -36,7 +36,7 @@ ifneq ($(DESTDIR),)
 	export LIBSEPOLA
 endif
 
-all install relabel clean test indent:
+all install relabel clean test:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
@@ -50,3 +50,12 @@ distclean:
 	@for subdir in $(DISTCLEANSUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
+
+# We shouldn't have any unformatted files in the repo without an explicit exception.
+FORMAT_SOURCE_FILES := $(shell find $(SUBDIRS) -type f \( -name '*.c' -o -name '*.h' \))
+
+format:
+	clang-format -i $(FORMAT_SOURCE_FILES)
+
+check-format:
+	clang-format --dry-run -Werror $(FORMAT_SOURCE_FILES)
checkpolicy/Makefile +0 -3
index 6e8008e3..0055f495 100644
@@ -77,6 +77,3 @@ relabel: install
 clean:
 	-rm -f $(TARGETS) $(CHECKPOLOBJS) $(CHECKMODOBJS) y.tab.c y.tab.h lex.yy.c tests/testpol.conf tests/testpol.bin
 	$(MAKE) -C test clean
-
-indent:
-	../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
gui/Makefile +0 -2
index b29610d4..b1a94224 100644
@@ -60,8 +60,6 @@ install: all
 clean:
 	(cd po && $(MAKE) $@)
 
-indent:
-
 relabel:
 
 test:
libselinux/Makefile +1 -1
index a50b6491..aeede2b5 100644
@@ -50,7 +50,7 @@ COMPILER := clang
 endif
 export COMPILER
 
-all install relabel clean distclean indent:
+all install relabel clean distclean:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
libselinux/include/Makefile +0 -4
index 8ab9ce92..a1e322ac 100644
@@ -10,9 +10,5 @@ install: all
 
 relabel:
 
-indent:
-	../../scripts/Lindent $(wildcard selinux/*.h)
-
 distclean clean:
 	-rm -f selinux/*~
-
libselinux/man/Makefile +1 -1
index fdc0825f..42b1662a 100644
@@ -35,4 +35,4 @@ install: all
 
 relabel:
 
-indent distclean clean:
+format distclean clean:
libselinux/src/Makefile +0 -3
index 261c22d4..f7412cd1 100644
@@ -223,7 +223,4 @@ clean: clean-pywrap clean-rubywrap
 distclean: clean
 	rm -f $(GENERATED) $(SWIGFILES)
 
-indent:
-	../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
-
 .PHONY: all clean clean-pywrap clean-rubywrap pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
libselinux/utils/Makefile +0 -4
index 6b1dc7c9..20dc44eb 100644
@@ -72,8 +72,4 @@ clean:
 
 distclean: clean
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
-
libsemanage/Makefile +0 -4
index dbd240da..591ce5f2 100644
@@ -29,9 +29,5 @@ clean distclean:
 	$(MAKE) -C src $@
 	$(MAKE) -C tests $@
 
-indent:
-	$(MAKE) -C src $@
-	$(MAKE) -C include $@
-
 test: all
 	$(MAKE) -C tests test
libsemanage/include/Makefile +0 -3
index 6e44a28a..b08e48a0 100644
@@ -7,6 +7,3 @@ all:
 install: all
 	test -d $(DESTDIR)$(INCDIR) || install -m 755 -d $(DESTDIR)$(INCDIR)
 	install -m 644 $(wildcard semanage/*.h) $(DESTDIR)$(INCDIR)
-
-indent:
-	../../scripts/Lindent $(wildcard semanage/*.h)
libsemanage/src/Makefile +0 -3
index fa3449fb..8e025112 100644
@@ -167,7 +167,4 @@ clean:
 distclean: clean
 	rm -f $(GENERATED) $(SWIGFILES)
 
-indent:
-	../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
-
 .PHONY: all clean pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
libsemanage/utils/Makefile +0 -3
index 5b8fbb6b..0f382934 100644
@@ -13,7 +13,4 @@ clean:
 
 distclean: clean
 
-indent:
-
 relabel:
-
libsepol/Makefile +0 -6
index 5b5d03e2..223edcec 100644
@@ -20,11 +20,5 @@ clean:
 	$(MAKE) -C utils clean
 	$(MAKE) -C tests clean
 
-indent:
-	$(MAKE) -C src $@
-	$(MAKE) -C include $@
-	$(MAKE) -C utils $@
-
 test:
 	$(MAKE) -C tests test
-
libsepol/include/Makefile +0 -3
index 1ad4ecab..f49b6e19 100644
@@ -12,6 +12,3 @@ install: all
 	install -m 644 $(wildcard sepol/*.h) $(DESTDIR)$(INCDIR)
 	install -m 644 $(wildcard sepol/policydb/*.h) $(DESTDIR)$(INCDIR)/policydb
 	install -m 644 $(wildcard $(CILDIR)/include/cil/*.h) $(DESTDIR)$(INCDIR)/cil
-
-indent:
-	../../scripts/Lindent $(wildcard sepol/*.h)
libsepol/src/Makefile +0 -4
index 90aed394..bb2b7f5e 100644
@@ -105,7 +105,3 @@ relabel:
 
 clean: 
 	-rm -f $(LIBPC) $(LIBMAP) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(TARGET) $(CIL_GENERATED)
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
libsepol/utils/Makefile +0 -4
index 31932c11..a8bedf2e 100644
@@ -18,8 +18,4 @@ install: all
 clean:
 	-rm -f $(TARGETS) *.o 
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
-
mcstrans/src/Makefile +0 -1
index 88487068..609b2d45 100644
@@ -37,4 +37,3 @@ install: all
 
 clean: 
 	-rm -f $(OBJS) $(LOBJS) $(TARGET) $(PROG) $(PROG_OBJS) *~ \#*
-
mcstrans/utils/Makefile +0 -1
index 57a73932..1d70b4ed 100644
@@ -36,4 +36,3 @@ clean:
 	rm -f $(TARGETS) *.o *~ \#*
 
 relabel:
-
policycoreutils/Makefile +1 -1
index 7c9706e3..9186a9ef 100644
@@ -6,7 +6,7 @@ LIBSELINUX_LDLIBS := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH):../libselinux/s
 LIBSEMANAGE_LDLIBS := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH):../libsemanage/src" $(PKG_CONFIG) --libs libsemanage)
 export LIBSELINUX_LDLIBS LIBSEMANAGE_LDLIBS
 
-all install relabel clean indent:
+all install relabel clean:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
policycoreutils/hll/Makefile +1 -1
index fe720e57..390f3a34 100644
@@ -1,6 +1,6 @@
 SUBDIRS = pp
 
-all install relabel clean indent:
+all install relabel clean:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
policycoreutils/hll/pp/Makefile +0 -3
index 3c9b0efc..a0d475a3 100644
@@ -25,6 +25,3 @@ relabel:
 
 clean:
 	-rm -f pp $(PP_OBJS)
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
policycoreutils/load_policy/Makefile +0 -3
index c61fee70..7754cadc 100644
@@ -28,8 +28,5 @@ install: all
 clean:
 	-rm -f $(TARGETS) *.o 
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
 	/sbin/restorecon $(DESTDIR)$(SBINDIR)/load_policy 
policycoreutils/man/Makefile +0 -2
index a4539f24..2d5bc4b5 100644
@@ -17,5 +17,3 @@ install: all
 			install -m 644 $${lang}/man5/*.5 $(DESTDIR)$(MANDIR)/$${lang}/man5 ; \
 		fi ; \
 	done
-
-relabel:
policycoreutils/newrole/Makefile +0 -3
index df64483f..fcd2661f 100644
@@ -87,9 +87,6 @@ endif
 clean:
 	rm -f newrole *.o 
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
 	/sbin/restorecon $(DESTDIR)$(BINDIR)/newrole
 
policycoreutils/run_init/Makefile +0 -3
index c89fffb9..0e904dba 100644
@@ -53,8 +53,5 @@ endif
 clean:
 	-rm -f $(TARGETS) *.o 
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
 	/sbin/restorecon $(DESTDIR)$(SBINDIR)/run_init $(DESTDIR)$(SBINDIR)/open_init_pty
policycoreutils/scripts/Makefile +0 -2
index 6d8196c6..1058bbab 100644
@@ -21,6 +21,4 @@ install: all
 
 clean:
 
-indent:
-
 relabel:
policycoreutils/secon/Makefile +0 -3
index c9296f19..b2f276f9 100644
@@ -35,9 +35,6 @@ relabel:
 clean:
 	rm -f *.o core* secon *~ *.bak
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 bare: clean
 
 .PHONY: clean bare
policycoreutils/semodule/Makefile +0 -4
index 018ee2ca..47bde5f8 100644
@@ -35,7 +35,3 @@ relabel:
 
 clean:
 	-rm -f semodule *.o genhomedircon
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
policycoreutils/sestatus/Makefile +0 -3
index 1ce617c0..70f46956 100644
@@ -41,7 +41,4 @@ install: all
 clean:
 	rm -f sestatus *.o
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
policycoreutils/setfiles/Makefile +0 -3
index dab6313c..aa3e80fc 100644
@@ -43,8 +43,5 @@ install: all
 clean:
 	rm -f setfiles restorecon restorecon_xattr *.o
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
 	$(DESTDIR)$(SBINDIR)/restorecon $(DESTDIR)$(SBINDIR)/setfiles $(DESTDIR)$(SBINDIR)/restorecon_xattr
policycoreutils/setsebool/Makefile +0 -4
index 87494c55..360e0ea0 100644
@@ -35,7 +35,3 @@ relabel:
 
 clean:
 	-rm -f setsebool *.o
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
policycoreutils/unsetfiles/Makefile +0 -3
index 9cb51494..8dbf85af 100644
@@ -20,8 +20,5 @@ install: all
 clean:
 	-rm -f unsetfiles *.o
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
 	/sbin/restorecon $(DESTDIR)$(SBINDIR)/unsetfiles
python/Makefile +1 -1
index 00312dbd..c4c31a17 100644
@@ -1,6 +1,6 @@
 SUBDIRS = sepolicy audit2allow semanage sepolgen chcat po
 
-all install relabel clean indent test:
+all install relabel clean format test:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
python/audit2allow/Makefile +0 -3
index 28b8f7f2..90d10d1a 100644
@@ -51,7 +51,4 @@ install: all
 clean:
 	rm -f *~ *.o sepolgen-ifgen-attr-helper test_dummy_policy
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: ;
python/chcat/Makefile +0 -2
index 7b3ee17f..334b749d 100644
@@ -21,8 +21,6 @@ install: all
 
 clean:
 
-indent:
-
 relabel:
 
 test:
python/semanage/Makefile +0 -2
index 628d135a..51208d9e 100644
@@ -35,6 +35,4 @@ test:
 	@$(PYTHON) test-semanage.py -a
 clean:
 
-indent:
-
 relabel:
python/sepolgen/Makefile +0 -5
index 3aed3304..2ad7b13c 100644
@@ -11,10 +11,5 @@ clean:
 	rm -f *~ *.pyc
 	rm -f parser.out parsetab.py
 
-indent: ;
-
 test:
 	$(MAKE) -C tests $@
-
-
-
python/sepolgen/src/Makefile +0 -6
index 2815a13d..a2456652 100644
@@ -12,10 +12,4 @@ clean:
 	rm -f *~ *.pyc
 	rm -f parser.out parsetab.py
 
-indent: ;
-
-
 test: ;
-
-
-
restorecond/Makefile +0 -3
index b8b86eb4..51eb7608 100644
@@ -59,7 +59,4 @@ relabel: install
 clean:
 	-rm -f restorecond *.o *~
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 test:
sandbox/Makefile +0 -3
index 0799ab7c..eaaad75c 100644
@@ -50,7 +50,4 @@ clean:
 	-rm -f seunshare *.o *~
 	(cd po && $(MAKE) $@)
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
scripts/Lindent deleted +0 -18
deleted file mode 100755
index 9c4b3e2b..00000000
@@ -1,18 +0,0 @@
-#!/bin/sh
-PARAM="-npro -kr -i8 -ts8 -sob -l80 -ss -ncs -cp1"
-RES=`indent --version`
-V1=`echo $RES | cut -d' ' -f3 | cut -d'.' -f1`
-V2=`echo $RES | cut -d' ' -f3 | cut -d'.' -f2`
-V3=`echo $RES | cut -d' ' -f3 | cut -d'.' -f3`
-if [ $V1 -gt 2 ]; then
-  PARAM="$PARAM -il0"
-elif [ $V1 -eq 2 ]; then
-  if [ $V2 -gt 2 ]; then
-    PARAM="$PARAM -il0";
-  elif [ $V2 -eq 2 ]; then
-    if [ $V3 -ge 10 ]; then
-      PARAM="$PARAM -il0"
-    fi
-  fi
-fi
-indent $PARAM "$@"
semodule-utils/Makefile +1 -1
index e0a65792..81c97af0 100644
@@ -1,6 +1,6 @@
 SUBDIRS = semodule_package semodule_link semodule_expand
 
-all install relabel clean indent:
+all install relabel clean:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
semodule-utils/semodule_expand/Makefile +0 -4
index ad776b15..72f09a46 100644
@@ -27,7 +27,3 @@ relabel:
 
 clean:
 	-rm -f semodule_expand *.o
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
semodule-utils/semodule_link/Makefile +0 -4
index 936d161c..83590c07 100644
@@ -27,7 +27,3 @@ relabel:
 
 clean:
 	-rm -f semodule_link *.o
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
semodule-utils/semodule_package/Makefile +0 -4
index 6a289f73..2465f987 100644
@@ -29,7 +29,3 @@ relabel:
 
 clean:
 	-rm -f semodule_package semodule_unpackage *.o
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
Signature · 1 line
2.51.0
maintainer Acked-by on v4 1/1
#11 lore raw
Quoted 27 lines · Rahul Sandhu

Currently only an RFC.

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

As well as this, replace all the existing indent targets with a single toplevel format target. Managing all the source files to be formatted is not pretty to maintain, and doesn't really give us much.

Also define a toplevel check-format target to verify that all code is formatted properly. This only becomes useful in the future once we have reformatted all existing code, but is wired up for now.

For the future:
1. Reformat all existing code. I understand this is a big change, hence the RFC, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.
2. Add a CI target to check that all code is formatted as per the new clang-format configuration. The check-format target can be used for this.

Quoted 1 line

LGTM but still posted as an RFC so will await a final non-RFC version.

maintainer
#12 lore raw
Quoted 29 lines · Stephen Smalley
Quoted 27 lines · Rahul Sandhu

Currently only an RFC.

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

As well as this, replace all the existing indent targets with a single toplevel format target. Managing all the source files to be formatted is not pretty to maintain, and doesn't really give us much.

Also define a toplevel check-format target to verify that all code is formatted properly. This only becomes useful in the future once we have reformatted all existing code, but is wired up for now.

For the future:
1. Reformat all existing code. I understand this is a big change, hence the RFC, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.
2. Add a CI target to check that all code is formatted as per the new clang-format configuration. The check-format target can be used for this.

Quoted 5 lines
Quoted 1 line

LGTM but still posted as an RFC so will await a final non-RFC version.

Ah, actually you seem to have accidentally deleted an empty relabel:
target from policycoreutils/man/Makefile, which breaks a make relabel at top-level.

#13 lore raw

PATCH v5 1/1 treewide: add .clang-format configuration file

Currently only an RFC.

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

As well as this, replace all the existing indent targets with a single toplevel format target. Managing all the source files to be formatted is not pretty to maintain, and doesn't really give us much.

Also define a toplevel check-format target to verify that all code is formatted properly. This only becomes useful in the future once we have reformatted all existing code, but is wired up for now.

For the future:
1. Reformat all existing code. I understand this is a big change, hence the RFC, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.
2. Add a CI target to check that all code is formatted as per the new clang-format configuration. The check-format target can be used for this.

---
 .clang-format                            | 130 +++++++++++++++++++++++
 CONTRIBUTING.md                          |   2 +
 Makefile                                 |  11 +-
 checkpolicy/Makefile                     |   3 -
 gui/Makefile                             |   2 -
 libselinux/Makefile                      |   2 +-
 libselinux/include/Makefile              |   4 -
 libselinux/man/Makefile                  |   2 +-
 libselinux/src/Makefile                  |   3 -
 libselinux/utils/Makefile                |   4 -
 libsemanage/Makefile                     |   4 -
 libsemanage/include/Makefile             |   3 -
 libsemanage/src/Makefile                 |   3 -
 libsemanage/utils/Makefile               |   3 -
 libsepol/Makefile                        |   6 --
 libsepol/include/Makefile                |   3 -
 libsepol/src/Makefile                    |   4 -
 libsepol/utils/Makefile                  |   4 -
 mcstrans/src/Makefile                    |   1 -
 mcstrans/utils/Makefile                  |   1 -
 policycoreutils/Makefile                 |   2 +-
 policycoreutils/hll/Makefile             |   2 +-
 policycoreutils/hll/pp/Makefile          |   3 -
 policycoreutils/load_policy/Makefile     |   3 -
 policycoreutils/newrole/Makefile         |   3 -
 policycoreutils/run_init/Makefile        |   3 -
 policycoreutils/scripts/Makefile         |   2 -
 policycoreutils/secon/Makefile           |   3 -
 policycoreutils/semodule/Makefile        |   4 -
 policycoreutils/sestatus/Makefile        |   3 -
 policycoreutils/setfiles/Makefile        |   3 -
 policycoreutils/setsebool/Makefile       |   4 -
 policycoreutils/unsetfiles/Makefile      |   3 -
 python/Makefile                          |   2 +-
 python/audit2allow/Makefile              |   3 -
 python/chcat/Makefile                    |   2 -
 python/semanage/Makefile                 |   2 -
 python/sepolgen/Makefile                 |   5 -
 python/sepolgen/src/Makefile             |   6 --
 restorecond/Makefile                     |   3 -
 sandbox/Makefile                         |   3 -
 scripts/Lindent                          |  18 ----
 semodule-utils/Makefile                  |   2 +-
 semodule-utils/semodule_expand/Makefile  |   4 -
 semodule-utils/semodule_link/Makefile    |   4 -
 semodule-utils/semodule_package/Makefile |   4 -
 46 files changed, 148 insertions(+), 143 deletions(-)
 create mode 100644 .clang-format
 delete mode 100755 scripts/Lindent

v2: remove linux kernel ForEachMacros and replace them with ours
v3: replace the indent target with the new format target. also remove any
mention of .editorconfig from the commit message; those changes are better suited for another patch (they're small and self-contained enough that they're a smaller thing to merge).
v4: drop the subdir format targets entirely, replacing with a single toplevel
format and check-format target.
v5: add back relabel target accidently removed.

.clang-format new file +130 -0
new file mode 100644
index 00000000..35595d87
@@ -0,0 +1,130 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# clang-format configuration file. Intended for clang-format >= 11.
+#
+# For more information, see:
+#
+#   Documentation/dev-tools/clang-format.rst
+#   https://clang.llvm.org/docs/ClangFormat.html
+#   https://clang.llvm.org/docs/ClangFormatStyleOptions.html
+#
+---
+AccessModifierOffset: -4
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlines: Left
+AlignOperands: true
+AlignTrailingComments: false
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: false
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:
+  AfterClass: false
+  AfterControlStatement: false
+  AfterEnum: false
+  AfterFunction: true
+  AfterNamespace: true
+  AfterObjCDeclaration: false
+  AfterStruct: false
+  AfterUnion: false
+  AfterExternBlock: false
+  BeforeCatch: false
+  BeforeElse: false
+  IndentBraces: false
+  SplitEmptyFunction: true
+  SplitEmptyRecord: true
+  SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Custom
+BreakBeforeInheritanceComma: false
+BreakBeforeTernaryOperators: false
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeComma
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: false
+ColumnLimit: 80
+CommentPragmas: '^ IWYU pragma:'
+CompactNamespaces: false
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+ConstructorInitializerIndentWidth: 8
+ContinuationIndentWidth: 8
+Cpp11BracedListStyle: false
+DerivePointerAlignment: false
+DisableFormat: false
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: false
+
+# Taken from:
+#   git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' \
+#   | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$,  - '\1'," \
+#   | LC_ALL=C sort -u
+ForEachMacros:
+  - 'cil_list_for_each'
+  - 'cil_stack_for_each'
+  - 'cil_stack_for_each_starting_at'
+  - 'ebitmap_for_each_bit'
+  - 'ebitmap_for_each_positive_bit'
+
+IncludeBlocks: Preserve
+IncludeCategories:
+  - Regex: '.*'
+    Priority: 1
+IncludeIsMainRegex: '(Test)?$'
+IndentCaseLabels: false
+IndentGotoLabels: false
+IndentPPDirectives: None
+IndentWidth: 8
+IndentWrappedFunctionNames: false
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: false
+MacroBlockBegin: ''
+MacroBlockEnd: ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Auto
+ObjCBlockIndentWidth: 8
+ObjCSpaceAfterProperty: true
+ObjCSpaceBeforeProtocolList: true
+
+# Taken from git's rules
+PenaltyBreakAssignment: 10
+PenaltyBreakBeforeFirstCallParameter: 30
+PenaltyBreakComment: 10
+PenaltyBreakFirstLessLess: 0
+PenaltyBreakString: 10
+PenaltyExcessCharacter: 100
+PenaltyReturnTypeOnItsOwnLine: 60
+
+PointerAlignment: Right
+ReflowComments: false
+SortIncludes: false
+SortUsingDeclarations: false
+SpaceAfterCStyleCast: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatementsExceptForEachMacros
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles: false
+SpacesInContainerLiterals: false
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard: Cpp03
+TabWidth: 8
+UseTab: Always
+...
CONTRIBUTING.md +2 -0
index c501cf84..7ec8cb0f 100644
@@ -53,6 +53,8 @@ When preparing patches, please follow these guidelines:
 -   Separate large patches into logical patches
 -   Patch descriptions must end with your "Signed-off-by" line. This means your
     code meets the Developer's certificate of origin, see below.
+-   C code should be formatted using clang-format, using the .clang-format
+    configuration file at the root of this repository.
 
 When adding new, large features or tools it is best to discuss the
 design on the mailing list prior to submitting the patch.
Makefile +10 -1
index 2ffba8e9..972d5525 100644
@@ -36,7 +36,7 @@ ifneq ($(DESTDIR),)
 	export LIBSEPOLA
 endif
 
-all install relabel clean test indent:
+all install relabel clean test:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
@@ -50,3 +50,12 @@ distclean:
 	@for subdir in $(DISTCLEANSUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
+
+# We shouldn't have any unformatted files in the repo without an explicit exception.
+FORMAT_SOURCE_FILES := $(shell find $(SUBDIRS) -type f \( -name '*.c' -o -name '*.h' \))
+
+format:
+	clang-format -i $(FORMAT_SOURCE_FILES)
+
+check-format:
+	clang-format --dry-run -Werror $(FORMAT_SOURCE_FILES)
checkpolicy/Makefile +0 -3
index 6e8008e3..0055f495 100644
@@ -77,6 +77,3 @@ relabel: install
 clean:
 	-rm -f $(TARGETS) $(CHECKPOLOBJS) $(CHECKMODOBJS) y.tab.c y.tab.h lex.yy.c tests/testpol.conf tests/testpol.bin
 	$(MAKE) -C test clean
-
-indent:
-	../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
gui/Makefile +0 -2
index b29610d4..b1a94224 100644
@@ -60,8 +60,6 @@ install: all
 clean:
 	(cd po && $(MAKE) $@)
 
-indent:
-
 relabel:
 
 test:
libselinux/Makefile +1 -1
index a50b6491..aeede2b5 100644
@@ -50,7 +50,7 @@ COMPILER := clang
 endif
 export COMPILER
 
-all install relabel clean distclean indent:
+all install relabel clean distclean:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
libselinux/include/Makefile +0 -4
index 8ab9ce92..a1e322ac 100644
@@ -10,9 +10,5 @@ install: all
 
 relabel:
 
-indent:
-	../../scripts/Lindent $(wildcard selinux/*.h)
-
 distclean clean:
 	-rm -f selinux/*~
-
libselinux/man/Makefile +1 -1
index fdc0825f..42b1662a 100644
@@ -35,4 +35,4 @@ install: all
 
 relabel:
 
-indent distclean clean:
+format distclean clean:
libselinux/src/Makefile +0 -3
index 261c22d4..f7412cd1 100644
@@ -223,7 +223,4 @@ clean: clean-pywrap clean-rubywrap
 distclean: clean
 	rm -f $(GENERATED) $(SWIGFILES)
 
-indent:
-	../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
-
 .PHONY: all clean clean-pywrap clean-rubywrap pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
libselinux/utils/Makefile +0 -4
index 6b1dc7c9..20dc44eb 100644
@@ -72,8 +72,4 @@ clean:
 
 distclean: clean
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
-
libsemanage/Makefile +0 -4
index dbd240da..591ce5f2 100644
@@ -29,9 +29,5 @@ clean distclean:
 	$(MAKE) -C src $@
 	$(MAKE) -C tests $@
 
-indent:
-	$(MAKE) -C src $@
-	$(MAKE) -C include $@
-
 test: all
 	$(MAKE) -C tests test
libsemanage/include/Makefile +0 -3
index 6e44a28a..b08e48a0 100644
@@ -7,6 +7,3 @@ all:
 install: all
 	test -d $(DESTDIR)$(INCDIR) || install -m 755 -d $(DESTDIR)$(INCDIR)
 	install -m 644 $(wildcard semanage/*.h) $(DESTDIR)$(INCDIR)
-
-indent:
-	../../scripts/Lindent $(wildcard semanage/*.h)
libsemanage/src/Makefile +0 -3
index fa3449fb..8e025112 100644
@@ -167,7 +167,4 @@ clean:
 distclean: clean
 	rm -f $(GENERATED) $(SWIGFILES)
 
-indent:
-	../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
-
 .PHONY: all clean pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
libsemanage/utils/Makefile +0 -3
index 5b8fbb6b..0f382934 100644
@@ -13,7 +13,4 @@ clean:
 
 distclean: clean
 
-indent:
-
 relabel:
-
libsepol/Makefile +0 -6
index 5b5d03e2..223edcec 100644
@@ -20,11 +20,5 @@ clean:
 	$(MAKE) -C utils clean
 	$(MAKE) -C tests clean
 
-indent:
-	$(MAKE) -C src $@
-	$(MAKE) -C include $@
-	$(MAKE) -C utils $@
-
 test:
 	$(MAKE) -C tests test
-
libsepol/include/Makefile +0 -3
index 1ad4ecab..f49b6e19 100644
@@ -12,6 +12,3 @@ install: all
 	install -m 644 $(wildcard sepol/*.h) $(DESTDIR)$(INCDIR)
 	install -m 644 $(wildcard sepol/policydb/*.h) $(DESTDIR)$(INCDIR)/policydb
 	install -m 644 $(wildcard $(CILDIR)/include/cil/*.h) $(DESTDIR)$(INCDIR)/cil
-
-indent:
-	../../scripts/Lindent $(wildcard sepol/*.h)
libsepol/src/Makefile +0 -4
index 90aed394..bb2b7f5e 100644
@@ -105,7 +105,3 @@ relabel:
 
 clean: 
 	-rm -f $(LIBPC) $(LIBMAP) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(TARGET) $(CIL_GENERATED)
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
libsepol/utils/Makefile +0 -4
index 31932c11..a8bedf2e 100644
@@ -18,8 +18,4 @@ install: all
 clean:
 	-rm -f $(TARGETS) *.o 
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
-
mcstrans/src/Makefile +0 -1
index 88487068..609b2d45 100644
@@ -37,4 +37,3 @@ install: all
 
 clean: 
 	-rm -f $(OBJS) $(LOBJS) $(TARGET) $(PROG) $(PROG_OBJS) *~ \#*
-
mcstrans/utils/Makefile +0 -1
index 57a73932..1d70b4ed 100644
@@ -36,4 +36,3 @@ clean:
 	rm -f $(TARGETS) *.o *~ \#*
 
 relabel:
-
policycoreutils/Makefile +1 -1
index 7c9706e3..9186a9ef 100644
@@ -6,7 +6,7 @@ LIBSELINUX_LDLIBS := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH):../libselinux/s
 LIBSEMANAGE_LDLIBS := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH):../libsemanage/src" $(PKG_CONFIG) --libs libsemanage)
 export LIBSELINUX_LDLIBS LIBSEMANAGE_LDLIBS
 
-all install relabel clean indent:
+all install relabel clean:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
policycoreutils/hll/Makefile +1 -1
index fe720e57..390f3a34 100644
@@ -1,6 +1,6 @@
 SUBDIRS = pp
 
-all install relabel clean indent:
+all install relabel clean:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
policycoreutils/hll/pp/Makefile +0 -3
index 3c9b0efc..a0d475a3 100644
@@ -25,6 +25,3 @@ relabel:
 
 clean:
 	-rm -f pp $(PP_OBJS)
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
policycoreutils/load_policy/Makefile +0 -3
index c61fee70..7754cadc 100644
@@ -28,8 +28,5 @@ install: all
 clean:
 	-rm -f $(TARGETS) *.o 
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
 	/sbin/restorecon $(DESTDIR)$(SBINDIR)/load_policy 
policycoreutils/newrole/Makefile +0 -3
index df64483f..fcd2661f 100644
@@ -87,9 +87,6 @@ endif
 clean:
 	rm -f newrole *.o 
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
 	/sbin/restorecon $(DESTDIR)$(BINDIR)/newrole
 
policycoreutils/run_init/Makefile +0 -3
index c89fffb9..0e904dba 100644
@@ -53,8 +53,5 @@ endif
 clean:
 	-rm -f $(TARGETS) *.o 
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
 	/sbin/restorecon $(DESTDIR)$(SBINDIR)/run_init $(DESTDIR)$(SBINDIR)/open_init_pty
policycoreutils/scripts/Makefile +0 -2
index 6d8196c6..1058bbab 100644
@@ -21,6 +21,4 @@ install: all
 
 clean:
 
-indent:
-
 relabel:
policycoreutils/secon/Makefile +0 -3
index c9296f19..b2f276f9 100644
@@ -35,9 +35,6 @@ relabel:
 clean:
 	rm -f *.o core* secon *~ *.bak
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 bare: clean
 
 .PHONY: clean bare
policycoreutils/semodule/Makefile +0 -4
index 018ee2ca..47bde5f8 100644
@@ -35,7 +35,3 @@ relabel:
 
 clean:
 	-rm -f semodule *.o genhomedircon
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
policycoreutils/sestatus/Makefile +0 -3
index 1ce617c0..70f46956 100644
@@ -41,7 +41,4 @@ install: all
 clean:
 	rm -f sestatus *.o
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
policycoreutils/setfiles/Makefile +0 -3
index dab6313c..aa3e80fc 100644
@@ -43,8 +43,5 @@ install: all
 clean:
 	rm -f setfiles restorecon restorecon_xattr *.o
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
 	$(DESTDIR)$(SBINDIR)/restorecon $(DESTDIR)$(SBINDIR)/setfiles $(DESTDIR)$(SBINDIR)/restorecon_xattr
policycoreutils/setsebool/Makefile +0 -4
index 87494c55..360e0ea0 100644
@@ -35,7 +35,3 @@ relabel:
 
 clean:
 	-rm -f setsebool *.o
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
policycoreutils/unsetfiles/Makefile +0 -3
index 9cb51494..8dbf85af 100644
@@ -20,8 +20,5 @@ install: all
 clean:
 	-rm -f unsetfiles *.o
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
 	/sbin/restorecon $(DESTDIR)$(SBINDIR)/unsetfiles
python/Makefile +1 -1
index 00312dbd..c4c31a17 100644
@@ -1,6 +1,6 @@
 SUBDIRS = sepolicy audit2allow semanage sepolgen chcat po
 
-all install relabel clean indent test:
+all install relabel clean format test:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
python/audit2allow/Makefile +0 -3
index 28b8f7f2..90d10d1a 100644
@@ -51,7 +51,4 @@ install: all
 clean:
 	rm -f *~ *.o sepolgen-ifgen-attr-helper test_dummy_policy
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: ;
python/chcat/Makefile +0 -2
index 7b3ee17f..334b749d 100644
@@ -21,8 +21,6 @@ install: all
 
 clean:
 
-indent:
-
 relabel:
 
 test:
python/semanage/Makefile +0 -2
index 628d135a..51208d9e 100644
@@ -35,6 +35,4 @@ test:
 	@$(PYTHON) test-semanage.py -a
 clean:
 
-indent:
-
 relabel:
python/sepolgen/Makefile +0 -5
index 3aed3304..2ad7b13c 100644
@@ -11,10 +11,5 @@ clean:
 	rm -f *~ *.pyc
 	rm -f parser.out parsetab.py
 
-indent: ;
-
 test:
 	$(MAKE) -C tests $@
-
-
-
python/sepolgen/src/Makefile +0 -6
index 2815a13d..a2456652 100644
@@ -12,10 +12,4 @@ clean:
 	rm -f *~ *.pyc
 	rm -f parser.out parsetab.py
 
-indent: ;
-
-
 test: ;
-
-
-
restorecond/Makefile +0 -3
index b8b86eb4..51eb7608 100644
@@ -59,7 +59,4 @@ relabel: install
 clean:
 	-rm -f restorecond *.o *~
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 test:
sandbox/Makefile +0 -3
index 0799ab7c..eaaad75c 100644
@@ -50,7 +50,4 @@ clean:
 	-rm -f seunshare *.o *~
 	(cd po && $(MAKE) $@)
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
scripts/Lindent deleted +0 -18
deleted file mode 100755
index 9c4b3e2b..00000000
@@ -1,18 +0,0 @@
-#!/bin/sh
-PARAM="-npro -kr -i8 -ts8 -sob -l80 -ss -ncs -cp1"
-RES=`indent --version`
-V1=`echo $RES | cut -d' ' -f3 | cut -d'.' -f1`
-V2=`echo $RES | cut -d' ' -f3 | cut -d'.' -f2`
-V3=`echo $RES | cut -d' ' -f3 | cut -d'.' -f3`
-if [ $V1 -gt 2 ]; then
-  PARAM="$PARAM -il0"
-elif [ $V1 -eq 2 ]; then
-  if [ $V2 -gt 2 ]; then
-    PARAM="$PARAM -il0";
-  elif [ $V2 -eq 2 ]; then
-    if [ $V3 -ge 10 ]; then
-      PARAM="$PARAM -il0"
-    fi
-  fi
-fi
-indent $PARAM "$@"
semodule-utils/Makefile +1 -1
index e0a65792..81c97af0 100644
@@ -1,6 +1,6 @@
 SUBDIRS = semodule_package semodule_link semodule_expand
 
-all install relabel clean indent:
+all install relabel clean:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
semodule-utils/semodule_expand/Makefile +0 -4
index ad776b15..72f09a46 100644
@@ -27,7 +27,3 @@ relabel:
 
 clean:
 	-rm -f semodule_expand *.o
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
semodule-utils/semodule_link/Makefile +0 -4
index 936d161c..83590c07 100644
@@ -27,7 +27,3 @@ relabel:
 
 clean:
 	-rm -f semodule_link *.o
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
semodule-utils/semodule_package/Makefile +0 -4
index 6a289f73..2465f987 100644
@@ -29,7 +29,3 @@ relabel:
 
 clean:
 	-rm -f semodule_package semodule_unpackage *.o
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
Signature · 1 line
2.51.0
#14 lore raw

PATCH v6 1/1 treewide: add .clang-format configuration file

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

As well as this, replace all the existing indent targets with a single toplevel format target. Managing all the source files to be formatted is not pretty to maintain, and doesn't really give us much.

Also define a toplevel check-format target to verify that all code is formatted properly. This only becomes useful in the future once we have reformatted all existing code, but is wired up for now.

For the future:
1. Reformat all existing code. I understand this is a big change, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.

2. Add a CI target to check that all code is formatted as per the new clang-format configuration. The check-format target can be used for this.

---
 .clang-format                            | 130 +++++++++++++++++++++++
 CONTRIBUTING.md                          |   2 +
 Makefile                                 |  11 +-
 checkpolicy/Makefile                     |   3 -
 gui/Makefile                             |   2 -
 libselinux/Makefile                      |   2 +-
 libselinux/include/Makefile              |   4 -
 libselinux/man/Makefile                  |   2 +-
 libselinux/src/Makefile                  |   3 -
 libselinux/utils/Makefile                |   4 -
 libsemanage/Makefile                     |   4 -
 libsemanage/include/Makefile             |   3 -
 libsemanage/src/Makefile                 |   3 -
 libsemanage/utils/Makefile               |   3 -
 libsepol/Makefile                        |   6 --
 libsepol/include/Makefile                |   3 -
 libsepol/src/Makefile                    |   4 -
 libsepol/utils/Makefile                  |   4 -
 mcstrans/src/Makefile                    |   1 -
 mcstrans/utils/Makefile                  |   1 -
 policycoreutils/Makefile                 |   2 +-
 policycoreutils/hll/Makefile             |   2 +-
 policycoreutils/hll/pp/Makefile          |   3 -
 policycoreutils/load_policy/Makefile     |   3 -
 policycoreutils/newrole/Makefile         |   3 -
 policycoreutils/run_init/Makefile        |   3 -
 policycoreutils/scripts/Makefile         |   2 -
 policycoreutils/secon/Makefile           |   3 -
 policycoreutils/semodule/Makefile        |   4 -
 policycoreutils/sestatus/Makefile        |   3 -
 policycoreutils/setfiles/Makefile        |   3 -
 policycoreutils/setsebool/Makefile       |   4 -
 policycoreutils/unsetfiles/Makefile      |   3 -
 python/Makefile                          |   2 +-
 python/audit2allow/Makefile              |   3 -
 python/chcat/Makefile                    |   2 -
 python/semanage/Makefile                 |   2 -
 python/sepolgen/Makefile                 |   5 -
 python/sepolgen/src/Makefile             |   6 --
 restorecond/Makefile                     |   3 -
 sandbox/Makefile                         |   3 -
 scripts/Lindent                          |  18 ----
 semodule-utils/Makefile                  |   2 +-
 semodule-utils/semodule_expand/Makefile  |   4 -
 semodule-utils/semodule_link/Makefile    |   4 -
 semodule-utils/semodule_package/Makefile |   4 -
 46 files changed, 148 insertions(+), 143 deletions(-)
 create mode 100644 .clang-format
 delete mode 100755 scripts/Lindent

v2: remove linux kernel ForEachMacros and replace them with ours
v3: replace the indent target with the new format target. also remove any
mention of .editorconfig from the commit message; those changes are better suited for another patch (they're small and self-contained enough that they're a smaller thing to merge).
v4: drop the subdir format targets entirely, replacing with a single toplevel
format and check-format target.
v5: add back relabel target accidently removed.
v6: remove all references to being an RFC in the commit message.

.clang-format new file +130 -0
new file mode 100644
index 00000000..35595d87
@@ -0,0 +1,130 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# clang-format configuration file. Intended for clang-format >= 11.
+#
+# For more information, see:
+#
+#   Documentation/dev-tools/clang-format.rst
+#   https://clang.llvm.org/docs/ClangFormat.html
+#   https://clang.llvm.org/docs/ClangFormatStyleOptions.html
+#
+---
+AccessModifierOffset: -4
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlines: Left
+AlignOperands: true
+AlignTrailingComments: false
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: false
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:
+  AfterClass: false
+  AfterControlStatement: false
+  AfterEnum: false
+  AfterFunction: true
+  AfterNamespace: true
+  AfterObjCDeclaration: false
+  AfterStruct: false
+  AfterUnion: false
+  AfterExternBlock: false
+  BeforeCatch: false
+  BeforeElse: false
+  IndentBraces: false
+  SplitEmptyFunction: true
+  SplitEmptyRecord: true
+  SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Custom
+BreakBeforeInheritanceComma: false
+BreakBeforeTernaryOperators: false
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeComma
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: false
+ColumnLimit: 80
+CommentPragmas: '^ IWYU pragma:'
+CompactNamespaces: false
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+ConstructorInitializerIndentWidth: 8
+ContinuationIndentWidth: 8
+Cpp11BracedListStyle: false
+DerivePointerAlignment: false
+DisableFormat: false
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: false
+
+# Taken from:
+#   git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' \
+#   | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$,  - '\1'," \
+#   | LC_ALL=C sort -u
+ForEachMacros:
+  - 'cil_list_for_each'
+  - 'cil_stack_for_each'
+  - 'cil_stack_for_each_starting_at'
+  - 'ebitmap_for_each_bit'
+  - 'ebitmap_for_each_positive_bit'
+
+IncludeBlocks: Preserve
+IncludeCategories:
+  - Regex: '.*'
+    Priority: 1
+IncludeIsMainRegex: '(Test)?$'
+IndentCaseLabels: false
+IndentGotoLabels: false
+IndentPPDirectives: None
+IndentWidth: 8
+IndentWrappedFunctionNames: false
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: false
+MacroBlockBegin: ''
+MacroBlockEnd: ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Auto
+ObjCBlockIndentWidth: 8
+ObjCSpaceAfterProperty: true
+ObjCSpaceBeforeProtocolList: true
+
+# Taken from git's rules
+PenaltyBreakAssignment: 10
+PenaltyBreakBeforeFirstCallParameter: 30
+PenaltyBreakComment: 10
+PenaltyBreakFirstLessLess: 0
+PenaltyBreakString: 10
+PenaltyExcessCharacter: 100
+PenaltyReturnTypeOnItsOwnLine: 60
+
+PointerAlignment: Right
+ReflowComments: false
+SortIncludes: false
+SortUsingDeclarations: false
+SpaceAfterCStyleCast: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatementsExceptForEachMacros
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles: false
+SpacesInContainerLiterals: false
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard: Cpp03
+TabWidth: 8
+UseTab: Always
+...
CONTRIBUTING.md +2 -0
index c501cf84..7ec8cb0f 100644
@@ -53,6 +53,8 @@ When preparing patches, please follow these guidelines:
 -   Separate large patches into logical patches
 -   Patch descriptions must end with your "Signed-off-by" line. This means your
     code meets the Developer's certificate of origin, see below.
+-   C code should be formatted using clang-format, using the .clang-format
+    configuration file at the root of this repository.
 
 When adding new, large features or tools it is best to discuss the
 design on the mailing list prior to submitting the patch.
Makefile +10 -1
index 2ffba8e9..972d5525 100644
@@ -36,7 +36,7 @@ ifneq ($(DESTDIR),)
 	export LIBSEPOLA
 endif
 
-all install relabel clean test indent:
+all install relabel clean test:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
@@ -50,3 +50,12 @@ distclean:
 	@for subdir in $(DISTCLEANSUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
+
+# We shouldn't have any unformatted files in the repo without an explicit exception.
+FORMAT_SOURCE_FILES := $(shell find $(SUBDIRS) -type f \( -name '*.c' -o -name '*.h' \))
+
+format:
+	clang-format -i $(FORMAT_SOURCE_FILES)
+
+check-format:
+	clang-format --dry-run -Werror $(FORMAT_SOURCE_FILES)
checkpolicy/Makefile +0 -3
index 6e8008e3..0055f495 100644
@@ -77,6 +77,3 @@ relabel: install
 clean:
 	-rm -f $(TARGETS) $(CHECKPOLOBJS) $(CHECKMODOBJS) y.tab.c y.tab.h lex.yy.c tests/testpol.conf tests/testpol.bin
 	$(MAKE) -C test clean
-
-indent:
-	../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
gui/Makefile +0 -2
index b29610d4..b1a94224 100644
@@ -60,8 +60,6 @@ install: all
 clean:
 	(cd po && $(MAKE) $@)
 
-indent:
-
 relabel:
 
 test:
libselinux/Makefile +1 -1
index a50b6491..aeede2b5 100644
@@ -50,7 +50,7 @@ COMPILER := clang
 endif
 export COMPILER
 
-all install relabel clean distclean indent:
+all install relabel clean distclean:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
libselinux/include/Makefile +0 -4
index 8ab9ce92..a1e322ac 100644
@@ -10,9 +10,5 @@ install: all
 
 relabel:
 
-indent:
-	../../scripts/Lindent $(wildcard selinux/*.h)
-
 distclean clean:
 	-rm -f selinux/*~
-
libselinux/man/Makefile +1 -1
index fdc0825f..42b1662a 100644
@@ -35,4 +35,4 @@ install: all
 
 relabel:
 
-indent distclean clean:
+format distclean clean:
libselinux/src/Makefile +0 -3
index 261c22d4..f7412cd1 100644
@@ -223,7 +223,4 @@ clean: clean-pywrap clean-rubywrap
 distclean: clean
 	rm -f $(GENERATED) $(SWIGFILES)
 
-indent:
-	../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
-
 .PHONY: all clean clean-pywrap clean-rubywrap pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
libselinux/utils/Makefile +0 -4
index 6b1dc7c9..20dc44eb 100644
@@ -72,8 +72,4 @@ clean:
 
 distclean: clean
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
-
libsemanage/Makefile +0 -4
index dbd240da..591ce5f2 100644
@@ -29,9 +29,5 @@ clean distclean:
 	$(MAKE) -C src $@
 	$(MAKE) -C tests $@
 
-indent:
-	$(MAKE) -C src $@
-	$(MAKE) -C include $@
-
 test: all
 	$(MAKE) -C tests test
libsemanage/include/Makefile +0 -3
index 6e44a28a..b08e48a0 100644
@@ -7,6 +7,3 @@ all:
 install: all
 	test -d $(DESTDIR)$(INCDIR) || install -m 755 -d $(DESTDIR)$(INCDIR)
 	install -m 644 $(wildcard semanage/*.h) $(DESTDIR)$(INCDIR)
-
-indent:
-	../../scripts/Lindent $(wildcard semanage/*.h)
libsemanage/src/Makefile +0 -3
index fa3449fb..8e025112 100644
@@ -167,7 +167,4 @@ clean:
 distclean: clean
 	rm -f $(GENERATED) $(SWIGFILES)
 
-indent:
-	../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
-
 .PHONY: all clean pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
libsemanage/utils/Makefile +0 -3
index 5b8fbb6b..0f382934 100644
@@ -13,7 +13,4 @@ clean:
 
 distclean: clean
 
-indent:
-
 relabel:
-
libsepol/Makefile +0 -6
index 5b5d03e2..223edcec 100644
@@ -20,11 +20,5 @@ clean:
 	$(MAKE) -C utils clean
 	$(MAKE) -C tests clean
 
-indent:
-	$(MAKE) -C src $@
-	$(MAKE) -C include $@
-	$(MAKE) -C utils $@
-
 test:
 	$(MAKE) -C tests test
-
libsepol/include/Makefile +0 -3
index 1ad4ecab..f49b6e19 100644
@@ -12,6 +12,3 @@ install: all
 	install -m 644 $(wildcard sepol/*.h) $(DESTDIR)$(INCDIR)
 	install -m 644 $(wildcard sepol/policydb/*.h) $(DESTDIR)$(INCDIR)/policydb
 	install -m 644 $(wildcard $(CILDIR)/include/cil/*.h) $(DESTDIR)$(INCDIR)/cil
-
-indent:
-	../../scripts/Lindent $(wildcard sepol/*.h)
libsepol/src/Makefile +0 -4
index 90aed394..bb2b7f5e 100644
@@ -105,7 +105,3 @@ relabel:
 
 clean: 
 	-rm -f $(LIBPC) $(LIBMAP) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(TARGET) $(CIL_GENERATED)
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
libsepol/utils/Makefile +0 -4
index 31932c11..a8bedf2e 100644
@@ -18,8 +18,4 @@ install: all
 clean:
 	-rm -f $(TARGETS) *.o 
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
-
mcstrans/src/Makefile +0 -1
index 88487068..609b2d45 100644
@@ -37,4 +37,3 @@ install: all
 
 clean: 
 	-rm -f $(OBJS) $(LOBJS) $(TARGET) $(PROG) $(PROG_OBJS) *~ \#*
-
mcstrans/utils/Makefile +0 -1
index 57a73932..1d70b4ed 100644
@@ -36,4 +36,3 @@ clean:
 	rm -f $(TARGETS) *.o *~ \#*
 
 relabel:
-
policycoreutils/Makefile +1 -1
index 7c9706e3..9186a9ef 100644
@@ -6,7 +6,7 @@ LIBSELINUX_LDLIBS := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH):../libselinux/s
 LIBSEMANAGE_LDLIBS := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH):../libsemanage/src" $(PKG_CONFIG) --libs libsemanage)
 export LIBSELINUX_LDLIBS LIBSEMANAGE_LDLIBS
 
-all install relabel clean indent:
+all install relabel clean:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
policycoreutils/hll/Makefile +1 -1
index fe720e57..390f3a34 100644
@@ -1,6 +1,6 @@
 SUBDIRS = pp
 
-all install relabel clean indent:
+all install relabel clean:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
policycoreutils/hll/pp/Makefile +0 -3
index 3c9b0efc..a0d475a3 100644
@@ -25,6 +25,3 @@ relabel:
 
 clean:
 	-rm -f pp $(PP_OBJS)
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
policycoreutils/load_policy/Makefile +0 -3
index c61fee70..7754cadc 100644
@@ -28,8 +28,5 @@ install: all
 clean:
 	-rm -f $(TARGETS) *.o 
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
 	/sbin/restorecon $(DESTDIR)$(SBINDIR)/load_policy 
policycoreutils/newrole/Makefile +0 -3
index df64483f..fcd2661f 100644
@@ -87,9 +87,6 @@ endif
 clean:
 	rm -f newrole *.o 
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
 	/sbin/restorecon $(DESTDIR)$(BINDIR)/newrole
 
policycoreutils/run_init/Makefile +0 -3
index c89fffb9..0e904dba 100644
@@ -53,8 +53,5 @@ endif
 clean:
 	-rm -f $(TARGETS) *.o 
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
 	/sbin/restorecon $(DESTDIR)$(SBINDIR)/run_init $(DESTDIR)$(SBINDIR)/open_init_pty
policycoreutils/scripts/Makefile +0 -2
index 6d8196c6..1058bbab 100644
@@ -21,6 +21,4 @@ install: all
 
 clean:
 
-indent:
-
 relabel:
policycoreutils/secon/Makefile +0 -3
index c9296f19..b2f276f9 100644
@@ -35,9 +35,6 @@ relabel:
 clean:
 	rm -f *.o core* secon *~ *.bak
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 bare: clean
 
 .PHONY: clean bare
policycoreutils/semodule/Makefile +0 -4
index 018ee2ca..47bde5f8 100644
@@ -35,7 +35,3 @@ relabel:
 
 clean:
 	-rm -f semodule *.o genhomedircon
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
policycoreutils/sestatus/Makefile +0 -3
index 1ce617c0..70f46956 100644
@@ -41,7 +41,4 @@ install: all
 clean:
 	rm -f sestatus *.o
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
policycoreutils/setfiles/Makefile +0 -3
index dab6313c..aa3e80fc 100644
@@ -43,8 +43,5 @@ install: all
 clean:
 	rm -f setfiles restorecon restorecon_xattr *.o
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
 	$(DESTDIR)$(SBINDIR)/restorecon $(DESTDIR)$(SBINDIR)/setfiles $(DESTDIR)$(SBINDIR)/restorecon_xattr
policycoreutils/setsebool/Makefile +0 -4
index 87494c55..360e0ea0 100644
@@ -35,7 +35,3 @@ relabel:
 
 clean:
 	-rm -f setsebool *.o
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
policycoreutils/unsetfiles/Makefile +0 -3
index 9cb51494..8dbf85af 100644
@@ -20,8 +20,5 @@ install: all
 clean:
 	-rm -f unsetfiles *.o
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
 	/sbin/restorecon $(DESTDIR)$(SBINDIR)/unsetfiles
python/Makefile +1 -1
index 00312dbd..c4c31a17 100644
@@ -1,6 +1,6 @@
 SUBDIRS = sepolicy audit2allow semanage sepolgen chcat po
 
-all install relabel clean indent test:
+all install relabel clean format test:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
python/audit2allow/Makefile +0 -3
index 28b8f7f2..90d10d1a 100644
@@ -51,7 +51,4 @@ install: all
 clean:
 	rm -f *~ *.o sepolgen-ifgen-attr-helper test_dummy_policy
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: ;
python/chcat/Makefile +0 -2
index 7b3ee17f..334b749d 100644
@@ -21,8 +21,6 @@ install: all
 
 clean:
 
-indent:
-
 relabel:
 
 test:
python/semanage/Makefile +0 -2
index 628d135a..51208d9e 100644
@@ -35,6 +35,4 @@ test:
 	@$(PYTHON) test-semanage.py -a
 clean:
 
-indent:
-
 relabel:
python/sepolgen/Makefile +0 -5
index 3aed3304..2ad7b13c 100644
@@ -11,10 +11,5 @@ clean:
 	rm -f *~ *.pyc
 	rm -f parser.out parsetab.py
 
-indent: ;
-
 test:
 	$(MAKE) -C tests $@
-
-
-
python/sepolgen/src/Makefile +0 -6
index 2815a13d..a2456652 100644
@@ -12,10 +12,4 @@ clean:
 	rm -f *~ *.pyc
 	rm -f parser.out parsetab.py
 
-indent: ;
-
-
 test: ;
-
-
-
restorecond/Makefile +0 -3
index b8b86eb4..51eb7608 100644
@@ -59,7 +59,4 @@ relabel: install
 clean:
 	-rm -f restorecond *.o *~
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 test:
sandbox/Makefile +0 -3
index 0799ab7c..eaaad75c 100644
@@ -50,7 +50,4 @@ clean:
 	-rm -f seunshare *.o *~
 	(cd po && $(MAKE) $@)
 
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
scripts/Lindent deleted +0 -18
deleted file mode 100755
index 9c4b3e2b..00000000
@@ -1,18 +0,0 @@
-#!/bin/sh
-PARAM="-npro -kr -i8 -ts8 -sob -l80 -ss -ncs -cp1"
-RES=`indent --version`
-V1=`echo $RES | cut -d' ' -f3 | cut -d'.' -f1`
-V2=`echo $RES | cut -d' ' -f3 | cut -d'.' -f2`
-V3=`echo $RES | cut -d' ' -f3 | cut -d'.' -f3`
-if [ $V1 -gt 2 ]; then
-  PARAM="$PARAM -il0"
-elif [ $V1 -eq 2 ]; then
-  if [ $V2 -gt 2 ]; then
-    PARAM="$PARAM -il0";
-  elif [ $V2 -eq 2 ]; then
-    if [ $V3 -ge 10 ]; then
-      PARAM="$PARAM -il0"
-    fi
-  fi
-fi
-indent $PARAM "$@"
semodule-utils/Makefile +1 -1
index e0a65792..81c97af0 100644
@@ -1,6 +1,6 @@
 SUBDIRS = semodule_package semodule_link semodule_expand
 
-all install relabel clean indent:
+all install relabel clean:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
semodule-utils/semodule_expand/Makefile +0 -4
index ad776b15..72f09a46 100644
@@ -27,7 +27,3 @@ relabel:
 
 clean:
 	-rm -f semodule_expand *.o
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
semodule-utils/semodule_link/Makefile +0 -4
index 936d161c..83590c07 100644
@@ -27,7 +27,3 @@ relabel:
 
 clean:
 	-rm -f semodule_link *.o
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
semodule-utils/semodule_package/Makefile +0 -4
index 6a289f73..2465f987 100644
@@ -29,7 +29,3 @@ relabel:
 
 clean:
 	-rm -f semodule_package semodule_unpackage *.o
-
-indent:
-	../../scripts/Lindent $(wildcard *.[ch])
-
Signature · 1 line
2.51.0
maintainer Acked-by on v6 1/1
#15 lore raw
Quoted 21 lines · Rahul Sandhu

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

As well as this, replace all the existing indent targets with a single toplevel format target. Managing all the source files to be formatted is not pretty to maintain, and doesn't really give us much.

Also define a toplevel check-format target to verify that all code is formatted properly. This only becomes useful in the future once we have reformatted all existing code, but is wired up for now.

For the future:
1. Reformat all existing code. I understand this is a big change, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.

Quoted 5 lines

2. Add a CI target to check that all code is formatted as per the new clang-format configuration. The check-format target can be used for this.

Will defer to distro package maintainers to decide when to apply make format to the entire tree. Common practice I think is to do it just before or after a release.

Quoted 781 lines
---
 .clang-format                            | 130 +++++++++++++++++++++++
 CONTRIBUTING.md                          |   2 +
 Makefile                                 |  11 +-
 checkpolicy/Makefile                     |   3 -
 gui/Makefile                             |   2 -
 libselinux/Makefile                      |   2 +-
 libselinux/include/Makefile              |   4 -
 libselinux/man/Makefile                  |   2 +-
 libselinux/src/Makefile                  |   3 -
 libselinux/utils/Makefile                |   4 -
 libsemanage/Makefile                     |   4 -
 libsemanage/include/Makefile             |   3 -
 libsemanage/src/Makefile                 |   3 -
 libsemanage/utils/Makefile               |   3 -
 libsepol/Makefile                        |   6 --
 libsepol/include/Makefile                |   3 -
 libsepol/src/Makefile                    |   4 -
 libsepol/utils/Makefile                  |   4 -
 mcstrans/src/Makefile                    |   1 -
 mcstrans/utils/Makefile                  |   1 -
 policycoreutils/Makefile                 |   2 +-
 policycoreutils/hll/Makefile             |   2 +-
 policycoreutils/hll/pp/Makefile          |   3 -
 policycoreutils/load_policy/Makefile     |   3 -
 policycoreutils/newrole/Makefile         |   3 -
 policycoreutils/run_init/Makefile        |   3 -
 policycoreutils/scripts/Makefile         |   2 -
 policycoreutils/secon/Makefile           |   3 -
 policycoreutils/semodule/Makefile        |   4 -
 policycoreutils/sestatus/Makefile        |   3 -
 policycoreutils/setfiles/Makefile        |   3 -
 policycoreutils/setsebool/Makefile       |   4 -
 policycoreutils/unsetfiles/Makefile      |   3 -
 python/Makefile                          |   2 +-
 python/audit2allow/Makefile              |   3 -
 python/chcat/Makefile                    |   2 -
 python/semanage/Makefile                 |   2 -
 python/sepolgen/Makefile                 |   5 -
 python/sepolgen/src/Makefile             |   6 --
 restorecond/Makefile                     |   3 -
 sandbox/Makefile                         |   3 -
 scripts/Lindent                          |  18 ----
 semodule-utils/Makefile                  |   2 +-
 semodule-utils/semodule_expand/Makefile  |   4 -
 semodule-utils/semodule_link/Makefile    |   4 -
 semodule-utils/semodule_package/Makefile |   4 -
 46 files changed, 148 insertions(+), 143 deletions(-)
 create mode 100644 .clang-format
 delete mode 100755 scripts/Lindent

v2: remove linux kernel ForEachMacros and replace them with ours
v3: replace the indent target with the new format target. also remove any
mention of .editorconfig from the commit message; those changes are better suited for another patch (they're small and self-contained enough that they're a smaller thing to merge).
v4: drop the subdir format targets entirely, replacing with a single toplevel
format and check-format target.
v5: add back relabel target accidently removed.
v6: remove all references to being an RFC in the commit message.

.clang-format new file +130 -0
new file mode 100644
index 00000000..35595d87
@@ -0,0 +1,130 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# clang-format configuration file. Intended for clang-format >= 11.
+#
+# For more information, see:
+#
+#   Documentation/dev-tools/clang-format.rst
+#   https://clang.llvm.org/docs/ClangFormat.html
+#   https://clang.llvm.org/docs/ClangFormatStyleOptions.html
+#
+---
+AccessModifierOffset: -4
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlines: Left
+AlignOperands: true
+AlignTrailingComments: false
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: false
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:
+  AfterClass: false
+  AfterControlStatement: false
+  AfterEnum: false
+  AfterFunction: true
+  AfterNamespace: true
+  AfterObjCDeclaration: false
+  AfterStruct: false
+  AfterUnion: false
+  AfterExternBlock: false
+  BeforeCatch: false
+  BeforeElse: false
+  IndentBraces: false
+  SplitEmptyFunction: true
+  SplitEmptyRecord: true
+  SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Custom
+BreakBeforeInheritanceComma: false
+BreakBeforeTernaryOperators: false
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeComma
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: false
+ColumnLimit: 80
+CommentPragmas: '^ IWYU pragma:'
+CompactNamespaces: false
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+ConstructorInitializerIndentWidth: 8
+ContinuationIndentWidth: 8
+Cpp11BracedListStyle: false
+DerivePointerAlignment: false
+DisableFormat: false
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: false
+
+# Taken from:
+#   git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' \
+#   | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$,  - '\1'," \
+#   | LC_ALL=C sort -u
+ForEachMacros:
+  - 'cil_list_for_each'
+  - 'cil_stack_for_each'
+  - 'cil_stack_for_each_starting_at'
+  - 'ebitmap_for_each_bit'
+  - 'ebitmap_for_each_positive_bit'
+
+IncludeBlocks: Preserve
+IncludeCategories:
+  - Regex: '.*'
+    Priority: 1
+IncludeIsMainRegex: '(Test)?$'
+IndentCaseLabels: false
+IndentGotoLabels: false
+IndentPPDirectives: None
+IndentWidth: 8
+IndentWrappedFunctionNames: false
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: false
+MacroBlockBegin: ''
+MacroBlockEnd: ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Auto
+ObjCBlockIndentWidth: 8
+ObjCSpaceAfterProperty: true
+ObjCSpaceBeforeProtocolList: true
+
+# Taken from git's rules
+PenaltyBreakAssignment: 10
+PenaltyBreakBeforeFirstCallParameter: 30
+PenaltyBreakComment: 10
+PenaltyBreakFirstLessLess: 0
+PenaltyBreakString: 10
+PenaltyExcessCharacter: 100
+PenaltyReturnTypeOnItsOwnLine: 60
+
+PointerAlignment: Right
+ReflowComments: false
+SortIncludes: false
+SortUsingDeclarations: false
+SpaceAfterCStyleCast: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatementsExceptForEachMacros
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles: false
+SpacesInContainerLiterals: false
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard: Cpp03
+TabWidth: 8
+UseTab: Always
+...
CONTRIBUTING.md +2 -0
index c501cf84..7ec8cb0f 100644
@@ -53,6 +53,8 @@ When preparing patches, please follow these guidelines:
 -   Separate large patches into logical patches
 -   Patch descriptions must end with your "Signed-off-by" line. This means your
     code meets the Developer's certificate of origin, see below.
+-   C code should be formatted using clang-format, using the .clang-format
+    configuration file at the root of this repository.
 
 When adding new, large features or tools it is best to discuss the
 design on the mailing list prior to submitting the patch.
Makefile +10 -1
index 2ffba8e9..972d5525 100644
@@ -36,7 +36,7 @@ ifneq ($(DESTDIR),)
        export LIBSEPOLA
 endif
 
-all install relabel clean test indent:
+all install relabel clean test:
        @for subdir in $(SUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
@@ -50,3 +50,12 @@ distclean:
        @for subdir in $(DISTCLEANSUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
+
+# We shouldn't have any unformatted files in the repo without an explicit exception.
+FORMAT_SOURCE_FILES := $(shell find $(SUBDIRS) -type f \( -name '*.c' -o -name '*.h' \))
+
+format:
+       clang-format -i $(FORMAT_SOURCE_FILES)
+
+check-format:
+       clang-format --dry-run -Werror $(FORMAT_SOURCE_FILES)
checkpolicy/Makefile +0 -3
index 6e8008e3..0055f495 100644
@@ -77,6 +77,3 @@ relabel: install
 clean:
        -rm -f $(TARGETS) $(CHECKPOLOBJS) $(CHECKMODOBJS) y.tab.c y.tab.h lex.yy.c tests/testpol.conf tests/testpol.bin
        $(MAKE) -C test clean
-
-indent:
-       ../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
gui/Makefile +0 -2
index b29610d4..b1a94224 100644
@@ -60,8 +60,6 @@ install: all
 clean:
        (cd po && $(MAKE) $@)
 
-indent:
-
 relabel:
 
 test:
libselinux/Makefile +1 -1
index a50b6491..aeede2b5 100644
@@ -50,7 +50,7 @@ COMPILER := clang
 endif
 export COMPILER
 
-all install relabel clean distclean indent:
+all install relabel clean distclean:
        @for subdir in $(SUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
libselinux/include/Makefile +0 -4
index 8ab9ce92..a1e322ac 100644
@@ -10,9 +10,5 @@ install: all
 
 relabel:
 
-indent:
-       ../../scripts/Lindent $(wildcard selinux/*.h)
-
 distclean clean:
        -rm -f selinux/*~
-
libselinux/man/Makefile +1 -1
index fdc0825f..42b1662a 100644
@@ -35,4 +35,4 @@ install: all
 
 relabel:
 
-indent distclean clean:
+format distclean clean:
libselinux/src/Makefile +0 -3
index 261c22d4..f7412cd1 100644
@@ -223,7 +223,4 @@ clean: clean-pywrap clean-rubywrap
 distclean: clean
        rm -f $(GENERATED) $(SWIGFILES)
 
-indent:
-       ../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
-
 .PHONY: all clean clean-pywrap clean-rubywrap pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
libselinux/utils/Makefile +0 -4
index 6b1dc7c9..20dc44eb 100644
@@ -72,8 +72,4 @@ clean:
 
 distclean: clean
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
-
libsemanage/Makefile +0 -4
index dbd240da..591ce5f2 100644
@@ -29,9 +29,5 @@ clean distclean:
        $(MAKE) -C src $@
        $(MAKE) -C tests $@
 
-indent:
-       $(MAKE) -C src $@
-       $(MAKE) -C include $@
-
 test: all
        $(MAKE) -C tests test
libsemanage/include/Makefile +0 -3
index 6e44a28a..b08e48a0 100644
@@ -7,6 +7,3 @@ all:
 install: all
        test -d $(DESTDIR)$(INCDIR) || install -m 755 -d $(DESTDIR)$(INCDIR)
        install -m 644 $(wildcard semanage/*.h) $(DESTDIR)$(INCDIR)
-
-indent:
-       ../../scripts/Lindent $(wildcard semanage/*.h)
libsemanage/src/Makefile +0 -3
index fa3449fb..8e025112 100644
@@ -167,7 +167,4 @@ clean:
 distclean: clean
        rm -f $(GENERATED) $(SWIGFILES)
 
-indent:
-       ../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
-
 .PHONY: all clean pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
libsemanage/utils/Makefile +0 -3
index 5b8fbb6b..0f382934 100644
@@ -13,7 +13,4 @@ clean:
 
 distclean: clean
 
-indent:
-
 relabel:
-
libsepol/Makefile +0 -6
index 5b5d03e2..223edcec 100644
@@ -20,11 +20,5 @@ clean:
        $(MAKE) -C utils clean
        $(MAKE) -C tests clean
 
-indent:
-       $(MAKE) -C src $@
-       $(MAKE) -C include $@
-       $(MAKE) -C utils $@
-
 test:
        $(MAKE) -C tests test
-
libsepol/include/Makefile +0 -3
index 1ad4ecab..f49b6e19 100644
@@ -12,6 +12,3 @@ install: all
        install -m 644 $(wildcard sepol/*.h) $(DESTDIR)$(INCDIR)
        install -m 644 $(wildcard sepol/policydb/*.h) $(DESTDIR)$(INCDIR)/policydb
        install -m 644 $(wildcard $(CILDIR)/include/cil/*.h) $(DESTDIR)$(INCDIR)/cil
-
-indent:
-       ../../scripts/Lindent $(wildcard sepol/*.h)
libsepol/src/Makefile +0 -4
index 90aed394..bb2b7f5e 100644
@@ -105,7 +105,3 @@ relabel:
 
 clean:
        -rm -f $(LIBPC) $(LIBMAP) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(TARGET) $(CIL_GENERATED)
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
libsepol/utils/Makefile +0 -4
index 31932c11..a8bedf2e 100644
@@ -18,8 +18,4 @@ install: all
 clean:
        -rm -f $(TARGETS) *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
-
mcstrans/src/Makefile +0 -1
index 88487068..609b2d45 100644
@@ -37,4 +37,3 @@ install: all
 
 clean:
        -rm -f $(OBJS) $(LOBJS) $(TARGET) $(PROG) $(PROG_OBJS) *~ \#*
-
mcstrans/utils/Makefile +0 -1
index 57a73932..1d70b4ed 100644
@@ -36,4 +36,3 @@ clean:
        rm -f $(TARGETS) *.o *~ \#*
 
 relabel:
-
policycoreutils/Makefile +1 -1
index 7c9706e3..9186a9ef 100644
@@ -6,7 +6,7 @@ LIBSELINUX_LDLIBS := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH):../libselinux/s
 LIBSEMANAGE_LDLIBS := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH):../libsemanage/src" $(PKG_CONFIG) --libs libsemanage)
 export LIBSELINUX_LDLIBS LIBSEMANAGE_LDLIBS
 
-all install relabel clean indent:
+all install relabel clean:
        @for subdir in $(SUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
policycoreutils/hll/Makefile +1 -1
index fe720e57..390f3a34 100644
@@ -1,6 +1,6 @@
 SUBDIRS = pp
 
-all install relabel clean indent:
+all install relabel clean:
        @for subdir in $(SUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
policycoreutils/hll/pp/Makefile +0 -3
index 3c9b0efc..a0d475a3 100644
@@ -25,6 +25,3 @@ relabel:
 
 clean:
        -rm -f pp $(PP_OBJS)
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
policycoreutils/load_policy/Makefile +0 -3
index c61fee70..7754cadc 100644
@@ -28,8 +28,5 @@ install: all
 clean:
        -rm -f $(TARGETS) *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
        /sbin/restorecon $(DESTDIR)$(SBINDIR)/load_policy
policycoreutils/newrole/Makefile +0 -3
index df64483f..fcd2661f 100644
@@ -87,9 +87,6 @@ endif
 clean:
        rm -f newrole *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
        /sbin/restorecon $(DESTDIR)$(BINDIR)/newrole
 
policycoreutils/run_init/Makefile +0 -3
index c89fffb9..0e904dba 100644
@@ -53,8 +53,5 @@ endif
 clean:
        -rm -f $(TARGETS) *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
        /sbin/restorecon $(DESTDIR)$(SBINDIR)/run_init $(DESTDIR)$(SBINDIR)/open_init_pty
policycoreutils/scripts/Makefile +0 -2
index 6d8196c6..1058bbab 100644
@@ -21,6 +21,4 @@ install: all
 
 clean:
 
-indent:
-
 relabel:
policycoreutils/secon/Makefile +0 -3
index c9296f19..b2f276f9 100644
@@ -35,9 +35,6 @@ relabel:
 clean:
        rm -f *.o core* secon *~ *.bak
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 bare: clean
 
 .PHONY: clean bare
policycoreutils/semodule/Makefile +0 -4
index 018ee2ca..47bde5f8 100644
@@ -35,7 +35,3 @@ relabel:
 
 clean:
        -rm -f semodule *.o genhomedircon
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
policycoreutils/sestatus/Makefile +0 -3
index 1ce617c0..70f46956 100644
@@ -41,7 +41,4 @@ install: all
 clean:
        rm -f sestatus *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
policycoreutils/setfiles/Makefile +0 -3
index dab6313c..aa3e80fc 100644
@@ -43,8 +43,5 @@ install: all
 clean:
        rm -f setfiles restorecon restorecon_xattr *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
        $(DESTDIR)$(SBINDIR)/restorecon $(DESTDIR)$(SBINDIR)/setfiles $(DESTDIR)$(SBINDIR)/restorecon_xattr
policycoreutils/setsebool/Makefile +0 -4
index 87494c55..360e0ea0 100644
@@ -35,7 +35,3 @@ relabel:
 
 clean:
        -rm -f setsebool *.o
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
policycoreutils/unsetfiles/Makefile +0 -3
index 9cb51494..8dbf85af 100644
@@ -20,8 +20,5 @@ install: all
 clean:
        -rm -f unsetfiles *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
        /sbin/restorecon $(DESTDIR)$(SBINDIR)/unsetfiles
python/Makefile +1 -1
index 00312dbd..c4c31a17 100644
@@ -1,6 +1,6 @@
 SUBDIRS = sepolicy audit2allow semanage sepolgen chcat po
 
-all install relabel clean indent test:
+all install relabel clean format test:
        @for subdir in $(SUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
python/audit2allow/Makefile +0 -3
index 28b8f7f2..90d10d1a 100644
@@ -51,7 +51,4 @@ install: all
 clean:
        rm -f *~ *.o sepolgen-ifgen-attr-helper test_dummy_policy
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: ;
python/chcat/Makefile +0 -2
index 7b3ee17f..334b749d 100644
@@ -21,8 +21,6 @@ install: all
 
 clean:
 
-indent:
-
 relabel:
 
 test:
python/semanage/Makefile +0 -2
index 628d135a..51208d9e 100644
@@ -35,6 +35,4 @@ test:
        @$(PYTHON) test-semanage.py -a
 clean:
 
-indent:
-
 relabel:
python/sepolgen/Makefile +0 -5
index 3aed3304..2ad7b13c 100644
@@ -11,10 +11,5 @@ clean:
        rm -f *~ *.pyc
        rm -f parser.out parsetab.py
 
-indent: ;
-
 test:
        $(MAKE) -C tests $@
-
-
-
python/sepolgen/src/Makefile +0 -6
index 2815a13d..a2456652 100644
@@ -12,10 +12,4 @@ clean:
        rm -f *~ *.pyc
        rm -f parser.out parsetab.py
 
-indent: ;
-
-
 test: ;
-
-
-
restorecond/Makefile +0 -3
index b8b86eb4..51eb7608 100644
@@ -59,7 +59,4 @@ relabel: install
 clean:
        -rm -f restorecond *.o *~
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 test:
sandbox/Makefile +0 -3
index 0799ab7c..eaaad75c 100644
@@ -50,7 +50,4 @@ clean:
        -rm -f seunshare *.o *~
        (cd po && $(MAKE) $@)
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
scripts/Lindent deleted +0 -18
deleted file mode 100755
index 9c4b3e2b..00000000
@@ -1,18 +0,0 @@
-#!/bin/sh
-PARAM="-npro -kr -i8 -ts8 -sob -l80 -ss -ncs -cp1"
-RES=`indent --version`
-V1=`echo $RES | cut -d' ' -f3 | cut -d'.' -f1`
-V2=`echo $RES | cut -d' ' -f3 | cut -d'.' -f2`
-V3=`echo $RES | cut -d' ' -f3 | cut -d'.' -f3`
-if [ $V1 -gt 2 ]; then
-  PARAM="$PARAM -il0"
-elif [ $V1 -eq 2 ]; then
-  if [ $V2 -gt 2 ]; then
-    PARAM="$PARAM -il0";
-  elif [ $V2 -eq 2 ]; then
-    if [ $V3 -ge 10 ]; then
-      PARAM="$PARAM -il0"
-    fi
-  fi
-fi
-indent $PARAM "$@"
semodule-utils/Makefile +1 -1
index e0a65792..81c97af0 100644
@@ -1,6 +1,6 @@
 SUBDIRS = semodule_package semodule_link semodule_expand
 
-all install relabel clean indent:
+all install relabel clean:
        @for subdir in $(SUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
semodule-utils/semodule_expand/Makefile +0 -4
index ad776b15..72f09a46 100644
@@ -27,7 +27,3 @@ relabel:
 
 clean:
        -rm -f semodule_expand *.o
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
semodule-utils/semodule_link/Makefile +0 -4
index 936d161c..83590c07 100644
@@ -27,7 +27,3 @@ relabel:
 
 clean:
        -rm -f semodule_link *.o
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
semodule-utils/semodule_package/Makefile +0 -5
index 6a289f73..2465f987 100644
@@ -29,7 +29,3 @@ relabel:
 
 clean:
        -rm -f semodule_package semodule_unpackage *.o
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
--

2.51.0

#16 lore raw

When to apply `make format` to the entire tree

Quoted 26 lines · Stephen Smalley
Quoted 25 lines · Rahul Sandhu

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

As well as this, replace all the existing indent targets with a single toplevel format target. Managing all the source files to be formatted is not pretty to maintain, and doesn't really give us much.

Also define a toplevel check-format target to verify that all code is formatted properly. This only becomes useful in the future once we have reformatted all existing code, but is wired up for now.

For the future:
1. Reformat all existing code. I understand this is a big change, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.

2. Add a CI target to check that all code is formatted as per the new clang-format configuration. The check-format target can be used for this.

Quoted 7 lines
Quoted 1 line

Will defer to distro package maintainers to decide when to apply make format to the entire tree. Common practice I think is to do it just before or after a release.

For me, it's better do it before the release so I would not need to backport the format patch together with some future change.

Petr

Quoted 781 lines
Quoted 781 lines
---
 .clang-format                            | 130 +++++++++++++++++++++++
 CONTRIBUTING.md                          |   2 +
 Makefile                                 |  11 +-
 checkpolicy/Makefile                     |   3 -
 gui/Makefile                             |   2 -
 libselinux/Makefile                      |   2 +-
 libselinux/include/Makefile              |   4 -
 libselinux/man/Makefile                  |   2 +-
 libselinux/src/Makefile                  |   3 -
 libselinux/utils/Makefile                |   4 -
 libsemanage/Makefile                     |   4 -
 libsemanage/include/Makefile             |   3 -
 libsemanage/src/Makefile                 |   3 -
 libsemanage/utils/Makefile               |   3 -
 libsepol/Makefile                        |   6 --
 libsepol/include/Makefile                |   3 -
 libsepol/src/Makefile                    |   4 -
 libsepol/utils/Makefile                  |   4 -
 mcstrans/src/Makefile                    |   1 -
 mcstrans/utils/Makefile                  |   1 -
 policycoreutils/Makefile                 |   2 +-
 policycoreutils/hll/Makefile             |   2 +-
 policycoreutils/hll/pp/Makefile          |   3 -
 policycoreutils/load_policy/Makefile     |   3 -
 policycoreutils/newrole/Makefile         |   3 -
 policycoreutils/run_init/Makefile        |   3 -
 policycoreutils/scripts/Makefile         |   2 -
 policycoreutils/secon/Makefile           |   3 -
 policycoreutils/semodule/Makefile        |   4 -
 policycoreutils/sestatus/Makefile        |   3 -
 policycoreutils/setfiles/Makefile        |   3 -
 policycoreutils/setsebool/Makefile       |   4 -
 policycoreutils/unsetfiles/Makefile      |   3 -
 python/Makefile                          |   2 +-
 python/audit2allow/Makefile              |   3 -
 python/chcat/Makefile                    |   2 -
 python/semanage/Makefile                 |   2 -
 python/sepolgen/Makefile                 |   5 -
 python/sepolgen/src/Makefile             |   6 --
 restorecond/Makefile                     |   3 -
 sandbox/Makefile                         |   3 -
 scripts/Lindent                          |  18 ----
 semodule-utils/Makefile                  |   2 +-
 semodule-utils/semodule_expand/Makefile  |   4 -
 semodule-utils/semodule_link/Makefile    |   4 -
 semodule-utils/semodule_package/Makefile |   4 -
 46 files changed, 148 insertions(+), 143 deletions(-)
 create mode 100644 .clang-format
 delete mode 100755 scripts/Lindent

v2: remove linux kernel ForEachMacros and replace them with ours
v3: replace the indent target with the new format target. also remove any
mention of .editorconfig from the commit message; those changes are better suited for another patch (they're small and self-contained enough that they're a smaller thing to merge).
v4: drop the subdir format targets entirely, replacing with a single toplevel
format and check-format target.
v5: add back relabel target accidently removed.
v6: remove all references to being an RFC in the commit message.

.clang-format new file +130 -0
new file mode 100644
index 00000000..35595d87
@@ -0,0 +1,130 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# clang-format configuration file. Intended for clang-format >= 11.
+#
+# For more information, see:
+#
+#   Documentation/dev-tools/clang-format.rst
+#   https://clang.llvm.org/docs/ClangFormat.html
+#   https://clang.llvm.org/docs/ClangFormatStyleOptions.html
+#
+---
+AccessModifierOffset: -4
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlines: Left
+AlignOperands: true
+AlignTrailingComments: false
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: false
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:
+  AfterClass: false
+  AfterControlStatement: false
+  AfterEnum: false
+  AfterFunction: true
+  AfterNamespace: true
+  AfterObjCDeclaration: false
+  AfterStruct: false
+  AfterUnion: false
+  AfterExternBlock: false
+  BeforeCatch: false
+  BeforeElse: false
+  IndentBraces: false
+  SplitEmptyFunction: true
+  SplitEmptyRecord: true
+  SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Custom
+BreakBeforeInheritanceComma: false
+BreakBeforeTernaryOperators: false
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeComma
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: false
+ColumnLimit: 80
+CommentPragmas: '^ IWYU pragma:'
+CompactNamespaces: false
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+ConstructorInitializerIndentWidth: 8
+ContinuationIndentWidth: 8
+Cpp11BracedListStyle: false
+DerivePointerAlignment: false
+DisableFormat: false
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: false
+
+# Taken from:
+#   git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' \
+#   | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$,  - '\1'," \
+#   | LC_ALL=C sort -u
+ForEachMacros:
+  - 'cil_list_for_each'
+  - 'cil_stack_for_each'
+  - 'cil_stack_for_each_starting_at'
+  - 'ebitmap_for_each_bit'
+  - 'ebitmap_for_each_positive_bit'
+
+IncludeBlocks: Preserve
+IncludeCategories:
+  - Regex: '.*'
+    Priority: 1
+IncludeIsMainRegex: '(Test)?$'
+IndentCaseLabels: false
+IndentGotoLabels: false
+IndentPPDirectives: None
+IndentWidth: 8
+IndentWrappedFunctionNames: false
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: false
+MacroBlockBegin: ''
+MacroBlockEnd: ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Auto
+ObjCBlockIndentWidth: 8
+ObjCSpaceAfterProperty: true
+ObjCSpaceBeforeProtocolList: true
+
+# Taken from git's rules
+PenaltyBreakAssignment: 10
+PenaltyBreakBeforeFirstCallParameter: 30
+PenaltyBreakComment: 10
+PenaltyBreakFirstLessLess: 0
+PenaltyBreakString: 10
+PenaltyExcessCharacter: 100
+PenaltyReturnTypeOnItsOwnLine: 60
+
+PointerAlignment: Right
+ReflowComments: false
+SortIncludes: false
+SortUsingDeclarations: false
+SpaceAfterCStyleCast: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatementsExceptForEachMacros
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles: false
+SpacesInContainerLiterals: false
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard: Cpp03
+TabWidth: 8
+UseTab: Always
+...
CONTRIBUTING.md +2 -0
index c501cf84..7ec8cb0f 100644
@@ -53,6 +53,8 @@ When preparing patches, please follow these guidelines:
 -   Separate large patches into logical patches
 -   Patch descriptions must end with your "Signed-off-by" line. This means your
     code meets the Developer's certificate of origin, see below.
+-   C code should be formatted using clang-format, using the .clang-format
+    configuration file at the root of this repository.
 
 When adding new, large features or tools it is best to discuss the
 design on the mailing list prior to submitting the patch.
Makefile +10 -1
index 2ffba8e9..972d5525 100644
@@ -36,7 +36,7 @@ ifneq ($(DESTDIR),)
        export LIBSEPOLA
 endif
 
-all install relabel clean test indent:
+all install relabel clean test:
        @for subdir in $(SUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
@@ -50,3 +50,12 @@ distclean:
        @for subdir in $(DISTCLEANSUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
+
+# We shouldn't have any unformatted files in the repo without an explicit exception.
+FORMAT_SOURCE_FILES := $(shell find $(SUBDIRS) -type f \( -name '*.c' -o -name '*.h' \))
+
+format:
+       clang-format -i $(FORMAT_SOURCE_FILES)
+
+check-format:
+       clang-format --dry-run -Werror $(FORMAT_SOURCE_FILES)
checkpolicy/Makefile +0 -3
index 6e8008e3..0055f495 100644
@@ -77,6 +77,3 @@ relabel: install
 clean:
        -rm -f $(TARGETS) $(CHECKPOLOBJS) $(CHECKMODOBJS) y.tab.c y.tab.h lex.yy.c tests/testpol.conf tests/testpol.bin
        $(MAKE) -C test clean
-
-indent:
-       ../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
gui/Makefile +0 -2
index b29610d4..b1a94224 100644
@@ -60,8 +60,6 @@ install: all
 clean:
        (cd po && $(MAKE) $@)
 
-indent:
-
 relabel:
 
 test:
libselinux/Makefile +1 -1
index a50b6491..aeede2b5 100644
@@ -50,7 +50,7 @@ COMPILER := clang
 endif
 export COMPILER
 
-all install relabel clean distclean indent:
+all install relabel clean distclean:
        @for subdir in $(SUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
libselinux/include/Makefile +0 -4
index 8ab9ce92..a1e322ac 100644
@@ -10,9 +10,5 @@ install: all
 
 relabel:
 
-indent:
-       ../../scripts/Lindent $(wildcard selinux/*.h)
-
 distclean clean:
        -rm -f selinux/*~
-
libselinux/man/Makefile +1 -1
index fdc0825f..42b1662a 100644
@@ -35,4 +35,4 @@ install: all
 
 relabel:
 
-indent distclean clean:
+format distclean clean:
libselinux/src/Makefile +0 -3
index 261c22d4..f7412cd1 100644
@@ -223,7 +223,4 @@ clean: clean-pywrap clean-rubywrap
 distclean: clean
        rm -f $(GENERATED) $(SWIGFILES)
 
-indent:
-       ../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
-
 .PHONY: all clean clean-pywrap clean-rubywrap pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
libselinux/utils/Makefile +0 -4
index 6b1dc7c9..20dc44eb 100644
@@ -72,8 +72,4 @@ clean:
 
 distclean: clean
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
-
libsemanage/Makefile +0 -4
index dbd240da..591ce5f2 100644
@@ -29,9 +29,5 @@ clean distclean:
        $(MAKE) -C src $@
        $(MAKE) -C tests $@
 
-indent:
-       $(MAKE) -C src $@
-       $(MAKE) -C include $@
-
 test: all
        $(MAKE) -C tests test
libsemanage/include/Makefile +0 -3
index 6e44a28a..b08e48a0 100644
@@ -7,6 +7,3 @@ all:
 install: all
        test -d $(DESTDIR)$(INCDIR) || install -m 755 -d $(DESTDIR)$(INCDIR)
        install -m 644 $(wildcard semanage/*.h) $(DESTDIR)$(INCDIR)
-
-indent:
-       ../../scripts/Lindent $(wildcard semanage/*.h)
libsemanage/src/Makefile +0 -3
index fa3449fb..8e025112 100644
@@ -167,7 +167,4 @@ clean:
 distclean: clean
        rm -f $(GENERATED) $(SWIGFILES)
 
-indent:
-       ../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
-
 .PHONY: all clean pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
libsemanage/utils/Makefile +0 -3
index 5b8fbb6b..0f382934 100644
@@ -13,7 +13,4 @@ clean:
 
 distclean: clean
 
-indent:
-
 relabel:
-
libsepol/Makefile +0 -6
index 5b5d03e2..223edcec 100644
@@ -20,11 +20,5 @@ clean:
        $(MAKE) -C utils clean
        $(MAKE) -C tests clean
 
-indent:
-       $(MAKE) -C src $@
-       $(MAKE) -C include $@
-       $(MAKE) -C utils $@
-
 test:
        $(MAKE) -C tests test
-
libsepol/include/Makefile +0 -3
index 1ad4ecab..f49b6e19 100644
@@ -12,6 +12,3 @@ install: all
        install -m 644 $(wildcard sepol/*.h) $(DESTDIR)$(INCDIR)
        install -m 644 $(wildcard sepol/policydb/*.h) $(DESTDIR)$(INCDIR)/policydb
        install -m 644 $(wildcard $(CILDIR)/include/cil/*.h) $(DESTDIR)$(INCDIR)/cil
-
-indent:
-       ../../scripts/Lindent $(wildcard sepol/*.h)
libsepol/src/Makefile +0 -4
index 90aed394..bb2b7f5e 100644
@@ -105,7 +105,3 @@ relabel:
 
 clean:
        -rm -f $(LIBPC) $(LIBMAP) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(TARGET) $(CIL_GENERATED)
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
libsepol/utils/Makefile +0 -4
index 31932c11..a8bedf2e 100644
@@ -18,8 +18,4 @@ install: all
 clean:
        -rm -f $(TARGETS) *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
-
mcstrans/src/Makefile +0 -1
index 88487068..609b2d45 100644
@@ -37,4 +37,3 @@ install: all
 
 clean:
        -rm -f $(OBJS) $(LOBJS) $(TARGET) $(PROG) $(PROG_OBJS) *~ \#*
-
mcstrans/utils/Makefile +0 -1
index 57a73932..1d70b4ed 100644
@@ -36,4 +36,3 @@ clean:
        rm -f $(TARGETS) *.o *~ \#*
 
 relabel:
-
policycoreutils/Makefile +1 -1
index 7c9706e3..9186a9ef 100644
@@ -6,7 +6,7 @@ LIBSELINUX_LDLIBS := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH):../libselinux/s
 LIBSEMANAGE_LDLIBS := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH):../libsemanage/src" $(PKG_CONFIG) --libs libsemanage)
 export LIBSELINUX_LDLIBS LIBSEMANAGE_LDLIBS
 
-all install relabel clean indent:
+all install relabel clean:
        @for subdir in $(SUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
policycoreutils/hll/Makefile +1 -1
index fe720e57..390f3a34 100644
@@ -1,6 +1,6 @@
 SUBDIRS = pp
 
-all install relabel clean indent:
+all install relabel clean:
        @for subdir in $(SUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
policycoreutils/hll/pp/Makefile +0 -3
index 3c9b0efc..a0d475a3 100644
@@ -25,6 +25,3 @@ relabel:
 
 clean:
        -rm -f pp $(PP_OBJS)
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
policycoreutils/load_policy/Makefile +0 -3
index c61fee70..7754cadc 100644
@@ -28,8 +28,5 @@ install: all
 clean:
        -rm -f $(TARGETS) *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
        /sbin/restorecon $(DESTDIR)$(SBINDIR)/load_policy
policycoreutils/newrole/Makefile +0 -3
index df64483f..fcd2661f 100644
@@ -87,9 +87,6 @@ endif
 clean:
        rm -f newrole *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
        /sbin/restorecon $(DESTDIR)$(BINDIR)/newrole
 
policycoreutils/run_init/Makefile +0 -3
index c89fffb9..0e904dba 100644
@@ -53,8 +53,5 @@ endif
 clean:
        -rm -f $(TARGETS) *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
        /sbin/restorecon $(DESTDIR)$(SBINDIR)/run_init $(DESTDIR)$(SBINDIR)/open_init_pty
policycoreutils/scripts/Makefile +0 -2
index 6d8196c6..1058bbab 100644
@@ -21,6 +21,4 @@ install: all
 
 clean:
 
-indent:
-
 relabel:
policycoreutils/secon/Makefile +0 -3
index c9296f19..b2f276f9 100644
@@ -35,9 +35,6 @@ relabel:
 clean:
        rm -f *.o core* secon *~ *.bak
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 bare: clean
 
 .PHONY: clean bare
policycoreutils/semodule/Makefile +0 -4
index 018ee2ca..47bde5f8 100644
@@ -35,7 +35,3 @@ relabel:
 
 clean:
        -rm -f semodule *.o genhomedircon
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
policycoreutils/sestatus/Makefile +0 -3
index 1ce617c0..70f46956 100644
@@ -41,7 +41,4 @@ install: all
 clean:
        rm -f sestatus *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
policycoreutils/setfiles/Makefile +0 -3
index dab6313c..aa3e80fc 100644
@@ -43,8 +43,5 @@ install: all
 clean:
        rm -f setfiles restorecon restorecon_xattr *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
        $(DESTDIR)$(SBINDIR)/restorecon $(DESTDIR)$(SBINDIR)/setfiles $(DESTDIR)$(SBINDIR)/restorecon_xattr
policycoreutils/setsebool/Makefile +0 -4
index 87494c55..360e0ea0 100644
@@ -35,7 +35,3 @@ relabel:
 
 clean:
        -rm -f setsebool *.o
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
policycoreutils/unsetfiles/Makefile +0 -3
index 9cb51494..8dbf85af 100644
@@ -20,8 +20,5 @@ install: all
 clean:
        -rm -f unsetfiles *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
        /sbin/restorecon $(DESTDIR)$(SBINDIR)/unsetfiles
python/Makefile +1 -1
index 00312dbd..c4c31a17 100644
@@ -1,6 +1,6 @@
 SUBDIRS = sepolicy audit2allow semanage sepolgen chcat po
 
-all install relabel clean indent test:
+all install relabel clean format test:
        @for subdir in $(SUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
python/audit2allow/Makefile +0 -3
index 28b8f7f2..90d10d1a 100644
@@ -51,7 +51,4 @@ install: all
 clean:
        rm -f *~ *.o sepolgen-ifgen-attr-helper test_dummy_policy
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: ;
python/chcat/Makefile +0 -2
index 7b3ee17f..334b749d 100644
@@ -21,8 +21,6 @@ install: all
 
 clean:
 
-indent:
-
 relabel:
 
 test:
python/semanage/Makefile +0 -2
index 628d135a..51208d9e 100644
@@ -35,6 +35,4 @@ test:
        @$(PYTHON) test-semanage.py -a
 clean:
 
-indent:
-
 relabel:
python/sepolgen/Makefile +0 -5
index 3aed3304..2ad7b13c 100644
@@ -11,10 +11,5 @@ clean:
        rm -f *~ *.pyc
        rm -f parser.out parsetab.py
 
-indent: ;
-
 test:
        $(MAKE) -C tests $@
-
-
-
python/sepolgen/src/Makefile +0 -6
index 2815a13d..a2456652 100644
@@ -12,10 +12,4 @@ clean:
        rm -f *~ *.pyc
        rm -f parser.out parsetab.py
 
-indent: ;
-
-
 test: ;
-
-
-
restorecond/Makefile +0 -3
index b8b86eb4..51eb7608 100644
@@ -59,7 +59,4 @@ relabel: install
 clean:
        -rm -f restorecond *.o *~
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 test:
sandbox/Makefile +0 -3
index 0799ab7c..eaaad75c 100644
@@ -50,7 +50,4 @@ clean:
        -rm -f seunshare *.o *~
        (cd po && $(MAKE) $@)
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
scripts/Lindent deleted +0 -18
deleted file mode 100755
index 9c4b3e2b..00000000
@@ -1,18 +0,0 @@
-#!/bin/sh
-PARAM="-npro -kr -i8 -ts8 -sob -l80 -ss -ncs -cp1"
-RES=`indent --version`
-V1=`echo $RES | cut -d' ' -f3 | cut -d'.' -f1`
-V2=`echo $RES | cut -d' ' -f3 | cut -d'.' -f2`
-V3=`echo $RES | cut -d' ' -f3 | cut -d'.' -f3`
-if [ $V1 -gt 2 ]; then
-  PARAM="$PARAM -il0"
-elif [ $V1 -eq 2 ]; then
-  if [ $V2 -gt 2 ]; then
-    PARAM="$PARAM -il0";
-  elif [ $V2 -eq 2 ]; then
-    if [ $V3 -ge 10 ]; then
-      PARAM="$PARAM -il0"
-    fi
-  fi
-fi
-indent $PARAM "$@"
semodule-utils/Makefile +1 -1
index e0a65792..81c97af0 100644
@@ -1,6 +1,6 @@
 SUBDIRS = semodule_package semodule_link semodule_expand
 
-all install relabel clean indent:
+all install relabel clean:
        @for subdir in $(SUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
semodule-utils/semodule_expand/Makefile +0 -4
index ad776b15..72f09a46 100644
@@ -27,7 +27,3 @@ relabel:
 
 clean:
        -rm -f semodule_expand *.o
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
semodule-utils/semodule_link/Makefile +0 -4
index 936d161c..83590c07 100644
@@ -27,7 +27,3 @@ relabel:
 
 clean:
        -rm -f semodule_link *.o
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
semodule-utils/semodule_package/Makefile +0 -5
index 6a289f73..2465f987 100644
@@ -29,7 +29,3 @@ relabel:
 
 clean:
        -rm -f semodule_package semodule_unpackage *.o
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
--

2.51.0

#17 lore raw

Re: When to apply `make format` to the entire tree

Quoted 32 lines · Petr Lautrbach
Quoted 30 lines · Stephen Smalley
Quoted 21 lines · Rahul Sandhu

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

As well as this, replace all the existing indent targets with a single toplevel format target. Managing all the source files to be formatted is not pretty to maintain, and doesn't really give us much.

Also define a toplevel check-format target to verify that all code is formatted properly. This only becomes useful in the future once we have reformatted all existing code, but is wired up for now.

For the future:
1. Reformat all existing code. I understand this is a big change, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.

Quoted 5 lines

2. Add a CI target to check that all code is formatted as per the new clang-format configuration. The check-format target can be used for this.

Quoted 7 lines
Quoted 3 lines

Will defer to distro package maintainers to decide when to apply make format to the entire tree. Common practice I think is to do it just before or after a release.

For me, it's better do it before the release so I would not need to backport the format patch together with some future change.

Alright. I'll send a patch (non-rfc then) to format the code, and then maybe just before the release someone can reply and I'll rebase it? I also am happy to let a maintainer apply the patch instead (I mean it is just one command) because of course the diff will be pretty huge, so it is a real concern to make sure that the code is "untampered" with.

If that's the preferred route (I still think it is best regardless of whoever sends the patch for people to check it locally by running make check-format and committing it locally, and then diff'ing the patches or something akin to that), I can simply send a patch hooking up CI and updating the contribution guide telling people to format the code and that can be applied after the format patch?

Quoted 776 lines

Petr

Quoted 772 lines
Quoted 772 lines
---
 .clang-format                            | 130 +++++++++++++++++++++++
 CONTRIBUTING.md                          |   2 +
 Makefile                                 |  11 +-
 checkpolicy/Makefile                     |   3 -
 gui/Makefile                             |   2 -
 libselinux/Makefile                      |   2 +-
 libselinux/include/Makefile              |   4 -
 libselinux/man/Makefile                  |   2 +-
 libselinux/src/Makefile                  |   3 -
 libselinux/utils/Makefile                |   4 -
 libsemanage/Makefile                     |   4 -
 libsemanage/include/Makefile             |   3 -
 libsemanage/src/Makefile                 |   3 -
 libsemanage/utils/Makefile               |   3 -
 libsepol/Makefile                        |   6 --
 libsepol/include/Makefile                |   3 -
 libsepol/src/Makefile                    |   4 -
 libsepol/utils/Makefile                  |   4 -
 mcstrans/src/Makefile                    |   1 -
 mcstrans/utils/Makefile                  |   1 -
 policycoreutils/Makefile                 |   2 +-
 policycoreutils/hll/Makefile             |   2 +-
 policycoreutils/hll/pp/Makefile          |   3 -
 policycoreutils/load_policy/Makefile     |   3 -
 policycoreutils/newrole/Makefile         |   3 -
 policycoreutils/run_init/Makefile        |   3 -
 policycoreutils/scripts/Makefile         |   2 -
 policycoreutils/secon/Makefile           |   3 -
 policycoreutils/semodule/Makefile        |   4 -
 policycoreutils/sestatus/Makefile        |   3 -
 policycoreutils/setfiles/Makefile        |   3 -
 policycoreutils/setsebool/Makefile       |   4 -
 policycoreutils/unsetfiles/Makefile      |   3 -
 python/Makefile                          |   2 +-
 python/audit2allow/Makefile              |   3 -
 python/chcat/Makefile                    |   2 -
 python/semanage/Makefile                 |   2 -
 python/sepolgen/Makefile                 |   5 -
 python/sepolgen/src/Makefile             |   6 --
 restorecond/Makefile                     |   3 -
 sandbox/Makefile                         |   3 -
 scripts/Lindent                          |  18 ----
 semodule-utils/Makefile                  |   2 +-
 semodule-utils/semodule_expand/Makefile  |   4 -
 semodule-utils/semodule_link/Makefile    |   4 -
 semodule-utils/semodule_package/Makefile |   4 -
 46 files changed, 148 insertions(+), 143 deletions(-)
 create mode 100644 .clang-format
 delete mode 100755 scripts/Lindent

v2: remove linux kernel ForEachMacros and replace them with ours
v3: replace the indent target with the new format target. also remove any
mention of .editorconfig from the commit message; those changes are better suited for another patch (they're small and self-contained enough that they're a smaller thing to merge).
v4: drop the subdir format targets entirely, replacing with a single toplevel
format and check-format target.
v5: add back relabel target accidently removed.
v6: remove all references to being an RFC in the commit message.

.clang-format new file +130 -0
new file mode 100644
index 00000000..35595d87
@@ -0,0 +1,130 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# clang-format configuration file. Intended for clang-format >= 11.
+#
+# For more information, see:
+#
+#   Documentation/dev-tools/clang-format.rst
+#   https://clang.llvm.org/docs/ClangFormat.html
+#   https://clang.llvm.org/docs/ClangFormatStyleOptions.html
+#
+---
+AccessModifierOffset: -4
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlines: Left
+AlignOperands: true
+AlignTrailingComments: false
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: false
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:
+  AfterClass: false
+  AfterControlStatement: false
+  AfterEnum: false
+  AfterFunction: true
+  AfterNamespace: true
+  AfterObjCDeclaration: false
+  AfterStruct: false
+  AfterUnion: false
+  AfterExternBlock: false
+  BeforeCatch: false
+  BeforeElse: false
+  IndentBraces: false
+  SplitEmptyFunction: true
+  SplitEmptyRecord: true
+  SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Custom
+BreakBeforeInheritanceComma: false
+BreakBeforeTernaryOperators: false
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeComma
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: false
+ColumnLimit: 80
+CommentPragmas: '^ IWYU pragma:'
+CompactNamespaces: false
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+ConstructorInitializerIndentWidth: 8
+ContinuationIndentWidth: 8
+Cpp11BracedListStyle: false
+DerivePointerAlignment: false
+DisableFormat: false
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: false
+
+# Taken from:
+#   git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' \
+#   | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$,  - '\1'," \
+#   | LC_ALL=C sort -u
+ForEachMacros:
+  - 'cil_list_for_each'
+  - 'cil_stack_for_each'
+  - 'cil_stack_for_each_starting_at'
+  - 'ebitmap_for_each_bit'
+  - 'ebitmap_for_each_positive_bit'
+
+IncludeBlocks: Preserve
+IncludeCategories:
+  - Regex: '.*'
+    Priority: 1
+IncludeIsMainRegex: '(Test)?$'
+IndentCaseLabels: false
+IndentGotoLabels: false
+IndentPPDirectives: None
+IndentWidth: 8
+IndentWrappedFunctionNames: false
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: false
+MacroBlockBegin: ''
+MacroBlockEnd: ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Auto
+ObjCBlockIndentWidth: 8
+ObjCSpaceAfterProperty: true
+ObjCSpaceBeforeProtocolList: true
+
+# Taken from git's rules
+PenaltyBreakAssignment: 10
+PenaltyBreakBeforeFirstCallParameter: 30
+PenaltyBreakComment: 10
+PenaltyBreakFirstLessLess: 0
+PenaltyBreakString: 10
+PenaltyExcessCharacter: 100
+PenaltyReturnTypeOnItsOwnLine: 60
+
+PointerAlignment: Right
+ReflowComments: false
+SortIncludes: false
+SortUsingDeclarations: false
+SpaceAfterCStyleCast: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatementsExceptForEachMacros
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles: false
+SpacesInContainerLiterals: false
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard: Cpp03
+TabWidth: 8
+UseTab: Always
+...
CONTRIBUTING.md +2 -0
index c501cf84..7ec8cb0f 100644
@@ -53,6 +53,8 @@ When preparing patches, please follow these guidelines:
 -   Separate large patches into logical patches
 -   Patch descriptions must end with your "Signed-off-by" line. This means your
     code meets the Developer's certificate of origin, see below.
+-   C code should be formatted using clang-format, using the .clang-format
+    configuration file at the root of this repository.
 
 When adding new, large features or tools it is best to discuss the
 design on the mailing list prior to submitting the patch.
Makefile +10 -1
index 2ffba8e9..972d5525 100644
@@ -36,7 +36,7 @@ ifneq ($(DESTDIR),)
        export LIBSEPOLA
 endif
 
-all install relabel clean test indent:
+all install relabel clean test:
        @for subdir in $(SUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
@@ -50,3 +50,12 @@ distclean:
        @for subdir in $(DISTCLEANSUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
+
+# We shouldn't have any unformatted files in the repo without an explicit exception.
+FORMAT_SOURCE_FILES := $(shell find $(SUBDIRS) -type f \( -name '*.c' -o -name '*.h' \))
+
+format:
+       clang-format -i $(FORMAT_SOURCE_FILES)
+
+check-format:
+       clang-format --dry-run -Werror $(FORMAT_SOURCE_FILES)
checkpolicy/Makefile +0 -3
index 6e8008e3..0055f495 100644
@@ -77,6 +77,3 @@ relabel: install
 clean:
        -rm -f $(TARGETS) $(CHECKPOLOBJS) $(CHECKMODOBJS) y.tab.c y.tab.h lex.yy.c tests/testpol.conf tests/testpol.bin
        $(MAKE) -C test clean
-
-indent:
-       ../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
gui/Makefile +0 -2
index b29610d4..b1a94224 100644
@@ -60,8 +60,6 @@ install: all
 clean:
        (cd po && $(MAKE) $@)
 
-indent:
-
 relabel:
 
 test:
libselinux/Makefile +1 -1
index a50b6491..aeede2b5 100644
@@ -50,7 +50,7 @@ COMPILER := clang
 endif
 export COMPILER
 
-all install relabel clean distclean indent:
+all install relabel clean distclean:
        @for subdir in $(SUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
libselinux/include/Makefile +0 -4
index 8ab9ce92..a1e322ac 100644
@@ -10,9 +10,5 @@ install: all
 
 relabel:
 
-indent:
-       ../../scripts/Lindent $(wildcard selinux/*.h)
-
 distclean clean:
        -rm -f selinux/*~
-
libselinux/man/Makefile +1 -1
index fdc0825f..42b1662a 100644
@@ -35,4 +35,4 @@ install: all
 
 relabel:
 
-indent distclean clean:
+format distclean clean:
libselinux/src/Makefile +0 -3
index 261c22d4..f7412cd1 100644
@@ -223,7 +223,4 @@ clean: clean-pywrap clean-rubywrap
 distclean: clean
        rm -f $(GENERATED) $(SWIGFILES)
 
-indent:
-       ../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
-
 .PHONY: all clean clean-pywrap clean-rubywrap pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
libselinux/utils/Makefile +0 -4
index 6b1dc7c9..20dc44eb 100644
@@ -72,8 +72,4 @@ clean:
 
 distclean: clean
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
-
libsemanage/Makefile +0 -4
index dbd240da..591ce5f2 100644
@@ -29,9 +29,5 @@ clean distclean:
        $(MAKE) -C src $@
        $(MAKE) -C tests $@
 
-indent:
-       $(MAKE) -C src $@
-       $(MAKE) -C include $@
-
 test: all
        $(MAKE) -C tests test
libsemanage/include/Makefile +0 -3
index 6e44a28a..b08e48a0 100644
@@ -7,6 +7,3 @@ all:
 install: all
        test -d $(DESTDIR)$(INCDIR) || install -m 755 -d $(DESTDIR)$(INCDIR)
        install -m 644 $(wildcard semanage/*.h) $(DESTDIR)$(INCDIR)
-
-indent:
-       ../../scripts/Lindent $(wildcard semanage/*.h)
libsemanage/src/Makefile +0 -3
index fa3449fb..8e025112 100644
@@ -167,7 +167,4 @@ clean:
 distclean: clean
        rm -f $(GENERATED) $(SWIGFILES)
 
-indent:
-       ../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
-
 .PHONY: all clean pywrap rubywrap swigify install install-pywrap install-rubywrap distclean
libsemanage/utils/Makefile +0 -3
index 5b8fbb6b..0f382934 100644
@@ -13,7 +13,4 @@ clean:
 
 distclean: clean
 
-indent:
-
 relabel:
-
libsepol/Makefile +0 -6
index 5b5d03e2..223edcec 100644
@@ -20,11 +20,5 @@ clean:
        $(MAKE) -C utils clean
        $(MAKE) -C tests clean
 
-indent:
-       $(MAKE) -C src $@
-       $(MAKE) -C include $@
-       $(MAKE) -C utils $@
-
 test:
        $(MAKE) -C tests test
-
libsepol/include/Makefile +0 -3
index 1ad4ecab..f49b6e19 100644
@@ -12,6 +12,3 @@ install: all
        install -m 644 $(wildcard sepol/*.h) $(DESTDIR)$(INCDIR)
        install -m 644 $(wildcard sepol/policydb/*.h) $(DESTDIR)$(INCDIR)/policydb
        install -m 644 $(wildcard $(CILDIR)/include/cil/*.h) $(DESTDIR)$(INCDIR)/cil
-
-indent:
-       ../../scripts/Lindent $(wildcard sepol/*.h)
libsepol/src/Makefile +0 -4
index 90aed394..bb2b7f5e 100644
@@ -105,7 +105,3 @@ relabel:
 
 clean:
        -rm -f $(LIBPC) $(LIBMAP) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(TARGET) $(CIL_GENERATED)
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
libsepol/utils/Makefile +0 -4
index 31932c11..a8bedf2e 100644
@@ -18,8 +18,4 @@ install: all
 clean:
        -rm -f $(TARGETS) *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
-
mcstrans/src/Makefile +0 -1
index 88487068..609b2d45 100644
@@ -37,4 +37,3 @@ install: all
 
 clean:
        -rm -f $(OBJS) $(LOBJS) $(TARGET) $(PROG) $(PROG_OBJS) *~ \#*
-
mcstrans/utils/Makefile +0 -1
index 57a73932..1d70b4ed 100644
@@ -36,4 +36,3 @@ clean:
        rm -f $(TARGETS) *.o *~ \#*
 
 relabel:
-
policycoreutils/Makefile +1 -1
index 7c9706e3..9186a9ef 100644
@@ -6,7 +6,7 @@ LIBSELINUX_LDLIBS := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH):../libselinux/s
 LIBSEMANAGE_LDLIBS := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH):../libsemanage/src" $(PKG_CONFIG) --libs libsemanage)
 export LIBSELINUX_LDLIBS LIBSEMANAGE_LDLIBS
 
-all install relabel clean indent:
+all install relabel clean:
        @for subdir in $(SUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
policycoreutils/hll/Makefile +1 -1
index fe720e57..390f3a34 100644
@@ -1,6 +1,6 @@
 SUBDIRS = pp
 
-all install relabel clean indent:
+all install relabel clean:
        @for subdir in $(SUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
policycoreutils/hll/pp/Makefile +0 -3
index 3c9b0efc..a0d475a3 100644
@@ -25,6 +25,3 @@ relabel:
 
 clean:
        -rm -f pp $(PP_OBJS)
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
policycoreutils/load_policy/Makefile +0 -3
index c61fee70..7754cadc 100644
@@ -28,8 +28,5 @@ install: all
 clean:
        -rm -f $(TARGETS) *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
        /sbin/restorecon $(DESTDIR)$(SBINDIR)/load_policy
policycoreutils/newrole/Makefile +0 -3
index df64483f..fcd2661f 100644
@@ -87,9 +87,6 @@ endif
 clean:
        rm -f newrole *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
        /sbin/restorecon $(DESTDIR)$(BINDIR)/newrole
 
policycoreutils/run_init/Makefile +0 -3
index c89fffb9..0e904dba 100644
@@ -53,8 +53,5 @@ endif
 clean:
        -rm -f $(TARGETS) *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
        /sbin/restorecon $(DESTDIR)$(SBINDIR)/run_init $(DESTDIR)$(SBINDIR)/open_init_pty
policycoreutils/scripts/Makefile +0 -2
index 6d8196c6..1058bbab 100644
@@ -21,6 +21,4 @@ install: all
 
 clean:
 
-indent:
-
 relabel:
policycoreutils/secon/Makefile +0 -3
index c9296f19..b2f276f9 100644
@@ -35,9 +35,6 @@ relabel:
 clean:
        rm -f *.o core* secon *~ *.bak
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 bare: clean
 
 .PHONY: clean bare
policycoreutils/semodule/Makefile +0 -4
index 018ee2ca..47bde5f8 100644
@@ -35,7 +35,3 @@ relabel:
 
 clean:
        -rm -f semodule *.o genhomedircon
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
policycoreutils/sestatus/Makefile +0 -3
index 1ce617c0..70f46956 100644
@@ -41,7 +41,4 @@ install: all
 clean:
        rm -f sestatus *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
policycoreutils/setfiles/Makefile +0 -3
index dab6313c..aa3e80fc 100644
@@ -43,8 +43,5 @@ install: all
 clean:
        rm -f setfiles restorecon restorecon_xattr *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
        $(DESTDIR)$(SBINDIR)/restorecon $(DESTDIR)$(SBINDIR)/setfiles $(DESTDIR)$(SBINDIR)/restorecon_xattr
policycoreutils/setsebool/Makefile +0 -4
index 87494c55..360e0ea0 100644
@@ -35,7 +35,3 @@ relabel:
 
 clean:
        -rm -f setsebool *.o
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
policycoreutils/unsetfiles/Makefile +0 -3
index 9cb51494..8dbf85af 100644
@@ -20,8 +20,5 @@ install: all
 clean:
        -rm -f unsetfiles *.o
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: install
        /sbin/restorecon $(DESTDIR)$(SBINDIR)/unsetfiles
python/Makefile +1 -1
index 00312dbd..c4c31a17 100644
@@ -1,6 +1,6 @@
 SUBDIRS = sepolicy audit2allow semanage sepolgen chcat po
 
-all install relabel clean indent test:
+all install relabel clean format test:
        @for subdir in $(SUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
python/audit2allow/Makefile +0 -3
index 28b8f7f2..90d10d1a 100644
@@ -51,7 +51,4 @@ install: all
 clean:
        rm -f *~ *.o sepolgen-ifgen-attr-helper test_dummy_policy
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel: ;
python/chcat/Makefile +0 -2
index 7b3ee17f..334b749d 100644
@@ -21,8 +21,6 @@ install: all
 
 clean:
 
-indent:
-
 relabel:
 
 test:
python/semanage/Makefile +0 -2
index 628d135a..51208d9e 100644
@@ -35,6 +35,4 @@ test:
        @$(PYTHON) test-semanage.py -a
 clean:
 
-indent:
-
 relabel:
python/sepolgen/Makefile +0 -5
index 3aed3304..2ad7b13c 100644
@@ -11,10 +11,5 @@ clean:
        rm -f *~ *.pyc
        rm -f parser.out parsetab.py
 
-indent: ;
-
 test:
        $(MAKE) -C tests $@
-
-
-
python/sepolgen/src/Makefile +0 -6
index 2815a13d..a2456652 100644
@@ -12,10 +12,4 @@ clean:
        rm -f *~ *.pyc
        rm -f parser.out parsetab.py
 
-indent: ;
-
-
 test: ;
-
-
-
restorecond/Makefile +0 -3
index b8b86eb4..51eb7608 100644
@@ -59,7 +59,4 @@ relabel: install
 clean:
        -rm -f restorecond *.o *~
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 test:
sandbox/Makefile +0 -3
index 0799ab7c..eaaad75c 100644
@@ -50,7 +50,4 @@ clean:
        -rm -f seunshare *.o *~
        (cd po && $(MAKE) $@)
 
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
 relabel:
scripts/Lindent deleted +0 -18
deleted file mode 100755
index 9c4b3e2b..00000000
@@ -1,18 +0,0 @@
-#!/bin/sh
-PARAM="-npro -kr -i8 -ts8 -sob -l80 -ss -ncs -cp1"
-RES=`indent --version`
-V1=`echo $RES | cut -d' ' -f3 | cut -d'.' -f1`
-V2=`echo $RES | cut -d' ' -f3 | cut -d'.' -f2`
-V3=`echo $RES | cut -d' ' -f3 | cut -d'.' -f3`
-if [ $V1 -gt 2 ]; then
-  PARAM="$PARAM -il0"
-elif [ $V1 -eq 2 ]; then
-  if [ $V2 -gt 2 ]; then
-    PARAM="$PARAM -il0";
-  elif [ $V2 -eq 2 ]; then
-    if [ $V3 -ge 10 ]; then
-      PARAM="$PARAM -il0"
-    fi
-  fi
-fi
-indent $PARAM "$@"
semodule-utils/Makefile +1 -1
index e0a65792..81c97af0 100644
@@ -1,6 +1,6 @@
 SUBDIRS = semodule_package semodule_link semodule_expand
 
-all install relabel clean indent:
+all install relabel clean:
        @for subdir in $(SUBDIRS); do \
                (cd $$subdir && $(MAKE) $@) || exit 1; \
        done
semodule-utils/semodule_expand/Makefile +0 -4
index ad776b15..72f09a46 100644
@@ -27,7 +27,3 @@ relabel:
 
 clean:
        -rm -f semodule_expand *.o
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
semodule-utils/semodule_link/Makefile +0 -4
index 936d161c..83590c07 100644
@@ -27,7 +27,3 @@ relabel:
 
 clean:
        -rm -f semodule_link *.o
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
semodule-utils/semodule_package/Makefile +0 -0
index 6a289f73..2465f987 100644
@@ -29,7 +29,3 @@ relabel:
 
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
semodule-utils/semodule_package/Makefile +0 -5
 clean:
        -rm -f semodule_package semodule_unpackage *.o
-
-indent:
-       ../../scripts/Lindent $(wildcard *.[ch])
-
--

2.51.0

Regards,
Rahul

maintainer
#18 lore raw

Re: When to apply `make format` to the entire tree

Quoted 39 lines · Rahul Sandhu
Quoted 37 lines · Petr Lautrbach
Quoted 33 lines · Stephen Smalley
Quoted 21 lines · Rahul Sandhu

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

As well as this, replace all the existing indent targets with a single toplevel format target. Managing all the source files to be formatted is not pretty to maintain, and doesn't really give us much.

Also define a toplevel check-format target to verify that all code is formatted properly. This only becomes useful in the future once we have reformatted all existing code, but is wired up for now.

For the future:
1. Reformat all existing code. I understand this is a big change, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.

Quoted 5 lines

2. Add a CI target to check that all code is formatted as per the new clang-format configuration. The check-format target can be used for this.

Will defer to distro package maintainers to decide when to apply make format to the entire tree. Common practice I think is to do it just before or after a release.

Quoted 8 lines
Quoted 2 lines

For me, it's better do it before the release so I would not need to backport the format patch together with some future change.

Alright. I'll send a patch (non-rfc then) to format the code, and then maybe just before the release someone can reply and I'll rebase it? I also am happy to let a maintainer apply the patch instead (I mean it is just one command) because of course the diff will be pretty huge, so it is a real concern to make sure that the code is "untampered" with.

I don't think we need to send a patch to the mailing list for that, just have whoever cuts the next release run make format and commit the result before tagging.

Quoted 6 lines

If that's the preferred route (I still think it is best regardless of whoever sends the patch for people to check it locally by running make check-format and committing it locally, and then diff'ing the patches or something akin to that), I can simply send a patch hooking up CI and updating the contribution guide telling people to format the code and that can be applied after the format patch?

Yes, that seems fine.

maintainer
#19 lore raw
In reply to Stephen Smalley
Quoted 23 lines · Stephen Smalley
Quoted 21 lines · Rahul Sandhu

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

As well as this, replace all the existing indent targets with a single toplevel format target. Managing all the source files to be formatted is not pretty to maintain, and doesn't really give us much.

Also define a toplevel check-format target to verify that all code is formatted properly. This only becomes useful in the future once we have reformatted all existing code, but is wired up for now.

For the future:
1. Reformat all existing code. I understand this is a big change, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.

Quoted 7 lines
Quoted 5 lines

2. Add a CI target to check that all code is formatted as per the new clang-format configuration. The check-format target can be used for this.

Thanks, merged this patch but as I noted, will defer to one of the other maintainers to apply make format prior to making the next release.

#20 lore raw
In reply to Stephen Smalley
Quoted 29 lines · Stephen Smalley
Quoted 19 lines · Rahul Sandhu

Currently only an RFC.

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

As well as this, replace all the existing indent targets with format targets. Commands used to find and replace those targets:

git grep -l -E '(\.\./)*scripts/Lindent' | xargs sed -i -E 's@(\.\./)*scripts/Lindent@clang-format -i@g' git grep -l 'indent' -- '*Makefile' | xargs sed -i 's/indent/format/g'

Also add some empty format targets to Makefiles that previously were missing an indent target so that make format does not error.

Quoted 8 lines

A few other things to consider to do in the future:
1. Reformat all existing code. I understand this is a big change, hence the RFC, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.
2. Possibly add a CI target to check that all code is formatted as per the new clang-format configuration? The options --dry-run as well as -Werror can be passed to clang-format for this.

Quoted 1 line

Yes please.

Should this be added in another patch? My concern is that CI will fail should this be merged without formatting all code.

Quoted 21 lines
Quoted 21 lines

Comments/feedback appreciated, thanks.

---

v2: remove linux kernel ForEachMacros and replace them with ours
v3: replace the indent target with the new format target. also remove any
mention of .editorconfig from the commit message; those changes are better suited for another patch (they're small and self-contained enough that they're a smaller thing to merge).

mcstrans/Makefile +0 -0
index b20279ab..28d8c7bc 100644
@@ -21,4 +21,6 @@ clean:
 
Quoted 6 lines

mcstrans/Makefile clean: · line 25

Quoted 4 lines
mcstrans/Makefile +2 -0
 relabel:
 
+format:
+

We should add targets for mcstrans/utils/*.c and mcstrans/src/*.[hc]

I wrote a simple check-format target in the Makefile:

# We shouldn't have any unformatted files in the repo without an explicit exception. # Given that this is used with the check-format target, which does not modify any

# source files, merely checking if they're formatted properly, it's fine to rely on
# find to get all C source and header files.
CHECK_FORMAT_SOURCE_FILES := $(shell find $(SUBDIRS) -type f \( -name '*.c' -o -name '*.h' \))

check-format:

	clang-format --dry-run -Werror $(CHECK_FORMAT_SOURCE_FILES)

However, after running make format and make check-format, I noticed a whole load of failures caused by a load of other missing targets. I'm thinking that it may just be better off using that same find command to get all c sources and headers, and have a single, global format target. Afterall, in pushed code after we've formatted it all, I don't see any reason that some code should be left unformatted (especially as we have things such as the clang-format off comments for specific sub-sections of code), and while working locally if someone wishes to format only a specific globbed set of files, something like this should work fine:
clang-format -i src/*.c

Thoughts? Suggestions?

Quoted 13 lines

secilc/Makefile clean: · line 91

Quoted 11 lines
secilc/Makefile +2 -0
index ef7bc8cd..2518933f 100644
@@ -87,4 +87,6 @@ clean:
 
 relabel:
 
+format:
+

Should add a target for secilc/*.c

#21 lore raw
In reply to Stephen Smalley
Quoted 29 lines · Stephen Smalley
Quoted 10 lines · Rahul Sandhu

Currently only an RFC.

Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this.

Quoted 3 lines

.clang-format is also read by various editors and tooling for writing C code. It may also be worth adding an editorconfig file in the future as well?

Can't hurt.

Quoted 2 lines

It may well also be worth adding a format target to the Makefiles, is that something that would be useful?

IMHO, yes.

Quoted 6 lines

A few other things to consider to do in the future:
1. Reformat all existing code. I understand this is a big change, hence the RFC, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes.

Quoted 4 lines

Not opposed but will defer to the distro package maintainers on when they want to apply such a change since it makes back-porting future patches more painful for them.

No strong opinions from Gentoo. We aren't carrying any patches for C code for SELinux userspace.

All in all I think this is a good change.

Quoted 176 lines
Quoted 3 lines

2. Possibly add a CI target to check that all code is formatted as per the new clang-format configuration? The options --dry-run as well as -Werror can be passed to clang-format for this.

Sounds reasonable too.

Quoted 2 lines

Comments/feedback appreciated, thanks.

No strong opinions; would be fine with any standardized coding style. For comparison, selinux-testsuite has tools/check-syntax which can be used to check or fix coding style problems for C and perl code, but I'd be fine with using clang-format here as in the kernel.

Quoted 162 lines
---
 .clang-format   | 130 ++++++++++++++++++++++++++++++++++++++++++++++++
 CONTRIBUTING.md |   2 +
 2 files changed, 132 insertions(+)
 create mode 100644 .clang-format

v2: remove linux kernel ForEachMacros and replace them with ours

.clang-format new file +130 -0
new file mode 100644
index 00000000..35595d87
@@ -0,0 +1,130 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# clang-format configuration file. Intended for clang-format >= 11.
+#
+# For more information, see:
+#
+#   Documentation/dev-tools/clang-format.rst
+#   https://clang.llvm.org/docs/ClangFormat.html
+#   https://clang.llvm.org/docs/ClangFormatStyleOptions.html
+#
+---
+AccessModifierOffset: -4
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlines: Left
+AlignOperands: true
+AlignTrailingComments: false
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: false
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:
+  AfterClass: false
+  AfterControlStatement: false
+  AfterEnum: false
+  AfterFunction: true
+  AfterNamespace: true
+  AfterObjCDeclaration: false
+  AfterStruct: false
+  AfterUnion: false
+  AfterExternBlock: false
+  BeforeCatch: false
+  BeforeElse: false
+  IndentBraces: false
+  SplitEmptyFunction: true
+  SplitEmptyRecord: true
+  SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Custom
+BreakBeforeInheritanceComma: false
+BreakBeforeTernaryOperators: false
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeComma
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: false
+ColumnLimit: 80
+CommentPragmas: '^ IWYU pragma:'
+CompactNamespaces: false
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+ConstructorInitializerIndentWidth: 8
+ContinuationIndentWidth: 8
+Cpp11BracedListStyle: false
+DerivePointerAlignment: false
+DisableFormat: false
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: false
+
+# Taken from:
+#   git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' \
+#   | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$,  - '\1'," \
+#   | LC_ALL=C sort -u
+ForEachMacros:
+  - 'cil_list_for_each'
+  - 'cil_stack_for_each'
+  - 'cil_stack_for_each_starting_at'
+  - 'ebitmap_for_each_bit'
+  - 'ebitmap_for_each_positive_bit'
+
+IncludeBlocks: Preserve
+IncludeCategories:
+  - Regex: '.*'
+    Priority: 1
+IncludeIsMainRegex: '(Test)?$'
+IndentCaseLabels: false
+IndentGotoLabels: false
+IndentPPDirectives: None
+IndentWidth: 8
+IndentWrappedFunctionNames: false
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: false
+MacroBlockBegin: ''
+MacroBlockEnd: ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Auto
+ObjCBlockIndentWidth: 8
+ObjCSpaceAfterProperty: true
+ObjCSpaceBeforeProtocolList: true
+
+# Taken from git's rules
+PenaltyBreakAssignment: 10
+PenaltyBreakBeforeFirstCallParameter: 30
+PenaltyBreakComment: 10
+PenaltyBreakFirstLessLess: 0
+PenaltyBreakString: 10
+PenaltyExcessCharacter: 100
+PenaltyReturnTypeOnItsOwnLine: 60
+
+PointerAlignment: Right
+ReflowComments: false
+SortIncludes: false
+SortUsingDeclarations: false
+SpaceAfterCStyleCast: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatementsExceptForEachMacros
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles: false
+SpacesInContainerLiterals: false
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard: Cpp03
+TabWidth: 8
+UseTab: Always
+...
CONTRIBUTING.md +2 -1
index c501cf84..7ec8cb0f 100644
@@ -53,6 +53,8 @@ When preparing patches, please follow these guidelines:
 -   Separate large patches into logical patches
 -   Patch descriptions must end with your "Signed-off-by" line. This means your
     code meets the Developer's certificate of origin, see below.
+-   C code should be formatted using clang-format, using the .clang-format
+    configuration file at the root of this repository.
 
 When adding new, large features or tools it is best to discuss the
 design on the mailing list prior to submitting the patch.
--

2.50.1

Signature · 2 lines
Kenton Groombridge
Gentoo Linux Developer, SELinux Project