checkpatch.pl 60 KB

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