checkpatch.pl 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335
  1. #!/usr/bin/perl -w
  2. # (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (the file handling bit)
  3. # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
  4. # (c) 2007, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite, etc)
  5. # Licensed under the terms of the GNU GPL License version 2
  6. use strict;
  7. my $P = $0;
  8. $P =~ s@.*/@@g;
  9. my $V = '0.21';
  10. use Getopt::Long qw(:config no_auto_abbrev);
  11. my $quiet = 0;
  12. my $tree = 1;
  13. my $chk_signoff = 1;
  14. my $chk_patch = 1;
  15. my $tst_only;
  16. my $emacs = 0;
  17. my $terse = 0;
  18. my $file = 0;
  19. my $check = 0;
  20. my $summary = 1;
  21. my $mailback = 0;
  22. my $summary_file = 0;
  23. my $root;
  24. my %debug;
  25. GetOptions(
  26. 'q|quiet+' => \$quiet,
  27. 'tree!' => \$tree,
  28. 'signoff!' => \$chk_signoff,
  29. 'patch!' => \$chk_patch,
  30. 'emacs!' => \$emacs,
  31. 'terse!' => \$terse,
  32. 'file!' => \$file,
  33. 'subjective!' => \$check,
  34. 'strict!' => \$check,
  35. 'root=s' => \$root,
  36. 'summary!' => \$summary,
  37. 'mailback!' => \$mailback,
  38. 'summary-file!' => \$summary_file,
  39. 'debug=s' => \%debug,
  40. 'test-only=s' => \$tst_only,
  41. ) or exit;
  42. my $exit = 0;
  43. if ($#ARGV < 0) {
  44. print "usage: $P [options] patchfile\n";
  45. print "version: $V\n";
  46. print "options: -q => quiet\n";
  47. print " --no-tree => run without a kernel tree\n";
  48. print " --terse => one line per report\n";
  49. print " --emacs => emacs compile window format\n";
  50. print " --file => check a source file\n";
  51. print " --strict => enable more subjective tests\n";
  52. print " --root => path to the kernel tree root\n";
  53. print " --no-summary => suppress the per-file summary\n";
  54. print " --summary-file => include the filename in summary\n";
  55. exit(1);
  56. }
  57. my $dbg_values = 0;
  58. my $dbg_possible = 0;
  59. my $dbg_type = 0;
  60. for my $key (keys %debug) {
  61. eval "\${dbg_$key} = '$debug{$key}';"
  62. }
  63. if ($terse) {
  64. $emacs = 1;
  65. $quiet++;
  66. }
  67. if ($tree) {
  68. if (defined $root) {
  69. if (!top_of_kernel_tree($root)) {
  70. die "$P: $root: --root does not point at a valid tree\n";
  71. }
  72. } else {
  73. if (top_of_kernel_tree('.')) {
  74. $root = '.';
  75. } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
  76. top_of_kernel_tree($1)) {
  77. $root = $1;
  78. }
  79. }
  80. if (!defined $root) {
  81. print "Must be run from the top-level dir. of a kernel tree\n";
  82. exit(2);
  83. }
  84. }
  85. my $emitted_corrupt = 0;
  86. our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
  87. our $Storage = qr{extern|static|asmlinkage};
  88. our $Sparse = qr{
  89. __user|
  90. __kernel|
  91. __force|
  92. __iomem|
  93. __must_check|
  94. __init_refok|
  95. __kprobes
  96. }x;
  97. our $Attribute = qr{
  98. const|
  99. __read_mostly|
  100. __kprobes|
  101. __(?:mem|cpu|dev|)(?:initdata|init)
  102. }x;
  103. our $Modifier;
  104. our $Inline = qr{inline|__always_inline|noinline};
  105. our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
  106. our $Lval = qr{$Ident(?:$Member)*};
  107. our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
  108. our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
  109. our $Operators = qr{
  110. <=|>=|==|!=|
  111. =>|->|<<|>>|<|>|!|~|
  112. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
  113. }x;
  114. our $NonptrType;
  115. our $Type;
  116. our $Declare;
  117. our $UTF8 = qr {
  118. [\x09\x0A\x0D\x20-\x7E] # ASCII
  119. | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  120. | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
  121. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  122. | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  123. | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  124. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  125. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  126. }x;
  127. our @typeList = (
  128. qr{void},
  129. qr{(?:unsigned\s+)?char},
  130. qr{(?:unsigned\s+)?short},
  131. qr{(?:unsigned\s+)?int},
  132. qr{(?:unsigned\s+)?long},
  133. qr{(?:unsigned\s+)?long\s+int},
  134. qr{(?:unsigned\s+)?long\s+long},
  135. qr{(?:unsigned\s+)?long\s+long\s+int},
  136. qr{unsigned},
  137. qr{float},
  138. qr{double},
  139. qr{bool},
  140. qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
  141. qr{struct\s+$Ident},
  142. qr{union\s+$Ident},
  143. qr{enum\s+$Ident},
  144. qr{${Ident}_t},
  145. qr{${Ident}_handler},
  146. qr{${Ident}_handler_fn},
  147. );
  148. our @modifierList = (
  149. qr{fastcall},
  150. );
  151. sub build_types {
  152. my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
  153. my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
  154. $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
  155. $NonptrType = qr{
  156. (?:$Modifier\s+|const\s+)*
  157. (?:
  158. (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
  159. (?:${all}\b)
  160. )
  161. (?:\s+$Modifier|\s+const)*
  162. }x;
  163. $Type = qr{
  164. $NonptrType
  165. (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
  166. (?:\s+$Inline|\s+$Modifier)*
  167. }x;
  168. $Declare = qr{(?:$Storage\s+)?$Type};
  169. }
  170. build_types();
  171. $chk_signoff = 0 if ($file);
  172. my @dep_includes = ();
  173. my @dep_functions = ();
  174. my $removal = "Documentation/feature-removal-schedule.txt";
  175. if ($tree && -f "$root/$removal") {
  176. open(REMOVE, "<$root/$removal") ||
  177. die "$P: $removal: open failed - $!\n";
  178. while (<REMOVE>) {
  179. if (/^Check:\s+(.*\S)/) {
  180. for my $entry (split(/[, ]+/, $1)) {
  181. if ($entry =~ m@include/(.*)@) {
  182. push(@dep_includes, $1);
  183. } elsif ($entry !~ m@/@) {
  184. push(@dep_functions, $entry);
  185. }
  186. }
  187. }
  188. }
  189. }
  190. my @rawlines = ();
  191. my @lines = ();
  192. my $vname;
  193. for my $filename (@ARGV) {
  194. if ($file) {
  195. open(FILE, "diff -u /dev/null $filename|") ||
  196. die "$P: $filename: diff failed - $!\n";
  197. } else {
  198. open(FILE, "<$filename") ||
  199. die "$P: $filename: open failed - $!\n";
  200. }
  201. if ($filename eq '-') {
  202. $vname = 'Your patch';
  203. } else {
  204. $vname = $filename;
  205. }
  206. while (<FILE>) {
  207. chomp;
  208. push(@rawlines, $_);
  209. }
  210. close(FILE);
  211. if (!process($filename)) {
  212. $exit = 1;
  213. }
  214. @rawlines = ();
  215. @lines = ();
  216. }
  217. exit($exit);
  218. sub top_of_kernel_tree {
  219. my ($root) = @_;
  220. my @tree_check = (
  221. "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
  222. "README", "Documentation", "arch", "include", "drivers",
  223. "fs", "init", "ipc", "kernel", "lib", "scripts",
  224. );
  225. foreach my $check (@tree_check) {
  226. if (! -e $root . '/' . $check) {
  227. return 0;
  228. }
  229. }
  230. return 1;
  231. }
  232. sub expand_tabs {
  233. my ($str) = @_;
  234. my $res = '';
  235. my $n = 0;
  236. for my $c (split(//, $str)) {
  237. if ($c eq "\t") {
  238. $res .= ' ';
  239. $n++;
  240. for (; ($n % 8) != 0; $n++) {
  241. $res .= ' ';
  242. }
  243. next;
  244. }
  245. $res .= $c;
  246. $n++;
  247. }
  248. return $res;
  249. }
  250. sub copy_spacing {
  251. (my $res = shift) =~ tr/\t/ /c;
  252. return $res;
  253. }
  254. sub line_stats {
  255. my ($line) = @_;
  256. # Drop the diff line leader and expand tabs
  257. $line =~ s/^.//;
  258. $line = expand_tabs($line);
  259. # Pick the indent from the front of the line.
  260. my ($white) = ($line =~ /^(\s*)/);
  261. return (length($line), length($white));
  262. }
  263. my $sanitise_quote = '';
  264. sub sanitise_line_reset {
  265. my ($in_comment) = @_;
  266. if ($in_comment) {
  267. $sanitise_quote = '*/';
  268. } else {
  269. $sanitise_quote = '';
  270. }
  271. }
  272. sub sanitise_line {
  273. my ($line) = @_;
  274. my $res = '';
  275. my $l = '';
  276. my $qlen = 0;
  277. my $off = 0;
  278. my $c;
  279. # Always copy over the diff marker.
  280. $res = substr($line, 0, 1);
  281. for ($off = 1; $off < length($line); $off++) {
  282. $c = substr($line, $off, 1);
  283. # Comments we are wacking completly including the begin
  284. # and end, all to $;.
  285. if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
  286. $sanitise_quote = '*/';
  287. substr($res, $off, 2, "$;$;");
  288. $off++;
  289. next;
  290. }
  291. if (substr($line, $off, 2) eq '*/') {
  292. $sanitise_quote = '';
  293. substr($res, $off, 2, "$;$;");
  294. $off++;
  295. next;
  296. }
  297. # A \ in a string means ignore the next character.
  298. if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
  299. $c eq "\\") {
  300. substr($res, $off, 2, 'XX');
  301. $off++;
  302. next;
  303. }
  304. # Regular quotes.
  305. if ($c eq "'" || $c eq '"') {
  306. if ($sanitise_quote eq '') {
  307. $sanitise_quote = $c;
  308. substr($res, $off, 1, $c);
  309. next;
  310. } elsif ($sanitise_quote eq $c) {
  311. $sanitise_quote = '';
  312. }
  313. }
  314. #print "SQ:$sanitise_quote\n";
  315. if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
  316. substr($res, $off, 1, $;);
  317. } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
  318. substr($res, $off, 1, 'X');
  319. } else {
  320. substr($res, $off, 1, $c);
  321. }
  322. }
  323. # The pathname on a #include may be surrounded by '<' and '>'.
  324. if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
  325. my $clean = 'X' x length($1);
  326. $res =~ s@\<.*\>@<$clean>@;
  327. # The whole of a #error is a string.
  328. } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
  329. my $clean = 'X' x length($1);
  330. $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
  331. }
  332. return $res;
  333. }
  334. sub ctx_statement_block {
  335. my ($linenr, $remain, $off) = @_;
  336. my $line = $linenr - 1;
  337. my $blk = '';
  338. my $soff = $off;
  339. my $coff = $off - 1;
  340. my $coff_set = 0;
  341. my $loff = 0;
  342. my $type = '';
  343. my $level = 0;
  344. my $p;
  345. my $c;
  346. my $len = 0;
  347. my $remainder;
  348. while (1) {
  349. #warn "CSB: blk<$blk> remain<$remain>\n";
  350. # If we are about to drop off the end, pull in more
  351. # context.
  352. if ($off >= $len) {
  353. for (; $remain > 0; $line++) {
  354. next if ($lines[$line] =~ /^-/);
  355. $remain--;
  356. $loff = $len;
  357. $blk .= $lines[$line] . "\n";
  358. $len = length($blk);
  359. $line++;
  360. last;
  361. }
  362. # Bail if there is no further context.
  363. #warn "CSB: blk<$blk> off<$off> len<$len>\n";
  364. if ($off >= $len) {
  365. last;
  366. }
  367. }
  368. $p = $c;
  369. $c = substr($blk, $off, 1);
  370. $remainder = substr($blk, $off);
  371. #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
  372. # Statement ends at the ';' or a close '}' at the
  373. # outermost level.
  374. if ($level == 0 && $c eq ';') {
  375. last;
  376. }
  377. # An else is really a conditional as long as its not else if
  378. if ($level == 0 && $coff_set == 0 &&
  379. (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
  380. $remainder =~ /^(else)(?:\s|{)/ &&
  381. $remainder !~ /^else\s+if\b/) {
  382. $coff = $off + length($1) - 1;
  383. $coff_set = 1;
  384. #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
  385. #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
  386. }
  387. if (($type eq '' || $type eq '(') && $c eq '(') {
  388. $level++;
  389. $type = '(';
  390. }
  391. if ($type eq '(' && $c eq ')') {
  392. $level--;
  393. $type = ($level != 0)? '(' : '';
  394. if ($level == 0 && $coff < $soff) {
  395. $coff = $off;
  396. $coff_set = 1;
  397. #warn "CSB: mark coff<$coff>\n";
  398. }
  399. }
  400. if (($type eq '' || $type eq '{') && $c eq '{') {
  401. $level++;
  402. $type = '{';
  403. }
  404. if ($type eq '{' && $c eq '}') {
  405. $level--;
  406. $type = ($level != 0)? '{' : '';
  407. if ($level == 0) {
  408. last;
  409. }
  410. }
  411. $off++;
  412. }
  413. # We are truly at the end, so shuffle to the next line.
  414. if ($off == $len) {
  415. $loff = $len + 1;
  416. $line++;
  417. $remain--;
  418. }
  419. my $statement = substr($blk, $soff, $off - $soff + 1);
  420. my $condition = substr($blk, $soff, $coff - $soff + 1);
  421. #warn "STATEMENT<$statement>\n";
  422. #warn "CONDITION<$condition>\n";
  423. #print "coff<$coff> soff<$off> loff<$loff>\n";
  424. return ($statement, $condition,
  425. $line, $remain + 1, $off - $loff + 1, $level);
  426. }
  427. sub statement_lines {
  428. my ($stmt) = @_;
  429. # Strip the diff line prefixes and rip blank lines at start and end.
  430. $stmt =~ s/(^|\n)./$1/g;
  431. $stmt =~ s/^\s*//;
  432. $stmt =~ s/\s*$//;
  433. my @stmt_lines = ($stmt =~ /\n/g);
  434. return $#stmt_lines + 2;
  435. }
  436. sub statement_rawlines {
  437. my ($stmt) = @_;
  438. my @stmt_lines = ($stmt =~ /\n/g);
  439. return $#stmt_lines + 2;
  440. }
  441. sub statement_block_size {
  442. my ($stmt) = @_;
  443. $stmt =~ s/(^|\n)./$1/g;
  444. $stmt =~ s/^\s*{//;
  445. $stmt =~ s/}\s*$//;
  446. $stmt =~ s/^\s*//;
  447. $stmt =~ s/\s*$//;
  448. my @stmt_lines = ($stmt =~ /\n/g);
  449. my @stmt_statements = ($stmt =~ /;/g);
  450. my $stmt_lines = $#stmt_lines + 2;
  451. my $stmt_statements = $#stmt_statements + 1;
  452. if ($stmt_lines > $stmt_statements) {
  453. return $stmt_lines;
  454. } else {
  455. return $stmt_statements;
  456. }
  457. }
  458. sub ctx_statement_full {
  459. my ($linenr, $remain, $off) = @_;
  460. my ($statement, $condition, $level);
  461. my (@chunks);
  462. # Grab the first conditional/block pair.
  463. ($statement, $condition, $linenr, $remain, $off, $level) =
  464. ctx_statement_block($linenr, $remain, $off);
  465. #print "F: c<$condition> s<$statement> remain<$remain>\n";
  466. push(@chunks, [ $condition, $statement ]);
  467. if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
  468. return ($level, $linenr, @chunks);
  469. }
  470. # Pull in the following conditional/block pairs and see if they
  471. # could continue the statement.
  472. for (;;) {
  473. ($statement, $condition, $linenr, $remain, $off, $level) =
  474. ctx_statement_block($linenr, $remain, $off);
  475. #print "C: c<$condition> s<$statement> remain<$remain>\n";
  476. last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
  477. #print "C: push\n";
  478. push(@chunks, [ $condition, $statement ]);
  479. }
  480. return ($level, $linenr, @chunks);
  481. }
  482. sub ctx_block_get {
  483. my ($linenr, $remain, $outer, $open, $close, $off) = @_;
  484. my $line;
  485. my $start = $linenr - 1;
  486. my $blk = '';
  487. my @o;
  488. my @c;
  489. my @res = ();
  490. my $level = 0;
  491. for ($line = $start; $remain > 0; $line++) {
  492. next if ($rawlines[$line] =~ /^-/);
  493. $remain--;
  494. $blk .= $rawlines[$line];
  495. foreach my $c (split(//, $rawlines[$line])) {
  496. ##print "C<$c>L<$level><$open$close>O<$off>\n";
  497. if ($off > 0) {
  498. $off--;
  499. next;
  500. }
  501. if ($c eq $close && $level > 0) {
  502. $level--;
  503. last if ($level == 0);
  504. } elsif ($c eq $open) {
  505. $level++;
  506. }
  507. }
  508. if (!$outer || $level <= 1) {
  509. push(@res, $rawlines[$line]);
  510. }
  511. last if ($level == 0);
  512. }
  513. return ($level, @res);
  514. }
  515. sub ctx_block_outer {
  516. my ($linenr, $remain) = @_;
  517. my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
  518. return @r;
  519. }
  520. sub ctx_block {
  521. my ($linenr, $remain) = @_;
  522. my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
  523. return @r;
  524. }
  525. sub ctx_statement {
  526. my ($linenr, $remain, $off) = @_;
  527. my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
  528. return @r;
  529. }
  530. sub ctx_block_level {
  531. my ($linenr, $remain) = @_;
  532. return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
  533. }
  534. sub ctx_statement_level {
  535. my ($linenr, $remain, $off) = @_;
  536. return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
  537. }
  538. sub ctx_locate_comment {
  539. my ($first_line, $end_line) = @_;
  540. # Catch a comment on the end of the line itself.
  541. my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
  542. return $current_comment if (defined $current_comment);
  543. # Look through the context and try and figure out if there is a
  544. # comment.
  545. my $in_comment = 0;
  546. $current_comment = '';
  547. for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
  548. my $line = $rawlines[$linenr - 1];
  549. #warn " $line\n";
  550. if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
  551. $in_comment = 1;
  552. }
  553. if ($line =~ m@/\*@) {
  554. $in_comment = 1;
  555. }
  556. if (!$in_comment && $current_comment ne '') {
  557. $current_comment = '';
  558. }
  559. $current_comment .= $line . "\n" if ($in_comment);
  560. if ($line =~ m@\*/@) {
  561. $in_comment = 0;
  562. }
  563. }
  564. chomp($current_comment);
  565. return($current_comment);
  566. }
  567. sub ctx_has_comment {
  568. my ($first_line, $end_line) = @_;
  569. my $cmt = ctx_locate_comment($first_line, $end_line);
  570. ##print "LINE: $rawlines[$end_line - 1 ]\n";
  571. ##print "CMMT: $cmt\n";
  572. return ($cmt ne '');
  573. }
  574. sub cat_vet {
  575. my ($vet) = @_;
  576. my ($res, $coded);
  577. $res = '';
  578. while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
  579. $res .= $1;
  580. if ($2 ne '') {
  581. $coded = sprintf("^%c", unpack('C', $2) + 64);
  582. $res .= $coded;
  583. }
  584. }
  585. $res =~ s/$/\$/;
  586. return $res;
  587. }
  588. my $av_preprocessor = 0;
  589. my $av_pending;
  590. my @av_paren_type;
  591. my $av_pend_colon;
  592. sub annotate_reset {
  593. $av_preprocessor = 0;
  594. $av_pending = '_';
  595. @av_paren_type = ('E');
  596. $av_pend_colon = 'O';
  597. }
  598. sub annotate_values {
  599. my ($stream, $type) = @_;
  600. my $res;
  601. my $var = '_' x length($stream);
  602. my $cur = $stream;
  603. print "$stream\n" if ($dbg_values > 1);
  604. while (length($cur)) {
  605. @av_paren_type = ('E') if ($#av_paren_type < 0);
  606. print " <" . join('', @av_paren_type) .
  607. "> <$type> <$av_pending>" if ($dbg_values > 1);
  608. if ($cur =~ /^(\s+)/o) {
  609. print "WS($1)\n" if ($dbg_values > 1);
  610. if ($1 =~ /\n/ && $av_preprocessor) {
  611. $type = pop(@av_paren_type);
  612. $av_preprocessor = 0;
  613. }
  614. } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
  615. print "DECLARE($1)\n" if ($dbg_values > 1);
  616. $type = 'T';
  617. } elsif ($cur =~ /^($Modifier)\s*/) {
  618. print "MODIFIER($1)\n" if ($dbg_values > 1);
  619. $type = 'T';
  620. } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
  621. print "DEFINE($1,$2)\n" if ($dbg_values > 1);
  622. $av_preprocessor = 1;
  623. push(@av_paren_type, $type);
  624. if ($2 ne '') {
  625. $av_pending = 'N';
  626. }
  627. $type = 'E';
  628. } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
  629. print "UNDEF($1)\n" if ($dbg_values > 1);
  630. $av_preprocessor = 1;
  631. push(@av_paren_type, $type);
  632. } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
  633. print "PRE_START($1)\n" if ($dbg_values > 1);
  634. $av_preprocessor = 1;
  635. push(@av_paren_type, $type);
  636. push(@av_paren_type, $type);
  637. $type = 'E';
  638. } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
  639. print "PRE_RESTART($1)\n" if ($dbg_values > 1);
  640. $av_preprocessor = 1;
  641. push(@av_paren_type, $av_paren_type[$#av_paren_type]);
  642. $type = 'E';
  643. } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
  644. print "PRE_END($1)\n" if ($dbg_values > 1);
  645. $av_preprocessor = 1;
  646. # Assume all arms of the conditional end as this
  647. # one does, and continue as if the #endif was not here.
  648. pop(@av_paren_type);
  649. push(@av_paren_type, $type);
  650. $type = 'E';
  651. } elsif ($cur =~ /^(\\\n)/o) {
  652. print "PRECONT($1)\n" if ($dbg_values > 1);
  653. } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
  654. print "ATTR($1)\n" if ($dbg_values > 1);
  655. $av_pending = $type;
  656. $type = 'N';
  657. } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
  658. print "SIZEOF($1)\n" if ($dbg_values > 1);
  659. if (defined $2) {
  660. $av_pending = 'V';
  661. }
  662. $type = 'N';
  663. } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
  664. print "COND($1)\n" if ($dbg_values > 1);
  665. $av_pending = 'N';
  666. $type = 'N';
  667. } elsif ($cur =~/^(case)/o) {
  668. print "CASE($1)\n" if ($dbg_values > 1);
  669. $av_pend_colon = 'C';
  670. $type = 'N';
  671. } elsif ($cur =~/^(return|else|goto)/o) {
  672. print "KEYWORD($1)\n" if ($dbg_values > 1);
  673. $type = 'N';
  674. } elsif ($cur =~ /^(\()/o) {
  675. print "PAREN('$1')\n" if ($dbg_values > 1);
  676. push(@av_paren_type, $av_pending);
  677. $av_pending = '_';
  678. $type = 'N';
  679. } elsif ($cur =~ /^(\))/o) {
  680. my $new_type = pop(@av_paren_type);
  681. if ($new_type ne '_') {
  682. $type = $new_type;
  683. print "PAREN('$1') -> $type\n"
  684. if ($dbg_values > 1);
  685. } else {
  686. print "PAREN('$1')\n" if ($dbg_values > 1);
  687. }
  688. } elsif ($cur =~ /^($Ident)\s*\(/o) {
  689. print "FUNC($1)\n" if ($dbg_values > 1);
  690. $type = 'V';
  691. $av_pending = 'V';
  692. } elsif ($cur =~ /^($Ident\s*):/) {
  693. if ($type eq 'E') {
  694. $av_pend_colon = 'L';
  695. } elsif ($type eq 'T') {
  696. $av_pend_colon = 'B';
  697. }
  698. print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
  699. $type = 'V';
  700. } elsif ($cur =~ /^($Ident|$Constant)/o) {
  701. print "IDENT($1)\n" if ($dbg_values > 1);
  702. $type = 'V';
  703. } elsif ($cur =~ /^($Assignment)/o) {
  704. print "ASSIGN($1)\n" if ($dbg_values > 1);
  705. $type = 'N';
  706. } elsif ($cur =~/^(;|{|})/) {
  707. print "END($1)\n" if ($dbg_values > 1);
  708. $type = 'E';
  709. $av_pend_colon = 'O';
  710. } elsif ($cur =~ /^(\?)/o) {
  711. print "QUESTION($1)\n" if ($dbg_values > 1);
  712. $type = 'N';
  713. } elsif ($cur =~ /^(:)/o) {
  714. print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
  715. substr($var, length($res), 1, $av_pend_colon);
  716. if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
  717. $type = 'E';
  718. } else {
  719. $type = 'N';
  720. }
  721. $av_pend_colon = 'O';
  722. } elsif ($cur =~ /^(;|\[)/o) {
  723. print "CLOSE($1)\n" if ($dbg_values > 1);
  724. $type = 'N';
  725. } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&(?!\&))/o) {
  726. my $variant;
  727. print "OPV($1)\n" if ($dbg_values > 1);
  728. if ($type eq 'V') {
  729. $variant = 'B';
  730. } else {
  731. $variant = 'U';
  732. }
  733. substr($var, length($res), 1, $variant);
  734. $type = 'N';
  735. } elsif ($cur =~ /^($Operators)/o) {
  736. print "OP($1)\n" if ($dbg_values > 1);
  737. if ($1 ne '++' && $1 ne '--') {
  738. $type = 'N';
  739. }
  740. } elsif ($cur =~ /(^.)/o) {
  741. print "C($1)\n" if ($dbg_values > 1);
  742. }
  743. if (defined $1) {
  744. $cur = substr($cur, length($1));
  745. $res .= $type x length($1);
  746. }
  747. }
  748. return ($res, $var);
  749. }
  750. sub possible {
  751. my ($possible, $line) = @_;
  752. print "CHECK<$possible> ($line)\n" if ($dbg_possible > 1);
  753. if ($possible !~ /^(?:$Modifier|$Storage|$Type|DEFINE_\S+)$/ &&
  754. $possible ne 'goto' && $possible ne 'return' &&
  755. $possible ne 'case' && $possible ne 'else' &&
  756. $possible ne 'asm' && $possible ne '__asm__' &&
  757. $possible !~ /^(typedef|struct|enum)\b/) {
  758. # Check for modifiers.
  759. $possible =~ s/\s*$Storage\s*//g;
  760. $possible =~ s/\s*$Sparse\s*//g;
  761. if ($possible =~ /^\s*$/) {
  762. } elsif ($possible =~ /\s/) {
  763. $possible =~ s/\s*$Type\s*//g;
  764. for my $modifier (split(' ', $possible)) {
  765. warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
  766. push(@modifierList, $modifier);
  767. }
  768. } else {
  769. warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
  770. push(@typeList, $possible);
  771. }
  772. build_types();
  773. }
  774. }
  775. my $prefix = '';
  776. sub report {
  777. if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
  778. return 0;
  779. }
  780. my $line = $prefix . $_[0];
  781. $line = (split('\n', $line))[0] . "\n" if ($terse);
  782. push(our @report, $line);
  783. return 1;
  784. }
  785. sub report_dump {
  786. our @report;
  787. }
  788. sub ERROR {
  789. if (report("ERROR: $_[0]\n")) {
  790. our $clean = 0;
  791. our $cnt_error++;
  792. }
  793. }
  794. sub WARN {
  795. if (report("WARNING: $_[0]\n")) {
  796. our $clean = 0;
  797. our $cnt_warn++;
  798. }
  799. }
  800. sub CHK {
  801. if ($check && report("CHECK: $_[0]\n")) {
  802. our $clean = 0;
  803. our $cnt_chk++;
  804. }
  805. }
  806. sub process {
  807. my $filename = shift;
  808. my $linenr=0;
  809. my $prevline="";
  810. my $prevrawline="";
  811. my $stashline="";
  812. my $stashrawline="";
  813. my $length;
  814. my $indent;
  815. my $previndent=0;
  816. my $stashindent=0;
  817. our $clean = 1;
  818. my $signoff = 0;
  819. my $is_patch = 0;
  820. our @report = ();
  821. our $cnt_lines = 0;
  822. our $cnt_error = 0;
  823. our $cnt_warn = 0;
  824. our $cnt_chk = 0;
  825. # Trace the real file/line as we go.
  826. my $realfile = '';
  827. my $realline = 0;
  828. my $realcnt = 0;
  829. my $here = '';
  830. my $in_comment = 0;
  831. my $comment_edge = 0;
  832. my $first_line = 0;
  833. my $prev_values = 'E';
  834. # suppression flags
  835. my %suppress_ifbraces;
  836. # Pre-scan the patch sanitizing the lines.
  837. # Pre-scan the patch looking for any __setup documentation.
  838. #
  839. my @setup_docs = ();
  840. my $setup_docs = 0;
  841. sanitise_line_reset();
  842. my $line;
  843. foreach my $rawline (@rawlines) {
  844. $linenr++;
  845. $line = $rawline;
  846. if ($rawline=~/^\+\+\+\s+(\S+)/) {
  847. $setup_docs = 0;
  848. if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
  849. $setup_docs = 1;
  850. }
  851. #next;
  852. }
  853. if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
  854. $realline=$1-1;
  855. if (defined $2) {
  856. $realcnt=$3+1;
  857. } else {
  858. $realcnt=1+1;
  859. }
  860. $in_comment = 0;
  861. # Guestimate if this is a continuing comment. Run
  862. # the context looking for a comment "edge". If this
  863. # edge is a close comment then we must be in a comment
  864. # at context start.
  865. my $edge;
  866. for (my $ln = $linenr + 1; $ln < ($linenr + $realcnt); $ln++) {
  867. next if ($line =~ /^-/);
  868. ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
  869. last if (defined $edge);
  870. }
  871. if (defined $edge && $edge eq '*/') {
  872. $in_comment = 1;
  873. }
  874. # Guestimate if this is a continuing comment. If this
  875. # is the start of a diff block and this line starts
  876. # ' *' then it is very likely a comment.
  877. if (!defined $edge &&
  878. $rawlines[$linenr] =~ m@^.\s* \*(?:\s|$)@)
  879. {
  880. $in_comment = 1;
  881. }
  882. ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
  883. sanitise_line_reset($in_comment);
  884. } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
  885. # Standardise the strings and chars within the input to
  886. # simplify matching -- only bother with positive lines.
  887. $line = sanitise_line($rawline);
  888. }
  889. push(@lines, $line);
  890. if ($realcnt > 1) {
  891. $realcnt-- if ($line =~ /^(?:\+| |$)/);
  892. } else {
  893. $realcnt = 0;
  894. }
  895. #print "==>$rawline\n";
  896. #print "-->$line\n";
  897. if ($setup_docs && $line =~ /^\+/) {
  898. push(@setup_docs, $line);
  899. }
  900. }
  901. $prefix = '';
  902. $realcnt = 0;
  903. $linenr = 0;
  904. foreach my $line (@lines) {
  905. $linenr++;
  906. my $rawline = $rawlines[$linenr - 1];
  907. #extract the line range in the file after the patch is applied
  908. if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
  909. $is_patch = 1;
  910. $first_line = $linenr + 1;
  911. $realline=$1-1;
  912. if (defined $2) {
  913. $realcnt=$3+1;
  914. } else {
  915. $realcnt=1+1;
  916. }
  917. annotate_reset();
  918. $prev_values = 'E';
  919. %suppress_ifbraces = ();
  920. next;
  921. # track the line number as we move through the hunk, note that
  922. # new versions of GNU diff omit the leading space on completely
  923. # blank context lines so we need to count that too.
  924. } elsif ($line =~ /^( |\+|$)/) {
  925. $realline++;
  926. $realcnt-- if ($realcnt != 0);
  927. # Measure the line length and indent.
  928. ($length, $indent) = line_stats($rawline);
  929. # Track the previous line.
  930. ($prevline, $stashline) = ($stashline, $line);
  931. ($previndent, $stashindent) = ($stashindent, $indent);
  932. ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
  933. #warn "line<$line>\n";
  934. } elsif ($realcnt == 1) {
  935. $realcnt--;
  936. }
  937. #make up the handle for any error we report on this line
  938. $prefix = "$filename:$realline: " if ($emacs && $file);
  939. $prefix = "$filename:$linenr: " if ($emacs && !$file);
  940. $here = "#$linenr: " if (!$file);
  941. $here = "#$realline: " if ($file);
  942. # extract the filename as it passes
  943. if ($line=~/^\+\+\+\s+(\S+)/) {
  944. $realfile = $1;
  945. $realfile =~ s@^[^/]*/@@;
  946. if ($realfile =~ m@include/asm/@) {
  947. ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
  948. }
  949. next;
  950. }
  951. $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
  952. my $hereline = "$here\n$rawline\n";
  953. my $herecurr = "$here\n$rawline\n";
  954. my $hereprev = "$here\n$prevrawline\n$rawline\n";
  955. $cnt_lines++ if ($realcnt != 0);
  956. #check the patch for a signoff:
  957. if ($line =~ /^\s*signed-off-by:/i) {
  958. # This is a signoff, if ugly, so do not double report.
  959. $signoff++;
  960. if (!($line =~ /^\s*Signed-off-by:/)) {
  961. WARN("Signed-off-by: is the preferred form\n" .
  962. $herecurr);
  963. }
  964. if ($line =~ /^\s*signed-off-by:\S/i) {
  965. WARN("space required after Signed-off-by:\n" .
  966. $herecurr);
  967. }
  968. }
  969. # Check for wrappage within a valid hunk of the file
  970. if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
  971. ERROR("patch seems to be corrupt (line wrapped?)\n" .
  972. $herecurr) if (!$emitted_corrupt++);
  973. }
  974. # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
  975. if (($realfile =~ /^$/ || $line =~ /^\+/) &&
  976. $rawline !~ m/^$UTF8*$/) {
  977. my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
  978. my $blank = copy_spacing($rawline);
  979. my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
  980. my $hereptr = "$hereline$ptr\n";
  981. ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
  982. }
  983. #ignore lines being removed
  984. if ($line=~/^-/) {next;}
  985. # check we are in a valid source file if not then ignore this hunk
  986. next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
  987. #trailing whitespace
  988. if ($line =~ /^\+.*\015/) {
  989. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  990. ERROR("DOS line endings\n" . $herevet);
  991. } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
  992. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  993. ERROR("trailing whitespace\n" . $herevet);
  994. }
  995. #80 column limit
  996. if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
  997. $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
  998. $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
  999. $length > 80)
  1000. {
  1001. WARN("line over 80 characters\n" . $herecurr);
  1002. }
  1003. # check for adding lines without a newline.
  1004. if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
  1005. WARN("adding a line without newline at end of file\n" . $herecurr);
  1006. }
  1007. # check we are in a valid source file *.[hc] if not then ignore this hunk
  1008. next if ($realfile !~ /\.[hc]$/);
  1009. # at the beginning of a line any tabs must come first and anything
  1010. # more than 8 must use tabs.
  1011. if ($rawline =~ /^\+\s* \t\s*\S/ ||
  1012. $rawline =~ /^\+\s* \s*/) {
  1013. my $herevet = "$here\n" . cat_vet($rawline) . "\n";
  1014. ERROR("code indent should use tabs where possible\n" . $herevet);
  1015. }
  1016. # check for RCS/CVS revision markers
  1017. if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
  1018. WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
  1019. }
  1020. # Check for potential 'bare' types
  1021. my ($stat, $cond, $line_nr_next, $remain_next);
  1022. if ($realcnt && $line =~ /.\s*\S/) {
  1023. ($stat, $cond, $line_nr_next, $remain_next) =
  1024. ctx_statement_block($linenr, $realcnt, 0);
  1025. $stat =~ s/\n./\n /g;
  1026. $cond =~ s/\n./\n /g;
  1027. my $s = $stat;
  1028. $s =~ s/{.*$//s;
  1029. # Ignore goto labels.
  1030. if ($s =~ /$Ident:\*$/s) {
  1031. # Ignore functions being called
  1032. } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
  1033. # declarations always start with types
  1034. } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+?)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))(?:\s*$Modifier)?\s*(?:;|=|,|\()/s) {
  1035. my $type = $1;
  1036. $type =~ s/\s+/ /g;
  1037. possible($type, "A:" . $s);
  1038. # definitions in global scope can only start with types
  1039. } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/s) {
  1040. possible($1, "B:" . $s);
  1041. }
  1042. # any (foo ... *) is a pointer cast, and foo is a type
  1043. while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/sg) {
  1044. possible($1, "C:" . $s);
  1045. }
  1046. # Check for any sort of function declaration.
  1047. # int foo(something bar, other baz);
  1048. # void (*store_gdt)(x86_descr_ptr *);
  1049. if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
  1050. my ($name_len) = length($1);
  1051. my $ctx = $s;
  1052. substr($ctx, 0, $name_len + 1, '');
  1053. $ctx =~ s/\)[^\)]*$//;
  1054. for my $arg (split(/\s*,\s*/, $ctx)) {
  1055. if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
  1056. possible($1, "D:" . $s);
  1057. }
  1058. }
  1059. }
  1060. }
  1061. #
  1062. # Checks which may be anchored in the context.
  1063. #
  1064. # Check for switch () and associated case and default
  1065. # statements should be at the same indent.
  1066. if ($line=~/\bswitch\s*\(.*\)/) {
  1067. my $err = '';
  1068. my $sep = '';
  1069. my @ctx = ctx_block_outer($linenr, $realcnt);
  1070. shift(@ctx);
  1071. for my $ctx (@ctx) {
  1072. my ($clen, $cindent) = line_stats($ctx);
  1073. if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
  1074. $indent != $cindent) {
  1075. $err .= "$sep$ctx\n";
  1076. $sep = '';
  1077. } else {
  1078. $sep = "[...]\n";
  1079. }
  1080. }
  1081. if ($err ne '') {
  1082. ERROR("switch and case should be at the same indent\n$hereline$err");
  1083. }
  1084. }
  1085. if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
  1086. $line !~ /\G(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$/g) {
  1087. ERROR("trailing statements should be on next line\n" . $herecurr);
  1088. }
  1089. # if/while/etc brace do not go on next line, unless defining a do while loop,
  1090. # or if that brace on the next line is for something else
  1091. if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
  1092. my $pre_ctx = "$1$2";
  1093. my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
  1094. my $ctx_cnt = $realcnt - $#ctx - 1;
  1095. my $ctx = join("\n", @ctx);
  1096. my $ctx_ln = $linenr;
  1097. my $ctx_skip = $realcnt;
  1098. while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
  1099. defined $lines[$ctx_ln - 1] &&
  1100. $lines[$ctx_ln - 1] =~ /^-/)) {
  1101. ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
  1102. $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
  1103. $ctx_ln++;
  1104. }
  1105. #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
  1106. #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
  1107. if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
  1108. ERROR("that open brace { should be on the previous line\n" .
  1109. "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
  1110. }
  1111. if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
  1112. $ctx =~ /\)\s*\;\s*$/ &&
  1113. defined $lines[$ctx_ln - 1])
  1114. {
  1115. my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
  1116. if ($nindent > $indent) {
  1117. WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
  1118. "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
  1119. }
  1120. }
  1121. }
  1122. # Track the 'values' across context and added lines.
  1123. my $opline = $line; $opline =~ s/^./ /;
  1124. my ($curr_values, $curr_vars) =
  1125. annotate_values($opline . "\n", $prev_values);
  1126. $curr_values = $prev_values . $curr_values;
  1127. if ($dbg_values) {
  1128. my $outline = $opline; $outline =~ s/\t/ /g;
  1129. print "$linenr > .$outline\n";
  1130. print "$linenr > $curr_values\n";
  1131. print "$linenr > $curr_vars\n";
  1132. }
  1133. $prev_values = substr($curr_values, -1);
  1134. #ignore lines not being added
  1135. if ($line=~/^[^\+]/) {next;}
  1136. # TEST: allow direct testing of the type matcher.
  1137. if ($dbg_type) {
  1138. if ($line =~ /^.\s*$Declare\s*$/) {
  1139. ERROR("TEST: is type\n" . $herecurr);
  1140. } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
  1141. ERROR("TEST: is not type ($1 is)\n". $herecurr);
  1142. }
  1143. next;
  1144. }
  1145. # check for initialisation to aggregates open brace on the next line
  1146. if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
  1147. $line =~ /^.\s*{/) {
  1148. ERROR("that open brace { should be on the previous line\n" . $hereprev);
  1149. }
  1150. #
  1151. # Checks which are anchored on the added line.
  1152. #
  1153. # check for malformed paths in #include statements (uses RAW line)
  1154. if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
  1155. my $path = $1;
  1156. if ($path =~ m{//}) {
  1157. ERROR("malformed #include filename\n" .
  1158. $herecurr);
  1159. }
  1160. }
  1161. # no C99 // comments
  1162. if ($line =~ m{//}) {
  1163. ERROR("do not use C99 // comments\n" . $herecurr);
  1164. }
  1165. # Remove C99 comments.
  1166. $line =~ s@//.*@@;
  1167. $opline =~ s@//.*@@;
  1168. #EXPORT_SYMBOL should immediately follow its function closing }.
  1169. if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
  1170. ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
  1171. my $name = $1;
  1172. if (($prevline !~ /^}/) &&
  1173. ($prevline !~ /^\+}/) &&
  1174. ($prevline !~ /^ }/) &&
  1175. ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
  1176. ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
  1177. ($prevline !~ /^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(/) &&
  1178. ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
  1179. WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
  1180. }
  1181. }
  1182. # check for external initialisers.
  1183. if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
  1184. ERROR("do not initialise externals to 0 or NULL\n" .
  1185. $herecurr);
  1186. }
  1187. # check for static initialisers.
  1188. if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) {
  1189. ERROR("do not initialise statics to 0 or NULL\n" .
  1190. $herecurr);
  1191. }
  1192. # check for new typedefs, only function parameters and sparse annotations
  1193. # make sense.
  1194. if ($line =~ /\btypedef\s/ &&
  1195. $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
  1196. $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
  1197. $line !~ /\b__bitwise(?:__|)\b/) {
  1198. WARN("do not add new typedefs\n" . $herecurr);
  1199. }
  1200. # * goes on variable not on type
  1201. if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
  1202. ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
  1203. $herecurr);
  1204. } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
  1205. ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
  1206. $herecurr);
  1207. } elsif ($line =~ m{\b$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
  1208. ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
  1209. $herecurr);
  1210. } elsif ($line =~ m{\b$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
  1211. ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
  1212. $herecurr);
  1213. }
  1214. # # no BUG() or BUG_ON()
  1215. # if ($line =~ /\b(BUG|BUG_ON)\b/) {
  1216. # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
  1217. # print "$herecurr";
  1218. # $clean = 0;
  1219. # }
  1220. if ($line =~ /\bLINUX_VERSION_CODE\b/) {
  1221. WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
  1222. }
  1223. # printk should use KERN_* levels. Note that follow on printk's on the
  1224. # same line do not need a level, so we use the current block context
  1225. # to try and find and validate the current printk. In summary the current
  1226. # printk includes all preceeding printk's which have no newline on the end.
  1227. # we assume the first bad printk is the one to report.
  1228. if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
  1229. my $ok = 0;
  1230. for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
  1231. #print "CHECK<$lines[$ln - 1]\n";
  1232. # we have a preceeding printk if it ends
  1233. # with "\n" ignore it, else it is to blame
  1234. if ($lines[$ln - 1] =~ m{\bprintk\(}) {
  1235. if ($rawlines[$ln - 1] !~ m{\\n"}) {
  1236. $ok = 1;
  1237. }
  1238. last;
  1239. }
  1240. }
  1241. if ($ok == 0) {
  1242. WARN("printk() should include KERN_ facility level\n" . $herecurr);
  1243. }
  1244. }
  1245. # function brace can't be on same line, except for #defines of do while,
  1246. # or if closed on same line
  1247. if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
  1248. !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
  1249. ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
  1250. }
  1251. # open braces for enum, union and struct go on the same line.
  1252. if ($line =~ /^.\s*{/ &&
  1253. $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
  1254. ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
  1255. }
  1256. # check for spacing round square brackets; allowed:
  1257. # 1. with a type on the left -- int [] a;
  1258. # 2. at the beginning of a line for slice initialisers -- [0..10] = 5,
  1259. while ($line =~ /(.*?\s)\[/g) {
  1260. my ($where, $prefix) = ($-[1], $1);
  1261. if ($prefix !~ /$Type\s+$/ &&
  1262. ($where != 0 || $prefix !~ /^.\s+$/)) {
  1263. ERROR("space prohibited before open square bracket '['\n" . $herecurr);
  1264. }
  1265. }
  1266. # check for spaces between functions and their parentheses.
  1267. while ($line =~ /($Ident)\s+\(/g) {
  1268. my $name = $1;
  1269. my $ctx_before = substr($line, 0, $-[1]);
  1270. my $ctx = "$ctx_before$name";
  1271. # Ignore those directives where spaces _are_ permitted.
  1272. if ($name =~ /^(?:
  1273. if|for|while|switch|return|case|
  1274. volatile|__volatile__|
  1275. __attribute__|format|__extension__|
  1276. asm|__asm__)$/x)
  1277. {
  1278. # cpp #define statements have non-optional spaces, ie
  1279. # if there is a space between the name and the open
  1280. # parenthesis it is simply not a parameter group.
  1281. } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
  1282. # cpp #elif statement condition may start with a (
  1283. } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
  1284. # If this whole things ends with a type its most
  1285. # likely a typedef for a function.
  1286. } elsif ($ctx =~ /$Type$/) {
  1287. } else {
  1288. WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
  1289. }
  1290. }
  1291. # Check operator spacing.
  1292. if (!($line=~/\#\s*include/)) {
  1293. my $ops = qr{
  1294. <<=|>>=|<=|>=|==|!=|
  1295. \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
  1296. =>|->|<<|>>|<|>|=|!|~|
  1297. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
  1298. \?|:
  1299. }x;
  1300. my @elements = split(/($ops|;)/, $opline);
  1301. my $off = 0;
  1302. my $blank = copy_spacing($opline);
  1303. for (my $n = 0; $n < $#elements; $n += 2) {
  1304. $off += length($elements[$n]);
  1305. # Pick up the preceeding and succeeding characters.
  1306. my $ca = substr($opline, 0, $off);
  1307. my $cc = '';
  1308. if (length($opline) >= ($off + length($elements[$n + 1]))) {
  1309. $cc = substr($opline, $off + length($elements[$n + 1]));
  1310. }
  1311. my $cb = "$ca$;$cc";
  1312. my $a = '';
  1313. $a = 'V' if ($elements[$n] ne '');
  1314. $a = 'W' if ($elements[$n] =~ /\s$/);
  1315. $a = 'C' if ($elements[$n] =~ /$;$/);
  1316. $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
  1317. $a = 'O' if ($elements[$n] eq '');
  1318. $a = 'E' if ($ca =~ /^\s*$/);
  1319. my $op = $elements[$n + 1];
  1320. my $c = '';
  1321. if (defined $elements[$n + 2]) {
  1322. $c = 'V' if ($elements[$n + 2] ne '');
  1323. $c = 'W' if ($elements[$n + 2] =~ /^\s/);
  1324. $c = 'C' if ($elements[$n + 2] =~ /^$;/);
  1325. $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
  1326. $c = 'O' if ($elements[$n + 2] eq '');
  1327. $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
  1328. } else {
  1329. $c = 'E';
  1330. }
  1331. my $ctx = "${a}x${c}";
  1332. my $at = "(ctx:$ctx)";
  1333. my $ptr = substr($blank, 0, $off) . "^";
  1334. my $hereptr = "$hereline$ptr\n";
  1335. # Pull out the value of this operator.
  1336. my $op_type = substr($curr_values, $off + 1, 1);
  1337. # Get the full operator variant.
  1338. my $opv = $op . substr($curr_vars, $off, 1);
  1339. # Ignore operators passed as parameters.
  1340. if ($op_type ne 'V' &&
  1341. $ca =~ /\s$/ && $cc =~ /^\s*,/) {
  1342. # # Ignore comments
  1343. # } elsif ($op =~ /^$;+$/) {
  1344. # ; should have either the end of line or a space or \ after it
  1345. } elsif ($op eq ';') {
  1346. if ($ctx !~ /.x[WEBC]/ &&
  1347. $cc !~ /^\\/ && $cc !~ /^;/) {
  1348. ERROR("space required after that '$op' $at\n" . $hereptr);
  1349. }
  1350. # // is a comment
  1351. } elsif ($op eq '//') {
  1352. # No spaces for:
  1353. # ->
  1354. # : when part of a bitfield
  1355. } elsif ($op eq '->' || $opv eq ':B') {
  1356. if ($ctx =~ /Wx.|.xW/) {
  1357. ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
  1358. }
  1359. # , must have a space on the right.
  1360. } elsif ($op eq ',') {
  1361. if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
  1362. ERROR("space required after that '$op' $at\n" . $hereptr);
  1363. }
  1364. # '*' as part of a type definition -- reported already.
  1365. } elsif ($opv eq '*_') {
  1366. #warn "'*' is part of type\n";
  1367. # unary operators should have a space before and
  1368. # none after. May be left adjacent to another
  1369. # unary operator, or a cast
  1370. } elsif ($op eq '!' || $op eq '~' ||
  1371. $opv eq '*U' || $opv eq '-U' ||
  1372. $opv eq '&U') {
  1373. if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
  1374. ERROR("space required before that '$op' $at\n" . $hereptr);
  1375. }
  1376. if ($op eq '*' && $cc =~/\s*const\b/) {
  1377. # A unary '*' may be const
  1378. } elsif ($ctx =~ /.xW/) {
  1379. ERROR("space prohibited after that '$op' $at\n" . $hereptr);
  1380. }
  1381. # unary ++ and unary -- are allowed no space on one side.
  1382. } elsif ($op eq '++' or $op eq '--') {
  1383. if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
  1384. ERROR("space required one side of that '$op' $at\n" . $hereptr);
  1385. }
  1386. if ($ctx =~ /Wx[BE]/ ||
  1387. ($ctx =~ /Wx./ && $cc =~ /^;/)) {
  1388. ERROR("space prohibited before that '$op' $at\n" . $hereptr);
  1389. }
  1390. if ($ctx =~ /ExW/) {
  1391. ERROR("space prohibited after that '$op' $at\n" . $hereptr);
  1392. }
  1393. # << and >> may either have or not have spaces both sides
  1394. } elsif ($op eq '<<' or $op eq '>>' or
  1395. $op eq '&' or $op eq '^' or $op eq '|' or
  1396. $op eq '+' or $op eq '-' or
  1397. $op eq '*' or $op eq '/' or
  1398. $op eq '%')
  1399. {
  1400. if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
  1401. ERROR("need consistent spacing around '$op' $at\n" .
  1402. $hereptr);
  1403. }
  1404. # A colon needs no spaces before when it is
  1405. # terminating a case value or a label.
  1406. } elsif ($opv eq ':C' || $opv eq ':L') {
  1407. if ($ctx =~ /Wx./) {
  1408. ERROR("space prohibited before that '$op' $at\n" . $hereptr);
  1409. }
  1410. # All the others need spaces both sides.
  1411. } elsif ($ctx !~ /[EWC]x[CWE]/) {
  1412. my $ok = 0;
  1413. # Ignore email addresses <foo@bar>
  1414. if (($op eq '<' &&
  1415. $cc =~ /^\S+\@\S+>/) ||
  1416. ($op eq '>' &&
  1417. $ca =~ /<\S+\@\S+$/))
  1418. {
  1419. $ok = 1;
  1420. }
  1421. # Ignore ?:
  1422. if (($opv eq ':O' && $ca =~ /\?$/) ||
  1423. ($op eq '?' && $cc =~ /^:/)) {
  1424. $ok = 1;
  1425. }
  1426. if ($ok == 0) {
  1427. ERROR("spaces required around that '$op' $at\n" . $hereptr);
  1428. }
  1429. }
  1430. $off += length($elements[$n + 1]);
  1431. }
  1432. }
  1433. # check for multiple assignments
  1434. if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
  1435. CHK("multiple assignments should be avoided\n" . $herecurr);
  1436. }
  1437. ## # check for multiple declarations, allowing for a function declaration
  1438. ## # continuation.
  1439. ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
  1440. ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
  1441. ##
  1442. ## # Remove any bracketed sections to ensure we do not
  1443. ## # falsly report the parameters of functions.
  1444. ## my $ln = $line;
  1445. ## while ($ln =~ s/\([^\(\)]*\)//g) {
  1446. ## }
  1447. ## if ($ln =~ /,/) {
  1448. ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
  1449. ## }
  1450. ## }
  1451. #need space before brace following if, while, etc
  1452. if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
  1453. $line =~ /do{/) {
  1454. ERROR("space required before the open brace '{'\n" . $herecurr);
  1455. }
  1456. # closing brace should have a space following it when it has anything
  1457. # on the line
  1458. if ($line =~ /}(?!(?:,|;|\)))\S/) {
  1459. ERROR("space required after that close brace '}'\n" . $herecurr);
  1460. }
  1461. # check spacing on square brackets
  1462. if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
  1463. ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
  1464. }
  1465. if ($line =~ /\s\]/) {
  1466. ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
  1467. }
  1468. # check spacing on parentheses
  1469. if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
  1470. $line !~ /for\s*\(\s+;/) {
  1471. ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
  1472. }
  1473. if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
  1474. $line !~ /for\s*\(.*;\s+\)/ &&
  1475. $line !~ /:\s+\)/) {
  1476. ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
  1477. }
  1478. #goto labels aren't indented, allow a single space however
  1479. if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
  1480. !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
  1481. WARN("labels should not be indented\n" . $herecurr);
  1482. }
  1483. # Return is not a function.
  1484. if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
  1485. my $spacing = $1;
  1486. my $value = $2;
  1487. # Flatten any parentheses and braces
  1488. $value =~ s/\)\(/\) \(/g;
  1489. while ($value =~ s/\([^\(\)]*\)/1/) {
  1490. }
  1491. if ($value =~ /^(?:$Ident|-?$Constant)$/) {
  1492. ERROR("return is not a function, parentheses are not required\n" . $herecurr);
  1493. } elsif ($spacing !~ /\s+/) {
  1494. ERROR("space required before the open parenthesis '('\n" . $herecurr);
  1495. }
  1496. }
  1497. # Need a space before open parenthesis after if, while etc
  1498. if ($line=~/\b(if|while|for|switch)\(/) {
  1499. ERROR("space required before the open parenthesis '('\n" . $herecurr);
  1500. }
  1501. # Check for illegal assignment in if conditional -- and check for trailing
  1502. # statements after the conditional.
  1503. if ($line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
  1504. my ($s, $c) = ($stat, $cond);
  1505. if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
  1506. ERROR("do not use assignment in if condition\n" . $herecurr);
  1507. }
  1508. # Find out what is on the end of the line after the
  1509. # conditional.
  1510. substr($s, 0, length($c), '');
  1511. $s =~ s/\n.*//g;
  1512. $s =~ s/$;//g; # Remove any comments
  1513. if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
  1514. $c !~ /}\s*while\s*/)
  1515. {
  1516. ERROR("trailing statements should be on next line\n" . $herecurr);
  1517. }
  1518. }
  1519. # Check relative indent for conditionals and blocks.
  1520. if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
  1521. my ($s, $c) = ($stat, $cond);
  1522. substr($s, 0, length($c), '');
  1523. # Make sure we remove the line prefixes as we have
  1524. # none on the first line, and are going to readd them
  1525. # where necessary.
  1526. $s =~ s/\n./\n/gs;
  1527. # We want to check the first line inside the block
  1528. # starting at the end of the conditional, so remove:
  1529. # 1) any blank line termination
  1530. # 2) any opening brace { on end of the line
  1531. # 3) any do (...) {
  1532. my $continuation = 0;
  1533. my $check = 0;
  1534. $s =~ s/^.*\bdo\b//;
  1535. $s =~ s/^\s*{//;
  1536. if ($s =~ s/^\s*\\//) {
  1537. $continuation = 1;
  1538. }
  1539. if ($s =~ s/^\s*\n//) {
  1540. $check = 1;
  1541. }
  1542. # Also ignore a loop construct at the end of a
  1543. # preprocessor statement.
  1544. if (($prevline =~ /^.\s*#\s*define\s/ ||
  1545. $prevline =~ /\\\s*$/) && $continuation == 0) {
  1546. $check = 0;
  1547. }
  1548. # Ignore the current line if its is a preprocessor
  1549. # line.
  1550. if ($s =~ /^\s*#\s*/) {
  1551. $check = 0;
  1552. }
  1553. my (undef, $sindent) = line_stats("+" . $s);
  1554. ##print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s>\n";
  1555. if ($check && (($sindent % 8) != 0 ||
  1556. ($sindent <= $indent && $s ne ''))) {
  1557. WARN("suspect code indent for conditional statements\n" . $herecurr);
  1558. }
  1559. }
  1560. # Check for bitwise tests written as boolean
  1561. if ($line =~ /
  1562. (?:
  1563. (?:\[|\(|\&\&|\|\|)
  1564. \s*0[xX][0-9]+\s*
  1565. (?:\&\&|\|\|)
  1566. |
  1567. (?:\&\&|\|\|)
  1568. \s*0[xX][0-9]+\s*
  1569. (?:\&\&|\|\||\)|\])
  1570. )/x)
  1571. {
  1572. WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
  1573. }
  1574. # if and else should not have general statements after it
  1575. if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
  1576. my $s = $1;
  1577. $s =~ s/$;//g; # Remove any comments
  1578. if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
  1579. ERROR("trailing statements should be on next line\n" . $herecurr);
  1580. }
  1581. }
  1582. # Check for }<nl>else {, these must be at the same
  1583. # indent level to be relevant to each other.
  1584. if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
  1585. $previndent == $indent) {
  1586. ERROR("else should follow close brace '}'\n" . $hereprev);
  1587. }
  1588. if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
  1589. $previndent == $indent) {
  1590. my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
  1591. # Find out what is on the end of the line after the
  1592. # conditional.
  1593. substr($s, 0, length($c), '');
  1594. $s =~ s/\n.*//g;
  1595. if ($s =~ /^\s*;/) {
  1596. ERROR("while should follow close brace '}'\n" . $hereprev);
  1597. }
  1598. }
  1599. #studly caps, commented out until figure out how to distinguish between use of existing and adding new
  1600. # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
  1601. # print "No studly caps, use _\n";
  1602. # print "$herecurr";
  1603. # $clean = 0;
  1604. # }
  1605. #no spaces allowed after \ in define
  1606. if ($line=~/\#\s*define.*\\\s$/) {
  1607. WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
  1608. }
  1609. #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
  1610. if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
  1611. my $checkfile = "include/linux/$1.h";
  1612. if (-f "$root/$checkfile" && $realfile ne $checkfile &&
  1613. $1 ne 'irq')
  1614. {
  1615. WARN("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
  1616. $herecurr);
  1617. }
  1618. }
  1619. # multi-statement macros should be enclosed in a do while loop, grab the
  1620. # first statement and ensure its the whole macro if its not enclosed
  1621. # in a known good container
  1622. if ($realfile !~ m@/vmlinux.lds.h$@ &&
  1623. $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
  1624. my $ln = $linenr;
  1625. my $cnt = $realcnt;
  1626. my ($off, $dstat, $dcond, $rest);
  1627. my $ctx = '';
  1628. my $args = defined($1);
  1629. # Find the end of the macro and limit our statement
  1630. # search to that.
  1631. while ($cnt > 0 && defined $lines[$ln - 1] &&
  1632. $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
  1633. {
  1634. $ctx .= $rawlines[$ln - 1] . "\n";
  1635. $cnt-- if ($lines[$ln - 1] !~ /^-/);
  1636. $ln++;
  1637. }
  1638. $ctx .= $rawlines[$ln - 1];
  1639. ($dstat, $dcond, $ln, $cnt, $off) =
  1640. ctx_statement_block($linenr, $ln - $linenr + 1, 0);
  1641. #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
  1642. #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
  1643. # Extract the remainder of the define (if any) and
  1644. # rip off surrounding spaces, and trailing \'s.
  1645. $rest = '';
  1646. while ($off != 0 || ($cnt > 0 && $rest =~ /(?:^|\\)\s*$/)) {
  1647. #print "ADDING $off <" . substr($lines[$ln - 1], $off) . ">\n";
  1648. if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
  1649. $rest .= substr($lines[$ln - 1], $off) . "\n";
  1650. $cnt--;
  1651. }
  1652. $ln++;
  1653. $off = 0;
  1654. }
  1655. $rest =~ s/\\\n.//g;
  1656. $rest =~ s/^\s*//s;
  1657. $rest =~ s/\s*$//s;
  1658. # Clean up the original statement.
  1659. if ($args) {
  1660. substr($dstat, 0, length($dcond), '');
  1661. } else {
  1662. $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
  1663. }
  1664. $dstat =~ s/$;//g;
  1665. $dstat =~ s/\\\n.//g;
  1666. $dstat =~ s/^\s*//s;
  1667. $dstat =~ s/\s*$//s;
  1668. # Flatten any parentheses and braces
  1669. while ($dstat =~ s/\([^\(\)]*\)/1/) {
  1670. }
  1671. while ($dstat =~ s/\{[^\{\}]*\}/1/) {
  1672. }
  1673. my $exceptions = qr{
  1674. $Declare|
  1675. module_param_named|
  1676. MODULE_PARAM_DESC|
  1677. DECLARE_PER_CPU|
  1678. DEFINE_PER_CPU|
  1679. __typeof__\(
  1680. }x;
  1681. #print "REST<$rest>\n";
  1682. if ($rest ne '') {
  1683. if ($rest !~ /while\s*\(/ &&
  1684. $dstat !~ /$exceptions/)
  1685. {
  1686. ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
  1687. }
  1688. } elsif ($ctx !~ /;/) {
  1689. if ($dstat ne '' &&
  1690. $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
  1691. $dstat !~ /$exceptions/ &&
  1692. $dstat =~ /$Operators/)
  1693. {
  1694. ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
  1695. }
  1696. }
  1697. }
  1698. # check for redundant bracing round if etc
  1699. if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
  1700. my ($level, $endln, @chunks) =
  1701. ctx_statement_full($linenr, $realcnt, 1);
  1702. #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
  1703. #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
  1704. if ($#chunks > 0 && $level == 0) {
  1705. my $allowed = 0;
  1706. my $seen = 0;
  1707. my $herectx = $here . "\n";
  1708. my $ln = $linenr - 1;
  1709. for my $chunk (@chunks) {
  1710. my ($cond, $block) = @{$chunk};
  1711. # If the condition carries leading newlines, then count those as offsets.
  1712. my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
  1713. my $offset = statement_rawlines($whitespace) - 1;
  1714. #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
  1715. # We have looked at and allowed this specific line.
  1716. $suppress_ifbraces{$ln + $offset} = 1;
  1717. $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
  1718. $ln += statement_rawlines($block) - 1;
  1719. substr($block, 0, length($cond), '');
  1720. $seen++ if ($block =~ /^\s*{/);
  1721. #print "cond<$cond> block<$block> allowed<$allowed>\n";
  1722. if (statement_lines($cond) > 1) {
  1723. #print "APW: ALLOWED: cond<$cond>\n";
  1724. $allowed = 1;
  1725. }
  1726. if ($block =~/\b(?:if|for|while)\b/) {
  1727. #print "APW: ALLOWED: block<$block>\n";
  1728. $allowed = 1;
  1729. }
  1730. if (statement_block_size($block) > 1) {
  1731. #print "APW: ALLOWED: lines block<$block>\n";
  1732. $allowed = 1;
  1733. }
  1734. }
  1735. if ($seen && !$allowed) {
  1736. WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
  1737. }
  1738. }
  1739. }
  1740. if (!defined $suppress_ifbraces{$linenr - 1} &&
  1741. $line =~ /\b(if|while|for|else)\b/) {
  1742. my $allowed = 0;
  1743. # Check the pre-context.
  1744. if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
  1745. #print "APW: ALLOWED: pre<$1>\n";
  1746. $allowed = 1;
  1747. }
  1748. my ($level, $endln, @chunks) =
  1749. ctx_statement_full($linenr, $realcnt, $-[0]);
  1750. # Check the condition.
  1751. my ($cond, $block) = @{$chunks[0]};
  1752. #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
  1753. if (defined $cond) {
  1754. substr($block, 0, length($cond), '');
  1755. }
  1756. if (statement_lines($cond) > 1) {
  1757. #print "APW: ALLOWED: cond<$cond>\n";
  1758. $allowed = 1;
  1759. }
  1760. if ($block =~/\b(?:if|for|while)\b/) {
  1761. #print "APW: ALLOWED: block<$block>\n";
  1762. $allowed = 1;
  1763. }
  1764. if (statement_block_size($block) > 1) {
  1765. #print "APW: ALLOWED: lines block<$block>\n";
  1766. $allowed = 1;
  1767. }
  1768. # Check the post-context.
  1769. if (defined $chunks[1]) {
  1770. my ($cond, $block) = @{$chunks[1]};
  1771. if (defined $cond) {
  1772. substr($block, 0, length($cond), '');
  1773. }
  1774. if ($block =~ /^\s*\{/) {
  1775. #print "APW: ALLOWED: chunk-1 block<$block>\n";
  1776. $allowed = 1;
  1777. }
  1778. }
  1779. if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
  1780. my $herectx = $here . "\n";;
  1781. my $end = $linenr + statement_rawlines($block) - 1;
  1782. for (my $ln = $linenr - 1; $ln < $end; $ln++) {
  1783. $herectx .= $rawlines[$ln] . "\n";;
  1784. }
  1785. WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
  1786. }
  1787. }
  1788. # don't include deprecated include files (uses RAW line)
  1789. for my $inc (@dep_includes) {
  1790. if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
  1791. ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  1792. }
  1793. }
  1794. # don't use deprecated functions
  1795. for my $func (@dep_functions) {
  1796. if ($line =~ /\b$func\b/) {
  1797. ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  1798. }
  1799. }
  1800. # no volatiles please
  1801. my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
  1802. if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
  1803. WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
  1804. }
  1805. # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
  1806. if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
  1807. ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
  1808. }
  1809. # warn about #if 0
  1810. if ($line =~ /^.\s*\#\s*if\s+0\b/) {
  1811. CHK("if this code is redundant consider removing it\n" .
  1812. $herecurr);
  1813. }
  1814. # check for needless kfree() checks
  1815. if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
  1816. my $expr = $1;
  1817. if ($line =~ /\bkfree\(\Q$expr\E\);/) {
  1818. WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
  1819. }
  1820. }
  1821. # check for needless usb_free_urb() checks
  1822. if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
  1823. my $expr = $1;
  1824. if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
  1825. WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
  1826. }
  1827. }
  1828. # warn about #ifdefs in C files
  1829. # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
  1830. # print "#ifdef in C files should be avoided\n";
  1831. # print "$herecurr";
  1832. # $clean = 0;
  1833. # }
  1834. # warn about spacing in #ifdefs
  1835. if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
  1836. ERROR("exactly one space required after that #$1\n" . $herecurr);
  1837. }
  1838. # check for spinlock_t definitions without a comment.
  1839. if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
  1840. $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
  1841. my $which = $1;
  1842. if (!ctx_has_comment($first_line, $linenr)) {
  1843. CHK("$1 definition without comment\n" . $herecurr);
  1844. }
  1845. }
  1846. # check for memory barriers without a comment.
  1847. if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
  1848. if (!ctx_has_comment($first_line, $linenr)) {
  1849. CHK("memory barrier without comment\n" . $herecurr);
  1850. }
  1851. }
  1852. # check of hardware specific defines
  1853. if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
  1854. CHK("architecture specific defines should be avoided\n" . $herecurr);
  1855. }
  1856. # check the location of the inline attribute, that it is between
  1857. # storage class and type.
  1858. if ($line =~ /\b$Type\s+$Inline\b/ ||
  1859. $line =~ /\b$Inline\s+$Storage\b/) {
  1860. ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
  1861. }
  1862. # Check for __inline__ and __inline, prefer inline
  1863. if ($line =~ /\b(__inline__|__inline)\b/) {
  1864. WARN("plain inline is preferred over $1\n" . $herecurr);
  1865. }
  1866. # check for new externs in .c files.
  1867. if ($realfile =~ /\.c$/ && defined $stat &&
  1868. $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
  1869. {
  1870. my $function_name = $1;
  1871. my $paren_space = $2;
  1872. my $s = $stat;
  1873. if (defined $cond) {
  1874. substr($s, 0, length($cond), '');
  1875. }
  1876. if ($s =~ /^\s*;/ &&
  1877. $function_name ne 'uninitialized_var')
  1878. {
  1879. WARN("externs should be avoided in .c files\n" . $herecurr);
  1880. }
  1881. if ($paren_space =~ /\n/) {
  1882. WARN("arguments for function declarations should follow identifier\n" . $herecurr);
  1883. }
  1884. } elsif ($realfile =~ /\.c$/ && defined $stat &&
  1885. $stat =~ /^.\s*extern\s+/)
  1886. {
  1887. WARN("externs should be avoided in .c files\n" . $herecurr);
  1888. }
  1889. # checks for new __setup's
  1890. if ($rawline =~ /\b__setup\("([^"]*)"/) {
  1891. my $name = $1;
  1892. if (!grep(/$name/, @setup_docs)) {
  1893. CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
  1894. }
  1895. }
  1896. # check for pointless casting of kmalloc return
  1897. if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
  1898. WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
  1899. }
  1900. # check for gcc specific __FUNCTION__
  1901. if ($line =~ /__FUNCTION__/) {
  1902. WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
  1903. }
  1904. # check for semaphores used as mutexes
  1905. if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
  1906. WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
  1907. }
  1908. # check for semaphores used as mutexes
  1909. if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
  1910. WARN("consider using a completion\n" . $herecurr);
  1911. }
  1912. # recommend strict_strto* over simple_strto*
  1913. if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
  1914. WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
  1915. }
  1916. # check for __initcall(), use device_initcall() explicitly please
  1917. if ($line =~ /^.\s*__initcall\s*\(/) {
  1918. WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
  1919. }
  1920. # use of NR_CPUS is usually wrong
  1921. # ignore definitions of NR_CPUS and usage to define arrays as likely right
  1922. if ($line =~ /\bNR_CPUS\b/ &&
  1923. $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
  1924. $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
  1925. $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
  1926. $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
  1927. $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
  1928. {
  1929. WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
  1930. }
  1931. # check for %L{u,d,i} in strings
  1932. my $string;
  1933. while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
  1934. $string = substr($rawline, $-[1], $+[1] - $-[1]);
  1935. if ($string =~ /(?<!%)%L[udi]/) {
  1936. WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
  1937. last;
  1938. }
  1939. }
  1940. }
  1941. # If we have no input at all, then there is nothing to report on
  1942. # so just keep quiet.
  1943. if ($#rawlines == -1) {
  1944. exit(0);
  1945. }
  1946. # In mailback mode only produce a report in the negative, for
  1947. # things that appear to be patches.
  1948. if ($mailback && ($clean == 1 || !$is_patch)) {
  1949. exit(0);
  1950. }
  1951. # This is not a patch, and we are are in 'no-patch' mode so
  1952. # just keep quiet.
  1953. if (!$chk_patch && !$is_patch) {
  1954. exit(0);
  1955. }
  1956. if (!$is_patch) {
  1957. ERROR("Does not appear to be a unified-diff format patch\n");
  1958. }
  1959. if ($is_patch && $chk_signoff && $signoff == 0) {
  1960. ERROR("Missing Signed-off-by: line(s)\n");
  1961. }
  1962. print report_dump();
  1963. if ($summary && !($clean == 1 && $quiet == 1)) {
  1964. print "$filename " if ($summary_file);
  1965. print "total: $cnt_error errors, $cnt_warn warnings, " .
  1966. (($check)? "$cnt_chk checks, " : "") .
  1967. "$cnt_lines lines checked\n";
  1968. print "\n" if ($quiet == 0);
  1969. }
  1970. if ($clean == 1 && $quiet == 0) {
  1971. print "$vname has no obvious style problems and is ready for submission.\n"
  1972. }
  1973. if ($clean == 0 && $quiet == 0) {
  1974. print "$vname has style problems, please review. If any of these errors\n";
  1975. print "are false positives report them to the maintainer, see\n";
  1976. print "CHECKPATCH in MAINTAINERS.\n";
  1977. }
  1978. return $clean;
  1979. }