1#!/bin/sh -- # This comment tells perl not to loop! 2# 3#*************************************************************************** 4# 5# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6# 7# Copyright 2000, 2010 Oracle and/or its affiliates. 8# 9# OpenOffice.org - a multi-platform office productivity suite 10# 11# This file is part of OpenOffice.org. 12# 13# OpenOffice.org is free software: you can redistribute it and/or modify 14# it under the terms of the GNU Lesser General Public License version 3 15# only, as published by the Free Software Foundation. 16# 17# OpenOffice.org is distributed in the hope that it will be useful, 18# but WITHOUT ANY WARRANTY; without even the implied warranty of 19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20# GNU Lesser General Public License version 3 for more details 21# (a copy is included in the LICENSE file that accompanied this code). 22# 23# You should have received a copy of the GNU Lesser General Public License 24# version 3 along with OpenOffice.org. If not, see 25# <http://www.openoffice.org/license.html> 26# for a copy of the LGPLv3 License. 27# 28#*************************************************************************** 29 30 31eval 'exec perl -S $0 "$@"' 32if 0; 33# 34# @(#)jstyle 1.2 98/01/08 35# 36# jstyle - check for some common stylistic errors. 37# 38# jstyle is a sort of "lint" for Java coding style. 39# 40# There's a lot this can't check for, like proper 41# indentation of continuation lines. There's also 42# a lot more this could check for. 43# 44# A note to the non perl literate: 45# 46# perl regular expressions are pretty much like egrep 47# regular expressions, with the following special symbols 48# 49# \s any space character 50# \S any non-space character 51# \w any "word" character [a-zA-Z0-9_] 52# \W any non-word character 53# \d a digit [0-9] 54# \D a non-digit 55# \b word boundary (between \w and \W) 56# \B non-word boundary 57# 58#require "getopts.pl"; 59# XXX - because some versions of perl can not find the lib directory, 60# we just include this here. 61;# getopts.pl - a better getopt.pl 62 63;# Usage: 64;# do Getopts("a:bc"); # -a takes arg. -b & -c not. Sets opt_* as a 65;# # side effect. 66 67sub Getopts { 68 local($argumentative) = @_; 69 local(@args,$_,$first,$rest); 70 local($[) = 0; 71 local($errs) = 0; 72 73 @args = split( / */, $argumentative ); 74 while(($_ = $ARGV[0]) =~ /^-(.)(.*)/) { 75 ($first,$rest) = ($1,$2); 76 $pos = index($argumentative,$first); 77 if($pos >= $[) { 78 if($args[$pos+1] eq ":") { 79 shift(@ARGV); 80 if($rest eq "") { 81 $rest = shift(@ARGV); 82 } 83 eval "\$opt_$first = \$rest;"; 84 } 85 else { 86 eval "\$opt_$first = 1"; 87 if($rest eq "") { 88 shift(@ARGV); 89 } 90 else { 91 $ARGV[0] = "-$rest"; 92 } 93 } 94 } 95 else { 96 print STDERR "Unknown option: $first\n"; 97 ++$errs; 98 if($rest ne "") { 99 $ARGV[0] = "-$rest"; 100 } 101 else { 102 shift(@ARGV); 103 } 104 } 105 } 106 $errs == 0; 107} 108 1091; 110# end of getopts.pl 111 112$usage = 113"usage: jstyle [-c] [-h] [-p] [-s] [-t] [-v] [-C] file ... 114 -c check continuation line indenting 115 -h perform heuristic checks that are sometimes wrong 116 -p perform some of the more picky checks 117 -s check for spaces vs. tabs 118 -t insist on indenting by tabs 119 -v verbose 120 -C don't check anything in header block comments 121 -S print out overall statistics 122"; 123 124if (!&Getopts("chpstvCS")) { 125 print $usage; 126 exit 1; 127} 128 129$check_continuation = $opt_c; 130$heuristic = $opt_h; 131$picky = $opt_p; 132$spaces = $opt_s; 133$tabs = $opt_t; 134$verbose = $opt_v; 135$ignore_hdr_comment = $opt_C; 136$statistics = $opt_S; 137 138if ($verbose) { 139 $fmt = "%s: %d: %s\n%s\n"; 140} else { 141 $fmt = "%s: %d: %s\n"; 142} 143 144# Note, following must be in single quotes so that \s and \w work right. 145$typename = '(int|char|boolean|byte|short|long|float|double)'; 146 147if ($#ARGV >= 0) { 148 foreach $arg (@ARGV) { 149 if (!open(STDIN, $arg)) { 150 printf "%s: can not open\n", $arg; 151 } else { 152 &jstyle($arg); 153 close STDIN; 154 } 155 } 156} else { 157 &jstyle("<stdin>"); 158} 159 160if ($statistics != 0) { 161 foreach $key (sort(keys %errcount)) { 162 printf "%6d %s\n", $errcount{$key}, $key; 163 } 164 printf " -----\n"; 165 printf "%6d Total warnings\n", $tot_errcount; 166 printf "%6d Lines of code\n", $totlines; 167} 168 169sub err { 170 if ($statistics == 0) { 171 printf $fmt, $filename, $., $_[0], $line; 172 } else { 173 $msg = $_[0]; 174 $msg =~ s/ \([0-9][0-9]*\)$//; 175 $errcount{$msg} += 1; 176 $tot_errcount += 1; 177 } 178} 179 180sub jstyle { 181 182$in_comment = 0; 183$in_header_comment = 0; 184$in_continuation = 0; 185$in_class = 0; 186$in_declaration = 0; 187$note_level = 0; 188$nextok = 0; 189$nocheck = 0; 190$expect_continuation = 0; 191$prev = ''; 192 193$filename = $_[0]; 194 195line: while (<STDIN>) { 196 ++$totlines; 197 s/\r?\n$//; # strip return and newline 198 199 # save the original line, then remove all text from within 200 # double or single quotes, we do not want to check such text. 201 202 $line = $_; 203 s/"[^"]*"/\"\"/g; 204 s/'.'/''/g; 205 206 # an /* END JSTYLED */ comment ends a no-check block. 207 if ($nocheck) { 208 if (/\/\* *END *JSTYLED *\*\//) { 209 $nocheck = 0; 210 } else { 211 next line; 212 } 213 } 214 215 # a /*JSTYLED*/ comment indicates that the next line is ok. 216 if ($nextok) { 217 if ($okmsg) { 218 do err($okmsg); 219 } 220 $nextok = 0; 221 $okmsg = 0; 222 if (/\/\* *JSTYLED.*\*\//) { 223 /^.*\/\* *JSTYLED *(.*) *\*\/.*$/; 224 $okmsg = $1; 225 $nextok = 1; 226 } 227 $prev = $line; 228 next line; 229 } 230 231 # check length of line. 232 # first, a quick check to see if there is any chance of being too long. 233 if ($line =~ tr/\t/\t/ * 7 + length($line) > 100) { 234 # yes, there is a chance. 235 # replace tabs with spaces and check again. 236 $eline = $line; 237 1 while $eline =~ 238 s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e; 239 $l = length($eline); 240 if (length($eline) > 100) { 241 do err("line > 100 characters ($l)"); 242 } 243 } 244# this is the fastest way to check line length, 245# but it doesnt work with perl 3.0. 246# if ($line =~ tr/\t/\t/ * 7 + length($line) > 80) { 247# $pos = $oldp = $p = 0; 248# while (($p = index($line, "\t", $p)) >= 0) { 249# $pos = ($pos + $p - $oldp + 8) & ~7; 250# $oldp = ++$p; 251# } 252# $pos += length($line) - $oldp; 253# if ($pos > 80) { 254# do err("line > 80 characters"); 255# } 256# } 257 258 # remember whether we expect to be inside a continuation line. 259 $in_continuation = $expect_continuation; 260 261 # check for proper continuation line. blank lines 262 # in the middle of the 263 # continuation do not count. 264 # XXX - only check within functions. 265 if ($check_continuation && $expect_continuation && $in_class && 266 !/^\s*$/) { 267 # continuation line must start with whitespace of 268 # previous line, plus either 4 spaces or a tab, but 269 # do not check lines that start with a string constant 270 # since they are often shifted to the left to make them 271 # fit on the line. 272 if (!/^$continuation_indent \S/ && 273 !/^$continuation_indent\t\S/ && !/^\s*"/) { 274 do err("continuation line improperly indented"); 275 } 276 $expect_continuation = 0; 277 } 278 279 # a /* BEGIN JSTYLED */ comment starts a no-check block. 280 if (/\/\* *BEGIN *JSTYLED *\*\//) { 281 $nocheck = 1; 282 } 283 284 # a /*JSTYLED*/ comment indicates that the next line is ok. 285 if (/\/\* *JSTYLED.*\*\//) { 286 /^.*\/\* *JSTYLED *(.*) *\*\/.*$/; 287 $okmsg = $1; 288 $nextok = 1; 289 } 290 if (/\/\/ *JSTYLED/) { 291 /^.*\/\/ *JSTYLED *(.*)$/; 292 $okmsg = $1; 293 $nextok = 1; 294 } 295 296 # is this the beginning or ending of a class? 297 if (/^(public\s+)*\w(class|interface)\s/) { 298 $in_class = 1; 299 $in_declaration = 1; 300 $prev = $line; 301 next line; 302 } 303 if (/^}\s*(\/\*.*\*\/\s*)*$/) { 304 $in_class = 0; 305 $prev = $line; 306 next line; 307 } 308 309 if (!$spaces) { 310 # strip trailing spaces 311 s/\s*$//; 312 } 313 314 # does this looks like the start of a block comment? 315 if (/^\s*\/\*(\*|)$/) { 316 if (!/^(\t| )*\/\*(\*|)$/) { 317 do err("block comment not indented properly"); 318 } 319 $in_comment = 1; 320 s/\/\*(\*|)/ /; 321 $comment_prefix = $_; 322 if ($comment_prefix eq " ") { 323 $in_header_comment = 1; 324 } 325 $prev = $line; 326 next line; 327 } 328 if (/^\s*\/\*./ && !/^\s*\/\*\*$/ && !/^\s*\/\*.*\*\//) { 329 do err("improper first line of block comment"); 330 # it's a bad one, but it still is one. 331 # avoid ripple effect of not recognizing this. 332 if (!/^(\t| )*\/\*(\*|)/) { 333 do err("block comment not indented properly"); 334 } 335 $in_comment = 1; 336 s/\/\*.*/ /; 337 $comment_prefix = $_; 338 if ($comment_prefix eq " ") { 339 $in_header_comment = 1; 340 } 341 $prev = $line; 342 next line; 343 } 344 # are we still in the block comment? 345 if ($in_comment && !/^$comment_prefix\*/) { 346 # assume out of comment 347 $in_comment = 0; 348 $in_header_comment = 0; 349 } 350 351 if ($in_header_comment && $ignore_hdr_comment) { 352 $prev = $line; 353 next line; 354 } 355 356 # check for errors that might occur in comments and in code. 357 358 # allow spaces to be used to draw pictures in header comments. 359 if ($spaces && /[^ ] / && !/".* .*"/ && !$in_header_comment) { 360 do err("spaces instead of tabs"); 361 } 362 if ($tabs && /^ / && !/^ \*[ \t\/]/ && !/^ \*$/ && 363 (!/^ \w/ || $in_class != 0)) { 364 do err("indent by spaces instead of tabs"); 365 } 366 if (!$in_comment && (/^(\t )* {1,3}\S/ || /^(\t )* {5,7}\S/) && 367 !(/^\s*[-+|&\/?:=]/ || ($prev =~ /,\s*$/))) { 368 do err("indent not a multiple of 4"); 369 } 370 if ($spaces && /\s$/) { 371 do err("space or tab at end of line"); 372 } 373if (0) { 374 if (/^[\t]+ [^ \t\*]/ || /^[\t]+ \S/ || /^[\t]+ \S/) { 375 do err("continuation line not indented by 4 spaces"); 376 } 377} 378 if (/[^ \t(]\/\*/ && !/\w\(\/\*.*\*\/\);/) { 379 do err("comment preceded by non-blank"); 380 } 381 if ($spaces && /\t[ ]+\t/) { 382 do err("spaces between tabs"); 383 } 384 if ($spaces && / [\t]+ /) { 385 do err("tabs between spaces"); 386 } 387 388 if ($in_comment) { # still in comment 389 $prev = $line; 390 next line; 391 } 392 393 if ((/\/\*\S/ && !/\/\*\*/) || /\/\*\*\S/) { 394 do err("missing blank after open comment"); 395 } 396 if (/\S\*\//) { 397 do err("missing blank before close comment"); 398 } 399 # allow // at beginnging of line, often used to comment out code 400 if (/.\/\/\S/) { # C++ comments 401 do err("missing blank after start comment"); 402 } 403 # check for unterminated single line comments. 404 if (/\S.*\/\*/ && !/\S.*\/\*.*\*\//) { 405 do err("unterminated single line comment"); 406 } 407 408 # delete any comments and check everything else. 409 s/\/\*.*\*\///g; 410 s/\/\/.*$//; # C++ comments 411 412 # delete any trailing whitespace; we have already checked for that. 413 s/\s*$//; 414 415 # following checks do not apply to text in comments. 416 417 # if it looks like an operator at the end of the line, and it is 418 # not really the end of a comment (...*/), and it is not really 419 # a label (done:), and it is not a case label (case FOO:), 420 # or we are not in a function definition (ANSI C style) and the 421 # operator is a "," (to avoid hitting "int\nfoo(\n\tint i,\n\tint j)"), 422 # or we are in a function and the operator is a 423 # "*" (to avoid hitting on "char*\nfunc()"). 424 if ((/[-+|&\/?:=]$/ && !/\*\/$/ && !/^\s*\w*:$/ && 425 !/^\s\s*case\s\s*\w*:$/) || 426 /,$/ || 427 ($in_class && /\*$/)) { 428 $expect_continuation = 1; 429 if (!$in_continuation) { 430 /^(\s*)\S/; 431 $continuation_indent = $1; 432 } 433 } 434 if (/[^<>\s][!<>=]=/ || /[^<>][!<>=]=\S/ || 435 (/[^->]>[^=>\s]/ && !/[^->]>$/) || (/[^<]<[^=<\s]/ && !/[^<]<$/) || 436 /[^<\s]<[^<]/ || /[^->\s]>[^>]/) { 437 do err("missing space around relational operator"); 438 } 439 if (/\S>>=/ || /\S<<=/ || />>=\S/ || /<<=\S/ || /\S[-+*\/&|^%]=/ || 440 (/[^-+*\/&|^%!<>=\s]=[^=]/ && !/[^-+*\/&|^%!<>=\s]=$/) || 441 (/[^!<>=]=[^=\s]/ && !/[^!<>=]=$/)) { 442 do err("missing space around assignment operator"); 443 } 444 if (/[,;]\S/ && !/\bfor \(;;\)/) { 445 do err("comma or semicolon followed by non-blank"); 446 } 447 # allow "for" statements to have empty "while" clauses 448 if (/\s[,;]/ && !/^[\t]+;$/ && !/^\s*for \([^;]*; ;[^;]*\)/) { 449 do err("comma or semicolon preceded by blank"); 450 } 451if (0) { 452 if (/^\s*(&&|\|\|)/) { 453 do err("improper boolean continuation"); 454 } 455} 456 if ($picky && /\S *(&&|\|\|)/ || /(&&|\|\|) *\S/) { 457 do err("more than one space around boolean operator"); 458 } 459 if (/\b(for|if|while|switch|return|case|catch|synchronized)\(/) { 460 do err("missing space between keyword and paren"); 461 } 462 if (/(\b(for|if|while|switch|return|catch|synchronized)\b.*){2,}/) { 463 # multiple "case" allowed 464 do err("more than one keyword on line"); 465 } 466 if (/\b(for|if|while|switch|return|case|catch|synchronized)\s\s+\(/ && 467 !/^#if\s+\(/) { 468 do err("extra space between keyword and paren"); 469 } 470 # try to detect "func (x)" but not "if (x)" or 471 # "int (*func)();" 472 if (/\w\s\(/) { 473 $s = $_; 474 # strip off all keywords on the line 475 s/\b(for|if|while|switch|return|case|catch|synchronized)\s\(/XXX(/g; 476 #s/\b($typename|void)\s+\(+/XXX(/og; 477 if (/\w\s\(/) { 478 do err("extra space between function name and left paren"); 479 } 480 $_ = $s; 481 } 482 if (/\(\s/) { 483 do err("whitespace after left paren"); 484 } 485 # allow "for" statements to have empty "continue" clauses 486 if (/\s\)/ && !/^\s*for \([^;]*;[^;]*; \)/) { 487 do err("whitespace before right paren"); 488 } 489 if (/^\s*\(void\)[^ ]/) { 490 do err("missing space after (void) cast"); 491 } 492 if (/\S{/ && !/{{/) { 493 do err("missing space before left brace"); 494 } 495 if ($in_class && /^\s+{/ && ($prev =~ /\)\s*$/)) { 496 do err("left brace starting a line"); 497 } 498 if (/}(else|while)/) { 499 do err("missing space after right brace"); 500 } 501 if (/}\s\s+(else|while)/) { 502 do err("extra space after right brace"); 503 } 504 if (/\b$typename\*/o) { 505 do err("missing space between type name and *"); 506 } 507 if ($heuristic) { 508 # cannot check this everywhere due to "struct {\n...\n} foo;" 509 if ($in_class && !$in_declaration && 510 /}./ && !/}\s+=/ && !/{.*}[;,]$/ && !/}(\s|)*$/ && 511 !/} (else|while)/ && !/}}/) { 512 do err("possible bad text following right brace"); 513 } 514 # cannot check this because sub-blocks in 515 # the middle of code are ok 516 if ($in_class && /^\s+{/) { 517 do err("possible left brace starting a line"); 518 } 519 } 520 if (/^\s*else\W/) { 521 if ($prev =~ /^\s*}$/) { 522 $str = "else and right brace should be on same line"; 523 if ($statistics == 0) { 524 printf $fmt, $filename, $., $str, $prev; 525 if ($verbose) { 526 printf "%s\n", $line; 527 } 528 } else { 529 $errcount{$str} += 1; 530 $tot_errcount += 1; 531 } 532 } 533 } 534 $prev = $line; 535} 536 537if ($picky && $prev eq "") { 538 do err("last line in file is blank"); 539} 540 541} 542