Skip to content

Commit 1c53d2b

Browse files
liuguo09gregory-nutt
authored andcommitted
Nxstyle checkpatch support (#47)
* tools/nxstyle.c: Donot check unknown file extension files nxstyle only support c soure file and header file check, donot check other unknown file extension files. * tools/checkpatch.sh: Add checkpatch.sh script based on nxstyle tool Usage: checkpatch.sh patch-list // default as patch list checkpatch.sh -p patch-list checkpatch.sh -c commit-list checkpatch.sh -f file-list checkpatch.sh - // read from stdin, which used by git pre-commit hook And git pre-commit hook could use checkpatch.sh as below: git diff --cached | ./tools/checkpatch.sh -
1 parent 7179f29 commit 1c53d2b

File tree

2 files changed

+137
-3
lines changed

2 files changed

+137
-3
lines changed

tools/checkpatch.sh

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright (C) 2019 Xiaomi
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+
#
17+
18+
TOOLDIR=$(dirname $0)
19+
20+
usage() {
21+
echo "USAGE: ${0} [options] [list|-]"
22+
echo ""
23+
echo "Options:"
24+
echo "-h"
25+
echo "-p <patch list> (default)"
26+
echo "-c <commit list>"
27+
echo "-f <file list>"
28+
echo "- read standard input mainly used by git pre-commit hook as below:"
29+
echo " git diff --cached | ./tools/checkpatch.sh -"
30+
}
31+
32+
check_file() {
33+
$TOOLDIR/nxstyle -m 86 $@ 2>&1
34+
}
35+
36+
check_ranges() {
37+
local fail=0
38+
39+
while read; do
40+
if [[ $REPLY =~ \+\+\+\ (b/)?([^[:blank:]]+).* ]]; then
41+
if [ "$ranges" != "" ]; then
42+
check_file $ranges $path 2>&1
43+
if [ $? != 0 ]; then
44+
fail=1
45+
fi
46+
fi
47+
path=${BASH_REMATCH[2]}
48+
ranges=""
49+
elif [[ $REPLY =~ @@\ -[0-9]+(,[0-9]+)?\ \+([0-9]+,[0-9]+)?\ @@.* ]]; then
50+
ranges+="-r ${BASH_REMATCH[2]} "
51+
fi
52+
done
53+
if [ "$ranges" != "" ]; then
54+
check_file $ranges $path 2>&1
55+
if [ $? != 0 ]; then
56+
fail=1
57+
fi
58+
fi
59+
if [ $fail = 1 ]; then
60+
exit 1
61+
fi
62+
}
63+
64+
check_patch() {
65+
git apply --check $1
66+
if [ $? != 0 ]; then
67+
exit 1
68+
fi
69+
git apply $1
70+
cat $1 | check_ranges
71+
git apply -R $1
72+
}
73+
74+
check_commit() {
75+
git show $1 | check_ranges
76+
}
77+
78+
make -C $TOOLDIR -f Makefile.host nxstyle 1>/dev/null
79+
80+
if [ -z "$1" ]; then
81+
usage
82+
exit 0
83+
fi
84+
85+
while [ ! -z "$1" ]; do
86+
case "$1" in
87+
-h )
88+
usage
89+
exit 0
90+
;;
91+
-p )
92+
shift
93+
patches=$@
94+
break
95+
;;
96+
-c )
97+
shift
98+
commits=$@
99+
break
100+
;;
101+
-f )
102+
shift
103+
files=$@
104+
break
105+
;;
106+
- )
107+
check_ranges
108+
exit 0
109+
;;
110+
* )
111+
patches=$@
112+
break
113+
;;
114+
esac
115+
done
116+
117+
for patch in $patches; do
118+
check_patch $patch
119+
done
120+
121+
for commit in $commits; do
122+
check_commit $commit
123+
done
124+
125+
for file in $files; do
126+
check_file $file
127+
done

tools/nxstyle.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#define WARN(m, l, o) message(WARN, (m), (l), (o))
6161
#define ERROR(m, l, o) message(ERROR, (m), (l), (o))
6262
#define INFO(m, l, o) message(INFO, (m), (l), (o))
63+
#define INFOFL(m,s) message(INFO, (m), -1, -1)
6364

6465
/****************************************************************************
6566
* Private types
@@ -157,7 +158,7 @@ static int message(enum class_e class, const char *text, int lineno, int ndx)
157158
{
158159
if (lineno == -1 && ndx == -1)
159160
{
160-
fprintf(out, "%s:%s: %s\n", class_text[class], text, g_file_name);
161+
fprintf(out, "%s: %s: %s\n", g_file_name, class_text[class], text);
161162
}
162163
else
163164
{
@@ -301,11 +302,11 @@ int main(int argc, char **argv, char **envp)
301302

302303
/* Are we parsing a header file? */
303304

304-
ext = strrchr(g_file_name, '.');
305+
ext = strrchr(g_file_name, '.');
305306

306307
if (ext == 0)
307308
{
308-
WARN("No file extension", 0 , 0);
309+
INFOFL("No file extension", g_file_name);
309310
}
310311
else if (strcmp(ext, ".h") == 0)
311312
{
@@ -316,6 +317,12 @@ int main(int argc, char **argv, char **envp)
316317
g_file_type = C_SOURCE;
317318
}
318319

320+
if (g_file_type == UNKNOWN)
321+
{
322+
INFOFL("Unknown file extension", g_file_name);
323+
return 0;
324+
}
325+
319326
btabs = false; /* True: TAB characters found on the line */
320327
bcrs = false; /* True: Carriable return found on the line */
321328
bfunctions = false; /* True: In private or public functions */

0 commit comments

Comments
 (0)