hs-test: fixed timed out tests passing in the CI
[vpp.git] / extras / emacs / fix-coding-style.el
1 #!/usr/bin/emacs --script
2
3 ;;; Copyright (c) 2016 Cisco and/or its affiliates.
4 ;;; Licensed under the Apache License, Version 2.0 (the "License");
5 ;;; you may not use this file except in compliance with the License.
6 ;;; You may obtain a copy of the License at:
7 ;;;
8 ;;;     http://www.apache.org/licenses/LICENSE-2.0
9 ;;;
10 ;;; Unless required by applicable law or agreed to in writing, software
11 ;;; distributed under the License is distributed on an "AS IS" BASIS,
12 ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ;;; See the License for the specific language governing permissions and
14 ;;; limitations under the License.
15
16 ;; Insert style boilerplate if it's not already there
17 ;;
18 ;; Breaking the string in half keeps emacs
19 ;; from trying to interpret the local variable
20 ;; settings e.g. when it reads the lisp source code
21
22 (defun insert-style-boilerplate () (interactive)
23        (save-excursion 
24          (goto-char (point-min))
25          (if (eq nil (search-forward "coding-style-patch-verification" 
26                                      (point-max) t))
27              (let ((junk 0)) (goto-char (point-max))
28               (insert "
29 /*
30  * fd.io coding-style-patch-verification: ON
31  *
32  * Local Var" "iables:
33  * eval: (c-set-style \"gnu\")
34  * End:
35  */")))))
36
37 ;; (cons xxx <list>) means insert xxx at the head of <list>
38 ;; Build a sorted list of *INDENT-OFF* lines, by searching
39 ;; backwards. The initial (setq indent-offset-list nil)
40 ;; results in (cdr <last-cell>) nil, which makes it a proper list
41
42 (defun find-indent-offs () (interactive)
43        (save-excursion
44          (if (boundp 'indent-offset-list)
45              (makunbound 'indent-offset-list))
46          (setq indent-offset-list nil)
47          (goto-char (point-max))
48          (while (search-backward "*INDENT-OFF*" (point-min) t)
49            (move-beginning-of-line nil)
50            (setq indent-offset-list (cons (point) indent-offset-list))
51            (previous-line))))
52
53 ;; Insert indent-off ... indent-on brackets around
54 ;; a certain xxx_foreach macro, etc. which "indent"
55 ;; completely screws up. Doesn't handle nesting, of which there
56 ;; are few examples (fortunately).
57
58 (defun fix-initializer (what) (interactive)
59        (find-indent-offs)
60        (save-excursion 
61          (goto-char (point-min))
62          (while (search-forward-regexp what (point-max) t)
63            (move-beginning-of-line nil)
64            (previous-line)
65            (let ((index 0)(pointval 0))
66              (while (and (< pointval (point))(elt indent-offset-list index))
67                (setq pointval (elt indent-offset-list index))
68                (setq index (1+ index)))
69              (if (not (eq pointval (point)))
70                  (let ((junk 0))
71                    (next-line)
72                    (open-line 1)
73                    (c-indent-line-or-region)
74                    (insert "/* *INDENT-OFF* */")
75                    (search-forward "{")
76                    (backward-char)
77                    (forward-sexp)
78                    (move-end-of-line nil)
79                    (newline 1)
80                    (c-indent-line-or-region)
81                    (insert "/* *INDENT-ON* */")
82                    (find-indent-offs))
83                (search-forward "*INDENT-ON*"))))))
84
85 (defun fix-pool-foreach () (interactive)
86        (fix-initializer "pool_foreach *("))
87
88 (defun fix-pool-foreach-index () (interactive)
89        (fix-initializer "pool_foreach_index *("))
90
91 (defun fix-hash-foreach () (interactive)
92        (fix-initializer "hash_foreach *("))
93
94 (defun fix-hash-foreach-pair () (interactive)
95        (fix-initializer "hash_foreach_pair *("))
96
97 (defun fix-hash-foreach-mem () (interactive)
98        (fix-initializer "hash_foreach_mem *("))
99
100 (defun fix-clib-fifo-foreach () (interactive)
101        (fix-initializer "clib_fifo_foreach *("))
102
103 (defun fix-clib-bitmap-foreach () (interactive)
104        (fix-initializer "clib_bitmap_foreach *("))
105
106 (defun fix-foreach-ip-interface-address () (interactive)
107        (fix-initializer "foreach_ip_interface_address *("))
108
109 (defun fix-vlib-register-thread () (interactive)
110        (fix-initializer "VLIB_REGISTER_THREAD *("))
111
112 (defun fix-vlib-cli-command () (interactive)
113        (fix-initializer "VLIB_CLI_COMMAND *("))
114
115 (defun fix-vlib-register-node () (interactive)
116        (fix-initializer "VLIB_REGISTER_NODE *("))
117
118 (defun fix-reply-macro2 () (interactive)
119        (fix-initializer "REPLY_MACRO2 *("))
120
121 (defun fix-vnet-device-class () (interactive)
122        (fix-initializer "VNET_DEVICE_CLASS *("))
123
124 (defun fix-vnet-hw-interface-class () (interactive)
125        (fix-initializer "VNET_HW_INTERFACE_CLASS *("))
126
127 (defun fix-clib-packed () (interactive)
128        (fix-initializer "CLIB_PACKED *("))
129 (defun fix-vl-api-packed () (interactive)
130        (fix-initializer "VL_API_PACKED *("))
131
132 ;; Driver routine which runs the set of functions
133 ;; defined above, as well as the bottom boilerplate function
134
135 (defun fd-io-styleify () (interactive)
136        (fix-pool-foreach)
137        (fix-pool-foreach-index)
138        (fix-hash-foreach)
139        (fix-hash-foreach-pair)
140        (fix-hash-foreach-mem)
141        (fix-foreach-ip-interface-address)
142        (fix-clib-fifo-foreach)
143        (fix-clib-bitmap-foreach)
144        (fix-vlib-register-thread)
145        (fix-vlib-cli-command)
146        (fix-vlib-register-node)
147        (fix-reply-macro2)
148        (fix-vnet-device-class)
149        (fix-vnet-hw-interface-class)
150        (fix-clib-packed)
151        (fix-vl-api-packed)
152        (insert-style-boilerplate)
153        (if (boundp 'indent-offset-list)
154            (makunbound 'indent-offset-list)))
155
156 ;; When run as a script, this sexp
157 ;; walks the list of files supplied on the command line.
158 ;; 
159 ;; (elt argv index) returns nil if you M-x eval-buffer
160 ;; or M-x load-file the file, so we won't accidentally
161 ;; evaluate (save-buffers-kill-emacs)...
162
163 (let ((file-index 0))
164   (if (elt argv file-index)
165       (while (elt argv file-index)
166         (find-file (elt argv file-index))
167         (fd-io-styleify)
168         (message "Done %s..." (elt argv file-index))
169         (setq file-index (1+ file-index))))
170   (if (> file-index 0)
171       (let ((junk 0))
172         (message "Save and quit...")
173         (save-buffers-kill-emacs t))))
174   
175