checkpatch.pl 67 KB

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