1- # !/usr/bin/perl -w
1+ # !/usr/bin/env perl
22#
33# CDDL HEADER START
44#
2323# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2424# Use is subject to license terms.
2525#
26+ # Portions copyright 2017, Intel Corporation.
27+ #
2628# @(#)cstyle 1.58 98/09/09 (from shannon)
2729# ident "%Z%%M% %I% %E% SMI"
2830#
5456use IO::File;
5557use Getopt::Std;
5658use strict;
59+ use warnings;
5760
5861my $usage =
5962" usage: cstyle [-chpvCP] [-o constructs] file ...
@@ -328,7 +331,21 @@ ($$)
328331 1 while $eline =~
329332 s /\t +/ ' ' x (length($& ) * 8 - length($` ) % 8)/ e ;
330333 if (length ($eline ) > 80) {
331- err(" line > 80 characters" );
334+ # allow long line if it is user visible string
335+ # find if line start from " and ends
336+ # with " + 2 optional characters
337+ # (these characters can be i.e. '");' '" \' or '",' etc...)
338+ if ($eline =~ / ^ *".*"[^"]{0,2}$ / ) {
339+ # check if entire line is one string literal
340+ $eline =~ s / ^ *"// ;
341+ $eline =~ s / "[^"]{0,2}$// ;
342+
343+ if ($eline =~ / [^\\ ]"|[^\\ ](\\\\ )+"/ ) {
344+ err(" line > 80 characters" );
345+ }
346+ } else {
347+ err(" line > 80 characters" );
348+ }
332349 }
333350 }
334351
513530 # ".*?" is a non-greedy match, so that we don't get confused by
514531 # multiple comments on the same line.
515532 #
516- s /\/\* .*?\*\/ / / g ;
517- s /\/\/ .*$/ / ; # C++ comments
533+ s /\/\* .*?\*\/ / \x01 / g ;
534+ s /\/\/ .*$/ \x01 / ; # C++ comments
518535
519536 # delete any trailing whitespace; we have already checked for that.
520537 s /\s *$// ;
606623 s /\w\s\( +\* / XXX(*/ g ;
607624 s /\b ($typename|void)\s +\( +/ XXX(/ og ;
608625 s /\b typedef\s ($typename|void)\s +\( +/ XXX(/ og ;
626+ # do not match "__attribute__ ((format (...)))"
627+ s /\b __attribute__\s *\(\( format\s *\( / __attribute__((XXX(/ g ;
609628 if (/ \w\s\( / ) {
610629 err(" extra space between function name and left paren" );
611630 }
@@ -614,12 +633,12 @@ ($$)
614633 # try to detect "int foo(x)", but not "extern int foo(x);"
615634 # XXX - this still trips over too many legitimate things,
616635 # like "int foo(x,\n\ty);"
617- # if (/^(\w+(\s|\*)+)+\w+\(/ && !/\)[;,](\s|)*$/ &&
636+ # if (/^(\w+(\s|\*)+)+\w+\(/ && !/\)[;,](\s|\x01 )*$/ &&
618637# !/^(extern|static)\b/) {
619638# err("return type of function not on separate line");
620639# }
621640 # this is a close approximation
622- if (/ ^(\w +(\s |\* )+)+\w +\( .*\) (\s |)*$ / &&
641+ if (/ ^(\w +(\s |\* )+)+\w +\( .*\) (\s |\x01 )*$ / &&
623642 !/^(extern|static)\b/) {
624643 err(" return type of function not on separate line" );
625644 }
649668 if (/ ^\s *\( void\) [^ ]/ ) {
650669 err(" missing space after (void) cast" );
651670 }
652- if (/ \S\{ / && !/\{\{/ ) {
671+ if (/ \S\{ / && !/\{\{/ && !/ \( struct \w + \)\{ / ) {
653672 err(" missing space before left brace" );
654673 }
655674 if ($in_function && / ^\s +{/ &&
729748 if ($heuristic ) {
730749 # cannot check this everywhere due to "struct {\n...\n} foo;"
731750 if ($in_function && !$in_declaration &&
732- / }./ && !/}\s +=/ && !/{.*}[;,]$/ && !/}(\s |)*$/ &&
751+ / }./ && !/}\s +=/ && !/{.*}[;,]$/ && !/}(\s |\ x 01 )*$/ &&
733752 !/} (else |while )/ && !/}}/) {
734753 err(" possible bad text following right brace" );
735754 }
814833 require strict;
815834 local $_ = $_ [0]; # preserve the global $_
816835
817- s /// g ; # No comments
836+ s /\x01 // g ; # No comments
818837 s /\s +$// ; # Strip trailing whitespace
819838
820839 return if (/ ^$ / ); # skip empty lines
826845
827846 # skip over enumerations, array definitions, initializers, etc.
828847 if ($cont_off <= 0 && !/^\s *$special / &&
829- (/ (?:(?:\b (?:enum|struct|union)\s *[^\{ ]*)|(?:\s +=\s *)){/ ||
848+ (/ (?:(?:\b (?:enum|struct|union)\s *[^\{ ]*)|(?:\s +=\s *))\ {/ ||
830849 (/ ^\s *{/ && $prev =~ / =\s *(?:\/\* .*\*\/\s *)*$ / ))) {
831850 $cont_in = 0;
832851 $cont_off = tr / {/ {/ - tr / }/ }/ ;
0 commit comments