From 9a0b0d84695c55ab8bf28ec8a7a5ebdb0afa95fa Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 6 Jun 2020 03:54:12 -0500 Subject: [PATCH] Add a code formatter --- buildroot/bin/format_code | 30 +++ buildroot/bin/uncrust | 16 ++ buildroot/share/extras/uncrustify.cfg | 295 ++++++++++++++++++++++++++ 3 files changed, 341 insertions(+) create mode 100755 buildroot/bin/format_code create mode 100755 buildroot/bin/uncrust create mode 100644 buildroot/share/extras/uncrustify.cfg diff --git a/buildroot/bin/format_code b/buildroot/bin/format_code new file mode 100755 index 0000000000..8ae04b1b6e --- /dev/null +++ b/buildroot/bin/format_code @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# +# format_code [dir/file...] +# + +HERE=`dirname $0` + +while [[ $# -gt 0 ]]; do + + val="$1" + + if [ -d "$val" ]; then + + find $val -name *.cpp -exec "$HERE/uncrust" '{}' \; + + elif [ -d "./Marlin/src/$val" ]; then + + find "./Marlin/src/$val" -name *.cpp -exec "$HERE/uncrust" '{}' \; + + elif [ -f "./Marlin/src/$val" ]; then + + uncrust "./Marlin/src/$val" + + elif [ -f "$val" ]; then + + uncrust "$val" + + fi + +done diff --git a/buildroot/bin/uncrust b/buildroot/bin/uncrust new file mode 100755 index 0000000000..63eb250e3b --- /dev/null +++ b/buildroot/bin/uncrust @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +# +# Run uncrustify for a file in-place +# + +TMPDIR=`mktemp -d` + +# Reformat a single file to tmp/ +uncrustify -c ./buildroot/share/extras/uncrustify.cfg -f "$1" >$TMPDIR/uncrustify.out + +# Replace the original file +cp "$TMPDIR/uncrustify.out" "$1" + +# Clean up, deliberately +rm "$TMPDIR/uncrustify.out" +rmdir "$TMPDIR" diff --git a/buildroot/share/extras/uncrustify.cfg b/buildroot/share/extras/uncrustify.cfg new file mode 100644 index 0000000000..4d90da1a3a --- /dev/null +++ b/buildroot/share/extras/uncrustify.cfg @@ -0,0 +1,295 @@ +# +# http://uncrustify.sourceforge.net/ +# +# Source beautifier configuration which helps to create +# more consistent look to your source with the expected +# coding style in VICE +# +# Usage: "uncrustify -c uncrustify.cfg source.c" +# +# There's no guarantee the look is perfect after this +# but at least the most common stuff is corrected. +# +# Make sure to use the latest version. +# + +# no cr/lf +newlines = lf + +# no tabs +indent_with_tabs = 0 + +# no tabs +output_tab_size = 2 + +# small indents +indent_columns = 2 + +# Spaces to indent '{' from 'case'. +# By default, the brace will appear under the 'c' in case. +# Usually set to 0 or indent_columns. +indent_case_brace = 4 + +# Spaces to indent 'case' from 'switch' +# Usually 0 or indent_columns. +indent_switch_case = 4 + +# Add or remove space around arithmetic operator '+', '-', '/', '*', etc +sp_arith = force + +# Add or remove space around assignment operator '=', '+=', etc +sp_assign = force + +# Add or remove space around boolean operators '&&' and '||' +sp_bool = force + +# Add or remove space around compare operator '<', '>', '==', etc +sp_compare = force + +# Add or remove space around the ':' in 'b ? t : f' +sp_cond_colon = force + +# Add or remove space around the '?' in 'b ? t : f' +sp_cond_question = force + +# In the abbreviated ternary form '(a ?: b)', add or remove space between '?' +# and ':'. +# +# Overrides all other sp_cond_* options. +sp_cond_ternary_short = remove + +# Add or remove space between nested parens +sp_paren_paren = remove + +# Add or remove space inside '(' and ')' +sp_inside_sparen = remove + +# Add or remove space between 'else' and '{' if on the same line +sp_else_brace = force + +# Add or remove space between '}' and 'else' if on the same line +sp_brace_else = force + +# Add or remove space inside a non-empty '[' and ']' +sp_inside_square = remove + +# Add or remove space before '(' of 'if', 'for', 'switch', and 'while' +sp_before_sparen = force + +# Add or remove space after ',' +sp_after_comma = force + +# Add or remove space before ',' +sp_before_comma = remove + +# Add or remove space between ')' and '{' +sp_paren_brace = force + +# Add or remove space after ')' of 'if', 'for', 'switch', and 'while' +sp_after_sparen = force + +# Add or remove space after ';', except when followed by a comment. Default=Add +sp_after_semi = force + +# Add or remove newline between '}' and 'else' +nl_brace_else = force + +# Add or remove newline between 'enum' and '{' +nl_enum_brace = remove + +# Add or remove newline between 'struct and '{' +nl_struct_brace = remove + +# Add or remove newline between 'union' and '{' +nl_union_brace = remove + +# Whether to put a newline after brace open. +# This also adds a newline before the matching brace close. +nl_after_brace_open = true + +# Add or remove newline between 'if' and '{' +nl_if_brace = remove + +# Add or remove newline between 'else' and '{' +nl_else_brace = remove + +# Add or remove newline between 'switch' and '{' +nl_switch_brace = remove + +# Add or remove newline at the end of the file +nl_end_of_file = add + +# Add or remove newline between function signature and '{' +nl_fdef_brace = remove + +# Whether to remove blank lines after '{' +eat_blanks_after_open_brace = true + +# Whether to remove blank lines before '}' +eat_blanks_before_close_brace = false + +# Whether to enforce that all blocks of an 'if'/'else if'/'else' chain either +# have, or do not have, braces. If true, braces will be added if any block +# needs braces, and will only be removed if they can be removed from all +# blocks. +# +# Overrides mod_full_brace_if. +mod_full_brace_if_chain = true + +# Add or remove braces on single-line 'if' statement. Will not remove the braces if they contain an 'else'. +mod_full_brace_if = false + +# Add or remove braces on single-line 'do' statement +mod_full_brace_do = false + +# Add or remove braces on single-line 'for' statement +mod_full_brace_for = false + +# Add or remove braces on single-line 'while' statement +mod_full_brace_while = false + +# Whether to remove superfluous semicolons +mod_remove_extra_semicolon = true + +# If an #ifdef or #else body exceeds the specified number of newlines and +# doesn't have a comment after the #else, a comment will be added. +mod_add_long_ifdef_else_comment = 40 + + +# Whether to put a newline after a brace close. +# Does not apply if followed by a necessary ';'. +nl_after_brace_close = true + +# If false, disable all multi-line comment changes, including cmt_width. keyword substitution, and leading chars. +# Default is true. +cmt_indent_multi = false + +# Add or remove indentation of preprocessor directives inside #if blocks +# at brace level 0 (file-level). +pp_indent = add + +# Whether to indent #if/#else/#endif at the brace level. If false, these are +# indented from column 1. +pp_indent_at_level = true + +# If pp_indent_at_level=true, sets the indent for #if, #else and #endif when +# not at file-level. Negative values decrease indent down to the first column. +# +# =0: Indent preprocessors using output_tab_size +# >0: Column at which all preprocessors will be indented +pp_indent_if = 0 + +# Whether to indent case statements between #if, #else, and #endif. +# Only applies to the indent of the preprocesser that the case statements +# directly inside of. +# +# Default: true +pp_indent_case = false + +# Whether to indent the code between #if, #else and #endif. +pp_if_indent_code = true + +# Specifies the number of columns to indent preprocessors per level +# at brace level 0 (file-level). If pp_indent_at_level=false, also specifies +# the number of columns to indent preprocessors per level +# at brace level > 0 (function-level). +# +# Default: 1 +pp_indent_count = 2 + + +# Whether to indent '#define' at the brace level. If false, these are +# indented from column 1. +pp_define_at_level = true + +# Whether to ignore the '#define' body while formatting. +pp_ignore_define_body = true + +# +# Newline adding and removing options +# + +# Don't split one-line braced assignments, as in 'foo_t f = { 1, 2 };'. +nl_assign_leave_one_liners = true + +# Don't split one-line braced statements inside a 'class xx { }' body. +nl_class_leave_one_liners = true + +# Don't split one-line enums, as in 'enum foo { BAR = 15 };' +nl_enum_leave_one_liners = true + +# Don't split one-line get or set functions. +nl_getset_leave_one_liners = true + +# (C#) Don't split one-line property get or set functions. +nl_cs_property_leave_one_liners = true + +# Don't split one-line function definitions, as in 'int foo() { return 0; }'. +# might modify nl_func_type_name +nl_func_leave_one_liners = true + +# Don't split one-line C++11 lambdas, as in '[]() { return 0; }'. +nl_cpp_lambda_leave_one_liners = false + +# Don't split one-line if/else statements, as in 'if(...) b++;'. +nl_if_leave_one_liners = true + +# Don't split one-line while statements, as in 'while(...) b++;'. +nl_while_leave_one_liners = true + +# Don't split one-line for statements, as in 'for(...) b++;'. +nl_for_leave_one_liners = true + +# Whether to indent the body of a C++11 lambda. +indent_cpp_lambda_body = true + +# The value might be used twice: +# - at the assignment +# - at the opening brace +# +# To prevent the double use of the indentation value, use this option with the +# value 'true'. +# +# true: indentation will be used only once +# false: indentation will be used every time (default) +indent_cpp_lambda_only_once = true + +# How to reflow comments. +# +# 0: No reflowing (apart from the line wrapping due to cmt_width) (default) +# 1: No touching at all +# 2: Full reflow +cmt_reflow_mode = 1 + +# Whether to group cpp-comments that look like they are in a block. Only +# meaningful if cmt_cpp_to_c=true. +cmt_cpp_group = true + +# Whether to put a star on subsequent comment lines. +cmt_star_cont = true + +# The number of spaces to insert at the start of subsequent comment lines. +cmt_sp_before_star_cont = 1 + +# The number of spaces to insert after the star on subsequent comment lines. +cmt_sp_after_star_cont = 1 + +# Whether to convert all tabs to spaces in comments. If false, tabs in +# comments are left alone, unless used for indenting. +cmt_convert_tab_to_spaces = true + + +# Add a newline before ')' if an if/for/while/switch condition spans multiple +# lines. Overrides nl_before_if_closing_paren if both are specified. +nl_multi_line_sparen_close = ignore + +# Add or remove newline before 'if'/'else if' closing parenthesis. +nl_before_if_closing_paren = ignore + +# Add or remove space around assignment operator '=' in a prototype. +# +# If set to ignore, use sp_assign. +sp_assign_default = remove + +# Whether to right-align numbers. +align_number_right = true