Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 67d2cf0

Browse files
author
Sarah Jelinek
authored
Merge pull request #42 from GBuella/cstlye_sync
cstyle -- sync with NVML/cstyle
2 parents 7451220 + cafa7d1 commit 67d2cf0

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

utils/cstyle.pl

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/perl -w
1+
#!/usr/bin/env perl
22
#
33
# CDDL HEADER START
44
#
@@ -23,6 +23,8 @@
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
#
@@ -54,6 +56,7 @@
5456
use IO::File;
5557
use Getopt::Std;
5658
use strict;
59+
use warnings;
5760

5861
my $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

@@ -513,8 +530,8 @@ ($$)
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*$//;
@@ -606,6 +623,8 @@ ($$)
606623
s/\w\s\(+\*/XXX(*/g;
607624
s/\b($typename|void)\s+\(+/XXX(/og;
608625
s/\btypedef\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
}
@@ -649,7 +668,7 @@ ($$)
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+{/ &&
@@ -729,7 +748,7 @@ ($$)
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|\x01)*$/ &&
733752
!/} (else|while)/ && !/}}/) {
734753
err("possible bad text following right brace");
735754
}
@@ -814,7 +833,7 @@ ($$)
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
@@ -826,7 +845,7 @@ ($$)
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

Comments
 (0)