blob: 93f9b106698a37381844ebd67a31d05a50c23405 [file] [log] [blame]
Paolo Bonzini6afeb392018-12-14 10:33:22 +01001#! /bin/sh
2#
3# Fix multiline comments to match CODING_STYLE
4#
5# Copyright (C) 2018 Red Hat, Inc.
6#
7# Author: Paolo Bonzini
8#
9# Usage: scripts/fix-multiline-comments.sh [-i] FILE...
10#
11# -i edits the file in place (requires gawk 4.1.0).
12#
13# Set the AWK environment variable to choose the awk interpreter to use
14# (default 'awk')
15
16if test "$1" = -i; then
17 # gawk extension
18 inplace="-i inplace"
19 shift
20fi
21${AWK-awk} $inplace 'BEGIN { indent = -1 }
22{
23 line = $0
24 # apply a star to the indent on lines after the first
25 if (indent != -1) {
26 if (line == "") {
27 line = sp " *"
28 } else if (substr(line, 1, indent + 2) == sp " ") {
29 line = sp " *" substr(line, indent + 3)
30 }
31 }
32
33 is_lead = (line ~ /^[ \t]*\/\*/)
34 is_trail = (line ~ /\*\//)
35 if (is_lead && !is_trail) {
36 # grab the indent at the start of a comment, but not for
37 # single-line comments
38 match(line, /^[ \t]*\/\*/)
39 indent = RLENGTH - 2
40 sp = substr(line, 1, indent)
41 }
42
43 # the regular expression filters out lone /*, /**, or */
44 if (indent != -1 && !(line ~ /^[ \t]*(\/\*+|\*\/)[ \t]*$/)) {
45 if (is_lead) {
46 # split the leading /* or /** on a separate line
47 match(line, /^[ \t]*\/\*+/)
48 lead = substr(line, 1, RLENGTH)
49 match(line, /^[ \t]*\/\*+[ \t]*/)
50 line = lead "\n" sp " *" substr(line, RLENGTH)
51 }
52 if (is_trail) {
53 # split the trailing */ on a separate line
54 match(line, /[ \t]*\*\//)
55 line = substr(line, 1, RSTART - 1) "\n" sp " */"
56 }
57 }
58 if (is_trail) {
59 indent = -1
60 }
61 print line
62}' "$@"