checkpatch.pl 71 KB

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