checkpatch.pl 53 KB

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