Print this page
7085 add support for "if" and "else" statements in dtrace
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/dtrace/test/cmd/scripts/dstyle.pl
+++ new/usr/src/cmd/dtrace/test/cmd/scripts/dstyle.pl
1 1 #!/usr/perl5/bin/perl
2 2 #
3 3 # CDDL HEADER START
4 4 #
5 5 # The contents of this file are subject to the terms of the
6 6 # Common Development and Distribution License (the "License").
7 7 # You may not use this file except in compliance with the License.
8 8 #
9 9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 10 # or http://www.opensolaris.org/os/licensing.
11 11 # See the License for the specific language governing permissions
12 12 # and limitations under the License.
13 13 #
14 14 # When distributing Covered Code, include this CDDL HEADER in each
15 15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 16 # If applicable, add the following below this CDDL HEADER, with the
17 17 # fields enclosed by brackets "[]" replaced with your own identifying
↓ open down ↓ |
17 lines elided |
↑ open up ↑ |
18 18 # information: Portions Copyright [yyyy] [name of copyright owner]
19 19 #
20 20 # CDDL HEADER END
21 21 #
22 22
23 23 #
24 24 # Copyright 2008 Sun Microsystems, Inc. All rights reserved.
25 25 # Use is subject to license terms.
26 26 #
27 27
28 +#
29 +# Copyright (c) 2014 by Delphix. All rights reserved.
30 +#
31 +
28 32 require 5.8.4;
29 33
30 34 $PNAME = $0;
31 35 $PNAME =~ s:.*/::;
32 36 $USAGE = "Usage: $PNAME [file ...]\n";
33 37 $errs = 0;
34 38
35 39 sub err
36 40 {
37 41 my($msg) = @_;
38 42
39 43 print "$file: $lineno: $msg\n";
40 44 $errs++;
41 45 }
42 46
43 47 sub dstyle
44 48 {
45 49 open(FILE, "$file");
46 50 $lineno = 0;
47 51 $inclause = 0;
48 52 $skipnext = 0;
49 53
50 54 while (<FILE>) {
51 55 $lineno++;
52 56
53 57 chop;
54 58
55 59 if ($skipnext) {
56 60 $skipnext = 0;
57 61 next;
58 62 }
59 63
60 64 #
61 65 # Amazingly, some ident strings are longer than 80 characters!
62 66 #
63 67 if (/^#pragma ident/) {
64 68 next;
65 69 }
66 70
67 71 #
68 72 # The algorithm to calculate line length from cstyle.
69 73 #
70 74 $line = $_;
71 75 if ($line =~ tr/\t/\t/ * 7 + length($line) > 80) {
72 76 # yes, there is a chance.
73 77 # replace tabs with spaces and check again.
74 78 $eline = $line;
75 79 1 while $eline =~
76 80 s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
77 81
78 82 if (length($eline) > 80) {
79 83 err "line > 80 characters";
80 84 }
81 85 }
82 86
83 87 if (/\/\*DSTYLED\*\//) {
84 88 $skipnext = 1;
85 89 next;
86 90 }
87 91
88 92 if (/^#pragma/) {
89 93 next;
90 94 }
91 95
92 96 if (/^#include/) {
93 97 next;
94 98 }
95 99
96 100 #
97 101 # Before we do any more analysis, we want to prune out any
98 102 # quoted strings. This is a bit tricky because we need
99 103 # to be careful of backslashed quotes within quoted strings.
100 104 # I'm sure there is a very crafty way to do this with a
101 105 # single regular expression, but that will have to wait for
102 106 # somone with better regex juju that I; we do this by first
103 107 # eliminating the backslashed quotes, and then eliminating
104 108 # whatever quoted strings are left. Note that we eliminate
105 109 # the string by replacing it with "quotedstr"; this is to
106 110 # allow lines to end with a quoted string. (If we simply
107 111 # eliminated the quoted string, dstyle might complain about
108 112 # the line ending in a space or tab.)
109 113 #
110 114 s/\\\"//g;
111 115 s/\"[^\"]*\"/quotedstr/g;
112 116
113 117 if (/[ \t]$/) {
114 118 err "space or tab at end of line";
115 119 }
116 120
117 121 if (/^[\t]+[ ]+[\t]+/) {
118 122 err "spaces between tabs";
119 123 }
120 124
121 125 if (/^[\t]* \*/) {
122 126 next;
123 127 }
↓ open down ↓ |
86 lines elided |
↑ open up ↑ |
124 128
125 129 if (/^ /) {
126 130 err "indented by spaces not tabs";
127 131 }
128 132
129 133 if (/^{}$/) {
130 134 next;
131 135 }
132 136
133 137 if (!/^enum/ && !/^\t*struct/ && !/^\t*union/ && !/^typedef/ &&
134 - !/^translator/ && !/^provider/) {
138 + !/^translator/ && !/^provider/ && !/\tif / &&
139 + !/ else /) {
135 140 if (/[\w\s]+{/) {
136 141 err "left brace not on its own line";
137 142 }
138 143
139 144 if (/{[\w\s]+/) {
140 145 err "left brace not on its own line";
141 146 }
142 147 }
143 148
144 - if (!/;$/) {
149 + if (!/;$/ && !/\t*}$/ && !/ else /) {
145 150 if (/[\w\s]+}/) {
146 151 err "right brace not on its own line";
147 152 }
148 153
149 154 if (/}[\w\s]+/) {
150 155 err "right brace not on its own line";
151 156 }
152 157 }
153 158
154 159 if (/^}/) {
155 160 $inclause = 0;
156 161 }
157 162
158 163 if (!$inclause && /^[\w ]+\//) {
159 164 err "predicate not at beginning of line";
160 165 }
161 166
162 167 if (!$inclause && /^\/[ \t]+\w/) {
163 168 err "space between '/' and expression in predicate";
164 169 }
165 170
166 171 if (!$inclause && /\w[ \t]+\/$/) {
167 172 err "space between expression and '/' in predicate";
168 173 }
169 174
170 175 if (!$inclause && /\s,/) {
171 176 err "space before comma in probe description";
172 177 }
173 178
174 179 if (!$inclause && /\w,[\w\s]/ && !/;$/) {
175 180 if (!/extern/ && !/\(/ && !/inline/) {
176 181 err "multiple probe descriptions on same line";
177 182 }
178 183 }
179 184
180 185 if ($inclause && /sizeof\(/) {
181 186 err "missing space after sizeof";
182 187 }
183 188
184 189 if ($inclause && /^[\w ]/) {
185 190 err "line doesn't begin with a tab";
186 191 }
187 192
188 193 if ($inclause && /,[\w]/) {
189 194 err "comma without trailing space";
190 195 }
191 196
192 197 if (/\w&&/ || /&&\w/ || /\w\|\|/ || /\|\|\w/) {
193 198 err "logical operator not set off with spaces";
194 199 }
195 200
196 201 #
197 202 # We want to catch "i<0" variants, but we don't want to
198 203 # erroneously flag translators.
199 204 #
200 205 if (!/\w<\w+>\(/) {
201 206 if (/\w>/ || / >\w/ || /\w</ || /<\w/) {
202 207 err "comparison operator not set " .
203 208 "off with spaces";
204 209 }
205 210 }
206 211
207 212 if (/\w==/ || /==\w/ || /\w<=/ || />=\w/ || /\w!=/ || /!=\w/) {
208 213 err "comparison operator not set off with spaces";
209 214 }
210 215
211 216 if (/\w=/ || /=\w/) {
212 217 err "assignment operator not set off with spaces";
213 218 }
214 219
215 220 if (/^{/) {
216 221 $inclause = 1;
217 222 }
218 223 }
219 224 }
220 225
221 226 foreach $arg (@ARGV) {
222 227 if (-f $arg) {
223 228 push(@files, $arg);
224 229 } else {
225 230 die "$PNAME: $arg is not a valid file\n";
226 231 }
227 232 }
228 233
229 234 die $USAGE if (scalar(@files) == 0);
230 235
231 236 foreach $file (@files) {
232 237 dstyle($file);
233 238 }
234 239
235 240 exit($errs != 0);
↓ open down ↓ |
81 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX