blob: 06e45837d8fc1f763dd4229b20cc53f91d0adb7c [file] [log] [blame]
Steven Moreland0d14f5b2020-12-23 22:48:23 +00001#!/usr/bin/env bash
2
3# Copyright (C) 2020 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17set -e
18
19# future considerations:
20# - could we make this work with git-clang-format instead?
21# - should we have our own formatter?
22
23function _aidl-format() (
Steven Moreland0d14f5b2020-12-23 22:48:23 +000024 # Do a "reversible" conversion of the input file so that it is more friendly
25 # to clang-format. For example 'oneway interface Foo{}' is not recognized as
26 # an interface. Convert it to 'interface __aidl_oneway__ Foo{}'.
27 function prepare() {
28 # oneway interface Foo {} is not correctly recognized as an interface by
29 # clang-format. Change it to interface __aidl_oneway__ Foo {}.
30 sed -i -E 's/oneway[[:space:]]+interface/interface\ __aidl_oneway__/g' "$1"
31
32 # When a declaration becomes too long, clang-format splits the declaration
33 # into multiple lines. In doing so, annotations that are at the front of
34 # the declaration are always split. i.e.
35 #
36 # @utf8InCpp @nullable void foo(int looooooo....ong, int looo....ong);
37 #
38 # becomes
39 #
40 # @utf8InCpp
41 # @nullable
42 # void foo(int loooooo...ong,
43 # int looo.....ong);
44 #
45 # This isn't desirable for utf8InCpp and nullable annotations which are
46 # semantically tagged to the type, not the member (field/method). We want
47 # to have the annotations in the same line as the type that they actually
48 # annotate. i.e.
49 #
50 # @utf8InCpp @nullable void foo(int looo....ong,
51 # int looo.....ong);
52 #
53 # To do so, the annotations are temporarily replaced with tokens that are
54 # not annotations.
55 sed -i -E 's/@utf8InCpp/__aidl_utf8inCpp__/g' "$1"
56 sed -i -E 's/@nullable/__aidl_nullable__/g' "$1"
57 }
58
59 function apply-clang-format() {
60 local input="$1"
61 local temp="$(mktemp)"
62 cat "$input" | clang-format \
63 --style='{BasedOnStyle: Google, BreakAfterJavaFieldAnnotations: false, ColumnLimit: 100}' \
64 --assume-filename=${input%.*}.java \
65 > "$temp"
66 mv "$temp" "$input"
67 }
68
69 # clang-format is good, but doesn't perfectly fit to our needs. Fix the
70 # minor mismatches manually.
71 function fixup() {
72 # Revert the changes done during the prepare call. Notice that the
73 # original tokens (@utf8InCpp, etc.) are shorter than the temporary tokens
74 # (__aidl_utf8InCpp, etc.). This can make the output text length shorter
75 # than the specified column limit. We can try to reduce the undesirable
76 # effect by keeping the tokens to have similar lengths, but that seems to
77 # be an overkill at this moment. We can revisit this when this becomes a
78 # real problem.
79 sed -i -E 's/interface\ __aidl_oneway__/oneway\ interface/g' "$1"
80 sed -i -E 's/__aidl_utf8inCpp__/@utf8InCpp/g' "$1"
81 sed -i -E 's/__aidl_nullable__/@nullable/g' "$1"
82
83 # clang-format adds space around "=" in annotation parameters. e.g.
84 # @Anno(a = 100). The following awk script removes the spaces back.
85 # @Anno(a = 1, b = 2) @Anno(c = 3, d = 4) int foo = 3; becomes
86 # @Anno(a=1, b=2) @Anno(c=3, d=4) int foo = 3;
87 # [^@,=] ensures that the match doesn't cross the characters, otherwise
88 # "a = 1, b = 2" would match only once and will become "a = 1, b=2".
89 gawk -i inplace \
90 '/@[^@]+\(.*=.*\)/ { # matches a line having @anno(param = val) \
91 print(gensub(/([^@,=]+) = ([^@,=]+)/, "\\1=\\2", "g", $0)); \
92 done=1;\
93 } \
94 {if (!done) {print($0);} done=0;}' "$1"
95 }
96
97 function format-one() {
Jiyong Park0b8c0b12021-01-20 17:26:08 +090098 local mode="$1"
99 local input="$2"
100 local output="$(mktemp)"
Steven Moreland0d14f5b2020-12-23 22:48:23 +0000101
Jiyong Park0b8c0b12021-01-20 17:26:08 +0900102 cp "$input" "$output"
103 prepare "$output"
104 apply-clang-format "$output"
105 fixup "$output"
Steven Moreland0d14f5b2020-12-23 22:48:23 +0000106
Jiyong Park0b8c0b12021-01-20 17:26:08 +0900107 if [ $mode = "diff" ]; then
108 diff "$input" "$output"
109 rm "$output"
110 elif [ $mode = "write" ]; then
Steven Moreland0d14f5b2020-12-23 22:48:23 +0000111 if diff -q "$output" "$input" >/dev/null; then
112 rm "$output"
113 else
114 mv "$output" "$input"
115 fi
Jiyong Park0b8c0b12021-01-20 17:26:08 +0900116 elif [ $mode = "print" ]; then
117 cat "$output"
118 rm "$output"
119 fi
Steven Moreland0d14f5b2020-12-23 22:48:23 +0000120 }
121
Jiyong Park0b8c0b12021-01-20 17:26:08 +0900122 function show-help-and-exit() {
123 echo "Usage: $0 [options] [path...]"
124 echo " -d: display diff instead of the formatted result"
125 echo " -w: rewrite the result back to the source file, instead of stdout"
126 echo " -h: show this help message"
127 echo " [path...]: source files. if none, input is read from stdin"
128 exit 1
129 }
130
131 local mode=print
132 if [ $# -gt 0 ]; then
133 case "$1" in
134 -d) mode=diff; shift;;
135 -w) mode=write; shift;;
136 -h) show-help-and-exit;;
137 -*) echo "$1" is wrong option; show-help-and-exit;;
138 esac
139 fi
140
141 if [ $# -lt 1 ]; then
142 if [ $mode = "write" ]; then
143 echo "-w not supported when input is stdin"
144 exit 1
145 fi
146 local input="$(mktemp)"
147 cat /dev/stdin > "$input"
148 format-one $mode "$input"
149 rm "$input"
150 else
151 for file in "$@"
152 do
153 if [ ! -f "$file" ]; then
154 echo "$file": no such file
155 exit 1
156 fi
157 format-one $mode "$file"
158 done
159 fi
Steven Moreland0d14f5b2020-12-23 22:48:23 +0000160)
161
162_aidl-format "$@"