forked from AVSystem/Anjay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_visibility.sh
executable file
·91 lines (80 loc) · 2.55 KB
/
test_visibility.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
#
# Copyright 2017 AVSystem <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
preprocess_file() {
# This uses ex/vim regexes and search-and-replace syntax.
# https://www.youtube.com/watch?v=0p_1QSUsbsM
#
# Some handy references:
# - http://jeetworks.org/vim-regular-expression-special-characters-to-escape-or-not-to-escape/
# - http://vim.wikia.com/wiki/Search_and_replace
# - http://vimdoc.sourceforge.net/cgi-bin/help?tag=/magic
# - http://vimdoc.sourceforge.net/cgi-bin/help?tag=pattern
#
# Also, things to note:
# - '@' is used here as the argument delimiter, instead of the
# usually used '/' (as in s/one/two/), due to slashes in patterns
# - in ex's language, double-quote " denotes line comment
ex -m "$1" <<-'EOF'
%s@\\\s*\n@ @g " joins lines on trailing backslash
%s@^\s*@@g " removes leading spaces
%s@/\*\_.\{-}\*/@@g " removes block comments (incl. multiline)
%s@//.*$@@g " removes line comments
%s@^#.*$@@g " removes preprocessor directives
%s@\s*$@@g " removes trailing spaces
%s@^extern .*);$@@g " removes extern function declarations
g/^$/d " removes empty lines
%p " prints the processed document
EOF
}
read_lines() {
FIRST=''
read FIRST || return 0
LAST="$FIRST"
while read REPLY; do
LAST="$REPLY"
done
}
_check_empty() {
test -z "$FIRST" -a -z "$LAST"
}
_check_private_header_top() {
test "$FIRST" = 'VISIBILITY_PRIVATE_HEADER_BEGIN'
}
_check_private_header_bottom() {
test "$LAST" = 'VISIBILITY_PRIVATE_HEADER_END'
}
check_source() {
test "$FIRST" = 'VISIBILITY_SOURCE_BEGIN'
}
check_private_header() {
_check_private_header_top && _check_private_header_bottom
}
check_public_header() {
! { [[ $FIRST =~ '^VISIBILITY' ]] || [[ $LAST =~ '^VISIBILITY' ]]; }
}
classify_file() {
if [[ $1 =~ .h$ ]]; then
if [[ $1 =~ /include_public/ ]]; then
echo 'public_header'
else
echo 'private_header'
fi
else
echo 'source'
fi
}
preprocess_file "$1" | { read_lines; _check_empty || check_"$(classify_file "$1")"; }