Browse code

dev-tools: Add reformat-all.sh for code style unification

This script will run all files related to the currently checked out
git branch through uncrustify using a standardized style configuration.

Due to a bug in uncrustify 0.64, it is needed to add a special treatment
to one of the files at the moment. So this both pre- and post-patched
before/after uncrustify is run. This is to simply to assure that all
file processing will happen consistently each time.

Also added doc/doxygen/doc_key_generation.h to an ignore list, as
it carries some specific Doxygen formatting we should be careful with.
This file is anyhow not so critical and can be managed manually.

The src/compat/compat-lz4.[ch] files are also not touched, as they
are based on upstream formatting. This makes it easier to update
to a newer LZ4 version later on and even see what the differences
are.

v2 - Include updated config from CodeStyle wiki page
Remove line lenght restriction for The Great Reformatting
Update the script with improvements by krzee

v3 - Update with a fixed config from the CodeStyle wiki page
Corrected a typo in the commit message (0.63->0.64)
Minor changes to the reformat script (no pushd/popd,
some new lines moved around, bash->sh)

Signed-off-by: David Sommerseth <davids@openvpn.net>
Acked-by: Steffan Karger <steffan@karger.me>
Message-Id: <1481749500-8795-1-git-send-email-davids@openvpn.net>
URL: http://www.mail-archive.com/search?l=mid&q=1481749500-8795-1-git-send-email-davids@openvpn.net

David Sommerseth authored on 2016/12/15 06:05:00
Showing 5 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,136 @@
0
+#!/bin/sh
1
+# reformat-all.sh - Reformat all git files in the checked out
2
+#                   git branch using uncrustify.
3
+#
4
+# Copyright (C) 2016 - David Sommerseth <davids@openvpn.net>
5
+#
6
+# This program is free software; you can redistribute it and/or
7
+# modify it under the terms of the GNU General Public License
8
+# as published by the Free Software Foundation; either version 2
9
+# of the License.
10
+#
11
+# This program is distributed in the hope that it will be useful,
12
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+# GNU General Public License for more details.
15
+#
16
+# You should have received a copy of the GNU General Public License
17
+# along with this program; if not, write to the Free Software
18
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19
+#
20
+
21
+tstamp="$(date +%Y%m%d-%H%M%S)"
22
+files="$(pwd)/reformat-all_files-$tstamp.lst"
23
+log="$(pwd)/reformat-all_log-$tstamp.txt"
24
+
25
+srcroot="$(git rev-parse --show-toplevel)"
26
+cfg="$srcroot/dev-tools/uncrustify.conf"
27
+specialfiles="$srcroot/dev-tools/special-files.lst"
28
+
29
+export gitfiles=0
30
+export procfiles=0
31
+
32
+# Go to the root of the source tree
33
+cd "$srcroot"
34
+
35
+{
36
+    echo -n "** Starting $0: "
37
+    date
38
+
39
+    # Find all C source/header files
40
+    git ls-files | grep -E ".*\.[ch](\.in$|$)" > "${files}.git"
41
+
42
+    # Manage files which needs special treatment
43
+    awk -F\# '{gsub("\n| ", "", $1); print $1}' "$specialfiles" > "${files}.sp"
44
+    while read srcfile
45
+    do
46
+        res=$(grep "$srcfile" "${files}.sp" 2>/dev/null)
47
+        if [ $? -ne 0 ]; then
48
+            # If grep didn't find the file among special files,
49
+            # process it normally
50
+            echo "$srcfile" >> "$files"
51
+        else
52
+            mode=$(echo "$res" | cut -d:  -f1)
53
+            case "$mode" in
54
+                E)
55
+                    echo "** INFO **  Excluding '$srcfile'"
56
+                    ;;
57
+                P)
58
+                    echo "** INFO **  Pre-patching '$srcfile'"
59
+                    patchfile="${srcroot}"/dev-tools/reformat-patches/before_$(echo "$srcfile" | tr "/" "_").patch
60
+                    if [ -r "$patchfile" ]; then
61
+                        git apply "$patchfile"
62
+                        if [ $? -ne 0 ]; then
63
+                            echo "** ERROR **  Failed to apply pre-patch file: $patchfile"
64
+                            exit 2
65
+                        fi
66
+                    else
67
+                        echo "** WARN ** Pre-patch file for $srcfile is missing: $patchfile"
68
+                    fi
69
+                    echo "$srcfile" >> "${files}.postpatch"
70
+                    echo "$srcfile" >> "$files"
71
+                    ;;
72
+                *)
73
+                    echo "** WARN ** Unknown mode '$mode' for file '$srcfile'"
74
+                    ;;
75
+            esac
76
+        fi
77
+    done < "${files}.git"
78
+    rm -f "${files}.git" "${files}.sp"
79
+
80
+    # Kick off uncrustify
81
+    echo
82
+    echo "** INFO ** Running: uncrustify -c $cfg --no-backup -l C -p debug.uncr -F $files"
83
+    uncrustify -c "$cfg" --no-backup -l C -p debug.uncr -F "$files" 2>&1
84
+    res=$?
85
+    echo "** INFO ** Uncrustify completed (exit code $res)"
86
+} | tee "${log}-1"  # Log needs to be closed here, to be processed in next block
87
+
88
+{
89
+    # Check the results
90
+    gitfiles=$(wc -l "$files" | cut -d\  -f1)
91
+    procfiles=$(grep "Parsing: " "${log}-1" | wc -l)
92
+    echo
93
+    echo "C source/header files checked into git: $gitfiles"
94
+    echo "Files processed by uncrustify:          $procfiles"
95
+    echo
96
+
97
+    # Post-Patch files modified after we uncrustify have adjusted them
98
+    if [ -r "${files}.postpatch" ]; then
99
+        while read srcfile;
100
+        do
101
+            patchfile="${srcroot}"/dev-tools/reformat-patches/after_$(echo "$srcfile" | tr "/" "_").patch
102
+            if [ -r "$patchfile" ]; then
103
+                echo "** INFO **  Post-patching '$srcfile'"
104
+                git apply "$patchfile"
105
+                if [ $? -ne 0 ]; then
106
+                    echo "** WARN ** Failed to apply $patchfile"
107
+                fi
108
+            else
109
+                echo "** WARN ** Post-patch file for $srcfile is missing: $patchfile"
110
+            fi
111
+        done < "${files}.postpatch"
112
+        rm -f "${files}.postpatch"
113
+    fi
114
+} | tee "${log}-2" # Log needs to be closed here, to be processed in next block
115
+
116
+cat "${log}-1" "${log}-2" > "$log"
117
+
118
+{
119
+    ec=1
120
+    echo
121
+    if [ "$gitfiles" -eq "$procfiles" ]; then
122
+        echo "Reformatting completed successfully"
123
+        ec=0
124
+    else
125
+        last=$(tail -n1 "${log}-1")
126
+        echo "** ERROR ** Reformating failed to process all files."
127
+        echo "            uncrustify exit code: $res"
128
+        echo "            Last log line: $last"
129
+        echo
130
+    fi
131
+    rm -f "${log}-1" "${log}-2"
132
+} | tee -a "$log"
133
+rm -f "${files}"
134
+
135
+exit $ec
0 136
new file mode 100644
... ...
@@ -0,0 +1,13 @@
0
+diff --git a/include/openvpn-plugin.h.in b/include/openvpn-plugin.h.in
1
+index 05bffab..05b4b6a 100644
2
+--- a/include/openvpn-plugin.h.in
3
+@@ -169,7 +169,7 @@ typedef void *openvpn_plugin_handle_t;
4
+ /*
5
+  * We are compiling OpenVPN.
6
+  */
7
+-/* #define OPENVPN_PLUGIN_DEF        typedef */
8
++#define OPENVPN_PLUGIN_DEF        typedef
9
+ #define OPENVPN_PLUGIN_FUNC(name) (*name)
10
+
11
+ #else  /* ifdef OPENVPN_PLUGIN_H */
0 12
new file mode 100644
... ...
@@ -0,0 +1,13 @@
0
+diff --git a/include/openvpn-plugin.h.in b/include/openvpn-plugin.h.in
1
+index 34ad18b..f4c5472 100644
2
+--- a/include/openvpn-plugin.h.in
3
+@@ -169,7 +169,7 @@ typedef void *openvpn_plugin_handle_t;
4
+ /*
5
+  * We are compiling OpenVPN.
6
+  */
7
+-#define OPENVPN_PLUGIN_DEF        typedef
8
++// #define OPENVPN_PLUGIN_DEF        typedef
9
+ #define OPENVPN_PLUGIN_FUNC(name) (*name)
10
+
11
+ #else
0 12
new file mode 100644
... ...
@@ -0,0 +1,4 @@
0
+E:doc/doxygen/doc_key_generation.h     # @verbatim section gets mistreated, exclude it
1
+E:src/compat/compat-lz4.c              # Preserve LZ4 upstream formatting
2
+E:src/compat/compat-lz4.h              # Preserve LZ4 upstream formatting
3
+P:include/openvpn-plugin.h.in          # uncrustify segfaults, patch it before+after
0 4
new file mode 100644
... ...
@@ -0,0 +1,65 @@
0
+# Use Allman-style
1
+indent_columns=4
2
+indent_braces=false
3
+indent_else_if=false
4
+indent_switch_case=4
5
+indent_label=1
6
+nl_if_brace=add
7
+nl_brace_else=add
8
+nl_elseif_brace=add
9
+nl_else_brace=add
10
+nl_else_if=remove
11
+sp_func_proto_paren=Remove
12
+sp_func_def_paren=Remove
13
+sp_func_call_paren=Remove
14
+sp_sizeof_paren=Remove
15
+
16
+# No tabs, spaces only
17
+indent_with_tabs=0
18
+align_with_tabs=false
19
+cmt_convert_tab_to_spaces=true
20
+
21
+# Do not put spaces between the # and preprocessor statements
22
+pp_space=remove
23
+
24
+# Various whitespace fiddling
25
+sp_assign=add
26
+sp_before_sparen=add
27
+sp_inside_sparen=remove
28
+sp_cond_colon=add
29
+sp_cond_question=add
30
+sp_bool=add
31
+sp_else_brace=add
32
+sp_brace_else=add
33
+pos_arith=Lead
34
+pos_bool=Lead
35
+nl_func_type_name=add
36
+nl_before_case=true
37
+nl_assign_leave_one_liners=true
38
+nl_enum_leave_one_liners=true
39
+nl_brace_fparen=add
40
+nl_max=4
41
+nl_after_func_proto=2
42
+
43
+# Always use scoping braces for conditionals
44
+mod_full_brace_if=add
45
+mod_full_brace_if_chain=false
46
+
47
+# Annotate #else and #endif statements
48
+mod_add_long_ifdef_endif_comment=20
49
+mod_add_long_ifdef_else_comment=5
50
+
51
+# Misc cleanup
52
+mod_remove_extra_semicolon=true
53
+
54
+# Use C-style comments (/* .. */)
55
+cmt_c_nl_end=true
56
+cmt_star_cont=true
57
+cmt_cpp_to_c=true
58
+
59
+# Use "char **a"-style pointer stars/dereferences
60
+sp_before_ptr_star=Add
61
+sp_between_ptr_star=Remove
62
+sp_after_ptr_star=Remove
63
+sp_before_byref=Add
64
+sp_after_byref=Remove