checkpatch.pl 72 KB

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