checkpatch.pl 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533
  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. if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
  1365. ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
  1366. $herecurr);
  1367. } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
  1368. ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
  1369. $herecurr);
  1370. } elsif ($line =~ m{\b$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
  1371. ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
  1372. $herecurr);
  1373. } elsif ($line =~ m{\b$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
  1374. ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
  1375. $herecurr);
  1376. }
  1377. # # no BUG() or BUG_ON()
  1378. # if ($line =~ /\b(BUG|BUG_ON)\b/) {
  1379. # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
  1380. # print "$herecurr";
  1381. # $clean = 0;
  1382. # }
  1383. if ($line =~ /\bLINUX_VERSION_CODE\b/) {
  1384. WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
  1385. }
  1386. # printk should use KERN_* levels. Note that follow on printk's on the
  1387. # same line do not need a level, so we use the current block context
  1388. # to try and find and validate the current printk. In summary the current
  1389. # printk includes all preceeding printk's which have no newline on the end.
  1390. # we assume the first bad printk is the one to report.
  1391. if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
  1392. my $ok = 0;
  1393. for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
  1394. #print "CHECK<$lines[$ln - 1]\n";
  1395. # we have a preceeding printk if it ends
  1396. # with "\n" ignore it, else it is to blame
  1397. if ($lines[$ln - 1] =~ m{\bprintk\(}) {
  1398. if ($rawlines[$ln - 1] !~ m{\\n"}) {
  1399. $ok = 1;
  1400. }
  1401. last;
  1402. }
  1403. }
  1404. if ($ok == 0) {
  1405. WARN("printk() should include KERN_ facility level\n" . $herecurr);
  1406. }
  1407. }
  1408. # function brace can't be on same line, except for #defines of do while,
  1409. # or if closed on same line
  1410. if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
  1411. !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
  1412. ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
  1413. }
  1414. # open braces for enum, union and struct go on the same line.
  1415. if ($line =~ /^.\s*{/ &&
  1416. $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
  1417. ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
  1418. }
  1419. # check for spacing round square brackets; allowed:
  1420. # 1. with a type on the left -- int [] a;
  1421. # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
  1422. # 3. inside a curly brace -- = { [0...10] = 5 }
  1423. while ($line =~ /(.*?\s)\[/g) {
  1424. my ($where, $prefix) = ($-[1], $1);
  1425. if ($prefix !~ /$Type\s+$/ &&
  1426. ($where != 0 || $prefix !~ /^.\s+$/) &&
  1427. $prefix !~ /{\s+$/) {
  1428. ERROR("space prohibited before open square bracket '['\n" . $herecurr);
  1429. }
  1430. }
  1431. # check for spaces between functions and their parentheses.
  1432. while ($line =~ /($Ident)\s+\(/g) {
  1433. my $name = $1;
  1434. my $ctx_before = substr($line, 0, $-[1]);
  1435. my $ctx = "$ctx_before$name";
  1436. # Ignore those directives where spaces _are_ permitted.
  1437. if ($name =~ /^(?:
  1438. if|for|while|switch|return|case|
  1439. volatile|__volatile__|
  1440. __attribute__|format|__extension__|
  1441. asm|__asm__)$/x)
  1442. {
  1443. # cpp #define statements have non-optional spaces, ie
  1444. # if there is a space between the name and the open
  1445. # parenthesis it is simply not a parameter group.
  1446. } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
  1447. # cpp #elif statement condition may start with a (
  1448. } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
  1449. # If this whole things ends with a type its most
  1450. # likely a typedef for a function.
  1451. } elsif ($ctx =~ /$Type$/) {
  1452. } else {
  1453. WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
  1454. }
  1455. }
  1456. # Check operator spacing.
  1457. if (!($line=~/\#\s*include/)) {
  1458. my $ops = qr{
  1459. <<=|>>=|<=|>=|==|!=|
  1460. \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
  1461. =>|->|<<|>>|<|>|=|!|~|
  1462. &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
  1463. \?|:
  1464. }x;
  1465. my @elements = split(/($ops|;)/, $opline);
  1466. my $off = 0;
  1467. my $blank = copy_spacing($opline);
  1468. for (my $n = 0; $n < $#elements; $n += 2) {
  1469. $off += length($elements[$n]);
  1470. # Pick up the preceeding and succeeding characters.
  1471. my $ca = substr($opline, 0, $off);
  1472. my $cc = '';
  1473. if (length($opline) >= ($off + length($elements[$n + 1]))) {
  1474. $cc = substr($opline, $off + length($elements[$n + 1]));
  1475. }
  1476. my $cb = "$ca$;$cc";
  1477. my $a = '';
  1478. $a = 'V' if ($elements[$n] ne '');
  1479. $a = 'W' if ($elements[$n] =~ /\s$/);
  1480. $a = 'C' if ($elements[$n] =~ /$;$/);
  1481. $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
  1482. $a = 'O' if ($elements[$n] eq '');
  1483. $a = 'E' if ($ca =~ /^\s*$/);
  1484. my $op = $elements[$n + 1];
  1485. my $c = '';
  1486. if (defined $elements[$n + 2]) {
  1487. $c = 'V' if ($elements[$n + 2] ne '');
  1488. $c = 'W' if ($elements[$n + 2] =~ /^\s/);
  1489. $c = 'C' if ($elements[$n + 2] =~ /^$;/);
  1490. $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
  1491. $c = 'O' if ($elements[$n + 2] eq '');
  1492. $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
  1493. } else {
  1494. $c = 'E';
  1495. }
  1496. my $ctx = "${a}x${c}";
  1497. my $at = "(ctx:$ctx)";
  1498. my $ptr = substr($blank, 0, $off) . "^";
  1499. my $hereptr = "$hereline$ptr\n";
  1500. # Pull out the value of this operator.
  1501. my $op_type = substr($curr_values, $off + 1, 1);
  1502. # Get the full operator variant.
  1503. my $opv = $op . substr($curr_vars, $off, 1);
  1504. # Ignore operators passed as parameters.
  1505. if ($op_type ne 'V' &&
  1506. $ca =~ /\s$/ && $cc =~ /^\s*,/) {
  1507. # # Ignore comments
  1508. # } elsif ($op =~ /^$;+$/) {
  1509. # ; should have either the end of line or a space or \ after it
  1510. } elsif ($op eq ';') {
  1511. if ($ctx !~ /.x[WEBC]/ &&
  1512. $cc !~ /^\\/ && $cc !~ /^;/) {
  1513. ERROR("space required after that '$op' $at\n" . $hereptr);
  1514. }
  1515. # // is a comment
  1516. } elsif ($op eq '//') {
  1517. # No spaces for:
  1518. # ->
  1519. # : when part of a bitfield
  1520. } elsif ($op eq '->' || $opv eq ':B') {
  1521. if ($ctx =~ /Wx.|.xW/) {
  1522. ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
  1523. }
  1524. # , must have a space on the right.
  1525. } elsif ($op eq ',') {
  1526. if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
  1527. ERROR("space required after that '$op' $at\n" . $hereptr);
  1528. }
  1529. # '*' as part of a type definition -- reported already.
  1530. } elsif ($opv eq '*_') {
  1531. #warn "'*' is part of type\n";
  1532. # unary operators should have a space before and
  1533. # none after. May be left adjacent to another
  1534. # unary operator, or a cast
  1535. } elsif ($op eq '!' || $op eq '~' ||
  1536. $opv eq '*U' || $opv eq '-U' ||
  1537. $opv eq '&U' || $opv eq '&&U') {
  1538. if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
  1539. ERROR("space required before that '$op' $at\n" . $hereptr);
  1540. }
  1541. if ($op eq '*' && $cc =~/\s*const\b/) {
  1542. # A unary '*' may be const
  1543. } elsif ($ctx =~ /.xW/) {
  1544. ERROR("space prohibited after that '$op' $at\n" . $hereptr);
  1545. }
  1546. # unary ++ and unary -- are allowed no space on one side.
  1547. } elsif ($op eq '++' or $op eq '--') {
  1548. if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
  1549. ERROR("space required one side of that '$op' $at\n" . $hereptr);
  1550. }
  1551. if ($ctx =~ /Wx[BE]/ ||
  1552. ($ctx =~ /Wx./ && $cc =~ /^;/)) {
  1553. ERROR("space prohibited before that '$op' $at\n" . $hereptr);
  1554. }
  1555. if ($ctx =~ /ExW/) {
  1556. ERROR("space prohibited after that '$op' $at\n" . $hereptr);
  1557. }
  1558. # << and >> may either have or not have spaces both sides
  1559. } elsif ($op eq '<<' or $op eq '>>' or
  1560. $op eq '&' or $op eq '^' or $op eq '|' or
  1561. $op eq '+' or $op eq '-' or
  1562. $op eq '*' or $op eq '/' or
  1563. $op eq '%')
  1564. {
  1565. if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
  1566. ERROR("need consistent spacing around '$op' $at\n" .
  1567. $hereptr);
  1568. }
  1569. # A colon needs no spaces before when it is
  1570. # terminating a case value or a label.
  1571. } elsif ($opv eq ':C' || $opv eq ':L') {
  1572. if ($ctx =~ /Wx./) {
  1573. ERROR("space prohibited before that '$op' $at\n" . $hereptr);
  1574. }
  1575. # All the others need spaces both sides.
  1576. } elsif ($ctx !~ /[EWC]x[CWE]/) {
  1577. my $ok = 0;
  1578. # Ignore email addresses <foo@bar>
  1579. if (($op eq '<' &&
  1580. $cc =~ /^\S+\@\S+>/) ||
  1581. ($op eq '>' &&
  1582. $ca =~ /<\S+\@\S+$/))
  1583. {
  1584. $ok = 1;
  1585. }
  1586. # Ignore ?:
  1587. if (($opv eq ':O' && $ca =~ /\?$/) ||
  1588. ($op eq '?' && $cc =~ /^:/)) {
  1589. $ok = 1;
  1590. }
  1591. if ($ok == 0) {
  1592. ERROR("spaces required around that '$op' $at\n" . $hereptr);
  1593. }
  1594. }
  1595. $off += length($elements[$n + 1]);
  1596. }
  1597. }
  1598. # check for multiple assignments
  1599. if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
  1600. CHK("multiple assignments should be avoided\n" . $herecurr);
  1601. }
  1602. ## # check for multiple declarations, allowing for a function declaration
  1603. ## # continuation.
  1604. ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
  1605. ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
  1606. ##
  1607. ## # Remove any bracketed sections to ensure we do not
  1608. ## # falsly report the parameters of functions.
  1609. ## my $ln = $line;
  1610. ## while ($ln =~ s/\([^\(\)]*\)//g) {
  1611. ## }
  1612. ## if ($ln =~ /,/) {
  1613. ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
  1614. ## }
  1615. ## }
  1616. #need space before brace following if, while, etc
  1617. if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
  1618. $line =~ /do{/) {
  1619. ERROR("space required before the open brace '{'\n" . $herecurr);
  1620. }
  1621. # closing brace should have a space following it when it has anything
  1622. # on the line
  1623. if ($line =~ /}(?!(?:,|;|\)))\S/) {
  1624. ERROR("space required after that close brace '}'\n" . $herecurr);
  1625. }
  1626. # check spacing on square brackets
  1627. if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
  1628. ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
  1629. }
  1630. if ($line =~ /\s\]/) {
  1631. ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
  1632. }
  1633. # check spacing on parentheses
  1634. if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
  1635. $line !~ /for\s*\(\s+;/) {
  1636. ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
  1637. }
  1638. if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
  1639. $line !~ /for\s*\(.*;\s+\)/ &&
  1640. $line !~ /:\s+\)/) {
  1641. ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
  1642. }
  1643. #goto labels aren't indented, allow a single space however
  1644. if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
  1645. !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
  1646. WARN("labels should not be indented\n" . $herecurr);
  1647. }
  1648. # Return is not a function.
  1649. if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
  1650. my $spacing = $1;
  1651. my $value = $2;
  1652. # Flatten any parentheses and braces
  1653. $value =~ s/\)\(/\) \(/g;
  1654. while ($value =~ s/\([^\(\)]*\)/1/) {
  1655. }
  1656. if ($value =~ /^(?:$Ident|-?$Constant)$/) {
  1657. ERROR("return is not a function, parentheses are not required\n" . $herecurr);
  1658. } elsif ($spacing !~ /\s+/) {
  1659. ERROR("space required before the open parenthesis '('\n" . $herecurr);
  1660. }
  1661. }
  1662. # Need a space before open parenthesis after if, while etc
  1663. if ($line=~/\b(if|while|for|switch)\(/) {
  1664. ERROR("space required before the open parenthesis '('\n" . $herecurr);
  1665. }
  1666. # Check for illegal assignment in if conditional -- and check for trailing
  1667. # statements after the conditional.
  1668. if ($line =~ /do\s*(?!{)/) {
  1669. my ($stat_next) = ctx_statement_block($line_nr_next,
  1670. $remain_next, $off_next);
  1671. $stat_next =~ s/\n./\n /g;
  1672. ##print "stat<$stat> stat_next<$stat_next>\n";
  1673. if ($stat_next =~ /^\s*while\b/) {
  1674. # If the statement carries leading newlines,
  1675. # then count those as offsets.
  1676. my ($whitespace) =
  1677. ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
  1678. my $offset =
  1679. statement_rawlines($whitespace) - 1;
  1680. $suppress_whiletrailers{$line_nr_next +
  1681. $offset} = 1;
  1682. }
  1683. }
  1684. if (!defined $suppress_whiletrailers{$linenr} &&
  1685. $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
  1686. my ($s, $c) = ($stat, $cond);
  1687. if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
  1688. ERROR("do not use assignment in if condition\n" . $herecurr);
  1689. }
  1690. # Find out what is on the end of the line after the
  1691. # conditional.
  1692. substr($s, 0, length($c), '');
  1693. $s =~ s/\n.*//g;
  1694. $s =~ s/$;//g; # Remove any comments
  1695. if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
  1696. $c !~ /}\s*while\s*/)
  1697. {
  1698. # Find out how long the conditional actually is.
  1699. my @newlines = ($c =~ /\n/gs);
  1700. my $cond_lines = 1 + $#newlines;
  1701. my $stat_real = raw_line($linenr, $cond_lines);
  1702. if (defined($stat_real) && $cond_lines > 1) {
  1703. $stat_real = "[...]\n$stat_real";
  1704. }
  1705. ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
  1706. }
  1707. }
  1708. # Check for bitwise tests written as boolean
  1709. if ($line =~ /
  1710. (?:
  1711. (?:\[|\(|\&\&|\|\|)
  1712. \s*0[xX][0-9]+\s*
  1713. (?:\&\&|\|\|)
  1714. |
  1715. (?:\&\&|\|\|)
  1716. \s*0[xX][0-9]+\s*
  1717. (?:\&\&|\|\||\)|\])
  1718. )/x)
  1719. {
  1720. WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
  1721. }
  1722. # if and else should not have general statements after it
  1723. if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
  1724. my $s = $1;
  1725. $s =~ s/$;//g; # Remove any comments
  1726. if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
  1727. ERROR("trailing statements should be on next line\n" . $herecurr);
  1728. }
  1729. }
  1730. # case and default should not have general statements after them
  1731. if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
  1732. $line !~ /\G(?:
  1733. (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
  1734. \s*return\s+
  1735. )/xg)
  1736. {
  1737. ERROR("trailing statements should be on next line\n" . $herecurr);
  1738. }
  1739. # Check for }<nl>else {, these must be at the same
  1740. # indent level to be relevant to each other.
  1741. if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
  1742. $previndent == $indent) {
  1743. ERROR("else should follow close brace '}'\n" . $hereprev);
  1744. }
  1745. if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
  1746. $previndent == $indent) {
  1747. my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
  1748. # Find out what is on the end of the line after the
  1749. # conditional.
  1750. substr($s, 0, length($c), '');
  1751. $s =~ s/\n.*//g;
  1752. if ($s =~ /^\s*;/) {
  1753. ERROR("while should follow close brace '}'\n" . $hereprev);
  1754. }
  1755. }
  1756. #studly caps, commented out until figure out how to distinguish between use of existing and adding new
  1757. # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
  1758. # print "No studly caps, use _\n";
  1759. # print "$herecurr";
  1760. # $clean = 0;
  1761. # }
  1762. #no spaces allowed after \ in define
  1763. if ($line=~/\#\s*define.*\\\s$/) {
  1764. WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
  1765. }
  1766. #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
  1767. if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
  1768. my $file = "$1.h";
  1769. my $checkfile = "include/linux/$file";
  1770. if (-f "$root/$checkfile" &&
  1771. $realfile ne $checkfile &&
  1772. $1 ne 'irq')
  1773. {
  1774. if ($realfile =~ m{^arch/}) {
  1775. CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
  1776. } else {
  1777. WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
  1778. }
  1779. }
  1780. }
  1781. # multi-statement macros should be enclosed in a do while loop, grab the
  1782. # first statement and ensure its the whole macro if its not enclosed
  1783. # in a known good container
  1784. if ($realfile !~ m@/vmlinux.lds.h$@ &&
  1785. $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
  1786. my $ln = $linenr;
  1787. my $cnt = $realcnt;
  1788. my ($off, $dstat, $dcond, $rest);
  1789. my $ctx = '';
  1790. my $args = defined($1);
  1791. # Find the end of the macro and limit our statement
  1792. # search to that.
  1793. while ($cnt > 0 && defined $lines[$ln - 1] &&
  1794. $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
  1795. {
  1796. $ctx .= $rawlines[$ln - 1] . "\n";
  1797. $cnt-- if ($lines[$ln - 1] !~ /^-/);
  1798. $ln++;
  1799. }
  1800. $ctx .= $rawlines[$ln - 1];
  1801. ($dstat, $dcond, $ln, $cnt, $off) =
  1802. ctx_statement_block($linenr, $ln - $linenr + 1, 0);
  1803. #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
  1804. #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
  1805. # Extract the remainder of the define (if any) and
  1806. # rip off surrounding spaces, and trailing \'s.
  1807. $rest = '';
  1808. while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
  1809. #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
  1810. if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
  1811. $rest .= substr($lines[$ln - 1], $off) . "\n";
  1812. $cnt--;
  1813. }
  1814. $ln++;
  1815. $off = 0;
  1816. }
  1817. $rest =~ s/\\\n.//g;
  1818. $rest =~ s/^\s*//s;
  1819. $rest =~ s/\s*$//s;
  1820. # Clean up the original statement.
  1821. if ($args) {
  1822. substr($dstat, 0, length($dcond), '');
  1823. } else {
  1824. $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
  1825. }
  1826. $dstat =~ s/$;//g;
  1827. $dstat =~ s/\\\n.//g;
  1828. $dstat =~ s/^\s*//s;
  1829. $dstat =~ s/\s*$//s;
  1830. # Flatten any parentheses and braces
  1831. while ($dstat =~ s/\([^\(\)]*\)/1/ ||
  1832. $dstat =~ s/\{[^\{\}]*\}/1/ ||
  1833. $dstat =~ s/\[[^\{\}]*\]/1/)
  1834. {
  1835. }
  1836. my $exceptions = qr{
  1837. $Declare|
  1838. module_param_named|
  1839. MODULE_PARAM_DESC|
  1840. DECLARE_PER_CPU|
  1841. DEFINE_PER_CPU|
  1842. __typeof__\(|
  1843. \.$Ident\s*=\s*
  1844. }x;
  1845. #print "REST<$rest> dstat<$dstat>\n";
  1846. if ($rest ne '') {
  1847. if ($rest !~ /while\s*\(/ &&
  1848. $dstat !~ /$exceptions/)
  1849. {
  1850. ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
  1851. }
  1852. } elsif ($ctx !~ /;/) {
  1853. if ($dstat ne '' &&
  1854. $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
  1855. $dstat !~ /$exceptions/ &&
  1856. $dstat !~ /^\.$Ident\s*=/ &&
  1857. $dstat =~ /$Operators/)
  1858. {
  1859. ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
  1860. }
  1861. }
  1862. }
  1863. # check for redundant bracing round if etc
  1864. if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
  1865. my ($level, $endln, @chunks) =
  1866. ctx_statement_full($linenr, $realcnt, 1);
  1867. #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
  1868. #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
  1869. if ($#chunks > 0 && $level == 0) {
  1870. my $allowed = 0;
  1871. my $seen = 0;
  1872. my $herectx = $here . "\n";
  1873. my $ln = $linenr - 1;
  1874. for my $chunk (@chunks) {
  1875. my ($cond, $block) = @{$chunk};
  1876. # If the condition carries leading newlines, then count those as offsets.
  1877. my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
  1878. my $offset = statement_rawlines($whitespace) - 1;
  1879. #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
  1880. # We have looked at and allowed this specific line.
  1881. $suppress_ifbraces{$ln + $offset} = 1;
  1882. $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
  1883. $ln += statement_rawlines($block) - 1;
  1884. substr($block, 0, length($cond), '');
  1885. $seen++ if ($block =~ /^\s*{/);
  1886. #print "cond<$cond> block<$block> allowed<$allowed>\n";
  1887. if (statement_lines($cond) > 1) {
  1888. #print "APW: ALLOWED: cond<$cond>\n";
  1889. $allowed = 1;
  1890. }
  1891. if ($block =~/\b(?:if|for|while)\b/) {
  1892. #print "APW: ALLOWED: block<$block>\n";
  1893. $allowed = 1;
  1894. }
  1895. if (statement_block_size($block) > 1) {
  1896. #print "APW: ALLOWED: lines block<$block>\n";
  1897. $allowed = 1;
  1898. }
  1899. }
  1900. if ($seen && !$allowed) {
  1901. WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
  1902. }
  1903. }
  1904. }
  1905. if (!defined $suppress_ifbraces{$linenr - 1} &&
  1906. $line =~ /\b(if|while|for|else)\b/) {
  1907. my $allowed = 0;
  1908. # Check the pre-context.
  1909. if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
  1910. #print "APW: ALLOWED: pre<$1>\n";
  1911. $allowed = 1;
  1912. }
  1913. my ($level, $endln, @chunks) =
  1914. ctx_statement_full($linenr, $realcnt, $-[0]);
  1915. # Check the condition.
  1916. my ($cond, $block) = @{$chunks[0]};
  1917. #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
  1918. if (defined $cond) {
  1919. substr($block, 0, length($cond), '');
  1920. }
  1921. if (statement_lines($cond) > 1) {
  1922. #print "APW: ALLOWED: cond<$cond>\n";
  1923. $allowed = 1;
  1924. }
  1925. if ($block =~/\b(?:if|for|while)\b/) {
  1926. #print "APW: ALLOWED: block<$block>\n";
  1927. $allowed = 1;
  1928. }
  1929. if (statement_block_size($block) > 1) {
  1930. #print "APW: ALLOWED: lines block<$block>\n";
  1931. $allowed = 1;
  1932. }
  1933. # Check the post-context.
  1934. if (defined $chunks[1]) {
  1935. my ($cond, $block) = @{$chunks[1]};
  1936. if (defined $cond) {
  1937. substr($block, 0, length($cond), '');
  1938. }
  1939. if ($block =~ /^\s*\{/) {
  1940. #print "APW: ALLOWED: chunk-1 block<$block>\n";
  1941. $allowed = 1;
  1942. }
  1943. }
  1944. if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
  1945. my $herectx = $here . "\n";;
  1946. my $cnt = statement_rawlines($block);
  1947. for (my $n = 0; $n < $cnt; $n++) {
  1948. $herectx .= raw_line($linenr, $n) . "\n";;
  1949. }
  1950. WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
  1951. }
  1952. }
  1953. # don't include deprecated include files (uses RAW line)
  1954. for my $inc (@dep_includes) {
  1955. if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
  1956. ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  1957. }
  1958. }
  1959. # don't use deprecated functions
  1960. for my $func (@dep_functions) {
  1961. if ($line =~ /\b$func\b/) {
  1962. ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
  1963. }
  1964. }
  1965. # no volatiles please
  1966. my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
  1967. if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
  1968. WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
  1969. }
  1970. # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
  1971. if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
  1972. ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
  1973. }
  1974. # warn about #if 0
  1975. if ($line =~ /^.\s*\#\s*if\s+0\b/) {
  1976. CHK("if this code is redundant consider removing it\n" .
  1977. $herecurr);
  1978. }
  1979. # check for needless kfree() checks
  1980. if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
  1981. my $expr = $1;
  1982. if ($line =~ /\bkfree\(\Q$expr\E\);/) {
  1983. WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
  1984. }
  1985. }
  1986. # check for needless usb_free_urb() checks
  1987. if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
  1988. my $expr = $1;
  1989. if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
  1990. WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
  1991. }
  1992. }
  1993. # warn about #ifdefs in C files
  1994. # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
  1995. # print "#ifdef in C files should be avoided\n";
  1996. # print "$herecurr";
  1997. # $clean = 0;
  1998. # }
  1999. # warn about spacing in #ifdefs
  2000. if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
  2001. ERROR("exactly one space required after that #$1\n" . $herecurr);
  2002. }
  2003. # check for spinlock_t definitions without a comment.
  2004. if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
  2005. $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
  2006. my $which = $1;
  2007. if (!ctx_has_comment($first_line, $linenr)) {
  2008. CHK("$1 definition without comment\n" . $herecurr);
  2009. }
  2010. }
  2011. # check for memory barriers without a comment.
  2012. if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
  2013. if (!ctx_has_comment($first_line, $linenr)) {
  2014. CHK("memory barrier without comment\n" . $herecurr);
  2015. }
  2016. }
  2017. # check of hardware specific defines
  2018. if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
  2019. CHK("architecture specific defines should be avoided\n" . $herecurr);
  2020. }
  2021. # check the location of the inline attribute, that it is between
  2022. # storage class and type.
  2023. if ($line =~ /\b$Type\s+$Inline\b/ ||
  2024. $line =~ /\b$Inline\s+$Storage\b/) {
  2025. ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
  2026. }
  2027. # Check for __inline__ and __inline, prefer inline
  2028. if ($line =~ /\b(__inline__|__inline)\b/) {
  2029. WARN("plain inline is preferred over $1\n" . $herecurr);
  2030. }
  2031. # check for new externs in .c files.
  2032. if ($realfile =~ /\.c$/ && defined $stat &&
  2033. $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
  2034. {
  2035. my $function_name = $1;
  2036. my $paren_space = $2;
  2037. my $s = $stat;
  2038. if (defined $cond) {
  2039. substr($s, 0, length($cond), '');
  2040. }
  2041. if ($s =~ /^\s*;/ &&
  2042. $function_name ne 'uninitialized_var')
  2043. {
  2044. WARN("externs should be avoided in .c files\n" . $herecurr);
  2045. }
  2046. if ($paren_space =~ /\n/) {
  2047. WARN("arguments for function declarations should follow identifier\n" . $herecurr);
  2048. }
  2049. } elsif ($realfile =~ /\.c$/ && defined $stat &&
  2050. $stat =~ /^.\s*extern\s+/)
  2051. {
  2052. WARN("externs should be avoided in .c files\n" . $herecurr);
  2053. }
  2054. # checks for new __setup's
  2055. if ($rawline =~ /\b__setup\("([^"]*)"/) {
  2056. my $name = $1;
  2057. if (!grep(/$name/, @setup_docs)) {
  2058. CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
  2059. }
  2060. }
  2061. # check for pointless casting of kmalloc return
  2062. if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
  2063. WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
  2064. }
  2065. # check for gcc specific __FUNCTION__
  2066. if ($line =~ /__FUNCTION__/) {
  2067. WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
  2068. }
  2069. # check for semaphores used as mutexes
  2070. if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
  2071. WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
  2072. }
  2073. # check for semaphores used as mutexes
  2074. if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
  2075. WARN("consider using a completion\n" . $herecurr);
  2076. }
  2077. # recommend strict_strto* over simple_strto*
  2078. if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
  2079. WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
  2080. }
  2081. # check for __initcall(), use device_initcall() explicitly please
  2082. if ($line =~ /^.\s*__initcall\s*\(/) {
  2083. WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
  2084. }
  2085. # use of NR_CPUS is usually wrong
  2086. # ignore definitions of NR_CPUS and usage to define arrays as likely right
  2087. if ($line =~ /\bNR_CPUS\b/ &&
  2088. $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
  2089. $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
  2090. $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
  2091. $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
  2092. $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
  2093. {
  2094. WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
  2095. }
  2096. # check for %L{u,d,i} in strings
  2097. my $string;
  2098. while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
  2099. $string = substr($rawline, $-[1], $+[1] - $-[1]);
  2100. $string =~ s/%%/__/g;
  2101. if ($string =~ /(?<!%)%L[udi]/) {
  2102. WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
  2103. last;
  2104. }
  2105. }
  2106. # whine mightly about in_atomic
  2107. if ($line =~ /\bin_atomic\s*\(/) {
  2108. if ($realfile =~ m@^drivers/@) {
  2109. ERROR("do not use in_atomic in drivers\n" . $herecurr);
  2110. } else {
  2111. WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
  2112. }
  2113. }
  2114. }
  2115. # If we have no input at all, then there is nothing to report on
  2116. # so just keep quiet.
  2117. if ($#rawlines == -1) {
  2118. exit(0);
  2119. }
  2120. # In mailback mode only produce a report in the negative, for
  2121. # things that appear to be patches.
  2122. if ($mailback && ($clean == 1 || !$is_patch)) {
  2123. exit(0);
  2124. }
  2125. # This is not a patch, and we are are in 'no-patch' mode so
  2126. # just keep quiet.
  2127. if (!$chk_patch && !$is_patch) {
  2128. exit(0);
  2129. }
  2130. if (!$is_patch) {
  2131. ERROR("Does not appear to be a unified-diff format patch\n");
  2132. }
  2133. if ($is_patch && $chk_signoff && $signoff == 0) {
  2134. ERROR("Missing Signed-off-by: line(s)\n");
  2135. }
  2136. print report_dump();
  2137. if ($summary && !($clean == 1 && $quiet == 1)) {
  2138. print "$filename " if ($summary_file);
  2139. print "total: $cnt_error errors, $cnt_warn warnings, " .
  2140. (($check)? "$cnt_chk checks, " : "") .
  2141. "$cnt_lines lines checked\n";
  2142. print "\n" if ($quiet == 0);
  2143. }
  2144. if ($clean == 1 && $quiet == 0) {
  2145. print "$vname has no obvious style problems and is ready for submission.\n"
  2146. }
  2147. if ($clean == 0 && $quiet == 0) {
  2148. print "$vname has style problems, please review. If any of these errors\n";
  2149. print "are false positives report them to the maintainer, see\n";
  2150. print "CHECKPATCH in MAINTAINERS.\n";
  2151. }
  2152. return $clean;
  2153. }