checkpatch.pl 62 KB

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