xref: /titanic_41/usr/src/tools/scripts/jstyle.pl (revision 2a9459bdd821c1cf59590a7a9069ac9c591e8a6b)
1#!/usr/bin/perl -w
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# ident	"%Z%%M%	%I%	%E% SMI"
25#
26# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27# Use is subject to license terms.
28#
29# jstyle - check for some common stylistic errors.
30#
31
32require 5.006;
33use Getopt::Std;
34use strict;
35
36my $usage =
37"usage: jstyle [-c] [-h] [-p] [-t] [-v] [-C] file ...
38	-c	check continuation line indenting
39	-h	perform heuristic checks that are sometimes wrong
40	-p	perform some of the more picky checks
41	-t	insist on indenting by tabs
42	-v	verbose
43	-C	don't check anything in header block comments
44";
45
46my %opts;
47
48# Keep -s, as it's been around for a while.  It just doesn't do anything
49# anymore.
50if (!getopts("chpstvC", \%opts)) {
51	print $usage;
52	exit 1;
53}
54
55my $check_continuation = $opts{'c'};
56my $heuristic = $opts{'h'};
57my $picky = $opts{'p'};
58my $tabs = $opts{'t'};
59my $verbose = $opts{'v'};
60my $ignore_hdr_comment = $opts{'C'};
61
62my ($filename, $line, $prev);
63
64my $fmt;
65
66if ($verbose) {
67	$fmt = "%s: %d: %s\n%s\n";
68} else {
69	$fmt = "%s: %d: %s\n";
70}
71
72# Note, following must be in single quotes so that \s and \w work right.
73my $typename = '(int|char|boolean|byte|short|long|float|double)';
74my $keywords = '(for|if|while|switch|return|catch|synchronized|throw|assert)';
75# See perlre(1) for the meaning of (??{ ... })
76my $annotations = ""; $annotations = qr/@\w+\((?:(?>[^()]+)|(??{ $annotations }))*\)/;
77my $generics = ""; $generics = qr/<(([\s\w,.?[\]]| & )+|(??{ $generics }))*>/;
78my $relationalops = qr/>=|<=|<|>|!=|==/;
79my $shiftops = qr/<<<|>>>|<<|>>/;
80my $shiftassignmentops = qr/[<>]{2,3}=/;
81my $assignmentops = qr/[-+\/*|&^%]?=/;
82# These need to be in decreasing order of length
83my $allops = qr/$shiftassignmentops|$shiftops|$relationalops|$assignmentops/;
84
85if ($#ARGV >= 0) {
86	foreach my $arg (@ARGV) {
87		if (!open(STDIN, $arg)) {
88			printf "%s: can not open\n", $arg;
89		} else {
90			&jstyle($arg);
91			close STDIN;
92		}
93	}
94} else {
95	&jstyle("<stdin>");
96}
97
98sub err($) {
99	printf $fmt, $filename, $., $_[0], $line;
100}
101
102sub jstyle($) {
103
104my $in_comment = 0;
105my $in_header_comment = 0;
106my $in_continuation = 0;
107my $in_class = 0;
108my $in_declaration = 0;
109my $nextok = 0;
110my $nocheck = 0;
111my $expect_continuation = 0;
112my $continuation_indent;
113my $okmsg;
114my $comment_prefix;
115my $comment_done;
116my $cpp_comment;
117
118$filename = $_[0];
119
120line: while (<STDIN>) {
121	s/\r?\n$//;	# strip return and newline
122
123	# save the original line, then remove all text from within
124	# double or single quotes, we do not want to check such text.
125
126	$line = $_;
127	s/"[^"]*"/\"\"/g;
128	s/'.'/''/g;
129
130	# an /* END JSTYLED */ comment ends a no-check block.
131	if ($nocheck) {
132		if (/\/\* *END *JSTYLED *\*\//) {
133			$nocheck = 0;
134		} else {
135			next line;
136		}
137	}
138
139	# a /*JSTYLED*/ comment indicates that the next line is ok.
140	if ($nextok) {
141		if ($okmsg) {
142			err($okmsg);
143		}
144		$nextok = 0;
145		$okmsg = 0;
146		if (/\/\* *JSTYLED.*\*\//) {
147			/^.*\/\* *JSTYLED *(.*) *\*\/.*$/;
148			$okmsg = $1;
149			$nextok = 1;
150		}
151		$prev = $line;
152		next line;
153	}
154
155	# remember whether we expect to be inside a continuation line.
156	$in_continuation = $expect_continuation;
157
158	# check for proper continuation line.  blank lines
159	# in the middle of the
160	# continuation do not count.
161	# XXX - only check within functions.
162	if ($check_continuation && $expect_continuation && $in_class &&
163	    !/^\s*$/) {
164		# continuation line must start with whitespace of
165		# previous line, plus either 4 spaces or a tab, but
166		# do not check lines that start with a string constant
167		# since they are often shifted to the left to make them
168		# fit on the line.
169		if (!/^$continuation_indent    \S/ &&
170		    !/^$continuation_indent\t\S/ && !/^\s*"/) {
171			err("continuation line improperly indented");
172		}
173		$expect_continuation = 0;
174	}
175
176	# a /* BEGIN JSTYLED */ comment starts a no-check block.
177	if (/\/\* *BEGIN *JSTYLED *\*\//) {
178		$nocheck = 1;
179	}
180
181	# a /*JSTYLED*/ comment indicates that the next line is ok.
182	if (/\/\* *JSTYLED.*\*\//) {
183		/^.*\/\* *JSTYLED *(.*) *\*\/.*$/;
184		$okmsg = $1;
185		$nextok = 1;
186	}
187	if (/\/\/ *JSTYLED/) {
188		/^.*\/\/ *JSTYLED *(.*)$/;
189		$okmsg = $1;
190		$nextok = 1;
191	}
192
193	# is this the beginning or ending of a class?
194	if (/^(public\s+)*\w(class|interface)\s/) {
195		$in_class = 1;
196		$in_declaration = 1;
197		$prev = $line;
198		next line;
199	}
200	if (/^}\s*(\/\*.*\*\/\s*)*$/) {
201		$in_class = 0;
202		$prev = $line;
203		next line;
204	}
205
206	if ($comment_done) {
207		$in_comment = 0;
208		$in_header_comment = 0;
209		$comment_done = 0;
210	}
211	# does this looks like the start of a block comment?
212	if (/^\s*\/\*/ && !/^\s*\/\*.*\*\//) {
213		if (/^\s*\/\*./ && !/^\s*\/\*\*$/) {
214			err("improper first line of block comment");
215		}
216		if (!/^(\t|    )*\/\*/) {
217			err("block comment not indented properly");
218		}
219		$in_comment = 1;
220		/^(\s*)\//;
221		$comment_prefix = $1;
222		if ($comment_prefix eq "") {
223			$in_header_comment = 1;
224		}
225		$prev = $line;
226		next line;
227	}
228	# are we still in the block comment?
229	if ($in_comment) {
230		if (/^$comment_prefix \*\/$/) {
231			$comment_done = 1;
232		} elsif (/\*\//) {
233			$comment_done = 1;
234			err("improper block comment close")
235			    unless ($ignore_hdr_comment && $in_header_comment);
236		} elsif (!/^$comment_prefix \*[ \t]/ &&
237		    !/^$comment_prefix \*$/) {
238			err("improper block comment")
239			    unless ($ignore_hdr_comment && $in_header_comment);
240		}
241	}
242
243	if ($in_header_comment && $ignore_hdr_comment) {
244		$prev = $line;
245		next line;
246	}
247
248	# check for errors that might occur in comments and in code.
249
250	# check length of line.
251	# first, a quick check to see if there is any chance of being too long.
252	if ($line =~ tr/\t/\t/ * 7 + length($line) > 80) {
253		# yes, there is a chance.
254		# replace tabs with spaces and check again.
255		my $eline = $line;
256		1 while $eline =~
257		    s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
258		if (length($eline) > 80) {
259			err("line > 80 characters");
260		}
261	}
262
263	# Allow spaces to be used to draw pictures in header comments, but
264	# disallow blocks of spaces almost everywhere else.  In particular,
265	# five spaces are also allowed at the end of a line's indentation
266	# if the rest of the line belongs to a block comment.
267	if (!$in_header_comment &&
268	    /[^ ]     / &&
269	    !(/^\t*     \*/ && !/^\t*     \*.*     /)) {
270		err("spaces instead of tabs");
271	}
272	if ($tabs && /^ / && !/^ \*[ \t\/]/ && !/^ \*$/ &&
273	    (!/^    \w/ || $in_class != 0)) {
274		err("indent by spaces instead of tabs");
275	}
276	if (!$in_comment && (/^(\t    )* {1,3}\S/ || /^(\t    )* {5,7}\S/) &&
277	    !(/^\s*[-+|&\/?:=]/ || ($prev =~ /,\s*$/))) {
278		err("indent not a multiple of 4");
279	}
280	if (/\s$/) {
281		err("space or tab at end of line");
282	}
283if (0) {
284	if (/^[\t]+ [^ \t\*]/ || /^[\t]+  \S/ || /^[\t]+   \S/) {
285		err("continuation line not indented by 4 spaces");
286	}
287}
288	if (/\/\//) {
289		$cpp_comment = 1;
290	}
291	if (!$cpp_comment && /[^ \t(\/]\/\*/ && !/\w\(\/\*.*\*\/\);/) {
292		err("comment preceded by non-blank");
293	}
294	if (/\t +\t/) {
295		err("spaces between tabs");
296	}
297	if (/ \t+ /) {
298		err("tabs between spaces");
299	}
300
301	if ($in_comment) {	# still in comment
302		$prev = $line;
303		next line;
304	}
305
306	if (!$cpp_comment && ((/\/\*\S/ && !/\/\*\*/) || /\/\*\*\S/)) {
307		err("missing blank after open comment");
308	}
309	if (!$cpp_comment && /\S\*\//) {
310		err("missing blank before close comment");
311	}
312	# check for unterminated single line comments.
313	if (/\S.*\/\*/ && !/\S.*\/\*.*\*\//) {
314		err("unterminated single line comment");
315	}
316
317	# delete any comments and check everything else.  Be sure to leave
318	# //-style comments intact, and if there are multiple comments on a
319	# line, preserve whatever's in between.
320	s/(?<!\/)\/\*.*?\*\///g;
321	# Check for //-style comments only outside of block comments
322	if (m{(//(?!$))} && substr($_, $+[0], 1) !~ /[ \t]/) {
323		err("missing blank after start comment");
324	}
325	s/\/\/.*$//;		# C++ comments
326	$cpp_comment = 0;
327
328	# delete any trailing whitespace; we have already checked for that.
329	s/\s*$//;
330
331	# We don't style (yet) what's inside annotations, so just delete them.
332	s/$annotations//;
333
334	# following checks do not apply to text in comments.
335
336	# if it looks like an operator at the end of the line, and it is
337	# not really the end of a comment (...*/), and it is not really
338	# a label (done:), and it is not a case label (case FOO:),
339	# or we are not in a function definition (ANSI C style) and the
340	# operator is a "," (to avoid hitting "int\nfoo(\n\tint i,\n\tint j)"),
341	# or we are in a function and the operator is a
342	# "*" (to avoid hitting on "char*\nfunc()").
343	if ((/[-+|&\/?:=]$/ && !/\*\/$/ && !/^\s*\w*:$/ &&
344	    !/^\s\s*case\s\s*\w*:$/) ||
345	    /,$/ ||
346	    ($in_class && /\*$/)) {
347		$expect_continuation = 1;
348		if (!$in_continuation) {
349			/^(\s*)\S/;
350			$continuation_indent = $1;
351		}
352	}
353	while (/($allops)/g) {
354		my $z = substr($_, $-[1] - 1);
355		if ($z !~ /\s\Q$1\E(?:\s|$)/) {
356			my $m = $1;
357			my $shift;
358			# @+ is available only in the currently active
359			# dynamic scope.  Assign it to a new variable
360			# to pass it into the if block.
361			if ($z =~ /($generics)/ &&
362			    ($shift = $+[1])) {
363				pos $_ += $shift;
364				next;
365			}
366
367			# These need to be in decreasing order of length
368			# (violable as long as there's no ambiguity)
369			my $nospace = "missing space around";
370			if ($m =~ $shiftassignmentops) {
371				err("$nospace assignment operator");
372			} elsif ($m =~ $shiftops) {
373				err("$nospace shift operator");
374			} elsif ($m =~ $relationalops) {
375				err("$nospace relational operator");
376			} elsif ($m =~ $assignmentops) {
377				err("$nospace assignment operator");
378			}
379		}
380	}
381	if (/[,;]\S/ && !/\bfor \(;;\)/) {
382		err("comma or semicolon followed by non-blank");
383	}
384	# allow "for" statements to have empty "while" clauses
385	if (/\s[,;]/ && !/^[\t]+;$/ && !/^\s*for \([^;]*; ;[^;]*\)/) {
386		err("comma or semicolon preceded by blank");
387	}
388if (0) {
389	if (/^\s*(&&|\|\|)/) {
390		err("improper boolean continuation");
391	}
392}
393	if ($picky && /\S   *(&&|\|\|)/ || /(&&|\|\|)   *\S/) {
394		err("more than one space around boolean operator");
395	}
396	if (/\b$keywords\(/) {
397		err("missing space between keyword and paren");
398	}
399	if (/(\b$keywords\b.*){2,}/ && !/\bcase\b.*/) { # "case" excepted
400		err("more than one keyword on line");
401	}
402	if (/\b$keywords\s\s+\(/ &&
403	    !/^#if\s+\(/) {
404		err("extra space between keyword and paren");
405	}
406	# try to detect "func (x)" but not "if (x)" or
407	# "int (*func)();"
408	if (/\w\s\(/) {
409		my $save = $_;
410		# strip off all keywords on the line
411		s/\b$keywords\s\(/XXX(/g;
412		#s/\b($typename|void)\s+\(+/XXX(/og;
413		if (/\w\s\(/) {
414			err("extra space between function name and left paren");
415		}
416		$_ = $save;
417	}
418	if (/\(\s/) {
419		err("whitespace after left paren");
420	}
421	# allow "for" statements to have empty "continue" clauses
422	if (/\s\)/ && !/^\s*for \([^;]*;[^;]*; \)/) {
423		err("whitespace before right paren");
424	}
425	if (/^\s*\(void\)[^ ]/) {
426		err("missing space after (void) cast");
427	}
428	if (/\S{/ && !/{{/) {
429		err("missing space before left brace");
430	}
431	if ($in_class && /^\s+{/ && ($prev =~ /\)\s*$/)) {
432		err("left brace starting a line");
433	}
434	if (/}(else|while)/) {
435		err("missing space after right brace");
436	}
437	if (/}\s\s+(else|while)/) {
438		err("extra space after right brace");
439	}
440	if (/\b$typename\*/o) {
441		err("missing space between type name and *");
442	}
443	if ($heuristic) {
444		# cannot check this everywhere due to "struct {\n...\n} foo;"
445		if ($in_class && !$in_declaration &&
446		    /}./ && !/}\s+=/ && !/{.*}[;,]$/ && !/}(\s|)*$/ &&
447		    !/} (else|while)/ && !/}}/) {
448			err("possible bad text following right brace");
449		}
450		# cannot check this because sub-blocks in
451		# the middle of code are ok
452		if ($in_class && /^\s+{/) {
453			err("possible left brace starting a line");
454		}
455	}
456	if (/^\s*else\W/) {
457		if ($prev =~ /^\s*}$/) {
458			my $str = "else and right brace should be on same line";
459			printf $fmt, $filename, $., $str, $prev;
460			if ($verbose) {
461				printf "%s\n", $line;
462			}
463		}
464	}
465	$prev = $line;
466}
467
468if ($picky && $prev eq "") {
469	err("last line in file is blank");
470}
471
472}
473