checkpatch.pl 68 KB

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